masqmail

view src/masqmail.c @ 257:8cca5305e4f0

fixed -oi to really be the same as -i
author markus schnalke <meillo@marmaro.de>
date Wed, 01 Dec 2010 17:29:52 -0300
parents 82d168dd52fd
children 05fa719b7002
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 static int
357 run_queue(gboolean do_runq, gboolean do_runq_online, char* route_name)
358 {
359 int ret;
361 /* queue runs */
362 set_identity(conf.orig_uid, "queue run");
364 if (do_runq) {
365 ret = queue_run();
366 }
368 if (do_runq_online) {
369 if (route_name) {
370 conf.online_detect = g_strdup("argument");
371 set_online_name(route_name);
372 }
373 ret = queue_run_online();
374 }
375 return ret;
376 }
378 int
379 main(int argc, char *argv[])
380 {
381 gchar *progname;
382 char* opt;
383 gint arg;
385 mta_mode mta_mode = MODE_ACCEPT;
386 gboolean do_listen = FALSE;
387 gboolean do_runq = FALSE;
388 gboolean do_runq_online = FALSE;
389 gboolean do_queue = FALSE;
390 gint queue_interval = 0;
391 gchar *M_cmd = NULL;
392 gboolean opt_t = FALSE;
393 gboolean opt_i = FALSE;
394 gboolean exit_failure = FALSE;
395 gint exit_code = EXIT_SUCCESS;
396 gchar *conf_file = CONF_FILE;
397 gchar *route_name = NULL;
398 gchar *f_address = NULL;
399 address *return_path = NULL; /* may be changed by -f option */
400 gchar *full_sender_name = NULL;
401 gboolean do_verbose = FALSE;
402 gint debug_level = -1;
404 progname = get_progname(argv[0]);
406 if (strcmp(progname, "mailq") == 0) {
407 mta_mode = MODE_LIST;
408 } else if (strcmp(progname, "mailrm") == 0) {
409 mta_mode = MODE_MCMD;
410 M_cmd = "rm";
411 } else if (strcmp(progname, "runq") == 0) {
412 mta_mode = MODE_RUNQUEUE;
413 do_runq = TRUE;
414 } else if (strcmp(progname, "rmail") == 0) {
415 /* the `rmail' alias should probably be removed now
416 that we have the rmail script. But let's keep it
417 for some while for compatibility. 2010-06-19 */
418 mta_mode = MODE_ACCEPT;
419 opt_i = TRUE;
420 } else if (strcmp(progname, "smtpd") == 0 || strcmp(progname, "in.smtpd") == 0) {
421 mta_mode = MODE_SMTP;
422 }
424 /* parse cmd line */
425 for (arg=1; arg<argc && argv[arg][0]=='-'; arg++) {
426 opt = argv[arg] + 1; /* points to the char after the dash */
428 if (strcmp(opt, "-") == 0) {
429 /* everything after `--' are address arguments */
430 arg++;
431 break;
433 } else if (strcmp(opt, "bd") == 0) {
434 do_listen = TRUE;
435 mta_mode = MODE_DAEMON;
437 } else if (strcmp(opt, "bi") == 0) {
438 /* ignored */
439 mta_mode = MODE_BI;
441 } else if (strcmp(opt, "bs") == 0) {
442 mta_mode = MODE_SMTP;
444 } else if (strcmp(opt, "bp") == 0) {
445 mta_mode = MODE_LIST;
447 } else if (strcmp(opt, "bV") == 0) {
448 mta_mode = MODE_VERSION;
450 } else if (strncmp(opt, "B", 1) == 0) {
451 /* we ignore this and throw the argument away */
452 get_optarg(argv, &arg, opt+1);
454 } else if (strncmp(opt, "C", 1) == 0) {
455 conf_file = get_optarg(argv, &arg, opt+1);
456 if (!conf_file) {
457 fprintf(stderr, "-C requires a filename as argument.\n");
458 exit(EXIT_FAILURE);
459 }
461 } else if (strncmp(opt, "d", 1) == 0) {
462 if (getuid() != 0) {
463 fprintf(stderr, "only root may set the debug level.\n");
464 exit(EXIT_FAILURE);
465 }
466 char *lvl = get_optarg(argv, &arg, opt+1);
467 if (!lvl) {
468 fprintf(stderr, "-d requires a number argument.\n");
469 exit(EXIT_FAILURE);
470 }
471 debug_level = atoi(lvl);
473 } else if (strncmp(opt, "f", 1) == 0) {
474 /* set return path */
475 gchar *address = get_optarg(argv, &arg, opt+1);
476 if (!address) {
477 fprintf(stderr, "-f requires an address argument\n");
478 exit(EXIT_FAILURE);
479 }
480 f_address = g_strdup(address);
482 } else if (strncmp(opt, "F", 1) == 0) {
483 full_sender_name = get_optarg(argv, &arg, opt+1);
484 if (!full_sender_name) {
485 fprintf(stderr, "-F requires a name argument\n");
486 exit(EXIT_FAILURE);
487 }
489 } else if (strcmp(opt, "i") == 0) {
490 opt_i = TRUE;
491 exit_failure = FALSE; /* may override -oem */
493 } else if (strcmp(opt, "m") == 0) {
494 /* ignore -m (me too) switch (see man page) */
496 } else if (strcmp(opt, "Mrm") == 0) {
497 mta_mode = MODE_MCMD;
498 M_cmd = "rm";
500 } else if (strcmp(opt, "odq") == 0) {
501 do_queue = TRUE;
503 } else if (strcmp(opt, "oem") == 0) {
504 if (!opt_i) {
505 /* TODO: Why is this related to -i in any way? */
506 exit_failure = TRUE;
507 }
509 } else if (strcmp(opt, "oi") == 0) {
510 opt_i = TRUE;
511 exit_failure = FALSE; /* may override -oem */
513 } else if (strncmp(opt, "o", 1) == 0) {
514 /* ignore all other -oXXX options */
516 } else if (strncmp(opt, "qo", 2) == 0) {
517 mta_mode = MODE_RUNQUEUE;
518 do_runq = FALSE;
519 do_runq_online = TRUE;
520 /* can be NULL, then we use online detection method */
521 route_name = get_optarg(argv, &arg, opt+2);
523 } else if (strncmp(opt, "q", 1) == 0) {
524 /* must be after the `qo' check */
525 gchar *optarg;
527 do_runq = TRUE;
528 mta_mode = MODE_RUNQUEUE;
529 optarg = get_optarg(argv, &arg, opt+1);
530 if (optarg) {
531 /* not just one single queue run but regular runs */
532 mta_mode = MODE_DAEMON;
533 queue_interval = time_interval(optarg);
534 }
536 } else if (strcmp(opt, "t") == 0) {
537 opt_t = TRUE;
539 } else if (strcmp(opt, "v") == 0) {
540 do_verbose = TRUE;
542 } else {
543 fprintf(stderr, "unrecognized option `-%s'\n", opt);
544 exit(EXIT_FAILURE);
545 }
546 }
548 if (mta_mode == MODE_VERSION) {
549 gchar *with_resolver = "";
550 gchar *with_auth = "";
551 gchar *with_ident = "";
553 #ifdef ENABLE_RESOLVER
554 with_resolver = " +resolver";
555 #endif
556 #ifdef ENABLE_AUTH
557 with_auth = " +auth";
558 #endif
559 #ifdef ENABLE_IDENT
560 with_ident = " +ident";
561 #endif
563 printf("%s %s%s%s%s\n", PACKAGE, VERSION, with_resolver, with_auth, with_ident);
565 exit(EXIT_SUCCESS);
566 }
568 /* initialize random generator */
569 srand(time(NULL));
570 /* ignore SIGPIPE signal */
571 signal(SIGPIPE, SIG_IGN);
573 /* close all possibly open file descriptors, except std{in,out,err} */
574 {
575 int i, max_fd = sysconf(_SC_OPEN_MAX);
577 if (max_fd <= 0) {
578 max_fd = 64;
579 }
580 for (i=3; i<max_fd; i++) {
581 close(i);
582 }
583 }
585 init_conf();
587 /* if we are not privileged, and the config file was changed we
588 implicetely set the the run_as_user flag and give up all
589 privileges.
591 So it is possible for a user to run his own daemon without
592 breaking security.
593 */
594 if ((strcmp(conf_file, CONF_FILE) != 0) && (conf.orig_uid != 0)) {
595 conf.run_as_user = TRUE;
596 seteuid(conf.orig_uid);
597 setegid(conf.orig_gid);
598 setuid(conf.orig_uid);
599 setgid(conf.orig_gid);
600 }
602 conf.log_dir = LOG_DIR;
603 logopen();
604 if (!read_conf(conf_file)) {
605 logwrite(LOG_ALERT, "SHUTTING DOWN due to problems reading config\n");
606 exit(5);
607 }
608 logclose();
610 if (do_queue) {
611 conf.do_queue = TRUE;
612 }
613 if (do_verbose) {
614 conf.do_verbose = TRUE;
615 }
616 if (debug_level >= 0) { /* if >= 0, it was given by argument */
617 conf.debug_level = debug_level;
618 }
620 /* It appears that changing to / ensures that we are never in
621 a directory which we cannot access. This situation could be
622 possible after changing identity.
623 Maybe we should only change to / if we not run as user, to
624 allow relative paths for log files in test setups for
625 instance.
626 */
627 chdir("/");
629 if (!conf.run_as_user) {
630 if (setgid(0) != 0) {
631 fprintf(stderr, "could not set gid to 0. Is the setuid bit set? : %s\n", strerror(errno));
632 exit(EXIT_FAILURE);
633 }
634 if (setuid(0) != 0) {
635 fprintf(stderr, "could not gain root privileges. Is the setuid bit set? : %s\n", strerror(errno));
636 exit(EXIT_FAILURE);
637 }
638 }
640 if (!logopen()) {
641 fprintf(stderr, "could not open log file\n");
642 exit(EXIT_FAILURE);
643 }
645 DEBUG(1) debugf("masqmail %s starting\n", VERSION);
647 DEBUG(5) {
648 gchar **str = argv;
649 debugf("args: \n");
650 while (*str) {
651 debugf("%s \n", *str);
652 str++;
653 }
654 }
655 DEBUG(5) debugf("queue_interval = %d\n", queue_interval);
657 if (f_address) {
658 return_path = create_address_qualified(f_address, TRUE, conf.host_name);
659 g_free(f_address);
660 if (!return_path) {
661 fprintf(stderr, "invalid RFC821 address: %s\n", f_address);
662 exit(EXIT_FAILURE);
663 }
664 }
666 switch (mta_mode) {
667 case MODE_DAEMON:
668 mode_daemon(do_listen, queue_interval, argv);
669 break;
671 case MODE_RUNQUEUE:
672 exit(run_queue(do_runq, do_runq_online, route_name) ? 0 : 1);
673 break;
675 case MODE_SMTP:
676 mode_smtp();
677 break;
679 case MODE_LIST:
680 queue_list();
681 break;
683 case MODE_BI:
684 exit(EXIT_SUCCESS);
685 break; /* well... */
687 case MODE_MCMD:
688 exit(manipulate_queue(M_cmd, &argv[arg]) ? 0 : 1);
689 break;
691 case MODE_ACCEPT:
692 {
693 guint accept_flags = (opt_t ? ACC_DEL_RCPTS | ACC_RCPT_FROM_HEAD : 0)
694 | (opt_i ? ACC_DOT_IGNORE : ACC_NODOT_RELAX);
695 mode_accept(return_path, full_sender_name, accept_flags, &(argv[arg]), argc - arg);
696 exit(exit_failure ? EXIT_FAILURE : EXIT_SUCCESS);
697 }
698 break;
699 case MODE_NONE:
700 break;
702 default:
703 fprintf(stderr, "unknown mode: %d\n", mta_mode);
704 break;
705 }
707 logclose();
709 exit(exit_code);
710 }