2ee6f69b66a77ff75b25ce2b8a3f7e06e3df60cf
[tprouty/samba.git] / source3 / winbindd / winbindd_wins.c
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
31 static 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
67 static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
68 {
69         int fd;
70         struct in_addr  ip;
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         ip = *interpret_addr2(addr);
80         status = node_status_query(fd,&nname,ip, count, NULL);
81
82         close(fd);
83         return status;
84 }
85
86 static struct in_addr *lookup_byname_backend(const char *name, int *count)
87 {
88         int fd;
89         struct ip_service *ret = NULL;
90         struct in_addr *return_ip = NULL;
91         int j, i, flags = 0;
92
93         *count = 0;
94
95         /* always try with wins first */
96         if (NT_STATUS_IS_OK(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;
102                 }
103
104                 /* copy the IP addresses */
105                 for ( i=0; i<(*count); i++ ) 
106                         return_ip[i] = ret[i].ip;
107                 
108                 free( ret );
109                 return return_ip;
110         }
111
112         fd = wins_lookup_open_socket_in();
113         if (fd == -1) {
114                 return NULL;
115         }
116
117         /* uggh, we have to broadcast to each interface in turn */
118         for (j=iface_count() - 1;
119              j >= 0;
120              j--) {
121                 const struct in_addr *bcast = iface_n_bcast_v4(j);
122                 if (!bcast) {
123                         continue;
124                 }
125                 return_ip = name_query(fd,name,0x20,True,True,*bcast,count, &flags, NULL);
126                 if (return_ip) {
127                         break;
128                 }
129         }
130
131         close(fd);
132         return return_ip;
133 }
134
135 /* Get hostname from IP  */
136
137 void winbindd_wins_byip(struct winbindd_cli_state *state)
138 {
139         fstring response;
140         int i, count, maxlen, size;
141         NODE_STATUS_STRUCT *status;
142
143         /* Ensure null termination */
144         state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
145
146         DEBUG(3, ("[%5lu]: wins_byip %s\n", (unsigned long)state->pid,
147                 state->request.data.winsreq));
148
149         *response = '\0';
150         maxlen = sizeof(response) - 1;
151
152         if ((status = lookup_byaddr_backend(state->request.data.winsreq, &count))){
153             size = strlen(state->request.data.winsreq);
154             if (size > maxlen) {
155                 SAFE_FREE(status);
156                 request_error(state);
157                 return;
158             }
159             fstrcat(response,state->request.data.winsreq);
160             fstrcat(response,"\t");
161             for (i = 0; i < count; i++) {
162                 /* ignore group names */
163                 if (status[i].flags & 0x80) continue;
164                 if (status[i].type == 0x20) {
165                         size = sizeof(status[i].name) + strlen(response);
166                         if (size > maxlen) {
167                             SAFE_FREE(status);
168                             request_error(state);
169                             return;
170                         }
171                         fstrcat(response, status[i].name);
172                         fstrcat(response, " ");
173                 }
174             }
175             /* make last character a newline */
176             response[strlen(response)-1] = '\n';
177             SAFE_FREE(status);
178         }
179         fstrcpy(state->response.data.winsresp,response);
180         request_ok(state);
181 }
182
183 /* Get IP from hostname */
184
185 void winbindd_wins_byname(struct winbindd_cli_state *state)
186 {
187         struct in_addr *ip_list;
188         int i, count, maxlen, size;
189         fstring response;
190         char * addr;
191
192         /* Ensure null termination */
193         state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
194
195         DEBUG(3, ("[%5lu]: wins_byname %s\n", (unsigned long)state->pid,
196                 state->request.data.winsreq));
197
198         *response = '\0';
199         maxlen = sizeof(response) - 1;
200
201         if ((ip_list = lookup_byname_backend(state->request.data.winsreq,&count))){
202                 for (i = count; i ; i--) {
203                     addr = inet_ntoa(ip_list[i-1]);
204                     size = strlen(addr);
205                     if (size > maxlen) {
206                         SAFE_FREE(ip_list);
207                         request_error(state);
208                         return;
209                     }
210                     if (i != 0) {
211                         /* Clear out the newline character */
212                         /* But only if there is something in there, 
213                            otherwise we clobber something in the stack */
214                         if (strlen(response))
215                                 response[strlen(response)-1] = ' '; 
216                     }
217                     fstrcat(response,addr);
218                     fstrcat(response,"\t");
219                 }
220                 size = strlen(state->request.data.winsreq) + strlen(response);
221                 if (size > maxlen) {
222                     SAFE_FREE(ip_list);
223                     request_error(state);
224                     return;
225                 }   
226                 fstrcat(response,state->request.data.winsreq);
227                 fstrcat(response,"\n");
228                 SAFE_FREE(ip_list);
229         } else {
230                 request_error(state);
231                 return;
232         }
233
234         fstrcpy(state->response.data.winsresp,response);
235
236         request_ok(state);
237 }