Add comment explaining the previous fix.
[Samba.git] / source / winbindd / winbindd_wins.c
blob4a3d2682b65f325ae62592878519cca93c4ca9cc
1 /*
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/>.
23 #include "includes.h"
24 #include "winbindd.h"
26 #undef DBGC_CLASS
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;
34 int val=1;
35 int res;
37 memset((char *)&sock,'\0',sizeof(sock));
39 #ifdef HAVE_SOCK_SIN_LEN
40 sock.sin_len = sizeof(sock);
41 #endif
42 sock.sin_port = 0;
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);
46 if (res == -1)
47 return -1;
49 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
50 #ifdef SO_REUSEPORT
51 setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val));
52 #endif /* SO_REUSEPORT */
54 /* now we've got a socket - we need to bind it */
56 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
57 close(res);
58 return(-1);
61 set_socket_options(res,"SO_BROADCAST");
63 return res;
67 static NODE_STATUS_STRUCT *lookup_byaddr_backend(const char *addr, int *count)
69 int fd;
70 struct sockaddr_storage ss;
71 struct nmb_name nname;
72 NODE_STATUS_STRUCT *status;
74 fd = wins_lookup_open_socket_in();
75 if (fd == -1)
76 return NULL;
78 make_nmb_name(&nname, "*", 0);
79 if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
80 return NULL;
82 status = node_status_query(fd, &nname, &ss, count, NULL);
84 close(fd);
85 return status;
88 static struct sockaddr_storage *lookup_byname_backend(const char *name,
89 int *count)
91 int fd;
92 struct ip_service *ret = NULL;
93 struct sockaddr_storage *return_ss = NULL;
94 int j, i, flags = 0;
96 *count = 0;
98 /* always try with wins first */
99 if (NT_STATUS_IS_OK(resolve_wins(name,0x20,&ret,count))) {
100 if ( *count == 0 )
101 return NULL;
102 if ( (return_ss = SMB_MALLOC_ARRAY(struct sockaddr_storage, *count)) == NULL ) {
103 free( ret );
104 return NULL;
107 /* copy the IP addresses */
108 for ( i=0; i<(*count); i++ )
109 return_ss[i] = ret[i].ss;
111 free( ret );
112 return return_ss;
115 fd = wins_lookup_open_socket_in();
116 if (fd == -1) {
117 return NULL;
120 /* uggh, we have to broadcast to each interface in turn */
121 for (j=iface_count() - 1;
122 j >= 0;
123 j--) {
124 const struct sockaddr_storage *bcast_ss = iface_n_bcast(j);
125 if (!bcast_ss) {
126 continue;
128 return_ss = name_query(fd,name,0x20,True,True,bcast_ss,count, &flags, NULL);
129 if (return_ss) {
130 break;
134 close(fd);
135 return return_ss;
138 /* Get hostname from IP */
140 void winbindd_wins_byip(struct winbindd_cli_state *state)
142 fstring response;
143 int i, count, maxlen, size;
144 NODE_STATUS_STRUCT *status;
146 /* Ensure null termination */
147 state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
149 DEBUG(3, ("[%5lu]: wins_byip %s\n", (unsigned long)state->pid,
150 state->request.data.winsreq));
152 *response = '\0';
153 maxlen = sizeof(response) - 1;
155 if ((status = lookup_byaddr_backend(state->request.data.winsreq, &count))){
156 size = strlen(state->request.data.winsreq);
157 if (size > maxlen) {
158 SAFE_FREE(status);
159 request_error(state);
160 return;
162 fstrcat(response,state->request.data.winsreq);
163 fstrcat(response,"\t");
164 for (i = 0; i < count; i++) {
165 /* ignore group names */
166 if (status[i].flags & 0x80) continue;
167 if (status[i].type == 0x20) {
168 size = sizeof(status[i].name) + strlen(response);
169 if (size > maxlen) {
170 SAFE_FREE(status);
171 request_error(state);
172 return;
174 fstrcat(response, status[i].name);
175 fstrcat(response, " ");
178 /* make last character a newline */
179 response[strlen(response)-1] = '\n';
180 SAFE_FREE(status);
182 fstrcpy(state->response.data.winsresp,response);
183 request_ok(state);
186 /* Get IP from hostname */
188 void winbindd_wins_byname(struct winbindd_cli_state *state)
190 struct sockaddr_storage *ip_list = NULL;
191 int i, count, maxlen, size;
192 fstring response;
193 char addr[INET6_ADDRSTRLEN];
195 /* Ensure null termination */
196 state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
198 DEBUG(3, ("[%5lu]: wins_byname %s\n", (unsigned long)state->pid,
199 state->request.data.winsreq));
201 *response = '\0';
202 maxlen = sizeof(response) - 1;
204 if ((ip_list = lookup_byname_backend(state->request.data.winsreq,&count))){
205 for (i = count; i ; i--) {
206 print_sockaddr(addr, sizeof(addr), &ip_list[i-1]);
207 size = strlen(addr);
208 if (size > maxlen) {
209 SAFE_FREE(ip_list);
210 request_error(state);
211 return;
213 if (i != 0) {
214 /* Clear out the newline character */
215 /* But only if there is something in there,
216 otherwise we clobber something in the stack */
217 if (strlen(response)) {
218 response[strlen(response)-1] = ' ';
221 fstrcat(response,addr);
222 fstrcat(response,"\t");
224 size = strlen(state->request.data.winsreq) + strlen(response);
225 if (size > maxlen) {
226 SAFE_FREE(ip_list);
227 request_error(state);
228 return;
230 fstrcat(response,state->request.data.winsreq);
231 fstrcat(response,"\n");
232 SAFE_FREE(ip_list);
233 } else {
234 request_error(state);
235 return;
238 fstrcpy(state->response.data.winsresp,response);
240 request_ok(state);