source: branches/samba-3.2.x/source/winbindd/winbindd_wins.c

Last change on this file was 133, checked in by Paul Smedley, 17 years ago

Update trunk to 3.2.0pre3

File size: 5.7 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 Winbind daemon - WINS related functions
5
6 Copyright (C) Andrew Tridgell 1999
7 Copyright (C) Herb Lewis 2002
8
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.
13
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.
18
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/>.
21*/
22
23#include "includes.h"
24#include "winbindd.h"
25
26#undef DBGC_CLASS
27#define DBGC_CLASS DBGC_WINBIND
28
29/* Use our own create socket code so we don't recurse.... */
30
31static int wins_lookup_open_socket_in(void)
32{
33 struct sockaddr_in sock;
34 int val=1;
35 int res;
36
37 memset((char *)&sock,'\0',sizeof(sock));
38
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;
48
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 */
53
54 /* now we've got a socket - we need to bind it */
55
56 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
57 close(res);
58 return(-1);
59 }
60
61 set_socket_options(res,"SO_BROADCAST");
62
63 return res;
64}
65
66
67static NODE_STATUS_STRUCT *lookup_byaddr_backend(const char *addr, int *count)
68{
69 int fd;
70 struct sockaddr_storage ss;
71 struct nmb_name nname;
72 NODE_STATUS_STRUCT *status;
73
74 fd = wins_lookup_open_socket_in();
75 if (fd == -1)
76 return NULL;
77
78 make_nmb_name(&nname, "*", 0);
79 if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
80 return NULL;
81 }
82 status = node_status_query(fd, &nname, &ss, count, NULL);
83
84 close(fd);
85 return status;
86}
87
88static struct sockaddr_storage *lookup_byname_backend(const char *name,
89 int *count)
90{
91 int fd;
92 struct ip_service *ret = NULL;
93 struct sockaddr_storage *return_ss = NULL;
94 int j, i, flags = 0;
95
96 *count = 0;
97
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;
105 }
106
107 /* copy the IP addresses */
108 for ( i=0; i<(*count); i++ )
109 return_ss[i] = ret[i].ss;
110
111 free( ret );
112 return return_ss;
113 }
114
115 fd = wins_lookup_open_socket_in();
116 if (fd == -1) {
117 return NULL;
118 }
119
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;
127 }
128 return_ss = name_query(fd,name,0x20,True,True,bcast_ss,count, &flags, NULL);
129 if (return_ss) {
130 break;
131 }
132 }
133
134 close(fd);
135 return return_ss;
136}
137
138/* Get hostname from IP */
139
140void winbindd_wins_byip(struct winbindd_cli_state *state)
141{
142 fstring response;
143 int i, count, maxlen, size;
144 NODE_STATUS_STRUCT *status;
145
146 /* Ensure null termination */
147 state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
148
149 DEBUG(3, ("[%5lu]: wins_byip %s\n", (unsigned long)state->pid,
150 state->request.data.winsreq));
151
152 *response = '\0';
153 maxlen = sizeof(response) - 1;
154
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;
161 }
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;
173 }
174 fstrcat(response, status[i].name);
175 fstrcat(response, " ");
176 }
177 }
178 /* make last character a newline */
179 response[strlen(response)-1] = '\n';
180 SAFE_FREE(status);
181 }
182 fstrcpy(state->response.data.winsresp,response);
183 request_ok(state);
184}
185
186/* Get IP from hostname */
187
188void winbindd_wins_byname(struct winbindd_cli_state *state)
189{
190 struct sockaddr_storage *ip_list = NULL;
191 int i, count, maxlen, size;
192 fstring response;
193 char addr[INET6_ADDRSTRLEN];
194
195 /* Ensure null termination */
196 state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
197
198 DEBUG(3, ("[%5lu]: wins_byname %s\n", (unsigned long)state->pid,
199 state->request.data.winsreq));
200
201 *response = '\0';
202 maxlen = sizeof(response) - 1;
203
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;
212 }
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] = ' ';
219 }
220 }
221 fstrcat(response,addr);
222 fstrcat(response,"\t");
223 }
224 size = strlen(state->request.data.winsreq) + strlen(response);
225 if (size > maxlen) {
226 SAFE_FREE(ip_list);
227 request_error(state);
228 return;
229 }
230 fstrcat(response,state->request.data.winsreq);
231 fstrcat(response,"\n");
232 SAFE_FREE(ip_list);
233 } else {
234 request_error(state);
235 return;
236 }
237
238 fstrcpy(state->response.data.winsresp,response);
239
240 request_ok(state);
241}
Note: See TracBrowser for help on using the repository browser.