masqmail
view src/masqmail.c @ 250:a41c013c8458
moved the queue manipulation code to an new function
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Thu, 04 Nov 2010 12:32:11 -0300 |
parents | f9da5a7caeda |
children | 2babd21e7c75 |
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 no 'queue daemon' mode.
38 It, as well as the distinction beween the two (non exclusive) daemon
39 (queue and listen) modes, is handled 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_SMTP, /* accept SMTP on stdin */
45 MODE_LIST, /* list queue */
46 MODE_MCMD, /* do queue manipulation */
47 MODE_VERSION, /* show version */
48 MODE_BI, /* fake ;-) */
49 MODE_NONE /* to prevent default MODE_ACCEPT */
50 } mta_mode;
52 char *pidfile = NULL;
53 volatile int sigterm_in_progress = 0;
55 static void
56 sigterm_handler(int sig)
57 {
58 if (sigterm_in_progress)
59 raise(sig);
60 sigterm_in_progress = 1;
62 if (pidfile) {
63 uid_t uid;
64 uid = seteuid(0);
65 if (unlink(pidfile) != 0)
66 logwrite(LOG_WARNING, "could not delete pid file %s: %s\n", pidfile, strerror(errno));
67 seteuid(uid); /* we exit anyway after this, just to be sure */
68 }
70 signal(sig, SIG_DFL);
71 raise(sig);
72 }
74 #ifdef ENABLE_IDENT /* so far used for that only */
75 static gboolean
76 is_in_netlist(gchar * host, GList * netlist)
77 {
78 guint hostip = inet_addr(host);
79 struct in_addr addr;
81 addr.s_addr = hostip;
82 if (addr.s_addr != INADDR_NONE) {
83 GList *node;
84 foreach(netlist, node) {
85 struct in_addr *net = (struct in_addr *) (node->data);
86 if ((addr.s_addr & net->s_addr) == net->s_addr)
87 return TRUE;
88 }
89 }
90 return FALSE;
91 }
92 #endif
94 /*
95 argv: the original argv
96 argp: number of arg (may get modified!)
97 cp: pointing to the char after the option
98 e.g. `-d 6' `-d6'
99 ^ ^
100 */
101 gchar*
102 get_optarg(char* argv[], gint* argp, char* cp)
103 {
104 if (*cp) {
105 /* this kind: -xval */
106 return cp;
107 }
108 cp = argv[*argp+1];
109 if (cp && (*cp != '-')) {
110 /* this kind: -x val */
111 (*argp)++;
112 return cp;
113 }
114 return NULL;
115 }
117 gchar*
118 get_progname(gchar * arg0)
119 {
120 gchar *p = arg0 + strlen(arg0) - 1;
121 while (p > arg0) {
122 if (*p == '/')
123 return p + 1;
124 p--;
125 }
126 return p;
127 }
129 gboolean
130 write_pidfile(gchar * name)
131 {
132 FILE *fptr;
134 if ((fptr = fopen(name, "wt"))) {
135 fprintf(fptr, "%d\n", getpid());
136 fclose(fptr);
137 pidfile = strdup(name);
138 return TRUE;
139 }
140 logwrite(LOG_WARNING, "could not write pid file: %s\n", strerror(errno));
141 return FALSE;
142 }
144 static void
145 mode_daemon(gboolean do_listen, gint queue_interval, char *argv[])
146 {
147 guint pid;
149 /* daemon */
150 if (!conf.run_as_user) {
151 if ((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)) {
152 fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER);
153 exit(EXIT_FAILURE);
154 }
155 }
157 /* reparent to init only if init is not already the parent */
158 if (getppid() != 1) {
159 if ((pid = fork()) > 0) {
160 exit(EXIT_SUCCESS);
161 } else if (pid < 0) {
162 logwrite(LOG_ALERT, "could not fork!\n");
163 exit(EXIT_FAILURE);
164 }
165 }
167 signal(SIGTERM, sigterm_handler);
168 write_pidfile(PIDFILEDIR "/masqmail.pid");
170 conf.do_verbose = FALSE;
172 /* closing and reopening the log ensures that it is open afterwards
173 because it is possible that the log is assigned to fd 1 and gets
174 thus closes by fclose(stdout). Similar for the debugfile.
175 */
176 logclose();
177 fclose(stdin);
178 fclose(stdout);
179 fclose(stderr);
180 logopen();
182 logwrite(LOG_NOTICE, "%s %s daemon starting\n", PACKAGE, VERSION);
183 listen_port(do_listen ? conf.listen_addresses : NULL, queue_interval, argv);
184 }
186 static void
187 mode_smtp()
188 {
189 /* accept smtp message on stdin */
190 /* write responses to stderr. */
192 struct sockaddr_in saddr;
193 gchar *peername = NULL;
194 int dummy = sizeof(saddr);
196 conf.do_verbose = FALSE;
198 if (!conf.run_as_user) {
199 seteuid(conf.orig_uid);
200 setegid(conf.orig_gid);
201 }
203 DEBUG(5) debugf("accepting smtp message on stdin\n");
205 if (getpeername(0, (struct sockaddr *) (&saddr), &dummy) == 0) {
206 peername = g_strdup(inet_ntoa(saddr.sin_addr));
207 } else if (errno != ENOTSOCK)
208 exit(EXIT_FAILURE);
210 smtp_in(stdin, stderr, peername, NULL);
211 }
213 static void
214 mode_accept(address * return_path, gchar * full_sender_name, guint accept_flags, char **addresses, int addr_cnt)
215 {
216 /* accept message on stdin */
217 accept_error err;
218 message *msg = create_message();
219 gint i;
221 if (return_path && !is_privileged_user(conf.orig_uid)) {
222 fprintf(stderr, "must be root, %s or in group %s for setting return path.\n", DEF_MAIL_USER, DEF_MAIL_GROUP);
223 exit(EXIT_FAILURE);
224 }
226 if (!conf.run_as_user) {
227 seteuid(conf.orig_uid);
228 setegid(conf.orig_gid);
229 }
231 DEBUG(5) debugf("accepting message on stdin\n");
233 msg->received_prot = PROT_LOCAL;
234 for (i = 0; i < addr_cnt; i++) {
235 if (addresses[i][0] != '|')
236 msg->rcpt_list = g_list_append(msg->rcpt_list, create_address_qualified(addresses[i], TRUE, conf.host_name));
237 else {
238 logwrite(LOG_ALERT, "no pipe allowed as recipient address: %s\n", addresses[i]);
239 exit(EXIT_FAILURE);
240 }
241 }
243 /* -f option */
244 msg->return_path = return_path;
246 /* -F option */
247 msg->full_sender_name = full_sender_name;
249 if ((err = accept_message(stdin, msg, accept_flags)) == AERR_OK) {
250 if (spool_write(msg, TRUE)) {
251 pid_t pid;
252 logwrite(LOG_NOTICE, "%s <= %s with %s\n", msg->uid, addr_string(msg->return_path), prot_names[PROT_LOCAL]);
254 if (!conf.do_queue) {
255 if ((pid = fork()) == 0) {
256 conf.do_verbose = FALSE;
257 fclose(stdin);
258 fclose(stdout);
259 fclose(stderr);
260 if (deliver(msg)) {
261 exit(EXIT_SUCCESS);
262 } else
263 exit(EXIT_FAILURE);
264 } else if (pid < 0) {
265 logwrite(LOG_ALERT, "could not fork for delivery, id = %s\n", msg->uid);
266 }
267 }
268 } else {
269 fprintf(stderr, "Could not write spool file\n");
270 exit(EXIT_FAILURE);
271 }
272 } else {
273 switch (err) {
274 case AERR_EOF:
275 fprintf(stderr, "unexpected EOF.\n");
276 exit(EXIT_FAILURE);
277 case AERR_NORCPT:
278 fprintf(stderr, "no recipients.\n");
279 exit(EXIT_FAILURE);
280 case AERR_SIZE:
281 fprintf(stderr, "max message size exceeded.\n");
282 exit(EXIT_FAILURE);
283 default:
284 /* should never happen: */
285 fprintf(stderr, "Unknown error (%d)\r\n", err);
286 exit(EXIT_FAILURE);
287 }
288 exit(EXIT_FAILURE);
289 }
290 }
292 /*
293 currently only the `rm' command is supported
294 until this changes, we don't need any facility for further commands
295 return success if at least one message had been deleted
296 */
297 static int
298 manipulate_queue(char* cmd, char* id[])
299 {
300 gboolean ok = FALSE;
302 if (strcmp(cmd, "rm") != 0) {
303 fprintf(stderr, "unknown command %s\n", cmd);
304 return FALSE;
305 }
307 set_euidgid(conf.mail_uid, conf.mail_gid, NULL, NULL);
309 /* privileged users may delete any mail */
310 if (is_privileged_user(conf.orig_uid)) {
311 for (; *id; id++) {
312 fprintf(stderr, "id: %s\n", *id);
313 if (queue_delete(*id)) {
314 ok = TRUE;
315 }
316 }
317 return ok;
318 }
320 struct passwd *pw = getpwuid(conf.orig_uid);
321 if (!pw) {
322 fprintf(stderr, "could not find a passwd entry for uid %d: %s\n",
323 conf.orig_uid, strerror(errno));
324 return FALSE;
325 }
327 /* non-privileged users may only delete their own messages */
328 for (; *id; id++) {
329 message *msg = msg_spool_read(*id, FALSE);
331 fprintf(stderr, "id: %s\n", *id);
333 if (!msg->ident) {
334 fprintf(stderr, "message %s does not have an ident\n", *id);
335 continue;
336 }
337 if (strcmp(pw->pw_name, msg->ident) != 0) {
338 fprintf(stderr, "you do not own message id %s\n", *id);
339 continue;
340 }
342 if ( (msg->received_host || (msg->received_prot != PROT_LOCAL))
343 #ifdef ENABLE_IDENT
344 && !is_in_netlist(msg->received_host, conf.ident_trusted_nets)
345 #endif
346 ) {
347 fprintf(stderr, "message %s was not received locally or from a trusted network\n", *id);
348 continue;
349 }
351 ok = queue_delete(*id);
352 }
353 return ok;
354 }
356 int
357 main(int argc, char *argv[])
358 {
359 /* cmd line flags */
360 gchar *conf_file = CONF_FILE;
361 char* opt;
362 gint arg;
364 gboolean do_listen = FALSE;
365 gboolean do_runq = FALSE;
366 gboolean do_runq_online = FALSE;
368 gboolean do_queue = FALSE;
370 gboolean do_verbose = FALSE;
371 gint debug_level = -1;
373 mta_mode mta_mode = MODE_ACCEPT;
375 gint queue_interval = 0;
376 gboolean opt_t = FALSE;
377 gboolean opt_i = FALSE;
378 gboolean exit_failure = FALSE;
380 gchar *M_cmd = NULL;
382 gint exit_code = EXIT_SUCCESS;
383 gchar *route_name = NULL;
384 gchar *progname;
385 gchar *f_address = NULL;
386 gchar *full_sender_name = NULL;
387 address *return_path = NULL; /* may be changed by -f option */
389 progname = get_progname(argv[0]);
391 if (strcmp(progname, "mailq") == 0) {
392 mta_mode = MODE_LIST;
393 } else if (strcmp(progname, "mailrm") == 0) {
394 mta_mode = MODE_MCMD;
395 M_cmd = "rm";
396 } else if (strcmp(progname, "runq") == 0) {
397 mta_mode = MODE_RUNQUEUE;
398 do_runq = TRUE;
399 } else if (strcmp(progname, "rmail") == 0) {
400 /* the `rmail' alias should probably be removed now
401 that we have the rmail script. But let's keep it
402 for some while for compatibility. 2010-06-19 */
403 mta_mode = MODE_ACCEPT;
404 opt_i = TRUE;
405 } else if (strcmp(progname, "smtpd") == 0 || strcmp(progname, "in.smtpd") == 0) {
406 mta_mode = MODE_SMTP;
407 }
409 /* parse cmd line */
410 for (arg=1; arg<argc && argv[arg][0]=='-'; arg++) {
411 opt = argv[arg] + 1;
413 if (strcmp(opt, "-") == 0) {
414 /* everything after `--' are address arguments */
415 arg++;
416 break;
418 } else if (strcmp(opt, "bd") == 0) {
419 do_listen = TRUE;
420 mta_mode = MODE_DAEMON;
422 } else if (strcmp(opt, "bi") == 0) {
423 /* ignored */
424 mta_mode = MODE_BI;
426 } else if (strcmp(opt, "bs") == 0) {
427 mta_mode = MODE_SMTP;
429 } else if (strcmp(opt, "bp") == 0) {
430 mta_mode = MODE_LIST;
432 } else if (strcmp(opt, "bV") == 0) {
433 mta_mode = MODE_VERSION;
435 } else if (strncmp(opt, "B", 1) == 0) {
436 /* we ignore this and throw the argument away */
437 get_optarg(argv, &arg, opt+1);
439 } else if (strncmp(opt, "C", 1) == 0) {
440 if (!(conf_file = get_optarg(argv, &arg, opt+1))) {
441 fprintf(stderr, "-C requires a filename as argument.\n");
442 exit(EXIT_FAILURE);
443 }
445 } else if (strncmp(opt, "d", 1) == 0) {
446 if (getuid() != 0) {
447 fprintf(stderr, "only root may set the debug level.\n");
448 exit(EXIT_FAILURE);
449 }
450 char *lvl = get_optarg(argv, &arg, opt+1);
451 if (!lvl) {
452 fprintf(stderr, "-d requires a number argument.\n");
453 exit(EXIT_FAILURE);
454 }
455 debug_level = atoi(lvl);
457 } else if (strncmp(opt, "f", 1) == 0) {
458 /* set return path */
459 gchar *address = get_optarg(argv, &arg, opt+1);
460 if (!address) {
461 fprintf(stderr, "-f requires an address argument\n");
462 exit(EXIT_FAILURE);
463 }
464 f_address = g_strdup(address);
466 } else if (strncmp(opt, "F", 1) == 0) {
467 full_sender_name = get_optarg(argv, &arg, opt+1);
468 if (!full_sender_name) {
469 fprintf(stderr, "-F requires a name argument\n");
470 exit(EXIT_FAILURE);
471 }
473 } else if (strcmp(opt, "i") == 0) {
474 opt_i = TRUE;
475 exit_failure = FALSE; /* may override -oem */
477 } else if (strcmp(opt, "m") == 0) {
478 /* ignore -m (me too) switch (see man page) */
480 } else if (strcmp(opt, "Mrm") == 0) {
481 mta_mode = MODE_MCMD;
482 M_cmd = "rm";
484 } else if (strcmp(opt, "odq") == 0) {
485 do_queue = TRUE;
487 } else if (strcmp(opt, "oem") == 0) {
488 if (!opt_i) {
489 /* TODO: Why is this related to -i in any way? */
490 exit_failure = TRUE;
491 }
493 } else if (strcmp(opt, "oi") == 0) {
494 exit_failure = FALSE; /* may override -oem */
496 } else if (strncmp(opt, "o", 1) == 0) {
497 /* ignore all other -oXXX options */
499 } else if (strncmp(opt, "qo", 2) == 0) {
500 mta_mode = MODE_RUNQUEUE;
501 do_runq = FALSE;
502 do_runq_online = TRUE;
503 /* can be NULL, then we use online detection method */
504 route_name = get_optarg(argv, &arg, opt+2);
506 } else if (strncmp(opt, "q", 1) == 0) {
507 /* must be after the `qo' check */
508 gchar *optarg;
509 int dummy;
511 do_runq = TRUE;
512 mta_mode = MODE_RUNQUEUE;
513 if ((optarg = get_optarg(argv, &arg, opt+1))) {
514 mta_mode = MODE_DAEMON;
515 queue_interval = time_interval(optarg, &dummy);
516 }
518 } else if (strcmp(opt, "t") == 0) {
519 opt_t = TRUE;
521 } else if (strcmp(opt, "v") == 0) {
522 do_verbose = TRUE;
524 } else {
525 fprintf(stderr, "unrecognized option `-%s'\n", opt);
526 exit(EXIT_FAILURE);
527 }
528 }
530 if (mta_mode == MODE_VERSION) {
531 gchar *with_resolver = "";
532 gchar *with_auth = "";
533 gchar *with_ident = "";
535 #ifdef ENABLE_RESOLVER
536 with_resolver = " +resolver";
537 #endif
538 #ifdef ENABLE_AUTH
539 with_auth = " +auth";
540 #endif
541 #ifdef ENABLE_IDENT
542 with_ident = " +ident";
543 #endif
545 printf("%s %s%s%s%s\n", PACKAGE, VERSION, with_resolver, with_auth, with_ident);
547 exit(EXIT_SUCCESS);
548 }
550 /* initialize random generator */
551 srand(time(NULL));
552 /* ignore SIGPIPE signal */
553 signal(SIGPIPE, SIG_IGN);
555 /* close all possibly open file descriptors, except std{in,out,err} */
556 {
557 int i, max_fd = sysconf(_SC_OPEN_MAX);
559 if (max_fd <= 0)
560 max_fd = 64;
561 for (i = 3; i < max_fd; i++)
562 close(i);
563 }
565 init_conf();
567 /* if we are not privileged, and the config file was changed we
568 implicetely set the the run_as_user flag and give up all
569 privileges.
571 So it is possible for a user to run his own daemon without
572 breaking security.
573 */
574 if (strcmp(conf_file, CONF_FILE) != 0) {
575 if (conf.orig_uid != 0) {
576 conf.run_as_user = TRUE;
577 seteuid(conf.orig_uid);
578 setegid(conf.orig_gid);
579 setuid(conf.orig_uid);
580 setgid(conf.orig_gid);
581 }
582 }
584 conf.log_dir = LOG_DIR;
585 logopen();
586 if (!read_conf(conf_file)) {
587 logwrite(LOG_ALERT, "SHUTTING DOWN due to problems reading config\n");
588 exit(5);
589 }
590 logclose();
592 if (do_queue)
593 conf.do_queue = TRUE;
594 if (do_verbose)
595 conf.do_verbose = TRUE;
596 if (debug_level >= 0) /* if >= 0, it was given by argument */
597 conf.debug_level = debug_level;
599 /* It appears that changing to / ensures that we are never in
600 a directory which we cannot access. This situation could be
601 possible after changing identity.
602 Maybe we should only change to / if we not run as user, to
603 allow relative paths for log files in test setups for
604 instance.
605 */
606 chdir("/");
608 if (!conf.run_as_user) {
609 if (setgid(0) != 0) {
610 fprintf(stderr, "could not set gid to 0. Is the setuid bit set? : %s\n", strerror(errno));
611 exit(EXIT_FAILURE);
612 }
613 if (setuid(0) != 0) {
614 fprintf(stderr, "could not gain root privileges. Is the setuid bit set? : %s\n", strerror(errno));
615 exit(EXIT_FAILURE);
616 }
617 }
619 if (!logopen()) {
620 fprintf(stderr, "could not open log file\n");
621 exit(EXIT_FAILURE);
622 }
624 DEBUG(1) debugf("masqmail %s starting\n", VERSION);
626 DEBUG(5) {
627 gchar **str = argv;
628 debugf("args: \n");
629 while (*str) {
630 debugf("%s \n", *str);
631 str++;
632 }
633 }
634 DEBUG(5) debugf("queue_interval = %d\n", queue_interval);
636 if (f_address) {
637 return_path = create_address_qualified(f_address, TRUE, conf.host_name);
638 g_free(f_address);
639 if (!return_path) {
640 fprintf(stderr, "invalid RFC821 address: %s\n", f_address);
641 exit(EXIT_FAILURE);
642 }
643 }
645 switch (mta_mode) {
646 case MODE_DAEMON:
647 mode_daemon(do_listen, queue_interval, argv);
648 break;
649 case MODE_RUNQUEUE:
650 {
651 /* queue runs */
652 set_identity(conf.orig_uid, "queue run");
654 if (do_runq)
655 exit_code = queue_run() ? EXIT_SUCCESS : EXIT_FAILURE;
657 if (do_runq_online) {
658 if (route_name != NULL) {
659 conf.online_detect = g_strdup("argument");
660 set_online_name(route_name);
661 }
662 exit_code =
663 queue_run_online() ? EXIT_SUCCESS : EXIT_FAILURE;
664 }
665 }
666 break;
668 case MODE_SMTP:
669 mode_smtp();
670 break;
672 case MODE_LIST:
673 queue_list();
674 break;
676 case MODE_BI:
677 exit(EXIT_SUCCESS);
678 break; /* well... */
680 case MODE_MCMD:
681 exit(manipulate_queue(M_cmd, &argv[arg]) ? 0 : 1);
682 break;
684 case MODE_ACCEPT:
685 {
686 guint accept_flags = (opt_t ? ACC_DEL_RCPTS | ACC_RCPT_FROM_HEAD : 0)
687 | (opt_i ? ACC_DOT_IGNORE : ACC_NODOT_RELAX);
688 mode_accept(return_path, full_sender_name, accept_flags, &(argv[arg]), argc - arg);
689 exit(exit_failure ? EXIT_FAILURE : EXIT_SUCCESS);
690 }
691 break;
692 case MODE_NONE:
693 break;
694 default:
695 fprintf(stderr, "unknown mode: %d\n", mta_mode);
696 break;
697 }
699 logclose();
701 exit(exit_code);
702 }