masqmail

annotate src/masqmail.c @ 372:b0708fac99dd

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