meillo@0: /* MasqMail meillo@0: Copyright (C) 1999-2001 Oliver Kurth meillo@76: Copyright (C) 2010 markus schnalke meillo@0: meillo@0: This program is free software; you can redistribute it and/or modify meillo@0: it under the terms of the GNU General Public License as published by meillo@0: the Free Software Foundation; either version 2 of the License, or meillo@0: (at your option) any later version. meillo@0: meillo@0: This program is distributed in the hope that it will be useful, meillo@0: but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: GNU General Public License for more details. meillo@0: meillo@0: You should have received a copy of the GNU General Public License meillo@0: along with this program; if not, write to the Free Software meillo@0: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. meillo@0: */ meillo@0: meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: meillo@0: #include meillo@0: meillo@0: #include "masqmail.h" meillo@0: meillo@0: /* mutually exclusive modes. Note that there is neither a 'get' mode meillo@0: nor a 'queue daemon' mode. These, as well as the distinction beween meillo@0: the two (non exclusive) daemon (queue and listen) modes are handled meillo@0: by flags.*/ meillo@10: typedef enum _mta_mode { meillo@10: MODE_ACCEPT = 0, /* accept message on stdin */ meillo@10: MODE_DAEMON, /* run as daemon */ meillo@10: MODE_RUNQUEUE, /* single queue run, online or offline */ meillo@10: MODE_GET_DAEMON, /* run as get (retrieve) daemon */ meillo@10: MODE_SMTP, /* accept SMTP on stdin */ meillo@10: MODE_LIST, /* list queue */ meillo@10: MODE_MCMD, /* do queue manipulation */ meillo@10: MODE_VERSION, /* show version */ meillo@10: MODE_BI, /* fake ;-) */ meillo@10: MODE_NONE /* to prevent default MODE_ACCEPT */ meillo@10: } mta_mode; meillo@0: meillo@0: char *pidfile = NULL; meillo@0: volatile int sigterm_in_progress = 0; meillo@0: meillo@10: static void meillo@10: sigterm_handler(int sig) meillo@0: { meillo@10: if (sigterm_in_progress) meillo@10: raise(sig); meillo@10: sigterm_in_progress = 1; meillo@0: meillo@10: if (pidfile) { meillo@10: uid_t uid; meillo@10: uid = seteuid(0); meillo@10: if (unlink(pidfile) != 0) meillo@10: logwrite(LOG_WARNING, "could not delete pid file %s: %s\n", pidfile, strerror(errno)); meillo@10: seteuid(uid); /* we exit anyway after this, just to be sure */ meillo@10: } meillo@0: meillo@10: signal(sig, SIG_DFL); meillo@10: raise(sig); meillo@0: } meillo@0: meillo@10: #ifdef ENABLE_IDENT /* so far used for that only */ meillo@10: static gboolean meillo@10: is_in_netlist(gchar * host, GList * netlist) meillo@0: { meillo@10: guint hostip = inet_addr(host); meillo@10: struct in_addr addr; meillo@0: meillo@10: addr.s_addr = hostip; meillo@10: if (addr.s_addr != INADDR_NONE) { meillo@10: GList *node; meillo@10: foreach(netlist, node) { meillo@10: struct in_addr *net = (struct in_addr *) (node->data); meillo@10: if ((addr.s_addr & net->s_addr) == net->s_addr) meillo@10: return TRUE; meillo@10: } meillo@10: } meillo@10: return FALSE; meillo@0: } meillo@0: #endif meillo@0: meillo@10: gchar* meillo@10: get_optarg(char *argv[], gint argc, gint * argp, gint * pos) meillo@0: { meillo@10: if (argv[*argp][*pos]) meillo@10: return &(argv[*argp][*pos]); meillo@10: else { meillo@10: if (*argp + 1 < argc) { meillo@10: if (argv[(*argp) + 1][0] != '-') { meillo@10: (*argp)++; meillo@10: *pos = 0; meillo@10: return &(argv[*argp][*pos]); meillo@10: } meillo@10: } meillo@10: } meillo@10: return NULL; meillo@0: } meillo@0: meillo@10: gchar* meillo@10: get_progname(gchar * arg0) meillo@0: { meillo@10: gchar *p = arg0 + strlen(arg0) - 1; meillo@10: while (p > arg0) { meillo@10: if (*p == '/') meillo@10: return p + 1; meillo@10: p--; meillo@10: } meillo@10: return p; meillo@0: } meillo@0: meillo@10: gboolean meillo@10: write_pidfile(gchar * name) meillo@0: { meillo@10: FILE *fptr; meillo@0: meillo@10: if ((fptr = fopen(name, "wt"))) { meillo@10: fprintf(fptr, "%d\n", getpid()); meillo@10: fclose(fptr); meillo@10: pidfile = strdup(name); meillo@10: return TRUE; meillo@10: } meillo@10: logwrite(LOG_WARNING, "could not write pid file: %s\n", strerror(errno)); meillo@10: return FALSE; meillo@10: } meillo@0: meillo@10: static void meillo@10: mode_daemon(gboolean do_listen, gint queue_interval, char *argv[]) meillo@10: { meillo@10: guint pid; meillo@0: meillo@10: /* daemon */ meillo@10: if (!conf.run_as_user) { meillo@10: if ((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)) { meillo@10: fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: } meillo@0: meillo@74: /* reparent to init only if init is not already the parent */ meillo@74: if (getppid() != 1) { meillo@74: if ((pid = fork()) > 0) { meillo@74: exit(EXIT_SUCCESS); meillo@74: } else if (pid < 0) { meillo@74: logwrite(LOG_ALERT, "could not fork!"); meillo@74: exit(EXIT_FAILURE); meillo@74: } meillo@10: } meillo@0: meillo@10: signal(SIGTERM, sigterm_handler); meillo@10: write_pidfile(PIDFILEDIR "/masqmail.pid"); meillo@0: meillo@10: conf.do_verbose = FALSE; meillo@10: meillo@72: /* closing and reopening the log ensures that it is open afterwards meillo@72: because it is possible that the log is assigned to fd 1 and gets meillo@72: thus closes by fclose(stdout). Similar for the debugfile. meillo@72: */ meillo@72: logclose(); meillo@10: fclose(stdin); meillo@10: fclose(stdout); meillo@10: fclose(stderr); meillo@72: logopen(); meillo@10: meillo@10: listen_port(do_listen ? conf.listen_addresses : NULL, queue_interval, argv); meillo@0: } meillo@0: meillo@0: #ifdef ENABLE_POP3 meillo@10: static void meillo@10: mode_get_daemon(gint get_interval, char *argv[]) meillo@0: { meillo@10: guint pid; meillo@0: meillo@10: /* daemon */ meillo@10: if (!conf.run_as_user) { meillo@10: if ((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)) { meillo@10: fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: } meillo@0: meillo@74: /* reparent to init only if init is not already the parent */ meillo@74: if (getppid() != 1) { meillo@74: if ((pid = fork()) > 0) { meillo@74: exit(EXIT_SUCCESS); meillo@74: } else if (pid < 0) { meillo@74: logwrite(LOG_ALERT, "could not fork!"); meillo@74: exit(EXIT_FAILURE); meillo@74: } meillo@10: } meillo@0: meillo@10: signal(SIGTERM, sigterm_handler); meillo@10: write_pidfile(PIDFILEDIR "/masqmail-get.pid"); meillo@0: meillo@10: conf.do_verbose = FALSE; meillo@0: meillo@72: /* closing and reopening the log ensures that it is open afterwards meillo@72: because it is possible that the log is assigned to fd 1 and gets meillo@72: thus closes by fclose(stdout). Similar for the debugfile. meillo@72: */ meillo@72: logclose(); meillo@10: fclose(stdin); meillo@10: fclose(stdout); meillo@10: fclose(stderr); meillo@72: logopen(); meillo@0: meillo@10: get_daemon(get_interval, argv); meillo@0: } meillo@0: #endif meillo@0: meillo@0: #ifdef ENABLE_SMTP_SERVER meillo@10: static void meillo@10: mode_smtp() meillo@0: { meillo@10: /* accept smtp message on stdin */ meillo@10: /* write responses to stderr. */ meillo@0: meillo@10: struct sockaddr_in saddr; meillo@10: gchar *peername = NULL; meillo@10: int dummy = sizeof(saddr); meillo@0: meillo@10: conf.do_verbose = FALSE; meillo@0: meillo@10: if (!conf.run_as_user) { meillo@10: seteuid(conf.orig_uid); meillo@10: setegid(conf.orig_gid); meillo@10: } meillo@0: meillo@10: DEBUG(5) debugf("accepting smtp message on stdin\n"); meillo@0: meillo@10: if (getpeername(0, (struct sockaddr *) (&saddr), &dummy) == 0) { meillo@10: peername = g_strdup(inet_ntoa(saddr.sin_addr)); meillo@10: } else if (errno != ENOTSOCK) meillo@10: exit(EXIT_FAILURE); meillo@0: meillo@10: smtp_in(stdin, stderr, peername, NULL); meillo@0: } meillo@0: #endif meillo@0: meillo@10: static void meillo@10: mode_accept(address * return_path, gchar * full_sender_name, guint accept_flags, char **addresses, int addr_cnt) meillo@0: { meillo@10: /* accept message on stdin */ meillo@10: accept_error err; meillo@10: message *msg = create_message(); meillo@10: gint i; meillo@0: meillo@83: if (return_path && !is_privileged_user(conf.orig_uid)) { meillo@96: fprintf(stderr, "must be root, %s or in group %s for setting return path.\n", DEF_MAIL_USER, DEF_MAIL_GROUP); meillo@83: exit(EXIT_FAILURE); meillo@10: } meillo@0: meillo@10: if (!conf.run_as_user) { meillo@10: seteuid(conf.orig_uid); meillo@10: setegid(conf.orig_gid); meillo@10: } meillo@0: meillo@10: DEBUG(5) debugf("accepting message on stdin\n"); meillo@0: meillo@10: msg->received_prot = PROT_LOCAL; meillo@10: for (i = 0; i < addr_cnt; i++) { meillo@10: if (addresses[i][0] != '|') meillo@10: msg->rcpt_list = g_list_append(msg->rcpt_list, create_address_qualified(addresses[i], TRUE, conf.host_name)); meillo@10: else { meillo@10: logwrite(LOG_ALERT, "no pipe allowed as recipient address: %s\n", addresses[i]); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: } meillo@0: meillo@10: /* -f option */ meillo@10: msg->return_path = return_path; meillo@0: meillo@10: /* -F option */ meillo@10: msg->full_sender_name = full_sender_name; meillo@0: meillo@10: if ((err = accept_message(stdin, msg, accept_flags)) == AERR_OK) { meillo@10: if (spool_write(msg, TRUE)) { meillo@10: pid_t pid; meillo@10: logwrite(LOG_NOTICE, "%s <= %s with %s\n", msg->uid, addr_string(msg->return_path), prot_names[PROT_LOCAL]); meillo@0: meillo@10: if (!conf.do_queue) { meillo@10: if ((pid = fork()) == 0) { meillo@10: conf.do_verbose = FALSE; meillo@10: fclose(stdin); meillo@10: fclose(stdout); meillo@10: fclose(stderr); meillo@10: if (deliver(msg)) { meillo@10: exit(EXIT_SUCCESS); meillo@10: } else meillo@10: exit(EXIT_FAILURE); meillo@10: } else if (pid < 0) { meillo@10: logwrite(LOG_ALERT, "could not fork for delivery, id = %s", msg->uid); meillo@10: } meillo@10: } meillo@10: } else { meillo@10: fprintf(stderr, "Could not write spool file\n"); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: } else { meillo@10: switch (err) { meillo@10: case AERR_EOF: meillo@10: fprintf(stderr, "unexpected EOF.\n"); meillo@10: exit(EXIT_FAILURE); meillo@10: case AERR_NORCPT: meillo@10: fprintf(stderr, "no recipients.\n"); meillo@10: exit(EXIT_FAILURE); meillo@117: case AERR_SIZE: meillo@117: fprintf(stderr, "max message size exceeded.\n"); meillo@117: exit(EXIT_FAILURE); meillo@10: default: meillo@10: /* should never happen: */ meillo@10: fprintf(stderr, "Unknown error (%d)\r\n", err); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: exit(EXIT_FAILURE); meillo@0: } meillo@0: } meillo@0: meillo@0: int meillo@0: main(int argc, char *argv[]) meillo@0: { meillo@10: /* cmd line flags */ meillo@10: gchar *conf_file = CONF_FILE; meillo@10: gint arg = 1; meillo@10: gboolean do_get = FALSE; meillo@10: gboolean do_get_online = FALSE; meillo@0: meillo@10: gboolean do_listen = FALSE; meillo@10: gboolean do_runq = FALSE; meillo@10: gboolean do_runq_online = FALSE; meillo@0: meillo@10: gboolean do_queue = FALSE; meillo@0: meillo@10: gboolean do_verbose = FALSE; meillo@10: gint debug_level = -1; meillo@0: meillo@10: mta_mode mta_mode = MODE_ACCEPT; meillo@0: meillo@10: gint queue_interval = 0; meillo@10: gint get_interval = 0; meillo@10: gboolean opt_t = FALSE; meillo@10: gboolean opt_i = FALSE; meillo@10: gboolean opt_odb = FALSE; meillo@10: gboolean opt_oem = FALSE; meillo@10: gboolean exit_failure = FALSE; meillo@0: meillo@10: gchar *M_cmd = NULL; meillo@0: meillo@10: gint exit_code = EXIT_SUCCESS; meillo@10: gchar *route_name = NULL; meillo@10: gchar *get_name = NULL; meillo@10: gchar *progname; meillo@10: gchar *f_address = NULL; meillo@10: gchar *full_sender_name = NULL; meillo@10: address *return_path = NULL; /* may be changed by -f option */ meillo@0: meillo@10: progname = get_progname(argv[0]); meillo@0: meillo@10: if (strcmp(progname, "mailq") == 0) { meillo@10: mta_mode = MODE_LIST; meillo@10: } else if (strcmp(progname, "mailrm") == 0) { meillo@10: mta_mode = MODE_MCMD; meillo@10: M_cmd = "rm"; meillo@10: } else if (strcmp(progname, "runq") == 0) { meillo@10: mta_mode = MODE_RUNQUEUE; meillo@10: do_runq = TRUE; meillo@10: } else if (strcmp(progname, "rmail") == 0) { meillo@89: /* the `rmail' alias should probably be removed now meillo@89: that we have the rmail script. But let's keep it meillo@89: for some while for compatibility. 2010-06-19 */ meillo@10: mta_mode = MODE_ACCEPT; meillo@10: opt_i = TRUE; meillo@10: } else if (strcmp(progname, "smtpd") == 0 || strcmp(progname, "in.smtpd") == 0) { meillo@10: mta_mode = MODE_SMTP; meillo@10: } meillo@0: meillo@10: /* parse cmd line */ meillo@10: while (arg < argc) { meillo@10: gint pos = 0; meillo@10: if ((argv[arg][pos] == '-') && (argv[arg][pos + 1] != '-')) { meillo@10: pos++; meillo@10: switch (argv[arg][pos++]) { meillo@10: case 'b': meillo@10: switch (argv[arg][pos++]) { meillo@10: case 'd': meillo@10: do_listen = TRUE; meillo@10: mta_mode = MODE_DAEMON; meillo@10: break; meillo@10: case 'i': meillo@10: /* ignored */ meillo@10: mta_mode = MODE_BI; meillo@10: break; meillo@10: case 's': meillo@10: mta_mode = MODE_SMTP; meillo@10: break; meillo@10: case 'p': meillo@10: mta_mode = MODE_LIST; meillo@10: break; meillo@10: case 'V': meillo@10: mta_mode = MODE_VERSION; meillo@10: break; meillo@10: default: meillo@10: fprintf(stderr, "unrecognized option '%s'\n", argv[arg]); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: break; meillo@10: case 'B': meillo@10: /* we ignore this and throw the argument away */ meillo@10: get_optarg(argv, argc, &arg, &pos); meillo@10: break; meillo@10: case 'C': meillo@10: if (!(conf_file = get_optarg(argv, argc, &arg, &pos))) { meillo@10: fprintf(stderr, "-C requires a filename as argument.\n"); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: break; meillo@10: case 'F': meillo@10: { meillo@10: full_sender_name = get_optarg(argv, argc, &arg, &pos); meillo@10: if (!full_sender_name) { meillo@10: fprintf(stderr, "-F requires a name as an argument\n"); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: } meillo@10: break; meillo@10: case 'd': meillo@10: if (getuid() == 0) { meillo@10: char *lvl = get_optarg(argv, argc, &arg, &pos); meillo@10: if (lvl) meillo@10: debug_level = atoi(lvl); meillo@10: else { meillo@10: fprintf(stderr, "-d requires a number as an argument.\n"); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: } else { meillo@10: fprintf(stderr, "only root may set the debug level.\n"); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: break; meillo@10: case 'f': meillo@10: /* set return path */ meillo@10: { meillo@10: gchar *address; meillo@10: address = get_optarg(argv, argc, &arg, &pos); meillo@10: if (address) { meillo@10: f_address = g_strdup(address); meillo@10: } else { meillo@10: fprintf(stderr, "-f requires an address as an argument\n"); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: } meillo@10: break; meillo@10: case 'g': meillo@10: do_get = TRUE; meillo@10: if (!mta_mode) meillo@10: mta_mode = MODE_NONE; /* to prevent default MODE_ACCEPT */ meillo@10: if (argv[arg][pos] == 'o') { meillo@10: pos++; meillo@10: do_get_online = TRUE; meillo@10: /* can be NULL, then we use online detection method */ meillo@10: route_name = get_optarg(argv, argc, &arg, &pos); meillo@10: meillo@10: if (route_name != NULL) { meillo@10: if (isdigit(route_name[0])) { meillo@10: get_interval = time_interval(route_name, &pos); meillo@10: route_name = get_optarg(argv, argc, &arg, &pos); meillo@10: mta_mode = MODE_GET_DAEMON; meillo@10: do_get = FALSE; meillo@10: } meillo@10: } meillo@10: } else { meillo@10: if ((optarg = get_optarg(argv, argc, &arg, &pos))) { meillo@10: get_name = get_optarg(argv, argc, &arg, &pos); meillo@10: } meillo@10: } meillo@10: break; meillo@10: case 'i': meillo@10: if (argv[arg][pos] == 0) { meillo@10: opt_i = TRUE; meillo@10: exit_failure = FALSE; /* may override -oem */ meillo@10: } else { meillo@10: fprintf(stderr, "unrecognized option '%s'\n", argv[arg]); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: break; meillo@10: case 'M': meillo@10: { meillo@10: mta_mode = MODE_MCMD; meillo@10: M_cmd = g_strdup(&(argv[arg][pos])); meillo@10: } meillo@10: break; meillo@10: case 'o': meillo@10: switch (argv[arg][pos++]) { meillo@10: case 'e': meillo@10: if (argv[arg][pos++] == 'm') /* -oem */ meillo@10: if (!opt_i) meillo@10: exit_failure = TRUE; meillo@10: opt_oem = TRUE; meillo@10: break; meillo@10: case 'd': meillo@10: if (argv[arg][pos] == 'b') /* -odb */ meillo@10: opt_odb = TRUE; meillo@10: else if (argv[arg][pos] == 'q') /* -odq */ meillo@10: do_queue = TRUE; meillo@10: break; meillo@10: case 'i': meillo@10: opt_i = TRUE; meillo@10: exit_failure = FALSE; /* may override -oem */ meillo@10: break; meillo@10: } meillo@10: break; meillo@10: meillo@10: case 'q': meillo@10: { meillo@10: gchar *optarg; meillo@10: meillo@10: do_runq = TRUE; meillo@10: mta_mode = MODE_RUNQUEUE; meillo@10: if (argv[arg][pos] == 'o') { meillo@10: pos++; meillo@10: do_runq = FALSE; meillo@10: do_runq_online = TRUE; meillo@10: /* can be NULL, then we use online detection method */ meillo@10: route_name = get_optarg(argv, argc, &arg, &pos); meillo@10: } else meillo@10: if ((optarg = get_optarg(argv, argc, &arg, &pos))) { meillo@10: mta_mode = MODE_DAEMON; meillo@10: queue_interval = time_interval(optarg, &pos); meillo@10: } meillo@10: } meillo@10: break; meillo@10: case 't': meillo@10: if (argv[arg][pos] == 0) { meillo@10: opt_t = TRUE; meillo@10: } else { meillo@10: fprintf(stderr, "unrecognized option '%s'\n", argv[arg]); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: break; meillo@10: case 'v': meillo@10: do_verbose = TRUE; meillo@10: break; meillo@10: default: meillo@10: fprintf(stderr, "unrecognized option '%s'\n", argv[arg]); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: } else { meillo@10: if (argv[arg][pos + 1] == '-') { meillo@10: if (argv[arg][pos + 2] != '\0') { meillo@10: fprintf(stderr, "unrecognized option '%s'\n", argv[arg]); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: arg++; meillo@10: } meillo@10: break; meillo@10: } meillo@10: arg++; meillo@0: } meillo@0: meillo@10: if (mta_mode == MODE_VERSION) { meillo@12: gchar *with_resolver = ""; meillo@12: gchar *with_smtp_server = ""; meillo@12: gchar *with_pop3 = ""; meillo@12: gchar *with_auth = ""; meillo@12: gchar *with_maildir = ""; meillo@12: gchar *with_ident = ""; meillo@0: meillo@0: #ifdef ENABLE_RESOLVER meillo@10: with_resolver = " +resolver"; meillo@0: #endif meillo@0: #ifdef ENABLE_SMTP_SERVER meillo@10: with_smtp_server = " +smtp-server"; meillo@0: #endif meillo@0: #ifdef ENABLE_POP3 meillo@10: with_pop3 = " +pop3"; meillo@0: #endif meillo@0: #ifdef ENABLE_AUTH meillo@10: with_auth = " +auth"; meillo@0: #endif meillo@0: #ifdef ENABLE_MAILDIR meillo@10: with_maildir = " +maildir"; meillo@0: #endif meillo@0: #ifdef ENABLE_IDENT meillo@10: with_ident = " +ident"; meillo@0: #endif meillo@0: meillo@164: printf("%s %s%s%s%s%s%s%s\n", PACKAGE, VERSION, with_resolver, with_smtp_server, meillo@164: with_pop3, with_auth, with_maildir, with_ident); meillo@0: meillo@10: exit(EXIT_SUCCESS); meillo@10: } meillo@0: meillo@10: /* initialize random generator */ meillo@10: srand(time(NULL)); meillo@10: /* ignore SIGPIPE signal */ meillo@10: signal(SIGPIPE, SIG_IGN); meillo@0: meillo@73: /* close all possibly open file descriptors, except std{in,out,err} */ meillo@10: { meillo@10: int i, max_fd = sysconf(_SC_OPEN_MAX); meillo@0: meillo@10: if (max_fd <= 0) meillo@10: max_fd = 64; meillo@10: for (i = 3; i < max_fd; i++) meillo@10: close(i); meillo@10: } meillo@0: meillo@10: init_conf(); meillo@0: meillo@10: /* if we are not privileged, and the config file was changed we meillo@10: implicetely set the the run_as_user flag and give up all meillo@10: privileges. meillo@0: meillo@10: So it is possible for a user to run his own daemon without meillo@10: breaking security. meillo@10: */ meillo@10: if (strcmp(conf_file, CONF_FILE) != 0) { meillo@10: if (conf.orig_uid != 0) { meillo@10: conf.run_as_user = TRUE; meillo@10: seteuid(conf.orig_uid); meillo@10: setegid(conf.orig_gid); meillo@10: setuid(conf.orig_uid); meillo@10: setgid(conf.orig_gid); meillo@10: } meillo@10: } meillo@0: meillo@155: conf.log_dir = LOG_DIR; meillo@155: logopen(); meillo@155: if (!read_conf(conf_file)) { meillo@155: logwrite(LOG_ALERT, "SHUTTING DOWN due to problems reading config\n"); meillo@155: exit(5); meillo@155: } meillo@155: logclose(); meillo@0: meillo@10: if (do_queue) meillo@10: conf.do_queue = TRUE; meillo@10: if (do_verbose) meillo@10: conf.do_verbose = TRUE; meillo@10: if (debug_level >= 0) /* if >= 0, it was given by argument */ meillo@10: conf.debug_level = debug_level; meillo@0: meillo@46: /* It appears that changing to / ensures that we are never in meillo@46: a directory which we cannot access. This situation could be meillo@46: possible after changing identity. meillo@46: Maybe we should only change to / if we not run as user, to meillo@46: allow relative paths for log files in test setups for meillo@46: instance. meillo@46: */ meillo@10: chdir("/"); meillo@0: meillo@10: if (!conf.run_as_user) { meillo@10: if (setgid(0) != 0) { meillo@10: fprintf(stderr, "could not set gid to 0. Is the setuid bit set? : %s\n", strerror(errno)); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: if (setuid(0) != 0) { meillo@10: fprintf(stderr, "could not gain root privileges. Is the setuid bit set? : %s\n", strerror(errno)); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: } meillo@0: meillo@10: if (!logopen()) { meillo@10: fprintf(stderr, "could not open log file\n"); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@0: meillo@10: DEBUG(1) debugf("masqmail %s starting\n", VERSION); meillo@0: meillo@10: DEBUG(5) { meillo@10: gchar **str = argv; meillo@10: debugf("args: \n"); meillo@10: while (*str) { meillo@10: debugf("%s \n", *str); meillo@10: str++; meillo@10: } meillo@10: } meillo@10: DEBUG(5) debugf("queue_interval = %d\n", queue_interval); meillo@0: meillo@10: if (f_address) { meillo@10: return_path = create_address_qualified(f_address, TRUE, conf.host_name); meillo@10: g_free(f_address); meillo@10: if (!return_path) { meillo@10: fprintf(stderr, "invalid RFC821 address: %s\n", f_address); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: } meillo@10: meillo@10: if (do_get) { meillo@0: #ifdef ENABLE_POP3 meillo@10: if ((mta_mode == MODE_NONE) || (mta_mode == MODE_RUNQUEUE)) { meillo@10: set_identity(conf.orig_uid, "getting mail"); meillo@10: if (do_get_online) { meillo@10: if (route_name != NULL) { meillo@10: conf.online_detect = g_strdup("argument"); meillo@10: set_online_name(route_name); meillo@10: } meillo@10: get_online(); meillo@10: } else { meillo@10: if (get_name) meillo@10: get_from_name(get_name); meillo@10: else meillo@10: get_all(); meillo@10: } meillo@10: } else { meillo@10: logwrite(LOG_ALERT, "get (-g) only allowed alone or together with queue run (-q)\n"); meillo@10: } meillo@10: #else meillo@10: fprintf(stderr, "get (pop) support not compiled in\n"); meillo@10: #endif meillo@10: } meillo@0: meillo@10: switch (mta_mode) { meillo@10: case MODE_DAEMON: meillo@10: mode_daemon(do_listen, queue_interval, argv); meillo@10: break; meillo@10: case MODE_RUNQUEUE: meillo@10: { meillo@10: /* queue runs */ meillo@10: set_identity(conf.orig_uid, "queue run"); meillo@0: meillo@10: if (do_runq) meillo@10: exit_code = queue_run() ? EXIT_SUCCESS : EXIT_FAILURE; meillo@10: meillo@10: if (do_runq_online) { meillo@10: if (route_name != NULL) { meillo@10: conf.online_detect = g_strdup("argument"); meillo@10: set_online_name(route_name); meillo@10: } meillo@10: exit_code = meillo@10: queue_run_online() ? EXIT_SUCCESS : EXIT_FAILURE; meillo@10: } meillo@10: } meillo@10: break; meillo@10: case MODE_GET_DAEMON: meillo@10: #ifdef ENABLE_POP3 meillo@10: if (route_name != NULL) { meillo@10: conf.online_detect = g_strdup("argument"); meillo@10: set_online_name(route_name); meillo@10: } meillo@10: mode_get_daemon(get_interval, argv); meillo@10: #endif meillo@10: break; meillo@10: meillo@10: case MODE_SMTP: meillo@10: #ifdef ENABLE_SMTP_SERVER meillo@10: mode_smtp(); meillo@10: #else meillo@10: fprintf(stderr, "smtp server support not compiled in\n"); meillo@10: #endif meillo@10: break; meillo@10: meillo@10: case MODE_LIST: meillo@10: queue_list(); meillo@10: break; meillo@10: meillo@10: case MODE_BI: meillo@10: exit(EXIT_SUCCESS); meillo@10: break; /* well... */ meillo@10: meillo@10: case MODE_MCMD: meillo@10: if (strcmp(M_cmd, "rm") == 0) { meillo@10: gboolean ok = FALSE; meillo@10: meillo@10: set_euidgid(conf.mail_uid, conf.mail_gid, NULL, NULL); meillo@10: meillo@10: if (is_privileged_user(conf.orig_uid)) { meillo@10: for (; arg < argc; arg++) { meillo@10: if (queue_delete(argv[arg])) meillo@10: ok = TRUE; meillo@10: } meillo@10: } else { meillo@10: struct passwd *pw = getpwuid(conf.orig_uid); meillo@10: if (pw) { meillo@10: for (; arg < argc; arg++) { meillo@10: message *msg = msg_spool_read(argv[arg], FALSE); meillo@10: #ifdef ENABLE_IDENT meillo@10: if (((msg->received_host == NULL) && (msg->received_prot == PROT_LOCAL)) meillo@10: || is_in_netlist(msg->received_host, conf.ident_trusted_nets)) { meillo@10: #else meillo@10: if ((msg->received_host == NULL) && (msg->received_prot == PROT_LOCAL)) { meillo@10: #endif meillo@10: if (msg->ident) { meillo@10: if (strcmp(pw->pw_name, msg->ident) == 0) { meillo@10: if (queue_delete(argv[arg])) meillo@10: ok = TRUE; meillo@10: } else { meillo@10: fprintf(stderr, "you do not own message id %s\n", argv[arg]); meillo@10: } meillo@10: } else meillo@10: fprintf(stderr, "message %s does not have an ident.\n", argv[arg]); meillo@10: } else { meillo@10: fprintf(stderr, "message %s was not received locally or from a trusted network.\n", argv[arg]); meillo@10: } meillo@10: } meillo@10: } else { meillo@10: fprintf(stderr, "could not find a passwd entry for uid %d: %s\n", conf.orig_uid, strerror(errno)); meillo@10: } meillo@10: } meillo@10: exit(ok ? EXIT_SUCCESS : EXIT_FAILURE); meillo@10: } else { meillo@10: fprintf(stderr, "unknown command %s\n", M_cmd); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: break; meillo@10: meillo@10: case MODE_ACCEPT: meillo@10: { meillo@107: guint accept_flags = (opt_t ? ACC_DEL_RCPTS | ACC_RCPT_FROM_HEAD : 0) meillo@110: | (opt_i ? ACC_DOT_IGNORE : ACC_NODOT_RELAX); meillo@10: mode_accept(return_path, full_sender_name, accept_flags, &(argv[arg]), argc - arg); meillo@10: exit(exit_failure ? EXIT_FAILURE : EXIT_SUCCESS); meillo@10: } meillo@10: break; meillo@10: case MODE_NONE: meillo@10: break; meillo@10: default: meillo@10: fprintf(stderr, "unknown mode: %d\n", mta_mode); meillo@10: break; meillo@0: } meillo@0: meillo@10: logclose(); meillo@0: meillo@10: exit(exit_code); meillo@0: }