10 date 94.04.21.18.22.06; author jromine; state Exp;
15 date 94.04.21.18.20.50; author jromine; state Exp;
20 date 93.09.01.21.41.48; author jromine; state Exp;
25 date 93.08.20.15.50.13; author jromine; state Exp;
30 date 92.12.15.00.20.22; author jromine; state Exp;
35 date 92.11.04.00.43.53; author jromine; state Exp;
40 date 92.10.16.16.51.35; author jromine; state Exp;
45 date 92.10.15.15.47.32; author jromine; state Exp;
50 date 92.05.12.21.56.01; author jromine; state Exp;
55 date 92.02.11.00.37.53; author jromine; state Exp;
60 date 92.02.10.19.20.02; author jromine; state Exp;
65 date 92.02.08.00.06.14; author jromine; state Exp;
70 date 92.02.08.00.04.32; author jromine; state Exp;
75 date 92.02.05.07.26.30; author jromine; state Exp;
80 date 92.02.04.21.59.06; author jromine; state Exp;
85 date 92.02.04.19.25.38; author jromine; state Exp;
90 date 92.02.04.00.03.05; author jromine; state Exp;
95 date 92.02.04.00.02.47; author jromine; state Exp;
100 date 92.01.31.22.49.20; author jromine; state Exp;
105 date 92.01.31.22.11.32; author jromine; state Exp;
110 date 90.04.05.14.57.51; author sources; state Exp;
115 date 90.02.06.13.19.35; author sources; state Exp;
120 date 89.11.17.16.11.58; author sources; state Exp;
125 date 89.11.17.16.10.26; author sources; state Exp;
139 @/* inc.c - incorporate messages from a maildrop into a folder */
141 static char ident[] = "@@(#)$Id: inc.c,v 1.23 1994/04/21 18:20:50 jromine Exp jromine $";
145 /* Revised: Sat Apr 14 17:08:17 PDT 1990 (marvit@@hplabs)
146 * Added hpux hacks to set and reset gid to be "mail" as needed. The reset
147 * is necessary so inc'ed mail is the group of the inc'er, rather than
148 * "mail". We setgid to egid only when [un]locking the mail file. This
149 * is also a major security precaution which will not be explained here.
151 * Fri Feb 7 16:04:57 PST 1992 John Romine <bug-mh@@ics.uci.edu>
152 * NB: I'm not 100% sure that this setgid stuff is secure even now.
158 #include "../h/dropsbr.h"
166 #include "../h/formatsbr.h"
167 #include "../h/scansbr.h"
168 #include "../zotnet/tws.h"
170 #include "../zotnet/mts.h"
173 #include <sys/types.h>
174 #include <sys/stat.h>
182 #define MFminc(a) (a)
188 #define POPminc(a) (a)
194 #define RPOPminc(a) (a)
196 #define RPOPminc(a) 0
200 #define APOPminc(a) (a)
202 #define APOPminc(a) 0
206 #define TMAminc(a) (a)
211 static struct swit switches[] = {
213 "audit audit-file", 0,
223 "decrypt", TMAminc (-7),
225 "nodecrypt", TMAminc (-9),
231 "form formatfile", 0,
236 "host host", POPminc (-4),
238 "user user", POPminc (-4),
240 "pack file", POPminc (-4),
242 "nopack", POPminc (-6),
245 "apop", APOPminc (-4),
247 "noapop", APOPminc (-6),
250 "rpop", RPOPminc (-4),
252 "norpop", RPOPminc (-6),
267 "nouucp", MFminc (-6),
284 extern char response[];
286 static char *file = NULL;
292 static int pd = NOTOK;
293 static FILE *pf = NULL;
295 static int pop_action (), pop_pack ();
296 static int map_count();
303 #ifdef MAILGROUP /* For setting and returning to "mail" gid */
304 static int return_gid;
305 #endif /* MAILGROUP */
350 FILE *in, *aud = NULL;
355 struct hes_postoffice *po;
360 setlocale(LC_ALL, "");
362 invo_name = r1bindex (argv[0], '/');
363 mts_init (invo_name);
369 * use MAILHOST environment variable if present,
371 * If that fails, use the default (if any)
372 * provided by mtstailor in mts_init()
374 if ((tmphost = getenv("MAILHOST")) != NULL)
376 else if ((po = hes_getmailhost(getusr())) != NULL &&
377 strcmp(po->po_type, "POP") == 0)
378 pophost = po->po_host;
380 if (pophost && *pophost)
383 if ((cp = getenv ("MHPOPDEBUG")) && *cp)
387 if ((cp = m_find (invo_name)) != NULL) {
388 ap = brkstring (cp = getcpy (cp), " ", "\n");
389 ap = copyip (ap, arguments);
393 (void) copyip (argv + 1, ap);
398 while (cp = *argp++) {
400 switch (smatch (++cp, switches)) {
402 ambigsw (cp, switches);
405 adios (NULLCP, "-%s unknown", cp);
407 (void) sprintf (buf, "%s [+folder] [switches]", invo_name);
408 help (buf, switches);
412 if (!(cp = *argp++) || *cp == '-')
413 adios (NULLCP, "missing argument to %s", argp[-2]);
414 audfile = getcpy (m_maildir (cp));
431 if (!(cp = *argp++) || *cp == '-')
432 adios (NULLCP, "missing argument to %s", argp[-2]);
433 from = path (cp, TFILE);/* fall */
446 if (!(form = *argp++) || *form == '-')
447 adios (NULLCP, "missing argument to %s", argp[-2]);
451 if (!(format = *argp++) || *format == '-')
452 adios (NULLCP, "missing argument to %s", argp[-2]);
457 if (!(cp = *argp++) || *cp == '-')
458 adios (NULLCP, "missing argument to %s", argp[-2]);
481 if (!(host = *argp++) || *host == '-')
482 adios (NULLCP, "missing argument to %s", argp[-2]);
485 if (!(user = *argp++) || *user == '-')
486 adios (NULLCP, "missing argument to %s", argp[-2]);
490 if (!(cp = *argp++) || *cp == '-')
491 adios (NULLCP, "missing argument to %s", argp[-2]);
493 if (!(file = *argp++) || *file == '-')
494 adios (NULLCP, "missing argument to %s", argp[-2]);
513 if (*cp == '+' || *cp == '@@') {
515 adios (NULLCP, "only one folder at a time!");
517 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
520 adios (NULLCP, "usage: %s [+folder] [switches]", invo_name);
526 return_gid = getegid(); /* Save effective gid, assuming we'll use it */
527 setgid(getgid()); /* Turn off extraordinary privileges */
528 #endif /* MAILGROUP */
533 if (from || !host || rpop <= 0)
534 (void) setuid (getuid ());
541 if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
542 adios (NULLCP, "no mail to incorporate");
551 ruserpass (host, &user, &pass);
553 if (pop_init (host, user, pass, snoop, rpop) == NOTOK
554 || pop_stat (&nmsgs, &nbytes) == NOTOK)
555 adios (NULLCP, "%s", response);
557 (void) setuid (getuid ());
560 adios (NULLCP, "no mail to incorporate");
565 if (((newmail = getenv ("MAILDROP")) && *newmail)
566 || ((newmail = m_find ("maildrop")) && *newmail))
567 newmail = m_mailpath (newmail);
570 if (uucp && umincproc && *umincproc)
573 newmail = concat (MAILDIR, "/", MAILFIL, NULLCP);
575 if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
576 adios (NULLCP, "no mail to incorporate");
583 if (!m_find ("path"))
584 free (path ("./", TFOLDER));
585 if (!folder && !(folder = m_find (inbox)))
587 maildir = m_maildir (folder);
589 if (stat (maildir, &st) == NOTOK) {
591 adios (maildir, "error on folder");
592 cp = concat ("Create folder \"", maildir, "\"? ", NULLCP);
593 if (noisy && !getanswer (cp))
596 if (!makedir (maildir))
597 adios (NULLCP, "unable to create folder %s", maildir);
600 if (chdir (maildir) == NOTOK)
601 adios (maildir, "unable to change directory to");
602 if (!(mp = m_gmsg (folder)))
603 adios (NULLCP, "unable to read folder %s", folder);
611 if (access (newmail, 02) == NOTOK) {
613 if ((in = fopen (newmail, "r")) == NULL)
614 adios (newmail, "unable to read");
619 (void) signal (SIGHUP, SIG_IGN);
620 (void) signal (SIGINT, SIG_IGN);
621 (void) signal (SIGQUIT, SIG_IGN);
622 (void) signal (SIGTERM, SIG_IGN);
626 setgid(return_gid); /* Reset gid to lock mail file */
627 #endif /* MAILGROUP */
628 if ((in = lkfopen (newmail, "r")) == NULL)
629 adios (NULLCP, "unable to lock and fopen %s", newmail);
631 setgid(getgid()); /* Return us to normal privileges */
632 #endif /* MAILGROUP */
633 (void) fstat (fileno(in), &s1);
637 setgid(getgid()); /* Return us to normal privileges */
638 #endif /* MAILGROUP */
641 if ((i = stat (audfile, &st)) == NOTOK)
642 advise (NULLCP, "Creating Receive-Audit: %s", audfile);
643 if ((aud = fopen (audfile, "a")) == NULL)
644 adios (audfile, "unable to append to");
647 (void) chmod (audfile, m_gmprot ());
649 fprintf (aud, from ? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
652 fprintf (aud, from ? "<<inc>> %s -ms %s\n"
653 : host ? "<<inc>> %s -host %s -user %s%s\n"
655 dtimenow (), from ? from : host, user,
656 rpop < 0 ? " -apop" : rpop > 0 ? " -rpop" : "");
661 if (m_find ("mhe")) {
662 cp = concat (maildir, "/++", NULLCP);
664 if ((mhe = fopen (cp, "a")) == NULL)
665 admonish (cp, "unable to append to");
668 (void) chmod (cp, m_gmprot ());
673 nfs = new_fs (form, format, FORMAT);
676 printf ("Incorporating new mail into %s...\n\n", folder);
677 (void) fflush (stdout);
685 file = path (file, TFILE);
686 if (stat (file, &st) == NOTOK) {
688 adios (file, "error on file");
689 cp = concat ("Create file \"", file, "\"? ", NULLCP);
690 if (noisy && !getanswer (cp))
694 msgnum = map_count ();
695 if ((pd = mbx_open (file, getuid (), getgid (), m_gmprot ()))
697 adios (file, "unable to open");
698 if ((pf = fdopen (pd, "w+")) == NULL)
699 adios (NULLCP, "unable to fdopen %s", file);
702 hghnum = msgnum = mp -> hghmsg;
703 if ((mp = m_remsg (mp, 0, mp -> hghmsg + nmsgs)) == NULL)
704 adios (NULLCP, "unable to allocate folder storage");
707 for (i = 1; i <= nmsgs; i++) {
710 (void) fseek (pf, 0L, 1);
713 (void) fwrite (mmdlm1, 1, strlen (mmdlm1), pf);
716 if (pop_retr (i, pop_pack) == NOTOK)
717 adios (NULLCP, "%s", response);
719 (void) fseek (pf, 0L, 1);
722 adios (file, "write error on");
723 (void) fseek (pf, start, 0);
726 cp = getcpy (m_name (msgnum));
727 if ((pf = fopen (cp, "w+")) == NULL)
728 adios (cp, "unable to write");
729 (void) chmod (cp, m_gmprot ());
732 if (pop_retr (i, pop_action) == NOTOK)
733 adios (NULLCP, "%s", response);
736 adios (cp, "write error on");
737 (void) fseek (pf, 0L, 0);
739 switch (p = scan (pf, msgnum, 0, nfs, width,
740 file ? 0 : msgnum == mp -> hghmsg + 1 && chgflag,
741 1, /* check UNSEEN? */
742 0, 0, stop - start, noisy)) {
744 printf ("%*d empty\n", DMAXFOLDER, msgnum);
750 /* advise (cp, "unable to read"); already advised */
767 (void) fflush (stdout);
769 mp -> msgstats[msgnum] = EXISTS;
772 if (mp -> lowsel == 0 || msgnum < mp -> lowsel)
773 mp -> lowsel = msgnum;
774 if (mp -> hghsel == 0 || msgnum > mp -> hghsel)
775 mp -> hghsel = msgnum;
777 mp -> msgstats[msgnum] |= SELECTED;
780 mp -> msgstats[msgnum] |= UNSEEN;
781 mp -> msgflags |= SEQMOD;
786 (void) fseek (pf, stop, 0);
787 (void) fwrite (mmdlm2, 1, strlen (mmdlm2), pf);
788 if (fflush (pf) || ferror (pf)) {
792 adios (file, "write error on");
794 (void) map_write (file, pd, 0, 0L, start, stop, pos, size,
798 if (ferror(pf) || fclose (pf)) {
803 adios (cp, "write error on");
808 if (trnflag && pop_dele (i) == NOTOK)
809 adios (NULLCP, "%s", response);
811 if (pop_quit () == NOTOK)
812 adios (NULLCP, "%s", response);
814 (void) mbx_close (file, pd);
823 m_unknown (in); /* the MAGIC invocation... */
824 hghnum = msgnum = mp -> hghmsg;
826 if (msgnum >= mp -> hghoff)
827 if ((mp = m_remsg (mp, 0, mp -> hghoff + MAXFOLDER)) == NULL) {
828 advise (NULLCP, "unable to allocate folder storage");
833 switch (i = scan (in, msgnum + 1, msgnum + 1, nfs, width,
834 msgnum == hghnum && chgflag,
835 1, /* check UNSEEN? */
843 fputs ("inc aborted!\n", aud);
844 advise (NULLCP, "aborted!"); /* doesn't clean up locks! */
849 "more than %d messages in folder %s, %s not zero'd",
850 MAXFOLDER, folder, newmail);
854 advise (NULLCP, "scan() botch (%d)", i);
866 (void) fflush (stdout);
868 msgnum++, mp -> hghmsg++;
869 mp -> msgstats[msgnum] = EXISTS;
872 if (mp -> lowsel == 0 || mp -> lowsel > msgnum)
873 mp -> lowsel = msgnum;
874 if (mp -> hghsel == 0 || mp -> hghsel < msgnum)
875 mp -> hghsel = msgnum;
877 mp -> msgstats[msgnum] |= SELECTED;
880 mp -> msgstats[msgnum] |= UNSEEN;
881 mp -> msgflags |= SEQMOD;
890 if (i < 0) { /* error } */
892 if (p < 0) { /* error */
897 setgid(return_gid); /* Be sure we can unlock mail file */
898 #endif /* MAILGROUP */
900 (void) lkfclose (in, newmail);
902 setgid(getgid()); /* And then return us to normal privileges */
904 #endif /* MAILGROUP */
907 adios (NULLCP, "failed");
917 (void) fflush (stdout);
929 if (stat (newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
930 advise (NULLCP, "new messages have arrived!\007");
932 if ((i = creat (newmail, 0600)) != NOTOK)
935 admonish (newmail, "error zero'ing");
936 (void) unlink (map_name (newmail));
941 printf ("%s not zero'd\n", newmail);
943 if (msgnum == hghnum)
944 admonish (NULLCP, "no messages incorporated");
946 m_replace (pfolder, folder);
948 mp -> curmsg = hghnum + 1;
949 mp -> hghmsg = msgnum;
950 if (mp -> lowmsg == 0)
952 if (chgflag) /* sigh... */
953 m_setcur (mp, mp -> curmsg);
962 setgid(return_gid); /* Be sure we can unlock mail file */
963 #endif /* MAILGROUP */
965 (void) lkfclose (in, newmail);
967 setgid(getgid()); /* And then return us to normal privileges */
969 #endif /* MAILGROUP */
978 if (decflag && mp -> numsel > 0) {
980 printf ("\nIncorporating encrypted mail into %s...\n\n", folder);
981 (void) fflush (stdout);
985 for (msgnum = mp -> lowsel; msgnum <= mp -> hghsel; msgnum++)
986 if (mp -> msgstats[msgnum] & SELECTED && decipher (msgnum) == OK) {
987 if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
988 admonish (cp, "unable to open message");
992 switch (scan (in, msgnum, 0, nfs, width,
993 msgnum == mp -> curmsg,
994 1, /* check UNSEEN? */
996 fstat (fileno (in), &st) != NOTOK ? (long) st.st_size
1000 printf ("%*d empty\n", DMAXFOLDER, msgnum);
1012 (void) fflush (stdout);
1025 if (file && pd != NOTOK)
1026 (void) mbx_close (file, pd);
1036 char buffer[BUFSIZ];
1039 (void) sprintf (buffer, "%s/%s", UUCPDIR, UUCPFIL);
1040 if (stat (buffer, &st) == NOTOK || st.st_size == 0)
1043 switch (child_id = vfork ()) {
1045 admonish ("fork", "unable to");
1049 execlp (umincproc, r1bindex (umincproc, '/'), NULLCP);
1050 fprintf (stderr, "unable to exec ");
1055 (void) pidXwait (child_id, umincproc);
1064 static int pop_action (s)
1067 fprintf (pf, "%s\n", s);
1068 stop += strlen (s) + 1;
1072 static int pop_pack (s)
1076 char buffer[BUFSIZ];
1078 (void) sprintf (buffer, "%s\n", s);
1079 for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
1081 for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
1084 size += strlen (buffer) + 1;
1087 static int map_count () {
1093 if (stat (file, &st) == NOTOK)
1095 if ((md = open (cp = map_name (file), 0)) == NOTOK
1096 || map_chk (cp, md, &d, (long) st.st_size, 1)) {
1110 @update for scansbr.c -- overload {folder}.c_flags with hdrflg
1115 static char ident[] = "@@(#)$Id: inc.c,v 1.22 1993/09/01 21:41:48 jromine Exp jromine $";
1124 @set pophost before parsing args, so user can override
1129 static char ident[] = "@@(#)$Id: inc.c,v 1.21 1993/08/20 15:50:13 jromine Exp jromine $";
1132 0, stop - start, noisy)) {
1146 @better user defaulting on rpop/apop
1147 need to check this against older releases
1152 static char ident[] = "@@(#)$Id: inc.c,v 1.20 1992/12/15 00:20:22 jromine Exp jromine $";
1158 * use MAILHOST environment variable if present,
1160 * If that fails, use the default (if any)
1161 * provided by mtstailor in mts_init()
1163 if (pophost == NULL || pophost[0] == '\0')
1165 if ((tmphost = getenv("MAILHOST")) != NULL)
1167 else if ((po = hes_getmailhost(getusr())) != NULL &&
1168 strcmp(po->po_type, "POP") == 0)
1169 pophost = po->po_host;
1172 if (pophost && *pophost)
1185 static char ident[] = "@@(#)$Id: inc.c,v 1.19 1992/11/04 00:43:53 jromine Exp jromine $";
1206 static char ident[] = "@@(#)$Id: inc.c,v 1.18 1992/10/16 16:51:35 jromine Exp jromine $";
1338 static char ident[] = "@@(#)$Id: inc.c,v 1.17 1992/10/15 15:47:32 jromine Exp jromine $";
1351 static char ident[] = "@@(#)$Id: inc.c,v 1.16 1992/05/12 21:56:01 jromine Exp jromine $";
1353 static char *RCSid = "$Header: /usr/src/bs/mh-develop/mh-6.7-new/uip/RCS/inc.c,v 1.16 1992/05/12 21:56:01 jromine Exp jromine $";
1367 static char ident[] = "@@(#)$Id: inc.c,v 1.15 1992/02/11 00:37:53 jromine Exp jromine $";
1370 static char *RCSid = "$Header: /usr/src/bs/mh-develop/mh-6.7-new/uip/RCS/inc.c,v 1.15 1992/02/11 00:37:53 jromine Exp jromine $";
1373 char *user = getusr ();
1376 else if ((po = hes_getmailhost(user)) != NULL &&
1393 static char ident[] = "@@(#)$Id: inc.c,v 1.14 1992/02/10 19:20:02 jromine Exp $";
1396 static char *RCSid = "$Header: /usr/src/bs/mh-develop/mh-6.7-new/uip/RCS/inc.c,v 1.14 1992/02/10 19:20:02 jromine Exp $";
1403 @changes to trap errors from m_getfld()
1408 static char ident[] = "@@(#)$Id: inc.c,v 1.13 1992/02/08 00:06:14 jromine Exp jromine $";
1411 static char *RCSid = "$Header: /usr/rand/src/bin/mh/uip/RCS/inc.c,v 1.3 91/02/05 14:19:24 pearlman Exp $";
1417 #endif /* MAILGROUP */
1423 #endif /* MAILGROUP */
1434 static char ident[] = "@@(#)$Id: inc.c,v 1.12 1992/02/08 00:04:32 jromine Exp jromine $";
1447 if ((mp = m_remsg (mp, 0, mp -> hghoff + MAXFOLDER)) == NULL)
1448 adios (NULLCP, "unable to allocate folder storage");
1455 adios (NULLCP, "aborted!");
1458 adios (NULLCP, "more than %d messages in folder %s, %s not zero'd",
1462 adios (NULLCP, "scan() botch (%d)", i);
1469 @add setgid's to MAILGROUP option
1474 static char ident[] = "@@(#)$Id: inc.c,v 1.11 1992/02/05 07:26:30 jromine Exp jromine $";
1481 @put unseen sequence in mh-format
1486 static char ident[] = "@@(#)$Id: inc.c,v 1.10 1992/02/04 21:59:06 jromine Exp jromine $";
1495 (void) lkfclose (in, newmail);
1506 static char ident[] = "@@(#)$Id: inc.c,v 1.9 1992/02/04 19:25:38 jromine Exp jromine $";
1520 static char ident[] = "@@(#)$Id: inc.c,v 1.8 1992/02/04 00:03:05 jromine Exp jromine $";
1559 if (from || !host || !rpop)
1567 dtimenow (), from ? from : host, user, rpop ? " -rpop" : "");
1578 static char ident[] = "@@(#)$Id: inc.c,v 1.7 1992/02/04 00:02:47 jromine Exp jromine $";
1581 if (ferror(pf) || fclose (pf))
1588 @try to catch read errors when POP
1593 static char ident[] = "@@(#)$Id: inc.c,v 1.6 1992/01/31 22:49:20 jromine Exp jromine $";
1607 static char ident[] = "@@(#)$Id: inc.c,v 1.5 1992/01/31 22:11:32 jromine Exp jromine $";
1614 adios (file, "write error on");
1625 static char ident[] = "@@(#)$Id: inc.c,v 1.4 1990/04/05 14:57:51 sources Exp jromine $";
1638 static char ident[] = "@@(#)$Id:$";
1652 if (pophost && *pophost)
1680 @try to detect full NFS file systems
1685 int pop_action (), pop_pack ();