r4348: syncing up for 3.0.11pre1
[Samba.git] / source / nsswitch / winbindd_wins.c
blobf199ebcb4371c97a5fcbb0168675558edbc2c01b
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 "includes.h"
25 #include "winbindd.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
30 /* Use our own create socket code so we don't recurse.... */
32 static int wins_lookup_open_socket_in(void)
34 struct sockaddr_in sock;
35 int val=1;
36 int res;
38 memset((char *)&sock,'\0',sizeof(sock));
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;
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 */
55 /* now we've got a socket - we need to bind it */
57 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
58 close(res);
59 return(-1);
62 set_socket_options(res,"SO_BROADCAST");
64 return res;
68 static struct node_status *lookup_byaddr_backend(char *addr, int *count)
70 int fd;
71 struct in_addr ip;
72 struct nmb_name nname;
73 struct node_status *status;
75 fd = wins_lookup_open_socket_in();
76 if (fd == -1)
77 return NULL;
79 make_nmb_name(&nname, "*", 0);
80 ip = *interpret_addr2(addr);
81 status = node_status_query(fd,&nname,ip, count, NULL);
83 close(fd);
84 return status;
87 static struct in_addr *lookup_byname_backend(const char *name, int *count)
89 int fd;
90 struct ip_service *ret = NULL;
91 struct in_addr *return_ip = NULL;
92 int j, i, flags = 0;
94 *count = 0;
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 = SMB_MALLOC_ARRAY(struct in_addr, *count)) == NULL ) {
101 free( ret );
102 return NULL;
105 /* copy the IP addresses */
106 for ( i=0; i<(*count); i++ )
107 return_ip[i] = ret[i].ip;
109 free( ret );
110 return return_ip;
113 fd = wins_lookup_open_socket_in();
114 if (fd == -1) {
115 return NULL;
118 /* uggh, we have to broadcast to each interface in turn */
119 for (j=iface_count() - 1;
120 j >= 0;
121 j--) {
122 struct in_addr *bcast = iface_n_bcast(j);
123 return_ip = name_query(fd,name,0x20,True,True,*bcast,count, &flags, NULL);
124 if (return_ip) {
125 break;
129 close(fd);
130 return return_ip;
133 /* Get hostname from IP */
135 enum winbindd_result winbindd_wins_byip(struct winbindd_cli_state *state)
137 fstring response;
138 int i, count, maxlen, size;
139 struct node_status *status;
141 /* Ensure null termination */
142 state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
144 DEBUG(3, ("[%5lu]: wins_byip %s\n", (unsigned long)state->pid,
145 state->request.data.winsreq));
147 *response = '\0';
148 maxlen = sizeof(response) - 1;
150 if ((status = lookup_byaddr_backend(state->request.data.winsreq, &count))){
151 size = strlen(state->request.data.winsreq);
152 if (size > maxlen) {
153 SAFE_FREE(status);
154 return WINBINDD_ERROR;
156 fstrcat(response,state->request.data.winsreq);
157 fstrcat(response,"\t");
158 for (i = 0; i < count; i++) {
159 /* ignore group names */
160 if (status[i].flags & 0x80) continue;
161 if (status[i].type == 0x20) {
162 size = sizeof(status[i].name) + strlen(response);
163 if (size > maxlen) {
164 SAFE_FREE(status);
165 return WINBINDD_ERROR;
167 fstrcat(response, status[i].name);
168 fstrcat(response, " ");
171 /* make last character a newline */
172 response[strlen(response)-1] = '\n';
173 SAFE_FREE(status);
175 fstrcpy(state->response.data.winsresp,response);
176 return WINBINDD_OK;
179 /* Get IP from hostname */
181 enum winbindd_result winbindd_wins_byname(struct winbindd_cli_state *state)
183 struct in_addr *ip_list;
184 int i, count, maxlen, size;
185 fstring response;
186 char * addr;
188 /* Ensure null termination */
189 state->request.data.winsreq[sizeof(state->request.data.winsreq)-1]='\0';
191 DEBUG(3, ("[%5lu]: wins_byname %s\n", (unsigned long)state->pid,
192 state->request.data.winsreq));
194 *response = '\0';
195 maxlen = sizeof(response) - 1;
197 if ((ip_list = lookup_byname_backend(state->request.data.winsreq,&count))){
198 for (i = count; i ; i--) {
199 addr = inet_ntoa(ip_list[i-1]);
200 size = strlen(addr);
201 if (size > maxlen) {
202 SAFE_FREE(ip_list);
203 return WINBINDD_ERROR;
205 if (i != 0) {
206 /* Clear out the newline character */
207 /* But only if there is something in there,
208 otherwise we clobber something in the stack */
209 if (strlen(response))
210 response[strlen(response)-1] = ' ';
212 fstrcat(response,addr);
213 fstrcat(response,"\t");
215 size = strlen(state->request.data.winsreq) + strlen(response);
216 if (size > maxlen) {
217 SAFE_FREE(ip_list);
218 return WINBINDD_ERROR;
220 fstrcat(response,state->request.data.winsreq);
221 fstrcat(response,"\n");
222 SAFE_FREE(ip_list);
223 } else
224 return WINBINDD_ERROR;
226 fstrcpy(state->response.data.winsresp,response);
228 return WINBINDD_OK;