25#include "ms_example.h"
30#include <seclabel_api.h>
33static char *optstr =
"u371m:d:p:w:M:D:P:W:o:O:r:l:R:y:C:iaqsAvf:kK:B:";
36static char *default_recip =
"/CN=P7User1/O=attlee/PRMD=TestPRMD/ADMD=TestADMD/C=GB/";
38static const char text[] =
"First line\r\nSecond line\r\n";
39static char *binary_data;
41static char *fwd_subject =
"Forwarded message subject";
43static int add_binary_attachment(
struct X400msMessage *mp,
char *filename_to_send);
44static int add_fwd_bp(
struct X400msMessage *mp,
char *recip_orn,
char *recip_dn);
45static int add_ftbp(
struct X400msMessage *mp);
46static void usage(
void);
48int main(
int argc,
char **argv) {
54 struct X400msSession *sp;
55 struct X400msMessage *mp;
56 struct X400Recipient *rp;
62 if (get_args(argc, argv, optstr)) {
67 printf(
"Connection type (0 = P7, 1 = P3 submit only, 2 = P3 both directions) [%d]: ", x400_contype);
68 contype = ic_fgetc(x400_contype, stdin);
70 ic_fgetc(x400_contype, stdin);
73 if (contype <
'0' ||
'2' < contype) {
74 contype = x400_contype;
81 def_oraddr = x400_ms_user_addr;
82 def_dn = x400_ms_user_dn;
83 def_pa = x400_ms_presentation_address;
86 def_oraddr = x400_mta_user_addr;
87 def_dn = x400_mta_user_dn;
88 def_pa = x400_mta_presentation_address;
91 printf(
"Your ORAddress [%s] > ", def_oraddr);
92 ic_fgets(orn,
sizeof orn, stdin);
94 if ((strlen(orn) > 0) && (orn[strlen(orn) - 1] ==
'\n')) {
95 orn[strlen(orn) - 1] =
'\0';
99 strcpy(orn, def_oraddr);
103 printf(
"Password [%s]: ", contype == 0 ? x400_p7_password : x400_p3_password);
104 if (ic_fgets(creds,
sizeof(creds), stdin) == NULL) {
108 if ((strlen(creds) > 0) && (creds[strlen(creds) - 1] ==
'\n')) {
109 creds[strlen(creds) - 1] =
'\0';
111 if (creds[0] ==
'\0') {
112 strcpy(creds, contype == 0 ? x400_p7_password : x400_p3_password);
116 printf(
"Presentation Address [%s] > ", def_pa);
117 ic_fgets(pa,
sizeof pa, stdin);
119 if ((strlen(pa) > 0) && (pa[strlen(pa) - 1] ==
'\n')) {
120 pa[strlen(pa) - 1] =
'\0';
127 if (talking_to_marben_ms) {
145 status =
X400msOpen(contype, orn, def_dn, creds, pa, NULL, &sp);
148 fprintf(stderr,
"Error in Open: %s\n",
X400msError(status));
152 if (talking_to_marben_ms) {
178 if (x400_default_recipient != NULL) {
179 recip = x400_default_recipient;
182 recip = default_recip;
185 printf(
"Message recipient [%s]: ", recip);
186 ic_fgets(tmp,
sizeof tmp, stdin);
188 if ((strlen(tmp) > 0) && (tmp[strlen(tmp) - 1] ==
'\n')) {
189 tmp[strlen(tmp) - 1] =
'\0';
191 if (strlen(tmp) != 0) {
195 printf(
"Subject [%s]: ", subject);
196 ic_fgets(tmp,
sizeof tmp, stdin);
198 if ((strlen(tmp) > 0) && (tmp[strlen(tmp) - 1] ==
'\n')) {
199 tmp[strlen(tmp) - 1] =
'\0';
201 if (strlen(tmp) != 0) {
202 subject = strdup(tmp);
207 fprintf(stderr,
"x400msMsgNew returned error: %s\n",
X400msError(status));
213 fprintf(stderr,
"x400msRecipNew returned error: %s\n",
X400msError(status));
219 fprintf(stderr,
"x400msRecipAddStrParam returned error: %s\n",
X400msError(status));
225 fprintf(stderr,
"x400msRecipAddStrParam returned error: %s\n",
X400msError(status));
231 fprintf(stderr,
"x400msMsgAddStrParam returned error: %s\n",
X400msError(status));
237 fprintf(stderr,
"x400msMsgAddStrParam returned error: %s\n",
X400msError(status));
242 printf(
"delivery report request %d ( 1 - No, 2 - Yes)\n", x400_dr_req);
245 fprintf(stderr,
"x400msRecipAddStrParam returned error: %s\n",
X400msError(status));
250 printf(
"read notification request %d ( 1 - RN, 2 - NRN, 4 - return of IPM with NRN )\n", x400_rn_req);
253 fprintf(stderr,
"x400msRecipAddIntParam returned error: %s\n",
X400msError(status));
258 char *contid =
"ContID00001";
276 printf(
"message priority is %d ( 0 - normal, 1 - non-urgent, 2 - urgent)\n", x400_default_priority);
283 printf(
"military message priority is %d ( 0 - low, 1 - high)\n", x400_default_priority);
293 char tmp_buffer[255];
295 snprintf(tmp_buffer, 244,
"%s '%s' '%.19s'", subject, get_x400_pty_str_from_4406(x400_default_priority), ctime(&t));
296 printf(
"Subject is '%s'\n", tmp_buffer);
299 fprintf(stderr,
"x400msMsgAddStrParam returned error: %s\n",
X400msError(status));
304 status = add_sec_label(mp);
306 fprintf(stderr,
"Failed to add Security Label: %s\n",
X400msError(status));
310 fprintf(stderr,
"added Security Label\n");
316 fprintf(stderr,
"x400msMsgAddStrParam returned error: %s\n",
X400msError(status));
322 fprintf(stderr,
"x400ms returned error: %s\n",
X400msError(status));
329 printf(
"failed to add X400_T_IA5TEXT BP\n");
334 if (filename_to_send != NULL) {
335 status = add_binary_attachment(mp, filename_to_send);
337 printf(
"failed to add X400_T_BINARY BP\n");
340 status = add_fwd_bp(mp, orn, def_dn);
342 printf(
"failed to add forwarded BP\n");
347 printf(
"no binary file set - not sending X400_T_BINARY\n");
348 printf(
"no binary file set - not sending forwarded BP\n");
352 status = add_ftbp(mp);
354 printf(
"failed to add X400_T_BINARY BP\n");
360#define XML_BUFSIZE 1024
361#define STRING_BUFSIZE 1024
363 const char *xml_filename =
"seclabel.xml";
364 char xml_content[XML_BUFSIZE];
365 char str_content[STRING_BUFSIZE];
366 int str_len = STRING_BUFSIZE;
370 fd = fopen(xml_filename,
"r");
372 fprintf(stderr,
"Failed to open %s : %s\n", xml_filename, strerror(errno));
375 fread(&xml_content, XML_BUFSIZE, 1, fd);
379 status = SecLabelInit(
"Example program");
380 if (status != SECLABEL_E_NOERROR) {
381 fprintf(stderr,
"SecLabelInit returned error %d\n", status);
386 status = SecLabelParse(xml_content, str_content, STRING_BUFSIZE, &str_len);
388 if (status != SECLABEL_E_NOERROR) {
389 fprintf(stderr,
"SecLabelParse returned error %d\n", status);
396 fprintf(stderr,
"x400msMsgAddStrParam returned error: %s\n",
X400msError(status));
404 fprintf(stderr,
"x400msMsgSend returned error: %s\n",
X400msError(status));
408 struct X400Recipient *irp;
418 fprintf(stderr,
"A recipient with ");
423 fprintf(stderr,
"ORAddress %s ", buf);
429 fprintf(stderr,
" and DN %s", buf);
431 fprintf(stderr,
" was rejected\n");
442 printf(
"Message submitted successfully\n");
446 fprintf(stderr,
"No MessageId present from submission result: error %d\n", status);
450 printf(
"MessageId from Submission Result = %s\n", buf);
455 fprintf(stderr,
"No MessageSubmissionTime present from submission result: error %d\n", status);
459 printf(
"MessageSubmissionTime from Submission Result = %s\n", buf);
464 fprintf(stderr,
"No ContentIdentifier present from submission result: error %d\n", status);
468 printf(
"ContentIdentifier from Submission Result = %s\n", buf);
476 fprintf(stderr,
"Failed to get raw message length: error %d\n", status);
479 struct X400msMessage *newmsg;
482 databuf = (
char *)malloc(retlen);
483 printf(
"Data buffer length required = %d\n", (
int)retlen);
486 fprintf(stderr,
"Failed to get raw message: error %d\n", status);
490 FILE *fd = fopen(
"message.dump",
"w");
492 fprintf(stderr,
"Failed to open file message.dump\n");
495 if (fwrite(databuf, 1, retlen, fd) < retlen) {
496 fprintf(stderr,
"Failed to write message to file message.dump\n");
499 fprintf(stdout,
"Dumped message to file message.dump\n");
508 fprintf(stderr,
"Failed to create message from bytes: error %d\n", status);
517 fprintf(stderr,
"x400msMsgDelete returned error: %s\n",
X400msError(status));
529static int add_binary_attachment(
struct X400msMessage *mp,
char *filename) {
536 printf(
"sending file %s\n", filename);
537 if ((fd = open(filename_to_send, O_RDONLY)) == -1) {
538 printf(
"Failed to open content file %s: ", filename);
542 if (fstat(fd, &buf) != 0) {
544 printf(
"Can't fstat file %s %d", filename, errno);
548 file_size = buf.st_size;
549 printf(
"Content file size = %d\n", file_size);
551 binary_data = (
char *)malloc(file_size);
552 if (binary_data == NULL) {
556 if ((fs = read(fd, binary_data, file_size)) == -1) {
557 printf(
"Cannot read from binary file %d\n", errno);
564 printf(
"failed to add X400_T_BINARY BP\n");
570static int add_fwd_bp(
struct X400msMessage *mp,
char *orig_orn,
char *orig_dn) {
571 struct X400Message *x400_mp;
572 struct X400Recipient *rp;
576 printf(
"sending fwd bp \n");
580 fprintf(stderr,
"x400MsgNew returned error: %s\n",
X400msError(status));
586 fprintf(stderr,
"x400MsgAddStrParam returned error: %s\n",
X400msError(status));
592 fprintf(stderr,
"x400MsgAddStrParam returned error: %s\n",
X400msError(status));
599 fprintf(stderr,
"x400MsgAddStrParam returned error: %s\n",
X400msError(status));
604 fprintf(stderr,
"x400MsgAddStrParam returned error: %s\n",
X400msError(status));
611 fprintf(stderr,
"x400RecipNew returned error: %s\n",
X400msError(status));
618 fprintf(stderr,
"x400RecipAddStrParam returned error: %s\n",
X400msError(status));
625 fprintf(stderr,
"x400msRecipAddStrParam returned error: %s\n",
X400msError(status));
632 fprintf(stderr,
"X400MsgAddRecip returned error: %s\n",
X400msError(status));
635 printf(
"Put %s in as originator\n", orig_orn);
640 fprintf(stderr,
"x400RecipNew returned error: %s\n",
X400msError(status));
646 fprintf(stderr,
"x400RecipAddStrParam returned error: %s\n",
X400msError(status));
652 fprintf(stderr,
"x400msRecipAddStrParam returned error: %s\n",
X400msError(status));
658 fprintf(stderr,
"X400MsgAddRecip returned error: %s\n",
X400msError(status));
661 printf(
"Put %s in as reipient\n", recip);
666 fprintf(stderr,
"x400RecipNew returned error: %s\n",
X400msError(status));
672 fprintf(stderr,
"x400RecipAddStrParam returned error: %s\n",
X400msError(status));
678 fprintf(stderr,
"x400msRecipAddStrParam returned error: %s\n",
X400msError(status));
685 fprintf(stderr,
"X400MsgAddRecip returned error: %s\n",
X400msError(status));
688 printf(
"Put %s in as reipient\n", recip);
693 printf(
"message priority is %d ( 0 - normal, 1 - non-urgent, 2 - urgent)\n", x400_default_priority);
700 printf(
"military message priority is %d ( 0 - low, 1 - high)\n", x400_default_priority);
710 fprintf(stderr,
"x400msMsgAddStrParam returned error: %s\n",
X400msError(status));
717 printf(
"failed to add X400_T_IA5TEXT BP\n");
725 fprintf(stderr,
"x400ms returned error: %s\n",
X400msError(status));
737 fprintf(stderr,
"X400msMsgAddMessageBody returned error: %s\n",
X400msError(status));
744static int add_ftbp(
struct X400msMessage *mp) {
749 struct X400Bodypart *bp;
752 if (filename_to_send == NULL) {
753 printf(
"no file set - not sending X400_T_FTBP\n");
756 fp = fopen(filename_to_send,
"rb");
757 if (fp == (FILE *)NULL) {
758 printf(
"Cannot open binary file\n");
761 binary_data = (
char *)malloc(bin_bp_size);
762 if (binary_data == NULL) {
765 if ((fs = fread(binary_data,
sizeof(
char), bin_bp_size /
sizeof(
char), fp)) == -1) {
766 printf(
"Cannot read from binary file\n");
772 if (fs < bin_bp_size) {
773 printf(
"Cannot read %d bytes from binary file (got %d)\n", bin_bp_size, fs);
791 printf(
"failed to add X400_T_FTBP BP\n");
794 printf(
"Sent %d bytes as X400_T_FTBP BP\n", fs);
798static void usage(
void) {
799 printf(
"usage: %s\n", optstr);
800 printf(
"\t where:\n");
801 printf(
"\t -u : Don't prompt to override defaults \n");
802 printf(
"\t -3 : Use P3 connection \n");
803 printf(
"\t -7 : Use P7 connection \n");
804 printf(
"\t -1 : Use Marben-compatibility mode for P7 connection \n");
805 printf(
"\t -m : OR Address in P7 bind arg \n");
806 printf(
"\t -d : DN in P7 bind arg \n");
807 printf(
"\t -p : Presentation Address of P7 Store \n");
808 printf(
"\t -w : P7 password of P7 user \n");
809 printf(
"\t -M : OR Address in P3 bind arg \n");
810 printf(
"\t -D : DN in P3 bind arg \n");
811 printf(
"\t -P : Presentation Address of P3 server\n");
812 printf(
"\t -W : P3 password of P3 user \n");
813 printf(
"\t -o : Originator \n");
814 printf(
"\t -O : Originator Return Address \n");
815 printf(
"\t -r : Recipient\n");
816 printf(
"\t -l : Logline\n");
817 printf(
"\t -R : Report setting: 0=none, 1=-ve, 2=+ve\n");
818 printf(
"\t -y : Military Priority \n");
819 printf(
"\t\t 0 - deferred, 1 - routine, 2 - priority \n");
820 printf(
"\t\t 3 - immediate, 4 - flash, 5 - override \n");
821 printf(
"\t -C : Content Type (2/22/772/OID) \n");
822 printf(
"\t -i : Implicit conversion prohibited = TRUE \n");
823 printf(
"\t -a : Alternate Recipient Prohibited = TRUE \n");
824 printf(
"\t -q : Content Return Request = TRUE \n");
825 printf(
"\t -s : Disclosure of Recipient = FALSE \n");
826 printf(
"\t -A : Recipient Reassignment Prohibited = FALSE \n");
827 printf(
"\t -v : Conversion with Loss Prohibited = FALSE \n");
828 printf(
"\t -f : Filename to transfer as binary bp\n");
829 printf(
"\t -k : Request Delivery Report\n");
830 printf(
"\t -K : Request Read Notification ( 1 - RN, 2 - NRN, 4 - return of IPM with NRN )\n");
831 printf(
"\t -B : Set alternative subject line \n");
int X400RecipNew(int type, struct X400Recipient **rpp)
Create a new recipient object.
int X400MsgAddRecip(struct X400Message *mp, int reciptype, struct X400Recipient *recip)
Add a recipient object to the message.
int X400RecipAddStrParam(struct X400Recipient *rp, int paramtype, const char *value, size_t length)
Add string-valued parameter to the recipient.
int X400MsgAddIntParam(struct X400Message *mp, int paramtype, int value)
Add integer-valued parameter to the message.
int X400BodypartAddStrParam(struct X400Bodypart *bp, int paramtype, const char *value, size_t length)
Add string-valued parameter to the body part.
int X400BodypartAddIntParam(struct X400Bodypart *bp, int paramtype, int value)
Add integer-valued parameter to the body part.
int X400MsgNew(int type, struct X400Message **mpp)
Creates new message.
int X400MsgAddAttachment(struct X400Message *mp, int type, const char *string, size_t length)
Add an attachment to the message.
int X400MsgAddStrParam(struct X400Message *mp, int paramtype, const char *value, size_t length)
Add string-valued parameter to the message.
int X400BodypartNew(int type, struct X400Bodypart **bpp)
Create a new body part object.
int X400msMsgAddStrParam(struct X400msMessage *mp, int paramtype, const char *value, size_t length)
Add string-valued parameter to the message.
int X400msMsgFromRaw(struct X400msSession *sp, char *buffer, size_t buflen, struct X400msMessage **mpp, int *typep)
Reconstruct a message from a binary representation.
int X400msMsgAddBodypart(struct X400msMessage *mp, struct X400Bodypart *bp)
int X400msRecipGet(struct X400msMessage *mp, int type, int number, struct X400Recipient **rpp)
Get recipient object from message.
int X400msRecipAddIntParam(struct X400Recipient *rp, int paramtype, int value)
Add integer-valued parameter to the message.
int X400msMsgAddIntParam(struct X400msMessage *mp, int paramtype, int value)
Add integer-valued parameter to the message.
int X400msMsgGetStrParam(struct X400msMessage *mp, int paramtype, char *buffer, size_t buflen, size_t *paramlenp)
Return a string-valued parameter from the message object.
int X400msMsgGetRaw(struct X400msSession *sp, struct X400msMessage *mp, char *buffer, size_t buflen, size_t *buflenp)
Get a binary representation of a message which can be subsequently be used to reconstruct the message...
int X400msRecipGetStrParam(struct X400Recipient *rp, int paramtype, char *buffer, size_t buflen, size_t *paramlenp)
Return a string-valued parameter from the recipient object.
int X400msRecipNew(struct X400msMessage *mp, int type, struct X400Recipient **rpp)
Add new recipient to a message.
const char * X400msError(int error)
Obtain a string describing the meaning of the given error code.
int X400msRecipAddStrParam(struct X400Recipient *rp, int paramtype, const char *value, size_t length)
Add string-valued parameter to the message.
int X400msSetIntDefault(struct X400msSession *sp, int paramtype, int value)
Set a default integer parameter value in a session.
int X400msMsgSend(struct X400msMessage *mp)
Send message object.
int X400msMsgAddMessageBody(struct X400msMessage *mp, struct X400Message *mbp)
int X400msMsgDelete(struct X400msMessage *mp, int retain)
Delete message object.
int X400msMsgAddAttachment(struct X400msMessage *mp, int type, const char *string, size_t length)
Add attachment to the message.
void X400msSetConfigRequest(int val)
Disable and enable configuration requests in MS Bind operations.
int X400msMsgNew(struct X400msSession *sp, int type, struct X400msMessage **mpp)
Creates new message.
int X400msClose(struct X400msSession *sp)
Close a X400 Session.
int X400msSetStrDefault(struct X400msSession *sp, int paramtype, const char *value, size_t length)
Set a default string parameter value in a session.
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_S_DIRECTORY_NAME
#define X400_S_OR_ADDRESS
#define X400_N_FTBP_OBJECT_SIZE
#define X400_S_FTBP_FILENAME
#define X400_S_FTBP_MODIFICATION_DATE
#define X400_S_FTBP_CREATION_DATE
#define X400_S_FTBP_CONTENT_DESCRIPTION
#define X400_N_NUM_ATTACHMENTS
#define X400_S_GRAPHIC_CHARSETS
#define X400_S_TELETEX_CHARSETS
#define X400_S_LOG_CONFIGURATION_FILE
#define X400_S_MESSAGE_DELIVERY_TIME
#define X400_N_MMTS_PRIORITY_QUALIFIER
#define X400_S_CONTENT_IDENTIFIER
#define X400_N_CONTENT_RETURN_REQUEST
#define X400_S_MESSAGE_SUBMISSION_TIME
#define X400_S_EXTERNAL_CONTENT_TYPE
#define X400_S_MESSAGE_IDENTIFIER
#define X400_S_SECURITY_LABEL
#define X400_E_RECIPIENT_ERROR
#define X400_N_ERROR_DUPLICATE_ATTRIBUTE
#define X400_N_STRICT_P7_1988
#define X400_N_USE_EXTENDED_SUBJECT
#define X400_N_REPORT_REQUEST
#define X400_N_NOTIFICATION_REQUEST
#define X400_RECIP_STANDARD
#define X400_RECIP_INVALID
X400 MA/MS (P3/P7) Interface.