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 result
= wbcResolveWinsByName(name
, &ip
);
73 if (result
!= WBC_ERR_SUCCESS
) {
77 ipp
= strchr(ip
, '\t');
87 static char *lookup_byaddr_backend(const char *ip
)
92 result
= wbcResolveWinsByIP(ip
, &name
);
93 if (result
!= WBC_ERR_SUCCESS
) {
106 nsd_logprintf(NSD_LOG_MIN
, "entering init (wins)\n");
108 ok
= nss_wins_init();
116 int lookup(nsd_file_t
*rq
)
121 int i
, count
, len
, size
;
125 nsd_logprintf(NSD_LOG_MIN
, "entering lookup (wins)\n");
129 map
= nsd_attr_fetch_string(rq
->f_attrs
, "table", (char*)0);
131 rq
->f_status
= NS_FATAL
;
135 key
= nsd_attr_fetch_string(rq
->f_attrs
, "key", (char*)0);
136 if (! key
|| ! *key
) {
137 rq
->f_status
= NS_FATAL
;
142 len
= sizeof(response
) - 2;
145 * response needs to be a string of the following format
146 * ip_address[ ip_address]*\tname[ alias]*
148 if (strcasecmp_m(map
,"hosts.byaddr") == 0) {
151 name
= lookup_byaddr_backend(key
);
153 size
= strlen(key
) + 1;
158 strncat(response
,key
,size
);
159 strncat(response
,"\t",1);
161 size
= strlen(name
) + 1;
166 strncat(response
, name
, size
);
167 strncat(response
, " ", 1);
170 response
[strlen(response
)-1] = '\n';
171 } else if (strcasecmp_m(map
,"hosts.byname") == 0) {
174 ip
= lookup_byname_backend(key
);
176 size
= strlen(ip
) + 1;
182 strncat(response
,ip
,size
);
183 strncat(response
,"\t",1);
184 size
= strlen(key
) + 1;
189 strncat(response
,key
,size
);
190 strncat(response
,"\n",1);
197 nsd_logprintf(NSD_LOG_LOW
, "lookup (wins %s) %s\n",map
,response
);
198 nsd_set_result(rq
,NS_SUCCESS
,response
,strlen(response
),VOLATILE
);
201 nsd_logprintf(NSD_LOG_LOW
, "lookup (wins) not found\n");
202 rq
->f_status
= NS_NOTFOUND
;
208 /* Allocate some space from the nss static buffer. The buffer and buflen
209 are the pointers passed in by the C library to the _nss_*_*
212 static char *get_static(char **buffer
, size_t *buflen
, size_t len
)
216 /* Error check. We return false if things aren't set up right, or
217 there isn't enough buffer space left. */
219 if ((buffer
== NULL
) || (buflen
== NULL
) || (*buflen
< len
)) {
223 /* Return an index into the static buffer */
232 /****************************************************************************
233 gethostbyname() - we ignore any domain portion of the name and only
234 handle names that are at most 15 characters long
235 **************************************************************************/
237 _nss_wins_gethostbyname_r(const char *hostname
,
244 NSS_STATUS nss_status
= NSS_STATUS_SUCCESS
;
253 pthread_mutex_lock(&wins_nss_mutex
);
256 memset(he
, '\0', sizeof(*he
));
257 fstrcpy(name
, hostname
);
261 ip
= lookup_byname_backend(name
);
263 *h_errnop
= HOST_NOT_FOUND
;
264 nss_status
= NSS_STATUS_NOTFOUND
;
268 rc
= inet_pton(AF_INET
, ip
, &in
);
272 *h_errnop
= NETDB_INTERNAL
;
273 nss_status
= NSS_STATUS_TRYAGAIN
;
279 namelen
= strlen(name
) + 1;
281 if ((he
->h_name
= get_static(&buffer
, &buflen
, namelen
)) == NULL
) {
283 *h_errnop
= NETDB_INTERNAL
;
284 nss_status
= NSS_STATUS_TRYAGAIN
;
288 memcpy(he
->h_name
, name
, namelen
);
290 /* Copy h_addr_list, align to pointer boundary first */
292 if ((i
= (unsigned long)(buffer
) % sizeof(char*)) != 0)
293 i
= sizeof(char*) - i
;
295 if (get_static(&buffer
, &buflen
, i
) == NULL
) {
297 *h_errnop
= NETDB_INTERNAL
;
298 nss_status
= NSS_STATUS_TRYAGAIN
;
302 if ((he
->h_addr_list
= (char **)get_static(
303 &buffer
, &buflen
, 2 * sizeof(char *))) == NULL
) {
305 *h_errnop
= NETDB_INTERNAL
;
306 nss_status
= NSS_STATUS_TRYAGAIN
;
310 if ((he
->h_addr_list
[0] = get_static(&buffer
, &buflen
,
311 INADDRSZ
)) == NULL
) {
313 *h_errnop
= NETDB_INTERNAL
;
314 nss_status
= NSS_STATUS_TRYAGAIN
;
318 memcpy(he
->h_addr_list
[0], &in
, INADDRSZ
);
320 he
->h_addr_list
[1] = NULL
;
322 /* Set h_addr_type and h_length */
324 he
->h_addrtype
= AF_INET
;
325 he
->h_length
= INADDRSZ
;
329 if ((i
= (unsigned long)(buffer
) % sizeof(char*)) != 0)
330 i
= sizeof(char*) - i
;
332 if (get_static(&buffer
, &buflen
, i
) == NULL
) {
334 *h_errnop
= NETDB_INTERNAL
;
335 nss_status
= NSS_STATUS_TRYAGAIN
;
339 if ((he
->h_aliases
= (char **)get_static(
340 &buffer
, &buflen
, sizeof(char *))) == NULL
) {
342 *h_errnop
= NETDB_INTERNAL
;
343 nss_status
= NSS_STATUS_TRYAGAIN
;
347 he
->h_aliases
[0] = NULL
;
349 *h_errnop
= NETDB_SUCCESS
;
350 nss_status
= NSS_STATUS_SUCCESS
;
355 pthread_mutex_unlock(&wins_nss_mutex
);
362 _nss_wins_gethostbyname2_r(const char *name
,
370 NSS_STATUS nss_status
;
373 *errnop
= EAFNOSUPPORT
;
375 nss_status
= NSS_STATUS_UNAVAIL
;
377 nss_status
= _nss_wins_gethostbyname_r(name
,