]>
diplodocus.org Git - nmh/blob - uip/inc.c
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
59 # define KPOPminc(a) (a)
61 # define KPOPminc(a) 0
64 static struct swit switches
[] = {
66 { "audit audit-file", 0 },
76 { "form formatfile", 0 },
78 { "format string", 5 },
80 { "host hostname", POPminc (-4) },
82 { "user username", POPminc (-4) },
84 { "pack file", POPminc (-4) },
86 { "nopack", POPminc (-6) },
88 { "apop", APOPminc (-4) },
90 { "noapop", APOPminc (-6) },
92 { "rpop", RPOPminc (-4) },
94 { "norpop", RPOPminc (-6) },
104 { "width columns", 0 },
112 { "kpop", KPOPminc (-4) },
119 * flags for the mail source
125 static int snoop
= 0;
128 extern char response
[];
130 static char *packfile
= NULL
;
136 static int mbx_style
= MMDF_FORMAT
;
137 static int pd
= NOTOK
;
138 static FILE *pf
= NULL
;
143 * For setting and returning to "mail" gid
146 static int return_gid
;
152 char *map_name(char *);
156 static int pop_action(char *);
157 static int pop_pack(char *);
158 static int map_count(void);
163 main (int argc
, char **argv
)
165 int chgflag
= 1, trnflag
= 1;
166 int noisy
= 1, width
= 0, locked
= 0;
167 int rpop
, i
, hghnum
, msgnum
;
169 char *cp
, *maildir
, *folder
= NULL
;
170 char *format
= NULL
, *form
= NULL
;
171 char *newmail
, *host
= NULL
, *user
= NULL
;
172 char *audfile
= NULL
, *from
= NULL
;
173 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
176 FILE *in
, *aud
= NULL
;
179 int nmsgs
, nbytes
, p
= 0;
188 struct hes_postoffice
*po
;
193 setlocale(LC_ALL
, "");
195 invo_name
= r1bindex (argv
[0], '/');
197 /* read user profile/context */
200 mts_init (invo_name
);
201 arguments
= getarguments (invo_name
, argc
, argv
, 1);
208 * use MAILHOST environment variable if present,
210 * If that fails, use the default (if any)
211 * provided by mts.conf in mts_init()
213 if ((tmphost
= getenv("MAILHOST")) != NULL
)
215 else if ((po
= hes_getmailhost(getusername())) != NULL
&&
216 strcmp(po
->po_type
, "POP") == 0)
217 pophost
= po
->po_host
;
220 * If there is a valid "pophost" entry in mts.conf,
221 * then use it as the default host.
223 if (pophost
&& *pophost
)
226 if ((cp
= getenv ("MHPOPDEBUG")) && *cp
)
232 while ((cp
= *argp
++)) {
234 switch (smatch (++cp
, switches
)) {
236 ambigsw (cp
, switches
);
239 adios (NULL
, "-%s unknown", cp
);
242 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
243 print_help (buf
, switches
, 1);
246 print_version(invo_name
);
250 if (!(cp
= *argp
++) || *cp
== '-')
251 adios (NULL
, "missing argument to %s", argp
[-2]);
252 audfile
= getcpy (m_maildir (cp
));
266 * The flag `trnflag' has the value:
268 * 2 if -truncate is given
269 * 1 by default (truncating is default)
270 * 0 if -notruncate is given
280 if (!(cp
= *argp
++) || *cp
== '-')
281 adios (NULL
, "missing argument to %s", argp
[-2]);
282 from
= path (cp
, TFILE
);
285 * If the truncate file is in default state,
286 * change to not truncate.
300 if (!(form
= *argp
++) || *form
== '-')
301 adios (NULL
, "missing argument to %s", argp
[-2]);
305 if (!(format
= *argp
++) || *format
== '-')
306 adios (NULL
, "missing argument to %s", argp
[-2]);
311 if (!(cp
= *argp
++) || *cp
== '-')
312 adios (NULL
, "missing argument to %s", argp
[-2]);
317 if (!(host
= *argp
++) || *host
== '-')
318 adios (NULL
, "missing argument to %s", argp
[-2]);
321 if (!(user
= *argp
++) || *user
== '-')
322 adios (NULL
, "missing argument to %s", argp
[-2]);
327 if (!(cp
= *argp
++) || *cp
== '-')
328 adios (NULL
, "missing argument to %s", argp
[-2]);
330 if (!(packfile
= *argp
++) || *packfile
== '-')
331 adios (NULL
, "missing argument to %s", argp
[-2]);
363 if (*cp
== '+' || *cp
== '@') {
365 adios (NULL
, "only one folder at a time!");
367 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
369 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
374 return_gid
= getegid(); /* Save effective gid, assuming we'll use it */
375 setgid(getgid()); /* Turn off extraordinary privileges */
376 #endif /* MAILGROUP */
381 if (from
|| !host
|| rpop
<= 0)
386 * Where are we getting the new mail?
399 * Are we getting the mail from
402 if (inc_type
== INC_POP
) {
404 user
= getusername ();
405 if (kpop
|| ( rpop
> 0))
406 pass
= getusername ();
408 ruserpass (host
, &user
, &pass
);
411 * initialize POP connection
413 if (pop_init (host
, user
, pass
, snoop
, kpop
? 1 : rpop
, kpop
) == NOTOK
)
414 adios (NULL
, "%s", response
);
416 /* Check if there are any messages */
417 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
418 adios (NULL
, "%s", response
);
424 adios (NULL
, "no mail to incorporate");
430 * We will get the mail from a file
431 * (typically the standard maildrop)
434 if (inc_type
== INC_FILE
) {
437 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
438 newmail
= m_mailpath (newmail
);
439 else if ((newmail
= context_find ("maildrop")) && *newmail
)
440 newmail
= m_mailpath (newmail
);
442 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
444 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
445 adios (NULL
, "no mail to incorporate");
449 /* skip the folder setup */
450 if ((inc_type
== INC_POP
) && packfile
)
454 if (!context_find ("path"))
455 free (path ("./", TFOLDER
));
457 folder
= getfolder (0);
458 maildir
= m_maildir (folder
);
460 if (stat (maildir
, &st
) == NOTOK
) {
462 adios (maildir
, "error on folder");
463 cp
= concat ("Create folder \"", maildir
, "\"? ", NULL
);
464 if (noisy
&& !getanswer (cp
))
467 if (!makedir (maildir
))
468 adios (NULL
, "unable to create folder %s", maildir
);
471 if (chdir (maildir
) == NOTOK
)
472 adios (maildir
, "unable to change directory to");
474 /* read folder and create message structure */
475 if (!(mp
= folder_read (folder
)))
476 adios (NULL
, "unable to read folder %s", folder
);
482 if (inc_type
== INC_FILE
) {
483 if (access (newmail
, W_OK
) != NOTOK
) {
486 SIGNAL (SIGHUP
, SIG_IGN
);
487 SIGNAL (SIGINT
, SIG_IGN
);
488 SIGNAL (SIGQUIT
, SIG_IGN
);
489 SIGNAL (SIGTERM
, SIG_IGN
);
493 setgid(return_gid
); /* Reset gid to lock mail file */
494 #endif /* MAILGROUP */
496 /* lock and fopen the mail spool */
497 if ((in
= lkfopen (newmail
, "r")) == NULL
)
498 adios (NULL
, "unable to lock and fopen %s", newmail
);
501 setgid(getgid()); /* Return us to normal privileges */
502 #endif /* MAILGROUP */
503 fstat (fileno(in
), &s1
);
506 if ((in
= fopen (newmail
, "r")) == NULL
)
507 adios (newmail
, "unable to read");
512 setgid(getgid()); /* Return us to normal privileges */
513 #endif /* MAILGROUP */
516 if ((i
= stat (audfile
, &st
)) == NOTOK
)
517 advise (NULL
, "Creating Receive-Audit: %s", audfile
);
518 if ((aud
= fopen (audfile
, "a")) == NULL
)
519 adios (audfile
, "unable to append to");
521 chmod (audfile
, m_gmprot ());
524 fprintf (aud
, from
? "<<inc>> %s -ms %s\n"
525 : host
? "<<inc>> %s -host %s -user %s%s\n"
527 dtimenow (0), from
? from
: host
, user
,
528 rpop
< 0 ? " -apop" : rpop
> 0 ? " -rpop" : "");
530 fprintf (aud
, from
? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
536 if (context_find ("mhe")) {
537 cp
= concat (maildir
, "/++", NULL
);
539 if ((mhe
= fopen (cp
, "a")) == NULL
)
540 admonish (cp
, "unable to append to");
543 chmod (cp
, m_gmprot ());
548 /* Get new format string */
549 nfs
= new_fs (form
, format
, FORMAT
);
552 printf ("Incorporating new mail into %s...\n\n", folder
);
558 * Get the mail from a POP server
560 if (inc_type
== INC_POP
) {
562 packfile
= path (packfile
, TFILE
);
563 if (stat (packfile
, &st
) == NOTOK
) {
565 adios (packfile
, "error on file");
566 cp
= concat ("Create file \"", packfile
, "\"? ", NULL
);
567 if (noisy
&& !getanswer (cp
))
571 msgnum
= map_count ();
572 if ((pd
= mbx_open (packfile
, mbx_style
, getuid(), getgid(), m_gmprot()))
574 adios (packfile
, "unable to open");
575 if ((pf
= fdopen (pd
, "w+")) == NULL
)
576 adios (NULL
, "unable to fdopen %s", packfile
);
578 hghnum
= msgnum
= mp
->hghmsg
;
580 * Check if we have enough message space for all the new
581 * messages. If not, then realloc the folder and add enough
582 * space for all new messages plus 10 additional slots.
584 if (mp
->hghmsg
+ nmsgs
>= mp
->hghoff
585 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ nmsgs
+ 10)))
586 adios (NULL
, "unable to allocate folder storage");
589 for (i
= 1; i
<= nmsgs
; i
++) {
592 fseek (pf
, 0L, SEEK_CUR
);
595 fwrite (mmdlm1
, 1, strlen (mmdlm1
), pf
);
598 if (pop_retr (i
, pop_pack
) == NOTOK
)
599 adios (NULL
, "%s", response
);
601 fseek (pf
, 0L, SEEK_CUR
);
604 adios (packfile
, "write error on");
605 fseek (pf
, start
, SEEK_SET
);
607 cp
= getcpy (m_name (msgnum
));
608 if ((pf
= fopen (cp
, "w+")) == NULL
)
609 adios (cp
, "unable to write");
610 chmod (cp
, m_gmprot ());
613 if (pop_retr (i
, pop_action
) == NOTOK
)
614 adios (NULL
, "%s", response
);
617 adios (cp
, "write error on");
618 fseek (pf
, 0L, SEEK_SET
);
620 switch (p
= scan (pf
, msgnum
, 0, nfs
, width
,
621 packfile
? 0 : msgnum
== mp
->hghmsg
+ 1 && chgflag
,
622 1, NULL
, stop
- start
, noisy
)) {
624 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
630 /* advise (cp, "unable to read"); already advised */
649 clear_msg_flags (mp
, msgnum
);
650 set_exists (mp
, msgnum
);
651 set_unseen (mp
, msgnum
);
652 mp
->msgflags
|= SEQMOD
;
657 fseek (pf
, stop
, SEEK_SET
);
658 fwrite (mmdlm2
, 1, strlen (mmdlm2
), pf
);
659 if (fflush (pf
) || ferror (pf
)) {
663 adios (packfile
, "write error on");
665 map_write (packfile
, pd
, 0, 0L, start
, stop
, pos
, size
, noisy
);
667 if (ferror(pf
) || fclose (pf
)) {
672 adios (cp
, "write error on");
677 if (trnflag
&& pop_dele (i
) == NOTOK
)
678 adios (NULL
, "%s", response
);
681 if (pop_quit () == NOTOK
)
682 adios (NULL
, "%s", response
);
684 mbx_close (packfile
, pd
);
691 * Get the mail from file (usually mail spool)
693 if (inc_type
== INC_FILE
) {
694 m_unknown (in
); /* the MAGIC invocation... */
695 hghnum
= msgnum
= mp
->hghmsg
;
698 * Check if we need to allocate more space for message status.
699 * If so, then add space for an additional 100 messages.
701 if (msgnum
>= mp
->hghoff
702 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghoff
+ 100))) {
703 advise (NULL
, "unable to allocate folder storage");
709 /* copy file from spool to tmp file */
710 tmpfilenam
= m_scratch ("", invo_name
);
711 if ((fd
= creat (tmpfilenam
, m_gmprot ())) == NOTOK
)
712 adios (tmpfilenam
, "unable to create");
713 chmod (tmpfilenam
, m_gmprot ());
714 if (!(in2
= fdopen (fd
, "r+")))
715 adios (tmpfilenam
, "unable to access");
718 /* link message into folder */
719 newmsg
= folder_addmsg(mp
, tmpfilenam
);
722 /* create scanline for new message */
723 switch (i
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
724 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
)) {
731 fputs ("inc aborted!\n", aud
);
732 advise (NULL
, "aborted!"); /* doesn't clean up locks! */
736 advise (NULL
, "BUG in %s, number out of range", invo_name
);
740 advise (NULL
, "BUG in %s, scan() botch (%d)", invo_name
, i
);
756 clear_msg_flags (mp
, msgnum
);
757 set_exists (mp
, msgnum
);
758 set_unseen (mp
, msgnum
);
759 mp
->msgflags
|= SEQMOD
;
767 if (p
< 0) { /* error */
769 if (i
< 0) { /* error */
773 /* Be sure we can unlock mail file */
775 #endif /* MAILGROUP */
777 lkfclose (in
, newmail
);
780 /* And then return us to normal privileges */
782 #endif /* MAILGROUP */
786 adios (NULL
, "failed");
801 if ((inc_type
== INC_POP
) && packfile
)
806 * truncate file we are incorporating from
808 if (inc_type
== INC_FILE
) {
810 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
811 advise (NULL
, "new messages have arrived!\007");
813 if ((i
= creat (newmail
, 0600)) != NOTOK
)
816 admonish (newmail
, "error zero'ing");
817 unlink(map_name(newmail
));
821 printf ("%s not zero'd\n", newmail
);
825 if (msgnum
== hghnum
) {
826 admonish (NULL
, "no messages incorporated");
828 context_replace (pfolder
, folder
); /* update current folder */
830 mp
->curmsg
= hghnum
+ 1;
834 if (chgflag
) /* sigh... */
835 seq_setcur (mp
, mp
->curmsg
);
839 * unlock the mail spool
841 if (inc_type
== INC_FILE
) {
844 setgid(return_gid
); /* Be sure we can unlock mail file */
845 #endif /* MAILGROUP */
847 lkfclose (in
, newmail
);
850 setgid(getgid()); /* And then return us to normal privileges */
851 #endif /* MAILGROUP */
857 seq_setunseen (mp
, 0); /* set the Unseen-Sequence */
858 seq_save (mp
); /* synchronize sequences */
859 context_save (); /* save the context file */
867 * Copy message message from spool into
868 * temporary file. Massage the "From " line
872 cpymsg (FILE *in
, FILE *out
)
875 char *tmpbuf
, name
[NAMESZ
];
878 state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, in
);
901 if (packfile
&& pd
!= NOTOK
)
902 mbx_close (packfile
, pd
);
905 return 1; /* dead code to satisfy the compiler */
911 fprintf (pf
, "%s\n", s
);
912 stop
+= strlen (s
) + 1;
913 return 0; /* Is return value used? This was missing before 1999-07-15. */
922 snprintf (buffer
, sizeof(buffer
), "%s\n", s
);
923 for (j
= 0; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
925 for (j
= 0; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
928 size
+= strlen (buffer
) + 1;
929 return 0; /* Is return value used? This was missing before 1999-07-15. */
940 if (stat (packfile
, &st
) == NOTOK
)
942 if ((md
= open (cp
= map_name (packfile
), O_RDONLY
)) == NOTOK
943 || map_chk (cp
, md
, &d
, (long) st
.st_size
, 1)) {