s3-auth: session keys in validation level 6 samlogon replies are *not* encrypted.
[Samba.git] / source3 / auth / server_info.c
blob02bf689b2c2db2d8e7f1bd6c48cd447ab5ce90b4
1 /*
2 Unix SMB/CIFS implementation.
3 Authentication utility functions
4 Copyright (C) Volker Lendecke 2010
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/>.
20 #include "includes.h"
21 #include "auth.h"
22 #include "../lib/crypto/arcfour.h"
23 #include "../librpc/gen_ndr/netlogon.h"
24 #include "../libcli/security/security.h"
25 #include "rpc_client/util_netlogon.h"
26 #include "nsswitch/libwbclient/wbclient.h"
27 #include "passdb.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_AUTH
32 /***************************************************************************
33 Make a server_info struct. Free with TALLOC_FREE().
34 ***************************************************************************/
36 struct auth_serversupplied_info *make_server_info(TALLOC_CTX *mem_ctx)
38 struct auth_serversupplied_info *result;
40 result = talloc_zero(mem_ctx, struct auth_serversupplied_info);
41 if (result == NULL) {
42 DEBUG(0, ("talloc failed\n"));
43 return NULL;
46 /* Initialise the uid and gid values to something non-zero
47 which may save us from giving away root access if there
48 is a bug in allocating these fields. */
50 result->utok.uid = -1;
51 result->utok.gid = -1;
53 return result;
56 /****************************************************************************
57 inits a netr_SamInfo2 structure from an auth_serversupplied_info. sam2 must
58 already be initialized and is used as the talloc parent for its members.
59 *****************************************************************************/
61 NTSTATUS serverinfo_to_SamInfo2(struct auth_serversupplied_info *server_info,
62 uint8_t *pipe_session_key,
63 size_t pipe_session_key_len,
64 struct netr_SamInfo2 *sam2)
66 struct netr_SamInfo3 *info3;
68 info3 = copy_netr_SamInfo3(sam2, server_info->info3);
69 if (!info3) {
70 return NT_STATUS_NO_MEMORY;
73 if (server_info->session_key.length) {
74 memcpy(info3->base.key.key,
75 server_info->session_key.data,
76 MIN(sizeof(info3->base.key.key),
77 server_info->session_key.length));
78 if (pipe_session_key) {
79 arcfour_crypt(info3->base.key.key,
80 pipe_session_key, 16);
83 if (server_info->lm_session_key.length) {
84 memcpy(info3->base.LMSessKey.key,
85 server_info->lm_session_key.data,
86 MIN(sizeof(info3->base.LMSessKey.key),
87 server_info->lm_session_key.length));
88 if (pipe_session_key) {
89 arcfour_crypt(info3->base.LMSessKey.key,
90 pipe_session_key, 8);
94 sam2->base = info3->base;
96 return NT_STATUS_OK;
99 /****************************************************************************
100 inits a netr_SamInfo3 structure from an auth_serversupplied_info. sam3 must
101 already be initialized and is used as the talloc parent for its members.
102 *****************************************************************************/
104 NTSTATUS serverinfo_to_SamInfo3(const struct auth_serversupplied_info *server_info,
105 uint8_t *pipe_session_key,
106 size_t pipe_session_key_len,
107 struct netr_SamInfo3 *sam3)
109 struct netr_SamInfo3 *info3;
111 info3 = copy_netr_SamInfo3(sam3, server_info->info3);
112 if (!info3) {
113 return NT_STATUS_NO_MEMORY;
116 if (server_info->session_key.length) {
117 memcpy(info3->base.key.key,
118 server_info->session_key.data,
119 MIN(sizeof(info3->base.key.key),
120 server_info->session_key.length));
121 if (pipe_session_key) {
122 arcfour_crypt(info3->base.key.key,
123 pipe_session_key, 16);
126 if (server_info->lm_session_key.length) {
127 memcpy(info3->base.LMSessKey.key,
128 server_info->lm_session_key.data,
129 MIN(sizeof(info3->base.LMSessKey.key),
130 server_info->lm_session_key.length));
131 if (pipe_session_key) {
132 arcfour_crypt(info3->base.LMSessKey.key,
133 pipe_session_key, 8);
137 sam3->base = info3->base;
139 sam3->sidcount = 0;
140 sam3->sids = NULL;
142 return NT_STATUS_OK;
145 /****************************************************************************
146 inits a netr_SamInfo6 structure from an auth_serversupplied_info. sam6 must
147 already be initialized and is used as the talloc parent for its members.
148 *****************************************************************************/
150 NTSTATUS serverinfo_to_SamInfo6(struct auth_serversupplied_info *server_info,
151 uint8_t *pipe_session_key,
152 size_t pipe_session_key_len,
153 struct netr_SamInfo6 *sam6)
155 struct pdb_domain_info *dominfo;
156 struct netr_SamInfo3 *info3;
158 if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
159 DEBUG(10,("Not adding validation info level 6 "
160 "without ADS passdb backend\n"));
161 return NT_STATUS_INVALID_INFO_CLASS;
164 dominfo = pdb_get_domain_info(sam6);
165 if (dominfo == NULL) {
166 return NT_STATUS_NO_MEMORY;
169 info3 = copy_netr_SamInfo3(sam6, server_info->info3);
170 if (!info3) {
171 return NT_STATUS_NO_MEMORY;
174 if (server_info->session_key.length) {
175 memcpy(info3->base.key.key,
176 server_info->session_key.data,
177 MIN(sizeof(info3->base.key.key),
178 server_info->session_key.length));
180 if (server_info->lm_session_key.length) {
181 memcpy(info3->base.LMSessKey.key,
182 server_info->lm_session_key.data,
183 MIN(sizeof(info3->base.LMSessKey.key),
184 server_info->lm_session_key.length));
187 sam6->base = info3->base;
189 sam6->sidcount = 0;
190 sam6->sids = NULL;
192 sam6->dns_domainname.string = talloc_strdup(sam6, dominfo->dns_domain);
193 if (sam6->dns_domainname.string == NULL) {
194 return NT_STATUS_NO_MEMORY;
197 sam6->principle.string = talloc_asprintf(sam6, "%s@%s",
198 sam6->base.account_name.string,
199 sam6->dns_domainname.string);
200 if (sam6->principle.string == NULL) {
201 return NT_STATUS_NO_MEMORY;
204 return NT_STATUS_OK;
207 static NTSTATUS append_netr_SidAttr(TALLOC_CTX *mem_ctx,
208 struct netr_SidAttr **sids,
209 uint32_t *count,
210 const struct dom_sid2 *asid,
211 uint32_t attributes)
213 uint32_t t = *count;
215 *sids = talloc_realloc(mem_ctx, *sids, struct netr_SidAttr, t + 1);
216 if (*sids == NULL) {
217 return NT_STATUS_NO_MEMORY;
219 (*sids)[t].sid = dom_sid_dup(*sids, asid);
220 if ((*sids)[t].sid == NULL) {
221 return NT_STATUS_NO_MEMORY;
223 (*sids)[t].attributes = attributes;
224 *count = t + 1;
226 return NT_STATUS_OK;
229 /* Fills the samr_RidWithAttributeArray with the provided sids.
230 * If it happens that we have additional groups that do not belong
231 * to the domain, add their sids as extra sids */
232 static NTSTATUS group_sids_to_info3(struct netr_SamInfo3 *info3,
233 const struct dom_sid *sids,
234 size_t num_sids)
236 uint32_t attributes = SE_GROUP_MANDATORY |
237 SE_GROUP_ENABLED_BY_DEFAULT |
238 SE_GROUP_ENABLED;
239 struct samr_RidWithAttributeArray *groups;
240 struct dom_sid *domain_sid;
241 unsigned int i;
242 NTSTATUS status;
243 uint32_t rid;
244 bool ok;
246 domain_sid = info3->base.domain_sid;
247 groups = &info3->base.groups;
249 groups->rids = talloc_array(info3,
250 struct samr_RidWithAttribute, num_sids);
251 if (!groups->rids) {
252 return NT_STATUS_NO_MEMORY;
255 for (i = 0; i < num_sids; i++) {
256 ok = sid_peek_check_rid(domain_sid, &sids[i], &rid);
257 if (ok) {
258 /* store domain group rid */
259 groups->rids[groups->count].rid = rid;
260 groups->rids[groups->count].attributes = attributes;
261 groups->count++;
262 continue;
265 /* if this wasn't a domain sid, add it as extra sid */
266 status = append_netr_SidAttr(info3, &info3->sids,
267 &info3->sidcount,
268 &sids[i], attributes);
269 if (!NT_STATUS_IS_OK(status)) {
270 return status;
274 return NT_STATUS_OK;
277 #define RET_NOMEM(ptr) do { \
278 if (!ptr) { \
279 TALLOC_FREE(info3); \
280 return NT_STATUS_NO_MEMORY; \
281 } } while(0)
283 NTSTATUS samu_to_SamInfo3(TALLOC_CTX *mem_ctx,
284 struct samu *samu,
285 const char *login_server,
286 struct netr_SamInfo3 **_info3,
287 struct extra_auth_info *extra)
289 struct netr_SamInfo3 *info3;
290 const struct dom_sid *user_sid;
291 const struct dom_sid *group_sid;
292 struct dom_sid domain_sid;
293 struct dom_sid *group_sids;
294 uint32_t num_group_sids = 0;
295 const char *tmp;
296 gid_t *gids;
297 NTSTATUS status;
298 bool ok;
300 user_sid = pdb_get_user_sid(samu);
301 group_sid = pdb_get_group_sid(samu);
303 if (!user_sid || !group_sid) {
304 DEBUG(1, ("Sam account is missing sids!\n"));
305 return NT_STATUS_UNSUCCESSFUL;
308 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
309 if (!info3) {
310 return NT_STATUS_NO_MEMORY;
313 ZERO_STRUCT(domain_sid);
315 /* check if this is a "Unix Users" domain user,
316 * we need to handle it in a special way if that's the case */
317 if (sid_check_is_in_unix_users(user_sid)) {
318 /* in info3 you can only set rids for the user and the
319 * primary group, and the domain sid must be that of
320 * the sam domain.
322 * Store a completely bogus value here.
323 * The real SID is stored in the extra sids.
324 * Other code will know to look there if (-1) is found
326 info3->base.rid = (uint32_t)(-1);
327 sid_copy(&extra->user_sid, user_sid);
329 DEBUG(10, ("Unix User found in struct samu. Rid marked as "
330 "special and sid (%s) saved as extra sid\n",
331 sid_string_dbg(user_sid)));
332 } else {
333 sid_copy(&domain_sid, user_sid);
334 sid_split_rid(&domain_sid, &info3->base.rid);
337 if (is_null_sid(&domain_sid)) {
338 sid_copy(&domain_sid, get_global_sam_sid());
341 /* check if this is a "Unix Groups" domain group,
342 * if so we need special handling */
343 if (sid_check_is_in_unix_groups(group_sid)) {
344 /* in info3 you can only set rids for the user and the
345 * primary group, and the domain sid must be that of
346 * the sam domain.
348 * Store a completely bogus value here.
349 * The real SID is stored in the extra sids.
350 * Other code will know to look there if (-1) is found
352 info3->base.primary_gid = (uint32_t)(-1);
353 sid_copy(&extra->pgid_sid, group_sid);
355 DEBUG(10, ("Unix Group found in struct samu. Rid marked as "
356 "special and sid (%s) saved as extra sid\n",
357 sid_string_dbg(group_sid)));
359 } else {
360 ok = sid_peek_check_rid(&domain_sid, group_sid,
361 &info3->base.primary_gid);
362 if (!ok) {
363 DEBUG(1, ("The primary group domain sid(%s) does not "
364 "match the domain sid(%s) for %s(%s)\n",
365 sid_string_dbg(group_sid),
366 sid_string_dbg(&domain_sid),
367 pdb_get_username(samu),
368 sid_string_dbg(user_sid)));
369 TALLOC_FREE(info3);
370 return NT_STATUS_UNSUCCESSFUL;
374 unix_to_nt_time(&info3->base.logon_time, pdb_get_logon_time(samu));
375 unix_to_nt_time(&info3->base.logoff_time, get_time_t_max());
376 unix_to_nt_time(&info3->base.kickoff_time, get_time_t_max());
377 unix_to_nt_time(&info3->base.last_password_change,
378 pdb_get_pass_last_set_time(samu));
379 unix_to_nt_time(&info3->base.allow_password_change,
380 pdb_get_pass_can_change_time(samu));
381 unix_to_nt_time(&info3->base.force_password_change,
382 pdb_get_pass_must_change_time(samu));
384 tmp = pdb_get_username(samu);
385 if (tmp) {
386 info3->base.account_name.string = talloc_strdup(info3, tmp);
387 RET_NOMEM(info3->base.account_name.string);
389 tmp = pdb_get_fullname(samu);
390 if (tmp) {
391 info3->base.full_name.string = talloc_strdup(info3, tmp);
392 RET_NOMEM(info3->base.full_name.string);
394 tmp = pdb_get_logon_script(samu);
395 if (tmp) {
396 info3->base.logon_script.string = talloc_strdup(info3, tmp);
397 RET_NOMEM(info3->base.logon_script.string);
399 tmp = pdb_get_profile_path(samu);
400 if (tmp) {
401 info3->base.profile_path.string = talloc_strdup(info3, tmp);
402 RET_NOMEM(info3->base.profile_path.string);
404 tmp = pdb_get_homedir(samu);
405 if (tmp) {
406 info3->base.home_directory.string = talloc_strdup(info3, tmp);
407 RET_NOMEM(info3->base.home_directory.string);
409 tmp = pdb_get_dir_drive(samu);
410 if (tmp) {
411 info3->base.home_drive.string = talloc_strdup(info3, tmp);
412 RET_NOMEM(info3->base.home_drive.string);
415 info3->base.logon_count = pdb_get_logon_count(samu);
416 info3->base.bad_password_count = pdb_get_bad_password_count(samu);
418 info3->base.logon_domain.string = talloc_strdup(info3,
419 pdb_get_domain(samu));
420 RET_NOMEM(info3->base.logon_domain.string);
422 info3->base.domain_sid = dom_sid_dup(info3, &domain_sid);
423 RET_NOMEM(info3->base.domain_sid);
425 status = pdb_enum_group_memberships(mem_ctx, samu,
426 &group_sids, &gids,
427 &num_group_sids);
428 if (!NT_STATUS_IS_OK(status)) {
429 DEBUG(1, ("Failed to get groups from sam account.\n"));
430 TALLOC_FREE(info3);
431 return status;
434 if (num_group_sids) {
435 status = group_sids_to_info3(info3, group_sids, num_group_sids);
436 if (!NT_STATUS_IS_OK(status)) {
437 TALLOC_FREE(info3);
438 return status;
442 /* We don't need sids and gids after the conversion */
443 TALLOC_FREE(group_sids);
444 TALLOC_FREE(gids);
445 num_group_sids = 0;
447 /* FIXME: should we add other flags ? */
448 info3->base.user_flags = NETLOGON_EXTRA_SIDS;
450 if (login_server) {
451 info3->base.logon_server.string = talloc_strdup(info3, login_server);
452 RET_NOMEM(info3->base.logon_server.string);
455 info3->base.acct_flags = pdb_get_acct_ctrl(samu);
457 *_info3 = info3;
458 return NT_STATUS_OK;
461 #undef RET_NOMEM
463 #define RET_NOMEM(ptr) do { \
464 if (!ptr) { \
465 TALLOC_FREE(info3); \
466 return NULL; \
467 } } while(0)
469 struct netr_SamInfo3 *copy_netr_SamInfo3(TALLOC_CTX *mem_ctx,
470 struct netr_SamInfo3 *orig)
472 struct netr_SamInfo3 *info3;
473 unsigned int i;
474 NTSTATUS status;
476 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
477 if (!info3) return NULL;
479 status = copy_netr_SamBaseInfo(info3, &orig->base, &info3->base);
480 if (!NT_STATUS_IS_OK(status)) {
481 TALLOC_FREE(info3);
482 return NULL;
485 if (orig->sidcount) {
486 info3->sidcount = orig->sidcount;
487 info3->sids = talloc_array(info3, struct netr_SidAttr,
488 orig->sidcount);
489 RET_NOMEM(info3->sids);
490 for (i = 0; i < orig->sidcount; i++) {
491 info3->sids[i].sid = dom_sid_dup(info3->sids,
492 orig->sids[i].sid);
493 RET_NOMEM(info3->sids[i].sid);
494 info3->sids[i].attributes =
495 orig->sids[i].attributes;
499 return info3;
502 static NTSTATUS wbcsids_to_samr_RidWithAttributeArray(
503 TALLOC_CTX *mem_ctx,
504 struct samr_RidWithAttributeArray *groups,
505 const struct dom_sid *domain_sid,
506 const struct wbcSidWithAttr *sids,
507 size_t num_sids)
509 unsigned int i, j = 0;
510 bool ok;
512 groups->rids = talloc_array(mem_ctx,
513 struct samr_RidWithAttribute, num_sids);
514 if (!groups->rids) {
515 return NT_STATUS_NO_MEMORY;
518 /* a wbcDomainSid is the same as a dom_sid */
519 for (i = 0; i < num_sids; i++) {
520 ok = sid_peek_check_rid(domain_sid,
521 (const struct dom_sid *)&sids[i].sid,
522 &groups->rids[j].rid);
523 if (!ok) continue;
525 groups->rids[j].attributes = SE_GROUP_MANDATORY |
526 SE_GROUP_ENABLED_BY_DEFAULT |
527 SE_GROUP_ENABLED;
528 j++;
531 groups->count = j;
532 return NT_STATUS_OK;
535 static NTSTATUS wbcsids_to_netr_SidAttrArray(
536 const struct dom_sid *domain_sid,
537 const struct wbcSidWithAttr *sids,
538 size_t num_sids,
539 TALLOC_CTX *mem_ctx,
540 struct netr_SidAttr **_info3_sids,
541 uint32_t *info3_num_sids)
543 unsigned int i, j = 0;
544 struct netr_SidAttr *info3_sids;
546 info3_sids = talloc_array(mem_ctx, struct netr_SidAttr, num_sids);
547 if (info3_sids == NULL) {
548 return NT_STATUS_NO_MEMORY;
551 /* a wbcDomainSid is the same as a dom_sid */
552 for (i = 0; i < num_sids; i++) {
553 const struct dom_sid *sid;
555 sid = (const struct dom_sid *)&sids[i].sid;
557 if (dom_sid_in_domain(domain_sid, sid)) {
558 continue;
561 info3_sids[j].sid = dom_sid_dup(info3_sids, sid);
562 if (info3_sids[j].sid == NULL) {
563 talloc_free(info3_sids);
564 return NT_STATUS_NO_MEMORY;
566 info3_sids[j].attributes = SE_GROUP_MANDATORY |
567 SE_GROUP_ENABLED_BY_DEFAULT |
568 SE_GROUP_ENABLED;
569 j++;
572 *info3_num_sids = j;
573 *_info3_sids = info3_sids;
574 return NT_STATUS_OK;
577 struct netr_SamInfo3 *wbcAuthUserInfo_to_netr_SamInfo3(TALLOC_CTX *mem_ctx,
578 const struct wbcAuthUserInfo *info)
580 struct netr_SamInfo3 *info3;
581 struct dom_sid user_sid;
582 struct dom_sid group_sid;
583 struct dom_sid domain_sid;
584 NTSTATUS status;
585 bool ok;
587 memcpy(&user_sid, &info->sids[0].sid, sizeof(user_sid));
588 memcpy(&group_sid, &info->sids[1].sid, sizeof(group_sid));
590 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
591 if (!info3) return NULL;
593 info3->base.logon_time = info->logon_time;
594 info3->base.logoff_time = info->logoff_time;
595 info3->base.kickoff_time = info->kickoff_time;
596 unix_to_nt_time(&info3->base.last_password_change, info->pass_last_set_time);
597 unix_to_nt_time(&info3->base.allow_password_change,
598 info->pass_can_change_time);
599 unix_to_nt_time(&info3->base.force_password_change,
600 info->pass_must_change_time);
602 if (info->account_name) {
603 info3->base.account_name.string =
604 talloc_strdup(info3, info->account_name);
605 RET_NOMEM(info3->base.account_name.string);
607 if (info->full_name) {
608 info3->base.full_name.string =
609 talloc_strdup(info3, info->full_name);
610 RET_NOMEM(info3->base.full_name.string);
612 if (info->logon_script) {
613 info3->base.logon_script.string =
614 talloc_strdup(info3, info->logon_script);
615 RET_NOMEM(info3->base.logon_script.string);
617 if (info->profile_path) {
618 info3->base.profile_path.string =
619 talloc_strdup(info3, info->profile_path);
620 RET_NOMEM(info3->base.profile_path.string);
622 if (info->home_directory) {
623 info3->base.home_directory.string =
624 talloc_strdup(info3, info->home_directory);
625 RET_NOMEM(info3->base.home_directory.string);
627 if (info->home_drive) {
628 info3->base.home_drive.string =
629 talloc_strdup(info3, info->home_drive);
630 RET_NOMEM(info3->base.home_drive.string);
633 info3->base.logon_count = info->logon_count;
634 info3->base.bad_password_count = info->bad_password_count;
636 sid_copy(&domain_sid, &user_sid);
637 sid_split_rid(&domain_sid, &info3->base.rid);
639 ok = sid_peek_check_rid(&domain_sid, &group_sid,
640 &info3->base.primary_gid);
641 if (!ok) {
642 DEBUG(1, ("The primary group sid domain does not"
643 "match user sid domain for user: %s\n",
644 info->account_name));
645 TALLOC_FREE(info3);
646 return NULL;
649 status = wbcsids_to_samr_RidWithAttributeArray(info3,
650 &info3->base.groups,
651 &domain_sid,
652 &info->sids[1],
653 info->num_sids - 1);
654 if (!NT_STATUS_IS_OK(status)) {
655 TALLOC_FREE(info3);
656 return NULL;
659 status = wbcsids_to_netr_SidAttrArray(&domain_sid,
660 &info->sids[1],
661 info->num_sids - 1,
662 info3,
663 &info3->sids,
664 &info3->sidcount);
665 if (!NT_STATUS_IS_OK(status)) {
666 TALLOC_FREE(info3);
667 return NULL;
670 info3->base.user_flags = info->user_flags;
671 memcpy(info3->base.key.key, info->user_session_key, 16);
673 if (info->logon_server) {
674 info3->base.logon_server.string =
675 talloc_strdup(info3, info->logon_server);
676 RET_NOMEM(info3->base.logon_server.string);
678 if (info->domain_name) {
679 info3->base.logon_domain.string =
680 talloc_strdup(info3, info->domain_name);
681 RET_NOMEM(info3->base.logon_domain.string);
684 info3->base.domain_sid = dom_sid_dup(info3, &domain_sid);
685 RET_NOMEM(info3->base.domain_sid);
687 memcpy(info3->base.LMSessKey.key, info->lm_session_key, 8);
688 info3->base.acct_flags = info->acct_flags;
690 return info3;