]>
diplodocus.org Git - nmh/blob - uip/inc.c
083a46e6a82968dc6f9b0852a54a167bc97c9ff6
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
33 #include <h/dropsbr.h>
35 #include <h/fmt_scan.h>
36 #include <h/scansbr.h>
37 #include <h/signals.h>
43 # define SASLminc(a) (a)
45 # define SASLminc(a) 0
48 #define INC_SWITCHES \
49 X("audit audit-file", 0, AUDSW) \
50 X("noaudit", 0, NAUDSW) \
51 X("changecur", 0, CHGSW) \
52 X("nochangecur", 0, NCHGSW) \
53 X("file name", 0, FILESW) \
54 X("form formatfile", 0, FORMSW) \
55 X("format string", 5, FMTSW) \
56 X("host hostname", 0, HOSTSW) \
57 X("user username", 0, USERSW) \
58 X("pack file", 0, PACKSW) \
59 X("nopack", 0, NPACKSW) \
60 X("port name/number", 0, PORTSW) \
61 X("silent", 0, SILSW) \
62 X("nosilent", 0, NSILSW) \
63 X("truncate", 0, TRNCSW) \
64 X("notruncate", 0, NTRNCSW) \
65 X("width columns", 0, WIDTHSW) \
66 X("version", 0, VERSIONSW) \
67 X("help", 0, HELPSW) \
68 X("snoop", -5, SNOOPSW) \
69 X("sasl", SASLminc(-4), SASLSW) \
70 X("nosasl", SASLminc(-6), NOSASLSW) \
71 X("saslmech", SASLminc(-8), SASLMECHSW) \
72 X("proxy command", 0, PROXYSW) \
74 #define X(sw, minchars, id) id,
75 DEFINE_SWITCH_ENUM(INC
);
78 #define X(sw, minchars, id) { sw, minchars, id },
79 DEFINE_SWITCH_ARRAY(INC
, switches
);
83 * flags for the mail source
89 static struct Maildir_entry
{
93 static int num_maildir_entries
= 0;
96 extern char response
[];
101 static int mbx_style
= MMDF_FORMAT
;
102 static int pd
= NOTOK
;
107 static char *packfile
= NULL
;
108 static FILE *pf
= NULL
;
110 /* This is an attempt to simplify things by putting all the
111 * privilege ops into macros.
112 * *GROUPPRIVS() is related to handling the setgid MAIL property,
113 * and only applies if MAILGROUP is defined.
114 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
115 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
116 * do the obvious thing.
118 * There's probably a better implementation if we're allowed to use
119 * BSD-style setreuid() rather than using POSIX saved-ids.
120 * Anyway, if you're euid root it's a bit pointless to drop the group
123 * I'm pretty happy that the security is good provided we aren't setuid root.
124 * The only things we trust with group=mail privilege are lkfopen()
129 * For setting and returning to "mail" gid
132 static int return_gid
;
133 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
134 #define DROPGROUPPRIVS() setgid(getgid())
135 #define GETGROUPPRIVS() setgid(return_gid)
136 #define SAVEGROUPPRIVS() return_gid = getegid()
138 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
139 #define TRYDROPGROUPPRIVS()
140 #define DROPGROUPPRIVS()
141 #define GETGROUPPRIVS()
142 #define SAVEGROUPPRIVS()
143 #endif /* not MAILGROUP */
145 /* these variables have to be globals so that done() can correctly clean up the lockfile */
146 static int locked
= 0;
147 static char *newmail
;
153 char *map_name(char *);
155 static void inc_done(int) NORETURN
;
156 static int pop_action(char *);
157 static int pop_pack(char *);
158 static int map_count(void);
161 maildir_srt(const void *va
, const void *vb
)
163 const struct Maildir_entry
*a
= va
, *b
= vb
;
164 if (a
->mtime
> b
->mtime
)
166 else if (a
->mtime
< b
->mtime
)
173 main (int argc
, char **argv
)
175 int chgflag
= 1, trnflag
= 1;
176 int noisy
= 1, width
= 0;
177 int hghnum
= 0, msgnum
= 0;
179 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
180 char *cp
, *maildir
= NULL
, *folder
= NULL
;
181 char *format
= NULL
, *form
= NULL
;
182 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
183 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
;
184 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
185 struct msgs
*mp
= NULL
;
188 char b
[PATH_MAX
+ 1];
189 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
192 char *MAILHOST_env_variable
;
196 /* absolutely the first thing we do is save our privileges,
197 * and drop them if we can.
203 setlocale(LC_ALL
, "");
205 invo_name
= r1bindex (argv
[0], '/');
207 /* read user profile/context */
210 mts_init (invo_name
);
211 arguments
= getarguments (invo_name
, argc
, argv
, 1);
216 * use MAILHOST environment variable if present,
218 * If that fails, use the default (if any)
219 * provided by mts.conf in mts_init()
221 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
222 pophost
= MAILHOST_env_variable
;
224 * If there is a valid "pophost" entry in mts.conf,
225 * then use it as the default host.
227 if (pophost
&& *pophost
)
230 while ((cp
= *argp
++)) {
232 switch (smatch (++cp
, switches
)) {
234 ambigsw (cp
, switches
);
237 adios (NULL
, "-%s unknown", cp
);
240 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
241 print_help (buf
, switches
, 1);
244 print_version(invo_name
);
248 if (!(cp
= *argp
++) || *cp
== '-')
249 adios (NULL
, "missing argument to %s", argp
[-2]);
250 audfile
= getcpy (m_maildir (cp
));
264 * The flag `trnflag' has the value:
266 * 2 if -truncate is given
267 * 1 by default (truncating is default)
268 * 0 if -notruncate is given
278 if (!(cp
= *argp
++) || *cp
== '-')
279 adios (NULL
, "missing argument to %s", argp
[-2]);
280 from
= path (cp
, TFILE
);
283 * If the truncate file is in default state,
284 * change to not truncate.
298 if (!(form
= *argp
++) || *form
== '-')
299 adios (NULL
, "missing argument to %s", argp
[-2]);
303 if (!(format
= *argp
++) || *format
== '-')
304 adios (NULL
, "missing argument to %s", argp
[-2]);
309 if (!(cp
= *argp
++) || *cp
== '-')
310 adios (NULL
, "missing argument to %s", argp
[-2]);
315 if (!(host
= *argp
++) || *host
== '-')
316 adios (NULL
, "missing argument to %s", argp
[-2]);
320 if (!(port
= *argp
++) || *port
== '-')
321 adios (NULL
, "missing argument to %s", argp
[-2]);
325 if (!(user
= *argp
++) || *user
== '-')
326 adios (NULL
, "missing argument to %s", argp
[-2]);
330 if (!(packfile
= *argp
++) || *packfile
== '-')
331 adios (NULL
, "missing argument to %s", argp
[-2]);
349 if (!(saslmech
= *argp
++) || *saslmech
== '-')
350 adios (NULL
, "missing argument to %s", argp
[-2]);
353 if (!(proxy
= *argp
++) || *proxy
== '-')
354 adios (NULL
, "missing argument to %s", argp
[-2]);
358 if (*cp
== '+' || *cp
== '@') {
360 adios (NULL
, "only one folder at a time!");
362 folder
= pluspath (cp
);
364 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
368 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
369 * not DROPGROUPPRIVS().
374 /* guarantee dropping group priveleges; we might not have done so earlier */
378 * Where are we getting the new mail?
388 * Are we getting the mail from
391 if (inc_type
== INC_POP
) {
392 struct nmh_creds creds
= { 0, 0, 0 };
395 * initialize POP connection
397 nmh_get_credentials (host
, user
, sasl
, &creds
);
398 if (pop_init (host
, port
, creds
.user
, creds
.password
, proxy
, snoop
,
399 sasl
, saslmech
) == NOTOK
)
400 adios (NULL
, "%s", response
);
402 /* Check if there are any messages */
403 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
404 adios (NULL
, "%s", response
);
408 adios (NULL
, "no mail to incorporate");
413 * We will get the mail from a file
414 * (typically the standard maildrop)
417 if (inc_type
== INC_FILE
) {
420 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
421 newmail
= m_mailpath (newmail
);
422 else if ((newmail
= context_find ("maildrop")) && *newmail
)
423 newmail
= m_mailpath (newmail
);
425 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
427 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
428 adios (NULL
, "no mail to incorporate");
429 if (s1
.st_mode
& S_IFDIR
) {
435 cp
= concat (newmail
, "/new", NULL
);
436 if ((md
= opendir(cp
)) == NULL
)
437 adios (NULL
, "unable to open %s", cp
);
438 while ((de
= readdir (md
)) != NULL
) {
439 if (de
->d_name
[0] == '.')
441 if (i
>= num_maildir_entries
) {
442 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
443 adios(NULL
, "not enough memory for %d messages", 2*i
+16);
444 num_maildir_entries
= 2*i
+16;
446 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
447 if (stat(Maildir
[i
].filename
, &ms
) != 0)
448 adios (Maildir
[i
].filename
, "couldn't get delivery time");
449 Maildir
[i
].mtime
= ms
.st_mtime
;
454 cp
= concat (newmail
, "/cur", NULL
);
455 if ((md
= opendir(cp
)) == NULL
)
456 adios (NULL
, "unable to open %s", cp
);
457 while ((de
= readdir (md
)) != NULL
) {
458 if (de
->d_name
[0] == '.')
460 if (i
>= num_maildir_entries
) {
461 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
462 adios(NULL
, "not enough memory for %d messages", 2*i
+16);
463 num_maildir_entries
= 2*i
+16;
465 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
466 if (stat(Maildir
[i
].filename
, &ms
) != 0)
467 adios (Maildir
[i
].filename
, "couldn't get delivery time");
468 Maildir
[i
].mtime
= ms
.st_mtime
;
474 adios (NULL
, "no mail to incorporate");
475 num_maildir_entries
= i
;
476 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
479 if ((cp
= strdup(newmail
)) == (char *)0)
480 adios (NULL
, "error allocating memory to copy newmail");
485 /* skip the folder setup */
486 if ((inc_type
== INC_POP
) && packfile
)
489 if (!context_find ("path"))
490 free (path ("./", TFOLDER
));
492 folder
= getfolder (0);
493 maildir
= m_maildir (folder
);
495 if ((maildir_copy
= strdup(maildir
)) == (char *)0)
496 adios (maildir
, "error allocating memory to copy maildir");
498 if (!folder_exists(maildir
)) {
499 /* If the folder doesn't exist, and we're given the -silent flag,
503 create_folder(maildir
, 0, done
);
508 if (chdir (maildir
) == NOTOK
)
509 adios (maildir
, "unable to change directory to");
511 /* read folder and create message structure */
512 if (!(mp
= folder_read (folder
, 0)))
513 adios (NULL
, "unable to read folder %s", folder
);
517 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
518 if (access (newmail
, W_OK
) != NOTOK
) {
521 SIGNAL (SIGHUP
, SIG_IGN
);
522 SIGNAL (SIGINT
, SIG_IGN
);
523 SIGNAL (SIGQUIT
, SIG_IGN
);
524 SIGNAL (SIGTERM
, SIG_IGN
);
527 GETGROUPPRIVS(); /* Reset gid to lock mail file */
528 in
= lkfopenspool (newmail
, "r");
531 adios (NULL
, "unable to lock and fopen %s", newmail
);
532 fstat (fileno(in
), &s1
);
535 if ((in
= fopen (newmail
, "r")) == NULL
)
536 adios (newmail
, "unable to read");
540 /* This shouldn't be necessary but it can't hurt. */
545 if ((i
= stat (audfile
, &st
)) == NOTOK
)
546 advise (NULL
, "Creating Receive-Audit: %s", audfile
);
547 if ((aud
= fopen (audfile
, "a")) == NULL
)
548 adios (audfile
, "unable to append to");
550 chmod (audfile
, m_gmprot ());
552 fprintf (aud
, from
? "<<inc>> %s -ms %s\n"
553 : host
? "<<inc>> %s -host %s -user %s\n"
555 dtimenow (0), from
? from
: host
, user
);
558 /* Get new format string */
559 nfs
= new_fs (form
, format
, FORMAT
);
562 printf ("Incorporating new mail into %s...\n\n", folder
);
568 * Get the mail from a POP server
570 if (inc_type
== INC_POP
) {
573 packfile
= path (packfile
, TFILE
);
574 if (stat (packfile
, &st
) == NOTOK
) {
576 adios (packfile
, "error on file");
577 cp
= concat ("Create file \"", packfile
, "\"? ", NULL
);
578 if (noisy
&& !getanswer (cp
))
582 msgnum
= map_count ();
583 if ((pd
= mbx_open (packfile
, mbx_style
, getuid(), getgid(), m_gmprot()))
585 adios (packfile
, "unable to open");
586 if ((pf
= fdopen (pd
, "w+")) == NULL
)
587 adios (NULL
, "unable to fdopen %s", packfile
);
589 hghnum
= msgnum
= mp
->hghmsg
;
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 (incerr
= 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 */
650 fseek (pf
, stop
, SEEK_SET
);
651 fwrite (mmdlm2
, 1, strlen (mmdlm2
), pf
);
652 if (fflush (pf
) || ferror (pf
)) {
656 adios (packfile
, "write error on");
658 map_write (packfile
, pd
, 0, 0L, start
, stop
, pos
, size
, noisy
);
660 if (ferror(pf
) || fclose (pf
)) {
665 adios (cp
, "write error on");
670 if (trnflag
&& pop_dele (i
) == NOTOK
)
671 adios (NULL
, "%s", response
);
674 if (pop_quit () == NOTOK
)
675 adios (NULL
, "%s", response
);
677 mbx_close (packfile
, pd
);
683 * Get the mail from file (usually mail spool)
685 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
686 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
687 hghnum
= msgnum
= mp
->hghmsg
;
689 /* create scanline for new message */
690 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
691 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
)) {
698 fputs ("inc aborted!\n", aud
);
699 advise (NULL
, "aborted!"); /* doesn't clean up locks! */
703 advise (NULL
, "BUG in %s, number out of range", invo_name
);
707 advise (NULL
, "BUG in %s, scan() botch (%d)", invo_name
, incerr
);
713 * Run the external program hook on the message.
716 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
717 (void)ext_hook("add-hook", b
, (char *)0);
727 /* If we get here there was some sort of error from scan(),
728 * so stop processing anything more from the spool.
732 } else if (inc_type
== INC_FILE
) { /* Maildir inbox to process */
737 hghnum
= msgnum
= mp
->hghmsg
;
738 for (i
= 0; i
< num_maildir_entries
; i
++) {
741 sp
= Maildir
[i
].filename
;
742 cp
= getcpy (m_name (msgnum
));
744 if (!trnflag
|| link(sp
, cp
) == -1) {
745 static char buf
[65536];
748 if ((sf
= fopen (sp
, "r")) == NULL
)
749 adios (sp
, "unable to read for copy");
750 if ((pf
= fopen (cp
, "w+")) == NULL
)
751 adios (cp
, "unable to write for copy");
752 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
753 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
755 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
757 fclose(pf
); fclose(sf
); unlink(cp
);
759 adios(cp
, "copy error %s -> %s", sp
, cp
);
764 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
765 adios (cp
, "not available");
766 chmod (cp
, m_gmprot ());
768 fseek (pf
, 0L, SEEK_SET
);
769 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
770 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
771 1, NULL
, stop
- start
, noisy
)) {
773 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
779 /* advise (cp, "unable to read"); already advised */
790 * Run the external program hook on the message.
793 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
794 (void)ext_hook("add-hook", b
, (char *)0);
802 if (ferror(pf
) || fclose (pf
)) {
806 adios (cp
, "write error on");
811 if (trnflag
&& unlink (sp
) == NOTOK
)
812 adios (sp
, "couldn't unlink");
813 free (sp
); /* Free Maildir[i]->filename */
815 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
820 if (incerr
< 0) { /* error */
822 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
823 (void) lkfclosespool (in
, newmail
); in
= NULL
;
824 DROPGROUPPRIVS(); /* And then return us to normal privileges */
826 fclose (in
); in
= NULL
;
828 adios (NULL
, "failed");
837 if ((inc_type
== INC_POP
) && packfile
)
841 * truncate file we are incorporating from
843 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
845 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
846 advise (NULL
, "new messages have arrived!\007");
849 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
852 admonish (newmail
, "error zero'ing");
853 unlink(map_name(newmail
));
857 printf ("%s not zero'd\n", newmail
);
861 if (msgnum
== hghnum
) {
862 admonish (NULL
, "no messages incorporated");
865 * Lock the sequence file now, and loop to set the right flags
866 * in the folder structure
872 context_replace (pfolder
, folder
); /* update current folder */
874 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
875 admonish(NULL
, "Unable to reread folder %s", folder
);
880 * Shouldn't happen, but just in case ...
883 if (msgnum
>= mp2
->hghoff
884 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
885 advise (NULL
, "unable to reallocate folder storage");
890 mp2
->curmsg
= hghnum
+ 1;
891 mp2
->hghmsg
= msgnum
;
893 if (mp2
->lowmsg
== 0)
895 if (chgflag
) /* sigh... */
896 seq_setcur (mp2
, mp2
->curmsg
);
898 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
899 clear_msg_flags (mp2
, i
);
903 mp2
->msgflags
|= SEQMOD
;
904 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
905 seq_save(mp2
); /* Save the sequence file */
911 * unlock the mail spool
913 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
915 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
916 (void) lkfclosespool (in
, newmail
); in
= NULL
;
917 DROPGROUPPRIVS(); /* And then return us to normal privileges */
919 fclose (in
); in
= NULL
;
923 context_save (); /* save the context file */
930 inc_done (int status
)
932 if (packfile
&& pd
!= NOTOK
)
933 mbx_close (packfile
, pd
);
937 lkfclosespool(in
, newmail
);
946 fprintf (pf
, "%s\n", s
);
947 stop
+= strlen (s
) + 1;
948 return 0; /* Is return value used? This was missing before 1999-07-15. */
957 snprintf (buffer
, sizeof(buffer
), "%s\n", s
);
958 for (j
= 0; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
960 for (j
= 0; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
963 size
+= strlen (buffer
) + 1;
964 return 0; /* Is return value used? This was missing before 1999-07-15. */
975 if (stat (packfile
, &st
) == NOTOK
)
977 if ((md
= open (cp
= map_name (packfile
), O_RDONLY
)) == NOTOK
978 || map_chk (cp
, md
, &d
, (long) st
.st_size
, 1)) {