2 Unix SMB/CIFS implementation.
4 Async helpers for blocking functions
6 Copyright (C) Volker Lendecke 2005
7 Copyright (C) Gerald Carter 2006
8 Copyright (C) Simo Sorce 2007
10 The helpers always consist of three functions:
12 * A request setup function that takes the necessary parameters together
13 with a continuation function that is to be called upon completion
15 * A private continuation function that is internal only. This is to be
16 called by the lower-level functions in do_async(). Its only task is to
17 properly call the continuation function named above.
19 * A worker function that is called inside the appropriate child process.
21 This program is free software; you can redistribute it and/or modify
22 it under the terms of the GNU General Public License as published by
23 the Free Software Foundation; either version 3 of the License, or
24 (at your option) any later version.
26 This program is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
31 You should have received a copy of the GNU General Public License
32 along with this program. If not, see <http://www.gnu.org/licenses/>.
39 #define DBGC_CLASS DBGC_WINBIND
41 static struct winbindd_child static_idmap_child;
43 struct winbindd_child *idmap_child(void)
45 return &static_idmap_child;
48 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
49 struct winbindd_cli_state *state)
54 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
55 state->request->data.dual_sid2id.sid));
57 if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
58 DEBUG(1, ("Could not get convert sid %s from string\n",
59 state->request->data.dual_sid2id.sid));
60 return WINBINDD_ERROR;
63 result = idmap_sid_to_uid(state->request->domain_name, &sid,
64 &state->response->data.uid);
66 DEBUG(10, ("winbindd_dual_sid2uid: 0x%08x - %s - %u\n",
67 NT_STATUS_V(result), sid_string_dbg(&sid),
68 (unsigned int)state->response->data.uid));
70 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
73 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
74 struct winbindd_cli_state *state)
79 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
80 state->request->data.dual_sid2id.sid));
82 if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
83 DEBUG(1, ("Could not get convert sid %s from string\n",
84 state->request->data.dual_sid2id.sid));
85 return WINBINDD_ERROR;
88 /* Find gid for this sid and return it, possibly ask the slow remote idmap */
90 result = idmap_sid_to_gid(state->request->domain_name, &sid,
91 &state->response->data.gid);
93 DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
94 NT_STATUS_V(result), sid_string_dbg(&sid),
95 (unsigned int)state->response->data.gid));
97 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
100 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
101 struct winbindd_cli_state *state)
106 DEBUG(3,("[%5lu]: uid to sid %lu\n",
107 (unsigned long)state->pid,
108 (unsigned long) state->request->data.uid));
110 /* Find sid for this uid and return it, possibly ask the slow remote idmap */
111 result = idmap_uid_to_sid(state->request->domain_name, &sid,
112 state->request->data.uid);
114 if (NT_STATUS_IS_OK(result)) {
115 sid_to_fstring(state->response->data.sid.sid, &sid);
116 state->response->data.sid.type = SID_NAME_USER;
120 return WINBINDD_ERROR;
123 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
124 struct winbindd_response *response,
125 void *c, void *private_data)
127 void (*cont)(void *priv, bool succ, const char *sid) =
128 (void (*)(void *, bool, const char *))c;
131 DEBUG(5, ("Could not trigger gid2sid\n"));
132 cont(private_data, False, NULL);
136 if (response->result != WINBINDD_OK) {
137 DEBUG(5, ("gid2sid returned an error\n"));
138 cont(private_data, False, NULL);
142 cont(private_data, True, response->data.sid.sid);
145 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
146 void (*cont)(void *private_data, bool success, const char *sid),
149 struct winbindd_domain *domain;
150 struct winbindd_request request;
152 ZERO_STRUCT(request);
153 request.cmd = WINBINDD_DUAL_GID2SID;
154 request.data.gid = gid;
156 for (domain = domain_list(); domain != NULL; domain = domain->next) {
157 if (domain->have_idmap_config
158 && (gid >= domain->id_range_low)
159 && (gid <= domain->id_range_high)) {
160 fstrcpy(request.domain_name, domain->name);
164 do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
165 (void *)cont, private_data);
168 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
169 struct winbindd_cli_state *state)
174 DEBUG(3,("[%5lu]: gid %lu to sid\n",
175 (unsigned long)state->pid,
176 (unsigned long) state->request->data.gid));
178 /* Find sid for this gid and return it, possibly ask the slow remote idmap */
179 result = idmap_gid_to_sid(state->request->domain_name, &sid,
180 state->request->data.gid);
182 if (NT_STATUS_IS_OK(result)) {
183 sid_to_fstring(state->response->data.sid.sid, &sid);
184 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
185 (unsigned long)state->pid,
186 state->response->data.sid.sid));
187 state->response->data.sid.type = SID_NAME_DOM_GRP;
191 return WINBINDD_ERROR;
194 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
197 .struct_cmd = WINBINDD_PING,
198 .struct_fn = winbindd_dual_ping,
200 .name = "DUAL_SID2UID",
201 .struct_cmd = WINBINDD_DUAL_SID2UID,
202 .struct_fn = winbindd_dual_sid2uid,
204 .name = "DUAL_SID2GID",
205 .struct_cmd = WINBINDD_DUAL_SID2GID,
206 .struct_fn = winbindd_dual_sid2gid,
208 .name = "DUAL_UID2SID",
209 .struct_cmd = WINBINDD_DUAL_UID2SID,
210 .struct_fn = winbindd_dual_uid2sid,
212 .name = "DUAL_GID2SID",
213 .struct_cmd = WINBINDD_DUAL_GID2SID,
214 .struct_fn = winbindd_dual_gid2sid,
217 .struct_cmd = WINBINDD_DUAL_NDRCMD,
218 .struct_fn = winbindd_dual_ndrcmd,
224 void init_idmap_child(void)
226 setup_child(NULL, &static_idmap_child,
227 idmap_dispatch_table,
228 "log.winbindd", "idmap");