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 in_addr *lookup_byname_backend(const char *name, int *count)
92 struct ip_service *address = NULL;
102 /* always try with wins first */
103 if (resolve_wins(name,0x20,&address,count)) {
104 if ( (ret = (struct in_addr *)malloc(sizeof(struct in_addr))) == NULL ) {
108 *ret = address[0].ip;
113 fd = wins_lookup_open_socket_in();
118 /* uggh, we have to broadcast to each interface in turn */
119 for (j=iface_count() - 1;j >= 0;j--) {
120 struct in_addr *bcast = iface_n_bcast(j);
121 ret = name_query(fd,name,0x20,True,True,*bcast,count, &flags, NULL);
131 static struct node_status *lookup_byaddr_backend(char *addr, int *count)
135 struct nmb_name nname;
136 struct node_status *status;
142 fd = wins_lookup_open_socket_in();
146 make_nmb_name(&nname, "*", 0);
147 ip = *interpret_addr2(addr);
148 status = node_status_query(fd,&nname,ip, count);
158 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
163 int lookup(nsd_file_t *rq)
168 struct in_addr *ip_list;
169 struct node_status *status;
170 int i, count, len, size;
174 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
178 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
180 rq->f_status = NS_FATAL;
184 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
185 if (! key || ! *key) {
186 rq->f_status = NS_FATAL;
191 len = sizeof(response) - 2;
194 * response needs to be a string of the following format
195 * ip_address[ ip_address]*\tname[ alias]*
197 if (strcasecmp(map,"hosts.byaddr") == 0) {
198 if ( status = lookup_byaddr_backend(key, &count)) {
199 size = strlen(key) + 1;
205 strncat(response,key,size);
206 strncat(response,"\t",1);
207 for (i = 0; i < count; i++) {
208 /* ignore group names */
209 if (status[i].flags & 0x80) continue;
210 if (status[i].type == 0x20) {
211 size = sizeof(status[i].name) + 1;
217 strncat(response, status[i].name, size);
218 strncat(response, " ", 1);
222 response[strlen(response)-1] = '\n';
225 } else if (strcasecmp(map,"hosts.byname") == 0) {
226 if (ip_list = lookup_byname_backend(key, &count)) {
227 for (i = count; i ; i--) {
228 addr = inet_ntoa(ip_list[i-1]);
229 size = strlen(addr) + 1;
236 response[strlen(response)-1] = ' ';
237 strncat(response,addr,size);
238 strncat(response,"\t",1);
240 size = strlen(key) + 1;
245 strncat(response,key,size);
246 strncat(response,"\n",1);
253 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
254 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
257 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
258 rq->f_status = NS_NOTFOUND;
263 /****************************************************************************
264 gethostbyname() - we ignore any domain portion of the name and only
265 handle names that are at most 15 characters long
266 **************************************************************************/
268 _nss_wins_gethostbyname_r(const char *name, struct hostent *he,
269 char *buffer, size_t buflen, int *errnop,
272 char **host_addresses;
273 struct in_addr *ip_list;
275 size_t namelen = strlen(name) + 1;
277 memset(he, '\0', sizeof(*he));
279 ip_list = lookup_byname_backend(name, &count);
281 return NSS_STATUS_NOTFOUND;
284 if (buflen < namelen + (2*count+1)*INADDRSZ) {
285 /* no ENOMEM error type?! */
286 return NSS_STATUS_NOTFOUND;
290 host_addresses = (char **)buffer;
291 he->h_addr_list = host_addresses;
292 host_addresses[count] = NULL;
293 buffer += (count + 1) * INADDRSZ;
294 buflen += (count + 1) * INADDRSZ;
295 he->h_addrtype = AF_INET;
296 he->h_length = INADDRSZ;
298 for (i=0;i<count;i++) {
299 memcpy(buffer, &ip_list[i].s_addr, INADDRSZ);
300 *host_addresses = buffer;
309 memcpy(buffer, name, namelen);
312 return NSS_STATUS_SUCCESS;
317 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
318 char *buffer, size_t buflen, int *errnop,
323 *errnop = EAFNOSUPPORT;
324 return NSS_STATUS_UNAVAIL;
327 return _nss_wins_gethostbyname_r(name,he,buffer,buflen,errnop,h_errnop);