examples/x400_mtrcv.c

This is an example program which transfers messages and reports out of the MTA.

00001 /*  Copyright (c) 2003-2007, Isode Limited, London, England.
00002  *  All rights reserved.
00003  *                                                                       
00004  *  Acquisition and use of this software and related materials for any      
00005  *  purpose requires a written licence agreement from Isode Limited,
00006  *  or a written licence from an organisation licenced by Isode Limited
00007  *  to grant such a licence.
00008  *
00009  */
00010 
00011 /* $RCSfile: x400_mtrcv.c,v $ $Revision: 1.30 $ $Date: 2007/09/25 14:16:14 $ $State: Exp $
00012  *
00013  * $Log: x400_mtrcv.c,v $
00014  * Revision 1.30  2007/09/25 14:16:14  paga
00015  * T070925E - #define out dl expansion history and security label code.
00016  *
00017  * Revision 1.29  2007/08/31 07:22:23  paga
00018  * T070830J - Add example security label calls to x400mt api
00019  *
00020  * Revision 1.28  2007/08/28 15:29:53  paga
00021  * T070828E - No longer delete the DL Expansion list structure explicity.
00022  *            (It's now deleted when the message is deleted).
00023  *
00024  * Revision 1.27  2007/07/31 16:25:34  paga
00025  * T070730F - Add example DL history expansion code
00026  *
00027  * Revision 1.26  2007/07/24 12:45:40  dbw
00028  * T070724D - Fix warnings
00029  *
00030  * Revision 1.25  2007/07/11 11:02:43  kurt
00031  * T070615E - %* requires int
00032  *
00033  * Revision 1.24  2007/05/15 13:11:10  dbw
00034  * T070515D - Update/Add copyright
00035  *
00036  * Revision 1.23  2007/05/09 16:22:59  ir
00037  * T070509W - Pass in reason/diag not as manifest constants into X400mtMsgGetFinish().
00038  *
00039  * Revision 1.22  2007/05/09 15:57:09  ir
00040  * T070509P - Replace deprecated call to X400mtMsgGet() with X400mtMsgGetStart()
00041  *            and X400mtMsgFinish().
00042  *            Add code under conditional comp to NDR message.
00043  *
00044  * Revision 1.21  2007/04/05 12:46:00  brs
00045  * T070405A - Fix some warnings.
00046  *
00047  * Revision 1.20  2006/09/11 09:43:30  ir
00048  * T060825A - Add switch which accepts defaults (as overridden on the command line)
00049  *            stopping the prompts so that the programs can be run unattended.
00050  *
00051  * Revision 1.19  2006/07/28 09:05:20  ir
00052  * T060728A - Add -u switch to example progs to accept cmd line args without
00053  *            prompting.
00054  *
00055  * Revision 1.18  2006/02/25 17:00:36  brs
00056  * T060224E - Change types to conform to function prototypes (between char*
00057  *            and u_char*, and int* and size_t*, primarily).
00058  *
00059  * Revision 1.17  2005/12/14 11:23:47  tc
00060  * T051214C - Get arrival time from reported-recipients.
00061  *
00062  * Revision 1.16  2005/11/03 11:55:05  tc
00063  * T051103A - Delete unnecessary Doxygen text.
00064  *
00065  * Revision 1.15  2005/10/12 10:42:57  ir
00066  * T051012A - Remove unused variables.
00067  *            Include <stdlib.h> for exit() prototype.
00068  *
00069  * Revision 1.14  2005/10/10 07:57:14  ir
00070  * T051007C - Call get_args() to parse cmdline args.
00071  *
00072  * Revision 1.13  2005/09/08 14:05:49  tc
00073  * T050908C - Add example of use of X400_S_CONTENT_FILE_PATH attribute.
00074  *            Add code to get and print out external content type.
00075  *
00076  * Revision 1.12  2005/05/25 16:23:36  dbw
00077  * T050525C - Files updated in 2004
00078  *
00079  * Revision 1.11  2004/06/30 09:31:17  ldv
00080  * T040625G - Improve the examples to use the new API functionality
00081  *
00082  * Revision 1.10  2004/05/27 12:01:42  dbw
00083  * T040527A - Use OR_ADDRESS rather than ORIGINATOR for originator address
00084  *
00085  * Revision 1.9  2004/05/06 09:26:35  tc
00086  * T040505C - Replace use of X400Message with X400msMessage.
00087  *         Call common 'do_tailoring' function to get data from x400tailor file.
00088  *         Call common 'set_defaults' routine.
00089  *            Add loop to retrieve all messages.
00090  *
00091  * Revision 1.8  2004/02/06 15:23:39  ir
00092  * T040205A - Add return values for functions not void.
00093  *
00094  * Revision 1.7  2003/12/11 10:06:14  ir
00095  * T031210A - Remove password prompt. Pick up originator using X400_S_ORIGINATOR.
00096  *            Check for expected error when reading report originator.
00097  *
00098  * Revision 1.6  2003/10/20 09:14:51  tc-local
00099  * T031017B - Rename amhs->x400 throughout.
00100  *
00101  * Revision 1.5  2003/10/16 15:32:22  ir
00102  * T031016A - Add ability to get report and display report info.
00103  *
00104  * Revision 1.4  2003/10/02 15:49:15  ir
00105  * T031002A - Enable main code to be called more than once.
00106  *
00107  * Revision 1.3  2003/09/29 16:36:31  ir
00108  * T030929C - Add use of all possible attributes.
00109  *
00110  * Revision 1.2  2003/09/11 11:01:21  ir
00111  * T030911A - Use AMHSMessage and AMHSRecipient over AMHS API.
00112  *
00113  * Revision 1.1  2003/09/04 12:30:30  ir
00114  * T030904B - Add amhs-common and initial MT support files.
00115  *
00116  *
00117  * Simple example program for transferring a message out of the MTA
00118  */
00119 
00120 #include <stdio.h>
00121 #include <stdlib.h>
00122 #include <x400_mtapi.h>
00123 #include <seclabel_api.h> /* For security labels */
00124 #include "example.h"
00125 /* to get NDR values */
00126 /*  #include "esc.h" */
00127 
00128 static int get_msg (
00129     int         argc,
00130     char      **argv
00131 ) ;
00132 static int do_msg_env(
00133     struct X400mtMessage *mp
00134 );
00135 static int do_msg_headers(
00136     struct X400mtMessage *mp
00137 );
00138 static void do_msg_content(
00139     struct X400mtMessage *mp
00140 );
00141 static int get_a_msg (
00142     struct X400mtSession *sp
00143 ) ;
00144 static int get_recips(
00145     struct      X400mtMessage *mp,
00146     int         recip_type,
00147     char        *recip_str
00148 );
00149 
00150 static int get_exempted_recips (
00151     struct      X400mtMessage *mp
00152 );
00153 
00154 static int do_rep_env(
00155     struct X400mtMessage *mp
00156 );
00157 
00158 static int do_rep_content(
00159     struct X400mtMessage *mp
00160 );
00161 
00162 static int do_rep_retcontent(
00163     struct X400mtMessage *mp
00164 );
00165 
00166 static void usage(void);
00167 
00168 static int do_dl_hist (
00169     struct X400mtMessage *mp
00170 );
00171 
00172 static int do_trace_info (
00173     struct X400mtMessage *mp
00174 );
00175 
00176 static int do_internal_trace_info (
00177     struct X400mtMessage *mp
00178 );
00179 
00180 static int do_redirection_hist (
00181     struct X400Recipient *recip
00182 );
00183 
00184 static int do_redirection_hist_env (
00185     struct X400mtMessage *msg
00186 );
00187 
00188 static int get_ALI (
00189     struct X400mtMessage *mp
00190 );
00191 
00192 static int get_hi (
00193     struct X400mtMessage *mp
00194 );
00195 
00196 static int get_mi (
00197     struct X400mtMessage *mp
00198 );
00199 
00200 static int get_dist_codes_sic(
00201     struct X400mtMessage *mp
00202 );
00203 
00204 static int get_other_recips(
00205     struct X400mtMessage *mp
00206 );
00207 
00208 static int get_acp127_ali (
00209     struct X400ACP127Resp *resp
00210 );
00211 
00212 static char *optstr = "uo:O:r:c:l:E";
00213 
00214 static int get_body_parts(
00215     struct X400mtMessage *mp
00216 );
00217 
00218 static int do_orig_and_dl (
00219     struct X400mtMessage *mp
00220 );
00221 
00222 
00223 int num_atts = 0;
00224 
00228 int main (
00229     int         argc,
00230     char      **argv
00231 ) 
00232 {
00233     int retval;
00234 
00235     if (x400_channel == NULL) {
00236         fprintf (stderr, "No x400_channel value set in x400tailor file");
00237         exit(1);
00238     }
00239 
00240     retval = get_msg(argc, argv);
00241 
00242     /*
00243     while (retval == X400_E_NOERROR || retval == X400_E_NO_MESSAGE) {
00244         retval = get_msg(argc, argv);
00245     }
00246     */
00247 
00248     retval = get_msg(argc, argv);
00249     fprintf (stderr, "Error in X400mtMsgGet: %s\n", X400mtError (retval));
00250 
00251     return(retval);
00252 }
00253 static int get_msg (
00254     int         argc,
00255     char      **argv
00256 ) 
00257 {
00258     int status;
00259     struct X400mtSession *sp;
00260     char *filepath = "/var/isode";
00261 
00262     if (get_args(argc, argv, optstr)) {
00263         usage();
00264         exit(-1);
00265     }
00266 
00267     /* open our X400 session */
00268     status = X400mtOpen (x400_channel, &sp);
00269     if ( status != X400_E_NOERROR ) {
00270         fprintf (stderr, "Error in Open: %s\n", X400mtError (status));
00271         return (status);
00272     }
00273 
00274     X400mtSetStrDefault(sp, X400_S_CONTENT_FILE_PATH, 
00275                         filepath, strlen(filepath));
00276 
00277     X400mtSetStrDefault(sp, X400_ADATP3_SEP,"\r\n",-1);
00278     
00279     get_a_msg(sp);
00280     if ( status != X400_E_NOERROR ) {
00281         /* close API session */
00282         status = X400mtClose (sp);
00283         if ( status != X400_E_NOERROR ) {
00284             printf("X400mtClose returned error %d\n", status);
00285             fprintf (stderr, "Error in X400mtClose: %s\n", 
00286                 X400mtError (status));
00287         }
00288         return status;
00289     }
00290 
00291     do {
00292         get_a_msg(sp);
00293         if ( status != X400_E_NOERROR ) {
00294             /* close API session */
00295             status = X400mtClose (sp);
00296             if ( status != X400_E_NOERROR ) {
00297                 printf("X400mtClose returned error %d\n", status);
00298                 fprintf (stderr, "Error in X400mtClose: %s\n", 
00299                     X400mtError (status));
00300             }
00301             return status;
00302         }
00303     }
00304     while (until_no_more_msgs);
00305 
00306     /* close API session */
00307     status = X400mtClose (sp);
00308     if ( status != X400_E_NOERROR ) {
00309         printf("X400mtClose returned error %d\n", status);
00310         fprintf (stderr, "Error in X400mtClose: %s\n", 
00311             X400mtError (status));
00312     }
00313     return (status);
00314 }
00315 
00316 
00317 static int get_a_msg (
00318     struct X400mtSession *sp
00319 ) 
00320 {
00321     struct X400mtMessage *mp;
00322     int type;
00323     int status;
00324     static int reported_none = 0;
00325     
00326     /* get the message */
00327     status = X400mtMsgGetStart (sp, &mp, &type);
00328     if ( status != X400_E_NOERROR ) {
00329         if ( status == X400_E_NO_MESSAGE ) {
00330             if (reported_none == 0) {
00331                 printf("No messages ...\n");
00332                 reported_none = 1;
00333             }
00334             return (X400_E_NO_MESSAGE);
00335         }
00336         printf("Error getting message (%d) %s\n", status, X400mtError(status));
00337         fprintf (stderr, "Error in X400mtMsgGet: %s\n", X400mtError (status));
00338         return (status);
00339     }
00340 
00341     /* process the message */
00342     switch ( type) {
00343         case X400_MSG_MESSAGE:
00344             printf("Message waiting\n\n");
00345             /* display message attributes */
00346             do_msg_env(mp);
00347             do_msg_headers(mp);
00348             do_msg_content(mp);
00349             break;
00350 
00351         case X400_MSG_REPORT:
00352             printf("Report waiting\n\n");
00353             /* display message attributes */
00354             do_rep_env(mp);
00355             do_rep_content(mp);
00356             do_rep_retcontent(mp);
00357             break;
00358 
00359         case X400_MSG_PROBE:
00360             /* Not handling a Probe here */
00361             printf ("Got a probe, ignoring ...\n");
00362             /* Delete internal copy */
00363             status = X400mtMsgDelete (mp);
00364             if ( status != X400_E_NOERROR ) {
00365                 printf("X400mtMsgDelete returned error %d\n", status);
00366                 fprintf (stderr, "Error in X400mtMsgDelete: %s\n", 
00367                     X400mtError (status));
00368             }
00369             return (status);
00370     }
00371 
00372 #ifdef always_dr_msg
00373 #define  DRD_UNRECOGNISED_OR                    0
00374 #define  DRR_UNABLE_TO_TRANSFER                 1
00375 
00376     /* finish transfer - NDR */
00377     status = X400mtMsgGetFinish (mp, X400_E_ADDRESS_ERROR, 
00378             DRR_UNABLE_TO_TRANSFER, DRD_UNRECOGNISED_OR, "Unable to transfer");
00379 #endif
00380     /* finish transfer - accept: no DR */
00381     status = X400mtMsgGetFinish (mp, X400_E_NOERROR, 
00382             -1, -1, "");
00383     if ( status != X400_E_NOERROR ) {
00384         printf("X400mtMsgFinish returned error %d\n", status);
00385         fprintf (stderr, "Error in X400mtMsgFinish: %s\n", 
00386             X400mtError (status));
00387     }
00388     printf("X400mtMsgFinish returned success %d\n", status);
00389 
00390     /* Delete internal copy */
00391     status = X400mtMsgDelete (mp);
00392     if ( status != X400_E_NOERROR ) {
00393         printf("X400mtMsgDelete returned error %d\n", status);
00394         fprintf (stderr, "Error in X400mtMsgDelete: %s\n", 
00395             X400mtError (status));
00396     }
00397     return status;
00398 }
00399 
00400 
00401 /*
00402  * Message functions
00403  */
00404 static int do_msg_env(
00405     struct X400mtMessage *mp
00406 )
00407 {
00408     char                        buffer[BUFSIZ];
00409     int                         status;
00410     size_t                      length;
00411     int                         int_param = 0;
00412 
00413     /* Envelope Attributes */
00414     
00415     /* string attributes */
00416     /* The ORADDRESS in the message is the (envelope) originator */
00417     status = X400mtMsgGetStrParam (mp, X400_S_OR_ADDRESS,
00418                                    buffer, sizeof buffer , &length);
00419     if ( status != X400_E_NOERROR ) {
00420         printf("error getting originator address\n");
00421         fprintf (stderr, 
00422                 "Error in X400mtMsgGetStrParam: %s\n", X400mtError (status));
00423         return (status);
00424     }
00425     printf ("Originator: %.*s\n", (int)length, buffer);
00426 
00427     /* Envelope message IDentifier */
00428     status = X400mtMsgGetStrParam (mp, X400_S_MESSAGE_IDENTIFIER,
00429                                    buffer, sizeof buffer , &length);
00430     if ( status != X400_E_NOERROR ) {
00431         printf("error getting message identifier\n");
00432         fprintf (stderr, "Error in X400mtMsgGetStrParam: %s\n", X400mtError (status));
00433         return (status);
00434     }
00435     printf ("MessageID: %.*s\n", (int)length, buffer);
00436 
00437     /* Content Identifier */
00438     status = X400mtMsgGetStrParam (mp, X400_S_CONTENT_IDENTIFIER,
00439                                    buffer, sizeof buffer , &length);
00440     if ( status == X400_E_NOERROR )
00441         printf ("Content Identifier: %.*s\n", (int)length, buffer);
00442     
00443     /* Content Correlator */
00444     status = X400mtMsgGetStrParam (mp, X400_S_CONTENT_CORRELATOR,
00445                                    buffer, sizeof buffer , &length);
00446     if ( status == X400_E_NOERROR )
00447         printf ("Content Correlator: %.*s\n", (int)length, buffer);
00448     
00449     /* Original EITs */
00450     status = X400mtMsgGetStrParam (mp, 
00451            X400_S_ORIGINAL_ENCODED_INFORMATION_TYPES,
00452            buffer, sizeof buffer , &length);
00453     if ( status == X400_E_NOERROR )
00454         printf ("Original EITs: %.*s\n", (int)length, buffer);
00455 
00456     /* Message Submission Time */
00457     status = X400mtMsgGetStrParam (mp, X400_S_MESSAGE_SUBMISSION_TIME,
00458                                    buffer, sizeof buffer , &length);
00459     if ( status == X400_E_NOERROR )
00460         printf ("Message Submission Time: %.*s\n", (int)length, buffer);
00461 
00462     /* Message Delivery Time */
00463     status = X400mtMsgGetStrParam (mp, X400_S_MESSAGE_DELIVERY_TIME,
00464                                    buffer, sizeof buffer , &length);
00465     if ( status == X400_E_NOERROR )
00466         printf ("Message Delivery Time: %.*s\n", (int)length, buffer);
00467 
00468     /* Latest Delivery Time */
00469     status = X400mtMsgGetStrParam (mp, X400_S_LATEST_DELIVERY_TIME,
00470                                    buffer, sizeof buffer , &length);
00471     if ( status == X400_E_NOERROR )
00472         printf ("Latest Delivery Time: %.*s\n", (int)length, buffer);
00473 
00474     /* Originator Return Address */
00475     status = X400mtMsgGetStrParam (mp, X400_S_ORIGINATOR_RETURN_ADDRESS,
00476                                    buffer, sizeof buffer , &length);
00477     if ( status == X400_E_NOERROR )
00478         printf ("Originator Return Address : %.*s\n", (int)length, buffer);
00479 
00480     status = X400mtMsgGetStrParam (mp, X400_S_EXTERNAL_CONTENT_TYPE, 
00481                                    buffer, sizeof buffer , &length);
00482     if ( status == X400_E_NOERROR )
00483         printf ("External Content Type : %.*s\n", (int)length, buffer);
00484 
00485     /* Integer parameters */
00486     /* Content Type */
00487     status = X400mtMsgGetIntParam (mp, X400_N_CONTENT_TYPE, &int_param);
00488     if ( status == X400_E_NOERROR )
00489         printf ("Content type : P%d\n", int_param);
00490 
00491                                    
00492     if ( status == X400_E_NOERROR )
00493         printf ("Content type : P%d\n", int_param);
00494 
00495     /* Content Length */
00496     status = X400mtMsgGetIntParam (mp, X400_N_CONTENT_LENGTH, &int_param);
00497     if ( status == X400_E_NOERROR )
00498         printf ("Content length: %d\n", int_param);
00499 
00500     /* Priority */
00501     status = X400mtMsgGetIntParam (mp, X400_N_PRIORITY, &int_param);
00502     if ( status == X400_E_NOERROR )
00503         printf ("Priority: %d\n", int_param);
00504 
00505     /* Disclosure of recips prohibited */
00506     status = X400mtMsgGetIntParam (mp, X400_N_DISCLOSURE, &int_param);
00507     if ( status == X400_E_NOERROR )
00508         printf ("Disclosure of recips prohibited %s\n", int_param == 0 ? "No": "Yes");
00509 
00510     /* Implicit conversion prohibited */
00511     status = X400mtMsgGetIntParam (mp, X400_N_IMPLICIT_CONVERSION_PROHIBITED, &int_param);
00512     if ( status == X400_E_NOERROR )
00513         printf ("Implicit conversion prohibited %s\n", int_param == 0 ? "No": "Yes");
00514 
00515     /*  Alternate recipient allowed */
00516     status = X400mtMsgGetIntParam (mp, X400_N_ALTERNATE_RECIPIENT_ALLOWED, &int_param);
00517     if ( status == X400_E_NOERROR )
00518         printf (" Alternate recipient allowed: %s\n", int_param == 0 ? "No": "Yes");
00519 
00520     /*  Content return request */
00521     status = X400mtMsgGetIntParam (mp, X400_N_CONTENT_RETURN_REQUEST, &int_param);
00522     if ( status == X400_E_NOERROR )
00523         printf (" Content return request: %s\n", int_param == 0 ? "No": "Yes");
00524 
00525     /* Recipient reassignment prohibited */
00526     status = X400mtMsgGetIntParam (mp, X400_N_RECIPIENT_REASSIGNMENT_PROHIBITED, &int_param);
00527     if ( status == X400_E_NOERROR )
00528         printf ("Recipient reassignment prohibited: %s\n", int_param == 0 ? "No": "Yes");
00529 
00530     /* Distribution List expansion prohibited */
00531     status = X400mtMsgGetIntParam (mp, X400_N_DL_EXPANSION_PROHIBITED, &int_param);
00532     if ( status == X400_E_NOERROR )
00533         printf ("Distribution List expansion prohibited: %s\n", int_param == 0 ? "No": "Yes");
00534 
00535     /* Conversion with loss prohibited */
00536     status = X400mtMsgGetIntParam (mp, X400_N_CONVERSION_WITH_LOSS_PROHIBITED, &int_param);
00537     if ( status == X400_E_NOERROR )
00538         printf ("Conversion with loss prohibited: %s\n", int_param == 0 ? "No": "Yes");
00539 
00540     /* Get the envelope recipients */
00541     status = get_recips(mp, X400_RECIP_ENVELOPE, "envelope");
00542     if ( status != X400_E_NOERROR && status != X400_E_NO_RECIP) {
00543         printf("error getting recipients\n");
00544         fprintf (stderr, "Error in getting recipient: %s\n", 
00545             X400mtError (status));
00546         return (status);
00547     }
00548    
00549     /* Get X.411 exempted recipients */
00550     status = get_recips(mp, X400_DL_EXEMPTED_RECIP, "DL Exempted recips");
00551     if ( status != X400_E_NOERROR && status != X400_E_NO_RECIP) {
00552         printf("error getting recipients\n");
00553         fprintf (stderr, "Error in getting recipient: %s\n", 
00554             X400mtError (status));
00555         return (status);
00556     }
00557     
00558     
00559 #ifdef WANT_DL_EXP_HIST  
00560     /* DL Expansion history */
00561     {
00562         
00563         /*Process the DL Expansion History object */
00564             status = do_dl_hist(mp);
00565         
00566         if ( status != X400_E_NO_VALUE ) {
00567             /* There has been an error fetching the DL expansion history*/
00568              printf("error getting DL Expansion History\n");
00569              fprintf (stderr, "Error in X400mtDLExpHistGet: %s\n", 
00570                       X400mtError (status));
00571             return status;
00572         }
00573     }
00574 #endif
00575 
00576         /* Security Label */
00577 
00578 #ifdef USE_SEC_LABEL
00579     
00580 #define XML_BUFSIZE 1024
00581  {
00582      char  xml_buffer[XML_BUFSIZE];
00583      unsigned char slab_buffer[XML_BUFSIZE];
00584      
00585      status = X400mtMsgGetStrParam(mp,
00586                                    X400_S_SECURITY_LABEL,
00587                                    (char*)slab_buffer,
00588                                    XML_BUFSIZE,
00589                                    &length);
00590     if (status == X400_E_NO_VALUE) {
00591         printf("No security label\n");
00592     } else if (status !=  X400_E_NOERROR) {
00593         fprintf(stderr,"Failed to fetch security label: %d",status);
00594         exit(1);
00595     } else {
00596         int sec_status = 0;
00597         
00598         sec_status = SecLabelInit("Example program");
00599         if (status != SECLABEL_E_NOERROR) {
00600             fprintf(stderr, "SecLabelInit returned error %d\n", status);
00601             exit(1);
00602         }
00603 
00604         status =  SecLabelPrint(slab_buffer,
00605                                 length,
00606                                 xml_buffer,
00607                                 XML_BUFSIZE);
00608         
00609         if (status != SECLABEL_E_NOERROR) {
00610             fprintf(stderr, "SecLabelParse returned error %d\n", status);
00611             exit(1);
00612         }
00613         
00614         /* You could now write out the XML file, or parse it in memory..*/
00615         printf("Got security label:%s\n",xml_buffer);
00616     }
00617 
00618     
00619  }
00620     
00621 #endif
00622     
00623 #define WANT_TRACE_INFO 1    
00624 #ifdef WANT_TRACE_INFO
00625  {
00626 
00627      status = do_trace_info(mp);
00628      if ( status == X400_E_NO_VALUE) {
00629          
00630      } else if (status == X400_E_MISSING_ATTR ) {
00631          printf("There is no Trace Info\n");
00632      } else {
00633          printf("error getting Trace Info\n");
00634          fprintf (stderr, "Error fetching trace info: %s\n", 
00635                   X400mtError (status));
00636          return status;
00637      }
00638          
00639  }  
00640 #endif
00641 
00642 #define USE_INTERNAL_TRACE_INFO 1
00643 #ifdef  USE_INTERNAL_TRACE_INFO
00644  {
00645 
00646      status = do_internal_trace_info(mp);
00647      if ( status == X400_E_NO_VALUE) {
00648          /* do nothing */
00649      } else if (status == X400_E_MISSING_ATTR ) {
00650          printf("There is no internal Trace Info\n");
00651      } else {
00652          printf("error getting Trace Info\n");
00653          fprintf (stderr, "Error fetching trace info: %s\n", 
00654                   X400mtError (status));
00655          return status;
00656      }
00657          
00658  }  
00659 #endif
00660  
00661 
00662  /* NB: Originator cert is just being treated as a binary blob */
00663     status = X400mtMsgGetStrParam (mp, X400_S_ORIG_CERT,
00664                                    buffer, sizeof buffer , &length);
00665     if ( status == X400_E_NOERROR )
00666         printf ("Originator Cert got %i bytes\n", (int)length);
00667 
00668 
00669     /* NB: Originator cert is just being treated as a binary blob */
00670     status = X400mtMsgGetStrParam (mp, X400_S_MOAC,
00671                                    buffer, sizeof buffer , &length);
00672     if ( status == X400_E_NOERROR )
00673         printf ("MOAC got %i bytes\n", (int)length);
00674     
00675     return X400_E_NOERROR;
00676 }
00677 
00678 static int do_dl_hist (
00679     struct X400mtMessage *mp
00680 )
00681 {
00682     struct X400DLExpHist *hist = NULL;
00683     char    DLORAddress[BUFSIZ];
00684     char    DLORDN[BUFSIZ];
00685     char    DLExpTime[BUFSIZ];
00686     size_t  length;
00687     int n;
00688     int status;
00689     
00690 
00691     /* Loop through the entries, pulling out the OR Address, DN, and expansion
00692      * time for each entry.
00693      * Stop looping when there is no n entry
00694      */
00695     for ( n=1; ;n++ ) {
00696 
00697         status = X400mtDLExpHistGet (mp,n,&hist);
00698         if (status != X400_E_NOERROR) {
00699             return status;
00700         }
00701         
00702         status = X400DLGetStrParam (hist, X400_S_OR_ADDRESS,
00703                                     DLORAddress, BUFSIZ , &length);
00704         if (status == X400_E_NO_VALUE) {
00705             return X400_E_NO_VALUE;
00706         }
00707         if ( status == X400_E_NOERROR ) {
00708             DLORAddress[length] = '\0';
00709             printf ("DLExpansion List entry %d OR Address:%s\n",
00710                     n,DLORAddress);
00711         } else {
00712             fprintf (stderr, "Error in :X400DLGetStrParam OR %s\n",
00713                      X400mtError (status));
00714         }
00715 
00716         status = X400DLGetStrParam (hist,X400_S_DIRECTORY_NAME,
00717                                     DLORDN, BUFSIZ , &length);
00718         if ( status == X400_E_NOERROR ) {
00719             DLORDN[length] = '\0';
00720             printf ("DLExpansion List entry %d DN :%s\n",n,DLORDN);
00721         } else {
00722             fprintf (stderr, "Error in :X400DLGetStrParam DN %s\n",
00723                      X400mtError (status));
00724         }
00725             
00726         status = X400DLGetStrParam (hist,X400_S_DLEXP_TIME,
00727                                     DLExpTime, BUFSIZ , &length);
00728         if ( status == X400_E_NOERROR ) {
00729             DLExpTime[length] = '\0';
00730             printf ("DLExpansion List entry %d Time :%s\n",n,DLExpTime);
00731         } else {
00732             fprintf (stderr, "Error in :X400DLGetStrParam Time %s\n",
00733                      X400mtError (status));
00734         }
00735             
00736     }
00737     return X400_E_NOERROR;
00738 }
00739 
00740 
00741 static int do_msg_headers(
00742     struct X400mtMessage *mp
00743 )
00744 {
00745     char                        buffer[BUFSIZ];
00746     int                         status;
00747     size_t                      length;
00748     int                         int_param;
00749 
00750     /* Message Headers */
00751     printf("\nContent:\n");
00752 
00753     /* Get the primary recipients */
00754     status = get_recips(mp, X400_RECIP_PRIMARY, "primary");
00755     if ( status != X400_E_NOERROR && status != X400_E_NO_RECIP) {
00756         printf("error getting recipients\n");
00757         fprintf (stderr, "Error in getting recipient: %s\n", 
00758             X400mtError (status));
00759         return (status);
00760     }
00761 
00762     /* Get the cc recipients */
00763     status = get_recips(mp, X400_RECIP_CC, "cc");
00764     if ( status != X400_E_NOERROR && status != X400_E_NO_RECIP) {
00765         printf("error getting recipients\n");
00766         fprintf (stderr, "Error in getting recipient: %s\n", 
00767             X400mtError (status));
00768         return (status);
00769     }
00770 
00771     /* Get the bcc recipients */
00772     status = get_recips(mp, X400_RECIP_BCC, "bcc");
00773     if ( status != X400_E_NOERROR && status != X400_E_NO_RECIP) {
00774         printf("error getting recipients\n");
00775         fprintf (stderr, "Error in getting recipient: %s\n", 
00776             X400mtError (status));
00777         return (status);
00778     }
00779 
00780     /* Get the reply recipients */
00781     status = get_recips(mp, X400_AUTH_ORIG, "auth-orig");
00782     if ( status != X400_E_NOERROR && status != X400_E_NO_RECIP) {
00783         printf("error getting recipients\n");
00784         fprintf (stderr, "Error in getting recipient: %s\n", 
00785             X400mtError (status));
00786         return (status);
00787     }
00788 
00789     /* Get the reply recipients */
00790     status = get_recips(mp, X400_RECIP_REPLY, "reply");
00791     if ( status != X400_E_NOERROR && status != X400_E_NO_RECIP) {
00792         printf("error getting recipients\n");
00793         fprintf (stderr, "Error in getting recipient: %s\n", 
00794             X400mtError (status));
00795         return (status);
00796     }
00797 
00798     /* Get the Exempted recipients */
00799     status = get_exempted_recips(mp);
00800     if ( status != X400_E_NOERROR && status != X400_E_NO_RECIP) {
00801         fprintf (stderr, "Error in getting exempted recipient: %s\n", 
00802             X400mtError (status));
00803         return (status);
00804     }
00805     
00806     
00807     /* string parameters */
00808     /* IPM IDentifier */
00809     status = X400mtMsgGetStrParam (mp, X400_S_IPM_IDENTIFIER,
00810                                    buffer, sizeof buffer , &length);
00811     if ( status == X400_E_NOERROR )
00812         printf ("IPM IDentifier: %.*s\n", (int)length, buffer);
00813 
00814     /* Subject */
00815     status = X400mtMsgGetStrParam (mp, X400_S_SUBJECT,
00816                                    buffer, sizeof buffer , &length);
00817     if ( status == X400_E_NOERROR )
00818         printf ("Subject: %.*s\n", (int)length, buffer);
00819 
00820     /* Replied-to-identifier */
00821     status = X400mtMsgGetStrParam (mp, X400_S_REPLIED_TO_IDENTIFIER,
00822                                    buffer, sizeof buffer , &length);
00823     if ( status == X400_E_NOERROR )
00824         printf ("Replied-to-identifier: %.*s\n", (int)length, buffer);
00825 
00826     /* Obsoleted IPMs */
00827     status = X400mtMsgGetStrParam (mp, X400_S_OBSOLETED_IPMS,
00828                                    buffer, sizeof buffer , &length);
00829     if ( status == X400_E_NOERROR )
00830         printf ("Obsoleted IPMs: %.*s\n", (int)length, buffer);
00831 
00832     /* Related IPMs */
00833     status = X400mtMsgGetStrParam (mp, X400_S_RELATED_IPMS,
00834                                    buffer, sizeof buffer , &length);
00835     if ( status == X400_E_NOERROR )
00836         printf ("Related IPMs: %.*s\n", (int)length, buffer);
00837 
00838     /* Expiry Time */
00839     status = X400mtMsgGetStrParam (mp, X400_S_EXPIRY_TIME,
00840                                    buffer, sizeof buffer , &length);
00841     if ( status == X400_E_NOERROR )
00842         printf ("Expiry Time: %.*s\n", (int)length, buffer);
00843 
00844     /* Reply Time */
00845     status = X400mtMsgGetStrParam (mp, X400_S_REPLY_TIME,
00846                                    buffer, sizeof buffer , &length);
00847     if ( status == X400_E_NOERROR )
00848         printf ("Reply Time: %.*s\n", (int)length, buffer);
00849 
00850     /* Authorisation Time */
00851     status = X400mtMsgGetStrParam (mp, X400_S_AUTHORIZATION_TIME,
00852                                    buffer, sizeof buffer , &length);
00853     if ( status == X400_E_NOERROR )
00854         printf ("Authorisation Time: %.*s\n", (int)length, buffer);
00855 
00856     /* Originator's Reference */
00857     status = X400mtMsgGetStrParam (mp, X400_S_ORIGINATORS_REFERENCE,
00858                                    buffer, sizeof buffer , &length);
00859     if ( status == X400_E_NOERROR )
00860         printf ("Originator's Reference: %.*s\n", (int)length, buffer);
00861 
00862     /* integer arguments */
00863     /* Importance */
00864     status = X400mtMsgGetIntParam (mp, X400_N_IMPORTANCE, &int_param);
00865     if ( status == X400_E_NOERROR )
00866         printf ("Importance: %d\n", int_param);
00867 
00868     /* Sensitivity: 1 - personal, 2 - private, 3 - company-confidential */
00869     status = X400mtMsgGetIntParam (mp, X400_N_SENSITIVITY, &int_param);
00870     if ( status == X400_E_NOERROR )
00871         printf ("Sensitivity: %d\n", int_param);
00872 
00873     /* Autoforwarded */
00874     status = X400mtMsgGetIntParam (mp, X400_N_AUTOFORWARDED, &int_param);
00875     if ( status == X400_E_NOERROR )
00876         printf ("Autoforwarded: %s\n", int_param ? "false" : "true");
00877 
00878     /*  number of attachments apart from main body part */
00879     status = X400mtMsgGetIntParam (mp, X400_N_NUM_ATTACHMENTS, &num_atts);
00880     if ( status == X400_E_NOERROR )
00881         printf ("number of attachments: %d\n", num_atts);
00882 
00883     /* Fetch P772 Extended Auth Info */
00884     status = X400mtMsgGetStrParam (mp, X400_S_EXT_AUTH_INFO,
00885                                    buffer, sizeof buffer , &length);
00886     if ( status == X400_E_NOERROR )
00887         printf ("P772 Ext Auth Info Time: %.*s\n", (int)length, buffer);
00888 
00889     /* Fetch P772 Codress value */
00890 
00891     status = X400mtMsgGetIntParam (mp, X400_N_EXT_CODRESS, &int_param);
00892     if ( status == X400_E_NOERROR )
00893         printf ("P772 Codress value: %d\n", int_param);
00894 
00895     /* Fetch P772 Message type value */
00896     status = X400mtMsgGetIntParam (mp, X400_N_EXT_MSG_TYPE, &int_param);
00897     if ( status == X400_E_NOERROR )
00898         printf ("P772 Message Type: %d\n", int_param);
00899 
00900     /* Fetch P772 Primary Precedence */
00901     status = X400mtMsgGetIntParam (mp, X400_N_EXT_PRIM_PREC, &int_param);
00902     if ( status == X400_E_NOERROR )
00903         printf ("P772 Primary Precedence: %d\n", int_param);
00904 
00905     /* Fetch P772 Copy Precedence */
00906     status = X400mtMsgGetIntParam (mp, X400_N_EXT_COPY_PREC, &int_param);
00907     if ( status == X400_E_NOERROR )
00908         printf ("P772 Copy Precedence: %d\n", int_param);
00909 
00910     /* Fetch P772 Address List Indicators*/
00911     get_ALI(mp);
00912 
00913     /* Fetch handling instructions */
00914     get_hi(mp);
00915 
00916     /* Fetch message instructions */
00917     get_mi(mp);
00918 
00919     /* Fetch distribution codes sic */
00920     get_dist_codes_sic(mp);
00921 
00922     /* Fetch Other Recipients */
00923     get_other_recips(mp);
00924 
00925     
00926     status = X400mtMsgGetStrParam (mp, X400_S_ORIG_REF,
00927                                    buffer, sizeof buffer , &length);
00928     if ( status == X400_E_NOERROR )
00929         printf ("P772 Originator Reference: %.*s\n", (int)length, buffer);
00930     
00931     status = X400mtMsgGetStrParam (mp, X400_S_ORIG_PLAD,
00932                                    buffer, sizeof buffer , &length);
00933     if ( status == X400_E_NOERROR )
00934         printf ("P772 Originator PLAD: %.*s\n", (int)length, buffer);
00935     
00936     status = X400mtMsgGetStrParam (mp, X400_S_ACP127_MSG_ID,
00937                                    buffer, sizeof buffer , &length);
00938     if ( status == X400_E_NOERROR )
00939         printf ("P772 ACP127 Msg ID: %.*s\n", (int)length, buffer);
00940 
00941     /* NB: Pilot forwarding info is just being treated as a binary blob */
00942     status = X400mtMsgGetStrParam (mp, X400_S_PILOT_FWD_INFO,
00943                                    buffer, sizeof buffer , &length);
00944     if ( status == X400_E_NOERROR )
00945         printf ("P772 Pilot Forwarding Info got %i bytes\n", (int)length);
00946     
00947    
00948     /* NB: information security label is just being treated as a binary blob */
00949     status = X400mtMsgGetStrParam (mp, X400_S_INFO_SEC_LABEL,
00950                                    buffer, sizeof buffer , &length);
00951     if ( status == X400_E_NOERROR )
00952         printf ("P772 Information Security Label got %i bytes\n", (int)length);
00953     
00954     return X400_E_NOERROR;
00955 }
00956 
00957 static void do_msg_content(
00958     struct X400mtMessage *mp
00959 )
00960 {
00961     char                        buffer[30000];
00962     int                         status;
00963     size_t                      length;
00964 
00965     memset(buffer,0,30000);
00966     
00967     status = X400mtMsgGetStrParam (mp, X400_S_CONTENT_FILENAME,
00968                                    buffer, sizeof buffer , &length);
00969 
00970     printf("Read raw content into file %s\n", buffer);
00971 
00972     /* Message Body */
00973 
00974     /* And now get message text (or it could be another type - ie not IA5) */
00975     status = X400mtMsgGetStrParam (mp, X400_T_IA5TEXT,
00976                                    buffer, sizeof buffer , &length);
00977    
00978     if ( status == X400_E_NOERROR ) {
00979         printf ("Text:\n%.*s\n", (int)length, buffer);
00980         num_atts--; /* The IA5text bodypart is infact the first "attachment"
00981                      * So there is one less to fetch */
00982     }
00983      /* Go through message bodyparts */
00984      status = get_body_parts(mp);
00985      
00986     return;
00987 }
00988 
00989 static int get_exempted_recips (
00990     struct      X400mtMessage *mp
00991 )
00992 {
00993     struct X400Recipient        *rp;
00994     int n;
00995     char                        buffer[BUFSIZ];
00996     size_t                      length;
00997     int                         status;
00998     
00999     for ( n = 1; ; n++ ) {
01000         status = X400mtRecipGet (mp,X400_EXEMPTED_ADDRESS , n, &rp);
01001         if ( status == X400_E_NO_RECIP ) 
01002             break;
01003         if ( status != X400_E_NOERROR ) {
01004             fprintf (stderr, "Error in X400mtRecipGet: %s\n", 
01005                 X400mtError (status));
01006             return (status);
01007         }
01008         status = X400mtRecipGetStrParam (rp, X400_S_IOB_OR_ADDRESS,
01009                                          buffer, sizeof buffer , &length);
01010         if ( status == X400_E_NOERROR ) {
01011             printf ("Exempted recipient OR address %d: %.*s\n",
01012                     n, (int)length, buffer);
01013         }
01014 
01015         status = X400mtRecipGetStrParam (rp, X400_S_IOB_DN_ADDRESS,
01016                                          buffer, sizeof buffer , &length);
01017         if ( status == X400_E_NOERROR ) {
01018             printf ("Exempted recipient DN address %d: %.*s\n",
01019                     n, (int)length, buffer);
01020         }
01021 
01022         status = X400mtRecipGetStrParam (rp, X400_S_IOB_FREE_FORM_NAME,
01023                                          buffer, sizeof buffer , &length);
01024         if ( status == X400_E_NOERROR ) {
01025             printf ("Exempted recipient free form name %d: %.*s\n",
01026                     n, (int)length, buffer);
01027         }
01028         
01029         status = X400mtRecipGetStrParam (rp, X400_S_IOB_TEL,
01030                                          buffer, sizeof buffer , &length);
01031         if ( status == X400_E_NOERROR ) {
01032             printf ("Exempted recipient Tel number %d: %.*s\n",
01033                     n, (int)length, buffer);
01034         }
01035         
01036         
01037     }
01038     return X400_E_NOERROR;
01039 }
01040 
01041 
01042 static int get_ALI (
01043     struct      X400mtMessage *mp
01044 )
01045 {
01046     struct X400ALI *ali  = NULL;
01047     int n;
01048     char                        buffer[BUFSIZ];
01049     size_t                      length;
01050     int                         status;
01051     int                         int_param;
01052   
01053     for ( n = 1; ; n++ ) {
01054         status = X400mtALIGet (mp,n,&ali);
01055         if ( status != X400_E_NOERROR ) 
01056             return status;
01057         
01058         status = X400ALIGetStrParam (ali,X400_S_IOB_OR_ADDRESS,
01059                                      buffer, sizeof buffer , &length);
01060         if ( status == X400_E_NOERROR ) {
01061             printf ("P772 ALI recipient OR address %d: %.*s\n",
01062                     n, (int)length, buffer);
01063         } else if ( status == X400_E_NO_VALUE) {
01064             return X400_E_NO_VALUE;
01065         }
01066             
01067 
01068         status = X400ALIGetStrParam (ali,X400_S_IOB_DN_ADDRESS,
01069                                      buffer, sizeof buffer , &length);
01070         if ( status == X400_E_NOERROR ) {
01071             printf ("P772 ALI recipient DN address %d: %.*s\n",
01072                     n, (int)length, buffer);
01073         }
01074 
01075         status = X400ALIGetStrParam (ali,X400_S_IOB_FREE_FORM_NAME,
01076                                      buffer, sizeof buffer , &length);
01077         if ( status == X400_E_NOERROR ) {
01078             printf ("P772 ALI recipient free form name %d: %.*s\n",
01079                     n, (int)length, buffer);
01080         }
01081         
01082         status = X400ALIGetStrParam (ali, X400_S_IOB_TEL,
01083                                      buffer, sizeof buffer , &length);
01084         if ( status == X400_E_NOERROR ) {
01085             printf ("P772 ALI recipient Tel number %d: %.*s\n",
01086                     n, (int)length, buffer);
01087         }
01088 
01089         status = X400ALIGetIntParam (ali,X400_N_ALI_TYPE , &int_param);
01090         if ( status == X400_E_NOERROR ) {
01091             printf ("P772 ALI Type  %d\n", int_param);
01092         }
01093 
01094         status = X400ALIGetIntParam (ali,X400_N_ALI_NOTIFICTAION_REQUEST ,
01095                                      &int_param);
01096         if ( status == X400_E_NOERROR ) {
01097             printf ("P772 ALI Notification Request  %d\n", int_param);
01098         }
01099         
01100 
01101         status = X400ALIGetIntParam (ali,X400_N_ALI_REPLY_REQUEST ,&int_param);
01102         if ( status == X400_E_NOERROR ) {
01103             printf ("P772 ALI Notification Request  %d\n", int_param);
01104         }
01105         
01106         
01107     }
01108 }
01109 
01110 
01111 static int get_hi (
01112     struct X400mtMessage *mp
01113 )
01114 {
01115     struct X400PSS *pss  = NULL;
01116     int n;
01117     char                        buffer[BUFSIZ];
01118     size_t                      length;
01119     int                         status;
01120 
01121   
01122     for ( n = 1; ; n++ ) {
01123         status = X400mtPSSGet (mp,X400_S_HANDLING_INSTRUCTIONS,n,&pss);
01124         if ( status != X400_E_NOERROR ) 
01125             return status;
01126         
01127         status = X400PSSGetStrParam (pss,buffer, sizeof buffer , &length);
01128         if ( status == X400_E_NOERROR ) {
01129             printf ("P772 Handling instruction %d: %.*s\n",
01130                     n, (int)length, buffer);
01131         } else if ( status == X400_E_NO_VALUE) {
01132             return X400_E_NO_VALUE;
01133         }
01134     }
01135 }
01136 
01137 static int get_mi (
01138     struct X400mtMessage *mp
01139 )
01140 {
01141     struct X400PSS *pss  = NULL;
01142     int n;
01143     char                        buffer[BUFSIZ];
01144     size_t                      length;
01145     int                         status;
01146 
01147   
01148     for ( n = 1; ; n++ ) {
01149         status = X400mtPSSGet (mp,X400_S_MESSAGE_INSTRUCTIONS,n,&pss);
01150         if ( status != X400_E_NOERROR ) 
01151             return status;
01152         
01153         status = X400PSSGetStrParam (pss,buffer, sizeof buffer , &length);
01154         if ( status == X400_E_NOERROR ) {
01155             printf ("P772 Message instruction %d: %.*s\n",
01156                     n, (int)length, buffer);
01157         } else if ( status == X400_E_NO_VALUE) {
01158             return X400_E_NO_VALUE;
01159         }
01160     }
01161 }
01162 
01163 static int get_dist_codes_sic(
01164     struct X400mtMessage *mp
01165 )
01166 {
01167     struct X400PSS *pss  = NULL;
01168     struct X400DistField *distfield = NULL;
01169     int n;
01170     char                        buffer[BUFSIZ];
01171     size_t                      length;
01172     int                         status;
01173 
01174    
01175     for ( n = 1; ; n++ ) {
01176         status = X400mtPSSGet (mp,X400_S_DIST_CODES_SIC,n,&pss);
01177         if (status == X400_E_NO_VALUE) {
01178             break; /* Now try the extension fields*/
01179         } else if ( status != X400_E_NOERROR ) {
01180             return status;
01181         }
01182         
01183         status = X400PSSGetStrParam (pss,buffer, sizeof buffer , &length);
01184         if ( status == X400_E_NOERROR ) {
01185             printf ("P772 Distribution code SIC %d: %.*s\n",
01186                     n, (int)length, buffer);
01187         } 
01188     }
01189 
01190     
01191  for ( n = 1; ; n++ ) {
01192         status = X400mtDistFieldGet (mp,n,&distfield);
01193         if (status == X400_E_NO_VALUE) {
01194             break; /* Now try the extension fields*/
01195         } else if ( status != X400_E_NOERROR ) {
01196             return status;
01197         }
01198         
01199         status = X400DistFieldGetStrParam (
01200             distfield,
01201             X400_S_DIST_CODES_EXT_OID,
01202             buffer,
01203             sizeof buffer,
01204             &length
01205         );
01206         if ( status == X400_E_NOERROR ) {
01207             printf ("P772 Distribution code Field Extension OID %d: %.*s\n",
01208                     n, (int)length, buffer);
01209         } 
01210         
01211         status = X400DistFieldGetStrParam (
01212             distfield,
01213             X400_S_DIST_CODES_EXT_VALUE,
01214             buffer,
01215             sizeof buffer,
01216             &length
01217         );
01218         if ( status == X400_E_NOERROR ) {
01219             printf ("P772 Distribution code Field Extension OID %d: %.*s\n",
01220                     n, (int)length, buffer);
01221         } 
01222  }
01223     
01224  return X400_E_NOERROR;
01225 }