s4-python: Add missing prototypes.
[Samba.git] / nsswitch / wins.c
blob6e3c84b6087abf1271d0f84922ea77926b26440c
1 /*
2 Unix SMB/CIFS implementation.
3 a WINS nsswitch module
4 Copyright (C) Andrew Tridgell 1999
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.
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.
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/>.
21 #include "includes.h"
22 #include "nsswitch/winbind_nss.h"
24 #ifdef HAVE_NS_API_H
26 #include <ns_daemon.h>
27 #endif
29 #if HAVE_PTHREAD_H
30 #include <pthread.h>
31 #endif
33 #if HAVE_PTHREAD
34 static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
35 #endif
37 #ifndef INADDRSZ
38 #define INADDRSZ 4
39 #endif
41 static int initialised;
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);
48 /* Use our own create socket code so we don't recurse.... */
50 static int wins_lookup_open_socket_in(void)
52 struct sockaddr_in sock;
53 int val=1;
54 int res;
56 memset((char *)&sock,'\0',sizeof(sock));
58 #ifdef HAVE_SOCK_SIN_LEN
59 sock.sin_len = sizeof(sock);
60 #endif
61 sock.sin_port = 0;
62 sock.sin_family = AF_INET;
63 sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
64 res = socket(AF_INET, SOCK_DGRAM, 0);
65 if (res == -1)
66 return -1;
68 if (setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) != 0) {
69 close(res);
70 return -1;
72 #ifdef SO_REUSEPORT
73 if (setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) != 0) {
74 close(res);
75 return -1;
77 #endif /* SO_REUSEPORT */
79 /* now we've got a socket - we need to bind it */
81 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
82 close(res);
83 return(-1);
86 set_socket_options(res,"SO_BROADCAST");
88 return res;
92 static void nss_wins_init(void)
94 initialised = 1;
95 load_case_tables();
96 lp_set_cmdline("log level", "0");
98 TimeInit();
99 setup_logging("nss_wins",False);
100 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
101 load_interfaces();
104 static struct in_addr *lookup_byname_backend(const char *name, int *count)
106 int fd = -1;
107 struct ip_service *address = NULL;
108 struct in_addr *ret = NULL;
109 int j, flags = 0;
111 if (!initialised) {
112 nss_wins_init();
115 *count = 0;
117 /* always try with wins first */
118 if (NT_STATUS_IS_OK(resolve_wins(name,0x00,&address,count))) {
119 if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
120 free( address );
121 return NULL;
123 if (address[0].ss.ss_family != AF_INET) {
124 free(address);
125 free(ret);
126 return NULL;
128 *ret = ((struct sockaddr_in *)(void *)&address[0].ss)
129 ->sin_addr;
130 free( address );
131 return ret;
134 fd = wins_lookup_open_socket_in();
135 if (fd == -1) {
136 return NULL;
139 /* uggh, we have to broadcast to each interface in turn */
140 for (j=iface_count() - 1;j >= 0;j--) {
141 const struct in_addr *bcast = iface_n_bcast_v4(j);
142 struct sockaddr_storage ss;
143 struct sockaddr_storage *pss;
144 NTSTATUS status;
146 if (!bcast) {
147 continue;
149 in_addr_to_sockaddr_storage(&ss, *bcast);
150 status = name_query(fd, name, 0x00, True, True, &ss,
151 NULL, &pss, count, &flags, NULL);
152 if (pss) {
153 if ((ret = SMB_MALLOC_P(struct in_addr)) == NULL) {
154 return NULL;
156 *ret = ((struct sockaddr_in *)pss)->sin_addr;
157 TALLOC_FREE(pss);
158 break;
162 close(fd);
163 return ret;
166 #ifdef HAVE_NS_API_H
168 static struct node_status *lookup_byaddr_backend(char *addr, int *count)
170 int fd;
171 struct sockaddr_storage ss;
172 struct nmb_name nname;
173 struct node_status *status;
175 if (!initialised) {
176 nss_wins_init();
179 fd = wins_lookup_open_socket_in();
180 if (fd == -1)
181 return NULL;
183 make_nmb_name(&nname, "*", 0);
184 if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
185 return NULL;
187 status = node_status_query(fd, &nname, &ss, count, NULL);
189 close(fd);
190 return status;
193 /* IRIX version */
195 int init(void)
197 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
198 nss_wins_init();
199 return NSD_OK;
202 int lookup(nsd_file_t *rq)
204 char *map;
205 char *key;
206 char *addr;
207 struct in_addr *ip_list;
208 struct node_status *status;
209 int i, count, len, size;
210 char response[1024];
211 bool found = False;
213 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
214 if (! rq)
215 return NSD_ERROR;
217 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
218 if (! map) {
219 rq->f_status = NS_FATAL;
220 return NSD_ERROR;
223 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
224 if (! key || ! *key) {
225 rq->f_status = NS_FATAL;
226 return NSD_ERROR;
229 response[0] = '\0';
230 len = sizeof(response) - 2;
233 * response needs to be a string of the following format
234 * ip_address[ ip_address]*\tname[ alias]*
236 if (StrCaseCmp(map,"hosts.byaddr") == 0) {
237 if ( status = lookup_byaddr_backend(key, &count)) {
238 size = strlen(key) + 1;
239 if (size > len) {
240 free(status);
241 return NSD_ERROR;
243 len -= size;
244 strncat(response,key,size);
245 strncat(response,"\t",1);
246 for (i = 0; i < count; i++) {
247 /* ignore group names */
248 if (status[i].flags & 0x80) continue;
249 if (status[i].type == 0x20) {
250 size = sizeof(status[i].name) + 1;
251 if (size > len) {
252 free(status);
253 return NSD_ERROR;
255 len -= size;
256 strncat(response, status[i].name, size);
257 strncat(response, " ", 1);
258 found = True;
261 response[strlen(response)-1] = '\n';
262 free(status);
264 } else if (StrCaseCmp(map,"hosts.byname") == 0) {
265 if (ip_list = lookup_byname_backend(key, &count)) {
266 for (i = count; i ; i--) {
267 addr = inet_ntoa(ip_list[i-1]);
268 size = strlen(addr) + 1;
269 if (size > len) {
270 free(ip_list);
271 return NSD_ERROR;
273 len -= size;
274 if (i != 0)
275 response[strlen(response)-1] = ' ';
276 strncat(response,addr,size);
277 strncat(response,"\t",1);
279 size = strlen(key) + 1;
280 if (size > len) {
281 free(ip_list);
282 return NSD_ERROR;
284 strncat(response,key,size);
285 strncat(response,"\n",1);
286 found = True;
287 free(ip_list);
291 if (found) {
292 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
293 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
294 return NSD_OK;
296 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
297 rq->f_status = NS_NOTFOUND;
298 return NSD_NEXT;
301 #else
303 /* Allocate some space from the nss static buffer. The buffer and buflen
304 are the pointers passed in by the C library to the _nss_*_*
305 functions. */
307 static char *get_static(char **buffer, size_t *buflen, int len)
309 char *result;
311 /* Error check. We return false if things aren't set up right, or
312 there isn't enough buffer space left. */
314 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
315 return NULL;
318 /* Return an index into the static buffer */
320 result = *buffer;
321 *buffer += len;
322 *buflen -= len;
324 return result;
327 /****************************************************************************
328 gethostbyname() - we ignore any domain portion of the name and only
329 handle names that are at most 15 characters long
330 **************************************************************************/
331 NSS_STATUS
332 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
333 char *buffer, size_t buflen, int *h_errnop)
335 NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
336 struct in_addr *ip_list;
337 int i, count;
338 fstring name;
339 size_t namelen;
340 TALLOC_CTX *frame;
342 #if HAVE_PTHREAD
343 pthread_mutex_lock(&wins_nss_mutex);
344 #endif
346 frame = talloc_stackframe();
348 memset(he, '\0', sizeof(*he));
349 fstrcpy(name, hostname);
351 /* Do lookup */
353 ip_list = lookup_byname_backend(name, &count);
355 if (!ip_list) {
356 nss_status = NSS_STATUS_NOTFOUND;
357 goto out;
360 /* Copy h_name */
362 namelen = strlen(name) + 1;
364 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
365 free(ip_list);
366 nss_status = NSS_STATUS_TRYAGAIN;
367 goto out;
370 memcpy(he->h_name, name, namelen);
372 /* Copy h_addr_list, align to pointer boundary first */
374 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
375 i = sizeof(char*) - i;
377 if (get_static(&buffer, &buflen, i) == NULL) {
378 free(ip_list);
379 nss_status = NSS_STATUS_TRYAGAIN;
380 goto out;
383 if ((he->h_addr_list = (char **)get_static(
384 &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL) {
385 free(ip_list);
386 nss_status = NSS_STATUS_TRYAGAIN;
387 goto out;
390 for (i = 0; i < count; i++) {
391 if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
392 INADDRSZ)) == NULL) {
393 free(ip_list);
394 nss_status = NSS_STATUS_TRYAGAIN;
395 goto out;
397 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
400 he->h_addr_list[count] = NULL;
402 free(ip_list);
404 /* Set h_addr_type and h_length */
406 he->h_addrtype = AF_INET;
407 he->h_length = INADDRSZ;
409 /* Set h_aliases */
411 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
412 i = sizeof(char*) - i;
414 if (get_static(&buffer, &buflen, i) == NULL) {
415 nss_status = NSS_STATUS_TRYAGAIN;
416 goto out;
419 if ((he->h_aliases = (char **)get_static(
420 &buffer, &buflen, sizeof(char *))) == NULL) {
421 nss_status = NSS_STATUS_TRYAGAIN;
422 goto out;
425 he->h_aliases[0] = NULL;
427 nss_status = NSS_STATUS_SUCCESS;
429 out:
431 TALLOC_FREE(frame);
433 #if HAVE_PTHREAD
434 pthread_mutex_unlock(&wins_nss_mutex);
435 #endif
436 return nss_status;
440 NSS_STATUS
441 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
442 char *buffer, size_t buflen, int *h_errnop)
444 NSS_STATUS nss_status;
446 if(af!=AF_INET) {
447 *h_errnop = NO_DATA;
448 nss_status = NSS_STATUS_UNAVAIL;
449 } else {
450 nss_status = _nss_wins_gethostbyname_r(
451 name, he, buffer, buflen, h_errnop);
453 return nss_status;
455 #endif