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