r24267: Fix the build farm
[Samba/gebeck_regimport.git] / source3 / nsswitch / wins.c
blobd54134d01f46524a812992ee0b0f519000ff7437
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 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
62 #ifdef SO_REUSEPORT
63 setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val));
64 #endif /* SO_REUSEPORT */
66 /* now we've got a socket - we need to bind it */
68 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
69 close(res);
70 return(-1);
73 set_socket_options(res,"SO_BROADCAST");
75 return res;
79 static void nss_wins_init(void)
81 initialised = 1;
82 DEBUGLEVEL = 0;
83 AllowDebugChange = False;
85 TimeInit();
86 setup_logging("nss_wins",False);
87 load_case_tables();
88 lp_load(dyn_CONFIGFILE,True,False,False,True);
89 load_interfaces();
92 static struct in_addr *lookup_byname_backend(const char *name, int *count)
94 int fd = -1;
95 struct ip_service *address = NULL;
96 struct in_addr *ret = NULL;
97 int j, flags = 0;
99 if (!initialised) {
100 nss_wins_init();
103 *count = 0;
105 /* always try with wins first */
106 if (resolve_wins(name,0x00,&address,count)) {
107 if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
108 free( address );
109 return NULL;
111 *ret = address[0].ip;
112 free( address );
113 return ret;
116 fd = wins_lookup_open_socket_in();
117 if (fd == -1) {
118 return NULL;
121 /* uggh, we have to broadcast to each interface in turn */
122 for (j=iface_count() - 1;j >= 0;j--) {
123 struct in_addr *bcast = iface_n_bcast(j);
124 ret = name_query(fd,name,0x00,True,True,*bcast,count, &flags, NULL);
125 if (ret) break;
128 close(fd);
129 return ret;
132 #ifdef HAVE_NS_API_H
134 static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
136 int fd;
137 struct in_addr ip;
138 struct nmb_name nname;
139 NODE_STATUS_STRUCT *status;
141 if (!initialised) {
142 nss_wins_init();
145 fd = wins_lookup_open_socket_in();
146 if (fd == -1)
147 return NULL;
149 make_nmb_name(&nname, "*", 0);
150 ip = *interpret_addr2(addr);
151 status = node_status_query(fd,&nname,ip, count, NULL);
153 close(fd);
154 return status;
157 /* IRIX version */
159 int init(void)
161 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
162 nss_wins_init();
163 return NSD_OK;
166 int lookup(nsd_file_t *rq)
168 char *map;
169 char *key;
170 char *addr;
171 struct in_addr *ip_list;
172 NODE_STATUS_STRUCT *status;
173 int i, count, len, size;
174 char response[1024];
175 BOOL found = False;
177 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
178 if (! rq)
179 return NSD_ERROR;
181 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
182 if (! map) {
183 rq->f_status = NS_FATAL;
184 return NSD_ERROR;
187 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
188 if (! key || ! *key) {
189 rq->f_status = NS_FATAL;
190 return NSD_ERROR;
193 response[0] = '\0';
194 len = sizeof(response) - 2;
197 * response needs to be a string of the following format
198 * ip_address[ ip_address]*\tname[ alias]*
200 if (StrCaseCmp(map,"hosts.byaddr") == 0) {
201 if ( status = lookup_byaddr_backend(key, &count)) {
202 size = strlen(key) + 1;
203 if (size > len) {
204 free(status);
205 return NSD_ERROR;
207 len -= size;
208 strncat(response,key,size);
209 strncat(response,"\t",1);
210 for (i = 0; i < count; i++) {
211 /* ignore group names */
212 if (status[i].flags & 0x80) continue;
213 if (status[i].type == 0x20) {
214 size = sizeof(status[i].name) + 1;
215 if (size > len) {
216 free(status);
217 return NSD_ERROR;
219 len -= size;
220 strncat(response, status[i].name, size);
221 strncat(response, " ", 1);
222 found = True;
225 response[strlen(response)-1] = '\n';
226 free(status);
228 } else if (StrCaseCmp(map,"hosts.byname") == 0) {
229 if (ip_list = lookup_byname_backend(key, &count)) {
230 for (i = count; i ; i--) {
231 addr = inet_ntoa(ip_list[i-1]);
232 size = strlen(addr) + 1;
233 if (size > len) {
234 free(ip_list);
235 return NSD_ERROR;
237 len -= size;
238 if (i != 0)
239 response[strlen(response)-1] = ' ';
240 strncat(response,addr,size);
241 strncat(response,"\t",1);
243 size = strlen(key) + 1;
244 if (size > len) {
245 free(ip_list);
246 return NSD_ERROR;
248 strncat(response,key,size);
249 strncat(response,"\n",1);
250 found = True;
251 free(ip_list);
255 if (found) {
256 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
257 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
258 return NSD_OK;
260 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
261 rq->f_status = NS_NOTFOUND;
262 return NSD_NEXT;
265 #else
267 /* Allocate some space from the nss static buffer. The buffer and buflen
268 are the pointers passed in by the C library to the _nss_*_*
269 functions. */
271 static char *get_static(char **buffer, size_t *buflen, int len)
273 char *result;
275 /* Error check. We return false if things aren't set up right, or
276 there isn't enough buffer space left. */
278 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
279 return NULL;
282 /* Return an index into the static buffer */
284 result = *buffer;
285 *buffer += len;
286 *buflen -= len;
288 return result;
291 /****************************************************************************
292 gethostbyname() - we ignore any domain portion of the name and only
293 handle names that are at most 15 characters long
294 **************************************************************************/
295 NSS_STATUS
296 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
297 char *buffer, size_t buflen, int *h_errnop)
299 struct in_addr *ip_list;
300 int i, count;
301 fstring name;
302 size_t namelen;
304 memset(he, '\0', sizeof(*he));
305 fstrcpy(name, hostname);
307 /* Do lookup */
309 ip_list = lookup_byname_backend(name, &count);
311 if (!ip_list)
312 return NSS_STATUS_NOTFOUND;
314 /* Copy h_name */
316 namelen = strlen(name) + 1;
318 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL)
319 return NSS_STATUS_TRYAGAIN;
321 memcpy(he->h_name, name, namelen);
323 /* Copy h_addr_list, align to pointer boundary first */
325 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
326 i = sizeof(char*) - i;
328 if (get_static(&buffer, &buflen, i) == NULL)
329 return NSS_STATUS_TRYAGAIN;
331 if ((he->h_addr_list = (char **)get_static(
332 &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL)
333 return NSS_STATUS_TRYAGAIN;
335 for (i = 0; i < count; i++) {
336 if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
337 INADDRSZ)) == NULL)
338 return NSS_STATUS_TRYAGAIN;
339 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
342 he->h_addr_list[count] = NULL;
344 if (ip_list)
345 free(ip_list);
347 /* Set h_addr_type and h_length */
349 he->h_addrtype = AF_INET;
350 he->h_length = INADDRSZ;
352 /* Set h_aliases */
354 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
355 i = sizeof(char*) - i;
357 if (get_static(&buffer, &buflen, i) == NULL)
358 return NSS_STATUS_TRYAGAIN;
360 if ((he->h_aliases = (char **)get_static(
361 &buffer, &buflen, sizeof(char *))) == NULL)
362 return NSS_STATUS_TRYAGAIN;
364 he->h_aliases[0] = NULL;
366 return NSS_STATUS_SUCCESS;
370 NSS_STATUS
371 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
372 char *buffer, size_t buflen, int *h_errnop)
374 if(af!=AF_INET) {
375 *h_errnop = NO_DATA;
376 return NSS_STATUS_UNAVAIL;
379 return _nss_wins_gethostbyname_r(
380 name, he, buffer, buflen, h_errnop);
382 #endif