masqmail

changeset 251:2babd21e7c75

moved run_queue to a new function plus various small cleanups, mostly cosmetic
author markus schnalke <meillo@marmaro.de>
date Thu, 04 Nov 2010 13:00:19 -0300
parents a41c013c8458
children 1b25fabdc3cb
files src/masqmail.c
diffstat 1 files changed, 54 insertions(+), 46 deletions(-) [+]
line diff
     1.1 --- a/src/masqmail.c	Thu Nov 04 12:32:11 2010 -0300
     1.2 +++ b/src/masqmail.c	Thu Nov 04 13:00:19 2010 -0300
     1.3 @@ -353,38 +353,53 @@
     1.4  	return ok;
     1.5  }
     1.6  
     1.7 +static int
     1.8 +run_queue(gboolean do_runq, gboolean do_runq_online, char* route_name)
     1.9 +{
    1.10 +	int ret;
    1.11 +
    1.12 +	/* queue runs */
    1.13 +	set_identity(conf.orig_uid, "queue run");
    1.14 +
    1.15 +	if (do_runq) {
    1.16 +		ret = queue_run();
    1.17 +	}
    1.18 +
    1.19 +	if (do_runq_online) {
    1.20 +		if (route_name) {
    1.21 +			conf.online_detect = g_strdup("argument");
    1.22 +			set_online_name(route_name);
    1.23 +		}
    1.24 +		ret = queue_run_online();
    1.25 +	}
    1.26 +	return ret;
    1.27 +}
    1.28 +
    1.29  int
    1.30  main(int argc, char *argv[])
    1.31  {
    1.32 -	/* cmd line flags */
    1.33 -	gchar *conf_file = CONF_FILE;
    1.34 +	gchar *progname;
    1.35  	char* opt;
    1.36  	gint arg;
    1.37  
    1.38 +	mta_mode mta_mode = MODE_ACCEPT;
    1.39  	gboolean do_listen = FALSE;
    1.40  	gboolean do_runq = FALSE;
    1.41  	gboolean do_runq_online = FALSE;
    1.42 -
    1.43  	gboolean do_queue = FALSE;
    1.44 -
    1.45 -	gboolean do_verbose = FALSE;
    1.46 -	gint debug_level = -1;
    1.47 -
    1.48 -	mta_mode mta_mode = MODE_ACCEPT;
    1.49 -
    1.50  	gint queue_interval = 0;
    1.51 +	gchar *M_cmd = NULL;
    1.52  	gboolean opt_t = FALSE;
    1.53  	gboolean opt_i = FALSE;
    1.54  	gboolean exit_failure = FALSE;
    1.55 -
    1.56 -	gchar *M_cmd = NULL;
    1.57 -
    1.58  	gint exit_code = EXIT_SUCCESS;
    1.59 +	gchar *conf_file = CONF_FILE;
    1.60  	gchar *route_name = NULL;
    1.61 -	gchar *progname;
    1.62  	gchar *f_address = NULL;
    1.63 +	address *return_path = NULL;  /* may be changed by -f option */
    1.64  	gchar *full_sender_name = NULL;
    1.65 -	address *return_path = NULL;  /* may be changed by -f option */
    1.66 +	gboolean do_verbose = FALSE;
    1.67 +	gint debug_level = -1;
    1.68  
    1.69  	progname = get_progname(argv[0]);
    1.70  
    1.71 @@ -408,7 +423,7 @@
    1.72  
    1.73  	/* parse cmd line */
    1.74  	for (arg=1; arg<argc && argv[arg][0]=='-'; arg++) {
    1.75 -		opt = argv[arg] + 1;
    1.76 +		opt = argv[arg] + 1;  /* points to the char after the dash */
    1.77  
    1.78  		if (strcmp(opt, "-") == 0) {
    1.79  			/* everything after `--' are address arguments */
    1.80 @@ -437,7 +452,8 @@
    1.81  			get_optarg(argv, &arg, opt+1);
    1.82  
    1.83  		} else if (strncmp(opt, "C", 1) == 0) {
    1.84 -			if (!(conf_file = get_optarg(argv, &arg, opt+1))) {
    1.85 +			conf_file = get_optarg(argv, &arg, opt+1);
    1.86 +			if (!conf_file) {
    1.87  				fprintf(stderr, "-C requires a filename as argument.\n");
    1.88  				exit(EXIT_FAILURE);
    1.89  			}
    1.90 @@ -510,7 +526,9 @@
    1.91  
    1.92  			do_runq = TRUE;
    1.93  			mta_mode = MODE_RUNQUEUE;
    1.94 -			if ((optarg = get_optarg(argv, &arg, opt+1))) {
    1.95 +			optarg = get_optarg(argv, &arg, opt+1);
    1.96 +			if (optarg) {
    1.97 +				/* not just one single queue run but regular runs */
    1.98  				mta_mode = MODE_DAEMON;
    1.99  				queue_interval = time_interval(optarg, &dummy);
   1.100  			}
   1.101 @@ -556,10 +574,12 @@
   1.102  	{
   1.103  		int i, max_fd = sysconf(_SC_OPEN_MAX);
   1.104  
   1.105 -		if (max_fd <= 0)
   1.106 +		if (max_fd <= 0) {
   1.107  			max_fd = 64;
   1.108 -		for (i = 3; i < max_fd; i++)
   1.109 +		}
   1.110 +		for (i=3; i<max_fd; i++) {
   1.111  			close(i);
   1.112 +		}
   1.113  	}
   1.114  
   1.115  	init_conf();
   1.116 @@ -571,14 +591,12 @@
   1.117  	   So it is possible for a user to run his own daemon without
   1.118  	   breaking security.
   1.119  	 */
   1.120 -	if (strcmp(conf_file, CONF_FILE) != 0) {
   1.121 -		if (conf.orig_uid != 0) {
   1.122 -			conf.run_as_user = TRUE;
   1.123 -			seteuid(conf.orig_uid);
   1.124 -			setegid(conf.orig_gid);
   1.125 -			setuid(conf.orig_uid);
   1.126 -			setgid(conf.orig_gid);
   1.127 -		}
   1.128 +	if ((strcmp(conf_file, CONF_FILE) != 0) && (conf.orig_uid != 0)) {
   1.129 +		conf.run_as_user = TRUE;
   1.130 +		seteuid(conf.orig_uid);
   1.131 +		setegid(conf.orig_gid);
   1.132 +		setuid(conf.orig_uid);
   1.133 +		setgid(conf.orig_gid);
   1.134  	}
   1.135  
   1.136  	conf.log_dir = LOG_DIR;
   1.137 @@ -589,12 +607,15 @@
   1.138  	}
   1.139  	logclose();
   1.140  
   1.141 -	if (do_queue)
   1.142 +	if (do_queue) {
   1.143  		conf.do_queue = TRUE;
   1.144 -	if (do_verbose)
   1.145 +	}
   1.146 +	if (do_verbose) {
   1.147  		conf.do_verbose = TRUE;
   1.148 -	if (debug_level >= 0)  /* if >= 0, it was given by argument */
   1.149 +	}
   1.150 +	if (debug_level >= 0) {  /* if >= 0, it was given by argument */
   1.151  		conf.debug_level = debug_level;
   1.152 +	}
   1.153  
   1.154  	/* It appears that changing to / ensures that we are never in
   1.155  	   a directory which we cannot access. This situation could be
   1.156 @@ -646,23 +667,9 @@
   1.157  	case MODE_DAEMON:
   1.158  		mode_daemon(do_listen, queue_interval, argv);
   1.159  		break;
   1.160 +
   1.161  	case MODE_RUNQUEUE:
   1.162 -		{
   1.163 -			/* queue runs */
   1.164 -			set_identity(conf.orig_uid, "queue run");
   1.165 -
   1.166 -			if (do_runq)
   1.167 -				exit_code = queue_run() ? EXIT_SUCCESS : EXIT_FAILURE;
   1.168 -
   1.169 -			if (do_runq_online) {
   1.170 -				if (route_name != NULL) {
   1.171 -					conf.online_detect = g_strdup("argument");
   1.172 -					set_online_name(route_name);
   1.173 -				}
   1.174 -				exit_code =
   1.175 -					queue_run_online() ? EXIT_SUCCESS : EXIT_FAILURE;
   1.176 -			}
   1.177 -		}
   1.178 +		exit(run_queue(do_runq, do_runq_online, route_name) ? 0 : 1);
   1.179  		break;
   1.180  
   1.181  	case MODE_SMTP:
   1.182 @@ -691,6 +698,7 @@
   1.183  		break;
   1.184  	case MODE_NONE:
   1.185  		break;
   1.186 +
   1.187  	default:
   1.188  		fprintf(stderr, "unknown mode: %d\n", mta_mode);
   1.189  		break;