masqmail

annotate src/masqmail.c @ 249:f9da5a7caeda

refactored the cmdline argument processing I replaced the nested switch statements with one single large else-if construct. Instead of char comparision now str(n)cmp(3) is used. Although this is slower it is much more readable and covers corner-cases which were uncovered before (e.g. -bdxxx). As always: Readability and simplicity matter, not performance.
author markus schnalke <meillo@marmaro.de>
date Thu, 04 Nov 2010 11:02:42 -0300
parents 018cfd163f5c
children a41c013c8458
rev   line source
meillo@0 1 /* MasqMail
meillo@0 2 Copyright (C) 1999-2001 Oliver Kurth
meillo@76 3 Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
meillo@0 4
meillo@0 5 This program is free software; you can redistribute it and/or modify
meillo@0 6 it under the terms of the GNU General Public License as published by
meillo@0 7 the Free Software Foundation; either version 2 of the License, or
meillo@0 8 (at your option) any later version.
meillo@0 9
meillo@0 10 This program is distributed in the hope that it will be useful,
meillo@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@0 13 GNU General Public License for more details.
meillo@0 14
meillo@0 15 You should have received a copy of the GNU General Public License
meillo@0 16 along with this program; if not, write to the Free Software
meillo@0 17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 18 */
meillo@0 19
meillo@0 20 #include <stdio.h>
meillo@0 21 #include <errno.h>
meillo@0 22 #include <stdlib.h>
meillo@0 23 #include <string.h>
meillo@0 24 #include <unistd.h>
meillo@0 25 #include <sys/types.h>
meillo@0 26 #include <sys/socket.h>
meillo@0 27 #include <sys/time.h>
meillo@0 28 #include <netinet/in.h>
meillo@0 29 #include <netdb.h>
meillo@0 30 #include <syslog.h>
meillo@0 31 #include <signal.h>
meillo@0 32
meillo@0 33 #include <glib.h>
meillo@0 34
meillo@0 35 #include "masqmail.h"
meillo@0 36
meillo@192 37 /* mutually exclusive modes. Note that there is no 'queue daemon' mode.
meillo@192 38 It, as well as the distinction beween the two (non exclusive) daemon
meillo@192 39 (queue and listen) modes, is handled by flags.*/
meillo@10 40 typedef enum _mta_mode {
meillo@10 41 MODE_ACCEPT = 0, /* accept message on stdin */
meillo@10 42 MODE_DAEMON, /* run as daemon */
meillo@10 43 MODE_RUNQUEUE, /* single queue run, online or offline */
meillo@10 44 MODE_SMTP, /* accept SMTP on stdin */
meillo@10 45 MODE_LIST, /* list queue */
meillo@10 46 MODE_MCMD, /* do queue manipulation */
meillo@10 47 MODE_VERSION, /* show version */
meillo@10 48 MODE_BI, /* fake ;-) */
meillo@10 49 MODE_NONE /* to prevent default MODE_ACCEPT */
meillo@10 50 } mta_mode;
meillo@0 51
meillo@0 52 char *pidfile = NULL;
meillo@0 53 volatile int sigterm_in_progress = 0;
meillo@0 54
meillo@10 55 static void
meillo@10 56 sigterm_handler(int sig)
meillo@0 57 {
meillo@10 58 if (sigterm_in_progress)
meillo@10 59 raise(sig);
meillo@10 60 sigterm_in_progress = 1;
meillo@0 61
meillo@10 62 if (pidfile) {
meillo@10 63 uid_t uid;
meillo@10 64 uid = seteuid(0);
meillo@10 65 if (unlink(pidfile) != 0)
meillo@10 66 logwrite(LOG_WARNING, "could not delete pid file %s: %s\n", pidfile, strerror(errno));
meillo@10 67 seteuid(uid); /* we exit anyway after this, just to be sure */
meillo@10 68 }
meillo@0 69
meillo@10 70 signal(sig, SIG_DFL);
meillo@10 71 raise(sig);
meillo@0 72 }
meillo@0 73
meillo@10 74 #ifdef ENABLE_IDENT /* so far used for that only */
meillo@10 75 static gboolean
meillo@10 76 is_in_netlist(gchar * host, GList * netlist)
meillo@0 77 {
meillo@10 78 guint hostip = inet_addr(host);
meillo@10 79 struct in_addr addr;
meillo@0 80
meillo@10 81 addr.s_addr = hostip;
meillo@10 82 if (addr.s_addr != INADDR_NONE) {
meillo@10 83 GList *node;
meillo@10 84 foreach(netlist, node) {
meillo@10 85 struct in_addr *net = (struct in_addr *) (node->data);
meillo@10 86 if ((addr.s_addr & net->s_addr) == net->s_addr)
meillo@10 87 return TRUE;
meillo@10 88 }
meillo@10 89 }
meillo@10 90 return FALSE;
meillo@0 91 }
meillo@0 92 #endif
meillo@0 93
meillo@249 94 /*
meillo@249 95 argv: the original argv
meillo@249 96 argp: number of arg (may get modified!)
meillo@249 97 cp: pointing to the char after the option
meillo@249 98 e.g. `-d 6' `-d6'
meillo@249 99 ^ ^
meillo@249 100 */
meillo@10 101 gchar*
meillo@249 102 get_optarg(char* argv[], gint* argp, char* cp)
meillo@0 103 {
meillo@249 104 if (*cp) {
meillo@249 105 /* this kind: -xval */
meillo@249 106 return cp;
meillo@249 107 }
meillo@249 108 cp = argv[*argp+1];
meillo@249 109 if (cp && (*cp != '-')) {
meillo@249 110 /* this kind: -x val */
meillo@249 111 (*argp)++;
meillo@249 112 return cp;
meillo@10 113 }
meillo@10 114 return NULL;
meillo@0 115 }
meillo@0 116
meillo@10 117 gchar*
meillo@10 118 get_progname(gchar * arg0)
meillo@0 119 {
meillo@10 120 gchar *p = arg0 + strlen(arg0) - 1;
meillo@10 121 while (p > arg0) {
meillo@10 122 if (*p == '/')
meillo@10 123 return p + 1;
meillo@10 124 p--;
meillo@10 125 }
meillo@10 126 return p;
meillo@0 127 }
meillo@0 128
meillo@10 129 gboolean
meillo@10 130 write_pidfile(gchar * name)
meillo@0 131 {
meillo@10 132 FILE *fptr;
meillo@0 133
meillo@10 134 if ((fptr = fopen(name, "wt"))) {
meillo@10 135 fprintf(fptr, "%d\n", getpid());
meillo@10 136 fclose(fptr);
meillo@10 137 pidfile = strdup(name);
meillo@10 138 return TRUE;
meillo@10 139 }
meillo@10 140 logwrite(LOG_WARNING, "could not write pid file: %s\n", strerror(errno));
meillo@10 141 return FALSE;
meillo@10 142 }
meillo@0 143
meillo@10 144 static void
meillo@10 145 mode_daemon(gboolean do_listen, gint queue_interval, char *argv[])
meillo@10 146 {
meillo@10 147 guint pid;
meillo@0 148
meillo@10 149 /* daemon */
meillo@10 150 if (!conf.run_as_user) {
meillo@10 151 if ((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)) {
meillo@10 152 fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER);
meillo@10 153 exit(EXIT_FAILURE);
meillo@10 154 }
meillo@10 155 }
meillo@0 156
meillo@74 157 /* reparent to init only if init is not already the parent */
meillo@74 158 if (getppid() != 1) {
meillo@74 159 if ((pid = fork()) > 0) {
meillo@74 160 exit(EXIT_SUCCESS);
meillo@74 161 } else if (pid < 0) {
meillo@208 162 logwrite(LOG_ALERT, "could not fork!\n");
meillo@74 163 exit(EXIT_FAILURE);
meillo@74 164 }
meillo@10 165 }
meillo@0 166
meillo@10 167 signal(SIGTERM, sigterm_handler);
meillo@10 168 write_pidfile(PIDFILEDIR "/masqmail.pid");
meillo@0 169
meillo@10 170 conf.do_verbose = FALSE;
meillo@10 171
meillo@72 172 /* closing and reopening the log ensures that it is open afterwards
meillo@72 173 because it is possible that the log is assigned to fd 1 and gets
meillo@72 174 thus closes by fclose(stdout). Similar for the debugfile.
meillo@72 175 */
meillo@72 176 logclose();
meillo@10 177 fclose(stdin);
meillo@10 178 fclose(stdout);
meillo@10 179 fclose(stderr);
meillo@72 180 logopen();
meillo@10 181
meillo@208 182 logwrite(LOG_NOTICE, "%s %s daemon starting\n", PACKAGE, VERSION);
meillo@10 183 listen_port(do_listen ? conf.listen_addresses : NULL, queue_interval, argv);
meillo@0 184 }
meillo@0 185
meillo@10 186 static void
meillo@10 187 mode_smtp()
meillo@0 188 {
meillo@10 189 /* accept smtp message on stdin */
meillo@10 190 /* write responses to stderr. */
meillo@0 191
meillo@10 192 struct sockaddr_in saddr;
meillo@10 193 gchar *peername = NULL;
meillo@10 194 int dummy = sizeof(saddr);
meillo@0 195
meillo@10 196 conf.do_verbose = FALSE;
meillo@0 197
meillo@10 198 if (!conf.run_as_user) {
meillo@10 199 seteuid(conf.orig_uid);
meillo@10 200 setegid(conf.orig_gid);
meillo@10 201 }
meillo@0 202
meillo@10 203 DEBUG(5) debugf("accepting smtp message on stdin\n");
meillo@0 204
meillo@10 205 if (getpeername(0, (struct sockaddr *) (&saddr), &dummy) == 0) {
meillo@10 206 peername = g_strdup(inet_ntoa(saddr.sin_addr));
meillo@10 207 } else if (errno != ENOTSOCK)
meillo@10 208 exit(EXIT_FAILURE);
meillo@0 209
meillo@10 210 smtp_in(stdin, stderr, peername, NULL);
meillo@0 211 }
meillo@0 212
meillo@10 213 static void
meillo@10 214 mode_accept(address * return_path, gchar * full_sender_name, guint accept_flags, char **addresses, int addr_cnt)
meillo@0 215 {
meillo@10 216 /* accept message on stdin */
meillo@10 217 accept_error err;
meillo@10 218 message *msg = create_message();
meillo@10 219 gint i;
meillo@0 220
meillo@83 221 if (return_path && !is_privileged_user(conf.orig_uid)) {
meillo@96 222 fprintf(stderr, "must be root, %s or in group %s for setting return path.\n", DEF_MAIL_USER, DEF_MAIL_GROUP);
meillo@83 223 exit(EXIT_FAILURE);
meillo@10 224 }
meillo@0 225
meillo@10 226 if (!conf.run_as_user) {
meillo@10 227 seteuid(conf.orig_uid);
meillo@10 228 setegid(conf.orig_gid);
meillo@10 229 }
meillo@0 230
meillo@10 231 DEBUG(5) debugf("accepting message on stdin\n");
meillo@0 232
meillo@10 233 msg->received_prot = PROT_LOCAL;
meillo@10 234 for (i = 0; i < addr_cnt; i++) {
meillo@10 235 if (addresses[i][0] != '|')
meillo@10 236 msg->rcpt_list = g_list_append(msg->rcpt_list, create_address_qualified(addresses[i], TRUE, conf.host_name));
meillo@10 237 else {
meillo@10 238 logwrite(LOG_ALERT, "no pipe allowed as recipient address: %s\n", addresses[i]);
meillo@10 239 exit(EXIT_FAILURE);
meillo@10 240 }
meillo@10 241 }
meillo@0 242
meillo@10 243 /* -f option */
meillo@10 244 msg->return_path = return_path;
meillo@0 245
meillo@10 246 /* -F option */
meillo@10 247 msg->full_sender_name = full_sender_name;
meillo@0 248
meillo@10 249 if ((err = accept_message(stdin, msg, accept_flags)) == AERR_OK) {
meillo@10 250 if (spool_write(msg, TRUE)) {
meillo@10 251 pid_t pid;
meillo@10 252 logwrite(LOG_NOTICE, "%s <= %s with %s\n", msg->uid, addr_string(msg->return_path), prot_names[PROT_LOCAL]);
meillo@0 253
meillo@10 254 if (!conf.do_queue) {
meillo@10 255 if ((pid = fork()) == 0) {
meillo@10 256 conf.do_verbose = FALSE;
meillo@10 257 fclose(stdin);
meillo@10 258 fclose(stdout);
meillo@10 259 fclose(stderr);
meillo@10 260 if (deliver(msg)) {
meillo@10 261 exit(EXIT_SUCCESS);
meillo@10 262 } else
meillo@10 263 exit(EXIT_FAILURE);
meillo@10 264 } else if (pid < 0) {
meillo@208 265 logwrite(LOG_ALERT, "could not fork for delivery, id = %s\n", msg->uid);
meillo@10 266 }
meillo@10 267 }
meillo@10 268 } else {
meillo@10 269 fprintf(stderr, "Could not write spool file\n");
meillo@10 270 exit(EXIT_FAILURE);
meillo@10 271 }
meillo@10 272 } else {
meillo@10 273 switch (err) {
meillo@10 274 case AERR_EOF:
meillo@10 275 fprintf(stderr, "unexpected EOF.\n");
meillo@10 276 exit(EXIT_FAILURE);
meillo@10 277 case AERR_NORCPT:
meillo@10 278 fprintf(stderr, "no recipients.\n");
meillo@10 279 exit(EXIT_FAILURE);
meillo@117 280 case AERR_SIZE:
meillo@117 281 fprintf(stderr, "max message size exceeded.\n");
meillo@117 282 exit(EXIT_FAILURE);
meillo@10 283 default:
meillo@10 284 /* should never happen: */
meillo@10 285 fprintf(stderr, "Unknown error (%d)\r\n", err);
meillo@10 286 exit(EXIT_FAILURE);
meillo@10 287 }
meillo@10 288 exit(EXIT_FAILURE);
meillo@0 289 }
meillo@0 290 }
meillo@0 291
meillo@0 292 int
meillo@0 293 main(int argc, char *argv[])
meillo@0 294 {
meillo@10 295 /* cmd line flags */
meillo@10 296 gchar *conf_file = CONF_FILE;
meillo@249 297 char* opt;
meillo@249 298 gint arg;
meillo@0 299
meillo@10 300 gboolean do_listen = FALSE;
meillo@10 301 gboolean do_runq = FALSE;
meillo@10 302 gboolean do_runq_online = FALSE;
meillo@0 303
meillo@10 304 gboolean do_queue = FALSE;
meillo@0 305
meillo@10 306 gboolean do_verbose = FALSE;
meillo@10 307 gint debug_level = -1;
meillo@0 308
meillo@10 309 mta_mode mta_mode = MODE_ACCEPT;
meillo@0 310
meillo@10 311 gint queue_interval = 0;
meillo@10 312 gboolean opt_t = FALSE;
meillo@10 313 gboolean opt_i = FALSE;
meillo@10 314 gboolean exit_failure = FALSE;
meillo@0 315
meillo@10 316 gchar *M_cmd = NULL;
meillo@0 317
meillo@10 318 gint exit_code = EXIT_SUCCESS;
meillo@10 319 gchar *route_name = NULL;
meillo@10 320 gchar *progname;
meillo@10 321 gchar *f_address = NULL;
meillo@10 322 gchar *full_sender_name = NULL;
meillo@10 323 address *return_path = NULL; /* may be changed by -f option */
meillo@0 324
meillo@10 325 progname = get_progname(argv[0]);
meillo@0 326
meillo@10 327 if (strcmp(progname, "mailq") == 0) {
meillo@10 328 mta_mode = MODE_LIST;
meillo@10 329 } else if (strcmp(progname, "mailrm") == 0) {
meillo@10 330 mta_mode = MODE_MCMD;
meillo@10 331 M_cmd = "rm";
meillo@10 332 } else if (strcmp(progname, "runq") == 0) {
meillo@10 333 mta_mode = MODE_RUNQUEUE;
meillo@10 334 do_runq = TRUE;
meillo@10 335 } else if (strcmp(progname, "rmail") == 0) {
meillo@89 336 /* the `rmail' alias should probably be removed now
meillo@89 337 that we have the rmail script. But let's keep it
meillo@89 338 for some while for compatibility. 2010-06-19 */
meillo@10 339 mta_mode = MODE_ACCEPT;
meillo@10 340 opt_i = TRUE;
meillo@10 341 } else if (strcmp(progname, "smtpd") == 0 || strcmp(progname, "in.smtpd") == 0) {
meillo@10 342 mta_mode = MODE_SMTP;
meillo@10 343 }
meillo@0 344
meillo@10 345 /* parse cmd line */
meillo@249 346 for (arg=1; arg<argc && argv[arg][0]=='-'; arg++) {
meillo@249 347 opt = argv[arg] + 1;
meillo@10 348
meillo@249 349 if (strcmp(opt, "-") == 0) {
meillo@249 350 /* everything after `--' are address arguments */
meillo@249 351 arg++;
meillo@249 352 break;
meillo@10 353
meillo@249 354 } else if (strcmp(opt, "bd") == 0) {
meillo@249 355 do_listen = TRUE;
meillo@249 356 mta_mode = MODE_DAEMON;
meillo@249 357
meillo@249 358 } else if (strcmp(opt, "bi") == 0) {
meillo@249 359 /* ignored */
meillo@249 360 mta_mode = MODE_BI;
meillo@249 361
meillo@249 362 } else if (strcmp(opt, "bs") == 0) {
meillo@249 363 mta_mode = MODE_SMTP;
meillo@249 364
meillo@249 365 } else if (strcmp(opt, "bp") == 0) {
meillo@249 366 mta_mode = MODE_LIST;
meillo@249 367
meillo@249 368 } else if (strcmp(opt, "bV") == 0) {
meillo@249 369 mta_mode = MODE_VERSION;
meillo@249 370
meillo@249 371 } else if (strncmp(opt, "B", 1) == 0) {
meillo@249 372 /* we ignore this and throw the argument away */
meillo@249 373 get_optarg(argv, &arg, opt+1);
meillo@249 374
meillo@249 375 } else if (strncmp(opt, "C", 1) == 0) {
meillo@249 376 if (!(conf_file = get_optarg(argv, &arg, opt+1))) {
meillo@249 377 fprintf(stderr, "-C requires a filename as argument.\n");
meillo@10 378 exit(EXIT_FAILURE);
meillo@10 379 }
meillo@249 380
meillo@249 381 } else if (strncmp(opt, "d", 1) == 0) {
meillo@249 382 if (getuid() != 0) {
meillo@249 383 fprintf(stderr, "only root may set the debug level.\n");
meillo@249 384 exit(EXIT_FAILURE);
meillo@249 385 }
meillo@249 386 char *lvl = get_optarg(argv, &arg, opt+1);
meillo@249 387 if (!lvl) {
meillo@249 388 fprintf(stderr, "-d requires a number argument.\n");
meillo@249 389 exit(EXIT_FAILURE);
meillo@249 390 }
meillo@249 391 debug_level = atoi(lvl);
meillo@249 392
meillo@249 393 } else if (strncmp(opt, "f", 1) == 0) {
meillo@249 394 /* set return path */
meillo@249 395 gchar *address = get_optarg(argv, &arg, opt+1);
meillo@249 396 if (!address) {
meillo@249 397 fprintf(stderr, "-f requires an address argument\n");
meillo@249 398 exit(EXIT_FAILURE);
meillo@249 399 }
meillo@249 400 f_address = g_strdup(address);
meillo@249 401
meillo@249 402 } else if (strncmp(opt, "F", 1) == 0) {
meillo@249 403 full_sender_name = get_optarg(argv, &arg, opt+1);
meillo@249 404 if (!full_sender_name) {
meillo@249 405 fprintf(stderr, "-F requires a name argument\n");
meillo@249 406 exit(EXIT_FAILURE);
meillo@249 407 }
meillo@249 408
meillo@249 409 } else if (strcmp(opt, "i") == 0) {
meillo@249 410 opt_i = TRUE;
meillo@249 411 exit_failure = FALSE; /* may override -oem */
meillo@249 412
meillo@249 413 } else if (strcmp(opt, "m") == 0) {
meillo@249 414 /* ignore -m (me too) switch (see man page) */
meillo@249 415
meillo@249 416 } else if (strcmp(opt, "Mrm") == 0) {
meillo@249 417 mta_mode = MODE_MCMD;
meillo@249 418 M_cmd = "rm";
meillo@249 419
meillo@249 420 } else if (strcmp(opt, "odq") == 0) {
meillo@249 421 do_queue = TRUE;
meillo@249 422
meillo@249 423 } else if (strcmp(opt, "oem") == 0) {
meillo@249 424 if (!opt_i) {
meillo@249 425 /* TODO: Why is this related to -i in any way? */
meillo@249 426 exit_failure = TRUE;
meillo@249 427 }
meillo@249 428
meillo@249 429 } else if (strcmp(opt, "oi") == 0) {
meillo@249 430 exit_failure = FALSE; /* may override -oem */
meillo@249 431
meillo@249 432 } else if (strncmp(opt, "o", 1) == 0) {
meillo@249 433 /* ignore all other -oXXX options */
meillo@249 434
meillo@249 435 } else if (strncmp(opt, "qo", 2) == 0) {
meillo@249 436 mta_mode = MODE_RUNQUEUE;
meillo@249 437 do_runq = FALSE;
meillo@249 438 do_runq_online = TRUE;
meillo@249 439 /* can be NULL, then we use online detection method */
meillo@249 440 route_name = get_optarg(argv, &arg, opt+2);
meillo@249 441
meillo@249 442 } else if (strncmp(opt, "q", 1) == 0) {
meillo@249 443 /* must be after the `qo' check */
meillo@249 444 gchar *optarg;
meillo@249 445 int dummy;
meillo@249 446
meillo@249 447 do_runq = TRUE;
meillo@249 448 mta_mode = MODE_RUNQUEUE;
meillo@249 449 if ((optarg = get_optarg(argv, &arg, opt+1))) {
meillo@249 450 mta_mode = MODE_DAEMON;
meillo@249 451 queue_interval = time_interval(optarg, &dummy);
meillo@249 452 }
meillo@249 453
meillo@249 454 } else if (strcmp(opt, "t") == 0) {
meillo@249 455 opt_t = TRUE;
meillo@249 456
meillo@249 457 } else if (strcmp(opt, "v") == 0) {
meillo@249 458 do_verbose = TRUE;
meillo@249 459
meillo@10 460 } else {
meillo@249 461 fprintf(stderr, "unrecognized option `-%s'\n", opt);
meillo@249 462 exit(EXIT_FAILURE);
meillo@10 463 }
meillo@0 464 }
meillo@0 465
meillo@10 466 if (mta_mode == MODE_VERSION) {
meillo@12 467 gchar *with_resolver = "";
meillo@12 468 gchar *with_auth = "";
meillo@12 469 gchar *with_ident = "";
meillo@0 470
meillo@0 471 #ifdef ENABLE_RESOLVER
meillo@10 472 with_resolver = " +resolver";
meillo@0 473 #endif
meillo@0 474 #ifdef ENABLE_AUTH
meillo@10 475 with_auth = " +auth";
meillo@0 476 #endif
meillo@0 477 #ifdef ENABLE_IDENT
meillo@10 478 with_ident = " +ident";
meillo@0 479 #endif
meillo@0 480
meillo@205 481 printf("%s %s%s%s%s\n", PACKAGE, VERSION, with_resolver, with_auth, with_ident);
meillo@0 482
meillo@10 483 exit(EXIT_SUCCESS);
meillo@10 484 }
meillo@0 485
meillo@10 486 /* initialize random generator */
meillo@10 487 srand(time(NULL));
meillo@10 488 /* ignore SIGPIPE signal */
meillo@10 489 signal(SIGPIPE, SIG_IGN);
meillo@0 490
meillo@73 491 /* close all possibly open file descriptors, except std{in,out,err} */
meillo@10 492 {
meillo@10 493 int i, max_fd = sysconf(_SC_OPEN_MAX);
meillo@0 494
meillo@10 495 if (max_fd <= 0)
meillo@10 496 max_fd = 64;
meillo@10 497 for (i = 3; i < max_fd; i++)
meillo@10 498 close(i);
meillo@10 499 }
meillo@0 500
meillo@10 501 init_conf();
meillo@0 502
meillo@10 503 /* if we are not privileged, and the config file was changed we
meillo@10 504 implicetely set the the run_as_user flag and give up all
meillo@10 505 privileges.
meillo@0 506
meillo@10 507 So it is possible for a user to run his own daemon without
meillo@10 508 breaking security.
meillo@10 509 */
meillo@10 510 if (strcmp(conf_file, CONF_FILE) != 0) {
meillo@10 511 if (conf.orig_uid != 0) {
meillo@10 512 conf.run_as_user = TRUE;
meillo@10 513 seteuid(conf.orig_uid);
meillo@10 514 setegid(conf.orig_gid);
meillo@10 515 setuid(conf.orig_uid);
meillo@10 516 setgid(conf.orig_gid);
meillo@10 517 }
meillo@10 518 }
meillo@0 519
meillo@155 520 conf.log_dir = LOG_DIR;
meillo@155 521 logopen();
meillo@155 522 if (!read_conf(conf_file)) {
meillo@155 523 logwrite(LOG_ALERT, "SHUTTING DOWN due to problems reading config\n");
meillo@155 524 exit(5);
meillo@155 525 }
meillo@155 526 logclose();
meillo@0 527
meillo@10 528 if (do_queue)
meillo@10 529 conf.do_queue = TRUE;
meillo@10 530 if (do_verbose)
meillo@10 531 conf.do_verbose = TRUE;
meillo@10 532 if (debug_level >= 0) /* if >= 0, it was given by argument */
meillo@10 533 conf.debug_level = debug_level;
meillo@0 534
meillo@46 535 /* It appears that changing to / ensures that we are never in
meillo@46 536 a directory which we cannot access. This situation could be
meillo@46 537 possible after changing identity.
meillo@46 538 Maybe we should only change to / if we not run as user, to
meillo@46 539 allow relative paths for log files in test setups for
meillo@46 540 instance.
meillo@46 541 */
meillo@10 542 chdir("/");
meillo@0 543
meillo@10 544 if (!conf.run_as_user) {
meillo@10 545 if (setgid(0) != 0) {
meillo@10 546 fprintf(stderr, "could not set gid to 0. Is the setuid bit set? : %s\n", strerror(errno));
meillo@10 547 exit(EXIT_FAILURE);
meillo@10 548 }
meillo@10 549 if (setuid(0) != 0) {
meillo@10 550 fprintf(stderr, "could not gain root privileges. Is the setuid bit set? : %s\n", strerror(errno));
meillo@10 551 exit(EXIT_FAILURE);
meillo@10 552 }
meillo@10 553 }
meillo@0 554
meillo@10 555 if (!logopen()) {
meillo@10 556 fprintf(stderr, "could not open log file\n");
meillo@10 557 exit(EXIT_FAILURE);
meillo@10 558 }
meillo@0 559
meillo@10 560 DEBUG(1) debugf("masqmail %s starting\n", VERSION);
meillo@0 561
meillo@10 562 DEBUG(5) {
meillo@10 563 gchar **str = argv;
meillo@10 564 debugf("args: \n");
meillo@10 565 while (*str) {
meillo@10 566 debugf("%s \n", *str);
meillo@10 567 str++;
meillo@10 568 }
meillo@10 569 }
meillo@10 570 DEBUG(5) debugf("queue_interval = %d\n", queue_interval);
meillo@0 571
meillo@10 572 if (f_address) {
meillo@10 573 return_path = create_address_qualified(f_address, TRUE, conf.host_name);
meillo@10 574 g_free(f_address);
meillo@10 575 if (!return_path) {
meillo@10 576 fprintf(stderr, "invalid RFC821 address: %s\n", f_address);
meillo@10 577 exit(EXIT_FAILURE);
meillo@10 578 }
meillo@10 579 }
meillo@10 580
meillo@10 581 switch (mta_mode) {
meillo@10 582 case MODE_DAEMON:
meillo@10 583 mode_daemon(do_listen, queue_interval, argv);
meillo@10 584 break;
meillo@10 585 case MODE_RUNQUEUE:
meillo@10 586 {
meillo@10 587 /* queue runs */
meillo@10 588 set_identity(conf.orig_uid, "queue run");
meillo@0 589
meillo@10 590 if (do_runq)
meillo@10 591 exit_code = queue_run() ? EXIT_SUCCESS : EXIT_FAILURE;
meillo@10 592
meillo@10 593 if (do_runq_online) {
meillo@10 594 if (route_name != NULL) {
meillo@10 595 conf.online_detect = g_strdup("argument");
meillo@10 596 set_online_name(route_name);
meillo@10 597 }
meillo@10 598 exit_code =
meillo@10 599 queue_run_online() ? EXIT_SUCCESS : EXIT_FAILURE;
meillo@10 600 }
meillo@10 601 }
meillo@10 602 break;
meillo@10 603
meillo@10 604 case MODE_SMTP:
meillo@10 605 mode_smtp();
meillo@10 606 break;
meillo@10 607
meillo@10 608 case MODE_LIST:
meillo@10 609 queue_list();
meillo@10 610 break;
meillo@10 611
meillo@10 612 case MODE_BI:
meillo@10 613 exit(EXIT_SUCCESS);
meillo@10 614 break; /* well... */
meillo@10 615
meillo@10 616 case MODE_MCMD:
meillo@10 617 if (strcmp(M_cmd, "rm") == 0) {
meillo@10 618 gboolean ok = FALSE;
meillo@10 619
meillo@10 620 set_euidgid(conf.mail_uid, conf.mail_gid, NULL, NULL);
meillo@10 621
meillo@10 622 if (is_privileged_user(conf.orig_uid)) {
meillo@10 623 for (; arg < argc; arg++) {
meillo@10 624 if (queue_delete(argv[arg]))
meillo@10 625 ok = TRUE;
meillo@10 626 }
meillo@10 627 } else {
meillo@10 628 struct passwd *pw = getpwuid(conf.orig_uid);
meillo@10 629 if (pw) {
meillo@10 630 for (; arg < argc; arg++) {
meillo@10 631 message *msg = msg_spool_read(argv[arg], FALSE);
meillo@10 632 #ifdef ENABLE_IDENT
meillo@10 633 if (((msg->received_host == NULL) && (msg->received_prot == PROT_LOCAL))
meillo@248 634 || is_in_netlist(msg->received_host, conf.ident_trusted_nets))
meillo@10 635 #else
meillo@248 636 if ((msg->received_host == NULL) && (msg->received_prot == PROT_LOCAL))
meillo@10 637 #endif
meillo@248 638 {
meillo@10 639 if (msg->ident) {
meillo@10 640 if (strcmp(pw->pw_name, msg->ident) == 0) {
meillo@10 641 if (queue_delete(argv[arg]))
meillo@10 642 ok = TRUE;
meillo@10 643 } else {
meillo@10 644 fprintf(stderr, "you do not own message id %s\n", argv[arg]);
meillo@10 645 }
meillo@10 646 } else
meillo@10 647 fprintf(stderr, "message %s does not have an ident.\n", argv[arg]);
meillo@10 648 } else {
meillo@10 649 fprintf(stderr, "message %s was not received locally or from a trusted network.\n", argv[arg]);
meillo@10 650 }
meillo@10 651 }
meillo@10 652 } else {
meillo@10 653 fprintf(stderr, "could not find a passwd entry for uid %d: %s\n", conf.orig_uid, strerror(errno));
meillo@10 654 }
meillo@10 655 }
meillo@10 656 exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
meillo@10 657 } else {
meillo@10 658 fprintf(stderr, "unknown command %s\n", M_cmd);
meillo@10 659 exit(EXIT_FAILURE);
meillo@10 660 }
meillo@10 661 break;
meillo@10 662
meillo@10 663 case MODE_ACCEPT:
meillo@10 664 {
meillo@107 665 guint accept_flags = (opt_t ? ACC_DEL_RCPTS | ACC_RCPT_FROM_HEAD : 0)
meillo@110 666 | (opt_i ? ACC_DOT_IGNORE : ACC_NODOT_RELAX);
meillo@10 667 mode_accept(return_path, full_sender_name, accept_flags, &(argv[arg]), argc - arg);
meillo@10 668 exit(exit_failure ? EXIT_FAILURE : EXIT_SUCCESS);
meillo@10 669 }
meillo@10 670 break;
meillo@10 671 case MODE_NONE:
meillo@10 672 break;
meillo@10 673 default:
meillo@10 674 fprintf(stderr, "unknown mode: %d\n", mta_mode);
meillo@10 675 break;
meillo@0 676 }
meillo@0 677
meillo@10 678 logclose();
meillo@0 679
meillo@10 680 exit(exit_code);
meillo@0 681 }