Mercurial > masqmail
comparison src/masqmail.c @ 251:2babd21e7c75
moved run_queue to a new function
plus various small cleanups, mostly cosmetic
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Thu, 04 Nov 2010 13:00:19 -0300 |
parents | a41c013c8458 |
children | 82d168dd52fd |
comparison
equal
deleted
inserted
replaced
250:a41c013c8458 | 251:2babd21e7c75 |
---|---|
351 ok = queue_delete(*id); | 351 ok = queue_delete(*id); |
352 } | 352 } |
353 return ok; | 353 return ok; |
354 } | 354 } |
355 | 355 |
356 static int | |
357 run_queue(gboolean do_runq, gboolean do_runq_online, char* route_name) | |
358 { | |
359 int ret; | |
360 | |
361 /* queue runs */ | |
362 set_identity(conf.orig_uid, "queue run"); | |
363 | |
364 if (do_runq) { | |
365 ret = queue_run(); | |
366 } | |
367 | |
368 if (do_runq_online) { | |
369 if (route_name) { | |
370 conf.online_detect = g_strdup("argument"); | |
371 set_online_name(route_name); | |
372 } | |
373 ret = queue_run_online(); | |
374 } | |
375 return ret; | |
376 } | |
377 | |
356 int | 378 int |
357 main(int argc, char *argv[]) | 379 main(int argc, char *argv[]) |
358 { | 380 { |
359 /* cmd line flags */ | 381 gchar *progname; |
360 gchar *conf_file = CONF_FILE; | |
361 char* opt; | 382 char* opt; |
362 gint arg; | 383 gint arg; |
363 | 384 |
385 mta_mode mta_mode = MODE_ACCEPT; | |
364 gboolean do_listen = FALSE; | 386 gboolean do_listen = FALSE; |
365 gboolean do_runq = FALSE; | 387 gboolean do_runq = FALSE; |
366 gboolean do_runq_online = FALSE; | 388 gboolean do_runq_online = FALSE; |
367 | |
368 gboolean do_queue = FALSE; | 389 gboolean do_queue = FALSE; |
369 | |
370 gboolean do_verbose = FALSE; | |
371 gint debug_level = -1; | |
372 | |
373 mta_mode mta_mode = MODE_ACCEPT; | |
374 | |
375 gint queue_interval = 0; | 390 gint queue_interval = 0; |
391 gchar *M_cmd = NULL; | |
376 gboolean opt_t = FALSE; | 392 gboolean opt_t = FALSE; |
377 gboolean opt_i = FALSE; | 393 gboolean opt_i = FALSE; |
378 gboolean exit_failure = FALSE; | 394 gboolean exit_failure = FALSE; |
379 | |
380 gchar *M_cmd = NULL; | |
381 | |
382 gint exit_code = EXIT_SUCCESS; | 395 gint exit_code = EXIT_SUCCESS; |
396 gchar *conf_file = CONF_FILE; | |
383 gchar *route_name = NULL; | 397 gchar *route_name = NULL; |
384 gchar *progname; | |
385 gchar *f_address = NULL; | 398 gchar *f_address = NULL; |
399 address *return_path = NULL; /* may be changed by -f option */ | |
386 gchar *full_sender_name = NULL; | 400 gchar *full_sender_name = NULL; |
387 address *return_path = NULL; /* may be changed by -f option */ | 401 gboolean do_verbose = FALSE; |
402 gint debug_level = -1; | |
388 | 403 |
389 progname = get_progname(argv[0]); | 404 progname = get_progname(argv[0]); |
390 | 405 |
391 if (strcmp(progname, "mailq") == 0) { | 406 if (strcmp(progname, "mailq") == 0) { |
392 mta_mode = MODE_LIST; | 407 mta_mode = MODE_LIST; |
406 mta_mode = MODE_SMTP; | 421 mta_mode = MODE_SMTP; |
407 } | 422 } |
408 | 423 |
409 /* parse cmd line */ | 424 /* parse cmd line */ |
410 for (arg=1; arg<argc && argv[arg][0]=='-'; arg++) { | 425 for (arg=1; arg<argc && argv[arg][0]=='-'; arg++) { |
411 opt = argv[arg] + 1; | 426 opt = argv[arg] + 1; /* points to the char after the dash */ |
412 | 427 |
413 if (strcmp(opt, "-") == 0) { | 428 if (strcmp(opt, "-") == 0) { |
414 /* everything after `--' are address arguments */ | 429 /* everything after `--' are address arguments */ |
415 arg++; | 430 arg++; |
416 break; | 431 break; |
435 } else if (strncmp(opt, "B", 1) == 0) { | 450 } else if (strncmp(opt, "B", 1) == 0) { |
436 /* we ignore this and throw the argument away */ | 451 /* we ignore this and throw the argument away */ |
437 get_optarg(argv, &arg, opt+1); | 452 get_optarg(argv, &arg, opt+1); |
438 | 453 |
439 } else if (strncmp(opt, "C", 1) == 0) { | 454 } else if (strncmp(opt, "C", 1) == 0) { |
440 if (!(conf_file = get_optarg(argv, &arg, opt+1))) { | 455 conf_file = get_optarg(argv, &arg, opt+1); |
456 if (!conf_file) { | |
441 fprintf(stderr, "-C requires a filename as argument.\n"); | 457 fprintf(stderr, "-C requires a filename as argument.\n"); |
442 exit(EXIT_FAILURE); | 458 exit(EXIT_FAILURE); |
443 } | 459 } |
444 | 460 |
445 } else if (strncmp(opt, "d", 1) == 0) { | 461 } else if (strncmp(opt, "d", 1) == 0) { |
508 gchar *optarg; | 524 gchar *optarg; |
509 int dummy; | 525 int dummy; |
510 | 526 |
511 do_runq = TRUE; | 527 do_runq = TRUE; |
512 mta_mode = MODE_RUNQUEUE; | 528 mta_mode = MODE_RUNQUEUE; |
513 if ((optarg = get_optarg(argv, &arg, opt+1))) { | 529 optarg = get_optarg(argv, &arg, opt+1); |
530 if (optarg) { | |
531 /* not just one single queue run but regular runs */ | |
514 mta_mode = MODE_DAEMON; | 532 mta_mode = MODE_DAEMON; |
515 queue_interval = time_interval(optarg, &dummy); | 533 queue_interval = time_interval(optarg, &dummy); |
516 } | 534 } |
517 | 535 |
518 } else if (strcmp(opt, "t") == 0) { | 536 } else if (strcmp(opt, "t") == 0) { |
554 | 572 |
555 /* close all possibly open file descriptors, except std{in,out,err} */ | 573 /* close all possibly open file descriptors, except std{in,out,err} */ |
556 { | 574 { |
557 int i, max_fd = sysconf(_SC_OPEN_MAX); | 575 int i, max_fd = sysconf(_SC_OPEN_MAX); |
558 | 576 |
559 if (max_fd <= 0) | 577 if (max_fd <= 0) { |
560 max_fd = 64; | 578 max_fd = 64; |
561 for (i = 3; i < max_fd; i++) | 579 } |
580 for (i=3; i<max_fd; i++) { | |
562 close(i); | 581 close(i); |
582 } | |
563 } | 583 } |
564 | 584 |
565 init_conf(); | 585 init_conf(); |
566 | 586 |
567 /* if we are not privileged, and the config file was changed we | 587 /* if we are not privileged, and the config file was changed we |
569 privileges. | 589 privileges. |
570 | 590 |
571 So it is possible for a user to run his own daemon without | 591 So it is possible for a user to run his own daemon without |
572 breaking security. | 592 breaking security. |
573 */ | 593 */ |
574 if (strcmp(conf_file, CONF_FILE) != 0) { | 594 if ((strcmp(conf_file, CONF_FILE) != 0) && (conf.orig_uid != 0)) { |
575 if (conf.orig_uid != 0) { | 595 conf.run_as_user = TRUE; |
576 conf.run_as_user = TRUE; | 596 seteuid(conf.orig_uid); |
577 seteuid(conf.orig_uid); | 597 setegid(conf.orig_gid); |
578 setegid(conf.orig_gid); | 598 setuid(conf.orig_uid); |
579 setuid(conf.orig_uid); | 599 setgid(conf.orig_gid); |
580 setgid(conf.orig_gid); | |
581 } | |
582 } | 600 } |
583 | 601 |
584 conf.log_dir = LOG_DIR; | 602 conf.log_dir = LOG_DIR; |
585 logopen(); | 603 logopen(); |
586 if (!read_conf(conf_file)) { | 604 if (!read_conf(conf_file)) { |
587 logwrite(LOG_ALERT, "SHUTTING DOWN due to problems reading config\n"); | 605 logwrite(LOG_ALERT, "SHUTTING DOWN due to problems reading config\n"); |
588 exit(5); | 606 exit(5); |
589 } | 607 } |
590 logclose(); | 608 logclose(); |
591 | 609 |
592 if (do_queue) | 610 if (do_queue) { |
593 conf.do_queue = TRUE; | 611 conf.do_queue = TRUE; |
594 if (do_verbose) | 612 } |
613 if (do_verbose) { | |
595 conf.do_verbose = TRUE; | 614 conf.do_verbose = TRUE; |
596 if (debug_level >= 0) /* if >= 0, it was given by argument */ | 615 } |
616 if (debug_level >= 0) { /* if >= 0, it was given by argument */ | |
597 conf.debug_level = debug_level; | 617 conf.debug_level = debug_level; |
618 } | |
598 | 619 |
599 /* It appears that changing to / ensures that we are never in | 620 /* It appears that changing to / ensures that we are never in |
600 a directory which we cannot access. This situation could be | 621 a directory which we cannot access. This situation could be |
601 possible after changing identity. | 622 possible after changing identity. |
602 Maybe we should only change to / if we not run as user, to | 623 Maybe we should only change to / if we not run as user, to |
644 | 665 |
645 switch (mta_mode) { | 666 switch (mta_mode) { |
646 case MODE_DAEMON: | 667 case MODE_DAEMON: |
647 mode_daemon(do_listen, queue_interval, argv); | 668 mode_daemon(do_listen, queue_interval, argv); |
648 break; | 669 break; |
670 | |
649 case MODE_RUNQUEUE: | 671 case MODE_RUNQUEUE: |
650 { | 672 exit(run_queue(do_runq, do_runq_online, route_name) ? 0 : 1); |
651 /* queue runs */ | |
652 set_identity(conf.orig_uid, "queue run"); | |
653 | |
654 if (do_runq) | |
655 exit_code = queue_run() ? EXIT_SUCCESS : EXIT_FAILURE; | |
656 | |
657 if (do_runq_online) { | |
658 if (route_name != NULL) { | |
659 conf.online_detect = g_strdup("argument"); | |
660 set_online_name(route_name); | |
661 } | |
662 exit_code = | |
663 queue_run_online() ? EXIT_SUCCESS : EXIT_FAILURE; | |
664 } | |
665 } | |
666 break; | 673 break; |
667 | 674 |
668 case MODE_SMTP: | 675 case MODE_SMTP: |
669 mode_smtp(); | 676 mode_smtp(); |
670 break; | 677 break; |
689 exit(exit_failure ? EXIT_FAILURE : EXIT_SUCCESS); | 696 exit(exit_failure ? EXIT_FAILURE : EXIT_SUCCESS); |
690 } | 697 } |
691 break; | 698 break; |
692 case MODE_NONE: | 699 case MODE_NONE: |
693 break; | 700 break; |
701 | |
694 default: | 702 default: |
695 fprintf(stderr, "unknown mode: %d\n", mta_mode); | 703 fprintf(stderr, "unknown mode: %d\n", mta_mode); |
696 break; | 704 break; |
697 } | 705 } |
698 | 706 |