r17509: same for old woody packaging stuff
[Samba.git] / source / nsswitch / wins.c
blob2cc7edb4c7b97edfe7519da970fcf9b892a6d14a
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 /* Use our own create socket code so we don't recurse.... */
39 static int wins_lookup_open_socket_in(void)
41 struct sockaddr_in sock;
42 int val=1;
43 int res;
45 memset((char *)&sock,'\0',sizeof(sock));
47 #ifdef HAVE_SOCK_SIN_LEN
48 sock.sin_len = sizeof(sock);
49 #endif
50 sock.sin_port = 0;
51 sock.sin_family = AF_INET;
52 sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
53 res = socket(AF_INET, SOCK_DGRAM, 0);
54 if (res == -1)
55 return -1;
57 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
58 #ifdef SO_REUSEPORT
59 setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val));
60 #endif /* SO_REUSEPORT */
62 /* now we've got a socket - we need to bind it */
64 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
65 close(res);
66 return(-1);
69 set_socket_options(res,"SO_BROADCAST");
71 return res;
75 static void nss_wins_init(void)
77 initialised = 1;
78 DEBUGLEVEL = 0;
79 AllowDebugChange = False;
81 TimeInit();
82 setup_logging("nss_wins",False);
83 load_case_tables();
84 lp_load(dyn_CONFIGFILE,True,False,False,True);
85 load_interfaces();
88 static struct in_addr *lookup_byname_backend(const char *name, int *count)
90 int fd = -1;
91 struct ip_service *address = NULL;
92 struct in_addr *ret = NULL;
93 int j, flags = 0;
95 if (!initialised) {
96 nss_wins_init();
99 *count = 0;
101 /* always try with wins first */
102 if (resolve_wins(name,0x00,&address,count)) {
103 if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
104 free( address );
105 return NULL;
107 *ret = address[0].ip;
108 free( address );
109 return ret;
112 fd = wins_lookup_open_socket_in();
113 if (fd == -1) {
114 return NULL;
117 /* uggh, we have to broadcast to each interface in turn */
118 for (j=iface_count() - 1;j >= 0;j--) {
119 struct in_addr *bcast = iface_n_bcast(j);
120 ret = name_query(fd,name,0x00,True,True,*bcast,count, &flags, NULL);
121 if (ret) break;
124 close(fd);
125 return ret;
128 #ifdef HAVE_NS_API_H
130 static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
132 int fd;
133 struct in_addr ip;
134 struct nmb_name nname;
135 NODE_STATUS_STRUCT *status;
137 if (!initialised) {
138 nss_wins_init();
141 fd = wins_lookup_open_socket_in();
142 if (fd == -1)
143 return NULL;
145 make_nmb_name(&nname, "*", 0);
146 ip = *interpret_addr2(addr);
147 status = node_status_query(fd,&nname,ip, count, NULL);
149 close(fd);
150 return status;
153 /* IRIX version */
155 int init(void)
157 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
158 nss_wins_init();
159 return NSD_OK;
162 int lookup(nsd_file_t *rq)
164 char *map;
165 char *key;
166 char *addr;
167 struct in_addr *ip_list;
168 NODE_STATUS_STRUCT *status;
169 int i, count, len, size;
170 char response[1024];
171 BOOL found = False;
173 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
174 if (! rq)
175 return NSD_ERROR;
177 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
178 if (! map) {
179 rq->f_status = NS_FATAL;
180 return NSD_ERROR;
183 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
184 if (! key || ! *key) {
185 rq->f_status = NS_FATAL;
186 return NSD_ERROR;
189 response[0] = '\0';
190 len = sizeof(response) - 2;
193 * response needs to be a string of the following format
194 * ip_address[ ip_address]*\tname[ alias]*
196 if (StrCaseCmp(map,"hosts.byaddr") == 0) {
197 if ( status = lookup_byaddr_backend(key, &count)) {
198 size = strlen(key) + 1;
199 if (size > len) {
200 free(status);
201 return NSD_ERROR;
203 len -= size;
204 strncat(response,key,size);
205 strncat(response,"\t",1);
206 for (i = 0; i < count; i++) {
207 /* ignore group names */
208 if (status[i].flags & 0x80) continue;
209 if (status[i].type == 0x20) {
210 size = sizeof(status[i].name) + 1;
211 if (size > len) {
212 free(status);
213 return NSD_ERROR;
215 len -= size;
216 strncat(response, status[i].name, size);
217 strncat(response, " ", 1);
218 found = True;
221 response[strlen(response)-1] = '\n';
222 free(status);
224 } else if (StrCaseCmp(map,"hosts.byname") == 0) {
225 if (ip_list = lookup_byname_backend(key, &count)) {
226 for (i = count; i ; i--) {
227 addr = inet_ntoa(ip_list[i-1]);
228 size = strlen(addr) + 1;
229 if (size > len) {
230 free(ip_list);
231 return NSD_ERROR;
233 len -= size;
234 if (i != 0)
235 response[strlen(response)-1] = ' ';
236 strncat(response,addr,size);
237 strncat(response,"\t",1);
239 size = strlen(key) + 1;
240 if (size > len) {
241 free(ip_list);
242 return NSD_ERROR;
244 strncat(response,key,size);
245 strncat(response,"\n",1);
246 found = True;
247 free(ip_list);
251 if (found) {
252 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
253 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
254 return NSD_OK;
256 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
257 rq->f_status = NS_NOTFOUND;
258 return NSD_NEXT;
261 #else
263 /* Allocate some space from the nss static buffer. The buffer and buflen
264 are the pointers passed in by the C library to the _nss_*_*
265 functions. */
267 static char *get_static(char **buffer, size_t *buflen, int len)
269 char *result;
271 /* Error check. We return false if things aren't set up right, or
272 there isn't enough buffer space left. */
274 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
275 return NULL;
278 /* Return an index into the static buffer */
280 result = *buffer;
281 *buffer += len;
282 *buflen -= len;
284 return result;
287 /****************************************************************************
288 gethostbyname() - we ignore any domain portion of the name and only
289 handle names that are at most 15 characters long
290 **************************************************************************/
291 NSS_STATUS
292 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
293 char *buffer, size_t buflen, int *h_errnop)
295 struct in_addr *ip_list;
296 int i, count;
297 fstring name;
298 size_t namelen;
300 memset(he, '\0', sizeof(*he));
301 fstrcpy(name, hostname);
303 /* Do lookup */
305 ip_list = lookup_byname_backend(name, &count);
307 if (!ip_list)
308 return NSS_STATUS_NOTFOUND;
310 /* Copy h_name */
312 namelen = strlen(name) + 1;
314 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL)
315 return NSS_STATUS_TRYAGAIN;
317 memcpy(he->h_name, name, namelen);
319 /* Copy h_addr_list, align to pointer boundary first */
321 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
322 i = sizeof(char*) - i;
324 if (get_static(&buffer, &buflen, i) == NULL)
325 return NSS_STATUS_TRYAGAIN;
327 if ((he->h_addr_list = (char **)get_static(
328 &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL)
329 return NSS_STATUS_TRYAGAIN;
331 for (i = 0; i < count; i++) {
332 if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
333 INADDRSZ)) == NULL)
334 return NSS_STATUS_TRYAGAIN;
335 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
338 he->h_addr_list[count] = NULL;
340 if (ip_list)
341 free(ip_list);
343 /* Set h_addr_type and h_length */
345 he->h_addrtype = AF_INET;
346 he->h_length = INADDRSZ;
348 /* Set h_aliases */
350 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
351 i = sizeof(char*) - i;
353 if (get_static(&buffer, &buflen, i) == NULL)
354 return NSS_STATUS_TRYAGAIN;
356 if ((he->h_aliases = (char **)get_static(
357 &buffer, &buflen, sizeof(char *))) == NULL)
358 return NSS_STATUS_TRYAGAIN;
360 he->h_aliases[0] = NULL;
362 return NSS_STATUS_SUCCESS;
366 NSS_STATUS
367 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
368 char *buffer, size_t buflen, int *h_errnop)
370 if(af!=AF_INET) {
371 *h_errnop = NO_DATA;
372 return NSS_STATUS_UNAVAIL;
375 return _nss_wins_gethostbyname_r(
376 name, he, buffer, buflen, h_errnop);
378 #endif