r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / nsswitch / winbindd_wins.c
blobb3c1f2c681e92bdbef5948ee697c22517644b959
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(char *addr, int *count)
69 int fd;
70 struct in_addr ip;
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 ip = *interpret_addr2(addr);
80 status = node_status_query(fd,&nname,ip, count, NULL);
82 close(fd);
83 return status;
86 static struct in_addr *lookup_byname_backend(const char *name, int *count)
88 int fd;
89 struct ip_service *ret = NULL;
90 struct in_addr *return_ip = NULL;
91 int j, i, flags = 0;
93 *count = 0;
95 /* always try with wins first */
96 if (resolve_wins(name,0x20,&ret,count)) {
97 if ( *count == 0 )
98 return NULL;
99 if ( (return_ip = SMB_MALLOC_ARRAY(struct in_addr, *count)) == NULL ) {
100 free( ret );
101 return NULL;
104 /* copy the IP addresses */
105 for ( i=0; i<(*count); i++ )
106 return_ip[i] = ret[i].ip;
108 free( ret );
109 return return_ip;
112 fd = wins_lookup_open_socket_in();
113 if (fd == -1) {
114 return NULL;
117 /* uggh, we have to broadcast to each interface in turn */
118 for (j=iface_count() - 1;
119 j >= 0;
120 j--) {
121 struct in_addr *bcast = iface_n_bcast(j);
122 return_ip = name_query(fd,name,0x20,True,True,*bcast,count, &flags, NULL);
123 if (return_ip) {
124 break;
128 close(fd);
129 return return_ip;
132 /* Get hostname from IP */
134 void winbindd_wins_byip(struct winbindd_cli_state *state)
136 fstring response;
137 int i, count, maxlen, size;
138 NODE_STATUS_STRUCT *status;
140 /* Ensure null termination */
141 state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
143 DEBUG(3, ("[%5lu]: wins_byip %s\n", (unsigned long)state->pid,
144 state->request.data.winsreq));
146 *response = '\0';
147 maxlen = sizeof(response) - 1;
149 if ((status = lookup_byaddr_backend(state->request.data.winsreq, &count))){
150 size = strlen(state->request.data.winsreq);
151 if (size > maxlen) {
152 SAFE_FREE(status);
153 request_error(state);
154 return;
156 fstrcat(response,state->request.data.winsreq);
157 fstrcat(response,"\t");
158 for (i = 0; i < count; i++) {
159 /* ignore group names */
160 if (status[i].flags & 0x80) continue;
161 if (status[i].type == 0x20) {
162 size = sizeof(status[i].name) + strlen(response);
163 if (size > maxlen) {
164 SAFE_FREE(status);
165 request_error(state);
166 return;
168 fstrcat(response, status[i].name);
169 fstrcat(response, " ");
172 /* make last character a newline */
173 response[strlen(response)-1] = '\n';
174 SAFE_FREE(status);
176 fstrcpy(state->response.data.winsresp,response);
177 request_ok(state);
180 /* Get IP from hostname */
182 void winbindd_wins_byname(struct winbindd_cli_state *state)
184 struct in_addr *ip_list;
185 int i, count, maxlen, size;
186 fstring response;
187 char * addr;
189 /* Ensure null termination */
190 state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
192 DEBUG(3, ("[%5lu]: wins_byname %s\n", (unsigned long)state->pid,
193 state->request.data.winsreq));
195 *response = '\0';
196 maxlen = sizeof(response) - 1;
198 if ((ip_list = lookup_byname_backend(state->request.data.winsreq,&count))){
199 for (i = count; i ; i--) {
200 addr = inet_ntoa(ip_list[i-1]);
201 size = strlen(addr);
202 if (size > maxlen) {
203 SAFE_FREE(ip_list);
204 request_error(state);
205 return;
207 if (i != 0) {
208 /* Clear out the newline character */
209 /* But only if there is something in there,
210 otherwise we clobber something in the stack */
211 if (strlen(response))
212 response[strlen(response)-1] = ' ';
214 fstrcat(response,addr);
215 fstrcat(response,"\t");
217 size = strlen(state->request.data.winsreq) + strlen(response);
218 if (size > maxlen) {
219 SAFE_FREE(ip_list);
220 request_error(state);
221 return;
223 fstrcat(response,state->request.data.winsreq);
224 fstrcat(response,"\n");
225 SAFE_FREE(ip_list);
226 } else {
227 request_error(state);
228 return;
231 fstrcpy(state->response.data.winsresp,response);
233 request_ok(state);