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