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 3 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, see <http://www.gnu.org/licenses/>.
27 #define DBGC_CLASS DBGC_WINBIND
29 /* Use our own create socket code so we don't recurse.... */
31 static int wins_lookup_open_socket_in(void)
33 struct sockaddr_in sock;
37 memset((char *)&sock,'\0',sizeof(sock));
39 #ifdef HAVE_SOCK_SIN_LEN
40 sock.sin_len = sizeof(sock);
43 sock.sin_family = AF_INET;
44 sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
45 res = socket(AF_INET, SOCK_DGRAM, 0);
49 if (setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val))) {
54 if (setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val))) {
58 #endif /* SO_REUSEPORT */
60 /* now we've got a socket - we need to bind it */
62 if (bind(res, (struct sockaddr *)(void *)&sock, sizeof(sock)) < 0) {
67 set_socket_options(res,"SO_BROADCAST");
73 static struct node_status *lookup_byaddr_backend(TALLOC_CTX *mem_ctx,
74 const char *addr, int *count)
77 struct sockaddr_storage ss;
78 struct nmb_name nname;
79 struct node_status *result;
82 fd = wins_lookup_open_socket_in();
86 make_nmb_name(&nname, "*", 0);
87 if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
90 status = node_status_query(fd, &nname, &ss, mem_ctx,
91 &result, count, NULL);
93 if (!NT_STATUS_IS_OK(status)) {
99 static struct sockaddr_storage *lookup_byname_backend(const char *name,
103 struct ip_service *ret = NULL;
104 struct sockaddr_storage *return_ss = NULL;
109 /* always try with wins first */
110 if (NT_STATUS_IS_OK(resolve_wins(name,0x20,&ret,count))) {
113 if ( (return_ss = SMB_MALLOC_ARRAY(struct sockaddr_storage, *count)) == NULL ) {
118 /* copy the IP addresses */
119 for ( i=0; i<(*count); i++ )
120 return_ss[i] = ret[i].ss;
126 fd = wins_lookup_open_socket_in();
131 /* uggh, we have to broadcast to each interface in turn */
132 for (j=iface_count() - 1;
135 const struct sockaddr_storage *bcast_ss = iface_n_bcast(j);
139 return_ss = name_query(fd,name,0x20,True,True,bcast_ss,count, &flags, NULL);
149 /* Get hostname from IP */
151 void winbindd_wins_byip(struct winbindd_cli_state *state)
154 int i, count, maxlen, size;
155 struct node_status *status;
157 /* Ensure null termination */
158 state->request->data.winsreq[sizeof(state->request->data.winsreq)-1]='\0';
160 DEBUG(3, ("[%5lu]: wins_byip %s\n", (unsigned long)state->pid,
161 state->request->data.winsreq));
164 maxlen = sizeof(response) - 1;
166 if ((status = lookup_byaddr_backend(
167 state->mem_ctx, state->request->data.winsreq, &count))) {
168 size = strlen(state->request->data.winsreq);
171 request_error(state);
174 fstrcat(response,state->request->data.winsreq);
175 fstrcat(response,"\t");
176 for (i = 0; i < count; i++) {
177 /* ignore group names */
178 if (status[i].flags & 0x80) continue;
179 if (status[i].type == 0x20) {
180 size = sizeof(status[i].name) + strlen(response);
183 request_error(state);
186 fstrcat(response, status[i].name);
187 fstrcat(response, " ");
190 /* make last character a newline */
191 response[strlen(response)-1] = '\n';
194 fstrcpy(state->response->data.winsresp,response);
198 /* Get IP from hostname */
200 void winbindd_wins_byname(struct winbindd_cli_state *state)
202 struct sockaddr_storage *ip_list = NULL;
203 int i, count, maxlen, size;
205 char addr[INET6_ADDRSTRLEN];
207 /* Ensure null termination */
208 state->request->data.winsreq[sizeof(state->request->data.winsreq)-1]='\0';
210 DEBUG(3, ("[%5lu]: wins_byname %s\n", (unsigned long)state->pid,
211 state->request->data.winsreq));
214 maxlen = sizeof(response) - 1;
216 if ((ip_list = lookup_byname_backend(state->request->data.winsreq,&count))){
217 for (i = count; i ; i--) {
218 print_sockaddr(addr, sizeof(addr), &ip_list[i-1]);
222 request_error(state);
226 /* Clear out the newline character */
227 /* But only if there is something in there,
228 otherwise we clobber something in the stack */
229 if (strlen(response)) {
230 response[strlen(response)-1] = ' ';
233 fstrcat(response,addr);
234 fstrcat(response,"\t");
236 size = strlen(state->request->data.winsreq) + strlen(response);
239 request_error(state);
242 fstrcat(response,state->request->data.winsreq);
243 fstrcat(response,"\n");
246 request_error(state);
250 fstrcpy(state->response->data.winsresp,response);