autobuild: move maptoguest and simpleserver to 'samba-fileserver'
[garming/samba-autobuild/.git] / source3 / winbindd / winbindd_xids_to_sids.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_SIDS_TO_XIDS
4    Copyright (C) Volker Lendecke 2011
5    Copyright (C) Michael Adam 2012
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "winbindd.h"
23 #include "../libcli/security/security.h"
24
25
26 struct winbindd_xids_to_sids_state {
27         struct tevent_context *ev;
28
29         struct unixid *xids;
30         uint32_t num_xids;
31
32         struct dom_sid *sids;
33 };
34
35 static void winbindd_xids_to_sids_done(struct tevent_req *subreq);
36
37 struct tevent_req *winbindd_xids_to_sids_send(TALLOC_CTX *mem_ctx,
38                                               struct tevent_context *ev,
39                                               struct winbindd_cli_state *cli,
40                                               struct winbindd_request *request)
41 {
42         struct tevent_req *req, *subreq;
43         struct winbindd_xids_to_sids_state *state;
44
45         req = tevent_req_create(mem_ctx, &state,
46                                 struct winbindd_xids_to_sids_state);
47         if (req == NULL) {
48                 return NULL;
49         }
50         state->ev = ev;
51
52         DEBUG(3, ("xids_to_sids\n"));
53
54         if (request->extra_len == 0) {
55                 tevent_req_done(req);
56                 return tevent_req_post(req, ev);
57         }
58         if (request->extra_data.data[request->extra_len-1] != '\0') {
59                 DEBUG(10, ("Got invalid xids list\n"));
60                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
61                 return tevent_req_post(req, ev);
62         }
63         if (!parse_xidlist(state, request->extra_data.data,
64                            &state->xids, &state->num_xids)) {
65                 DEBUG(10, ("parse_sidlist failed\n"));
66                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
67                 return tevent_req_post(req, ev);
68         }
69
70         DBG_DEBUG("num_xids: %"PRIu32"\n%s\n",
71                   state->num_xids,
72                   (char *)request->extra_data.data);
73
74         subreq = wb_xids2sids_send(state, ev, state->xids, state->num_xids);
75         if (tevent_req_nomem(subreq, req)) {
76                 return tevent_req_post(req, ev);
77         }
78         tevent_req_set_callback(subreq, winbindd_xids_to_sids_done, req);
79         return req;
80 }
81
82 static void winbindd_xids_to_sids_done(struct tevent_req *subreq)
83 {
84         struct tevent_req *req = tevent_req_callback_data(
85                 subreq, struct tevent_req);
86         struct winbindd_xids_to_sids_state *state = tevent_req_data(
87                 req, struct winbindd_xids_to_sids_state);
88         NTSTATUS status;
89
90         status = wb_xids2sids_recv(subreq, state, &state->sids);
91         TALLOC_FREE(subreq);
92         if (tevent_req_nterror(req, status)) {
93                 return;
94         }
95         tevent_req_done(req);
96 }
97
98 NTSTATUS winbindd_xids_to_sids_recv(struct tevent_req *req,
99                                     struct winbindd_response *response)
100 {
101         struct winbindd_xids_to_sids_state *state = tevent_req_data(
102                 req, struct winbindd_xids_to_sids_state);
103         NTSTATUS status;
104         char *result = NULL;
105         uint32_t i;
106
107         if (tevent_req_is_nterror(req, &status)) {
108                 DBG_INFO("Could not convert xids: %s\n", nt_errstr(status));
109                 return status;
110         }
111
112         result = talloc_strdup(response, "");
113         if (result == NULL) {
114                 return NT_STATUS_NO_MEMORY;
115         }
116
117         for (i=0; i<state->num_xids; i++) {
118                 struct dom_sid_buf sid_buf;
119                 const char *str = "-";
120
121                 if (!is_null_sid(&state->sids[i])) {
122                         dom_sid_str_buf(&state->sids[i], &sid_buf);
123                         str = sid_buf.buf;
124                 }
125
126                 result = talloc_asprintf_append_buffer(
127                         result, "%s\n", str);
128                 if (result == NULL) {
129                         return NT_STATUS_NO_MEMORY;
130                 }
131         }
132
133         DBG_DEBUG("sids:\n%s\n", result);
134
135         response->extra_data.data = result;
136         response->length += talloc_get_size(result);
137
138         return NT_STATUS_OK;
139 }