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/util_unixsids.h"
23 #include "../lib/crypto/arcfour.h"
24 #include "../librpc/gen_ndr/netlogon.h"
25 #include "../libcli/security/security.h"
26 #include "rpc_client/util_netlogon.h"
27 #include "nsswitch/libwbclient/wbclient.h"
28 #include "lib/winbind_util.h"
32 #define DBGC_CLASS DBGC_AUTH
34 /***************************************************************************
35 Make a server_info struct. Free with TALLOC_FREE().
36 ***************************************************************************/
38 struct auth_serversupplied_info
*make_server_info(TALLOC_CTX
*mem_ctx
)
40 struct auth_serversupplied_info
*result
;
42 result
= talloc_zero(mem_ctx
, struct auth_serversupplied_info
);
44 DEBUG(0, ("talloc failed\n"));
48 /* Initialise the uid and gid values to something non-zero
49 which may save us from giving away root access if there
50 is a bug in allocating these fields. */
52 result
->utok
.uid
= -1;
53 result
->utok
.gid
= -1;
58 /****************************************************************************
59 inits a netr_SamInfo2 structure from an auth_serversupplied_info. sam2 must
60 already be initialized and is used as the talloc parent for its members.
61 *****************************************************************************/
63 NTSTATUS
serverinfo_to_SamInfo2(struct auth_serversupplied_info
*server_info
,
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
));
79 if (server_info
->lm_session_key
.length
) {
80 memcpy(info3
->base
.LMSessKey
.key
,
81 server_info
->lm_session_key
.data
,
82 MIN(sizeof(info3
->base
.LMSessKey
.key
),
83 server_info
->lm_session_key
.length
));
86 sam2
->base
= info3
->base
;
91 /****************************************************************************
92 inits a netr_SamInfo3 structure from an auth_serversupplied_info. sam3 must
93 already be initialized and is used as the talloc parent for its members.
94 *****************************************************************************/
96 NTSTATUS
serverinfo_to_SamInfo3(const struct auth_serversupplied_info
*server_info
,
97 struct netr_SamInfo3
*sam3
)
99 struct netr_SamInfo3
*info3
;
101 info3
= copy_netr_SamInfo3(sam3
, server_info
->info3
);
103 return NT_STATUS_NO_MEMORY
;
106 if (server_info
->session_key
.length
) {
107 memcpy(info3
->base
.key
.key
,
108 server_info
->session_key
.data
,
109 MIN(sizeof(info3
->base
.key
.key
),
110 server_info
->session_key
.length
));
112 if (server_info
->lm_session_key
.length
) {
113 memcpy(info3
->base
.LMSessKey
.key
,
114 server_info
->lm_session_key
.data
,
115 MIN(sizeof(info3
->base
.LMSessKey
.key
),
116 server_info
->lm_session_key
.length
));
119 sam3
->base
= info3
->base
;
127 /****************************************************************************
128 inits a netr_SamInfo6 structure from an auth_serversupplied_info. sam6 must
129 already be initialized and is used as the talloc parent for its members.
130 *****************************************************************************/
132 NTSTATUS
serverinfo_to_SamInfo6(struct auth_serversupplied_info
*server_info
,
133 struct netr_SamInfo6
*sam6
)
135 struct pdb_domain_info
*dominfo
;
136 struct netr_SamInfo3
*info3
;
138 if ((pdb_capabilities() & PDB_CAP_ADS
) == 0) {
139 DEBUG(10,("Not adding validation info level 6 "
140 "without ADS passdb backend\n"));
141 return NT_STATUS_INVALID_INFO_CLASS
;
144 dominfo
= pdb_get_domain_info(sam6
);
145 if (dominfo
== NULL
) {
146 return NT_STATUS_NO_MEMORY
;
149 info3
= copy_netr_SamInfo3(sam6
, server_info
->info3
);
151 return NT_STATUS_NO_MEMORY
;
154 if (server_info
->session_key
.length
) {
155 memcpy(info3
->base
.key
.key
,
156 server_info
->session_key
.data
,
157 MIN(sizeof(info3
->base
.key
.key
),
158 server_info
->session_key
.length
));
160 if (server_info
->lm_session_key
.length
) {
161 memcpy(info3
->base
.LMSessKey
.key
,
162 server_info
->lm_session_key
.data
,
163 MIN(sizeof(info3
->base
.LMSessKey
.key
),
164 server_info
->lm_session_key
.length
));
167 sam6
->base
= info3
->base
;
172 sam6
->dns_domainname
.string
= talloc_strdup(sam6
, dominfo
->dns_domain
);
173 if (sam6
->dns_domainname
.string
== NULL
) {
174 return NT_STATUS_NO_MEMORY
;
177 sam6
->principal_name
.string
= talloc_asprintf(
178 sam6
, "%s@%s", sam6
->base
.account_name
.string
,
179 sam6
->dns_domainname
.string
);
180 if (sam6
->principal_name
.string
== NULL
) {
181 return NT_STATUS_NO_MEMORY
;
187 static NTSTATUS
append_netr_SidAttr(TALLOC_CTX
*mem_ctx
,
188 struct netr_SidAttr
**sids
,
190 const struct dom_sid2
*asid
,
195 *sids
= talloc_realloc(mem_ctx
, *sids
, struct netr_SidAttr
, t
+ 1);
197 return NT_STATUS_NO_MEMORY
;
199 (*sids
)[t
].sid
= dom_sid_dup(*sids
, asid
);
200 if ((*sids
)[t
].sid
== NULL
) {
201 return NT_STATUS_NO_MEMORY
;
203 (*sids
)[t
].attributes
= attributes
;
209 /* Fills the samr_RidWithAttributeArray with the provided sids.
210 * If it happens that we have additional groups that do not belong
211 * to the domain, add their sids as extra sids */
212 static NTSTATUS
group_sids_to_info3(struct netr_SamInfo3
*info3
,
213 const struct dom_sid
*sids
,
216 uint32_t attributes
= SE_GROUP_MANDATORY
|
217 SE_GROUP_ENABLED_BY_DEFAULT
|
219 struct samr_RidWithAttributeArray
*groups
;
220 struct dom_sid
*domain_sid
;
226 domain_sid
= info3
->base
.domain_sid
;
227 groups
= &info3
->base
.groups
;
229 groups
->rids
= talloc_array(info3
,
230 struct samr_RidWithAttribute
, num_sids
);
232 return NT_STATUS_NO_MEMORY
;
235 for (i
= 0; i
< num_sids
; i
++) {
236 ok
= sid_peek_check_rid(domain_sid
, &sids
[i
], &rid
);
238 /* store domain group rid */
239 groups
->rids
[groups
->count
].rid
= rid
;
240 groups
->rids
[groups
->count
].attributes
= attributes
;
245 /* if this wasn't a domain sid, add it as extra sid */
246 status
= append_netr_SidAttr(info3
, &info3
->sids
,
248 &sids
[i
], attributes
);
249 if (!NT_STATUS_IS_OK(status
)) {
258 * Merge resource SIDs, if any, into the passed in info3 structure.
261 static NTSTATUS
merge_resource_sids(const struct PAC_LOGON_INFO
*logon_info
,
262 struct netr_SamInfo3
*info3
)
265 const struct PAC_DOMAIN_GROUP_MEMBERSHIP
*rg
= NULL
;
267 if (logon_info
->info3
.base
.user_flags
& NETLOGON_RESOURCE_GROUPS
) {
268 rg
= &logon_info
->resource_groups
;
275 if (rg
->domain_sid
== NULL
) {
276 DEBUG(10, ("Missing Resource Group Domain SID\n"));
277 return NT_STATUS_INVALID_PARAMETER
;
280 /* The IDL layer would be a better place to check this, but to
281 * guard the integer addition below, we double-check */
282 if (rg
->groups
.count
> 65535) {
283 DEBUG(10, ("Too much Resource Group RIDs %u\n",
284 (unsigned)rg
->groups
.count
));
285 return NT_STATUS_INVALID_PARAMETER
;
289 * If there are any resource groups (SID Compression) add
290 * them to the extra sids portion of the info3 in the PAC.
292 * This makes the info3 look like it would if we got the info
293 * from the DC rather than the PAC.
297 * Construct a SID for each RID in the list and then append it
300 for (i
= 0; i
< rg
->groups
.count
; i
++) {
302 struct dom_sid new_sid
;
303 uint32_t attributes
= rg
->groups
.rids
[i
].attributes
;
305 sid_compose(&new_sid
,
307 rg
->groups
.rids
[i
].rid
);
309 DEBUG(10, ("Adding SID %s to extra SIDS\n",
310 sid_string_dbg(&new_sid
)));
312 status
= append_netr_SidAttr(info3
, &info3
->sids
,
316 if (!NT_STATUS_IS_OK(status
)) {
317 DEBUG(1, ("failed to append SID %s to extra SIDS: %s\n",
318 sid_string_dbg(&new_sid
),
328 * Create a copy of an info3 struct from the struct PAC_LOGON_INFO,
329 * then merge resource SIDs, if any, into it. If successful return
330 * the created info3 struct.
333 NTSTATUS
create_info3_from_pac_logon_info(TALLOC_CTX
*mem_ctx
,
334 const struct PAC_LOGON_INFO
*logon_info
,
335 struct netr_SamInfo3
**pp_info3
)
338 struct netr_SamInfo3
*info3
= copy_netr_SamInfo3(mem_ctx
,
341 return NT_STATUS_NO_MEMORY
;
343 status
= merge_resource_sids(logon_info
, info3
);
344 if (!NT_STATUS_IS_OK(status
)) {
353 * Check if this is a "Unix Users" domain user, or a
354 * "Unix Groups" domain group, we need to handle it
355 * in a special way if that's the case.
358 static NTSTATUS
SamInfo3_handle_sids(const char *username
,
359 const struct dom_sid
*user_sid
,
360 const struct dom_sid
*group_sid
,
361 struct netr_SamInfo3
*info3
,
362 struct dom_sid
*domain_sid
,
363 struct extra_auth_info
*extra
)
365 if (sid_check_is_in_unix_users(user_sid
)) {
366 /* in info3 you can only set rids for the user and the
367 * primary group, and the domain sid must be that of
370 * Store a completely bogus value here.
371 * The real SID is stored in the extra sids.
372 * Other code will know to look there if (-1) is found
374 info3
->base
.rid
= (uint32_t)(-1);
375 sid_copy(&extra
->user_sid
, user_sid
);
377 DEBUG(10, ("Unix User found. Rid marked as "
378 "special and sid (%s) saved as extra sid\n",
379 sid_string_dbg(user_sid
)));
381 sid_copy(domain_sid
, user_sid
);
382 sid_split_rid(domain_sid
, &info3
->base
.rid
);
385 if (is_null_sid(domain_sid
)) {
386 sid_copy(domain_sid
, get_global_sam_sid());
389 /* check if this is a "Unix Groups" domain group,
390 * if so we need special handling */
391 if (sid_check_is_in_unix_groups(group_sid
)) {
392 /* in info3 you can only set rids for the user and the
393 * primary group, and the domain sid must be that of
396 * Store a completely bogus value here.
397 * The real SID is stored in the extra sids.
398 * Other code will know to look there if (-1) is found
400 info3
->base
.primary_gid
= (uint32_t)(-1);
401 sid_copy(&extra
->pgid_sid
, group_sid
);
403 DEBUG(10, ("Unix Group found. Rid marked as "
404 "special and sid (%s) saved as extra sid\n",
405 sid_string_dbg(group_sid
)));
407 bool ok
= sid_peek_check_rid(domain_sid
, group_sid
,
408 &info3
->base
.primary_gid
);
410 DEBUG(1, ("The primary group domain sid(%s) does not "
411 "match the domain sid(%s) for %s(%s)\n",
412 sid_string_dbg(group_sid
),
413 sid_string_dbg(domain_sid
),
415 sid_string_dbg(user_sid
)));
416 return NT_STATUS_INVALID_SID
;
422 #define RET_NOMEM(ptr) do { \
424 TALLOC_FREE(info3); \
425 return NT_STATUS_NO_MEMORY; \
428 NTSTATUS
samu_to_SamInfo3(TALLOC_CTX
*mem_ctx
,
430 const char *login_server
,
431 struct netr_SamInfo3
**_info3
,
432 struct extra_auth_info
*extra
)
434 struct netr_SamInfo3
*info3
;
435 const struct dom_sid
*user_sid
;
436 const struct dom_sid
*group_sid
;
437 struct dom_sid domain_sid
;
438 struct dom_sid
*group_sids
;
439 uint32_t num_group_sids
= 0;
444 user_sid
= pdb_get_user_sid(samu
);
445 group_sid
= pdb_get_group_sid(samu
);
447 if (!user_sid
|| !group_sid
) {
448 DEBUG(1, ("Sam account is missing sids!\n"));
449 return NT_STATUS_UNSUCCESSFUL
;
452 info3
= talloc_zero(mem_ctx
, struct netr_SamInfo3
);
454 return NT_STATUS_NO_MEMORY
;
457 ZERO_STRUCT(domain_sid
);
459 status
= SamInfo3_handle_sids(pdb_get_username(samu
),
466 if (!NT_STATUS_IS_OK(status
)) {
471 unix_to_nt_time(&info3
->base
.logon_time
, pdb_get_logon_time(samu
));
472 unix_to_nt_time(&info3
->base
.logoff_time
, get_time_t_max());
473 unix_to_nt_time(&info3
->base
.kickoff_time
, get_time_t_max());
474 unix_to_nt_time(&info3
->base
.last_password_change
,
475 pdb_get_pass_last_set_time(samu
));
476 unix_to_nt_time(&info3
->base
.allow_password_change
,
477 pdb_get_pass_can_change_time(samu
));
478 unix_to_nt_time(&info3
->base
.force_password_change
,
479 pdb_get_pass_must_change_time(samu
));
481 tmp
= pdb_get_username(samu
);
483 info3
->base
.account_name
.string
= talloc_strdup(info3
, tmp
);
484 RET_NOMEM(info3
->base
.account_name
.string
);
486 tmp
= pdb_get_fullname(samu
);
488 info3
->base
.full_name
.string
= talloc_strdup(info3
, tmp
);
489 RET_NOMEM(info3
->base
.full_name
.string
);
491 tmp
= pdb_get_logon_script(samu
);
493 info3
->base
.logon_script
.string
= talloc_strdup(info3
, tmp
);
494 RET_NOMEM(info3
->base
.logon_script
.string
);
496 tmp
= pdb_get_profile_path(samu
);
498 info3
->base
.profile_path
.string
= talloc_strdup(info3
, tmp
);
499 RET_NOMEM(info3
->base
.profile_path
.string
);
501 tmp
= pdb_get_homedir(samu
);
503 info3
->base
.home_directory
.string
= talloc_strdup(info3
, tmp
);
504 RET_NOMEM(info3
->base
.home_directory
.string
);
506 tmp
= pdb_get_dir_drive(samu
);
508 info3
->base
.home_drive
.string
= talloc_strdup(info3
, tmp
);
509 RET_NOMEM(info3
->base
.home_drive
.string
);
512 info3
->base
.logon_count
= pdb_get_logon_count(samu
);
513 info3
->base
.bad_password_count
= pdb_get_bad_password_count(samu
);
515 info3
->base
.logon_domain
.string
= talloc_strdup(info3
,
516 pdb_get_domain(samu
));
517 RET_NOMEM(info3
->base
.logon_domain
.string
);
519 info3
->base
.domain_sid
= dom_sid_dup(info3
, &domain_sid
);
520 RET_NOMEM(info3
->base
.domain_sid
);
522 status
= pdb_enum_group_memberships(mem_ctx
, samu
,
525 if (!NT_STATUS_IS_OK(status
)) {
526 DEBUG(1, ("Failed to get groups from sam account.\n"));
531 if (num_group_sids
) {
532 status
= group_sids_to_info3(info3
, group_sids
, num_group_sids
);
533 if (!NT_STATUS_IS_OK(status
)) {
539 /* We don't need sids and gids after the conversion */
540 TALLOC_FREE(group_sids
);
544 /* FIXME: should we add other flags ? */
545 info3
->base
.user_flags
= NETLOGON_EXTRA_SIDS
;
548 info3
->base
.logon_server
.string
= talloc_strdup(info3
, login_server
);
549 RET_NOMEM(info3
->base
.logon_server
.string
);
552 info3
->base
.acct_flags
= pdb_get_acct_ctrl(samu
);
558 NTSTATUS
passwd_to_SamInfo3(TALLOC_CTX
*mem_ctx
,
559 const char *unix_username
,
560 const struct passwd
*pwd
,
561 struct netr_SamInfo3
**pinfo3
,
562 struct extra_auth_info
*extra
)
564 struct netr_SamInfo3
*info3
;
567 const char *domain_name
= NULL
;
568 const char *user_name
= NULL
;
569 struct dom_sid domain_sid
;
570 struct dom_sid user_sid
;
571 struct dom_sid group_sid
;
572 enum lsa_SidType type
;
573 uint32_t num_sids
= 0;
574 struct dom_sid
*user_sids
= NULL
;
578 tmp_ctx
= talloc_stackframe();
580 ok
= lookup_name_smbconf(tmp_ctx
,
588 status
= NT_STATUS_NO_SUCH_USER
;
592 if (type
!= SID_NAME_USER
) {
593 status
= NT_STATUS_NO_SUCH_USER
;
597 ok
= winbind_lookup_usersids(tmp_ctx
,
601 /* Check if winbind is running */
604 * Winbind is running and the first element of the user_sids
605 * is the primary group.
608 group_sid
= user_sids
[0];
612 * Winbind is not running, try to create the group_sid from the
617 * This can lead to a primary group of S-1-22-2-XX which
618 * will be rejected by other Samba code.
620 gid_to_sid(&group_sid
, pwd
->pw_gid
);
624 * If we are a unix group, or a wellknown/builtin alias,
625 * set the group_sid to the
626 * 'Domain Users' RID of 513 which will always resolve to a
629 if (sid_check_is_in_unix_groups(&group_sid
) ||
630 sid_check_is_in_builtin(&group_sid
) ||
631 sid_check_is_in_wellknown_domain(&group_sid
)) {
632 if (sid_check_is_in_unix_users(&user_sid
)) {
633 sid_compose(&group_sid
,
634 get_global_sam_sid(),
637 sid_copy(&domain_sid
, &user_sid
);
638 sid_split_rid(&domain_sid
, NULL
);
639 sid_compose(&group_sid
,
645 /* Make sure we have a valid group sid */
646 is_null
= is_null_sid(&group_sid
);
648 status
= NT_STATUS_NO_SUCH_USER
;
652 /* Construct a netr_SamInfo3 from the information we have */
653 info3
= talloc_zero(tmp_ctx
, struct netr_SamInfo3
);
655 status
= NT_STATUS_NO_MEMORY
;
659 info3
->base
.account_name
.string
= talloc_strdup(info3
, unix_username
);
660 if (info3
->base
.account_name
.string
== NULL
) {
661 status
= NT_STATUS_NO_MEMORY
;
665 ZERO_STRUCT(domain_sid
);
667 status
= SamInfo3_handle_sids(unix_username
,
674 if (!NT_STATUS_IS_OK(status
)) {
678 info3
->base
.domain_sid
= dom_sid_dup(info3
, &domain_sid
);
679 if (info3
->base
.domain_sid
== NULL
) {
680 status
= NT_STATUS_NO_MEMORY
;
684 ok
= sid_peek_check_rid(&domain_sid
, &group_sid
,
685 &info3
->base
.primary_gid
);
687 DEBUG(1, ("The primary group domain sid(%s) does not "
688 "match the domain sid(%s) for %s(%s)\n",
689 sid_string_dbg(&group_sid
),
690 sid_string_dbg(&domain_sid
),
692 sid_string_dbg(&user_sid
)));
693 status
= NT_STATUS_INVALID_SID
;
697 info3
->base
.acct_flags
= ACB_NORMAL
;
700 status
= group_sids_to_info3(info3
, user_sids
, num_sids
);
701 if (!NT_STATUS_IS_OK(status
)) {
706 *pinfo3
= talloc_steal(mem_ctx
, info3
);
708 status
= NT_STATUS_OK
;
710 talloc_free(tmp_ctx
);
717 #define RET_NOMEM(ptr) do { \
719 TALLOC_FREE(info3); \
723 struct netr_SamInfo3
*copy_netr_SamInfo3(TALLOC_CTX
*mem_ctx
,
724 const struct netr_SamInfo3
*orig
)
726 struct netr_SamInfo3
*info3
;
730 info3
= talloc_zero(mem_ctx
, struct netr_SamInfo3
);
731 if (!info3
) return NULL
;
733 status
= copy_netr_SamBaseInfo(info3
, &orig
->base
, &info3
->base
);
734 if (!NT_STATUS_IS_OK(status
)) {
739 if (orig
->sidcount
) {
740 info3
->sidcount
= orig
->sidcount
;
741 info3
->sids
= talloc_array(info3
, struct netr_SidAttr
,
743 RET_NOMEM(info3
->sids
);
744 for (i
= 0; i
< orig
->sidcount
; i
++) {
745 info3
->sids
[i
].sid
= dom_sid_dup(info3
->sids
,
747 RET_NOMEM(info3
->sids
[i
].sid
);
748 info3
->sids
[i
].attributes
=
749 orig
->sids
[i
].attributes
;