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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 setsockopt(res
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&val
,sizeof(val
));
72 setsockopt(res
,SOL_SOCKET
,SO_REUSEPORT
,(char *)&val
,sizeof(val
));
73 #endif /* SO_REUSEPORT */
75 /* now we've got a socket - we need to bind it */
77 if (bind(res
, (struct sockaddr
* ) &sock
,sizeof(sock
)) < 0) {
82 set_socket_options(res
,"SO_BROADCAST");
88 static void nss_wins_init(void)
92 AllowDebugChange
= False
;
95 setup_logging("nss_wins",False
);
97 lp_load(dyn_CONFIGFILE
,True
,False
,False
,True
);
101 static struct in_addr
*lookup_byname_backend(const char *name
, int *count
)
104 struct ip_service
*address
= NULL
;
105 struct in_addr
*ret
= NULL
;
114 /* always try with wins first */
115 if (resolve_wins(name
,0x00,&address
,count
)) {
116 if ( (ret
= SMB_MALLOC_P(struct in_addr
)) == NULL
) {
120 *ret
= address
[0].ip
;
125 fd
= wins_lookup_open_socket_in();
130 /* uggh, we have to broadcast to each interface in turn */
131 for (j
=iface_count() - 1;j
>= 0;j
--) {
132 struct in_addr
*bcast
= iface_n_bcast(j
);
133 ret
= name_query(fd
,name
,0x00,True
,True
,*bcast
,count
, &flags
, NULL
);
143 static NODE_STATUS_STRUCT
*lookup_byaddr_backend(char *addr
, int *count
)
147 struct nmb_name nname
;
148 NODE_STATUS_STRUCT
*status
;
154 fd
= wins_lookup_open_socket_in();
158 make_nmb_name(&nname
, "*", 0);
159 ip
= *interpret_addr2(addr
);
160 status
= node_status_query(fd
,&nname
,ip
, count
, NULL
);
170 nsd_logprintf(NSD_LOG_MIN
, "entering init (wins)\n");
175 int lookup(nsd_file_t
*rq
)
180 struct in_addr
*ip_list
;
181 NODE_STATUS_STRUCT
*status
;
182 int i
, count
, len
, size
;
186 nsd_logprintf(NSD_LOG_MIN
, "entering lookup (wins)\n");
190 map
= nsd_attr_fetch_string(rq
->f_attrs
, "table", (char*)0);
192 rq
->f_status
= NS_FATAL
;
196 key
= nsd_attr_fetch_string(rq
->f_attrs
, "key", (char*)0);
197 if (! key
|| ! *key
) {
198 rq
->f_status
= NS_FATAL
;
203 len
= sizeof(response
) - 2;
206 * response needs to be a string of the following format
207 * ip_address[ ip_address]*\tname[ alias]*
209 if (StrCaseCmp(map
,"hosts.byaddr") == 0) {
210 if ( status
= lookup_byaddr_backend(key
, &count
)) {
211 size
= strlen(key
) + 1;
217 strncat(response
,key
,size
);
218 strncat(response
,"\t",1);
219 for (i
= 0; i
< count
; i
++) {
220 /* ignore group names */
221 if (status
[i
].flags
& 0x80) continue;
222 if (status
[i
].type
== 0x20) {
223 size
= sizeof(status
[i
].name
) + 1;
229 strncat(response
, status
[i
].name
, size
);
230 strncat(response
, " ", 1);
234 response
[strlen(response
)-1] = '\n';
237 } else if (StrCaseCmp(map
,"hosts.byname") == 0) {
238 if (ip_list
= lookup_byname_backend(key
, &count
)) {
239 for (i
= count
; i
; i
--) {
240 addr
= inet_ntoa(ip_list
[i
-1]);
241 size
= strlen(addr
) + 1;
248 response
[strlen(response
)-1] = ' ';
249 strncat(response
,addr
,size
);
250 strncat(response
,"\t",1);
252 size
= strlen(key
) + 1;
257 strncat(response
,key
,size
);
258 strncat(response
,"\n",1);
265 nsd_logprintf(NSD_LOG_LOW
, "lookup (wins %s) %s\n",map
,response
);
266 nsd_set_result(rq
,NS_SUCCESS
,response
,strlen(response
),VOLATILE
);
269 nsd_logprintf(NSD_LOG_LOW
, "lookup (wins) not found\n");
270 rq
->f_status
= NS_NOTFOUND
;
276 /* Allocate some space from the nss static buffer. The buffer and buflen
277 are the pointers passed in by the C library to the _nss_*_*
280 static char *get_static(char **buffer
, size_t *buflen
, int len
)
284 /* Error check. We return false if things aren't set up right, or
285 there isn't enough buffer space left. */
287 if ((buffer
== NULL
) || (buflen
== NULL
) || (*buflen
< len
)) {
291 /* Return an index into the static buffer */
300 /****************************************************************************
301 gethostbyname() - we ignore any domain portion of the name and only
302 handle names that are at most 15 characters long
303 **************************************************************************/
305 _nss_wins_gethostbyname_r(const char *hostname
, struct hostent
*he
,
306 char *buffer
, size_t buflen
, int *h_errnop
)
308 NSS_STATUS nss_status
= NSS_STATUS_SUCCESS
;
309 struct in_addr
*ip_list
;
315 pthread_mutex_lock(&wins_nss_mutex
);
318 memset(he
, '\0', sizeof(*he
));
319 fstrcpy(name
, hostname
);
323 ip_list
= lookup_byname_backend(name
, &count
);
326 nss_status
= NSS_STATUS_NOTFOUND
;
332 namelen
= strlen(name
) + 1;
334 if ((he
->h_name
= get_static(&buffer
, &buflen
, namelen
)) == NULL
) {
335 nss_status
= NSS_STATUS_TRYAGAIN
;
339 memcpy(he
->h_name
, name
, namelen
);
341 /* Copy h_addr_list, align to pointer boundary first */
343 if ((i
= (unsigned long)(buffer
) % sizeof(char*)) != 0)
344 i
= sizeof(char*) - i
;
346 if (get_static(&buffer
, &buflen
, i
) == NULL
) {
347 nss_status
= NSS_STATUS_TRYAGAIN
;
351 if ((he
->h_addr_list
= (char **)get_static(
352 &buffer
, &buflen
, (count
+ 1) * sizeof(char *))) == NULL
) {
353 nss_status
= NSS_STATUS_TRYAGAIN
;
357 for (i
= 0; i
< count
; i
++) {
358 if ((he
->h_addr_list
[i
] = get_static(&buffer
, &buflen
,
359 INADDRSZ
)) == NULL
) {
360 nss_status
= NSS_STATUS_TRYAGAIN
;
363 memcpy(he
->h_addr_list
[i
], &ip_list
[i
], INADDRSZ
);
366 he
->h_addr_list
[count
] = NULL
;
371 /* Set h_addr_type and h_length */
373 he
->h_addrtype
= AF_INET
;
374 he
->h_length
= INADDRSZ
;
378 if ((i
= (unsigned long)(buffer
) % sizeof(char*)) != 0)
379 i
= sizeof(char*) - i
;
381 if (get_static(&buffer
, &buflen
, i
) == NULL
) {
382 nss_status
= NSS_STATUS_TRYAGAIN
;
386 if ((he
->h_aliases
= (char **)get_static(
387 &buffer
, &buflen
, sizeof(char *))) == NULL
) {
388 nss_status
= NSS_STATUS_TRYAGAIN
;
392 he
->h_aliases
[0] = NULL
;
394 nss_status
= NSS_STATUS_SUCCESS
;
399 pthread_mutex_unlock(&wins_nss_mutex
);
406 _nss_wins_gethostbyname2_r(const char *name
, int af
, struct hostent
*he
,
407 char *buffer
, size_t buflen
, int *h_errnop
)
409 NSS_STATUS nss_status
;
412 pthread_mutex_lock(&wins_nss_mutex
);
417 nss_status
= NSS_STATUS_UNAVAIL
;
419 nss_status
= _nss_wins_gethostbyname_r(
420 name
, he
, buffer
, buflen
, h_errnop
);
423 pthread_mutex_unlock(&wins_nss_mutex
);