libcli/security Provide a common, top level libcli/security/security.h
[bbaumbach/samba-autobuild/.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 #include "../libcli/security/security.h"
23
24 struct winbindd_getsidaliases_state {
25         struct dom_sid sid;
26         uint32_t num_aliases;
27         uint32_t *aliases;
28 };
29
30 static void winbindd_getsidaliases_done(struct tevent_req *subreq);
31
32 struct tevent_req *winbindd_getsidaliases_send(TALLOC_CTX *mem_ctx,
33                                                struct tevent_context *ev,
34                                                struct winbindd_cli_state *cli,
35                                                struct winbindd_request *request)
36 {
37         struct tevent_req *req, *subreq;
38         struct winbindd_getsidaliases_state *state;
39         struct winbindd_domain *domain;
40         uint32_t num_sids;
41         struct dom_sid *sids;
42
43         req = tevent_req_create(mem_ctx, &state,
44                                 struct winbindd_getsidaliases_state);
45         if (req == NULL) {
46                 return NULL;
47         }
48
49         /* Ensure null termination */
50         request->data.sid[sizeof(request->data.sid)-1]='\0';
51
52         DEBUG(3, ("getsidaliases %s\n", request->data.sid));
53
54         if (!string_to_sid(&state->sid, request->data.sid)) {
55                 DEBUG(1, ("Could not get convert sid %s from string\n",
56                           request->data.sid));
57                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
58                 return tevent_req_post(req, ev);
59         }
60
61         domain = find_domain_from_sid_noinit(&state->sid);
62         if (domain == NULL) {
63                 DEBUG(1,("could not find domain entry for sid %s\n",
64                          request->data.sid));
65                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
66                 return tevent_req_post(req, ev);
67         }
68
69         num_sids = 0;
70         sids = NULL;
71
72         if (request->extra_data.data != NULL) {
73                 if (request->extra_data.data[request->extra_len-1] != '\0') {
74                         DEBUG(1, ("Got non-NULL terminated sidlist\n"));
75                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
76                         return tevent_req_post(req, ev);
77                 }
78                 if (!parse_sidlist(state, request->extra_data.data,
79                                    &sids, &num_sids)) {
80                         DEBUG(1, ("Could not parse SID list: %s\n",
81                                   request->extra_data.data));
82                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
83                         return tevent_req_post(req, ev);
84                 }
85         }
86
87         if (DEBUGLEVEL >= 10) {
88                 size_t i;
89                 for (i=0; i<num_sids; i++) {
90                         fstring sidstr;
91                         sid_to_fstring(sidstr, &sids[i]);
92                         DEBUGADD(10, ("%s\n", sidstr));
93                 }
94         }
95
96         subreq = wb_lookupuseraliases_send(state, ev, domain, num_sids, sids);
97         if (tevent_req_nomem(subreq, req)) {
98                 return tevent_req_post(req, ev);
99         }
100         tevent_req_set_callback(subreq, winbindd_getsidaliases_done, req);
101         return req;
102 }
103
104 static void winbindd_getsidaliases_done(struct tevent_req *subreq)
105 {
106         struct tevent_req *req = tevent_req_callback_data(
107                 subreq, struct tevent_req);
108         struct winbindd_getsidaliases_state *state = tevent_req_data(
109                 req, struct winbindd_getsidaliases_state);
110         NTSTATUS status;
111
112         status = wb_lookupuseraliases_recv(subreq, state, &state->num_aliases,
113                                            &state->aliases);
114         TALLOC_FREE(subreq);
115         if (!NT_STATUS_IS_OK(status)) {
116                 tevent_req_nterror(req, status);
117                 return;
118         }
119         tevent_req_done(req);
120 }
121
122 NTSTATUS winbindd_getsidaliases_recv(struct tevent_req *req,
123                                      struct winbindd_response *response)
124 {
125         struct winbindd_getsidaliases_state *state = tevent_req_data(
126                 req, struct winbindd_getsidaliases_state);
127         NTSTATUS status;
128         int i;
129         char *sidlist;
130
131         if (tevent_req_is_nterror(req, &status)) {
132                 return status;
133         }
134
135         sidlist = talloc_strdup(response, "");
136         if (sidlist == NULL) {
137                 return NT_STATUS_NO_MEMORY;
138         }
139         for (i=0; i<state->num_aliases; i++) {
140                 struct dom_sid sid;
141                 fstring tmp;
142                 sid_compose(&sid, &state->sid, state->aliases[i]);
143
144                 sidlist = talloc_asprintf_append_buffer(
145                         sidlist, "%s\n", sid_to_fstring(tmp, &sid));
146                 if (sidlist == NULL) {
147                         return NT_STATUS_NO_MEMORY;
148                 }
149         }
150         response->extra_data.data = sidlist;
151         response->length += talloc_get_size(sidlist);
152         response->data.num_entries = state->num_aliases;
153         return NT_STATUS_OK;
154 }