]>
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 ( strcmp( POPSERVICE
, "kpop" ) == 0 ) {
408 if (kpop
|| ( rpop
> 0))
409 pass
= getusername ();
411 ruserpass (host
, &user
, &pass
);
414 * initialize POP connection
416 if (pop_init (host
, user
, pass
, snoop
, kpop
? 1 : rpop
, kpop
) == NOTOK
)
417 adios (NULL
, "%s", response
);
419 /* Check if there are any messages */
420 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
421 adios (NULL
, "%s", response
);
427 adios (NULL
, "no mail to incorporate");
433 * We will get the mail from a file
434 * (typically the standard maildrop)
437 if (inc_type
== INC_FILE
) {
440 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
441 newmail
= m_mailpath (newmail
);
442 else if ((newmail
= context_find ("maildrop")) && *newmail
)
443 newmail
= m_mailpath (newmail
);
445 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
447 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
448 adios (NULL
, "no mail to incorporate");
452 /* skip the folder setup */
453 if ((inc_type
== INC_POP
) && packfile
)
457 if (!context_find ("path"))
458 free (path ("./", TFOLDER
));
460 folder
= getfolder (0);
461 maildir
= m_maildir (folder
);
463 if (stat (maildir
, &st
) == NOTOK
) {
465 adios (maildir
, "error on folder");
466 cp
= concat ("Create folder \"", maildir
, "\"? ", NULL
);
467 if (noisy
&& !getanswer (cp
))
470 if (!makedir (maildir
))
471 adios (NULL
, "unable to create folder %s", maildir
);
474 if (chdir (maildir
) == NOTOK
)
475 adios (maildir
, "unable to change directory to");
477 /* read folder and create message structure */
478 if (!(mp
= folder_read (folder
)))
479 adios (NULL
, "unable to read folder %s", folder
);
485 if (inc_type
== INC_FILE
) {
486 if (access (newmail
, W_OK
) != NOTOK
) {
489 SIGNAL (SIGHUP
, SIG_IGN
);
490 SIGNAL (SIGINT
, SIG_IGN
);
491 SIGNAL (SIGQUIT
, SIG_IGN
);
492 SIGNAL (SIGTERM
, SIG_IGN
);
496 setgid(return_gid
); /* Reset gid to lock mail file */
497 #endif /* MAILGROUP */
499 /* lock and fopen the mail spool */
500 if ((in
= lkfopen (newmail
, "r")) == NULL
)
501 adios (NULL
, "unable to lock and fopen %s", newmail
);
504 setgid(getgid()); /* Return us to normal privileges */
505 #endif /* MAILGROUP */
506 fstat (fileno(in
), &s1
);
509 if ((in
= fopen (newmail
, "r")) == NULL
)
510 adios (newmail
, "unable to read");
515 setgid(getgid()); /* Return us to normal privileges */
516 #endif /* MAILGROUP */
519 if ((i
= stat (audfile
, &st
)) == NOTOK
)
520 advise (NULL
, "Creating Receive-Audit: %s", audfile
);
521 if ((aud
= fopen (audfile
, "a")) == NULL
)
522 adios (audfile
, "unable to append to");
524 chmod (audfile
, m_gmprot ());
527 fprintf (aud
, from
? "<<inc>> %s -ms %s\n"
528 : host
? "<<inc>> %s -host %s -user %s%s\n"
530 dtimenow (0), from
? from
: host
, user
,
531 rpop
< 0 ? " -apop" : rpop
> 0 ? " -rpop" : "");
533 fprintf (aud
, from
? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
539 if (context_find ("mhe")) {
540 cp
= concat (maildir
, "/++", NULL
);
542 if ((mhe
= fopen (cp
, "a")) == NULL
)
543 admonish (cp
, "unable to append to");
546 chmod (cp
, m_gmprot ());
551 /* Get new format string */
552 nfs
= new_fs (form
, format
, FORMAT
);
555 printf ("Incorporating new mail into %s...\n\n", folder
);
561 * Get the mail from a POP server
563 if (inc_type
== INC_POP
) {
565 packfile
= path (packfile
, TFILE
);
566 if (stat (packfile
, &st
) == NOTOK
) {
568 adios (packfile
, "error on file");
569 cp
= concat ("Create file \"", packfile
, "\"? ", NULL
);
570 if (noisy
&& !getanswer (cp
))
574 msgnum
= map_count ();
575 if ((pd
= mbx_open (packfile
, mbx_style
, getuid(), getgid(), m_gmprot()))
577 adios (packfile
, "unable to open");
578 if ((pf
= fdopen (pd
, "w+")) == NULL
)
579 adios (NULL
, "unable to fdopen %s", packfile
);
581 hghnum
= msgnum
= mp
->hghmsg
;
583 * Check if we have enough message space for all the new
584 * messages. If not, then realloc the folder and add enough
585 * space for all new messages plus 10 additional slots.
587 if (mp
->hghmsg
+ nmsgs
>= mp
->hghoff
588 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ nmsgs
+ 10)))
589 adios (NULL
, "unable to allocate folder storage");
592 for (i
= 1; i
<= nmsgs
; i
++) {
595 fseek (pf
, 0L, SEEK_CUR
);
598 fwrite (mmdlm1
, 1, strlen (mmdlm1
), pf
);
601 if (pop_retr (i
, pop_pack
) == NOTOK
)
602 adios (NULL
, "%s", response
);
604 fseek (pf
, 0L, SEEK_CUR
);
607 adios (packfile
, "write error on");
608 fseek (pf
, start
, SEEK_SET
);
610 cp
= getcpy (m_name (msgnum
));
611 if ((pf
= fopen (cp
, "w+")) == NULL
)
612 adios (cp
, "unable to write");
613 chmod (cp
, m_gmprot ());
616 if (pop_retr (i
, pop_action
) == NOTOK
)
617 adios (NULL
, "%s", response
);
620 adios (cp
, "write error on");
621 fseek (pf
, 0L, SEEK_SET
);
623 switch (p
= scan (pf
, msgnum
, 0, nfs
, width
,
624 packfile
? 0 : msgnum
== mp
->hghmsg
+ 1 && chgflag
,
625 1, NULL
, stop
- start
, noisy
)) {
627 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
633 /* advise (cp, "unable to read"); already advised */
652 clear_msg_flags (mp
, msgnum
);
653 set_exists (mp
, msgnum
);
654 set_unseen (mp
, msgnum
);
655 mp
->msgflags
|= SEQMOD
;
660 fseek (pf
, stop
, SEEK_SET
);
661 fwrite (mmdlm2
, 1, strlen (mmdlm2
), pf
);
662 if (fflush (pf
) || ferror (pf
)) {
666 adios (packfile
, "write error on");
668 map_write (packfile
, pd
, 0, 0L, start
, stop
, pos
, size
, noisy
);
670 if (ferror(pf
) || fclose (pf
)) {
675 adios (cp
, "write error on");
680 if (trnflag
&& pop_dele (i
) == NOTOK
)
681 adios (NULL
, "%s", response
);
684 if (pop_quit () == NOTOK
)
685 adios (NULL
, "%s", response
);
687 mbx_close (packfile
, pd
);
694 * Get the mail from file (usually mail spool)
696 if (inc_type
== INC_FILE
) {
697 m_unknown (in
); /* the MAGIC invocation... */
698 hghnum
= msgnum
= mp
->hghmsg
;
701 * Check if we need to allocate more space for message status.
702 * If so, then add space for an additional 100 messages.
704 if (msgnum
>= mp
->hghoff
705 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghoff
+ 100))) {
706 advise (NULL
, "unable to allocate folder storage");
712 /* copy file from spool to tmp file */
713 tmpfilenam
= m_scratch ("", invo_name
);
714 if ((fd
= creat (tmpfilenam
, m_gmprot ())) == NOTOK
)
715 adios (tmpfilenam
, "unable to create");
716 chmod (tmpfilenam
, m_gmprot ());
717 if (!(in2
= fdopen (fd
, "r+")))
718 adios (tmpfilenam
, "unable to access");
721 /* link message into folder */
722 newmsg
= folder_addmsg(mp
, tmpfilenam
);
725 /* create scanline for new message */
726 switch (i
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
727 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
)) {
734 fputs ("inc aborted!\n", aud
);
735 advise (NULL
, "aborted!"); /* doesn't clean up locks! */
739 advise (NULL
, "BUG in %s, number out of range", invo_name
);
743 advise (NULL
, "BUG in %s, scan() botch (%d)", invo_name
, i
);
759 clear_msg_flags (mp
, msgnum
);
760 set_exists (mp
, msgnum
);
761 set_unseen (mp
, msgnum
);
762 mp
->msgflags
|= SEQMOD
;
770 if (p
< 0) { /* error */
772 if (i
< 0) { /* error */
776 /* Be sure we can unlock mail file */
778 #endif /* MAILGROUP */
780 lkfclose (in
, newmail
);
783 /* And then return us to normal privileges */
785 #endif /* MAILGROUP */
789 adios (NULL
, "failed");
804 if ((inc_type
== INC_POP
) && packfile
)
809 * truncate file we are incorporating from
811 if (inc_type
== INC_FILE
) {
813 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
814 advise (NULL
, "new messages have arrived!\007");
816 if ((i
= creat (newmail
, 0600)) != NOTOK
)
819 admonish (newmail
, "error zero'ing");
820 unlink(map_name(newmail
));
824 printf ("%s not zero'd\n", newmail
);
828 if (msgnum
== hghnum
) {
829 admonish (NULL
, "no messages incorporated");
831 context_replace (pfolder
, folder
); /* update current folder */
833 mp
->curmsg
= hghnum
+ 1;
837 if (chgflag
) /* sigh... */
838 seq_setcur (mp
, mp
->curmsg
);
842 * unlock the mail spool
844 if (inc_type
== INC_FILE
) {
847 setgid(return_gid
); /* Be sure we can unlock mail file */
848 #endif /* MAILGROUP */
850 lkfclose (in
, newmail
);
853 setgid(getgid()); /* And then return us to normal privileges */
854 #endif /* MAILGROUP */
860 seq_setunseen (mp
, 0); /* set the Unseen-Sequence */
861 seq_save (mp
); /* synchronize sequences */
862 context_save (); /* save the context file */
870 * Copy message message from spool into
871 * temporary file. Massage the "From " line
875 cpymsg (FILE *in
, FILE *out
)
878 char *tmpbuf
, name
[NAMESZ
];
881 state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, in
);
904 if (packfile
&& pd
!= NOTOK
)
905 mbx_close (packfile
, pd
);
908 return 1; /* dead code to satisfy the compiler */
914 fprintf (pf
, "%s\n", s
);
915 stop
+= strlen (s
) + 1;
916 return 0; /* Is return value used? This was missing before 1999-07-15. */
925 snprintf (buffer
, sizeof(buffer
), "%s\n", s
);
926 for (j
= 0; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
928 for (j
= 0; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
931 size
+= strlen (buffer
) + 1;
932 return 0; /* Is return value used? This was missing before 1999-07-15. */
943 if (stat (packfile
, &st
) == NOTOK
)
945 if ((md
= open (cp
= map_name (packfile
), O_RDONLY
)) == NOTOK
946 || map_chk (cp
, md
, &d
, (long) st
.st_size
, 1)) {