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