s3: Fix a winbind race leading to 100% CPU
[Samba.git] / source3 / winbindd / winbindd_wins.c
blobe1beb1d80bf6fa89f89e8cfbc81b247f9700358f
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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "winbindd.h"
25 #include "libsmb/nmblib.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
30 static struct node_status *lookup_byaddr_backend(TALLOC_CTX *mem_ctx,
31 const char *addr, int *count)
33 struct sockaddr_storage ss;
34 struct nmb_name nname;
35 struct node_status *result;
36 NTSTATUS status;
38 make_nmb_name(&nname, "*", 0);
39 if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
40 return NULL;
42 status = node_status_query(mem_ctx, &nname, &ss,
43 &result, count, NULL);
44 if (!NT_STATUS_IS_OK(status)) {
45 return NULL;
47 return result;
50 static struct sockaddr_storage *lookup_byname_backend(TALLOC_CTX *mem_ctx,
51 const char *name,
52 int *count)
54 struct ip_service *ret = NULL;
55 struct sockaddr_storage *return_ss = NULL;
56 int j, i;
57 NTSTATUS status;
59 *count = 0;
61 /* always try with wins first */
62 if (NT_STATUS_IS_OK(resolve_wins(name,0x20,&ret,count))) {
63 if ( *count == 0 )
64 return NULL;
65 return_ss = TALLOC_ARRAY(mem_ctx, struct sockaddr_storage,
66 *count);
67 if (return_ss == NULL ) {
68 free( ret );
69 return NULL;
72 /* copy the IP addresses */
73 for ( i=0; i<(*count); i++ )
74 return_ss[i] = ret[i].ss;
76 free( ret );
77 return return_ss;
80 /* uggh, we have to broadcast to each interface in turn */
81 for (j=iface_count() - 1;
82 j >= 0;
83 j--) {
84 const struct sockaddr_storage *bcast_ss = iface_n_bcast(j);
85 if (!bcast_ss) {
86 continue;
88 status = name_query(name, 0x20, True, True,bcast_ss,
89 mem_ctx, &return_ss, count, NULL);
90 if (NT_STATUS_IS_OK(status)) {
91 break;
95 return return_ss;
98 /* Get hostname from IP */
100 void winbindd_wins_byip(struct winbindd_cli_state *state)
102 fstring response;
103 int i, count, maxlen, size;
104 struct node_status *status;
106 /* Ensure null termination */
107 state->request->data.winsreq[sizeof(state->request->data.winsreq)-1]='\0';
109 DEBUG(3, ("[%5lu]: wins_byip %s\n", (unsigned long)state->pid,
110 state->request->data.winsreq));
112 *response = '\0';
113 maxlen = sizeof(response) - 1;
115 if ((status = lookup_byaddr_backend(
116 state->mem_ctx, state->request->data.winsreq, &count))) {
117 size = strlen(state->request->data.winsreq);
118 if (size > maxlen) {
119 TALLOC_FREE(status);
120 request_error(state);
121 return;
123 fstrcat(response,state->request->data.winsreq);
124 fstrcat(response,"\t");
125 for (i = 0; i < count; i++) {
126 /* ignore group names */
127 if (status[i].flags & 0x80) continue;
128 if (status[i].type == 0x20) {
129 size = sizeof(status[i].name) + strlen(response);
130 if (size > maxlen) {
131 TALLOC_FREE(status);
132 request_error(state);
133 return;
135 fstrcat(response, status[i].name);
136 fstrcat(response, " ");
139 /* make last character a newline */
140 response[strlen(response)-1] = '\n';
141 TALLOC_FREE(status);
143 fstrcpy(state->response->data.winsresp,response);
144 request_ok(state);
147 /* Get IP from hostname */
149 void winbindd_wins_byname(struct winbindd_cli_state *state)
151 struct sockaddr_storage *ip_list = NULL;
152 int i, count, maxlen, size;
153 fstring response;
154 char addr[INET6_ADDRSTRLEN];
156 /* Ensure null termination */
157 state->request->data.winsreq[sizeof(state->request->data.winsreq)-1]='\0';
159 DEBUG(3, ("[%5lu]: wins_byname %s\n", (unsigned long)state->pid,
160 state->request->data.winsreq));
162 *response = '\0';
163 maxlen = sizeof(response) - 1;
165 ip_list = lookup_byname_backend(
166 state->mem_ctx, state->request->data.winsreq, &count);
167 if (ip_list != NULL){
168 for (i = count; i ; i--) {
169 print_sockaddr(addr, sizeof(addr), &ip_list[i-1]);
170 size = strlen(addr);
171 if (size > maxlen) {
172 TALLOC_FREE(ip_list);
173 request_error(state);
174 return;
176 if (i != 0) {
177 /* Clear out the newline character */
178 /* But only if there is something in there,
179 otherwise we clobber something in the stack */
180 if (strlen(response)) {
181 response[strlen(response)-1] = ' ';
184 fstrcat(response,addr);
185 fstrcat(response,"\t");
187 size = strlen(state->request->data.winsreq) + strlen(response);
188 if (size > maxlen) {
189 TALLOC_FREE(ip_list);
190 request_error(state);
191 return;
193 fstrcat(response,state->request->data.winsreq);
194 fstrcat(response,"\n");
195 TALLOC_FREE(ip_list);
196 } else {
197 request_error(state);
198 return;
201 fstrcpy(state->response->data.winsresp,response);
203 request_ok(state);