4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 #include <libdaemon/dfork.h>
37 #include <libdaemon/dsignal.h>
38 #include <libdaemon/dlog.h>
39 #include <libdaemon/dpid.h>
41 #include <avahi-core/core.h>
42 #include <avahi-core/log.h>
45 #include "simple-protocol.h"
46 #include "dbus-protocol.h"
47 #include "static-services.h"
49 AvahiServer *avahi_server = NULL;
60 AvahiServerConfig server_config;
61 DaemonCommand command;
66 gboolean publish_resolv_conf;
67 gchar ** publish_dns_servers;
70 #define RESOLV_CONF "/etc/resolv.conf"
72 static AvahiEntryGroup *dns_servers_entry_group = NULL;
73 static AvahiEntryGroup *resolv_conf_entry_group = NULL;
75 static gchar **resolv_conf = NULL;
77 static DaemonConfig config;
79 #define MAX_NAME_SERVERS 10
81 static gint load_resolv_conf(const DaemonConfig *config) {
86 g_strfreev(resolv_conf);
89 if (!config->publish_resolv_conf)
92 if (!(f = fopen(RESOLV_CONF, "r"))) {
93 avahi_log_warn("Failed to open "RESOLV_CONF".");
97 resolv_conf = g_new0(gchar*, MAX_NAME_SERVERS+1);
99 while (!feof(f) && i < MAX_NAME_SERVERS) {
103 if (!(fgets(ln, sizeof(ln), f)))
106 ln[strcspn(ln, "\r\n#")] = 0;
107 p = ln + strspn(ln, "\t ");
109 if (g_str_has_prefix(p, "nameserver")) {
111 p += strspn(p, "\t ");
112 p[strcspn(p, "\t ")] = 0;
113 resolv_conf[i++] = strdup(p);
122 g_strfreev(resolv_conf);
132 static AvahiEntryGroup* add_dns_servers(AvahiServer *s, gchar **l) {
139 g = avahi_entry_group_new(s, NULL, NULL);
141 for (p = l; *p; p++) {
144 if (!avahi_address_parse(*p, AF_UNSPEC, &a))
145 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
147 if (avahi_server_add_dns_server_address(s, g, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
148 avahi_entry_group_free(g);
153 avahi_entry_group_commit(g);
159 static void remove_dns_server_entry_groups(void) {
160 if (resolv_conf_entry_group) {
161 avahi_entry_group_free(resolv_conf_entry_group);
162 resolv_conf_entry_group = NULL;
165 if (dns_servers_entry_group) {
166 avahi_entry_group_free(dns_servers_entry_group);
167 dns_servers_entry_group = NULL;
171 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
172 DaemonConfig *config = userdata;
177 if (state == AVAHI_SERVER_RUNNING) {
178 avahi_log_info("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
179 static_service_add_to_server();
181 remove_dns_server_entry_groups();
183 if (resolv_conf && resolv_conf[0])
184 resolv_conf_entry_group = add_dns_servers(s, resolv_conf);
186 if (config->publish_dns_servers && config->publish_dns_servers[0])
187 dns_servers_entry_group = add_dns_servers(s, config->publish_dns_servers);
189 } else if (state == AVAHI_SERVER_COLLISION) {
192 static_service_remove_from_server();
194 remove_dns_server_entry_groups();
196 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
197 avahi_log_warn("Host name conflict, retrying with <%s>", n);
198 avahi_server_set_host_name(s, n);
203 static void help(FILE *f, const gchar *argv0) {
206 " -h --help Show this help\n"
207 " -D --daemonize Daemonize after startup\n"
208 " -k --kill Kill a running daemon\n"
209 " -r --reload Request a running daemon to reload static services\n"
210 " -V --version Show version\n"
211 " -f --file=FILE Load the specified configuration file instead of\n"
212 " "AVAHI_CONFIG_FILE"\n",
216 static gint parse_command_line(DaemonConfig *config, int argc, char *argv[]) {
219 static const struct option const long_options[] = {
220 { "help", no_argument, NULL, 'h' },
221 { "daemonize", no_argument, NULL, 'D' },
222 { "kill", no_argument, NULL, 'k' },
223 { "version", no_argument, NULL, 'V' },
224 { "file", required_argument, NULL, 'f' },
225 { "reload", no_argument, NULL, 'r' },
231 while ((c = getopt_long(argc, argv, "hDkVf:r", long_options, NULL)) >= 0) {
235 config->command = DAEMON_HELP;
238 config->daemonize = TRUE;
241 config->command = DAEMON_KILL;
244 config->command = DAEMON_VERSION;
247 g_free(config->config_file);
248 config->config_file = g_strdup(optarg);
251 config->command = DAEMON_RELOAD;
254 fprintf(stderr, "Invalid command line argument: %c\n", c);
260 fprintf(stderr, "Too many arguments\n");
267 static gboolean is_yes(const gchar *s) {
270 return *s == 'y' || *s == 'Y';
273 static gint load_config_file(DaemonConfig *config) {
277 gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
281 f = g_key_file_new();
282 g_key_file_set_list_separator(f, ',');
284 if (!g_key_file_load_from_file(f, config->config_file ? config->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
285 fprintf(stderr, "Unable to read config file: %s\n", err->message);
289 groups = g_key_file_get_groups(f, NULL);
291 for (g = groups; *g; g++) {
292 if (g_strcasecmp(*g, "server") == 0) {
295 keys = g_key_file_get_keys(f, *g, NULL, NULL);
297 for (k = keys; *k; k++) {
299 v = g_key_file_get_value(f, *g, *k, NULL);
301 if (g_strcasecmp(*k, "host-name") == 0) {
302 g_free(config->server_config.host_name);
303 config->server_config.host_name = v;
305 } else if (g_strcasecmp(*k, "domain-name") == 0) {
306 g_free(config->server_config.domain_name);
307 config->server_config.domain_name = v;
309 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
310 config->server_config.use_ipv4 = is_yes(v);
311 else if (g_strcasecmp(*k, "use-ipv6") == 0)
312 config->server_config.use_ipv6 = is_yes(v);
313 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
314 config->server_config.check_response_ttl = is_yes(v);
315 else if (g_strcasecmp(*k, "use-iff-running") == 0)
316 config->server_config.use_iff_running = is_yes(v);
317 else if (g_strcasecmp(*k, "enable-dbus") == 0)
318 config->enable_dbus = is_yes(v);
319 else if (g_strcasecmp(*k, "drop-root") == 0)
320 config->drop_root = is_yes(v);
322 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
333 } else if (g_strcasecmp(*g, "publish") == 0) {
336 keys = g_key_file_get_keys(f, *g, NULL, NULL);
338 for (k = keys; *k; k++) {
340 v = g_key_file_get_string(f, *g, *k, NULL);
342 if (g_strcasecmp(*k, "publish-addresses") == 0)
343 config->server_config.publish_addresses = is_yes(v);
344 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
345 config->server_config.publish_hinfo = is_yes(v);
346 else if (g_strcasecmp(*k, "publish-workstation") == 0)
347 config->server_config.publish_workstation = is_yes(v);
348 else if (g_strcasecmp(*k, "publish-domain") == 0)
349 config->server_config.publish_domain = is_yes(v);
350 else if (g_strcasecmp(*k, "publish-resolv-conf-dns-servers") == 0)
351 config->publish_resolv_conf = is_yes(v);
352 else if (g_strcasecmp(*k, "publish-dns-servers") == 0) {
353 g_strfreev(config->publish_dns_servers);
354 config->publish_dns_servers = g_key_file_get_string_list(f, *g, *k, NULL, NULL);
356 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
367 } else if (g_strcasecmp(*g, "reflector") == 0) {
370 keys = g_key_file_get_keys(f, *g, NULL, NULL);
372 for (k = keys; *k; k++) {
374 v = g_key_file_get_string(f, *g, *k, NULL);
376 if (g_strcasecmp(*k, "enable-reflector") == 0)
377 config->server_config.enable_reflector = is_yes(v);
378 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
379 config->server_config.reflect_ipv = is_yes(v);
381 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
393 fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
415 static void log_function(AvahiLogLevel level, const gchar *txt) {
417 static const int const log_level_map[] = {
425 g_assert(level < AVAHI_LOG_LEVEL_MAX);
428 daemon_log(log_level_map[level], "%s", txt);
431 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
433 GMainLoop *loop = data;
438 if ((sig = daemon_signal_next()) <= 0) {
439 avahi_log_error("daemon_signal_next() failed");
449 sig == SIGINT ? "SIGINT" :
450 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
451 g_main_loop_quit(loop);
455 avahi_log_info("Got SIGHUP, reloading.");
456 static_service_load();
457 static_service_add_to_server();
459 if (resolv_conf_entry_group) {
460 avahi_entry_group_free(resolv_conf_entry_group);
461 resolv_conf_entry_group = NULL;
464 load_resolv_conf(&config);
466 if (resolv_conf && resolv_conf[0])
467 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf);
472 avahi_log_warn("Got spurious signal, ignoring.");
479 static gint run_server(DaemonConfig *config) {
480 GMainLoop *loop = NULL;
482 GIOChannel *io = NULL;
483 guint watch_id = (guint) -1;
487 loop = g_main_loop_new(NULL, FALSE);
489 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, 0) < 0) {
490 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
494 if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
495 avahi_log_error( "Failed to create signal io channel.");
499 g_io_channel_set_close_on_unref(io, FALSE);
500 g_io_add_watch(io, G_IO_IN, signal_callback, loop);
502 if (simple_protocol_setup(NULL) < 0)
506 if (config->enable_dbus)
507 if (dbus_protocol_setup(loop) < 0)
511 if (!(avahi_server = avahi_server_new(NULL, &config->server_config, server_callback, config)))
514 load_resolv_conf(config);
516 static_service_load();
518 if (config->daemonize) {
519 daemon_retval_send(0);
523 g_main_loop_run(loop);
527 static_service_remove_from_server();
528 static_service_free_all();
529 remove_dns_server_entry_groups();
531 simple_protocol_shutdown();
534 if (config->enable_dbus)
535 dbus_protocol_shutdown();
540 avahi_server_free(avahi_server);
542 daemon_signal_done();
544 if (watch_id != (guint) -1)
545 g_source_remove(watch_id);
548 g_io_channel_unref(io);
552 g_main_loop_unref(loop);
554 if (r != 0 && config->daemonize)
555 daemon_retval_send(1);
560 static gint drop_root(void) {
565 if (!(pw = getpwnam(AVAHI_USER))) {
566 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
570 if (!(gr = getgrnam(AVAHI_GROUP))) {
571 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
575 avahi_log_info("Found user '"AVAHI_USER"' (UID %lu) and group '"AVAHI_GROUP"' (GID %lu).", (unsigned long) pw->pw_uid, (unsigned long) gr->gr_gid);
577 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
578 avahi_log_error("Failed to change group list: %s", strerror(errno));
582 #if defined(HAVE_SETRESGID)
583 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
584 #elif defined(HAVE_SETREGID)
585 r = setregid(gr->gr_gid, gr->gr_gid);
587 if ((r = setgid(gr->gr_gid)) >= 0)
588 r = setegid(gr->gr_gid);
592 avahi_log_error("Failed to change GID: %s", strerror(errno));
596 #if defined(HAVE_SETRESUID)
597 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
598 #elif defined(HAVE_SETREUID)
599 r = setreuid(pw->pw_uid, pw->pw_uid);
601 if ((r = setuid(pw->pw_uid)) >= 0)
602 r = seteuid(pw->pw_uid);
606 avahi_log_error("Failed to change UID: %s", strerror(errno));
610 g_setenv("USER", pw->pw_name, 1);
611 g_setenv("LOGNAME", pw->pw_name, 1);
612 g_setenv("HOME", pw->pw_dir, 1);
614 avahi_log_info("Successfully dropped root privileges.");
619 static const char* pid_file_proc(void) {
620 return AVAHI_RUNTIME_DIR"/avahi-daemon.pid";
623 static gint make_runtime_dir(void) {
626 gboolean reset_umask = FALSE;
631 if (!(pw = getpwnam(AVAHI_USER))) {
632 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
636 if (!(gr = getgrnam(AVAHI_GROUP))) {
637 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
644 if (mkdir(AVAHI_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
645 avahi_log_error("mkdir(\""AVAHI_RUNTIME_DIR"\"): %s", strerror(errno));
649 chown(AVAHI_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
651 if (stat(AVAHI_RUNTIME_DIR, &st) < 0) {
652 avahi_log_error("stat(): %s\n", strerror(errno));
656 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
657 avahi_log_error("Failed to create runtime directory "AVAHI_RUNTIME_DIR".");
670 int main(int argc, char *argv[]) {
673 gboolean wrote_pid_file = FALSE;
675 avahi_set_log_function(log_function);
677 avahi_server_config_init(&config.server_config);
678 config.command = DAEMON_RUN;
679 config.daemonize = FALSE;
680 config.config_file = NULL;
681 config.enable_dbus = TRUE;
682 config.drop_root = TRUE;
683 config.publish_dns_servers = NULL;
684 config.publish_resolv_conf = FALSE;
686 if ((argv0 = strrchr(argv[0], '/')))
691 daemon_pid_file_ident = daemon_log_ident = (char *) argv0;
692 daemon_pid_file_proc = pid_file_proc;
694 if (parse_command_line(&config, argc, argv) < 0)
697 if (config.command == DAEMON_HELP) {
700 } else if (config.command == DAEMON_VERSION) {
701 printf("%s "PACKAGE_VERSION"\n", argv0);
703 } else if (config.command == DAEMON_KILL) {
704 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
705 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
711 } else if (config.command == DAEMON_RELOAD) {
712 if (daemon_pid_file_kill(SIGHUP) < 0) {
713 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
719 } else if (config.command == DAEMON_RUN) {
723 avahi_log_error("This program is intended to be run as root.");
727 if ((pid = daemon_pid_file_is_running()) >= 0) {
728 avahi_log_error("Daemon already running on PID %u", pid);
732 if (load_config_file(&config) < 0)
735 if (config.daemonize) {
736 daemon_retval_init();
738 if ((pid = daemon_fork()) < 0)
744 if ((ret = daemon_retval_wait(20)) < 0) {
745 avahi_log_error("Could not recieve return value from daemon process.");
758 if (make_runtime_dir() < 0)
761 if (config.drop_root) {
766 if (daemon_pid_file_create() < 0) {
767 avahi_log_error("Failed to create PID file: %s", strerror(errno));
769 if (config.daemonize)
770 daemon_retval_send(1);
773 wrote_pid_file = TRUE;
775 if (run_server(&config) == 0)
781 if (config.daemonize)
782 daemon_retval_done();
784 avahi_server_config_free(&config.server_config);
785 g_free(config.config_file);
786 g_strfreev(config.publish_dns_servers);
787 g_strfreev(resolv_conf);
790 daemon_pid_file_remove();