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 /* Use our own create socket code so we don't recurse.... */
39 static int wins_lookup_open_socket_in(void)
41 struct sockaddr_in sock
;
45 memset((char *)&sock
,'\0',sizeof(sock
));
47 #ifdef HAVE_SOCK_SIN_LEN
48 sock
.sin_len
= sizeof(sock
);
51 sock
.sin_family
= AF_INET
;
52 sock
.sin_addr
.s_addr
= interpret_addr("0.0.0.0");
53 res
= socket(AF_INET
, SOCK_DGRAM
, 0);
57 setsockopt(res
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&val
,sizeof(val
));
59 setsockopt(res
,SOL_SOCKET
,SO_REUSEPORT
,(char *)&val
,sizeof(val
));
60 #endif /* SO_REUSEPORT */
62 /* now we've got a socket - we need to bind it */
64 if (bind(res
, (struct sockaddr
* ) &sock
,sizeof(sock
)) < 0) {
69 set_socket_options(res
,"SO_BROADCAST");
75 static void nss_wins_init(void)
79 AllowDebugChange
= False
;
82 setup_logging("nss_wins",DEBUG_FILE
);
83 lp_load(dyn_CONFIGFILE
,True
,False
,False
);
87 static struct node_status
*lookup_byaddr_backend(char *addr
, int *count
)
91 struct nmb_name nname
;
92 struct node_status
*status
;
98 fd
= wins_lookup_open_socket_in();
102 make_nmb_name(&nname
, "*", 0);
103 ip
= *interpret_addr2(addr
);
104 status
= node_status_query(fd
,&nname
,ip
, count
);
110 static struct in_addr
*lookup_byname_backend(const char *name
, int *count
)
113 struct in_addr
*ret
= NULL
;
123 /* always try with wins first */
124 if (resolve_wins(name
,0x20,&ret
,count
)) {
128 fd
= wins_lookup_open_socket_in();
133 /* uggh, we have to broadcast to each interface in turn */
134 for (j
=iface_count() - 1;j
>= 0;j
--) {
135 struct in_addr
*bcast
= iface_n_bcast(j
);
136 ret
= name_query(fd
,name
,0x20,True
,True
,*bcast
,count
, &flags
, NULL
);
151 nsd_logprintf(NSD_LOG_MIN
, "entering init (wins)\n");
156 int lookup(nsd_file_t
*rq
)
161 struct in_addr
*ip_list
;
162 struct node_status
*status
;
163 int i
, count
, len
, size
;
167 nsd_logprintf(NSD_LOG_MIN
, "entering lookup (wins)\n");
171 map
= nsd_attr_fetch_string(rq
->f_attrs
, "table", (char*)0);
173 rq
->f_status
= NS_FATAL
;
177 key
= nsd_attr_fetch_string(rq
->f_attrs
, "key", (char*)0);
178 if (! key
|| ! *key
) {
179 rq
->f_status
= NS_FATAL
;
184 len
= sizeof(response
) - 2;
187 * response needs to be a string of the following format
188 * ip_address[ ip_address]*\tname[ alias]*
190 if (strcasecmp(map
,"hosts.byaddr") == 0) {
191 if ( status
= lookup_byaddr_backend(key
, &count
)) {
192 size
= strlen(key
) + 1;
198 strncat(response
,key
,size
);
199 strncat(response
,"\t",1);
200 for (i
= 0; i
< count
; i
++) {
201 /* ignore group names */
202 if (status
[i
].flags
& 0x80) continue;
203 if (status
[i
].type
== 0x20) {
204 size
= sizeof(status
[i
].name
) + 1;
210 strncat(response
, status
[i
].name
, size
);
211 strncat(response
, " ", 1);
215 response
[strlen(response
)-1] = '\n';
218 } else if (strcasecmp(map
,"hosts.byname") == 0) {
219 if (ip_list
= lookup_byname_backend(key
, &count
)) {
220 for (i
= count
; i
; i
--) {
221 addr
= inet_ntoa(ip_list
[i
-1]);
222 size
= strlen(addr
) + 1;
229 response
[strlen(response
)-1] = ' ';
230 strncat(response
,addr
,size
);
231 strncat(response
,"\t",1);
233 size
= strlen(key
) + 1;
238 strncat(response
,key
,size
);
239 strncat(response
,"\n",1);
246 nsd_logprintf(NSD_LOG_LOW
, "lookup (wins %s) %s\n",map
,response
);
247 nsd_set_result(rq
,NS_SUCCESS
,response
,strlen(response
),VOLATILE
);
250 nsd_logprintf(NSD_LOG_LOW
, "lookup (wins) not found\n");
251 rq
->f_status
= NS_NOTFOUND
;
256 /****************************************************************************
257 gethostbyname() - we ignore any domain portion of the name and only
258 handle names that are at most 15 characters long
259 **************************************************************************/
261 _nss_wins_gethostbyname_r(const char *name
, struct hostent
*he
,
262 char *buffer
, size_t buflen
, int *errnop
,
265 char **host_addresses
;
266 struct in_addr
*ip_list
;
268 size_t namelen
= strlen(name
) + 1;
270 memset(he
, '\0', sizeof(*he
));
272 ip_list
= lookup_byname_backend(name
, &count
);
274 return NSS_STATUS_NOTFOUND
;
277 if (buflen
< namelen
+ (2*count
+1)*INADDRSZ
) {
278 /* no ENOMEM error type?! */
279 return NSS_STATUS_NOTFOUND
;
283 host_addresses
= (char **)buffer
;
284 he
->h_addr_list
= host_addresses
;
285 host_addresses
[count
] = NULL
;
286 buffer
+= (count
+ 1) * INADDRSZ
;
287 buflen
+= (count
+ 1) * INADDRSZ
;
288 he
->h_addrtype
= AF_INET
;
289 he
->h_length
= INADDRSZ
;
291 for (i
=0;i
<count
;i
++) {
292 memcpy(buffer
, &ip_list
[i
].s_addr
, INADDRSZ
);
293 *host_addresses
= buffer
;
302 memcpy(buffer
, name
, namelen
);
305 return NSS_STATUS_SUCCESS
;
310 _nss_wins_gethostbyname2_r(const char *name
, int af
, struct hostent
*he
,
311 char *buffer
, size_t buflen
, int *errnop
,
316 *errnop
= EAFNOSUPPORT
;
317 return NSS_STATUS_UNAVAIL
;
320 return _nss_wins_gethostbyname_r(name
,he
,buffer
,buflen
,errnop
,h_errnop
);