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>
33 static int initialised
;
35 extern BOOL AllowDebugChange
;
37 NSS_STATUS
_nss_wins_gethostbyname_r(const char *hostname
, struct hostent
*he
,
38 char *buffer
, size_t buflen
, int *h_errnop
);
39 NSS_STATUS
_nss_wins_gethostbyname2_r(const char *name
, int af
, struct hostent
*he
,
40 char *buffer
, size_t buflen
, int *h_errnop
);
42 /* Use our own create socket code so we don't recurse.... */
44 static int wins_lookup_open_socket_in(void)
46 struct sockaddr_in sock
;
50 memset((char *)&sock
,'\0',sizeof(sock
));
52 #ifdef HAVE_SOCK_SIN_LEN
53 sock
.sin_len
= sizeof(sock
);
56 sock
.sin_family
= AF_INET
;
57 sock
.sin_addr
.s_addr
= interpret_addr("0.0.0.0");
58 res
= socket(AF_INET
, SOCK_DGRAM
, 0);
62 setsockopt(res
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&val
,sizeof(val
));
64 setsockopt(res
,SOL_SOCKET
,SO_REUSEPORT
,(char *)&val
,sizeof(val
));
65 #endif /* SO_REUSEPORT */
67 /* now we've got a socket - we need to bind it */
69 if (bind(res
, (struct sockaddr
* ) &sock
,sizeof(sock
)) < 0) {
74 set_socket_options(res
,"SO_BROADCAST");
80 static void nss_wins_init(void)
84 AllowDebugChange
= False
;
87 setup_logging("nss_wins",False
);
89 lp_load(dyn_CONFIGFILE
,True
,False
,False
,True
);
93 static struct in_addr
*lookup_byname_backend(const char *name
, int *count
)
96 struct ip_service
*address
= NULL
;
97 struct in_addr
*ret
= NULL
;
106 /* always try with wins first */
107 if (resolve_wins(name
,0x00,&address
,count
)) {
108 if ( (ret
= SMB_MALLOC_P(struct in_addr
)) == NULL
) {
112 *ret
= address
[0].ip
;
117 fd
= wins_lookup_open_socket_in();
122 /* uggh, we have to broadcast to each interface in turn */
123 for (j
=iface_count() - 1;j
>= 0;j
--) {
124 struct in_addr
*bcast
= iface_n_bcast(j
);
125 ret
= name_query(fd
,name
,0x00,True
,True
,*bcast
,count
, &flags
, NULL
);
135 static NODE_STATUS_STRUCT
*lookup_byaddr_backend(char *addr
, int *count
)
139 struct nmb_name nname
;
140 NODE_STATUS_STRUCT
*status
;
146 fd
= wins_lookup_open_socket_in();
150 make_nmb_name(&nname
, "*", 0);
151 ip
= *interpret_addr2(addr
);
152 status
= node_status_query(fd
,&nname
,ip
, count
, NULL
);
162 nsd_logprintf(NSD_LOG_MIN
, "entering init (wins)\n");
167 int lookup(nsd_file_t
*rq
)
172 struct in_addr
*ip_list
;
173 NODE_STATUS_STRUCT
*status
;
174 int i
, count
, len
, size
;
178 nsd_logprintf(NSD_LOG_MIN
, "entering lookup (wins)\n");
182 map
= nsd_attr_fetch_string(rq
->f_attrs
, "table", (char*)0);
184 rq
->f_status
= NS_FATAL
;
188 key
= nsd_attr_fetch_string(rq
->f_attrs
, "key", (char*)0);
189 if (! key
|| ! *key
) {
190 rq
->f_status
= NS_FATAL
;
195 len
= sizeof(response
) - 2;
198 * response needs to be a string of the following format
199 * ip_address[ ip_address]*\tname[ alias]*
201 if (StrCaseCmp(map
,"hosts.byaddr") == 0) {
202 if ( status
= lookup_byaddr_backend(key
, &count
)) {
203 size
= strlen(key
) + 1;
209 strncat(response
,key
,size
);
210 strncat(response
,"\t",1);
211 for (i
= 0; i
< count
; i
++) {
212 /* ignore group names */
213 if (status
[i
].flags
& 0x80) continue;
214 if (status
[i
].type
== 0x20) {
215 size
= sizeof(status
[i
].name
) + 1;
221 strncat(response
, status
[i
].name
, size
);
222 strncat(response
, " ", 1);
226 response
[strlen(response
)-1] = '\n';
229 } else if (StrCaseCmp(map
,"hosts.byname") == 0) {
230 if (ip_list
= lookup_byname_backend(key
, &count
)) {
231 for (i
= count
; i
; i
--) {
232 addr
= inet_ntoa(ip_list
[i
-1]);
233 size
= strlen(addr
) + 1;
240 response
[strlen(response
)-1] = ' ';
241 strncat(response
,addr
,size
);
242 strncat(response
,"\t",1);
244 size
= strlen(key
) + 1;
249 strncat(response
,key
,size
);
250 strncat(response
,"\n",1);
257 nsd_logprintf(NSD_LOG_LOW
, "lookup (wins %s) %s\n",map
,response
);
258 nsd_set_result(rq
,NS_SUCCESS
,response
,strlen(response
),VOLATILE
);
261 nsd_logprintf(NSD_LOG_LOW
, "lookup (wins) not found\n");
262 rq
->f_status
= NS_NOTFOUND
;
268 /* Allocate some space from the nss static buffer. The buffer and buflen
269 are the pointers passed in by the C library to the _nss_*_*
272 static char *get_static(char **buffer
, size_t *buflen
, int len
)
276 /* Error check. We return false if things aren't set up right, or
277 there isn't enough buffer space left. */
279 if ((buffer
== NULL
) || (buflen
== NULL
) || (*buflen
< len
)) {
283 /* Return an index into the static buffer */
292 /****************************************************************************
293 gethostbyname() - we ignore any domain portion of the name and only
294 handle names that are at most 15 characters long
295 **************************************************************************/
297 _nss_wins_gethostbyname_r(const char *hostname
, struct hostent
*he
,
298 char *buffer
, size_t buflen
, int *h_errnop
)
300 struct in_addr
*ip_list
;
305 memset(he
, '\0', sizeof(*he
));
306 fstrcpy(name
, hostname
);
310 ip_list
= lookup_byname_backend(name
, &count
);
313 return NSS_STATUS_NOTFOUND
;
317 namelen
= strlen(name
) + 1;
319 if ((he
->h_name
= get_static(&buffer
, &buflen
, namelen
)) == NULL
)
320 return NSS_STATUS_TRYAGAIN
;
322 memcpy(he
->h_name
, name
, namelen
);
324 /* Copy h_addr_list, align to pointer boundary first */
326 if ((i
= (unsigned long)(buffer
) % sizeof(char*)) != 0)
327 i
= sizeof(char*) - i
;
329 if (get_static(&buffer
, &buflen
, i
) == NULL
)
330 return NSS_STATUS_TRYAGAIN
;
332 if ((he
->h_addr_list
= (char **)get_static(
333 &buffer
, &buflen
, (count
+ 1) * sizeof(char *))) == NULL
)
334 return NSS_STATUS_TRYAGAIN
;
336 for (i
= 0; i
< count
; i
++) {
337 if ((he
->h_addr_list
[i
] = get_static(&buffer
, &buflen
,
339 return NSS_STATUS_TRYAGAIN
;
340 memcpy(he
->h_addr_list
[i
], &ip_list
[i
], INADDRSZ
);
343 he
->h_addr_list
[count
] = NULL
;
348 /* Set h_addr_type and h_length */
350 he
->h_addrtype
= AF_INET
;
351 he
->h_length
= INADDRSZ
;
355 if ((i
= (unsigned long)(buffer
) % sizeof(char*)) != 0)
356 i
= sizeof(char*) - i
;
358 if (get_static(&buffer
, &buflen
, i
) == NULL
)
359 return NSS_STATUS_TRYAGAIN
;
361 if ((he
->h_aliases
= (char **)get_static(
362 &buffer
, &buflen
, sizeof(char *))) == NULL
)
363 return NSS_STATUS_TRYAGAIN
;
365 he
->h_aliases
[0] = NULL
;
367 return NSS_STATUS_SUCCESS
;
372 _nss_wins_gethostbyname2_r(const char *name
, int af
, struct hostent
*he
,
373 char *buffer
, size_t buflen
, int *h_errnop
)
377 return NSS_STATUS_UNAVAIL
;
380 return _nss_wins_gethostbyname_r(
381 name
, he
, buffer
, buflen
, h_errnop
);