Debian Codegraph @

actions.c

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 static char rcsid[] ="@(#)$Id: actions.c,v 5.8 1993/08/03 19:28:39 syd Exp $";
2
3 /*******************************************************************************
4 * The Elm Mail System - $Revision: 5.8 $ $State: Exp $
5 *
6 * Copyright (c) 1988-1992 USENET Community Trust
7 * Copyright (c) 1986,1987 Dave Taylor
8 *******************************************************************************
9 * Bug reports, patches, comments, suggestions should be sent to:
10 *
11 * Philip Brown [email protected]
12 *
13 *******************************************************************************
14 * $Log: actions.c,v $
15 * Revision 5.8 1993/08/03 19:28:39 syd
16 * Elm tries to replace the system toupper() and tolower() on current
17 * BSD systems, which is unnecessary. Even worse, the replacements
18 * collide during linking with routines in isctype.o. This patch adds
19 * a Configure test to determine whether replacements are really needed
20 * (BROKE_CTYPE definition). The <ctype.h> header file is now included
21 * globally through hdrs/defs.h and the BROKE_CTYPE patchup is handled
22 * there. Inclusion of <ctype.h> was removed from *all* the individual
23 * files, and the toupper() and tolower() routines in lib/opt_utils.c
24 * were dropped.
25 * From: [email protected] (Chip Rosenthal)
26 *
27 * Revision 5.7 1993/08/03 19:07:58 syd
28 * Removed bogus string lockfile.
29 * From: [email protected] (Jan Djarv)
30 *
31 * Revision 5.6 1993/04/21 01:25:45 syd
32 * I'm using Elm 2.4.21 under Linux. Linux has no Bourne shell. Each
33 * user installs her favorite shell as /bin/sh. I use Bash 1.12.
34 *
35 * Elm invokes the mail transport (MTA) like so:
36 *
37 * ( ( MTA destination; rm -f tempfile ) & ) < tempfile &
38 *
39 * This form of command doesn't work with my Bash, in which any command
40 * which is backgrounded ("&") gets its stdin attached to /dev/null.
41 *
42 * The below patch arranges for Elm to call the MTA thusly:
43 *
44 * ( MTA destination <tempfile; rm -f tempfile ) &
45 * From: decwrl!uunet.UU.NET!fin!chip (Chip Salzenberg)
46 *
47 * Revision 5.5 1993/01/27 19:40:01 syd
48 * I implemented a change to filter's default verbose message format
49 * including %x %X style date and time along with username
50 * From: [email protected] (Mark Lawrence)
51 *
52 * Revision 5.4 1993/01/20 03:37:16 syd
53 * Nits and typos in the NLS messages and corresponding default messages.
54 * From: [email protected] (Dave Wolfe)
55 *
56 * Revision 5.3 1992/12/24 19:22:05 syd
57 * Quote from the filter of phrase to prevent RFC-822 parsing problems
58 * From: Syd via request from Ian Stewartson <[email protected]>
59 *
60 * Revision 5.2 1992/12/11 02:16:08 syd
61 * remove unreachable return(0) at end of function
62 * From: Syd
63 *
64 * Revision 5.1 1992/10/03 22:18:09 syd
65 * Initial checkin as of 2.4 Release at PL0
66 *
67 *
68 ******************************************************************************/
69
70
71 /** RESULT oriented routines *chuckle*. These routines implement the
72 actions that result from either a specified rule being true or from
73 the default action being taken.
74 **/
75
76 #include <stdio.h>
77 #include <pwd.h>
78 #include <fcntl.h>
79
80 #include "defs.h"
81 #include "filter.h"
82 #include "utils.h"
83 #include "s_filter.h"
84 #include "version.h"
85 #include "lock.h"
86
87
88 FILE *emergency_local_delivery();
89 extern char *date_n_user();
90
91 /*
92 * open pipe to mailer,and (re)send mail we received to given address.
93 * ASSUME that global "tmpfilename" is set to name of tmpfile
94 * that we have (FILE*)ftmp into.
95 * Also assume that caller has done rewind(ftmp);
96 *
97 * return value suitable for passing to _exit()
98 */
99 int pipe_to_mailer(ftmp, resendflag, address)
100 FILE *ftmp;
101 int resendflag;
102 char *address;
103 {
104 FILE *pipefd;
105 char buffer[VERY_LONG_STRING];
106 int statusp,in_header = TRUE;
107
108 if (strcmp(sendmail, mailer) == 0)
109 sprintf(buffer, "%s %s %s", sendmail, smflags, address);
110 else
111 sprintf(buffer, "%s %s", mailer, address);
112
113
114 if ((pipefd = popen(buffer, "w")) == NULL) {
115 if (outfptr != NULL){
116 /* log failure */
117 fprintf(outfptr,
118 catgets(elm_msg_cat,
119 FilterSet,FilterPopenFailed,
120 "filter (%s): popen %s failed!\n"),
121 date_n_user(), buffer);
122 }
123
124 /* mail the "hard way" */
125 sprintf(buffer, "(%s %s %s < %s ) &",
126 sendmail, smflags, address, tmpfilename);
127 exit(system(buffer));
128
129 }
130
131 if(resendflag==1){
132 /* Strip off any possible leading "From " fake header,
133 * then just dump the whole thing to the sendmail pipe
134 */
135 if(fgets(buffer,SLEN,ftmp)==NULL){
136 return(pclose(pipefd));
137 }
138 /* We want to ignore first line,
139 * but ONLY IF it is the special mbox-added
140 * non-header "From " line
141 */
142 if(!THE_SAME(buffer,"From ")){
143 fprintf(pipefd,"%s", buffer);
144 }
145 while (fgets(buffer, SLEN, ftmp) != NULL){
146 fprintf(pipefd,"%s", buffer);
147 }
148 fflush(pipefd);
149 return(pclose(pipefd));
150 }
151
152 /* else */
153 fprintf(pipefd, "Subject: \"%s\"\n", subject);
154 fprintf(pipefd,
155 catgets(elm_msg_cat,FilterSet,
156 FilterFromTheFilterOf,
157 "From: \"The Filter of %s@%s\" <%s>\n"),
158 username, hostname, username);
159 fprintf(pipefd, "To: %s\n", address);
160 fprintf(pipefd,
161 catgets(elm_msg_cat,
162 FilterSet,FilterXFilteredBy,
163 "X-Filtered-By: %s\n\n"),
164 versionstring);
165
166 fprintf(pipefd,
167 catgets(elm_msg_cat,
168 FilterSet,FilterBeginMesg,
169 "-- Begin filtered message --\n\n"));
170
171
172 while (fgets(buffer, SLEN, ftmp) != NULL){
173 if (already_been_forwarded && in_header)
174 in_header = (strlen(buffer) == 1? 0 : in_header);
175 else {
176 /* note the indent! */
177 fprintf(pipefd," %s", buffer);
178 }
179 }
180
181 fprintf(pipefd,
182 catgets(elm_msg_cat,
183 FilterSet,FilterEndMesg,
184 "\n-- End of filtered message --\n"));
185
186 fflush(pipefd);
187 return(pclose(pipefd));
188
189
190 }
191
192 /* This is badly named, because it actually does what should be
193 * save_to_mailbox(), as WELL as send email to external users.
194 * (It checks and saves locally if local user, otherwise calls
195 * sendmail or whatever mailer was compiled in)
196 *
197 * fptr is assumed to be a FILE* to the original tmpfile that we dumped
198 * stdin to. We dont open it, and we dont close it neither.
199 */
200 mail_message(address, fptr,resendflag)
201 char *address;
202 FILE *fptr;
203 int resendflag; /* if 1, resend without any mangling */
204 {
205 /* Called with an address to send mail to. For various reasons
206 * that are too disgusting to go into herein, we're going to actually
207 * open the users mailbox and by hand add this message. Yech.
208 * EXCEPT if the user is somewhere else, then we'll try to use
209 * sendmail.
210 */
211
212 FILE *mailfptr;
213 int line_count = 0, mailunit, pid, statusp;
214 char buffer[VERY_LONG_STRING];
215
216 rewind(fptr);
217
218 if(show_only)
219 return 0;
220
221
222 if (strcmp(address, username) != 0) { /* mailing to someone else */
223
224 if (verbose && ! log_actions_only && outfptr != NULL)
225 fprintf(outfptr,
226 catgets(elm_msg_cat,
227 FilterSet,FilterMailingMessage,
228 "filter (%s): Mailing to %s via %s\n"),
229 date_n_user(), address, mailer);
230
231
232 if (already_been_forwarded) { /* potential looping! */
233 if (contains(Hfrom, username)) {
234 if (outfptr != NULL)
235 fprintf(outfptr,
236 catgets(elm_msg_cat,FilterSet,FilterLoopDetected,
237 "filter (%s): Filter loop detected! Message left in tempfile %s\n"),
238 date_n_user(), tmpfilename );
239 if (outfptr != NULL) fclose(outfptr);
240
241 /* deliberately clean-exit here to ensure tempfile
242 * not unlinked
243 */
244 exit(0);
245 }
246 }
247
248 /*
249 * we do a fork/exec here to prevent evil people
250 * from abusing the setgid privileges of filter.
251 */
252
253 if ( (pid=fork()) > 0 ) /* we're the parent */
254 {
255 wait(&statusp);
256 if (statusp!=0)
257 {
258 fprintf(outfptr,
259 catgets(elm_msg_cat,
260 FilterSet,FilterBadWaitStat,
261 "filter (%s): Command \"%s\" exited with value %d\n"),
262 date_n_user(),buffer,statusp);
263 }
264 }
265 else if (pid==0) /* we're the child */
266 {
267 /* use safe permissions */
268
269 setuid(user_uid);
270 setgid(user_gid);
271
272 _exit(pipe_to_mailer(fptr,resendflag,address));
273
274 }
275 else /* fork failed */
276 {
277 if (outfptr != NULL)
278 fprintf(outfptr,
279 catgets(elm_msg_cat,
280 FilterSet,FilterForkFailed,
281 "filter (%s): fork of command \"%s\" failed\n"),
282 date_n_user(),buffer);
283 }
284
285 return 1; /* YEAH! Wot a slick program, eh? */
286
287 } /* if (strcmp(address, username) != 0) */
288
289 /** OTHERWISE it is to the current user... **/
290
291
292 if (!lock()) {
293
294 if (outfptr != NULL) {
295 fprintf(outfptr, catgets(elm_msg_cat,FilterSet,
296 FilterCouldntCreateLockFile,
297 "filter (%s): Couldn't create lock file\n"),
298 date_n_user());
299 fprintf(outfptr, catgets(elm_msg_cat,FilterSet,
300 FilterCantOpenMailBox,
301 "filter (%s): Can't open mailbox %s!\n"),
302 date_n_user(), user_mailbox);
303 }
304 if ((mailfptr = emergency_local_delivery()) == NULL)
305 return(1);
306 }
307 else {
308 if (verbose && ! log_actions_only && outfptr != NULL)
309 fprintf(outfptr,
310 catgets(elm_msg_cat,
311 FilterSet,FilterMailingMessage,
312 "filter (%s): local delivery to %s\n"),
313 date_n_user(), address);
314
315 if ((mailunit = open(user_mailbox,
316 O_APPEND | O_WRONLY | O_CREAT, 0600)) >= 0)
317 mailfptr = fdopen(mailunit, "a");
318 else if ((mailfptr = emergency_local_delivery()) == NULL)
319 return(1);
320 }
321
322 while (fgets(buffer, sizeof(buffer), fptr) != NULL) {
323 #ifdef ESCAPE_FROM_LINE
324 line_count++;
325 if (THE_SAME(buffer, "From ") && line_count > 1){
326 fprintf(mailfptr, ">");
327 }
328 #endif
329 fputs(buffer, mailfptr);
330 }
331
332 /* add a little mailbox message separator. */
333 fputs("\n", mailfptr);
334
335 fclose(mailfptr);
336 unlock(); /* blamo or not? Let it decide! */
337
338 return 0;
339
340 }
341
342
343 /* save message to a folder, one way or another.
344 * But we may need to do special things with GID before starting the actual
345 * save, not to mention "~" expension.
346 * We may even fork().
347 *
348 * tmpfptr must be a FILE* to the temp file we dumped stdin to at start
349 * return 0 on okay, non-zero otherwise.
350 * Note that the return MAYBE something meaningful, like EX_TEMPFAIL.
351 * So caller should keep it as exit status.
352 */
353 int save_message(foldername, tmpfptr)
354 char *foldername;
355 FILE *tmpfptr;
356 {
357 /** Save the message in a folder. Use full file buffering to
358 make this work without contention problems **/
359
360 int gid, pid, statusp, ret;
361
362 /* allow for ~ file names expansion in rules */
363 (void) expand_filename(foldername);
364
365 if (verbose && outfptr != NULL)
366 fprintf(outfptr, catgets(elm_msg_cat,FilterSet,FilterSavedMessage,
367 "filter (%s): Saving message in folder %s\n"),
368 date_n_user(), foldername);
369
370 if (show_only)
371 return(0);
372
373 gid=getgid();
374
375
376 if (gid != user_gid) /* then fork */
377 {
378
379 if ((pid=fork()) > 0) /* we're the parent */
380 {
381 wait(&statusp);
382 return(statusp);
383
384 }
385 else if (pid == 0) /* we're the child */
386 {
387 /*
388 * we want the file to have
389 * the user's group id
390 */
391
392 ret = setgid(user_gid);
393 ret = save_to_folder(foldername,tmpfptr);
394
395 _exit(ret);
396
397 }
398 else /* fork failed */
399 {
400 fprintf(outfptr,
401 catgets(elm_msg_cat,
402 FilterSet,FilterForkSaveFailed,
403 "filter (%s): fork-and-save message failed\n\
404 \tsaving with current group ID\n"),
405 date_n_user());
406 /*
407 * save with the current group id.
408 */
409 ret = save_to_folder(foldername,tmpfptr);
410 return(ret);
411
412 }
413 }
414 else
415 {
416 /*
417 * ok to save with current group id.
418 */
419 ret = save_to_folder(foldername,tmpfptr);
420 return(ret);
421
422 }
423 return(ret); /* not reached, just to make warnings go away*/
424 }
425
426 /* We are definately saving to a FILE now.
427 * return 0 on okay, special-email-exit-status otherwise.
428 * (usually EX_TEMPFAIL)
429 */
430 int save_to_folder(foldername,tmpfptr)
431 char *foldername;
432 FILE *tmpfptr;
433 {
434 /*
435 * this function does the actual work of saving the message
436 * in the named folder.
437 */
438
439 FILE *folderfptr;
440 char buffer[SLEN];
441 int fdunit, line_count=0;
442
443 rewind(tmpfptr);
444 fflush(tmpfptr);
445
446 if(!lockfolder(foldername))
447 {
448 fprintf(outfptr, catgets(elm_msg_cat,FilterSet,
449 FilterCouldntCreateLockFile,
450 "filter (%s): Couldn't create lock file\n"),
451 date_n_user());
452 fprintf(outfptr, catgets(elm_msg_cat,FilterSet,
453 FilterCantOpenMailBox,
454 "filter (%s): Can't open mailfolder %s!\n"),
455 date_n_user(), foldername);
456
457 return(75); /* EX_TEMPFAIL */
458
459 }
460 if ((fdunit = open(foldername,
461 O_APPEND | O_WRONLY | O_CREAT, 0600)) < 0) {
462 if (outfptr != NULL)
463 fprintf(outfptr,
464 catgets(elm_msg_cat,
465 FilterSet,FilterCantSaveMessageToFolder,
466 "filter (%s): can't save message to requested folder %s!\n"),
467 date_n_user(), foldername);
468 return(75); /* EX_TEMPFAIL */
469 }
470 folderfptr = fdopen(fdunit,"a");
471
472 while (fgets(buffer, sizeof(buffer), tmpfptr) != NULL) {
473 #ifdef ESCAPE_FROM_LINE
474 /* "From " used as beginning-of-msg marker, so escape it
475 * if it is not actually the first line in the message
476 */
477 line_count++;
478 if((line_count >1) && THE_SAME(buffer, "From ") ){
479 fprintf(folderfptr, ">");
480 }
481 #endif
482 fputs(buffer, folderfptr);
483 }
484
485 /*
486 * Add two newlines, to ensure that other mailers (which, unlike
487 * elm, may only look for \n\nFrom_ as the start-of-message
488 * indicator).
489 */
490
491 fprintf(folderfptr, "\n\n");
492
493 fclose(folderfptr);
494
495 unlockfolder(foldername);
496
497 return(0);
498 }
499
500 void execute(command, tmpfptr)
501 char *command;
502 FILE *tmpfptr;
503 {
504 /** execute the indicated command, feeding as standard input the
505 message we have.
506 **/
507
508 int pid, statusp;
509 char buffer[SLEN];
510
511 rewind(tmpfptr);
512 fflush(tmpfptr);
513
514 if (verbose && outfptr != NULL)
515 fprintf(outfptr, catgets(elm_msg_cat,FilterSet,FilterExecutingCmd,
516 "filter (%s): Executing %s\n"), date_n_user(), command);
517
518 if(show_only){
519 return;
520 }
521
522 sprintf(buffer, "%s",command);
523
524 if ( (pid=fork()) > 0) /* we're the parent */
525 {
526 wait(&statusp);
527 if (statusp!=0)
528 {
529 if (outfptr != NULL)
530 fprintf(outfptr,
531 catgets(elm_msg_cat,
532 FilterSet,FilterBadWaitStat,
533 "filter (%s): Command \"%s\" exited with value %d\n"),
534 date_n_user(),command,statusp);
535 }
536
537 }
538 else if (pid==0)/* we're the child */
539 {
540 int tmpfd;
541 /*
542 * reset uid/gid to user's uid and gid
543 */
544 setgid(user_gid);
545 setuid(user_uid);
546
547 tmpfd=fileno(tmpfptr);
548 close(0);
549 dup(tmpfd);
550
551 exit(system(buffer));
552 }
553 else /* fork() failed */
554 {
555 fprintf(outfptr,
556 catgets(elm_msg_cat,
557 FilterSet,FilterForkFailed,
558 "filter (%s): fork of command \"%s\" failed\n"),
559 date_n_user(),command);
560 }
561
562
563 }
564
565 FILE *
566 emergency_local_delivery()
567 {
568 /** This is called when we can't deliver the mail to the usual
569 mailbox in the usual way ...
570 **/
571
572 FILE *fptr;
573 char mailbox[SLEN];
574 int mailunit;
575
576 sprintf(mailbox, "%s/%s", home, EMERGENCY_MAILBOX);
577
578 if ((mailunit = open(mailbox, O_APPEND | O_WRONLY | O_CREAT, 0600)) < 0) {
579 if (outfptr != NULL)
580 fprintf(outfptr, catgets(elm_msg_cat,FilterSet,FilterCantOpenEither,
581 "filter (%s): Can't open %s either!!\n"),
582 date_n_user(), mailbox);
583
584 sprintf(mailbox,"%s/%s", home, EMERG_MBOX);
585
586 if ((mailunit = open(mailbox, O_APPEND | O_WRONLY | O_CREAT, 0600)) < 0) {
587
588 if (outfptr != NULL) {
589 fprintf(outfptr, catgets(elm_msg_cat,FilterSet,
590 FilterCantOpenEither,
591 "filter (%s): Can't open %s either!!\n"),
592 date_n_user(), mailbox);
593 fprintf(outfptr,catgets(elm_msg_cat,FilterSet,FilterCantOpenAny,
594 "filter (%s): I can't open ANY mailboxes! Augh!!\n"),
595 date_n_user());
596 }
597
598 /* DIE DIE DIE DIE!! */
599 leave(catgets(elm_msg_cat,FilterSet,FilterCantOpenAnyLeave,
600 "Cannot open any mailbox"));
601 }
602 else
603 if (outfptr != NULL)
604 fprintf(outfptr,catgets(elm_msg_cat,FilterSet,
605 FilterUsingEmergMbox,
606 "filter (%s): Using %s as emergency mailbox\n"),
607 date_n_user(), mailbox);
608 }
609 else
610 if (outfptr != NULL)
611 fprintf(outfptr,catgets(elm_msg_cat,FilterSet,
612 FilterUsingEmergMbox,
613 "filter (%s): Using %s as emergency mailbox\n"),
614 date_n_user(), mailbox);
615
616 fptr = fdopen(mailunit, "a");
617 return((FILE *) fptr);
618 }