r25076: Checking in patches for 3.0.26
[Samba.git] / source / nsswitch / wins.c
blob697bc15f97a58c5d15d492aa44b5a71024897292
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 #ifndef INADDRSZ
30 #define INADDRSZ 4
31 #endif
33 static int initialised;
35 extern BOOL AllowDebugChange;
37 NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
38 char *buffer, size_t buflen, int *h_errnop);
39 NSS_STATUS _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
40 char *buffer, size_t buflen, int *h_errnop);
42 /* Use our own create socket code so we don't recurse.... */
44 static int wins_lookup_open_socket_in(void)
46 struct sockaddr_in sock;
47 int val=1;
48 int res;
50 memset((char *)&sock,'\0',sizeof(sock));
52 #ifdef HAVE_SOCK_SIN_LEN
53 sock.sin_len = sizeof(sock);
54 #endif
55 sock.sin_port = 0;
56 sock.sin_family = AF_INET;
57 sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
58 res = socket(AF_INET, SOCK_DGRAM, 0);
59 if (res == -1)
60 return -1;
62 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
63 #ifdef SO_REUSEPORT
64 setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val));
65 #endif /* SO_REUSEPORT */
67 /* now we've got a socket - we need to bind it */
69 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
70 close(res);
71 return(-1);
74 set_socket_options(res,"SO_BROADCAST");
76 return res;
80 static void nss_wins_init(void)
82 initialised = 1;
83 DEBUGLEVEL = 0;
84 AllowDebugChange = False;
86 TimeInit();
87 setup_logging("nss_wins",False);
88 load_case_tables();
89 lp_load(dyn_CONFIGFILE,True,False,False,True);
90 load_interfaces();
93 static struct in_addr *lookup_byname_backend(const char *name, int *count)
95 int fd = -1;
96 struct ip_service *address = NULL;
97 struct in_addr *ret = NULL;
98 int j, flags = 0;
100 if (!initialised) {
101 nss_wins_init();
104 *count = 0;
106 /* always try with wins first */
107 if (resolve_wins(name,0x00,&address,count)) {
108 if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
109 free( address );
110 return NULL;
112 *ret = address[0].ip;
113 free( address );
114 return ret;
117 fd = wins_lookup_open_socket_in();
118 if (fd == -1) {
119 return NULL;
122 /* uggh, we have to broadcast to each interface in turn */
123 for (j=iface_count() - 1;j >= 0;j--) {
124 struct in_addr *bcast = iface_n_bcast(j);
125 ret = name_query(fd,name,0x00,True,True,*bcast,count, &flags, NULL);
126 if (ret) break;
129 close(fd);
130 return ret;
133 #ifdef HAVE_NS_API_H
135 static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
137 int fd;
138 struct in_addr ip;
139 struct nmb_name nname;
140 NODE_STATUS_STRUCT *status;
142 if (!initialised) {
143 nss_wins_init();
146 fd = wins_lookup_open_socket_in();
147 if (fd == -1)
148 return NULL;
150 make_nmb_name(&nname, "*", 0);
151 ip = *interpret_addr2(addr);
152 status = node_status_query(fd,&nname,ip, count, NULL);
154 close(fd);
155 return status;
158 /* IRIX version */
160 int init(void)
162 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
163 nss_wins_init();
164 return NSD_OK;
167 int lookup(nsd_file_t *rq)
169 char *map;
170 char *key;
171 char *addr;
172 struct in_addr *ip_list;
173 NODE_STATUS_STRUCT *status;
174 int i, count, len, size;
175 char response[1024];
176 BOOL found = False;
178 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
179 if (! rq)
180 return NSD_ERROR;
182 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
183 if (! map) {
184 rq->f_status = NS_FATAL;
185 return NSD_ERROR;
188 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
189 if (! key || ! *key) {
190 rq->f_status = NS_FATAL;
191 return NSD_ERROR;
194 response[0] = '\0';
195 len = sizeof(response) - 2;
198 * response needs to be a string of the following format
199 * ip_address[ ip_address]*\tname[ alias]*
201 if (StrCaseCmp(map,"hosts.byaddr") == 0) {
202 if ( status = lookup_byaddr_backend(key, &count)) {
203 size = strlen(key) + 1;
204 if (size > len) {
205 free(status);
206 return NSD_ERROR;
208 len -= size;
209 strncat(response,key,size);
210 strncat(response,"\t",1);
211 for (i = 0; i < count; i++) {
212 /* ignore group names */
213 if (status[i].flags & 0x80) continue;
214 if (status[i].type == 0x20) {
215 size = sizeof(status[i].name) + 1;
216 if (size > len) {
217 free(status);
218 return NSD_ERROR;
220 len -= size;
221 strncat(response, status[i].name, size);
222 strncat(response, " ", 1);
223 found = True;
226 response[strlen(response)-1] = '\n';
227 free(status);
229 } else if (StrCaseCmp(map,"hosts.byname") == 0) {
230 if (ip_list = lookup_byname_backend(key, &count)) {
231 for (i = count; i ; i--) {
232 addr = inet_ntoa(ip_list[i-1]);
233 size = strlen(addr) + 1;
234 if (size > len) {
235 free(ip_list);
236 return NSD_ERROR;
238 len -= size;
239 if (i != 0)
240 response[strlen(response)-1] = ' ';
241 strncat(response,addr,size);
242 strncat(response,"\t",1);
244 size = strlen(key) + 1;
245 if (size > len) {
246 free(ip_list);
247 return NSD_ERROR;
249 strncat(response,key,size);
250 strncat(response,"\n",1);
251 found = True;
252 free(ip_list);
256 if (found) {
257 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
258 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
259 return NSD_OK;
261 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
262 rq->f_status = NS_NOTFOUND;
263 return NSD_NEXT;
266 #else
268 /* Allocate some space from the nss static buffer. The buffer and buflen
269 are the pointers passed in by the C library to the _nss_*_*
270 functions. */
272 static char *get_static(char **buffer, size_t *buflen, int len)
274 char *result;
276 /* Error check. We return false if things aren't set up right, or
277 there isn't enough buffer space left. */
279 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
280 return NULL;
283 /* Return an index into the static buffer */
285 result = *buffer;
286 *buffer += len;
287 *buflen -= len;
289 return result;
292 /****************************************************************************
293 gethostbyname() - we ignore any domain portion of the name and only
294 handle names that are at most 15 characters long
295 **************************************************************************/
296 NSS_STATUS
297 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
298 char *buffer, size_t buflen, int *h_errnop)
300 struct in_addr *ip_list;
301 int i, count;
302 fstring name;
303 size_t namelen;
305 memset(he, '\0', sizeof(*he));
306 fstrcpy(name, hostname);
308 /* Do lookup */
310 ip_list = lookup_byname_backend(name, &count);
312 if (!ip_list)
313 return NSS_STATUS_NOTFOUND;
315 /* Copy h_name */
317 namelen = strlen(name) + 1;
319 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL)
320 return NSS_STATUS_TRYAGAIN;
322 memcpy(he->h_name, name, namelen);
324 /* Copy h_addr_list, align to pointer boundary first */
326 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
327 i = sizeof(char*) - i;
329 if (get_static(&buffer, &buflen, i) == NULL)
330 return NSS_STATUS_TRYAGAIN;
332 if ((he->h_addr_list = (char **)get_static(
333 &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL)
334 return NSS_STATUS_TRYAGAIN;
336 for (i = 0; i < count; i++) {
337 if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
338 INADDRSZ)) == NULL)
339 return NSS_STATUS_TRYAGAIN;
340 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
343 he->h_addr_list[count] = NULL;
345 if (ip_list)
346 free(ip_list);
348 /* Set h_addr_type and h_length */
350 he->h_addrtype = AF_INET;
351 he->h_length = INADDRSZ;
353 /* Set h_aliases */
355 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
356 i = sizeof(char*) - i;
358 if (get_static(&buffer, &buflen, i) == NULL)
359 return NSS_STATUS_TRYAGAIN;
361 if ((he->h_aliases = (char **)get_static(
362 &buffer, &buflen, sizeof(char *))) == NULL)
363 return NSS_STATUS_TRYAGAIN;
365 he->h_aliases[0] = NULL;
367 return NSS_STATUS_SUCCESS;
371 NSS_STATUS
372 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
373 char *buffer, size_t buflen, int *h_errnop)
375 if(af!=AF_INET) {
376 *h_errnop = NO_DATA;
377 return NSS_STATUS_UNAVAIL;
380 return _nss_wins_gethostbyname_r(
381 name, he, buffer, buflen, h_errnop);
383 #endif