+#include <avahi-core/publish.h>
+#include <avahi-core/dns-srv-rr.h>
+#include <avahi-core/log.h>
+
+#ifdef ENABLE_CHROOT
+#include "chroot.h"
+#include "caps.h"
+#endif
+
+#include "setproctitle.h"
+#include "main.h"
+#include "simple-protocol.h"
+#include "static-services.h"
+#include "static-hosts.h"
+#include "ini-file-parser.h"
+
+#ifdef HAVE_DBUS
+#include "dbus-protocol.h"
+#endif
+
+AvahiServer *avahi_server = NULL;
+AvahiSimplePoll *simple_poll_api = NULL;
+static char *argv0 = NULL;
+int nss_support = 0;
+
+typedef enum {
+ DAEMON_RUN,
+ DAEMON_KILL,
+ DAEMON_VERSION,
+ DAEMON_HELP,
+ DAEMON_RELOAD,
+ DAEMON_CHECK
+} DaemonCommand;
+
+typedef struct {
+ AvahiServerConfig server_config;
+ DaemonCommand command;
+ int daemonize;
+ int use_syslog;
+ char *config_file;
+#ifdef HAVE_DBUS
+ int enable_dbus;
+ int fail_on_missing_dbus;
+#endif
+ int drop_root;
+ int set_rlimits;
+#ifdef ENABLE_CHROOT
+ int use_chroot;
+#endif
+ int modify_proc_title;
+
+ int disable_user_service_publishing;
+ int publish_resolv_conf;
+ char ** publish_dns_servers;
+ int debug;
+
+ int rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
+ rlim_t rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
+
+#ifdef RLIMIT_NPROC
+ int rlimit_nproc_set;
+ rlim_t rlimit_nproc;
+#endif
+} DaemonConfig;
+
+#define RESOLV_CONF "/etc/resolv.conf"
+
+static AvahiSEntryGroup *dns_servers_entry_group = NULL;
+static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
+
+static char **resolv_conf = NULL;
+
+static DaemonConfig config;
+
+static int has_prefix(const char *s, const char *prefix) {
+ size_t l;
+
+ l = strlen(prefix);
+
+ return strlen(s) >= l && strncmp(s, prefix, l) == 0;
+}
+
+static int load_resolv_conf(void) {
+ int ret = -1;
+ FILE *f;
+ int i = 0;
+
+ avahi_strfreev(resolv_conf);
+ resolv_conf = NULL;
+
+#ifdef ENABLE_CHROOT
+ f = avahi_chroot_helper_get_file(RESOLV_CONF);
+#else
+ f = fopen(RESOLV_CONF, "r");
+#endif
+
+ if (!f) {
+ avahi_log_warn("Failed to open "RESOLV_CONF": %s", strerror(errno));
+ goto finish;
+ }
+
+ resolv_conf = avahi_new0(char*, AVAHI_WIDE_AREA_SERVERS_MAX+1);
+
+ while (!feof(f) && i < AVAHI_WIDE_AREA_SERVERS_MAX) {
+ char ln[128];
+ char *p;
+
+ if (!(fgets(ln, sizeof(ln), f)))
+ break;
+
+ ln[strcspn(ln, "\r\n#")] = 0;
+ p = ln + strspn(ln, "\t ");
+
+ if (has_prefix(p, "nameserver")) {
+ p += 10;
+ p += strspn(p, "\t ");
+ p[strcspn(p, "\t ")] = 0;
+ resolv_conf[i++] = avahi_strdup(p);
+ }
+ }
+
+ ret = 0;
+
+finish:
+
+ if (ret != 0) {
+ avahi_strfreev(resolv_conf);
+ resolv_conf = NULL;
+ }
+
+ if (f)
+ fclose(f);
+
+ return ret;
+}
+
+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) {
+ avahi_server_set_wide_area_servers(avahi_server, NULL, 0);
+ return;
+ }
+
+ for (p = resolv_conf; *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 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 && resolv_conf[0])
+ resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
+
+ 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';
+}