krb5: Require krb5_get_host_realm and krb5_free_host_realm be available to build...
[samba.git] / source3 / passdb / passdb.c
index 1989f6d19194f5c6c0ebd2aa50a870ce6b1fbc41..276e0314c84295ab636d9ac9104af5f09f8fdd1e 100644 (file)
 */
 
 #include "includes.h"
+#include "passdb.h"
+#include "system/passwd.h"
 #include "../libcli/auth/libcli_auth.h"
 #include "secrets.h"
+#include "../libcli/security/security.h"
+#include "../lib/util/util_pw.h"
+#include "util_tdb.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_PASSDB
@@ -41,7 +46,7 @@ const char *my_sam_name(void)
 {
        /* Standalone servers can only use the local netbios name */
        if ( lp_server_role() == ROLE_STANDALONE )
-               return global_myname();
+               return lp_netbios_name();
 
        /* Default to the DOMAIN name when not specified */
        return lp_workgroup();
@@ -69,7 +74,7 @@ struct samu *samu_new( TALLOC_CTX *ctx )
 {
        struct samu *user;
 
-       if ( !(user = TALLOC_ZERO_P( ctx, struct samu )) ) {
+       if ( !(user = talloc_zero( ctx, struct samu )) ) {
                DEBUG(0,("samuser_new: Talloc failed!\n"));
                return NULL;
        }
@@ -141,10 +146,11 @@ static int count_commas(const char *str)
  attributes and a user SID.
 *********************************************************************/
 
-static NTSTATUS samu_set_unix_internal(struct samu *user, const struct passwd *pwd, bool create)
+static NTSTATUS samu_set_unix_internal(struct pdb_methods *methods,
+                                      struct samu *user, const struct passwd *pwd, bool create)
 {
        const char *guest_account = lp_guestaccount();
-       const char *domain = global_myname();
+       const char *domain = lp_netbios_name();
        char *fullname;
        uint32_t urid;
 
@@ -241,11 +247,11 @@ static NTSTATUS samu_set_unix_internal(struct samu *user, const struct passwd *p
           initialized and will fill in these fields later (such as from a 
           netr_SamInfo3 structure) */
 
-       if ( create && (pdb_capabilities() & PDB_CAP_STORE_RIDS)) {
+       if ( create && (methods->capabilities(methods) & PDB_CAP_STORE_RIDS)) {
                uint32_t user_rid;
                struct dom_sid user_sid;
 
-               if ( !pdb_new_rid( &user_rid ) ) {
+               if ( !methods->new_rid(methods, &user_rid) ) {
                        DEBUG(3, ("Could not allocate a new RID\n"));
                        return NT_STATUS_ACCESS_DENIED;
                }
@@ -277,12 +283,13 @@ static NTSTATUS samu_set_unix_internal(struct samu *user, const struct passwd *p
 
 NTSTATUS samu_set_unix(struct samu *user, const struct passwd *pwd)
 {
-       return samu_set_unix_internal( user, pwd, False );
+       return samu_set_unix_internal( NULL, user, pwd, False );
 }
 
-NTSTATUS samu_alloc_rid_unix(struct samu *user, const struct passwd *pwd)
+NTSTATUS samu_alloc_rid_unix(struct pdb_methods *methods,
+                            struct samu *user, const struct passwd *pwd)
 {
-       return samu_set_unix_internal( user, pwd, True );
+       return samu_set_unix_internal( methods, user, pwd, True );
 }
 
 /**********************************************************
@@ -380,9 +387,9 @@ void pdb_sethexpwd(char p[33], const unsigned char *pwd, uint32_t acct_ctrl)
                        slprintf(&p[i*2], 3, "%02X", pwd[i]);
        } else {
                if (acct_ctrl & ACB_PWNOTREQ)
-                       safe_strcpy(p, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX", 32);
+                       strlcpy(p, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX", 33);
                else
-                       safe_strcpy(p, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 32);
+                       strlcpy(p, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 33);
        }
 }
 
@@ -402,8 +409,8 @@ bool pdb_gethexpwd(const char *p, unsigned char *pwd)
                return false;
 
        for (i = 0; i < 32; i += 2) {
-               hinybble = toupper_ascii(p[i]);
-               lonybble = toupper_ascii(p[i + 1]);
+               hinybble = toupper_m(p[i]);
+               lonybble = toupper_m(p[i + 1]);
 
                p1 = strchr(hexchars, hinybble);
                p2 = strchr(hexchars, lonybble);
@@ -431,7 +438,7 @@ void pdb_sethexhours(char *p, const unsigned char *hours)
                        slprintf(&p[i*2], 3, "%02X", hours[i]);
                }
        } else {
-               safe_strcpy(p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 43);
+               strlcpy(p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 44);
        }
 }
 
@@ -452,8 +459,8 @@ bool pdb_gethexhours(const char *p, unsigned char *hours)
        }
 
        for (i = 0; i < 42; i += 2) {
-               hinybble = toupper_ascii(p[i]);
-               lonybble = toupper_ascii(p[i + 1]);
+               hinybble = toupper_m(p[i]);
+               lonybble = toupper_m(p[i + 1]);
 
                p1 = strchr(hexchars, hinybble);
                p2 = strchr(hexchars, lonybble);
@@ -584,7 +591,7 @@ bool algorithmic_pdb_rid_is_user(uint32_t rid)
 bool lookup_global_sam_name(const char *name, int flags, uint32_t *rid,
                            enum lsa_SidType *type)
 {
-       GROUP_MAP map;
+       GROUP_MAP *map;
        bool ret;
 
        /* Windows treats "MACHINE\None" as a special name for 
@@ -638,24 +645,32 @@ bool lookup_global_sam_name(const char *name, int flags, uint32_t *rid,
         * Maybe it is a group ?
         */
 
+       map = talloc_zero(NULL, GROUP_MAP);
+       if (!map) {
+               return false;
+       }
+
        become_root();
-       ret = pdb_getgrnam(&map, name);
+       ret = pdb_getgrnam(map, name);
        unbecome_root();
 
        if (!ret) {
+               TALLOC_FREE(map);
                return False;
        }
 
        /* BUILTIN groups are looked up elsewhere */
-       if (!sid_check_is_in_our_domain(&map.sid)) {
+       if (!sid_check_is_in_our_domain(&map->sid)) {
                DEBUG(10, ("Found group %s (%s) not in our domain -- "
-                          "ignoring.", name, sid_string_dbg(&map.sid)));
+                          "ignoring.", name, sid_string_dbg(&map->sid)));
+               TALLOC_FREE(map);
                return False;
        }
 
        /* yes it's a mapped group */
-       sid_peek_rid(&map.sid, rid);
-       *type = map.sid_name_use;
+       sid_peek_rid(&map->sid, rid);
+       *type = map->sid_name_use;
+       TALLOC_FREE(map);
        return True;
 }
 
@@ -1085,7 +1100,7 @@ static bool init_samu_from_buffer_v0(struct samu *sampass, uint8_t *buf, uint32_
        pdb_set_unknown_6(sampass, unknown_6, PDB_SET);
        pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
        pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
-       pdb_set_hours(sampass, hours, PDB_SET);
+       pdb_set_hours(sampass, hours, hours_len, PDB_SET);
 
 done:
 
@@ -1277,7 +1292,7 @@ static bool init_samu_from_buffer_v1(struct samu *sampass, uint8_t *buf, uint32_
        pdb_set_unknown_6(sampass, unknown_6, PDB_SET);
        pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
        pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
-       pdb_set_hours(sampass, hours, PDB_SET);
+       pdb_set_hours(sampass, hours, hours_len, PDB_SET);
 
 done:
 
@@ -1508,7 +1523,7 @@ static bool init_samu_from_buffer_v2(struct samu *sampass, uint8_t *buf, uint32_
        pdb_set_unknown_6(sampass, unknown_6, PDB_SET);
        pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
        pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
-       pdb_set_hours(sampass, hours, PDB_SET);
+       pdb_set_hours(sampass, hours, hours_len, PDB_SET);
 
 done:
 
@@ -1743,7 +1758,7 @@ static bool init_samu_from_buffer_v3(struct samu *sampass, uint8_t *buf, uint32_
        /* Change from V2 is the uint32_t acct_ctrl */
        pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
        pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
-       pdb_set_hours(sampass, hours, PDB_SET);
+       pdb_set_hours(sampass, hours, hours_len, PDB_SET);
 
 done:
 
@@ -2350,7 +2365,7 @@ bool get_trust_pw_clear(const char *domain, char **ret_pwd,
        if (pwd != NULL) {
                *ret_pwd = pwd;
                if (account_name != NULL) {
-                       *account_name = global_myname();
+                       *account_name = lp_netbios_name();
                }
 
                return true;
@@ -2388,7 +2403,7 @@ bool get_trust_pw_hash(const char *domain, uint8_t ret_pwd[16],
                                                        channel))
        {
                if (account_name != NULL) {
-                       *account_name = global_myname();
+                       *account_name = lp_netbios_name();
                }
 
                return true;