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/>.
21 #include "util/debug.h"
23 #include "librpc/gen_ndr/ndr_winbind_c.h"
24 #include "../libcli/security/security.h"
25 #include "libsmb/samlogon_cache.h"
26 #include "librpc/gen_ndr/ndr_winbind.h"
28 struct wb_queryuser_state
{
29 struct tevent_context
*ev
;
30 struct wbint_userinfo
*info
;
31 const struct wb_parent_idmap_config
*idmap_cfg
;
35 static void wb_queryuser_idmap_setup_done(struct tevent_req
*subreq
);
36 static void wb_queryuser_got_uid(struct tevent_req
*subreq
);
37 static void wb_queryuser_got_domain(struct tevent_req
*subreq
);
38 static void wb_queryuser_got_dc(struct tevent_req
*subreq
);
39 static void wb_queryuser_got_gid(struct tevent_req
*subreq
);
40 static void wb_queryuser_got_group_name(struct tevent_req
*subreq
);
41 static void wb_queryuser_done(struct tevent_req
*subreq
);
43 struct tevent_req
*wb_queryuser_send(TALLOC_CTX
*mem_ctx
,
44 struct tevent_context
*ev
,
45 const struct dom_sid
*user_sid
)
47 struct tevent_req
*req
, *subreq
;
48 struct wb_queryuser_state
*state
;
49 struct wbint_userinfo
*info
;
50 struct dom_sid_buf buf
;
52 req
= tevent_req_create(mem_ctx
, &state
, struct wb_queryuser_state
);
56 D_INFO("WB command queryuser start.\nQuery user sid %s\n",
57 dom_sid_str_buf(user_sid
, &buf
));
60 state
->info
= talloc_zero(state
, struct wbint_userinfo
);
61 if (tevent_req_nomem(state
->info
, req
)) {
62 return tevent_req_post(req
, ev
);
66 info
->primary_gid
= (gid_t
)-1;
68 sid_copy(&info
->user_sid
, user_sid
);
70 subreq
= wb_parent_idmap_setup_send(state
, state
->ev
);
71 if (tevent_req_nomem(subreq
, req
)) {
72 return tevent_req_post(req
, ev
);
74 tevent_req_set_callback(subreq
, wb_queryuser_idmap_setup_done
, req
);
78 static void wb_queryuser_idmap_setup_done(struct tevent_req
*subreq
)
80 struct tevent_req
*req
= tevent_req_callback_data(
81 subreq
, struct tevent_req
);
82 struct wb_queryuser_state
*state
= tevent_req_data(
83 req
, struct wb_queryuser_state
);
85 struct dom_sid_buf buf
;
87 status
= wb_parent_idmap_setup_recv(subreq
, &state
->idmap_cfg
);
89 if (tevent_req_nterror(req
, status
)) {
90 D_WARNING("wb_parent_idmap_setup_recv() failed with %s.\n",
95 D_DEBUG("Convert the user SID %s to XID.\n",
96 dom_sid_str_buf(&state
->info
->user_sid
, &buf
));
97 subreq
= wb_sids2xids_send(
98 state
, state
->ev
, &state
->info
->user_sid
, 1);
99 if (tevent_req_nomem(subreq
, req
)) {
102 tevent_req_set_callback(subreq
, wb_queryuser_got_uid
, req
);
106 static void wb_queryuser_got_uid(struct tevent_req
*subreq
)
108 struct tevent_req
*req
= tevent_req_callback_data(
109 subreq
, struct tevent_req
);
110 struct wb_queryuser_state
*state
= tevent_req_data(
111 req
, struct wb_queryuser_state
);
112 struct wbint_userinfo
*info
= state
->info
;
113 struct netr_SamInfo3
*info3
;
114 struct dcerpc_binding_handle
*child_binding_handle
= NULL
;
116 uint32_t user_rid
= 0;
118 struct dom_sid_buf buf
, buf1
;
120 status
= wb_sids2xids_recv(subreq
, &xid
, 1);
122 if (tevent_req_nterror(req
, status
)) {
123 D_WARNING("wb_sids2xids_recv() failed with %s.\n",
128 if ((xid
.type
!= ID_TYPE_UID
) && (xid
.type
!= ID_TYPE_BOTH
)) {
129 D_WARNING("XID type is %d, should be ID_TYPE_UID or ID_TYPE_BOTH.\n",
131 tevent_req_nterror(req
, NT_STATUS_NO_SUCH_USER
);
135 D_DEBUG("Received XID %"PRIu32
" for SID %s.\n",
137 dom_sid_str_buf(&info
->user_sid
, &buf1
));
141 * Default the group sid to "Domain Users" in the user's
142 * domain. The samlogon cache or the query_user call later on
144 * TODO: There is still missing functionality to set the correct group
145 * sid using samlogon cache (needs to use S4USelf).
146 * Once this is done, remove the workaround in test_membership_user() in
147 * source4/torture/local/nss_tests.c
149 sid_copy(&info
->group_sid
, &info
->user_sid
);
150 sid_split_rid(&info
->group_sid
, &user_rid
);
151 sid_append_rid(&info
->group_sid
,
152 user_rid
== DOMAIN_RID_GUEST
? DOMAIN_RID_GUESTS
155 D_DEBUG("Preconfigured 'Domain Users' RID %u was used to create group SID %s from user SID %s.\n",
157 dom_sid_str_buf(&info
->group_sid
, &buf
),
158 dom_sid_str_buf(&info
->user_sid
, &buf1
));
160 info
->homedir
= talloc_strdup(info
, lp_template_homedir());
161 D_DEBUG("Setting 'homedir' to the template '%s'.\n", info
->homedir
);
162 if (tevent_req_nomem(info
->homedir
, req
)) {
166 info
->shell
= talloc_strdup(info
, lp_template_shell());
167 D_DEBUG("Setting 'shell' to the template '%s'.\n", info
->shell
);
168 if (tevent_req_nomem(info
->shell
, req
)) {
172 info3
= netsamlogon_cache_get(state
, &info
->user_sid
);
174 D_DEBUG("Filling data received from netsamlogon_cache\n");
175 sid_compose(&info
->group_sid
, info3
->base
.domain_sid
,
176 info3
->base
.primary_gid
);
177 info
->acct_name
= talloc_move(
178 info
, &info3
->base
.account_name
.string
);
179 info
->full_name
= talloc_move(
180 info
, &info3
->base
.full_name
.string
);
182 info
->domain_name
= talloc_move(
183 state
, &info3
->base
.logon_domain
.string
);
188 NDR_PRINT_DEBUG_LEVEL(DBGLVL_DEBUG
, wbint_userinfo
, state
->info
);
189 if (info
->domain_name
== NULL
) {
190 D_DEBUG("Domain name is empty, calling wb_lookupsid_send() to get it.\n");
191 subreq
= wb_lookupsid_send(state
, state
->ev
, &info
->user_sid
);
192 if (tevent_req_nomem(subreq
, req
)) {
195 tevent_req_set_callback(subreq
, wb_queryuser_got_domain
, req
);
200 * Note wb_sids2xids_send/recv was called before,
201 * so we're sure that wb_parent_idmap_setup_send/recv
202 * was already called.
204 child_binding_handle
= idmap_child_handle();
205 D_DEBUG("Domain name is set, calling dcerpc_wbint_GetNssInfo_send()\n");
206 subreq
= dcerpc_wbint_GetNssInfo_send(
207 state
, state
->ev
, child_binding_handle
, info
);
208 if (tevent_req_nomem(subreq
, req
)) {
211 tevent_req_set_callback(subreq
, wb_queryuser_done
, req
);
214 static void wb_queryuser_got_domain(struct tevent_req
*subreq
)
216 struct tevent_req
*req
= tevent_req_callback_data(
217 subreq
, struct tevent_req
);
218 struct wb_queryuser_state
*state
= tevent_req_data(
219 req
, struct wb_queryuser_state
);
220 struct wbint_userinfo
*info
= state
->info
;
221 enum lsa_SidType type
;
222 struct dcerpc_binding_handle
*child_binding_handle
= NULL
;
225 status
= wb_lookupsid_recv(subreq
, state
, &type
,
226 &info
->domain_name
, &info
->acct_name
);
228 if (tevent_req_nterror(req
, status
)) {
229 D_WARNING("wb_lookupsid_recv failed with %s.\n",
234 NDR_PRINT_DEBUG_LEVEL(DBGLVL_DEBUG
, wbint_userinfo
, state
->info
);
237 case SID_NAME_COMPUTER
:
239 * user case: we only need the account name from lookup_sids
242 case SID_NAME_DOM_GRP
:
244 case SID_NAME_WKN_GRP
:
246 * also treat group-type SIDs (they might map to ID_TYPE_BOTH)
248 sid_copy(&info
->group_sid
, &info
->user_sid
);
251 D_WARNING("Unknown type:%d, return NT_STATUS_NO_SUCH_USER.\n",
253 tevent_req_nterror(req
, NT_STATUS_NO_SUCH_USER
);
258 * Note wb_sids2xids_send/recv was called before,
259 * so we're sure that wb_parent_idmap_setup_send/recv
260 * was already called.
262 child_binding_handle
= idmap_child_handle();
263 D_DEBUG("About to call dcerpc_wbint_GetNssInfo_send()\n");
264 subreq
= dcerpc_wbint_GetNssInfo_send(
265 state
, state
->ev
, child_binding_handle
, info
);
266 if (tevent_req_nomem(subreq
, req
)) {
269 tevent_req_set_callback(subreq
, wb_queryuser_done
, req
);
272 static void wb_queryuser_done(struct tevent_req
*subreq
)
274 struct tevent_req
*req
= tevent_req_callback_data(
275 subreq
, struct tevent_req
);
276 struct wb_queryuser_state
*state
= tevent_req_data(
277 req
, struct wb_queryuser_state
);
278 struct wbint_userinfo
*info
= state
->info
;
279 NTSTATUS status
, result
;
280 bool need_group_name
= false;
281 const char *tmpl
= NULL
;
283 status
= dcerpc_wbint_GetNssInfo_recv(subreq
, info
, &result
);
285 if (tevent_req_nterror(req
, status
)) {
286 D_WARNING("GetNssInfo failed with %s.\n", nt_errstr(status
));
290 if (NT_STATUS_EQUAL(result
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
) &&
291 !state
->tried_dclookup
) {
292 D_DEBUG("GetNssInfo got DOMAIN_CONTROLLER_NOT_FOUND, calling wb_dsgetdcname_send()\n");
293 subreq
= wb_dsgetdcname_send(
294 state
, state
->ev
, state
->info
->domain_name
, NULL
, NULL
,
296 if (tevent_req_nomem(subreq
, req
)) {
299 tevent_req_set_callback(subreq
, wb_queryuser_got_dc
, req
);
304 * Ignore failure in "result" here. We'll try to fill in stuff
305 * that misses further down.
308 if (state
->info
->primary_gid
== (gid_t
)-1) {
309 D_DEBUG("Calling wb_sids2xids_send() to resolve primary gid.\n");
310 subreq
= wb_sids2xids_send(
311 state
, state
->ev
, &info
->group_sid
, 1);
312 if (tevent_req_nomem(subreq
, req
)) {
315 tevent_req_set_callback(subreq
, wb_queryuser_got_gid
, req
);
319 tmpl
= lp_template_homedir();
320 if(strstr_m(tmpl
, "%g") || strstr_m(tmpl
, "%G")) {
321 need_group_name
= true;
323 tmpl
= lp_template_shell();
324 if(strstr_m(tmpl
, "%g") || strstr_m(tmpl
, "%G")) {
325 need_group_name
= true;
328 if (need_group_name
&& state
->info
->primary_group_name
== NULL
) {
329 D_DEBUG("Calling wb_lookupsid_send() to resolve primary group name.\n");
330 subreq
= wb_lookupsid_send(state
, state
->ev
, &info
->group_sid
);
331 if (tevent_req_nomem(subreq
, req
)) {
334 tevent_req_set_callback(subreq
, wb_queryuser_got_group_name
,
339 NDR_PRINT_DEBUG_LEVEL(DBGLVL_DEBUG
, wbint_userinfo
, state
->info
);
340 tevent_req_done(req
);
343 static void wb_queryuser_got_dc(struct tevent_req
*subreq
)
345 struct tevent_req
*req
= tevent_req_callback_data(
346 subreq
, struct tevent_req
);
347 struct wb_queryuser_state
*state
= tevent_req_data(
348 req
, struct wb_queryuser_state
);
349 struct wbint_userinfo
*info
= state
->info
;
350 struct netr_DsRGetDCNameInfo
*dcinfo
;
351 struct dcerpc_binding_handle
*child_binding_handle
= NULL
;
354 status
= wb_dsgetdcname_recv(subreq
, state
, &dcinfo
);
356 if (tevent_req_nterror(req
, status
)) {
357 D_WARNING("wb_dsgetdcname_recv() failed with %s.\n",
362 state
->tried_dclookup
= true;
364 D_DEBUG("Got DC name, calling wb_dsgetdcname_gencache_set().\n");
365 status
= wb_dsgetdcname_gencache_set(info
->domain_name
, dcinfo
);
366 if (tevent_req_nterror(req
, status
)) {
367 D_WARNING("wb_dsgetdcname_gencache_set() failed with %s.\n",
371 NDR_PRINT_DEBUG_LEVEL(DBGLVL_DEBUG
, wbint_userinfo
, state
->info
);
374 * Note wb_sids2xids_send/recv was called before,
375 * so we're sure that wb_parent_idmap_setup_send/recv
376 * was already called.
378 child_binding_handle
= idmap_child_handle();
379 subreq
= dcerpc_wbint_GetNssInfo_send(
380 state
, state
->ev
, child_binding_handle
, info
);
381 if (tevent_req_nomem(subreq
, req
)) {
384 tevent_req_set_callback(subreq
, wb_queryuser_done
, req
);
387 static void wb_queryuser_got_gid(struct tevent_req
*subreq
)
389 struct tevent_req
*req
= tevent_req_callback_data(
390 subreq
, struct tevent_req
);
391 struct wb_queryuser_state
*state
= tevent_req_data(
392 req
, struct wb_queryuser_state
);
395 bool need_group_name
= false;
396 const char *tmpl
= NULL
;
397 struct dom_sid_buf buf
;
399 status
= wb_sids2xids_recv(subreq
, &xid
, 1);
401 if (tevent_req_nterror(req
, status
)) {
402 D_WARNING("wb_sids2xids_recv() failed with %s.\n",
407 D_DEBUG("Got XID %"PRIu32
" with type %d.\n", xid
.id
, xid
.type
);
408 if ((xid
.type
!= ID_TYPE_GID
) && (xid
.type
!= ID_TYPE_BOTH
)) {
409 D_WARNING("Returning NT_STATUS_NO_SUCH_USER\n"
410 "xid.type must be ID_TYPE_UID or ID_TYPE_BOTH.\n");
411 tevent_req_nterror(req
, NT_STATUS_NO_SUCH_USER
);
415 state
->info
->primary_gid
= xid
.id
;
417 tmpl
= lp_template_homedir();
418 if(strstr_m(tmpl
, "%g") || strstr_m(tmpl
, "%G")) {
419 need_group_name
= true;
421 tmpl
= lp_template_shell();
422 if(strstr_m(tmpl
, "%g") || strstr_m(tmpl
, "%G")) {
423 need_group_name
= true;
426 NDR_PRINT_DEBUG_LEVEL(DBGLVL_DEBUG
, wbint_userinfo
, state
->info
);
428 if (need_group_name
&& state
->info
->primary_group_name
== NULL
) {
429 D_DEBUG("Calling wb_lookupsid_send for group SID %s.\n",
430 dom_sid_str_buf(&state
->info
->group_sid
, &buf
));
431 subreq
= wb_lookupsid_send(state
, state
->ev
,
432 &state
->info
->group_sid
);
433 if (tevent_req_nomem(subreq
, req
)) {
436 tevent_req_set_callback(subreq
, wb_queryuser_got_group_name
,
441 D_DEBUG("No need to lookup primary group name. Request is done!\n");
442 tevent_req_done(req
);
445 static void wb_queryuser_got_group_name(struct tevent_req
*subreq
)
447 struct tevent_req
*req
= tevent_req_callback_data(
448 subreq
, struct tevent_req
);
449 struct wb_queryuser_state
*state
= tevent_req_data(
450 req
, struct wb_queryuser_state
);
451 enum lsa_SidType type
;
453 const char *domain_name
;
455 status
= wb_lookupsid_recv(subreq
, state
->info
, &type
, &domain_name
,
456 &state
->info
->primary_group_name
);
458 if (tevent_req_nterror(req
, status
)) {
459 D_WARNING("wb_lookupsid_recv() failed with %s.\n",
463 tevent_req_done(req
);
466 NTSTATUS
wb_queryuser_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
467 struct wbint_userinfo
**pinfo
)
469 struct wb_queryuser_state
*state
= tevent_req_data(
470 req
, struct wb_queryuser_state
);
473 D_INFO("WB command queryuser end.\n");
474 NDR_PRINT_DEBUG_LEVEL(DBGLVL_INFO
, wbint_userinfo
, state
->info
);
475 if (tevent_req_is_nterror(req
, &status
)) {
478 *pinfo
= talloc_move(mem_ctx
, &state
->info
);