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