3 * inc.c -- incorporate messages from a maildrop into a folder
9 /* Revised: Sat Apr 14 17:08:17 PDT 1990 (marvit@hplabs)
10 * Added hpux hacks to set and reset gid to be "mail" as needed. The reset
11 * is necessary so inc'ed mail is the group of the inc'er, rather than
12 * "mail". We setgid to egid only when [un]locking the mail file. This
13 * is also a major security precaution which will not be explained here.
15 * Fri Feb 7 16:04:57 PST 1992 John Romine <bug-mh@ics.uci.edu>
16 * NB: I'm not 100% sure that this setgid stuff is secure even now.
24 # include <h/dropsbr.h>
25 # include <h/popsbr.h>
32 #include <h/fmt_scan.h>
33 #include <h/scansbr.h>
34 #include <h/signals.h>
35 #include <zotnet/tws/tws.h>
36 #include <zotnet/mts/mts.h>
41 # define POPminc(a) (a)
47 # define RPOPminc(a) (a)
49 # define RPOPminc(a) 0
53 # define APOPminc(a) (a)
55 # define APOPminc(a) 0
58 static struct swit switches[] = {
60 { "audit audit-file", 0 },
70 { "form formatfile", 0 },
72 { "format string", 5 },
74 { "host hostname", POPminc (-4) },
76 { "user username", POPminc (-4) },
78 { "pack file", POPminc (-4) },
80 { "nopack", POPminc (-6) },
82 { "apop", APOPminc (-4) },
84 { "noapop", APOPminc (-6) },
86 { "rpop", RPOPminc (-4) },
88 { "norpop", RPOPminc (-6) },
98 { "width columns", 0 },
111 * flags for the mail source
117 static int snoop = 0;
120 extern char response[];
122 static char *packfile = NULL;
128 static int mbx_style = MMDF_FORMAT;
129 static int pd = NOTOK;
130 static FILE *pf = NULL;
135 * For setting and returning to "mail" gid
138 static int return_gid;
144 char *map_name(char *);
148 static int pop_action(char *);
149 static int pop_pack(char *);
150 static int map_count(void);
155 main (int argc, char **argv)
157 int chgflag = 1, trnflag = 1;
158 int noisy = 1, width = 0, locked = 0;
159 int rpop, i, hghnum, msgnum;
160 char *cp, *maildir, *folder = NULL;
161 char *format = NULL, *form = NULL;
162 char *newmail, *host = NULL, *user = NULL;
163 char *audfile = NULL, *from = NULL;
164 char buf[BUFSIZ], **argp, *nfs, **arguments;
167 FILE *in, *aud = NULL;
170 int nmsgs, nbytes, p = 0;
179 struct hes_postoffice *po;
184 setlocale(LC_ALL, "");
186 invo_name = r1bindex (argv[0], '/');
188 /* read user profile/context */
191 mts_init (invo_name);
192 arguments = getarguments (invo_name, argc, argv, 1);
199 * use MAILHOST environment variable if present,
201 * If that fails, use the default (if any)
202 * provided by mts.conf in mts_init()
204 if ((tmphost = getenv("MAILHOST")) != NULL)
206 else if ((po = hes_getmailhost(getusername())) != NULL &&
207 strcmp(po->po_type, "POP") == 0)
208 pophost = po->po_host;
211 * If there is a valid "pophost" entry in mts.conf,
212 * then use it as the default host.
214 if (pophost && *pophost)
217 if ((cp = getenv ("MHPOPDEBUG")) && *cp)
227 while ((cp = *argp++)) {
229 switch (smatch (++cp, switches)) {
231 ambigsw (cp, switches);
234 adios (NULL, "-%s unknown", cp);
237 snprintf (buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
238 print_help (buf, switches, 1);
241 print_version(invo_name);
245 if (!(cp = *argp++) || *cp == '-')
246 adios (NULL, "missing argument to %s", argp[-2]);
247 audfile = getcpy (m_maildir (cp));
261 * The flag `trnflag' has the value:
263 * 2 if -truncate is given
264 * 1 by default (truncating is default)
265 * 0 if -notruncate is given
275 if (!(cp = *argp++) || *cp == '-')
276 adios (NULL, "missing argument to %s", argp[-2]);
277 from = path (cp, TFILE);
280 * If the truncate file is in default state,
281 * change to not truncate.
295 if (!(form = *argp++) || *form == '-')
296 adios (NULL, "missing argument to %s", argp[-2]);
300 if (!(format = *argp++) || *format == '-')
301 adios (NULL, "missing argument to %s", argp[-2]);
306 if (!(cp = *argp++) || *cp == '-')
307 adios (NULL, "missing argument to %s", argp[-2]);
312 if (!(host = *argp++) || *host == '-')
313 adios (NULL, "missing argument to %s", argp[-2]);
316 if (!(user = *argp++) || *user == '-')
317 adios (NULL, "missing argument to %s", argp[-2]);
322 if (!(cp = *argp++) || *cp == '-')
323 adios (NULL, "missing argument to %s", argp[-2]);
325 if (!(packfile = *argp++) || *packfile == '-')
326 adios (NULL, "missing argument to %s", argp[-2]);
354 if (*cp == '+' || *cp == '@') {
356 adios (NULL, "only one folder at a time!");
358 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
360 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
365 return_gid = getegid(); /* Save effective gid, assuming we'll use it */
366 setgid(getgid()); /* Turn off extraordinary privileges */
367 #endif /* MAILGROUP */
372 if (from || !host || rpop <= 0)
377 * Where are we getting the new mail?
390 * Are we getting the mail from
393 if (inc_type == INC_POP) {
395 user = getusername ();
397 pass = getusername ();
399 ruserpass (host, &user, &pass);
402 * initialize POP connection
404 if (pop_init (host, user, pass, snoop, rpop) == NOTOK)
405 adios (NULL, "%s", response);
407 /* Check if there are any messages */
408 if (pop_stat (&nmsgs, &nbytes) == NOTOK)
409 adios (NULL, "%s", response);
415 adios (NULL, "no mail to incorporate");
421 * We will get the mail from a file
422 * (typically the standard maildrop)
425 if (inc_type == INC_FILE) {
428 else if ((newmail = getenv ("MAILDROP")) && *newmail)
429 newmail = m_mailpath (newmail);
430 else if ((newmail = context_find ("maildrop")) && *newmail)
431 newmail = m_mailpath (newmail);
433 newmail = concat (MAILDIR, "/", MAILFIL, NULL);
435 if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
436 adios (NULL, "no mail to incorporate");
440 /* skip the folder setup */
441 if ((inc_type == INC_POP) && packfile)
445 if (!context_find ("path"))
446 free (path ("./", TFOLDER));
448 folder = getfolder (0);
449 maildir = m_maildir (folder);
451 if (stat (maildir, &st) == NOTOK) {
453 adios (maildir, "error on folder");
454 cp = concat ("Create folder \"", maildir, "\"? ", NULL);
455 if (noisy && !getanswer (cp))
458 if (!makedir (maildir))
459 adios (NULL, "unable to create folder %s", maildir);
462 if (chdir (maildir) == NOTOK)
463 adios (maildir, "unable to change directory to");
465 /* read folder and create message structure */
466 if (!(mp = folder_read (folder)))
467 adios (NULL, "unable to read folder %s", folder);
473 if (inc_type == INC_FILE) {
474 if (access (newmail, W_OK) != NOTOK) {
477 SIGNAL (SIGHUP, SIG_IGN);
478 SIGNAL (SIGINT, SIG_IGN);
479 SIGNAL (SIGQUIT, SIG_IGN);
480 SIGNAL (SIGTERM, SIG_IGN);
484 setgid(return_gid); /* Reset gid to lock mail file */
485 #endif /* MAILGROUP */
487 /* lock and fopen the mail spool */
488 if ((in = lkfopen (newmail, "r")) == NULL)
489 adios (NULL, "unable to lock and fopen %s", newmail);
492 setgid(getgid()); /* Return us to normal privileges */
493 #endif /* MAILGROUP */
494 fstat (fileno(in), &s1);
497 if ((in = fopen (newmail, "r")) == NULL)
498 adios (newmail, "unable to read");
503 setgid(getgid()); /* Return us to normal privileges */
504 #endif /* MAILGROUP */
507 if ((i = stat (audfile, &st)) == NOTOK)
508 advise (NULL, "Creating Receive-Audit: %s", audfile);
509 if ((aud = fopen (audfile, "a")) == NULL)
510 adios (audfile, "unable to append to");
512 chmod (audfile, m_gmprot ());
515 fprintf (aud, from ? "<<inc>> %s -ms %s\n"
516 : host ? "<<inc>> %s -host %s -user %s%s\n"
518 dtimenow (0), from ? from : host, user,
519 rpop < 0 ? " -apop" : rpop > 0 ? " -rpop" : "");
521 fprintf (aud, from ? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
527 if (context_find ("mhe")) {
528 cp = concat (maildir, "/++", NULL);
530 if ((mhe = fopen (cp, "a")) == NULL)
531 admonish (cp, "unable to append to");
534 chmod (cp, m_gmprot ());
539 /* Get new format string */
540 nfs = new_fs (form, format, FORMAT);
543 printf ("Incorporating new mail into %s...\n\n", folder);
549 * Get the mail from a POP server
551 if (inc_type == INC_POP) {
553 packfile = path (packfile, TFILE);
554 if (stat (packfile, &st) == NOTOK) {
556 adios (packfile, "error on file");
557 cp = concat ("Create file \"", packfile, "\"? ", NULL);
558 if (noisy && !getanswer (cp))
562 msgnum = map_count ();
563 if ((pd = mbx_open (packfile, mbx_style, getuid(), getgid(), m_gmprot()))
565 adios (packfile, "unable to open");
566 if ((pf = fdopen (pd, "w+")) == NULL)
567 adios (NULL, "unable to fdopen %s", packfile);
569 hghnum = msgnum = mp->hghmsg;
571 * Check if we have enough message space for all the new
572 * messages. If not, then realloc the folder and add enough
573 * space for all new messages plus 10 additional slots.
575 if (mp->hghmsg + nmsgs >= mp->hghoff
576 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + nmsgs + 10)))
577 adios (NULL, "unable to allocate folder storage");
580 for (i = 1; i <= nmsgs; i++) {
583 fseek (pf, 0L, SEEK_CUR);
586 fwrite (mmdlm1, 1, strlen (mmdlm1), pf);
589 if (pop_retr (i, pop_pack) == NOTOK)
590 adios (NULL, "%s", response);
592 fseek (pf, 0L, SEEK_CUR);
595 adios (packfile, "write error on");
596 fseek (pf, start, SEEK_SET);
598 cp = getcpy (m_name (msgnum));
599 if ((pf = fopen (cp, "w+")) == NULL)
600 adios (cp, "unable to write");
601 chmod (cp, m_gmprot ());
604 if (pop_retr (i, pop_action) == NOTOK)
605 adios (NULL, "%s", response);
608 adios (cp, "write error on");
609 fseek (pf, 0L, SEEK_SET);
611 switch (p = scan (pf, msgnum, 0, nfs, width,
612 packfile ? 0 : msgnum == mp->hghmsg + 1 && chgflag,
613 1, NULL, stop - start, noisy)) {
615 printf ("%*d empty\n", DMAXFOLDER, msgnum);
621 /* advise (cp, "unable to read"); already advised */
640 clear_msg_flags (mp, msgnum);
641 set_exists (mp, msgnum);
642 set_unseen (mp, msgnum);
643 mp->msgflags |= SEQMOD;
648 fseek (pf, stop, SEEK_SET);
649 fwrite (mmdlm2, 1, strlen (mmdlm2), pf);
650 if (fflush (pf) || ferror (pf)) {
654 adios (packfile, "write error on");
656 map_write (packfile, pd, 0, 0L, start, stop, pos, size, noisy);
658 if (ferror(pf) || fclose (pf)) {
663 adios (cp, "write error on");
668 if (trnflag && pop_dele (i) == NOTOK)
669 adios (NULL, "%s", response);
672 if (pop_quit () == NOTOK)
673 adios (NULL, "%s", response);
675 mbx_close (packfile, pd);
682 * Get the mail from file (usually mail spool)
684 if (inc_type == INC_FILE) {
685 m_unknown (in); /* the MAGIC invocation... */
686 hghnum = msgnum = mp->hghmsg;
689 * Check if we need to allocate more space for message status.
690 * If so, then add space for an additional 100 messages.
692 if (msgnum >= mp->hghoff
693 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghoff + 100))) {
694 advise (NULL, "unable to allocate folder storage");
700 /* copy file from spool to tmp file */
701 tmpfilenam = m_scratch ("", invo_name);
702 if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK)
703 adios (tmpfilenam, "unable to create");
704 chmod (tmpfilenam, m_gmprot ());
705 if (!(in2 = fdopen (fd, "r+")))
706 adios (tmpfilenam, "unable to access");
709 /* link message into folder */
710 newmsg = folder_addmsg(mp, tmpfilenam);
713 /* create scanline for new message */
714 switch (i = scan (in, msgnum + 1, msgnum + 1, nfs, width,
715 msgnum == hghnum && chgflag, 1, NULL, 0L, noisy)) {
722 fputs ("inc aborted!\n", aud);
723 advise (NULL, "aborted!"); /* doesn't clean up locks! */
727 advise (NULL, "BUG in %s, number out of range", invo_name);
731 advise (NULL, "BUG in %s, scan() botch (%d)", invo_name, i);
747 clear_msg_flags (mp, msgnum);
748 set_exists (mp, msgnum);
749 set_unseen (mp, msgnum);
750 mp->msgflags |= SEQMOD;
758 if (p < 0) { /* error */
760 if (i < 0) { /* error */
764 /* Be sure we can unlock mail file */
766 #endif /* MAILGROUP */
768 lkfclose (in, newmail);
771 /* And then return us to normal privileges */
773 #endif /* MAILGROUP */
777 adios (NULL, "failed");
792 if ((inc_type == INC_POP) && packfile)
797 * truncate file we are incorporating from
799 if (inc_type == INC_FILE) {
801 if (stat (newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
802 advise (NULL, "new messages have arrived!\007");
804 if ((i = creat (newmail, 0600)) != NOTOK)
807 admonish (newmail, "error zero'ing");
808 unlink(map_name(newmail));
812 printf ("%s not zero'd\n", newmail);
816 if (msgnum == hghnum) {
817 admonish (NULL, "no messages incorporated");
819 context_replace (pfolder, folder); /* update current folder */
821 mp->curmsg = hghnum + 1;
825 if (chgflag) /* sigh... */
826 seq_setcur (mp, mp->curmsg);
830 * unlock the mail spool
832 if (inc_type == INC_FILE) {
835 setgid(return_gid); /* Be sure we can unlock mail file */
836 #endif /* MAILGROUP */
838 lkfclose (in, newmail);
841 setgid(getgid()); /* And then return us to normal privileges */
842 #endif /* MAILGROUP */
848 seq_setunseen (mp, 0); /* set the Unseen-Sequence */
849 seq_save (mp); /* synchronize sequences */
850 context_save (); /* save the context file */
858 * Copy message message from spool into
859 * temporary file. Massage the "From " line
863 cpymsg (FILE *in, FILE *out)
866 char *tmpbuf, name[NAMESZ];
869 state = m_getfld (state, name, tmpbuf, rlwidth, in);
892 if (packfile && pd != NOTOK)
893 mbx_close (packfile, pd);
901 fprintf (pf, "%s\n", s);
902 stop += strlen (s) + 1;
911 snprintf (buffer, sizeof(buffer), "%s\n", s);
912 for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
914 for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
917 size += strlen (buffer) + 1;
928 if (stat (packfile, &st) == NOTOK)
930 if ((md = open (cp = map_name (packfile), O_RDONLY)) == NOTOK
931 || map_chk (cp, md, &d, (long) st.st_size, 1)) {