s4/heimdal_build: use GetTimeOfDay macro instead of gettimeofday
[Samba/ita.git] / nsswitch / wins.c
blobb4bc788f841815865eebfb6560d561e9475011a0
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 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 if (setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) != 0) {
71 close(res);
72 return -1;
74 #ifdef SO_REUSEPORT
75 if (setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) != 0) {
76 close(res);
77 return -1;
79 #endif /* SO_REUSEPORT */
81 /* now we've got a socket - we need to bind it */
83 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
84 close(res);
85 return(-1);
88 set_socket_options(res,"SO_BROADCAST");
90 return res;
94 static void nss_wins_init(void)
96 initialised = 1;
97 DEBUGLEVEL = 0;
98 AllowDebugChange = False;
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();
107 static struct in_addr *lookup_byname_backend(const char *name, int *count)
109 int fd = -1;
110 struct ip_service *address = NULL;
111 struct in_addr *ret = NULL;
112 int j, flags = 0;
114 if (!initialised) {
115 nss_wins_init();
118 *count = 0;
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;
126 if (address[0].ss.ss_family != AF_INET) {
127 free(address);
128 free(ret);
129 return NULL;
131 *ret = ((struct sockaddr_in *)&address[0].ss)->sin_addr;
132 free( address );
133 return ret;
136 fd = wins_lookup_open_socket_in();
137 if (fd == -1) {
138 return NULL;
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;
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;
155 *ret = ((struct sockaddr_in *)pss)->sin_addr;
156 break;
160 close(fd);
161 return ret;
164 #ifdef HAVE_NS_API_H
166 static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
168 int fd;
169 struct sockaddr_storage ss;
170 struct nmb_name nname;
171 NODE_STATUS_STRUCT *status;
173 if (!initialised) {
174 nss_wins_init();
177 fd = wins_lookup_open_socket_in();
178 if (fd == -1)
179 return NULL;
181 make_nmb_name(&nname, "*", 0);
182 if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
183 return NULL;
185 status = node_status_query(fd, &nname, &ss, count, NULL);
187 close(fd);
188 return status;
191 /* IRIX version */
193 int init(void)
195 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
196 nss_wins_init();
197 return NSD_OK;
200 int lookup(nsd_file_t *rq)
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;
211 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
212 if (! rq)
213 return NSD_ERROR;
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;
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;
227 response[0] = '\0';
228 len = sizeof(response) - 2;
231 * response needs to be a string of the following format
232 * ip_address[ ip_address]*\tname[ alias]*
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;
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;
253 len -= size;
254 strncat(response, status[i].name, size);
255 strncat(response, " ", 1);
256 found = True;
259 response[strlen(response)-1] = '\n';
260 free(status);
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;
271 len -= size;
272 if (i != 0)
273 response[strlen(response)-1] = ' ';
274 strncat(response,addr,size);
275 strncat(response,"\t",1);
277 size = strlen(key) + 1;
278 if (size > len) {
279 free(ip_list);
280 return NSD_ERROR;
282 strncat(response,key,size);
283 strncat(response,"\n",1);
284 found = True;
285 free(ip_list);
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;
294 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
295 rq->f_status = NS_NOTFOUND;
296 return NSD_NEXT;
299 #else
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. */
305 static char *get_static(char **buffer, size_t *buflen, int len)
307 char *result;
309 /* Error check. We return false if things aren't set up right, or
310 there isn't enough buffer space left. */
312 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
313 return NULL;
316 /* Return an index into the static buffer */
318 result = *buffer;
319 *buffer += len;
320 *buflen -= len;
322 return result;
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)
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;
340 #if HAVE_PTHREAD
341 pthread_mutex_lock(&wins_nss_mutex);
342 #endif
344 frame = talloc_stackframe();
346 memset(he, '\0', sizeof(*he));
347 fstrcpy(name, hostname);
349 /* Do lookup */
351 ip_list = lookup_byname_backend(name, &count);
353 if (!ip_list) {
354 nss_status = NSS_STATUS_NOTFOUND;
355 goto out;
358 /* Copy h_name */
360 namelen = strlen(name) + 1;
362 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
363 free(ip_list);
364 nss_status = NSS_STATUS_TRYAGAIN;
365 goto out;
368 memcpy(he->h_name, name, namelen);
370 /* Copy h_addr_list, align to pointer boundary first */
372 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
373 i = sizeof(char*) - i;
375 if (get_static(&buffer, &buflen, i) == NULL) {
376 free(ip_list);
377 nss_status = NSS_STATUS_TRYAGAIN;
378 goto out;
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;
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;
395 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
398 he->h_addr_list[count] = NULL;
400 free(ip_list);
402 /* Set h_addr_type and h_length */
404 he->h_addrtype = AF_INET;
405 he->h_length = INADDRSZ;
407 /* Set h_aliases */
409 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
410 i = sizeof(char*) - i;
412 if (get_static(&buffer, &buflen, i) == NULL) {
413 nss_status = NSS_STATUS_TRYAGAIN;
414 goto out;
417 if ((he->h_aliases = (char **)get_static(
418 &buffer, &buflen, sizeof(char *))) == NULL) {
419 nss_status = NSS_STATUS_TRYAGAIN;
420 goto out;
423 he->h_aliases[0] = NULL;
425 nss_status = NSS_STATUS_SUCCESS;
427 out:
429 TALLOC_FREE(frame);
431 #if HAVE_PTHREAD
432 pthread_mutex_unlock(&wins_nss_mutex);
433 #endif
434 return nss_status;
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)
442 NSS_STATUS nss_status;
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);
451 return nss_status;
453 #endif