32 #include <seclabel_api.h> 34 #include "ms_example.h" 46 #define FIRST_SEQ_NUM 2 49 int seq_num_val_1 = FIRST_SEQ_NUM;
50 int seq_num_val_2 = FIRST_SEQ_NUM + 100;
54 int send_ia5_as_att = 0;
60 static char *optstr =
"u37m:d:p:w:M:D:P:W:r:o:O:r:g:G:c:l:R:y:C:iaqsAve:x:b:f:Y:S:j:";
63 static char *default_recip =
"/CN=P7User1/OU=sales/O=attlee/PRMD=TestPRMD/ADMD=TestADMD/C=GB/";
66 static char *content_id =
"030924.140212";
67 static const char text[] =
"First line\r\nSecond line\r\n";
69 int adding_sec_label = 1;
73 static void usage(
void) ;
81 static int submit_msg(
83 struct X400msSession *sp
86 static int setup_default_new_sec_env(
87 struct X400msSession *sp,
88 char *identity_filename,
93 static int setup_default_old_sec_env(
94 struct X400msSession *sp,
101 static int setup_msg_new_sec_env(
102 struct X400msMessage *mp,
103 char *identity_filename,
108 static int setup_msg_old_sec_env(
109 struct X400msMessage *mp,
115 static int setup_recip_new_sec_env(
116 struct X400Recipient *rp,
117 char *identity_filename,
121 static int setup_recip_old_sec_env(
122 struct X400Recipient *rp,
131 static int add_sec_label(
132 struct X400msMessage *mp
135 static int add_recip(
136 struct X400msMessage *mp,
140 static int add_content(
141 struct X400msMessage *mp
144 static int add_binary_bp(
145 struct X400msMessage *mp
149 struct X400msMessage *mp
152 static int add_ia5_str(
153 struct X400msMessage *mp
169 char password[BUFSIZ];
171 if (get_args(argc, argv, optstr)) {
176 printf(
"Connection type (0 = P7, 1 = P3 submit only, 2 = P3 both directions) [%d]: ", x400_contype);
177 contype = ic_fgetc(x400_contype, stdin);
179 ic_fgetc(x400_contype, stdin);
181 if ( contype <
'0' ||
'2' < contype )
182 contype = x400_contype;
187 def_oraddr = x400_ms_user_addr;
188 def_dn = x400_ms_user_dn;
189 def_pa = x400_ms_presentation_address;
191 def_oraddr = x400_mta_user_addr;
192 def_dn = x400_mta_user_dn;
193 def_pa = x400_mta_presentation_address;
196 printf(
"Your ORAddress [%s] > ", def_oraddr);
197 ic_fgets (orn,
sizeof orn, stdin);
199 if ( orn[strlen(orn)-1] ==
'\n' )
200 orn[strlen(orn)-1] =
'\0';
203 strcpy(orn, def_oraddr);
206 printf (
"Password [%s]: ",
207 contype == 0 ? x400_p7_password : x400_p3_password);
208 if ( ic_fgets (password,
sizeof password, stdin) == NULL )
211 if (password[strlen(password)-1] ==
'\n' )
212 password[strlen(password)-1] =
'\0';
213 if (password[0] ==
'\0')
214 strcpy(password, contype == 0 ? x400_p7_password : x400_p3_password);
217 printf(
"Presentation Address [%s] > ", def_pa);
218 ic_fgets (pa,
sizeof pa, stdin);
220 if ( pa[strlen(pa)-1] ==
'\n' )
221 pa[strlen(pa)-1] =
'\0';
226 printf(
"sending message using session 1\n");
227 if ((status = send_msg(contype, orn, def_dn, pa, password))
229 fprintf (stderr,
"Error in sending message\n");
234 printf(
"sending message using session 2\n");
235 if ((status = send_msg(contype, orn, def_dn, pa, password))
237 fprintf (stderr,
"Error sending message\n");
253 struct X400msSession *sp;
260 status =
X400msOpen (contype, orn, def_dn, password, pa, &nummsg, &sp);
262 fprintf (stderr,
"Error in Open: %s\n",
X400msError (status));
263 fprintf (stderr,
"%s %s %s\n", orn, def_dn, pa);
284 if (use_new_sec_env) {
285 status = setup_default_new_sec_env(sp, identity_filename, passphrase);
287 status = setup_default_old_sec_env(sp, security_id,
288 identity_dn, passphrase);
292 fprintf (stderr,
"Can't setup security environment\n");
296 printf(
"sending message 1\n");
297 status = submit_msg(orn, sp);
299 fprintf (stderr,
"Can't submit\n");
319 fprintf (stderr,
"X400msClose returned error: %s\n",
X400msError (status));
325 static int submit_msg(
327 struct X400msSession *sp
330 struct X400msMessage *mp;
332 struct X400Recipient *rp2;
335 static int msg_num = 0;
340 if (x400_default_recipient != NULL) {
341 recip = x400_default_recipient;
342 recip2 = x400_default_recipient;
344 recip = default_recip;
345 recip2 = default_recip;
348 printf(
"Message recipient [%s]: ", recip);
349 ic_fgets (tmp,
sizeof tmp, stdin);
351 if ( tmp[strlen(tmp)-1] ==
'\n' )
352 tmp[strlen(tmp)-1] =
'\0';
353 if (strlen(tmp) != 0) {
355 recip2 = strdup(tmp);
358 printf(
"Subject [%s]: ", subject);
359 ic_fgets (tmp,
sizeof tmp, stdin);
361 if ( tmp[strlen(tmp)-1] ==
'\n' )
362 tmp[strlen(tmp)-1] =
'\0';
363 if (strlen(tmp) != 0)
364 subject = strdup(tmp);
369 fprintf (stderr,
"x400msMsgNew returned error: %s\n",
374 #ifdef use_diff_sec_env_second_msg 378 status = setup_msg_sec_env(mp,
"/root",
379 "cn=P7User1,o=Address Book,c=GB",
"secret");
381 fprintf (stderr,
"setup_msg_sec_env returned error: %s\n",
397 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
401 fprintf (stderr,
"Adding MOAC\n");
409 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
415 "CN=originator;c=gb", -1);
417 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
422 if (adding_sec_label) {
423 status = add_sec_label (mp);
425 fprintf (stderr,
"Failed to add Security Label: %s\n",
429 fprintf (stderr,
"added Security Label\n");
435 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
452 printf(
"military message priority is %d (%s) \n",
453 x400_default_priority,
454 get_x400_pty_str_from_4406(x400_default_priority));
456 get_x400_pty_from_4406(x400_default_priority));
460 printf(
"military message priority qualifer is %d ( 0 - low, 1 - high)\n",
461 get_x400_pty_qual_from_4406(x400_default_priority));
463 get_x400_pty_qual_from_4406(x400_default_priority));
470 char tmp_buffer[255];
472 snprintf(tmp_buffer, 244,
"%s '%s' '%.19s'",
473 subject, get_x400_pty_str_from_4406(x400_default_priority), ctime(&t));
474 printf(
"Subject is '%s'\n", tmp_buffer);
477 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
486 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
X400msError (status));
491 fprintf (stderr,
"add_recip() returned error: %s\n",
507 fprintf (stderr,
"add_content() returned error: %s\n",
514 fprintf (stderr,
"x400msMsgSend returned error: %s\n",
518 printf(
"Message submitted successfully\n");
523 fprintf (stderr,
"x400msMsgDelete returned error: %s\n",
533 static int add_recip(
534 struct X400msMessage *mp,
539 struct X400Recipient *rp;
544 fprintf (stderr,
"x400msRecipNew returned error: %s\n",
552 fprintf (stderr,
"x400msRecipAddIntParam returned error: %s\n",
560 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
566 "CN=recipient;c=gb", -1);
568 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
576 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
584 fprintf (stderr,
"X400msRecipAddIntParam returned error: %s\n",
590 fprintf (stderr,
"requesting token\n");
593 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
603 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
607 fprintf (stderr,
"Added sequence number %d for token\n", seq_num_val_1);
618 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
626 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
634 static int setup_default_new_sec_env(
635 struct X400msSession *sp,
642 fprintf (stderr,
"using new_sec_nv %s\n", idf);
649 fprintf (stderr,
"X400msSetStrDefault returned error: %s\n",
657 fprintf (stderr,
"X400msSetStrDefault returned error: %s\n",
665 fprintf (stderr,
"X400msTestSecurityEnv returned error: %s\n",
674 static int setup_default_old_sec_env(
675 struct X400msSession *sp,
683 fprintf (stderr,
"using old_sec_nv\n");
693 fprintf (stderr,
"X400msSetStrDefault returned error: %s\n",
701 fprintf (stderr,
"X400msSetStrDefault returned error: %s\n",
709 static int setup_msg_new_sec_env(
710 struct X400msMessage *mp,
711 char *identity_filename,
721 identity_filename, -1);
723 fprintf (stderr,
"X400msMsgAddStrParam returned error: %s\n",
728 printf(
"setting up new message security env %s\n", identity_filename);
733 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
740 static int setup_msg_old_sec_env(
741 struct X400msMessage *mp,
749 printf(
"setting up message security env %s\n",
id);
756 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
764 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
769 printf(
"setting up new message security env %s\n", identity_filename);
774 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
781 static int setup_recip_new_sec_env(
782 struct X400Recipient *rp,
783 char *identity_filename,
789 printf(
"setting up recipient security env %s\n", identity_filename);
795 identity_filename, -1);
797 fprintf (stderr,
"X400msRecipAddStrParam returned error: %s\n",
805 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
814 static int setup_recip_old_sec_env(
815 struct X400Recipient *rp,
823 printf(
"setting up recipient security env %s\n",
id);
830 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
837 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
845 fprintf (stderr,
"x400msRecipAddStrParam returned error: %s\n",
854 static int add_sec_label(
855 struct X400msMessage *mp
858 #define XML_BUFSIZE 1024 859 #define STRING_BUFSIZE 1024 861 const char* xml_filename =
"seclabel.xml";
862 char xml_content[XML_BUFSIZE];
863 char str_content[STRING_BUFSIZE];
864 int str_len = STRING_BUFSIZE;
869 fd = fopen(xml_filename,
"r");
871 fprintf(stderr,
"Failed to open %s : %s\n",
872 xml_filename,strerror(errno));
876 fread(&xml_content,XML_BUFSIZE,1,fd);
880 status = SecLabelInit(
"Example program");
881 if (status != SECLABEL_E_NOERROR) {
882 fprintf(stderr,
"SecLabelInit returned error %d\n", status);
887 status = SecLabelParse(xml_content,
892 if (status != SECLABEL_E_NOERROR) {
893 fprintf(stderr,
"SecLabelParse returned error %d\n", status);
899 str_content,str_len);
901 fprintf (stderr,
"x400msMsgAddStrParam returned error: %s\n",
908 static int add_content(
909 struct X400msMessage *mp
934 fprintf (stderr,
"x400ms returned error: %s\n",
X400msError (status));
937 printf(
"Sent 8859 attachment as string:\n%s\n", text);
939 if (send_ia5_as_att) {
944 printf(
"failed to add X400_T_IA5TEXT attachment\n");
947 printf(
"Sent IA5 as first attachment:\n%s\n", text);
950 if (num_of_ia5_bytes == 0) {
953 printf(
"failed to add X400_T_IA5TEXT BP\n");
956 printf(
"Sent IA5 as string in message \n%s\n", text);
958 status = add_ia5_str(mp);
960 printf(
"failed to add IA5 str\n");
969 printf(
"failed to add X400_T_ISO8859_1, attachment\n");
972 printf(
"Sent 8859-1 as attachment:\n%s\n", text);
975 status = add_binary_bp(mp);
977 printf(
"failed to add X400_T_BINARY BP\n");
982 status = add_ftbp(mp);
984 printf(
"failed to add X400_T_BINARY BP\n");
990 if (filename_to_send != NULL) {
991 binary_data = (
char *) malloc(100000);
992 if ( binary_data == NULL )
994 fp = fopen(filename_to_send,
"r");
995 if (fp == (FILE *)NULL) {
996 printf(
"Cannot open binary file\n");
999 if ((fs = fread (binary_data,
sizeof(
char), 100000/
sizeof(
char), fp))
1001 printf(
"Cannot read from binary file\n");
1008 printf(
"failed to add X400_T_BINARY BP\n");
1014 printf(
"failed to add X400_T_IA5TEXT BP\n");
1017 printf(
"Sent IA5 as string in message \n%s\n", text);
1022 printf(
"no binary file set - not sending X400_T_BINARY\n");
1028 static int add_ia5_str(
1029 struct X400msMessage *mp
1035 char *sample_ia5 = {
"Here is some sample text to put into the IA5 attachment."};
1037 if ((tmp = malloc(num_of_ia5_bytes + 1)) == NULL) {
1038 printf(
"failed to malloc %d bytes\n", num_of_ia5_bytes);
1042 for (i = strlen(text) ; i < num_of_ia5_bytes ; i++)
1043 tmp[i] = sample_ia5[i%strlen(sample_ia5)];
1048 printf(
"failed to add X400_T_IA5TEXT BP\n");
1051 printf(
"Sent %d bytes IA5 as string in message \n%s\n", i, tmp);
1056 static int add_binary_bp(
1057 struct X400msMessage *mp
1066 if (filename_to_send != NULL) {
1067 fp = fopen(filename_to_send,
"rb");
1068 if (fp == (FILE *)NULL) {
1069 printf(
"Cannot open binary file\n");
1072 binary_data = (
char *) malloc(bin_bp_size);
1073 if ( binary_data == NULL )
1075 if ((fs = fread (binary_data,
sizeof(
char),
1076 bin_bp_size/
sizeof(
char), fp) ) == -1) {
1077 printf(
"Cannot read from binary file\n");
1083 if (fs < bin_bp_size) {
1084 printf(
"Cannot read %d bytes from binary file (got %d)\n",
1093 printf(
"failed to add X400_T_BINARY BP\n");
1096 printf(
"Sent %d bytes as X400_T_BINARY BP\n", fs);
1098 printf(
"no binary file set - not sending X400_T_BINARY\n");
1104 static int add_ftbp(
1105 struct X400msMessage *mp
1112 struct X400Bodypart *bp;
1115 if (filename_to_send != NULL) {
1116 fp = fopen(filename_to_send,
"rb");
1117 if (fp == (FILE *)NULL) {
1118 printf(
"Cannot open binary file\n");
1121 binary_data = (
char *) malloc(bin_bp_size);
1122 if ( binary_data == NULL )
1124 if ((fs = fread (binary_data,
sizeof(
char),
1125 bin_bp_size/
sizeof(
char), fp) ) == -1) {
1126 printf(
"Cannot read from binary file\n");
1132 if (fs < bin_bp_size) {
1133 printf(
"Cannot read %d bytes from binary file (got %d)\n",
1142 "Test FTBP File", -1);
1147 "20160801060101.0Z", -1);
1149 "20160801060202.0Z", -1);
1155 printf(
"failed to add X400_T_FTBP BP\n");
1158 printf(
"Sent %d bytes as X400_T_FTBP BP\n", fs);
1160 printf(
"no binary file set - not sending X400_T_FTBP\n");
1166 static void usage(
void) {
1167 printf(
"usage: %s\n", optstr);
1168 printf(
"\t where:\n");
1169 printf(
"\t -u : Don't prompt to override defaults \n");
1170 printf(
"\t -3 : Use P3 connection \n");
1171 printf(
"\t -7 : Use P7 connection \n");
1172 printf(
"\t -m : OR Address in P7 bind arg \n");
1173 printf(
"\t -d : DN in P7 bind arg \n");
1174 printf(
"\t -p : Presentation Address of P7 Store \n");
1175 printf(
"\t -w : P7 password of P7 user \n");
1176 printf(
"\t -M : OR Address in P3 bind arg \n");
1177 printf(
"\t -D : DN in P3 bind arg \n");
1178 printf(
"\t -P : Presentation Address of P3 server\n");
1179 printf(
"\t -W : P3 password of P3 user \n");
1180 printf(
"\t -o : Originator \n");
1181 printf(
"\t -O : Originator Return Address \n");
1182 printf(
"\t -r : Recipient\n");
1183 printf(
"\t -l : Logline\n");
1184 printf(
"\t -R : Report requests\n");
1185 printf(
"\t\t : 0 - none, 1 - non-delivery, 2 - delivery (implies 1)\n");
1186 printf(
"\t -y : Military Priority \n");
1187 printf(
"\t\t 0 - deferred, 1 - routine, 2 - priority \n");
1188 printf(
"\t\t 3 - immediate, 4 - flash, 5 - override \n");
1189 printf(
"\t -C : Content Type (2/22/772/OID) \n");
1190 printf(
"\t -i : Implicit conversion prohibited = TRUE \n");
1191 printf(
"\t -a : Alternate Recipient Prohibited = TRUE \n");
1192 printf(
"\t -q : Content Return Request = TRUE \n");
1193 printf(
"\t -s : Disclosure of Recipient = FALSE \n");
1194 printf(
"\t -A : Recipient Reassignment Prohibited = FALSE \n");
1195 printf(
"\t -v : Conversion with Loss Prohibited = FALSE \n");
1196 printf(
"\t -e : Security Environment (dir with x509 subdir): obsolete, use -Y <p12file>\n");
1197 printf(
"\t -x : DN of X.509 Digital Identity\n");
1198 printf(
"\t -b : Passphrase for private key in PKCS12 file\n");
1199 printf(
"\t -f : Filename to transfer as binary bp\n");
1200 printf(
"\t -Y : Filename of PKCS12 file containing Digital Identity\n");
1201 printf(
"\t -S : Number of bytes to put into Binary BP and FTBP (default 100)\n");
1202 printf(
"\t -j : Number of bytes to put into IA5 BP (default 100)\n");
#define X400_S_DIRECTORY_NAME
#define X400_S_EXTERNAL_CONTENT_TYPE
int X400BodypartNew(int type, struct X400Bodypart **bpp)
Create a new body part object.
#define X400_S_LOG_CONFIGURATION_FILE
int X400msSetStrDefault(struct X400msSession *sp, int paramtype, const char *value, size_t length)
Set a default string parameter value in a session.
#define X400_S_FTBP_CONTENT_DESCRIPTION
#define X400_S_CONTENT_IDENTIFIER
int X400msMsgAddStrParam(struct X400msMessage *mp, int paramtype, const char *value, size_t length)
Add string-valued parameter to the message.
#define X400_S_SEC_IDENTITY
int X400BodypartAddStrParam(struct X400Bodypart *bp, int paramtype, const char *value, size_t length)
Add string-valued parameter to the body part.
int X400msTestSecurityEnv(struct X400msSession *sp)
Test the default Security Environment.
int X400msRecipAddIntParam(struct X400Recipient *rp, int paramtype, int value)
Add integer-valued parameter to the message.
int X400msMsgAddBodypart(struct X400msMessage *mp, struct X400Bodypart *bp)
#define X400_N_PRECEDENCE
const char * X400msError(int error)
Obtain a string describing the meaning of the given error code.
#define X400_S_FTBP_FILENAME
int X400msMsgNew(struct X400msSession *sp, int type, struct X400msMessage **mpp)
Creates new message.
int X400msClose(struct X400msSession *sp)
Close a X400 Session.
#define X400_N_PROOF_OF_DEL_REQ
#define X400_S_FTBP_CREATION_DATE
#define X400_N_NOTIFICATION_REQUEST
#define X400_S_FTBP_MODIFICATION_DATE
int X400msMsgAddAttachment(struct X400msMessage *mp, int type, const char *string, size_t length)
Add attachment to the message.
int X400msRecipAddStrParam(struct X400Recipient *rp, int paramtype, const char *value, size_t length)
Add string-valued parameter to the message.
int X400msOpen(int type, const char *oraddr, const char *dirname, const char *credentials, const char *pa, int *messages, struct X400msSession **spp)
Open a session to a Message Store (P7) or MTA (P3) in synchronous mode.
#define X400_N_FTBP_OBJECT_SIZE
#define X400_S_OR_ADDRESS
#define X400_S_SEC_IDENTITY_PASSPHRASE
#define X400_B_SEC_GEN_MESSAGE_TOKEN
#define X400_RECIP_STANDARD
#define X400_S_SECURITY_LABEL
#define X400_S_SEC_IDENTITY_DN
#define X400_N_REPORT_REQUEST
int X400msMsgSend(struct X400msMessage *mp)
Send message object.
int X400BodypartAddIntParam(struct X400Bodypart *bp, int paramtype, int value)
Add integer-valued parameter to the body part.
#define X400_B_SEC_CONTENT_INTEGRITY_CHECK
X400 MA/MS (P3/P7) Interface.
#define X400_S_SEC_IDENTITY_FILE
int X400msRecipNew(struct X400msMessage *mp, int type, struct X400Recipient **rpp)
Add new recipient to a message.
#define X400_N_MSGTOK_SEQ_NUM
#define X400_B_SEC_GEN_MOAC
#define X400_N_MMTS_PRIORITY_QUALIFIER
int X400msMsgAddIntParam(struct X400msMessage *mp, int paramtype, int value)
Add integer-valued parameter to the message.
int X400msMsgDelete(struct X400msMessage *mp, int retain)
Delete message object.