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.
28 #include <ns_daemon.h>
35 static int initialised
;
37 extern BOOL AllowDebugChange
;
39 /* Use our own create socket code so we don't recurse.... */
41 static int wins_lookup_open_socket_in(void)
43 struct sockaddr_in sock
;
47 memset((char *)&sock
,'\0',sizeof(sock
));
49 #ifdef HAVE_SOCK_SIN_LEN
50 sock
.sin_len
= sizeof(sock
);
53 sock
.sin_family
= AF_INET
;
54 sock
.sin_addr
.s_addr
= interpret_addr("0.0.0.0");
55 res
= socket(AF_INET
, SOCK_DGRAM
, 0);
59 setsockopt(res
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&val
,sizeof(val
));
61 setsockopt(res
,SOL_SOCKET
,SO_REUSEPORT
,(char *)&val
,sizeof(val
));
62 #endif /* SO_REUSEPORT */
64 /* now we've got a socket - we need to bind it */
66 if (bind(res
, (struct sockaddr
* ) &sock
,sizeof(sock
)) < 0) {
71 set_socket_options(res
,"SO_BROADCAST");
77 static void nss_wins_init(void)
81 AllowDebugChange
= False
;
84 setup_logging("nss_wins",False
);
85 lp_load(dyn_CONFIGFILE
,True
,False
,False
);
89 static struct node_status
*lookup_byaddr_backend(char *addr
, int *count
)
93 struct nmb_name nname
;
94 struct node_status
*status
;
100 fd
= wins_lookup_open_socket_in();
104 make_nmb_name(&nname
, "*", 0);
105 ip
= *interpret_addr2(addr
);
106 status
= node_status_query(fd
,&nname
,ip
, count
);
112 static struct in_addr
*lookup_byname_backend(const char *name
, int *count
)
115 struct in_addr
*ret
= NULL
;
125 /* always try with wins first */
126 if (resolve_wins(name
,0x20,&ret
,count
)) {
130 fd
= wins_lookup_open_socket_in();
135 /* uggh, we have to broadcast to each interface in turn */
136 for (j
=iface_count() - 1;j
>= 0;j
--) {
137 struct in_addr
*bcast
= iface_n_bcast(j
);
138 ret
= name_query(fd
,name
,0x20,True
,True
,*bcast
,count
, &flags
, NULL
);
153 nsd_logprintf(NSD_LOG_MIN
, "entering init (wins)\n");
158 int lookup(nsd_file_t
*rq
)
163 struct in_addr
*ip_list
;
164 struct node_status
*status
;
165 int i
, count
, len
, size
;
169 nsd_logprintf(NSD_LOG_MIN
, "entering lookup (wins)\n");
173 map
= nsd_attr_fetch_string(rq
->f_attrs
, "table", (char*)0);
175 rq
->f_status
= NS_FATAL
;
179 key
= nsd_attr_fetch_string(rq
->f_attrs
, "key", (char*)0);
180 if (! key
|| ! *key
) {
181 rq
->f_status
= NS_FATAL
;
186 len
= sizeof(response
) - 2;
189 * response needs to be a string of the following format
190 * ip_address[ ip_address]*\tname[ alias]*
192 if (strcasecmp(map
,"hosts.byaddr") == 0) {
193 if ( status
= lookup_byaddr_backend(key
, &count
)) {
194 size
= strlen(key
) + 1;
200 strncat(response
,key
,size
);
201 strncat(response
,"\t",1);
202 for (i
= 0; i
< count
; i
++) {
203 /* ignore group names */
204 if (status
[i
].flags
& 0x80) continue;
205 if (status
[i
].type
== 0x20) {
206 size
= sizeof(status
[i
].name
) + 1;
212 strncat(response
, status
[i
].name
, size
);
213 strncat(response
, " ", 1);
217 response
[strlen(response
)-1] = '\n';
220 } else if (strcasecmp(map
,"hosts.byname") == 0) {
221 if (ip_list
= lookup_byname_backend(key
, &count
)) {
222 for (i
= count
; i
; i
--) {
223 addr
= inet_ntoa(ip_list
[i
-1]);
224 size
= strlen(addr
) + 1;
231 response
[strlen(response
)-1] = ' ';
232 strncat(response
,addr
,size
);
233 strncat(response
,"\t",1);
235 size
= strlen(key
) + 1;
240 strncat(response
,key
,size
);
241 strncat(response
,"\n",1);
248 nsd_logprintf(NSD_LOG_LOW
, "lookup (wins %s) %s\n",map
,response
);
249 nsd_set_result(rq
,NS_SUCCESS
,response
,strlen(response
),VOLATILE
);
252 nsd_logprintf(NSD_LOG_LOW
, "lookup (wins) not found\n");
253 rq
->f_status
= NS_NOTFOUND
;
258 /****************************************************************************
259 gethostbyname() - we ignore any domain portion of the name and only
260 handle names that are at most 15 characters long
261 **************************************************************************/
263 _nss_wins_gethostbyname_r(const char *name
, struct hostent
*he
,
264 char *buffer
, size_t buflen
, int *errnop
,
267 char **host_addresses
;
268 struct in_addr
*ip_list
;
270 size_t namelen
= strlen(name
) + 1;
272 memset(he
, '\0', sizeof(*he
));
274 ip_list
= lookup_byname_backend(name
, &count
);
276 return NSS_STATUS_NOTFOUND
;
279 if (buflen
< namelen
+ (2*count
+1)*INADDRSZ
) {
280 /* no ENOMEM error type?! */
281 return NSS_STATUS_NOTFOUND
;
285 host_addresses
= (char **)buffer
;
286 he
->h_addr_list
= host_addresses
;
287 host_addresses
[count
] = NULL
;
288 buffer
+= (count
+ 1) * INADDRSZ
;
289 buflen
+= (count
+ 1) * INADDRSZ
;
290 he
->h_addrtype
= AF_INET
;
291 he
->h_length
= INADDRSZ
;
293 for (i
=0;i
<count
;i
++) {
294 memcpy(buffer
, &ip_list
[i
].s_addr
, INADDRSZ
);
295 *host_addresses
= buffer
;
304 memcpy(buffer
, name
, namelen
);
307 return NSS_STATUS_SUCCESS
;
312 _nss_wins_gethostbyname2_r(const char *name
, int af
, struct hostent
*he
,
313 char *buffer
, size_t buflen
, int *errnop
,
318 *errnop
= EAFNOSUPPORT
;
319 return NSS_STATUS_UNAVAIL
;
322 return _nss_wins_gethostbyname_r(name
,he
,buffer
,buflen
,errnop
,h_errnop
);