+static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
+ char **p;
+
+ assert(s);
+ assert(l);
+
+ if (!g)
+ g = avahi_s_entry_group_new(s, NULL, NULL);
+
+ assert(avahi_s_entry_group_is_empty(g));
+
+ for (p = l; *p; p++) {
+ AvahiAddress a;
+
+ if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a))
+ avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
+ else
+ if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
+ avahi_s_entry_group_free(g);
+ avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
+ return NULL;
+ }
+ }
+
+ avahi_s_entry_group_commit(g);
+
+ return g;
+}
+
+static void remove_dns_server_entry_groups(void) {
+
+ if (resolv_conf_entry_group)
+ avahi_s_entry_group_reset(resolv_conf_entry_group);
+
+ if (dns_servers_entry_group)
+ avahi_s_entry_group_reset(dns_servers_entry_group);
+}
+
+static void update_wide_area_servers(void) {
+ AvahiAddress a[AVAHI_WIDE_AREA_SERVERS_MAX];
+ unsigned n = 0;
+ char **p;
+
+ if (!resolv_conf_name_servers) {
+ avahi_server_set_wide_area_servers(avahi_server, NULL, 0);
+ return;
+ }
+
+ for (p = resolv_conf_name_servers; *p && n < AVAHI_WIDE_AREA_SERVERS_MAX; p++) {
+ if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a[n]))
+ avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
+ else
+ n++;
+ }
+
+ avahi_server_set_wide_area_servers(avahi_server, a, n);
+}
+
+static AvahiStringList *filter_duplicate_domains(AvahiStringList *l) {
+ AvahiStringList *e, *n, *p;
+
+ if (!l)
+ return l;
+
+ for (p = l, e = l->next; e; e = n) {
+ n = e->next;
+
+ if (avahi_domain_equal((char*) e->text, (char*) l->text)) {
+ p->next = e->next;
+ avahi_free(e);
+ } else
+ p = e;
+ }
+
+ l->next = filter_duplicate_domains(l->next);
+ return l;
+}
+
+static void update_browse_domains(void) {
+ AvahiStringList *l;
+ int n;
+ char **p;
+
+ if (!resolv_conf_search_domains) {
+ avahi_server_set_browse_domains(avahi_server, NULL);
+ return;
+ }
+
+ l = avahi_string_list_copy(config.server_config.browse_domains);
+
+ for (p = resolv_conf_search_domains, n = 0; *p && n < BROWSE_DOMAINS_MAX; p++, n++) {
+ if (!avahi_is_valid_domain_name(*p))
+ avahi_log_warn("'%s' is no valid domain name, ignoring.", *p);
+ else
+ l = avahi_string_list_add(l, *p);
+ }
+
+ l = filter_duplicate_domains(l);
+
+ avahi_server_set_browse_domains(avahi_server, l);
+ avahi_string_list_free(l);
+}
+
+static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
+ DaemonConfig *c = userdata;
+
+ assert(s);
+ assert(c);
+
+ /* This function is possibly called before the global variable
+ * avahi_server has been set, therefore we do it explicitly */
+
+ avahi_server = s;
+
+#ifdef HAVE_DBUS
+ if (c->enable_dbus && state != AVAHI_SERVER_INVALID && state != AVAHI_SERVER_FAILURE)
+ dbus_protocol_server_state_changed(state);
+#endif
+
+ switch (state) {
+ case AVAHI_SERVER_RUNNING:
+ avahi_log_info("Server startup complete. Host name is %s. Local service cookie is %u.", avahi_server_get_host_name_fqdn(s), avahi_server_get_local_service_cookie(s));
+
+ avahi_set_proc_title(argv0, "%s: running [%s]", argv0, avahi_server_get_host_name_fqdn(s));
+
+ static_service_add_to_server();
+ static_hosts_add_to_server();
+
+ remove_dns_server_entry_groups();
+
+ if (c->publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
+ resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf_name_servers);
+
+ if (c->publish_dns_servers && c->publish_dns_servers[0])
+ dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
+
+ simple_protocol_restart_queries();
+ break;
+
+ case AVAHI_SERVER_COLLISION: {
+ char *n;
+
+ avahi_set_proc_title(argv0, "%s: collision", argv0);
+
+ static_service_remove_from_server();
+ static_hosts_remove_from_server();
+ remove_dns_server_entry_groups();
+
+ n = avahi_alternative_host_name(avahi_server_get_host_name(s));
+ avahi_log_warn("Host name conflict, retrying with <%s>", n);
+ avahi_server_set_host_name(s, n);
+ avahi_free(n);
+
+ break;
+ }
+
+ case AVAHI_SERVER_FAILURE:
+
+ avahi_log_error("Server error: %s", avahi_strerror(avahi_server_errno(s)));
+ avahi_simple_poll_quit(simple_poll_api);
+ break;
+
+ case AVAHI_SERVER_REGISTERING:
+
+ avahi_set_proc_title(argv0, "%s: registering [%s]", argv0, avahi_server_get_host_name_fqdn(s));
+
+ static_service_remove_from_server();
+ static_hosts_remove_from_server();
+ remove_dns_server_entry_groups();
+
+ break;
+
+ case AVAHI_SERVER_INVALID:
+ break;
+
+ }
+}
+
+static void help(FILE *f) {
+ fprintf(f,
+ "%s [options]\n"
+ " -h --help Show this help\n"
+ " -D --daemonize Daemonize after startup (implies -s)\n"
+ " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
+ " -k --kill Kill a running daemon\n"
+ " -r --reload Request a running daemon to reload static services\n"
+ " -c --check Return 0 if a daemon is already running\n"
+ " -V --version Show version\n"
+ " -f --file=FILE Load the specified configuration file instead of\n"
+ " "AVAHI_CONFIG_FILE"\n"
+ " --no-rlimits Don't enforce resource limits\n"
+ " --no-drop-root Don't drop privileges\n"
+#ifdef ENABLE_CHROOT
+ " --no-chroot Don't chroot()\n"
+#endif
+ " --no-proc-title Don't modify process title\n"
+ " --debug Increase verbosity\n",
+ argv0);
+}
+
+
+static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
+ int o;
+
+ enum {
+ OPTION_NO_RLIMITS = 256,
+ OPTION_NO_DROP_ROOT,
+#ifdef ENABLE_CHROOT
+ OPTION_NO_CHROOT,
+#endif
+ OPTION_NO_PROC_TITLE,
+ OPTION_DEBUG
+ };
+
+ static const struct option long_options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "daemonize", no_argument, NULL, 'D' },
+ { "kill", no_argument, NULL, 'k' },
+ { "version", no_argument, NULL, 'V' },
+ { "file", required_argument, NULL, 'f' },
+ { "reload", no_argument, NULL, 'r' },
+ { "check", no_argument, NULL, 'c' },
+ { "syslog", no_argument, NULL, 's' },
+ { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
+ { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
+#ifdef ENABLE_CHROOT
+ { "no-chroot", no_argument, NULL, OPTION_NO_CHROOT },
+#endif
+ { "no-proc-title", no_argument, NULL, OPTION_NO_PROC_TITLE },
+ { "debug", no_argument, NULL, OPTION_DEBUG },
+ { NULL, 0, NULL, 0 }
+ };
+
+ assert(c);
+
+ while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
+
+ switch(o) {
+ case 's':
+ c->use_syslog = 1;
+ break;
+ case 'h':
+ c->command = DAEMON_HELP;
+ break;
+ case 'D':
+ c->daemonize = 1;
+ break;
+ case 'k':
+ c->command = DAEMON_KILL;
+ break;
+ case 'V':
+ c->command = DAEMON_VERSION;
+ break;
+ case 'f':
+ avahi_free(c->config_file);
+ c->config_file = avahi_strdup(optarg);
+ break;
+ case 'r':
+ c->command = DAEMON_RELOAD;
+ break;
+ case 'c':
+ c->command = DAEMON_CHECK;
+ break;
+ case OPTION_NO_RLIMITS:
+ c->set_rlimits = 0;
+ break;
+ case OPTION_NO_DROP_ROOT:
+ c->drop_root = 0;
+ break;
+#ifdef ENABLE_CHROOT
+ case OPTION_NO_CHROOT:
+ c->use_chroot = 0;
+ break;
+#endif
+ case OPTION_NO_PROC_TITLE:
+ c->modify_proc_title = 0;
+ break;
+ case OPTION_DEBUG:
+ c->debug = 1;
+ break;
+ default:
+ return -1;
+ }
+ }
+
+ if (optind < argc) {
+ fprintf(stderr, "Too many arguments\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int is_yes(const char *s) {
+ assert(s);
+
+ return *s == 'y' || *s == 'Y' || *s == '1' || *s == 't' || *s == 'T';
+}
+
+static int parse_unsigned(const char *s, unsigned *u) {
+ char *e = NULL;
+ unsigned long ul;
+ unsigned k;
+
+ errno = 0;
+ ul = strtoul(s, &e, 0);
+
+ if (!e || *e || errno != 0)
+ return -1;
+
+ k = (unsigned) ul;
+
+ if ((unsigned long) k != ul)
+ return -1;
+
+ *u = k;
+ return 0;
+}
+
+static int parse_usec(const char *s, AvahiUsec *u) {
+ char *e = NULL;
+ unsigned long long ull;
+ AvahiUsec k;
+
+ errno = 0;
+ ull = strtoull(s, &e, 0);
+
+ if (!e || *e || errno != 0)
+ return -1;
+
+ k = (AvahiUsec) ull;
+
+ if ((unsigned long long) k != ull)
+ return -1;
+
+ *u = k;
+ return 0;
+}
+
+static int load_config_file(DaemonConfig *c) {
+ int r = -1;
+ AvahiIniFile *f;
+ AvahiIniFileGroup *g;
+
+ assert(c);
+
+ if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
+ goto finish;
+
+ 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, "ratelimit-interval-usec") == 0) {
+ AvahiUsec k;
+
+ if (parse_usec(p->value, &k) < 0) {
+ avahi_log_error("Invalid ratelimit-interval-usec setting %s", p->value);
+ goto finish;
+ }
+
+ c->server_config.ratelimit_interval = k;
+
+ } else if (strcasecmp(p->key, "ratelimit-burst") == 0) {
+ unsigned k;
+
+ if (parse_unsigned(p->value, &k) < 0) {
+ avahi_log_error("Invalid ratelimit-burst setting %s", p->value);
+ goto finish;
+ }
+
+ c->server_config.ratelimit_burst = k;
+
+ } 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);