1 /* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
2 
3 #include "lib.h"
4 #include "lib-signals.h"
5 #include "lib-event-private.h"
6 #include "event-filter.h"
7 #include "ioloop.h"
8 #include "hostpid.h"
9 #include "path-util.h"
10 #include "array.h"
11 #include "strescape.h"
12 #include "env-util.h"
13 #include "home-expand.h"
14 #include "process-title.h"
15 #include "time-util.h"
16 #include "restrict-access.h"
17 #include "settings-parser.h"
18 #include "syslog-util.h"
19 #include "stats-client.h"
20 #include "master-instance.h"
21 #include "master-login.h"
22 #include "master-service-ssl.h"
23 #include "master-service-private.h"
24 #include "master-service-settings.h"
25 #include "iostream-ssl.h"
26 
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #include <syslog.h>
30 
31 #define DEFAULT_CONFIG_FILE_PATH SYSCONFDIR"/dovecot.conf"
32 
33 /* getenv(MASTER_CONFIG_FILE_ENV) provides path to configuration file/socket */
34 #define MASTER_CONFIG_FILE_ENV "CONFIG_FILE"
35 
36 /* getenv(MASTER_DOVECOT_VERSION_ENV) provides master's version number */
37 #define MASTER_DOVECOT_VERSION_ENV "DOVECOT_VERSION"
38 
39 /* when we're full of connections, how often to check if login state has
40    changed. we normally notice it immediately because of a signal, so this is
41    just a fallback against race conditions. */
42 #define MASTER_SERVICE_STATE_CHECK_MSECS 1000
43 
44 /* If die callback hasn't managed to stop the service for this many seconds,
45    force it. */
46 #define MASTER_SERVICE_DIE_TIMEOUT_MSECS (30*1000)
47 
48 struct master_service *master_service;
49 
50 static struct event_category master_service_category = {
51 	.name = NULL, /* set dynamically later */
52 };
53 static char *master_service_category_name;
54 
55 static void master_service_io_listeners_close(struct master_service *service);
56 static int master_service_get_login_state(enum master_login_state *state_r);
57 static void master_service_refresh_login_state(struct master_service *service);
58 static void
59 master_status_send(struct master_service *service, bool important_update);
60 
master_service_getopt_string(void)61 const char *master_service_getopt_string(void)
62 {
63 	return "c:i:ko:OL";
64 }
65 
sig_die(const siginfo_t * si,void * context)66 static void sig_die(const siginfo_t *si, void *context)
67 {
68 	struct master_service *service = context;
69 
70 	/* SIGINT comes either from master process or from keyboard. we don't
71 	   want to log it in either case.*/
72 	if (si->si_signo != SIGINT) {
73 		i_warning("Killed with signal %d (by pid=%s uid=%s code=%s)",
74 			  si->si_signo, dec2str(si->si_pid),
75 			  dec2str(si->si_uid),
76 			  lib_signal_code_to_str(si->si_signo, si->si_code));
77 	} else if ((service->flags & MASTER_SERVICE_FLAG_NO_IDLE_DIE) != 0) {
78 		/* never die when idling */
79 		return;
80 	} else if ((service->flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
81 		/* SIGINT came from master. die only if we're not handling
82 		   any clients currently. */
83 		if (service->master_status.available_count !=
84 		    service->total_available_count)
85 			return;
86 
87 		if (service->idle_die_callback != NULL &&
88 		    !service->idle_die_callback()) {
89 			/* we don't want to die - send a notification to master
90 			   so it doesn't think we're ignoring it completely. */
91 			master_status_send(service, FALSE);
92 			return;
93 		}
94 	}
95 
96 	service->killed = TRUE;
97 	io_loop_stop(service->ioloop);
98 }
99 
sig_close_listeners(const siginfo_t * si ATTR_UNUSED,void * context)100 static void sig_close_listeners(const siginfo_t *si ATTR_UNUSED, void *context)
101 {
102 	struct master_service *service = context;
103 
104 	/* We're in a signal handler: Close listeners immediately so master
105 	   can successfully restart. We can safely close only those listeners
106 	   that don't have an io, but this shouldn't be a big problem. If there
107 	   is an active io, the service is unlikely to be unresposive for
108 	   longer periods of time, so the listener gets closed soon enough via
109 	   master_status_error().
110 
111 	   For extra safety we don't actually close() the fd, but instead
112 	   replace it with /dev/null. This way it won't be replaced with some
113 	   other new fd and attempted to be used in unexpected ways. */
114 	for (unsigned int i = 0; i < service->socket_count; i++) {
115 		if (service->listeners[i].fd != -1 &&
116 		    service->listeners[i].io == NULL) {
117 			if (dup2(dev_null_fd, service->listeners[i].fd) < 0)
118 				lib_signals_syscall_error("signal: dup2(/dev/null, listener) failed: ");
119 			service->listeners[i].closed = TRUE;
120 		}
121 	}
122 }
123 
124 static void
sig_state_changed(const siginfo_t * si ATTR_UNUSED,void * context)125 sig_state_changed(const siginfo_t *si ATTR_UNUSED, void *context)
126 {
127 	struct master_service *service = context;
128 
129 	master_service_refresh_login_state(service);
130 }
131 
132 static bool
master_service_event_callback(struct event * event,enum event_callback_type type,struct failure_context * ctx ATTR_UNUSED,const char * fmt ATTR_UNUSED,va_list args ATTR_UNUSED)133 master_service_event_callback(struct event *event,
134 			      enum event_callback_type type,
135 			      struct failure_context *ctx ATTR_UNUSED,
136 			      const char *fmt ATTR_UNUSED,
137 			      va_list args ATTR_UNUSED)
138 {
139 	if (type == EVENT_CALLBACK_TYPE_CREATE && event->parent == NULL) {
140 		/* Add service:<name> category for all events. It's enough
141 		   to do it only for root events, because all other events
142 		   inherit the category from them. */
143 		event_add_category(event, &master_service_category);
144 	}
145 	return TRUE;
146 }
147 
master_service_verify_version_string(struct master_service * service)148 static void master_service_verify_version_string(struct master_service *service)
149 {
150 	if (service->version_string != NULL &&
151 	    strcmp(service->version_string, PACKAGE_VERSION) != 0) {
152 		i_fatal("Dovecot version mismatch: "
153 			"Master is v%s, %s is v"PACKAGE_VERSION" "
154 			"(if you don't care, set version_ignore=yes)",
155 			service->version_string, service->name);
156 	}
157 }
158 
master_service_init_socket_listeners(struct master_service * service)159 static void master_service_init_socket_listeners(struct master_service *service)
160 {
161 	unsigned int i;
162 	const char *value;
163 	bool have_ssl_sockets = FALSE;
164 
165 	if (service->socket_count == 0)
166 		return;
167 
168 	service->listeners =
169 		i_new(struct master_service_listener, service->socket_count);
170 
171 	for (i = 0; i < service->socket_count; i++) {
172 		struct master_service_listener *l = &service->listeners[i];
173 
174 		l->service = service;
175 		l->fd = MASTER_LISTEN_FD_FIRST + i;
176 
177 		value = getenv(t_strdup_printf("SOCKET%u_SETTINGS", i));
178 		if (value != NULL) {
179 			const char *const *settings =
180 				t_strsplit_tabescaped(value);
181 
182 			if (*settings != NULL) {
183 				l->name = i_strdup_empty(*settings);
184 				settings++;
185 			}
186 			while (*settings != NULL) {
187 				if (strcmp(*settings, "ssl") == 0) {
188 					l->ssl = TRUE;
189 					have_ssl_sockets = TRUE;
190 				} else if (strcmp(*settings, "haproxy") == 0) {
191 					l->haproxy = TRUE;
192 				}
193 				settings++;
194 			}
195 		}
196 	}
197 	service->want_ssl_server = have_ssl_sockets ||
198 		(service->flags & MASTER_SERVICE_FLAG_HAVE_STARTTLS) != 0;
199 }
200 
201 struct master_service *
master_service_init(const char * name,enum master_service_flags flags,int * argc,char ** argv[],const char * getopt_str)202 master_service_init(const char *name, enum master_service_flags flags,
203 		    int *argc, char **argv[], const char *getopt_str)
204 {
205 	struct master_service *service;
206 	data_stack_frame_t datastack_frame_id = 0;
207 	unsigned int count;
208 	const char *service_configured_name, *value;
209 
210 	i_assert(name != NULL);
211 
212 #ifdef DEBUG
213 	if (getenv("GDB") == NULL &&
214 	    (flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
215 		value = getenv("SOCKET_COUNT");
216 		if (value == NULL || str_to_uint(value, &count) < 0)
217 			count = 0;
218 		fd_debug_verify_leaks(MASTER_LISTEN_FD_FIRST + count, 1024);
219 	}
220 #endif
221 	if ((flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
222 		/* make sure we can dump core, at least until
223 		   privileges are dropped. (i'm not really sure why this
224 		   is needed, because doing the same just before exec
225 		   doesn't help, and exec shouldn't affect this with
226 		   non-setuid/gid binaries..) */
227 		restrict_access_allow_coredumps(TRUE);
228 	}
229 
230 	/* NOTE: we start rooted, so keep the code minimal until
231 	   restrict_access_by_env() is called */
232 	lib_init();
233 	/* Get the service name from environment. This usually differs from the
234 	   service name parameter if the executable is used for multiple
235 	   services. For example "auth" vs "auth-worker". It can also be a
236 	   service with slightly different settings, like "lmtp" vs
237 	   "lmtp-no-quota". We don't want to use the configured name as the
238 	   service's primary name, because that could break some lookups (e.g.
239 	   auth would suddenly see service=lmtp-no-quota. However, this can be
240 	   very useful in events to differentiate e.g. auth master and
241 	   auth-worker events which might otherwise look very similar. It's
242 	   also useful in log prefixes. */
243 	service_configured_name = getenv(MASTER_SERVICE_ENV);
244 	if (service_configured_name == NULL)
245 		service_configured_name = name;
246 	/* Set a logging prefix temporarily. This will be ignored once the log
247 	   is properly initialized */
248 	i_set_failure_prefix("%s(init): ", service_configured_name);
249 
250 	/* make sure all the data stack allocations during init will be freed
251 	   before we get to ioloop. the corresponding t_pop() is in
252 	   master_service_init_finish(). */
253 	if ((flags & MASTER_SERVICE_FLAG_NO_INIT_DATASTACK_FRAME) == 0)
254 		datastack_frame_id = t_push("master_service_init");
255 
256 	/* ignore these signals as early as possible */
257 	lib_signals_init();
258         lib_signals_ignore(SIGPIPE, TRUE);
259         lib_signals_ignore(SIGALRM, FALSE);
260 
261 	if (getenv(MASTER_UID_ENV) == NULL)
262 		flags |= MASTER_SERVICE_FLAG_STANDALONE;
263 
264 	process_title_init(*argc, argv);
265 
266 	/* process_title_init() might destroy all environments.
267 	   Need to look this up again. */
268 	service_configured_name = getenv(MASTER_SERVICE_ENV);
269 	if (service_configured_name == NULL)
270 		service_configured_name = name;
271 
272 	service = i_new(struct master_service, 1);
273 	service->argc = *argc;
274 	service->argv = *argv;
275 	service->name = i_strdup(name);
276 	service->configured_name = i_strdup(service_configured_name);
277 	/* keep getopt_str first in case it contains "+" */
278 	service->getopt_str = *getopt_str == '\0' ?
279 		i_strdup(master_service_getopt_string()) :
280 		i_strconcat(getopt_str, master_service_getopt_string(), NULL);
281 	service->flags = flags;
282 	service->ioloop = io_loop_create();
283 	service->service_count_left = UINT_MAX;
284 	service->config_fd = -1;
285 	service->datastack_frame_id = datastack_frame_id;
286 
287 	service->config_path = i_strdup(getenv(MASTER_CONFIG_FILE_ENV));
288 	if (service->config_path == NULL)
289 		service->config_path = i_strdup(DEFAULT_CONFIG_FILE_PATH);
290 	else
291 		service->config_path_from_master = TRUE;
292 
293 	if ((flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
294 		service->version_string = getenv(MASTER_DOVECOT_VERSION_ENV);
295 		service->socket_count = 1;
296 	} else {
297 		service->version_string = PACKAGE_VERSION;
298 	}
299 
300 	/* listener configuration */
301 	value = getenv("SOCKET_COUNT");
302 	if (value != NULL && str_to_uint(value, &service->socket_count) < 0)
303 		i_fatal("Invalid SOCKET_COUNT environment");
304 	T_BEGIN {
305 		master_service_init_socket_listeners(service);
306 	} T_END;
307 
308 #ifdef HAVE_SSL
309 	/* Load the SSL module if we already know it is necessary. It can also
310 	   get loaded later on-demand. */
311 	if (service->want_ssl_server) {
312 		const char *error;
313 		if (ssl_module_load(&error) < 0)
314 			i_fatal("Cannot load SSL module: %s", error);
315 	}
316 #endif
317 
318 	/* set up some kind of logging until we know exactly how and where
319 	   we want to log */
320 	if (getenv("LOG_SERVICE") != NULL)
321 		i_set_failure_internal();
322 	if (getenv("USER") != NULL) {
323 		i_set_failure_prefix("%s(%s): ", service->configured_name,
324 				     getenv("USER"));
325 	} else {
326 		i_set_failure_prefix("%s: ", service->configured_name);
327 	}
328 
329 	master_service_category_name =
330 		i_strdup_printf("service:%s", service->configured_name);
331 	master_service_category.name = master_service_category_name;
332 	event_register_callback(master_service_event_callback);
333 
334 	/* Initialize debug logging */
335 	value = getenv(DOVECOT_LOG_DEBUG_ENV);
336 	if (value != NULL) {
337 		struct event_filter *filter;
338 		const char *error;
339 		filter = event_filter_create();
340 		if (event_filter_parse(value, filter, &error) < 0) {
341 			i_error("Invalid "DOVECOT_LOG_DEBUG_ENV" - ignoring: %s",
342 				error);
343 		} else {
344 			event_set_global_debug_log_filter(filter);
345 		}
346 		event_filter_unref(&filter);
347 	}
348 
349 	if ((flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
350 		/* initialize master_status structure */
351 		value = getenv(MASTER_UID_ENV);
352 		if (value == NULL ||
353 		    str_to_uint(value, &service->master_status.uid) < 0)
354 			i_fatal(MASTER_UID_ENV" missing");
355 		service->master_status.pid = getpid();
356 
357 		/* set the default limit */
358 		value = getenv(MASTER_CLIENT_LIMIT_ENV);
359 		if (value == NULL || str_to_uint(value, &count) < 0 ||
360 		    count == 0)
361 			i_fatal(MASTER_CLIENT_LIMIT_ENV" missing");
362 		master_service_set_client_limit(service, count);
363 
364 		/* seve the process limit */
365 		value = getenv(MASTER_PROCESS_LIMIT_ENV);
366 		if (value != NULL && str_to_uint(value, &count) == 0 &&
367 		    count > 0)
368 			service->process_limit = count;
369 
370 		value = getenv(MASTER_PROCESS_MIN_AVAIL_ENV);
371 		if (value != NULL && str_to_uint(value, &count) == 0 &&
372 		    count > 0)
373 			service->process_min_avail = count;
374 
375 		/* set the default service count */
376 		value = getenv(MASTER_SERVICE_COUNT_ENV);
377 		if (value != NULL && str_to_uint(value, &count) == 0 &&
378 		    count > 0)
379 			master_service_set_service_count(service, count);
380 
381 		/* set the idle kill timeout */
382 		value = getenv(MASTER_SERVICE_IDLE_KILL_ENV);
383 		if (value != NULL && str_to_uint(value, &count) == 0)
384 			service->idle_kill_secs = count;
385 	} else {
386 		master_service_set_client_limit(service, 1);
387 		master_service_set_service_count(service, 1);
388 	}
389 	if ((flags & MASTER_SERVICE_FLAG_KEEP_CONFIG_OPEN) != 0) {
390 		/* since we're going to keep the config socket open anyway,
391 		   open it now so we can read settings even after privileges
392 		   are dropped. */
393 		master_service_config_socket_try_open(service);
394 	}
395 	if ((flags & MASTER_SERVICE_FLAG_DONT_SEND_STATS) == 0) {
396 		/* Initialize stats-client early so it can see all events. */
397 		value = getenv(DOVECOT_STATS_WRITER_SOCKET_PATH);
398 		if (value != NULL && value[0] != '\0')
399 			service->stats_client = stats_client_init(value, FALSE);
400 	}
401 
402 	master_service_verify_version_string(service);
403 	return service;
404 }
405 
master_getopt(struct master_service * service)406 int master_getopt(struct master_service *service)
407 {
408 	int c;
409 
410 	i_assert(master_getopt_str_is_valid(service->getopt_str));
411 
412 	while ((c = getopt(service->argc, service->argv,
413 			   service->getopt_str)) > 0) {
414 		if (!master_service_parse_option(service, c, optarg))
415 			break;
416 	}
417 	return c;
418 }
419 
master_getopt_str_is_valid(const char * str)420 bool master_getopt_str_is_valid(const char *str)
421 {
422 	unsigned int i, j;
423 
424 	/* make sure there are no duplicates. there are few enough characters
425 	   that this should be fast enough. */
426 	for (i = 0; str[i] != '\0'; i++) {
427 		if (str[i] == ':' || str[i] == '+' || str[i] == '-')
428 			continue;
429 		for (j = i+1; str[j] != '\0'; j++) {
430 			if (str[i] == str[j])
431 				return FALSE;
432 		}
433 	}
434 	return TRUE;
435 }
436 
437 static bool
master_service_try_init_log(struct master_service * service,const char * prefix)438 master_service_try_init_log(struct master_service *service,
439 			    const char *prefix)
440 {
441 	const char *path, *timestamp;
442 
443 	if ((service->flags & MASTER_SERVICE_FLAG_STANDALONE) != 0 &&
444 	    (service->flags & MASTER_SERVICE_FLAG_DONT_LOG_TO_STDERR) == 0) {
445 		timestamp = getenv("LOG_STDERR_TIMESTAMP");
446 		if (timestamp != NULL)
447 			i_set_failure_timestamp_format(timestamp);
448 		i_set_failure_file("/dev/stderr", "");
449 		return TRUE;
450 	}
451 
452 	if (getenv("LOG_SERVICE") != NULL && !service->log_directly) {
453 		/* logging via log service */
454 		i_set_failure_internal();
455 		i_set_failure_prefix("%s", prefix);
456 		return TRUE;
457 	}
458 
459 	if (service->set == NULL) {
460 		i_set_failure_file("/dev/stderr", prefix);
461 		/* may be called again after we have settings */
462 		return FALSE;
463 	}
464 
465 	if (strcmp(service->set->log_path, "syslog") != 0) {
466 		/* error logging goes to file or stderr */
467 		path = home_expand(service->set->log_path);
468 		i_set_failure_file(path, prefix);
469 	}
470 
471 	if (strcmp(service->set->log_path, "syslog") == 0 ||
472 	    strcmp(service->set->info_log_path, "syslog") == 0 ||
473 	    strcmp(service->set->debug_log_path, "syslog") == 0) {
474 		/* something gets logged to syslog */
475 		int facility;
476 
477 		if (!syslog_facility_find(service->set->syslog_facility,
478 					  &facility))
479 			facility = LOG_MAIL;
480 		i_set_failure_syslog(service->set->instance_name, LOG_NDELAY,
481 		                     facility);
482 		i_set_failure_prefix("%s", prefix);
483 
484 		if (strcmp(service->set->log_path, "syslog") != 0) {
485 			/* set error handlers back to file */
486 			i_set_fatal_handler(default_fatal_handler);
487 			i_set_error_handler(default_error_handler);
488 		}
489 	}
490 
491 	if (*service->set->info_log_path != '\0' &&
492 	    strcmp(service->set->info_log_path, "syslog") != 0) {
493 		path = home_expand(service->set->info_log_path);
494 		if (*path != '\0')
495 			i_set_info_file(path);
496 	}
497 
498 	if (*service->set->debug_log_path != '\0' &&
499 	    strcmp(service->set->debug_log_path, "syslog") != 0) {
500 		path = home_expand(service->set->debug_log_path);
501 		if (*path != '\0')
502 			i_set_debug_file(path);
503 	}
504 	i_set_failure_timestamp_format(service->set->log_timestamp);
505 	return TRUE;
506 }
507 
master_service_init_log(struct master_service * service)508 void master_service_init_log(struct master_service *service)
509 {
510 	master_service_init_log_with_prefix(service, t_strdup_printf(
511 		"%s: ", service->configured_name));
512 }
513 
master_service_init_log_with_prefix(struct master_service * service,const char * prefix)514 void master_service_init_log_with_prefix(struct master_service *service,
515 					 const char *prefix)
516 {
517 	if (service->log_initialized) {
518 		/* change only the prefix */
519 		i_set_failure_prefix("%s", prefix);
520 		return;
521 	}
522 	if (master_service_try_init_log(service, prefix))
523 		service->log_initialized = TRUE;
524 }
525 
master_service_init_log_with_pid(struct master_service * service)526 void master_service_init_log_with_pid(struct master_service *service)
527 {
528 	master_service_init_log_with_prefix(service, t_strdup_printf(
529 		"%s(%s): ", service->configured_name, my_pid));
530 }
531 
master_service_init_stats_client(struct master_service * service,bool silent_notfound_errors)532 void master_service_init_stats_client(struct master_service *service,
533 				      bool silent_notfound_errors)
534 {
535 	if (service->stats_client == NULL &&
536 	    service->set->stats_writer_socket_path[0] != '\0') T_BEGIN {
537 		const char *path = t_strdup_printf("%s/%s",
538 			service->set->base_dir,
539 			service->set->stats_writer_socket_path);
540 		service->stats_client =
541 			stats_client_init(path, silent_notfound_errors);
542 	} T_END;
543 }
544 
master_service_set_die_with_master(struct master_service * service,bool set)545 void master_service_set_die_with_master(struct master_service *service,
546 					bool set)
547 {
548 	service->die_with_master = set;
549 }
550 
master_service_set_die_callback(struct master_service * service,void (* callback)(void))551 void master_service_set_die_callback(struct master_service *service,
552 				     void (*callback)(void))
553 {
554 	service->die_callback = callback;
555 }
556 
master_service_set_idle_die_callback(struct master_service * service,bool (* callback)(void))557 void master_service_set_idle_die_callback(struct master_service *service,
558 					  bool (*callback)(void))
559 {
560 	service->idle_die_callback = callback;
561 }
562 
get_instance_config(const char * name,const char ** config_path_r)563 static bool get_instance_config(const char *name, const char **config_path_r)
564 {
565 	struct master_instance_list *list;
566 	const struct master_instance *inst;
567 	const char *instance_path, *path;
568 
569 	/* note that we don't have any settings yet. we're just finding out
570 	   which dovecot.conf we even want to read! so we must use the
571 	   hardcoded state_dir path. */
572 	instance_path = t_strconcat(PKG_STATEDIR"/"MASTER_INSTANCE_FNAME, NULL);
573 	list = master_instance_list_init(instance_path);
574 	inst = master_instance_list_find_by_name(list, name);
575 	if (inst != NULL) {
576 		path = t_strdup_printf("%s/dovecot.conf", inst->base_dir);
577 		const char *error;
578 		if (t_readlink(path, config_path_r, &error) < 0)
579 			i_fatal("t_readlink(%s) failed: %s", path, error);
580 	}
581 	master_instance_list_deinit(&list);
582 	return inst != NULL;
583 }
584 
master_service_parse_option(struct master_service * service,int opt,const char * arg)585 bool master_service_parse_option(struct master_service *service,
586 				 int opt, const char *arg)
587 {
588 	const char *path;
589 
590 	switch (opt) {
591 	case 'c':
592 		i_free(service->config_path);
593 		service->config_path = i_strdup(arg);
594 		service->config_path_changed_with_param = TRUE;
595 		service->config_path_from_master = FALSE;
596 		break;
597 	case 'i':
598 		if (!get_instance_config(arg, &path))
599 			i_fatal("Unknown instance name: %s", arg);
600 		service->config_path = i_strdup(path);
601 		service->config_path_changed_with_param = TRUE;
602 		break;
603 	case 'k':
604 		service->keep_environment = TRUE;
605 		break;
606 	case 'o':
607 		if (!array_is_created(&service->config_overrides))
608 			i_array_init(&service->config_overrides, 16);
609 		array_push_back(&service->config_overrides, &arg);
610 		break;
611 	case 'O':
612 		service->flags |= MASTER_SERVICE_FLAG_NO_CONFIG_SETTINGS;
613 		break;
614 	case 'L':
615 		service->log_directly = TRUE;
616 		break;
617 	default:
618 		return FALSE;
619 	}
620 	return TRUE;
621 }
622 
master_service_error(struct master_service * service)623 static void master_service_error(struct master_service *service)
624 {
625 	master_service_stop_new_connections(service);
626 	if (service->master_status.available_count ==
627 	    service->total_available_count || service->die_with_master) {
628 		if (service->die_callback == NULL)
629 			master_service_stop(service);
630 		else {
631 			service->to_die =
632 				timeout_add(MASTER_SERVICE_DIE_TIMEOUT_MSECS,
633 					    master_service_stop,
634 					    service);
635 			service->die_callback();
636 		}
637 	}
638 }
639 
master_status_error(struct master_service * service)640 static void master_status_error(struct master_service *service)
641 {
642 	/* status fd is a write-only pipe, so if we're here it means the
643 	   master wants us to die (or died itself). don't die until all
644 	   service connections are finished. */
645 	io_remove(&service->io_status_error);
646 
647 	/* the log fd may also be closed already, don't die when trying to
648 	   log later */
649 	i_set_failure_ignore_errors(TRUE);
650 
651 	master_service_error(service);
652 }
653 
master_service_init_finish(struct master_service * service)654 void master_service_init_finish(struct master_service *service)
655 {
656 	enum libsig_flags sigint_flags = LIBSIG_FLAG_DELAYED;
657 	struct stat st;
658 
659 	i_assert(!service->init_finished);
660 	service->init_finished = TRUE;
661 
662 	/* From now on we'll abort() if exit() is called unexpectedly. */
663 	lib_set_clean_exit(FALSE);
664 
665 	/* set default signal handlers */
666 	if ((service->flags & MASTER_SERVICE_FLAG_STANDALONE) == 0)
667 		sigint_flags |= LIBSIG_FLAG_RESTART;
668         lib_signals_set_handler(SIGINT, sigint_flags, sig_die, service);
669 	lib_signals_set_handler(SIGTERM, LIBSIG_FLAG_DELAYED, sig_die, service);
670 	if ((service->flags & MASTER_SERVICE_FLAG_TRACK_LOGIN_STATE) != 0) {
671 		lib_signals_set_handler(SIGUSR1, LIBSIG_FLAGS_SAFE,
672 					sig_state_changed, service);
673 	}
674 
675 	if ((service->flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
676 		if (fstat(MASTER_STATUS_FD, &st) < 0 || !S_ISFIFO(st.st_mode))
677 			i_fatal("Must be started by dovecot master process");
678 
679 		/* start listening errors for status fd, it means master died */
680 		service->io_status_error = io_add(MASTER_DEAD_FD, IO_ERROR,
681 						  master_status_error, service);
682 		lib_signals_set_handler(SIGQUIT, 0, sig_close_listeners, service);
683 	}
684 	master_service_io_listeners_add(service);
685 	if (service->want_ssl_server &&
686 	    (service->flags & MASTER_SERVICE_FLAG_NO_SSL_INIT) == 0)
687 		master_service_ssl_ctx_init(service);
688 
689 	if ((service->flags & MASTER_SERVICE_FLAG_STD_CLIENT) != 0) {
690 		/* we already have a connection to be served */
691 		service->master_status.available_count--;
692 	}
693 	master_status_update(service);
694 
695 	/* close data stack frame opened by master_service_init() */
696 	if ((service->flags & MASTER_SERVICE_FLAG_NO_INIT_DATASTACK_FRAME) == 0) {
697 		if (!t_pop(&service->datastack_frame_id))
698 			i_panic("Leaked t_pop() call");
699 	}
700 }
701 
master_service_import_environment_real(const char * import_environment)702 static void master_service_import_environment_real(const char *import_environment)
703 {
704 	const char *const *envs, *key, *value;
705 	ARRAY_TYPE(const_string) keys;
706 
707 	if (*import_environment == '\0')
708 		return;
709 
710 	t_array_init(&keys, 8);
711 	/* preserve existing DOVECOT_PRESERVE_ENVS */
712 	value = getenv(DOVECOT_PRESERVE_ENVS_ENV);
713 	if (value != NULL)
714 		array_push_back(&keys, &value);
715 #ifdef HAVE_LIBSYSTEMD
716 	/* Always import systemd variables, otherwise it is possible to break
717 	   systemd startup in obscure ways. */
718 	value = "NOTIFY_SOCKET LISTEN_FDS LISTEN_PID";
719 	array_push_back(&keys, &value);
720 #endif
721 	/* add new environments */
722 	envs = t_strsplit_spaces(import_environment, " ");
723 	for (; *envs != NULL; envs++) {
724 		value = strchr(*envs, '=');
725 		if (value == NULL)
726 			key = *envs;
727 		else {
728 			key = t_strdup_until(*envs, value++);
729 			env_put(key, value);
730 		}
731 		array_push_back(&keys, &key);
732 	}
733 	array_append_zero(&keys);
734 
735 	value = t_strarray_join(array_front(&keys), " ");
736 	env_put(DOVECOT_PRESERVE_ENVS_ENV, value);
737 }
738 
master_service_import_environment(const char * import_environment)739 void master_service_import_environment(const char *import_environment)
740 {
741 	T_BEGIN {
742 		master_service_import_environment_real(import_environment);
743 	} T_END;
744 }
745 
master_service_env_clean(void)746 void master_service_env_clean(void)
747 {
748 	const char *value = getenv(DOVECOT_PRESERVE_ENVS_ENV);
749 
750 	if (value == NULL || *value == '\0')
751 		env_clean();
752 	else T_BEGIN {
753 		value = t_strconcat(value, " "DOVECOT_PRESERVE_ENVS_ENV, NULL);
754 		env_clean_except(t_strsplit_spaces(value, " "));
755 	} T_END;
756 }
757 
master_service_set_client_limit(struct master_service * service,unsigned int client_limit)758 void master_service_set_client_limit(struct master_service *service,
759 				     unsigned int client_limit)
760 {
761 	unsigned int used;
762 
763 	i_assert(service->master_status.available_count ==
764 		 service->total_available_count);
765 
766 	used = service->total_available_count -
767 		service->master_status.available_count;
768 	i_assert(client_limit >= used);
769 
770 	service->total_available_count = client_limit;
771 	service->master_status.available_count = client_limit - used;
772 }
773 
master_service_get_client_limit(struct master_service * service)774 unsigned int master_service_get_client_limit(struct master_service *service)
775 {
776 	return service->total_available_count;
777 }
778 
master_service_get_process_limit(struct master_service * service)779 unsigned int master_service_get_process_limit(struct master_service *service)
780 {
781 	return service->process_limit;
782 }
783 
master_service_get_process_min_avail(struct master_service * service)784 unsigned int master_service_get_process_min_avail(struct master_service *service)
785 {
786 	return service->process_min_avail;
787 }
788 
master_service_get_idle_kill_secs(struct master_service * service)789 unsigned int master_service_get_idle_kill_secs(struct master_service *service)
790 {
791 	return service->idle_kill_secs;
792 }
793 
master_service_set_service_count(struct master_service * service,unsigned int count)794 void master_service_set_service_count(struct master_service *service,
795 				      unsigned int count)
796 {
797 	unsigned int used;
798 
799 	used = service->total_available_count -
800 		service->master_status.available_count;
801 	i_assert(count >= used);
802 
803 	if (service->total_available_count > count) {
804 		service->total_available_count = count;
805 		service->master_status.available_count = count - used;
806 	}
807 	service->service_count_left = count;
808 }
809 
master_service_get_service_count(struct master_service * service)810 unsigned int master_service_get_service_count(struct master_service *service)
811 {
812 	return service->service_count_left;
813 }
814 
master_service_get_socket_count(struct master_service * service)815 unsigned int master_service_get_socket_count(struct master_service *service)
816 {
817 	return service->socket_count;
818 }
819 
master_service_get_socket_name(struct master_service * service,int listen_fd)820 const char *master_service_get_socket_name(struct master_service *service,
821 					   int listen_fd)
822 {
823 	unsigned int i;
824 
825 	i_assert(listen_fd >= MASTER_LISTEN_FD_FIRST);
826 
827 	i = listen_fd - MASTER_LISTEN_FD_FIRST;
828 	i_assert(i < service->socket_count);
829 	return service->listeners[i].name != NULL ?
830 		service->listeners[i].name : "";
831 }
832 
master_service_set_avail_overflow_callback(struct master_service * service,master_service_avail_overflow_callback_t * callback)833 void master_service_set_avail_overflow_callback(struct master_service *service,
834 	master_service_avail_overflow_callback_t *callback)
835 {
836 	service->avail_overflow_callback = callback;
837 }
838 
master_service_get_config_path(struct master_service * service)839 const char *master_service_get_config_path(struct master_service *service)
840 {
841 	return service->config_path;
842 }
843 
master_service_get_version_string(struct master_service * service)844 const char *master_service_get_version_string(struct master_service *service)
845 {
846 	return service->version_string;
847 }
848 
master_service_get_name(struct master_service * service)849 const char *master_service_get_name(struct master_service *service)
850 {
851 	return service->name;
852 }
853 
master_service_get_configured_name(struct master_service * service)854 const char *master_service_get_configured_name(struct master_service *service)
855 {
856 	return service->configured_name;
857 }
858 
master_service_run(struct master_service * service,master_service_connection_callback_t * callback)859 void master_service_run(struct master_service *service,
860 			master_service_connection_callback_t *callback)
861 {
862 	service->callback = callback;
863 	io_loop_run(service->ioloop);
864 	service->callback = NULL;
865 }
866 
master_service_stop(struct master_service * service)867 void master_service_stop(struct master_service *service)
868 {
869         io_loop_stop(service->ioloop);
870 }
871 
master_service_stop_new_connections(struct master_service * service)872 void master_service_stop_new_connections(struct master_service *service)
873 {
874 	unsigned int current_count;
875 
876 	if (service->stopping)
877 		return;
878 
879 	service->stopping = TRUE;
880 	master_service_io_listeners_remove(service);
881 	master_service_io_listeners_close(service);
882 
883 	/* make sure we stop after servicing current connections */
884 	current_count = service->total_available_count -
885 		service->master_status.available_count;
886 	service->service_count_left = current_count;
887 	service->total_available_count = current_count;
888 
889 	if (current_count == 0)
890 		master_service_stop(service);
891 	else {
892 		/* notify master that we're not accepting any more
893 		   connections */
894 		service->master_status.available_count = 0;
895 		master_status_update(service);
896 	}
897 	if (service->login != NULL)
898 		master_login_stop(service->login);
899 }
900 
master_service_is_killed(struct master_service * service)901 bool master_service_is_killed(struct master_service *service)
902 {
903 	return service->killed;
904 }
905 
master_service_is_master_stopped(struct master_service * service)906 bool master_service_is_master_stopped(struct master_service *service)
907 {
908 	return service->io_status_error == NULL &&
909 		(service->flags & MASTER_SERVICE_FLAG_STANDALONE) == 0;
910 }
911 
master_service_anvil_send(struct master_service * service,const char * cmd)912 void master_service_anvil_send(struct master_service *service, const char *cmd)
913 {
914 	ssize_t ret;
915 
916 	if ((service->flags & MASTER_SERVICE_FLAG_STANDALONE) != 0)
917 		return;
918 
919 	ret = write(MASTER_ANVIL_FD, cmd, strlen(cmd));
920 	if (ret < 0) {
921 		if (errno == EPIPE) {
922 			/* anvil process was probably recreated, don't bother
923 			   logging an error about losing connection to it */
924 			return;
925 		}
926 		i_error("write(anvil) failed: %m");
927 	} else if (ret == 0)
928 		i_error("write(anvil) failed: EOF");
929 	else {
930 		i_assert((size_t)ret == strlen(cmd));
931 	}
932 }
933 
master_service_client_connection_created(struct master_service * service)934 void master_service_client_connection_created(struct master_service *service)
935 {
936 	i_assert(service->master_status.available_count > 0);
937 	service->master_status.available_count--;
938 	master_status_update(service);
939 }
940 
master_service_want_listener(struct master_service * service)941 static bool master_service_want_listener(struct master_service *service)
942 {
943 	if (service->master_status.available_count > 0) {
944 		/* more concurrent clients can still be added */
945 		return TRUE;
946 	}
947 	if (service->service_count_left == 1) {
948 		/* after handling this client, the whole process will stop. */
949 		return FALSE;
950 	}
951 	if (service->avail_overflow_callback != NULL) {
952 		/* overflow callback is set. it's possible that the current
953 		   existing client may be replaced by a new client, which needs
954 		   the listener to try to accept new connections. */
955 		return TRUE;
956 	}
957 	/* the listener isn't needed until the current client is disconnected */
958 	return FALSE;
959 }
960 
master_service_client_connection_handled(struct master_service * service,struct master_service_connection * conn)961 void master_service_client_connection_handled(struct master_service *service,
962 					      struct master_service_connection *conn)
963 {
964 	if (!conn->accepted) {
965 		if (close(conn->fd) < 0)
966 			i_error("close(service connection) failed: %m");
967 		master_service_client_connection_destroyed(service);
968 	} else if (conn->fifo) {
969 		/* reading FIFOs stays open forever, don't count them
970 		   as real clients */
971 		master_service_client_connection_destroyed(service);
972 	}
973 	if (!master_service_want_listener(service)) {
974 		i_assert(service->listeners != NULL);
975 		master_service_io_listeners_remove(service);
976 		if (service->service_count_left == 1 &&
977 		   service->avail_overflow_callback == NULL) {
978 			/* we're not going to accept any more connections after
979 			   this. go ahead and close the connection early. don't
980 			   do this before calling callback, because it may want
981 			   to access the listen_fd (e.g. to check socket
982 			   permissions).
983 
984 			   Don't do this if overflow callback is set, because
985 			   otherwise it's never called with service_count=1.
986 			   Actually this isn't important anymore to do with
987 			   any service, since nowadays master can request the
988 			   listeners to be closed via SIGQUIT. Still, closing
989 			   the fd when possible saves a little bit of memory. */
990 			master_service_io_listeners_close(service);
991 		}
992 	}
993 }
994 
master_service_client_connection_callback(struct master_service * service,struct master_service_connection * conn)995 void master_service_client_connection_callback(struct master_service *service,
996 					       struct master_service_connection *conn)
997 {
998 	service->callback(conn);
999 
1000 	master_service_client_connection_handled(service, conn);
1001 }
1002 
master_service_client_connection_accept(struct master_service_connection * conn)1003 void master_service_client_connection_accept(struct master_service_connection *conn)
1004 {
1005 	conn->accepted = TRUE;
1006 }
1007 
master_service_client_connection_destroyed(struct master_service * service)1008 void master_service_client_connection_destroyed(struct master_service *service)
1009 {
1010 	/* we can listen again */
1011 	master_service_io_listeners_add(service);
1012 
1013 	i_assert(service->total_available_count > 0);
1014 	i_assert(service->service_count_left > 0);
1015 
1016 	if (service->service_count_left == service->total_available_count) {
1017 		service->total_available_count--;
1018                 service->service_count_left--;
1019 	} else {
1020 		if (service->service_count_left != UINT_MAX)
1021 			service->service_count_left--;
1022 
1023 		i_assert(service->master_status.available_count <
1024 			 service->total_available_count);
1025 		service->master_status.available_count++;
1026 	}
1027 
1028 	if (service->service_count_left == 0) {
1029 		i_assert(service->master_status.available_count ==
1030 			 service->total_available_count);
1031 		master_service_stop(service);
1032 	} else if ((service->io_status_error == NULL ||
1033 		    service->listeners == NULL) &&
1034 		   service->master_status.available_count ==
1035 		   service->total_available_count) {
1036 		/* we've finished handling all clients, and
1037 		   a) master has closed the connection
1038 		   b) there are no listeners (std-client?) */
1039 		master_service_stop(service);
1040 	} else {
1041 		master_status_update(service);
1042 	}
1043 }
1044 
master_service_set_login_state(struct master_service * service,enum master_login_state state)1045 static void master_service_set_login_state(struct master_service *service,
1046 					   enum master_login_state state)
1047 {
1048 	timeout_remove(&service->to_overflow_state);
1049 
1050 	switch (state) {
1051 	case MASTER_LOGIN_STATE_NONFULL:
1052 		service->call_avail_overflow = FALSE;
1053 		if (service->master_status.available_count > 0)
1054 			return;
1055 
1056 		/* some processes should now be able to handle new connections,
1057 		   although we can't. but there may be race conditions, so
1058 		   make sure that we'll check again soon if the state has
1059 		   changed to "full" without our knowledge. */
1060 		service->to_overflow_state =
1061 			timeout_add(MASTER_SERVICE_STATE_CHECK_MSECS,
1062 				    master_service_refresh_login_state,
1063 				    service);
1064 		return;
1065 	case MASTER_LOGIN_STATE_FULL:
1066 		/* make sure we're listening for more connections */
1067 		service->call_avail_overflow = TRUE;
1068 		master_service_io_listeners_add(service);
1069 		return;
1070 	}
1071 	i_error("Invalid master login state: %d", state);
1072 }
1073 
master_service_get_login_state(enum master_login_state * state_r)1074 static int master_service_get_login_state(enum master_login_state *state_r)
1075 {
1076 	off_t ret;
1077 
1078 	ret = lseek(MASTER_LOGIN_NOTIFY_FD, 0, SEEK_CUR);
1079 	if (ret < 0) {
1080 		i_error("lseek(login notify fd) failed: %m");
1081 		return -1;
1082 	}
1083 	*state_r = ret == MASTER_LOGIN_STATE_FULL ?
1084 		MASTER_LOGIN_STATE_FULL : MASTER_LOGIN_STATE_NONFULL;
1085 	return 0;
1086 }
1087 
master_service_refresh_login_state(struct master_service * service)1088 static void master_service_refresh_login_state(struct master_service *service)
1089 {
1090 	enum master_login_state state;
1091 
1092 	if (master_service_get_login_state(&state) == 0)
1093 		master_service_set_login_state(service, state);
1094 }
1095 
master_service_close_config_fd(struct master_service * service)1096 void master_service_close_config_fd(struct master_service *service)
1097 {
1098 	i_close_fd(&service->config_fd);
1099 }
1100 
master_service_deinit_real(struct master_service ** _service)1101 static void master_service_deinit_real(struct master_service **_service)
1102 {
1103 	struct master_service *service = *_service;
1104 
1105 	*_service = NULL;
1106 
1107 	if (!service->init_finished &&
1108 	    (service->flags & MASTER_SERVICE_FLAG_NO_INIT_DATASTACK_FRAME) == 0) {
1109 		if (!t_pop(&service->datastack_frame_id))
1110 			i_panic("Leaked t_pop() call");
1111 	}
1112 	master_service_haproxy_abort(service);
1113 
1114 	master_service_io_listeners_remove(service);
1115 	master_service_ssl_ctx_deinit(service);
1116 
1117 	if (service->stats_client != NULL)
1118 		stats_client_deinit(&service->stats_client);
1119 	master_service_close_config_fd(service);
1120 	timeout_remove(&service->to_overflow_call);
1121 	timeout_remove(&service->to_die);
1122 	timeout_remove(&service->to_overflow_state);
1123 	timeout_remove(&service->to_status);
1124 	io_remove(&service->io_status_error);
1125 	io_remove(&service->io_status_write);
1126 	if (array_is_created(&service->config_overrides))
1127 		array_free(&service->config_overrides);
1128 
1129 	if (service->set_parser != NULL) {
1130 		settings_parser_deinit(&service->set_parser);
1131 		pool_unref(&service->set_pool);
1132 	}
1133 	i_free(master_service_category_name);
1134 	master_service_category.name = NULL;
1135 	event_unregister_callback(master_service_event_callback);
1136 }
1137 
master_service_free(struct master_service * service)1138 static void master_service_free(struct master_service *service)
1139 {
1140 	unsigned int i;
1141 
1142 	for (i = 0; i < service->socket_count; i++)
1143 		i_free(service->listeners[i].name);
1144 	i_free(service->listeners);
1145 	i_free(service->getopt_str);
1146 	i_free(service->configured_name);
1147 	i_free(service->name);
1148 	i_free(service->config_path);
1149 	i_free(service);
1150 }
1151 
master_service_deinit(struct master_service ** _service)1152 void master_service_deinit(struct master_service **_service)
1153 {
1154 	struct master_service *service = *_service;
1155 
1156 	master_service_deinit_real(_service);
1157 
1158 	lib_signals_deinit();
1159 	/* run atexit callbacks before destroying ioloop */
1160 	lib_atexit_run();
1161 	io_loop_destroy(&service->ioloop);
1162 
1163 	master_service_free(service);
1164 	lib_deinit();
1165 }
1166 
master_service_deinit_forked(struct master_service ** _service)1167 void master_service_deinit_forked(struct master_service **_service)
1168 {
1169 	struct master_service *service = *_service;
1170 
1171 	master_service_deinit_real(_service);
1172 	io_loop_destroy(&service->ioloop);
1173 
1174 	master_service_free(service);
1175 }
1176 
master_service_overflow(struct master_service * service)1177 static void master_service_overflow(struct master_service *service)
1178 {
1179 	enum master_login_state state;
1180 	struct timeval created;
1181 
1182 	timeout_remove(&service->to_overflow_call);
1183 
1184 	if (master_service_get_login_state(&state) < 0 ||
1185 	    state != MASTER_LOGIN_STATE_FULL) {
1186 		/* service is no longer full (or we couldn't check if it is) */
1187 		return;
1188 	}
1189 
1190 	if (!service->avail_overflow_callback(TRUE, &created)) {
1191 		/* can't kill the client anymore after all */
1192 		return;
1193 	}
1194 	if (service->master_status.available_count == 0) {
1195 		/* Client was destroyed, but service_count is now 0.
1196 		   The servive was already stopped, so the process will
1197 		   shutdown and a new process can handle the waiting client
1198 		   connection. */
1199 		i_assert(service->service_count_left == 0);
1200 		i_assert(!io_loop_is_running(service->ioloop));
1201 		return;
1202 	}
1203 	master_service_io_listeners_add(service);
1204 
1205 	/* The connection is soon accepted by the listener IO callback.
1206 	   Note that this often results in killing two connections, because
1207 	   after the first process has accepted the new client the service is
1208 	   full again. The second process sees this and kills another client.
1209 	   After this the other processes see that the service is no longer
1210 	   full and kill no more clients. */
1211 }
1212 
1213 static unsigned int
master_service_overflow_timeout_msecs(const struct timeval * created)1214 master_service_overflow_timeout_msecs(const struct timeval *created)
1215 {
1216 	/* Returns a value between 0..max_wait. The oldest clients return the
1217 	   lowest wait so they get killed before newer clients. For simplicity
1218 	   this code treats all clients older than 10 seconds the same. */
1219 	const unsigned int max_wait = 100;
1220 	const int max_since = 10*1000;
1221 	int created_since = timeval_diff_msecs(&ioloop_timeval, created);
1222 	unsigned int msecs;
1223 
1224 	created_since = I_MAX(created_since, 0);
1225 	created_since = I_MIN(created_since, max_since);
1226 
1227 	msecs = created_since * max_wait / max_since;
1228 	i_assert(msecs <= max_wait);
1229 	msecs = max_wait - msecs;
1230 
1231 	/* Add some extra randomness, so even if all clients have exactly the
1232 	   same creation time they won't all be killed. */
1233 	return msecs + i_rand_limit(10);
1234 }
1235 
master_service_full(struct master_service * service)1236 static bool master_service_full(struct master_service *service)
1237 {
1238 	struct timeval created;
1239 
1240 	/* This process can't handle any more connections. */
1241 	if (!service->call_avail_overflow ||
1242 	    service->avail_overflow_callback == NULL)
1243 		return TRUE;
1244 
1245 	/* Master has notified us that all processes are full, and
1246 	   we have the ability to kill old connections. */
1247 	if (service->total_available_count > 1) {
1248 		/* This process can still create multiple concurrent
1249 		   clients if we just kill some of the existing ones.
1250 		   Do it immediately. */
1251 		return !service->avail_overflow_callback(TRUE, &created);
1252 	}
1253 
1254 	/* This process can't create more than a single client. Most likely
1255 	   running with service_count=1. Check the overflow again after a short
1256 	   delay before killing anything. This way only some of the connections
1257 	   get killed instead of all of them. The delay is based on the
1258 	   connection age with a bit of randomness, so the oldest connections
1259 	   should die first, but even if all the connections have time same
1260 	   timestamp they still don't all die at once. */
1261 	if (!service->avail_overflow_callback(FALSE, &created)) {
1262 		/* can't kill any clients */
1263 		return TRUE;
1264 	}
1265 	i_assert(service->to_overflow_call == NULL);
1266 	service->to_overflow_call =
1267 		timeout_add(master_service_overflow_timeout_msecs(&created),
1268 			    master_service_overflow, service);
1269 	return TRUE;
1270 }
1271 
master_service_listen(struct master_service_listener * l)1272 static void master_service_listen(struct master_service_listener *l)
1273 {
1274 	struct master_service *service = l->service;
1275 	struct master_service_connection conn;
1276 
1277 	if (service->master_status.available_count == 0) {
1278 		if (master_service_full(service)) {
1279 			/* Stop the listener until a client has disconnected or
1280 			   overflow callback has killed one. */
1281 			master_service_io_listeners_remove(service);
1282 			return;
1283 		}
1284 		/* we can accept another client */
1285 		i_assert(service->master_status.available_count > 0);
1286 	}
1287 
1288 	i_zero(&conn);
1289 	conn.listen_fd = l->fd;
1290 	conn.fd = net_accept(l->fd, &conn.remote_ip, &conn.remote_port);
1291 	if (conn.fd < 0) {
1292 		struct stat st;
1293 		int orig_errno = errno;
1294 
1295 		if (conn.fd == -1)
1296 			return;
1297 
1298 		if (errno == ENOTSOCK) {
1299 			/* it's not a socket. should be a fifo. */
1300 		} else if (errno == EINVAL &&
1301 			   (fstat(l->fd, &st) == 0 && S_ISFIFO(st.st_mode))) {
1302 			/* BSDI fails accept(fifo) with EINVAL. */
1303 		} else {
1304 			errno = orig_errno;
1305 			i_error("net_accept() failed: %m");
1306 			/* try again later after one of the existing
1307 			   connections has died */
1308 			master_service_io_listeners_remove(service);
1309 			return;
1310 		}
1311 		/* use the "listener" as the connection fd and stop the
1312 		   listener. */
1313 		conn.fd = l->fd;
1314 		conn.listen_fd = l->fd;
1315 		conn.fifo = TRUE;
1316 
1317 		io_remove(&l->io);
1318 		l->fd = -1;
1319 	}
1320 	conn.ssl = l->ssl;
1321 	conn.name = master_service_get_socket_name(service, conn.listen_fd);
1322 
1323 	(void)net_getsockname(conn.fd, &conn.local_ip, &conn.local_port);
1324 	conn.real_remote_ip = conn.remote_ip;
1325 	conn.real_remote_port = conn.remote_port;
1326 	conn.real_local_ip = conn.local_ip;
1327 	conn.real_local_port = conn.local_port;
1328 
1329 	net_set_nonblock(conn.fd, TRUE);
1330 
1331 	master_service_client_connection_created(service);
1332 	if (l->haproxy)
1333 		master_service_haproxy_new(service, &conn);
1334 	else
1335 		master_service_client_connection_callback(service, &conn);
1336 }
1337 
master_service_io_listeners_add(struct master_service * service)1338 void master_service_io_listeners_add(struct master_service *service)
1339 {
1340 	unsigned int i;
1341 
1342 	/* If there's a pending overflow call, remove it now since new
1343 	   clients just became available. */
1344 	timeout_remove(&service->to_overflow_call);
1345 
1346 	if (service->stopping)
1347 		return;
1348 
1349 	for (i = 0; i < service->socket_count; i++) {
1350 		struct master_service_listener *l = &service->listeners[i];
1351 
1352 		if (l->io == NULL && l->fd != -1 && !l->closed) {
1353 			l->io = io_add(MASTER_LISTEN_FD_FIRST + i, IO_READ,
1354 				       master_service_listen, l);
1355 		}
1356 	}
1357 }
1358 
master_service_io_listeners_remove(struct master_service * service)1359 void master_service_io_listeners_remove(struct master_service *service)
1360 {
1361 	unsigned int i;
1362 
1363 	for (i = 0; i < service->socket_count; i++) {
1364 		io_remove(&service->listeners[i].io);
1365 	}
1366 }
1367 
master_service_ssl_io_listeners_remove(struct master_service * service)1368 void master_service_ssl_io_listeners_remove(struct master_service *service)
1369 {
1370 	unsigned int i;
1371 
1372 	for (i = 0; i < service->socket_count; i++) {
1373 		if (service->listeners[i].io != NULL &&
1374 		    service->listeners[i].ssl)
1375 			io_remove(&service->listeners[i].io);
1376 	}
1377 }
1378 
master_service_io_listeners_close(struct master_service * service)1379 static void master_service_io_listeners_close(struct master_service *service)
1380 {
1381 	unsigned int i;
1382 
1383 	/* close via listeners. some fds might be pipes that are
1384 	   currently handled as clients. we don't want to close them. */
1385 	for (i = 0; i < service->socket_count; i++) {
1386 		if (service->listeners[i].fd != -1) {
1387 			if (close(service->listeners[i].fd) < 0) {
1388 				i_error("close(listener %d) failed: %m",
1389 					service->listeners[i].fd);
1390 			}
1391 			service->listeners[i].fd = -1;
1392 		}
1393 	}
1394 }
1395 
master_status_update_is_important(struct master_service * service)1396 static bool master_status_update_is_important(struct master_service *service)
1397 {
1398 	if (service->master_status.available_count == 0) {
1399 		/* client_limit reached for this process */
1400 		return TRUE;
1401 	}
1402 	if (service->last_sent_status_avail_count == 0) {
1403 		/* This process can now handle more clients. This is important
1404 		   to know for master if all the existing processes have
1405 		   avail_count=0 so it doesn't unnecessarily create more
1406 		   processes. */
1407 		return TRUE;
1408 	}
1409 	/* The previous check should have triggered also for the initial
1410 	   status notification. */
1411 	i_assert(service->initial_status_sent);
1412 	return FALSE;
1413 }
1414 
1415 static void
master_status_send(struct master_service * service,bool important_update)1416 master_status_send(struct master_service *service, bool important_update)
1417 {
1418 	ssize_t ret;
1419 
1420 	timeout_remove(&service->to_status);
1421 
1422 	ret = write(MASTER_STATUS_FD, &service->master_status,
1423 		    sizeof(service->master_status));
1424 	if (ret == (ssize_t)sizeof(service->master_status)) {
1425 		/* success */
1426 		io_remove(&service->io_status_write);
1427 		service->last_sent_status_time = ioloop_time;
1428 		service->last_sent_status_avail_count =
1429 			service->master_status.available_count;
1430 		service->initial_status_sent = TRUE;
1431 	} else if (ret >= 0) {
1432 		/* shouldn't happen? */
1433 		i_error("write(master_status_fd) returned %d", (int)ret);
1434 		service->master_status.pid = 0;
1435 	} else if (errno != EAGAIN) {
1436 		/* failure */
1437 		if (errno != EPIPE)
1438 			i_error("write(master_status_fd) failed: %m");
1439 		service->master_status.pid = 0;
1440 	} else if (important_update) {
1441 		/* reader is busy, but it's important to get this notification
1442 		   through. send it when possible. */
1443 		if (service->io_status_write == NULL) {
1444 			service->io_status_write =
1445 				io_add(MASTER_STATUS_FD, IO_WRITE,
1446 				       master_status_update, service);
1447 		}
1448 	}
1449 }
1450 
master_status_update(struct master_service * service)1451 void master_status_update(struct master_service *service)
1452 {
1453 	bool important_update;
1454 
1455 	if ((service->flags & MASTER_SERVICE_FLAG_UPDATE_PROCTITLE) != 0 &&
1456 	    service->set != NULL && service->set->verbose_proctitle) T_BEGIN {
1457 		unsigned int used_count = service->total_available_count -
1458 			service->master_status.available_count;
1459 
1460 		process_title_set(t_strdup_printf("[%u connections]",
1461 						  used_count));
1462 	} T_END;
1463 
1464 	important_update = master_status_update_is_important(service);
1465 	if (service->master_status.pid == 0 ||
1466 	    service->master_status.available_count ==
1467 	    service->last_sent_status_avail_count) {
1468 		/* a) closed, b) updating to same state */
1469 		timeout_remove(&service->to_status);
1470 		io_remove(&service->io_status_write);
1471 		return;
1472 	}
1473 	if (ioloop_time == service->last_sent_status_time &&
1474 	    !important_update) {
1475 		/* don't spam master */
1476 		if (service->to_status != NULL)
1477 			timeout_reset(service->to_status);
1478 		else {
1479 			service->to_status =
1480 				timeout_add(1000, master_status_update,
1481 					    service);
1482 		}
1483 		if (service->io_status_write != NULL)
1484 			io_remove(&service->io_status_write);
1485 		return;
1486 	}
1487 	master_status_send(service, important_update);
1488 }
1489 
version_string_verify(const char * line,const char * service_name,unsigned major_version)1490 bool version_string_verify(const char *line, const char *service_name,
1491 			   unsigned major_version)
1492 {
1493 	unsigned int minor_version;
1494 
1495 	return version_string_verify_full(line, service_name,
1496 					  major_version, &minor_version);
1497 }
1498 
version_string_verify_full(const char * line,const char * service_name,unsigned major_version,unsigned int * minor_version_r)1499 bool version_string_verify_full(const char *line, const char *service_name,
1500 				unsigned major_version,
1501 				unsigned int *minor_version_r)
1502 {
1503 	size_t service_name_len = strlen(service_name);
1504 	bool ret;
1505 
1506 	if (!str_begins(line, "VERSION\t"))
1507 		return FALSE;
1508 	line += 8;
1509 
1510 	if (strncmp(line, service_name, service_name_len) != 0 ||
1511 	    line[service_name_len] != '\t')
1512 		return FALSE;
1513 	line += service_name_len + 1;
1514 
1515 	T_BEGIN {
1516 		const char *p = strchr(line, '\t');
1517 
1518 		if (p == NULL)
1519 			ret = FALSE;
1520 		else {
1521 			ret = str_uint_equals(t_strdup_until(line, p),
1522 					      major_version);
1523 			if (str_to_uint(p+1, minor_version_r) < 0)
1524 				ret = FALSE;
1525 		}
1526 	} T_END;
1527 	return ret;
1528 }
1529