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