Merge winbind from Samba 3.0 onto HEAD.
[samba.git] / source / nsswitch / 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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "winbindd.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
29
30 /* Use our own create socket code so we don't recurse.... */
31
32 static int wins_lookup_open_socket_in(void)
33 {
34         struct sockaddr_in sock;
35         int val=1;
36         int res;
37
38         memset((char *)&sock,'\0',sizeof(sock));
39
40 #ifdef HAVE_SOCK_SIN_LEN
41         sock.sin_len = sizeof(sock);
42 #endif
43         sock.sin_port = 0;
44         sock.sin_family = AF_INET;
45         sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
46         res = socket(AF_INET, SOCK_DGRAM, 0);
47         if (res == -1)
48                 return -1;
49
50         setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
51 #ifdef SO_REUSEPORT
52         setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val));
53 #endif /* SO_REUSEPORT */
54
55         /* now we've got a socket - we need to bind it */
56
57         if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
58                 close(res);
59                 return(-1);
60         }
61
62         set_socket_options(res,"SO_BROADCAST");
63
64         return res;
65 }
66
67
68 static struct node_status *lookup_byaddr_backend(char *addr, int *count)
69 {
70         int fd;
71         struct in_addr  ip;
72         struct nmb_name nname;
73         struct node_status *status;
74
75         fd = wins_lookup_open_socket_in();
76         if (fd == -1)
77                 return NULL;
78
79         make_nmb_name(&nname, "*", 0);
80         ip = *interpret_addr2(addr);
81         status = node_status_query(fd,&nname,ip, count);
82
83         close(fd);
84         return status;
85 }
86
87 static struct in_addr *lookup_byname_backend(const char *name, int *count)
88 {
89         int fd;
90         struct ip_service *ret = NULL;
91         struct in_addr *return_ip;
92         int j, i, flags = 0;
93
94         *count = 0;
95
96         /* always try with wins first */
97         if (resolve_wins(name,0x20,&ret,count)) {
98                 if ( count == 0 )
99                         return NULL;
100                 if ( (return_ip = (struct in_addr *)malloc((*count)*sizeof(struct in_addr))) == NULL ) {
101                         free( ret );
102                         return NULL;
103                 }
104
105                 /* copy the IP addresses */
106                 for ( i=0; i<(*count); i++ ) 
107                         return_ip[i] = ret[i].ip;
108                 
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                 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) break;
124         }
125
126         close(fd);
127         return return_ip;
128 }
129
130 /* Get hostname from IP  */
131
132 enum winbindd_result winbindd_wins_byip(struct winbindd_cli_state *state)
133 {
134         fstring response;
135         int i, count, maxlen, size;
136         struct node_status *status;
137
138         /* Ensure null termination */
139         state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
140
141         DEBUG(3, ("[%5lu]: wins_byip %s\n", (unsigned long)state->pid,
142                 state->request.data.winsreq));
143
144         *response = '\0';
145         maxlen = sizeof(response) - 1;
146
147         if ((status = lookup_byaddr_backend(state->request.data.winsreq, &count))){
148             size = strlen(state->request.data.winsreq);
149             if (size > maxlen) {
150                 SAFE_FREE(status);
151                 return WINBINDD_ERROR;
152             }
153             fstrcat(response,state->request.data.winsreq);
154             fstrcat(response,"\t");
155             for (i = 0; i < count; i++) {
156                 /* ignore group names */
157                 if (status[i].flags & 0x80) continue;
158                 if (status[i].type == 0x20) {
159                         size = sizeof(status[i].name) + strlen(response);
160                         if (size > maxlen) {
161                             SAFE_FREE(status);
162                             return WINBINDD_ERROR;
163                         }
164                         fstrcat(response, status[i].name);
165                         fstrcat(response, " ");
166                 }
167             }
168             /* make last character a newline */
169             response[strlen(response)-1] = '\n';
170             SAFE_FREE(status);
171         }
172         fstrcpy(state->response.data.winsresp,response);
173         return WINBINDD_OK;
174 }
175
176 /* Get IP from hostname */
177
178 enum winbindd_result winbindd_wins_byname(struct winbindd_cli_state *state)
179 {
180         struct in_addr *ip_list;
181         int i, count, maxlen, size;
182         fstring response;
183         char * addr;
184
185         /* Ensure null termination */
186         state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
187
188         DEBUG(3, ("[%5lu]: wins_byname %s\n", (unsigned long)state->pid,
189                 state->request.data.winsreq));
190
191         *response = '\0';
192         maxlen = sizeof(response) - 1;
193
194         if ((ip_list = lookup_byname_backend(state->request.data.winsreq,&count))){
195                 for (i = count; i ; i--) {
196                     addr = inet_ntoa(ip_list[i-1]);
197                     size = strlen(addr);
198                     if (size > maxlen) {
199                         SAFE_FREE(ip_list);
200                         return WINBINDD_ERROR;
201                     }
202                     if (i != 0) {
203                         /* Clear out the newline character */
204                         response[strlen(response)-1] = ' '; 
205                     }
206                     fstrcat(response,addr);
207                     fstrcat(response,"\t");
208                 }
209                 size = strlen(state->request.data.winsreq) + strlen(response);
210                 if (size > maxlen) {
211                     SAFE_FREE(ip_list);
212                     return WINBINDD_ERROR;
213                 }   
214                 fstrcat(response,state->request.data.winsreq);
215                 fstrcat(response,"\n");
216                 SAFE_FREE(ip_list);
217         } else
218                 return WINBINDD_ERROR;
219
220         fstrcpy(state->response.data.winsresp,response);
221
222         return WINBINDD_OK;
223 }