2 Unix SMB/CIFS implementation.
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/>.
22 #include "nsswitch/winbind_client.h"
23 #include "nsswitch/libwbclient/wbclient.h"
27 #include <ns_daemon.h>
35 static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
42 NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname,
48 NSS_STATUS _nss_wins_gethostbyname2_r(const char *name,
56 static char *lookup_byname_backend(const char *name)
63 nbt_len = strlen(name);
64 if (nbt_len > MAX_NETBIOSNAME_LEN - 1) {
67 p = strchr(name, '.');
72 wbcSetClientProcessName("nss_wins");
73 result = wbcResolveWinsByName(name, &ip);
74 if (result != WBC_ERR_SUCCESS) {
78 ipp = strchr(ip, '\t');
88 static char *lookup_byaddr_backend(const char *ip)
93 wbcSetClientProcessName("nss_wins");
94 result = wbcResolveWinsByIP(ip, &name);
95 if (result != WBC_ERR_SUCCESS) {
108 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
110 ok = nss_wins_init();
118 int lookup(nsd_file_t *rq)
123 int i, count, len, size;
127 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
131 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
133 rq->f_status = NS_FATAL;
137 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
138 if (! key || ! *key) {
139 rq->f_status = NS_FATAL;
144 len = sizeof(response) - 2;
147 * response needs to be a string of the following format
148 * ip_address[ ip_address]*\tname[ alias]*
150 if (strcasecmp_m(map,"hosts.byaddr") == 0) {
153 name = lookup_byaddr_backend(key);
155 size = strlen(key) + 1;
160 strncat(response,key,size);
161 strncat(response,"\t",1);
163 size = strlen(name) + 1;
168 strncat(response, name, size);
169 strncat(response, " ", 1);
172 response[strlen(response)-1] = '\n';
173 } else if (strcasecmp_m(map,"hosts.byname") == 0) {
176 ip = lookup_byname_backend(key);
178 size = strlen(ip) + 1;
184 strncat(response,ip,size);
185 strncat(response,"\t",1);
186 size = strlen(key) + 1;
191 strncat(response,key,size);
192 strncat(response,"\n",1);
199 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
200 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
203 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
204 rq->f_status = NS_NOTFOUND;
210 /* Allocate some space from the nss static buffer. The buffer and buflen
211 are the pointers passed in by the C library to the _nss_*_*
214 static char *get_static(char **buffer, size_t *buflen, size_t len)
218 /* Error check. We return false if things aren't set up right, or
219 there isn't enough buffer space left. */
221 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
225 /* Return an index into the static buffer */
234 /****************************************************************************
235 gethostbyname() - we ignore any domain portion of the name and only
236 handle names that are at most 15 characters long
237 **************************************************************************/
239 _nss_wins_gethostbyname_r(const char *hostname,
246 NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
255 pthread_mutex_lock(&wins_nss_mutex);
258 memset(he, '\0', sizeof(*he));
259 fstrcpy(name, hostname);
263 ip = lookup_byname_backend(name);
265 *h_errnop = HOST_NOT_FOUND;
266 nss_status = NSS_STATUS_NOTFOUND;
270 rc = inet_pton(AF_INET, ip, &in);
274 *h_errnop = NETDB_INTERNAL;
275 nss_status = NSS_STATUS_TRYAGAIN;
281 namelen = strlen(name) + 1;
283 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
285 *h_errnop = NETDB_INTERNAL;
286 nss_status = NSS_STATUS_TRYAGAIN;
290 memcpy(he->h_name, name, namelen);
292 /* Copy h_addr_list, align to pointer boundary first */
294 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
295 i = sizeof(char*) - i;
297 if (get_static(&buffer, &buflen, i) == NULL) {
299 *h_errnop = NETDB_INTERNAL;
300 nss_status = NSS_STATUS_TRYAGAIN;
304 if ((he->h_addr_list = (char **)get_static(
305 &buffer, &buflen, 2 * sizeof(char *))) == NULL) {
307 *h_errnop = NETDB_INTERNAL;
308 nss_status = NSS_STATUS_TRYAGAIN;
312 if ((he->h_addr_list[0] = get_static(&buffer, &buflen,
313 INADDRSZ)) == NULL) {
315 *h_errnop = NETDB_INTERNAL;
316 nss_status = NSS_STATUS_TRYAGAIN;
320 memcpy(he->h_addr_list[0], &in, INADDRSZ);
322 he->h_addr_list[1] = NULL;
324 /* Set h_addr_type and h_length */
326 he->h_addrtype = AF_INET;
327 he->h_length = INADDRSZ;
331 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
332 i = sizeof(char*) - i;
334 if (get_static(&buffer, &buflen, i) == NULL) {
336 *h_errnop = NETDB_INTERNAL;
337 nss_status = NSS_STATUS_TRYAGAIN;
341 if ((he->h_aliases = (char **)get_static(
342 &buffer, &buflen, sizeof(char *))) == NULL) {
344 *h_errnop = NETDB_INTERNAL;
345 nss_status = NSS_STATUS_TRYAGAIN;
349 he->h_aliases[0] = NULL;
351 *h_errnop = NETDB_SUCCESS;
352 nss_status = NSS_STATUS_SUCCESS;
357 pthread_mutex_unlock(&wins_nss_mutex);
364 _nss_wins_gethostbyname2_r(const char *name,
372 NSS_STATUS nss_status;
375 *errnop = EAFNOSUPPORT;
377 nss_status = NSS_STATUS_UNAVAIL;
379 nss_status = _nss_wins_gethostbyname_r(name,