]> git.meshlink.io Git - catta/blob - avahi-dnsconfd/main.c
* update todo list
[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/util.h>
43 #include <avahi-core/llist.h>
44
45 #include <libdaemon/dfork.h>
46 #include <libdaemon/dsignal.h>
47 #include <libdaemon/dlog.h>
48 #include <libdaemon/dpid.h>
49 #include <libdaemon/dexec.h>
50
51 #define BROWSE_DNS_SERVERS "BROWSE-DNS-SERVERS\n"
52
53 static enum {
54     ACKWAIT,
55     BROWSING
56 } state = ACKWAIT;
57
58 static gboolean quit = FALSE;
59
60 static enum {
61     DAEMON_RUN,
62     DAEMON_KILL,
63     DAEMON_RELOAD,
64     DAEMON_VERSION,
65     DAEMON_HELP
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 = (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 --reload      Request a running daemon to reload static services\n"
346             "    -V --version     Show version\n",
347             argv0);
348 }
349
350 static gint parse_command_line(int argc, char *argv[]) {
351     gint c;
352     
353     static const struct option const long_options[] = {
354         { "help",      no_argument,       NULL, 'h' },
355         { "daemonize", no_argument,       NULL, 'D' },
356         { "kill",      no_argument,       NULL, 'k' },
357         { "version",   no_argument,       NULL, 'V' },
358         { "reload",    no_argument,       NULL, 'r' },
359     };
360
361     opterr = 0;
362     while ((c = getopt_long(argc, argv, "hDkVr", long_options, NULL)) >= 0) {
363
364         switch(c) {
365             case 'h':
366                 command = DAEMON_HELP;
367                 break;
368             case 'D':
369                 daemonize = TRUE;
370                 break;
371             case 'k':
372                 command = DAEMON_KILL;
373                 break;
374             case 'V':
375                 command = DAEMON_VERSION;
376                 break;
377             case 'r':
378                 command = DAEMON_RELOAD;
379                 break;
380             default:
381                 fprintf(stderr, "Invalid command line argument: %c\n", c);
382                 return -1;
383         }
384     }
385
386     if (optind < argc) {
387         fprintf(stderr, "Too many arguments\n");
388         return -1;
389     }
390         
391     return 0;
392 }
393
394 static int run_daemon(void) {
395     gint fd = -1, ret = -1;
396     gchar buf[1024];
397     size_t buflen = 0;
398
399     AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
400     
401     daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
402     
403     if ((fd = do_connect()) < 0)
404         goto finish;
405
406     if (daemonize)
407         daemon_retval_send(0);
408     
409     while (!quit) {
410         fd_set rfds, wfds;
411
412         FD_ZERO(&rfds);
413         FD_ZERO(&wfds);
414
415         FD_SET(fd, &rfds);
416         FD_SET(daemon_signal_fd(), &rfds);
417
418         for (;;) {
419             if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
420                 if (errno == EINTR)
421                     continue;
422
423                 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
424                 goto finish;
425             }
426
427             break;
428         }
429
430         if (FD_ISSET(daemon_signal_fd(), &rfds)) {
431
432             int sig;
433
434             if ((sig = daemon_signal_next()) <= 0) {
435                 daemon_log(LOG_ERR, "daemon_signal_next() failed");
436                 goto finish;
437             }
438
439             switch(sig) {
440                 case SIGINT:
441                 case SIGTERM:
442                     daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
443                     ret = 0;
444                     goto finish;
445
446                 case SIGCHLD:
447                     waitpid(-1, NULL, WNOHANG);
448                     break;
449                     
450                 case SIGHUP:
451                     daemon_log(LOG_INFO, "Rrefreshing DNS Server list");
452                     
453                     close(fd);
454                     free_dns_server_info_list();
455                     
456                     if ((fd = do_connect()) < 0)
457                         goto finish;
458                     
459                     break;
460             }
461             
462         } else if (FD_ISSET(fd, &rfds)) {
463             ssize_t r;
464             gchar *n;
465
466             if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
467                 daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
468                 goto finish;
469             }
470
471             buflen += r;
472             g_assert(buflen <= sizeof(buf)-1);
473
474             while ((n = memchr(buf, '\n', buflen))) {
475                 *(n++) = 0;
476
477                 if (new_line(buf) < 0)
478                     goto finish;
479
480                 buflen -= (n - buf);
481                 memmove(buf, n, buflen);
482             }
483
484             if (buflen >= sizeof(buf)-1) {
485                 /* The incoming line is horribly long */
486                 buf[sizeof(buf)-1] = 0;
487                 
488                 if (new_line(buf) < 0)
489                     goto finish;
490                 
491                 buflen = 0;
492             }
493         }
494     }
495
496     ret = 0;
497     
498 finish:
499
500     free_dns_server_info_list();
501
502     if (fd >= 0)
503         close(fd);
504     
505     daemon_signal_done();
506
507     return ret;
508 }
509
510 static const char* pid_file_proc(void) {
511     return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
512 }
513
514 gint main(gint argc, gchar *argv[]) {
515     gchar *argv0;
516     gint r = 1;
517     gboolean wrote_pid_file = FALSE;
518
519     if ((argv0 = strrchr(argv[0], '/')))
520         argv0++;
521     else
522         argv0 = argv[0];
523
524     daemon_pid_file_ident = daemon_log_ident = argv0;
525     daemon_pid_file_proc = pid_file_proc;
526     
527     if (parse_command_line(argc, argv) < 0)
528         goto finish;
529
530     if (command == DAEMON_RUN) {
531         pid_t pid;
532
533         if (getuid() != 0) {
534             daemon_log(LOG_ERR, "This program is intended to be run as root.");
535             goto finish;
536         }
537
538         if ((pid = daemon_pid_file_is_running()) >= 0) {
539             daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
540             goto finish;
541         }
542
543         if (daemonize) {
544             daemon_retval_init();
545             
546             if ((pid = daemon_fork()) < 0)
547                 goto finish;
548             else if (pid != 0) {
549                 int ret;
550                 /** Parent **/
551
552                 if ((ret = daemon_retval_wait(20)) < 0) {
553                     daemon_log(LOG_ERR, "Could not recieve return value from daemon process.");
554                     goto finish;
555                 }
556
557                 r = ret;
558                 goto finish;
559             }
560
561             /* Child */
562         }
563
564         chdir("/");
565
566         if (daemon_pid_file_create() < 0) {
567             daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
568
569             if (daemonize)
570                 daemon_retval_send(1);
571             goto finish;
572         } else
573             wrote_pid_file = TRUE;
574
575         if (run_daemon() < 0)
576             goto finish;
577
578     } else if (command == DAEMON_HELP) {
579         help(stdout, argv0);
580         
581     } else if (command == DAEMON_VERSION) {
582         printf("%s "PACKAGE_VERSION"\n", argv0);
583         
584     } else if (command == DAEMON_KILL) {
585         if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
586             daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
587             goto finish;
588         }
589         
590     } else if (command == DAEMON_RELOAD) {
591         if (daemon_pid_file_kill(SIGHUP) < 0) {
592             daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
593             goto finish;
594         }
595     }
596
597     r = 0;
598     
599 finish:
600
601     if (daemonize)
602         daemon_retval_done();
603     
604     if (wrote_pid_file)
605         daemon_pid_file_remove();
606
607     return r;
608 }