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;
61 AvahiServerConfig server_config;
62 DaemonCommand command;
67 gboolean publish_resolv_conf;
68 gchar ** publish_dns_servers;
71 #define RESOLV_CONF "/etc/resolv.conf"
73 static AvahiEntryGroup *dns_servers_entry_group = NULL;
74 static AvahiEntryGroup *resolv_conf_entry_group = NULL;
76 static gchar **resolv_conf = NULL;
78 static DaemonConfig config;
80 #define MAX_NAME_SERVERS 10
82 static gint load_resolv_conf(const DaemonConfig *config) {
87 g_strfreev(resolv_conf);
90 if (!config->publish_resolv_conf)
93 if (!(f = fopen(RESOLV_CONF, "r"))) {
94 avahi_log_warn("Failed to open "RESOLV_CONF".");
98 resolv_conf = g_new0(gchar*, MAX_NAME_SERVERS+1);
100 while (!feof(f) && i < MAX_NAME_SERVERS) {
104 if (!(fgets(ln, sizeof(ln), f)))
107 ln[strcspn(ln, "\r\n#")] = 0;
108 p = ln + strspn(ln, "\t ");
110 if (g_str_has_prefix(p, "nameserver")) {
112 p += strspn(p, "\t ");
113 p[strcspn(p, "\t ")] = 0;
114 resolv_conf[i++] = strdup(p);
123 g_strfreev(resolv_conf);
133 static AvahiEntryGroup* add_dns_servers(AvahiServer *s, gchar **l) {
140 g = avahi_entry_group_new(s, NULL, NULL);
142 for (p = l; *p; p++) {
145 if (!avahi_address_parse(*p, AF_UNSPEC, &a))
146 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
148 if (avahi_server_add_dns_server_address(s, g, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
149 avahi_entry_group_free(g);
154 avahi_entry_group_commit(g);
160 static void remove_dns_server_entry_groups(void) {
161 if (resolv_conf_entry_group) {
162 avahi_entry_group_free(resolv_conf_entry_group);
163 resolv_conf_entry_group = NULL;
166 if (dns_servers_entry_group) {
167 avahi_entry_group_free(dns_servers_entry_group);
168 dns_servers_entry_group = NULL;
172 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
173 DaemonConfig *config = userdata;
178 if (state == AVAHI_SERVER_RUNNING) {
179 avahi_log_info("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
180 static_service_add_to_server();
182 remove_dns_server_entry_groups();
184 if (resolv_conf && resolv_conf[0])
185 resolv_conf_entry_group = add_dns_servers(s, resolv_conf);
187 if (config->publish_dns_servers && config->publish_dns_servers[0])
188 dns_servers_entry_group = add_dns_servers(s, config->publish_dns_servers);
190 simple_protocol_restart_queries();
192 } else if (state == AVAHI_SERVER_COLLISION) {
195 static_service_remove_from_server();
197 remove_dns_server_entry_groups();
199 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
200 avahi_log_warn("Host name conflict, retrying with <%s>", n);
201 avahi_server_set_host_name(s, n);
206 static void help(FILE *f, const gchar *argv0) {
209 " -h --help Show this help\n"
210 " -D --daemonize Daemonize after startup\n"
211 " -k --kill Kill a running daemon\n"
212 " -r --reload Request a running daemon to reload static services\n"
213 " -c --check Return 0 if a daemon is already running\n"
214 " -V --version Show version\n"
215 " -f --file=FILE Load the specified configuration file instead of\n"
216 " "AVAHI_CONFIG_FILE"\n",
220 static gint parse_command_line(DaemonConfig *config, int argc, char *argv[]) {
223 static const struct option const long_options[] = {
224 { "help", no_argument, NULL, 'h' },
225 { "daemonize", no_argument, NULL, 'D' },
226 { "kill", no_argument, NULL, 'k' },
227 { "version", no_argument, NULL, 'V' },
228 { "file", required_argument, NULL, 'f' },
229 { "reload", no_argument, NULL, 'r' },
230 { "check", no_argument, NULL, 'c' },
236 while ((c = getopt_long(argc, argv, "hDkVf:rc", long_options, NULL)) >= 0) {
240 config->command = DAEMON_HELP;
243 config->daemonize = TRUE;
246 config->command = DAEMON_KILL;
249 config->command = DAEMON_VERSION;
252 g_free(config->config_file);
253 config->config_file = g_strdup(optarg);
256 config->command = DAEMON_RELOAD;
259 config->command = DAEMON_CHECK;
262 fprintf(stderr, "Invalid command line argument: %c\n", c);
268 fprintf(stderr, "Too many arguments\n");
275 static gboolean is_yes(const gchar *s) {
278 return *s == 'y' || *s == 'Y';
281 static gint load_config_file(DaemonConfig *config) {
285 gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
289 f = g_key_file_new();
290 g_key_file_set_list_separator(f, ',');
292 if (!g_key_file_load_from_file(f, config->config_file ? config->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
293 fprintf(stderr, "Unable to read config file: %s\n", err->message);
297 groups = g_key_file_get_groups(f, NULL);
299 for (g = groups; *g; g++) {
300 if (g_strcasecmp(*g, "server") == 0) {
303 keys = g_key_file_get_keys(f, *g, NULL, NULL);
305 for (k = keys; *k; k++) {
307 v = g_key_file_get_value(f, *g, *k, NULL);
309 if (g_strcasecmp(*k, "host-name") == 0) {
310 g_free(config->server_config.host_name);
311 config->server_config.host_name = v;
313 } else if (g_strcasecmp(*k, "domain-name") == 0) {
314 g_free(config->server_config.domain_name);
315 config->server_config.domain_name = v;
317 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
318 config->server_config.use_ipv4 = is_yes(v);
319 else if (g_strcasecmp(*k, "use-ipv6") == 0)
320 config->server_config.use_ipv6 = is_yes(v);
321 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
322 config->server_config.check_response_ttl = is_yes(v);
323 else if (g_strcasecmp(*k, "use-iff-running") == 0)
324 config->server_config.use_iff_running = is_yes(v);
325 else if (g_strcasecmp(*k, "enable-dbus") == 0)
326 config->enable_dbus = is_yes(v);
327 else if (g_strcasecmp(*k, "drop-root") == 0)
328 config->drop_root = is_yes(v);
330 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
341 } else if (g_strcasecmp(*g, "publish") == 0) {
344 keys = g_key_file_get_keys(f, *g, NULL, NULL);
346 for (k = keys; *k; k++) {
348 v = g_key_file_get_string(f, *g, *k, NULL);
350 if (g_strcasecmp(*k, "publish-addresses") == 0)
351 config->server_config.publish_addresses = is_yes(v);
352 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
353 config->server_config.publish_hinfo = is_yes(v);
354 else if (g_strcasecmp(*k, "publish-workstation") == 0)
355 config->server_config.publish_workstation = is_yes(v);
356 else if (g_strcasecmp(*k, "publish-domain") == 0)
357 config->server_config.publish_domain = is_yes(v);
358 else if (g_strcasecmp(*k, "publish-resolv-conf-dns-servers") == 0)
359 config->publish_resolv_conf = is_yes(v);
360 else if (g_strcasecmp(*k, "publish-dns-servers") == 0) {
361 g_strfreev(config->publish_dns_servers);
362 config->publish_dns_servers = g_key_file_get_string_list(f, *g, *k, NULL, NULL);
364 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
375 } else if (g_strcasecmp(*g, "reflector") == 0) {
378 keys = g_key_file_get_keys(f, *g, NULL, NULL);
380 for (k = keys; *k; k++) {
382 v = g_key_file_get_string(f, *g, *k, NULL);
384 if (g_strcasecmp(*k, "enable-reflector") == 0)
385 config->server_config.enable_reflector = is_yes(v);
386 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
387 config->server_config.reflect_ipv = is_yes(v);
389 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
401 fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
423 static void log_function(AvahiLogLevel level, const gchar *txt) {
425 static const int const log_level_map[] = {
433 g_assert(level < AVAHI_LOG_LEVEL_MAX);
436 daemon_log(log_level_map[level], "%s", txt);
439 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
441 GMainLoop *loop = data;
446 if ((sig = daemon_signal_next()) <= 0) {
447 avahi_log_error("daemon_signal_next() failed");
457 sig == SIGINT ? "SIGINT" :
458 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
459 g_main_loop_quit(loop);
463 avahi_log_info("Got SIGHUP, reloading.");
464 static_service_load();
465 static_service_add_to_server();
467 if (resolv_conf_entry_group) {
468 avahi_entry_group_free(resolv_conf_entry_group);
469 resolv_conf_entry_group = NULL;
472 load_resolv_conf(&config);
474 if (resolv_conf && resolv_conf[0])
475 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf);
480 avahi_log_warn("Got spurious signal, ignoring.");
487 static gint run_server(DaemonConfig *config) {
488 GMainLoop *loop = NULL;
490 GIOChannel *io = NULL;
491 guint watch_id = (guint) -1;
495 loop = g_main_loop_new(NULL, FALSE);
497 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, 0) < 0) {
498 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
502 if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
503 avahi_log_error( "Failed to create signal io channel.");
507 g_io_channel_set_close_on_unref(io, FALSE);
508 g_io_add_watch(io, G_IO_IN, signal_callback, loop);
510 if (simple_protocol_setup(NULL) < 0)
514 if (config->enable_dbus)
515 if (dbus_protocol_setup(loop) < 0)
519 if (!(avahi_server = avahi_server_new(NULL, &config->server_config, server_callback, config)))
522 load_resolv_conf(config);
524 static_service_load();
526 if (config->daemonize) {
527 daemon_retval_send(0);
531 g_main_loop_run(loop);
535 static_service_remove_from_server();
536 static_service_free_all();
537 remove_dns_server_entry_groups();
539 simple_protocol_shutdown();
542 if (config->enable_dbus)
543 dbus_protocol_shutdown();
548 avahi_server_free(avahi_server);
550 daemon_signal_done();
552 if (watch_id != (guint) -1)
553 g_source_remove(watch_id);
556 g_io_channel_unref(io);
560 g_main_loop_unref(loop);
562 if (r != 0 && config->daemonize)
563 daemon_retval_send(1);
568 static gint drop_root(void) {
573 if (!(pw = getpwnam(AVAHI_USER))) {
574 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
578 if (!(gr = getgrnam(AVAHI_GROUP))) {
579 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
583 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);
585 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
586 avahi_log_error("Failed to change group list: %s", strerror(errno));
590 #if defined(HAVE_SETRESGID)
591 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
592 #elif defined(HAVE_SETREGID)
593 r = setregid(gr->gr_gid, gr->gr_gid);
595 if ((r = setgid(gr->gr_gid)) >= 0)
596 r = setegid(gr->gr_gid);
600 avahi_log_error("Failed to change GID: %s", strerror(errno));
604 #if defined(HAVE_SETRESUID)
605 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
606 #elif defined(HAVE_SETREUID)
607 r = setreuid(pw->pw_uid, pw->pw_uid);
609 if ((r = setuid(pw->pw_uid)) >= 0)
610 r = seteuid(pw->pw_uid);
614 avahi_log_error("Failed to change UID: %s", strerror(errno));
618 g_setenv("USER", pw->pw_name, 1);
619 g_setenv("LOGNAME", pw->pw_name, 1);
620 g_setenv("HOME", pw->pw_dir, 1);
622 avahi_log_info("Successfully dropped root privileges.");
627 static const char* pid_file_proc(void) {
628 return AVAHI_RUNTIME_DIR"/avahi-daemon.pid";
631 static gint make_runtime_dir(void) {
634 gboolean reset_umask = FALSE;
639 if (!(pw = getpwnam(AVAHI_USER))) {
640 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
644 if (!(gr = getgrnam(AVAHI_GROUP))) {
645 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
652 if (mkdir(AVAHI_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
653 avahi_log_error("mkdir(\""AVAHI_RUNTIME_DIR"\"): %s", strerror(errno));
657 chown(AVAHI_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
659 if (stat(AVAHI_RUNTIME_DIR, &st) < 0) {
660 avahi_log_error("stat(): %s\n", strerror(errno));
664 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
665 avahi_log_error("Failed to create runtime directory "AVAHI_RUNTIME_DIR".");
678 int main(int argc, char *argv[]) {
681 gboolean wrote_pid_file = FALSE;
683 avahi_set_log_function(log_function);
685 avahi_server_config_init(&config.server_config);
686 config.command = DAEMON_RUN;
687 config.daemonize = FALSE;
688 config.config_file = NULL;
689 config.enable_dbus = TRUE;
690 config.drop_root = TRUE;
691 config.publish_dns_servers = NULL;
692 config.publish_resolv_conf = FALSE;
694 if ((argv0 = strrchr(argv[0], '/')))
699 daemon_pid_file_ident = daemon_log_ident = (char *) argv0;
700 daemon_pid_file_proc = pid_file_proc;
702 if (parse_command_line(&config, argc, argv) < 0)
705 if (config.command == DAEMON_HELP) {
708 } else if (config.command == DAEMON_VERSION) {
709 printf("%s "PACKAGE_VERSION"\n", argv0);
711 } else if (config.command == DAEMON_KILL) {
712 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
713 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
719 } else if (config.command == DAEMON_RELOAD) {
720 if (daemon_pid_file_kill(SIGHUP) < 0) {
721 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
727 } else if (config.command == DAEMON_CHECK)
728 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
729 else if (config.command == DAEMON_RUN) {
733 avahi_log_error("This program is intended to be run as root.");
737 if ((pid = daemon_pid_file_is_running()) >= 0) {
738 avahi_log_error("Daemon already running on PID %u", pid);
742 if (load_config_file(&config) < 0)
745 if (config.daemonize) {
746 daemon_retval_init();
748 if ((pid = daemon_fork()) < 0)
754 if ((ret = daemon_retval_wait(20)) < 0) {
755 avahi_log_error("Could not recieve return value from daemon process.");
768 if (make_runtime_dir() < 0)
771 if (config.drop_root) {
776 if (daemon_pid_file_create() < 0) {
777 avahi_log_error("Failed to create PID file: %s", strerror(errno));
779 if (config.daemonize)
780 daemon_retval_send(1);
783 wrote_pid_file = TRUE;
785 if (run_server(&config) == 0)
791 if (config.daemonize)
792 daemon_retval_done();
794 avahi_server_config_free(&config.server_config);
795 g_free(config.config_file);
796 g_strfreev(config.publish_dns_servers);
797 g_strfreev(resolv_conf);
800 daemon_pid_file_remove();