x400_mtcontent.c
1 /* Copyright (c) 2003-2009, Isode Limited, London, England.
2  * All rights reserved.
3  *
4  * Acquisition and use of this software and related materials for any
5  * purpose requires a written licence agreement from Isode Limited,
6  * or a written licence from an organisation licenced by Isode Limited
7  * to grant such a licence.
8  *
9  */
10 
11 /*
12  *
13  * @VERSION@
14  * Test harness for MT Content manipulation functions
15  */
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <x400_mtapi.h>
20 #include <seclabel_api.h> /* For security labels */
21 #include "example.h"
22 /* to get NDR values */
23 /* #include "esc.h" */
24 
25 static int get_msg (
26  int argc,
27  char **argv
28 ) ;
29 
30 static int get_a_msg (struct X400mtSession *sp);
31 
32 static char *optstr = "uo:O:r:c:l:EQ:";
33 
34 static void usage(void) {
35  printf("usage: %s\n", optstr);
36  printf("\t where:\n");
37  printf("\t -u : Don't prompt to override defaults \n");
38  printf("\t -o : Originator \n");
39  printf("\t -O : Originator Return Address \n");
40  printf("\t -r : Recipient\n");
41  printf("\t -c : X.400 passive channel\n");
42  printf("\t -l : Logline\n");
43  printf("\t -E : Stop after one attempt to transfer a msg\n");
44  return;
45 }
46 
50 int main (
51  int argc,
52  char **argv
53 )
54 {
55  int retval;
56 
57  if (x400_channel == NULL) {
58  fprintf (stderr, "No x400_channel value set in x400tailor file");
59  exit(1);
60  }
61 
62  get_msg(argc, argv);
63 
64  /*
65  while (retval == X400_E_NOERROR || retval == X400_E_NO_MESSAGE) {
66  retval = get_msg(argc, argv);
67  }
68  */
69 
70  retval = get_msg(argc, argv);
71  fprintf (stderr, "Error in X400mtMsgGet: %s\n", X400mtError (retval));
72 
73  return(retval);
74 }
75 
76 static int get_msg (
77  int argc,
78  char **argv
79 )
80 {
81  int status;
82  struct X400mtSession *sp;
83 
84  if (get_args(argc, argv, optstr)) {
85  usage();
86  exit(-1);
87  }
88 
89  /* open our X400 session */
90  status = X400mtOpen (x400_channel, &sp);
91  if ( status != X400_E_NOERROR ) {
92  fprintf (stderr, "Error in Open: %s\n", X400mtError (status));
93  return (status);
94  }
95 
96  get_a_msg(sp);
97 
98  if ( status != X400_E_NOERROR ) {
99  /* close API session */
100  status = X400mtClose (sp);
101  if ( status != X400_E_NOERROR ) {
102  printf("X400mtClose returned error %d\n", status);
103  fprintf (stderr, "Error in X400mtClose: %s\n",
104  X400mtError (status));
105  }
106  return status;
107  }
108 
109  do {
110  get_a_msg(sp);
111  if ( status != X400_E_NOERROR ) {
112  /* close API session */
113  status = X400mtClose (sp);
114  if ( status != X400_E_NOERROR ) {
115  printf("X400mtClose returned error %d\n", status);
116  fprintf (stderr, "Error in X400mtClose: %s\n",
117  X400mtError (status));
118  }
119  return status;
120  }
121  }
122  while (until_no_more_msgs);
123 
124  /* close API session */
125  status = X400mtClose (sp);
126  if ( status != X400_E_NOERROR ) {
127  printf("X400mtClose returned error %d\n", status);
128  fprintf (stderr, "Error in X400mtClose: %s\n",
129  X400mtError (status));
130  }
131  return (status);
132 }
133 
134 static int map[] = { X400_S_SUBJECT,
138  -1 };
139 
140 
141 static int get_a_msg (struct X400mtSession *sp)
142 {
143  struct X400mtMessage *mp, *new1, *new2, *new3;
144  int type;
145  int status;
146  static int reported_none = 0;
147  char *buf = NULL;
148  size_t buflen = 0;
149  size_t required = 0;
150  char tbuf[1024];
151  int i;
152 
153  /* get the message */
154  status = X400mtMsgGetStart (sp, &mp, &type);
155  if ( status != X400_E_NOERROR ) {
156  if ( status == X400_E_NO_MESSAGE ) {
157  if (reported_none == 0) {
158  printf("No messages ...\n");
159  reported_none = 1;
160  }
161  return (X400_E_NO_MESSAGE);
162  }
163  fprintf (stderr, "Error in X400mtMsgGet: %s\n", X400mtError (status));
164  return (status);
165  }
166 
167  /* Create new message object */
168  X400mtMsgNew(sp, X400_MSG_MESSAGE, &new1);
169 
170 #define INITIAL_BUFSIZE (10 * 1024)
171 
172  /* Extract content from original message */
173  buf = (char*)malloc(INITIAL_BUFSIZE);
174  buflen = INITIAL_BUFSIZE;
175 
176  /* Get CONTENT as a string from the original message */
177  status = X400mtMsgGetStrParam(mp, X400_S_CONTENT_STRING, buf, buflen, &required);
178  if ((status != X400_E_NOERROR) && (status != X400_E_NOSPACE)) {
179  fprintf (stderr, "Error in X400mtGetStrParam: %s\n", X400mtError (status));
180  free(buf);
181  return (status);
182  }
183 
184  if (status == X400_E_NOSPACE) {
185  buf = (char *)realloc(buf, required);
186  buflen = required;
187 
188  status = X400mtMsgGetStrParam(mp, X400_S_CONTENT_STRING, buf, buflen, &required);
189  if (status != X400_E_NOERROR) {
190  fprintf (stderr, "Error in X400mtGetContentOctets: %s\n", X400mtError (status));
191  free(buf);
192  return (status);
193  }
194  }
195 
196  /* Finished with original message now */
197  status = X400mtMsgGetFinish(mp, X400_E_NOERROR, 0, 0, NULL);
198  if ( status != X400_E_NOERROR ) {
199  fprintf (stderr, "Error in X400mtMsgGetFinish: %s\n", X400mtError (status));
200  return (status);
201  }
202 
203  X400mtMsgDelete(mp);
204 
205  /* Here we would mangle it somehow - e.g extract P772 content from inside
206  something else which is wrapping it up.*/
207 
208  /* Put content into new message */
209  status = X400mtSetContentOctets(new1, buf, required);
210  if ((status != X400_E_NOERROR) && (status != X400_E_NOSPACE)) {
211  fprintf (stderr, "Error in X400mtSetContentOctets: %s\n", X400mtError (status));
212  free(buf);
213  return (status);
214  }
215  free(buf);
216 
217  /* Create yet another new message !*/
218  X400mtMsgNew(sp, X400_MSG_MESSAGE, &new2);
219 
220  for (i = 0; map[i] != -1; i++) {
221  int v;
222 
223  switch (map[i]) {
224  case X400_S_SUBJECT:
226  if (X400mtMsgGetStrParam(new1, map[i],
227  tbuf, sizeof(tbuf), &required) != X400_E_NOERROR) {
228  fprintf (stderr, "Error in X400mtMsgGetStrParam: %s\n", X400mtError (status));
229  } else {
230  tbuf[required] = '\0';
231  printf("Param type %d from copy message = %s\n", map[i], tbuf);
232 
233  if (map[i] == X400_S_SUBJECT) {
234  char *c;
235  /* Alter subject as example */
236  c = strstr(tbuf, "Test Message");
237  if (c != NULL)
238  strcat(c, "REDACTED!!!!");
239  }
240 
241  /* Copy attribute into new message object */
242  X400mtMsgAddStrParam(new2, map[i], tbuf, (size_t)-1);
243  }
244  break;
245 
246  case X400_N_IMPORTANCE:
247  if (X400mtMsgGetIntParam(new1, map[i], &v) != X400_E_NOERROR) {
248  fprintf (stderr, "Error in X400mtMsgGetIntParam: %s\n", X400mtError (status));
249  } else {
250  printf("Param type %d from copy message = %d\n", map[i], v);
251 
252  /* Copy attribute into new message object */
253  X400mtMsgAddIntParam(new2, map[i], v);
254  }
255  break;
256 
257  case X400_ORIGINATOR:
258  {
259  struct X400Recipient *rp;
260 
261  if (X400mtRecipGet(new1, map[i], 1, &rp) == X400_E_NOERROR) {
262  struct X400Recipient *nrp;
263 
264  if ((status = X400mtRecipNew(new2, X400_ORIGINATOR, &nrp)) != X400_E_NOERROR) {
265  fprintf (stderr, "Error in X400mtRecipNew: %s\n", X400mtError (status));
266  } else {
268  sizeof(tbuf), &required) == X400_E_NOERROR) {
269  tbuf[required] = '\0';
271  }
272  }
273  }
274  }
275  break;
276 
277  default:
278  /* Can't do these yet */
279  break;
280  }
281  }
282 
283  /* Done with this one now */
284  X400mtMsgDelete(new1);
285 
286  buf = (char*)malloc(INITIAL_BUFSIZE);
287  buflen = INITIAL_BUFSIZE;
288 
289  /* Now get content octets for this new message */
290  status = X400mtGetContentOctets(new2, buf, buflen, &required);
291  if ((status != X400_E_NOERROR) && (status != X400_E_NOSPACE)) {
292  fprintf (stderr, "Error in X400mtGetContentOctets: %s\n", X400mtError (status));
293  free(buf);
294  return (status);
295  }
296 
297  if (status == X400_E_NOSPACE) {
298  buf = (char *)realloc(buf, required);
299  buflen = required;
300 
301  status = X400mtGetContentOctets(new2, buf, buflen, &required);
302  if (status != X400_E_NOERROR) {
303  fprintf (stderr, "Error in X400mtGetContentOctets: %s\n", X400mtError (status));
304  free(buf);
305  return (status);
306  }
307  }
308 
309  /* Now finished wih this one */
310  X400mtMsgDelete(new2);
311 
312  /* Here we would wrap the content up somehow */
313 
314  /* And put the resulting stuff back into yet another message for transfer-in */
315  X400mtMsgNew(sp, X400_MSG_MESSAGE, &new3);
316 
317  status = X400mtMsgAddStrParam(new3, X400_S_CONTENT_STRING, buf, required);
318  free(buf);
319 
320  if (status != X400_E_NOERROR) {
321  fprintf (stderr, "Error in X400mtAddStrParam: %s\n", X400mtError (status));
322  return (status);
323  }
324 
325  /* Set up envelope attributes and transfer out to MTA */
326 
327  X400mtMsgDelete(new3);
328 
329  return X400_E_NOERROR;
330 }
#define X400_MSG_MESSAGE
Definition: x400_att.h:29
int X400mtOpen(const char *credentials, struct X400mtSession **spp)
Open a session to the MTA.
int X400mtGetContentOctets(struct X400mtMessage *mp, char *buf, size_t buflen, size_t *lenp)
Obtains the bytes containing the ASN.1 encoding of the Content of a message. The Content is obtained ...
int X400mtMsgNew(struct X400mtSession *sp, int type, struct X400mtMessage **mpp)
Creates new message.
int X400mtSetContentOctets(struct X400mtMessage *mp, char *buf, size_t len)
Takes a byte stream containing the ASN.1 encoding of a P22 (or P772) content, decodes it and sets it ...
int X400mtMsgGetStart(struct X400mtSession *sp, struct X400mtMessage **mpp, int *typep)
Get message object for transfer out from MTA.
int X400mtMsgGetFinish(struct X400mtMessage *mp, int status, int reason, int diag, const char *info)
Finish transfer-out of message from MTA, generate DR if required.
X.400 Gateway Interface.
int X400mtMsgGetIntParam(struct X400mtMessage *mp, int paramtype, int *valp)
Return a integer-valued parameter from the message object.
int X400mtMsgGetStrParam(struct X400mtMessage *mp, int paramtype, char *buffer, size_t buflen, size_t *paramlenp)
Return a string-valued parameter from the message object.
int X400mtRecipNew(struct X400mtMessage *mp, int type, struct X400Recipient **rpp)
Add new recipient to a message.
#define X400_S_CONTENT_STRING
Definition: x400_att.h:766
int X400mtMsgAddIntParam(struct X400mtMessage *mp, int paramtype, int value)
Add integer-valued parameter to the message.
#define X400_E_NOERROR
Definition: x400_att.h:46
int X400mtRecipAddStrParam(struct X400Recipient *rp, int paramtype, const char *value, size_t length)
Add string-valued parameter to the message.
#define X400_ORIGINATOR
Definition: x400_att.h:305
#define X400_S_OR_ADDRESS
Definition: x400_att.h:349
const char * X400mtError(int error)
Return string for error code.
#define X400_S_IPM_IDENTIFIER
Definition: x400_att.h:701
#define X400_N_IMPORTANCE
Definition: x400_att.h:742
int X400mtMsgAddStrParam(struct X400mtMessage *mp, int paramtype, const char *value, size_t length)
Add string-valued parameter to the message.
int X400mtClose(struct X400mtSession *sp)
Close a X400 Session.
#define X400_S_SUBJECT
Definition: x400_att.h:722
int X400mtRecipGetStrParam(struct X400Recipient *rp, int paramtype, char *buffer, size_t buflen, size_t *paramlenp)
Return a string-valued parameter from the recipient object.
int X400mtRecipGet(struct X400mtMessage *mp, int type, int number, struct X400Recipient **rpp)
Get recipient object from message.
#define X400_E_NOSPACE
Definition: x400_att.h:112
#define X400_E_NO_MESSAGE
Definition: x400_att.h:103
int X400mtMsgDelete(struct X400mtMessage *mp)
Delete message object.