Merge branch 'master' of ctdb into 'master' of samba
[samba.git] / source3 / winbindd / winbindd_getusersids.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_GETUSERSIDS
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_getusersids_state {
25         struct dom_sid sid;
26         int num_sids;
27         struct dom_sid *sids;
28 };
29
30 static void winbindd_getusersids_done(struct tevent_req *subreq);
31
32 struct tevent_req *winbindd_getusersids_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_getusersids_state *state;
39
40         req = tevent_req_create(mem_ctx, &state,
41                                 struct winbindd_getusersids_state);
42         if (req == NULL) {
43                 return NULL;
44         }
45
46         /* Ensure null termination */
47         request->data.sid[sizeof(request->data.sid)-1]='\0';
48
49         DEBUG(3, ("getusersids %s\n", request->data.sid));
50
51         if (!string_to_sid(&state->sid, request->data.sid)) {
52                 DEBUG(1, ("Could not get convert sid %s from string\n",
53                           request->data.sid));
54                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
55                 return tevent_req_post(req, ev);
56         }
57
58         subreq = wb_gettoken_send(state, ev, &state->sid);
59         if (tevent_req_nomem(subreq, req)) {
60                 return tevent_req_post(req, ev);
61         }
62         tevent_req_set_callback(subreq, winbindd_getusersids_done, req);
63         return req;
64 }
65
66 static void winbindd_getusersids_done(struct tevent_req *subreq)
67 {
68         struct tevent_req *req = tevent_req_callback_data(
69                 subreq, struct tevent_req);
70         struct winbindd_getusersids_state *state = tevent_req_data(
71                 req, struct winbindd_getusersids_state);
72         NTSTATUS status;
73
74         status = wb_gettoken_recv(subreq, state, &state->num_sids,
75                                   &state->sids);
76         TALLOC_FREE(subreq);
77         if (tevent_req_nterror(req, status)) {
78                 return;
79         }
80         tevent_req_done(req);
81 }
82
83 NTSTATUS winbindd_getusersids_recv(struct tevent_req *req,
84                                    struct winbindd_response *response)
85 {
86         struct winbindd_getusersids_state *state = tevent_req_data(
87                 req, struct winbindd_getusersids_state);
88         NTSTATUS status;
89         int i;
90         char *result;
91
92         if (tevent_req_is_nterror(req, &status)) {
93                 DEBUG(5, ("Could not convert sid %s: %s\n",
94                           sid_string_dbg(&state->sid), nt_errstr(status)));
95                 return status;
96         }
97
98         result = talloc_strdup(response, "");
99         if (result == NULL) {
100                 return NT_STATUS_NO_MEMORY;
101         }
102
103         for (i=0; i<state->num_sids; i++) {
104                 char *str = sid_string_tos(&state->sids[i]);
105                 if (str == NULL) {
106                         TALLOC_FREE(result);
107                         return NT_STATUS_NO_MEMORY;
108                 }
109                 result = talloc_asprintf_append_buffer(result, "%s\n", str);
110                 TALLOC_FREE(str);
111                 if (result == NULL) {
112                         return NT_STATUS_NO_MEMORY;
113                 }
114         }
115
116         response->data.num_entries = state->num_sids;
117         response->extra_data.data = result;
118         response->length += talloc_get_size(result);
119         return NT_STATUS_OK;
120 }