1 /* $NetBSD: single_server.c,v 1.4 2022/10/08 16:12:46 christos Exp $ */
2
3 /*++
4 /* NAME
5 /* single_server 3
6 /* SUMMARY
7 /* skeleton single-threaded mail subsystem
8 /* SYNOPSIS
9 /* #include <mail_server.h>
10 /*
11 /* NORETURN single_server_main(argc, argv, service, key, value, ...)
12 /* int argc;
13 /* char **argv;
14 /* void (*service)(VSTREAM *stream, char *service_name, char **argv);
15 /* int key;
16 /* DESCRIPTION
17 /* This module implements a skeleton for single-threaded
18 /* mail subsystems: mail subsystem programs that service one
19 /* client at a time. The resulting program expects to be run
20 /* from the \fBmaster\fR process.
21 /*
22 /* single_server_main() is the skeleton entry point. It should be
23 /* called from the application main program. The skeleton does the
24 /* generic command-line options processing, initialization of
25 /* configurable parameters, and connection management.
26 /* The skeleton never returns.
27 /*
28 /* Arguments:
29 /* .IP "void (*service)(VSTREAM *fp, char *service_name, char **argv)"
30 /* A pointer to a function that is called by the skeleton each time
31 /* a client connects to the program's service port. The function is
32 /* run after the program has irrevocably dropped its privileges.
33 /* The stream initial state is non-blocking mode.
34 /* Optional connection attributes are provided as a hash that
35 /* is attached as stream context.
36 /* The service name argument corresponds to the service name in the
37 /* master.cf file.
38 /* The argv argument specifies command-line arguments left over
39 /* after options processing.
40 /* .PP
41 /* Optional arguments are specified as a null-terminated list
42 /* with macros that have zero or more arguments:
43 /* .IP "CA_MAIL_SERVER_INT_TABLE(CONFIG_INT_TABLE *)"
44 /* A table with configurable parameters, to be loaded from the
45 /* global Postfix configuration file. Tables are loaded in the
46 /* order as specified, and multiple instances of the same type
47 /* are allowed.
48 /* .IP "CA_MAIL_SERVER_LONG_TABLE(CONFIG_LONG_TABLE *)"
49 /* A table with configurable parameters, to be loaded from the
50 /* global Postfix configuration file. Tables are loaded in the
51 /* order as specified, and multiple instances of the same type
52 /* are allowed.
53 /* .IP "CA_MAIL_SERVER_STR_TABLE(CONFIG_STR_TABLE *)"
54 /* A table with configurable parameters, to be loaded from the
55 /* global Postfix configuration file. Tables are loaded in the
56 /* order as specified, and multiple instances of the same type
57 /* are allowed.
58 /* .IP "CA_MAIL_SERVER_BOOL_TABLE(CONFIG_BOOL_TABLE *)"
59 /* A table with configurable parameters, to be loaded from the
60 /* global Postfix configuration file. Tables are loaded in the
61 /* order as specified, and multiple instances of the same type
62 /* are allowed.
63 /* .IP "CA_MAIL_SERVER_TIME_TABLE(CONFIG_TIME_TABLE *)"
64 /* A table with configurable parameters, to be loaded from the
65 /* global Postfix configuration file. Tables are loaded in the
66 /* order as specified, and multiple instances of the same type
67 /* are allowed.
68 /* .IP "CA_MAIL_SERVER_RAW_TABLE(CONFIG_RAW_TABLE *)"
69 /* A table with configurable parameters, to be loaded from the
70 /* global Postfix configuration file. Tables are loaded in the
71 /* order as specified, and multiple instances of the same type
72 /* are allowed. Raw parameters are not subjected to $name
73 /* evaluation.
74 /* .IP "CA_MAIL_SERVER_NINT_TABLE(CONFIG_NINT_TABLE *)"
75 /* A table with configurable parameters, to be loaded from the
76 /* global Postfix configuration file. Tables are loaded in the
77 /* order as specified, and multiple instances of the same type
78 /* are allowed.
79 /* .IP "CA_MAIL_SERVER_NBOOL_TABLE(CONFIG_NBOOL_TABLE *)"
80 /* A table with configurable parameters, to be loaded from the
81 /* global Postfix configuration file. Tables are loaded in the
82 /* order as specified, and multiple instances of the same type
83 /* are allowed.
84 /* .IP "CA_MAIL_SERVER_PRE_INIT(void *(char *service_name, char **argv))"
85 /* A pointer to a function that is called once
86 /* by the skeleton after it has read the global configuration file
87 /* and after it has processed command-line arguments, but before
88 /* the skeleton has optionally relinquished the process privileges.
89 /* .sp
90 /* Only the last instance of this parameter type is remembered.
91 /* .IP "CA_MAIL_SERVER_POST_INIT(void *(char *service_name, char **argv))"
92 /* A pointer to a function that is called once
93 /* by the skeleton after it has optionally relinquished the process
94 /* privileges, but before servicing client connection requests.
95 /* .sp
96 /* Only the last instance of this parameter type is remembered.
97 /* .IP "CA_MAIL_SERVER_LOOP(int *(char *service_name, char **argv))"
98 /* A pointer to function that is executed from
99 /* within the event loop, whenever an I/O or timer event has happened,
100 /* or whenever nothing has happened for a specified amount of time.
101 /* The result value of the function specifies how long to wait until
102 /* the next event. Specify -1 to wait for "as long as it takes".
103 /* .sp
104 /* Only the last instance of this parameter type is remembered.
105 /* .IP "CA_MAIL_SERVER_EXIT(void *(void))"
106 /* A pointer to function that is executed immediately before normal
107 /* process termination.
108 /* .sp
109 /* Only the last instance of this parameter type is remembered.
110 /* .IP "CA_MAIL_SERVER_PRE_ACCEPT(void *(char *service_name, char **argv))"
111 /* Function to be executed prior to accepting a new connection.
112 /* .sp
113 /* Only the last instance of this parameter type is remembered.
114 /* .IP "CA_MAIL_SERVER_IN_FLOW_DELAY(none)"
115 /* Pause $in_flow_delay seconds when no "mail flow control token"
116 /* is available. A token is consumed for each connection request.
117 /* .IP CA_MAIL_SERVER_SOLITARY
118 /* This service must be configured with process limit of 1.
119 /* .IP CA_MAIL_SERVER_UNLIMITED
120 /* This service must be configured with process limit of 0.
121 /* .IP CA_MAIL_SERVER_PRIVILEGED
122 /* This service must be configured as privileged.
123 /* .IP "CA_MAIL_SERVER_BOUNCE_INIT(const char *, const char **)"
124 /* Initialize the DSN filter for the bounce/defer service
125 /* clients with the specified map source and map names.
126 /* .IP "CA_MAIL_SERVER_RETIRE_ME"
127 /* Prevent a process from being reused indefinitely. After
128 /* (var_max_use * var_max_idle) seconds or some sane constant,
129 /* terminate voluntarily when the process becomes idle.
130 /* .PP
131 /* The var_use_limit variable limits the number of clients
132 /* that a server can service before it commits suicide. This
133 /* value is taken from the global \fBmain.cf\fR configuration
134 /* file. Setting \fBvar_use_limit\fR to zero disables the
135 /* client limit.
136 /*
137 /* The var_idle_limit variable limits the time that a service
138 /* receives no client connection requests before it commits suicide.
139 /* Do not change this setting before calling single_server_main().
140 /* This value is taken from the global \fBmain.cf\fR configuration
141 /* file. Setting \fBvar_idle_limit\fR to zero disables the idle limit.
142 /* DIAGNOSTICS
143 /* Problems and transactions are logged to \fBsyslogd\fR(8)
144 /* or \fBpostlogd\fR(8).
145 /* BUGS
146 /* SEE ALSO
147 /* master(8), master process
148 /* postlogd(8), Postfix logging
149 /* syslogd(8), system logging
150 /* LICENSE
151 /* .ad
152 /* .fi
153 /* The Secure Mailer license must be distributed with this software.
154 /* AUTHOR(S)
155 /* Wietse Venema
156 /* IBM T.J. Watson Research
157 /* P.O. Box 704
158 /* Yorktown Heights, NY 10598, USA
159 /*
160 /* Wietse Venema
161 /* Google, Inc.
162 /* 111 8th Avenue
163 /* New York, NY 10011, USA
164 /*--*/
165
166 /* System library. */
167
168 #include <sys_defs.h>
169 #include <sys/socket.h>
170 #include <unistd.h>
171 #include <signal.h>
172 #include <stdlib.h>
173 #include <limits.h>
174 #include <string.h>
175 #include <errno.h>
176 #include <fcntl.h>
177 #include <stdarg.h>
178 #ifdef STRCASECMP_IN_STRINGS_H
179 #include <strings.h>
180 #endif
181 #include <time.h>
182
183 /* Utility library. */
184
185 #include <msg.h>
186 #include <msg_vstream.h>
187 #include <chroot_uid.h>
188 #include <vstring.h>
189 #include <vstream.h>
190 #include <msg_vstream.h>
191 #include <mymalloc.h>
192 #include <events.h>
193 #include <iostuff.h>
194 #include <stringops.h>
195 #include <sane_accept.h>
196 #include <myflock.h>
197 #include <safe_open.h>
198 #include <listen.h>
199 #include <watchdog.h>
200 #include <split_at.h>
201
202 /* Global library. */
203
204 #include <mail_params.h>
205 #include <mail_task.h>
206 #include <debug_process.h>
207 #include <mail_conf.h>
208 #include <mail_dict.h>
209 #include <timed_ipc.h>
210 #include <resolve_local.h>
211 #include <mail_flow.h>
212 #include <mail_version.h>
213 #include <bounce.h>
214 #include <maillog_client.h>
215
216 /* Process manager. */
217
218 #include "master_proto.h"
219
220 /* Application-specific */
221
222 #include "mail_server.h"
223
224 /*
225 * Global state.
226 */
227 static int use_count;
228
229 static void (*single_server_service) (VSTREAM *, char *, char **);
230 static char *single_server_name;
231 static char **single_server_argv;
232 static void (*single_server_accept) (int, void *);
233 static void (*single_server_onexit) (char *, char **);
234 static void (*single_server_pre_accept) (char *, char **);
235 static VSTREAM *single_server_lock;
236 static int single_server_in_flow_delay;
237 static unsigned single_server_generation;
238
239 /* single_server_exit - normal termination */
240
single_server_exit(void)241 static NORETURN single_server_exit(void)
242 {
243 if (single_server_onexit)
244 single_server_onexit(single_server_name, single_server_argv);
245 exit(0);
246 }
247
248 /* single_server_retire - retire when idle */
249
single_server_retire(int unused_event,void * unused_context)250 static NORETURN single_server_retire(int unused_event, void *unused_context)
251 {
252 if (msg_verbose)
253 msg_info("time to retire -- exiting");
254 single_server_exit();
255 }
256
257 /* single_server_abort - terminate after abnormal master exit */
258
single_server_abort(int unused_event,void * unused_context)259 static void single_server_abort(int unused_event, void *unused_context)
260 {
261 if (msg_verbose)
262 msg_info("master disconnect -- exiting");
263 single_server_exit();
264 }
265
266 /* single_server_timeout - idle time exceeded */
267
single_server_timeout(int unused_event,void * unused_context)268 static void single_server_timeout(int unused_event, void *unused_context)
269 {
270 if (msg_verbose)
271 msg_info("idle timeout -- exiting");
272 single_server_exit();
273 }
274
275 /* single_server_wakeup - wake up application */
276
single_server_wakeup(int fd,HTABLE * attr)277 static void single_server_wakeup(int fd, HTABLE *attr)
278 {
279 VSTREAM *stream;
280 char *tmp;
281
282 /*
283 * If the accept() succeeds, be sure to disable non-blocking I/O, because
284 * the application is supposed to be single-threaded. Notice the master
285 * of our (un)availability to service connection requests. Commit suicide
286 * when the master process disconnected from us. Don't drop the already
287 * accepted client request after "postfix reload"; that would be rude.
288 */
289 if (msg_verbose)
290 msg_info("connection established");
291 non_blocking(fd, BLOCKING);
292 close_on_exec(fd, CLOSE_ON_EXEC);
293 stream = vstream_fdopen(fd, O_RDWR);
294 tmp = concatenate(single_server_name, " socket", (char *) 0);
295 vstream_control(stream,
296 CA_VSTREAM_CTL_PATH(tmp),
297 CA_VSTREAM_CTL_CONTEXT((void *) attr),
298 CA_VSTREAM_CTL_END);
299 myfree(tmp);
300 timed_ipc_setup(stream);
301 if (master_notify(var_pid, single_server_generation, MASTER_STAT_TAKEN) < 0)
302 /* void */ ;
303 if (single_server_in_flow_delay && mail_flow_get(1) < 0)
304 doze(var_in_flow_delay * 1000000);
305 single_server_service(stream, single_server_name, single_server_argv);
306 (void) vstream_fclose(stream);
307 if (master_notify(var_pid, single_server_generation, MASTER_STAT_AVAIL) < 0)
308 single_server_abort(EVENT_NULL_TYPE, EVENT_NULL_CONTEXT);
309 if (msg_verbose)
310 msg_info("connection closed");
311 /* Avoid integer wrap-around in a persistent process. */
312 if (use_count < INT_MAX)
313 use_count++;
314 if (var_idle_limit > 0)
315 event_request_timer(single_server_timeout, (void *) 0, var_idle_limit);
316 if (attr)
317 htable_free(attr, myfree);
318 }
319
320 /* single_server_accept_local - accept client connection request */
321
single_server_accept_local(int unused_event,void * context)322 static void single_server_accept_local(int unused_event, void *context)
323 {
324 int listen_fd = CAST_ANY_PTR_TO_INT(context);
325 int time_left = -1;
326 int fd;
327
328 /*
329 * Be prepared for accept() to fail because some other process already
330 * got the connection. We use select() + accept(), instead of simply
331 * blocking in accept(), because we must be able to detect that the
332 * master process has gone away unexpectedly.
333 */
334 if (var_idle_limit > 0)
335 time_left = event_cancel_timer(single_server_timeout, (void *) 0);
336
337 if (single_server_pre_accept)
338 single_server_pre_accept(single_server_name, single_server_argv);
339 fd = LOCAL_ACCEPT(listen_fd);
340 if (single_server_lock != 0
341 && myflock(vstream_fileno(single_server_lock), INTERNAL_LOCK,
342 MYFLOCK_OP_NONE) < 0)
343 msg_fatal("select unlock: %m");
344 if (fd < 0) {
345 if (errno != EAGAIN)
346 msg_error("accept connection: %m");
347 if (time_left >= 0)
348 event_request_timer(single_server_timeout, (void *) 0, time_left);
349 return;
350 }
351 single_server_wakeup(fd, (HTABLE *) 0);
352 }
353
354 #ifdef MASTER_XPORT_NAME_PASS
355
356 /* single_server_accept_pass - accept descriptor */
357
single_server_accept_pass(int unused_event,void * context)358 static void single_server_accept_pass(int unused_event, void *context)
359 {
360 int listen_fd = CAST_ANY_PTR_TO_INT(context);
361 int time_left = -1;
362 int fd;
363 HTABLE *attr = 0;
364
365 /*
366 * Be prepared for accept() to fail because some other process already
367 * got the connection. We use select() + accept(), instead of simply
368 * blocking in accept(), because we must be able to detect that the
369 * master process has gone away unexpectedly.
370 */
371 if (var_idle_limit > 0)
372 time_left = event_cancel_timer(single_server_timeout, (void *) 0);
373
374 if (single_server_pre_accept)
375 single_server_pre_accept(single_server_name, single_server_argv);
376 fd = pass_accept_attr(listen_fd, &attr);
377 if (single_server_lock != 0
378 && myflock(vstream_fileno(single_server_lock), INTERNAL_LOCK,
379 MYFLOCK_OP_NONE) < 0)
380 msg_fatal("select unlock: %m");
381 if (fd < 0) {
382 if (errno != EAGAIN)
383 msg_error("accept connection: %m");
384 if (time_left >= 0)
385 event_request_timer(single_server_timeout, (void *) 0, time_left);
386 return;
387 }
388 single_server_wakeup(fd, attr);
389 }
390
391 #endif
392
393 /* single_server_accept_inet - accept client connection request */
394
single_server_accept_inet(int unused_event,void * context)395 static void single_server_accept_inet(int unused_event, void *context)
396 {
397 int listen_fd = CAST_ANY_PTR_TO_INT(context);
398 int time_left = -1;
399 int fd;
400
401 /*
402 * Be prepared for accept() to fail because some other process already
403 * got the connection. We use select() + accept(), instead of simply
404 * blocking in accept(), because we must be able to detect that the
405 * master process has gone away unexpectedly.
406 */
407 if (var_idle_limit > 0)
408 time_left = event_cancel_timer(single_server_timeout, (void *) 0);
409
410 if (single_server_pre_accept)
411 single_server_pre_accept(single_server_name, single_server_argv);
412 fd = inet_accept(listen_fd);
413 if (single_server_lock != 0
414 && myflock(vstream_fileno(single_server_lock), INTERNAL_LOCK,
415 MYFLOCK_OP_NONE) < 0)
416 msg_fatal("select unlock: %m");
417 if (fd < 0) {
418 if (errno != EAGAIN)
419 msg_error("accept connection: %m");
420 if (time_left >= 0)
421 event_request_timer(single_server_timeout, (void *) 0, time_left);
422 return;
423 }
424 single_server_wakeup(fd, (HTABLE *) 0);
425 }
426
427 /* single_server_main - the real main program */
428
single_server_main(int argc,char ** argv,SINGLE_SERVER_FN service,...)429 NORETURN single_server_main(int argc, char **argv, SINGLE_SERVER_FN service,...)
430 {
431 const char *myname = "single_server_main";
432 VSTREAM *stream = 0;
433 char *root_dir = 0;
434 char *user_name = 0;
435 int debug_me = 0;
436 int daemon_mode = 1;
437 char *service_name = basename(argv[0]);
438 int delay;
439 int c;
440 int socket_count = 1;
441 int fd;
442 va_list ap;
443 MAIL_SERVER_INIT_FN pre_init = 0;
444 MAIL_SERVER_INIT_FN post_init = 0;
445 MAIL_SERVER_LOOP_FN loop = 0;
446 int key;
447 char *transport = 0;
448 char *lock_path;
449 VSTRING *why;
450 int alone = 0;
451 int zerolimit = 0;
452 WATCHDOG *watchdog;
453 char *oname_val;
454 char *oname;
455 char *oval;
456 const char *err;
457 char *generation;
458 int msg_vstream_needed = 0;
459 const char *dsn_filter_title;
460 const char **dsn_filter_maps;
461 int retire_me_from_flags = 0;
462 int retire_me = 0;
463
464 /*
465 * Process environment options as early as we can.
466 */
467 if (getenv(CONF_ENV_VERB))
468 msg_verbose = 1;
469 if (getenv(CONF_ENV_DEBUG))
470 debug_me = 1;
471
472 /*
473 * Don't die when a process goes away unexpectedly.
474 */
475 signal(SIGPIPE, SIG_IGN);
476
477 /*
478 * Don't die for frivolous reasons.
479 */
480 #ifdef SIGXFSZ
481 signal(SIGXFSZ, SIG_IGN);
482 #endif
483
484 /*
485 * May need this every now and then.
486 */
487 var_procname = mystrdup(basename(argv[0]));
488 set_mail_conf_str(VAR_PROCNAME, var_procname);
489
490 /*
491 * Initialize logging and exit handler. Do the syslog first, so that its
492 * initialization completes before we enter the optional chroot jail.
493 */
494 maillog_client_init(mail_task(var_procname), MAILLOG_CLIENT_FLAG_NONE);
495 if (msg_verbose)
496 msg_info("daemon started");
497
498 /*
499 * Check the Postfix library version as soon as we enable logging.
500 */
501 MAIL_VERSION_CHECK;
502
503 /*
504 * Initialize from the configuration file. Allow command-line options to
505 * override compiled-in defaults or configured parameter values.
506 */
507 mail_conf_suck();
508
509 /*
510 * After database open error, continue execution with reduced
511 * functionality.
512 */
513 dict_allow_surrogate = 1;
514
515 /*
516 * Pick up policy settings from master process. Shut up error messages to
517 * stderr, because no-one is going to see them.
518 */
519 opterr = 0;
520 while ((c = GETOPT(argc, argv, "cdDi:lm:n:o:r:s:St:uvVz")) > 0) {
521 switch (c) {
522 case 'c':
523 root_dir = "setme";
524 break;
525 case 'd':
526 daemon_mode = 0;
527 break;
528 case 'D':
529 debug_me = 1;
530 break;
531 case 'i':
532 mail_conf_update(VAR_MAX_IDLE, optarg);
533 break;
534 case 'l':
535 alone = 1;
536 break;
537 case 'm':
538 mail_conf_update(VAR_MAX_USE, optarg);
539 break;
540 case 'n':
541 service_name = optarg;
542 break;
543 case 'o':
544 oname_val = mystrdup(optarg);
545 if ((err = split_nameval(oname_val, &oname, &oval)) != 0)
546 msg_fatal("invalid \"-o %s\" option value: %s", optarg, err);
547 mail_conf_update(oname, oval);
548 myfree(oname_val);
549 break;
550 case 'r':
551 if ((retire_me_from_flags = atoi(optarg)) <= 0)
552 msg_fatal("invalid retirement time: %s", optarg);
553 break;
554 case 's':
555 if ((socket_count = atoi(optarg)) <= 0)
556 msg_fatal("invalid socket_count: %s", optarg);
557 break;
558 case 'S':
559 stream = VSTREAM_IN;
560 break;
561 case 'u':
562 user_name = "setme";
563 break;
564 case 't':
565 transport = optarg;
566 break;
567 case 'v':
568 msg_verbose++;
569 break;
570 case 'V':
571 if (++msg_vstream_needed == 1)
572 msg_vstream_init(mail_task(var_procname), VSTREAM_ERR);
573 break;
574 case 'z':
575 zerolimit = 1;
576 break;
577 default:
578 msg_fatal("invalid option: %c", optopt);
579 break;
580 }
581 }
582 set_mail_conf_str(VAR_SERVNAME, service_name);
583
584 /*
585 * Initialize generic parameters.
586 */
587 mail_params_init();
588 maillog_client_init(mail_task(var_procname), MAILLOG_CLIENT_FLAG_NONE);
589
590 /*
591 * Register higher-level dictionaries and initialize the support for
592 * dynamically-loaded dictionaries.
593 */
594 mail_dict_init();
595
596 /*
597 * If not connected to stdin, stdin must not be a terminal.
598 */
599 if (daemon_mode && stream == 0 && isatty(STDIN_FILENO)) {
600 msg_vstream_init(var_procname, VSTREAM_ERR);
601 msg_fatal("do not run this command by hand");
602 }
603
604 /*
605 * Application-specific initialization.
606 */
607 va_start(ap, service);
608 while ((key = va_arg(ap, int)) != 0) {
609 switch (key) {
610 case MAIL_SERVER_INT_TABLE:
611 get_mail_conf_int_table(va_arg(ap, CONFIG_INT_TABLE *));
612 break;
613 case MAIL_SERVER_LONG_TABLE:
614 get_mail_conf_long_table(va_arg(ap, CONFIG_LONG_TABLE *));
615 break;
616 case MAIL_SERVER_STR_TABLE:
617 get_mail_conf_str_table(va_arg(ap, CONFIG_STR_TABLE *));
618 break;
619 case MAIL_SERVER_BOOL_TABLE:
620 get_mail_conf_bool_table(va_arg(ap, CONFIG_BOOL_TABLE *));
621 break;
622 case MAIL_SERVER_TIME_TABLE:
623 get_mail_conf_time_table(va_arg(ap, CONFIG_TIME_TABLE *));
624 break;
625 case MAIL_SERVER_RAW_TABLE:
626 get_mail_conf_raw_table(va_arg(ap, CONFIG_RAW_TABLE *));
627 break;
628 case MAIL_SERVER_NINT_TABLE:
629 get_mail_conf_nint_table(va_arg(ap, CONFIG_NINT_TABLE *));
630 break;
631 case MAIL_SERVER_NBOOL_TABLE:
632 get_mail_conf_nbool_table(va_arg(ap, CONFIG_NBOOL_TABLE *));
633 break;
634 case MAIL_SERVER_PRE_INIT:
635 pre_init = va_arg(ap, MAIL_SERVER_INIT_FN);
636 break;
637 case MAIL_SERVER_POST_INIT:
638 post_init = va_arg(ap, MAIL_SERVER_INIT_FN);
639 break;
640 case MAIL_SERVER_LOOP:
641 loop = va_arg(ap, MAIL_SERVER_LOOP_FN);
642 break;
643 case MAIL_SERVER_EXIT:
644 single_server_onexit = va_arg(ap, MAIL_SERVER_EXIT_FN);
645 break;
646 case MAIL_SERVER_PRE_ACCEPT:
647 single_server_pre_accept = va_arg(ap, MAIL_SERVER_ACCEPT_FN);
648 break;
649 case MAIL_SERVER_IN_FLOW_DELAY:
650 single_server_in_flow_delay = 1;
651 break;
652 case MAIL_SERVER_SOLITARY:
653 if (stream == 0 && !alone)
654 msg_fatal("service %s requires a process limit of 1",
655 service_name);
656 break;
657 case MAIL_SERVER_UNLIMITED:
658 if (stream == 0 && !zerolimit)
659 msg_fatal("service %s requires a process limit of 0",
660 service_name);
661 break;
662 case MAIL_SERVER_PRIVILEGED:
663 if (user_name)
664 msg_fatal("service %s requires privileged operation",
665 service_name);
666 break;
667 case MAIL_SERVER_BOUNCE_INIT:
668 dsn_filter_title = va_arg(ap, const char *);
669 dsn_filter_maps = va_arg(ap, const char **);
670 bounce_client_init(dsn_filter_title, *dsn_filter_maps);
671 break;
672 case MAIL_SERVER_RETIRE_ME:
673 if (retire_me_from_flags > 0)
674 retire_me = retire_me_from_flags;
675 else if (var_idle_limit == 0 || var_use_limit == 0
676 || var_idle_limit > 18000 / var_use_limit)
677 retire_me = 18000;
678 else
679 retire_me = var_idle_limit * var_use_limit;
680 break;
681 default:
682 msg_panic("%s: unknown argument type: %d", myname, key);
683 }
684 }
685 va_end(ap);
686
687 if (root_dir)
688 root_dir = var_queue_dir;
689 if (user_name)
690 user_name = var_mail_owner;
691
692 /*
693 * Can options be required?
694 */
695 if (stream == 0) {
696 if (transport == 0)
697 msg_fatal("no transport type specified");
698 if (strcasecmp(transport, MASTER_XPORT_NAME_INET) == 0)
699 single_server_accept = single_server_accept_inet;
700 else if (strcasecmp(transport, MASTER_XPORT_NAME_UNIX) == 0)
701 single_server_accept = single_server_accept_local;
702 #ifdef MASTER_XPORT_NAME_PASS
703 else if (strcasecmp(transport, MASTER_XPORT_NAME_PASS) == 0)
704 single_server_accept = single_server_accept_pass;
705 #endif
706 else
707 msg_fatal("unsupported transport type: %s", transport);
708 }
709
710 /*
711 * Retrieve process generation from environment.
712 */
713 if ((generation = getenv(MASTER_GEN_NAME)) != 0) {
714 if (!alldig(generation))
715 msg_fatal("bad generation: %s", generation);
716 OCTAL_TO_UNSIGNED(single_server_generation, generation);
717 if (msg_verbose)
718 msg_info("process generation: %s (%o)",
719 generation, single_server_generation);
720 }
721
722 /*
723 * Optionally start the debugger on ourself.
724 */
725 if (debug_me)
726 debug_process();
727
728 /*
729 * Traditionally, BSD select() can't handle multiple processes selecting
730 * on the same socket, and wakes up every process in select(). See TCP/IP
731 * Illustrated volume 2 page 532. We avoid select() collisions with an
732 * external lock file.
733 */
734 if (stream == 0 && !alone) {
735 lock_path = concatenate(DEF_PID_DIR, "/", transport,
736 ".", service_name, (void *) 0);
737 why = vstring_alloc(1);
738 if ((single_server_lock = safe_open(lock_path, O_CREAT | O_RDWR, 0600,
739 (struct stat *) 0, -1, -1, why)) == 0)
740 msg_fatal("open lock file %s: %s", lock_path, vstring_str(why));
741 close_on_exec(vstream_fileno(single_server_lock), CLOSE_ON_EXEC);
742 myfree(lock_path);
743 vstring_free(why);
744 }
745
746 /*
747 * Set up call-back info.
748 */
749 single_server_service = service;
750 single_server_name = service_name;
751 single_server_argv = argv + optind;
752
753 /*
754 * Run pre-jail initialization.
755 */
756 if (chdir(var_queue_dir) < 0)
757 msg_fatal("chdir(\"%s\"): %m", var_queue_dir);
758 if (pre_init)
759 pre_init(single_server_name, single_server_argv);
760
761 /*
762 * Optionally, restrict the damage that this process can do.
763 */
764 resolve_local_init();
765 tzset();
766 chroot_uid(root_dir, user_name);
767
768 /*
769 * Run post-jail initialization.
770 */
771 if (post_init)
772 post_init(single_server_name, single_server_argv);
773
774 /*
775 * Are we running as a one-shot server with the client connection on
776 * standard input? If so, make sure the output is written to stdout so as
777 * to satisfy common expectation.
778 */
779 if (stream != 0) {
780 vstream_control(stream,
781 CA_VSTREAM_CTL_DOUBLE,
782 CA_VSTREAM_CTL_WRITE_FD(STDOUT_FILENO),
783 CA_VSTREAM_CTL_END);
784 service(stream, single_server_name, single_server_argv);
785 vstream_fflush(stream);
786 single_server_exit();
787 }
788
789 /*
790 * Running as a semi-resident server. Service connection requests.
791 * Terminate when we have serviced a sufficient number of clients, when
792 * no-one has been talking to us for a configurable amount of time, or
793 * when the master process terminated abnormally.
794 */
795 if (var_idle_limit > 0)
796 event_request_timer(single_server_timeout, (void *) 0, var_idle_limit);
797 if (retire_me)
798 event_request_timer(single_server_retire, (void *) 0, retire_me);
799 for (fd = MASTER_LISTEN_FD; fd < MASTER_LISTEN_FD + socket_count; fd++) {
800 event_enable_read(fd, single_server_accept, CAST_INT_TO_VOID_PTR(fd));
801 close_on_exec(fd, CLOSE_ON_EXEC);
802 }
803 event_enable_read(MASTER_STATUS_FD, single_server_abort, (void *) 0);
804 close_on_exec(MASTER_STATUS_FD, CLOSE_ON_EXEC);
805 close_on_exec(MASTER_FLOW_READ, CLOSE_ON_EXEC);
806 close_on_exec(MASTER_FLOW_WRITE, CLOSE_ON_EXEC);
807 watchdog = watchdog_create(var_daemon_timeout, (WATCHDOG_FN) 0, (void *) 0);
808
809 /*
810 * The event loop, at last.
811 */
812 while (var_use_limit == 0 || use_count < var_use_limit) {
813 if (single_server_lock != 0) {
814 watchdog_stop(watchdog);
815 if (myflock(vstream_fileno(single_server_lock), INTERNAL_LOCK,
816 MYFLOCK_OP_EXCLUSIVE) < 0)
817 msg_fatal("select lock: %m");
818 }
819 watchdog_start(watchdog);
820 delay = loop ? loop(single_server_name, single_server_argv) : -1;
821 event_loop(delay);
822 }
823 single_server_exit();
824 }
825