]>
diplodocus.org Git - nmh/blob - uip/inc.c
3 * inc.c -- incorporate messages from a maildrop into a folder
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 /* Revised: Sat Apr 14 17:08:17 PDT 1990 (marvit@hplabs)
14 * Added hpux hacks to set and reset gid to be "mail" as needed. The reset
15 * is necessary so inc'ed mail is the group of the inc'er, rather than
16 * "mail". We setgid to egid only when [un]locking the mail file. This
17 * is also a major security precaution which will not be explained here.
19 * Fri Feb 7 16:04:57 PST 1992 John Romine <bug-mh@ics.uci.edu>
20 * NB: I'm not 100% sure that this setgid stuff is secure even now.
22 * See the *GROUPPRIVS() macros later. I'm reasonably happy with the setgid
23 * attribute. Running setuid root is probably not a terribly good idea, though.
24 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk>, 04/1998
26 * Peter Maydell's patch slightly modified for nmh 0.28-pre2.
27 * Ruud de Rooij <ruud@debian.org> Wed, 22 Jul 1998 13:24:22 +0200
36 # include <h/dropsbr.h>
37 # include <h/popsbr.h>
44 #include <h/fmt_scan.h>
45 #include <h/scansbr.h>
46 #include <h/signals.h>
53 # define POPminc(a) (a)
59 # define RPOPminc(a) (a)
61 # define RPOPminc(a) 0
65 # define APOPminc(a) (a)
67 # define APOPminc(a) 0
71 # define KPOPminc(a) (a)
73 # define KPOPminc(a) 0
77 # define SASLminc(a) (a)
79 # define SASLminc(a) 0
82 static struct swit switches
[] = {
84 { "audit audit-file", 0 },
94 { "form formatfile", 0 },
96 { "format string", 5 },
98 { "host hostname", POPminc (-4) },
100 { "user username", POPminc (-4) },
102 { "pack file", POPminc (-4) },
104 { "nopack", POPminc (-6) },
106 { "apop", APOPminc (-4) },
108 { "noapop", APOPminc (-6) },
110 { "rpop", RPOPminc (-4) },
112 { "norpop", RPOPminc (-6) },
122 { "width columns", 0 },
130 { "kpop", KPOPminc (-4) },
132 { "sasl", SASLminc(-4) },
133 #define SASLMECHSW 25
134 { "saslmech", SASLminc(-8) },
136 { "proxy command", POPminc(-5) },
141 * flags for the mail source
147 static int snoop
= 0;
150 extern char response
[];
152 static char *packfile
= NULL
;
158 static int mbx_style
= MMDF_FORMAT
;
159 static int pd
= NOTOK
;
160 static FILE *pf
= NULL
;
163 /* This is an attempt to simplify things by putting all the
164 * privilege ops into macros.
165 * *GROUPPRIVS() is related to handling the setgid MAIL property,
166 * and only applies if MAILGROUP is defined.
167 * *USERPRIVS() is related to handling the setuid root property,
168 * and only applies if POP is defined [why does POP => setuid root?]
169 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
170 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
171 * do the obvious thing. TRYDROPGROUPPRIVS() has to be safe to call
172 * before DROPUSERPRIVS() is called [this is needed because setgid()
173 * sets both effective and real uids if euid is root.]
175 * There's probably a better implementation if we're allowed to use
176 * BSD-style setreuid() rather than using POSIX saved-ids.
177 * Anyway, if you're euid root it's a bit pointless to drop the group
180 * I'm pretty happy that the security is good provided we aren't setuid root.
181 * The only things we trust with group=mail privilege are lkfopen()
186 * For setting and returning to "mail" gid
189 static int return_gid
;
191 /* easy case; we're not setuid root, so can drop group privs
194 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
195 #else /* POP ie we are setuid root */
196 #define TRYDROPGROUPPRIVS() \
197 if (geteuid() != 0) DROPGROUPPRIVS()
199 #define DROPGROUPPRIVS() setgid(getgid())
200 #define GETGROUPPRIVS() setgid(return_gid)
201 #define SAVEGROUPPRIVS() return_gid = getegid()
203 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
204 #define TRYDROPGROUPPRIVS()
205 #define DROPGROUPPRIVS()
206 #define GETGROUPPRIVS()
207 #define SAVEGROUPPRIVS()
208 #endif /* not MAILGROUP */
211 #define DROPUSERPRIVS() setuid(getuid())
213 #define DROPUSERPRIVS()
216 /* these variables have to be globals so that done() can correctly clean up the lockfile */
217 static int locked
= 0;
218 static char *newmail
;
224 char *map_name(char *);
226 static void inc_done(int);
228 static int pop_action(char *);
229 static int pop_pack(char *);
230 static int map_count(void);
235 main (int argc
, char **argv
)
237 int chgflag
= 1, trnflag
= 1;
238 int noisy
= 1, width
= 0;
239 int rpop
, i
, hghnum
, msgnum
;
240 int kpop
= 0, sasl
= 0;
241 char *cp
, *maildir
, *folder
= NULL
;
242 char *format
= NULL
, *form
= NULL
;
243 char *host
= NULL
, *user
= NULL
, *proxy
= NULL
;
244 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
;
245 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
249 char b
[MAXPATHLEN
+ 1];
250 char *maildir_copy
; /* copy of mail directory because the static gets overwritten */
253 int nmsgs
, nbytes
, p
= 0;
255 char *MAILHOST_env_variable
;
263 struct hes_postoffice
*po
;
268 /* absolutely the first thing we do is save our privileges,
269 * and drop them if we can.
275 setlocale(LC_ALL
, "");
277 invo_name
= r1bindex (argv
[0], '/');
279 /* read user profile/context */
282 mts_init (invo_name
);
283 arguments
= getarguments (invo_name
, argc
, argv
, 1);
289 * use MAILHOST environment variable if present,
291 * If that fails, use the default (if any)
292 * provided by mts.conf in mts_init()
294 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
295 pophost
= MAILHOST_env_variable
;
297 else if ((po
= hes_getmailhost(getusername())) != NULL
&&
298 strcmp(po
->po_type
, "POP") == 0)
299 pophost
= po
->po_host
;
302 * If there is a valid "pophost" entry in mts.conf,
303 * then use it as the default host.
305 if (pophost
&& *pophost
)
308 if ((cp
= getenv ("MHPOPDEBUG")) && *cp
)
314 while ((cp
= *argp
++)) {
316 switch (smatch (++cp
, switches
)) {
318 ambigsw (cp
, switches
);
321 adios (NULL
, "-%s unknown", cp
);
324 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
325 print_help (buf
, switches
, 1);
328 print_version(invo_name
);
332 if (!(cp
= *argp
++) || *cp
== '-')
333 adios (NULL
, "missing argument to %s", argp
[-2]);
334 audfile
= getcpy (m_maildir (cp
));
348 * The flag `trnflag' has the value:
350 * 2 if -truncate is given
351 * 1 by default (truncating is default)
352 * 0 if -notruncate is given
362 if (!(cp
= *argp
++) || *cp
== '-')
363 adios (NULL
, "missing argument to %s", argp
[-2]);
364 from
= path (cp
, TFILE
);
367 * If the truncate file is in default state,
368 * change to not truncate.
382 if (!(form
= *argp
++) || *form
== '-')
383 adios (NULL
, "missing argument to %s", argp
[-2]);
387 if (!(format
= *argp
++) || *format
== '-')
388 adios (NULL
, "missing argument to %s", argp
[-2]);
393 if (!(cp
= *argp
++) || *cp
== '-')
394 adios (NULL
, "missing argument to %s", argp
[-2]);
399 if (!(host
= *argp
++) || *host
== '-')
400 adios (NULL
, "missing argument to %s", argp
[-2]);
403 if (!(user
= *argp
++) || *user
== '-')
404 adios (NULL
, "missing argument to %s", argp
[-2]);
409 if (!(cp
= *argp
++) || *cp
== '-')
410 adios (NULL
, "missing argument to %s", argp
[-2]);
412 if (!(packfile
= *argp
++) || *packfile
== '-')
413 adios (NULL
, "missing argument to %s", argp
[-2]);
449 if (!(saslmech
= *argp
++) || *saslmech
== '-')
450 adios (NULL
, "missing argument to %s", argp
[-2]);
453 if (!(proxy
= *argp
++) || *proxy
== '-')
454 adios (NULL
, "missing argument to %s", argp
[-2]);
458 if (*cp
== '+' || *cp
== '@') {
460 adios (NULL
, "only one folder at a time!");
462 folder
= pluspath (cp
);
464 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
468 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
469 * not DROPGROUPPRIVS().
474 if (from
|| !host
|| rpop
<= 0)
478 /* guarantee dropping group priveleges; we might not have done so earlier */
482 * Where are we getting the new mail?
495 * Are we getting the mail from
498 if (inc_type
== INC_POP
) {
500 user
= getusername ();
501 if ( strcmp( POPSERVICE
, "kpop" ) == 0 ) {
504 if (kpop
|| sasl
|| ( rpop
> 0))
505 pass
= getusername ();
507 ruserpass (host
, &user
, &pass
);
510 * initialize POP connection
512 if (pop_init (host
, user
, pass
, proxy
, snoop
, kpop
? 1 : rpop
, kpop
,
513 sasl
, saslmech
) == NOTOK
)
514 adios (NULL
, "%s", response
);
516 /* Check if there are any messages */
517 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
518 adios (NULL
, "%s", response
);
524 adios (NULL
, "no mail to incorporate");
530 * We will get the mail from a file
531 * (typically the standard maildrop)
534 if (inc_type
== INC_FILE
) {
537 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
538 newmail
= m_mailpath (newmail
);
539 else if ((newmail
= context_find ("maildrop")) && *newmail
)
540 newmail
= m_mailpath (newmail
);
542 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
544 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
545 adios (NULL
, "no mail to incorporate");
547 if ((cp
= strdup(newmail
)) == (char *)0)
548 adios (maildir
, "error allocating memory to copy newmail");
554 /* skip the folder setup */
555 if ((inc_type
== INC_POP
) && packfile
)
559 if (!context_find ("path"))
560 free (path ("./", TFOLDER
));
562 folder
= getfolder (0);
563 maildir
= m_maildir (folder
);
565 if ((maildir_copy
= strdup(maildir
)) == (char *)0)
566 adios (maildir
, "error allocating memory to copy maildir");
568 if (!folder_exists(maildir
)) {
569 /* If the folder doesn't exist, and we're given the -silent flag,
573 create_folder(maildir
, 0, done
);
578 if (chdir (maildir
) == NOTOK
)
579 adios (maildir
, "unable to change directory to");
581 /* read folder and create message structure */
582 if (!(mp
= folder_read (folder
)))
583 adios (NULL
, "unable to read folder %s", folder
);
589 if (inc_type
== INC_FILE
) {
590 if (access (newmail
, W_OK
) != NOTOK
) {
593 SIGNAL (SIGHUP
, SIG_IGN
);
594 SIGNAL (SIGINT
, SIG_IGN
);
595 SIGNAL (SIGQUIT
, SIG_IGN
);
596 SIGNAL (SIGTERM
, SIG_IGN
);
599 GETGROUPPRIVS(); /* Reset gid to lock mail file */
600 in
= lkfopen (newmail
, "r");
603 adios (NULL
, "unable to lock and fopen %s", newmail
);
604 fstat (fileno(in
), &s1
);
607 if ((in
= fopen (newmail
, "r")) == NULL
)
608 adios (newmail
, "unable to read");
612 /* This shouldn't be necessary but it can't hurt. */
616 if ((i
= stat (audfile
, &st
)) == NOTOK
)
617 advise (NULL
, "Creating Receive-Audit: %s", audfile
);
618 if ((aud
= fopen (audfile
, "a")) == NULL
)
619 adios (audfile
, "unable to append to");
621 chmod (audfile
, m_gmprot ());
624 fprintf (aud
, from
? "<<inc>> %s -ms %s\n"
625 : host
? "<<inc>> %s -host %s -user %s%s\n"
627 dtimenow (0), from
? from
: host
, user
,
628 rpop
< 0 ? " -apop" : rpop
> 0 ? " -rpop" : "");
630 fprintf (aud
, from
? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
636 if (context_find ("mhe")) {
637 cp
= concat (maildir
, "/++", NULL
);
639 if ((mhe
= fopen (cp
, "a")) == NULL
)
640 admonish (cp
, "unable to append to");
643 chmod (cp
, m_gmprot ());
648 /* Get new format string */
649 nfs
= new_fs (form
, format
, FORMAT
);
652 printf ("Incorporating new mail into %s...\n\n", folder
);
658 * Get the mail from a POP server
660 if (inc_type
== INC_POP
) {
662 packfile
= path (packfile
, TFILE
);
663 if (stat (packfile
, &st
) == NOTOK
) {
665 adios (packfile
, "error on file");
666 cp
= concat ("Create file \"", packfile
, "\"? ", NULL
);
667 if (noisy
&& !getanswer (cp
))
671 msgnum
= map_count ();
672 if ((pd
= mbx_open (packfile
, mbx_style
, getuid(), getgid(), m_gmprot()))
674 adios (packfile
, "unable to open");
675 if ((pf
= fdopen (pd
, "w+")) == NULL
)
676 adios (NULL
, "unable to fdopen %s", packfile
);
678 hghnum
= msgnum
= mp
->hghmsg
;
680 * Check if we have enough message space for all the new
681 * messages. If not, then realloc the folder and add enough
682 * space for all new messages plus 10 additional slots.
684 if (mp
->hghmsg
+ nmsgs
>= mp
->hghoff
685 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ nmsgs
+ 10)))
686 adios (NULL
, "unable to allocate folder storage");
689 for (i
= 1; i
<= nmsgs
; i
++) {
692 fseek (pf
, 0L, SEEK_CUR
);
695 fwrite (mmdlm1
, 1, strlen (mmdlm1
), pf
);
698 if (pop_retr (i
, pop_pack
) == NOTOK
)
699 adios (NULL
, "%s", response
);
701 fseek (pf
, 0L, SEEK_CUR
);
704 adios (packfile
, "write error on");
705 fseek (pf
, start
, SEEK_SET
);
707 cp
= getcpy (m_name (msgnum
));
708 if ((pf
= fopen (cp
, "w+")) == NULL
)
709 adios (cp
, "unable to write");
710 chmod (cp
, m_gmprot ());
713 if (pop_retr (i
, pop_action
) == NOTOK
)
714 adios (NULL
, "%s", response
);
717 adios (cp
, "write error on");
718 fseek (pf
, 0L, SEEK_SET
);
720 switch (p
= scan (pf
, msgnum
, 0, nfs
, width
,
721 packfile
? 0 : msgnum
== mp
->hghmsg
+ 1 && chgflag
,
722 1, NULL
, stop
- start
, noisy
)) {
724 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
730 /* advise (cp, "unable to read"); already advised */
749 clear_msg_flags (mp
, msgnum
);
750 set_exists (mp
, msgnum
);
751 set_unseen (mp
, msgnum
);
752 mp
->msgflags
|= SEQMOD
;
757 fseek (pf
, stop
, SEEK_SET
);
758 fwrite (mmdlm2
, 1, strlen (mmdlm2
), pf
);
759 if (fflush (pf
) || ferror (pf
)) {
763 adios (packfile
, "write error on");
765 map_write (packfile
, pd
, 0, 0L, start
, stop
, pos
, size
, noisy
);
767 if (ferror(pf
) || fclose (pf
)) {
772 adios (cp
, "write error on");
777 if (trnflag
&& pop_dele (i
) == NOTOK
)
778 adios (NULL
, "%s", response
);
781 if (pop_quit () == NOTOK
)
782 adios (NULL
, "%s", response
);
784 mbx_close (packfile
, pd
);
791 * Get the mail from file (usually mail spool)
793 if (inc_type
== INC_FILE
) {
794 m_unknown (in
); /* the MAGIC invocation... */
795 hghnum
= msgnum
= mp
->hghmsg
;
798 * Check if we need to allocate more space for message status.
799 * If so, then add space for an additional 100 messages.
801 if (msgnum
>= mp
->hghoff
802 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghoff
+ 100))) {
803 advise (NULL
, "unable to allocate folder storage");
809 /* copy file from spool to tmp file */
810 tmpfilenam
= m_scratch ("", invo_name
);
811 if ((fd
= creat (tmpfilenam
, m_gmprot ())) == NOTOK
)
812 adios (tmpfilenam
, "unable to create");
813 chmod (tmpfilenam
, m_gmprot ());
814 if (!(in2
= fdopen (fd
, "r+")))
815 adios (tmpfilenam
, "unable to access");
818 /* link message into folder */
819 newmsg
= folder_addmsg(mp
, tmpfilenam
);
821 /* create scanline for new message */
822 switch (i
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
823 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
)) {
830 fputs ("inc aborted!\n", aud
);
831 advise (NULL
, "aborted!"); /* doesn't clean up locks! */
835 advise (NULL
, "BUG in %s, number out of range", invo_name
);
839 advise (NULL
, "BUG in %s, scan() botch (%d)", invo_name
, i
);
845 * Run the external program hook on the message.
848 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
849 (void)ext_hook("add-hook", b
, (char *)0);
863 if (mp
->lowmsg
== 0) mp
->lowmsg
= 1;
865 clear_msg_flags (mp
, msgnum
);
866 set_exists (mp
, msgnum
);
867 set_unseen (mp
, msgnum
);
868 mp
->msgflags
|= SEQMOD
;
876 if (p
< 0) { /* error */
878 if (i
< 0) { /* error */
881 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
882 (void) lkfclose (in
, newmail
); in
= NULL
;
883 DROPGROUPPRIVS(); /* And then return us to normal privileges */
885 fclose (in
); in
= NULL
;
887 adios (NULL
, "failed");
902 if ((inc_type
== INC_POP
) && packfile
)
907 * truncate file we are incorporating from
909 if (inc_type
== INC_FILE
) {
911 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
912 advise (NULL
, "new messages have arrived!\007");
914 if ((i
= creat (newmail
, 0600)) != NOTOK
)
917 admonish (newmail
, "error zero'ing");
918 unlink(map_name(newmail
));
922 printf ("%s not zero'd\n", newmail
);
926 if (msgnum
== hghnum
) {
927 admonish (NULL
, "no messages incorporated");
929 context_replace (pfolder
, folder
); /* update current folder */
931 mp
->curmsg
= hghnum
+ 1;
935 if (chgflag
) /* sigh... */
936 seq_setcur (mp
, mp
->curmsg
);
940 * unlock the mail spool
942 if (inc_type
== INC_FILE
) {
944 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
945 (void) lkfclose (in
, newmail
); in
= NULL
;
946 DROPGROUPPRIVS(); /* And then return us to normal privileges */
948 fclose (in
); in
= NULL
;
952 seq_setunseen (mp
, 0); /* set the Unseen-Sequence */
953 seq_save (mp
); /* synchronize sequences */
954 context_save (); /* save the context file */
963 * Copy message message from spool into
964 * temporary file. Massage the "From " line
968 cpymsg (FILE *in
, FILE *out
)
971 char *tmpbuf
, name
[NAMESZ
];
974 state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, in
);
994 inc_done (int status
)
997 if (packfile
&& pd
!= NOTOK
)
998 mbx_close (packfile
, pd
);
1003 lkfclose(in
, newmail
);
1011 pop_action (char *s
)
1013 fprintf (pf
, "%s\n", s
);
1014 stop
+= strlen (s
) + 1;
1015 return 0; /* Is return value used? This was missing before 1999-07-15. */
1022 char buffer
[BUFSIZ
];
1024 snprintf (buffer
, sizeof(buffer
), "%s\n", s
);
1025 for (j
= 0; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
1027 for (j
= 0; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
1030 size
+= strlen (buffer
) + 1;
1031 return 0; /* Is return value used? This was missing before 1999-07-15. */
1042 if (stat (packfile
, &st
) == NOTOK
)
1044 if ((md
= open (cp
= map_name (packfile
), O_RDONLY
)) == NOTOK
1045 || map_chk (cp
, md
, &d
, (long) st
.st_size
, 1)) {