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