ctdb-build: Separate test backtrace support into separate subsystem
[Samba.git] / nsswitch / wins.c
blobe202a45e26e874adcccd38abf8fca11e1ded2b85
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 #include "nsswitch/winbind_client.h"
23 #include "nsswitch/libwbclient/wbclient.h"
24 #include "lib/util/string_wrappers.h"
26 #ifdef HAVE_NS_API_H
28 #include <ns_daemon.h>
29 #endif
31 #ifdef HAVE_PTHREAD_H
32 #include <pthread.h>
33 #endif
35 #ifdef HAVE_PTHREAD
36 static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
37 #endif
39 #ifndef INADDRSZ
40 #define INADDRSZ 4
41 #endif
43 _PUBLIC_ON_LINUX_
44 NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname,
45 struct hostent *he,
46 char *buffer,
47 size_t buflen,
48 int *errnop,
49 int *h_errnop);
50 NSS_STATUS _nss_wins_gethostbyname2_r(const char *name,
51 int af,
52 struct hostent *he,
53 char *buffer,
54 size_t buflen,
55 int *errnop,
56 int *h_errnop);
58 static char *lookup_byname_backend(const char *name)
60 const char *p;
61 char *ip, *ipp;
62 size_t nbt_len;
63 wbcErr result;
65 nbt_len = strlen(name);
66 if (nbt_len > MAX_NETBIOSNAME_LEN - 1) {
67 return NULL;
69 p = strchr(name, '.');
70 if (p != NULL) {
71 return NULL;
74 wbcSetClientProcessName("nss_wins");
75 result = wbcResolveWinsByName(name, &ip);
76 if (result != WBC_ERR_SUCCESS) {
77 return NULL;
80 ipp = strchr(ip, '\t');
81 if (ipp != NULL) {
82 *ipp = '\0';
85 return ip;
88 #ifdef HAVE_NS_API_H
90 static char *lookup_byaddr_backend(const char *ip)
92 wbcErr result;
93 char *name = NULL;
95 wbcSetClientProcessName("nss_wins");
96 result = wbcResolveWinsByIP(ip, &name);
97 if (result != WBC_ERR_SUCCESS) {
98 return NULL;
101 return name;
104 /* IRIX version */
106 int init(void)
108 bool ok;
110 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
112 ok = nss_wins_init();
113 if (!ok) {
114 return NSD_ERROR;
117 return NSD_OK;
120 int lookup(nsd_file_t *rq)
122 char *map;
123 char *key;
124 char *addr;
125 int i, count, len, size;
126 char response[1024];
127 bool found = False;
129 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
130 if (! rq)
131 return NSD_ERROR;
133 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
134 if (! map) {
135 rq->f_status = NS_FATAL;
136 return NSD_ERROR;
139 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
140 if (! key || ! *key) {
141 rq->f_status = NS_FATAL;
142 return NSD_ERROR;
145 response[0] = '\0';
146 len = sizeof(response) - 2;
149 * response needs to be a string of the following format
150 * ip_address[ ip_address]*\tname[ alias]*
152 if (strcasecmp_m(map,"hosts.byaddr") == 0) {
153 char *name;
155 name = lookup_byaddr_backend(key);
156 if (name != NULL) {
157 size = strlen(key) + 1;
158 if (size > len) {
159 return NSD_ERROR;
161 len -= size;
162 strncat(response,key,size);
163 strncat(response,"\t",1);
165 size = strlen(name) + 1;
166 if (size > len) {
167 return NSD_ERROR;
169 len -= size;
170 strncat(response, name, size);
171 strncat(response, " ", 1);
172 found = True;
174 response[strlen(response)-1] = '\n';
175 } else if (strcasecmp_m(map,"hosts.byname") == 0) {
176 char *ip;
178 ip = lookup_byname_backend(key);
179 if (ip != NULL) {
180 size = strlen(ip) + 1;
181 if (size > len) {
182 wbcFreeMemory(ip);
183 return NSD_ERROR;
185 len -= size;
186 strncat(response,ip,size);
187 strncat(response,"\t",1);
188 size = strlen(key) + 1;
189 wbcFreeMemory(ip);
190 if (size > len) {
191 return NSD_ERROR;
193 strncat(response,key,size);
194 strncat(response,"\n",1);
196 found = True;
200 if (found) {
201 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
202 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
203 return NSD_OK;
205 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
206 rq->f_status = NS_NOTFOUND;
207 return NSD_NEXT;
210 #else
212 /* Allocate some space from the nss static buffer. The buffer and buflen
213 are the pointers passed in by the C library to the _nss_*_*
214 functions. */
216 static char *get_static(char **buffer, size_t *buflen, size_t len)
218 char *result;
220 /* Error check. We return false if things aren't set up right, or
221 there isn't enough buffer space left. */
223 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
224 return NULL;
227 /* Return an index into the static buffer */
229 result = *buffer;
230 *buffer += len;
231 *buflen -= len;
233 return result;
236 /****************************************************************************
237 gethostbyname() - we ignore any domain portion of the name and only
238 handle names that are at most 15 characters long
239 **************************************************************************/
240 _PUBLIC_ON_LINUX_
241 NSS_STATUS
242 _nss_wins_gethostbyname_r(const char *hostname,
243 struct hostent *he,
244 char *buffer,
245 size_t buflen,
246 int *errnop,
247 int *h_errnop)
249 NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
250 char *ip;
251 struct in_addr in;
252 int i;
253 fstring name;
254 size_t namelen;
255 int rc;
257 #ifdef HAVE_PTHREAD
258 pthread_mutex_lock(&wins_nss_mutex);
259 #endif
261 memset(he, '\0', sizeof(*he));
262 fstrcpy(name, hostname);
264 /* Do lookup */
266 ip = lookup_byname_backend(name);
267 if (ip == NULL) {
268 *h_errnop = HOST_NOT_FOUND;
269 nss_status = NSS_STATUS_NOTFOUND;
270 goto out;
273 rc = inet_pton(AF_INET, ip, &in);
274 wbcFreeMemory(ip);
275 if (rc == 0) {
276 *errnop = errno;
277 *h_errnop = NETDB_INTERNAL;
278 nss_status = NSS_STATUS_TRYAGAIN;
279 goto out;
282 /* Copy h_name */
284 namelen = strlen(name) + 1;
286 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
287 *errnop = EAGAIN;
288 *h_errnop = NETDB_INTERNAL;
289 nss_status = NSS_STATUS_TRYAGAIN;
290 goto out;
293 memcpy(he->h_name, name, namelen);
295 /* Copy h_addr_list, align to pointer boundary first */
297 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
298 i = sizeof(char*) - i;
300 if (get_static(&buffer, &buflen, i) == NULL) {
301 *errnop = EAGAIN;
302 *h_errnop = NETDB_INTERNAL;
303 nss_status = NSS_STATUS_TRYAGAIN;
304 goto out;
307 if ((he->h_addr_list = (char **)get_static(
308 &buffer, &buflen, 2 * sizeof(char *))) == NULL) {
309 *errnop = EAGAIN;
310 *h_errnop = NETDB_INTERNAL;
311 nss_status = NSS_STATUS_TRYAGAIN;
312 goto out;
315 if ((he->h_addr_list[0] = get_static(&buffer, &buflen,
316 INADDRSZ)) == NULL) {
317 *errnop = EAGAIN;
318 *h_errnop = NETDB_INTERNAL;
319 nss_status = NSS_STATUS_TRYAGAIN;
320 goto out;
323 memcpy(he->h_addr_list[0], &in, INADDRSZ);
325 he->h_addr_list[1] = NULL;
327 /* Set h_addr_type and h_length */
329 he->h_addrtype = AF_INET;
330 he->h_length = INADDRSZ;
332 /* Set h_aliases */
334 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
335 i = sizeof(char*) - i;
337 if (get_static(&buffer, &buflen, i) == NULL) {
338 *errnop = EAGAIN;
339 *h_errnop = NETDB_INTERNAL;
340 nss_status = NSS_STATUS_TRYAGAIN;
341 goto out;
344 if ((he->h_aliases = (char **)get_static(
345 &buffer, &buflen, sizeof(char *))) == NULL) {
346 *errnop = EAGAIN;
347 *h_errnop = NETDB_INTERNAL;
348 nss_status = NSS_STATUS_TRYAGAIN;
349 goto out;
352 he->h_aliases[0] = NULL;
354 *h_errnop = NETDB_SUCCESS;
355 nss_status = NSS_STATUS_SUCCESS;
357 out:
359 #ifdef HAVE_PTHREAD
360 pthread_mutex_unlock(&wins_nss_mutex);
361 #endif
362 return nss_status;
366 _PUBLIC_ON_LINUX_
367 NSS_STATUS
368 _nss_wins_gethostbyname2_r(const char *name,
369 int af,
370 struct hostent *he,
371 char *buffer,
372 size_t buflen,
373 int *errnop,
374 int *h_errnop)
376 NSS_STATUS nss_status;
378 if(af!=AF_INET) {
379 *errnop = EAFNOSUPPORT;
380 *h_errnop = NO_DATA;
381 nss_status = NSS_STATUS_UNAVAIL;
382 } else {
383 nss_status = _nss_wins_gethostbyname_r(name,
385 buffer,
386 buflen,
387 errnop,
388 h_errnop);
390 return nss_status;
392 #endif