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