s4: Rename NSS_WRAPPER to nss_wrapper.
[kai/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 extern bool AllowDebugChange;
44
45 NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
46                           char *buffer, size_t buflen, int *h_errnop);
47 NSS_STATUS _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
48                            char *buffer, size_t buflen, int *h_errnop);
49
50 /* Use our own create socket code so we don't recurse.... */
51
52 static int wins_lookup_open_socket_in(void)
53 {
54         struct sockaddr_in sock;
55         int val=1;
56         int res;
57
58         memset((char *)&sock,'\0',sizeof(sock));
59
60 #ifdef HAVE_SOCK_SIN_LEN
61         sock.sin_len = sizeof(sock);
62 #endif
63         sock.sin_port = 0;
64         sock.sin_family = AF_INET;
65         sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
66         res = socket(AF_INET, SOCK_DGRAM, 0);
67         if (res == -1)
68                 return -1;
69
70         if (setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) != 0) {
71                 close(res);
72                 return -1;
73         }
74 #ifdef SO_REUSEPORT
75         if (setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) != 0) {
76                 close(res);
77                 return -1;
78         }
79 #endif /* SO_REUSEPORT */
80
81         /* now we've got a socket - we need to bind it */
82
83         if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
84                 close(res);
85                 return(-1);
86         }
87
88         set_socket_options(res,"SO_BROADCAST");
89
90         return res;
91 }
92
93
94 static void nss_wins_init(void)
95 {
96         initialised = 1;
97         DEBUGLEVEL = 0;
98         AllowDebugChange = False;
99
100         TimeInit();
101         setup_logging("nss_wins",False);
102         load_case_tables();
103         lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
104         load_interfaces();
105 }
106
107 static struct in_addr *lookup_byname_backend(const char *name, int *count)
108 {
109         int fd = -1;
110         struct ip_service *address = NULL;
111         struct in_addr *ret = NULL;
112         int j, flags = 0;
113
114         if (!initialised) {
115                 nss_wins_init();
116         }
117
118         *count = 0;
119
120         /* always try with wins first */
121         if (NT_STATUS_IS_OK(resolve_wins(name,0x00,&address,count))) {
122                 if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
123                         free( address );
124                         return NULL;
125                 }
126                 if (address[0].ss.ss_family != AF_INET) {
127                         free(address);
128                         free(ret);
129                         return NULL;
130                 }
131                 *ret = ((struct sockaddr_in *)&address[0].ss)->sin_addr;
132                 free( address );
133                 return ret;
134         }
135
136         fd = wins_lookup_open_socket_in();
137         if (fd == -1) {
138                 return NULL;
139         }
140
141         /* uggh, we have to broadcast to each interface in turn */
142         for (j=iface_count() - 1;j >= 0;j--) {
143                 const struct in_addr *bcast = iface_n_bcast_v4(j);
144                 struct sockaddr_storage ss;
145                 struct sockaddr_storage *pss;
146                 if (!bcast) {
147                         continue;
148                 }
149                 in_addr_to_sockaddr_storage(&ss, *bcast);
150                 pss = name_query(fd,name,0x00,True,True,&ss,count, &flags, NULL);
151                 if (pss) {
152                         if ((ret = SMB_MALLOC_P(struct in_addr)) == NULL) {
153                                 return NULL;
154                         }
155                         *ret = ((struct sockaddr_in *)pss)->sin_addr;
156                         break;
157                 }
158         }
159
160         close(fd);
161         return ret;
162 }
163
164 #ifdef HAVE_NS_API_H
165
166 static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
167 {
168         int fd;
169         struct sockaddr_storage ss;
170         struct nmb_name nname;
171         NODE_STATUS_STRUCT *status;
172
173         if (!initialised) {
174                 nss_wins_init();
175         }
176
177         fd = wins_lookup_open_socket_in();
178         if (fd == -1)
179                 return NULL;
180
181         make_nmb_name(&nname, "*", 0);
182         if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
183                 return NULL;
184         }
185         status = node_status_query(fd, &nname, &ss, count, NULL);
186
187         close(fd);
188         return status;
189 }
190
191 /* IRIX version */
192
193 int init(void)
194 {
195         nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
196         nss_wins_init();
197         return NSD_OK;
198 }
199
200 int lookup(nsd_file_t *rq)
201 {
202         char *map;
203         char *key;
204         char *addr;
205         struct in_addr *ip_list;
206         NODE_STATUS_STRUCT *status;
207         int i, count, len, size;
208         char response[1024];
209         bool found = False;
210
211         nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
212         if (! rq)
213                 return NSD_ERROR;
214
215         map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
216         if (! map) {
217                 rq->f_status = NS_FATAL;
218                 return NSD_ERROR;
219         }
220
221         key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
222         if (! key || ! *key) {
223                 rq->f_status = NS_FATAL;
224                 return NSD_ERROR;
225         }
226
227         response[0] = '\0';
228         len = sizeof(response) - 2;
229
230         /*
231          * response needs to be a string of the following format
232          * ip_address[ ip_address]*\tname[ alias]*
233          */
234         if (StrCaseCmp(map,"hosts.byaddr") == 0) {
235                 if ( status = lookup_byaddr_backend(key, &count)) {
236                     size = strlen(key) + 1;
237                     if (size > len) {
238                         free(status);
239                         return NSD_ERROR;
240                     }
241                     len -= size;
242                     strncat(response,key,size);
243                     strncat(response,"\t",1);
244                     for (i = 0; i < count; i++) {
245                         /* ignore group names */
246                         if (status[i].flags & 0x80) continue;
247                         if (status[i].type == 0x20) {
248                                 size = sizeof(status[i].name) + 1;
249                                 if (size > len) {
250                                     free(status);
251                                     return NSD_ERROR;
252                                 }
253                                 len -= size;
254                                 strncat(response, status[i].name, size);
255                                 strncat(response, " ", 1);
256                                 found = True;
257                         }
258                     }
259                     response[strlen(response)-1] = '\n';
260                     free(status);
261                 }
262         } else if (StrCaseCmp(map,"hosts.byname") == 0) {
263             if (ip_list = lookup_byname_backend(key, &count)) {
264                 for (i = count; i ; i--) {
265                     addr = inet_ntoa(ip_list[i-1]);
266                     size = strlen(addr) + 1;
267                     if (size > len) {
268                         free(ip_list);
269                         return NSD_ERROR;
270                     }
271                     len -= size;
272                     if (i != 0)
273                         response[strlen(response)-1] = ' ';
274                     strncat(response,addr,size);
275                     strncat(response,"\t",1);
276                 }
277                 size = strlen(key) + 1;
278                 if (size > len) {
279                     free(ip_list);
280                     return NSD_ERROR;
281                 }
282                 strncat(response,key,size);
283                 strncat(response,"\n",1);
284                 found = True;
285                 free(ip_list);
286             }
287         }
288
289         if (found) {
290             nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
291             nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
292             return NSD_OK;
293         }
294         nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
295         rq->f_status = NS_NOTFOUND;
296         return NSD_NEXT;
297 }
298
299 #else
300
301 /* Allocate some space from the nss static buffer.  The buffer and buflen
302    are the pointers passed in by the C library to the _nss_*_*
303    functions. */
304
305 static char *get_static(char **buffer, size_t *buflen, int len)
306 {
307         char *result;
308
309         /* Error check.  We return false if things aren't set up right, or
310            there isn't enough buffer space left. */
311
312         if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
313                 return NULL;
314         }
315
316         /* Return an index into the static buffer */
317
318         result = *buffer;
319         *buffer += len;
320         *buflen -= len;
321
322         return result;
323 }
324
325 /****************************************************************************
326 gethostbyname() - we ignore any domain portion of the name and only
327 handle names that are at most 15 characters long
328   **************************************************************************/
329 NSS_STATUS
330 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
331                           char *buffer, size_t buflen, int *h_errnop)
332 {
333         NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
334         struct in_addr *ip_list;
335         int i, count;
336         fstring name;
337         size_t namelen;
338         TALLOC_CTX *frame;
339
340 #if HAVE_PTHREAD
341         pthread_mutex_lock(&wins_nss_mutex);
342 #endif
343
344         frame = talloc_stackframe();
345
346         memset(he, '\0', sizeof(*he));
347         fstrcpy(name, hostname);
348
349         /* Do lookup */
350
351         ip_list = lookup_byname_backend(name, &count);
352
353         if (!ip_list) {
354                 nss_status = NSS_STATUS_NOTFOUND;
355                 goto out;
356         }
357
358         /* Copy h_name */
359
360         namelen = strlen(name) + 1;
361
362         if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
363                 free(ip_list);
364                 nss_status = NSS_STATUS_TRYAGAIN;
365                 goto out;
366         }
367
368         memcpy(he->h_name, name, namelen);
369
370         /* Copy h_addr_list, align to pointer boundary first */
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                 free(ip_list);
377                 nss_status = NSS_STATUS_TRYAGAIN;
378                 goto out;
379         }
380
381         if ((he->h_addr_list = (char **)get_static(
382                      &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL) {
383                 free(ip_list);
384                 nss_status = NSS_STATUS_TRYAGAIN;
385                 goto out;
386         }
387
388         for (i = 0; i < count; i++) {
389                 if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
390                                                      INADDRSZ)) == NULL) {
391                         free(ip_list);
392                         nss_status = NSS_STATUS_TRYAGAIN;
393                         goto out;
394                 }
395                 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
396         }
397
398         he->h_addr_list[count] = NULL;
399
400         free(ip_list);
401
402         /* Set h_addr_type and h_length */
403
404         he->h_addrtype = AF_INET;
405         he->h_length = INADDRSZ;
406
407         /* Set h_aliases */
408
409         if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
410                 i = sizeof(char*) - i;
411
412         if (get_static(&buffer, &buflen, i) == NULL) {
413                 nss_status = NSS_STATUS_TRYAGAIN;
414                 goto out;
415         }
416
417         if ((he->h_aliases = (char **)get_static(
418                      &buffer, &buflen, sizeof(char *))) == NULL) {
419                 nss_status = NSS_STATUS_TRYAGAIN;
420                 goto out;
421         }
422
423         he->h_aliases[0] = NULL;
424
425         nss_status = NSS_STATUS_SUCCESS;
426
427   out:
428
429         TALLOC_FREE(frame);
430
431 #if HAVE_PTHREAD
432         pthread_mutex_unlock(&wins_nss_mutex);
433 #endif
434         return nss_status;
435 }
436
437
438 NSS_STATUS
439 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
440                            char *buffer, size_t buflen, int *h_errnop)
441 {
442         NSS_STATUS nss_status;
443
444         if(af!=AF_INET) {
445                 *h_errnop = NO_DATA;
446                 nss_status = NSS_STATUS_UNAVAIL;
447         } else {
448                 nss_status = _nss_wins_gethostbyname_r(
449                                 name, he, buffer, buflen, h_errnop);
450         }
451         return nss_status;
452 }
453 #endif