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