masqmail-0.2

view src/masqmail.c @ 184:b3835b6b834b

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