ctdb-tests: Fix some incorrect memory allocations
[obnox/samba/samba-obnox.git] / 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 #include "nsswitch/winbind_nss.h"
23 #include "nsswitch/libwbclient/wbclient.h"
24
25 #ifdef HAVE_NS_API_H
26
27 #include <ns_daemon.h>
28 #endif
29
30 #if HAVE_PTHREAD_H
31 #include <pthread.h>
32 #endif
33
34 #if HAVE_PTHREAD
35 static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
36 #endif
37
38 #ifndef INADDRSZ
39 #define INADDRSZ 4
40 #endif
41
42 NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
43                           char *buffer, size_t buflen, int *h_errnop);
44 NSS_STATUS _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
45                            char *buffer, size_t buflen, int *h_errnop);
46
47 static char *lookup_byname_backend(const char *name)
48 {
49         const char *p;
50         char *ip;
51         size_t nbt_len;
52         wbcErr result;
53
54         nbt_len = strlen(name);
55         if (nbt_len > MAX_NETBIOSNAME_LEN - 1) {
56                 return NULL;
57         }
58         p = strchr(name, '.');
59         if (p != NULL) {
60                 return NULL;
61         }
62
63         result = wbcResolveWinsByName(name, &ip);
64         if (result != WBC_ERR_SUCCESS) {
65                 return NULL;
66         }
67
68         return ip;
69 }
70
71 #ifdef HAVE_NS_API_H
72
73 static char *lookup_byaddr_backend(const char *ip)
74 {
75         wbcErr result;
76         char *name = NULL;
77
78         result = wbcResolveWinsByIP(ip, &name);
79         if (result != WBC_ERR_SUCCESS) {
80                 return NULL;
81         }
82
83         return name;
84 }
85
86 /* IRIX version */
87
88 int init(void)
89 {
90         bool ok;
91
92         nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
93
94         ok = nss_wins_init();
95         if (!ok) {
96                 return NSD_ERROR;
97         }
98
99         return NSD_OK;
100 }
101
102 int lookup(nsd_file_t *rq)
103 {
104         char *map;
105         char *key;
106         char *addr;
107         int i, count, len, size;
108         char response[1024];
109         bool found = False;
110
111         nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
112         if (! rq)
113                 return NSD_ERROR;
114
115         map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
116         if (! map) {
117                 rq->f_status = NS_FATAL;
118                 return NSD_ERROR;
119         }
120
121         key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
122         if (! key || ! *key) {
123                 rq->f_status = NS_FATAL;
124                 return NSD_ERROR;
125         }
126
127         response[0] = '\0';
128         len = sizeof(response) - 2;
129
130         /*
131          * response needs to be a string of the following format
132          * ip_address[ ip_address]*\tname[ alias]*
133          */
134         if (strcasecmp_m(map,"hosts.byaddr") == 0) {
135                 char *name;
136
137                 name = lookup_byaddr_backend(key);
138                 if (name != NULL) {
139                         size = strlen(key) + 1;
140                         if (size > len) {
141                                 return NSD_ERROR;
142                         }
143                         len -= size;
144                         strncat(response,key,size);
145                         strncat(response,"\t",1);
146
147                         size = strlen(name) + 1;
148                         if (size > len) {
149                                 return NSD_ERROR;
150                         }
151                         len -= size;
152                         strncat(response, name, size);
153                         strncat(response, " ", 1);
154                         found = True;
155                 }
156                 response[strlen(response)-1] = '\n';
157         } else if (strcasecmp_m(map,"hosts.byname") == 0) {
158                 char *ip;
159
160                 ip = lookup_byname_backend(key);
161                 if (ip != NULL) {
162                         size = strlen(ip) + 1;
163                         if (size > len) {
164                                 wbcFreeMemory(ip);
165                                 return NSD_ERROR;
166                         }
167                         len -= size;
168                         strncat(response,ip,size);
169                         strncat(response,"\t",1);
170                         size = strlen(key) + 1;
171                         wbcFreeMemory(ip);
172                         if (size > len) {
173                                 return NSD_ERROR;
174                         }
175                         strncat(response,key,size);
176                         strncat(response,"\n",1);
177
178                         found = True;
179                 }
180         }
181
182         if (found) {
183             nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
184             nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
185             return NSD_OK;
186         }
187         nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
188         rq->f_status = NS_NOTFOUND;
189         return NSD_NEXT;
190 }
191
192 #else
193
194 /* Allocate some space from the nss static buffer.  The buffer and buflen
195    are the pointers passed in by the C library to the _nss_*_*
196    functions. */
197
198 static char *get_static(char **buffer, size_t *buflen, size_t len)
199 {
200         char *result;
201
202         /* Error check.  We return false if things aren't set up right, or
203            there isn't enough buffer space left. */
204
205         if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
206                 return NULL;
207         }
208
209         /* Return an index into the static buffer */
210
211         result = *buffer;
212         *buffer += len;
213         *buflen -= len;
214
215         return result;
216 }
217
218 /****************************************************************************
219 gethostbyname() - we ignore any domain portion of the name and only
220 handle names that are at most 15 characters long
221   **************************************************************************/
222 NSS_STATUS
223 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
224                           char *buffer, size_t buflen, int *h_errnop)
225 {
226         NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
227         char *ip;
228         struct in_addr in;
229         int i;
230         fstring name;
231         size_t namelen;
232         int rc;
233
234 #if HAVE_PTHREAD
235         pthread_mutex_lock(&wins_nss_mutex);
236 #endif
237
238         memset(he, '\0', sizeof(*he));
239         fstrcpy(name, hostname);
240
241         /* Do lookup */
242
243         ip = lookup_byname_backend(name);
244         if (ip == NULL) {
245                 nss_status = NSS_STATUS_NOTFOUND;
246                 goto out;
247         }
248
249         rc = inet_pton(AF_INET, ip, &in);
250         wbcFreeMemory(ip);
251         if (rc == 0) {
252                 nss_status = NSS_STATUS_TRYAGAIN;
253                 goto out;
254         }
255
256         /* Copy h_name */
257
258         namelen = strlen(name) + 1;
259
260         if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
261                 nss_status = NSS_STATUS_TRYAGAIN;
262                 goto out;
263         }
264
265         memcpy(he->h_name, name, namelen);
266
267         /* Copy h_addr_list, align to pointer boundary first */
268
269         if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
270                 i = sizeof(char*) - i;
271
272         if (get_static(&buffer, &buflen, i) == NULL) {
273                 nss_status = NSS_STATUS_TRYAGAIN;
274                 goto out;
275         }
276
277         if ((he->h_addr_list = (char **)get_static(
278                      &buffer, &buflen, i * sizeof(char *))) == NULL) {
279                 nss_status = NSS_STATUS_TRYAGAIN;
280                 goto out;
281         }
282
283         if ((he->h_addr_list[0] = get_static(&buffer, &buflen,
284                                              INADDRSZ)) == NULL) {
285                 nss_status = NSS_STATUS_TRYAGAIN;
286                 goto out;
287         }
288
289         memcpy(he->h_addr_list[i], &in, INADDRSZ);
290
291         he->h_addr_list[0] = NULL;
292
293         /* Set h_addr_type and h_length */
294
295         he->h_addrtype = AF_INET;
296         he->h_length = INADDRSZ;
297
298         /* Set h_aliases */
299
300         if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
301                 i = sizeof(char*) - i;
302
303         if (get_static(&buffer, &buflen, i) == NULL) {
304                 nss_status = NSS_STATUS_TRYAGAIN;
305                 goto out;
306         }
307
308         if ((he->h_aliases = (char **)get_static(
309                      &buffer, &buflen, sizeof(char *))) == NULL) {
310                 nss_status = NSS_STATUS_TRYAGAIN;
311                 goto out;
312         }
313
314         he->h_aliases[0] = NULL;
315
316         nss_status = NSS_STATUS_SUCCESS;
317
318   out:
319
320 #if HAVE_PTHREAD
321         pthread_mutex_unlock(&wins_nss_mutex);
322 #endif
323         return nss_status;
324 }
325
326
327 NSS_STATUS
328 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
329                            char *buffer, size_t buflen, int *h_errnop)
330 {
331         NSS_STATUS nss_status;
332
333         if(af!=AF_INET) {
334                 *h_errnop = NO_DATA;
335                 nss_status = NSS_STATUS_UNAVAIL;
336         } else {
337                 nss_status = _nss_wins_gethostbyname_r(
338                                 name, he, buffer, buflen, h_errnop);
339         }
340         return nss_status;
341 }
342 #endif