Fix a memleak
[Samba.git] / source / nsswitch / wins.c
blobe74cfaf69f2a0591c0c27664c48b221a834063f8
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(get_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 (NT_STATUS_IS_OK(resolve_wins(name,0x00,&address,count))) {
107 if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
108 free( address );
109 return NULL;
111 if (address[0].ss.ss_family != AF_INET) {
112 free(address);
113 free(ret);
114 return NULL;
116 *ret = ((struct sockaddr_in *)&address[0].ss)->sin_addr;
117 free( address );
118 return ret;
121 fd = wins_lookup_open_socket_in();
122 if (fd == -1) {
123 return NULL;
126 /* uggh, we have to broadcast to each interface in turn */
127 for (j=iface_count() - 1;j >= 0;j--) {
128 const struct in_addr *bcast = iface_n_bcast_v4(j);
129 struct sockaddr_storage ss;
130 struct sockaddr_storage *pss;
131 if (!bcast) {
132 continue;
134 in_addr_to_sockaddr_storage(&ss, *bcast);
135 pss = name_query(fd,name,0x00,True,True,&ss,count, &flags, NULL);
136 if (pss) {
137 *ret = ((struct sockaddr_in *)pss)->sin_addr;
138 break;
142 close(fd);
143 return ret;
146 #ifdef HAVE_NS_API_H
148 static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
150 int fd;
151 struct sockaddr_storage ss;
152 struct nmb_name nname;
153 NODE_STATUS_STRUCT *status;
155 if (!initialised) {
156 nss_wins_init();
159 fd = wins_lookup_open_socket_in();
160 if (fd == -1)
161 return NULL;
163 make_nmb_name(&nname, "*", 0);
164 if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
165 return NULL;
167 status = node_status_query(fd, &nname, &ss, count, NULL);
169 close(fd);
170 return status;
173 /* IRIX version */
175 int init(void)
177 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
178 nss_wins_init();
179 return NSD_OK;
182 int lookup(nsd_file_t *rq)
184 char *map;
185 char *key;
186 char *addr;
187 struct in_addr *ip_list;
188 NODE_STATUS_STRUCT *status;
189 int i, count, len, size;
190 char response[1024];
191 bool found = False;
193 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
194 if (! rq)
195 return NSD_ERROR;
197 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
198 if (! map) {
199 rq->f_status = NS_FATAL;
200 return NSD_ERROR;
203 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
204 if (! key || ! *key) {
205 rq->f_status = NS_FATAL;
206 return NSD_ERROR;
209 response[0] = '\0';
210 len = sizeof(response) - 2;
213 * response needs to be a string of the following format
214 * ip_address[ ip_address]*\tname[ alias]*
216 if (StrCaseCmp(map,"hosts.byaddr") == 0) {
217 if ( status = lookup_byaddr_backend(key, &count)) {
218 size = strlen(key) + 1;
219 if (size > len) {
220 free(status);
221 return NSD_ERROR;
223 len -= size;
224 strncat(response,key,size);
225 strncat(response,"\t",1);
226 for (i = 0; i < count; i++) {
227 /* ignore group names */
228 if (status[i].flags & 0x80) continue;
229 if (status[i].type == 0x20) {
230 size = sizeof(status[i].name) + 1;
231 if (size > len) {
232 free(status);
233 return NSD_ERROR;
235 len -= size;
236 strncat(response, status[i].name, size);
237 strncat(response, " ", 1);
238 found = True;
241 response[strlen(response)-1] = '\n';
242 free(status);
244 } else if (StrCaseCmp(map,"hosts.byname") == 0) {
245 if (ip_list = lookup_byname_backend(key, &count)) {
246 for (i = count; i ; i--) {
247 addr = inet_ntoa(ip_list[i-1]);
248 size = strlen(addr) + 1;
249 if (size > len) {
250 free(ip_list);
251 return NSD_ERROR;
253 len -= size;
254 if (i != 0)
255 response[strlen(response)-1] = ' ';
256 strncat(response,addr,size);
257 strncat(response,"\t",1);
259 size = strlen(key) + 1;
260 if (size > len) {
261 free(ip_list);
262 return NSD_ERROR;
264 strncat(response,key,size);
265 strncat(response,"\n",1);
266 found = True;
267 free(ip_list);
271 if (found) {
272 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
273 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
274 return NSD_OK;
276 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
277 rq->f_status = NS_NOTFOUND;
278 return NSD_NEXT;
281 #else
283 /* Allocate some space from the nss static buffer. The buffer and buflen
284 are the pointers passed in by the C library to the _nss_*_*
285 functions. */
287 static char *get_static(char **buffer, size_t *buflen, int len)
289 char *result;
291 /* Error check. We return false if things aren't set up right, or
292 there isn't enough buffer space left. */
294 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
295 return NULL;
298 /* Return an index into the static buffer */
300 result = *buffer;
301 *buffer += len;
302 *buflen -= len;
304 return result;
307 /****************************************************************************
308 gethostbyname() - we ignore any domain portion of the name and only
309 handle names that are at most 15 characters long
310 **************************************************************************/
311 NSS_STATUS
312 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
313 char *buffer, size_t buflen, int *h_errnop)
315 struct in_addr *ip_list;
316 int i, count;
317 fstring name;
318 size_t namelen;
320 memset(he, '\0', sizeof(*he));
321 fstrcpy(name, hostname);
323 /* Do lookup */
325 ip_list = lookup_byname_backend(name, &count);
327 if (!ip_list)
328 return NSS_STATUS_NOTFOUND;
330 /* Copy h_name */
332 namelen = strlen(name) + 1;
334 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
335 free(ip_list);
336 return NSS_STATUS_TRYAGAIN;
339 memcpy(he->h_name, name, namelen);
341 /* Copy h_addr_list, align to pointer boundary first */
343 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
344 i = sizeof(char*) - i;
346 if (get_static(&buffer, &buflen, i) == NULL) {
347 free(ip_list);
348 return NSS_STATUS_TRYAGAIN;
351 if ((he->h_addr_list = (char **)get_static(
352 &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL) {
353 free(ip_list);
354 return NSS_STATUS_TRYAGAIN;
357 for (i = 0; i < count; i++) {
358 if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
359 INADDRSZ)) == NULL) {
360 free(ip_list);
361 return NSS_STATUS_TRYAGAIN;
363 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
366 he->h_addr_list[count] = NULL;
368 free(ip_list);
370 /* Set h_addr_type and h_length */
372 he->h_addrtype = AF_INET;
373 he->h_length = INADDRSZ;
375 /* Set h_aliases */
377 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
378 i = sizeof(char*) - i;
380 if (get_static(&buffer, &buflen, i) == NULL)
381 return NSS_STATUS_TRYAGAIN;
383 if ((he->h_aliases = (char **)get_static(
384 &buffer, &buflen, sizeof(char *))) == NULL)
385 return NSS_STATUS_TRYAGAIN;
387 he->h_aliases[0] = NULL;
389 return NSS_STATUS_SUCCESS;
393 NSS_STATUS
394 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
395 char *buffer, size_t buflen, int *h_errnop)
397 if(af!=AF_INET) {
398 *h_errnop = NO_DATA;
399 return NSS_STATUS_UNAVAIL;
402 return _nss_wins_gethostbyname_r(
403 name, he, buffer, buflen, h_errnop);
405 #endif