2 Unix SMB/CIFS implementation.
4 Winbind daemon - WINS related functions
6 Copyright (C) Andrew Tridgell 1999
7 Copyright (C) Herb Lewis 2002
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* Use our own create socket code so we don't recurse.... */
28 static int wins_lookup_open_socket_in(void)
30 struct sockaddr_in sock;
34 memset((char *)&sock,'\0',sizeof(sock));
36 #ifdef HAVE_SOCK_SIN_LEN
37 sock.sin_len = sizeof(sock);
40 sock.sin_family = AF_INET;
41 sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
42 res = socket(AF_INET, SOCK_DGRAM, 0);
46 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
48 setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val));
49 #endif /* SO_REUSEPORT */
51 /* now we've got a socket - we need to bind it */
53 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
58 set_socket_options(res,"SO_BROADCAST");
64 static struct node_status *lookup_byaddr_backend(char *addr, int *count)
68 struct nmb_name nname;
69 struct node_status *status;
71 fd = wins_lookup_open_socket_in();
75 make_nmb_name(&nname, "*", 0);
76 ip = *interpret_addr2(addr);
77 status = node_status_query(fd,&nname,ip, count);
83 static struct in_addr *lookup_byname_backend(const char *name, int *count)
86 struct in_addr *ret = NULL;
92 fd = wins_lookup_open_socket_in();
98 ret = name_query(fd,name,0x20,False,True, p, count);
102 if (lp_wins_support()) {
103 /* we are our own WINS server */
104 ret = name_query(fd,name,0x20,False,True, *interpret_addr2("127.0.0.1"), count);
108 /* uggh, we have to broadcast to each interface in turn */
109 for (j=iface_count() - 1;
112 struct in_addr *bcast = iface_n_bcast(j);
113 ret = name_query(fd,name,0x20,True,True,*bcast,count);
123 /* Get hostname from IP */
125 enum winbindd_result winbindd_wins_byip(struct winbindd_cli_state *state)
128 int i, count, len, size;
129 struct node_status *status;
131 DEBUG(3, ("[%5d]: wins_byip %s\n", state->pid,
132 state->request.data.name));
135 len = sizeof(response) - 2;
137 if ((status = lookup_byaddr_backend(state->request.data.name, &count))){
138 size = strlen(state->request.data.name) + 1;
141 return WINBINDD_ERROR;
144 safe_strcat(response,state->request.data.name,size);
145 safe_strcat(response,"\t",1);
146 for (i = 0; i < count; i++) {
147 /* ignore group names */
148 if (status[i].flags & 0x80) continue;
149 if (status[i].type == 0x20) {
150 size = sizeof(status[i].name) + 1;
153 return WINBINDD_ERROR;
156 safe_strcat(response, status[i].name, size);
157 safe_strcat(response, " ", 1);
160 response[strlen(response)-1] = '\n';
163 fstrcpy(state->response.data.name.name,response);
167 /* Get IP from hostname */
169 enum winbindd_result winbindd_wins_byname(struct winbindd_cli_state *state)
171 struct in_addr *ip_list;
172 int i, count, len, size;
176 DEBUG(3, ("[%5d]: wins_byname %s\n", state->pid,
177 state->request.data.name));
180 len = sizeof(response) - 2;
182 if ((ip_list = lookup_byname_backend(state->request.data.name,&count))){
183 for (i = count; i ; i--) {
184 addr = inet_ntoa(ip_list[i-1]);
185 size = strlen(addr) + 1;
188 return WINBINDD_ERROR;
192 response[strlen(response)-1] = ' ';
193 safe_strcat(response,addr,size);
194 safe_strcat(response,"\t",1);
196 size = strlen(state->request.data.name) + 1;
199 return WINBINDD_ERROR;
201 safe_strcat(response,state->request.data.name,size);
202 safe_strcat(response,"\n",1);
205 return WINBINDD_ERROR;
207 fstrcpy(state->response.data.name.name,response);