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"
26 #include <ns_daemon.h>
34 static pthread_mutex_t wins_nss_mutex
= PTHREAD_MUTEX_INITIALIZER
;
41 static int initialised
;
43 extern bool AllowDebugChange
;
45 NSS_STATUS
_nss_wins_gethostbyname_r(const char *hostname
, struct hostent
*he
,
46 char *buffer
, size_t buflen
, int *h_errnop
);
47 NSS_STATUS
_nss_wins_gethostbyname2_r(const char *name
, int af
, struct hostent
*he
,
48 char *buffer
, size_t buflen
, int *h_errnop
);
50 /* Use our own create socket code so we don't recurse.... */
52 static int wins_lookup_open_socket_in(void)
54 struct sockaddr_in sock
;
58 memset((char *)&sock
,'\0',sizeof(sock
));
60 #ifdef HAVE_SOCK_SIN_LEN
61 sock
.sin_len
= sizeof(sock
);
64 sock
.sin_family
= AF_INET
;
65 sock
.sin_addr
.s_addr
= interpret_addr("0.0.0.0");
66 res
= socket(AF_INET
, SOCK_DGRAM
, 0);
70 if (setsockopt(res
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&val
,sizeof(val
)) != 0) {
75 if (setsockopt(res
,SOL_SOCKET
,SO_REUSEPORT
,(char *)&val
,sizeof(val
)) != 0) {
79 #endif /* SO_REUSEPORT */
81 /* now we've got a socket - we need to bind it */
83 if (bind(res
, (struct sockaddr
* ) &sock
,sizeof(sock
)) < 0) {
88 set_socket_options(res
,"SO_BROADCAST");
94 static void nss_wins_init(void)
98 AllowDebugChange
= False
;
101 setup_logging("nss_wins",False
);
103 lp_load(get_dyn_CONFIGFILE(),True
,False
,False
,True
);
107 static struct in_addr
*lookup_byname_backend(const char *name
, int *count
)
110 struct ip_service
*address
= NULL
;
111 struct in_addr
*ret
= NULL
;
120 /* always try with wins first */
121 if (NT_STATUS_IS_OK(resolve_wins(name
,0x00,&address
,count
))) {
122 if ( (ret
= SMB_MALLOC_P(struct in_addr
)) == NULL
) {
126 if (address
[0].ss
.ss_family
!= AF_INET
) {
131 *ret
= ((struct sockaddr_in
*)&address
[0].ss
)->sin_addr
;
136 fd
= wins_lookup_open_socket_in();
141 /* uggh, we have to broadcast to each interface in turn */
142 for (j
=iface_count() - 1;j
>= 0;j
--) {
143 const struct in_addr
*bcast
= iface_n_bcast_v4(j
);
144 struct sockaddr_storage ss
;
145 struct sockaddr_storage
*pss
;
149 in_addr_to_sockaddr_storage(&ss
, *bcast
);
150 pss
= name_query(fd
,name
,0x00,True
,True
,&ss
,count
, &flags
, NULL
);
152 if ((ret
= SMB_MALLOC_P(struct in_addr
)) == NULL
) {
155 *ret
= ((struct sockaddr_in
*)pss
)->sin_addr
;
166 static NODE_STATUS_STRUCT
*lookup_byaddr_backend(char *addr
, int *count
)
169 struct sockaddr_storage ss
;
170 struct nmb_name nname
;
171 NODE_STATUS_STRUCT
*status
;
177 fd
= wins_lookup_open_socket_in();
181 make_nmb_name(&nname
, "*", 0);
182 if (!interpret_string_addr(&ss
, addr
, AI_NUMERICHOST
)) {
185 status
= node_status_query(fd
, &nname
, &ss
, count
, NULL
);
195 nsd_logprintf(NSD_LOG_MIN
, "entering init (wins)\n");
200 int lookup(nsd_file_t
*rq
)
205 struct in_addr
*ip_list
;
206 NODE_STATUS_STRUCT
*status
;
207 int i
, count
, len
, size
;
211 nsd_logprintf(NSD_LOG_MIN
, "entering lookup (wins)\n");
215 map
= nsd_attr_fetch_string(rq
->f_attrs
, "table", (char*)0);
217 rq
->f_status
= NS_FATAL
;
221 key
= nsd_attr_fetch_string(rq
->f_attrs
, "key", (char*)0);
222 if (! key
|| ! *key
) {
223 rq
->f_status
= NS_FATAL
;
228 len
= sizeof(response
) - 2;
231 * response needs to be a string of the following format
232 * ip_address[ ip_address]*\tname[ alias]*
234 if (StrCaseCmp(map
,"hosts.byaddr") == 0) {
235 if ( status
= lookup_byaddr_backend(key
, &count
)) {
236 size
= strlen(key
) + 1;
242 strncat(response
,key
,size
);
243 strncat(response
,"\t",1);
244 for (i
= 0; i
< count
; i
++) {
245 /* ignore group names */
246 if (status
[i
].flags
& 0x80) continue;
247 if (status
[i
].type
== 0x20) {
248 size
= sizeof(status
[i
].name
) + 1;
254 strncat(response
, status
[i
].name
, size
);
255 strncat(response
, " ", 1);
259 response
[strlen(response
)-1] = '\n';
262 } else if (StrCaseCmp(map
,"hosts.byname") == 0) {
263 if (ip_list
= lookup_byname_backend(key
, &count
)) {
264 for (i
= count
; i
; i
--) {
265 addr
= inet_ntoa(ip_list
[i
-1]);
266 size
= strlen(addr
) + 1;
273 response
[strlen(response
)-1] = ' ';
274 strncat(response
,addr
,size
);
275 strncat(response
,"\t",1);
277 size
= strlen(key
) + 1;
282 strncat(response
,key
,size
);
283 strncat(response
,"\n",1);
290 nsd_logprintf(NSD_LOG_LOW
, "lookup (wins %s) %s\n",map
,response
);
291 nsd_set_result(rq
,NS_SUCCESS
,response
,strlen(response
),VOLATILE
);
294 nsd_logprintf(NSD_LOG_LOW
, "lookup (wins) not found\n");
295 rq
->f_status
= NS_NOTFOUND
;
301 /* Allocate some space from the nss static buffer. The buffer and buflen
302 are the pointers passed in by the C library to the _nss_*_*
305 static char *get_static(char **buffer
, size_t *buflen
, int len
)
309 /* Error check. We return false if things aren't set up right, or
310 there isn't enough buffer space left. */
312 if ((buffer
== NULL
) || (buflen
== NULL
) || (*buflen
< len
)) {
316 /* Return an index into the static buffer */
325 /****************************************************************************
326 gethostbyname() - we ignore any domain portion of the name and only
327 handle names that are at most 15 characters long
328 **************************************************************************/
330 _nss_wins_gethostbyname_r(const char *hostname
, struct hostent
*he
,
331 char *buffer
, size_t buflen
, int *h_errnop
)
333 NSS_STATUS nss_status
= NSS_STATUS_SUCCESS
;
334 struct in_addr
*ip_list
;
341 pthread_mutex_lock(&wins_nss_mutex
);
344 frame
= talloc_stackframe();
346 memset(he
, '\0', sizeof(*he
));
347 fstrcpy(name
, hostname
);
351 ip_list
= lookup_byname_backend(name
, &count
);
354 nss_status
= NSS_STATUS_NOTFOUND
;
360 namelen
= strlen(name
) + 1;
362 if ((he
->h_name
= get_static(&buffer
, &buflen
, namelen
)) == NULL
) {
364 nss_status
= NSS_STATUS_TRYAGAIN
;
368 memcpy(he
->h_name
, name
, namelen
);
370 /* Copy h_addr_list, align to pointer boundary first */
372 if ((i
= (unsigned long)(buffer
) % sizeof(char*)) != 0)
373 i
= sizeof(char*) - i
;
375 if (get_static(&buffer
, &buflen
, i
) == NULL
) {
377 nss_status
= NSS_STATUS_TRYAGAIN
;
381 if ((he
->h_addr_list
= (char **)get_static(
382 &buffer
, &buflen
, (count
+ 1) * sizeof(char *))) == NULL
) {
384 nss_status
= NSS_STATUS_TRYAGAIN
;
388 for (i
= 0; i
< count
; i
++) {
389 if ((he
->h_addr_list
[i
] = get_static(&buffer
, &buflen
,
390 INADDRSZ
)) == NULL
) {
392 nss_status
= NSS_STATUS_TRYAGAIN
;
395 memcpy(he
->h_addr_list
[i
], &ip_list
[i
], INADDRSZ
);
398 he
->h_addr_list
[count
] = NULL
;
402 /* Set h_addr_type and h_length */
404 he
->h_addrtype
= AF_INET
;
405 he
->h_length
= INADDRSZ
;
409 if ((i
= (unsigned long)(buffer
) % sizeof(char*)) != 0)
410 i
= sizeof(char*) - i
;
412 if (get_static(&buffer
, &buflen
, i
) == NULL
) {
413 nss_status
= NSS_STATUS_TRYAGAIN
;
417 if ((he
->h_aliases
= (char **)get_static(
418 &buffer
, &buflen
, sizeof(char *))) == NULL
) {
419 nss_status
= NSS_STATUS_TRYAGAIN
;
423 he
->h_aliases
[0] = NULL
;
425 nss_status
= NSS_STATUS_SUCCESS
;
432 pthread_mutex_unlock(&wins_nss_mutex
);
439 _nss_wins_gethostbyname2_r(const char *name
, int af
, struct hostent
*he
,
440 char *buffer
, size_t buflen
, int *h_errnop
)
442 NSS_STATUS nss_status
;
446 nss_status
= NSS_STATUS_UNAVAIL
;
448 nss_status
= _nss_wins_gethostbyname_r(
449 name
, he
, buffer
, buflen
, h_errnop
);