]> git.meshlink.io Git - catta/blob - avahi-dnsconfd/main.c
99bae64d093c4d864ab5d8832bc0ed0481eb54ac
[catta] / avahi-dnsconfd / main.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/un.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <sys/select.h>
30 #include <sys/socket.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <sys/ioctl.h>
35 #include <net/if.h>
36 #include <signal.h>
37 #include <sys/wait.h>
38 #include <getopt.h>
39
40 #include <glib.h>
41
42 #include <avahi-common/llist.h>
43
44 #include <libdaemon/dfork.h>
45 #include <libdaemon/dsignal.h>
46 #include <libdaemon/dlog.h>
47 #include <libdaemon/dpid.h>
48 #include <libdaemon/dexec.h>
49
50 #define BROWSE_DNS_SERVERS "BROWSE-DNS-SERVERS\n"
51
52 static enum {
53     ACKWAIT,
54     BROWSING
55 } state = ACKWAIT;
56
57 static gboolean quit = FALSE;
58
59 static enum {
60     DAEMON_RUN,
61     DAEMON_KILL,
62     DAEMON_REFRESH,
63     DAEMON_VERSION,
64     DAEMON_HELP,
65     DAEMON_CHECK
66 } command = DAEMON_RUN;
67
68 static gboolean daemonize = FALSE;
69
70 typedef struct DNSServerInfo DNSServerInfo;
71
72 struct DNSServerInfo {
73     gint interface;
74     guchar protocol;
75     gchar *address;
76     AVAHI_LLIST_FIELDS(DNSServerInfo, servers);
77 };
78
79 static AVAHI_LLIST_HEAD(DNSServerInfo, servers);
80
81 static void server_info_free(DNSServerInfo *i) {
82     g_assert(i);
83
84     g_free(i->address);
85     
86     AVAHI_LLIST_REMOVE(DNSServerInfo, servers, servers, i);
87     g_free(i);
88 }
89
90 static DNSServerInfo* get_server_info(gint interface, guchar protocol, const gchar *address) {
91     DNSServerInfo *i;
92     g_assert(address);
93
94     for (i = servers; i; i = i->servers_next)
95         if (i->interface == interface &&
96             i->protocol == protocol &&
97             strcmp(i->address, address) == 0)
98             return i;
99
100     return NULL;
101 }
102
103 static DNSServerInfo* new_server_info(gint interface, guchar protocol, const gchar *address) {
104     DNSServerInfo *i;
105     
106     g_assert(address);
107
108     i = g_new(DNSServerInfo, 1);
109     i->interface = interface;
110     i->protocol = protocol;
111     i->address = g_strdup(address);
112
113     AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);
114     
115     return i;
116 }
117
118 static int set_cloexec(int fd) {
119     int n;
120
121     assert(fd >= 0);
122     
123     if ((n = fcntl(fd, F_GETFD)) < 0)
124         return -1;
125
126     if (n & FD_CLOEXEC)
127         return 0;
128
129     return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
130 }
131
132 static int open_socket(void) {
133     int fd = -1;
134     struct sockaddr_un sa;
135
136     if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
137         daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
138         goto fail;
139     }
140
141     if (set_cloexec(fd) < 0) {
142         daemon_log(LOG_ERR, "fcntl(): %s", strerror(errno));
143         goto fail;
144     }
145     
146     memset(&sa, 0, sizeof(sa));
147     sa.sun_family = AF_UNIX;
148     strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
149     sa.sun_path[sizeof(sa.sun_path)-1] = 0;
150
151     if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
152         daemon_log(LOG_ERR, "connect(): %s", strerror(errno));
153         goto fail;
154     }
155
156     return fd;
157     
158 fail:
159     if (fd >= 0)
160         close(fd);
161
162     return -1;
163 }
164
165 static ssize_t loop_write(int fd, const void*data, size_t size) {
166
167     ssize_t ret = 0;
168     g_assert(fd >= 0 && data && size);
169
170     while (size > 0) {
171         ssize_t r;
172
173         if ((r = write(fd, data, size)) < 0)
174             return r;
175
176         if (r == 0)
177             break;
178
179         ret += r;
180         data = (const guint8*) data + r;
181         size -= r;
182     }
183
184     return ret;
185 }
186
187 static gchar *concat_dns_servers(gint interface) {
188     DNSServerInfo *i;
189     gchar *r = NULL;
190     
191     for (i = servers; i; i = i->servers_next)
192         if (i->interface == interface || interface <= 0) {
193             gchar *t;
194
195             if (!r)
196                 t = g_strdup(i->address);
197             else
198                 t = g_strdup_printf("%s %s", r, i->address);
199
200             g_free(r);
201             r = t;
202         }
203
204     return r;
205 }
206
207 static gchar *getifname(gint interface, gchar *name, guint len) {
208     int fd = -1;
209     gchar *ret = NULL;
210     struct ifreq ifr;
211
212     g_assert(interface >= 0);
213     
214     if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
215         daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
216         goto finish;
217     }
218
219     memset(&ifr, 0, sizeof(ifr));
220     ifr.ifr_ifindex = interface;
221     
222     if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
223         daemon_log(LOG_ERR, "SIOCGIFNAME: %s\n", strerror(errno));
224         goto finish;
225     }
226
227     strncpy(name, ifr.ifr_name, len-1);
228     name[len-1] = 0;
229     ret = name;
230     
231 finish:
232     if (fd >= 0)
233         close(fd);
234     
235     return ret;
236  }
237
238 static void run_script(gboolean new, gint interface, guchar protocol, const gchar *address) {
239     gchar *p;
240     g_assert(interface > 0);
241     gint ret;
242     gchar ia[16], pa[16];
243     gchar name[IFNAMSIZ+1];
244
245     if (!getifname(interface, name, sizeof(name))) 
246         return;
247     
248     p = concat_dns_servers(interface);
249     g_setenv("AVAHI_INTERFACE_DNS_SERVERS", p ? p : "", TRUE);
250     g_free(p); 
251
252     p = concat_dns_servers(-1);
253     g_setenv("AVAHI_DNS_SERVERS", p ? p : "", TRUE);
254     g_free(p); 
255
256     g_setenv("AVAHI_INTERFACE", name, TRUE);
257     
258     snprintf(ia, sizeof(ia), "%i", interface);
259     snprintf(pa, sizeof(pa), "%u", protocol);
260
261     if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, NULL) < 0)
262         daemon_log(LOG_WARNING, "Failed to run script");
263     else if (ret != 0)
264         daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
265 }
266
267 static gint new_line(const gchar *l) {
268     g_assert(l);
269
270     if (state == ACKWAIT) {
271         if (*l != '+') {
272             daemon_log(LOG_ERR, "Avahi command failed: %s", l);
273             return -1;
274         }
275
276         daemon_log(LOG_INFO, "Successfully connected to Avahi daemon.");
277         state = BROWSING;
278     } else {
279         gint interface;
280         guint protocol;
281         guint port;
282         gchar a[64];
283         
284         g_assert(state == BROWSING); 
285
286         if (*l != '<' && *l != '>') {
287             daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
288             return -1;
289         }
290
291         if (sscanf(l+1, "%i %u %64s %u", &interface, &protocol, a, &port) != 4) {
292             daemon_log(LOG_ERR, "Failed to parse browsing line: %s", l);
293             return -1;
294         }
295
296         if (*l == '>') {
297             if (port != 53)
298                 daemon_log(LOG_WARNING, "DNS server with port address != 53 found, ignoring");
299             else {
300                 daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%u)", a, interface, protocol);
301                 new_server_info(interface, (guchar) protocol, a);
302                 run_script(TRUE, interface, (guchar) protocol, a);
303             }
304         } else {
305             DNSServerInfo *i;
306
307             if (port == 53) 
308                 if ((i = get_server_info(interface, (guchar) protocol, a))) {
309                     daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%u)", a, interface, protocol);
310                     server_info_free(i);
311                     run_script(FALSE, interface, (guchar) protocol, a);
312                 }
313         }
314
315     }
316     
317     return 0;
318 }
319
320 static gint do_connect(void) {
321     gint fd = -1;
322     
323     if ((fd = open_socket()) < 0)
324         goto fail;
325
326     if (loop_write(fd, BROWSE_DNS_SERVERS, sizeof(BROWSE_DNS_SERVERS)-1) < 0) {
327         daemon_log(LOG_ERR, "write(): %s", strerror(errno));
328         goto fail;
329     }
330
331     state = ACKWAIT;
332     return fd;
333
334 fail:
335     if (fd >= 0)
336         close(fd);
337     
338     return -1;
339 }
340
341 static void free_dns_server_info_list(void) {
342     while (servers) {
343         gint interface = servers->interface;
344         guchar protocol = servers->protocol;
345         gchar *address = g_strdup(servers->address);
346         server_info_free(servers);
347         
348         run_script(FALSE, interface, protocol, address);
349         g_free(address);
350     }
351 }
352
353 static void help(FILE *f, const gchar *argv0) {
354     fprintf(f,
355             "%s [options]\n"
356             "    -h --help        Show this help\n"
357             "    -D --daemonize   Daemonize after startup\n"
358             "    -k --kill        Kill a running daemon\n"
359             "    -r --refresh     Request a running daemon to refresh DNS server data\n"
360             "    -c --check       Return 0 if a daemon is already running\n"
361             "    -V --version     Show version\n",
362             argv0);
363 }
364
365 static gint parse_command_line(int argc, char *argv[]) {
366     gint c;
367     
368     static const struct option const long_options[] = {
369         { "help",      no_argument,       NULL, 'h' },
370         { "daemonize", no_argument,       NULL, 'D' },
371         { "kill",      no_argument,       NULL, 'k' },
372         { "version",   no_argument,       NULL, 'V' },
373         { "refresh",   no_argument,       NULL, 'r' },
374         { "check",     no_argument,       NULL, 'c' },
375     };
376
377     opterr = 0;
378     while ((c = getopt_long(argc, argv, "hDkVrc", long_options, NULL)) >= 0) {
379
380         switch(c) {
381             case 'h':
382                 command = DAEMON_HELP;
383                 break;
384             case 'D':
385                 daemonize = TRUE;
386                 break;
387             case 'k':
388                 command = DAEMON_KILL;
389                 break;
390             case 'V':
391                 command = DAEMON_VERSION;
392                 break;
393             case 'r':
394                 command = DAEMON_REFRESH;
395                 break;
396             case 'c':
397                 command = DAEMON_CHECK;
398                 break;
399             default:
400                 fprintf(stderr, "Invalid command line argument: %c\n", c);
401                 return -1;
402         }
403     }
404
405     if (optind < argc) {
406         fprintf(stderr, "Too many arguments\n");
407         return -1;
408     }
409         
410     return 0;
411 }
412
413 static int run_daemon(void) {
414     gint fd = -1, ret = -1;
415     gchar buf[1024];
416     size_t buflen = 0;
417
418     AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
419     
420     daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
421     
422     if ((fd = do_connect()) < 0)
423         goto finish;
424
425     if (daemonize)
426         daemon_retval_send(0);
427
428     ret = 0;
429
430     while (!quit) {
431         fd_set rfds, wfds;
432
433         FD_ZERO(&rfds);
434         FD_ZERO(&wfds);
435
436         FD_SET(fd, &rfds);
437         FD_SET(daemon_signal_fd(), &rfds);
438
439         for (;;) {
440             if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
441                 if (errno == EINTR)
442                     continue;
443
444                 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
445                 goto finish;
446             }
447
448             break;
449         }
450
451         if (FD_ISSET(daemon_signal_fd(), &rfds)) {
452
453             int sig;
454
455             if ((sig = daemon_signal_next()) <= 0) {
456                 daemon_log(LOG_ERR, "daemon_signal_next() failed");
457                 goto finish;
458             }
459
460             switch(sig) {
461                 case SIGINT:
462                 case SIGTERM:
463                     daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
464                     ret = 0;
465                     goto finish;
466
467                 case SIGCHLD:
468                     waitpid(-1, NULL, WNOHANG);
469                     break;
470                     
471                 case SIGHUP:
472                     daemon_log(LOG_INFO, "Refreshing DNS Server list");
473                     
474                     close(fd);
475                     free_dns_server_info_list();
476                     
477                     if ((fd = do_connect()) < 0)
478                         goto finish;
479                     
480                     break;
481             }
482             
483         } else if (FD_ISSET(fd, &rfds)) {
484             ssize_t r;
485             gchar *n;
486
487             if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
488                 daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
489                 goto finish;
490             }
491
492             buflen += r;
493             g_assert(buflen <= sizeof(buf)-1);
494
495             while ((n = memchr(buf, '\n', buflen))) {
496                 *(n++) = 0;
497
498                 if (new_line(buf) < 0)
499                     goto finish;
500
501                 buflen -= (n - buf);
502                 memmove(buf, n, buflen);
503             }
504
505             if (buflen >= sizeof(buf)-1) {
506                 /* The incoming line is horribly long */
507                 buf[sizeof(buf)-1] = 0;
508                 
509                 if (new_line(buf) < 0)
510                     goto finish;
511                 
512                 buflen = 0;
513             }
514         }
515     }
516     
517 finish:
518
519     free_dns_server_info_list();
520
521     if (fd >= 0)
522         close(fd);
523     
524     daemon_signal_done();
525
526     if (ret != 0 && daemonize)
527         daemon_retval_send(1);
528     
529     return ret;
530 }
531
532 static const char* pid_file_proc(void) {
533     return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
534 }
535
536 gint main(gint argc, gchar *argv[]) {
537     gchar *argv0;
538     gint r = 1;
539     gboolean wrote_pid_file = FALSE;
540
541     if ((argv0 = strrchr(argv[0], '/')))
542         argv0++;
543     else
544         argv0 = argv[0];
545
546     daemon_pid_file_ident = daemon_log_ident = argv0;
547     daemon_pid_file_proc = pid_file_proc;
548     
549     if (parse_command_line(argc, argv) < 0)
550         goto finish;
551
552     if (command == DAEMON_RUN) {
553         pid_t pid;
554
555         if (getuid() != 0) {
556             daemon_log(LOG_ERR, "This program is intended to be run as root.");
557             goto finish;
558         }
559
560         if ((pid = daemon_pid_file_is_running()) >= 0) {
561             daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
562             goto finish;
563         }
564
565         if (daemonize) {
566             daemon_retval_init();
567             
568             if ((pid = daemon_fork()) < 0)
569                 goto finish;
570             else if (pid != 0) {
571                 int ret;
572                 /** Parent **/
573
574                 if ((ret = daemon_retval_wait(20)) < 0) {
575                     daemon_log(LOG_ERR, "Could not recieve return value from daemon process.");
576                     goto finish;
577                 }
578
579                 r = ret;
580                 goto finish;
581             }
582
583             /* Child */
584         }
585
586         chdir("/");
587
588         if (daemon_pid_file_create() < 0) {
589             daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
590
591             if (daemonize)
592                 daemon_retval_send(1);
593             goto finish;
594         } else
595             wrote_pid_file = TRUE;
596
597         if (run_daemon() < 0)
598             goto finish;
599
600         r = 0;
601     } else if (command == DAEMON_HELP) {
602         help(stdout, argv0);
603         
604         r = 0;
605     } else if (command == DAEMON_VERSION) {
606         printf("%s "PACKAGE_VERSION"\n", argv0);
607         
608         r = 0;
609     } else if (command == DAEMON_KILL) {
610         if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
611             daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
612             goto finish;
613         }
614         
615         r = 0;
616     } else if (command == DAEMON_REFRESH) {
617         if (daemon_pid_file_kill(SIGHUP) < 0) {
618             daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
619             goto finish;
620         }
621
622         r = 0;
623     } else if (command == DAEMON_CHECK)
624         r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
625
626
627     
628 finish:
629
630     if (daemonize)
631         daemon_retval_done();
632     
633     if (wrote_pid_file)
634         daemon_pid_file_remove();
635
636     return r;
637 }