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_nss.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, 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);
47 static char *lookup_byname_backend(const char *name)
54 nbt_len = strlen(name);
55 if (nbt_len > MAX_NETBIOSNAME_LEN - 1) {
58 p = strchr(name, '.');
63 result = wbcResolveWinsByName(name, &ip);
64 if (result != WBC_ERR_SUCCESS) {
73 static char *lookup_byaddr_backend(const char *ip)
78 result = wbcResolveWinsByIP(ip, &name);
79 if (result != WBC_ERR_SUCCESS) {
92 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
102 int lookup(nsd_file_t *rq)
107 int i, count, len, size;
111 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
115 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
117 rq->f_status = NS_FATAL;
121 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
122 if (! key || ! *key) {
123 rq->f_status = NS_FATAL;
128 len = sizeof(response) - 2;
131 * response needs to be a string of the following format
132 * ip_address[ ip_address]*\tname[ alias]*
134 if (strcasecmp_m(map,"hosts.byaddr") == 0) {
137 name = lookup_byaddr_backend(key);
139 size = strlen(key) + 1;
144 strncat(response,key,size);
145 strncat(response,"\t",1);
147 size = strlen(name) + 1;
152 strncat(response, name, size);
153 strncat(response, " ", 1);
156 response[strlen(response)-1] = '\n';
157 } else if (strcasecmp_m(map,"hosts.byname") == 0) {
160 ip = lookup_byname_backend(key);
162 size = strlen(ip) + 1;
168 strncat(response,ip,size);
169 strncat(response,"\t",1);
170 size = strlen(key) + 1;
175 strncat(response,key,size);
176 strncat(response,"\n",1);
183 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
184 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
187 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
188 rq->f_status = NS_NOTFOUND;
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_*_*
198 static char *get_static(char **buffer, size_t *buflen, size_t len)
202 /* Error check. We return false if things aren't set up right, or
203 there isn't enough buffer space left. */
205 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
209 /* Return an index into the static buffer */
218 /****************************************************************************
219 gethostbyname() - we ignore any domain portion of the name and only
220 handle names that are at most 15 characters long
221 **************************************************************************/
223 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
224 char *buffer, size_t buflen, int *h_errnop)
226 NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
235 pthread_mutex_lock(&wins_nss_mutex);
238 memset(he, '\0', sizeof(*he));
239 fstrcpy(name, hostname);
243 ip = lookup_byname_backend(name);
245 nss_status = NSS_STATUS_NOTFOUND;
249 rc = inet_pton(AF_INET, ip, &in);
252 nss_status = NSS_STATUS_TRYAGAIN;
258 namelen = strlen(name) + 1;
260 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
261 nss_status = NSS_STATUS_TRYAGAIN;
265 memcpy(he->h_name, name, namelen);
267 /* Copy h_addr_list, align to pointer boundary first */
269 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
270 i = sizeof(char*) - i;
272 if (get_static(&buffer, &buflen, i) == NULL) {
273 nss_status = NSS_STATUS_TRYAGAIN;
277 if ((he->h_addr_list = (char **)get_static(
278 &buffer, &buflen, i * sizeof(char *))) == NULL) {
279 nss_status = NSS_STATUS_TRYAGAIN;
283 if ((he->h_addr_list[0] = get_static(&buffer, &buflen,
284 INADDRSZ)) == NULL) {
285 nss_status = NSS_STATUS_TRYAGAIN;
289 memcpy(he->h_addr_list[i], &in, INADDRSZ);
291 he->h_addr_list[0] = NULL;
293 /* Set h_addr_type and h_length */
295 he->h_addrtype = AF_INET;
296 he->h_length = INADDRSZ;
300 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
301 i = sizeof(char*) - i;
303 if (get_static(&buffer, &buflen, i) == NULL) {
304 nss_status = NSS_STATUS_TRYAGAIN;
308 if ((he->h_aliases = (char **)get_static(
309 &buffer, &buflen, sizeof(char *))) == NULL) {
310 nss_status = NSS_STATUS_TRYAGAIN;
314 he->h_aliases[0] = NULL;
316 nss_status = NSS_STATUS_SUCCESS;
321 pthread_mutex_unlock(&wins_nss_mutex);
328 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
329 char *buffer, size_t buflen, int *h_errnop)
331 NSS_STATUS nss_status;
335 nss_status = NSS_STATUS_UNAVAIL;
337 nss_status = _nss_wins_gethostbyname_r(
338 name, he, buffer, buflen, h_errnop);