2 Unix SMB/CIFS implementation.
4 Convert a server info struct into the form for PAC and NETLOGON replies
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2011
7 Copyright (C) Stefan Metzmacher <metze@samba.org> 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "librpc/gen_ndr/auth.h"
25 #include "libcli/security/security.h"
26 #include "auth/auth_sam_reply.h"
28 NTSTATUS auth_convert_user_info_dc_sambaseinfo(TALLOC_CTX *mem_ctx,
29 const struct auth_user_info_dc *user_info_dc,
30 struct netr_SamBaseInfo **_sam)
33 struct auth_user_info *info;
34 struct netr_SamBaseInfo *sam = talloc_zero(mem_ctx, struct netr_SamBaseInfo);
35 NT_STATUS_HAVE_NO_MEMORY(sam);
37 if (user_info_dc->num_sids > PRIMARY_USER_SID_INDEX) {
38 status = dom_sid_split_rid(sam, &user_info_dc->sids[PRIMARY_USER_SID_INDEX],
39 &sam->domain_sid, &sam->rid);
40 if (!NT_STATUS_IS_OK(status)) {
44 return NT_STATUS_INVALID_PARAMETER;
47 if (user_info_dc->num_sids > PRIMARY_GROUP_SID_INDEX) {
48 status = dom_sid_split_rid(NULL, &user_info_dc->sids[PRIMARY_GROUP_SID_INDEX],
49 NULL, &sam->primary_gid);
50 if (!NT_STATUS_IS_OK(status)) {
54 /* if we have to encode something like SYSTEM (with no
55 * second SID in the token) then this is the only
57 sam->primary_gid = sam->rid;
60 info = user_info_dc->info;
62 sam->logon_time = info->last_logon;
63 sam->logoff_time = info->last_logoff;
64 sam->kickoff_time = info->acct_expiry;
65 sam->last_password_change = info->last_password_change;
66 sam->allow_password_change = info->allow_password_change;
67 sam->force_password_change = info->force_password_change;
69 sam->account_name.string = info->account_name;
70 sam->full_name.string = info->full_name;
71 sam->logon_script.string = info->logon_script;
72 sam->profile_path.string = info->profile_path;
73 sam->home_directory.string = info->home_directory;
74 sam->home_drive.string = info->home_drive;
76 sam->logon_count = info->logon_count;
77 sam->bad_password_count = info->bad_password_count;
78 sam->groups.count = 0;
79 sam->groups.rids = NULL;
81 if (user_info_dc->num_sids > 2) {
83 sam->groups.rids = talloc_array(sam, struct samr_RidWithAttribute,
84 user_info_dc->num_sids);
86 if (sam->groups.rids == NULL)
87 return NT_STATUS_NO_MEMORY;
89 for (i=2; i<user_info_dc->num_sids; i++) {
90 struct dom_sid *group_sid = &user_info_dc->sids[i];
91 if (!dom_sid_in_domain(sam->domain_sid, group_sid)) {
92 /* We handle this elsewhere */
95 sam->groups.rids[sam->groups.count].rid =
96 group_sid->sub_auths[group_sid->num_auths-1];
98 sam->groups.rids[sam->groups.count].attributes =
99 SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
100 sam->groups.count += 1;
104 sam->user_flags = 0; /* w2k3 uses NETLOGON_EXTRA_SIDS | NETLOGON_NTLMV2_ENABLED */
105 if (!user_info_dc->info->authenticated) {
106 sam->user_flags |= NETLOGON_GUEST;
108 sam->acct_flags = user_info_dc->info->acct_flags;
109 sam->logon_server.string = user_info_dc->info->logon_server;
110 sam->logon_domain.string = user_info_dc->info->domain_name;
111 sam->sub_auth_status = 0;
112 sam->last_successful_logon = 0;
113 sam->last_failed_logon = 0;
114 sam->failed_logon_count = 0;
117 ZERO_STRUCT(sam->key);
118 if (user_info_dc->user_session_key.length == sizeof(sam->key.key)) {
119 memcpy(sam->key.key, user_info_dc->user_session_key.data, sizeof(sam->key.key));
122 ZERO_STRUCT(sam->LMSessKey);
123 if (user_info_dc->lm_session_key.length == sizeof(sam->LMSessKey.key)) {
124 memcpy(sam->LMSessKey.key, user_info_dc->lm_session_key.data,
125 sizeof(sam->LMSessKey.key));
133 /* Note that the validity of the _sam6 structure is only as long as
134 * the user_info_dc it was generated from */
135 NTSTATUS auth_convert_user_info_dc_saminfo6(TALLOC_CTX *mem_ctx,
136 const struct auth_user_info_dc *user_info_dc,
137 struct netr_SamInfo6 **_sam6)
140 struct netr_SamBaseInfo *sam = NULL;
141 struct netr_SamInfo6 *sam6 = NULL;
144 sam6 = talloc_zero(mem_ctx, struct netr_SamInfo6);
146 return NT_STATUS_NO_MEMORY;
149 status = auth_convert_user_info_dc_sambaseinfo(sam6, user_info_dc, &sam);
150 if (!NT_STATUS_IS_OK(status)) {
156 sam6->sids = talloc_array(sam6, struct netr_SidAttr,
157 user_info_dc->num_sids);
158 if (sam6->sids == NULL) {
160 return NT_STATUS_NO_MEMORY;
163 /* We don't put the user and group SIDs in there */
164 for (i=2; i<user_info_dc->num_sids; i++) {
165 if (dom_sid_in_domain(sam->domain_sid, &user_info_dc->sids[i])) {
168 sam6->sids[sam6->sidcount].sid = dom_sid_dup(sam6->sids, &user_info_dc->sids[i]);
169 if (sam6->sids[sam6->sidcount].sid == NULL) {
171 return NT_STATUS_NO_MEMORY;
173 sam6->sids[sam6->sidcount].attributes =
174 SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
177 if (sam6->sidcount) {
178 sam6->base.user_flags |= NETLOGON_EXTRA_SIDS;
183 if (user_info_dc->info->dns_domain_name != NULL) {
184 sam6->dns_domainname.string = talloc_strdup(sam6,
185 user_info_dc->info->dns_domain_name);
186 if (sam6->dns_domainname.string == NULL) {
188 return NT_STATUS_NO_MEMORY;
192 if (user_info_dc->info->user_principal_name != NULL) {
193 sam6->principal_name.string = talloc_strdup(sam6,
194 user_info_dc->info->user_principal_name);
195 if (sam6->principal_name.string == NULL) {
197 return NT_STATUS_NO_MEMORY;
205 /* Note that the validity of the _sam2 structure is only as long as
206 * the user_info_dc it was generated from */
207 NTSTATUS auth_convert_user_info_dc_saminfo2(TALLOC_CTX *mem_ctx,
208 const struct auth_user_info_dc *user_info_dc,
209 struct netr_SamInfo2 **_sam2)
212 struct netr_SamInfo6 *sam6 = NULL;
213 struct netr_SamInfo2 *sam2 = NULL;
215 sam2 = talloc_zero(mem_ctx, struct netr_SamInfo2);
217 return NT_STATUS_NO_MEMORY;
220 status = auth_convert_user_info_dc_saminfo6(sam2, user_info_dc, &sam6);
221 if (!NT_STATUS_IS_OK(status)) {
225 sam2->base = sam6->base;
231 /* Note that the validity of the _sam3 structure is only as long as
232 * the user_info_dc it was generated from */
233 NTSTATUS auth_convert_user_info_dc_saminfo3(TALLOC_CTX *mem_ctx,
234 const struct auth_user_info_dc *user_info_dc,
235 struct netr_SamInfo3 **_sam3)
238 struct netr_SamInfo6 *sam6 = NULL;
239 struct netr_SamInfo3 *sam3 = NULL;
241 sam3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
243 return NT_STATUS_NO_MEMORY;
246 status = auth_convert_user_info_dc_saminfo6(sam3, user_info_dc, &sam6);
247 if (!NT_STATUS_IS_OK(status)) {
251 sam3->base = sam6->base;
252 sam3->sidcount = sam6->sidcount;
253 sam3->sids = sam6->sids;
260 * Make a user_info struct from the info3 or similar returned by a domain logon.
262 * The netr_SamInfo3 is also a key structure in the source3 auth subsystem
265 NTSTATUS make_user_info_SamBaseInfo(TALLOC_CTX *mem_ctx,
266 const char *account_name,
267 const struct netr_SamBaseInfo *base,
269 struct auth_user_info **_user_info)
271 struct auth_user_info *info;
273 info = talloc_zero(mem_ctx, struct auth_user_info);
274 NT_STATUS_HAVE_NO_MEMORY(info);
276 if (base->account_name.string) {
277 info->account_name = talloc_strdup(info, base->account_name.string);
279 info->account_name = talloc_strdup(info, account_name);
281 NT_STATUS_HAVE_NO_MEMORY(info->account_name);
283 if (base->logon_domain.string) {
284 info->domain_name = talloc_strdup(info, base->logon_domain.string);
285 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
288 if (base->full_name.string) {
289 info->full_name = talloc_strdup(info, base->full_name.string);
290 NT_STATUS_HAVE_NO_MEMORY(info->full_name);
292 if (base->logon_script.string) {
293 info->logon_script = talloc_strdup(info, base->logon_script.string);
294 NT_STATUS_HAVE_NO_MEMORY(info->logon_script);
296 if (base->profile_path.string) {
297 info->profile_path = talloc_strdup(info, base->profile_path.string);
298 NT_STATUS_HAVE_NO_MEMORY(info->profile_path);
300 if (base->home_directory.string) {
301 info->home_directory = talloc_strdup(info, base->home_directory.string);
302 NT_STATUS_HAVE_NO_MEMORY(info->home_directory);
304 if (base->home_drive.string) {
305 info->home_drive = talloc_strdup(info, base->home_drive.string);
306 NT_STATUS_HAVE_NO_MEMORY(info->home_drive);
308 if (base->logon_server.string) {
309 info->logon_server = talloc_strdup(info, base->logon_server.string);
310 NT_STATUS_HAVE_NO_MEMORY(info->logon_server);
312 info->last_logon = base->logon_time;
313 info->last_logoff = base->logoff_time;
314 info->acct_expiry = base->kickoff_time;
315 info->last_password_change = base->last_password_change;
316 info->allow_password_change = base->allow_password_change;
317 info->force_password_change = base->force_password_change;
318 info->logon_count = base->logon_count;
319 info->bad_password_count = base->bad_password_count;
320 info->acct_flags = base->acct_flags;
322 /* Only set authenticated if both NETLOGON_GUEST is not set, and authenticated is set */
323 info->authenticated = (authenticated && (!(base->user_flags & NETLOGON_GUEST)));
330 * Make a user_info_dc struct from the info3 returned by a domain logon
332 NTSTATUS make_user_info_dc_netlogon_validation(TALLOC_CTX *mem_ctx,
333 const char *account_name,
334 uint16_t validation_level,
335 const union netr_Validation *validation,
337 struct auth_user_info_dc **_user_info_dc)
340 struct auth_user_info_dc *user_info_dc = NULL;
341 const struct netr_SamBaseInfo *base = NULL;
342 uint32_t sidcount = 0;
343 const struct netr_SidAttr *sids = NULL;
344 const char *dns_domainname = NULL;
345 const char *principal = NULL;
348 switch (validation_level) {
350 if (!validation || !validation->sam2) {
351 return NT_STATUS_INVALID_PARAMETER;
353 base = &validation->sam2->base;
356 if (!validation || !validation->sam3) {
357 return NT_STATUS_INVALID_PARAMETER;
359 base = &validation->sam3->base;
360 sidcount = validation->sam3->sidcount;
361 sids = validation->sam3->sids;
364 if (!validation || !validation->sam6) {
365 return NT_STATUS_INVALID_PARAMETER;
367 base = &validation->sam6->base;
368 sidcount = validation->sam6->sidcount;
369 sids = validation->sam6->sids;
370 dns_domainname = validation->sam6->dns_domainname.string;
371 principal = validation->sam6->principal_name.string;
374 return NT_STATUS_INVALID_LEVEL;
377 user_info_dc = talloc(mem_ctx, struct auth_user_info_dc);
378 NT_STATUS_HAVE_NO_MEMORY(user_info_dc);
381 Here is where we should check the list of
382 trusted domains, and verify that the SID
385 if (!base->domain_sid) {
386 DEBUG(0, ("Cannot operate on a Netlogon Validation without a domain SID"));
387 return NT_STATUS_INVALID_PARAMETER;
390 /* The IDL layer would be a better place to check this, but to
391 * guard the integer addition below, we double-check */
392 if (base->groups.count > 65535) {
393 return NT_STATUS_INVALID_PARAMETER;
396 user_info_dc->num_sids = 2;
398 user_info_dc->sids = talloc_array(user_info_dc, struct dom_sid, user_info_dc->num_sids + base->groups.count);
399 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->sids);
401 user_info_dc->sids[PRIMARY_USER_SID_INDEX] = *base->domain_sid;
402 if (!sid_append_rid(&user_info_dc->sids[PRIMARY_USER_SID_INDEX], base->rid)) {
403 return NT_STATUS_INVALID_PARAMETER;
406 user_info_dc->sids[PRIMARY_GROUP_SID_INDEX] = *base->domain_sid;
407 if (!sid_append_rid(&user_info_dc->sids[PRIMARY_GROUP_SID_INDEX], base->primary_gid)) {
408 return NT_STATUS_INVALID_PARAMETER;
411 for (i = 0; i < base->groups.count; i++) {
412 user_info_dc->sids[user_info_dc->num_sids] = *base->domain_sid;
413 if (!sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids], base->groups.rids[i].rid)) {
414 return NT_STATUS_INVALID_PARAMETER;
416 user_info_dc->num_sids++;
419 /* Copy 'other' sids. We need to do sid filtering here to
420 prevent possible elevation of privileges. See:
422 http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
426 * The IDL layer would be a better place to check this, but to
427 * guard the integer addition below, we double-check
429 if (sidcount > UINT16_MAX) {
430 return NT_STATUS_INVALID_PARAMETER;
434 struct dom_sid *dgrps = user_info_dc->sids;
437 dgrps_count = user_info_dc->num_sids + sidcount;
438 dgrps = talloc_realloc(user_info_dc, dgrps, struct dom_sid,
441 return NT_STATUS_NO_MEMORY;
444 for (i = 0; i < sidcount; i++) {
446 dgrps[user_info_dc->num_sids] = *sids[i].sid;
447 user_info_dc->num_sids++;
451 user_info_dc->sids = dgrps;
453 /* Where are the 'global' sids?... */
456 status = make_user_info_SamBaseInfo(user_info_dc, account_name, base, authenticated, &user_info_dc->info);
457 if (!NT_STATUS_IS_OK(status)) {
461 if (dns_domainname != NULL) {
462 user_info_dc->info->dns_domain_name = talloc_strdup(user_info_dc->info,
464 if (user_info_dc->info->dns_domain_name == NULL) {
465 return NT_STATUS_NO_MEMORY;
469 if (principal != NULL) {
470 user_info_dc->info->user_principal_name = talloc_strdup(user_info_dc->info,
472 if (user_info_dc->info->user_principal_name == NULL) {
473 return NT_STATUS_NO_MEMORY;
477 /* ensure we are never given NULL session keys */
479 if (all_zero(base->key.key, sizeof(base->key.key))) {
480 user_info_dc->user_session_key = data_blob(NULL, 0);
482 user_info_dc->user_session_key = data_blob_talloc(user_info_dc, base->key.key, sizeof(base->key.key));
483 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->user_session_key.data);
486 if (all_zero(base->LMSessKey.key, sizeof(base->LMSessKey.key))) {
487 user_info_dc->lm_session_key = data_blob(NULL, 0);
489 user_info_dc->lm_session_key = data_blob_talloc(user_info_dc, base->LMSessKey.key, sizeof(base->LMSessKey.key));
490 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->lm_session_key.data);
493 *_user_info_dc = user_info_dc;
498 * Make a user_info_dc struct from the PAC_LOGON_INFO supplied in the krb5 logon
500 NTSTATUS make_user_info_dc_pac(TALLOC_CTX *mem_ctx,
501 const struct PAC_LOGON_INFO *pac_logon_info,
502 struct auth_user_info_dc **_user_info_dc)
506 union netr_Validation validation;
507 struct auth_user_info_dc *user_info_dc;
509 validation.sam3 = discard_const_p(struct netr_SamInfo3, &pac_logon_info->info3);
511 nt_status = make_user_info_dc_netlogon_validation(mem_ctx, "", 3, &validation,
512 true, /* This user was authenticated */
514 if (!NT_STATUS_IS_OK(nt_status)) {
518 if (pac_logon_info->res_groups.count > 0) {
520 /* The IDL layer would be a better place to check this, but to
521 * guard the integer addition below, we double-check */
522 if (pac_logon_info->res_groups.count > 65535) {
523 talloc_free(user_info_dc);
524 return NT_STATUS_INVALID_PARAMETER;
528 Here is where we should check the list of
529 trusted domains, and verify that the SID
532 if (!pac_logon_info->res_group_dom_sid) {
533 DEBUG(0, ("Cannot operate on a PAC without a resource domain SID"));
534 return NT_STATUS_INVALID_PARAMETER;
537 sidcount = user_info_dc->num_sids + pac_logon_info->res_groups.count;
539 = talloc_realloc(user_info_dc, user_info_dc->sids, struct dom_sid, sidcount);
540 if (user_info_dc->sids == NULL) {
541 TALLOC_FREE(user_info_dc);
542 return NT_STATUS_NO_MEMORY;
545 for (i = 0; pac_logon_info->res_group_dom_sid && i < pac_logon_info->res_groups.count; i++) {
546 user_info_dc->sids[user_info_dc->num_sids] = *pac_logon_info->res_group_dom_sid;
547 if (!sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids],
548 pac_logon_info->res_groups.rids[i].rid)) {
549 return NT_STATUS_INVALID_PARAMETER;
551 user_info_dc->num_sids++;
554 *_user_info_dc = user_info_dc;