]> diplodocus.org Git - nmh/blob - uip/sendsbr.c
cppflags.m4: Don't trample CFLAGS and CPPFLAGS.
[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, "%s", 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;
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 gstate = m_getfld_state_init(in);
214 m_getfld_track_filepos2(&gstate);
215 for (compnum = 1;;) {
216 int bufsz = sizeof buffer;
217 switch (state = m_getfld2(&gstate, name, buffer, &bufsz)) {
218 case FLD:
219 case FLDPLUS:
220 compnum++;
221
222 /*
223 * This header field is discarded.
224 */
225 if (!strcasecmp (name, "Message-ID")) {
226 while (state == FLDPLUS) {
227 bufsz = sizeof buffer;
228 state = m_getfld2(&gstate, name, buffer, &bufsz);
229 }
230 } else if (uprf (name, XXX_FIELD_PRF)
231 || !strcasecmp (name, VRSN_FIELD)
232 || !strcasecmp (name, "Subject")
233 || !strcasecmp (name, "Encrypted")) {
234 /*
235 * These header fields are copied to the enclosed
236 * header of the first message in the collection
237 * of message/partials. For the "Subject" header
238 * field, we also record it, so that a modified
239 * version of it, can be copied to the header
240 * of each message/partial in the collection.
241 */
242 if (!strcasecmp (name, "Subject")) {
243 size_t sublen;
244
245 strncpy (subject, buffer, BUFSIZ);
246 sublen = strlen (subject);
247 if (sublen > 0 && subject[sublen - 1] == '\n')
248 subject[sublen - 1] = '\0';
249 }
250
251 dp = add (concat (name, ":", buffer, NULL), dp);
252 while (state == FLDPLUS) {
253 bufsz = sizeof buffer;
254 state = m_getfld2(&gstate, name, buffer, &bufsz);
255 dp = add (buffer, dp);
256 }
257 } else {
258 /*
259 * These header fields are copied to the header of
260 * each message/partial in the collection.
261 */
262 cp = add (concat (name, ":", buffer, NULL), cp);
263 while (state == FLDPLUS) {
264 bufsz = sizeof buffer;
265 state = m_getfld2(&gstate, name, buffer, &bufsz);
266 cp = add (buffer, cp);
267 }
268 }
269
270 start = ftell (in) + 1;
271 continue;
272
273 case BODY:
274 case FILEEOF:
275 break;
276
277 case LENERR:
278 case FMTERR:
279 adios (NULL, "message format error in component #%d", compnum);
280
281 default:
282 adios (NULL, "getfld () returned %d", state);
283 }
284
285 break;
286 }
287 m_getfld_state_destroy (&gstate);
288 if (cp == NULL)
289 adios (NULL, "headers missing from draft");
290
291 nparts = 1;
292 pos = start;
293 while (fgets (buffer, sizeof buffer, in)) {
294 long len;
295
296 if ((pos += (len = strlen (buffer))) > CPERMSG) {
297 nparts++;
298 pos = len;
299 }
300 }
301
302 /* Only one part, nothing to split */
303 if (nparts == 1) {
304 free (cp);
305 mh_xfree(dp);
306
307 fclose (in);
308 return sendaux (vec, vecp, program, drft, st);
309 }
310
311 if (!pushsw) {
312 printf ("Sending as %d Partial Messages\n", nparts);
313 fflush (stdout);
314 }
315 status = OK;
316
317 vec[vecp++] = "-partno";
318 vec[vecp++] = partnum;
319 if (delay == 0)
320 vec[vecp++] = "-queued";
321
322 time (&clock);
323 snprintf (msgid, sizeof(msgid), "%s", message_id (clock, 0));
324
325 fseek (in, start, SEEK_SET);
326 for (partno = 1; partno <= nparts; partno++) {
327 char tmpdrf[BUFSIZ];
328 FILE *out;
329
330 char *cp = m_mktemp2(drft, invo_name, NULL, &out);
331 if (cp == NULL) {
332 adios(NULL, "unable to create temporary file in %s",
333 get_temp_dir());
334 }
335 strncpy(tmpdrf, cp, sizeof(tmpdrf));
336
337 /*
338 * Output the header fields
339 */
340 fputs (cp, out);
341 fprintf (out, "Subject: %s (part %d of %d)\n", subject, partno, nparts);
342 fprintf (out, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
343 fprintf (out, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD, msgid);
344 fprintf (out, "\tnumber=%d; total=%d\n", partno, nparts);
345 fprintf (out, "%s: part %d of %d\n\n", DESCR_FIELD, partno, nparts);
346
347 /*
348 * If this is the first in the collection, output the
349 * header fields we are encapsulating at the beginning
350 * of the body of the first message.
351 */
352 if (partno == 1) {
353 if (dp)
354 fputs (dp, out);
355 fprintf (out, "Message-ID: %s\n", msgid);
356 fprintf (out, "\n");
357 }
358
359 pos = 0;
360 for (;;) {
361 long len;
362
363 if (!fgets (buffer, sizeof buffer, in)) {
364 if (partno == nparts)
365 break;
366 adios (NULL, "premature eof");
367 }
368
369 if ((pos += (len = strlen (buffer))) > CPERMSG) {
370 fseek (in, -len, SEEK_CUR);
371 break;
372 }
373
374 fputs (buffer, out);
375 }
376
377 if (fflush (out))
378 adios (tmpdrf, "error writing to");
379
380 fclose (out);
381
382 if (!pushsw && verbsw) {
383 putchar('\n');
384 fflush (stdout);
385 }
386
387 /* Pause here, if a delay is specified */
388 if (delay > 0 && 1 < partno && partno <= nparts) {
389 if (!pushsw) {
390 printf ("pausing %d seconds before sending part %d...\n",
391 delay, partno);
392 fflush (stdout);
393 }
394 sleep ((unsigned int) delay);
395 }
396
397 snprintf (partnum, sizeof(partnum), "%d", partno);
398 status = sendaux (vec, vecp, program, tmpdrf, st);
399 (void) m_unlink (tmpdrf);
400 if (status != OK)
401 break;
402
403 /*
404 * This is so sendaux will only annotate
405 * the altmsg the first time it is called.
406 */
407 annotext = NULL;
408 }
409
410 free (cp);
411 mh_xfree(dp);
412
413 fclose (in); /* close the draft */
414 return status;
415 }
416
417
418 /*
419 * Annotate original message, and
420 * call `postproc' (which is passed down in "program") to send message.
421 */
422
423 static int
424 sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st)
425 {
426 pid_t child_id;
427 int i, status, fd, fd2;
428 char backup[BUFSIZ], buf[BUFSIZ];
429
430 fd = pushsw ? tmp_fd () : NOTOK;
431 fd2 = NOTOK;
432
433 if (annotext) {
434 if ((fd2 = tmp_fd ()) != NOTOK) {
435 vec[vecp++] = "-idanno";
436 snprintf (buf, sizeof(buf), "%d", fd2);
437 vec[vecp++] = buf;
438 } else {
439 inform("unable to create temporary file in %s for "
440 "annotation list, continuing...", get_temp_dir());
441 }
442 }
443 vec[vecp++] = drft;
444 if (distfile && distout (drft, distfile, backup) == NOTOK)
445 done (1);
446 vec[vecp] = NULL;
447
448 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
449 sleep (5);
450
451 switch (child_id) {
452 case -1:
453 /* oops -- fork error */
454 adios ("fork", "unable to");
455 break; /* NOT REACHED */
456
457 case 0:
458 /*
459 * child process -- send it
460 *
461 * If fd is OK, then we are pushing and fd points to temp
462 * file, so capture anything on stdout and stderr there.
463 */
464 if (fd != NOTOK) {
465 dup2 (fd, fileno (stdout));
466 dup2 (fd, fileno (stderr));
467 close (fd);
468 }
469 execvp (program, vec);
470 fprintf (stderr, "unable to exec ");
471 perror (postproc);
472 _exit (-1);
473
474 default:
475 /*
476 * parent process -- wait for it
477 */
478 if ((status = pidwait(child_id, NOTOK)) == OK) {
479 if (annotext && fd2 != NOTOK)
480 anno (fd2, st);
481 } else {
482 /*
483 * If postproc failed, and we have good fd (which means
484 * we pushed), then mail error message (and possibly the
485 * draft) back to the user.
486 */
487 if (fd != NOTOK) {
488 alert (drft, fd);
489 close (fd);
490 } else {
491 inform("message not delivered to anyone");
492 }
493 if (annotext && fd2 != NOTOK)
494 close (fd2);
495 if (distfile) {
496 (void) m_unlink (drft);
497 if (rename (backup, drft) == NOTOK)
498 advise (drft, "unable to rename %s to", backup);
499 }
500 }
501 break;
502 }
503
504 return status;
505 }
506
507
508 /*
509 * Mail error notification (and possibly a copy of the
510 * message) back to the user, using the mailproc
511 */
512
513 static void
514 alert (char *file, int out)
515 {
516 pid_t child_id;
517 int i, in, argp;
518 char buf[BUFSIZ];
519 char *program;
520 char **arglist;
521
522 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
523 sleep (5);
524
525 switch (child_id) {
526 case NOTOK:
527 /* oops -- fork error */
528 advise ("fork", "unable to");
529 /* FALLTHRU */
530
531 case OK:
532 /* child process -- send it */
533 SIGNAL (SIGHUP, SIG_IGN);
534 SIGNAL (SIGINT, SIG_IGN);
535 SIGNAL (SIGQUIT, SIG_IGN);
536 SIGNAL (SIGTERM, SIG_IGN);
537 if (forwsw) {
538 if ((in = open (file, O_RDONLY)) == NOTOK) {
539 admonish (file, "unable to re-open");
540 } else {
541 lseek(out, 0, SEEK_END);
542 strncpy (buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
543 if (write (out, buf, strlen (buf)) < 0) {
544 advise (file, "write");
545 }
546 strncpy (buf, "\n------- Unsent Draft\n\n", sizeof(buf));
547 if (write (out, buf, strlen (buf)) < 0) {
548 advise (file, "write");
549 }
550 cpydgst (in, out, file, "temporary file");
551 close (in);
552 strncpy (buf, "\n------- End of Unsent Draft\n", sizeof(buf));
553 if (write (out, buf, strlen (buf)) < 0) {
554 advise (file, "write");
555 }
556 if (rename (file, strncpy (buf, m_backup (file), sizeof(buf))) == NOTOK)
557 admonish (buf, "unable to rename %s to", file);
558 }
559 }
560 lseek(out, 0, SEEK_SET);
561 dup2 (out, fileno (stdin));
562 close (out);
563 /* create subject for error notification */
564 snprintf (buf, sizeof(buf), "send failed on %s",
565 forwsw ? "enclosed draft" : file);
566
567 arglist = argsplit(mailproc, &program, &argp);
568
569 arglist[argp++] = getusername();
570 arglist[argp++] = "-subject";
571 arglist[argp++] = buf;
572 arglist[argp] = NULL;
573
574 execvp (program, arglist);
575 fprintf (stderr, "unable to exec ");
576 perror (mailproc);
577 _exit (-1);
578
579 default: /* no waiting... */
580 break;
581 }
582 }
583
584
585 static int
586 tmp_fd (void)
587 {
588 int fd;
589 char *tfile;
590
591 if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) return NOTOK;
592
593 if (debugsw)
594 inform("temporary file %s selected", tfile);
595 else
596 if (m_unlink (tfile) == NOTOK)
597 advise (tfile, "unable to remove");
598
599 return fd;
600 }
601
602
603 static void
604 anno (int fd, struct stat *st)
605 {
606 pid_t child_id;
607 sigset_t set, oset;
608 static char *cwd = NULL;
609 struct stat st2;
610
611 if (altmsg &&
612 (stat (altmsg, &st2) == NOTOK
613 || st->st_mtime != st2.st_mtime
614 || st->st_dev != st2.st_dev
615 || st->st_ino != st2.st_ino)) {
616 if (debugsw)
617 inform("$mhaltmsg mismatch, continuing...");
618 return;
619 }
620
621 child_id = debugsw ? NOTOK : fork ();
622 switch (child_id) {
623 case NOTOK: /* oops */
624 if (!debugsw)
625 inform("unable to fork, so doing annotations by hand...");
626 if (cwd == NULL)
627 cwd = mh_xstrdup(pwd ());
628 /* FALLTHRU */
629
630 case OK:
631 /* block a few signals */
632 sigemptyset (&set);
633 sigaddset (&set, SIGHUP);
634 sigaddset (&set, SIGINT);
635 sigaddset (&set, SIGQUIT);
636 sigaddset (&set, SIGTERM);
637 sigprocmask (SIG_BLOCK, &set, &oset);
638
639 unregister_for_removal(0);
640
641 annoaux (fd);
642 if (child_id == OK)
643 _exit (0);
644
645 /* reset the signal mask */
646 sigprocmask (SIG_SETMASK, &oset, &set);
647
648 if (chdir (cwd) < 0) {
649 advise (cwd, "chdir");
650 }
651 break;
652
653 default: /* no waiting... */
654 close (fd);
655 break;
656 }
657 }
658
659
660 static void
661 annoaux (int fd)
662 {
663 int fd2, fd3, msgnum;
664 char *cp, *folder, *maildir;
665 char buffer[BUFSIZ], **ap;
666 FILE *fp;
667 struct msgs *mp;
668
669 if ((folder = getenv ("mhfolder")) == NULL || *folder == 0) {
670 if (debugsw)
671 inform("$mhfolder not set, continuing...");
672 return;
673 }
674 maildir = m_maildir (folder);
675 if (chdir (maildir) == NOTOK) {
676 if (debugsw)
677 admonish (maildir, "unable to change directory to");
678 return;
679 }
680 if (!(mp = folder_read (folder, 0))) {
681 if (debugsw)
682 inform("unable to read folder %s, continuing...", folder);
683 return;
684 }
685
686 /* check for empty folder */
687 if (mp->nummsg == 0) {
688 if (debugsw)
689 inform("no messages in %s, continuing...", folder);
690 goto oops;
691 }
692
693 if ((cp = getenv ("mhmessages")) == NULL || *cp == 0) {
694 if (debugsw)
695 inform("$mhmessages not set, continuing...");
696 goto oops;
697 }
698 if (!debugsw /* MOBY HACK... */
699 && pushsw
700 && (fd3 = open ("/dev/null", O_RDWR)) != NOTOK
701 && (fd2 = dup (fileno (stderr))) != NOTOK) {
702 dup2 (fd3, fileno (stderr));
703 close (fd3);
704 }
705 else
706 fd2 = NOTOK;
707 for (ap = brkstring (cp = mh_xstrdup(cp), " ", NULL); *ap; ap++)
708 m_convert (mp, *ap);
709 free (cp);
710 if (fd2 != NOTOK)
711 dup2 (fd2, fileno (stderr));
712 if (mp->numsel == 0) {
713 if (debugsw)
714 inform("no messages to annotate, continuing...");
715 goto oops;
716 }
717
718 lseek(fd, 0, SEEK_SET);
719 if ((fp = fdopen (fd, "r")) == NULL) {
720 if (debugsw)
721 inform("unable to fdopen annotation list, continuing...");
722 goto oops;
723 }
724 cp = NULL;
725 while (fgets (buffer, sizeof(buffer), fp) != NULL)
726 cp = add (buffer, cp);
727 fclose (fp);
728
729 if (debugsw)
730 inform("annotate%s with %s: \"%s\"",
731 inplace ? " inplace" : "", annotext, cp);
732 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
733 if (is_selected(mp, msgnum)) {
734 if (debugsw)
735 inform("annotate message %d", msgnum);
736 annotate (m_name (msgnum), annotext, cp, inplace, 1, -2, 0);
737 }
738 }
739
740 free (cp);
741
742 oops:
743 folder_free (mp); /* free folder/message structure */
744 }
745
746
747 static
748 void
749 handle_sendfrom(char **vec, int *vecp, char *draft, const char *auth_svc) {
750 const char *addr, *host;
751 const char *message;
752
753 /* Extract address and host from From: header line in draft. */
754 if (get_from_header_info(draft, &addr, &host, &message) != OK) {
755 adios(draft, "%s", message);
756 }
757
758 /* Merge in any address or host specific switches to post(1) from profile. */
759 merge_profile_entry(addr, host, vec, vecp);
760 free((void *) host);
761 free((void *) addr);
762
763 vec[*vecp] = NULL;
764
765 {
766 char **vp;
767
768 for (vp = vec; *vp; ++vp) {
769 if (strcmp(*vp, "xoauth2") == 0) {
770 #ifdef OAUTH_SUPPORT
771 if (setup_oauth_params(vec, vecp, auth_svc, &message) != OK) {
772 adios(NULL, "%s", message);
773 }
774 break;
775 #else
776 NMH_UNUSED(auth_svc);
777 adios(NULL, "send built without OAUTH_SUPPORT, "
778 "so -saslmech xoauth2 is not supported");
779 #endif /* OAUTH_SUPPORT */
780 }
781 }
782 }
783 }
784
785
786 #ifdef OAUTH_SUPPORT
787 /*
788 * For XOAUTH2, append profile entries so post can do the heavy lifting
789 */
790 static int
791 setup_oauth_params(char *vec[], int *vecp, const char *auth_svc,
792 const char **message) {
793 const char *saslmech = NULL, *user = NULL;
794 mh_oauth_service_info svc;
795 char errbuf[256];
796 int i;
797
798 /* Make sure we have all the information we need. */
799 for (i = 1; i < *vecp; ++i) {
800 /* Don't support abbreviated switches, to avoid collisions in the
801 future if new ones are added. */
802 if (! strcmp(vec[i-1], "-saslmech")) {
803 saslmech = vec[i];
804 } else if (! strcmp(vec[i-1], "-user")) {
805 user = vec[i];
806 } else if (! strcmp(vec[i-1], "-authservice")) {
807 auth_svc = vec[i];
808 }
809 }
810
811 if (auth_svc == NULL) {
812 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
813 *message = "must specify -authservice with -saslmech xoauth2";
814 return NOTOK;
815 }
816 } else {
817 if (user == NULL) {
818 *message = "must specify -user with -saslmech xoauth2";
819 return NOTOK;
820 }
821
822 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
823 if (! mh_oauth_get_service_info(auth_svc, &svc, errbuf,
824 sizeof(errbuf)))
825 adios(NULL, "Unable to retrieve oauth profile entries: %s",
826 errbuf);
827
828 vec[(*vecp)++] = mh_xstrdup("-authservice");
829 vec[(*vecp)++] = mh_xstrdup(auth_svc);
830 vec[(*vecp)++] = mh_xstrdup("-oauthcredfile");
831 vec[(*vecp)++] = mh_xstrdup(mh_oauth_cred_fn(auth_svc));
832 vec[(*vecp)++] = mh_xstrdup("-oauthclientid");
833 vec[(*vecp)++] = getcpy(svc.client_id);
834 vec[(*vecp)++] = mh_xstrdup("-oauthclientsecret");
835 vec[(*vecp)++] = getcpy(svc.client_secret);
836 vec[(*vecp)++] = mh_xstrdup("-oauthauthendpoint");
837 vec[(*vecp)++] = getcpy(svc.auth_endpoint);
838 vec[(*vecp)++] = mh_xstrdup("-oauthredirect");
839 vec[(*vecp)++] = getcpy(svc.redirect_uri);
840 vec[(*vecp)++] = mh_xstrdup("-oauthtokenendpoint");
841 vec[(*vecp)++] = getcpy(svc.token_endpoint);
842 vec[(*vecp)++] = mh_xstrdup("-oauthscope");
843 vec[(*vecp)++] = getcpy(svc.scope);
844 }
845 }
846
847 return 0;
848 }
849 #endif /* OAUTH_SUPPORT */
850
851
852 /*
853 * Extract user and domain from From: header line in draft.
854 */
855 static
856 int
857 get_from_header_info(const char *filename, const char **addr, const char **host, const char **message) {
858 struct stat st;
859 FILE *in;
860
861 if (stat (filename, &st) == NOTOK) {
862 *message = "unable to stat draft file";
863 return NOTOK;
864 }
865
866 if ((in = fopen (filename, "r")) != NULL) {
867 /* There must be a non-blank Envelope-From or {Resent-}Sender or
868 {Resent-}From header. */
869 char *addrformat = "%(addr{Envelope-From})";
870 char *hostformat = "%(host{Envelope-From})";
871
872 if ((*addr = get_message_header_info (in, addrformat)) == NULL ||
873 !**addr) {
874 addrformat = distfile == NULL ? "%(addr{Sender})" : "%(addr{Resent-Sender})";
875 hostformat = distfile == NULL ? "%(host{Sender})" : "%(host{Resent-Sender})";
876
877 if ((*addr = get_message_header_info (in, addrformat)) == NULL) {
878 addrformat = distfile == NULL ? "%(addr{From})" : "%(addr{Resent-From})";
879 hostformat = distfile == NULL ? "%(host{From})" : "%(host{Resent-From})";
880
881 if ((*addr = get_message_header_info (in, addrformat)) == NULL) {
882 *message = "unable to find sender address in";
883 fclose(in);
884 return NOTOK;
885 }
886 }
887 }
888
889 /* Use the hostformat that corresponds to the successful addrformat. */
890 if ((*host = get_message_header_info(in, hostformat)) == NULL) {
891 *message = "unable to find sender host";
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;
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 gstate = m_getfld_state_init(in);
933 do {
934 char name[NAMESZ], rbuf[NMH_BUFSIZ];
935 int bufsz = sizeof rbuf;
936 int state = m_getfld2(&gstate, name, rbuf, &bufsz);
937
938 switch (state) {
939 case FLD:
940 case FLDPLUS: {
941 int bucket = fmt_addcomptext(name, rbuf);
942
943 if (bucket != -1) {
944 while (state == FLDPLUS) {
945 bufsz = sizeof rbuf;
946 state = m_getfld2(&gstate, name, rbuf, &bufsz);
947 fmt_appendcomp(bucket, name, rbuf);
948 }
949 }
950
951 while (state == FLDPLUS) {
952 bufsz = sizeof rbuf;
953 state = m_getfld2(&gstate, name, rbuf, &bufsz);
954 }
955 break;
956 }
957 default:
958 parsing_header = 0;
959 }
960 } while (parsing_header);
961 m_getfld_state_destroy(&gstate);
962
963 fmt_scan(fmt, buffer, INT_MAX, dat, NULL);
964 fmt_free(fmt, 1);
965
966 /* Trim trailing newline, if any. */
967 retval = rtrim(charstring_buffer_copy((buffer)));
968 charstring_free(buffer);
969 if (*retval)
970 return retval;
971
972 free(retval);
973 return NULL;
974 }
975
976
977 /*
978 * Look in profile for entry corresponding to addr or host, and add its contents to vec.
979 *
980 * Could do some of this automatically, by looking for:
981 * 1) access-$(mbox{from}) in oauth-svc file using mh_oauth_cred_load(), which isn't
982 * static and doesn't have side effects; free the result with mh_oauth_cred_free())
983 * 2) machine $(mbox{from}) in creds
984 * If no -server passed in from profile or commandline, could use smtp.<svc>.com for gmail,
985 * but that might not generalize for other svcs.
986 */
987 static
988 void
989 merge_profile_entry(const char *addr, const char *host, char *vec[], int *vecp) {
990 char *addr_entry = concat("sendfrom-", addr, NULL);
991 char *profile_entry = context_find(addr_entry);
992
993 free(addr_entry);
994 if (profile_entry == NULL) {
995 /* No entry for the user. Look for one for the host. */
996 char *host_entry = concat("sendfrom-", host, NULL);
997
998 profile_entry = context_find(host_entry);
999 free(host_entry);
1000 }
1001
1002 /* Use argsplit() to do the real work of splitting the args in the profile entry. */
1003 if (profile_entry && *profile_entry) {
1004 int profile_vecp;
1005 char *file;
1006 char **profile_vec = argsplit(profile_entry, &file, &profile_vecp);
1007 int i;
1008
1009 for (i = 0; i < profile_vecp; ++i) {
1010 vec[(*vecp)++] = getcpy(profile_vec[i]);
1011 }
1012
1013 arglist_free(file, profile_vec);
1014 }
1015 }
1016
1017
1018 static void NORETURN
1019 armed_done (int status)
1020 {
1021 longjmp (env, status ? status : NOTOK);
1022 }