don't dereference null pointer
[Samba/gebeck_regimport.git] / source4 / nsswitch / wins.c
blobd5791d7af50eb681e65b6e574fece1ea41e4bf05
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",DEBUG_FILE);
83 lp_load(dyn_CONFIGFILE,True,False,False);
84 load_interfaces();
87 static struct node_status *lookup_byaddr_backend(char *addr, int *count)
89 int fd;
90 struct in_addr ip;
91 struct nmb_name nname;
92 struct node_status *status;
94 if (!initialised) {
95 nss_wins_init();
98 fd = wins_lookup_open_socket_in();
99 if (fd == -1)
100 return NULL;
102 make_nmb_name(&nname, "*", 0);
103 ip = *interpret_addr2(addr);
104 status = node_status_query(fd,&nname,ip, count);
106 close(fd);
107 return status;
110 static struct in_addr *lookup_byname_backend(const char *name, int *count)
112 int fd = -1;
113 struct in_addr *ret = NULL;
114 struct in_addr p;
115 int j, flags = 0;
117 if (!initialised) {
118 nss_wins_init();
121 *count = 0;
123 /* always try with wins first */
124 if (resolve_wins(name,0x20,&ret,count)) {
125 return ret;
128 fd = wins_lookup_open_socket_in();
129 if (fd == -1) {
130 return NULL;
133 /* uggh, we have to broadcast to each interface in turn */
134 for (j=iface_count() - 1;j >= 0;j--) {
135 struct in_addr *bcast = iface_n_bcast(j);
136 ret = name_query(fd,name,0x20,True,True,*bcast,count, &flags, NULL);
137 if (ret) break;
140 out:
141 close(fd);
142 return ret;
146 #ifdef HAVE_NS_API_H
147 /* IRIX version */
149 int init(void)
151 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
152 nss_wins_init();
153 return NSD_OK;
156 int lookup(nsd_file_t *rq)
158 char *map;
159 char *key;
160 char *addr;
161 struct in_addr *ip_list;
162 struct node_status *status;
163 int i, count, len, size;
164 char response[1024];
165 BOOL found = False;
167 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
168 if (! rq)
169 return NSD_ERROR;
171 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
172 if (! map) {
173 rq->f_status = NS_FATAL;
174 return NSD_ERROR;
177 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
178 if (! key || ! *key) {
179 rq->f_status = NS_FATAL;
180 return NSD_ERROR;
183 response[0] = '\0';
184 len = sizeof(response) - 2;
187 * response needs to be a string of the following format
188 * ip_address[ ip_address]*\tname[ alias]*
190 if (strcasecmp(map,"hosts.byaddr") == 0) {
191 if ( status = lookup_byaddr_backend(key, &count)) {
192 size = strlen(key) + 1;
193 if (size > len) {
194 free(status);
195 return NSD_ERROR;
197 len -= size;
198 strncat(response,key,size);
199 strncat(response,"\t",1);
200 for (i = 0; i < count; i++) {
201 /* ignore group names */
202 if (status[i].flags & 0x80) continue;
203 if (status[i].type == 0x20) {
204 size = sizeof(status[i].name) + 1;
205 if (size > len) {
206 free(status);
207 return NSD_ERROR;
209 len -= size;
210 strncat(response, status[i].name, size);
211 strncat(response, " ", 1);
212 found = True;
215 response[strlen(response)-1] = '\n';
216 free(status);
218 } else if (strcasecmp(map,"hosts.byname") == 0) {
219 if (ip_list = lookup_byname_backend(key, &count)) {
220 for (i = count; i ; i--) {
221 addr = inet_ntoa(ip_list[i-1]);
222 size = strlen(addr) + 1;
223 if (size > len) {
224 free(ip_list);
225 return NSD_ERROR;
227 len -= size;
228 if (i != 0)
229 response[strlen(response)-1] = ' ';
230 strncat(response,addr,size);
231 strncat(response,"\t",1);
233 size = strlen(key) + 1;
234 if (size > len) {
235 free(ip_list);
236 return NSD_ERROR;
238 strncat(response,key,size);
239 strncat(response,"\n",1);
240 found = True;
241 free(ip_list);
245 if (found) {
246 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
247 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
248 return NSD_OK;
250 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
251 rq->f_status = NS_NOTFOUND;
252 return NSD_NEXT;
255 #else
256 /****************************************************************************
257 gethostbyname() - we ignore any domain portion of the name and only
258 handle names that are at most 15 characters long
259 **************************************************************************/
260 NSS_STATUS
261 _nss_wins_gethostbyname_r(const char *name, struct hostent *he,
262 char *buffer, size_t buflen, int *errnop,
263 int *h_errnop)
265 char **host_addresses;
266 struct in_addr *ip_list;
267 int i, count;
268 size_t namelen = strlen(name) + 1;
270 memset(he, '\0', sizeof(*he));
272 ip_list = lookup_byname_backend(name, &count);
273 if (!ip_list) {
274 return NSS_STATUS_NOTFOUND;
277 if (buflen < namelen + (2*count+1)*INADDRSZ) {
278 /* no ENOMEM error type?! */
279 return NSS_STATUS_NOTFOUND;
283 host_addresses = (char **)buffer;
284 he->h_addr_list = host_addresses;
285 host_addresses[count] = NULL;
286 buffer += (count + 1) * INADDRSZ;
287 buflen += (count + 1) * INADDRSZ;
288 he->h_addrtype = AF_INET;
289 he->h_length = INADDRSZ;
291 for (i=0;i<count;i++) {
292 memcpy(buffer, &ip_list[i].s_addr, INADDRSZ);
293 *host_addresses = buffer;
294 buffer += INADDRSZ;
295 buflen -= INADDRSZ;
296 host_addresses++;
299 if (ip_list)
300 free(ip_list);
302 memcpy(buffer, name, namelen);
303 he->h_name = buffer;
305 return NSS_STATUS_SUCCESS;
309 NSS_STATUS
310 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
311 char *buffer, size_t buflen, int *errnop,
312 int *h_errnop)
314 if(af!=AF_INET) {
315 *h_errnop = NO_DATA;
316 *errnop = EAFNOSUPPORT;
317 return NSS_STATUS_UNAVAIL;
320 return _nss_wins_gethostbyname_r(name,he,buffer,buflen,errnop,h_errnop);
322 #endif