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