masqmail

annotate src/masqmail.c @ 426:d48d3622b64f

Updated AUTHORS and THANKS files.
author markus schnalke <meillo@marmaro.de>
date Wed, 30 May 2012 11:49:40 +0200
parents f37384470855
children
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@421 107 /*
meillo@421 108 ** Create any missing directory in pathname `dir'. (Like `mkdir -p'.)
meillo@421 109 ** The code is taken from nmh.
meillo@421 110 */
meillo@421 111 gboolean
meillo@421 112 makedir_rec(char *dir, int perms)
meillo@421 113 {
meillo@421 114 char path[PATH_MAX];
meillo@421 115 char *cp, *c;
meillo@421 116 int had_an_error = 0;
meillo@425 117 mode_t savedmask;
meillo@421 118
meillo@421 119 c = strncpy(path, dir, sizeof(path));
meillo@421 120
meillo@425 121 savedmask = umask(0);
meillo@425 122
meillo@421 123 while (!had_an_error && (c = strchr(c+1, '/'))) {
meillo@421 124 *c = '\0';
meillo@421 125 /* Create an outer directory. */
meillo@421 126 if (mkdir(path, perms) == -1 && errno != EEXIST) {
meillo@421 127 fprintf(stderr, "unable to create `%s': %s\n",
meillo@421 128 path, strerror(errno));
meillo@421 129 had_an_error = 1;
meillo@421 130 }
meillo@421 131 *c = '/';
meillo@425 132 }
meillo@421 133
meillo@421 134 /*
meillo@421 135 ** Create the innermost nested subdirectory of the
meillo@421 136 ** path we're being asked to create.
meillo@421 137 */
meillo@421 138 if (!had_an_error && mkdir(dir, perms)==-1 && errno != EEXIST) {
meillo@421 139 fprintf(stderr, "unable to create `%s': %s\n",
meillo@421 140 dir, strerror(errno));
meillo@421 141 had_an_error = 1;
meillo@425 142 }
meillo@425 143 umask(savedmask);
meillo@421 144
meillo@421 145 return (had_an_error) ? 0 : 1;
meillo@421 146 }
meillo@421 147
meillo@10 148 gboolean
meillo@366 149 write_pidfile(gchar *name)
meillo@0 150 {
meillo@10 151 FILE *fptr;
meillo@0 152
meillo@10 153 if ((fptr = fopen(name, "wt"))) {
meillo@10 154 fprintf(fptr, "%d\n", getpid());
meillo@10 155 fclose(fptr);
meillo@10 156 pidfile = strdup(name);
meillo@10 157 return TRUE;
meillo@10 158 }
meillo@409 159 logwrite(LOG_WARNING, "could not write pid file: %s\n",
meillo@409 160 strerror(errno));
meillo@10 161 return FALSE;
meillo@10 162 }
meillo@0 163
meillo@264 164 /* on -bd or if -q has an argument */
meillo@10 165 static void
meillo@10 166 mode_daemon(gboolean do_listen, gint queue_interval, char *argv[])
meillo@10 167 {
meillo@10 168 guint pid;
meillo@0 169
meillo@10 170 /* daemon */
meillo@10 171 if (!conf.run_as_user) {
meillo@10 172 if ((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)) {
meillo@409 173 fprintf(stderr, "must be root or %s for daemon.\n",
meillo@409 174 DEF_MAIL_USER);
meillo@262 175 exit(1);
meillo@10 176 }
meillo@10 177 }
meillo@0 178
meillo@74 179 /* reparent to init only if init is not already the parent */
meillo@74 180 if (getppid() != 1) {
meillo@74 181 if ((pid = fork()) > 0) {
meillo@262 182 exit(0);
meillo@74 183 } else if (pid < 0) {
meillo@208 184 logwrite(LOG_ALERT, "could not fork!\n");
meillo@262 185 exit(1);
meillo@74 186 }
meillo@10 187 }
meillo@0 188
meillo@10 189 signal(SIGTERM, sigterm_handler);
meillo@421 190 makedir_rec(PID_DIR, 0755);
meillo@419 191 write_pidfile(PID_DIR "/masqmail.pid");
meillo@0 192
meillo@10 193 conf.do_verbose = FALSE;
meillo@10 194
meillo@367 195 /*
meillo@367 196 ** closing and reopening the log ensures that it is open afterwards
meillo@367 197 ** because it is possible that the log is assigned to fd 1 and gets
meillo@367 198 ** thus closes by fclose(stdout). Similar for the debugfile.
meillo@72 199 */
meillo@72 200 logclose();
meillo@10 201 fclose(stdin);
meillo@10 202 fclose(stdout);
meillo@10 203 fclose(stderr);
meillo@72 204 logopen();
meillo@10 205
meillo@208 206 logwrite(LOG_NOTICE, "%s %s daemon starting\n", PACKAGE, VERSION);
meillo@409 207 listen_port(do_listen ? conf.listen_addresses : NULL,
meillo@409 208 queue_interval, argv);
meillo@0 209 }
meillo@0 210
meillo@264 211 /* -bs or called as smtpd or in.smtpd */
meillo@10 212 static void
meillo@10 213 mode_smtp()
meillo@0 214 {
meillo@10 215 /* accept smtp message on stdin */
meillo@10 216 /* write responses to stderr. */
meillo@0 217
meillo@10 218 struct sockaddr_in saddr;
meillo@10 219 gchar *peername = NULL;
meillo@10 220 int dummy = sizeof(saddr);
meillo@0 221
meillo@10 222 conf.do_verbose = FALSE;
meillo@0 223
meillo@10 224 if (!conf.run_as_user) {
meillo@331 225 set_euidgid(conf.orig_uid, conf.orig_gid, NULL, NULL);
meillo@10 226 }
meillo@0 227
meillo@10 228 DEBUG(5) debugf("accepting smtp message on stdin\n");
meillo@0 229
meillo@10 230 if (getpeername(0, (struct sockaddr *) (&saddr), &dummy) == 0) {
meillo@10 231 peername = g_strdup(inet_ntoa(saddr.sin_addr));
meillo@10 232 } else if (errno != ENOTSOCK)
meillo@262 233 exit(1);
meillo@0 234
meillo@10 235 smtp_in(stdin, stderr, peername, NULL);
meillo@0 236 }
meillo@0 237
meillo@264 238 /* default mode if address args or -t is specified, or called as rmail */
meillo@10 239 static void
meillo@367 240 mode_accept(address *return_path, gchar *full_sender_name, guint accept_flags,
meillo@367 241 char **addresses, int addr_cnt)
meillo@0 242 {
meillo@10 243 /* accept message on stdin */
meillo@10 244 accept_error err;
meillo@10 245 message *msg = create_message();
meillo@10 246 gint i;
meillo@266 247 pid_t pid;
meillo@0 248
meillo@83 249 if (return_path && !is_privileged_user(conf.orig_uid)) {
meillo@409 250 fprintf(stderr, "must be root, %s or in group %s for "
meillo@409 251 "setting return path.\n",
meillo@409 252 DEF_MAIL_USER, DEF_MAIL_GROUP);
meillo@262 253 exit(1);
meillo@10 254 }
meillo@0 255
meillo@10 256 if (!conf.run_as_user) {
meillo@331 257 set_euidgid(conf.orig_uid, conf.orig_gid, NULL, NULL);
meillo@10 258 }
meillo@0 259
meillo@10 260 DEBUG(5) debugf("accepting message on stdin\n");
meillo@0 261
meillo@10 262 msg->received_prot = PROT_LOCAL;
meillo@276 263
meillo@276 264 /* warn if -t option and cmdline addr args */
meillo@276 265 if (addr_cnt && (accept_flags & ACC_RCPT_FROM_HEAD)) {
meillo@409 266 logwrite(LOG_ALERT, "command line address arguments are "
meillo@409 267 "now *added* to the mail header\\\n");
meillo@409 268 logwrite(LOG_ALERT, " recipient addresses (instead of "
meillo@409 269 "substracted) when -t is given.\\\n");
meillo@276 270 logwrite(LOG_ALERT, " this changed with version 0.3.1\n");
meillo@276 271 }
meillo@276 272
meillo@10 273 for (i = 0; i < addr_cnt; i++) {
meillo@266 274 if (addresses[i][0] == '|') {
meillo@409 275 logwrite(LOG_ALERT, "no pipe allowed as recipient "
meillo@409 276 "address: %s\n", addresses[i]);
meillo@276 277 /* should we better ignore this one addr? */
meillo@262 278 exit(1);
meillo@10 279 }
meillo@409 280 msg->rcpt_list = g_list_append(msg->rcpt_list,
meillo@409 281 create_address_qualified(addresses[i],
meillo@409 282 TRUE, conf.host_name));
meillo@10 283 }
meillo@0 284
meillo@10 285 /* -f option */
meillo@10 286 msg->return_path = return_path;
meillo@0 287
meillo@10 288 /* -F option */
meillo@10 289 msg->full_sender_name = full_sender_name;
meillo@0 290
meillo@266 291 err = accept_message(stdin, msg, accept_flags);
meillo@0 292
meillo@266 293 switch (err) {
meillo@266 294 case AERR_OK:
meillo@266 295 /* to continue; all other cases exit */
meillo@266 296 break;
meillo@266 297 case AERR_EOF:
meillo@266 298 fprintf(stderr, "unexpected EOF.\n");
meillo@266 299 exit(1);
meillo@266 300 case AERR_NORCPT:
meillo@266 301 fprintf(stderr, "no recipients.\n");
meillo@266 302 exit(1);
meillo@266 303 case AERR_SIZE:
meillo@266 304 fprintf(stderr, "max message size exceeded.\n");
meillo@266 305 exit(1);
meillo@266 306 default:
meillo@266 307 /* should never happen: */
meillo@266 308 fprintf(stderr, "Unknown error (%d)\r\n", err);
meillo@266 309 exit(1);
meillo@266 310 }
meillo@266 311
meillo@266 312 if (!spool_write(msg, TRUE)) {
meillo@266 313 fprintf(stderr, "Could not write spool file\n");
meillo@266 314 exit(1);
meillo@266 315 }
meillo@266 316
meillo@267 317 /* here the mail is queued and thus in our responsibility */
meillo@409 318 logwrite(LOG_NOTICE, "%s <= %s with %s\n", msg->uid,
meillo@409 319 addr_string(msg->return_path), prot_names[PROT_LOCAL]);
meillo@266 320
meillo@266 321 if (conf.do_queue) {
meillo@266 322 /* we're finished as we only need to queue it */
meillo@266 323 return;
meillo@266 324 }
meillo@266 325
meillo@266 326 /* deliver at once */
meillo@266 327 if ((pid = fork()) < 0) {
meillo@409 328 logwrite(LOG_ALERT, "could not fork for delivery, id = %s\n",
meillo@409 329 msg->uid);
meillo@266 330 } else if (pid == 0) {
meillo@266 331 conf.do_verbose = FALSE;
meillo@266 332 fclose(stdin);
meillo@266 333 fclose(stdout);
meillo@266 334 fclose(stderr);
meillo@266 335 if (deliver(msg)) {
meillo@266 336 exit(0);
meillo@10 337 } else {
meillo@267 338 /*
meillo@367 339 ** TODO: Should we really fail here? Because the
meillo@367 340 ** mail is queued already. If we fail the client
meillo@367 341 ** might submit it again. If at-once-delivery
meillo@367 342 ** is seen as an additional best-effort service,
meillo@367 343 ** then we should still exit successful here.
meillo@267 344 */
meillo@262 345 exit(1);
meillo@10 346 }
meillo@0 347 }
meillo@0 348 }
meillo@0 349
meillo@250 350 /*
meillo@367 351 ** if -Mrm is given
meillo@367 352 **
meillo@367 353 ** currently only the `rm' command is supported
meillo@367 354 ** until this changes, we don't need any facility for further commands
meillo@367 355 ** return success if at least one message had been deleted
meillo@250 356 */
meillo@250 357 static int
meillo@366 358 manipulate_queue(char *cmd, char *id[])
meillo@250 359 {
meillo@250 360 gboolean ok = FALSE;
meillo@250 361
meillo@250 362 if (strcmp(cmd, "rm") != 0) {
meillo@250 363 fprintf(stderr, "unknown command %s\n", cmd);
meillo@250 364 return FALSE;
meillo@250 365 }
meillo@250 366
meillo@250 367 set_euidgid(conf.mail_uid, conf.mail_gid, NULL, NULL);
meillo@250 368
meillo@250 369 /* privileged users may delete any mail */
meillo@250 370 if (is_privileged_user(conf.orig_uid)) {
meillo@250 371 for (; *id; id++) {
meillo@250 372 fprintf(stderr, "id: %s\n", *id);
meillo@250 373 if (queue_delete(*id)) {
meillo@250 374 ok = TRUE;
meillo@250 375 }
meillo@250 376 }
meillo@250 377 return ok;
meillo@250 378 }
meillo@250 379
meillo@250 380 struct passwd *pw = getpwuid(conf.orig_uid);
meillo@250 381 if (!pw) {
meillo@409 382 fprintf(stderr, "could not find a passwd entry for "
meillo@409 383 "uid %d: %s\n",
meillo@409 384 conf.orig_uid, strerror(errno));
meillo@250 385 return FALSE;
meillo@250 386 }
meillo@250 387
meillo@250 388 /* non-privileged users may only delete their own messages */
meillo@250 389 for (; *id; id++) {
meillo@349 390 message *msg = msg_spool_read(*id);
meillo@250 391
meillo@250 392 fprintf(stderr, "id: %s\n", *id);
meillo@250 393
meillo@250 394 if (!msg->ident) {
meillo@409 395 fprintf(stderr, "message %s does not have an ident\n",
meillo@409 396 *id);
meillo@250 397 continue;
meillo@250 398 }
meillo@250 399 if (strcmp(pw->pw_name, msg->ident) != 0) {
meillo@250 400 fprintf(stderr, "you do not own message id %s\n", *id);
meillo@250 401 continue;
meillo@250 402 }
meillo@250 403
meillo@378 404 if (msg->received_host || (msg->received_prot != PROT_LOCAL)) {
meillo@409 405 fprintf(stderr, "message %s was not received "
meillo@409 406 "locally\n", *id);
meillo@250 407 continue;
meillo@250 408 }
meillo@250 409
meillo@250 410 ok = queue_delete(*id);
meillo@250 411 }
meillo@250 412 return ok;
meillo@250 413 }
meillo@250 414
meillo@264 415 /* -qo, -q (without argument), or called as runq */
meillo@251 416 static int
meillo@366 417 run_queue(gboolean do_runq, gboolean do_runq_online, char *route_name)
meillo@251 418 {
meillo@251 419 int ret;
meillo@251 420
meillo@251 421 /* queue runs */
meillo@251 422 set_identity(conf.orig_uid, "queue run");
meillo@251 423
meillo@251 424 if (do_runq) {
meillo@251 425 ret = queue_run();
meillo@251 426 }
meillo@251 427
meillo@251 428 if (do_runq_online) {
meillo@251 429 if (route_name) {
meillo@409 430 conf.online_query = g_strdup_printf("/bin/echo %s",
meillo@409 431 route_name);
meillo@251 432 }
meillo@367 433 /*
meillo@367 434 ** TODO: change behavior of `-qo without argument'?
meillo@367 435 ** Because that behavior is included in -q.
meillo@367 436 */
meillo@251 437 ret = queue_run_online();
meillo@251 438 }
meillo@251 439 return ret;
meillo@251 440 }
meillo@251 441
meillo@264 442 /* -bV or default mode if neither addr arg nor -t */
meillo@261 443 static void
meillo@261 444 mode_version(void)
meillo@261 445 {
meillo@261 446 gchar *with_resolver = "";
meillo@261 447 gchar *with_auth = "";
meillo@261 448
meillo@261 449 #ifdef ENABLE_RESOLVER
meillo@261 450 with_resolver = " +resolver";
meillo@261 451 #endif
meillo@261 452 #ifdef ENABLE_AUTH
meillo@261 453 with_auth = " +auth";
meillo@261 454 #endif
meillo@261 455
meillo@378 456 printf("%s %s%s%s\n", PACKAGE, VERSION, with_resolver, with_auth);
meillo@261 457 }
meillo@261 458
meillo@281 459 void
meillo@281 460 set_mode(enum mta_mode mode)
meillo@281 461 {
meillo@281 462 if (mta_mode && mta_mode!=mode) {
meillo@409 463 fprintf(stderr, "operation mode was already specified "
meillo@409 464 "(%d vs. %d)\n", mta_mode, mode);
meillo@281 465 exit(1);
meillo@281 466 }
meillo@281 467
meillo@281 468 mta_mode = mode;
meillo@281 469 return;
meillo@281 470 }
meillo@281 471
meillo@0 472 int
meillo@0 473 main(int argc, char *argv[])
meillo@0 474 {
meillo@251 475 gchar *progname;
meillo@366 476 char *opt;
meillo@249 477 gint arg;
meillo@0 478
meillo@10 479 gboolean do_listen = FALSE;
meillo@10 480 gboolean do_runq = FALSE;
meillo@10 481 gboolean do_runq_online = FALSE;
meillo@10 482 gboolean do_queue = FALSE;
meillo@10 483 gint queue_interval = 0;
meillo@251 484 gchar *M_cmd = NULL;
meillo@10 485 gboolean opt_t = FALSE;
meillo@10 486 gboolean opt_i = FALSE;
meillo@251 487 gchar *conf_file = CONF_FILE;
meillo@10 488 gchar *route_name = NULL;
meillo@10 489 gchar *f_address = NULL;
meillo@251 490 address *return_path = NULL; /* may be changed by -f option */
meillo@10 491 gchar *full_sender_name = NULL;
meillo@251 492 gboolean do_verbose = FALSE;
meillo@251 493 gint debug_level = -1;
meillo@0 494
meillo@260 495 /* strip the path part */
meillo@261 496 progname = strrchr(argv[0], '/');
meillo@261 497 progname = (progname) ? progname+1 : argv[0];
meillo@0 498
meillo@10 499 if (strcmp(progname, "mailq") == 0) {
meillo@10 500 mta_mode = MODE_LIST;
meillo@10 501 } else if (strcmp(progname, "mailrm") == 0) {
meillo@10 502 mta_mode = MODE_MCMD;
meillo@10 503 M_cmd = "rm";
meillo@284 504 } else if (strcmp(progname, "newaliases") == 0) {
meillo@284 505 mta_mode = MODE_BI;
meillo@10 506 } else if (strcmp(progname, "rmail") == 0) {
meillo@367 507 /*
meillo@367 508 ** the `rmail' alias should probably be removed now
meillo@367 509 ** that we have the rmail script. But let's keep it
meillo@367 510 ** for some while for compatibility. 2010-06-19
meillo@367 511 */
meillo@10 512 mta_mode = MODE_ACCEPT;
meillo@10 513 opt_i = TRUE;
meillo@284 514 } else if (strcmp(progname, "runq") == 0) {
meillo@284 515 mta_mode = MODE_RUNQUEUE;
meillo@284 516 do_runq = TRUE;
meillo@284 517 } else if (strcmp(progname, "smtpd") == 0
meillo@284 518 || strcmp(progname, "in.smtpd") == 0) {
meillo@10 519 mta_mode = MODE_SMTP;
meillo@10 520 }
meillo@0 521
meillo@10 522 /* parse cmd line */
meillo@249 523 for (arg=1; arg<argc && argv[arg][0]=='-'; arg++) {
meillo@251 524 opt = argv[arg] + 1; /* points to the char after the dash */
meillo@10 525
meillo@249 526 if (strcmp(opt, "-") == 0) {
meillo@249 527 /* everything after `--' are address arguments */
meillo@249 528 arg++;
meillo@249 529 break;
meillo@10 530
meillo@278 531 } else if (strcmp(opt, "bm") == 0) {
meillo@281 532 set_mode(MODE_ACCEPT);
meillo@278 533
meillo@249 534 } else if (strcmp(opt, "bd") == 0) {
meillo@281 535 set_mode(MODE_DAEMON);
meillo@249 536 do_listen = TRUE;
meillo@249 537
meillo@249 538 } else if (strcmp(opt, "bi") == 0) {
meillo@281 539 set_mode(MODE_BI);
meillo@249 540
meillo@249 541 } else if (strcmp(opt, "bs") == 0) {
meillo@281 542 set_mode(MODE_SMTP);
meillo@249 543
meillo@249 544 } else if (strcmp(opt, "bp") == 0) {
meillo@281 545 set_mode(MODE_LIST);
meillo@249 546
meillo@249 547 } else if (strcmp(opt, "bV") == 0) {
meillo@281 548 set_mode(MODE_VERSION);
meillo@249 549
meillo@249 550 } else if (strncmp(opt, "B", 1) == 0) {
meillo@249 551 /* we ignore this and throw the argument away */
meillo@249 552 get_optarg(argv, &arg, opt+1);
meillo@249 553
meillo@249 554 } else if (strncmp(opt, "C", 1) == 0) {
meillo@251 555 conf_file = get_optarg(argv, &arg, opt+1);
meillo@251 556 if (!conf_file) {
meillo@409 557 fprintf(stderr, "-C requires filename arg.\n");
meillo@262 558 exit(1);
meillo@10 559 }
meillo@249 560
meillo@249 561 } else if (strncmp(opt, "d", 1) == 0) {
meillo@249 562 if (getuid() != 0) {
meillo@409 563 fprintf(stderr, "only root may set the "
meillo@409 564 "debug level.\n");
meillo@262 565 exit(1);
meillo@249 566 }
meillo@249 567 char *lvl = get_optarg(argv, &arg, opt+1);
meillo@249 568 if (!lvl) {
meillo@409 569 fprintf(stderr, "-d requires number arg.\n");
meillo@262 570 exit(1);
meillo@249 571 }
meillo@249 572 debug_level = atoi(lvl);
meillo@249 573
meillo@249 574 } else if (strncmp(opt, "f", 1) == 0) {
meillo@249 575 /* set return path */
meillo@249 576 gchar *address = get_optarg(argv, &arg, opt+1);
meillo@249 577 if (!address) {
meillo@409 578 fprintf(stderr, "-f requires address arg.\n");
meillo@262 579 exit(1);
meillo@249 580 }
meillo@249 581 f_address = g_strdup(address);
meillo@249 582
meillo@249 583 } else if (strncmp(opt, "F", 1) == 0) {
meillo@249 584 full_sender_name = get_optarg(argv, &arg, opt+1);
meillo@249 585 if (!full_sender_name) {
meillo@409 586 fprintf(stderr, "-F requires name arg.\n");
meillo@262 587 exit(1);
meillo@249 588 }
meillo@249 589
meillo@249 590 } else if (strcmp(opt, "i") == 0) {
meillo@249 591 opt_i = TRUE;
meillo@249 592
meillo@249 593 } else if (strcmp(opt, "m") == 0) {
meillo@249 594 /* ignore -m (me too) switch (see man page) */
meillo@249 595
meillo@249 596 } else if (strcmp(opt, "Mrm") == 0) {
meillo@281 597 set_mode(MODE_MCMD);
meillo@249 598 M_cmd = "rm";
meillo@249 599
meillo@249 600 } else if (strcmp(opt, "odq") == 0) {
meillo@249 601 do_queue = TRUE;
meillo@249 602
meillo@249 603 } else if (strcmp(opt, "oi") == 0) {
meillo@257 604 opt_i = TRUE;
meillo@249 605
meillo@249 606 } else if (strncmp(opt, "o", 1) == 0) {
meillo@249 607 /* ignore all other -oXXX options */
meillo@249 608
meillo@249 609 } else if (strncmp(opt, "qo", 2) == 0) {
meillo@278 610 /* must be before the `q' check */
meillo@281 611 set_mode(MODE_RUNQUEUE);
meillo@249 612 do_runq_online = TRUE;
meillo@249 613 /* can be NULL, then we use online detection method */
meillo@290 614 /* TODO: behavior might change if it is NULL */
meillo@249 615 route_name = get_optarg(argv, &arg, opt+2);
meillo@290 616 if (!route_name) {
meillo@409 617 fprintf(stderr, "Please do not use -qo "
meillo@409 618 "without argument anymore; "
meillo@409 619 "use -q instead.\n");
meillo@409 620 fprintf(stderr, "The behavior for -qo without "
meillo@409 621 "argument is likely to "
meillo@409 622 "change.\n");
meillo@290 623 }
meillo@249 624
meillo@249 625 } else if (strncmp(opt, "q", 1) == 0) {
meillo@249 626 /* must be after the `qo' check */
meillo@249 627 gchar *optarg;
meillo@249 628
meillo@251 629 optarg = get_optarg(argv, &arg, opt+1);
meillo@251 630 if (optarg) {
meillo@409 631 /* do regular queue runs */
meillo@281 632 set_mode(MODE_DAEMON);
meillo@254 633 queue_interval = time_interval(optarg);
meillo@281 634 } else {
meillo@409 635 /* do a single queue run */
meillo@281 636 set_mode(MODE_RUNQUEUE);
meillo@281 637 do_runq = TRUE;
meillo@249 638 }
meillo@249 639
meillo@249 640 } else if (strcmp(opt, "t") == 0) {
meillo@249 641 opt_t = TRUE;
meillo@249 642
meillo@249 643 } else if (strcmp(opt, "v") == 0) {
meillo@249 644 do_verbose = TRUE;
meillo@249 645
meillo@10 646 } else {
meillo@249 647 fprintf(stderr, "unrecognized option `-%s'\n", opt);
meillo@262 648 exit(1);
meillo@10 649 }
meillo@0 650 }
meillo@0 651
meillo@281 652 if (!mta_mode && arg==argc && !opt_t) {
meillo@278 653 /*
meillo@367 654 ** In this case no rcpts can be found, thus no mail
meillo@367 655 ** can be sent, thus masqmail will always fail. We
meillo@367 656 ** rather do something better instead. This covers
meillo@367 657 ** also the case of calling masqmail without args.
meillo@278 658 */
meillo@281 659 mode_version();
meillo@281 660 exit(0);
meillo@259 661 }
meillo@259 662
meillo@10 663 if (mta_mode == MODE_VERSION) {
meillo@261 664 mode_version();
meillo@262 665 exit(0);
meillo@10 666 }
meillo@0 667
meillo@281 668 if (!mta_mode) {
meillo@281 669 mta_mode = MODE_ACCEPT;
meillo@281 670 }
meillo@281 671
meillo@10 672 /* initialize random generator */
meillo@10 673 srand(time(NULL));
meillo@10 674 /* ignore SIGPIPE signal */
meillo@10 675 signal(SIGPIPE, SIG_IGN);
meillo@0 676
meillo@73 677 /* close all possibly open file descriptors, except std{in,out,err} */
meillo@10 678 {
meillo@10 679 int i, max_fd = sysconf(_SC_OPEN_MAX);
meillo@0 680
meillo@251 681 if (max_fd <= 0) {
meillo@10 682 max_fd = 64;
meillo@251 683 }
meillo@251 684 for (i=3; i<max_fd; i++) {
meillo@10 685 close(i);
meillo@251 686 }
meillo@10 687 }
meillo@0 688
meillo@10 689 init_conf();
meillo@0 690
meillo@367 691 /*
meillo@367 692 ** if we are not privileged, and the config file was changed we
meillo@367 693 ** implicetely set the the run_as_user flag and give up all
meillo@367 694 ** privileges.
meillo@367 695 **
meillo@367 696 ** So it is possible for a user to run his own daemon without
meillo@367 697 ** breaking security.
meillo@367 698 */
meillo@251 699 if ((strcmp(conf_file, CONF_FILE) != 0) && (conf.orig_uid != 0)) {
meillo@421 700 logwrite(LOG_NOTICE, "Changing to run_as_user.\n");
meillo@251 701 conf.run_as_user = TRUE;
meillo@331 702 set_euidgid(conf.orig_uid, conf.orig_gid, NULL, NULL);
meillo@331 703 if (setgid(conf.orig_gid)) {
meillo@409 704 logwrite(LOG_ALERT, "could not set gid to %d: %s\n",
meillo@409 705 conf.orig_gid, strerror(errno));
meillo@331 706 exit(1);
meillo@331 707 }
meillo@331 708 if (setuid(conf.orig_uid)) {
meillo@409 709 logwrite(LOG_ALERT, "could not set uid to %d: %s\n",
meillo@409 710 conf.orig_uid, strerror(errno));
meillo@331 711 exit(1);
meillo@331 712 }
meillo@10 713 }
meillo@0 714
meillo@155 715 conf.log_dir = LOG_DIR;
meillo@397 716 conf.debug_level = debug_level; /* for debuggin during read_conf() */
meillo@288 717 /* FIXME: fails if we run as user */
meillo@155 718 logopen();
meillo@155 719 if (!read_conf(conf_file)) {
meillo@409 720 logwrite(LOG_ALERT, "SHUTTING DOWN due to problems reading "
meillo@409 721 "config\n");
meillo@155 722 exit(5);
meillo@155 723 }
meillo@155 724 logclose();
meillo@0 725
meillo@251 726 if (do_queue) {
meillo@10 727 conf.do_queue = TRUE;
meillo@251 728 }
meillo@251 729 if (do_verbose) {
meillo@10 730 conf.do_verbose = TRUE;
meillo@251 731 }
meillo@251 732 if (debug_level >= 0) { /* if >= 0, it was given by argument */
meillo@10 733 conf.debug_level = debug_level;
meillo@251 734 }
meillo@0 735
meillo@367 736 /*
meillo@367 737 ** It appears that changing to / ensures that we are never in
meillo@367 738 ** a directory which we cannot access. This situation could be
meillo@367 739 ** possible after changing identity.
meillo@367 740 ** Maybe we should only change to / if we not run as user, to
meillo@367 741 ** allow relative paths for log files in test setups for
meillo@367 742 ** instance.
meillo@46 743 */
meillo@10 744 chdir("/");
meillo@0 745
meillo@10 746 if (!conf.run_as_user) {
meillo@10 747 if (setgid(0) != 0) {
meillo@409 748 fprintf(stderr, "could not set gid to 0. "
meillo@409 749 "Is the setuid bit set? : %s\n",
meillo@409 750 strerror(errno));
meillo@262 751 exit(1);
meillo@10 752 }
meillo@10 753 if (setuid(0) != 0) {
meillo@409 754 fprintf(stderr, "could not gain root privileges. "
meillo@409 755 "Is the setuid bit set? : %s\n",
meillo@409 756 strerror(errno));
meillo@262 757 exit(1);
meillo@10 758 }
meillo@10 759 }
meillo@0 760
meillo@421 761 if (conf.run_as_user) {
meillo@421 762 logwrite(LOG_NOTICE, "Using spool directory `%s' for "
meillo@421 763 "lock files.\n", conf.spool_dir);
meillo@421 764 conf.lock_dir = conf.spool_dir;
meillo@425 765 makedir_rec(conf.spool_dir, 0755);
meillo@425 766 makedir_rec(conf.log_dir, 0755);
meillo@421 767 } else {
meillo@421 768 makedir_rec(conf.lock_dir, 0775);
meillo@425 769 chown(conf.lock_dir, conf.mail_uid, conf.mail_gid);
meillo@425 770 makedir_rec(conf.spool_dir, 0755);
meillo@425 771 chown(conf.spool_dir, conf.mail_uid, conf.mail_gid);
meillo@425 772 makedir_rec(conf.log_dir, 0755);
meillo@425 773 chown(conf.log_dir, conf.mail_uid, conf.mail_gid);
meillo@421 774 }
meillo@421 775
meillo@10 776 if (!logopen()) {
meillo@10 777 fprintf(stderr, "could not open log file\n");
meillo@262 778 exit(1);
meillo@10 779 }
meillo@0 780
meillo@415 781 DEBUG(1) debugf("----STARTING---- masqmail %s\n", VERSION);
meillo@0 782
meillo@10 783 DEBUG(5) {
meillo@10 784 gchar **str = argv;
meillo@10 785 debugf("args: \n");
meillo@10 786 while (*str) {
meillo@10 787 debugf("%s \n", *str);
meillo@10 788 str++;
meillo@10 789 }
meillo@10 790 }
meillo@10 791 DEBUG(5) debugf("queue_interval = %d\n", queue_interval);
meillo@0 792
meillo@10 793 if (f_address) {
meillo@409 794 return_path = create_address_qualified(f_address, TRUE,
meillo@409 795 conf.host_name);
meillo@10 796 g_free(f_address);
meillo@10 797 if (!return_path) {
meillo@409 798 fprintf(stderr, "invalid RFC821 address: %s\n",
meillo@409 799 f_address);
meillo@262 800 exit(1);
meillo@10 801 }
meillo@10 802 }
meillo@10 803
meillo@10 804 switch (mta_mode) {
meillo@10 805 case MODE_DAEMON:
meillo@10 806 mode_daemon(do_listen, queue_interval, argv);
meillo@10 807 break;
meillo@251 808
meillo@10 809 case MODE_RUNQUEUE:
meillo@251 810 exit(run_queue(do_runq, do_runq_online, route_name) ? 0 : 1);
meillo@10 811 break;
meillo@10 812
meillo@10 813 case MODE_SMTP:
meillo@10 814 mode_smtp();
meillo@10 815 break;
meillo@10 816
meillo@10 817 case MODE_LIST:
meillo@10 818 queue_list();
meillo@10 819 break;
meillo@10 820
meillo@10 821 case MODE_BI:
meillo@262 822 exit(0);
meillo@10 823 break; /* well... */
meillo@10 824
meillo@10 825 case MODE_MCMD:
meillo@250 826 exit(manipulate_queue(M_cmd, &argv[arg]) ? 0 : 1);
meillo@10 827 break;
meillo@10 828
meillo@10 829 case MODE_ACCEPT:
meillo@10 830 {
meillo@409 831 guint accept_flags = 0;
meillo@409 832 accept_flags |= (opt_t ? ACC_RCPT_FROM_HEAD : 0);
meillo@409 833 accept_flags |= (opt_i ?
meillo@409 834 ACC_DOT_IGNORE : ACC_NODOT_RELAX);
meillo@409 835 mode_accept(return_path, full_sender_name,
meillo@409 836 accept_flags, argv + arg, argc - arg);
meillo@258 837 exit(0);
meillo@10 838 }
meillo@10 839 break;
meillo@251 840
meillo@10 841 default:
meillo@10 842 fprintf(stderr, "unknown mode: %d\n", mta_mode);
meillo@10 843 break;
meillo@0 844 }
meillo@0 845
meillo@10 846 logclose();
meillo@0 847
meillo@263 848 exit(0);
meillo@0 849 }