masqmail

annotate src/masqmail.c @ 411:9b93c0a3bd8c

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