]>
diplodocus.org Git - nmh/blob - uip/inc.c
3 * inc.c -- incorporate messages from a maildrop into a folder
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 /* Revised: Sat Apr 14 17:08:17 PDT 1990 (marvit@hplabs)
12 * Added hpux hacks to set and reset gid to be "mail" as needed. The reset
13 * is necessary so inc'ed mail is the group of the inc'er, rather than
14 * "mail". We setgid to egid only when [un]locking the mail file. This
15 * is also a major security precaution which will not be explained here.
17 * Fri Feb 7 16:04:57 PST 1992 John Romine <bug-mh@ics.uci.edu>
18 * NB: I'm not 100% sure that this setgid stuff is secure even now.
20 * See the *GROUPPRIVS() macros later. I'm reasonably happy with the setgid
21 * attribute. Running setuid root is probably not a terribly good idea, though.
22 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk>, 04/1998
24 * Peter Maydell's patch slightly modified for nmh 0.28-pre2.
25 * Ruud de Rooij <ruud@debian.org> Wed, 22 Jul 1998 13:24:22 +0200
34 # include <h/dropsbr.h>
35 # include <h/popsbr.h>
38 #include <h/fmt_scan.h>
39 #include <h/scansbr.h>
40 #include <h/signals.h>
47 # define POPminc(a) (a)
53 # define SASLminc(a) (a)
55 # define SASLminc(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 { "port name/number", POPminc (-4) },
92 { "width columns", 0 },
100 { "sasl", SASLminc(-4) },
101 #define SASLMECHSW 21
102 { "saslmech", SASLminc(-8) },
104 { "proxy command", POPminc(-5) },
109 * flags for the mail source
115 static int snoop
= 0;
118 extern char response
[];
120 static char *packfile
= NULL
;
126 static int mbx_style
= MMDF_FORMAT
;
127 static int pd
= NOTOK
;
128 static FILE *pf
= NULL
;
131 /* This is an attempt to simplify things by putting all the
132 * privilege ops into macros.
133 * *GROUPPRIVS() is related to handling the setgid MAIL property,
134 * and only applies if MAILGROUP is defined.
135 * *USERPRIVS() is related to handling the setuid root property,
136 * and only applies if POP is defined [why does POP => setuid root?]
137 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
138 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
139 * do the obvious thing. TRYDROPGROUPPRIVS() has to be safe to call
140 * before DROPUSERPRIVS() is called [this is needed because setgid()
141 * sets both effective and real uids if euid is root.]
143 * There's probably a better implementation if we're allowed to use
144 * BSD-style setreuid() rather than using POSIX saved-ids.
145 * Anyway, if you're euid root it's a bit pointless to drop the group
148 * I'm pretty happy that the security is good provided we aren't setuid root.
149 * The only things we trust with group=mail privilege are lkfopen()
154 * For setting and returning to "mail" gid
157 static int return_gid
;
159 /* easy case; we're not setuid root, so can drop group privs
162 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
163 #else /* POP ie we are setuid root */
164 #define TRYDROPGROUPPRIVS() \
165 if (geteuid() != 0) DROPGROUPPRIVS()
167 #define DROPGROUPPRIVS() setgid(getgid())
168 #define GETGROUPPRIVS() setgid(return_gid)
169 #define SAVEGROUPPRIVS() return_gid = getegid()
171 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
172 #define TRYDROPGROUPPRIVS()
173 #define DROPGROUPPRIVS()
174 #define GETGROUPPRIVS()
175 #define SAVEGROUPPRIVS()
176 #endif /* not MAILGROUP */
178 /* these variables have to be globals so that done() can correctly clean up the lockfile */
179 static int locked
= 0;
180 static char *newmail
;
186 char *map_name(char *);
188 static void inc_done(int) NORETURN
;
190 static int pop_action(char *);
191 static int pop_pack(char *);
192 static int map_count(void);
197 main (int argc
, char **argv
)
199 int chgflag
= 1, trnflag
= 1;
200 int noisy
= 1, width
= 0;
201 int i
, hghnum
= 0, msgnum
= 0;
203 char *cp
, *maildir
= NULL
, *folder
= NULL
;
204 char *format
= NULL
, *form
= NULL
;
205 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
206 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
;
207 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
208 struct msgs
*mp
= NULL
;
211 char b
[MAXPATHLEN
+ 1];
212 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
215 int nmsgs
, nbytes
, p
= 0;
217 char *MAILHOST_env_variable
;
226 /* absolutely the first thing we do is save our privileges,
227 * and drop them if we can.
233 setlocale(LC_ALL
, "");
235 invo_name
= r1bindex (argv
[0], '/');
237 /* read user profile/context */
240 mts_init (invo_name
);
241 arguments
= getarguments (invo_name
, argc
, argv
, 1);
247 * use MAILHOST environment variable if present,
249 * If that fails, use the default (if any)
250 * provided by mts.conf in mts_init()
252 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
253 pophost
= MAILHOST_env_variable
;
255 * If there is a valid "pophost" entry in mts.conf,
256 * then use it as the default host.
258 if (pophost
&& *pophost
)
261 if ((cp
= getenv ("MHPOPDEBUG")) && *cp
)
265 while ((cp
= *argp
++)) {
267 switch (smatch (++cp
, switches
)) {
269 ambigsw (cp
, switches
);
272 adios (NULL
, "-%s unknown", cp
);
275 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
276 print_help (buf
, switches
, 1);
279 print_version(invo_name
);
283 if (!(cp
= *argp
++) || *cp
== '-')
284 adios (NULL
, "missing argument to %s", argp
[-2]);
285 audfile
= getcpy (m_maildir (cp
));
299 * The flag `trnflag' has the value:
301 * 2 if -truncate is given
302 * 1 by default (truncating is default)
303 * 0 if -notruncate is given
313 if (!(cp
= *argp
++) || *cp
== '-')
314 adios (NULL
, "missing argument to %s", argp
[-2]);
315 from
= path (cp
, TFILE
);
318 * If the truncate file is in default state,
319 * change to not truncate.
333 if (!(form
= *argp
++) || *form
== '-')
334 adios (NULL
, "missing argument to %s", argp
[-2]);
338 if (!(format
= *argp
++) || *format
== '-')
339 adios (NULL
, "missing argument to %s", argp
[-2]);
344 if (!(cp
= *argp
++) || *cp
== '-')
345 adios (NULL
, "missing argument to %s", argp
[-2]);
350 if (!(host
= *argp
++) || *host
== '-')
351 adios (NULL
, "missing argument to %s", argp
[-2]);
355 if (!(host
= *argp
++) || *port
== '-')
356 adios (NULL
, "missing argument to %s", argp
[-2]);
360 if (!(user
= *argp
++) || *user
== '-')
361 adios (NULL
, "missing argument to %s", argp
[-2]);
366 if (!(cp
= *argp
++) || *cp
== '-')
367 adios (NULL
, "missing argument to %s", argp
[-2]);
369 if (!(packfile
= *argp
++) || *packfile
== '-')
370 adios (NULL
, "missing argument to %s", argp
[-2]);
388 if (!(saslmech
= *argp
++) || *saslmech
== '-')
389 adios (NULL
, "missing argument to %s", argp
[-2]);
392 if (!(proxy
= *argp
++) || *proxy
== '-')
393 adios (NULL
, "missing argument to %s", argp
[-2]);
397 if (*cp
== '+' || *cp
== '@') {
399 adios (NULL
, "only one folder at a time!");
401 folder
= pluspath (cp
);
403 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
407 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
408 * not DROPGROUPPRIVS().
415 /* guarantee dropping group priveleges; we might not have done so earlier */
419 * Where are we getting the new mail?
432 * Are we getting the mail from
435 if (inc_type
== INC_POP
) {
437 user
= getusername ();
439 pass
= getusername ();
441 ruserpass (host
, &user
, &pass
);
444 * initialize POP connection
446 if (pop_init (host
, port
, user
, pass
, proxy
, snoop
, sasl
,
448 adios (NULL
, "%s", response
);
450 /* Check if there are any messages */
451 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
452 adios (NULL
, "%s", response
);
456 adios (NULL
, "no mail to incorporate");
462 * We will get the mail from a file
463 * (typically the standard maildrop)
466 if (inc_type
== INC_FILE
) {
469 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
470 newmail
= m_mailpath (newmail
);
471 else if ((newmail
= context_find ("maildrop")) && *newmail
)
472 newmail
= m_mailpath (newmail
);
474 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
476 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
477 adios (NULL
, "no mail to incorporate");
479 if ((cp
= strdup(newmail
)) == (char *)0)
480 adios (NULL
, "error allocating memory to copy newmail");
486 /* skip the folder setup */
487 if ((inc_type
== INC_POP
) && packfile
)
491 if (!context_find ("path"))
492 free (path ("./", TFOLDER
));
494 folder
= getfolder (0);
495 maildir
= m_maildir (folder
);
497 if ((maildir_copy
= strdup(maildir
)) == (char *)0)
498 adios (maildir
, "error allocating memory to copy maildir");
500 if (!folder_exists(maildir
)) {
501 /* If the folder doesn't exist, and we're given the -silent flag,
505 create_folder(maildir
, 0, done
);
510 if (chdir (maildir
) == NOTOK
)
511 adios (maildir
, "unable to change directory to");
513 /* read folder and create message structure */
514 if (!(mp
= folder_read (folder
)))
515 adios (NULL
, "unable to read folder %s", folder
);
521 if (inc_type
== INC_FILE
) {
522 if (access (newmail
, W_OK
) != NOTOK
) {
525 SIGNAL (SIGHUP
, SIG_IGN
);
526 SIGNAL (SIGINT
, SIG_IGN
);
527 SIGNAL (SIGQUIT
, SIG_IGN
);
528 SIGNAL (SIGTERM
, SIG_IGN
);
531 GETGROUPPRIVS(); /* Reset gid to lock mail file */
532 in
= lkfopen (newmail
, "r");
535 adios (NULL
, "unable to lock and fopen %s", newmail
);
536 fstat (fileno(in
), &s1
);
539 if ((in
= fopen (newmail
, "r")) == NULL
)
540 adios (newmail
, "unable to read");
544 /* This shouldn't be necessary but it can't hurt. */
548 if ((i
= stat (audfile
, &st
)) == NOTOK
)
549 advise (NULL
, "Creating Receive-Audit: %s", audfile
);
550 if ((aud
= fopen (audfile
, "a")) == NULL
)
551 adios (audfile
, "unable to append to");
553 chmod (audfile
, m_gmprot ());
556 fprintf (aud
, from
? "<<inc>> %s -ms %s\n"
557 : host
? "<<inc>> %s -host %s -user %s\n"
559 dtimenow (0), from
? from
: host
, user
);
561 fprintf (aud
, from
? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
567 if (context_find ("mhe")) {
568 cp
= concat (maildir
, "/++", NULL
);
570 if ((mhe
= fopen (cp
, "a")) == NULL
)
571 admonish (cp
, "unable to append to");
574 chmod (cp
, m_gmprot ());
579 /* Get new format string */
580 nfs
= new_fs (form
, format
, FORMAT
);
583 printf ("Incorporating new mail into %s...\n\n", folder
);
589 * Get the mail from a POP server
591 if (inc_type
== INC_POP
) {
593 packfile
= path (packfile
, TFILE
);
594 if (stat (packfile
, &st
) == NOTOK
) {
596 adios (packfile
, "error on file");
597 cp
= concat ("Create file \"", packfile
, "\"? ", NULL
);
598 if (noisy
&& !getanswer (cp
))
602 msgnum
= map_count ();
603 if ((pd
= mbx_open (packfile
, mbx_style
, getuid(), getgid(), m_gmprot()))
605 adios (packfile
, "unable to open");
606 if ((pf
= fdopen (pd
, "w+")) == NULL
)
607 adios (NULL
, "unable to fdopen %s", packfile
);
609 hghnum
= msgnum
= mp
->hghmsg
;
611 * Check if we have enough message space for all the new
612 * messages. If not, then realloc the folder and add enough
613 * space for all new messages plus 10 additional slots.
615 if (mp
->hghmsg
+ nmsgs
>= mp
->hghoff
616 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ nmsgs
+ 10)))
617 adios (NULL
, "unable to allocate folder storage");
620 for (i
= 1; i
<= nmsgs
; i
++) {
623 fseek (pf
, 0L, SEEK_CUR
);
626 fwrite (mmdlm1
, 1, strlen (mmdlm1
), pf
);
629 if (pop_retr (i
, pop_pack
) == NOTOK
)
630 adios (NULL
, "%s", response
);
632 fseek (pf
, 0L, SEEK_CUR
);
635 adios (packfile
, "write error on");
636 fseek (pf
, start
, SEEK_SET
);
638 cp
= getcpy (m_name (msgnum
));
639 if ((pf
= fopen (cp
, "w+")) == NULL
)
640 adios (cp
, "unable to write");
641 chmod (cp
, m_gmprot ());
644 if (pop_retr (i
, pop_action
) == NOTOK
)
645 adios (NULL
, "%s", response
);
648 adios (cp
, "write error on");
649 fseek (pf
, 0L, SEEK_SET
);
651 switch (p
= scan (pf
, msgnum
, 0, nfs
, width
,
652 packfile
? 0 : msgnum
== mp
->hghmsg
+ 1 && chgflag
,
653 1, NULL
, stop
- start
, noisy
)) {
655 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
661 /* advise (cp, "unable to read"); already advised */
680 clear_msg_flags (mp
, msgnum
);
681 set_exists (mp
, msgnum
);
682 set_unseen (mp
, msgnum
);
683 mp
->msgflags
|= SEQMOD
;
688 fseek (pf
, stop
, SEEK_SET
);
689 fwrite (mmdlm2
, 1, strlen (mmdlm2
), pf
);
690 if (fflush (pf
) || ferror (pf
)) {
694 adios (packfile
, "write error on");
696 map_write (packfile
, pd
, 0, 0L, start
, stop
, pos
, size
, noisy
);
698 if (ferror(pf
) || fclose (pf
)) {
703 adios (cp
, "write error on");
708 if (trnflag
&& pop_dele (i
) == NOTOK
)
709 adios (NULL
, "%s", response
);
712 if (pop_quit () == NOTOK
)
713 adios (NULL
, "%s", response
);
715 mbx_close (packfile
, pd
);
722 * Get the mail from file (usually mail spool)
724 if (inc_type
== INC_FILE
) {
725 m_unknown (in
); /* the MAGIC invocation... */
726 hghnum
= msgnum
= mp
->hghmsg
;
729 * Check if we need to allocate more space for message status.
730 * If so, then add space for an additional 100 messages.
732 if (msgnum
>= mp
->hghoff
733 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghoff
+ 100))) {
734 advise (NULL
, "unable to allocate folder storage");
740 /* copy file from spool to tmp file */
741 tmpfilenam
= m_scratch ("", invo_name
);
742 if ((fd
= creat (tmpfilenam
, m_gmprot ())) == NOTOK
)
743 adios (tmpfilenam
, "unable to create");
744 chmod (tmpfilenam
, m_gmprot ());
745 if (!(in2
= fdopen (fd
, "r+")))
746 adios (tmpfilenam
, "unable to access");
749 /* link message into folder */
750 newmsg
= folder_addmsg(mp
, tmpfilenam
);
752 /* create scanline for new message */
753 switch (i
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
754 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
)) {
761 fputs ("inc aborted!\n", aud
);
762 advise (NULL
, "aborted!"); /* doesn't clean up locks! */
766 advise (NULL
, "BUG in %s, number out of range", invo_name
);
770 advise (NULL
, "BUG in %s, scan() botch (%d)", invo_name
, i
);
776 * Run the external program hook on the message.
779 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
780 (void)ext_hook("add-hook", b
, (char *)0);
794 if (mp
->lowmsg
== 0) mp
->lowmsg
= 1;
796 clear_msg_flags (mp
, msgnum
);
797 set_exists (mp
, msgnum
);
798 set_unseen (mp
, msgnum
);
799 mp
->msgflags
|= SEQMOD
;
807 if (p
< 0) { /* error */
809 if (i
< 0) { /* error */
812 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
813 (void) lkfclose (in
, newmail
); in
= NULL
;
814 DROPGROUPPRIVS(); /* And then return us to normal privileges */
816 fclose (in
); in
= NULL
;
818 adios (NULL
, "failed");
833 if ((inc_type
== INC_POP
) && packfile
)
838 * truncate file we are incorporating from
840 if (inc_type
== INC_FILE
) {
842 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
843 advise (NULL
, "new messages have arrived!\007");
845 if ((i
= creat (newmail
, 0600)) != NOTOK
)
848 admonish (newmail
, "error zero'ing");
849 unlink(map_name(newmail
));
853 printf ("%s not zero'd\n", newmail
);
857 if (msgnum
== hghnum
) {
858 admonish (NULL
, "no messages incorporated");
860 context_replace (pfolder
, folder
); /* update current folder */
862 mp
->curmsg
= hghnum
+ 1;
866 if (chgflag
) /* sigh... */
867 seq_setcur (mp
, mp
->curmsg
);
871 * unlock the mail spool
873 if (inc_type
== INC_FILE
) {
875 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
876 (void) lkfclose (in
, newmail
); in
= NULL
;
877 DROPGROUPPRIVS(); /* And then return us to normal privileges */
879 fclose (in
); in
= NULL
;
883 seq_setunseen (mp
, 0); /* set the Unseen-Sequence */
884 seq_save (mp
); /* synchronize sequences */
885 context_save (); /* save the context file */
894 * Copy message message from spool into
895 * temporary file. Massage the "From " line
899 cpymsg (FILE *in
, FILE *out
)
902 char *tmpbuf
, name
[NAMESZ
];
905 state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, in
);
925 inc_done (int status
)
928 if (packfile
&& pd
!= NOTOK
)
929 mbx_close (packfile
, pd
);
934 lkfclose(in
, newmail
);
944 fprintf (pf
, "%s\n", s
);
945 stop
+= strlen (s
) + 1;
946 return 0; /* Is return value used? This was missing before 1999-07-15. */
955 snprintf (buffer
, sizeof(buffer
), "%s\n", s
);
956 for (j
= 0; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
958 for (j
= 0; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
961 size
+= strlen (buffer
) + 1;
962 return 0; /* Is return value used? This was missing before 1999-07-15. */
973 if (stat (packfile
, &st
) == NOTOK
)
975 if ((md
= open (cp
= map_name (packfile
), O_RDONLY
)) == NOTOK
976 || map_chk (cp
, md
, &d
, (long) st
.st_size
, 1)) {