]> git.meshlink.io Git - catta/blob - avahi-daemon/simple-protocol.c
* add chroot() support on Linux
[catta] / avahi-daemon / simple-protocol.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 <assert.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <sys/un.h>
33 #include <errno.h>
34 #include <fcntl.h>
35
36 #include <avahi-common/llist.h>
37 #include <avahi-common/malloc.h>
38 #include <avahi-common/error.h>
39
40 #include <avahi-core/log.h>
41 #include <avahi-core/lookup.h>
42 #include <avahi-core/dns-srv-rr.h>
43
44 #include "simple-protocol.h"
45 #include "main.h"
46
47 #ifdef ENABLE_CHROOT
48 #include "chroot.h"
49 #endif
50
51 #define BUFFER_SIZE (20*1024)
52
53 #define CLIENTS_MAX 50
54
55 typedef struct Client Client;
56 typedef struct Server Server;
57
58 typedef enum {
59     CLIENT_IDLE,
60     CLIENT_RESOLVE_HOSTNAME,
61     CLIENT_RESOLVE_ADDRESS,
62     CLIENT_BROWSE_DNS_SERVERS,
63     CLIENT_DEAD
64 } ClientState;
65
66 struct Client {
67     Server *server;
68
69     ClientState state;
70     
71     int fd;
72     AvahiWatch *watch;
73
74     char inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
75     size_t inbuf_length, outbuf_length;
76
77     AvahiSHostNameResolver *host_name_resolver;
78     AvahiSAddressResolver *address_resolver;
79     AvahiSDNSServerBrowser *dns_server_browser;
80
81     AvahiProtocol afquery;
82     
83     AVAHI_LLIST_FIELDS(Client, clients);
84 };
85
86 struct Server {
87     const AvahiPoll *poll_api;
88     int fd;
89     AvahiWatch *watch;
90     AVAHI_LLIST_HEAD(Client, clients);
91
92     unsigned n_clients;
93     int bind_successful;
94 };
95
96 static Server *server = NULL;
97
98 static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata);
99
100 static void client_free(Client *c) {
101     assert(c);
102
103     assert(c->server->n_clients >= 1);
104     c->server->n_clients--;
105
106     if (c->host_name_resolver)
107         avahi_s_host_name_resolver_free(c->host_name_resolver);
108
109     if (c->address_resolver)
110         avahi_s_address_resolver_free(c->address_resolver);
111
112     if (c->dns_server_browser)
113         avahi_s_dns_server_browser_free(c->dns_server_browser);
114
115     c->server->poll_api->watch_free(c->watch);
116     close(c->fd);
117     
118     AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
119     avahi_free(c);
120 }
121
122 static void client_new(Server *s, int fd) {
123     Client *c;
124
125     assert(fd >= 0);
126
127     c = avahi_new(Client, 1);
128     c->server = s;
129     c->fd = fd;
130     c->state = CLIENT_IDLE;
131
132     c->inbuf_length = c->outbuf_length = 0;
133
134     c->host_name_resolver = NULL;
135     c->address_resolver = NULL;
136     c->dns_server_browser = NULL;
137
138     c->watch = s->poll_api->watch_new(s->poll_api, fd, AVAHI_WATCH_IN, client_work, c);
139
140     AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
141     s->n_clients++;
142 }
143
144 static void client_output(Client *c, const uint8_t*data, size_t size) {
145     size_t k, m;
146     
147     assert(c);
148     assert(data);
149
150     if (!size)
151         return;
152
153     k = sizeof(c->outbuf) - c->outbuf_length;
154     m = size > k ? k : size;
155
156     memcpy(c->outbuf + c->outbuf_length, data, m);
157     c->outbuf_length += m;
158
159     server->poll_api->watch_update(c->watch, AVAHI_WATCH_OUT);
160 }
161
162 static void client_output_printf(Client *c, const char *format, ...) {
163     char *t;
164     va_list ap;
165     
166     va_start(ap, format);
167     t = avahi_strdup_vprintf(format, ap);
168     va_end(ap);
169
170     client_output(c, (uint8_t*) t, strlen(t));
171     avahi_free(t);
172 }
173
174 static void host_name_resolver_callback(
175     AVAHI_GCC_UNUSED AvahiSHostNameResolver *r,
176     AvahiIfIndex iface,
177     AvahiProtocol protocol,
178     AvahiResolverEvent event,
179     const char *hostname,
180     const AvahiAddress *a,
181     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, 
182     void* userdata) {
183
184     Client *c = userdata;
185     
186     assert(c);
187
188     if (event == AVAHI_RESOLVER_FAILURE)
189         client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
190     else if (event == AVAHI_RESOLVER_FOUND) {
191         char t[AVAHI_ADDRESS_STR_MAX];
192         avahi_address_snprint(t, sizeof(t), a);
193         client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
194     }
195
196     c->state = CLIENT_DEAD;
197 }
198
199 static void address_resolver_callback(
200     AVAHI_GCC_UNUSED AvahiSAddressResolver *r,
201     AvahiIfIndex iface,
202     AvahiProtocol protocol,
203     AvahiResolverEvent event,
204     AVAHI_GCC_UNUSED const AvahiAddress *a,
205     const char *hostname,
206     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
207     void* userdata) {
208     
209     Client *c = userdata;
210     
211     assert(c);
212
213     if (event == AVAHI_RESOLVER_FAILURE)
214         client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
215     else if (event == AVAHI_RESOLVER_FOUND)
216         client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
217
218     c->state = CLIENT_DEAD;
219 }
220
221 static void dns_server_browser_callback(
222     AVAHI_GCC_UNUSED AvahiSDNSServerBrowser *b,
223     AvahiIfIndex interface,
224     AvahiProtocol protocol,
225     AvahiBrowserEvent event,
226     AVAHI_GCC_UNUSED const char *host_name,
227     const AvahiAddress *a,
228     uint16_t port,
229     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
230     void* userdata) {
231     
232     Client *c = userdata;
233     char t[AVAHI_ADDRESS_STR_MAX];
234     
235     assert(c);
236
237     if (!a)
238         return;
239
240     switch (event) {
241         case AVAHI_BROWSER_FAILURE:
242             client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
243             c->state = CLIENT_DEAD;
244             break;
245
246         case AVAHI_BROWSER_ALL_FOR_NOW:
247         case AVAHI_BROWSER_CACHE_EXHAUSTED:
248             break;
249
250         case AVAHI_BROWSER_NEW:
251         case AVAHI_BROWSER_REMOVE:
252     
253             avahi_address_snprint(t, sizeof(t), a);
254             client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<',  interface, protocol, t, port);
255             break;
256     }
257 }
258
259 static void handle_line(Client *c, const char *s) {
260     char cmd[64], arg[64];
261     int n_args;
262
263     assert(c);
264     assert(s);
265
266     if (c->state != CLIENT_IDLE)
267         return;
268
269     if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
270         client_output_printf(c, "%+i Failed to parse command, try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION);
271         c->state = CLIENT_DEAD;
272         return;
273     }
274
275     if (strcmp(cmd, "HELP") == 0) {
276         client_output_printf(c,
277                              "+ Available commands are:\n"
278                              "+      RESOLVE-HOSTNAME <hostname>\n"
279                              "+      RESOLVE-HOSTNAME-IPV6 <hostname>\n"
280                              "+      RESOLVE-HOSTNAME-IPV4 <hostname>\n"
281                              "+      RESOLVE-ADDRESS <address>\n"
282                              "+      BROWSE-DNS-SERVERS\n"
283                              "+      BROWSE-DNS-SERVERS-IPV4\n"
284                              "+      BROWSE-DNS-SERVERS-IPV6\n");
285         c->state = CLIENT_DEAD; }
286     else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) {
287         client_output_printf(c, "+ FUCK: Go fuck yourself!\n");
288         c->state = CLIENT_DEAD;
289     } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
290         c->state = CLIENT_RESOLVE_HOSTNAME;
291         if (!(c->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, arg, c->afquery = AVAHI_PROTO_INET, AVAHI_LOOKUP_USE_MULTICAST, host_name_resolver_callback, c)))
292             goto fail;
293
294         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
295     } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 0 && n_args == 2) {
296         c->state = CLIENT_RESOLVE_HOSTNAME;
297         if (!(c->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, arg, c->afquery = AVAHI_PROTO_INET6, AVAHI_LOOKUP_USE_MULTICAST, host_name_resolver_callback, c)))
298             goto fail;
299
300         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
301     } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 0 && n_args == 2) {
302         c->state = CLIENT_RESOLVE_HOSTNAME;
303         if (!(c->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, arg, c->afquery = AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_USE_MULTICAST, host_name_resolver_callback, c)))
304             goto fail;
305
306         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
307     } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
308         AvahiAddress addr;
309         
310         if (!(avahi_address_parse(arg, AVAHI_PROTO_UNSPEC, &addr))) {
311             client_output_printf(c, "%+i Failed to parse address \"%s\".\n", AVAHI_ERR_INVALID_ADDRESS, arg);
312             c->state = CLIENT_DEAD;
313         } else {
314             c->state = CLIENT_RESOLVE_ADDRESS;
315             if (!(c->address_resolver = avahi_s_address_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, &addr, AVAHI_LOOKUP_USE_MULTICAST, address_resolver_callback, c)))
316                 goto fail;
317         }
318
319         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
320
321     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) {
322         c->state = CLIENT_BROWSE_DNS_SERVERS;
323         if (!(c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AVAHI_PROTO_INET, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c)))
324             goto fail;
325         client_output_printf(c, "+ Browsing ...\n");
326
327         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
328
329     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) {
330         c->state = CLIENT_BROWSE_DNS_SERVERS;
331         if (!(c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AVAHI_PROTO_INET6, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c)))
332             goto fail;
333         client_output_printf(c, "+ Browsing ...\n");
334
335         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
336
337     } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) {
338         c->state = CLIENT_BROWSE_DNS_SERVERS;
339         if (!(c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c)))
340             goto fail;
341         client_output_printf(c, "+ Browsing ...\n");
342
343         avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
344
345     } else {
346         client_output_printf(c, "%+i Invalid command \"%s\", try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION, cmd);
347         c->state = CLIENT_DEAD;
348
349         avahi_log_debug(__FILE__": Got invalid request '%s'.", cmd);
350     }
351
352     return;
353
354 fail:
355     client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
356     c->state = CLIENT_DEAD;
357 }
358
359 static void handle_input(Client *c) {
360     assert(c);
361
362     for (;;) {
363         char *e;
364         size_t k;
365
366         if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
367             break;
368
369         k = e - (char*) c->inbuf;
370         *e = 0;
371         
372         handle_line(c, c->inbuf);
373         c->inbuf_length -= k + 1;
374         memmove(c->inbuf, e+1, c->inbuf_length);
375     }
376 }
377
378 static void client_work(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AvahiWatchEvent events, void *userdata) {
379     Client *c = userdata;
380
381     assert(c);
382
383     if ((events & AVAHI_WATCH_IN) && c->inbuf_length < sizeof(c->inbuf)) {
384         ssize_t r;
385         
386         if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
387             if (r < 0)
388                 avahi_log_warn("read(): %s", strerror(errno));
389             client_free(c);
390             return;
391         }
392
393         c->inbuf_length += r;
394         assert(c->inbuf_length <= sizeof(c->inbuf));
395
396         handle_input(c);
397     }
398
399     if ((events & AVAHI_WATCH_OUT) && c->outbuf_length > 0) {
400         ssize_t r;
401
402         if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
403             avahi_log_warn("write(): %s", strerror(errno));
404             client_free(c);
405             return;
406         }
407
408         assert((size_t) r <= c->outbuf_length);
409         c->outbuf_length -= r;
410         
411         if (c->outbuf_length)
412             memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
413
414         if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
415             client_free(c);
416             return;
417         }
418     }
419
420     c->server->poll_api->watch_update(
421         watch, 
422         (c->outbuf_length > 0 ? AVAHI_WATCH_OUT : 0) |
423         (c->inbuf_length < sizeof(c->inbuf) ? AVAHI_WATCH_IN : 0));
424 }
425
426 static void server_work(AVAHI_GCC_UNUSED AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
427     Server *s = userdata;
428
429     assert(s);
430
431     if (events & AVAHI_WATCH_IN) {
432         int cfd;
433
434         if ((cfd = accept(fd, NULL, NULL)) < 0)
435             avahi_log_error("accept(): %s", strerror(errno));
436         else
437             client_new(s, cfd);
438     }
439 }
440     
441 int simple_protocol_setup(const AvahiPoll *poll_api) {
442     struct sockaddr_un sa;
443     mode_t u;
444
445     assert(!server);
446
447     server = avahi_new(Server, 1);
448     server->poll_api = poll_api;
449     server->bind_successful = 0;
450     server->fd = -1;
451     server->n_clients = 0;
452     AVAHI_LLIST_HEAD_INIT(Client, server->clients);
453     server->watch = NULL;
454     
455     u = umask(0000);
456
457     if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
458         avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
459         goto fail;
460     }
461
462     memset(&sa, 0, sizeof(sa));
463     sa.sun_family = AF_LOCAL;
464     strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
465
466     /* We simply remove existing UNIX sockets under this name. The
467        Avahi daemons makes sure that it runs only once on a host,
468        therefore sockets that already exist are stale and may be
469        removed without any ill effects */
470
471     unlink(AVAHI_SOCKET);
472     
473     if (bind(server->fd, &sa, sizeof(sa)) < 0) {
474         avahi_log_warn("bind(): %s", strerror(errno));
475         goto fail;
476     }
477
478     server->bind_successful = 1;
479     
480     if (listen(server->fd, 2) < 0) {
481         avahi_log_warn("listen(): %s", strerror(errno));
482         goto fail;
483     }
484
485     umask(u);
486
487     server->watch = poll_api->watch_new(poll_api, server->fd, AVAHI_WATCH_IN, server_work, server);
488     
489     return 0;
490
491 fail:
492     
493     umask(u);
494     simple_protocol_shutdown();
495
496     return -1;
497 }
498
499 void simple_protocol_shutdown(void) {
500
501     if (server) {
502
503         if (server->bind_successful)
504 #ifdef ENABLE_CHROOT
505             avahi_chroot_helper_unlink(AVAHI_SOCKET);
506 #else
507             unlink(AVAHI_SOCKET);
508 #endif
509
510         while (server->clients)
511             client_free(server->clients);
512
513         if (server->watch)
514             server->poll_api->watch_free(server->watch);
515         
516         if (server->fd >= 0)
517             close(server->fd);
518
519         avahi_free(server);
520         
521         server = NULL;
522     }
523 }
524
525 void simple_protocol_restart_queries(void) {
526     Client *c;
527
528     /* Restart queries in case of local domain name changes */
529     
530     assert(server);
531
532     for (c = server->clients; c; c = c->clients_next)
533         if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
534             avahi_s_dns_server_browser_free(c->dns_server_browser);
535             c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c);
536         }
537 }
538
539