]> diplodocus.org Git - nmh/blob - uip/sendsbr.c
Makefile.am: Add test/inc/test-eom-align to XFAIL_TESTS.
[nmh] / uip / sendsbr.c
1 /* sendsbr.c -- routines to help WhatNow/Send along
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/fmt_scan.h>
10 #include <h/fmt_compile.h>
11 #include <h/signals.h>
12 #include <setjmp.h>
13 #include <fcntl.h>
14 #include <h/mime.h>
15 #include <h/tws.h>
16 #include <h/utils.h>
17 #include <h/mts.h>
18
19 #ifdef HAVE_SYS_TIME_H
20 # include <sys/time.h>
21 #endif
22 #include <time.h>
23
24 #ifdef OAUTH_SUPPORT
25 #include <h/oauth.h>
26 #endif
27 #include "../sbr/m_maildir.h"
28 #include "../sbr/m_mktemp.h"
29 #include "../sbr/message_id.h"
30
31 #ifdef OAUTH_SUPPORT
32 static int setup_oauth_params(char *[], int *, const char *, const char **);
33 #endif /* OAUTH_SUPPORT */
34
35 int debugsw = 0; /* global */
36 int forwsw = 1;
37 int inplace = 1;
38 int pushsw = 0;
39 int splitsw = -1;
40 int unique = 0;
41 int verbsw = 0;
42
43 char *altmsg = NULL; /* .. */
44 char *annotext = NULL;
45 char *distfile = NULL;
46
47 static jmp_buf env;
48
49 /*
50 * static prototypes
51 */
52 static void alert (char *, int);
53 static int tmp_fd (void);
54 static void anno (int, struct stat *);
55 static void annoaux (int);
56 static int splitmsg (char **, int, char *, char *, struct stat *, int);
57 static int sendaux (char **, int, char *, char *, struct stat *);
58 static void handle_sendfrom(char **, int *, char *, const char *);
59 static int get_from_header_info(const char *, const char **, const char **, const char **);
60 static const char *get_message_header_info(FILE *, char *);
61 static void merge_profile_entry(const char *, const char *, char *[], int *);
62 static void armed_done (int) NORETURN;
63
64 /*
65 * Entry point into (back-end) routines to send message.
66 */
67
68 int
69 sendsbr (char **vec, int vecp, char *program, char *draft, struct stat *st,
70 int rename_drft, const char *auth_svc)
71 {
72 int status, i;
73 pid_t child;
74 char buffer[BUFSIZ], file[BUFSIZ];
75 struct stat sts;
76 char **buildvec, *buildprogram;
77 char *volatile drft = draft;
78 /* nvecs is volatile to prevent warning from gcc about possible clobbering
79 by longjmp. */
80 volatile int nvecs = vecp;
81 int *nvecsp = (int *) &nvecs;
82
83 /*
84 * Run the mimebuildproc (which is by default mhbuild) on the message
85 * with the addition of the "-auto" flag
86 */
87
88 switch (child = fork()) {
89 case NOTOK:
90 adios("fork", "unable to");
91 break;
92
93 case OK:
94 buildvec = argsplit(buildmimeproc, &buildprogram, &i);
95 buildvec[i++] = "-auto";
96 if (distfile)
97 buildvec[i++] = "-dist";
98 buildvec[i++] = (char *) drft;
99 buildvec[i] = NULL;
100 execvp(buildprogram, buildvec);
101 fprintf(stderr, "unable to exec ");
102 perror(buildmimeproc);
103 _exit(-1);
104 break;
105
106 default:
107 if (pidXwait(child, buildmimeproc))
108 return NOTOK;
109 break;
110 }
111
112 done=armed_done;
113 switch (setjmp (env)) {
114 case OK:
115 /*
116 * If given -push and -unique (which is undocumented), then
117 * rename the draft file. I'm not quite sure why.
118 */
119 if (pushsw && unique) {
120 char *cp = m_mktemp2(drft, invo_name, NULL, NULL);
121 if (cp == NULL) {
122 adios(NULL, "unable to create temporary file in %s",
123 get_temp_dir());
124 }
125 if (rename (drft, strncpy(file, cp, sizeof(file))) == NOTOK)
126 adios (file, "unable to rename %s to", drft);
127 drft = file;
128 }
129
130 /*
131 * Add in any necessary profile entries for xoauth
132 */
133
134 if (auth_svc) {
135 #ifdef OAUTH_SUPPORT
136 const char *errmsg;
137 if (setup_oauth_params(vec, nvecsp, auth_svc, &errmsg) != OK) {
138 adios(NULL, errmsg);
139 }
140 #else
141 adios(NULL, "send built without OAUTH_SUPPORT, "
142 "so auth_svc %s is not supported", auth_svc);
143 #endif /* OAUTH_SUPPORT */
144 }
145
146 /*
147 * Rework the vec based on From: header in draft, as specified
148 * by sendfrom-address entries in profile.
149 */
150 if (context_find_prefix("sendfrom-")) {
151 handle_sendfrom(vec, nvecsp, draft, auth_svc);
152 }
153
154 /*
155 * Check if we need to split the message into
156 * multiple messages of type "message/partial".
157 */
158 if (splitsw >= 0 && !distfile && stat ((char *) drft, &sts) != NOTOK
159 && sts.st_size >= CPERMSG) {
160 status = splitmsg (vec, nvecs, program, drft,
161 st, splitsw) ? NOTOK : OK;
162 } else {
163 status = sendaux (vec, nvecs, program, drft, st) ? NOTOK : OK;
164 }
165
166 /* rename the original draft */
167 if (rename_drft && status == OK &&
168 rename (drft, strncpy (buffer, m_backup (drft),
169 sizeof(buffer))) == NOTOK)
170 advise (buffer, "unable to rename %s to", drft);
171 break;
172
173 default:
174 status = DONE;
175 break;
176 }
177
178 done=exit;
179 if (distfile)
180 (void) m_unlink (distfile);
181
182 return status;
183 }
184
185 /*
186 * Split large message into several messages of
187 * type "message/partial" and send them.
188 */
189
190 static int
191 splitmsg (char **vec, int vecp, char *program, char *drft,
192 struct stat *st, int delay)
193 {
194 int compnum, nparts, partno, state, status;
195 long pos, start;
196 time_t clock;
197 char *cp, *dp, buffer[NMH_BUFSIZ], msgid[BUFSIZ];
198 char subject[BUFSIZ];
199 char name[NAMESZ], partnum[BUFSIZ];
200 FILE *in;
201 m_getfld_state_t gstate = 0;
202
203 if ((in = fopen (drft, "r")) == NULL)
204 adios (drft, "unable to open for reading");
205
206 cp = dp = NULL;
207 start = 0L;
208
209 /*
210 * Scan through the message and examine the various header fields,
211 * as well as locate the beginning of the message body.
212 */
213 m_getfld_track_filepos (&gstate, in);
214 for (compnum = 1;;) {
215 int bufsz = sizeof buffer;
216 switch (state = m_getfld (&gstate, name, buffer, &bufsz, in)) {
217 case FLD:
218 case FLDPLUS:
219 compnum++;
220
221 /*
222 * This header field is discarded.
223 */
224 if (!strcasecmp (name, "Message-ID")) {
225 while (state == FLDPLUS) {
226 bufsz = sizeof buffer;
227 state = m_getfld (&gstate, name, buffer, &bufsz, in);
228 }
229 } else if (uprf (name, XXX_FIELD_PRF)
230 || !strcasecmp (name, VRSN_FIELD)
231 || !strcasecmp (name, "Subject")
232 || !strcasecmp (name, "Encrypted")) {
233 /*
234 * These header fields are copied to the enclosed
235 * header of the first message in the collection
236 * of message/partials. For the "Subject" header
237 * field, we also record it, so that a modified
238 * version of it, can be copied to the header
239 * of each message/partial in the collection.
240 */
241 if (!strcasecmp (name, "Subject")) {
242 size_t sublen;
243
244 strncpy (subject, buffer, BUFSIZ);
245 sublen = strlen (subject);
246 if (sublen > 0 && subject[sublen - 1] == '\n')
247 subject[sublen - 1] = '\0';
248 }
249
250 dp = add (concat (name, ":", buffer, NULL), dp);
251 while (state == FLDPLUS) {
252 bufsz = sizeof buffer;
253 state = m_getfld (&gstate, name, buffer, &bufsz, in);
254 dp = add (buffer, dp);
255 }
256 } else {
257 /*
258 * These header fields are copied to the header of
259 * each message/partial in the collection.
260 */
261 cp = add (concat (name, ":", buffer, NULL), cp);
262 while (state == FLDPLUS) {
263 bufsz = sizeof buffer;
264 state = m_getfld (&gstate, name, buffer, &bufsz, in);
265 cp = add (buffer, cp);
266 }
267 }
268
269 start = ftell (in) + 1;
270 continue;
271
272 case BODY:
273 case FILEEOF:
274 break;
275
276 case LENERR:
277 case FMTERR:
278 adios (NULL, "message format error in component #%d", compnum);
279
280 default:
281 adios (NULL, "getfld () returned %d", state);
282 }
283
284 break;
285 }
286 m_getfld_state_destroy (&gstate);
287 if (cp == NULL)
288 adios (NULL, "headers missing from draft");
289
290 nparts = 1;
291 pos = start;
292 while (fgets (buffer, sizeof buffer, in)) {
293 long len;
294
295 if ((pos += (len = strlen (buffer))) > CPERMSG) {
296 nparts++;
297 pos = len;
298 }
299 }
300
301 /* Only one part, nothing to split */
302 if (nparts == 1) {
303 free (cp);
304 mh_xfree(dp);
305
306 fclose (in);
307 return sendaux (vec, vecp, program, drft, st);
308 }
309
310 if (!pushsw) {
311 printf ("Sending as %d Partial Messages\n", nparts);
312 fflush (stdout);
313 }
314 status = OK;
315
316 vec[vecp++] = "-partno";
317 vec[vecp++] = partnum;
318 if (delay == 0)
319 vec[vecp++] = "-queued";
320
321 time (&clock);
322 snprintf (msgid, sizeof(msgid), "%s", message_id (clock, 0));
323
324 fseek (in, start, SEEK_SET);
325 for (partno = 1; partno <= nparts; partno++) {
326 char tmpdrf[BUFSIZ];
327 FILE *out;
328
329 char *cp = m_mktemp2(drft, invo_name, NULL, &out);
330 if (cp == NULL) {
331 adios(NULL, "unable to create temporary file in %s",
332 get_temp_dir());
333 }
334 strncpy(tmpdrf, cp, sizeof(tmpdrf));
335
336 /*
337 * Output the header fields
338 */
339 fputs (cp, out);
340 fprintf (out, "Subject: %s (part %d of %d)\n", subject, partno, nparts);
341 fprintf (out, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
342 fprintf (out, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD, msgid);
343 fprintf (out, "\tnumber=%d; total=%d\n", partno, nparts);
344 fprintf (out, "%s: part %d of %d\n\n", DESCR_FIELD, partno, nparts);
345
346 /*
347 * If this is the first in the collection, output the
348 * header fields we are encapsulating at the beginning
349 * of the body of the first message.
350 */
351 if (partno == 1) {
352 if (dp)
353 fputs (dp, out);
354 fprintf (out, "Message-ID: %s\n", msgid);
355 fprintf (out, "\n");
356 }
357
358 pos = 0;
359 for (;;) {
360 long len;
361
362 if (!fgets (buffer, sizeof buffer, in)) {
363 if (partno == nparts)
364 break;
365 adios (NULL, "premature eof");
366 }
367
368 if ((pos += (len = strlen (buffer))) > CPERMSG) {
369 fseek (in, -len, SEEK_CUR);
370 break;
371 }
372
373 fputs (buffer, out);
374 }
375
376 if (fflush (out))
377 adios (tmpdrf, "error writing to");
378
379 fclose (out);
380
381 if (!pushsw && verbsw) {
382 putchar('\n');
383 fflush (stdout);
384 }
385
386 /* Pause here, if a delay is specified */
387 if (delay > 0 && 1 < partno && partno <= nparts) {
388 if (!pushsw) {
389 printf ("pausing %d seconds before sending part %d...\n",
390 delay, partno);
391 fflush (stdout);
392 }
393 sleep ((unsigned int) delay);
394 }
395
396 snprintf (partnum, sizeof(partnum), "%d", partno);
397 status = sendaux (vec, vecp, program, tmpdrf, st);
398 (void) m_unlink (tmpdrf);
399 if (status != OK)
400 break;
401
402 /*
403 * This is so sendaux will only annotate
404 * the altmsg the first time it is called.
405 */
406 annotext = NULL;
407 }
408
409 free (cp);
410 mh_xfree(dp);
411
412 fclose (in); /* close the draft */
413 return status;
414 }
415
416
417 /*
418 * Annotate original message, and
419 * call `postproc' (which is passed down in "program") to send message.
420 */
421
422 static int
423 sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st)
424 {
425 pid_t child_id;
426 int i, status, fd, fd2;
427 char backup[BUFSIZ], buf[BUFSIZ];
428
429 fd = pushsw ? tmp_fd () : NOTOK;
430 fd2 = NOTOK;
431
432 if (annotext) {
433 if ((fd2 = tmp_fd ()) != NOTOK) {
434 vec[vecp++] = "-idanno";
435 snprintf (buf, sizeof(buf), "%d", fd2);
436 vec[vecp++] = buf;
437 } else {
438 inform("unable to create temporary file in %s for "
439 "annotation list, continuing...", get_temp_dir());
440 }
441 }
442 vec[vecp++] = drft;
443 if (distfile && distout (drft, distfile, backup) == NOTOK)
444 done (1);
445 vec[vecp] = NULL;
446
447 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
448 sleep (5);
449
450 switch (child_id) {
451 case -1:
452 /* oops -- fork error */
453 adios ("fork", "unable to");
454 break; /* NOT REACHED */
455
456 case 0:
457 /*
458 * child process -- send it
459 *
460 * If fd is OK, then we are pushing and fd points to temp
461 * file, so capture anything on stdout and stderr there.
462 */
463 if (fd != NOTOK) {
464 dup2 (fd, fileno (stdout));
465 dup2 (fd, fileno (stderr));
466 close (fd);
467 }
468 execvp (program, vec);
469 fprintf (stderr, "unable to exec ");
470 perror (postproc);
471 _exit (-1);
472
473 default:
474 /*
475 * parent process -- wait for it
476 */
477 if ((status = pidwait(child_id, NOTOK)) == OK) {
478 if (annotext && fd2 != NOTOK)
479 anno (fd2, st);
480 } else {
481 /*
482 * If postproc failed, and we have good fd (which means
483 * we pushed), then mail error message (and possibly the
484 * draft) back to the user.
485 */
486 if (fd != NOTOK) {
487 alert (drft, fd);
488 close (fd);
489 } else {
490 inform("message not delivered to anyone");
491 }
492 if (annotext && fd2 != NOTOK)
493 close (fd2);
494 if (distfile) {
495 (void) m_unlink (drft);
496 if (rename (backup, drft) == NOTOK)
497 advise (drft, "unable to rename %s to", backup);
498 }
499 }
500 break;
501 }
502
503 return status;
504 }
505
506
507 /*
508 * Mail error notification (and possibly a copy of the
509 * message) back to the user, using the mailproc
510 */
511
512 static void
513 alert (char *file, int out)
514 {
515 pid_t child_id;
516 int i, in, argp;
517 char buf[BUFSIZ];
518 char *program;
519 char **arglist;
520
521 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
522 sleep (5);
523
524 switch (child_id) {
525 case NOTOK:
526 /* oops -- fork error */
527 advise ("fork", "unable to");
528 /* FALLTHRU */
529
530 case OK:
531 /* child process -- send it */
532 SIGNAL (SIGHUP, SIG_IGN);
533 SIGNAL (SIGINT, SIG_IGN);
534 SIGNAL (SIGQUIT, SIG_IGN);
535 SIGNAL (SIGTERM, SIG_IGN);
536 if (forwsw) {
537 if ((in = open (file, O_RDONLY)) == NOTOK) {
538 admonish (file, "unable to re-open");
539 } else {
540 lseek(out, 0, SEEK_END);
541 strncpy (buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
542 if (write (out, buf, strlen (buf)) < 0) {
543 advise (file, "write");
544 }
545 strncpy (buf, "\n------- Unsent Draft\n\n", sizeof(buf));
546 if (write (out, buf, strlen (buf)) < 0) {
547 advise (file, "write");
548 }
549 cpydgst (in, out, file, "temporary file");
550 close (in);
551 strncpy (buf, "\n------- End of Unsent Draft\n", sizeof(buf));
552 if (write (out, buf, strlen (buf)) < 0) {
553 advise (file, "write");
554 }
555 if (rename (file, strncpy (buf, m_backup (file), sizeof(buf))) == NOTOK)
556 admonish (buf, "unable to rename %s to", file);
557 }
558 }
559 lseek(out, 0, SEEK_SET);
560 dup2 (out, fileno (stdin));
561 close (out);
562 /* create subject for error notification */
563 snprintf (buf, sizeof(buf), "send failed on %s",
564 forwsw ? "enclosed draft" : file);
565
566 arglist = argsplit(mailproc, &program, &argp);
567
568 arglist[argp++] = getusername();
569 arglist[argp++] = "-subject";
570 arglist[argp++] = buf;
571 arglist[argp] = NULL;
572
573 execvp (program, arglist);
574 fprintf (stderr, "unable to exec ");
575 perror (mailproc);
576 _exit (-1);
577
578 default: /* no waiting... */
579 break;
580 }
581 }
582
583
584 static int
585 tmp_fd (void)
586 {
587 int fd;
588 char *tfile;
589
590 if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) return NOTOK;
591
592 if (debugsw)
593 inform("temporary file %s selected", tfile);
594 else
595 if (m_unlink (tfile) == NOTOK)
596 advise (tfile, "unable to remove");
597
598 return fd;
599 }
600
601
602 static void
603 anno (int fd, struct stat *st)
604 {
605 pid_t child_id;
606 sigset_t set, oset;
607 static char *cwd = NULL;
608 struct stat st2;
609
610 if (altmsg &&
611 (stat (altmsg, &st2) == NOTOK
612 || st->st_mtime != st2.st_mtime
613 || st->st_dev != st2.st_dev
614 || st->st_ino != st2.st_ino)) {
615 if (debugsw)
616 inform("$mhaltmsg mismatch, continuing...");
617 return;
618 }
619
620 child_id = debugsw ? NOTOK : fork ();
621 switch (child_id) {
622 case NOTOK: /* oops */
623 if (!debugsw)
624 inform("unable to fork, so doing annotations by hand...");
625 if (cwd == NULL)
626 cwd = mh_xstrdup(pwd ());
627 /* FALLTHRU */
628
629 case OK:
630 /* block a few signals */
631 sigemptyset (&set);
632 sigaddset (&set, SIGHUP);
633 sigaddset (&set, SIGINT);
634 sigaddset (&set, SIGQUIT);
635 sigaddset (&set, SIGTERM);
636 sigprocmask (SIG_BLOCK, &set, &oset);
637
638 unregister_for_removal(0);
639
640 annoaux (fd);
641 if (child_id == OK)
642 _exit (0);
643
644 /* reset the signal mask */
645 sigprocmask (SIG_SETMASK, &oset, &set);
646
647 if (chdir (cwd) < 0) {
648 advise (cwd, "chdir");
649 }
650 break;
651
652 default: /* no waiting... */
653 close (fd);
654 break;
655 }
656 }
657
658
659 static void
660 annoaux (int fd)
661 {
662 int fd2, fd3, msgnum;
663 char *cp, *folder, *maildir;
664 char buffer[BUFSIZ], **ap;
665 FILE *fp;
666 struct msgs *mp;
667
668 if ((folder = getenv ("mhfolder")) == NULL || *folder == 0) {
669 if (debugsw)
670 inform("$mhfolder not set, continuing...");
671 return;
672 }
673 maildir = m_maildir (folder);
674 if (chdir (maildir) == NOTOK) {
675 if (debugsw)
676 admonish (maildir, "unable to change directory to");
677 return;
678 }
679 if (!(mp = folder_read (folder, 0))) {
680 if (debugsw)
681 inform("unable to read folder %s, continuing...", folder);
682 return;
683 }
684
685 /* check for empty folder */
686 if (mp->nummsg == 0) {
687 if (debugsw)
688 inform("no messages in %s, continuing...", folder);
689 goto oops;
690 }
691
692 if ((cp = getenv ("mhmessages")) == NULL || *cp == 0) {
693 if (debugsw)
694 inform("$mhmessages not set, continuing...");
695 goto oops;
696 }
697 if (!debugsw /* MOBY HACK... */
698 && pushsw
699 && (fd3 = open ("/dev/null", O_RDWR)) != NOTOK
700 && (fd2 = dup (fileno (stderr))) != NOTOK) {
701 dup2 (fd3, fileno (stderr));
702 close (fd3);
703 }
704 else
705 fd2 = NOTOK;
706 for (ap = brkstring (cp = mh_xstrdup(cp), " ", NULL); *ap; ap++)
707 m_convert (mp, *ap);
708 free (cp);
709 if (fd2 != NOTOK)
710 dup2 (fd2, fileno (stderr));
711 if (mp->numsel == 0) {
712 if (debugsw)
713 inform("no messages to annotate, continuing...");
714 goto oops;
715 }
716
717 lseek(fd, 0, SEEK_SET);
718 if ((fp = fdopen (fd, "r")) == NULL) {
719 if (debugsw)
720 inform("unable to fdopen annotation list, continuing...");
721 goto oops;
722 }
723 cp = NULL;
724 while (fgets (buffer, sizeof(buffer), fp) != NULL)
725 cp = add (buffer, cp);
726 fclose (fp);
727
728 if (debugsw)
729 inform("annotate%s with %s: \"%s\"",
730 inplace ? " inplace" : "", annotext, cp);
731 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
732 if (is_selected(mp, msgnum)) {
733 if (debugsw)
734 inform("annotate message %d", msgnum);
735 annotate (m_name (msgnum), annotext, cp, inplace, 1, -2, 0);
736 }
737 }
738
739 free (cp);
740
741 oops:
742 folder_free (mp); /* free folder/message structure */
743 }
744
745
746 static
747 void
748 handle_sendfrom(char **vec, int *vecp, char *draft, const char *auth_svc) {
749 const char *addr, *host;
750 const char *message;
751
752 /* Extract address and host from From: header line in draft. */
753 if (get_from_header_info(draft, &addr, &host, &message) != OK) {
754 adios(draft, message);
755 }
756
757 /* Merge in any address or host specific switches to post(1) from profile. */
758 merge_profile_entry(addr, host, vec, vecp);
759 free((void *) host);
760 free((void *) addr);
761
762 vec[*vecp] = NULL;
763
764 {
765 char **vp;
766
767 for (vp = vec; *vp; ++vp) {
768 if (strcmp(*vp, "xoauth2") == 0) {
769 #ifdef OAUTH_SUPPORT
770 if (setup_oauth_params(vec, vecp, auth_svc, &message) != OK) {
771 adios(NULL, message);
772 }
773 break;
774 #else
775 NMH_UNUSED(auth_svc);
776 adios(NULL, "send built without OAUTH_SUPPORT, "
777 "so -saslmech xoauth2 is not supported");
778 #endif /* OAUTH_SUPPORT */
779 }
780 }
781 }
782 }
783
784
785 #ifdef OAUTH_SUPPORT
786 /*
787 * For XOAUTH2, append profile entries so post can do the heavy lifting
788 */
789 static int
790 setup_oauth_params(char *vec[], int *vecp, const char *auth_svc,
791 const char **message) {
792 const char *saslmech = NULL, *user = NULL;
793 mh_oauth_service_info svc;
794 char errbuf[256];
795 int i;
796
797 /* Make sure we have all the information we need. */
798 for (i = 1; i < *vecp; ++i) {
799 /* Don't support abbreviated switches, to avoid collisions in the
800 future if new ones are added. */
801 if (! strcmp(vec[i-1], "-saslmech")) {
802 saslmech = vec[i];
803 } else if (! strcmp(vec[i-1], "-user")) {
804 user = vec[i];
805 } else if (! strcmp(vec[i-1], "-authservice")) {
806 auth_svc = vec[i];
807 }
808 }
809
810 if (auth_svc == NULL) {
811 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
812 *message = "must specify -authservice with -saslmech xoauth2";
813 return NOTOK;
814 }
815 } else {
816 if (user == NULL) {
817 *message = "must specify -user with -saslmech xoauth2";
818 return NOTOK;
819 }
820
821 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
822 if (! mh_oauth_get_service_info(auth_svc, &svc, errbuf,
823 sizeof(errbuf)))
824 adios(NULL, "Unable to retrieve oauth profile entries: %s",
825 errbuf);
826
827 vec[(*vecp)++] = mh_xstrdup("-authservice");
828 vec[(*vecp)++] = mh_xstrdup(auth_svc);
829 vec[(*vecp)++] = mh_xstrdup("-oauthcredfile");
830 vec[(*vecp)++] = mh_xstrdup(mh_oauth_cred_fn(auth_svc));
831 vec[(*vecp)++] = mh_xstrdup("-oauthclientid");
832 vec[(*vecp)++] = getcpy(svc.client_id);
833 vec[(*vecp)++] = mh_xstrdup("-oauthclientsecret");
834 vec[(*vecp)++] = getcpy(svc.client_secret);
835 vec[(*vecp)++] = mh_xstrdup("-oauthauthendpoint");
836 vec[(*vecp)++] = getcpy(svc.auth_endpoint);
837 vec[(*vecp)++] = mh_xstrdup("-oauthredirect");
838 vec[(*vecp)++] = getcpy(svc.redirect_uri);
839 vec[(*vecp)++] = mh_xstrdup("-oauthtokenendpoint");
840 vec[(*vecp)++] = getcpy(svc.token_endpoint);
841 vec[(*vecp)++] = mh_xstrdup("-oauthscope");
842 vec[(*vecp)++] = getcpy(svc.scope);
843 }
844 }
845
846 return 0;
847 }
848 #endif /* OAUTH_SUPPORT */
849
850
851 /*
852 * Extract user and domain from From: header line in draft.
853 */
854 static
855 int
856 get_from_header_info(const char *filename, const char **addr, const char **host, const char **message) {
857 struct stat st;
858 FILE *in;
859
860 if (stat (filename, &st) == NOTOK) {
861 *message = "unable to stat draft file";
862 return NOTOK;
863 }
864
865 if ((in = fopen (filename, "r")) != NULL) {
866 /* There must be a non-blank Envelope-From or {Resent-}Sender or
867 {Resent-}From header. */
868 char *addrformat = "%(addr{Envelope-From})";
869 char *hostformat = "%(host{Envelope-From})";
870
871 if ((*addr = get_message_header_info (in, addrformat)) == NULL ||
872 !**addr) {
873 addrformat = distfile == NULL ? "%(addr{Sender})" : "%(addr{Resent-Sender})";
874 hostformat = distfile == NULL ? "%(host{Sender})" : "%(host{Resent-Sender})";
875
876 if ((*addr = get_message_header_info (in, addrformat)) == NULL) {
877 addrformat = distfile == NULL ? "%(addr{From})" : "%(addr{Resent-From})";
878 hostformat = distfile == NULL ? "%(host{From})" : "%(host{Resent-From})";
879
880 if ((*addr = get_message_header_info (in, addrformat)) == NULL) {
881 *message = "unable to find sender address in";
882 fclose(in);
883 return NOTOK;
884 }
885 }
886 }
887
888 /* Use the hostformat that corresponds to the successful addrformat. */
889 if ((*host = get_message_header_info(in, hostformat)) == NULL) {
890 fclose(in);
891 *message = "unable to find sender host in";
892 fclose(in);
893 return NOTOK;
894 }
895 fclose(in);
896
897 return OK;
898 }
899
900 *message = "unable to open";
901 return NOTOK;
902 }
903
904
905 /*
906 * Get formatted information from header of a message.
907 * Adapted from process_single_file() in uip/fmttest.c.
908 */
909 static
910 const char *
911 get_message_header_info(FILE *in, char *format) {
912 int dat[5];
913 struct format *fmt;
914 struct stat st;
915 int parsing_header;
916 m_getfld_state_t gstate = 0;
917 charstring_t buffer = charstring_create(0);
918 char *retval;
919
920 dat[0] = dat[1] = dat[4] = 0;
921 dat[2] = fstat(fileno(in), &st) == 0 ? st.st_size : 0;
922 dat[3] = INT_MAX;
923
924 (void) fmt_compile(new_fs(NULL, NULL, format), &fmt, 1);
925 free_fs();
926
927 /*
928 * Read in the message and process the header.
929 */
930 rewind (in);
931 parsing_header = 1;
932 do {
933 char name[NAMESZ], rbuf[NMH_BUFSIZ];
934 int bufsz = sizeof rbuf;
935 int state = m_getfld(&gstate, name, rbuf, &bufsz, in);
936
937 switch (state) {
938 case FLD:
939 case FLDPLUS: {
940 int bucket = fmt_addcomptext(name, rbuf);
941
942 if (bucket != -1) {
943 while (state == FLDPLUS) {
944 bufsz = sizeof rbuf;
945 state = m_getfld(&gstate, name, rbuf, &bufsz, in);
946 fmt_appendcomp(bucket, name, rbuf);
947 }
948 }
949
950 while (state == FLDPLUS) {
951 bufsz = sizeof rbuf;
952 state = m_getfld(&gstate, name, rbuf, &bufsz, in);
953 }
954 break;
955 }
956 default:
957 parsing_header = 0;
958 }
959 } while (parsing_header);
960 m_getfld_state_destroy(&gstate);
961
962 fmt_scan(fmt, buffer, INT_MAX, dat, NULL);
963 fmt_free(fmt, 1);
964
965 /* Trim trailing newline, if any. */
966 retval = rtrim(charstring_buffer_copy((buffer)));
967 charstring_free(buffer);
968 if (*retval)
969 return retval;
970
971 free(retval);
972 return NULL;
973 }
974
975
976 /*
977 * Look in profile for entry corresponding to addr or host, and add its contents to vec.
978 *
979 * Could do some of this automatically, by looking for:
980 * 1) access-$(mbox{from}) in oauth-svc file using mh_oauth_cred_load(), which isn't
981 * static and doesn't have side effects; free the result with mh_oauth_cred_free())
982 * 2) machine $(mbox{from}) in creds
983 * If no -server passed in from profile or commandline, could use smtp.<svc>.com for gmail,
984 * but that might not generalize for other svcs.
985 */
986 static
987 void
988 merge_profile_entry(const char *addr, const char *host, char *vec[], int *vecp) {
989 char *addr_entry = concat("sendfrom-", addr, NULL);
990 char *profile_entry = context_find(addr_entry);
991
992 free(addr_entry);
993 if (profile_entry == NULL) {
994 /* No entry for the user. Look for one for the host. */
995 char *host_entry = concat("sendfrom-", host, NULL);
996
997 profile_entry = context_find(host_entry);
998 free(host_entry);
999 }
1000
1001 /* Use argsplit() to do the real work of splitting the args in the profile entry. */
1002 if (profile_entry && *profile_entry) {
1003 int profile_vecp;
1004 char *file;
1005 char **profile_vec = argsplit(profile_entry, &file, &profile_vecp);
1006 int i;
1007
1008 for (i = 0; i < profile_vecp; ++i) {
1009 vec[(*vecp)++] = getcpy(profile_vec[i]);
1010 }
1011
1012 arglist_free(file, profile_vec);
1013 }
1014 }
1015
1016
1017 static void NORETURN
1018 armed_done (int status)
1019 {
1020 longjmp (env, status ? status : NOTOK);
1021 }