s3: Remove close_fn from idmap_methods
[kai/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 #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 bool parse_sidlist(TALLOC_CTX *mem_ctx, const char *sidstr,
31                           struct dom_sid **sids, uint32_t *num_sids);
32
33 static void winbindd_getsidaliases_done(struct tevent_req *subreq);
34
35 struct tevent_req *winbindd_getsidaliases_send(TALLOC_CTX *mem_ctx,
36                                                struct tevent_context *ev,
37                                                struct winbindd_cli_state *cli,
38                                                struct winbindd_request *request)
39 {
40         struct tevent_req *req, *subreq;
41         struct winbindd_getsidaliases_state *state;
42         struct winbindd_domain *domain;
43         uint32_t num_sids;
44         struct dom_sid *sids;
45
46         req = tevent_req_create(mem_ctx, &state,
47                                 struct winbindd_getsidaliases_state);
48         if (req == NULL) {
49                 return NULL;
50         }
51
52         /* Ensure null termination */
53         request->data.sid[sizeof(request->data.sid)-1]='\0';
54
55         DEBUG(3, ("getsidaliases %s\n", request->data.sid));
56
57         if (!string_to_sid(&state->sid, request->data.sid)) {
58                 DEBUG(1, ("Could not get convert sid %s from string\n",
59                           request->data.sid));
60                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
61                 return tevent_req_post(req, ev);
62         }
63
64         domain = find_domain_from_sid_noinit(&state->sid);
65         if (domain == NULL) {
66                 DEBUG(1,("could not find domain entry for sid %s\n",
67                          request->data.sid));
68                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
69                 return tevent_req_post(req, ev);
70         }
71
72         num_sids = 0;
73         sids = NULL;
74
75         if (request->extra_data.data != NULL) {
76                 if (request->extra_data.data[request->extra_len-1] != '\0') {
77                         DEBUG(1, ("Got non-NULL terminated sidlist\n"));
78                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
79                         return tevent_req_post(req, ev);
80                 }
81                 if (!parse_sidlist(state, request->extra_data.data,
82                                    &sids, &num_sids)) {
83                         DEBUG(1, ("Could not parse SID list: %s\n",
84                                   request->extra_data.data));
85                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
86                         return tevent_req_post(req, ev);
87                 }
88         }
89
90         if (DEBUGLEVEL >= 10) {
91                 size_t i;
92                 for (i=0; i<num_sids; i++) {
93                         fstring sidstr;
94                         sid_to_fstring(sidstr, &sids[i]);
95                         DEBUGADD(10, ("%s\n", sidstr));
96                 }
97         }
98
99         subreq = wb_lookupuseraliases_send(state, ev, domain, num_sids, sids);
100         if (tevent_req_nomem(subreq, req)) {
101                 return tevent_req_post(req, ev);
102         }
103         tevent_req_set_callback(subreq, winbindd_getsidaliases_done, req);
104         return req;
105 }
106
107 static void winbindd_getsidaliases_done(struct tevent_req *subreq)
108 {
109         struct tevent_req *req = tevent_req_callback_data(
110                 subreq, struct tevent_req);
111         struct winbindd_getsidaliases_state *state = tevent_req_data(
112                 req, struct winbindd_getsidaliases_state);
113         NTSTATUS status;
114
115         status = wb_lookupuseraliases_recv(subreq, state, &state->num_aliases,
116                                            &state->aliases);
117         TALLOC_FREE(subreq);
118         if (!NT_STATUS_IS_OK(status)) {
119                 tevent_req_nterror(req, status);
120                 return;
121         }
122         tevent_req_done(req);
123 }
124
125 NTSTATUS winbindd_getsidaliases_recv(struct tevent_req *req,
126                                      struct winbindd_response *response)
127 {
128         struct winbindd_getsidaliases_state *state = tevent_req_data(
129                 req, struct winbindd_getsidaliases_state);
130         NTSTATUS status;
131         int i;
132         char *sidlist;
133
134         if (tevent_req_is_nterror(req, &status)) {
135                 return status;
136         }
137
138         sidlist = talloc_strdup(response, "");
139         if (sidlist == NULL) {
140                 return NT_STATUS_NO_MEMORY;
141         }
142         for (i=0; i<state->num_aliases; i++) {
143                 struct dom_sid sid;
144                 fstring tmp;
145                 sid_compose(&sid, &state->sid, state->aliases[i]);
146
147                 sidlist = talloc_asprintf_append_buffer(
148                         sidlist, "%s\n", sid_to_fstring(tmp, &sid));
149                 if (sidlist == NULL) {
150                         return NT_STATUS_NO_MEMORY;
151                 }
152         }
153         response->extra_data.data = sidlist;
154         response->length += talloc_get_size(sidlist);
155         response->data.num_entries = state->num_aliases;
156         return NT_STATUS_OK;
157 }
158
159 static bool parse_sidlist(TALLOC_CTX *mem_ctx, const char *sidstr,
160                           struct dom_sid **sids, uint32_t *num_sids)
161 {
162         const char *p, *q;
163
164         p = sidstr;
165         if (p == NULL)
166                 return False;
167
168         while (p[0] != '\0') {
169                 fstring tmp;
170                 size_t sidlen;
171                 struct dom_sid sid;
172                 q = strchr(p, '\n');
173                 if (q == NULL) {
174                         DEBUG(0, ("Got invalid sidstr: %s\n", p));
175                         return False;
176                 }
177                 sidlen = PTR_DIFF(q, p);
178                 if (sidlen >= sizeof(tmp)-1) {
179                         return false;
180                 }
181                 memcpy(tmp, p, sidlen);
182                 tmp[sidlen] = '\0';
183                 q += 1;
184                 if (!string_to_sid(&sid, tmp)) {
185                         DEBUG(0, ("Could not parse sid %s\n", p));
186                         return False;
187                 }
188                 if (!NT_STATUS_IS_OK(add_sid_to_array(mem_ctx, &sid, sids,
189                                                       num_sids)))
190                 {
191                         return False;
192                 }
193                 p = q;
194         }
195         return True;
196 }