masqmail
diff src/masqmail.c @ 281:ea5f86e0a81c
modes are now enforced exclusive
Other MTAs (exim, postfix) are more relaxing, but as combinations
of exclusive modes are senseless we behave more obvious if we
fail early. This makes understanding the behavior easier too.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Tue, 07 Dec 2010 14:04:56 -0300 |
parents | c35c59a36a2a |
children | ba53e648906f |
line diff
1.1 --- a/src/masqmail.c Mon Dec 06 18:07:01 2010 -0300 1.2 +++ b/src/masqmail.c Tue Dec 07 14:04:56 2010 -0300 1.3 @@ -37,8 +37,9 @@ 1.4 /* mutually exclusive modes. Note that there is no 'queue daemon' mode. 1.5 It, as well as the distinction beween the two (non exclusive) daemon 1.6 (queue and listen) modes, is handled by flags.*/ 1.7 -typedef enum _mta_mode { 1.8 - MODE_ACCEPT = 0, /* accept message on stdin (fallback mode) */ 1.9 +enum mta_mode { 1.10 + MODE_NONE = 0, /* to check if a mode was set */ 1.11 + MODE_ACCEPT, /* accept message on stdin (fallback mode) */ 1.12 MODE_DAEMON, /* run as daemon */ 1.13 MODE_RUNQUEUE, /* single queue run, online or offline */ 1.14 MODE_SMTP, /* accept SMTP on stdin */ 1.15 @@ -46,7 +47,8 @@ 1.16 MODE_MCMD, /* do queue manipulation */ 1.17 MODE_VERSION, /* show version */ 1.18 MODE_BI, /* fake ;-) */ 1.19 -} mta_mode; 1.20 +}; 1.21 +enum mta_mode mta_mode = MODE_NONE; 1.22 1.23 char *pidfile = NULL; 1.24 volatile int sigterm_in_progress = 0; 1.25 @@ -414,6 +416,18 @@ 1.26 printf("%s %s%s%s%s\n", PACKAGE, VERSION, with_resolver, with_auth, with_ident); 1.27 } 1.28 1.29 +void 1.30 +set_mode(enum mta_mode mode) 1.31 +{ 1.32 + if (mta_mode && mta_mode!=mode) { 1.33 + fprintf(stderr, "operation mode was already specified (%d vs. %d)\n", mta_mode, mode); 1.34 + exit(1); 1.35 + } 1.36 + 1.37 + mta_mode = mode; 1.38 + return; 1.39 +} 1.40 + 1.41 int 1.42 main(int argc, char *argv[]) 1.43 { 1.44 @@ -421,7 +435,6 @@ 1.45 char* opt; 1.46 gint arg; 1.47 1.48 - mta_mode mta_mode = MODE_ACCEPT; 1.49 gboolean do_listen = FALSE; 1.50 gboolean do_runq = FALSE; 1.51 gboolean do_runq_online = FALSE; 1.52 @@ -470,24 +483,23 @@ 1.53 break; 1.54 1.55 } else if (strcmp(opt, "bm") == 0) { 1.56 - mta_mode = MODE_ACCEPT; 1.57 + set_mode(MODE_ACCEPT); 1.58 1.59 } else if (strcmp(opt, "bd") == 0) { 1.60 + set_mode(MODE_DAEMON); 1.61 do_listen = TRUE; 1.62 - mta_mode = MODE_DAEMON; 1.63 1.64 } else if (strcmp(opt, "bi") == 0) { 1.65 - /* ignored */ 1.66 - mta_mode = MODE_BI; 1.67 + set_mode(MODE_BI); 1.68 1.69 } else if (strcmp(opt, "bs") == 0) { 1.70 - mta_mode = MODE_SMTP; 1.71 + set_mode(MODE_SMTP); 1.72 1.73 } else if (strcmp(opt, "bp") == 0) { 1.74 - mta_mode = MODE_LIST; 1.75 + set_mode(MODE_LIST); 1.76 1.77 } else if (strcmp(opt, "bV") == 0) { 1.78 - mta_mode = MODE_VERSION; 1.79 + set_mode(MODE_VERSION); 1.80 1.81 } else if (strncmp(opt, "B", 1) == 0) { 1.82 /* we ignore this and throw the argument away */ 1.83 @@ -535,7 +547,7 @@ 1.84 /* ignore -m (me too) switch (see man page) */ 1.85 1.86 } else if (strcmp(opt, "Mrm") == 0) { 1.87 - mta_mode = MODE_MCMD; 1.88 + set_mode(MODE_MCMD); 1.89 M_cmd = "rm"; 1.90 1.91 } else if (strcmp(opt, "odq") == 0) { 1.92 @@ -549,7 +561,7 @@ 1.93 1.94 } else if (strncmp(opt, "qo", 2) == 0) { 1.95 /* must be before the `q' check */ 1.96 - mta_mode = MODE_RUNQUEUE; 1.97 + set_mode(MODE_RUNQUEUE); 1.98 do_runq = FALSE; 1.99 do_runq_online = TRUE; 1.100 /* can be NULL, then we use online detection method */ 1.101 @@ -559,13 +571,14 @@ 1.102 /* must be after the `qo' check */ 1.103 gchar *optarg; 1.104 1.105 - do_runq = TRUE; 1.106 - mta_mode = MODE_RUNQUEUE; 1.107 optarg = get_optarg(argv, &arg, opt+1); 1.108 if (optarg) { 1.109 /* not just one single queue run but regular runs */ 1.110 - mta_mode = MODE_DAEMON; 1.111 + set_mode(MODE_DAEMON); 1.112 queue_interval = time_interval(optarg); 1.113 + } else { 1.114 + set_mode(MODE_RUNQUEUE); 1.115 + do_runq = TRUE; 1.116 } 1.117 1.118 } else if (strcmp(opt, "t") == 0) { 1.119 @@ -580,14 +593,15 @@ 1.120 } 1.121 } 1.122 1.123 - if (mta_mode==MODE_ACCEPT && arg==argc && !opt_t) { 1.124 + if (!mta_mode && arg==argc && !opt_t) { 1.125 /* 1.126 In this case no rcpts can be found, thus no mail 1.127 can be sent, thus masqmail will always fail. We 1.128 rather do something better instead. This covers 1.129 also the case of calling masqmail without args. 1.130 */ 1.131 - mta_mode = MODE_VERSION; 1.132 + mode_version(); 1.133 + exit(0); 1.134 } 1.135 1.136 if (mta_mode == MODE_VERSION) { 1.137 @@ -595,6 +609,10 @@ 1.138 exit(0); 1.139 } 1.140 1.141 + if (!mta_mode) { 1.142 + mta_mode = MODE_ACCEPT; 1.143 + } 1.144 + 1.145 /* initialize random generator */ 1.146 srand(time(NULL)); 1.147 /* ignore SIGPIPE signal */