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