client: Fix the proto header to fix the build.
[Samba.git] / source3 / nsswitch / wins.c
blob7d42381986df537ab9f22fd1ea14c642ea9d8087
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 #ifdef HAVE_NS_API_H
23 #undef VOLATILE
25 #include <ns_daemon.h>
26 #endif
28 #ifndef INADDRSZ
29 #define INADDRSZ 4
30 #endif
32 static int initialised;
34 extern bool AllowDebugChange;
36 NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
37 char *buffer, size_t buflen, int *h_errnop);
38 NSS_STATUS _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
39 char *buffer, size_t buflen, int *h_errnop);
41 /* Use our own create socket code so we don't recurse.... */
43 static int wins_lookup_open_socket_in(void)
45 struct sockaddr_in sock;
46 int val=1;
47 int res;
49 memset((char *)&sock,'\0',sizeof(sock));
51 #ifdef HAVE_SOCK_SIN_LEN
52 sock.sin_len = sizeof(sock);
53 #endif
54 sock.sin_port = 0;
55 sock.sin_family = AF_INET;
56 sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
57 res = socket(AF_INET, SOCK_DGRAM, 0);
58 if (res == -1)
59 return -1;
61 if (setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) != 0) {
62 close(res);
63 return -1;
65 #ifdef SO_REUSEPORT
66 if (setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) != 0) {
67 close(res);
68 return -1;
70 #endif /* SO_REUSEPORT */
72 /* now we've got a socket - we need to bind it */
74 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
75 close(res);
76 return(-1);
79 set_socket_options(res,"SO_BROADCAST");
81 return res;
85 static void nss_wins_init(void)
87 initialised = 1;
88 DEBUGLEVEL = 0;
89 AllowDebugChange = False;
91 TimeInit();
92 setup_logging("nss_wins",False);
93 load_case_tables();
94 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
95 load_interfaces();
98 static struct in_addr *lookup_byname_backend(const char *name, int *count)
100 int fd = -1;
101 struct ip_service *address = NULL;
102 struct in_addr *ret = NULL;
103 int j, flags = 0;
105 if (!initialised) {
106 nss_wins_init();
109 *count = 0;
111 /* always try with wins first */
112 if (NT_STATUS_IS_OK(resolve_wins(name,0x00,&address,count))) {
113 if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
114 free( address );
115 return NULL;
117 if (address[0].ss.ss_family != AF_INET) {
118 free(address);
119 free(ret);
120 return NULL;
122 *ret = ((struct sockaddr_in *)&address[0].ss)->sin_addr;
123 free( address );
124 return ret;
127 fd = wins_lookup_open_socket_in();
128 if (fd == -1) {
129 return NULL;
132 /* uggh, we have to broadcast to each interface in turn */
133 for (j=iface_count() - 1;j >= 0;j--) {
134 const struct in_addr *bcast = iface_n_bcast_v4(j);
135 struct sockaddr_storage ss;
136 struct sockaddr_storage *pss;
137 if (!bcast) {
138 continue;
140 in_addr_to_sockaddr_storage(&ss, *bcast);
141 pss = name_query(fd,name,0x00,True,True,&ss,count, &flags, NULL);
142 if (pss) {
143 if ((ret = SMB_MALLOC_P(struct in_addr)) == NULL) {
144 return NULL;
146 *ret = ((struct sockaddr_in *)pss)->sin_addr;
147 break;
151 close(fd);
152 return ret;
155 #ifdef HAVE_NS_API_H
157 static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
159 int fd;
160 struct sockaddr_storage ss;
161 struct nmb_name nname;
162 NODE_STATUS_STRUCT *status;
164 if (!initialised) {
165 nss_wins_init();
168 fd = wins_lookup_open_socket_in();
169 if (fd == -1)
170 return NULL;
172 make_nmb_name(&nname, "*", 0);
173 if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
174 return NULL;
176 status = node_status_query(fd, &nname, &ss, count, NULL);
178 close(fd);
179 return status;
182 /* IRIX version */
184 int init(void)
186 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
187 nss_wins_init();
188 return NSD_OK;
191 int lookup(nsd_file_t *rq)
193 char *map;
194 char *key;
195 char *addr;
196 struct in_addr *ip_list;
197 NODE_STATUS_STRUCT *status;
198 int i, count, len, size;
199 char response[1024];
200 bool found = False;
202 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
203 if (! rq)
204 return NSD_ERROR;
206 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
207 if (! map) {
208 rq->f_status = NS_FATAL;
209 return NSD_ERROR;
212 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
213 if (! key || ! *key) {
214 rq->f_status = NS_FATAL;
215 return NSD_ERROR;
218 response[0] = '\0';
219 len = sizeof(response) - 2;
222 * response needs to be a string of the following format
223 * ip_address[ ip_address]*\tname[ alias]*
225 if (StrCaseCmp(map,"hosts.byaddr") == 0) {
226 if ( status = lookup_byaddr_backend(key, &count)) {
227 size = strlen(key) + 1;
228 if (size > len) {
229 free(status);
230 return NSD_ERROR;
232 len -= size;
233 strncat(response,key,size);
234 strncat(response,"\t",1);
235 for (i = 0; i < count; i++) {
236 /* ignore group names */
237 if (status[i].flags & 0x80) continue;
238 if (status[i].type == 0x20) {
239 size = sizeof(status[i].name) + 1;
240 if (size > len) {
241 free(status);
242 return NSD_ERROR;
244 len -= size;
245 strncat(response, status[i].name, size);
246 strncat(response, " ", 1);
247 found = True;
250 response[strlen(response)-1] = '\n';
251 free(status);
253 } else if (StrCaseCmp(map,"hosts.byname") == 0) {
254 if (ip_list = lookup_byname_backend(key, &count)) {
255 for (i = count; i ; i--) {
256 addr = inet_ntoa(ip_list[i-1]);
257 size = strlen(addr) + 1;
258 if (size > len) {
259 free(ip_list);
260 return NSD_ERROR;
262 len -= size;
263 if (i != 0)
264 response[strlen(response)-1] = ' ';
265 strncat(response,addr,size);
266 strncat(response,"\t",1);
268 size = strlen(key) + 1;
269 if (size > len) {
270 free(ip_list);
271 return NSD_ERROR;
273 strncat(response,key,size);
274 strncat(response,"\n",1);
275 found = True;
276 free(ip_list);
280 if (found) {
281 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
282 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
283 return NSD_OK;
285 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
286 rq->f_status = NS_NOTFOUND;
287 return NSD_NEXT;
290 #else
292 /* Allocate some space from the nss static buffer. The buffer and buflen
293 are the pointers passed in by the C library to the _nss_*_*
294 functions. */
296 static char *get_static(char **buffer, size_t *buflen, int len)
298 char *result;
300 /* Error check. We return false if things aren't set up right, or
301 there isn't enough buffer space left. */
303 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
304 return NULL;
307 /* Return an index into the static buffer */
309 result = *buffer;
310 *buffer += len;
311 *buflen -= len;
313 return result;
316 /****************************************************************************
317 gethostbyname() - we ignore any domain portion of the name and only
318 handle names that are at most 15 characters long
319 **************************************************************************/
320 NSS_STATUS
321 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
322 char *buffer, size_t buflen, int *h_errnop)
324 struct in_addr *ip_list;
325 int i, count;
326 fstring name;
327 size_t namelen;
329 memset(he, '\0', sizeof(*he));
330 fstrcpy(name, hostname);
332 /* Do lookup */
334 ip_list = lookup_byname_backend(name, &count);
336 if (!ip_list)
337 return NSS_STATUS_NOTFOUND;
339 /* Copy h_name */
341 namelen = strlen(name) + 1;
343 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
344 free(ip_list);
345 return NSS_STATUS_TRYAGAIN;
348 memcpy(he->h_name, name, namelen);
350 /* Copy h_addr_list, align to pointer boundary first */
352 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
353 i = sizeof(char*) - i;
355 if (get_static(&buffer, &buflen, i) == NULL) {
356 free(ip_list);
357 return NSS_STATUS_TRYAGAIN;
360 if ((he->h_addr_list = (char **)get_static(
361 &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL) {
362 free(ip_list);
363 return NSS_STATUS_TRYAGAIN;
366 for (i = 0; i < count; i++) {
367 if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
368 INADDRSZ)) == NULL) {
369 free(ip_list);
370 return NSS_STATUS_TRYAGAIN;
372 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
375 he->h_addr_list[count] = NULL;
377 free(ip_list);
379 /* Set h_addr_type and h_length */
381 he->h_addrtype = AF_INET;
382 he->h_length = INADDRSZ;
384 /* Set h_aliases */
386 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
387 i = sizeof(char*) - i;
389 if (get_static(&buffer, &buflen, i) == NULL)
390 return NSS_STATUS_TRYAGAIN;
392 if ((he->h_aliases = (char **)get_static(
393 &buffer, &buflen, sizeof(char *))) == NULL)
394 return NSS_STATUS_TRYAGAIN;
396 he->h_aliases[0] = NULL;
398 return NSS_STATUS_SUCCESS;
402 NSS_STATUS
403 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
404 char *buffer, size_t buflen, int *h_errnop)
406 if(af!=AF_INET) {
407 *h_errnop = NO_DATA;
408 return NSS_STATUS_UNAVAIL;
411 return _nss_wins_gethostbyname_r(
412 name, he, buffer, buflen, h_errnop);
414 #endif