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/>.
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"
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
);
42 DEBUG(0, ("talloc failed\n"));
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;
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
);
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
,
94 sam2
->base
= info3
->base
;
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
);
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
;
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
);
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
));
179 if (pipe_session_key
) {
180 arcfour_crypt(info3
->base
.key
.key
,
181 pipe_session_key
, 16);
184 if (server_info
->lm_session_key
.length
) {
185 memcpy(info3
->base
.LMSessKey
.key
,
186 server_info
->lm_session_key
.data
,
187 MIN(sizeof(info3
->base
.LMSessKey
.key
),
188 server_info
->lm_session_key
.length
));
189 if (pipe_session_key
) {
190 arcfour_crypt(info3
->base
.LMSessKey
.key
,
191 pipe_session_key
, 8);
195 sam6
->base
= info3
->base
;
200 sam6
->dns_domainname
.string
= talloc_strdup(sam6
, dominfo
->dns_domain
);
201 if (sam6
->dns_domainname
.string
== NULL
) {
202 return NT_STATUS_NO_MEMORY
;
205 sam6
->principle
.string
= talloc_asprintf(sam6
, "%s@%s",
206 sam6
->base
.account_name
.string
,
207 sam6
->dns_domainname
.string
);
208 if (sam6
->principle
.string
== NULL
) {
209 return NT_STATUS_NO_MEMORY
;
215 static NTSTATUS
append_netr_SidAttr(TALLOC_CTX
*mem_ctx
,
216 struct netr_SidAttr
**sids
,
218 const struct dom_sid2
*asid
,
223 *sids
= talloc_realloc(mem_ctx
, *sids
, struct netr_SidAttr
, t
+ 1);
225 return NT_STATUS_NO_MEMORY
;
227 (*sids
)[t
].sid
= dom_sid_dup(*sids
, asid
);
228 if ((*sids
)[t
].sid
== NULL
) {
229 return NT_STATUS_NO_MEMORY
;
231 (*sids
)[t
].attributes
= attributes
;
237 /* Fills the samr_RidWithAttributeArray with the provided sids.
238 * If it happens that we have additional groups that do not belong
239 * to the domain, add their sids as extra sids */
240 static NTSTATUS
group_sids_to_info3(struct netr_SamInfo3
*info3
,
241 const struct dom_sid
*sids
,
244 uint32_t attributes
= SE_GROUP_MANDATORY
|
245 SE_GROUP_ENABLED_BY_DEFAULT
|
247 struct samr_RidWithAttributeArray
*groups
;
248 struct dom_sid
*domain_sid
;
254 domain_sid
= info3
->base
.domain_sid
;
255 groups
= &info3
->base
.groups
;
257 groups
->rids
= talloc_array(info3
,
258 struct samr_RidWithAttribute
, num_sids
);
260 return NT_STATUS_NO_MEMORY
;
263 for (i
= 0; i
< num_sids
; i
++) {
264 ok
= sid_peek_check_rid(domain_sid
, &sids
[i
], &rid
);
267 /* if it is the primary gid, skip it, we
268 * obviously already have it */
269 if (info3
->base
.primary_gid
== rid
) continue;
271 /* store domain group rid */
272 groups
->rids
[groups
->count
].rid
= rid
;
273 groups
->rids
[groups
->count
].attributes
= attributes
;
278 /* if this wasn't a domain sid, add it as extra sid */
279 status
= append_netr_SidAttr(info3
, &info3
->sids
,
281 &sids
[i
], attributes
);
282 if (!NT_STATUS_IS_OK(status
)) {
290 #define RET_NOMEM(ptr) do { \
292 TALLOC_FREE(info3); \
293 return NT_STATUS_NO_MEMORY; \
296 NTSTATUS
samu_to_SamInfo3(TALLOC_CTX
*mem_ctx
,
298 const char *login_server
,
299 struct netr_SamInfo3
**_info3
,
300 struct extra_auth_info
*extra
)
302 struct netr_SamInfo3
*info3
;
303 const struct dom_sid
*user_sid
;
304 const struct dom_sid
*group_sid
;
305 struct dom_sid domain_sid
;
306 struct dom_sid
*group_sids
;
307 uint32_t num_group_sids
= 0;
313 user_sid
= pdb_get_user_sid(samu
);
314 group_sid
= pdb_get_group_sid(samu
);
316 if (!user_sid
|| !group_sid
) {
317 DEBUG(1, ("Sam account is missing sids!\n"));
318 return NT_STATUS_UNSUCCESSFUL
;
321 info3
= talloc_zero(mem_ctx
, struct netr_SamInfo3
);
323 return NT_STATUS_NO_MEMORY
;
326 ZERO_STRUCT(domain_sid
);
328 /* check if this is a "Unix Users" domain user,
329 * we need to handle it in a special way if that's the case */
330 if (sid_check_is_in_unix_users(user_sid
)) {
331 /* in info3 you can only set rids for the user and the
332 * primary group, and the domain sid must be that of
335 * Store a completely bogus value here.
336 * The real SID is stored in the extra sids.
337 * Other code will know to look there if (-1) is found
339 info3
->base
.rid
= (uint32_t)(-1);
340 sid_copy(&extra
->user_sid
, user_sid
);
342 DEBUG(10, ("Unix User found in struct samu. Rid marked as "
343 "special and sid (%s) saved as extra sid\n",
344 sid_string_dbg(user_sid
)));
346 sid_copy(&domain_sid
, user_sid
);
347 sid_split_rid(&domain_sid
, &info3
->base
.rid
);
350 if (is_null_sid(&domain_sid
)) {
351 sid_copy(&domain_sid
, get_global_sam_sid());
354 /* check if this is a "Unix Groups" domain group,
355 * if so we need special handling */
356 if (sid_check_is_in_unix_groups(group_sid
)) {
357 /* in info3 you can only set rids for the user and the
358 * primary group, and the domain sid must be that of
361 * Store a completely bogus value here.
362 * The real SID is stored in the extra sids.
363 * Other code will know to look there if (-1) is found
365 info3
->base
.primary_gid
= (uint32_t)(-1);
366 sid_copy(&extra
->pgid_sid
, group_sid
);
368 DEBUG(10, ("Unix Group found in struct samu. Rid marked as "
369 "special and sid (%s) saved as extra sid\n",
370 sid_string_dbg(group_sid
)));
373 ok
= sid_peek_check_rid(&domain_sid
, group_sid
,
374 &info3
->base
.primary_gid
);
376 DEBUG(1, ("The primary group domain sid(%s) does not "
377 "match the domain sid(%s) for %s(%s)\n",
378 sid_string_dbg(group_sid
),
379 sid_string_dbg(&domain_sid
),
380 pdb_get_username(samu
),
381 sid_string_dbg(user_sid
)));
383 return NT_STATUS_UNSUCCESSFUL
;
387 unix_to_nt_time(&info3
->base
.logon_time
, pdb_get_logon_time(samu
));
388 unix_to_nt_time(&info3
->base
.logoff_time
, get_time_t_max());
389 unix_to_nt_time(&info3
->base
.kickoff_time
, get_time_t_max());
390 unix_to_nt_time(&info3
->base
.last_password_change
,
391 pdb_get_pass_last_set_time(samu
));
392 unix_to_nt_time(&info3
->base
.allow_password_change
,
393 pdb_get_pass_can_change_time(samu
));
394 unix_to_nt_time(&info3
->base
.force_password_change
,
395 pdb_get_pass_must_change_time(samu
));
397 tmp
= pdb_get_username(samu
);
399 info3
->base
.account_name
.string
= talloc_strdup(info3
, tmp
);
400 RET_NOMEM(info3
->base
.account_name
.string
);
402 tmp
= pdb_get_fullname(samu
);
404 info3
->base
.full_name
.string
= talloc_strdup(info3
, tmp
);
405 RET_NOMEM(info3
->base
.full_name
.string
);
407 tmp
= pdb_get_logon_script(samu
);
409 info3
->base
.logon_script
.string
= talloc_strdup(info3
, tmp
);
410 RET_NOMEM(info3
->base
.logon_script
.string
);
412 tmp
= pdb_get_profile_path(samu
);
414 info3
->base
.profile_path
.string
= talloc_strdup(info3
, tmp
);
415 RET_NOMEM(info3
->base
.profile_path
.string
);
417 tmp
= pdb_get_homedir(samu
);
419 info3
->base
.home_directory
.string
= talloc_strdup(info3
, tmp
);
420 RET_NOMEM(info3
->base
.home_directory
.string
);
422 tmp
= pdb_get_dir_drive(samu
);
424 info3
->base
.home_drive
.string
= talloc_strdup(info3
, tmp
);
425 RET_NOMEM(info3
->base
.home_drive
.string
);
428 info3
->base
.logon_count
= pdb_get_logon_count(samu
);
429 info3
->base
.bad_password_count
= pdb_get_bad_password_count(samu
);
431 info3
->base
.logon_domain
.string
= talloc_strdup(info3
,
432 pdb_get_domain(samu
));
433 RET_NOMEM(info3
->base
.logon_domain
.string
);
435 info3
->base
.domain_sid
= dom_sid_dup(info3
, &domain_sid
);
436 RET_NOMEM(info3
->base
.domain_sid
);
438 status
= pdb_enum_group_memberships(mem_ctx
, samu
,
441 if (!NT_STATUS_IS_OK(status
)) {
442 DEBUG(1, ("Failed to get groups from sam account.\n"));
447 if (num_group_sids
) {
448 status
= group_sids_to_info3(info3
, group_sids
, num_group_sids
);
449 if (!NT_STATUS_IS_OK(status
)) {
455 /* We don't need sids and gids after the conversion */
456 TALLOC_FREE(group_sids
);
460 /* FIXME: should we add other flags ? */
461 info3
->base
.user_flags
= NETLOGON_EXTRA_SIDS
;
464 info3
->base
.logon_server
.string
= talloc_strdup(info3
, login_server
);
465 RET_NOMEM(info3
->base
.logon_server
.string
);
468 info3
->base
.acct_flags
= pdb_get_acct_ctrl(samu
);
476 #define RET_NOMEM(ptr) do { \
478 TALLOC_FREE(info3); \
482 struct netr_SamInfo3
*copy_netr_SamInfo3(TALLOC_CTX
*mem_ctx
,
483 struct netr_SamInfo3
*orig
)
485 struct netr_SamInfo3
*info3
;
489 info3
= talloc_zero(mem_ctx
, struct netr_SamInfo3
);
490 if (!info3
) return NULL
;
492 status
= copy_netr_SamBaseInfo(info3
, &orig
->base
, &info3
->base
);
493 if (!NT_STATUS_IS_OK(status
)) {
498 if (orig
->sidcount
) {
499 info3
->sidcount
= orig
->sidcount
;
500 info3
->sids
= talloc_array(info3
, struct netr_SidAttr
,
502 RET_NOMEM(info3
->sids
);
503 for (i
= 0; i
< orig
->sidcount
; i
++) {
504 info3
->sids
[i
].sid
= dom_sid_dup(info3
->sids
,
506 RET_NOMEM(info3
->sids
[i
].sid
);
507 info3
->sids
[i
].attributes
=
508 orig
->sids
[i
].attributes
;
515 static NTSTATUS
wbcsids_to_samr_RidWithAttributeArray(
517 struct samr_RidWithAttributeArray
*groups
,
518 const struct dom_sid
*domain_sid
,
519 const struct wbcSidWithAttr
*sids
,
522 unsigned int i
, j
= 0;
525 groups
->rids
= talloc_array(mem_ctx
,
526 struct samr_RidWithAttribute
, num_sids
);
528 return NT_STATUS_NO_MEMORY
;
531 /* a wbcDomainSid is the same as a dom_sid */
532 for (i
= 0; i
< num_sids
; i
++) {
533 ok
= sid_peek_check_rid(domain_sid
,
534 (const struct dom_sid
*)&sids
[i
].sid
,
535 &groups
->rids
[j
].rid
);
538 groups
->rids
[j
].attributes
= SE_GROUP_MANDATORY
|
539 SE_GROUP_ENABLED_BY_DEFAULT
|
548 static NTSTATUS
wbcsids_to_netr_SidAttrArray(
549 const struct dom_sid
*domain_sid
,
550 const struct wbcSidWithAttr
*sids
,
553 struct netr_SidAttr
**_info3_sids
,
554 uint32_t *info3_num_sids
)
556 unsigned int i
, j
= 0;
557 struct netr_SidAttr
*info3_sids
;
559 info3_sids
= talloc_array(mem_ctx
, struct netr_SidAttr
, num_sids
);
560 if (info3_sids
== NULL
) {
561 return NT_STATUS_NO_MEMORY
;
564 /* a wbcDomainSid is the same as a dom_sid */
565 for (i
= 0; i
< num_sids
; i
++) {
566 const struct dom_sid
*sid
;
568 sid
= (const struct dom_sid
*)&sids
[i
].sid
;
570 if (dom_sid_in_domain(domain_sid
, sid
)) {
574 info3_sids
[j
].sid
= dom_sid_dup(info3_sids
, sid
);
575 if (info3_sids
[j
].sid
== NULL
) {
576 talloc_free(info3_sids
);
577 return NT_STATUS_NO_MEMORY
;
579 info3_sids
[j
].attributes
= SE_GROUP_MANDATORY
|
580 SE_GROUP_ENABLED_BY_DEFAULT
|
586 *_info3_sids
= info3_sids
;
590 struct netr_SamInfo3
*wbcAuthUserInfo_to_netr_SamInfo3(TALLOC_CTX
*mem_ctx
,
591 const struct wbcAuthUserInfo
*info
)
593 struct netr_SamInfo3
*info3
;
594 struct dom_sid user_sid
;
595 struct dom_sid group_sid
;
596 struct dom_sid domain_sid
;
600 memcpy(&user_sid
, &info
->sids
[0].sid
, sizeof(user_sid
));
601 memcpy(&group_sid
, &info
->sids
[1].sid
, sizeof(group_sid
));
603 info3
= talloc_zero(mem_ctx
, struct netr_SamInfo3
);
604 if (!info3
) return NULL
;
606 info3
->base
.logon_time
= info
->logon_time
;
607 info3
->base
.logoff_time
= info
->logoff_time
;
608 info3
->base
.kickoff_time
= info
->kickoff_time
;
609 unix_to_nt_time(&info3
->base
.last_password_change
, info
->pass_last_set_time
);
610 unix_to_nt_time(&info3
->base
.allow_password_change
,
611 info
->pass_can_change_time
);
612 unix_to_nt_time(&info3
->base
.force_password_change
,
613 info
->pass_must_change_time
);
615 if (info
->account_name
) {
616 info3
->base
.account_name
.string
=
617 talloc_strdup(info3
, info
->account_name
);
618 RET_NOMEM(info3
->base
.account_name
.string
);
620 if (info
->full_name
) {
621 info3
->base
.full_name
.string
=
622 talloc_strdup(info3
, info
->full_name
);
623 RET_NOMEM(info3
->base
.full_name
.string
);
625 if (info
->logon_script
) {
626 info3
->base
.logon_script
.string
=
627 talloc_strdup(info3
, info
->logon_script
);
628 RET_NOMEM(info3
->base
.logon_script
.string
);
630 if (info
->profile_path
) {
631 info3
->base
.profile_path
.string
=
632 talloc_strdup(info3
, info
->profile_path
);
633 RET_NOMEM(info3
->base
.profile_path
.string
);
635 if (info
->home_directory
) {
636 info3
->base
.home_directory
.string
=
637 talloc_strdup(info3
, info
->home_directory
);
638 RET_NOMEM(info3
->base
.home_directory
.string
);
640 if (info
->home_drive
) {
641 info3
->base
.home_drive
.string
=
642 talloc_strdup(info3
, info
->home_drive
);
643 RET_NOMEM(info3
->base
.home_drive
.string
);
646 info3
->base
.logon_count
= info
->logon_count
;
647 info3
->base
.bad_password_count
= info
->bad_password_count
;
649 sid_copy(&domain_sid
, &user_sid
);
650 sid_split_rid(&domain_sid
, &info3
->base
.rid
);
652 ok
= sid_peek_check_rid(&domain_sid
, &group_sid
,
653 &info3
->base
.primary_gid
);
655 DEBUG(1, ("The primary group sid domain does not"
656 "match user sid domain for user: %s\n",
657 info
->account_name
));
662 status
= wbcsids_to_samr_RidWithAttributeArray(info3
,
667 if (!NT_STATUS_IS_OK(status
)) {
672 status
= wbcsids_to_netr_SidAttrArray(&domain_sid
,
678 if (!NT_STATUS_IS_OK(status
)) {
683 info3
->base
.user_flags
= info
->user_flags
;
684 memcpy(info3
->base
.key
.key
, info
->user_session_key
, 16);
686 if (info
->logon_server
) {
687 info3
->base
.logon_server
.string
=
688 talloc_strdup(info3
, info
->logon_server
);
689 RET_NOMEM(info3
->base
.logon_server
.string
);
691 if (info
->domain_name
) {
692 info3
->base
.logon_domain
.string
=
693 talloc_strdup(info3
, info
->domain_name
);
694 RET_NOMEM(info3
->base
.logon_domain
.string
);
697 info3
->base
.domain_sid
= dom_sid_dup(info3
, &domain_sid
);
698 RET_NOMEM(info3
->base
.domain_sid
);
700 memcpy(info3
->base
.LMSessKey
.key
, info
->lm_session_key
, 8);
701 info3
->base
.acct_flags
= info
->acct_flags
;