ctdb-scripts: Move connection tracking to 10.interface
[samba4-gss.git] / source3 / winbindd / winbindd_wins_byname.c
blobae170b2e0b9e50abf4a34e25ed100d97f05e3905
1 /*
2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_WINS_BYNAME
4 Copyright (C) Volker Lendecke 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "libsmb/namequery.h"
23 #include "librpc/gen_ndr/ndr_winbind_c.h"
24 #include "libsmb/nmblib.h"
25 #include "lib/util/string_wrappers.h"
27 struct winbindd_wins_byname_state {
28 struct tevent_context *ev;
29 struct winbindd_request *request;
30 struct sockaddr_storage *addrs;
31 size_t num_addrs;
34 static void winbindd_wins_byname_wins_done(struct tevent_req *subreq);
35 static void winbindd_wins_byname_bcast_done(struct tevent_req *subreq);
37 struct tevent_req *winbindd_wins_byname_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 struct winbindd_cli_state *cli,
40 struct winbindd_request *request)
42 struct tevent_req *req, *subreq;
43 struct winbindd_wins_byname_state *state;
45 req = tevent_req_create(mem_ctx, &state,
46 struct winbindd_wins_byname_state);
47 if (req == NULL) {
48 return NULL;
50 state->ev = ev;
51 state->request = request;
53 /* Ensure null termination */
54 request->data.winsreq[sizeof(request->data.winsreq)-1]='\0';
56 D_NOTICE("[%s (%u)] Winbind external command WINS_BYNAME start.\n"
57 "Resolving wins byname for '%s'.\n",
58 cli->client_name,
59 (unsigned int)cli->pid,
60 request->data.winsreq);
62 subreq = resolve_wins_send(state, ev, state->request->data.winsreq,
63 0x20);
64 if (tevent_req_nomem(subreq, req)) {
65 return tevent_req_post(req, ev);
67 tevent_req_set_callback(subreq, winbindd_wins_byname_wins_done, req);
68 return req;
71 static void winbindd_wins_byname_wins_done(struct tevent_req *subreq)
73 struct tevent_req *req = tevent_req_callback_data(
74 subreq, struct tevent_req);
75 struct winbindd_wins_byname_state *state = tevent_req_data(
76 req, struct winbindd_wins_byname_state);
77 NTSTATUS status;
79 status = resolve_wins_recv(subreq, talloc_tos(), &state->addrs,
80 &state->num_addrs, NULL);
81 TALLOC_FREE(subreq);
82 if (NT_STATUS_IS_OK(status)) {
83 tevent_req_done(req);
84 return;
86 subreq = name_resolve_bcast_send(state, state->ev,
87 state->request->data.winsreq, 0x20);
88 if (tevent_req_nomem(subreq, req)) {
89 return;
91 tevent_req_set_callback(subreq, winbindd_wins_byname_bcast_done, req);
94 static void winbindd_wins_byname_bcast_done(struct tevent_req *subreq)
96 struct tevent_req *req = tevent_req_callback_data(
97 subreq, struct tevent_req);
98 struct winbindd_wins_byname_state *state = tevent_req_data(
99 req, struct winbindd_wins_byname_state);
100 NTSTATUS status;
102 status = name_resolve_bcast_recv(subreq, talloc_tos(), &state->addrs,
103 &state->num_addrs);
104 TALLOC_FREE(subreq);
105 if (tevent_req_nterror(req, status)) {
106 return;
108 tevent_req_done(req);
111 NTSTATUS winbindd_wins_byname_recv(struct tevent_req *req,
112 struct winbindd_response *presp)
114 struct winbindd_wins_byname_state *state = tevent_req_data(
115 req, struct winbindd_wins_byname_state);
116 char *response;
117 NTSTATUS status;
118 size_t i;
120 if (tevent_req_is_nterror(req, &status)) {
121 return status;
124 response = talloc_strdup(talloc_tos(), "");
125 if (response == NULL) {
126 return NT_STATUS_NO_MEMORY;
129 D_NOTICE("Winbind external command WINS_BYNAME end.\n"
130 "Received %zu address(es).\n",
131 state->num_addrs);
132 for (i=0; i<state->num_addrs; i++) {
133 char addr[INET6_ADDRSTRLEN];
134 print_sockaddr(addr, sizeof(addr), &state->addrs[i]);
135 D_NOTICE("%zu: %s\n", i, addr);
136 talloc_asprintf_addbuf(
137 &response, "%s%s", addr,
138 i < (state->num_addrs-1) ? " " : "");
141 talloc_asprintf_addbuf(
142 &response, "\t%s\n", state->request->data.winsreq);
143 if (response == NULL) {
144 return NT_STATUS_NO_MEMORY;
147 if (talloc_get_size(response) > sizeof(presp->data.winsresp)) {
148 TALLOC_FREE(response);
149 return NT_STATUS_MARSHALL_OVERFLOW;
151 fstrcpy(presp->data.winsresp, response);
152 TALLOC_FREE(response);
153 return NT_STATUS_OK;