This commit was generated by cvs2svn to compensate for changes in r30,
[samba.git] / source4 / 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 "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 struct node_status *lookup_byaddr_backend(char *addr, int *count)
68 {
69         int fd;
70         struct in_addr  ip;
71         struct nmb_name nname;
72         struct node_status *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);
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 in_addr *ret = NULL;
90         int j, flags = 0;
91
92         *count = 0;
93
94         /* always try with wins first */
95         if (resolve_wins(name,0x20,&ret,count)) {
96                 return ret;
97         }
98
99         fd = wins_lookup_open_socket_in();
100         if (fd == -1) {
101                 return NULL;
102         }
103
104         /* uggh, we have to broadcast to each interface in turn */
105         for (j=iface_count() - 1;
106              j >= 0;
107              j--) {
108                 struct in_addr *bcast = iface_n_bcast(j);
109                 ret = name_query(fd,name,0x20,True,True,*bcast,count, &flags, NULL);
110                 if (ret) break;
111         }
112
113         close(fd);
114         return ret;
115 }
116
117 /* Get hostname from IP  */
118
119 enum winbindd_result winbindd_wins_byip(struct winbindd_cli_state *state)
120 {
121         fstring response;
122         int i, count, maxlen, size;
123         struct node_status *status;
124
125         /* Ensure null termination */
126         state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
127
128         DEBUG(3, ("[%5d]: wins_byip %s\n", state->pid,
129                 state->request.data.winsreq));
130
131         *response = '\0';
132         maxlen = sizeof(response) - 1;
133
134         if ((status = lookup_byaddr_backend(state->request.data.winsreq, &count))){
135             size = strlen(state->request.data.winsreq);
136             if (size > maxlen) {
137                 SAFE_FREE(status);
138                 return WINBINDD_ERROR;
139             }
140             safe_strcat(response,state->request.data.winsreq,maxlen);
141             safe_strcat(response,"\t",maxlen);
142             for (i = 0; i < count; i++) {
143                 /* ignore group names */
144                 if (status[i].flags & 0x80) continue;
145                 if (status[i].type == 0x20) {
146                         size = sizeof(status[i].name) + strlen(response);
147                         if (size > maxlen) {
148                             SAFE_FREE(status);
149                             return WINBINDD_ERROR;
150                         }
151                         safe_strcat(response, status[i].name, maxlen);
152                         safe_strcat(response, " ", maxlen);
153                 }
154             }
155             /* make last character a newline */
156             response[strlen(response)-1] = '\n';
157             SAFE_FREE(status);
158         }
159         fstrcpy(state->response.data.winsresp,response);
160         return WINBINDD_OK;
161 }
162
163 /* Get IP from hostname */
164
165 enum winbindd_result winbindd_wins_byname(struct winbindd_cli_state *state)
166 {
167         struct in_addr *ip_list;
168         int i, count, maxlen, size;
169         fstring response;
170         char * addr;
171
172         /* Ensure null termination */
173         state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
174
175         DEBUG(3, ("[%5d]: wins_byname %s\n", state->pid,
176                 state->request.data.winsreq));
177
178         *response = '\0';
179         maxlen = sizeof(response) - 1;
180
181         if ((ip_list = lookup_byname_backend(state->request.data.winsreq,&count))){
182                 for (i = count; i ; i--) {
183                     addr = inet_ntoa(ip_list[i-1]);
184                     size = strlen(addr);
185                     if (size > maxlen) {
186                         SAFE_FREE(ip_list);
187                         return WINBINDD_ERROR;
188                     }
189                     if (i != 0) {
190                         /* Clear out the newline character */
191                         response[strlen(response)-1] = ' '; 
192                     }
193                     safe_strcat(response,addr,maxlen);
194                     safe_strcat(response,"\t",maxlen);
195                 }
196                 size = strlen(state->request.data.winsreq) + strlen(response);
197                 if (size > maxlen) {
198                     SAFE_FREE(ip_list);
199                     return WINBINDD_ERROR;
200                 }   
201                 safe_strcat(response,state->request.data.winsreq,maxlen);
202                 safe_strcat(response,"\n",maxlen);
203                 SAFE_FREE(ip_list);
204         } else
205                 return WINBINDD_ERROR;
206
207         fstrcpy(state->response.data.winsresp,response);
208
209         return WINBINDD_OK;
210 }