c995dd1b0dc51f689cd34e2289c31a04d8de6995
[nivanova/samba-autobuild/.git] / source3 / winbindd / winbindd_wins_byip.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_WINS_BYIP
4    Copyright (C) Volker Lendecke 2011
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "librpc/gen_ndr/ndr_winbind_c.h"
23 #include "libsmb/nmblib.h"
24
25 struct winbindd_wins_byip_state {
26         struct nmb_name star;
27         struct sockaddr_storage addr;
28         fstring response;
29 };
30
31 static void winbindd_wins_byip_done(struct tevent_req *subreq);
32
33 struct tevent_req *winbindd_wins_byip_send(TALLOC_CTX *mem_ctx,
34                                            struct tevent_context *ev,
35                                            struct winbindd_cli_state *cli,
36                                            struct winbindd_request *request)
37 {
38         struct tevent_req *req, *subreq;
39         struct winbindd_wins_byip_state *state;
40
41         req = tevent_req_create(mem_ctx, &state,
42                                 struct winbindd_wins_byip_state);
43         if (req == NULL) {
44                 return NULL;
45         }
46
47         /* Ensure null termination */
48         request->data.winsreq[sizeof(request->data.winsreq)-1]='\0';
49
50         fstr_sprintf(state->response, "%s\t", request->data.winsreq);
51
52         DEBUG(3, ("[%5lu]: wins_byip %s\n", (unsigned long)cli->pid,
53                   request->data.winsreq));
54
55         make_nmb_name(&state->star, "*", 0);
56
57         if (!interpret_string_addr(&state->addr, request->data.winsreq,
58                                    AI_NUMERICHOST)) {
59                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
60                 return tevent_req_post(req, ev);
61         }
62
63         subreq = node_status_query_send(state, ev, &state->star,
64                                         &state->addr);
65         if (tevent_req_nomem(subreq, req)) {
66                 return tevent_req_post(req, ev);
67         }
68         tevent_req_set_callback(subreq, winbindd_wins_byip_done, req);
69         return req;
70 }
71
72 static void winbindd_wins_byip_done(struct tevent_req *subreq)
73 {
74         struct tevent_req *req = tevent_req_callback_data(
75                 subreq, struct tevent_req);
76         struct winbindd_wins_byip_state *state = tevent_req_data(
77                 req, struct winbindd_wins_byip_state);
78         struct node_status *names;
79         int i, num_names;
80         NTSTATUS status;
81
82         status = node_status_query_recv(subreq, talloc_tos(), &names,
83                                         &num_names, NULL);
84         TALLOC_FREE(subreq);
85         if (tevent_req_nterror(req, status)) {
86                 return;
87         }
88
89         for (i=0; i<num_names; i++) {
90                 size_t size;
91                 /*
92                  * ignore group names
93                  */
94                 if (names[i].flags & 0x80) {
95                         continue;
96                 }
97                 /*
98                  * Only report 0x20
99                  */
100                 if (names[i].type != 0x20) {
101                         continue;
102                 }
103
104                 DEBUG(10, ("got name %s\n", names[i].name));
105
106                 size = strlen(names[i].name + strlen(state->response));
107                 if (size > sizeof(state->response) - 1) {
108                         DEBUG(10, ("To much data\n"));
109                         tevent_req_nterror(req, STATUS_BUFFER_OVERFLOW);
110                         return;
111                 }
112                 fstrcat(state->response, names[i].name);
113                 fstrcat(state->response, " ");
114         }
115         state->response[strlen(state->response)-1] = '\n';
116
117         DEBUG(10, ("response: %s", state->response));
118
119         TALLOC_FREE(names);
120         tevent_req_done(req);
121 }
122
123 NTSTATUS winbindd_wins_byip_recv(struct tevent_req *req,
124                                  struct winbindd_response *presp)
125 {
126         struct winbindd_wins_byip_state *state = tevent_req_data(
127                 req, struct winbindd_wins_byip_state);
128         NTSTATUS status;
129
130         if (tevent_req_is_nterror(req, &status)) {
131                 return status;
132         }
133         fstrcpy(presp->data.winsresp, state->response);
134         return NT_STATUS_OK;
135 }