]>
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.
18 * See the *GROUPPRIVS() macros later. I'm reasonably happy with the setgid
19 * attribute. Running setuid root is probably not a terribly good idea, though.
20 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk>, 04/1998
22 * Peter Maydell's patch slightly modified for nmh 0.28-pre2.
23 * Ruud de Rooij <ruud@debian.org> Wed, 22 Jul 1998 13:24:22 +0200
31 # include <h/dropsbr.h>
32 # include <h/popsbr.h>
39 #include <h/fmt_scan.h>
40 #include <h/scansbr.h>
41 #include <h/signals.h>
48 # define POPminc(a) (a)
54 # define RPOPminc(a) (a)
56 # define RPOPminc(a) 0
60 # define APOPminc(a) (a)
62 # define APOPminc(a) 0
66 # define KPOPminc(a) (a)
68 # define KPOPminc(a) 0
72 # define SASLminc(a) (a)
74 # define SASLminc(a) 0
77 static struct swit switches
[] = {
79 { "audit audit-file", 0 },
89 { "form formatfile", 0 },
91 { "format string", 5 },
93 { "host hostname", POPminc (-4) },
95 { "user username", POPminc (-4) },
97 { "pack file", POPminc (-4) },
99 { "nopack", POPminc (-6) },
101 { "apop", APOPminc (-4) },
103 { "noapop", APOPminc (-6) },
105 { "rpop", RPOPminc (-4) },
107 { "norpop", RPOPminc (-6) },
117 { "width columns", 0 },
125 { "kpop", KPOPminc (-4) },
127 { "sasl", SASLminc(-4) },
128 #define SASLMECHSW 25
129 { "saslmech", SASLminc(-8) },
136 * flags for the mail source
142 static int snoop
= 0;
145 extern char response
[];
147 static char *packfile
= NULL
;
153 static int mbx_style
= MMDF_FORMAT
;
154 static int pd
= NOTOK
;
155 static FILE *pf
= NULL
;
158 /* This is an attempt to simplify things by putting all the
159 * privilege ops into macros.
160 * *GROUPPRIVS() is related to handling the setgid MAIL property,
161 * and only applies if MAILGROUP is defined.
162 * *USERPRIVS() is related to handling the setuid root property,
163 * and only applies if POP is defined [why does POP => setuid root?]
164 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
165 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
166 * do the obvious thing. TRYDROPGROUPPRIVS() has to be safe to call
167 * before DROPUSERPRIVS() is called [this is needed because setgid()
168 * sets both effective and real uids if euid is root.]
170 * There's probably a better implementation if we're allowed to use
171 * BSD-style setreuid() rather than using POSIX saved-ids.
172 * Anyway, if you're euid root it's a bit pointless to drop the group
175 * I'm pretty happy that the security is good provided we aren't setuid root.
176 * The only things we trust with group=mail privilege are lkfopen()
181 * For setting and returning to "mail" gid
184 static int return_gid
;
186 /* easy case; we're not setuid root, so can drop group privs
189 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
190 #else /* POP ie we are setuid root */
191 #define TRYDROPGROUPPRIVS() \
192 if (geteuid() != 0) DROPGROUPPRIVS()
194 #define DROPGROUPPRIVS() setgid(getgid())
195 #define GETGROUPPRIVS() setgid(return_gid)
196 #define SAVEGROUPPRIVS() return_gid = getegid()
198 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
199 #define TRYDROPGROUPPRIVS()
200 #define DROPGROUPPRIVS()
201 #define GETGROUPPRIVS()
202 #define SAVEGROUPPRIVS()
203 #endif /* not MAILGROUP */
206 #define DROPUSERPRIVS() setuid(getuid())
208 #define DROPUSERPRIVS()
211 /* these variables have to be globals so that done() can correctly clean up the lockfile */
212 static int locked
= 0;
213 static char *newmail
;
219 char *map_name(char *);
223 static int pop_action(char *);
224 static int pop_pack(char *);
225 static int map_count(void);
230 main (int argc
, char **argv
)
232 int chgflag
= 1, trnflag
= 1;
233 int noisy
= 1, width
= 0;
234 int rpop
, i
, hghnum
, msgnum
;
235 int kpop
= 0, sasl
= 0;
236 char *cp
, *maildir
, *folder
= NULL
;
237 char *format
= NULL
, *form
= NULL
;
238 char *host
= NULL
, *user
= NULL
;
239 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
;
240 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
246 int nmsgs
, nbytes
, p
= 0;
248 char *MAILHOST_env_variable
;
256 struct hes_postoffice
*po
;
259 /* absolutely the first thing we do is save our privileges,
260 * and drop them if we can.
266 setlocale(LC_ALL
, "");
268 invo_name
= r1bindex (argv
[0], '/');
270 /* read user profile/context */
273 mts_init (invo_name
);
274 arguments
= getarguments (invo_name
, argc
, argv
, 1);
280 * use MAILHOST environment variable if present,
282 * If that fails, use the default (if any)
283 * provided by mts.conf in mts_init()
285 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
286 pophost
= MAILHOST_env_variable
;
288 else if ((po
= hes_getmailhost(getusername())) != NULL
&&
289 strcmp(po
->po_type
, "POP") == 0)
290 pophost
= po
->po_host
;
293 * If there is a valid "pophost" entry in mts.conf,
294 * then use it as the default host.
296 if (pophost
&& *pophost
)
299 if ((cp
= getenv ("MHPOPDEBUG")) && *cp
)
305 while ((cp
= *argp
++)) {
307 switch (smatch (++cp
, switches
)) {
309 ambigsw (cp
, switches
);
312 adios (NULL
, "-%s unknown", cp
);
315 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
316 print_help (buf
, switches
, 1);
319 print_version(invo_name
);
323 if (!(cp
= *argp
++) || *cp
== '-')
324 adios (NULL
, "missing argument to %s", argp
[-2]);
325 audfile
= getcpy (m_maildir (cp
));
339 * The flag `trnflag' has the value:
341 * 2 if -truncate is given
342 * 1 by default (truncating is default)
343 * 0 if -notruncate is given
353 if (!(cp
= *argp
++) || *cp
== '-')
354 adios (NULL
, "missing argument to %s", argp
[-2]);
355 from
= path (cp
, TFILE
);
358 * If the truncate file is in default state,
359 * change to not truncate.
373 if (!(form
= *argp
++) || *form
== '-')
374 adios (NULL
, "missing argument to %s", argp
[-2]);
378 if (!(format
= *argp
++) || *format
== '-')
379 adios (NULL
, "missing argument to %s", argp
[-2]);
384 if (!(cp
= *argp
++) || *cp
== '-')
385 adios (NULL
, "missing argument to %s", argp
[-2]);
390 if (!(host
= *argp
++) || *host
== '-')
391 adios (NULL
, "missing argument to %s", argp
[-2]);
394 if (!(user
= *argp
++) || *user
== '-')
395 adios (NULL
, "missing argument to %s", argp
[-2]);
400 if (!(cp
= *argp
++) || *cp
== '-')
401 adios (NULL
, "missing argument to %s", argp
[-2]);
403 if (!(packfile
= *argp
++) || *packfile
== '-')
404 adios (NULL
, "missing argument to %s", argp
[-2]);
440 if (!(saslmech
= *argp
++) || *saslmech
== '-')
441 adios (NULL
, "missing argument to %s", argp
[-2]);
445 if (*cp
== '+' || *cp
== '@') {
447 adios (NULL
, "only one folder at a time!");
449 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
451 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
455 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
456 * not DROPGROUPPRIVS().
461 if (from
|| !host
|| rpop
<= 0)
465 /* guarantee dropping group priveleges; we might not have done so earlier */
469 * Where are we getting the new mail?
482 * Are we getting the mail from
485 if (inc_type
== INC_POP
) {
487 user
= getusername ();
488 if ( strcmp( POPSERVICE
, "kpop" ) == 0 ) {
491 if (kpop
|| sasl
|| ( rpop
> 0))
492 pass
= getusername ();
494 ruserpass (host
, &user
, &pass
);
497 * initialize POP connection
499 if (pop_init (host
, user
, pass
, snoop
, kpop
? 1 : rpop
, kpop
,
500 sasl
, saslmech
) == NOTOK
)
501 adios (NULL
, "%s", response
);
503 /* Check if there are any messages */
504 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
505 adios (NULL
, "%s", response
);
511 adios (NULL
, "no mail to incorporate");
517 * We will get the mail from a file
518 * (typically the standard maildrop)
521 if (inc_type
== INC_FILE
) {
524 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
525 newmail
= m_mailpath (newmail
);
526 else if ((newmail
= context_find ("maildrop")) && *newmail
)
527 newmail
= m_mailpath (newmail
);
529 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
531 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
532 adios (NULL
, "no mail to incorporate");
536 /* skip the folder setup */
537 if ((inc_type
== INC_POP
) && packfile
)
541 if (!context_find ("path"))
542 free (path ("./", TFOLDER
));
544 folder
= getfolder (0);
545 maildir
= m_maildir (folder
);
547 if (stat (maildir
, &st
) == NOTOK
) {
549 adios (maildir
, "error on folder");
550 cp
= concat ("Create folder \"", maildir
, "\"? ", NULL
);
551 if (noisy
&& !getanswer (cp
))
554 if (!makedir (maildir
))
555 adios (NULL
, "unable to create folder %s", maildir
);
558 if (chdir (maildir
) == NOTOK
)
559 adios (maildir
, "unable to change directory to");
561 /* read folder and create message structure */
562 if (!(mp
= folder_read (folder
)))
563 adios (NULL
, "unable to read folder %s", folder
);
569 if (inc_type
== INC_FILE
) {
570 if (access (newmail
, W_OK
) != NOTOK
) {
573 SIGNAL (SIGHUP
, SIG_IGN
);
574 SIGNAL (SIGINT
, SIG_IGN
);
575 SIGNAL (SIGQUIT
, SIG_IGN
);
576 SIGNAL (SIGTERM
, SIG_IGN
);
579 GETGROUPPRIVS(); /* Reset gid to lock mail file */
580 in
= lkfopen (newmail
, "r");
583 adios (NULL
, "unable to lock and fopen %s", newmail
);
584 fstat (fileno(in
), &s1
);
587 if ((in
= fopen (newmail
, "r")) == NULL
)
588 adios (newmail
, "unable to read");
592 /* This shouldn't be necessary but it can't hurt. */
596 if ((i
= stat (audfile
, &st
)) == NOTOK
)
597 advise (NULL
, "Creating Receive-Audit: %s", audfile
);
598 if ((aud
= fopen (audfile
, "a")) == NULL
)
599 adios (audfile
, "unable to append to");
601 chmod (audfile
, m_gmprot ());
604 fprintf (aud
, from
? "<<inc>> %s -ms %s\n"
605 : host
? "<<inc>> %s -host %s -user %s%s\n"
607 dtimenow (0), from
? from
: host
, user
,
608 rpop
< 0 ? " -apop" : rpop
> 0 ? " -rpop" : "");
610 fprintf (aud
, from
? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
616 if (context_find ("mhe")) {
617 cp
= concat (maildir
, "/++", NULL
);
619 if ((mhe
= fopen (cp
, "a")) == NULL
)
620 admonish (cp
, "unable to append to");
623 chmod (cp
, m_gmprot ());
628 /* Get new format string */
629 nfs
= new_fs (form
, format
, FORMAT
);
632 printf ("Incorporating new mail into %s...\n\n", folder
);
638 * Get the mail from a POP server
640 if (inc_type
== INC_POP
) {
642 packfile
= path (packfile
, TFILE
);
643 if (stat (packfile
, &st
) == NOTOK
) {
645 adios (packfile
, "error on file");
646 cp
= concat ("Create file \"", packfile
, "\"? ", NULL
);
647 if (noisy
&& !getanswer (cp
))
651 msgnum
= map_count ();
652 if ((pd
= mbx_open (packfile
, mbx_style
, getuid(), getgid(), m_gmprot()))
654 adios (packfile
, "unable to open");
655 if ((pf
= fdopen (pd
, "w+")) == NULL
)
656 adios (NULL
, "unable to fdopen %s", packfile
);
658 hghnum
= msgnum
= mp
->hghmsg
;
660 * Check if we have enough message space for all the new
661 * messages. If not, then realloc the folder and add enough
662 * space for all new messages plus 10 additional slots.
664 if (mp
->hghmsg
+ nmsgs
>= mp
->hghoff
665 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ nmsgs
+ 10)))
666 adios (NULL
, "unable to allocate folder storage");
669 for (i
= 1; i
<= nmsgs
; i
++) {
672 fseek (pf
, 0L, SEEK_CUR
);
675 fwrite (mmdlm1
, 1, strlen (mmdlm1
), pf
);
678 if (pop_retr (i
, pop_pack
) == NOTOK
)
679 adios (NULL
, "%s", response
);
681 fseek (pf
, 0L, SEEK_CUR
);
684 adios (packfile
, "write error on");
685 fseek (pf
, start
, SEEK_SET
);
687 cp
= getcpy (m_name (msgnum
));
688 if ((pf
= fopen (cp
, "w+")) == NULL
)
689 adios (cp
, "unable to write");
690 chmod (cp
, m_gmprot ());
693 if (pop_retr (i
, pop_action
) == NOTOK
)
694 adios (NULL
, "%s", response
);
697 adios (cp
, "write error on");
698 fseek (pf
, 0L, SEEK_SET
);
700 switch (p
= scan (pf
, msgnum
, 0, nfs
, width
,
701 packfile
? 0 : msgnum
== mp
->hghmsg
+ 1 && chgflag
,
702 1, NULL
, stop
- start
, noisy
)) {
704 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
710 /* advise (cp, "unable to read"); already advised */
729 clear_msg_flags (mp
, msgnum
);
730 set_exists (mp
, msgnum
);
731 set_unseen (mp
, msgnum
);
732 mp
->msgflags
|= SEQMOD
;
737 fseek (pf
, stop
, SEEK_SET
);
738 fwrite (mmdlm2
, 1, strlen (mmdlm2
), pf
);
739 if (fflush (pf
) || ferror (pf
)) {
743 adios (packfile
, "write error on");
745 map_write (packfile
, pd
, 0, 0L, start
, stop
, pos
, size
, noisy
);
747 if (ferror(pf
) || fclose (pf
)) {
752 adios (cp
, "write error on");
757 if (trnflag
&& pop_dele (i
) == NOTOK
)
758 adios (NULL
, "%s", response
);
761 if (pop_quit () == NOTOK
)
762 adios (NULL
, "%s", response
);
764 mbx_close (packfile
, pd
);
771 * Get the mail from file (usually mail spool)
773 if (inc_type
== INC_FILE
) {
774 m_unknown (in
); /* the MAGIC invocation... */
775 hghnum
= msgnum
= mp
->hghmsg
;
778 * Check if we need to allocate more space for message status.
779 * If so, then add space for an additional 100 messages.
781 if (msgnum
>= mp
->hghoff
782 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghoff
+ 100))) {
783 advise (NULL
, "unable to allocate folder storage");
789 /* copy file from spool to tmp file */
790 tmpfilenam
= m_scratch ("", invo_name
);
791 if ((fd
= creat (tmpfilenam
, m_gmprot ())) == NOTOK
)
792 adios (tmpfilenam
, "unable to create");
793 chmod (tmpfilenam
, m_gmprot ());
794 if (!(in2
= fdopen (fd
, "r+")))
795 adios (tmpfilenam
, "unable to access");
798 /* link message into folder */
799 newmsg
= folder_addmsg(mp
, tmpfilenam
);
802 /* create scanline for new message */
803 switch (i
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
804 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
)) {
811 fputs ("inc aborted!\n", aud
);
812 advise (NULL
, "aborted!"); /* doesn't clean up locks! */
816 advise (NULL
, "BUG in %s, number out of range", invo_name
);
820 advise (NULL
, "BUG in %s, scan() botch (%d)", invo_name
, i
);
836 clear_msg_flags (mp
, msgnum
);
837 set_exists (mp
, msgnum
);
838 set_unseen (mp
, msgnum
);
839 mp
->msgflags
|= SEQMOD
;
847 if (p
< 0) { /* error */
849 if (i
< 0) { /* error */
852 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
853 (void) lkfclose (in
, newmail
); in
= NULL
;
854 DROPGROUPPRIVS(); /* And then return us to normal privileges */
856 fclose (in
); in
= NULL
;
858 adios (NULL
, "failed");
873 if ((inc_type
== INC_POP
) && packfile
)
878 * truncate file we are incorporating from
880 if (inc_type
== INC_FILE
) {
882 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
883 advise (NULL
, "new messages have arrived!\007");
885 if ((i
= creat (newmail
, 0600)) != NOTOK
)
888 admonish (newmail
, "error zero'ing");
889 unlink(map_name(newmail
));
893 printf ("%s not zero'd\n", newmail
);
897 if (msgnum
== hghnum
) {
898 admonish (NULL
, "no messages incorporated");
900 context_replace (pfolder
, folder
); /* update current folder */
902 mp
->curmsg
= hghnum
+ 1;
906 if (chgflag
) /* sigh... */
907 seq_setcur (mp
, mp
->curmsg
);
911 * unlock the mail spool
913 if (inc_type
== INC_FILE
) {
915 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
916 (void) lkfclose (in
, newmail
); in
= NULL
;
917 DROPGROUPPRIVS(); /* And then return us to normal privileges */
919 fclose (in
); in
= NULL
;
923 seq_setunseen (mp
, 0); /* set the Unseen-Sequence */
924 seq_save (mp
); /* synchronize sequences */
925 context_save (); /* save the context file */
933 * Copy message message from spool into
934 * temporary file. Massage the "From " line
938 cpymsg (FILE *in
, FILE *out
)
941 char *tmpbuf
, name
[NAMESZ
];
944 state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, in
);
967 if (packfile
&& pd
!= NOTOK
)
968 mbx_close (packfile
, pd
);
973 lkfclose(in
, newmail
);
977 return 1; /* dead code to satisfy the compiler */
984 fprintf (pf
, "%s\n", s
);
985 stop
+= strlen (s
) + 1;
986 return 0; /* Is return value used? This was missing before 1999-07-15. */
995 snprintf (buffer
, sizeof(buffer
), "%s\n", s
);
996 for (j
= 0; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
998 for (j
= 0; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
1001 size
+= strlen (buffer
) + 1;
1002 return 0; /* Is return value used? This was missing before 1999-07-15. */
1013 if (stat (packfile
, &st
) == NOTOK
)
1015 if ((md
= open (cp
= map_name (packfile
), O_RDONLY
)) == NOTOK
1016 || map_chk (cp
, md
, &d
, (long) st
.st_size
, 1)) {