s3:winbind: Convert WINBINDD_GETSIDALIASES to the new API
[samba.git] / source3 / winbindd / winbindd_getsidaliases.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_GETSIDALIASES
4    Copyright (C) Volker Lendecke 2009
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
23 struct winbindd_getsidaliases_state {
24         struct dom_sid sid;
25         uint32_t num_aliases;
26         uint32_t *aliases;
27 };
28
29 static void winbindd_getsidaliases_done(struct tevent_req *subreq);
30
31 struct tevent_req *winbindd_getsidaliases_send(TALLOC_CTX *mem_ctx,
32                                                struct tevent_context *ev,
33                                                struct winbindd_request *request)
34 {
35         struct tevent_req *req, *subreq;
36         struct winbindd_getsidaliases_state *state;
37         struct winbindd_domain *domain;
38         size_t num_sids;
39         struct dom_sid *sids;
40
41         req = tevent_req_create(mem_ctx, &state,
42                                 struct winbindd_getsidaliases_state);
43         if (req == NULL) {
44                 return NULL;
45         }
46
47         /* Ensure null termination */
48         request->data.sid[sizeof(request->data.sid)-1]='\0';
49
50         DEBUG(3, ("getsidaliases %s\n", request->data.sid));
51
52         if (!string_to_sid(&state->sid, request->data.sid)) {
53                 DEBUG(1, ("Could not get convert sid %s from string\n",
54                           request->data.sid));
55                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
56                 return tevent_req_post(req, ev);
57         }
58
59         domain = find_domain_from_sid_noinit(&state->sid);
60         if (domain == NULL) {
61                 DEBUG(1,("could not find domain entry for sid %s\n",
62                          request->data.sid));
63                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
64                 return tevent_req_post(req, ev);
65         }
66
67         num_sids = 0;
68         sids = NULL;
69
70         if ((request->extra_data.data != NULL)
71             && !parse_sidlist(state, request->extra_data.data,
72                               &sids, &num_sids)) {
73                 DEBUG(1, ("Could not parse SID list: %s\n",
74                           request->extra_data.data));
75                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
76                 return tevent_req_post(req, ev);
77         }
78
79         subreq = wb_lookupuseraliases_send(state, ev, domain, num_sids, sids);
80         if (tevent_req_nomem(subreq, req)) {
81                 return tevent_req_post(req, ev);
82         }
83         tevent_req_set_callback(subreq, winbindd_getsidaliases_done, req);
84         return req;
85 }
86
87 static void winbindd_getsidaliases_done(struct tevent_req *subreq)
88 {
89         struct tevent_req *req = tevent_req_callback_data(
90                 subreq, struct tevent_req);
91         struct winbindd_getsidaliases_state *state = tevent_req_data(
92                 req, struct winbindd_getsidaliases_state);
93         NTSTATUS status;
94
95         status = wb_lookupuseraliases_recv(subreq, state, &state->num_aliases,
96                                            &state->aliases);
97         TALLOC_FREE(subreq);
98         if (!NT_STATUS_IS_OK(status)) {
99                 tevent_req_nterror(req, status);
100                 return;
101         }
102         tevent_req_done(req);
103 }
104
105 NTSTATUS winbindd_getsidaliases_recv(struct tevent_req *req,
106                                      struct winbindd_response *response)
107 {
108         struct winbindd_getsidaliases_state *state = tevent_req_data(
109                 req, struct winbindd_getsidaliases_state);
110         NTSTATUS status;
111         int i;
112         char *sidlist;
113
114         if (tevent_req_is_nterror(req, &status)) {
115                 return status;
116         }
117
118         sidlist = talloc_strdup(response, "");
119         if (sidlist == NULL) {
120                 return NT_STATUS_NO_MEMORY;
121         }
122         for (i=0; i<state->num_aliases; i++) {
123                 struct dom_sid sid;
124                 fstring tmp;
125                 sid_compose(&sid, &state->sid, state->aliases[i]);
126
127                 sidlist = talloc_asprintf_append_buffer(
128                         sidlist, "%s\n", sid_to_fstring(tmp, &sid));
129                 if (sidlist == NULL) {
130                         return NT_STATUS_NO_MEMORY;
131                 }
132         }
133         response->extra_data.data = sidlist;
134         response->length += talloc_get_size(sidlist);
135         response->data.num_entries = state->num_aliases;
136         return NT_STATUS_OK;
137 }