diff options
Diffstat (limited to 'source3/auth/token_util.c')
-rw-r--r-- | source3/auth/token_util.c | 224 |
1 files changed, 164 insertions, 60 deletions
diff --git a/source3/auth/token_util.c b/source3/auth/token_util.c index ff55a671f3..841bc52792 100644 --- a/source3/auth/token_util.c +++ b/source3/auth/token_util.c @@ -25,6 +25,7 @@ /* functions moved from auth/auth_util.c to minimize linker deps */ #include "includes.h" +#include "system/passwd.h" #include "auth.h" #include "secrets.h" #include "memcache.h" @@ -92,10 +93,10 @@ struct security_token *get_root_nt_token( void ) cache_data, struct security_token); } - if ( !(pw = sys_getpwuid(0)) ) { - if ( !(pw = sys_getpwnam("root")) ) { - DEBUG(0,("get_root_nt_token: both sys_getpwuid(0) " - "and sys_getpwnam(\"root\") failed!\n")); + if ( !(pw = getpwuid(0)) ) { + if ( !(pw = getpwnam("root")) ) { + DEBUG(0,("get_root_nt_token: both getpwuid(0) " + "and getpwnam(\"root\") failed!\n")); return NULL; } } @@ -211,8 +212,8 @@ static NTSTATUS finalize_local_nt_token(struct security_token *result, NTSTATUS create_local_nt_token_from_info3(TALLOC_CTX *mem_ctx, bool is_guest, - struct netr_SamInfo3 *info3, - struct extra_auth_info *extra, + const struct netr_SamInfo3 *info3, + const struct extra_auth_info *extra, struct security_token **ntok) { struct security_token *usrtok = NULL; @@ -338,7 +339,7 @@ struct security_token *create_local_nt_token(TALLOC_CTX *mem_ctx, DEBUG(10, ("Create local NT token for %s\n", sid_string_dbg(user_sid))); - if (!(result = TALLOC_ZERO_P(mem_ctx, struct security_token))) { + if (!(result = talloc_zero(mem_ctx, struct security_token))) { DEBUG(0, ("talloc failed\n")); return NULL; } @@ -535,11 +536,7 @@ void debug_unix_user_token(int dbg_class, int dbg_lev, uid_t uid, gid_t gid, } /* - * Create an artificial NT token given just a username. (Initially intended - * for force user) - * - * We go through lookup_name() to avoid problems we had with 'winbind use - * default domain'. + * Create an artificial NT token given just a domain SID. * * We have 3 cases: * @@ -553,16 +550,15 @@ void debug_unix_user_token(int dbg_class, int dbg_lev, uid_t uid, gid_t gid, * http://lists.samba.org/archive/samba-technical/2006-January/044803.html. */ -NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username, - bool is_guest, - uid_t *uid, gid_t *gid, - char **found_username, - struct security_token **token) +static NTSTATUS create_token_from_sid(TALLOC_CTX *mem_ctx, + const struct dom_sid *user_sid, + bool is_guest, + uid_t *uid, gid_t *gid, + char **found_username, + struct security_token **token) { NTSTATUS result = NT_STATUS_NO_SUCH_USER; TALLOC_CTX *tmp_ctx = talloc_stackframe(); - struct dom_sid user_sid; - enum lsa_SidType type; gid_t *gids; struct dom_sid *group_sids; struct dom_sid unix_group_sid; @@ -570,19 +566,7 @@ NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username, uint32_t num_gids; uint32_t i; - if (!lookup_name_smbconf(tmp_ctx, username, LOOKUP_NAME_ALL, - NULL, NULL, &user_sid, &type)) { - DEBUG(1, ("lookup_name_smbconf for %s failed\n", username)); - goto done; - } - - if (type != SID_NAME_USER) { - DEBUG(1, ("%s is a %s, not a user\n", username, - sid_type_lookup(type))); - goto done; - } - - if (sid_check_is_in_our_domain(&user_sid)) { + if (sid_check_is_in_our_sam(user_sid)) { bool ret; uint32_t pdb_num_group_sids; /* This is a passdb user, so ask passdb */ @@ -595,13 +579,13 @@ NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username, } become_root(); - ret = pdb_getsampwsid(sam_acct, &user_sid); + ret = pdb_getsampwsid(sam_acct, user_sid); unbecome_root(); if (!ret) { - DEBUG(1, ("pdb_getsampwsid(%s) for user %s failed\n", - sid_string_dbg(&user_sid), username)); - DEBUGADD(1, ("Fall back to unix user %s\n", username)); + DEBUG(1, ("pdb_getsampwsid(%s) failed\n", + sid_string_dbg(user_sid))); + DEBUGADD(1, ("Fall back to unix user\n")); goto unix_user; } @@ -609,10 +593,10 @@ NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username, &group_sids, &gids, &pdb_num_group_sids); if (!NT_STATUS_IS_OK(result)) { - DEBUG(1, ("enum_group_memberships failed for %s (%s): " - "%s\n", username, sid_string_dbg(&user_sid), + DEBUG(1, ("enum_group_memberships failed for %s: " + "%s\n", sid_string_dbg(user_sid), nt_errstr(result))); - DEBUGADD(1, ("Fall back to unix user %s\n", username)); + DEBUGADD(1, ("Fall back to unix uid lookup\n")); goto unix_user; } num_group_sids = pdb_num_group_sids; @@ -626,6 +610,11 @@ NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username, *found_username = talloc_strdup(mem_ctx, pdb_get_username(sam_acct)); + if (found_username == NULL) { + result = NT_STATUS_NO_MEMORY; + goto done; + } + /* * If the SID from lookup_name() was the guest sid, passdb knows * about the mapping of guest sid to lp_guestaccount() @@ -653,7 +642,8 @@ NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username, } *uid = sam_acct->unix_pw->pw_uid; - } else if (sid_check_is_in_unix_users(&user_sid)) { + } else if (sid_check_is_in_unix_users(user_sid)) { + struct dom_sid tmp_sid; uint32_t getgroups_num_group_sids; /* This is a unix user not in passdb. We need to ask nss * directly, without consulting passdb */ @@ -668,34 +658,35 @@ NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username, unix_user: - if (!sid_to_uid(&user_sid, uid)) { - DEBUG(1, ("unix_user case, sid_to_uid for %s (%s) failed\n", - username, sid_string_dbg(&user_sid))); + if (!sid_to_uid(user_sid, uid)) { + DEBUG(1, ("unix_user case, sid_to_uid for %s failed\n", + sid_string_dbg(user_sid))); result = NT_STATUS_NO_SUCH_USER; goto done; } - uid_to_unix_users_sid(*uid, &user_sid); + uid_to_unix_users_sid(*uid, &tmp_sid); + user_sid = &tmp_sid; pass = getpwuid_alloc(tmp_ctx, *uid); if (pass == NULL) { - DEBUG(1, ("getpwuid(%u) for user %s failed\n", - (unsigned int)*uid, username)); + DEBUG(1, ("getpwuid(%u) failed\n", + (unsigned int)*uid)); goto done; } - if (!getgroups_unix_user(tmp_ctx, username, pass->pw_gid, + if (!getgroups_unix_user(tmp_ctx, pass->pw_name, pass->pw_gid, &gids, &getgroups_num_group_sids)) { DEBUG(1, ("getgroups_unix_user for user %s failed\n", - username)); + pass->pw_name)); goto done; } num_group_sids = getgroups_num_group_sids; if (num_group_sids) { - group_sids = TALLOC_ARRAY(tmp_ctx, struct dom_sid, num_group_sids); + group_sids = talloc_array(tmp_ctx, struct dom_sid, num_group_sids); if (group_sids == NULL) { - DEBUG(1, ("TALLOC_ARRAY failed\n")); + DEBUG(1, ("talloc_array failed\n")); result = NT_STATUS_NO_MEMORY; goto done; } @@ -714,6 +705,10 @@ NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username, /* Ensure we're returning the found_username on the right context. */ *found_username = talloc_strdup(mem_ctx, pass->pw_name); + if (found_username == NULL) { + result = NT_STATUS_NO_MEMORY; + goto done; + } } else { /* This user is from winbind, force the primary gid to the @@ -724,22 +719,22 @@ NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username, * information. */ /* We must always assign the *uid. */ - if (!sid_to_uid(&user_sid, uid)) { - DEBUG(1, ("winbindd case, sid_to_uid for %s (%s) failed\n", - username, sid_string_dbg(&user_sid))); + if (!sid_to_uid(user_sid, uid)) { + DEBUG(1, ("winbindd case, sid_to_uid for %s failed\n", + sid_string_dbg(user_sid))); result = NT_STATUS_NO_SUCH_USER; goto done; } num_group_sids = 1; - group_sids = TALLOC_ARRAY(tmp_ctx, struct dom_sid, num_group_sids); + group_sids = talloc_array(tmp_ctx, struct dom_sid, num_group_sids); if (group_sids == NULL) { - DEBUG(1, ("TALLOC_ARRAY failed\n")); + DEBUG(1, ("talloc_array failed\n")); result = NT_STATUS_NO_MEMORY; goto done; } - sid_copy(&group_sids[0], &user_sid); + sid_copy(&group_sids[0], user_sid); sid_split_rid(&group_sids[0], NULL); sid_append_rid(&group_sids[0], DOMAIN_RID_USERS); @@ -751,8 +746,7 @@ NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username, gids = gid; - /* Ensure we're returning the found_username on the right context. */ - *found_username = talloc_strdup(mem_ctx, username); + *found_username = NULL; } /* Add the "Unix Group" SID for each gid to catch mapped groups @@ -781,10 +775,10 @@ NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username, } /* Ensure we're creating the nt_token on the right context. */ - *token = create_local_nt_token(mem_ctx, &user_sid, + *token = create_local_nt_token(mem_ctx, user_sid, is_guest, num_group_sids, group_sids); - if ((*token == NULL) || (*found_username == NULL)) { + if (*token == NULL) { result = NT_STATUS_NO_MEMORY; goto done; } @@ -795,6 +789,116 @@ NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username, return result; } +/* + * Create an artificial NT token given just a username. (Initially intended + * for force user) + * + * We go through lookup_name() to avoid problems we had with 'winbind use + * default domain'. + * + * We have 3 cases: + * + * unmapped unix users: Go directly to nss to find the user's group. + * + * A passdb user: The list of groups is provided by pdb_enum_group_memberships. + * + * If the user is provided by winbind, the primary gid is set to "domain + * users" of the user's domain. For an explanation why this is necessary, see + * the thread starting at + * http://lists.samba.org/archive/samba-technical/2006-January/044803.html. + */ + +NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username, + bool is_guest, + uid_t *uid, gid_t *gid, + char **found_username, + struct security_token **token) +{ + NTSTATUS result = NT_STATUS_NO_SUCH_USER; + TALLOC_CTX *tmp_ctx = talloc_stackframe(); + struct dom_sid user_sid; + enum lsa_SidType type; + + if (!lookup_name_smbconf(tmp_ctx, username, LOOKUP_NAME_ALL, + NULL, NULL, &user_sid, &type)) { + DEBUG(1, ("lookup_name_smbconf for %s failed\n", username)); + goto done; + } + + if (type != SID_NAME_USER) { + DEBUG(1, ("%s is a %s, not a user\n", username, + sid_type_lookup(type))); + goto done; + } + + result = create_token_from_sid(mem_ctx, &user_sid, is_guest, uid, gid, found_username, token); + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + if (*found_username == NULL) { + *found_username = talloc_strdup(mem_ctx, username); + } + + if ((*token == NULL) || (*found_username == NULL)) { + result = NT_STATUS_NO_MEMORY; + goto done; + } + + result = NT_STATUS_OK; +done: + TALLOC_FREE(tmp_ctx); + return result; +} + +/*************************************************************************** + Build upon create_token_from_sid: + + Expensive helper function to figure out whether a user given its sid is + member of a particular group. +***************************************************************************/ + +bool user_sid_in_group_sid(const struct dom_sid *sid, const struct dom_sid *group_sid) +{ + NTSTATUS status; + uid_t uid; + gid_t gid; + char *found_username; + struct security_token *token; + bool result; + enum lsa_SidType type; + TALLOC_CTX *mem_ctx = talloc_stackframe(); + + if (!lookup_sid(mem_ctx, sid, + NULL, NULL, &type)) { + DEBUG(1, ("lookup_sid for %s failed\n", dom_sid_string(mem_ctx, sid))); + goto done; + } + + if (type != SID_NAME_USER) { + DEBUG(5, ("%s is a %s, not a user\n", dom_sid_string(mem_ctx, sid), + sid_type_lookup(type))); + goto done; + } + + status = create_token_from_sid(mem_ctx, sid, False, + &uid, &gid, &found_username, + &token); + + if (!NT_STATUS_IS_OK(status)) { + DEBUG(10, ("could not create token for %s\n", dom_sid_string(mem_ctx, sid))); + TALLOC_FREE(mem_ctx); + return False; + } + + result = security_token_has_sid(token, group_sid); + +done: + TALLOC_FREE(mem_ctx); + return result; +} + /*************************************************************************** Build upon create_token_from_username: |