s3/smbd: register Time Machine shares with Avahi
[nivanova/samba-autobuild/.git] / source3 / smbd / avahi_register.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Register _smb._tcp with avahi
4  *
5  * Copyright (C) Volker Lendecke 2009
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "includes.h"
22 #include "smbd/smbd.h"
23
24 #include <avahi-client/client.h>
25 #include <avahi-client/publish.h>
26 #include <avahi-common/error.h>
27 #include <avahi-common/malloc.h>
28 #include <avahi-common/strlst.h>
29
30 struct avahi_state_struct {
31         struct AvahiPoll *poll;
32         AvahiClient *client;
33         AvahiEntryGroup *entry_group;
34         uint16_t port;
35 };
36
37 static void *avahi_allocator_ctx = NULL;
38
39 static void * avahi_allocator_malloc(size_t size)
40 {
41         return talloc_size(avahi_allocator_ctx, size);
42 }
43
44 static void avahi_allocator_free(void *p)
45 {
46         TALLOC_FREE(p);
47 }
48
49 static void * avahi_allocator_realloc(void *p, size_t size)
50 {
51         return talloc_realloc_size(avahi_allocator_ctx, p, size);
52 }
53
54 static void * avahi_allocator_calloc(size_t count, size_t size)
55 {
56         void *p = talloc_array_size(avahi_allocator_ctx, size, count);
57         if (p) {
58                 memset(p, 0, size * count);
59         }
60         return p;
61 }
62
63 static const struct AvahiAllocator avahi_talloc_allocator = {
64         &avahi_allocator_malloc,
65         &avahi_allocator_free,
66         &avahi_allocator_realloc,
67         &avahi_allocator_calloc
68 };
69
70 static void avahi_entry_group_callback(AvahiEntryGroup *g,
71                                        AvahiEntryGroupState status,
72                                        void *userdata)
73 {
74         struct avahi_state_struct *state = talloc_get_type_abort(
75                 userdata, struct avahi_state_struct);
76         int error;
77
78         switch (status) {
79         case AVAHI_ENTRY_GROUP_ESTABLISHED:
80                 DBG_DEBUG("AVAHI_ENTRY_GROUP_ESTABLISHED\n");
81                 break;
82         case AVAHI_ENTRY_GROUP_FAILURE:
83                 error = avahi_client_errno(state->client);
84
85                 DBG_DEBUG("AVAHI_ENTRY_GROUP_FAILURE: %s\n",
86                           avahi_strerror(error));
87                 break;
88         case AVAHI_ENTRY_GROUP_COLLISION:
89                 DBG_DEBUG("AVAHI_ENTRY_GROUP_COLLISION\n");
90                 break;
91         case AVAHI_ENTRY_GROUP_UNCOMMITED:
92                 DBG_DEBUG("AVAHI_ENTRY_GROUP_UNCOMMITED\n");
93                 break;
94         case AVAHI_ENTRY_GROUP_REGISTERING:
95                 DBG_DEBUG("AVAHI_ENTRY_GROUP_REGISTERING\n");
96                 break;
97         }
98 }
99
100 static void avahi_client_callback(AvahiClient *c, AvahiClientState status,
101                                   void *userdata)
102 {
103         struct avahi_state_struct *state = talloc_get_type_abort(
104                 userdata, struct avahi_state_struct);
105         int error;
106
107         switch (status) {
108         case AVAHI_CLIENT_S_RUNNING: {
109                 int snum;
110                 int num_services = lp_numservices();
111                 int dk = 0;
112                 AvahiStringList *adisk = NULL;
113                 AvahiStringList *adisk2 = NULL;
114
115                 DBG_DEBUG("AVAHI_CLIENT_S_RUNNING\n");
116
117                 state->entry_group = avahi_entry_group_new(
118                         c, avahi_entry_group_callback, state);
119                 if (state->entry_group == NULL) {
120                         error = avahi_client_errno(c);
121                         DBG_DEBUG("avahi_entry_group_new failed: %s\n",
122                                   avahi_strerror(error));
123                         break;
124                 }
125
126                 error = avahi_entry_group_add_service(
127                             state->entry_group, AVAHI_IF_UNSPEC,
128                             AVAHI_PROTO_UNSPEC, 0, lp_netbios_name(),
129                             "_smb._tcp", NULL, NULL, state->port, NULL);
130                 if (error != AVAHI_OK) {
131                         DBG_DEBUG("avahi_entry_group_add_service failed: %s\n",
132                                   avahi_strerror(error));
133                         avahi_entry_group_free(state->entry_group);
134                         state->entry_group = NULL;
135                         break;
136                 }
137
138                 for (snum = 0; snum < num_services; snum++) {
139                         if (lp_snum_ok(snum) &&
140                             lp_parm_bool(snum, "fruit", "time machine", false))
141                         {
142                                 adisk2 = avahi_string_list_add_printf(
143                                             adisk, "dk%d=adVN=%s,adVF=0x82",
144                                             dk++, lp_const_servicename(snum));
145                                 if (adisk2 == NULL) {
146                                         DBG_DEBUG("avahi_string_list_add_printf"
147                                                   "failed: returned NULL\n");
148                                         avahi_string_list_free(adisk);
149                                         avahi_entry_group_free(state->entry_group);
150                                         state->entry_group = NULL;
151                                         break;
152                                 }
153                                 adisk = adisk2;
154                                 adisk2 = NULL;
155                         }
156                 }
157                 if (dk > 0) {
158                         adisk2 = avahi_string_list_add(adisk, "sys=adVF=0x100");
159                         if (adisk2 == NULL) {
160                                 DBG_DEBUG("avahi_string_list_add failed: "
161                                           "returned NULL\n");
162                                 avahi_string_list_free(adisk);
163                                 avahi_entry_group_free(state->entry_group);
164                                 state->entry_group = NULL;
165                                 break;
166                         }
167                         adisk = adisk2;
168                         adisk2 = NULL;
169
170                         error = avahi_entry_group_add_service_strlst(
171                                     state->entry_group, AVAHI_IF_UNSPEC,
172                                     AVAHI_PROTO_UNSPEC, 0, lp_netbios_name(),
173                                     "_adisk._tcp", NULL, NULL, 0, adisk);
174                         avahi_string_list_free(adisk);
175                         adisk = NULL;
176                         if (error != AVAHI_OK) {
177                                 DBG_DEBUG("avahi_entry_group_add_service_strlst "
178                                           "failed: %s\n", avahi_strerror(error));
179                                 avahi_entry_group_free(state->entry_group);
180                                 state->entry_group = NULL;
181                                 break;
182                         }
183                 }
184
185                 error = avahi_entry_group_commit(state->entry_group);
186                 if (error != AVAHI_OK) {
187                         DBG_DEBUG("avahi_entry_group_commit failed: %s\n",
188                                   avahi_strerror(error));
189                         avahi_entry_group_free(state->entry_group);
190                         state->entry_group = NULL;
191                         break;
192                 }
193                 break;
194         }
195         case AVAHI_CLIENT_FAILURE:
196                 error = avahi_client_errno(c);
197
198                 DBG_DEBUG("AVAHI_CLIENT_FAILURE: %s\n", avahi_strerror(error));
199
200                 if (error != AVAHI_ERR_DISCONNECTED) {
201                         break;
202                 }
203                 avahi_client_free(c);
204                 state->client = avahi_client_new(state->poll, AVAHI_CLIENT_NO_FAIL,
205                                                  avahi_client_callback, state,
206                                                  &error);
207                 if (state->client == NULL) {
208                         DBG_DEBUG("avahi_client_new failed: %s\n",
209                                   avahi_strerror(error));
210                         break;
211                 }
212                 break;
213         case AVAHI_CLIENT_S_COLLISION:
214                 DBG_DEBUG("AVAHI_CLIENT_S_COLLISION\n");
215                 break;
216         case AVAHI_CLIENT_S_REGISTERING:
217                 DBG_DEBUG("AVAHI_CLIENT_S_REGISTERING\n");
218                 break;
219         case AVAHI_CLIENT_CONNECTING:
220                 DBG_DEBUG("AVAHI_CLIENT_CONNECTING\n");
221                 break;
222         }
223 }
224
225 void *avahi_start_register(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
226                            uint16_t port)
227 {
228         struct avahi_state_struct *state;
229         int error;
230
231         avahi_allocator_ctx = talloc_new(mem_ctx);
232         if (avahi_allocator_ctx == NULL) {
233                 return NULL;
234         }
235         avahi_set_allocator(&avahi_talloc_allocator);
236
237         state = talloc(mem_ctx, struct avahi_state_struct);
238         if (state == NULL) {
239                 return state;
240         }
241         state->port = port;
242         state->poll = tevent_avahi_poll(state, ev);
243         if (state->poll == NULL) {
244                 goto fail;
245         }
246         state->client = avahi_client_new(state->poll, AVAHI_CLIENT_NO_FAIL,
247                                          avahi_client_callback, state,
248                                          &error);
249         if (state->client == NULL) {
250                 DBG_DEBUG("avahi_client_new failed: %s\n",
251                           avahi_strerror(error));
252                 goto fail;
253         }
254         return state;
255
256  fail:
257         TALLOC_FREE(state);
258         return NULL;
259 }