]> git.meshlink.io Git - catta/blob - avahi-dnsconfd/main.c
* strip glib from avahi-core
[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     gint ret;
241     gchar ia[16], pa[16];
242     gchar name[IFNAMSIZ+1];
243
244     g_assert(interface > 0);
245
246     if (!getifname(interface, name, sizeof(name))) 
247         return;
248     
249     p = concat_dns_servers(interface);
250     g_setenv("AVAHI_INTERFACE_DNS_SERVERS", p ? p : "", TRUE);
251     g_free(p); 
252
253     p = concat_dns_servers(-1);
254     g_setenv("AVAHI_DNS_SERVERS", p ? p : "", TRUE);
255     g_free(p); 
256
257     g_setenv("AVAHI_INTERFACE", name, TRUE);
258     
259     snprintf(ia, sizeof(ia), "%i", interface);
260     snprintf(pa, sizeof(pa), "%u", protocol);
261
262     if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, NULL) < 0)
263         daemon_log(LOG_WARNING, "Failed to run script");
264     else if (ret != 0)
265         daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
266 }
267
268 static gint new_line(const gchar *l) {
269     g_assert(l);
270
271     if (state == ACKWAIT) {
272         if (*l != '+') {
273             daemon_log(LOG_ERR, "Avahi command failed: %s", l);
274             return -1;
275         }
276
277         daemon_log(LOG_INFO, "Successfully connected to Avahi daemon.");
278         state = BROWSING;
279     } else {
280         gint interface;
281         guint protocol;
282         guint port;
283         gchar a[64];
284         
285         g_assert(state == BROWSING); 
286
287         if (*l != '<' && *l != '>') {
288             daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
289             return -1;
290         }
291
292         if (sscanf(l+1, "%i %u %64s %u", &interface, &protocol, a, &port) != 4) {
293             daemon_log(LOG_ERR, "Failed to parse browsing line: %s", l);
294             return -1;
295         }
296
297         if (*l == '>') {
298             if (port != 53)
299                 daemon_log(LOG_WARNING, "DNS server with port address != 53 found, ignoring");
300             else {
301                 daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%u)", a, interface, protocol);
302                 new_server_info(interface, (guchar) protocol, a);
303                 run_script(TRUE, interface, (guchar) protocol, a);
304             }
305         } else {
306             DNSServerInfo *i;
307
308             if (port == 53) 
309                 if ((i = get_server_info(interface, (guchar) protocol, a))) {
310                     daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%u)", a, interface, protocol);
311                     server_info_free(i);
312                     run_script(FALSE, interface, (guchar) protocol, a);
313                 }
314         }
315
316     }
317     
318     return 0;
319 }
320
321 static gint do_connect(void) {
322     gint fd = -1;
323     
324     if ((fd = open_socket()) < 0)
325         goto fail;
326
327     if (loop_write(fd, BROWSE_DNS_SERVERS, sizeof(BROWSE_DNS_SERVERS)-1) < 0) {
328         daemon_log(LOG_ERR, "write(): %s", strerror(errno));
329         goto fail;
330     }
331
332     state = ACKWAIT;
333     return fd;
334
335 fail:
336     if (fd >= 0)
337         close(fd);
338     
339     return -1;
340 }
341
342 static void free_dns_server_info_list(void) {
343     while (servers) {
344         gint interface = servers->interface;
345         guchar protocol = servers->protocol;
346         gchar *address = g_strdup(servers->address);
347         server_info_free(servers);
348         
349         run_script(FALSE, interface, protocol, address);
350         g_free(address);
351     }
352 }
353
354 static void help(FILE *f, const gchar *argv0) {
355     fprintf(f,
356             "%s [options]\n"
357             "    -h --help        Show this help\n"
358             "    -D --daemonize   Daemonize after startup\n"
359             "    -k --kill        Kill a running daemon\n"
360             "    -r --refresh     Request a running daemon to refresh DNS server data\n"
361             "    -c --check       Return 0 if a daemon is already running\n"
362             "    -V --version     Show version\n",
363             argv0);
364 }
365
366 static gint parse_command_line(int argc, char *argv[]) {
367     gint c;
368     
369     static const struct option const long_options[] = {
370         { "help",      no_argument,       NULL, 'h' },
371         { "daemonize", no_argument,       NULL, 'D' },
372         { "kill",      no_argument,       NULL, 'k' },
373         { "version",   no_argument,       NULL, 'V' },
374         { "refresh",   no_argument,       NULL, 'r' },
375         { "check",     no_argument,       NULL, 'c' },
376     };
377
378     opterr = 0;
379     while ((c = getopt_long(argc, argv, "hDkVrc", long_options, NULL)) >= 0) {
380
381         switch(c) {
382             case 'h':
383                 command = DAEMON_HELP;
384                 break;
385             case 'D':
386                 daemonize = TRUE;
387                 break;
388             case 'k':
389                 command = DAEMON_KILL;
390                 break;
391             case 'V':
392                 command = DAEMON_VERSION;
393                 break;
394             case 'r':
395                 command = DAEMON_REFRESH;
396                 break;
397             case 'c':
398                 command = DAEMON_CHECK;
399                 break;
400             default:
401                 fprintf(stderr, "Invalid command line argument: %c\n", c);
402                 return -1;
403         }
404     }
405
406     if (optind < argc) {
407         fprintf(stderr, "Too many arguments\n");
408         return -1;
409     }
410         
411     return 0;
412 }
413
414 static int run_daemon(void) {
415     gint fd = -1, ret = -1;
416     gchar buf[1024];
417     size_t buflen = 0;
418
419     AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
420     
421     daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
422     
423     if ((fd = do_connect()) < 0)
424         goto finish;
425
426     if (daemonize)
427         daemon_retval_send(0);
428
429     ret = 0;
430
431     while (!quit) {
432         fd_set rfds, wfds;
433
434         FD_ZERO(&rfds);
435         FD_ZERO(&wfds);
436
437         FD_SET(fd, &rfds);
438         FD_SET(daemon_signal_fd(), &rfds);
439
440         for (;;) {
441             if (select(fd+1, &rfds, NULL, NULL, NULL) < 0) {
442                 if (errno == EINTR)
443                     continue;
444
445                 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
446                 goto finish;
447             }
448
449             break;
450         }
451
452         if (FD_ISSET(daemon_signal_fd(), &rfds)) {
453
454             int sig;
455
456             if ((sig = daemon_signal_next()) <= 0) {
457                 daemon_log(LOG_ERR, "daemon_signal_next() failed");
458                 goto finish;
459             }
460
461             switch(sig) {
462                 case SIGINT:
463                 case SIGTERM:
464                     daemon_log(LOG_INFO, "Got %s, quitting.", sig == SIGINT ? "SIGINT" : "SIGTERM");
465                     ret = 0;
466                     goto finish;
467
468                 case SIGCHLD:
469                     waitpid(-1, NULL, WNOHANG);
470                     break;
471                     
472                 case SIGHUP:
473                     daemon_log(LOG_INFO, "Refreshing DNS Server list");
474                     
475                     close(fd);
476                     free_dns_server_info_list();
477                     
478                     if ((fd = do_connect()) < 0)
479                         goto finish;
480                     
481                     break;
482             }
483             
484         } else if (FD_ISSET(fd, &rfds)) {
485             ssize_t r;
486             gchar *n;
487
488             if ((r = read(fd, buf, sizeof(buf) - buflen - 1)) <= 0) {
489                 daemon_log(LOG_ERR, "read(): %s", r < 0 ? strerror(errno) : "EOF");
490                 goto finish;
491             }
492
493             buflen += r;
494             g_assert(buflen <= sizeof(buf)-1);
495
496             while ((n = memchr(buf, '\n', buflen))) {
497                 *(n++) = 0;
498
499                 if (new_line(buf) < 0)
500                     goto finish;
501
502                 buflen -= (n - buf);
503                 memmove(buf, n, buflen);
504             }
505
506             if (buflen >= sizeof(buf)-1) {
507                 /* The incoming line is horribly long */
508                 buf[sizeof(buf)-1] = 0;
509                 
510                 if (new_line(buf) < 0)
511                     goto finish;
512                 
513                 buflen = 0;
514             }
515         }
516     }
517     
518 finish:
519
520     free_dns_server_info_list();
521
522     if (fd >= 0)
523         close(fd);
524     
525     daemon_signal_done();
526
527     if (ret != 0 && daemonize)
528         daemon_retval_send(1);
529     
530     return ret;
531 }
532
533 static const char* pid_file_proc(void) {
534     return AVAHI_RUNTIME_DIR"/avahi-dnsconfd.pid";
535 }
536
537 gint main(gint argc, gchar *argv[]) {
538     gchar *argv0;
539     gint r = 1;
540     gboolean wrote_pid_file = FALSE;
541
542     if ((argv0 = strrchr(argv[0], '/')))
543         argv0++;
544     else
545         argv0 = argv[0];
546
547     daemon_pid_file_ident = daemon_log_ident = argv0;
548     daemon_pid_file_proc = pid_file_proc;
549     
550     if (parse_command_line(argc, argv) < 0)
551         goto finish;
552
553     if (command == DAEMON_RUN) {
554         pid_t pid;
555
556         if (getuid() != 0) {
557             daemon_log(LOG_ERR, "This program is intended to be run as root.");
558             goto finish;
559         }
560
561         if ((pid = daemon_pid_file_is_running()) >= 0) {
562             daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
563             goto finish;
564         }
565
566         if (daemonize) {
567             daemon_retval_init();
568             
569             if ((pid = daemon_fork()) < 0)
570                 goto finish;
571             else if (pid != 0) {
572                 int ret;
573                 /** Parent **/
574
575                 if ((ret = daemon_retval_wait(20)) < 0) {
576                     daemon_log(LOG_ERR, "Could not recieve return value from daemon process.");
577                     goto finish;
578                 }
579
580                 r = ret;
581                 goto finish;
582             }
583
584             /* Child */
585         }
586
587         chdir("/");
588
589         if (daemon_pid_file_create() < 0) {
590             daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
591
592             if (daemonize)
593                 daemon_retval_send(1);
594             goto finish;
595         } else
596             wrote_pid_file = TRUE;
597
598         if (run_daemon() < 0)
599             goto finish;
600
601         r = 0;
602     } else if (command == DAEMON_HELP) {
603         help(stdout, argv0);
604         
605         r = 0;
606     } else if (command == DAEMON_VERSION) {
607         printf("%s "PACKAGE_VERSION"\n", argv0);
608         
609         r = 0;
610     } else if (command == DAEMON_KILL) {
611         if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
612             daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
613             goto finish;
614         }
615         
616         r = 0;
617     } else if (command == DAEMON_REFRESH) {
618         if (daemon_pid_file_kill(SIGHUP) < 0) {
619             daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
620             goto finish;
621         }
622
623         r = 0;
624     } else if (command == DAEMON_CHECK)
625         r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
626
627
628     
629 finish:
630
631     if (daemonize)
632         daemon_retval_done();
633     
634     if (wrote_pid_file)
635         daemon_pid_file_remove();
636
637     return r;
638 }