forked from avahi/avahi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
1719 lines (1355 loc) · 49.7 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***
This file is part of avahi.
avahi is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
avahi is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with avahi; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <assert.h>
#include <getopt.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <grp.h>
#include <pwd.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/socket.h>
#ifdef HAVE_INOTIFY
#include <sys/inotify.h>
#endif
#ifdef HAVE_KQUEUE
#include <sys/types.h>
#include <sys/event.h>
#include <unistd.h>
#endif
#include <libdaemon/dfork.h>
#include <libdaemon/dsignal.h>
#include <libdaemon/dlog.h>
#include <libdaemon/dpid.h>
#include <avahi-common/malloc.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/error.h>
#include <avahi-common/alternative.h>
#include <avahi-common/domain.h>
#include <avahi-core/core.h>
#include <avahi-core/publish.h>
#include <avahi-core/dns-srv-rr.h>
#include <avahi-core/log.h>
#include <avahi-core/util.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"
#include "sd-daemon.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;
unsigned n_clients_max;
unsigned n_objects_per_client_max;
unsigned n_entries_per_entry_group_max;
#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"
#define BROWSE_DOMAINS_MAX 16
static AvahiSEntryGroup *dns_servers_entry_group = NULL;
static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
static char **resolv_conf_name_servers = NULL;
static char **resolv_conf_search_domains = 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, j = 0;
avahi_strfreev(resolv_conf_name_servers);
resolv_conf_name_servers = NULL;
avahi_strfreev(resolv_conf_search_domains);
resolv_conf_search_domains = 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_name_servers = avahi_new0(char*, AVAHI_WIDE_AREA_SERVERS_MAX+1);
resolv_conf_search_domains = avahi_new0(char*, BROWSE_DOMAINS_MAX+1);
while (!feof(f)) {
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 ") || has_prefix(p, "nameserver\t")) && i < AVAHI_WIDE_AREA_SERVERS_MAX) {
p += 10;
p += strspn(p, "\t ");
p[strcspn(p, "\t ")] = 0;
resolv_conf_name_servers[i++] = avahi_strdup(p);
}
if ((has_prefix(p, "search ") || has_prefix(p, "search\t") ||
has_prefix(p, "domain ") || has_prefix(p, "domain\t"))) {
p += 6;
while (j < BROWSE_DOMAINS_MAX) {
size_t k;
p += strspn(p, "\t ");
k = strcspn(p, "\t ");
if (k > 0) {
resolv_conf_search_domains[j++] = avahi_strndup(p, k);
p += k;
}
if (!*p)
break;
}
}
}
ret = 0;
finish:
if (ret != 0) {
avahi_strfreev(resolv_conf_name_servers);
resolv_conf_name_servers = NULL;
avahi_strfreev(resolv_conf_search_domains);
resolv_conf_search_domains = 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_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));
sd_notifyf(0, "STATUS=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;
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);
sd_notifyf(0, "STATUS=Host name conflict, retrying with %s", n);
avahi_set_proc_title(argv0, "%s: collision [%s]", argv0, 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)));
sd_notifyf(0, "STATUS=Server error: %s", avahi_strerror(avahi_server_errno(s)));
avahi_simple_poll_quit(simple_poll_api);
break;
case AVAHI_SERVER_REGISTERING:
sd_notifyf(0, "STATUS=Registering host name %s", avahi_server_get_host_name_fqdn(s));
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;
#ifdef DAEMON_SET_VERBOSITY_AVAILABLE
daemon_set_verbosity(LOG_DEBUG);
#endif
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 char *get_machine_id(void) {
int fd;
char buf[32];
fd = open("/etc/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd == -1 && errno == ENOENT)
fd = open("/var/lib/dbus/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd == -1)
return NULL;
/* File is on a filesystem so we never get EINTR or partial reads */
if (read(fd, buf, sizeof buf) != sizeof buf) {
close(fd);
return NULL;
}
close(fd);
/* Contents can be lower, upper and even mixed case so normalize */
avahi_strdown(buf);
return avahi_strndup(buf, sizeof buf);
}
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);
else if (strcasecmp(p->key, "host-name-from-machine-id") == 0) {
if (*(p->value) == 'y' || *(p->value) == 'Y') {
char *machine_id = get_machine_id();
if (machine_id != NULL) {
avahi_free(c->server_config.host_name);
c->server_config.host_name = machine_id;
}
}
}
#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 if (strcasecmp(p->key, "reflect-filters") == 0) {
char **e, **t;
avahi_string_list_free(c->server_config.reflect_filters);
c->server_config.reflect_filters = NULL;
e = avahi_split_csv(p->value);
for (t = e; *t; t++)
c->server_config.reflect_filters = avahi_string_list_add(c->server_config.reflect_filters, *t);
avahi_strfreev(e);
}
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