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