2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2011
5 Copyright (C) Michael Adam 2012
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.
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.
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/>.
23 #include "../libcli/security/security.h"
24 #include "idmap_cache.h"
25 #include "librpc/gen_ndr/ndr_wbint_c.h"
28 struct wb_sids2xids_state
{
29 struct tevent_context
*ev
;
34 struct id_map
*cached
;
36 struct dom_sid
*non_cached
;
37 uint32_t num_non_cached
;
39 struct lsa_RefDomainList
*domains
;
40 struct lsa_TransNameArray
*names
;
43 * Domain array to use for the idmap call. The output from
44 * lookupsids cannot be used directly since for migrated
45 * objects the returned domain SID can be different that the
46 * original one. The new domain SID cannot be combined with
47 * the RID from the previous domain.
49 * The proper way would be asking for the correct RID in the
50 * new domain, but this approach avoids id mappings for
53 struct lsa_RefDomainList
*idmap_doms
;
55 struct wbint_TransIDArray ids
;
59 static bool wb_sids2xids_in_cache(struct dom_sid
*sid
, struct id_map
*map
);
60 static void wb_sids2xids_lookupsids_done(struct tevent_req
*subreq
);
61 static void wb_sids2xids_done(struct tevent_req
*subreq
);
63 struct tevent_req
*wb_sids2xids_send(TALLOC_CTX
*mem_ctx
,
64 struct tevent_context
*ev
,
65 const struct dom_sid
*sids
,
66 const uint32_t num_sids
)
68 struct tevent_req
*req
, *subreq
;
69 struct wb_sids2xids_state
*state
;
72 req
= tevent_req_create(mem_ctx
, &state
,
73 struct wb_sids2xids_state
);
80 state
->num_sids
= num_sids
;
82 state
->sids
= talloc_zero_array(state
, struct dom_sid
, num_sids
);
83 if (tevent_req_nomem(state
->sids
, req
)) {
84 return tevent_req_post(req
, ev
);
87 for (i
= 0; i
< num_sids
; i
++) {
88 sid_copy(&state
->sids
[i
], &sids
[i
]);
91 state
->cached
= talloc_zero_array(state
, struct id_map
, num_sids
);
92 if (tevent_req_nomem(state
->cached
, req
)) {
93 return tevent_req_post(req
, ev
);
96 state
->non_cached
= talloc_array(state
, struct dom_sid
, num_sids
);
97 if (tevent_req_nomem(state
->non_cached
, req
)) {
98 return tevent_req_post(req
, ev
);
102 * Extract those sids that can not be resolved from cache
103 * into a separate list to be handed to id mapping, keeping
106 for (i
=0; i
<state
->num_sids
; i
++) {
108 DEBUG(10, ("SID %d: %s\n", (int)i
,
109 sid_string_dbg(&state
->sids
[i
])));
111 if (wb_sids2xids_in_cache(&state
->sids
[i
], &state
->cached
[i
])) {
114 sid_copy(&state
->non_cached
[state
->num_non_cached
],
116 state
->num_non_cached
+= 1;
119 if (state
->num_non_cached
== 0) {
120 tevent_req_done(req
);
121 return tevent_req_post(req
, ev
);
124 subreq
= wb_lookupsids_send(state
, ev
, state
->non_cached
,
125 state
->num_non_cached
);
126 if (tevent_req_nomem(subreq
, req
)) {
127 return tevent_req_post(req
, ev
);
129 tevent_req_set_callback(subreq
, wb_sids2xids_lookupsids_done
, req
);
133 static bool wb_sids2xids_in_cache(struct dom_sid
*sid
, struct id_map
*map
)
138 if (!winbindd_use_idmap_cache()) {
141 if (idmap_cache_find_sid2unixid(sid
, &id
, &expired
)) {
142 if (expired
&& is_domain_online(find_our_domain())) {
147 map
->status
= ID_MAPPED
;
153 static enum id_type
lsa_SidType_to_id_type(const enum lsa_SidType sid_type
);
155 static void wb_sids2xids_lookupsids_done(struct tevent_req
*subreq
)
157 struct tevent_req
*req
= tevent_req_callback_data(
158 subreq
, struct tevent_req
);
159 struct wb_sids2xids_state
*state
= tevent_req_data(
160 req
, struct wb_sids2xids_state
);
161 struct winbindd_child
*child
;
165 status
= wb_lookupsids_recv(subreq
, state
, &state
->domains
,
168 if (tevent_req_nterror(req
, status
)) {
172 state
->ids
.num_ids
= state
->num_non_cached
;
173 state
->ids
.ids
= talloc_array(state
, struct wbint_TransID
,
174 state
->num_non_cached
);
175 if (tevent_req_nomem(state
->ids
.ids
, req
)) {
179 state
->idmap_doms
= talloc_zero(state
, struct lsa_RefDomainList
);
180 if (tevent_req_nomem(state
->idmap_doms
, req
)) {
184 for (i
=0; i
<state
->num_non_cached
; i
++) {
185 struct dom_sid dom_sid
;
186 struct lsa_DomainInfo
*info
;
187 struct lsa_TranslatedName
*n
= &state
->names
->names
[i
];
188 struct wbint_TransID
*t
= &state
->ids
.ids
[i
];
190 sid_copy(&dom_sid
, &state
->non_cached
[i
]);
191 sid_split_rid(&dom_sid
, &t
->rid
);
193 info
= &state
->domains
->domains
[n
->sid_index
];
194 t
->type
= lsa_SidType_to_id_type(n
->sid_type
);
195 t
->domain_index
= init_lsa_ref_domain_list(state
,
199 t
->xid
.id
= UINT32_MAX
;
200 t
->xid
.type
= t
->type
;
203 child
= idmap_child();
205 subreq
= dcerpc_wbint_Sids2UnixIDs_send(
206 state
, state
->ev
, child
->binding_handle
, state
->idmap_doms
,
208 if (tevent_req_nomem(subreq
, req
)) {
211 tevent_req_set_callback(subreq
, wb_sids2xids_done
, req
);
214 static enum id_type
lsa_SidType_to_id_type(const enum lsa_SidType sid_type
)
219 case SID_NAME_COMPUTER
:
223 case SID_NAME_DOM_GRP
:
225 case SID_NAME_WKN_GRP
:
229 type
= ID_TYPE_NOT_SPECIFIED
;
237 static void wb_sids2xids_done(struct tevent_req
*subreq
)
239 struct tevent_req
*req
= tevent_req_callback_data(
240 subreq
, struct tevent_req
);
241 struct wb_sids2xids_state
*state
= tevent_req_data(
242 req
, struct wb_sids2xids_state
);
243 NTSTATUS status
, result
;
245 status
= dcerpc_wbint_Sids2UnixIDs_recv(subreq
, state
, &result
);
247 if (any_nt_status_not_ok(status
, result
, &status
)) {
248 tevent_req_nterror(req
, status
);
251 tevent_req_done(req
);
254 NTSTATUS
wb_sids2xids_recv(struct tevent_req
*req
,
257 struct wb_sids2xids_state
*state
= tevent_req_data(
258 req
, struct wb_sids2xids_state
);
260 uint32_t i
, num_non_cached
;
262 if (tevent_req_is_nterror(req
, &status
)) {
263 DEBUG(5, ("wb_sids_to_xids failed: %s\n", nt_errstr(status
)));
269 for (i
=0; i
<state
->num_sids
; i
++) {
274 if (state
->cached
[i
].sid
!= NULL
) {
275 xid
= state
->cached
[i
].xid
;
277 xid
= state
->ids
.ids
[num_non_cached
].xid
;
279 idmap_cache_set_sid2unixid(
280 &state
->non_cached
[num_non_cached
],