masqmail

diff src/masqmail.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children 9fb7ddbaf129
line diff
     1.1 --- a/src/masqmail.c	Mon Oct 27 16:21:27 2008 +0100
     1.2 +++ b/src/masqmail.c	Mon Oct 27 16:23:10 2008 +0100
     1.3 @@ -37,792 +37,783 @@
     1.4     nor a 'queue daemon' mode. These, as well as the distinction beween
     1.5     the two (non exclusive) daemon (queue and listen) modes are handled
     1.6     by flags.*/
     1.7 -typedef enum _mta_mode
     1.8 -{
     1.9 -  MODE_ACCEPT = 0, /* accept message on stdin */
    1.10 -  MODE_DAEMON,     /* run as daemon */
    1.11 -  MODE_RUNQUEUE,   /* single queue run, online or offline */
    1.12 -  MODE_GET_DAEMON, /* run as get (retrieve) daemon */
    1.13 -  MODE_SMTP,       /* accept SMTP on stdin */
    1.14 -  MODE_LIST,       /* list queue */
    1.15 -  MODE_MCMD,       /* do queue manipulation */
    1.16 -  MODE_VERSION,    /* show version */
    1.17 -  MODE_BI,         /* fake ;-) */
    1.18 -  MODE_NONE        /* to prevent default MODE_ACCEPT */    
    1.19 -}mta_mode;
    1.20 +typedef enum _mta_mode {
    1.21 +	MODE_ACCEPT = 0,  /* accept message on stdin */
    1.22 +	MODE_DAEMON,  /* run as daemon */
    1.23 +	MODE_RUNQUEUE,  /* single queue run, online or offline */
    1.24 +	MODE_GET_DAEMON,  /* run as get (retrieve) daemon */
    1.25 +	MODE_SMTP,  /* accept SMTP on stdin */
    1.26 +	MODE_LIST,  /* list queue */
    1.27 +	MODE_MCMD,  /* do queue manipulation */
    1.28 +	MODE_VERSION,  /* show version */
    1.29 +	MODE_BI,  /* fake ;-) */
    1.30 +	MODE_NONE  /* to prevent default MODE_ACCEPT */
    1.31 +} mta_mode;
    1.32  
    1.33  char *pidfile = NULL;
    1.34  volatile int sigterm_in_progress = 0;
    1.35  
    1.36 -static
    1.37 -void sigterm_handler(int sig)
    1.38 +static void
    1.39 +sigterm_handler(int sig)
    1.40  {
    1.41 -  if(sigterm_in_progress)
    1.42 -    raise(sig);
    1.43 -  sigterm_in_progress = 1;
    1.44 +	if (sigterm_in_progress)
    1.45 +		raise(sig);
    1.46 +	sigterm_in_progress = 1;
    1.47  
    1.48 -  if(pidfile){
    1.49 -    uid_t uid;
    1.50 -    uid = seteuid(0);
    1.51 -    if(unlink(pidfile) != 0)
    1.52 -      logwrite(LOG_WARNING, "could not delete pid file %s: %s\n",
    1.53 -	       pidfile, strerror(errno));
    1.54 -    seteuid(uid); /* we exit anyway after this, just to be sure */
    1.55 -  }
    1.56 +	if (pidfile) {
    1.57 +		uid_t uid;
    1.58 +		uid = seteuid(0);
    1.59 +		if (unlink(pidfile) != 0)
    1.60 +			logwrite(LOG_WARNING, "could not delete pid file %s: %s\n", pidfile, strerror(errno));
    1.61 +		seteuid(uid);  /* we exit anyway after this, just to be sure */
    1.62 +	}
    1.63  
    1.64 -  signal(sig, SIG_DFL);
    1.65 -  raise(sig);
    1.66 +	signal(sig, SIG_DFL);
    1.67 +	raise(sig);
    1.68  }
    1.69  
    1.70 -#ifdef ENABLE_IDENT /* so far used for that only */
    1.71 -static
    1.72 -gboolean is_in_netlist(gchar *host, GList *netlist)
    1.73 +#ifdef ENABLE_IDENT  /* so far used for that only */
    1.74 +static gboolean
    1.75 +is_in_netlist(gchar * host, GList * netlist)
    1.76  {
    1.77 -  guint hostip = inet_addr(host);
    1.78 -  struct in_addr addr;
    1.79 +	guint hostip = inet_addr(host);
    1.80 +	struct in_addr addr;
    1.81  
    1.82 -  addr.s_addr = hostip;
    1.83 -  if(addr.s_addr != INADDR_NONE){
    1.84 -    GList *node;
    1.85 -    foreach(netlist, node){
    1.86 -      struct in_addr *net = (struct in_addr *)(node->data);
    1.87 -      if((addr.s_addr & net->s_addr) == net->s_addr)
    1.88 -	return TRUE;
    1.89 -    }
    1.90 -  }
    1.91 -  return FALSE;
    1.92 +	addr.s_addr = hostip;
    1.93 +	if (addr.s_addr != INADDR_NONE) {
    1.94 +		GList *node;
    1.95 +		foreach(netlist, node) {
    1.96 +			struct in_addr *net = (struct in_addr *) (node->data);
    1.97 +			if ((addr.s_addr & net->s_addr) == net->s_addr)
    1.98 +				return TRUE;
    1.99 +		}
   1.100 +	}
   1.101 +	return FALSE;
   1.102  }
   1.103  #endif
   1.104  
   1.105 -gchar *get_optarg(char *argv[], gint argc, gint *argp, gint *pos)
   1.106 +gchar*
   1.107 +get_optarg(char *argv[], gint argc, gint * argp, gint * pos)
   1.108  {
   1.109 -  if(argv[*argp][*pos])
   1.110 -    return &(argv[*argp][*pos]);
   1.111 -  else{
   1.112 -    if(*argp+1 < argc){
   1.113 -      if(argv[(*argp)+1][0] != '-'){
   1.114 -	(*argp)++;
   1.115 -	*pos = 0;
   1.116 -	return &(argv[*argp][*pos]);
   1.117 -      }
   1.118 -    }
   1.119 -  }
   1.120 -  return NULL;
   1.121 -}  
   1.122 -
   1.123 -gchar *get_progname(gchar *arg0)
   1.124 -{
   1.125 -  gchar *p = arg0 + strlen(arg0) - 1;
   1.126 -  while(p > arg0){
   1.127 -    if(*p == '/')
   1.128 -      return p+1;
   1.129 -    p--;
   1.130 -  }
   1.131 -  return p;
   1.132 +	if (argv[*argp][*pos])
   1.133 +		return &(argv[*argp][*pos]);
   1.134 +	else {
   1.135 +		if (*argp + 1 < argc) {
   1.136 +			if (argv[(*argp) + 1][0] != '-') {
   1.137 +				(*argp)++;
   1.138 +				*pos = 0;
   1.139 +				return &(argv[*argp][*pos]);
   1.140 +			}
   1.141 +		}
   1.142 +	}
   1.143 +	return NULL;
   1.144  }
   1.145  
   1.146 -gboolean write_pidfile(gchar *name)
   1.147 +gchar*
   1.148 +get_progname(gchar * arg0)
   1.149  {
   1.150 -  FILE *fptr;
   1.151 -
   1.152 -  if((fptr = fopen(name, "wt"))){
   1.153 -    fprintf(fptr, "%d\n", getpid());
   1.154 -    fclose(fptr);
   1.155 -    pidfile = strdup(name);
   1.156 -    return TRUE;
   1.157 -  }
   1.158 -  logwrite(LOG_WARNING, "could not write pid file: %s\n", strerror(errno));
   1.159 -  return FALSE;
   1.160 +	gchar *p = arg0 + strlen(arg0) - 1;
   1.161 +	while (p > arg0) {
   1.162 +		if (*p == '/')
   1.163 +			return p + 1;
   1.164 +		p--;
   1.165 +	}
   1.166 +	return p;
   1.167  }
   1.168  
   1.169 -static
   1.170 -void mode_daemon(gboolean do_listen, gint queue_interval, char *argv[])
   1.171 +gboolean
   1.172 +write_pidfile(gchar * name)
   1.173  {
   1.174 -  guint pid;
   1.175 +	FILE *fptr;
   1.176  
   1.177 -  /* daemon */
   1.178 -  if(!conf.run_as_user){
   1.179 -    if((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)){
   1.180 -      fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER);
   1.181 -      exit(EXIT_FAILURE);
   1.182 -    }
   1.183 -  }
   1.184 +	if ((fptr = fopen(name, "wt"))) {
   1.185 +		fprintf(fptr, "%d\n", getpid());
   1.186 +		fclose(fptr);
   1.187 +		pidfile = strdup(name);
   1.188 +		return TRUE;
   1.189 +	}
   1.190 +	logwrite(LOG_WARNING, "could not write pid file: %s\n", strerror(errno));
   1.191 +	return FALSE;
   1.192 +}
   1.193  
   1.194 -  if((pid = fork()) > 0){
   1.195 -    exit(EXIT_SUCCESS);
   1.196 -  }else if(pid < 0){
   1.197 -    logwrite(LOG_ALERT, "could not fork!");
   1.198 -    exit(EXIT_FAILURE);
   1.199 -  }
   1.200 +static void
   1.201 +mode_daemon(gboolean do_listen, gint queue_interval, char *argv[])
   1.202 +{
   1.203 +	guint pid;
   1.204  
   1.205 -  signal(SIGTERM, sigterm_handler);
   1.206 -  write_pidfile(PIDFILEDIR"/masqmail.pid");
   1.207 +	/* daemon */
   1.208 +	if (!conf.run_as_user) {
   1.209 +		if ((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)) {
   1.210 +			fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER);
   1.211 +			exit(EXIT_FAILURE);
   1.212 +		}
   1.213 +	}
   1.214  
   1.215 -  conf.do_verbose = FALSE;
   1.216 +	if ((pid = fork()) > 0) {
   1.217 +		exit(EXIT_SUCCESS);
   1.218 +	} else if (pid < 0) {
   1.219 +		logwrite(LOG_ALERT, "could not fork!");
   1.220 +		exit(EXIT_FAILURE);
   1.221 +	}
   1.222  
   1.223 -  fclose(stdin);
   1.224 -  fclose(stdout);
   1.225 -  fclose(stderr);
   1.226 +	signal(SIGTERM, sigterm_handler);
   1.227 +	write_pidfile(PIDFILEDIR "/masqmail.pid");
   1.228  
   1.229 -  listen_port(do_listen ? conf.listen_addresses : NULL,
   1.230 -	      queue_interval, argv);
   1.231 +	conf.do_verbose = FALSE;
   1.232 +
   1.233 +	fclose(stdin);
   1.234 +	fclose(stdout);
   1.235 +	fclose(stderr);
   1.236 +
   1.237 +	listen_port(do_listen ? conf.listen_addresses : NULL, queue_interval, argv);
   1.238  }
   1.239  
   1.240  #ifdef ENABLE_POP3
   1.241 -static
   1.242 -void mode_get_daemon(gint get_interval, char *argv[])
   1.243 +static void
   1.244 +mode_get_daemon(gint get_interval, char *argv[])
   1.245  {
   1.246 -  guint pid;
   1.247 +	guint pid;
   1.248  
   1.249 -  /* daemon */
   1.250 -  if(!conf.run_as_user){
   1.251 -    if((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)){
   1.252 -      fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER);
   1.253 -      exit(EXIT_FAILURE);
   1.254 -    }
   1.255 -  }
   1.256 +	/* daemon */
   1.257 +	if (!conf.run_as_user) {
   1.258 +		if ((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)) {
   1.259 +			fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER);
   1.260 +			exit(EXIT_FAILURE);
   1.261 +		}
   1.262 +	}
   1.263  
   1.264 -  if((pid = fork()) > 0){
   1.265 -    exit(EXIT_SUCCESS);
   1.266 -  }else if(pid < 0){
   1.267 -    logwrite(LOG_ALERT, "could not fork!");
   1.268 -    exit(EXIT_FAILURE);
   1.269 -  }
   1.270 +	if ((pid = fork()) > 0) {
   1.271 +		exit(EXIT_SUCCESS);
   1.272 +	} else if (pid < 0) {
   1.273 +		logwrite(LOG_ALERT, "could not fork!");
   1.274 +		exit(EXIT_FAILURE);
   1.275 +	}
   1.276  
   1.277 -  signal(SIGTERM, sigterm_handler);
   1.278 -  write_pidfile(PIDFILEDIR"/masqmail-get.pid");
   1.279 +	signal(SIGTERM, sigterm_handler);
   1.280 +	write_pidfile(PIDFILEDIR "/masqmail-get.pid");
   1.281  
   1.282 -  conf.do_verbose = FALSE;
   1.283 +	conf.do_verbose = FALSE;
   1.284  
   1.285 -  fclose(stdin);
   1.286 -  fclose(stdout);
   1.287 -  fclose(stderr);
   1.288 +	fclose(stdin);
   1.289 +	fclose(stdout);
   1.290 +	fclose(stderr);
   1.291  
   1.292 -  get_daemon(get_interval, argv);
   1.293 +	get_daemon(get_interval, argv);
   1.294  }
   1.295  #endif
   1.296  
   1.297  #ifdef ENABLE_SMTP_SERVER
   1.298 -static void mode_smtp()
   1.299 +static void
   1.300 +mode_smtp()
   1.301  {
   1.302 -  /* accept smtp message on stdin */
   1.303 -  /* write responses to stderr. */
   1.304 +	/* accept smtp message on stdin */
   1.305 +	/* write responses to stderr. */
   1.306  
   1.307 -  struct sockaddr_in saddr;
   1.308 -  gchar *peername = NULL;
   1.309 -  int dummy = sizeof(saddr);
   1.310 +	struct sockaddr_in saddr;
   1.311 +	gchar *peername = NULL;
   1.312 +	int dummy = sizeof(saddr);
   1.313  #ifdef ENABLE_IDENT
   1.314 -  gchar *ident = NULL;
   1.315 +	gchar *ident = NULL;
   1.316  #endif
   1.317  
   1.318 -  conf.do_verbose = FALSE;
   1.319 +	conf.do_verbose = FALSE;
   1.320  
   1.321 -  if(!conf.run_as_user){
   1.322 -    seteuid(conf.orig_uid);
   1.323 -    setegid(conf.orig_gid);
   1.324 -  }
   1.325 +	if (!conf.run_as_user) {
   1.326 +		seteuid(conf.orig_uid);
   1.327 +		setegid(conf.orig_gid);
   1.328 +	}
   1.329  
   1.330 -  DEBUG(5) debugf("accepting smtp message on stdin\n");
   1.331 +	DEBUG(5) debugf("accepting smtp message on stdin\n");
   1.332  
   1.333 -  if(getpeername(0, (struct sockaddr *)(&saddr), &dummy) == 0){
   1.334 -    peername = g_strdup(inet_ntoa(saddr.sin_addr));
   1.335 +	if (getpeername(0, (struct sockaddr *) (&saddr), &dummy) == 0) {
   1.336 +		peername = g_strdup(inet_ntoa(saddr.sin_addr));
   1.337  #ifdef ENABLE_IDENT
   1.338 -    {
   1.339 -      gchar *id = NULL;
   1.340 -      if((id = (gchar *)ident_id(0, 60))){
   1.341 -	ident = g_strdup(id);
   1.342 -      }
   1.343 -    }
   1.344 +		{
   1.345 +			gchar *id = NULL;
   1.346 +			if ((id = (gchar *) ident_id(0, 60))) {
   1.347 +				ident = g_strdup(id);
   1.348 +			}
   1.349 +		}
   1.350  #endif
   1.351 -  }else if(errno != ENOTSOCK)
   1.352 -    exit(EXIT_FAILURE);
   1.353 +	} else if (errno != ENOTSOCK)
   1.354 +		exit(EXIT_FAILURE);
   1.355  
   1.356 -  //smtp_in(stdin, stdout, peername);
   1.357 -  smtp_in(stdin, stderr, peername, NULL);
   1.358 +	//smtp_in(stdin, stdout, peername);
   1.359 +	smtp_in(stdin, stderr, peername, NULL);
   1.360  
   1.361  #ifdef ENABLE_IDENT
   1.362 -  if(ident) g_free(ident);
   1.363 +	if (ident)
   1.364 +		g_free(ident);
   1.365  #endif
   1.366  }
   1.367  #endif
   1.368  
   1.369 -static void mode_accept(address *return_path, gchar *full_sender_name,
   1.370 -			guint accept_flags, char **addresses, int addr_cnt)
   1.371 +static void
   1.372 +mode_accept(address * return_path, gchar * full_sender_name, guint accept_flags, char **addresses, int addr_cnt)
   1.373  {
   1.374 -  /* accept message on stdin */
   1.375 -  accept_error err;
   1.376 -  message *msg = create_message();
   1.377 -  gint i;
   1.378 +	/* accept message on stdin */
   1.379 +	accept_error err;
   1.380 +	message *msg = create_message();
   1.381 +	gint i;
   1.382  
   1.383 -  if(return_path != NULL){
   1.384 -    if((conf.orig_uid != 0) &&
   1.385 -       (conf.orig_uid != conf.mail_uid) &&
   1.386 -       (!is_ingroup(conf.orig_uid, conf.mail_gid))){
   1.387 -      fprintf(stderr,
   1.388 -	      "must be in root, %s or in group %s for setting return path.\n",
   1.389 -	      DEF_MAIL_USER, DEF_MAIL_GROUP);
   1.390 -      exit(EXIT_FAILURE);
   1.391 -    }
   1.392 -  }
   1.393 +	if (return_path != NULL) {
   1.394 +		if ((conf.orig_uid != 0)
   1.395 +		    && (conf.orig_uid != conf.mail_uid)
   1.396 +		    && (!is_ingroup(conf.orig_uid, conf.mail_gid))) {
   1.397 +			fprintf(stderr, "must be in root, %s or in group %s for setting return path.\n", DEF_MAIL_USER, DEF_MAIL_GROUP);
   1.398 +			exit(EXIT_FAILURE);
   1.399 +		}
   1.400 +	}
   1.401  
   1.402 -  if(!conf.run_as_user){
   1.403 -    seteuid(conf.orig_uid);
   1.404 -    setegid(conf.orig_gid);
   1.405 -  }
   1.406 +	if (!conf.run_as_user) {
   1.407 +		seteuid(conf.orig_uid);
   1.408 +		setegid(conf.orig_gid);
   1.409 +	}
   1.410  
   1.411 -  DEBUG(5) debugf("accepting message on stdin\n");
   1.412 +	DEBUG(5) debugf("accepting message on stdin\n");
   1.413  
   1.414 -  msg->received_prot = PROT_LOCAL;
   1.415 -  for(i = 0; i < addr_cnt; i++){
   1.416 -    if(addresses[i][0] != '|')
   1.417 -      msg->rcpt_list =
   1.418 -	g_list_append(msg->rcpt_list,
   1.419 -		      create_address_qualified(addresses[i], TRUE, conf.host_name));
   1.420 -    else{
   1.421 -      logwrite(LOG_ALERT, "no pipe allowed as recipient address: %s\n", addresses[i]);
   1.422 -      exit(EXIT_FAILURE);
   1.423 -    }
   1.424 -  }
   1.425 +	msg->received_prot = PROT_LOCAL;
   1.426 +	for (i = 0; i < addr_cnt; i++) {
   1.427 +		if (addresses[i][0] != '|')
   1.428 +			msg->rcpt_list = g_list_append(msg->rcpt_list, create_address_qualified(addresses[i], TRUE, conf.host_name));
   1.429 +		else {
   1.430 +			logwrite(LOG_ALERT, "no pipe allowed as recipient address: %s\n", addresses[i]);
   1.431 +			exit(EXIT_FAILURE);
   1.432 +		}
   1.433 +	}
   1.434  
   1.435 -  /* -f option */
   1.436 -  msg->return_path = return_path;
   1.437 +	/* -f option */
   1.438 +	msg->return_path = return_path;
   1.439  
   1.440 -  /* -F option */
   1.441 -  msg->full_sender_name = full_sender_name;
   1.442 -    
   1.443 -  if((err = accept_message(stdin, msg, accept_flags)) == AERR_OK){
   1.444 -    if(spool_write(msg, TRUE)){
   1.445 -      pid_t pid;
   1.446 -      logwrite(LOG_NOTICE, "%s <= %s with %s\n",
   1.447 -	       msg->uid, addr_string(msg->return_path),
   1.448 -	       prot_names[PROT_LOCAL]);
   1.449 +	/* -F option */
   1.450 +	msg->full_sender_name = full_sender_name;
   1.451  
   1.452 -      if(!conf.do_queue){
   1.453 +	if ((err = accept_message(stdin, msg, accept_flags)) == AERR_OK) {
   1.454 +		if (spool_write(msg, TRUE)) {
   1.455 +			pid_t pid;
   1.456 +			logwrite(LOG_NOTICE, "%s <= %s with %s\n", msg->uid, addr_string(msg->return_path), prot_names[PROT_LOCAL]);
   1.457  
   1.458 -	if((pid = fork()) == 0){
   1.459 -
   1.460 -	  conf.do_verbose = FALSE;
   1.461 -
   1.462 -	  fclose(stdin);
   1.463 -	  fclose(stdout);
   1.464 -	  fclose(stderr);
   1.465 -
   1.466 -	  if(deliver(msg)){
   1.467 -	    exit(EXIT_SUCCESS);
   1.468 -	  }else
   1.469 -	    exit(EXIT_FAILURE);
   1.470 -	}else if(pid < 0){
   1.471 -	  logwrite(LOG_ALERT, "could not fork for delivery, id = %s",
   1.472 -		   msg->uid);
   1.473 +			if (!conf.do_queue) {
   1.474 +				if ((pid = fork()) == 0) {
   1.475 +					conf.do_verbose = FALSE;
   1.476 +					fclose(stdin);
   1.477 +					fclose(stdout);
   1.478 +					fclose(stderr);
   1.479 +					if (deliver(msg)) {
   1.480 +						exit(EXIT_SUCCESS);
   1.481 +					} else
   1.482 +						exit(EXIT_FAILURE);
   1.483 +				} else if (pid < 0) {
   1.484 +					logwrite(LOG_ALERT, "could not fork for delivery, id = %s", msg->uid);
   1.485 +				}
   1.486 +			}
   1.487 +		} else {
   1.488 +			fprintf(stderr, "Could not write spool file\n");
   1.489 +			exit(EXIT_FAILURE);
   1.490 +		}
   1.491 +	} else {
   1.492 +		switch (err) {
   1.493 +		case AERR_EOF:
   1.494 +			fprintf(stderr, "unexpected EOF.\n");
   1.495 +			exit(EXIT_FAILURE);
   1.496 +		case AERR_NORCPT:
   1.497 +			fprintf(stderr, "no recipients.\n");
   1.498 +			exit(EXIT_FAILURE);
   1.499 +		default:
   1.500 +			/* should never happen: */
   1.501 +			fprintf(stderr, "Unknown error (%d)\r\n", err);
   1.502 +			exit(EXIT_FAILURE);
   1.503 +		}
   1.504 +		exit(EXIT_FAILURE);
   1.505  	}
   1.506 -      }
   1.507 -    }else{
   1.508 -      fprintf(stderr, "Could not write spool file\n");
   1.509 -      exit(EXIT_FAILURE);
   1.510 -    }
   1.511 -  }else{
   1.512 -    switch(err){
   1.513 -    case AERR_EOF:
   1.514 -      fprintf(stderr, "unexpected EOF.\n");
   1.515 -      exit(EXIT_FAILURE);
   1.516 -    case AERR_NORCPT:
   1.517 -      fprintf(stderr, "no recipients.\n");
   1.518 -      exit(EXIT_FAILURE);
   1.519 -    default:
   1.520 -      /* should never happen: */
   1.521 -      fprintf(stderr, "Unknown error (%d)\r\n", err);
   1.522 -      exit(EXIT_FAILURE);
   1.523 -    }
   1.524 -    exit(EXIT_FAILURE);
   1.525 -  }
   1.526  }
   1.527  
   1.528  int
   1.529  main(int argc, char *argv[])
   1.530  {
   1.531 -  /* cmd line flags */
   1.532 -  gchar *conf_file = CONF_FILE;
   1.533 -  gint arg = 1;
   1.534 -  gboolean do_get = FALSE;
   1.535 -  gboolean do_get_online = FALSE;
   1.536 +	/* cmd line flags */
   1.537 +	gchar *conf_file = CONF_FILE;
   1.538 +	gint arg = 1;
   1.539 +	gboolean do_get = FALSE;
   1.540 +	gboolean do_get_online = FALSE;
   1.541  
   1.542 -  gboolean do_listen = FALSE;
   1.543 -  gboolean do_runq = FALSE;
   1.544 -  gboolean do_runq_online = FALSE;
   1.545 +	gboolean do_listen = FALSE;
   1.546 +	gboolean do_runq = FALSE;
   1.547 +	gboolean do_runq_online = FALSE;
   1.548  
   1.549 -  gboolean do_queue = FALSE;
   1.550 +	gboolean do_queue = FALSE;
   1.551  
   1.552 -  gboolean do_verbose = FALSE;
   1.553 -  gint debug_level = -1;
   1.554 +	gboolean do_verbose = FALSE;
   1.555 +	gint debug_level = -1;
   1.556  
   1.557 -  mta_mode mta_mode = MODE_ACCEPT;
   1.558 +	mta_mode mta_mode = MODE_ACCEPT;
   1.559  
   1.560 -  gint queue_interval = 0;
   1.561 -  gint get_interval = 0;
   1.562 -  gboolean opt_t = FALSE;
   1.563 -  gboolean opt_i = FALSE;
   1.564 -  gboolean opt_odb = FALSE;
   1.565 -  gboolean opt_oem = FALSE;
   1.566 -  gboolean exit_failure = FALSE;
   1.567 +	gint queue_interval = 0;
   1.568 +	gint get_interval = 0;
   1.569 +	gboolean opt_t = FALSE;
   1.570 +	gboolean opt_i = FALSE;
   1.571 +	gboolean opt_odb = FALSE;
   1.572 +	gboolean opt_oem = FALSE;
   1.573 +	gboolean exit_failure = FALSE;
   1.574  
   1.575 -  gchar *M_cmd = NULL;
   1.576 +	gchar *M_cmd = NULL;
   1.577  
   1.578 -  gint exit_code = EXIT_SUCCESS;
   1.579 -  gchar *route_name = NULL;
   1.580 -  gchar *get_name = NULL;
   1.581 -  gchar *progname;
   1.582 -  gchar *f_address = NULL;
   1.583 -  gchar *full_sender_name = NULL;
   1.584 -  address *return_path = NULL; /* may be changed by -f option */
   1.585 +	gint exit_code = EXIT_SUCCESS;
   1.586 +	gchar *route_name = NULL;
   1.587 +	gchar *get_name = NULL;
   1.588 +	gchar *progname;
   1.589 +	gchar *f_address = NULL;
   1.590 +	gchar *full_sender_name = NULL;
   1.591 +	address *return_path = NULL;  /* may be changed by -f option */
   1.592  
   1.593 -  progname = get_progname(argv[0]);
   1.594 +	progname = get_progname(argv[0]);
   1.595  
   1.596 -  if(strcmp(progname, "mailq") == 0)
   1.597 -    { mta_mode = MODE_LIST; }
   1.598 -  else if(strcmp(progname, "mailrm") == 0)
   1.599 -    { mta_mode = MODE_MCMD; M_cmd = "rm"; }
   1.600 -  else if(strcmp(progname, "runq") == 0)
   1.601 -    { mta_mode = MODE_RUNQUEUE; do_runq = TRUE; }
   1.602 -  else if(strcmp(progname, "rmail") == 0)
   1.603 -    { mta_mode = MODE_ACCEPT; opt_i = TRUE; }
   1.604 -  else if(strcmp(progname, "smtpd") == 0 || strcmp(progname, "in.smtpd") == 0)
   1.605 -    { mta_mode = MODE_SMTP; }
   1.606 +	if (strcmp(progname, "mailq") == 0) {
   1.607 +		mta_mode = MODE_LIST;
   1.608 +	} else if (strcmp(progname, "mailrm") == 0) {
   1.609 +		mta_mode = MODE_MCMD;
   1.610 +		M_cmd = "rm";
   1.611 +	} else if (strcmp(progname, "runq") == 0) {
   1.612 +		mta_mode = MODE_RUNQUEUE;
   1.613 +		do_runq = TRUE;
   1.614 +	} else if (strcmp(progname, "rmail") == 0) {
   1.615 +		mta_mode = MODE_ACCEPT;
   1.616 +		opt_i = TRUE;
   1.617 +	} else if (strcmp(progname, "smtpd") == 0 || strcmp(progname, "in.smtpd") == 0) {
   1.618 +		mta_mode = MODE_SMTP;
   1.619 +	}
   1.620  
   1.621 -  /* parse cmd line */
   1.622 -  while(arg < argc){
   1.623 -    gint pos = 0;
   1.624 -    if((argv[arg][pos] == '-') && (argv[arg][pos+1] != '-')){
   1.625 -      pos++;
   1.626 -      switch(argv[arg][pos++]){
   1.627 -      case 'b':
   1.628 -	switch(argv[arg][pos++]){
   1.629 -	case 'd':
   1.630 -	  do_listen = TRUE;
   1.631 -	  mta_mode = MODE_DAEMON;
   1.632 -	  break;
   1.633 -	case 'i':
   1.634 -	  /* ignored */
   1.635 -	  mta_mode = MODE_BI;
   1.636 -	  break;
   1.637 -	case 's':
   1.638 -	  mta_mode = MODE_SMTP;
   1.639 -	  break;
   1.640 -	case 'p':
   1.641 -	  mta_mode = MODE_LIST;
   1.642 -	  break;
   1.643 -	case 'V':
   1.644 -	  mta_mode = MODE_VERSION;
   1.645 -	  break;
   1.646 -	default:
   1.647 -	  fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
   1.648 -	  exit(EXIT_FAILURE);
   1.649 +	/* parse cmd line */
   1.650 +	while (arg < argc) {
   1.651 +		gint pos = 0;
   1.652 +		if ((argv[arg][pos] == '-') && (argv[arg][pos + 1] != '-')) {
   1.653 +			pos++;
   1.654 +			switch (argv[arg][pos++]) {
   1.655 +			case 'b':
   1.656 +				switch (argv[arg][pos++]) {
   1.657 +				case 'd':
   1.658 +					do_listen = TRUE;
   1.659 +					mta_mode = MODE_DAEMON;
   1.660 +					break;
   1.661 +				case 'i':
   1.662 +					/* ignored */
   1.663 +					mta_mode = MODE_BI;
   1.664 +					break;
   1.665 +				case 's':
   1.666 +					mta_mode = MODE_SMTP;
   1.667 +					break;
   1.668 +				case 'p':
   1.669 +					mta_mode = MODE_LIST;
   1.670 +					break;
   1.671 +				case 'V':
   1.672 +					mta_mode = MODE_VERSION;
   1.673 +					break;
   1.674 +				default:
   1.675 +					fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
   1.676 +					exit(EXIT_FAILURE);
   1.677 +				}
   1.678 +				break;
   1.679 +			case 'B':
   1.680 +				/* we ignore this and throw the argument away */
   1.681 +				get_optarg(argv, argc, &arg, &pos);
   1.682 +				break;
   1.683 +			case 'C':
   1.684 +				if (!(conf_file = get_optarg(argv, argc, &arg, &pos))) {
   1.685 +					fprintf(stderr, "-C requires a filename as argument.\n");
   1.686 +					exit(EXIT_FAILURE);
   1.687 +				}
   1.688 +				break;
   1.689 +			case 'F':
   1.690 +				{
   1.691 +					full_sender_name = get_optarg(argv, argc, &arg, &pos);
   1.692 +					if (!full_sender_name) {
   1.693 +						fprintf(stderr, "-F requires a name as an argument\n");
   1.694 +						exit(EXIT_FAILURE);
   1.695 +					}
   1.696 +				}
   1.697 +				break;
   1.698 +			case 'd':
   1.699 +				if (getuid() == 0) {
   1.700 +					char *lvl = get_optarg(argv, argc, &arg, &pos);
   1.701 +					if (lvl)
   1.702 +						debug_level = atoi(lvl);
   1.703 +					else {
   1.704 +						fprintf(stderr, "-d requires a number as an argument.\n");
   1.705 +						exit(EXIT_FAILURE);
   1.706 +					}
   1.707 +				} else {
   1.708 +					fprintf(stderr, "only root may set the debug level.\n");
   1.709 +					exit(EXIT_FAILURE);
   1.710 +				}
   1.711 +				break;
   1.712 +			case 'f':
   1.713 +				/* set return path */
   1.714 +				{
   1.715 +					gchar *address;
   1.716 +					address = get_optarg(argv, argc, &arg, &pos);
   1.717 +					if (address) {
   1.718 +						f_address = g_strdup(address);
   1.719 +					} else {
   1.720 +						fprintf(stderr, "-f requires an address as an argument\n");
   1.721 +						exit(EXIT_FAILURE);
   1.722 +					}
   1.723 +				}
   1.724 +				break;
   1.725 +			case 'g':
   1.726 +				do_get = TRUE;
   1.727 +				if (!mta_mode)
   1.728 +					mta_mode = MODE_NONE;  /* to prevent default MODE_ACCEPT */
   1.729 +				if (argv[arg][pos] == 'o') {
   1.730 +					pos++;
   1.731 +					do_get_online = TRUE;
   1.732 +					/* can be NULL, then we use online detection method */
   1.733 +					route_name = get_optarg(argv, argc, &arg, &pos);
   1.734 +
   1.735 +					if (route_name != NULL) {
   1.736 +						if (isdigit(route_name[0])) {
   1.737 +							get_interval = time_interval(route_name, &pos);
   1.738 +							route_name = get_optarg(argv, argc, &arg, &pos);
   1.739 +							mta_mode = MODE_GET_DAEMON;
   1.740 +							do_get = FALSE;
   1.741 +						}
   1.742 +					}
   1.743 +				} else {
   1.744 +					if ((optarg = get_optarg(argv, argc, &arg, &pos))) {
   1.745 +						get_name = get_optarg(argv, argc, &arg, &pos);
   1.746 +					}
   1.747 +				}
   1.748 +				break;
   1.749 +			case 'i':
   1.750 +				if (argv[arg][pos] == 0) {
   1.751 +					opt_i = TRUE;
   1.752 +					exit_failure = FALSE;  /* may override -oem */
   1.753 +				} else {
   1.754 +					fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
   1.755 +					exit(EXIT_FAILURE);
   1.756 +				}
   1.757 +				break;
   1.758 +			case 'M':
   1.759 +				{
   1.760 +					mta_mode = MODE_MCMD;
   1.761 +					M_cmd = g_strdup(&(argv[arg][pos]));
   1.762 +				}
   1.763 +				break;
   1.764 +			case 'o':
   1.765 +				switch (argv[arg][pos++]) {
   1.766 +				case 'e':
   1.767 +					if (argv[arg][pos++] == 'm')  /* -oem */
   1.768 +						if (!opt_i)
   1.769 +							exit_failure = TRUE;
   1.770 +					opt_oem = TRUE;
   1.771 +					break;
   1.772 +				case 'd':
   1.773 +					if (argv[arg][pos] == 'b')  /* -odb */
   1.774 +						opt_odb = TRUE;
   1.775 +					else if (argv[arg][pos] == 'q')  /* -odq */
   1.776 +						do_queue = TRUE;
   1.777 +					break;
   1.778 +				case 'i':
   1.779 +					opt_i = TRUE;
   1.780 +					exit_failure = FALSE;  /* may override -oem */
   1.781 +					break;
   1.782 +				}
   1.783 +				break;
   1.784 +
   1.785 +			case 'q':
   1.786 +				{
   1.787 +					gchar *optarg;
   1.788 +
   1.789 +					do_runq = TRUE;
   1.790 +					mta_mode = MODE_RUNQUEUE;
   1.791 +					if (argv[arg][pos] == 'o') {
   1.792 +						pos++;
   1.793 +						do_runq = FALSE;
   1.794 +						do_runq_online = TRUE;
   1.795 +						/* can be NULL, then we use online detection method */
   1.796 +						route_name = get_optarg(argv, argc, &arg, &pos);
   1.797 +					} else
   1.798 +						if ((optarg = get_optarg(argv, argc, &arg, &pos))) {
   1.799 +						mta_mode = MODE_DAEMON;
   1.800 +						queue_interval = time_interval(optarg, &pos);
   1.801 +					}
   1.802 +				}
   1.803 +				break;
   1.804 +			case 't':
   1.805 +				if (argv[arg][pos] == 0) {
   1.806 +					opt_t = TRUE;
   1.807 +				} else {
   1.808 +					fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
   1.809 +					exit(EXIT_FAILURE);
   1.810 +				}
   1.811 +				break;
   1.812 +			case 'v':
   1.813 +				do_verbose = TRUE;
   1.814 +				break;
   1.815 +			default:
   1.816 +				fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
   1.817 +				exit(EXIT_FAILURE);
   1.818 +			}
   1.819 +		} else {
   1.820 +			if (argv[arg][pos + 1] == '-') {
   1.821 +				if (argv[arg][pos + 2] != '\0') {
   1.822 +					fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
   1.823 +					exit(EXIT_FAILURE);
   1.824 +				}
   1.825 +				arg++;
   1.826 +			}
   1.827 +			break;
   1.828 +		}
   1.829 +		arg++;
   1.830  	}
   1.831 -	break;
   1.832 -      case 'B':
   1.833 -	/* we ignore this and throw the argument away */
   1.834 -	get_optarg(argv, argc, &arg, &pos);
   1.835 -	break;
   1.836 -      case 'C':
   1.837 -	if(!(conf_file = get_optarg(argv, argc, &arg, &pos))){
   1.838 -	  fprintf(stderr, "-C requires a filename as argument.\n");
   1.839 -	  exit(EXIT_FAILURE);
   1.840 -	}
   1.841 -	break;
   1.842 -      case 'F':
   1.843 -	{
   1.844 -	  full_sender_name = get_optarg(argv, argc, &arg, &pos);
   1.845 -	  if(!full_sender_name){
   1.846 -	    fprintf(stderr, "-F requires a name as an argument\n");
   1.847 -	    exit(EXIT_FAILURE);
   1.848 -	  }
   1.849 -	}
   1.850 -	break;
   1.851 -      case 'd':
   1.852 -	if(getuid() == 0){
   1.853 -	  char *lvl = get_optarg(argv, argc, &arg, &pos);
   1.854 -	  if(lvl)
   1.855 -	    debug_level = atoi(lvl);
   1.856 -	  else{
   1.857 -	    fprintf(stderr, "-d requires a number as an argument.\n");
   1.858 -	    exit(EXIT_FAILURE);
   1.859 -	  }
   1.860 -	}else{
   1.861 -	  fprintf(stderr, "only root may set the debug level.\n");
   1.862 -	  exit(EXIT_FAILURE);
   1.863 -	}
   1.864 -	break;
   1.865 -      case 'f':
   1.866 -	/* set return path */
   1.867 -	{
   1.868 -	  gchar *address;
   1.869 -	  address = get_optarg(argv, argc, &arg, &pos);
   1.870 -	  if(address){
   1.871 -	    f_address = g_strdup(address);
   1.872 -	  }else{
   1.873 -	    fprintf(stderr, "-f requires an address as an argument\n");
   1.874 -	    exit(EXIT_FAILURE);
   1.875 -	  }
   1.876 -	}
   1.877 -	break;
   1.878 -      case 'g':
   1.879 -	do_get = TRUE;
   1.880 -	if(!mta_mode) mta_mode = MODE_NONE; /* to prevent default MODE_ACCEPT */
   1.881 -	if(argv[arg][pos] == 'o'){
   1.882 -	  pos++;
   1.883 -	  do_get_online = TRUE;
   1.884 -	  /* can be NULL, then we use online detection method */
   1.885 -	  route_name = get_optarg(argv, argc, &arg, &pos);
   1.886  
   1.887 -	  if(route_name != NULL){
   1.888 -	    if(isdigit(route_name[0])){
   1.889 -	      get_interval = time_interval(route_name, &pos);
   1.890 -	      route_name = get_optarg(argv, argc, &arg, &pos);
   1.891 -	      mta_mode = MODE_GET_DAEMON;
   1.892 -	      do_get = FALSE;
   1.893 -	    }
   1.894 -	  }
   1.895 -	}else{
   1.896 -	  if((optarg = get_optarg(argv, argc, &arg, &pos))){
   1.897 -	    get_name = get_optarg(argv, argc, &arg, &pos);
   1.898 -	  }
   1.899 -	}
   1.900 -	break;
   1.901 -      case 'i':
   1.902 -	if(argv[arg][pos] == 0){
   1.903 -	  opt_i = TRUE;
   1.904 -	  exit_failure = FALSE; /* may override -oem */
   1.905 -	}else{
   1.906 -	  fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
   1.907 -	  exit(EXIT_FAILURE);
   1.908 -	}
   1.909 -	break;
   1.910 -      case 'M':
   1.911 -	{
   1.912 -	  mta_mode = MODE_MCMD;
   1.913 -	  M_cmd = g_strdup(&(argv[arg][pos]));
   1.914 -	}
   1.915 -	break;
   1.916 -      case 'o':
   1.917 -	switch(argv[arg][pos++]){
   1.918 -	case 'e':
   1.919 -	  if(argv[arg][pos++] == 'm') /* -oem */
   1.920 -	    if(!opt_i) exit_failure = TRUE;
   1.921 -	    opt_oem = TRUE;
   1.922 -	  break;
   1.923 -	case 'd':
   1.924 -	  if(argv[arg][pos] == 'b') /* -odb */
   1.925 -	    opt_odb = TRUE;
   1.926 -	  else if(argv[arg][pos] == 'q') /* -odq */
   1.927 -	    do_queue = TRUE;
   1.928 -	  break;
   1.929 -	case 'i':
   1.930 -	  opt_i = TRUE;
   1.931 -	  exit_failure = FALSE; /* may override -oem */
   1.932 -	  break;
   1.933 -	}
   1.934 -	break;
   1.935 +	if (mta_mode == MODE_VERSION) {
   1.936 +		gchar *with_resolver = "", *with_smtp_server = "", *with_pop3 = "",
   1.937 +		*with_auth = "", *with_maildir = "", *with_ident = "", *with_mserver = "";
   1.938  
   1.939 -      case 'q':
   1.940 -	{
   1.941 -	  gchar *optarg;
   1.942 -
   1.943 -	  do_runq = TRUE;
   1.944 -	  mta_mode = MODE_RUNQUEUE;
   1.945 -	  if(argv[arg][pos] == 'o'){
   1.946 -	    pos++;
   1.947 -	    do_runq = FALSE;
   1.948 -	    do_runq_online = TRUE;
   1.949 -	    /* can be NULL, then we use online detection method */
   1.950 -	    route_name = get_optarg(argv, argc, &arg, &pos);
   1.951 -	  }else if((optarg = get_optarg(argv, argc, &arg, &pos))){
   1.952 -	    mta_mode = MODE_DAEMON;
   1.953 -	    queue_interval = time_interval(optarg, &pos);
   1.954 -	  }
   1.955 -	}
   1.956 -	break;
   1.957 -      case 't':
   1.958 -	if(argv[arg][pos] == 0){
   1.959 -	  opt_t = TRUE;
   1.960 -	}else{
   1.961 -	  fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
   1.962 -	  exit(EXIT_FAILURE);
   1.963 -	}
   1.964 -	break;
   1.965 -      case 'v':
   1.966 -	do_verbose = TRUE;
   1.967 -	break;
   1.968 -      default:
   1.969 -	fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
   1.970 -	exit(EXIT_FAILURE);
   1.971 -      }
   1.972 -    }else{
   1.973 -      if(argv[arg][pos+1] == '-'){
   1.974 -        if(argv[arg][pos+2] != '\0'){
   1.975 -	  fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
   1.976 -	  exit(EXIT_FAILURE);
   1.977 -        }
   1.978 -	arg++;
   1.979 -      }
   1.980 -      break;
   1.981 -    }
   1.982 -    arg++;
   1.983 -  }
   1.984 -
   1.985 -  if(mta_mode == MODE_VERSION){
   1.986 -    gchar *with_resolver = "", *with_smtp_server = "", *with_pop3 = "", *with_auth = "", 
   1.987 -      *with_maildir = "", *with_ident = "", *with_mserver = "";
   1.988 -    
   1.989  #ifdef ENABLE_RESOLVER
   1.990 -    with_resolver = " +resolver";
   1.991 +		with_resolver = " +resolver";
   1.992  #endif
   1.993  #ifdef ENABLE_SMTP_SERVER
   1.994 -    with_smtp_server = " +smtp-server";
   1.995 +		with_smtp_server = " +smtp-server";
   1.996  #endif
   1.997  #ifdef ENABLE_POP3
   1.998 -    with_pop3 = " +pop3";
   1.999 +		with_pop3 = " +pop3";
  1.1000  #endif
  1.1001  #ifdef ENABLE_AUTH
  1.1002 -    with_auth = " +auth";
  1.1003 +		with_auth = " +auth";
  1.1004  #endif
  1.1005  #ifdef ENABLE_MAILDIR
  1.1006 -    with_maildir = " +maildir";
  1.1007 +		with_maildir = " +maildir";
  1.1008  #endif
  1.1009  #ifdef ENABLE_IDENT
  1.1010 -    with_ident = " +ident";
  1.1011 +		with_ident = " +ident";
  1.1012  #endif
  1.1013  #ifdef ENABLE_MSERVER
  1.1014 -    with_mserver = " +mserver";
  1.1015 +		with_mserver = " +mserver";
  1.1016  #endif
  1.1017  
  1.1018 -    printf("%s %s%s%s%s%s%s%s%s\n", PACKAGE, VERSION,
  1.1019 -	   with_resolver, with_smtp_server, with_pop3, with_auth,
  1.1020 -	   with_maildir, with_ident, with_mserver);
  1.1021 -      
  1.1022 -    exit(EXIT_SUCCESS);
  1.1023 -  }
  1.1024 +		printf("%s %s%s%s%s%s%s%s%s\n", PACKAGE, VERSION, with_resolver, with_smtp_server,
  1.1025 +		       with_pop3, with_auth, with_maildir, with_ident, with_mserver);
  1.1026  
  1.1027 -  /* initialize random generator */
  1.1028 -  srand(time(NULL));
  1.1029 -  /* ignore SIGPIPE signal */
  1.1030 -  signal(SIGPIPE, SIG_IGN);
  1.1031 +		exit(EXIT_SUCCESS);
  1.1032 +	}
  1.1033  
  1.1034 -  /* close all possibly open file descriptors */
  1.1035 -  {
  1.1036 -    int i, max_fd = sysconf(_SC_OPEN_MAX);
  1.1037 +	/* initialize random generator */
  1.1038 +	srand(time(NULL));
  1.1039 +	/* ignore SIGPIPE signal */
  1.1040 +	signal(SIGPIPE, SIG_IGN);
  1.1041  
  1.1042 -    if(max_fd <= 0) max_fd = 64;
  1.1043 -    for(i = 3; i < max_fd; i++)
  1.1044 -      close(i);
  1.1045 -  }
  1.1046 +	/* close all possibly open file descriptors */
  1.1047 +	{
  1.1048 +		int i, max_fd = sysconf(_SC_OPEN_MAX);
  1.1049  
  1.1050 -  init_conf();
  1.1051 +		if (max_fd <= 0)
  1.1052 +			max_fd = 64;
  1.1053 +		for (i = 3; i < max_fd; i++)
  1.1054 +			close(i);
  1.1055 +	}
  1.1056  
  1.1057 -  /* if we are not privileged, and the config file was changed we
  1.1058 -     implicetely set the the run_as_user flag and give up all
  1.1059 -     privileges.
  1.1060 +	init_conf();
  1.1061  
  1.1062 -     So it is possible for a user to run his own daemon without
  1.1063 -     breaking security.
  1.1064 -  */
  1.1065 -  if(strcmp(conf_file, CONF_FILE) != 0){
  1.1066 -    if(conf.orig_uid != 0){
  1.1067 -      conf.run_as_user = TRUE;
  1.1068 -      seteuid(conf.orig_uid);
  1.1069 -      setegid(conf.orig_gid);
  1.1070 -      setuid(conf.orig_uid);
  1.1071 -      setgid(conf.orig_gid);
  1.1072 -    }
  1.1073 -  }
  1.1074 +	/* if we are not privileged, and the config file was changed we
  1.1075 +	   implicetely set the the run_as_user flag and give up all
  1.1076 +	   privileges.
  1.1077  
  1.1078 -  read_conf(conf_file);
  1.1079 +	   So it is possible for a user to run his own daemon without
  1.1080 +	   breaking security.
  1.1081 +	 */
  1.1082 +	if (strcmp(conf_file, CONF_FILE) != 0) {
  1.1083 +		if (conf.orig_uid != 0) {
  1.1084 +			conf.run_as_user = TRUE;
  1.1085 +			seteuid(conf.orig_uid);
  1.1086 +			setegid(conf.orig_gid);
  1.1087 +			setuid(conf.orig_uid);
  1.1088 +			setgid(conf.orig_gid);
  1.1089 +		}
  1.1090 +	}
  1.1091  
  1.1092 -  if(do_queue) conf.do_queue = TRUE;
  1.1093 -  if(do_verbose) conf.do_verbose = TRUE;
  1.1094 -  if(debug_level >= 0) /* if >= 0, it was given by argument */
  1.1095 -    conf.debug_level = debug_level;
  1.1096 +	read_conf(conf_file);
  1.1097  
  1.1098 -  chdir("/");
  1.1099 +	if (do_queue)
  1.1100 +		conf.do_queue = TRUE;
  1.1101 +	if (do_verbose)
  1.1102 +		conf.do_verbose = TRUE;
  1.1103 +	if (debug_level >= 0)  /* if >= 0, it was given by argument */
  1.1104 +		conf.debug_level = debug_level;
  1.1105  
  1.1106 -  if(!conf.run_as_user){
  1.1107 -    if(setgid(0) != 0){
  1.1108 -      fprintf(stderr,
  1.1109 -	      "could not set gid to 0. Is the setuid bit set? : %s\n",
  1.1110 -	      strerror(errno));
  1.1111 -      exit(EXIT_FAILURE);
  1.1112 -    }
  1.1113 -    if(setuid(0) != 0){
  1.1114 -      fprintf(stderr,
  1.1115 -	      "could not gain root privileges. Is the setuid bit set? : %s\n",
  1.1116 -	      strerror(errno));
  1.1117 -      exit(EXIT_FAILURE);
  1.1118 -    }
  1.1119 -  }
  1.1120 +	chdir("/");
  1.1121  
  1.1122 -  if(!logopen()){
  1.1123 -    fprintf(stderr, "could not open log file\n");
  1.1124 -    exit(EXIT_FAILURE);
  1.1125 -  }
  1.1126 +	if (!conf.run_as_user) {
  1.1127 +		if (setgid(0) != 0) {
  1.1128 +			fprintf(stderr, "could not set gid to 0. Is the setuid bit set? : %s\n", strerror(errno));
  1.1129 +			exit(EXIT_FAILURE);
  1.1130 +		}
  1.1131 +		if (setuid(0) != 0) {
  1.1132 +			fprintf(stderr, "could not gain root privileges. Is the setuid bit set? : %s\n", strerror(errno));
  1.1133 +			exit(EXIT_FAILURE);
  1.1134 +		}
  1.1135 +	}
  1.1136  
  1.1137 -  DEBUG(1) debugf("masqmail %s starting\n", VERSION);
  1.1138 +	if (!logopen()) {
  1.1139 +		fprintf(stderr, "could not open log file\n");
  1.1140 +		exit(EXIT_FAILURE);
  1.1141 +	}
  1.1142  
  1.1143 -  DEBUG(5){
  1.1144 -    gchar **str = argv;
  1.1145 -    debugf("args: \n");
  1.1146 -    while(*str){
  1.1147 -      debugf("%s \n", *str);
  1.1148 -      str++;
  1.1149 -    }
  1.1150 -  }
  1.1151 -  DEBUG(5) debugf("queue_interval = %d\n", queue_interval);
  1.1152 +	DEBUG(1) debugf("masqmail %s starting\n", VERSION);
  1.1153  
  1.1154 -  if(f_address){
  1.1155 -    return_path = create_address_qualified(f_address, TRUE, conf.host_name);
  1.1156 -    g_free(f_address);
  1.1157 -    if(!return_path){
  1.1158 -      fprintf(stderr, "invalid RFC821 address: %s\n", f_address);
  1.1159 -      exit(EXIT_FAILURE);
  1.1160 -    }
  1.1161 -  }
  1.1162 +	DEBUG(5) {
  1.1163 +		gchar **str = argv;
  1.1164 +		debugf("args: \n");
  1.1165 +		while (*str) {
  1.1166 +			debugf("%s \n", *str);
  1.1167 +			str++;
  1.1168 +		}
  1.1169 +	}
  1.1170 +	DEBUG(5) debugf("queue_interval = %d\n", queue_interval);
  1.1171  
  1.1172 -  if(do_get){
  1.1173 +	if (f_address) {
  1.1174 +		return_path = create_address_qualified(f_address, TRUE, conf.host_name);
  1.1175 +		g_free(f_address);
  1.1176 +		if (!return_path) {
  1.1177 +			fprintf(stderr, "invalid RFC821 address: %s\n", f_address);
  1.1178 +			exit(EXIT_FAILURE);
  1.1179 +		}
  1.1180 +	}
  1.1181 +
  1.1182 +	if (do_get) {
  1.1183  #ifdef ENABLE_POP3
  1.1184 -    if((mta_mode == MODE_NONE) || (mta_mode == MODE_RUNQUEUE)){
  1.1185 +		if ((mta_mode == MODE_NONE) || (mta_mode == MODE_RUNQUEUE)) {
  1.1186 +			set_identity(conf.orig_uid, "getting mail");
  1.1187 +			if (do_get_online) {
  1.1188 +				if (route_name != NULL) {
  1.1189 +					conf.online_detect = g_strdup("argument");
  1.1190 +					set_online_name(route_name);
  1.1191 +				}
  1.1192 +				get_online();
  1.1193 +			} else {
  1.1194 +				if (get_name)
  1.1195 +					get_from_name(get_name);
  1.1196 +				else
  1.1197 +					get_all();
  1.1198 +			}
  1.1199 +		} else {
  1.1200 +			logwrite(LOG_ALERT, "get (-g) only allowed alone or together with queue run (-q)\n");
  1.1201 +		}
  1.1202 +#else
  1.1203 +		fprintf(stderr, "get (pop) support not compiled in\n");
  1.1204 +#endif
  1.1205 +	}
  1.1206  
  1.1207 -      set_identity(conf.orig_uid, "getting mail");
  1.1208 +	switch (mta_mode) {
  1.1209 +	case MODE_DAEMON:
  1.1210 +		mode_daemon(do_listen, queue_interval, argv);
  1.1211 +		break;
  1.1212 +	case MODE_RUNQUEUE:
  1.1213 +		{
  1.1214 +			/* queue runs */
  1.1215 +			set_identity(conf.orig_uid, "queue run");
  1.1216  
  1.1217 -      if(do_get_online){
  1.1218 -	if(route_name != NULL){
  1.1219 -	  conf.online_detect = g_strdup("argument");
  1.1220 -	  set_online_name(route_name);
  1.1221 +			if (do_runq)
  1.1222 +				exit_code = queue_run() ? EXIT_SUCCESS : EXIT_FAILURE;
  1.1223 +
  1.1224 +			if (do_runq_online) {
  1.1225 +				if (route_name != NULL) {
  1.1226 +					conf.online_detect = g_strdup("argument");
  1.1227 +					set_online_name(route_name);
  1.1228 +				}
  1.1229 +				exit_code =
  1.1230 +					queue_run_online() ? EXIT_SUCCESS : EXIT_FAILURE;
  1.1231 +			}
  1.1232 +		}
  1.1233 +		break;
  1.1234 +	case MODE_GET_DAEMON:
  1.1235 +#ifdef ENABLE_POP3
  1.1236 +		if (route_name != NULL) {
  1.1237 +			conf.online_detect = g_strdup("argument");
  1.1238 +			set_online_name(route_name);
  1.1239 +		}
  1.1240 +		mode_get_daemon(get_interval, argv);
  1.1241 +#endif
  1.1242 +		break;
  1.1243 +
  1.1244 +	case MODE_SMTP:
  1.1245 +#ifdef ENABLE_SMTP_SERVER
  1.1246 +		mode_smtp();
  1.1247 +#else
  1.1248 +		fprintf(stderr, "smtp server support not compiled in\n");
  1.1249 +#endif
  1.1250 +		break;
  1.1251 +
  1.1252 +	case MODE_LIST:
  1.1253 +		queue_list();
  1.1254 +		break;
  1.1255 +
  1.1256 +	case MODE_BI:
  1.1257 +		exit(EXIT_SUCCESS);
  1.1258 +		break;  /* well... */
  1.1259 +
  1.1260 +	case MODE_MCMD:
  1.1261 +		if (strcmp(M_cmd, "rm") == 0) {
  1.1262 +			gboolean ok = FALSE;
  1.1263 +
  1.1264 +			set_euidgid(conf.mail_uid, conf.mail_gid, NULL, NULL);
  1.1265 +
  1.1266 +			if (is_privileged_user(conf.orig_uid)) {
  1.1267 +				for (; arg < argc; arg++) {
  1.1268 +					if (queue_delete(argv[arg]))
  1.1269 +						ok = TRUE;
  1.1270 +				}
  1.1271 +			} else {
  1.1272 +				struct passwd *pw = getpwuid(conf.orig_uid);
  1.1273 +				if (pw) {
  1.1274 +					for (; arg < argc; arg++) {
  1.1275 +						message *msg = msg_spool_read(argv[arg], FALSE);
  1.1276 +#ifdef ENABLE_IDENT
  1.1277 +						if (((msg->received_host == NULL) && (msg->received_prot == PROT_LOCAL))
  1.1278 +						    || is_in_netlist(msg->received_host, conf.ident_trusted_nets)) {
  1.1279 +#else
  1.1280 +						if ((msg->received_host == NULL) && (msg->received_prot == PROT_LOCAL)) {
  1.1281 +#endif
  1.1282 +							if (msg->ident) {
  1.1283 +								if (strcmp(pw->pw_name, msg->ident) == 0) {
  1.1284 +									if (queue_delete(argv[arg]))
  1.1285 +										ok = TRUE;
  1.1286 +								} else {
  1.1287 +									fprintf(stderr, "you do not own message id %s\n", argv[arg]);
  1.1288 +								}
  1.1289 +							} else
  1.1290 +								fprintf(stderr, "message %s does not have an ident.\n", argv[arg]);
  1.1291 +						} else {
  1.1292 +							fprintf(stderr, "message %s was not received locally or from a trusted network.\n", argv[arg]);
  1.1293 +						}
  1.1294 +					}
  1.1295 +				} else {
  1.1296 +					fprintf(stderr, "could not find a passwd entry for uid %d: %s\n", conf.orig_uid, strerror(errno));
  1.1297 +				}
  1.1298 +			}
  1.1299 +			exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
  1.1300 +		} else {
  1.1301 +			fprintf(stderr, "unknown command %s\n", M_cmd);
  1.1302 +			exit(EXIT_FAILURE);
  1.1303 +		}
  1.1304 +		break;
  1.1305 +
  1.1306 +	case MODE_ACCEPT:
  1.1307 +		{
  1.1308 +			guint accept_flags = (opt_t ? ACC_DEL_RCPTS | ACC_DEL_BCC | ACC_RCPT_FROM_HEAD : ACC_HEAD_FROM_RCPT)
  1.1309 +			                     | (opt_i ? ACC_NODOT_TERM : ACC_NODOT_RELAX);
  1.1310 +			mode_accept(return_path, full_sender_name, accept_flags, &(argv[arg]), argc - arg);
  1.1311 +			exit(exit_failure ? EXIT_FAILURE : EXIT_SUCCESS);
  1.1312 +		}
  1.1313 +		break;
  1.1314 +	case MODE_NONE:
  1.1315 +		break;
  1.1316 +	default:
  1.1317 +		fprintf(stderr, "unknown mode: %d\n", mta_mode);
  1.1318 +		break;
  1.1319  	}
  1.1320 -	get_online();
  1.1321 -      }else{
  1.1322 -	if(get_name)
  1.1323 -	  get_from_name(get_name);
  1.1324 -	else
  1.1325 -	  get_all();
  1.1326 -      }
  1.1327 -    }else{
  1.1328 -      logwrite(LOG_ALERT, "get (-g) only allowed alone or together with queue run (-q)\n");
  1.1329 -    }
  1.1330 -#else
  1.1331 -    fprintf(stderr, "get (pop) support not compiled in\n");
  1.1332 -#endif
  1.1333 -  }
  1.1334  
  1.1335 -  switch(mta_mode){
  1.1336 -  case MODE_DAEMON:
  1.1337 -    mode_daemon(do_listen, queue_interval, argv);
  1.1338 -    break;
  1.1339 -  case MODE_RUNQUEUE:
  1.1340 -    {
  1.1341 -      /* queue runs */
  1.1342 -      set_identity(conf.orig_uid, "queue run");
  1.1343 +	logclose();
  1.1344  
  1.1345 -      if(do_runq)
  1.1346 -	exit_code = queue_run() ? EXIT_SUCCESS : EXIT_FAILURE;
  1.1347 -
  1.1348 -      if(do_runq_online){
  1.1349 -	if(route_name != NULL){
  1.1350 -	  conf.online_detect = g_strdup("argument");
  1.1351 -	  set_online_name(route_name);
  1.1352 -	}
  1.1353 -	exit_code = queue_run_online() ? EXIT_SUCCESS : EXIT_FAILURE;
  1.1354 -      }
  1.1355 -    }
  1.1356 -    break;
  1.1357 -  case MODE_GET_DAEMON:
  1.1358 -#ifdef ENABLE_POP3
  1.1359 -    if(route_name != NULL){
  1.1360 -      conf.online_detect = g_strdup("argument");
  1.1361 -      set_online_name(route_name);
  1.1362 -    }
  1.1363 -    mode_get_daemon(get_interval, argv);
  1.1364 -#endif
  1.1365 -    break;
  1.1366 -
  1.1367 -  case MODE_SMTP:
  1.1368 -#ifdef ENABLE_SMTP_SERVER
  1.1369 -    mode_smtp();
  1.1370 -#else
  1.1371 -    fprintf(stderr, "smtp server support not compiled in\n");
  1.1372 -#endif
  1.1373 -    break;
  1.1374 -  case MODE_LIST:
  1.1375 -
  1.1376 -    queue_list();
  1.1377 -    break;
  1.1378 -
  1.1379 -  case MODE_BI:
  1.1380 -    
  1.1381 -    exit(EXIT_SUCCESS);
  1.1382 -    break; /* well... */
  1.1383 -    
  1.1384 -  case MODE_MCMD:
  1.1385 -    if(strcmp(M_cmd, "rm") == 0){
  1.1386 -      gboolean ok = FALSE;
  1.1387 -
  1.1388 -      set_euidgid(conf.mail_uid, conf.mail_gid, NULL, NULL);
  1.1389 -
  1.1390 -      if(is_privileged_user(conf.orig_uid)){
  1.1391 -	for(; arg < argc; arg++){
  1.1392 -	  if(queue_delete(argv[arg]))
  1.1393 -	    ok = TRUE;
  1.1394 -	}
  1.1395 -      }else{
  1.1396 -	struct passwd *pw = getpwuid(conf.orig_uid);
  1.1397 -	if(pw){
  1.1398 -	  for(; arg < argc; arg++){
  1.1399 -	    message *msg = msg_spool_read(argv[arg], FALSE);
  1.1400 -#ifdef ENABLE_IDENT
  1.1401 -	    if(((msg->received_host == NULL) && (msg->received_prot == PROT_LOCAL)) ||
  1.1402 -	       is_in_netlist(msg->received_host, conf.ident_trusted_nets)){
  1.1403 -#else
  1.1404 -	      if((msg->received_host == NULL) && (msg->received_prot == PROT_LOCAL)){
  1.1405 -#endif
  1.1406 -	      if(msg->ident){
  1.1407 -		if(strcmp(pw->pw_name, msg->ident) == 0){
  1.1408 -		  if(queue_delete(argv[arg]))
  1.1409 -		    ok = TRUE;
  1.1410 -		}else{
  1.1411 -		  fprintf(stderr, "you do not own message id %s\n", argv[arg]);
  1.1412 -		}
  1.1413 -	      }else
  1.1414 -		fprintf(stderr, "message %s does not have an ident.\n", argv[arg]);
  1.1415 -	    }else{
  1.1416 -	      fprintf(stderr, "message %s was not received locally or from a trusted network.\n", argv[arg]);
  1.1417 -	    }
  1.1418 -	  }
  1.1419 -	}else{
  1.1420 -	  fprintf(stderr, "could not find a passwd entry for uid %d: %s\n", conf.orig_uid, strerror(errno));
  1.1421 -	}
  1.1422 -      }
  1.1423 -      exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
  1.1424 -    }else{
  1.1425 -      fprintf(stderr, "unknown command %s\n", M_cmd);
  1.1426 -      exit(EXIT_FAILURE);
  1.1427 -    }
  1.1428 -    break;
  1.1429 -
  1.1430 -    case MODE_ACCEPT:
  1.1431 -    {
  1.1432 -      guint accept_flags =
  1.1433 -	(opt_t ? ACC_DEL_RCPTS|ACC_DEL_BCC|ACC_RCPT_FROM_HEAD : ACC_HEAD_FROM_RCPT) |
  1.1434 -	(opt_i ? ACC_NODOT_TERM : ACC_NODOT_RELAX);
  1.1435 -    
  1.1436 -      mode_accept(return_path, full_sender_name, accept_flags, &(argv[arg]), argc - arg);
  1.1437 -
  1.1438 -      exit(exit_failure ? EXIT_FAILURE : EXIT_SUCCESS);
  1.1439 -    }
  1.1440 -    break;
  1.1441 -  case MODE_NONE:
  1.1442 -      break;
  1.1443 -  default:
  1.1444 -      fprintf(stderr, "unknown mode: %d\n", mta_mode);
  1.1445 -      break;
  1.1446 -    }
  1.1447 -    
  1.1448 -  logclose();
  1.1449 -
  1.1450 -  exit(exit_code);
  1.1451 +	exit(exit_code);
  1.1452  }