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 simple_protocol_restart_queries();
191 } else if (state == AVAHI_SERVER_COLLISION) {
194 static_service_remove_from_server();
196 remove_dns_server_entry_groups();
198 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
199 avahi_log_warn("Host name conflict, retrying with <%s>", n);
200 avahi_server_set_host_name(s, n);
205 static void help(FILE *f, const gchar *argv0) {
208 " -h --help Show this help\n"
209 " -D --daemonize Daemonize after startup\n"
210 " -k --kill Kill a running daemon\n"
211 " -r --reload Request a running daemon to reload static services\n"
212 " -V --version Show version\n"
213 " -f --file=FILE Load the specified configuration file instead of\n"
214 " "AVAHI_CONFIG_FILE"\n",
218 static gint parse_command_line(DaemonConfig *config, int argc, char *argv[]) {
221 static const struct option const long_options[] = {
222 { "help", no_argument, NULL, 'h' },
223 { "daemonize", no_argument, NULL, 'D' },
224 { "kill", no_argument, NULL, 'k' },
225 { "version", no_argument, NULL, 'V' },
226 { "file", required_argument, NULL, 'f' },
227 { "reload", no_argument, NULL, 'r' },
233 while ((c = getopt_long(argc, argv, "hDkVf:r", long_options, NULL)) >= 0) {
237 config->command = DAEMON_HELP;
240 config->daemonize = TRUE;
243 config->command = DAEMON_KILL;
246 config->command = DAEMON_VERSION;
249 g_free(config->config_file);
250 config->config_file = g_strdup(optarg);
253 config->command = DAEMON_RELOAD;
256 fprintf(stderr, "Invalid command line argument: %c\n", c);
262 fprintf(stderr, "Too many arguments\n");
269 static gboolean is_yes(const gchar *s) {
272 return *s == 'y' || *s == 'Y';
275 static gint load_config_file(DaemonConfig *config) {
279 gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
283 f = g_key_file_new();
284 g_key_file_set_list_separator(f, ',');
286 if (!g_key_file_load_from_file(f, config->config_file ? config->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
287 fprintf(stderr, "Unable to read config file: %s\n", err->message);
291 groups = g_key_file_get_groups(f, NULL);
293 for (g = groups; *g; g++) {
294 if (g_strcasecmp(*g, "server") == 0) {
297 keys = g_key_file_get_keys(f, *g, NULL, NULL);
299 for (k = keys; *k; k++) {
301 v = g_key_file_get_value(f, *g, *k, NULL);
303 if (g_strcasecmp(*k, "host-name") == 0) {
304 g_free(config->server_config.host_name);
305 config->server_config.host_name = v;
307 } else if (g_strcasecmp(*k, "domain-name") == 0) {
308 g_free(config->server_config.domain_name);
309 config->server_config.domain_name = v;
311 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
312 config->server_config.use_ipv4 = is_yes(v);
313 else if (g_strcasecmp(*k, "use-ipv6") == 0)
314 config->server_config.use_ipv6 = is_yes(v);
315 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
316 config->server_config.check_response_ttl = is_yes(v);
317 else if (g_strcasecmp(*k, "use-iff-running") == 0)
318 config->server_config.use_iff_running = is_yes(v);
319 else if (g_strcasecmp(*k, "enable-dbus") == 0)
320 config->enable_dbus = is_yes(v);
321 else if (g_strcasecmp(*k, "drop-root") == 0)
322 config->drop_root = is_yes(v);
324 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
335 } else if (g_strcasecmp(*g, "publish") == 0) {
338 keys = g_key_file_get_keys(f, *g, NULL, NULL);
340 for (k = keys; *k; k++) {
342 v = g_key_file_get_string(f, *g, *k, NULL);
344 if (g_strcasecmp(*k, "publish-addresses") == 0)
345 config->server_config.publish_addresses = is_yes(v);
346 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
347 config->server_config.publish_hinfo = is_yes(v);
348 else if (g_strcasecmp(*k, "publish-workstation") == 0)
349 config->server_config.publish_workstation = is_yes(v);
350 else if (g_strcasecmp(*k, "publish-domain") == 0)
351 config->server_config.publish_domain = is_yes(v);
352 else if (g_strcasecmp(*k, "publish-resolv-conf-dns-servers") == 0)
353 config->publish_resolv_conf = is_yes(v);
354 else if (g_strcasecmp(*k, "publish-dns-servers") == 0) {
355 g_strfreev(config->publish_dns_servers);
356 config->publish_dns_servers = g_key_file_get_string_list(f, *g, *k, NULL, NULL);
358 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
369 } else if (g_strcasecmp(*g, "reflector") == 0) {
372 keys = g_key_file_get_keys(f, *g, NULL, NULL);
374 for (k = keys; *k; k++) {
376 v = g_key_file_get_string(f, *g, *k, NULL);
378 if (g_strcasecmp(*k, "enable-reflector") == 0)
379 config->server_config.enable_reflector = is_yes(v);
380 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
381 config->server_config.reflect_ipv = is_yes(v);
383 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
395 fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
417 static void log_function(AvahiLogLevel level, const gchar *txt) {
419 static const int const log_level_map[] = {
427 g_assert(level < AVAHI_LOG_LEVEL_MAX);
430 daemon_log(log_level_map[level], "%s", txt);
433 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
435 GMainLoop *loop = data;
440 if ((sig = daemon_signal_next()) <= 0) {
441 avahi_log_error("daemon_signal_next() failed");
451 sig == SIGINT ? "SIGINT" :
452 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
453 g_main_loop_quit(loop);
457 avahi_log_info("Got SIGHUP, reloading.");
458 static_service_load();
459 static_service_add_to_server();
461 if (resolv_conf_entry_group) {
462 avahi_entry_group_free(resolv_conf_entry_group);
463 resolv_conf_entry_group = NULL;
466 load_resolv_conf(&config);
468 if (resolv_conf && resolv_conf[0])
469 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf);
474 avahi_log_warn("Got spurious signal, ignoring.");
481 static gint run_server(DaemonConfig *config) {
482 GMainLoop *loop = NULL;
484 GIOChannel *io = NULL;
485 guint watch_id = (guint) -1;
489 loop = g_main_loop_new(NULL, FALSE);
491 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, 0) < 0) {
492 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
496 if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
497 avahi_log_error( "Failed to create signal io channel.");
501 g_io_channel_set_close_on_unref(io, FALSE);
502 g_io_add_watch(io, G_IO_IN, signal_callback, loop);
504 if (simple_protocol_setup(NULL) < 0)
508 if (config->enable_dbus)
509 if (dbus_protocol_setup(loop) < 0)
513 if (!(avahi_server = avahi_server_new(NULL, &config->server_config, server_callback, config)))
516 load_resolv_conf(config);
518 static_service_load();
520 if (config->daemonize) {
521 daemon_retval_send(0);
525 g_main_loop_run(loop);
529 static_service_remove_from_server();
530 static_service_free_all();
531 remove_dns_server_entry_groups();
533 simple_protocol_shutdown();
536 if (config->enable_dbus)
537 dbus_protocol_shutdown();
542 avahi_server_free(avahi_server);
544 daemon_signal_done();
546 if (watch_id != (guint) -1)
547 g_source_remove(watch_id);
550 g_io_channel_unref(io);
554 g_main_loop_unref(loop);
556 if (r != 0 && config->daemonize)
557 daemon_retval_send(1);
562 static gint drop_root(void) {
567 if (!(pw = getpwnam(AVAHI_USER))) {
568 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
572 if (!(gr = getgrnam(AVAHI_GROUP))) {
573 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
577 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);
579 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
580 avahi_log_error("Failed to change group list: %s", strerror(errno));
584 #if defined(HAVE_SETRESGID)
585 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
586 #elif defined(HAVE_SETREGID)
587 r = setregid(gr->gr_gid, gr->gr_gid);
589 if ((r = setgid(gr->gr_gid)) >= 0)
590 r = setegid(gr->gr_gid);
594 avahi_log_error("Failed to change GID: %s", strerror(errno));
598 #if defined(HAVE_SETRESUID)
599 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
600 #elif defined(HAVE_SETREUID)
601 r = setreuid(pw->pw_uid, pw->pw_uid);
603 if ((r = setuid(pw->pw_uid)) >= 0)
604 r = seteuid(pw->pw_uid);
608 avahi_log_error("Failed to change UID: %s", strerror(errno));
612 g_setenv("USER", pw->pw_name, 1);
613 g_setenv("LOGNAME", pw->pw_name, 1);
614 g_setenv("HOME", pw->pw_dir, 1);
616 avahi_log_info("Successfully dropped root privileges.");
621 static const char* pid_file_proc(void) {
622 return AVAHI_RUNTIME_DIR"/avahi-daemon.pid";
625 static gint make_runtime_dir(void) {
628 gboolean reset_umask = FALSE;
633 if (!(pw = getpwnam(AVAHI_USER))) {
634 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
638 if (!(gr = getgrnam(AVAHI_GROUP))) {
639 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
646 if (mkdir(AVAHI_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
647 avahi_log_error("mkdir(\""AVAHI_RUNTIME_DIR"\"): %s", strerror(errno));
651 chown(AVAHI_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
653 if (stat(AVAHI_RUNTIME_DIR, &st) < 0) {
654 avahi_log_error("stat(): %s\n", strerror(errno));
658 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
659 avahi_log_error("Failed to create runtime directory "AVAHI_RUNTIME_DIR".");
672 int main(int argc, char *argv[]) {
675 gboolean wrote_pid_file = FALSE;
677 avahi_set_log_function(log_function);
679 avahi_server_config_init(&config.server_config);
680 config.command = DAEMON_RUN;
681 config.daemonize = FALSE;
682 config.config_file = NULL;
683 config.enable_dbus = TRUE;
684 config.drop_root = TRUE;
685 config.publish_dns_servers = NULL;
686 config.publish_resolv_conf = FALSE;
688 if ((argv0 = strrchr(argv[0], '/')))
693 daemon_pid_file_ident = daemon_log_ident = (char *) argv0;
694 daemon_pid_file_proc = pid_file_proc;
696 if (parse_command_line(&config, argc, argv) < 0)
699 if (config.command == DAEMON_HELP) {
702 } else if (config.command == DAEMON_VERSION) {
703 printf("%s "PACKAGE_VERSION"\n", argv0);
705 } else if (config.command == DAEMON_KILL) {
706 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
707 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
713 } else if (config.command == DAEMON_RELOAD) {
714 if (daemon_pid_file_kill(SIGHUP) < 0) {
715 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
721 } else if (config.command == DAEMON_RUN) {
725 avahi_log_error("This program is intended to be run as root.");
729 if ((pid = daemon_pid_file_is_running()) >= 0) {
730 avahi_log_error("Daemon already running on PID %u", pid);
734 if (load_config_file(&config) < 0)
737 if (config.daemonize) {
738 daemon_retval_init();
740 if ((pid = daemon_fork()) < 0)
746 if ((ret = daemon_retval_wait(20)) < 0) {
747 avahi_log_error("Could not recieve return value from daemon process.");
760 if (make_runtime_dir() < 0)
763 if (config.drop_root) {
768 if (daemon_pid_file_create() < 0) {
769 avahi_log_error("Failed to create PID file: %s", strerror(errno));
771 if (config.daemonize)
772 daemon_retval_send(1);
775 wrote_pid_file = TRUE;
777 if (run_server(&config) == 0)
783 if (config.daemonize)
784 daemon_retval_done();
786 avahi_server_config_free(&config.server_config);
787 g_free(config.config_file);
788 g_strfreev(config.publish_dns_servers);
789 g_strfreev(resolv_conf);
792 daemon_pid_file_remove();