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