Coverity fixes
[samba.git] / source3 / nsswitch / wins.c
1 /* 
2    Unix SMB/CIFS implementation.
3    a WINS nsswitch module 
4    Copyright (C) Andrew Tridgell 1999
5    
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.
10    
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.
15    
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/>.
18    
19 */
20
21 #include "includes.h"
22 #ifdef HAVE_NS_API_H
23 #undef VOLATILE
24
25 #include <ns_daemon.h>
26 #endif
27
28 #ifndef INADDRSZ
29 #define INADDRSZ 4
30 #endif
31
32 static int initialised;
33
34 extern bool AllowDebugChange;
35
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);
40
41 /* Use our own create socket code so we don't recurse.... */
42
43 static int wins_lookup_open_socket_in(void)
44 {
45         struct sockaddr_in sock;
46         int val=1;
47         int res;
48
49         memset((char *)&sock,'\0',sizeof(sock));
50
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;
60
61         if (setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) != 0) {
62                 close(res);
63                 return -1;
64         }
65 #ifdef SO_REUSEPORT
66         if (setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) != 0) {
67                 close(res);
68                 return -1;
69         }
70 #endif /* SO_REUSEPORT */
71
72         /* now we've got a socket - we need to bind it */
73
74         if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
75                 close(res);
76                 return(-1);
77         }
78
79         set_socket_options(res,"SO_BROADCAST");
80
81         return res;
82 }
83
84
85 static void nss_wins_init(void)
86 {
87         initialised = 1;
88         DEBUGLEVEL = 0;
89         AllowDebugChange = False;
90
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();
96 }
97
98 static struct in_addr *lookup_byname_backend(const char *name, int *count)
99 {
100         int fd = -1;
101         struct ip_service *address = NULL;
102         struct in_addr *ret = NULL;
103         int j, flags = 0;
104
105         if (!initialised) {
106                 nss_wins_init();
107         }
108
109         *count = 0;
110
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;
116                 }
117                 if (address[0].ss.ss_family != AF_INET) {
118                         free(address);
119                         free(ret);
120                         return NULL;
121                 }
122                 *ret = ((struct sockaddr_in *)&address[0].ss)->sin_addr;
123                 free( address );
124                 return ret;
125         }
126
127         fd = wins_lookup_open_socket_in();
128         if (fd == -1) {
129                 return NULL;
130         }
131
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;
139                 }
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                         *ret = ((struct sockaddr_in *)pss)->sin_addr;
144                         break;
145                 }
146         }
147
148         close(fd);
149         return ret;
150 }
151
152 #ifdef HAVE_NS_API_H
153
154 static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
155 {
156         int fd;
157         struct sockaddr_storage ss;
158         struct nmb_name nname;
159         NODE_STATUS_STRUCT *status;
160
161         if (!initialised) {
162                 nss_wins_init();
163         }
164
165         fd = wins_lookup_open_socket_in();
166         if (fd == -1)
167                 return NULL;
168
169         make_nmb_name(&nname, "*", 0);
170         if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
171                 return NULL;
172         }
173         status = node_status_query(fd, &nname, &ss, count, NULL);
174
175         close(fd);
176         return status;
177 }
178
179 /* IRIX version */
180
181 int init(void)
182 {
183         nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
184         nss_wins_init();
185         return NSD_OK;
186 }
187
188 int lookup(nsd_file_t *rq)
189 {
190         char *map;
191         char *key;
192         char *addr;
193         struct in_addr *ip_list;
194         NODE_STATUS_STRUCT *status;
195         int i, count, len, size;
196         char response[1024];
197         bool found = False;
198
199         nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
200         if (! rq) 
201                 return NSD_ERROR;
202
203         map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
204         if (! map) {
205                 rq->f_status = NS_FATAL;
206                 return NSD_ERROR;
207         }
208
209         key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
210         if (! key || ! *key) {
211                 rq->f_status = NS_FATAL;
212                 return NSD_ERROR;
213         }
214
215         response[0] = '\0';
216         len = sizeof(response) - 2;
217
218         /* 
219          * response needs to be a string of the following format
220          * ip_address[ ip_address]*\tname[ alias]*
221          */
222         if (StrCaseCmp(map,"hosts.byaddr") == 0) {
223                 if ( status = lookup_byaddr_backend(key, &count)) {
224                     size = strlen(key) + 1;
225                     if (size > len) {
226                         free(status);
227                         return NSD_ERROR;
228                     }
229                     len -= size;
230                     strncat(response,key,size);
231                     strncat(response,"\t",1);
232                     for (i = 0; i < count; i++) {
233                         /* ignore group names */
234                         if (status[i].flags & 0x80) continue;
235                         if (status[i].type == 0x20) {
236                                 size = sizeof(status[i].name) + 1;
237                                 if (size > len) {
238                                     free(status);
239                                     return NSD_ERROR;
240                                 }
241                                 len -= size;
242                                 strncat(response, status[i].name, size);
243                                 strncat(response, " ", 1);
244                                 found = True;
245                         }
246                     }
247                     response[strlen(response)-1] = '\n';
248                     free(status);
249                 }
250         } else if (StrCaseCmp(map,"hosts.byname") == 0) {
251             if (ip_list = lookup_byname_backend(key, &count)) {
252                 for (i = count; i ; i--) {
253                     addr = inet_ntoa(ip_list[i-1]);
254                     size = strlen(addr) + 1;
255                     if (size > len) {
256                         free(ip_list);
257                         return NSD_ERROR;
258                     }
259                     len -= size;
260                     if (i != 0)
261                         response[strlen(response)-1] = ' ';
262                     strncat(response,addr,size);
263                     strncat(response,"\t",1);
264                 }
265                 size = strlen(key) + 1;
266                 if (size > len) {
267                     free(ip_list);
268                     return NSD_ERROR;
269                 }   
270                 strncat(response,key,size);
271                 strncat(response,"\n",1);
272                 found = True;
273                 free(ip_list);
274             }
275         }
276
277         if (found) {
278             nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
279             nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
280             return NSD_OK;
281         }
282         nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
283         rq->f_status = NS_NOTFOUND;
284         return NSD_NEXT;
285 }
286
287 #else
288
289 /* Allocate some space from the nss static buffer.  The buffer and buflen
290    are the pointers passed in by the C library to the _nss_*_*
291    functions. */
292
293 static char *get_static(char **buffer, size_t *buflen, int len)
294 {
295         char *result;
296
297         /* Error check.  We return false if things aren't set up right, or
298            there isn't enough buffer space left. */
299         
300         if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
301                 return NULL;
302         }
303
304         /* Return an index into the static buffer */
305
306         result = *buffer;
307         *buffer += len;
308         *buflen -= len;
309
310         return result;
311 }
312
313 /****************************************************************************
314 gethostbyname() - we ignore any domain portion of the name and only
315 handle names that are at most 15 characters long
316   **************************************************************************/
317 NSS_STATUS
318 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
319                           char *buffer, size_t buflen, int *h_errnop)
320 {
321         struct in_addr *ip_list;
322         int i, count;
323         fstring name;
324         size_t namelen;
325                 
326         memset(he, '\0', sizeof(*he));
327         fstrcpy(name, hostname);
328
329         /* Do lookup */
330
331         ip_list = lookup_byname_backend(name, &count);
332
333         if (!ip_list)
334                 return NSS_STATUS_NOTFOUND;
335
336         /* Copy h_name */
337
338         namelen = strlen(name) + 1;
339
340         if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
341                 free(ip_list);
342                 return NSS_STATUS_TRYAGAIN;
343         }
344
345         memcpy(he->h_name, name, namelen);
346
347         /* Copy h_addr_list, align to pointer boundary first */
348
349         if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
350                 i = sizeof(char*) - i;
351
352         if (get_static(&buffer, &buflen, i) == NULL) {
353                 free(ip_list);
354                 return NSS_STATUS_TRYAGAIN;
355         }
356
357         if ((he->h_addr_list = (char **)get_static(
358                      &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL) {
359                 free(ip_list);
360                 return NSS_STATUS_TRYAGAIN;
361         }
362
363         for (i = 0; i < count; i++) {
364                 if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
365                                                      INADDRSZ)) == NULL) {
366                         free(ip_list);
367                         return NSS_STATUS_TRYAGAIN;
368                 }
369                 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
370         }
371
372         he->h_addr_list[count] = NULL;
373
374         free(ip_list);
375
376         /* Set h_addr_type and h_length */
377
378         he->h_addrtype = AF_INET;
379         he->h_length = INADDRSZ;
380
381         /* Set h_aliases */
382
383         if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
384                 i = sizeof(char*) - i;
385
386         if (get_static(&buffer, &buflen, i) == NULL)
387                 return NSS_STATUS_TRYAGAIN;
388
389         if ((he->h_aliases = (char **)get_static(
390                      &buffer, &buflen, sizeof(char *))) == NULL)
391                 return NSS_STATUS_TRYAGAIN;
392
393         he->h_aliases[0] = NULL;
394
395         return NSS_STATUS_SUCCESS;
396 }
397
398
399 NSS_STATUS
400 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
401                            char *buffer, size_t buflen, int *h_errnop)
402 {
403         if(af!=AF_INET) {
404                 *h_errnop = NO_DATA;
405                 return NSS_STATUS_UNAVAIL;
406         }
407
408         return _nss_wins_gethostbyname_r(
409                 name, he, buffer, buflen, h_errnop);
410 }
411 #endif