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