]>
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
33 #include <h/dropsbr.h>
35 #include <h/fmt_scan.h>
36 #include <h/scansbr.h>
37 #include <h/signals.h>
44 # define SASLminc(a) (a)
46 # define SASLminc(a) 0
49 extern m_getfld_state_t gstate
;
51 static struct swit switches
[] = {
53 { "audit audit-file", 0 },
63 { "form formatfile", 0 },
65 { "format string", 5 },
67 { "host hostname", 0 },
69 { "user username", 0 },
75 { "port name/number", 0 },
85 { "width columns", 0 },
93 { "sasl", SASLminc(-4) },
95 { "nosasl", SASLminc(-6) },
97 { "saslmech", SASLminc(-8) },
99 { "proxy command", 0 },
104 * flags for the mail source
110 static struct Maildir_entry
{
114 static int num_maildir_entries
= 0;
115 static int snoop
= 0;
117 extern char response
[];
122 static int mbx_style
= MMDF_FORMAT
;
123 static int pd
= NOTOK
;
128 static char *packfile
= NULL
;
129 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 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
136 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
137 * do the obvious thing.
139 * There's probably a better implementation if we're allowed to use
140 * BSD-style setreuid() rather than using POSIX saved-ids.
141 * Anyway, if you're euid root it's a bit pointless to drop the group
144 * I'm pretty happy that the security is good provided we aren't setuid root.
145 * The only things we trust with group=mail privilege are lkfopen()
150 * For setting and returning to "mail" gid
153 static int return_gid
;
154 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
155 #define DROPGROUPPRIVS() setgid(getgid())
156 #define GETGROUPPRIVS() setgid(return_gid)
157 #define SAVEGROUPPRIVS() return_gid = getegid()
159 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
160 #define TRYDROPGROUPPRIVS()
161 #define DROPGROUPPRIVS()
162 #define GETGROUPPRIVS()
163 #define SAVEGROUPPRIVS()
164 #endif /* not MAILGROUP */
166 /* these variables have to be globals so that done() can correctly clean up the lockfile */
167 static int locked
= 0;
168 static char *newmail
;
174 char *map_name(char *);
176 static void inc_done(int) NORETURN
;
177 static int pop_action(char *);
178 static int pop_pack(char *);
179 static int map_count(void);
182 maildir_srt(const void *va
, const void *vb
)
184 const struct Maildir_entry
*a
= va
, *b
= vb
;
185 if (a
->mtime
> b
->mtime
)
187 else if (a
->mtime
< b
->mtime
)
194 main (int argc
, char **argv
)
196 int chgflag
= 1, trnflag
= 1;
197 int noisy
= 1, width
= 0;
198 int hghnum
= 0, msgnum
= 0;
200 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
201 char *cp
, *maildir
= NULL
, *folder
= NULL
;
202 char *format
= NULL
, *form
= NULL
;
203 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
204 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
;
205 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
206 struct msgs
*mp
= NULL
;
209 char b
[PATH_MAX
+ 1];
210 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
214 char *MAILHOST_env_variable
;
218 /* absolutely the first thing we do is save our privileges,
219 * and drop them if we can.
225 setlocale(LC_ALL
, "");
227 invo_name
= r1bindex (argv
[0], '/');
229 /* read user profile/context */
232 mts_init (invo_name
);
233 arguments
= getarguments (invo_name
, argc
, argv
, 1);
238 * use MAILHOST environment variable if present,
240 * If that fails, use the default (if any)
241 * provided by mts.conf in mts_init()
243 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
244 pophost
= MAILHOST_env_variable
;
246 * If there is a valid "pophost" entry in mts.conf,
247 * then use it as the default host.
249 if (pophost
&& *pophost
)
252 while ((cp
= *argp
++)) {
254 switch (smatch (++cp
, switches
)) {
256 ambigsw (cp
, switches
);
259 adios (NULL
, "-%s unknown", cp
);
262 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
263 print_help (buf
, switches
, 1);
266 print_version(invo_name
);
270 if (!(cp
= *argp
++) || *cp
== '-')
271 adios (NULL
, "missing argument to %s", argp
[-2]);
272 audfile
= getcpy (m_maildir (cp
));
286 * The flag `trnflag' has the value:
288 * 2 if -truncate is given
289 * 1 by default (truncating is default)
290 * 0 if -notruncate is given
300 if (!(cp
= *argp
++) || *cp
== '-')
301 adios (NULL
, "missing argument to %s", argp
[-2]);
302 from
= path (cp
, TFILE
);
305 * If the truncate file is in default state,
306 * change to not truncate.
320 if (!(form
= *argp
++) || *form
== '-')
321 adios (NULL
, "missing argument to %s", argp
[-2]);
325 if (!(format
= *argp
++) || *format
== '-')
326 adios (NULL
, "missing argument to %s", argp
[-2]);
331 if (!(cp
= *argp
++) || *cp
== '-')
332 adios (NULL
, "missing argument to %s", argp
[-2]);
337 if (!(host
= *argp
++) || *host
== '-')
338 adios (NULL
, "missing argument to %s", argp
[-2]);
342 if (!(port
= *argp
++) || *port
== '-')
343 adios (NULL
, "missing argument to %s", argp
[-2]);
347 if (!(user
= *argp
++) || *user
== '-')
348 adios (NULL
, "missing argument to %s", argp
[-2]);
352 if (!(packfile
= *argp
++) || *packfile
== '-')
353 adios (NULL
, "missing argument to %s", argp
[-2]);
371 if (!(saslmech
= *argp
++) || *saslmech
== '-')
372 adios (NULL
, "missing argument to %s", argp
[-2]);
375 if (!(proxy
= *argp
++) || *proxy
== '-')
376 adios (NULL
, "missing argument to %s", argp
[-2]);
380 if (*cp
== '+' || *cp
== '@') {
382 adios (NULL
, "only one folder at a time!");
384 folder
= pluspath (cp
);
386 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
390 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
391 * not DROPGROUPPRIVS().
396 /* guarantee dropping group priveleges; we might not have done so earlier */
400 * Where are we getting the new mail?
410 * Are we getting the mail from
413 if (inc_type
== INC_POP
) {
415 user
= getusername ();
417 pass
= getusername ();
419 ruserpass (host
, &user
, &pass
);
422 * initialize POP connection
424 if (pop_init (host
, port
, user
, pass
, proxy
, snoop
, sasl
,
426 adios (NULL
, "%s", response
);
428 /* Check if there are any messages */
429 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
430 adios (NULL
, "%s", response
);
434 adios (NULL
, "no mail to incorporate");
439 * We will get the mail from a file
440 * (typically the standard maildrop)
443 if (inc_type
== INC_FILE
) {
446 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
447 newmail
= m_mailpath (newmail
);
448 else if ((newmail
= context_find ("maildrop")) && *newmail
)
449 newmail
= m_mailpath (newmail
);
451 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
453 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
454 adios (NULL
, "no mail to incorporate");
455 if (s1
.st_mode
& S_IFDIR
) {
461 cp
= concat (newmail
, "/new", NULL
);
462 if ((md
= opendir(cp
)) == NULL
)
463 adios (NULL
, "unable to open %s", cp
);
464 while ((de
= readdir (md
)) != NULL
) {
465 if (de
->d_name
[0] == '.')
467 if (i
>= num_maildir_entries
) {
468 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
469 adios(NULL
, "not enough memory for %d messages", 2*i
+16);
470 num_maildir_entries
= 2*i
+16;
472 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
473 if (stat(Maildir
[i
].filename
, &ms
) != 0)
474 adios (Maildir
[i
].filename
, "couldn't get delivery time");
475 Maildir
[i
].mtime
= ms
.st_mtime
;
480 cp
= concat (newmail
, "/cur", NULL
);
481 if ((md
= opendir(cp
)) == NULL
)
482 adios (NULL
, "unable to open %s", cp
);
483 while ((de
= readdir (md
)) != NULL
) {
484 if (de
->d_name
[0] == '.')
486 if (i
>= num_maildir_entries
) {
487 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
488 adios(NULL
, "not enough memory for %d messages", 2*i
+16);
489 num_maildir_entries
= 2*i
+16;
491 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
492 if (stat(Maildir
[i
].filename
, &ms
) != 0)
493 adios (Maildir
[i
].filename
, "couldn't get delivery time");
494 Maildir
[i
].mtime
= ms
.st_mtime
;
500 adios (NULL
, "no mail to incorporate");
501 num_maildir_entries
= i
;
502 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
505 if ((cp
= strdup(newmail
)) == (char *)0)
506 adios (NULL
, "error allocating memory to copy newmail");
511 /* skip the folder setup */
512 if ((inc_type
== INC_POP
) && packfile
)
515 if (!context_find ("path"))
516 free (path ("./", TFOLDER
));
518 folder
= getfolder (0);
519 maildir
= m_maildir (folder
);
521 if ((maildir_copy
= strdup(maildir
)) == (char *)0)
522 adios (maildir
, "error allocating memory to copy maildir");
524 if (!folder_exists(maildir
)) {
525 /* If the folder doesn't exist, and we're given the -silent flag,
529 create_folder(maildir
, 0, done
);
534 if (chdir (maildir
) == NOTOK
)
535 adios (maildir
, "unable to change directory to");
537 /* read folder and create message structure */
538 if (!(mp
= folder_read (folder
)))
539 adios (NULL
, "unable to read folder %s", folder
);
543 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
544 if (access (newmail
, W_OK
) != NOTOK
) {
547 SIGNAL (SIGHUP
, SIG_IGN
);
548 SIGNAL (SIGINT
, SIG_IGN
);
549 SIGNAL (SIGQUIT
, SIG_IGN
);
550 SIGNAL (SIGTERM
, SIG_IGN
);
553 GETGROUPPRIVS(); /* Reset gid to lock mail file */
554 in
= lkfopen (newmail
, "r");
557 adios (NULL
, "unable to lock and fopen %s", newmail
);
558 fstat (fileno(in
), &s1
);
561 if ((in
= fopen (newmail
, "r")) == NULL
)
562 adios (newmail
, "unable to read");
566 /* This shouldn't be necessary but it can't hurt. */
571 if ((i
= stat (audfile
, &st
)) == NOTOK
)
572 advise (NULL
, "Creating Receive-Audit: %s", audfile
);
573 if ((aud
= fopen (audfile
, "a")) == NULL
)
574 adios (audfile
, "unable to append to");
576 chmod (audfile
, m_gmprot ());
578 fprintf (aud
, from
? "<<inc>> %s -ms %s\n"
579 : host
? "<<inc>> %s -host %s -user %s\n"
581 dtimenow (0), from
? from
: host
, user
);
584 /* Get new format string */
585 nfs
= new_fs (form
, format
, FORMAT
);
588 printf ("Incorporating new mail into %s...\n\n", folder
);
592 m_getfld_state_init (&gstate
);
595 * Get the mail from a POP server
597 if (inc_type
== INC_POP
) {
600 packfile
= path (packfile
, TFILE
);
601 if (stat (packfile
, &st
) == NOTOK
) {
603 adios (packfile
, "error on file");
604 cp
= concat ("Create file \"", packfile
, "\"? ", NULL
);
605 if (noisy
&& !getanswer (cp
))
609 msgnum
= map_count ();
610 if ((pd
= mbx_open (packfile
, mbx_style
, getuid(), getgid(), m_gmprot()))
612 adios (packfile
, "unable to open");
613 if ((pf
= fdopen (pd
, "w+")) == NULL
)
614 adios (NULL
, "unable to fdopen %s", packfile
);
616 hghnum
= msgnum
= mp
->hghmsg
;
618 * Check if we have enough message space for all the new
619 * messages. If not, then realloc the folder and add enough
620 * space for all new messages plus 10 additional slots.
622 if (mp
->hghmsg
+ nmsgs
>= mp
->hghoff
623 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ nmsgs
+ 10)))
624 adios (NULL
, "unable to allocate folder storage");
627 for (i
= 1; i
<= nmsgs
; i
++) {
630 fseek (pf
, 0L, SEEK_CUR
);
633 fwrite (mmdlm1
, 1, strlen (mmdlm1
), pf
);
636 if (pop_retr (i
, pop_pack
) == NOTOK
)
637 adios (NULL
, "%s", response
);
639 fseek (pf
, 0L, SEEK_CUR
);
642 adios (packfile
, "write error on");
643 fseek (pf
, start
, SEEK_SET
);
645 cp
= getcpy (m_name (msgnum
));
646 if ((pf
= fopen (cp
, "w+")) == NULL
)
647 adios (cp
, "unable to write");
648 chmod (cp
, m_gmprot ());
651 if (pop_retr (i
, pop_action
) == NOTOK
)
652 adios (NULL
, "%s", response
);
655 adios (cp
, "write error on");
656 fseek (pf
, 0L, SEEK_SET
);
658 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
659 packfile
? 0 : msgnum
== mp
->hghmsg
+ 1 && chgflag
,
660 1, NULL
, stop
- start
, noisy
)) {
662 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
668 /* advise (cp, "unable to read"); already advised */
683 clear_msg_flags (mp
, msgnum
);
684 set_exists (mp
, msgnum
);
685 set_unseen (mp
, msgnum
);
686 mp
->msgflags
|= SEQMOD
;
691 fseek (pf
, stop
, SEEK_SET
);
692 fwrite (mmdlm2
, 1, strlen (mmdlm2
), pf
);
693 if (fflush (pf
) || ferror (pf
)) {
697 adios (packfile
, "write error on");
699 map_write (packfile
, pd
, 0, 0L, start
, stop
, pos
, size
, noisy
);
701 if (ferror(pf
) || fclose (pf
)) {
706 adios (cp
, "write error on");
711 if (trnflag
&& pop_dele (i
) == NOTOK
)
712 adios (NULL
, "%s", response
);
715 if (pop_quit () == NOTOK
)
716 adios (NULL
, "%s", response
);
718 mbx_close (packfile
, pd
);
724 * Get the mail from file (usually mail spool)
726 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
727 m_unknown (gstate
, in
); /* the MAGIC invocation... */
728 hghnum
= msgnum
= mp
->hghmsg
;
731 * Check if we need to allocate more space for message status.
732 * If so, then add space for an additional 100 messages.
734 if (msgnum
>= mp
->hghoff
735 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghoff
+ 100))) {
736 advise (NULL
, "unable to allocate folder storage");
741 /* create scanline for new message */
742 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
743 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
)) {
750 fputs ("inc aborted!\n", aud
);
751 advise (NULL
, "aborted!"); /* doesn't clean up locks! */
755 advise (NULL
, "BUG in %s, number out of range", invo_name
);
759 advise (NULL
, "BUG in %s, scan() botch (%d)", invo_name
, incerr
);
765 * Run the external program hook on the message.
768 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
769 (void)ext_hook("add-hook", b
, (char *)0);
779 if (mp
->lowmsg
== 0) mp
->lowmsg
= 1;
781 clear_msg_flags (mp
, msgnum
);
782 set_exists (mp
, msgnum
);
783 set_unseen (mp
, msgnum
);
784 mp
->msgflags
|= SEQMOD
;
787 /* If we get here there was some sort of error from scan(),
788 * so stop processing anything more from the spool.
792 } else if (inc_type
== INC_FILE
) { /* Maildir inbox to process */
797 hghnum
= msgnum
= mp
->hghmsg
;
798 for (i
= 0; i
< num_maildir_entries
; i
++) {
801 * Check if we need to allocate more space for message status.
802 * If so, then add space for an additional 100 messages.
804 if (msgnum
>= mp
->hghoff
805 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghoff
+ 100))) {
806 advise (NULL
, "unable to allocate folder storage");
811 sp
= Maildir
[i
].filename
;
812 cp
= getcpy (m_name (msgnum
));
814 if (!trnflag
|| link(sp
, cp
) == -1) {
815 static char buf
[65536];
818 if ((sf
= fopen (sp
, "r")) == NULL
)
819 adios (sp
, "unable to read for copy");
820 if ((pf
= fopen (cp
, "w+")) == NULL
)
821 adios (cp
, "unable to write for copy");
822 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
823 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
825 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
827 fclose(pf
); fclose(sf
); unlink(cp
);
829 adios(cp
, "copy error %s -> %s", sp
, cp
);
834 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
835 adios (cp
, "not available");
836 chmod (cp
, m_gmprot ());
838 fseek (pf
, 0L, SEEK_SET
);
839 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
840 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
841 1, NULL
, stop
- start
, noisy
)) {
843 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
849 /* advise (cp, "unable to read"); already advised */
860 * Run the external program hook on the message.
863 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
864 (void)ext_hook("add-hook", b
, (char *)0);
871 clear_msg_flags (mp
, msgnum
);
872 set_exists (mp
, msgnum
);
873 set_unseen (mp
, msgnum
);
874 mp
->msgflags
|= SEQMOD
;
878 if (ferror(pf
) || fclose (pf
)) {
882 adios (cp
, "write error on");
887 if (trnflag
&& unlink (sp
) == NOTOK
)
888 adios (sp
, "couldn't unlink");
889 free (sp
); /* Free Maildir[i]->filename */
891 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
894 m_getfld_state_destroy (&gstate
);
896 if (incerr
< 0) { /* error */
898 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
899 (void) lkfclose (in
, newmail
); in
= NULL
;
900 DROPGROUPPRIVS(); /* And then return us to normal privileges */
902 fclose (in
); in
= NULL
;
904 adios (NULL
, "failed");
913 if ((inc_type
== INC_POP
) && packfile
)
917 * truncate file we are incorporating from
919 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
921 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
922 advise (NULL
, "new messages have arrived!\007");
925 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
928 admonish (newmail
, "error zero'ing");
929 unlink(map_name(newmail
));
933 printf ("%s not zero'd\n", newmail
);
937 if (msgnum
== hghnum
) {
938 admonish (NULL
, "no messages incorporated");
940 context_replace (pfolder
, folder
); /* update current folder */
942 mp
->curmsg
= hghnum
+ 1;
946 if (chgflag
) /* sigh... */
947 seq_setcur (mp
, mp
->curmsg
);
951 * unlock the mail spool
953 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
955 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
956 (void) lkfclose (in
, newmail
); in
= NULL
;
957 DROPGROUPPRIVS(); /* And then return us to normal privileges */
959 fclose (in
); in
= NULL
;
963 seq_setunseen (mp
, 0); /* set the Unseen-Sequence */
964 seq_save (mp
); /* synchronize sequences */
965 context_save (); /* save the context file */
972 inc_done (int status
)
974 if (packfile
&& pd
!= NOTOK
)
975 mbx_close (packfile
, pd
);
979 lkfclose(in
, newmail
);
988 fprintf (pf
, "%s\n", s
);
989 stop
+= strlen (s
) + 1;
990 return 0; /* Is return value used? This was missing before 1999-07-15. */
999 snprintf (buffer
, sizeof(buffer
), "%s\n", s
);
1000 for (j
= 0; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
1002 for (j
= 0; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
1005 size
+= strlen (buffer
) + 1;
1006 return 0; /* Is return value used? This was missing before 1999-07-15. */
1017 if (stat (packfile
, &st
) == NOTOK
)
1019 if ((md
= open (cp
= map_name (packfile
), O_RDONLY
)) == NOTOK
1020 || map_chk (cp
, md
, &d
, (long) st
.st_size
, 1)) {