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