masqmail

view src/masqmail.c @ 72:ad034b57f3b2

fixed Debian bug 536060 (log files are closed after SIGHUP receival) Explanation: When run in daemon mode, first the log files are opened. They get assigned to the file descriptors 3 and 4 usually. Then std{in,out,err} are closed. When SIGHUP comes in, all open files are closes and masqmail reexecutes itself. The new masqmail instance opens the log files at fd 0 and 1 now, but std{in,out,err} are closed afterwards, thus the log files are closed. The fix is to close the log files before std{in,out,err} are closed, in case the log files have higher fds. After std{in,out,err} were closed, the log files get opened again, now. See also: http://bugs.debian.org/536060
author meillo@marmaro.de
date Wed, 16 Jun 2010 10:32:20 +0200
parents 3cb6f383f07e
children 9db75b801dc4
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
19 #include <stdio.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <sys/time.h>
27 #include <netinet/in.h>
28 #include <netdb.h>
29 #include <syslog.h>
30 #include <signal.h>
32 #include <glib.h>
34 #include "masqmail.h"
36 /* mutually exclusive modes. Note that there is neither a 'get' mode
37 nor a 'queue daemon' mode. These, as well as the distinction beween
38 the two (non exclusive) daemon (queue and listen) modes are handled
39 by flags.*/
40 typedef enum _mta_mode {
41 MODE_ACCEPT = 0, /* accept message on stdin */
42 MODE_DAEMON, /* run as daemon */
43 MODE_RUNQUEUE, /* single queue run, online or offline */
44 MODE_GET_DAEMON, /* run as get (retrieve) daemon */
45 MODE_SMTP, /* accept SMTP on stdin */
46 MODE_LIST, /* list queue */
47 MODE_MCMD, /* do queue manipulation */
48 MODE_VERSION, /* show version */
49 MODE_BI, /* fake ;-) */
50 MODE_NONE /* to prevent default MODE_ACCEPT */
51 } mta_mode;
53 char *pidfile = NULL;
54 volatile int sigterm_in_progress = 0;
56 static void
57 sigterm_handler(int sig)
58 {
59 if (sigterm_in_progress)
60 raise(sig);
61 sigterm_in_progress = 1;
63 if (pidfile) {
64 uid_t uid;
65 uid = seteuid(0);
66 if (unlink(pidfile) != 0)
67 logwrite(LOG_WARNING, "could not delete pid file %s: %s\n", pidfile, strerror(errno));
68 seteuid(uid); /* we exit anyway after this, just to be sure */
69 }
71 signal(sig, SIG_DFL);
72 raise(sig);
73 }
75 #ifdef ENABLE_IDENT /* so far used for that only */
76 static gboolean
77 is_in_netlist(gchar * host, GList * netlist)
78 {
79 guint hostip = inet_addr(host);
80 struct in_addr addr;
82 addr.s_addr = hostip;
83 if (addr.s_addr != INADDR_NONE) {
84 GList *node;
85 foreach(netlist, node) {
86 struct in_addr *net = (struct in_addr *) (node->data);
87 if ((addr.s_addr & net->s_addr) == net->s_addr)
88 return TRUE;
89 }
90 }
91 return FALSE;
92 }
93 #endif
95 gchar*
96 get_optarg(char *argv[], gint argc, gint * argp, gint * pos)
97 {
98 if (argv[*argp][*pos])
99 return &(argv[*argp][*pos]);
100 else {
101 if (*argp + 1 < argc) {
102 if (argv[(*argp) + 1][0] != '-') {
103 (*argp)++;
104 *pos = 0;
105 return &(argv[*argp][*pos]);
106 }
107 }
108 }
109 return NULL;
110 }
112 gchar*
113 get_progname(gchar * arg0)
114 {
115 gchar *p = arg0 + strlen(arg0) - 1;
116 while (p > arg0) {
117 if (*p == '/')
118 return p + 1;
119 p--;
120 }
121 return p;
122 }
124 gboolean
125 write_pidfile(gchar * name)
126 {
127 FILE *fptr;
129 if ((fptr = fopen(name, "wt"))) {
130 fprintf(fptr, "%d\n", getpid());
131 fclose(fptr);
132 pidfile = strdup(name);
133 return TRUE;
134 }
135 logwrite(LOG_WARNING, "could not write pid file: %s\n", strerror(errno));
136 return FALSE;
137 }
139 static void
140 mode_daemon(gboolean do_listen, gint queue_interval, char *argv[])
141 {
142 guint pid;
144 /* daemon */
145 if (!conf.run_as_user) {
146 if ((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)) {
147 fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER);
148 exit(EXIT_FAILURE);
149 }
150 }
152 if ((pid = fork()) > 0) {
153 exit(EXIT_SUCCESS);
154 } else if (pid < 0) {
155 logwrite(LOG_ALERT, "could not fork!");
156 exit(EXIT_FAILURE);
157 }
159 signal(SIGTERM, sigterm_handler);
160 write_pidfile(PIDFILEDIR "/masqmail.pid");
162 conf.do_verbose = FALSE;
164 /* closing and reopening the log ensures that it is open afterwards
165 because it is possible that the log is assigned to fd 1 and gets
166 thus closes by fclose(stdout). Similar for the debugfile.
167 */
168 logclose();
169 fclose(stdin);
170 fclose(stdout);
171 fclose(stderr);
172 logopen();
174 listen_port(do_listen ? conf.listen_addresses : NULL, queue_interval, argv);
175 }
177 #ifdef ENABLE_POP3
178 static void
179 mode_get_daemon(gint get_interval, char *argv[])
180 {
181 guint pid;
183 /* daemon */
184 if (!conf.run_as_user) {
185 if ((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)) {
186 fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER);
187 exit(EXIT_FAILURE);
188 }
189 }
191 if ((pid = fork()) > 0) {
192 exit(EXIT_SUCCESS);
193 } else if (pid < 0) {
194 logwrite(LOG_ALERT, "could not fork!");
195 exit(EXIT_FAILURE);
196 }
198 signal(SIGTERM, sigterm_handler);
199 write_pidfile(PIDFILEDIR "/masqmail-get.pid");
201 conf.do_verbose = FALSE;
203 /* closing and reopening the log ensures that it is open afterwards
204 because it is possible that the log is assigned to fd 1 and gets
205 thus closes by fclose(stdout). Similar for the debugfile.
206 */
207 logclose();
208 fclose(stdin);
209 fclose(stdout);
210 fclose(stderr);
211 logopen();
213 get_daemon(get_interval, argv);
214 }
215 #endif
217 #ifdef ENABLE_SMTP_SERVER
218 static void
219 mode_smtp()
220 {
221 /* accept smtp message on stdin */
222 /* write responses to stderr. */
224 struct sockaddr_in saddr;
225 gchar *peername = NULL;
226 int dummy = sizeof(saddr);
228 conf.do_verbose = FALSE;
230 if (!conf.run_as_user) {
231 seteuid(conf.orig_uid);
232 setegid(conf.orig_gid);
233 }
235 DEBUG(5) debugf("accepting smtp message on stdin\n");
237 if (getpeername(0, (struct sockaddr *) (&saddr), &dummy) == 0) {
238 peername = g_strdup(inet_ntoa(saddr.sin_addr));
239 } else if (errno != ENOTSOCK)
240 exit(EXIT_FAILURE);
242 smtp_in(stdin, stderr, peername, NULL);
243 }
244 #endif
246 static void
247 mode_accept(address * return_path, gchar * full_sender_name, guint accept_flags, char **addresses, int addr_cnt)
248 {
249 /* accept message on stdin */
250 accept_error err;
251 message *msg = create_message();
252 gint i;
254 if (return_path != NULL) {
255 if ((conf.orig_uid != 0)
256 && (conf.orig_uid != conf.mail_uid)
257 && (!is_ingroup(conf.orig_uid, conf.mail_gid))) {
258 fprintf(stderr, "must be in root, %s or in group %s for setting return path.\n", DEF_MAIL_USER, DEF_MAIL_GROUP);
259 exit(EXIT_FAILURE);
260 }
261 }
263 if (!conf.run_as_user) {
264 seteuid(conf.orig_uid);
265 setegid(conf.orig_gid);
266 }
268 DEBUG(5) debugf("accepting message on stdin\n");
270 msg->received_prot = PROT_LOCAL;
271 for (i = 0; i < addr_cnt; i++) {
272 if (addresses[i][0] != '|')
273 msg->rcpt_list = g_list_append(msg->rcpt_list, create_address_qualified(addresses[i], TRUE, conf.host_name));
274 else {
275 logwrite(LOG_ALERT, "no pipe allowed as recipient address: %s\n", addresses[i]);
276 exit(EXIT_FAILURE);
277 }
278 }
280 /* -f option */
281 msg->return_path = return_path;
283 /* -F option */
284 msg->full_sender_name = full_sender_name;
286 if ((err = accept_message(stdin, msg, accept_flags)) == AERR_OK) {
287 if (spool_write(msg, TRUE)) {
288 pid_t pid;
289 logwrite(LOG_NOTICE, "%s <= %s with %s\n", msg->uid, addr_string(msg->return_path), prot_names[PROT_LOCAL]);
291 if (!conf.do_queue) {
292 if ((pid = fork()) == 0) {
293 conf.do_verbose = FALSE;
294 fclose(stdin);
295 fclose(stdout);
296 fclose(stderr);
297 if (deliver(msg)) {
298 exit(EXIT_SUCCESS);
299 } else
300 exit(EXIT_FAILURE);
301 } else if (pid < 0) {
302 logwrite(LOG_ALERT, "could not fork for delivery, id = %s", msg->uid);
303 }
304 }
305 } else {
306 fprintf(stderr, "Could not write spool file\n");
307 exit(EXIT_FAILURE);
308 }
309 } else {
310 switch (err) {
311 case AERR_EOF:
312 fprintf(stderr, "unexpected EOF.\n");
313 exit(EXIT_FAILURE);
314 case AERR_NORCPT:
315 fprintf(stderr, "no recipients.\n");
316 exit(EXIT_FAILURE);
317 default:
318 /* should never happen: */
319 fprintf(stderr, "Unknown error (%d)\r\n", err);
320 exit(EXIT_FAILURE);
321 }
322 exit(EXIT_FAILURE);
323 }
324 }
326 int
327 main(int argc, char *argv[])
328 {
329 /* cmd line flags */
330 gchar *conf_file = CONF_FILE;
331 gint arg = 1;
332 gboolean do_get = FALSE;
333 gboolean do_get_online = FALSE;
335 gboolean do_listen = FALSE;
336 gboolean do_runq = FALSE;
337 gboolean do_runq_online = FALSE;
339 gboolean do_queue = FALSE;
341 gboolean do_verbose = FALSE;
342 gint debug_level = -1;
344 mta_mode mta_mode = MODE_ACCEPT;
346 gint queue_interval = 0;
347 gint get_interval = 0;
348 gboolean opt_t = FALSE;
349 gboolean opt_i = FALSE;
350 gboolean opt_odb = FALSE;
351 gboolean opt_oem = FALSE;
352 gboolean exit_failure = FALSE;
354 gchar *M_cmd = NULL;
356 gint exit_code = EXIT_SUCCESS;
357 gchar *route_name = NULL;
358 gchar *get_name = NULL;
359 gchar *progname;
360 gchar *f_address = NULL;
361 gchar *full_sender_name = NULL;
362 address *return_path = NULL; /* may be changed by -f option */
364 progname = get_progname(argv[0]);
366 if (strcmp(progname, "mailq") == 0) {
367 mta_mode = MODE_LIST;
368 } else if (strcmp(progname, "mailrm") == 0) {
369 mta_mode = MODE_MCMD;
370 M_cmd = "rm";
371 } else if (strcmp(progname, "runq") == 0) {
372 mta_mode = MODE_RUNQUEUE;
373 do_runq = TRUE;
374 } else if (strcmp(progname, "rmail") == 0) {
375 mta_mode = MODE_ACCEPT;
376 opt_i = TRUE;
377 } else if (strcmp(progname, "smtpd") == 0 || strcmp(progname, "in.smtpd") == 0) {
378 mta_mode = MODE_SMTP;
379 }
381 /* parse cmd line */
382 while (arg < argc) {
383 gint pos = 0;
384 if ((argv[arg][pos] == '-') && (argv[arg][pos + 1] != '-')) {
385 pos++;
386 switch (argv[arg][pos++]) {
387 case 'b':
388 switch (argv[arg][pos++]) {
389 case 'd':
390 do_listen = TRUE;
391 mta_mode = MODE_DAEMON;
392 break;
393 case 'i':
394 /* ignored */
395 mta_mode = MODE_BI;
396 break;
397 case 's':
398 mta_mode = MODE_SMTP;
399 break;
400 case 'p':
401 mta_mode = MODE_LIST;
402 break;
403 case 'V':
404 mta_mode = MODE_VERSION;
405 break;
406 default:
407 fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
408 exit(EXIT_FAILURE);
409 }
410 break;
411 case 'B':
412 /* we ignore this and throw the argument away */
413 get_optarg(argv, argc, &arg, &pos);
414 break;
415 case 'C':
416 if (!(conf_file = get_optarg(argv, argc, &arg, &pos))) {
417 fprintf(stderr, "-C requires a filename as argument.\n");
418 exit(EXIT_FAILURE);
419 }
420 break;
421 case 'F':
422 {
423 full_sender_name = get_optarg(argv, argc, &arg, &pos);
424 if (!full_sender_name) {
425 fprintf(stderr, "-F requires a name as an argument\n");
426 exit(EXIT_FAILURE);
427 }
428 }
429 break;
430 case 'd':
431 if (getuid() == 0) {
432 char *lvl = get_optarg(argv, argc, &arg, &pos);
433 if (lvl)
434 debug_level = atoi(lvl);
435 else {
436 fprintf(stderr, "-d requires a number as an argument.\n");
437 exit(EXIT_FAILURE);
438 }
439 } else {
440 fprintf(stderr, "only root may set the debug level.\n");
441 exit(EXIT_FAILURE);
442 }
443 break;
444 case 'f':
445 /* set return path */
446 {
447 gchar *address;
448 address = get_optarg(argv, argc, &arg, &pos);
449 if (address) {
450 f_address = g_strdup(address);
451 } else {
452 fprintf(stderr, "-f requires an address as an argument\n");
453 exit(EXIT_FAILURE);
454 }
455 }
456 break;
457 case 'g':
458 do_get = TRUE;
459 if (!mta_mode)
460 mta_mode = MODE_NONE; /* to prevent default MODE_ACCEPT */
461 if (argv[arg][pos] == 'o') {
462 pos++;
463 do_get_online = TRUE;
464 /* can be NULL, then we use online detection method */
465 route_name = get_optarg(argv, argc, &arg, &pos);
467 if (route_name != NULL) {
468 if (isdigit(route_name[0])) {
469 get_interval = time_interval(route_name, &pos);
470 route_name = get_optarg(argv, argc, &arg, &pos);
471 mta_mode = MODE_GET_DAEMON;
472 do_get = FALSE;
473 }
474 }
475 } else {
476 if ((optarg = get_optarg(argv, argc, &arg, &pos))) {
477 get_name = get_optarg(argv, argc, &arg, &pos);
478 }
479 }
480 break;
481 case 'i':
482 if (argv[arg][pos] == 0) {
483 opt_i = TRUE;
484 exit_failure = FALSE; /* may override -oem */
485 } else {
486 fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
487 exit(EXIT_FAILURE);
488 }
489 break;
490 case 'M':
491 {
492 mta_mode = MODE_MCMD;
493 M_cmd = g_strdup(&(argv[arg][pos]));
494 }
495 break;
496 case 'o':
497 switch (argv[arg][pos++]) {
498 case 'e':
499 if (argv[arg][pos++] == 'm') /* -oem */
500 if (!opt_i)
501 exit_failure = TRUE;
502 opt_oem = TRUE;
503 break;
504 case 'd':
505 if (argv[arg][pos] == 'b') /* -odb */
506 opt_odb = TRUE;
507 else if (argv[arg][pos] == 'q') /* -odq */
508 do_queue = TRUE;
509 break;
510 case 'i':
511 opt_i = TRUE;
512 exit_failure = FALSE; /* may override -oem */
513 break;
514 }
515 break;
517 case 'q':
518 {
519 gchar *optarg;
521 do_runq = TRUE;
522 mta_mode = MODE_RUNQUEUE;
523 if (argv[arg][pos] == 'o') {
524 pos++;
525 do_runq = FALSE;
526 do_runq_online = TRUE;
527 /* can be NULL, then we use online detection method */
528 route_name = get_optarg(argv, argc, &arg, &pos);
529 } else
530 if ((optarg = get_optarg(argv, argc, &arg, &pos))) {
531 mta_mode = MODE_DAEMON;
532 queue_interval = time_interval(optarg, &pos);
533 }
534 }
535 break;
536 case 't':
537 if (argv[arg][pos] == 0) {
538 opt_t = TRUE;
539 } else {
540 fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
541 exit(EXIT_FAILURE);
542 }
543 break;
544 case 'v':
545 do_verbose = TRUE;
546 break;
547 default:
548 fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
549 exit(EXIT_FAILURE);
550 }
551 } else {
552 if (argv[arg][pos + 1] == '-') {
553 if (argv[arg][pos + 2] != '\0') {
554 fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
555 exit(EXIT_FAILURE);
556 }
557 arg++;
558 }
559 break;
560 }
561 arg++;
562 }
564 if (mta_mode == MODE_VERSION) {
565 gchar *with_resolver = "";
566 gchar *with_smtp_server = "";
567 gchar *with_pop3 = "";
568 gchar *with_auth = "";
569 gchar *with_maildir = "";
570 gchar *with_ident = "";
571 gchar *with_mserver = "";
573 #ifdef ENABLE_RESOLVER
574 with_resolver = " +resolver";
575 #endif
576 #ifdef ENABLE_SMTP_SERVER
577 with_smtp_server = " +smtp-server";
578 #endif
579 #ifdef ENABLE_POP3
580 with_pop3 = " +pop3";
581 #endif
582 #ifdef ENABLE_AUTH
583 with_auth = " +auth";
584 #endif
585 #ifdef ENABLE_MAILDIR
586 with_maildir = " +maildir";
587 #endif
588 #ifdef ENABLE_IDENT
589 with_ident = " +ident";
590 #endif
591 #ifdef ENABLE_MSERVER
592 with_mserver = " +mserver";
593 #endif
595 printf("%s %s%s%s%s%s%s%s%s\n", PACKAGE, VERSION, with_resolver, with_smtp_server,
596 with_pop3, with_auth, with_maildir, with_ident, with_mserver);
598 exit(EXIT_SUCCESS);
599 }
601 /* initialize random generator */
602 srand(time(NULL));
603 /* ignore SIGPIPE signal */
604 signal(SIGPIPE, SIG_IGN);
606 /* close all possibly open file descriptors */
607 {
608 int i, max_fd = sysconf(_SC_OPEN_MAX);
610 if (max_fd <= 0)
611 max_fd = 64;
612 for (i = 3; i < max_fd; i++)
613 close(i);
614 }
616 init_conf();
618 /* if we are not privileged, and the config file was changed we
619 implicetely set the the run_as_user flag and give up all
620 privileges.
622 So it is possible for a user to run his own daemon without
623 breaking security.
624 */
625 if (strcmp(conf_file, CONF_FILE) != 0) {
626 if (conf.orig_uid != 0) {
627 conf.run_as_user = TRUE;
628 seteuid(conf.orig_uid);
629 setegid(conf.orig_gid);
630 setuid(conf.orig_uid);
631 setgid(conf.orig_gid);
632 }
633 }
635 read_conf(conf_file);
637 if (do_queue)
638 conf.do_queue = TRUE;
639 if (do_verbose)
640 conf.do_verbose = TRUE;
641 if (debug_level >= 0) /* if >= 0, it was given by argument */
642 conf.debug_level = debug_level;
644 /* It appears that changing to / ensures that we are never in
645 a directory which we cannot access. This situation could be
646 possible after changing identity.
647 Maybe we should only change to / if we not run as user, to
648 allow relative paths for log files in test setups for
649 instance.
650 */
651 chdir("/");
653 if (!conf.run_as_user) {
654 if (setgid(0) != 0) {
655 fprintf(stderr, "could not set gid to 0. Is the setuid bit set? : %s\n", strerror(errno));
656 exit(EXIT_FAILURE);
657 }
658 if (setuid(0) != 0) {
659 fprintf(stderr, "could not gain root privileges. Is the setuid bit set? : %s\n", strerror(errno));
660 exit(EXIT_FAILURE);
661 }
662 }
664 if (!logopen()) {
665 fprintf(stderr, "could not open log file\n");
666 exit(EXIT_FAILURE);
667 }
669 DEBUG(1) debugf("masqmail %s starting\n", VERSION);
671 DEBUG(5) {
672 gchar **str = argv;
673 debugf("args: \n");
674 while (*str) {
675 debugf("%s \n", *str);
676 str++;
677 }
678 }
679 DEBUG(5) debugf("queue_interval = %d\n", queue_interval);
681 if (f_address) {
682 return_path = create_address_qualified(f_address, TRUE, conf.host_name);
683 g_free(f_address);
684 if (!return_path) {
685 fprintf(stderr, "invalid RFC821 address: %s\n", f_address);
686 exit(EXIT_FAILURE);
687 }
688 }
690 if (do_get) {
691 #ifdef ENABLE_POP3
692 if ((mta_mode == MODE_NONE) || (mta_mode == MODE_RUNQUEUE)) {
693 set_identity(conf.orig_uid, "getting mail");
694 if (do_get_online) {
695 if (route_name != NULL) {
696 conf.online_detect = g_strdup("argument");
697 set_online_name(route_name);
698 }
699 get_online();
700 } else {
701 if (get_name)
702 get_from_name(get_name);
703 else
704 get_all();
705 }
706 } else {
707 logwrite(LOG_ALERT, "get (-g) only allowed alone or together with queue run (-q)\n");
708 }
709 #else
710 fprintf(stderr, "get (pop) support not compiled in\n");
711 #endif
712 }
714 switch (mta_mode) {
715 case MODE_DAEMON:
716 mode_daemon(do_listen, queue_interval, argv);
717 break;
718 case MODE_RUNQUEUE:
719 {
720 /* queue runs */
721 set_identity(conf.orig_uid, "queue run");
723 if (do_runq)
724 exit_code = queue_run() ? EXIT_SUCCESS : EXIT_FAILURE;
726 if (do_runq_online) {
727 if (route_name != NULL) {
728 conf.online_detect = g_strdup("argument");
729 set_online_name(route_name);
730 }
731 exit_code =
732 queue_run_online() ? EXIT_SUCCESS : EXIT_FAILURE;
733 }
734 }
735 break;
736 case MODE_GET_DAEMON:
737 #ifdef ENABLE_POP3
738 if (route_name != NULL) {
739 conf.online_detect = g_strdup("argument");
740 set_online_name(route_name);
741 }
742 mode_get_daemon(get_interval, argv);
743 #endif
744 break;
746 case MODE_SMTP:
747 #ifdef ENABLE_SMTP_SERVER
748 mode_smtp();
749 #else
750 fprintf(stderr, "smtp server support not compiled in\n");
751 #endif
752 break;
754 case MODE_LIST:
755 queue_list();
756 break;
758 case MODE_BI:
759 exit(EXIT_SUCCESS);
760 break; /* well... */
762 case MODE_MCMD:
763 if (strcmp(M_cmd, "rm") == 0) {
764 gboolean ok = FALSE;
766 set_euidgid(conf.mail_uid, conf.mail_gid, NULL, NULL);
768 if (is_privileged_user(conf.orig_uid)) {
769 for (; arg < argc; arg++) {
770 if (queue_delete(argv[arg]))
771 ok = TRUE;
772 }
773 } else {
774 struct passwd *pw = getpwuid(conf.orig_uid);
775 if (pw) {
776 for (; arg < argc; arg++) {
777 message *msg = msg_spool_read(argv[arg], FALSE);
778 #ifdef ENABLE_IDENT
779 if (((msg->received_host == NULL) && (msg->received_prot == PROT_LOCAL))
780 || is_in_netlist(msg->received_host, conf.ident_trusted_nets)) {
781 #else
782 if ((msg->received_host == NULL) && (msg->received_prot == PROT_LOCAL)) {
783 #endif
784 if (msg->ident) {
785 if (strcmp(pw->pw_name, msg->ident) == 0) {
786 if (queue_delete(argv[arg]))
787 ok = TRUE;
788 } else {
789 fprintf(stderr, "you do not own message id %s\n", argv[arg]);
790 }
791 } else
792 fprintf(stderr, "message %s does not have an ident.\n", argv[arg]);
793 } else {
794 fprintf(stderr, "message %s was not received locally or from a trusted network.\n", argv[arg]);
795 }
796 }
797 } else {
798 fprintf(stderr, "could not find a passwd entry for uid %d: %s\n", conf.orig_uid, strerror(errno));
799 }
800 }
801 exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
802 } else {
803 fprintf(stderr, "unknown command %s\n", M_cmd);
804 exit(EXIT_FAILURE);
805 }
806 break;
808 case MODE_ACCEPT:
809 {
810 guint accept_flags = (opt_t ? ACC_DEL_RCPTS | ACC_DEL_BCC | ACC_RCPT_FROM_HEAD : ACC_HEAD_FROM_RCPT)
811 | (opt_i ? ACC_NODOT_TERM : ACC_NODOT_RELAX);
812 mode_accept(return_path, full_sender_name, accept_flags, &(argv[arg]), argc - arg);
813 exit(exit_failure ? EXIT_FAILURE : EXIT_SUCCESS);
814 }
815 break;
816 case MODE_NONE:
817 break;
818 default:
819 fprintf(stderr, "unknown mode: %d\n", mta_mode);
820 break;
821 }
823 logclose();
825 exit(exit_code);
826 }