pam_winbind: Fix a segfault if initialization fails
[samba.git] / nsswitch / wins.c
1 /*
2    Unix SMB/CIFS implementation.
3    a WINS nsswitch module
4    Copyright (C) Andrew Tridgell 1999
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "includes.h"
22 #include "nsswitch/winbind_nss.h"
23
24 #ifdef HAVE_NS_API_H
25
26 #include <ns_daemon.h>
27 #endif
28
29 #if HAVE_PTHREAD_H
30 #include <pthread.h>
31 #endif
32
33 #if HAVE_PTHREAD
34 static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
35 #endif
36
37 #ifndef INADDRSZ
38 #define INADDRSZ 4
39 #endif
40
41 static int initialised;
42
43 NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
44                           char *buffer, size_t buflen, int *h_errnop);
45 NSS_STATUS _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
46                            char *buffer, size_t buflen, int *h_errnop);
47
48 static void nss_wins_init(void)
49 {
50         initialised = 1;
51         lp_set_cmdline("log level", "0");
52
53         TimeInit();
54         setup_logging("nss_wins",False);
55         lp_load_global(get_dyn_CONFIGFILE());
56         load_interfaces();
57 }
58
59 static struct in_addr *lookup_byname_backend(const char *name, int *count)
60 {
61         TALLOC_CTX *frame;
62         struct sockaddr_storage *address = NULL;
63         struct in_addr *ret = NULL;
64         NTSTATUS status;
65         const char *p;
66         size_t nbt_len;
67         int j;
68
69         if (!initialised) {
70                 nss_wins_init();
71         }
72
73         *count = 0;
74
75         nbt_len = strlen(name);
76         if (nbt_len > MAX_NETBIOSNAME_LEN - 1) {
77                 return NULL;
78         }
79         p = strchr(name, '.');
80         if (p != NULL) {
81                 return NULL;
82         }
83
84         frame = talloc_stackframe();
85         /* always try with wins first */
86         status = resolve_wins(name, 0x00, talloc_tos(),
87                               &address, count);
88         if (NT_STATUS_IS_OK(status)) {
89                 if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
90                         TALLOC_FREE(frame);
91                         return NULL;
92                 }
93                 if (address[0].ss_family != AF_INET) {
94                         free(ret);
95                         TALLOC_FREE(frame);
96                         return NULL;
97                 }
98                 *ret = ((struct sockaddr_in *)(void *)address)
99                         ->sin_addr;
100                 TALLOC_FREE(frame);
101                 return ret;
102         }
103
104         /* uggh, we have to broadcast to each interface in turn */
105         for (j=iface_count() - 1;j >= 0;j--) {
106                 const struct in_addr *bcast = iface_n_bcast_v4(j);
107                 struct sockaddr_storage ss;
108                 struct sockaddr_storage *pss;
109
110                 if (!bcast) {
111                         continue;
112                 }
113                 in_addr_to_sockaddr_storage(&ss, *bcast);
114                 status = name_query(name, 0x00, True, True, &ss,
115                                     talloc_tos(), &pss, count, NULL);
116                 if (NT_STATUS_IS_OK(status) && (*count > 0)) {
117                         if ((ret = SMB_MALLOC_P(struct in_addr)) == NULL) {
118                                 TALLOC_FREE(frame);
119                                 return NULL;
120                         }
121                         *ret = ((struct sockaddr_in *)pss)->sin_addr;
122                         break;
123                 }
124         }
125         TALLOC_FREE(frame);
126         return ret;
127 }
128
129 #ifdef HAVE_NS_API_H
130
131 static struct node_status *lookup_byaddr_backend(char *addr, int *count)
132 {
133         struct sockaddr_storage ss;
134         struct nmb_name nname;
135         struct node_status *result;
136         NTSTATUS status;
137
138         if (!initialised) {
139                 nss_wins_init();
140         }
141
142         make_nmb_name(&nname, "*", 0);
143         if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
144                 return NULL;
145         }
146         status = node_status_query(NULL, &nname, &ss, &result, count, NULL);
147         if (!NT_STATUS_IS_OK(status)) {
148                 return NULL;
149         }
150
151         return result;
152 }
153
154 /* IRIX version */
155
156 int init(void)
157 {
158         nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
159         nss_wins_init();
160         return NSD_OK;
161 }
162
163 int lookup(nsd_file_t *rq)
164 {
165         char *map;
166         char *key;
167         char *addr;
168         struct in_addr *ip_list;
169         struct node_status *status;
170         int i, count, len, size;
171         char response[1024];
172         bool found = False;
173
174         nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
175         if (! rq)
176                 return NSD_ERROR;
177
178         map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
179         if (! map) {
180                 rq->f_status = NS_FATAL;
181                 return NSD_ERROR;
182         }
183
184         key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
185         if (! key || ! *key) {
186                 rq->f_status = NS_FATAL;
187                 return NSD_ERROR;
188         }
189
190         response[0] = '\0';
191         len = sizeof(response) - 2;
192
193         /*
194          * response needs to be a string of the following format
195          * ip_address[ ip_address]*\tname[ alias]*
196          */
197         if (strcasecmp_m(map,"hosts.byaddr") == 0) {
198                 if ( status = lookup_byaddr_backend(key, &count)) {
199                     size = strlen(key) + 1;
200                     if (size > len) {
201                         talloc_free(status);
202                         return NSD_ERROR;
203                     }
204                     len -= size;
205                     strncat(response,key,size);
206                     strncat(response,"\t",1);
207                     for (i = 0; i < count; i++) {
208                         /* ignore group names */
209                         if (status[i].flags & 0x80) continue;
210                         if (status[i].type == 0x20) {
211                                 size = sizeof(status[i].name) + 1;
212                                 if (size > len) {
213                                     talloc_free(status);
214                                     return NSD_ERROR;
215                                 }
216                                 len -= size;
217                                 strncat(response, status[i].name, size);
218                                 strncat(response, " ", 1);
219                                 found = True;
220                         }
221                     }
222                     response[strlen(response)-1] = '\n';
223                     talloc_free(status);
224                 }
225         } else if (strcasecmp_m(map,"hosts.byname") == 0) {
226             if (ip_list = lookup_byname_backend(key, &count)) {
227                 for (i = count; i ; i--) {
228                     addr = inet_ntoa(ip_list[i-1]);
229                     size = strlen(addr) + 1;
230                     if (size > len) {
231                         free(ip_list);
232                         return NSD_ERROR;
233                     }
234                     len -= size;
235                     if (i != 0)
236                         response[strlen(response)-1] = ' ';
237                     strncat(response,addr,size);
238                     strncat(response,"\t",1);
239                 }
240                 size = strlen(key) + 1;
241                 if (size > len) {
242                     free(ip_list);
243                     return NSD_ERROR;
244                 }
245                 strncat(response,key,size);
246                 strncat(response,"\n",1);
247                 found = True;
248                 free(ip_list);
249             }
250         }
251
252         if (found) {
253             nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
254             nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
255             return NSD_OK;
256         }
257         nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
258         rq->f_status = NS_NOTFOUND;
259         return NSD_NEXT;
260 }
261
262 #else
263
264 /* Allocate some space from the nss static buffer.  The buffer and buflen
265    are the pointers passed in by the C library to the _nss_*_*
266    functions. */
267
268 static char *get_static(char **buffer, size_t *buflen, int len)
269 {
270         char *result;
271
272         /* Error check.  We return false if things aren't set up right, or
273            there isn't enough buffer space left. */
274
275         if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
276                 return NULL;
277         }
278
279         /* Return an index into the static buffer */
280
281         result = *buffer;
282         *buffer += len;
283         *buflen -= len;
284
285         return result;
286 }
287
288 /****************************************************************************
289 gethostbyname() - we ignore any domain portion of the name and only
290 handle names that are at most 15 characters long
291   **************************************************************************/
292 NSS_STATUS
293 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
294                           char *buffer, size_t buflen, int *h_errnop)
295 {
296         NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
297         struct in_addr *ip_list;
298         int i, count;
299         fstring name;
300         size_t namelen;
301         TALLOC_CTX *frame;
302
303 #if HAVE_PTHREAD
304         pthread_mutex_lock(&wins_nss_mutex);
305 #endif
306
307         frame = talloc_stackframe();
308
309         memset(he, '\0', sizeof(*he));
310         fstrcpy(name, hostname);
311
312         /* Do lookup */
313
314         ip_list = lookup_byname_backend(name, &count);
315
316         if (!ip_list) {
317                 nss_status = NSS_STATUS_NOTFOUND;
318                 goto out;
319         }
320
321         /* Copy h_name */
322
323         namelen = strlen(name) + 1;
324
325         if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
326                 free(ip_list);
327                 nss_status = NSS_STATUS_TRYAGAIN;
328                 goto out;
329         }
330
331         memcpy(he->h_name, name, namelen);
332
333         /* Copy h_addr_list, align to pointer boundary first */
334
335         if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
336                 i = sizeof(char*) - i;
337
338         if (get_static(&buffer, &buflen, i) == NULL) {
339                 free(ip_list);
340                 nss_status = NSS_STATUS_TRYAGAIN;
341                 goto out;
342         }
343
344         if ((he->h_addr_list = (char **)get_static(
345                      &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL) {
346                 free(ip_list);
347                 nss_status = NSS_STATUS_TRYAGAIN;
348                 goto out;
349         }
350
351         for (i = 0; i < count; i++) {
352                 if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
353                                                      INADDRSZ)) == NULL) {
354                         free(ip_list);
355                         nss_status = NSS_STATUS_TRYAGAIN;
356                         goto out;
357                 }
358                 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
359         }
360
361         he->h_addr_list[count] = NULL;
362
363         free(ip_list);
364
365         /* Set h_addr_type and h_length */
366
367         he->h_addrtype = AF_INET;
368         he->h_length = INADDRSZ;
369
370         /* Set h_aliases */
371
372         if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
373                 i = sizeof(char*) - i;
374
375         if (get_static(&buffer, &buflen, i) == NULL) {
376                 nss_status = NSS_STATUS_TRYAGAIN;
377                 goto out;
378         }
379
380         if ((he->h_aliases = (char **)get_static(
381                      &buffer, &buflen, sizeof(char *))) == NULL) {
382                 nss_status = NSS_STATUS_TRYAGAIN;
383                 goto out;
384         }
385
386         he->h_aliases[0] = NULL;
387
388         nss_status = NSS_STATUS_SUCCESS;
389
390   out:
391
392         TALLOC_FREE(frame);
393
394 #if HAVE_PTHREAD
395         pthread_mutex_unlock(&wins_nss_mutex);
396 #endif
397         return nss_status;
398 }
399
400
401 NSS_STATUS
402 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
403                            char *buffer, size_t buflen, int *h_errnop)
404 {
405         NSS_STATUS nss_status;
406
407         if(af!=AF_INET) {
408                 *h_errnop = NO_DATA;
409                 nss_status = NSS_STATUS_UNAVAIL;
410         } else {
411                 nss_status = _nss_wins_gethostbyname_r(
412                                 name, he, buffer, buflen, h_errnop);
413         }
414         return nss_status;
415 }
416 #endif