VERSION: Raise version number up to 3.0.36.
[Samba.git] / source / nsswitch / wins.c
bloba8736f041c8e6c1a187d4b8b6605cd41a07c2d95
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
23 #ifdef HAVE_NS_API_H
24 #undef VOLATILE
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 extern BOOL AllowDebugChange;
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);
50 /* Use our own create socket code so we don't recurse.... */
52 static int wins_lookup_open_socket_in(void)
54 struct sockaddr_in sock;
55 int val=1;
56 int res;
58 memset((char *)&sock,'\0',sizeof(sock));
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;
70 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
71 #ifdef SO_REUSEPORT
72 setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val));
73 #endif /* SO_REUSEPORT */
75 /* now we've got a socket - we need to bind it */
77 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
78 close(res);
79 return(-1);
82 set_socket_options(res,"SO_BROADCAST");
84 return res;
88 static void nss_wins_init(void)
90 initialised = 1;
91 DEBUGLEVEL = 0;
92 AllowDebugChange = False;
94 TimeInit();
95 setup_logging("nss_wins",False);
96 load_case_tables();
97 lp_load(dyn_CONFIGFILE,True,False,False,True);
98 load_interfaces();
101 static struct in_addr *lookup_byname_backend(const char *name, int *count)
103 int fd = -1;
104 struct ip_service *address = NULL;
105 struct in_addr *ret = NULL;
106 int j, flags = 0;
108 if (!initialised) {
109 nss_wins_init();
112 *count = 0;
114 /* always try with wins first */
115 if (resolve_wins(name,0x00,&address,count)) {
116 if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
117 free( address );
118 return NULL;
120 *ret = address[0].ip;
121 free( address );
122 return ret;
125 fd = wins_lookup_open_socket_in();
126 if (fd == -1) {
127 return NULL;
130 /* uggh, we have to broadcast to each interface in turn */
131 for (j=iface_count() - 1;j >= 0;j--) {
132 struct in_addr *bcast = iface_n_bcast(j);
133 ret = name_query(fd,name,0x00,True,True,*bcast,count, &flags, NULL);
134 if (ret) break;
137 close(fd);
138 return ret;
141 #ifdef HAVE_NS_API_H
143 static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
145 int fd;
146 struct in_addr ip;
147 struct nmb_name nname;
148 NODE_STATUS_STRUCT *status;
150 if (!initialised) {
151 nss_wins_init();
154 fd = wins_lookup_open_socket_in();
155 if (fd == -1)
156 return NULL;
158 make_nmb_name(&nname, "*", 0);
159 ip = *interpret_addr2(addr);
160 status = node_status_query(fd,&nname,ip, count, NULL);
162 close(fd);
163 return status;
166 /* IRIX version */
168 int init(void)
170 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
171 nss_wins_init();
172 return NSD_OK;
175 int lookup(nsd_file_t *rq)
177 char *map;
178 char *key;
179 char *addr;
180 struct in_addr *ip_list;
181 NODE_STATUS_STRUCT *status;
182 int i, count, len, size;
183 char response[1024];
184 BOOL found = False;
186 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
187 if (! rq)
188 return NSD_ERROR;
190 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
191 if (! map) {
192 rq->f_status = NS_FATAL;
193 return NSD_ERROR;
196 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
197 if (! key || ! *key) {
198 rq->f_status = NS_FATAL;
199 return NSD_ERROR;
202 response[0] = '\0';
203 len = sizeof(response) - 2;
206 * response needs to be a string of the following format
207 * ip_address[ ip_address]*\tname[ alias]*
209 if (StrCaseCmp(map,"hosts.byaddr") == 0) {
210 if ( status = lookup_byaddr_backend(key, &count)) {
211 size = strlen(key) + 1;
212 if (size > len) {
213 free(status);
214 return NSD_ERROR;
216 len -= size;
217 strncat(response,key,size);
218 strncat(response,"\t",1);
219 for (i = 0; i < count; i++) {
220 /* ignore group names */
221 if (status[i].flags & 0x80) continue;
222 if (status[i].type == 0x20) {
223 size = sizeof(status[i].name) + 1;
224 if (size > len) {
225 free(status);
226 return NSD_ERROR;
228 len -= size;
229 strncat(response, status[i].name, size);
230 strncat(response, " ", 1);
231 found = True;
234 response[strlen(response)-1] = '\n';
235 free(status);
237 } else if (StrCaseCmp(map,"hosts.byname") == 0) {
238 if (ip_list = lookup_byname_backend(key, &count)) {
239 for (i = count; i ; i--) {
240 addr = inet_ntoa(ip_list[i-1]);
241 size = strlen(addr) + 1;
242 if (size > len) {
243 free(ip_list);
244 return NSD_ERROR;
246 len -= size;
247 if (i != 0)
248 response[strlen(response)-1] = ' ';
249 strncat(response,addr,size);
250 strncat(response,"\t",1);
252 size = strlen(key) + 1;
253 if (size > len) {
254 free(ip_list);
255 return NSD_ERROR;
257 strncat(response,key,size);
258 strncat(response,"\n",1);
259 found = True;
260 free(ip_list);
264 if (found) {
265 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
266 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
267 return NSD_OK;
269 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
270 rq->f_status = NS_NOTFOUND;
271 return NSD_NEXT;
274 #else
276 /* Allocate some space from the nss static buffer. The buffer and buflen
277 are the pointers passed in by the C library to the _nss_*_*
278 functions. */
280 static char *get_static(char **buffer, size_t *buflen, int len)
282 char *result;
284 /* Error check. We return false if things aren't set up right, or
285 there isn't enough buffer space left. */
287 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
288 return NULL;
291 /* Return an index into the static buffer */
293 result = *buffer;
294 *buffer += len;
295 *buflen -= len;
297 return result;
300 /****************************************************************************
301 gethostbyname() - we ignore any domain portion of the name and only
302 handle names that are at most 15 characters long
303 **************************************************************************/
304 NSS_STATUS
305 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
306 char *buffer, size_t buflen, int *h_errnop)
308 NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
309 struct in_addr *ip_list;
310 int i, count;
311 fstring name;
312 size_t namelen;
314 #if HAVE_PTHREAD
315 pthread_mutex_lock(&wins_nss_mutex);
316 #endif
318 memset(he, '\0', sizeof(*he));
319 fstrcpy(name, hostname);
321 /* Do lookup */
323 ip_list = lookup_byname_backend(name, &count);
325 if (!ip_list) {
326 nss_status = NSS_STATUS_NOTFOUND;
327 goto out;
330 /* Copy h_name */
332 namelen = strlen(name) + 1;
334 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
335 nss_status = NSS_STATUS_TRYAGAIN;
336 goto out;
339 memcpy(he->h_name, name, namelen);
341 /* Copy h_addr_list, align to pointer boundary first */
343 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
344 i = sizeof(char*) - i;
346 if (get_static(&buffer, &buflen, i) == NULL) {
347 nss_status = NSS_STATUS_TRYAGAIN;
348 goto out;
351 if ((he->h_addr_list = (char **)get_static(
352 &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL) {
353 nss_status = NSS_STATUS_TRYAGAIN;
354 goto out;
357 for (i = 0; i < count; i++) {
358 if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
359 INADDRSZ)) == NULL) {
360 nss_status = NSS_STATUS_TRYAGAIN;
361 goto out;
363 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
366 he->h_addr_list[count] = NULL;
368 if (ip_list)
369 free(ip_list);
371 /* Set h_addr_type and h_length */
373 he->h_addrtype = AF_INET;
374 he->h_length = INADDRSZ;
376 /* Set h_aliases */
378 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
379 i = sizeof(char*) - i;
381 if (get_static(&buffer, &buflen, i) == NULL) {
382 nss_status = NSS_STATUS_TRYAGAIN;
383 goto out;
386 if ((he->h_aliases = (char **)get_static(
387 &buffer, &buflen, sizeof(char *))) == NULL) {
388 nss_status = NSS_STATUS_TRYAGAIN;
389 goto out;
392 he->h_aliases[0] = NULL;
394 nss_status = NSS_STATUS_SUCCESS;
396 out:
398 #if HAVE_PTHREAD
399 pthread_mutex_unlock(&wins_nss_mutex);
400 #endif
401 return nss_status;
405 NSS_STATUS
406 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
407 char *buffer, size_t buflen, int *h_errnop)
409 NSS_STATUS nss_status;
411 #if HAVE_PTHREAD
412 pthread_mutex_lock(&wins_nss_mutex);
413 #endif
415 if(af!=AF_INET) {
416 *h_errnop = NO_DATA;
417 nss_status = NSS_STATUS_UNAVAIL;
418 } else {
419 nss_status = _nss_wins_gethostbyname_r(
420 name, he, buffer, buflen, h_errnop);
422 #if HAVE_PTHREAD
423 pthread_mutex_unlock(&wins_nss_mutex);
424 #endif
425 return nss_status;
427 #endif