Remove some unused variables uncovered by the build farm.
[Samba/gebeck_regimport.git] / source3 / nsswitch / winbindd_wins.c
blob49bee2dc9f741e69a5677df14ba90e99de1edada
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 struct node_status *lookup_byaddr_backend(char *addr, int *count)
69 int fd;
70 struct in_addr ip;
71 struct nmb_name nname;
72 struct node_status *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);
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;
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 = (struct in_addr *)malloc((*count)*sizeof(struct in_addr))) == 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 return return_ip;
111 fd = wins_lookup_open_socket_in();
112 if (fd == -1) {
113 return NULL;
116 /* uggh, we have to broadcast to each interface in turn */
117 for (j=iface_count() - 1;
118 j >= 0;
119 j--) {
120 struct in_addr *bcast = iface_n_bcast(j);
121 return_ip = name_query(fd,name,0x20,True,True,*bcast,count, &flags, NULL);
122 if (return_ip) break;
125 close(fd);
126 return return_ip;
129 /* Get hostname from IP */
131 enum winbindd_result winbindd_wins_byip(struct winbindd_cli_state *state)
133 fstring response;
134 int i, count, maxlen, size;
135 struct node_status *status;
137 /* Ensure null termination */
138 state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
140 DEBUG(3, ("[%5lu]: wins_byip %s\n", (unsigned long)state->pid,
141 state->request.data.winsreq));
143 *response = '\0';
144 maxlen = sizeof(response) - 1;
146 if ((status = lookup_byaddr_backend(state->request.data.winsreq, &count))){
147 size = strlen(state->request.data.winsreq);
148 if (size > maxlen) {
149 SAFE_FREE(status);
150 return WINBINDD_ERROR;
152 fstrcat(response,state->request.data.winsreq);
153 fstrcat(response,"\t");
154 for (i = 0; i < count; i++) {
155 /* ignore group names */
156 if (status[i].flags & 0x80) continue;
157 if (status[i].type == 0x20) {
158 size = sizeof(status[i].name) + strlen(response);
159 if (size > maxlen) {
160 SAFE_FREE(status);
161 return WINBINDD_ERROR;
163 fstrcat(response, status[i].name);
164 fstrcat(response, " ");
167 /* make last character a newline */
168 response[strlen(response)-1] = '\n';
169 SAFE_FREE(status);
171 fstrcpy(state->response.data.winsresp,response);
172 return WINBINDD_OK;
175 /* Get IP from hostname */
177 enum winbindd_result winbindd_wins_byname(struct winbindd_cli_state *state)
179 struct in_addr *ip_list;
180 int i, count, maxlen, size;
181 fstring response;
182 char * addr;
184 /* Ensure null termination */
185 state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
187 DEBUG(3, ("[%5lu]: wins_byname %s\n", (unsigned long)state->pid,
188 state->request.data.winsreq));
190 *response = '\0';
191 maxlen = sizeof(response) - 1;
193 if ((ip_list = lookup_byname_backend(state->request.data.winsreq,&count))){
194 for (i = count; i ; i--) {
195 addr = inet_ntoa(ip_list[i-1]);
196 size = strlen(addr);
197 if (size > maxlen) {
198 SAFE_FREE(ip_list);
199 return WINBINDD_ERROR;
201 if (i != 0) {
202 /* Clear out the newline character */
203 response[strlen(response)-1] = ' ';
205 fstrcat(response,addr);
206 fstrcat(response,"\t");
208 size = strlen(state->request.data.winsreq) + strlen(response);
209 if (size > maxlen) {
210 SAFE_FREE(ip_list);
211 return WINBINDD_ERROR;
213 fstrcat(response,state->request.data.winsreq);
214 fstrcat(response,"\n");
215 SAFE_FREE(ip_list);
216 } else
217 return WINBINDD_ERROR;
219 fstrcpy(state->response.data.winsresp,response);
221 return WINBINDD_OK;