+ for (g = f->groups; g; g = g->groups_next) {
+
+ if (strcasecmp(g->name, "server") == 0) {
+ AvahiIniFilePair *p;
+
+ for (p = g->pairs; p; p = p->pairs_next) {
+
+ if (strcasecmp(p->key, "host-name") == 0) {
+ avahi_free(c->server_config.host_name);
+ c->server_config.host_name = avahi_strdup(p->value);
+ } else if (strcasecmp(p->key, "domain-name") == 0) {
+ avahi_free(c->server_config.domain_name);
+ c->server_config.domain_name = avahi_strdup(p->value);
+ } else if (strcasecmp(p->key, "browse-domains") == 0) {
+ char **e, **t;
+
+ e = avahi_split_csv(p->value);
+
+ for (t = e; *t; t++) {
+ char cleaned[AVAHI_DOMAIN_NAME_MAX];
+
+ if (!avahi_normalize_name(*t, cleaned, sizeof(cleaned))) {
+ avahi_log_error("Invalid domain name \"%s\" for key \"%s\" in group \"%s\"\n", *t, p->key, g->name);
+ avahi_strfreev(e);
+ goto finish;
+ }
+
+ c->server_config.browse_domains = avahi_string_list_add(c->server_config.browse_domains, cleaned);
+ }
+
+ avahi_strfreev(e);
+
+ c->server_config.browse_domains = filter_duplicate_domains(c->server_config.browse_domains);
+ } else if (strcasecmp(p->key, "use-ipv4") == 0)
+ c->server_config.use_ipv4 = is_yes(p->value);
+ else if (strcasecmp(p->key, "use-ipv6") == 0)
+ c->server_config.use_ipv6 = is_yes(p->value);
+ else if (strcasecmp(p->key, "check-response-ttl") == 0)
+ c->server_config.check_response_ttl = is_yes(p->value);
+ else if (strcasecmp(p->key, "allow-point-to-point") == 0)
+ c->server_config.allow_point_to_point = is_yes(p->value);
+ else if (strcasecmp(p->key, "use-iff-running") == 0)
+ c->server_config.use_iff_running = is_yes(p->value);
+ else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
+ c->server_config.disallow_other_stacks = is_yes(p->value);
+#ifdef HAVE_DBUS
+ else if (strcasecmp(p->key, "enable-dbus") == 0) {
+
+ if (*(p->value) == 'w' || *(p->value) == 'W') {
+ c->fail_on_missing_dbus = 0;
+ c->enable_dbus = 1;
+ } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
+ c->fail_on_missing_dbus = 1;
+ c->enable_dbus = 1;
+ } else {
+ c->enable_dbus = 0;
+ }
+ }
+#endif
+ else if (strcasecmp(p->key, "allow-interfaces") == 0) {
+ char **e, **t;
+
+ avahi_string_list_free(c->server_config.allow_interfaces);
+ c->server_config.allow_interfaces = NULL;
+ e = avahi_split_csv(p->value);
+
+ for (t = e; *t; t++)
+ c->server_config.allow_interfaces = avahi_string_list_add(c->server_config.allow_interfaces, *t);
+
+ avahi_strfreev(e);
+ } else if (strcasecmp(p->key, "deny-interfaces") == 0) {
+ char **e, **t;
+
+ avahi_string_list_free(c->server_config.deny_interfaces);
+ c->server_config.deny_interfaces = NULL;
+ e = avahi_split_csv(p->value);
+
+ for (t = e; *t; t++)
+ c->server_config.deny_interfaces = avahi_string_list_add(c->server_config.deny_interfaces, *t);
+
+ avahi_strfreev(e);
+ } else if (strcasecmp(p->key, "cache-entries-max") == 0) {
+ unsigned k;
+
+ if (parse_unsigned(p->value, &k) < 0) {
+ avahi_log_error("Invalid cache-entries-max setting %s", p->value);
+ goto finish;
+ }
+
+ c->server_config.n_cache_entries_max = k;
+#ifdef HAVE_DBUS
+ } else if (strcasecmp(p->key, "clients-max") == 0) {
+ unsigned k;
+
+ if (parse_unsigned(p->value, &k) < 0) {
+ avahi_log_error("Invalid clients-max setting %s", p->value);
+ goto finish;
+ }
+
+ c->n_clients_max = k;
+ } else if (strcasecmp(p->key, "objects-per-client-max") == 0) {
+ unsigned k;
+
+ if (parse_unsigned(p->value, &k) < 0) {
+ avahi_log_error("Invalid objects-per-client-max setting %s", p->value);
+ goto finish;
+ }
+
+ c->n_objects_per_client_max = k;
+ } else if (strcasecmp(p->key, "entries-per-entry-group-max") == 0) {
+ unsigned k;
+
+ if (parse_unsigned(p->value, &k) < 0) {
+ avahi_log_error("Invalid entries-per-entry-group-max setting %s", p->value);
+ goto finish;
+ }
+
+ c->n_entries_per_entry_group_max = k;
+#endif
+ } else {
+ avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
+ goto finish;
+ }
+ }
+
+ } else if (strcasecmp(g->name, "publish") == 0) {
+ AvahiIniFilePair *p;
+
+ for (p = g->pairs; p; p = p->pairs_next) {
+
+ if (strcasecmp(p->key, "publish-addresses") == 0)
+ c->server_config.publish_addresses = is_yes(p->value);
+ else if (strcasecmp(p->key, "publish-hinfo") == 0)
+ c->server_config.publish_hinfo = is_yes(p->value);
+ else if (strcasecmp(p->key, "publish-workstation") == 0)
+ c->server_config.publish_workstation = is_yes(p->value);
+ else if (strcasecmp(p->key, "publish-domain") == 0)
+ c->server_config.publish_domain = is_yes(p->value);
+ else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
+ c->publish_resolv_conf = is_yes(p->value);
+ else if (strcasecmp(p->key, "disable-publishing") == 0)
+ c->server_config.disable_publishing = is_yes(p->value);
+ else if (strcasecmp(p->key, "disable-user-service-publishing") == 0)
+ c->disable_user_service_publishing = is_yes(p->value);
+ else if (strcasecmp(p->key, "add-service-cookie") == 0)
+ c->server_config.add_service_cookie = is_yes(p->value);
+ else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
+ avahi_strfreev(c->publish_dns_servers);
+ c->publish_dns_servers = avahi_split_csv(p->value);
+ } else if (strcasecmp(p->key, "publish-a-on-ipv6") == 0)
+ c->server_config.publish_a_on_ipv6 = is_yes(p->value);
+ else if (strcasecmp(p->key, "publish-aaaa-on-ipv4") == 0)
+ c->server_config.publish_aaaa_on_ipv4 = is_yes(p->value);
+ else {
+ avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
+ goto finish;
+ }
+ }
+
+ } else if (strcasecmp(g->name, "wide-area") == 0) {
+ AvahiIniFilePair *p;
+
+ for (p = g->pairs; p; p = p->pairs_next) {
+
+ if (strcasecmp(p->key, "enable-wide-area") == 0)
+ c->server_config.enable_wide_area = is_yes(p->value);
+ else {
+ avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
+ goto finish;
+ }
+ }
+
+ } else if (strcasecmp(g->name, "reflector") == 0) {
+ AvahiIniFilePair *p;
+
+ for (p = g->pairs; p; p = p->pairs_next) {
+
+ if (strcasecmp(p->key, "enable-reflector") == 0)
+ c->server_config.enable_reflector = is_yes(p->value);
+ else if (strcasecmp(p->key, "reflect-ipv") == 0)
+ c->server_config.reflect_ipv = is_yes(p->value);
+ else {
+ avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
+ goto finish;
+ }
+ }
+
+ } else if (strcasecmp(g->name, "rlimits") == 0) {
+ AvahiIniFilePair *p;
+
+ for (p = g->pairs; p; p = p->pairs_next) {
+
+ if (strcasecmp(p->key, "rlimit-as") == 0) {
+ c->rlimit_as_set = 1;
+ c->rlimit_as = atoi(p->value);
+ } else if (strcasecmp(p->key, "rlimit-core") == 0) {
+ c->rlimit_core_set = 1;
+ c->rlimit_core = atoi(p->value);
+ } else if (strcasecmp(p->key, "rlimit-data") == 0) {
+ c->rlimit_data_set = 1;
+ c->rlimit_data = atoi(p->value);
+ } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
+ c->rlimit_fsize_set = 1;
+ c->rlimit_fsize = atoi(p->value);
+ } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
+ c->rlimit_nofile_set = 1;
+ c->rlimit_nofile = atoi(p->value);
+ } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
+ c->rlimit_stack_set = 1;
+ c->rlimit_stack = atoi(p->value);
+ } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
+#ifdef RLIMIT_NPROC
+ c->rlimit_nproc_set = 1;
+ c->rlimit_nproc = atoi(p->value);
+#else
+ avahi_log_error("Ignoring configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
+#endif
+ } else {
+ avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
+ goto finish;
+ }
+
+ }
+
+ } else {
+ avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
+ goto finish;
+ }
+ }
+
+ r = 0;
+
+finish:
+
+ if (f)
+ avahi_ini_file_free(f);
+
+ return r;
+}
+
+static void log_function(AvahiLogLevel level, const char *txt) {
+
+ static const int log_level_map[] = {
+ LOG_ERR,
+ LOG_WARNING,
+ LOG_NOTICE,
+ LOG_INFO,
+ LOG_DEBUG
+ };
+
+ assert(level < AVAHI_LOG_LEVEL_MAX);
+ assert(txt);
+
+ if (!config.debug && level == AVAHI_LOG_DEBUG)
+ return;
+
+ daemon_log(log_level_map[level], "%s", txt);
+}
+
+static void dump(const char *text, AVAHI_GCC_UNUSED void* userdata) {
+ avahi_log_info("%s", text);
+}
+
+#ifdef HAVE_INOTIFY
+
+static int inotify_fd = -1;
+
+static void add_inotify_watches(void) {
+ int c = 0;
+ /* We ignore the return values, because one or more of these files
+ * might not exist and we're OK with that. In addition we never
+ * want to remove these watches, hence we keep their ids? */
+
+#ifdef ENABLE_CHROOT
+ c = config.use_chroot;
+#endif
+
+ inotify_add_watch(inotify_fd, c ? "/services" : AVAHI_SERVICE_DIR, IN_CLOSE_WRITE|IN_DELETE|IN_DELETE_SELF|IN_MOVED_FROM|IN_MOVED_TO|IN_MOVE_SELF
+#ifdef IN_ONLYDIR
+ |IN_ONLYDIR
+#endif
+ );
+ inotify_add_watch(inotify_fd, c ? "/" : AVAHI_CONFIG_DIR, IN_CLOSE_WRITE|IN_DELETE|IN_DELETE_SELF|IN_MOVED_FROM|IN_MOVED_TO|IN_MOVE_SELF
+#ifdef IN_ONLYDIR
+ |IN_ONLYDIR
+#endif
+ );
+}
+
+#endif
+
+#ifdef HAVE_KQUEUE
+
+#define NUM_WATCHES 2
+
+static int kq = -1;
+static int kfds[NUM_WATCHES];
+static int num_kfds = 0;
+
+static void add_kqueue_watch(const char *dir);
+
+static void add_kqueue_watches(void) {
+ int c = 0;
+
+#ifdef ENABLE_CHROOT
+ c = config.use_chroot;
+#endif
+
+ add_kqueue_watch(c ? "/" : AVAHI_CONFIG_DIR);
+ add_kqueue_watch(c ? "/services" : AVAHI_SERVICE_DIR);
+}
+
+static void add_kqueue_watch(const char *dir) {
+ int fd;
+ struct kevent ev;
+
+ if (kq < 0)
+ return;
+
+ if (num_kfds >= NUM_WATCHES)
+ return;
+
+ fd = open(dir, O_RDONLY);
+ if (fd < 0)
+ return;
+ EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
+ NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_RENAME,
+ 0, 0);
+ if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) {
+ close(fd);
+ return;
+ }
+
+ kfds[num_kfds++] = fd;
+}
+
+#endif
+
+static void reload_config(void) {
+
+#ifdef HAVE_INOTIFY
+ /* Refresh in case the config dirs have been removed */
+ add_inotify_watches();
+#endif
+
+#ifdef HAVE_KQUEUE
+ add_kqueue_watches();
+#endif
+
+#ifdef ENABLE_CHROOT
+ static_service_load(config.use_chroot);
+ static_hosts_load(config.use_chroot);
+#else
+ static_service_load(0);
+ static_hosts_load(0);
+#endif
+ static_service_add_to_server();
+ static_hosts_add_to_server();
+
+ if (resolv_conf_entry_group)
+ avahi_s_entry_group_reset(resolv_conf_entry_group);
+
+ load_resolv_conf();
+
+ update_wide_area_servers();
+ update_browse_domains();
+
+ if (config.publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
+ resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf_name_servers);
+}
+
+#ifdef HAVE_INOTIFY
+
+static void inotify_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
+ char* buffer;
+ int n = 0;
+
+ assert(fd == inotify_fd);
+ assert(watch);
+
+ ioctl(inotify_fd, FIONREAD, &n);
+ if (n <= 0)
+ n = 128;
+
+ buffer = avahi_malloc(n);
+ if (read(inotify_fd, buffer, n) < 0 ) {
+ avahi_free(buffer);
+ avahi_log_error("Failed to read inotify event: %s", avahi_strerror(errno));
+ return;
+ }
+ avahi_free(buffer);
+
+ avahi_log_info("Files changed, reloading.");
+ reload_config();
+}
+
+#endif
+
+#ifdef HAVE_KQUEUE
+
+static void kqueue_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
+ struct kevent ev;
+ struct timespec nullts = { 0, 0 };
+ int res;
+
+ assert(fd == kq);
+ assert(watch);
+
+ res = kevent(kq, NULL, 0, &ev, 1, &nullts);
+
+ if (res > 0) {
+ /* Sleep for a half-second to avoid potential races
+ * during install/uninstall. */
+ usleep(500000);
+ avahi_log_info("Files changed, reloading.");
+ reload_config();
+ } else {
+ avahi_log_error("Failed to read kqueue event: %s", avahi_strerror(errno));
+ }
+}
+
+#endif
+
+static void signal_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
+ int sig;
+ const AvahiPoll *poll_api;
+
+ assert(watch);
+ assert(simple_poll_api);
+
+ poll_api = avahi_simple_poll_get(simple_poll_api);
+
+ if ((sig = daemon_signal_next()) <= 0) {
+ avahi_log_error("daemon_signal_next() failed");
+ poll_api->watch_free(watch);
+ return;
+ }
+
+ switch (sig) {
+ case SIGINT:
+ case SIGQUIT:
+ case SIGTERM:
+ avahi_log_info(
+ "Got %s, quitting.",
+ sig == SIGINT ? "SIGINT" :
+ (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
+ avahi_simple_poll_quit(simple_poll_api);
+ break;
+
+ case SIGHUP:
+ avahi_log_info("Got SIGHUP, reloading.");
+
+ reload_config();
+ break;
+
+ case SIGUSR1:
+ avahi_log_info("Got SIGUSR1, dumping record data.");
+ avahi_server_dump(avahi_server, dump, NULL);
+ break;
+
+ default:
+ avahi_log_warn("Got spurious signal, ignoring.");
+ break;
+ }
+}
+
+/* Imported from ../avahi-client/nss-check.c */
+int avahi_nss_support(void);
+
+static int run_server(DaemonConfig *c) {
+ int r = -1;
+ int error;
+ const AvahiPoll *poll_api = NULL;
+ AvahiWatch *sig_watch = NULL;
+ int retval_is_sent = 0;
+#ifdef HAVE_INOTIFY
+ AvahiWatch *inotify_watch = NULL;
+#endif
+#ifdef HAVE_KQUEUE
+ int i;
+ AvahiWatch *kqueue_watch = NULL;
+#endif
+
+ assert(c);
+
+ if (!(nss_support = avahi_nss_support()))
+ avahi_log_warn("WARNING: No NSS support for mDNS detected, consider installing nss-mdns!");
+
+ if (!(simple_poll_api = avahi_simple_poll_new())) {
+ avahi_log_error("Failed to create main loop object.");