Line data Source code
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_sids_to_xids_state {
27 : struct tevent_context *ev;
28 : struct dom_sid *sids;
29 : uint32_t num_sids;
30 : struct unixid *xids;
31 : };
32 :
33 : static void winbindd_sids_to_xids_done(struct tevent_req *subreq);
34 :
35 5155 : struct tevent_req *winbindd_sids_to_xids_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_sids_to_xids_state *state;
42 :
43 5155 : req = tevent_req_create(mem_ctx, &state,
44 : struct winbindd_sids_to_xids_state);
45 5155 : if (req == NULL) {
46 0 : return NULL;
47 : }
48 5155 : state->ev = ev;
49 :
50 5155 : D_NOTICE("[%s (%u)] Winbind external command SIDS_TO_XIDS start.\n",
51 : cli->client_name,
52 : (unsigned int)cli->pid);
53 :
54 5155 : if (request->extra_len == 0) {
55 0 : tevent_req_done(req);
56 0 : return tevent_req_post(req, ev);
57 : }
58 5155 : if (request->extra_data.data[request->extra_len-1] != '\0') {
59 0 : D_DEBUG("Got invalid sids list\n");
60 0 : tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
61 0 : return tevent_req_post(req, ev);
62 : }
63 9961 : if (!parse_sidlist(state, request->extra_data.data,
64 9961 : &state->sids, &state->num_sids)) {
65 0 : D_DEBUG("parse_sidlist failed\n");
66 0 : tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
67 0 : return tevent_req_post(req, ev);
68 : }
69 :
70 5155 : D_DEBUG("Resolving %"PRIu32" SID(s).\n", state->num_sids);
71 :
72 5155 : subreq = wb_sids2xids_send(state, ev, state->sids, state->num_sids);
73 5155 : if (tevent_req_nomem(subreq, req)) {
74 0 : return tevent_req_post(req, ev);
75 : }
76 :
77 5155 : tevent_req_set_callback(subreq, winbindd_sids_to_xids_done, req);
78 5155 : return req;
79 : }
80 :
81 5144 : static void winbindd_sids_to_xids_done(struct tevent_req *subreq)
82 : {
83 5144 : struct tevent_req *req = tevent_req_callback_data(
84 : subreq, struct tevent_req);
85 5144 : struct winbindd_sids_to_xids_state *state = tevent_req_data(
86 : req, struct winbindd_sids_to_xids_state);
87 : NTSTATUS status;
88 :
89 5144 : state->xids = talloc_zero_array(state, struct unixid, state->num_sids);
90 5144 : if (tevent_req_nomem(state->xids, req)) {
91 516 : return;
92 : }
93 :
94 5144 : status = wb_sids2xids_recv(subreq, state->xids, state->num_sids);
95 5144 : TALLOC_FREE(subreq);
96 5144 : if (tevent_req_nterror(req, status)) {
97 516 : return;
98 : }
99 4628 : tevent_req_done(req);
100 : }
101 :
102 5144 : NTSTATUS winbindd_sids_to_xids_recv(struct tevent_req *req,
103 : struct winbindd_response *response)
104 : {
105 5144 : struct winbindd_sids_to_xids_state *state = tevent_req_data(
106 : req, struct winbindd_sids_to_xids_state);
107 : NTSTATUS status;
108 5144 : char *result = NULL;
109 : uint32_t i;
110 :
111 5144 : D_NOTICE("Winbind external command SIDS_TO_XIDS end.\n");
112 5144 : if (tevent_req_is_nterror(req, &status)) {
113 516 : D_WARNING("Could not convert sids: %s\n", nt_errstr(status));
114 516 : return status;
115 : }
116 :
117 4628 : result = talloc_strdup(response, "");
118 :
119 50817 : for (i=0; i<state->num_sids; i++) {
120 46189 : char type = '\0';
121 46189 : bool found = true;
122 : struct unixid xid;
123 :
124 46189 : xid = state->xids[i];
125 :
126 46189 : switch (xid.type) {
127 3368 : case ID_TYPE_UID:
128 3368 : type = 'U';
129 3368 : break;
130 3122 : case ID_TYPE_GID:
131 3122 : type = 'G';
132 3122 : break;
133 39639 : case ID_TYPE_BOTH:
134 39639 : type = 'B';
135 39639 : break;
136 60 : default:
137 60 : found = false;
138 60 : break;
139 : }
140 :
141 46189 : if (xid.id == UINT32_MAX) {
142 60 : found = false;
143 : }
144 :
145 46189 : if (found) {
146 46129 : talloc_asprintf_addbuf(
147 : &result,
148 : "%c%lu\n",
149 : type,
150 46129 : (unsigned long)xid.id);
151 : } else {
152 60 : talloc_asprintf_addbuf(&result, "\n");
153 : }
154 : }
155 :
156 4628 : if (result == NULL) {
157 0 : return NT_STATUS_NO_MEMORY;
158 : }
159 :
160 4628 : response->extra_data.data = result;
161 4628 : response->length += talloc_get_size(result);
162 :
163 4628 : return NT_STATUS_OK;
164 : }
|