]>
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
35 # include <h/dropsbr.h>
36 # include <h/popsbr.h>
43 #include <h/fmt_scan.h>
44 #include <h/scansbr.h>
45 #include <h/signals.h>
52 # define POPminc(a) (a)
58 # define RPOPminc(a) (a)
60 # define RPOPminc(a) 0
64 # define APOPminc(a) (a)
66 # define APOPminc(a) 0
70 # define KPOPminc(a) (a)
72 # define KPOPminc(a) 0
76 # define SASLminc(a) (a)
78 # define SASLminc(a) 0
81 static struct swit switches
[] = {
83 { "audit audit-file", 0 },
93 { "form formatfile", 0 },
95 { "format string", 5 },
97 { "host hostname", POPminc (-4) },
99 { "user username", POPminc (-4) },
101 { "pack file", POPminc (-4) },
103 { "nopack", POPminc (-6) },
105 { "apop", APOPminc (-4) },
107 { "noapop", APOPminc (-6) },
109 { "rpop", RPOPminc (-4) },
111 { "norpop", RPOPminc (-6) },
121 { "width columns", 0 },
129 { "kpop", KPOPminc (-4) },
131 { "sasl", SASLminc(-4) },
132 #define SASLMECHSW 25
133 { "saslmech", SASLminc(-8) },
140 * flags for the mail source
146 static int snoop
= 0;
149 extern char response
[];
151 static char *packfile
= NULL
;
157 static int mbx_style
= MMDF_FORMAT
;
158 static int pd
= NOTOK
;
159 static FILE *pf
= NULL
;
162 /* This is an attempt to simplify things by putting all the
163 * privilege ops into macros.
164 * *GROUPPRIVS() is related to handling the setgid MAIL property,
165 * and only applies if MAILGROUP is defined.
166 * *USERPRIVS() is related to handling the setuid root property,
167 * and only applies if POP is defined [why does POP => setuid root?]
168 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
169 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
170 * do the obvious thing. TRYDROPGROUPPRIVS() has to be safe to call
171 * before DROPUSERPRIVS() is called [this is needed because setgid()
172 * sets both effective and real uids if euid is root.]
174 * There's probably a better implementation if we're allowed to use
175 * BSD-style setreuid() rather than using POSIX saved-ids.
176 * Anyway, if you're euid root it's a bit pointless to drop the group
179 * I'm pretty happy that the security is good provided we aren't setuid root.
180 * The only things we trust with group=mail privilege are lkfopen()
185 * For setting and returning to "mail" gid
188 static int return_gid
;
190 /* easy case; we're not setuid root, so can drop group privs
193 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
194 #else /* POP ie we are setuid root */
195 #define TRYDROPGROUPPRIVS() \
196 if (geteuid() != 0) DROPGROUPPRIVS()
198 #define DROPGROUPPRIVS() setgid(getgid())
199 #define GETGROUPPRIVS() setgid(return_gid)
200 #define SAVEGROUPPRIVS() return_gid = getegid()
202 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
203 #define TRYDROPGROUPPRIVS()
204 #define DROPGROUPPRIVS()
205 #define GETGROUPPRIVS()
206 #define SAVEGROUPPRIVS()
207 #endif /* not MAILGROUP */
210 #define DROPUSERPRIVS() setuid(getuid())
212 #define DROPUSERPRIVS()
215 /* these variables have to be globals so that done() can correctly clean up the lockfile */
216 static int locked
= 0;
217 static char *newmail
;
223 char *map_name(char *);
227 static int pop_action(char *);
228 static int pop_pack(char *);
229 static int map_count(void);
234 main (int argc
, char **argv
)
236 int chgflag
= 1, trnflag
= 1;
237 int noisy
= 1, width
= 0;
238 int rpop
, i
, hghnum
, msgnum
;
239 int kpop
= 0, sasl
= 0;
240 char *cp
, *maildir
, *folder
= NULL
;
241 char *format
= NULL
, *form
= NULL
;
242 char *host
= NULL
, *user
= NULL
;
243 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
;
244 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
248 char b
[MAXPATHLEN
+ 1];
251 int nmsgs
, nbytes
, p
= 0;
253 char *MAILHOST_env_variable
;
261 struct hes_postoffice
*po
;
264 /* absolutely the first thing we do is save our privileges,
265 * and drop them if we can.
271 setlocale(LC_ALL
, "");
273 invo_name
= r1bindex (argv
[0], '/');
275 /* read user profile/context */
278 mts_init (invo_name
);
279 arguments
= getarguments (invo_name
, argc
, argv
, 1);
285 * use MAILHOST environment variable if present,
287 * If that fails, use the default (if any)
288 * provided by mts.conf in mts_init()
290 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
291 pophost
= MAILHOST_env_variable
;
293 else if ((po
= hes_getmailhost(getusername())) != NULL
&&
294 strcmp(po
->po_type
, "POP") == 0)
295 pophost
= po
->po_host
;
298 * If there is a valid "pophost" entry in mts.conf,
299 * then use it as the default host.
301 if (pophost
&& *pophost
)
304 if ((cp
= getenv ("MHPOPDEBUG")) && *cp
)
310 while ((cp
= *argp
++)) {
312 switch (smatch (++cp
, switches
)) {
314 ambigsw (cp
, switches
);
317 adios (NULL
, "-%s unknown", cp
);
320 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
321 print_help (buf
, switches
, 1);
324 print_version(invo_name
);
328 if (!(cp
= *argp
++) || *cp
== '-')
329 adios (NULL
, "missing argument to %s", argp
[-2]);
330 audfile
= getcpy (m_maildir (cp
));
344 * The flag `trnflag' has the value:
346 * 2 if -truncate is given
347 * 1 by default (truncating is default)
348 * 0 if -notruncate is given
358 if (!(cp
= *argp
++) || *cp
== '-')
359 adios (NULL
, "missing argument to %s", argp
[-2]);
360 from
= path (cp
, TFILE
);
363 * If the truncate file is in default state,
364 * change to not truncate.
378 if (!(form
= *argp
++) || *form
== '-')
379 adios (NULL
, "missing argument to %s", argp
[-2]);
383 if (!(format
= *argp
++) || *format
== '-')
384 adios (NULL
, "missing argument to %s", argp
[-2]);
389 if (!(cp
= *argp
++) || *cp
== '-')
390 adios (NULL
, "missing argument to %s", argp
[-2]);
395 if (!(host
= *argp
++) || *host
== '-')
396 adios (NULL
, "missing argument to %s", argp
[-2]);
399 if (!(user
= *argp
++) || *user
== '-')
400 adios (NULL
, "missing argument to %s", argp
[-2]);
405 if (!(cp
= *argp
++) || *cp
== '-')
406 adios (NULL
, "missing argument to %s", argp
[-2]);
408 if (!(packfile
= *argp
++) || *packfile
== '-')
409 adios (NULL
, "missing argument to %s", argp
[-2]);
445 if (!(saslmech
= *argp
++) || *saslmech
== '-')
446 adios (NULL
, "missing argument to %s", argp
[-2]);
450 if (*cp
== '+' || *cp
== '@') {
452 adios (NULL
, "only one folder at a time!");
454 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
456 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
460 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
461 * not DROPGROUPPRIVS().
466 if (from
|| !host
|| rpop
<= 0)
470 /* guarantee dropping group priveleges; we might not have done so earlier */
474 * Where are we getting the new mail?
487 * Are we getting the mail from
490 if (inc_type
== INC_POP
) {
492 user
= getusername ();
493 if ( strcmp( POPSERVICE
, "kpop" ) == 0 ) {
496 if (kpop
|| sasl
|| ( rpop
> 0))
497 pass
= getusername ();
499 ruserpass (host
, &user
, &pass
);
502 * initialize POP connection
504 if (pop_init (host
, user
, pass
, snoop
, kpop
? 1 : rpop
, kpop
,
505 sasl
, saslmech
) == NOTOK
)
506 adios (NULL
, "%s", response
);
508 /* Check if there are any messages */
509 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
510 adios (NULL
, "%s", response
);
516 adios (NULL
, "no mail to incorporate");
522 * We will get the mail from a file
523 * (typically the standard maildrop)
526 if (inc_type
== INC_FILE
) {
529 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
530 newmail
= m_mailpath (newmail
);
531 else if ((newmail
= context_find ("maildrop")) && *newmail
)
532 newmail
= m_mailpath (newmail
);
534 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
536 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
537 adios (NULL
, "no mail to incorporate");
541 /* skip the folder setup */
542 if ((inc_type
== INC_POP
) && packfile
)
546 if (!context_find ("path"))
547 free (path ("./", TFOLDER
));
549 folder
= getfolder (0);
550 maildir
= m_maildir (folder
);
552 if (stat (maildir
, &st
) == NOTOK
) {
554 adios (maildir
, "error on folder");
555 cp
= concat ("Create folder \"", maildir
, "\"? ", NULL
);
556 if (noisy
&& !getanswer (cp
))
559 if (!makedir (maildir
))
560 adios (NULL
, "unable to create folder %s", maildir
);
563 if (chdir (maildir
) == NOTOK
)
564 adios (maildir
, "unable to change directory to");
566 /* read folder and create message structure */
567 if (!(mp
= folder_read (folder
)))
568 adios (NULL
, "unable to read folder %s", folder
);
574 if (inc_type
== INC_FILE
) {
575 if (access (newmail
, W_OK
) != NOTOK
) {
578 SIGNAL (SIGHUP
, SIG_IGN
);
579 SIGNAL (SIGINT
, SIG_IGN
);
580 SIGNAL (SIGQUIT
, SIG_IGN
);
581 SIGNAL (SIGTERM
, SIG_IGN
);
584 GETGROUPPRIVS(); /* Reset gid to lock mail file */
585 in
= lkfopen (newmail
, "r");
588 adios (NULL
, "unable to lock and fopen %s", newmail
);
589 fstat (fileno(in
), &s1
);
592 if ((in
= fopen (newmail
, "r")) == NULL
)
593 adios (newmail
, "unable to read");
597 /* This shouldn't be necessary but it can't hurt. */
601 if ((i
= stat (audfile
, &st
)) == NOTOK
)
602 advise (NULL
, "Creating Receive-Audit: %s", audfile
);
603 if ((aud
= fopen (audfile
, "a")) == NULL
)
604 adios (audfile
, "unable to append to");
606 chmod (audfile
, m_gmprot ());
609 fprintf (aud
, from
? "<<inc>> %s -ms %s\n"
610 : host
? "<<inc>> %s -host %s -user %s%s\n"
612 dtimenow (0), from
? from
: host
, user
,
613 rpop
< 0 ? " -apop" : rpop
> 0 ? " -rpop" : "");
615 fprintf (aud
, from
? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
621 if (context_find ("mhe")) {
622 cp
= concat (maildir
, "/++", NULL
);
624 if ((mhe
= fopen (cp
, "a")) == NULL
)
625 admonish (cp
, "unable to append to");
628 chmod (cp
, m_gmprot ());
633 /* Get new format string */
634 nfs
= new_fs (form
, format
, FORMAT
);
637 printf ("Incorporating new mail into %s...\n\n", folder
);
643 * Get the mail from a POP server
645 if (inc_type
== INC_POP
) {
647 packfile
= path (packfile
, TFILE
);
648 if (stat (packfile
, &st
) == NOTOK
) {
650 adios (packfile
, "error on file");
651 cp
= concat ("Create file \"", packfile
, "\"? ", NULL
);
652 if (noisy
&& !getanswer (cp
))
656 msgnum
= map_count ();
657 if ((pd
= mbx_open (packfile
, mbx_style
, getuid(), getgid(), m_gmprot()))
659 adios (packfile
, "unable to open");
660 if ((pf
= fdopen (pd
, "w+")) == NULL
)
661 adios (NULL
, "unable to fdopen %s", packfile
);
663 hghnum
= msgnum
= mp
->hghmsg
;
665 * Check if we have enough message space for all the new
666 * messages. If not, then realloc the folder and add enough
667 * space for all new messages plus 10 additional slots.
669 if (mp
->hghmsg
+ nmsgs
>= mp
->hghoff
670 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghmsg
+ nmsgs
+ 10)))
671 adios (NULL
, "unable to allocate folder storage");
674 for (i
= 1; i
<= nmsgs
; i
++) {
677 fseek (pf
, 0L, SEEK_CUR
);
680 fwrite (mmdlm1
, 1, strlen (mmdlm1
), pf
);
683 if (pop_retr (i
, pop_pack
) == NOTOK
)
684 adios (NULL
, "%s", response
);
686 fseek (pf
, 0L, SEEK_CUR
);
689 adios (packfile
, "write error on");
690 fseek (pf
, start
, SEEK_SET
);
692 cp
= getcpy (m_name (msgnum
));
693 if ((pf
= fopen (cp
, "w+")) == NULL
)
694 adios (cp
, "unable to write");
695 chmod (cp
, m_gmprot ());
698 if (pop_retr (i
, pop_action
) == NOTOK
)
699 adios (NULL
, "%s", response
);
702 adios (cp
, "write error on");
703 fseek (pf
, 0L, SEEK_SET
);
705 switch (p
= scan (pf
, msgnum
, 0, nfs
, width
,
706 packfile
? 0 : msgnum
== mp
->hghmsg
+ 1 && chgflag
,
707 1, NULL
, stop
- start
, noisy
)) {
709 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
715 /* advise (cp, "unable to read"); already advised */
734 clear_msg_flags (mp
, msgnum
);
735 set_exists (mp
, msgnum
);
736 set_unseen (mp
, msgnum
);
737 mp
->msgflags
|= SEQMOD
;
742 fseek (pf
, stop
, SEEK_SET
);
743 fwrite (mmdlm2
, 1, strlen (mmdlm2
), pf
);
744 if (fflush (pf
) || ferror (pf
)) {
748 adios (packfile
, "write error on");
750 map_write (packfile
, pd
, 0, 0L, start
, stop
, pos
, size
, noisy
);
752 if (ferror(pf
) || fclose (pf
)) {
757 adios (cp
, "write error on");
762 if (trnflag
&& pop_dele (i
) == NOTOK
)
763 adios (NULL
, "%s", response
);
766 if (pop_quit () == NOTOK
)
767 adios (NULL
, "%s", response
);
769 mbx_close (packfile
, pd
);
776 * Get the mail from file (usually mail spool)
778 if (inc_type
== INC_FILE
) {
779 m_unknown (in
); /* the MAGIC invocation... */
780 hghnum
= msgnum
= mp
->hghmsg
;
783 * Check if we need to allocate more space for message status.
784 * If so, then add space for an additional 100 messages.
786 if (msgnum
>= mp
->hghoff
787 && !(mp
= folder_realloc (mp
, mp
->lowoff
, mp
->hghoff
+ 100))) {
788 advise (NULL
, "unable to allocate folder storage");
794 /* copy file from spool to tmp file */
795 tmpfilenam
= m_scratch ("", invo_name
);
796 if ((fd
= creat (tmpfilenam
, m_gmprot ())) == NOTOK
)
797 adios (tmpfilenam
, "unable to create");
798 chmod (tmpfilenam
, m_gmprot ());
799 if (!(in2
= fdopen (fd
, "r+")))
800 adios (tmpfilenam
, "unable to access");
803 /* link message into folder */
804 newmsg
= folder_addmsg(mp
, tmpfilenam
);
806 /* create scanline for new message */
807 switch (i
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
808 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
)) {
815 fputs ("inc aborted!\n", aud
);
816 advise (NULL
, "aborted!"); /* doesn't clean up locks! */
820 advise (NULL
, "BUG in %s, number out of range", invo_name
);
824 advise (NULL
, "BUG in %s, scan() botch (%d)", invo_name
, i
);
830 * Run the external program hook on the message.
833 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir
, msgnum
+ 1);
834 (void)ext_hook("add-hook", b
, (char *)0);
847 clear_msg_flags (mp
, msgnum
);
848 set_exists (mp
, msgnum
);
849 set_unseen (mp
, msgnum
);
850 mp
->msgflags
|= SEQMOD
;
858 if (p
< 0) { /* error */
860 if (i
< 0) { /* error */
863 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
864 (void) lkfclose (in
, newmail
); in
= NULL
;
865 DROPGROUPPRIVS(); /* And then return us to normal privileges */
867 fclose (in
); in
= NULL
;
869 adios (NULL
, "failed");
884 if ((inc_type
== INC_POP
) && packfile
)
889 * truncate file we are incorporating from
891 if (inc_type
== INC_FILE
) {
893 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
894 advise (NULL
, "new messages have arrived!\007");
896 if ((i
= creat (newmail
, 0600)) != NOTOK
)
899 admonish (newmail
, "error zero'ing");
900 unlink(map_name(newmail
));
904 printf ("%s not zero'd\n", newmail
);
908 if (msgnum
== hghnum
) {
909 admonish (NULL
, "no messages incorporated");
911 context_replace (pfolder
, folder
); /* update current folder */
913 mp
->curmsg
= hghnum
+ 1;
917 if (chgflag
) /* sigh... */
918 seq_setcur (mp
, mp
->curmsg
);
922 * unlock the mail spool
924 if (inc_type
== INC_FILE
) {
926 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
927 (void) lkfclose (in
, newmail
); in
= NULL
;
928 DROPGROUPPRIVS(); /* And then return us to normal privileges */
930 fclose (in
); in
= NULL
;
934 seq_setunseen (mp
, 0); /* set the Unseen-Sequence */
935 seq_save (mp
); /* synchronize sequences */
936 context_save (); /* save the context file */
944 * Copy message message from spool into
945 * temporary file. Massage the "From " line
949 cpymsg (FILE *in
, FILE *out
)
952 char *tmpbuf
, name
[NAMESZ
];
955 state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, in
);
978 if (packfile
&& pd
!= NOTOK
)
979 mbx_close (packfile
, pd
);
984 lkfclose(in
, newmail
);
988 return 1; /* dead code to satisfy the compiler */
995 fprintf (pf
, "%s\n", s
);
996 stop
+= strlen (s
) + 1;
997 return 0; /* Is return value used? This was missing before 1999-07-15. */
1004 char buffer
[BUFSIZ
];
1006 snprintf (buffer
, sizeof(buffer
), "%s\n", s
);
1007 for (j
= 0; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
1009 for (j
= 0; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
1012 size
+= strlen (buffer
) + 1;
1013 return 0; /* Is return value used? This was missing before 1999-07-15. */
1024 if (stat (packfile
, &st
) == NOTOK
)
1026 if ((md
= open (cp
= map_name (packfile
), O_RDONLY
)) == NOTOK
1027 || map_chk (cp
, md
, &d
, (long) st
.st_size
, 1)) {