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/>.
25 #include "libsmb/nmblib.h"
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;
38 make_nmb_name(&nname, "*", 0);
39 if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
42 status = node_status_query(mem_ctx, &nname, &ss,
43 &result, count, NULL);
44 if (!NT_STATUS_IS_OK(status)) {
50 static struct sockaddr_storage *lookup_byname_backend(TALLOC_CTX *mem_ctx,
54 struct ip_service *ret = NULL;
55 struct sockaddr_storage *return_ss = NULL;
61 /* always try with wins first */
62 if (NT_STATUS_IS_OK(resolve_wins(name,0x20,&ret,count))) {
65 return_ss = TALLOC_ARRAY(mem_ctx, struct sockaddr_storage,
67 if (return_ss == NULL ) {
72 /* copy the IP addresses */
73 for ( i=0; i<(*count); i++ )
74 return_ss[i] = ret[i].ss;
80 /* uggh, we have to broadcast to each interface in turn */
81 for (j=iface_count() - 1;
84 const struct sockaddr_storage *bcast_ss = iface_n_bcast(j);
88 status = name_query(name, 0x20, True, True,bcast_ss,
89 mem_ctx, &return_ss, count, NULL);
90 if (NT_STATUS_IS_OK(status)) {
98 /* Get hostname from IP */
100 void winbindd_wins_byip(struct winbindd_cli_state *state)
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));
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);
120 request_error(state);
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);
132 request_error(state);
135 fstrcat(response, status[i].name);
136 fstrcat(response, " ");
139 /* make last character a newline */
140 response[strlen(response)-1] = '\n';
143 strlcpy(state->response->data.winsresp,
145 sizeof(state->response->data.winsresp));
149 /* Get IP from hostname */
151 void winbindd_wins_byname(struct winbindd_cli_state *state)
153 struct sockaddr_storage *ip_list = NULL;
154 int i, count, maxlen, size;
156 char addr[INET6_ADDRSTRLEN];
158 /* Ensure null termination */
159 state->request->data.winsreq[sizeof(state->request->data.winsreq)-1]='\0';
161 DEBUG(3, ("[%5lu]: wins_byname %s\n", (unsigned long)state->pid,
162 state->request->data.winsreq));
165 maxlen = sizeof(response) - 1;
167 ip_list = lookup_byname_backend(
168 state->mem_ctx, state->request->data.winsreq, &count);
169 if (ip_list != NULL){
170 for (i = count; i ; i--) {
171 print_sockaddr(addr, sizeof(addr), &ip_list[i-1]);
174 TALLOC_FREE(ip_list);
175 request_error(state);
179 /* Clear out the newline character */
180 /* But only if there is something in there,
181 otherwise we clobber something in the stack */
182 if (strlen(response)) {
183 response[strlen(response)-1] = ' ';
186 strlcat(response,addr,sizeof(response));
187 strlcat(response,"\t",sizeof(response));
189 size = strlen(state->request->data.winsreq) + strlen(response);
191 TALLOC_FREE(ip_list);
192 request_error(state);
195 strlcat(response,state->request->data.winsreq,sizeof(response));
196 strlcat(response,"\n",sizeof(response));
197 TALLOC_FREE(ip_list);
199 request_error(state);
203 strlcpy(state->response->data.winsresp,
205 sizeof(state->response->data.winsresp));