2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
22 #include "librpc/gen_ndr/cli_wbint.h"
23 #include "idmap_cache.h"
25 struct wb_sid2gid_state {
26 struct tevent_context *ev;
33 static void wb_sid2gid_lookup_done(struct tevent_req *subreq);
34 static void wb_sid2gid_done(struct tevent_req *subreq);
37 struct tevent_req *wb_sid2gid_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 const struct dom_sid *sid)
41 struct tevent_req *req, *subreq;
42 struct wb_sid2gid_state *state;
45 req = tevent_req_create(mem_ctx, &state, struct wb_sid2gid_state);
49 sid_copy(&state->sid, sid);
52 if (winbindd_use_idmap_cache()
53 && idmap_cache_find_sid2gid(sid, &state->gid, &expired)) {
55 DEBUG(10, ("idmap_cache_find_sid2gid found %d%s\n",
56 (int)state->gid, expired ? " (expired)": ""));
58 if (!expired || is_domain_offline(find_our_domain())) {
59 if (state->gid == -1) {
60 tevent_req_nterror(req, NT_STATUS_NONE_MAPPED);
64 return tevent_req_post(req, ev);
69 * We need to make sure the sid is of the right type to not flood
70 * idmap with wrong entries
73 subreq = wb_lookupsid_send(state, ev, &state->sid);
74 if (tevent_req_nomem(subreq, req)) {
75 return tevent_req_post(req, ev);
77 tevent_req_set_callback(subreq, wb_sid2gid_lookup_done, req);
81 static void wb_sid2gid_lookup_done(struct tevent_req *subreq)
83 struct tevent_req *req = tevent_req_callback_data(
84 subreq, struct tevent_req);
85 struct wb_sid2gid_state *state = tevent_req_data(
86 req, struct wb_sid2gid_state);
87 struct winbindd_domain *domain;
90 enum lsa_SidType type;
92 struct winbindd_child *child;
94 status = wb_lookupsid_recv(subreq, talloc_tos(), &type, &domname,
96 if (!NT_STATUS_IS_OK(status)) {
97 tevent_req_nterror(req, status);
101 if ((type != SID_NAME_DOM_GRP) && (type != SID_NAME_ALIAS)
102 && (type != SID_NAME_WKN_GRP)) {
103 DEBUG(5, ("Sid %s is not a group.\n",
104 sid_string_dbg(&state->sid)));
106 * We have to set the cache ourselves here, the child
107 * which is normally responsible was not queried yet.
109 idmap_cache_set_sid2gid(&state->sid, -1);
110 tevent_req_nterror(req, NT_STATUS_INVALID_SID);
114 domain = find_domain_from_sid_noinit(&state->sid);
117 * TODO: Issue a gettrustinfo here in case we don't have "domain" yet?
120 if ((domain != NULL) && domain->have_idmap_config) {
121 state->dom_name = domain->name;
123 state->dom_name = NULL;
126 child = idmap_child();
128 subreq = dcerpc_wbint_Sid2Gid_send(state, state->ev, child->binding_handle,
129 state->dom_name, &state->sid,
131 if (tevent_req_nomem(subreq, req)) {
134 tevent_req_set_callback(subreq, wb_sid2gid_done, req);
137 static void wb_sid2gid_done(struct tevent_req *subreq)
139 struct tevent_req *req = tevent_req_callback_data(
140 subreq, struct tevent_req);
141 struct wb_sid2gid_state *state = tevent_req_data(
142 req, struct wb_sid2gid_state);
143 NTSTATUS status, result;
145 status = dcerpc_wbint_Sid2Gid_recv(subreq, state, &result);
147 if (!NT_STATUS_IS_OK(status)) {
148 tevent_req_nterror(req, status);
151 if (!NT_STATUS_IS_OK(result)) {
152 tevent_req_nterror(req, result);
156 state->gid = state->gid64;
157 tevent_req_done(req);
160 NTSTATUS wb_sid2gid_recv(struct tevent_req *req, gid_t *gid)
162 struct wb_sid2gid_state *state = tevent_req_data(
163 req, struct wb_sid2gid_state);
166 if (tevent_req_is_nterror(req, &status)) {