Mercurial > masqmail
comparison src/masqmail.c @ 262:fc1c6425c024
s/EXIT_SUCCESS/0/ && s/EXIT_FAILURE/1/
The constants are all to bulky.
We should have different, meaningful exit codes anyway.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Thu, 02 Dec 2010 17:11:25 -0300 |
parents | 0afe18a9ee03 |
children | e9e73505ab2c |
comparison
equal
deleted
inserted
replaced
261:0afe18a9ee03 | 262:fc1c6425c024 |
---|---|
136 | 136 |
137 /* daemon */ | 137 /* daemon */ |
138 if (!conf.run_as_user) { | 138 if (!conf.run_as_user) { |
139 if ((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)) { | 139 if ((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)) { |
140 fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER); | 140 fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER); |
141 exit(EXIT_FAILURE); | 141 exit(1); |
142 } | 142 } |
143 } | 143 } |
144 | 144 |
145 /* reparent to init only if init is not already the parent */ | 145 /* reparent to init only if init is not already the parent */ |
146 if (getppid() != 1) { | 146 if (getppid() != 1) { |
147 if ((pid = fork()) > 0) { | 147 if ((pid = fork()) > 0) { |
148 exit(EXIT_SUCCESS); | 148 exit(0); |
149 } else if (pid < 0) { | 149 } else if (pid < 0) { |
150 logwrite(LOG_ALERT, "could not fork!\n"); | 150 logwrite(LOG_ALERT, "could not fork!\n"); |
151 exit(EXIT_FAILURE); | 151 exit(1); |
152 } | 152 } |
153 } | 153 } |
154 | 154 |
155 signal(SIGTERM, sigterm_handler); | 155 signal(SIGTERM, sigterm_handler); |
156 write_pidfile(PIDFILEDIR "/masqmail.pid"); | 156 write_pidfile(PIDFILEDIR "/masqmail.pid"); |
191 DEBUG(5) debugf("accepting smtp message on stdin\n"); | 191 DEBUG(5) debugf("accepting smtp message on stdin\n"); |
192 | 192 |
193 if (getpeername(0, (struct sockaddr *) (&saddr), &dummy) == 0) { | 193 if (getpeername(0, (struct sockaddr *) (&saddr), &dummy) == 0) { |
194 peername = g_strdup(inet_ntoa(saddr.sin_addr)); | 194 peername = g_strdup(inet_ntoa(saddr.sin_addr)); |
195 } else if (errno != ENOTSOCK) | 195 } else if (errno != ENOTSOCK) |
196 exit(EXIT_FAILURE); | 196 exit(1); |
197 | 197 |
198 smtp_in(stdin, stderr, peername, NULL); | 198 smtp_in(stdin, stderr, peername, NULL); |
199 } | 199 } |
200 | 200 |
201 static void | 201 static void |
206 message *msg = create_message(); | 206 message *msg = create_message(); |
207 gint i; | 207 gint i; |
208 | 208 |
209 if (return_path && !is_privileged_user(conf.orig_uid)) { | 209 if (return_path && !is_privileged_user(conf.orig_uid)) { |
210 fprintf(stderr, "must be root, %s or in group %s for setting return path.\n", DEF_MAIL_USER, DEF_MAIL_GROUP); | 210 fprintf(stderr, "must be root, %s or in group %s for setting return path.\n", DEF_MAIL_USER, DEF_MAIL_GROUP); |
211 exit(EXIT_FAILURE); | 211 exit(1); |
212 } | 212 } |
213 | 213 |
214 if (!conf.run_as_user) { | 214 if (!conf.run_as_user) { |
215 seteuid(conf.orig_uid); | 215 seteuid(conf.orig_uid); |
216 setegid(conf.orig_gid); | 216 setegid(conf.orig_gid); |
222 for (i = 0; i < addr_cnt; i++) { | 222 for (i = 0; i < addr_cnt; i++) { |
223 if (addresses[i][0] != '|') | 223 if (addresses[i][0] != '|') |
224 msg->rcpt_list = g_list_append(msg->rcpt_list, create_address_qualified(addresses[i], TRUE, conf.host_name)); | 224 msg->rcpt_list = g_list_append(msg->rcpt_list, create_address_qualified(addresses[i], TRUE, conf.host_name)); |
225 else { | 225 else { |
226 logwrite(LOG_ALERT, "no pipe allowed as recipient address: %s\n", addresses[i]); | 226 logwrite(LOG_ALERT, "no pipe allowed as recipient address: %s\n", addresses[i]); |
227 exit(EXIT_FAILURE); | 227 exit(1); |
228 } | 228 } |
229 } | 229 } |
230 | 230 |
231 /* -f option */ | 231 /* -f option */ |
232 msg->return_path = return_path; | 232 msg->return_path = return_path; |
244 conf.do_verbose = FALSE; | 244 conf.do_verbose = FALSE; |
245 fclose(stdin); | 245 fclose(stdin); |
246 fclose(stdout); | 246 fclose(stdout); |
247 fclose(stderr); | 247 fclose(stderr); |
248 if (deliver(msg)) { | 248 if (deliver(msg)) { |
249 exit(EXIT_SUCCESS); | 249 exit(0); |
250 } else | 250 } else |
251 exit(EXIT_FAILURE); | 251 exit(1); |
252 } else if (pid < 0) { | 252 } else if (pid < 0) { |
253 logwrite(LOG_ALERT, "could not fork for delivery, id = %s\n", msg->uid); | 253 logwrite(LOG_ALERT, "could not fork for delivery, id = %s\n", msg->uid); |
254 } | 254 } |
255 } | 255 } |
256 } else { | 256 } else { |
257 fprintf(stderr, "Could not write spool file\n"); | 257 fprintf(stderr, "Could not write spool file\n"); |
258 exit(EXIT_FAILURE); | 258 exit(1); |
259 } | 259 } |
260 } else { | 260 } else { |
261 switch (err) { | 261 switch (err) { |
262 case AERR_EOF: | 262 case AERR_EOF: |
263 fprintf(stderr, "unexpected EOF.\n"); | 263 fprintf(stderr, "unexpected EOF.\n"); |
264 exit(EXIT_FAILURE); | 264 exit(1); |
265 case AERR_NORCPT: | 265 case AERR_NORCPT: |
266 fprintf(stderr, "no recipients.\n"); | 266 fprintf(stderr, "no recipients.\n"); |
267 exit(EXIT_FAILURE); | 267 exit(1); |
268 case AERR_SIZE: | 268 case AERR_SIZE: |
269 fprintf(stderr, "max message size exceeded.\n"); | 269 fprintf(stderr, "max message size exceeded.\n"); |
270 exit(EXIT_FAILURE); | 270 exit(1); |
271 default: | 271 default: |
272 /* should never happen: */ | 272 /* should never happen: */ |
273 fprintf(stderr, "Unknown error (%d)\r\n", err); | 273 fprintf(stderr, "Unknown error (%d)\r\n", err); |
274 exit(EXIT_FAILURE); | 274 exit(1); |
275 } | 275 } |
276 exit(EXIT_FAILURE); | 276 exit(1); |
277 } | 277 } |
278 } | 278 } |
279 | 279 |
280 /* | 280 /* |
281 currently only the `rm' command is supported | 281 currently only the `rm' command is supported |
397 gboolean do_queue = FALSE; | 397 gboolean do_queue = FALSE; |
398 gint queue_interval = 0; | 398 gint queue_interval = 0; |
399 gchar *M_cmd = NULL; | 399 gchar *M_cmd = NULL; |
400 gboolean opt_t = FALSE; | 400 gboolean opt_t = FALSE; |
401 gboolean opt_i = FALSE; | 401 gboolean opt_i = FALSE; |
402 gint exit_code = EXIT_SUCCESS; | 402 gint exit_code = 0; |
403 gchar *conf_file = CONF_FILE; | 403 gchar *conf_file = CONF_FILE; |
404 gchar *route_name = NULL; | 404 gchar *route_name = NULL; |
405 gchar *f_address = NULL; | 405 gchar *f_address = NULL; |
406 address *return_path = NULL; /* may be changed by -f option */ | 406 address *return_path = NULL; /* may be changed by -f option */ |
407 gchar *full_sender_name = NULL; | 407 gchar *full_sender_name = NULL; |
462 | 462 |
463 } else if (strncmp(opt, "C", 1) == 0) { | 463 } else if (strncmp(opt, "C", 1) == 0) { |
464 conf_file = get_optarg(argv, &arg, opt+1); | 464 conf_file = get_optarg(argv, &arg, opt+1); |
465 if (!conf_file) { | 465 if (!conf_file) { |
466 fprintf(stderr, "-C requires a filename as argument.\n"); | 466 fprintf(stderr, "-C requires a filename as argument.\n"); |
467 exit(EXIT_FAILURE); | 467 exit(1); |
468 } | 468 } |
469 | 469 |
470 } else if (strncmp(opt, "d", 1) == 0) { | 470 } else if (strncmp(opt, "d", 1) == 0) { |
471 if (getuid() != 0) { | 471 if (getuid() != 0) { |
472 fprintf(stderr, "only root may set the debug level.\n"); | 472 fprintf(stderr, "only root may set the debug level.\n"); |
473 exit(EXIT_FAILURE); | 473 exit(1); |
474 } | 474 } |
475 char *lvl = get_optarg(argv, &arg, opt+1); | 475 char *lvl = get_optarg(argv, &arg, opt+1); |
476 if (!lvl) { | 476 if (!lvl) { |
477 fprintf(stderr, "-d requires a number argument.\n"); | 477 fprintf(stderr, "-d requires a number argument.\n"); |
478 exit(EXIT_FAILURE); | 478 exit(1); |
479 } | 479 } |
480 debug_level = atoi(lvl); | 480 debug_level = atoi(lvl); |
481 | 481 |
482 } else if (strncmp(opt, "f", 1) == 0) { | 482 } else if (strncmp(opt, "f", 1) == 0) { |
483 /* set return path */ | 483 /* set return path */ |
484 gchar *address = get_optarg(argv, &arg, opt+1); | 484 gchar *address = get_optarg(argv, &arg, opt+1); |
485 if (!address) { | 485 if (!address) { |
486 fprintf(stderr, "-f requires an address argument\n"); | 486 fprintf(stderr, "-f requires an address argument\n"); |
487 exit(EXIT_FAILURE); | 487 exit(1); |
488 } | 488 } |
489 f_address = g_strdup(address); | 489 f_address = g_strdup(address); |
490 | 490 |
491 } else if (strncmp(opt, "F", 1) == 0) { | 491 } else if (strncmp(opt, "F", 1) == 0) { |
492 full_sender_name = get_optarg(argv, &arg, opt+1); | 492 full_sender_name = get_optarg(argv, &arg, opt+1); |
493 if (!full_sender_name) { | 493 if (!full_sender_name) { |
494 fprintf(stderr, "-F requires a name argument\n"); | 494 fprintf(stderr, "-F requires a name argument\n"); |
495 exit(EXIT_FAILURE); | 495 exit(1); |
496 } | 496 } |
497 | 497 |
498 } else if (strcmp(opt, "i") == 0) { | 498 } else if (strcmp(opt, "i") == 0) { |
499 opt_i = TRUE; | 499 opt_i = TRUE; |
500 | 500 |
540 } else if (strcmp(opt, "v") == 0) { | 540 } else if (strcmp(opt, "v") == 0) { |
541 do_verbose = TRUE; | 541 do_verbose = TRUE; |
542 | 542 |
543 } else { | 543 } else { |
544 fprintf(stderr, "unrecognized option `-%s'\n", opt); | 544 fprintf(stderr, "unrecognized option `-%s'\n", opt); |
545 exit(EXIT_FAILURE); | 545 exit(1); |
546 } | 546 } |
547 } | 547 } |
548 | 548 |
549 if (!mta_mode) { | 549 if (!mta_mode) { |
550 mta_mode = (arg<argc || opt_t) ? MODE_ACCEPT : MODE_VERSION; | 550 mta_mode = (arg<argc || opt_t) ? MODE_ACCEPT : MODE_VERSION; |
551 } | 551 } |
552 | 552 |
553 if (mta_mode == MODE_VERSION) { | 553 if (mta_mode == MODE_VERSION) { |
554 mode_version(); | 554 mode_version(); |
555 exit(EXIT_SUCCESS); | 555 exit(0); |
556 } | 556 } |
557 | 557 |
558 /* initialize random generator */ | 558 /* initialize random generator */ |
559 srand(time(NULL)); | 559 srand(time(NULL)); |
560 /* ignore SIGPIPE signal */ | 560 /* ignore SIGPIPE signal */ |
617 chdir("/"); | 617 chdir("/"); |
618 | 618 |
619 if (!conf.run_as_user) { | 619 if (!conf.run_as_user) { |
620 if (setgid(0) != 0) { | 620 if (setgid(0) != 0) { |
621 fprintf(stderr, "could not set gid to 0. Is the setuid bit set? : %s\n", strerror(errno)); | 621 fprintf(stderr, "could not set gid to 0. Is the setuid bit set? : %s\n", strerror(errno)); |
622 exit(EXIT_FAILURE); | 622 exit(1); |
623 } | 623 } |
624 if (setuid(0) != 0) { | 624 if (setuid(0) != 0) { |
625 fprintf(stderr, "could not gain root privileges. Is the setuid bit set? : %s\n", strerror(errno)); | 625 fprintf(stderr, "could not gain root privileges. Is the setuid bit set? : %s\n", strerror(errno)); |
626 exit(EXIT_FAILURE); | 626 exit(1); |
627 } | 627 } |
628 } | 628 } |
629 | 629 |
630 if (!logopen()) { | 630 if (!logopen()) { |
631 fprintf(stderr, "could not open log file\n"); | 631 fprintf(stderr, "could not open log file\n"); |
632 exit(EXIT_FAILURE); | 632 exit(1); |
633 } | 633 } |
634 | 634 |
635 DEBUG(1) debugf("masqmail %s starting\n", VERSION); | 635 DEBUG(1) debugf("masqmail %s starting\n", VERSION); |
636 | 636 |
637 DEBUG(5) { | 637 DEBUG(5) { |
647 if (f_address) { | 647 if (f_address) { |
648 return_path = create_address_qualified(f_address, TRUE, conf.host_name); | 648 return_path = create_address_qualified(f_address, TRUE, conf.host_name); |
649 g_free(f_address); | 649 g_free(f_address); |
650 if (!return_path) { | 650 if (!return_path) { |
651 fprintf(stderr, "invalid RFC821 address: %s\n", f_address); | 651 fprintf(stderr, "invalid RFC821 address: %s\n", f_address); |
652 exit(EXIT_FAILURE); | 652 exit(1); |
653 } | 653 } |
654 } | 654 } |
655 | 655 |
656 switch (mta_mode) { | 656 switch (mta_mode) { |
657 case MODE_DAEMON: | 657 case MODE_DAEMON: |
669 case MODE_LIST: | 669 case MODE_LIST: |
670 queue_list(); | 670 queue_list(); |
671 break; | 671 break; |
672 | 672 |
673 case MODE_BI: | 673 case MODE_BI: |
674 exit(EXIT_SUCCESS); | 674 exit(0); |
675 break; /* well... */ | 675 break; /* well... */ |
676 | 676 |
677 case MODE_MCMD: | 677 case MODE_MCMD: |
678 exit(manipulate_queue(M_cmd, &argv[arg]) ? 0 : 1); | 678 exit(manipulate_queue(M_cmd, &argv[arg]) ? 0 : 1); |
679 break; | 679 break; |