Fix up an embarrsing bug I introduced when I moved the id21/id23 -> SAM_ACCOUNT
[ira/wip.git] / source / passdb / passdb.c
index 99eb872b5eb5c6374ce74c51337d74ec1403b14a..c014c3221f7c9e4bf8ed160d64c9a6442ea99970 100644 (file)
@@ -1,10 +1,11 @@
 /* 
    Unix SMB/Netbios implementation.
-   Version 1.9.
+   Version 3.0
    Password and authentication handling
    Copyright (C) Jeremy Allison                1996-2001
    Copyright (C) Luke Kenneth Casson Leighton  1996-1998
    Copyright (C) Gerald (Jerry) Carter         2000-2001
+   Copyright (C) Andrew Bartlett               2001-2002
       
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -57,14 +58,9 @@ BOOL initialize_password_db(BOOL reload)
  Fill the SAM_ACCOUNT with default values.
  ***********************************************************/
 
-static BOOL pdb_fill_default_sam(SAM_ACCOUNT *user)
+static void pdb_fill_default_sam(SAM_ACCOUNT *user)
 {
-       if (user == NULL) {
-               DEBUG(0,("pdb_fill_default_sam: SAM_ACCOUNT was NULL\n"));
-               return False;
-       }
-       
-       ZERO_STRUCTP(user);
+       ZERO_STRUCT(user->private); /* Don't touch the talloc context */
 
         /* Don't change these timestamp settings without a good reason.
            They are important for NT member server compatibility. */
@@ -84,34 +80,92 @@ static BOOL pdb_fill_default_sam(SAM_ACCOUNT *user)
        memset(user->private.hours, 0xff, user->private.hours_len); /* available at all hours */
        user->private.unknown_5 = 0x00000000; /* don't know */
        user->private.unknown_6 = 0x000004ec; /* don't know */
-       return True;
+
+       /* Some parts of samba strlen their pdb_get...() returns, 
+          so this keeps the interface unchanged for now. */
+          
+       user->private.username = "";
+       user->private.domain = "";
+       user->private.nt_username = "";
+       user->private.full_name = "";
+       user->private.home_dir = "";
+       user->private.logon_script = "";
+       user->private.profile_path = "";
+       user->private.acct_desc = "";
+       user->private.workstations = "";
+       user->private.unknown_str = "";
+       user->private.munged_dial = "";
 }      
 
+static void destroy_pdb_talloc(SAM_ACCOUNT **user) 
+{
+       if (*user) {
+               talloc_destroy((*user)->mem_ctx);
+               *user = NULL;
+       }
+}
 
-/*************************************************************
- Alloc memory and initialises a struct sam_passwd.
- ************************************************************/
 
-BOOL pdb_init_sam(SAM_ACCOUNT **user)
+/**********************************************************************
+ Alloc memory and initialises a struct sam_passwd on supplied mem_ctx.
+***********************************************************************/
+
+NTSTATUS pdb_init_sam_talloc(TALLOC_CTX *mem_ctx, SAM_ACCOUNT **user)
 {
        if (*user != NULL) {
                DEBUG(0,("pdb_init_sam: SAM_ACCOUNT was non NULL\n"));
 #if 0
                smb_panic("NULL pointer passed to pdb_init_sam\n");
 #endif
-               return False;
+               return NT_STATUS_UNSUCCESSFUL;
        }
-       
-       *user=(SAM_ACCOUNT *)malloc(sizeof(SAM_ACCOUNT));
+
+       if (!mem_ctx) {
+               DEBUG(0,("pdb_init_sam_talloc: mem_ctx was NULL!\n"));
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+
+       *user=(SAM_ACCOUNT *)talloc(mem_ctx, sizeof(SAM_ACCOUNT));
 
        if (*user==NULL) {
                DEBUG(0,("pdb_init_sam: error while allocating memory\n"));
-               return False;
+               return NT_STATUS_NO_MEMORY;
        }
 
+       (*user)->mem_ctx = mem_ctx;
+
+       (*user)->free_fn = NULL;
+
        pdb_fill_default_sam(*user);
+       
+       return NT_STATUS_OK;
+}
 
-       return True;
+
+/*************************************************************
+ Alloc memory and initialises a struct sam_passwd.
+ ************************************************************/
+
+NTSTATUS pdb_init_sam(SAM_ACCOUNT **user)
+{
+       TALLOC_CTX *mem_ctx;
+       NTSTATUS nt_status;
+       
+       mem_ctx = talloc_init_named("passdb internal SAM_ACCOUNT allocation");
+
+       if (!mem_ctx) {
+               DEBUG(0,("pdb_init_sam: error while doing talloc_init()\n"));
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(mem_ctx, user))) {
+               talloc_destroy(mem_ctx);
+               return nt_status;
+       }
+       
+       (*user)->free_fn = destroy_pdb_talloc;
+
+       return NT_STATUS_OK;
 }
 
 
@@ -119,20 +173,21 @@ BOOL pdb_init_sam(SAM_ACCOUNT **user)
  Initialises a struct sam_passwd with sane values.
  ************************************************************/
 
-BOOL pdb_init_sam_pw(SAM_ACCOUNT **new_sam_acct, const struct passwd *pwd)
+NTSTATUS pdb_init_sam_pw(SAM_ACCOUNT **new_sam_acct, const struct passwd *pwd)
 {
        pstring str;
        GROUP_MAP map;
        uint32 rid;
+       NTSTATUS nt_status;
 
        if (!pwd) {
                new_sam_acct = NULL;
-               return False;
+               return NT_STATUS_UNSUCCESSFUL;
        }
 
-       if (!pdb_init_sam(new_sam_acct)) {
+       if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(new_sam_acct))) {
                new_sam_acct = NULL;
-               return False;
+               return nt_status;
        }
 
        pdb_set_username(*new_sam_acct, pwd->pw_name);
@@ -169,7 +224,7 @@ BOOL pdb_init_sam_pw(SAM_ACCOUNT **new_sam_acct, const struct passwd *pwd)
        standard_sub_advanced(-1, pwd->pw_name, "", pwd->pw_gid, pwd->pw_name, str);
        pdb_set_logon_script(*new_sam_acct, str, False);
        
-       return True;
+       return NT_STATUS_OK;
 }
 
 
@@ -181,23 +236,13 @@ BOOL pdb_init_sam_pw(SAM_ACCOUNT **new_sam_acct, const struct passwd *pwd)
  * @param user SAM_ACCOUNT to free members of.
  **/
 
-static BOOL pdb_free_sam_contents(SAM_ACCOUNT *user)
+static void pdb_free_sam_contents(SAM_ACCOUNT *user)
 {
-       if (user == NULL) {
-               DEBUG(0,("pdb_free_sam_contents: SAM_ACCOUNT was NULL\n"));
-#if 0
-               smb_panic("NULL pointer passed to pdb_free_sam_contents\n");
-#endif
-               return False;
-       }
-
        /* As we start mallocing more strings this is where  
           we should free them. */
 
        data_blob_clear_free(&(user->private.lm_pw));
        data_blob_clear_free(&(user->private.nt_pw));
-
-       return True;    
 }
 
 
@@ -205,25 +250,21 @@ static BOOL pdb_free_sam_contents(SAM_ACCOUNT *user)
  Reset the SAM_ACCOUNT and free the NT/LM hashes.
  ***********************************************************/
 
-BOOL pdb_reset_sam(SAM_ACCOUNT *user)
+NTSTATUS pdb_reset_sam(SAM_ACCOUNT *user)
 {
        if (user == NULL) {
                DEBUG(0,("pdb_reset_sam: SAM_ACCOUNT was NULL\n"));
 #if 0
                smb_panic("NULL pointer passed to pdb_free_sam\n");
 #endif
-               return False;
+               return NT_STATUS_UNSUCCESSFUL;
        }
        
-       if (!pdb_free_sam_contents(user)) {
-               return False;
-       }
+       pdb_free_sam_contents(user);
 
-       if (!pdb_fill_default_sam(user)) {
-               return False;
-       }
+       pdb_fill_default_sam(user);
 
-       return True;
+       return NT_STATUS_OK;
 }
 
 
@@ -231,23 +272,23 @@ BOOL pdb_reset_sam(SAM_ACCOUNT *user)
  Free the SAM_ACCOUNT and the member pointers.
  ***********************************************************/
 
-BOOL pdb_free_sam(SAM_ACCOUNT **user)
+NTSTATUS pdb_free_sam(SAM_ACCOUNT **user)
 {
        if (*user == NULL) {
                DEBUG(0,("pdb_free_sam: SAM_ACCOUNT was NULL\n"));
 #if 0
                smb_panic("NULL pointer passed to pdb_free_sam\n");
 #endif
-               return False;
+               return NT_STATUS_UNSUCCESSFUL;
        }
 
-       if (!pdb_free_sam_contents(*user)) {
-               return False;
+       pdb_free_sam_contents(*user);
+       
+       if ((*user)->free_fn) {
+               (*user)->free_fn(user);
        }
 
-       SAFE_FREE(*user);
-       
-       return True;    
+       return NT_STATUS_OK;    
 }
 
 
@@ -729,12 +770,14 @@ BOOL local_sid_to_uid(uid_t *puid, DOM_SID *psid, enum SID_NAME_USE *name_type)
        /*
         * Ensure this uid really does exist.
         */
-       if(!(pass = sys_getpwuid(*puid)))
+       if(!(pass = getpwuid_alloc(*puid)))
                return False;
 
        DEBUG(10,("local_sid_to_uid: SID %s -> uid (%u) (%s).\n", sid_to_string( str, psid),
                (unsigned int)*puid, pass->pw_name ));
 
+       passwd_free(&pass);
+
        *name_type = SID_NAME_USER;
 
        return True;
@@ -853,16 +896,26 @@ void copy_id23_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_23 *from)
        pdb_set_pass_can_change_time(to, nt_time_to_unix(&from->pass_can_change_time));
        pdb_set_pass_must_change_time(to, nt_time_to_unix(&from->pass_must_change_time));
 
-       pdb_set_username(to      , pdb_convert(&from->uni_user_name   ));
-       pdb_set_fullname(to      , pdb_convert(&from->uni_full_name   ));
-       pdb_set_homedir(to       , pdb_convert(&from->uni_home_dir    ), True);
-       pdb_set_dir_drive(to     , pdb_convert(&from->uni_dir_drive   ), True);
-       pdb_set_logon_script(to  , pdb_convert(&from->uni_logon_script), True);
-       pdb_set_profile_path(to  , pdb_convert(&from->uni_profile_path), True);
-       pdb_set_acct_desc(to     , pdb_convert(&from->uni_acct_desc   ));
-       pdb_set_workstations(to  , pdb_convert(&from->uni_workstations));
-       pdb_set_unknown_str(to   , pdb_convert(&from->uni_unknown_str ));
-       pdb_set_munged_dial(to   , pdb_convert(&from->uni_munged_dial ));
+       if (from->uni_user_name.buffer)
+               pdb_set_username(to      , pdb_convert(&from->uni_user_name   ));
+       if (from->uni_full_name.buffer)
+               pdb_set_fullname(to      , pdb_convert(&from->uni_full_name   ));
+       if (from->uni_home_dir.buffer)
+               pdb_set_homedir(to       , pdb_convert(&from->uni_home_dir    ), True);
+       if (from->uni_dir_drive.buffer)
+               pdb_set_dir_drive(to     , pdb_convert(&from->uni_dir_drive   ), True);
+       if (from->uni_logon_script.buffer)
+               pdb_set_logon_script(to  , pdb_convert(&from->uni_logon_script), True);
+       if (from->uni_profile_path.buffer)
+               pdb_set_profile_path(to  , pdb_convert(&from->uni_profile_path), True);
+       if (from->uni_acct_desc.buffer)
+               pdb_set_acct_desc(to     , pdb_convert(&from->uni_acct_desc   ));
+       if (from->uni_workstations.buffer)
+               pdb_set_workstations(to  , pdb_convert(&from->uni_workstations));
+       if (from->uni_unknown_str.buffer)
+               pdb_set_unknown_str(to   , pdb_convert(&from->uni_unknown_str ));
+       if (from->uni_munged_dial.buffer)
+               pdb_set_munged_dial(to   , pdb_convert(&from->uni_munged_dial ));
 
        if (from->user_rid)
                pdb_set_user_rid(to, from->user_rid);
@@ -897,16 +950,26 @@ void copy_id21_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_21 *from)
        pdb_set_pass_can_change_time(to, nt_time_to_unix(&from->pass_can_change_time));
        pdb_set_pass_must_change_time(to, nt_time_to_unix(&from->pass_must_change_time));
 
-       pdb_set_username(to      , pdb_convert(&from->uni_user_name   ));
-       pdb_set_fullname(to      , pdb_convert(&from->uni_full_name   ));
-       pdb_set_homedir(to       , pdb_convert(&from->uni_home_dir    ), True);
-       pdb_set_dir_drive(to     , pdb_convert(&from->uni_dir_drive   ), True);
-       pdb_set_logon_script(to  , pdb_convert(&from->uni_logon_script), True);
-       pdb_set_profile_path(to  , pdb_convert(&from->uni_profile_path), True);
-       pdb_set_acct_desc(to     , pdb_convert(&from->uni_acct_desc   ));
-       pdb_set_workstations(to  , pdb_convert(&from->uni_workstations));
-       pdb_set_unknown_str(to   , pdb_convert(&from->uni_unknown_str ));
-       pdb_set_munged_dial(to   , pdb_convert(&from->uni_munged_dial ));
+       if (from->uni_user_name.buffer)
+               pdb_set_username(to      , pdb_convert(&from->uni_user_name   ));
+       if (from->uni_full_name.buffer)
+               pdb_set_fullname(to      , pdb_convert(&from->uni_full_name   ));
+       if (from->uni_home_dir.buffer)
+               pdb_set_homedir(to       , pdb_convert(&from->uni_home_dir    ), True);
+       if (from->uni_dir_drive.buffer)
+               pdb_set_dir_drive(to     , pdb_convert(&from->uni_dir_drive   ), True);
+       if (from->uni_logon_script.buffer)
+               pdb_set_logon_script(to  , pdb_convert(&from->uni_logon_script), True);
+       if (from->uni_profile_path.buffer)
+               pdb_set_profile_path(to  , pdb_convert(&from->uni_profile_path), True);
+       if (from->uni_acct_desc.buffer)
+               pdb_set_acct_desc(to     , pdb_convert(&from->uni_acct_desc   ));
+       if (from->uni_workstations.buffer)
+               pdb_set_workstations(to  , pdb_convert(&from->uni_workstations));
+       if (from->uni_unknown_str.buffer)
+               pdb_set_unknown_str(to   , pdb_convert(&from->uni_unknown_str ));
+       if (from->uni_munged_dial.buffer)
+               pdb_set_munged_dial(to   , pdb_convert(&from->uni_munged_dial ));
 
        if (from->user_rid)
                pdb_set_user_rid(to, from->user_rid);
@@ -962,7 +1025,7 @@ BOOL local_password_change(const char *user_name, int local_flags,
                         * Check for a local account - if we're adding only.
                         */
                        
-                       if(!(pwd = sys_getpwnam(user_name))) {
+                       if(!(pwd = getpwnam_alloc(user_name))) {
                                slprintf(err_str, err_str_len - 1, "User %s does not \
 exist in system password file (usually /etc/passwd). Cannot add \
 account without a valid local system user.\n", user_name);
@@ -973,11 +1036,13 @@ account without a valid local system user.\n", user_name);
                        return False;
                }
 
-               if (!pdb_init_sam_pw(&sam_pass, pwd)) {
+               if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sam_pass, pwd))){
                        slprintf(err_str, err_str_len-1, "Failed initialise SAM_ACCOUNT for user %s.\n", user_name);
+                       passwd_free(&pwd);
                        return False;
                }
-
+               
+               passwd_free(&pwd);
        
                if (local_flags & LOCAL_TRUST_ACCOUNT) {
                        if (!pdb_set_acct_ctrl(sam_pass, ACB_WSTRUST)) {
@@ -1094,803 +1159,6 @@ account without a valid local system user.\n", user_name);
        return True;
 }
 
-/*********************************************************************
- Collection of get...() functions for SAM_ACCOUNT_INFO.
- ********************************************************************/
-
-uint16 pdb_get_acct_ctrl (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.acct_ctrl);
-       else
-               return (ACB_DISABLED);
-}
-
-time_t pdb_get_logon_time (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.logon_time);
-       else
-               return (0);
-}
-
-time_t pdb_get_logoff_time (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.logoff_time);
-       else
-               return (-1);
-}
-
-time_t pdb_get_kickoff_time (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.kickoff_time);
-       else
-               return (-1);
-}
-
-time_t pdb_get_pass_last_set_time (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.pass_last_set_time);
-       else
-               return (-1);
-}
-
-time_t pdb_get_pass_can_change_time (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.pass_can_change_time);
-       else
-               return (-1);
-}
-
-time_t pdb_get_pass_must_change_time (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.pass_must_change_time);
-       else
-               return (-1);
-}
-
-uint16 pdb_get_logon_divs (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.logon_divs);
-       else
-               return (-1);
-}
-
-uint32 pdb_get_hours_len (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.hours_len);
-       else
-               return (-1);
-}
-
-const uint8* pdb_get_hours (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.hours);
-       else
-               return (NULL);
-}
-
-const uint8* pdb_get_nt_passwd (const SAM_ACCOUNT *sampass)
-{
-       if (sampass) {
-               SMB_ASSERT((!sampass->private.nt_pw.data) 
-                          || sampass->private.nt_pw.length == NT_HASH_LEN);
-               return ((uint8*)sampass->private.nt_pw.data);
-       }
-       else
-               return (NULL);
-}
-
-const uint8* pdb_get_lanman_passwd (const SAM_ACCOUNT *sampass)
-{
-       if (sampass) {
-               SMB_ASSERT((!sampass->private.lm_pw.data) 
-                          || sampass->private.lm_pw.length == LM_HASH_LEN);
-               return ((uint8*)sampass->private.lm_pw.data);
-       }
-       else
-               return (NULL);
-}
-
-uint32 pdb_get_user_rid (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.user_rid);
-       else
-               return (-1);
-}
-
-uint32 pdb_get_group_rid (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.group_rid);
-       else
-               return (-1);
-}
-
-/**
- * Get flags showing what is initalised in the SAM_ACCOUNT
- * @param sampass the SAM_ACCOUNT in question
- * @return the flags indicating the members initialised in the struct.
- **/
-uint32 pdb_get_init_flag (SAM_ACCOUNT *sampass)
-{
-        if (sampass)
-               return sampass->private.init_flag;
-       else 
-                return FLAG_SAM_UNINIT;
-}
-
-uid_t pdb_get_uid (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.uid);
-       else
-               return (-1);
-}
-
-gid_t pdb_get_gid (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.gid);
-       else
-               return (-1);
-}
-
-const char* pdb_get_username (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.username);
-       else
-               return (NULL);
-}
-
-const char* pdb_get_domain (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.domain);
-       else
-               return (NULL);
-}
-
-const char* pdb_get_nt_username (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.nt_username);
-       else
-               return (NULL);
-}
-
-const char* pdb_get_fullname (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.full_name);
-       else
-               return (NULL);
-}
-
-const char* pdb_get_homedir (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.home_dir);
-       else
-               return (NULL);
-}
-
-const char* pdb_get_dirdrive (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.dir_drive);
-       else
-               return (NULL);
-}
-
-const char* pdb_get_logon_script (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.logon_script);
-       else
-               return (NULL);
-}
-
-const char* pdb_get_profile_path (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.profile_path);
-       else
-               return (NULL);
-}
-
-const char* pdb_get_acct_desc (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.acct_desc);
-       else
-               return (NULL);
-}
-
-const char* pdb_get_workstations (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.workstations);
-       else
-               return (NULL);
-}
-
-const char* pdb_get_unknown_str (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.unknown_str);
-       else
-               return (NULL);
-}
-
-const char* pdb_get_munged_dial (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.munged_dial);
-       else
-               return (NULL);
-}
-
-uint32 pdb_get_unknown3 (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.unknown_3);
-       else
-               return (-1);
-}
-
-uint32 pdb_get_unknown5 (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.unknown_5);
-       else
-               return (-1);
-}
-
-uint32 pdb_get_unknown6 (const SAM_ACCOUNT *sampass)
-{
-       if (sampass)
-               return (sampass->private.unknown_6);
-       else
-               return (-1);
-}
-
-/*********************************************************************
- Collection of set...() functions for SAM_ACCOUNT_INFO.
- ********************************************************************/
-
-BOOL pdb_set_acct_ctrl (SAM_ACCOUNT *sampass, uint16 flags)
-{
-       if (!sampass)
-               return False;
-               
-       if (sampass) {
-               sampass->private.acct_ctrl = flags;
-               return True;
-       }
-       
-       return False;
-}
-
-BOOL pdb_set_logon_time (SAM_ACCOUNT *sampass, time_t mytime)
-{
-       if (!sampass)
-               return False;
-
-       sampass->private.logon_time = mytime;
-       return True;
-}
-
-BOOL pdb_set_logoff_time (SAM_ACCOUNT *sampass, time_t mytime)
-{
-       if (!sampass)
-               return False;
-
-       sampass->private.logoff_time = mytime;
-       return True;
-}
-
-BOOL pdb_set_kickoff_time (SAM_ACCOUNT *sampass, time_t mytime)
-{
-       if (!sampass)
-               return False;
-
-       sampass->private.kickoff_time = mytime;
-       return True;
-}
-
-BOOL pdb_set_pass_can_change_time (SAM_ACCOUNT *sampass, time_t mytime)
-{
-       if (!sampass)
-               return False;
-
-       sampass->private.pass_can_change_time = mytime;
-       return True;
-}
-
-BOOL pdb_set_pass_must_change_time (SAM_ACCOUNT *sampass, time_t mytime)
-{
-       if (!sampass)
-               return False;
-
-       sampass->private.pass_must_change_time = mytime;
-       return True;
-}
-
-BOOL pdb_set_pass_last_set_time (SAM_ACCOUNT *sampass, time_t mytime)
-{
-       if (!sampass)
-               return False;
-
-       sampass->private.pass_last_set_time = mytime;
-       return True;
-}
-
-BOOL pdb_set_hours_len (SAM_ACCOUNT *sampass, uint32 len)
-{
-       if (!sampass)
-               return False;
-
-       sampass->private.hours_len = len;
-       return True;
-}
-
-BOOL pdb_set_logon_divs (SAM_ACCOUNT *sampass, uint16 hours)
-{
-       if (!sampass)
-               return False;
-
-       sampass->private.logon_divs = hours;
-       return True;
-}
-
-/**
- * Set flags showing what is initalised in the SAM_ACCOUNT
- * @param sampass the SAM_ACCOUNT in question
- * @param flag The *new* flag to be set.  Old flags preserved
- *             this flag is only added.  
- **/
-BOOL pdb_set_init_flag (SAM_ACCOUNT *sampass, uint32 flag)
-{
-        if (!sampass)
-                return False;
-
-        sampass->private.init_flag |= flag;
-
-        return True;
-}
-
-BOOL pdb_set_uid (SAM_ACCOUNT *sampass, const uid_t uid)
-{
-       if (!sampass)
-               return False;
-       
-       DEBUG(10, ("pdb_set_uid: setting uid %d, was %d\n", 
-                  (int)uid, (int)sampass->private.uid));
-       sampass->private.uid = uid;
-       pdb_set_init_flag(sampass, FLAG_SAM_UID); 
-
-       return True;
-
-}
-
-BOOL pdb_set_gid (SAM_ACCOUNT *sampass, const gid_t gid)
-{
-       if (!sampass)
-               return False;
-               
-       DEBUG(10, ("pdb_set_gid: setting gid %d, was %d\n", 
-                  (int)gid, (int)sampass->private.gid));
-       sampass->private.gid = gid; 
-       pdb_set_init_flag(sampass, FLAG_SAM_GID); 
-
-       return True;
-
-}
-
-BOOL pdb_set_user_rid (SAM_ACCOUNT *sampass, uint32 rid)
-{
-       if (!sampass)
-               return False;
-
-       DEBUG(10, ("pdb_set_rid: setting user rid %d, was %d\n", 
-                  rid, sampass->private.user_rid));
-       sampass->private.user_rid = rid;
-       return True;
-}
-
-BOOL pdb_set_group_rid (SAM_ACCOUNT *sampass, uint32 grid)
-{
-       if (!sampass)
-               return False;
-
-       DEBUG(10, ("pdb_set_group_rid: setting group rid %d, was %d\n", 
-                  grid, sampass->private.group_rid));
-       sampass->private.group_rid = grid;
-       return True;
-}
-
-/*********************************************************************
- Set the user's UNIX name.
- ********************************************************************/
-
-BOOL pdb_set_username(SAM_ACCOUNT *sampass, const char *username)
-{      
-       if (!sampass)
-               return False;
-       
-       *sampass->private.username = '\0';
-       DEBUG(10, ("pdb_set_username: setting username %s, was %s\n", 
-                  username, sampass->private.username));
-       if (!username)
-               return False;
-       StrnCpy (sampass->private.username, username, strlen(username));
-
-       return True;
-}
-
-/*********************************************************************
- Set the domain name.
- ********************************************************************/
-
-BOOL pdb_set_domain(SAM_ACCOUNT *sampass, const char *domain)
-{      
-       if (!sampass)
-               return False;
-       *sampass->private.domain = '\0';
-       if (!domain)
-               return False;
-
-       StrnCpy (sampass->private.domain, domain, strlen(domain));
-
-       return True;
-}
-
-/*********************************************************************
- Set the user's NT name.
- ********************************************************************/
-
-BOOL pdb_set_nt_username(SAM_ACCOUNT *sampass, const char *nt_username)
-{
-       if (!sampass)
-               return False;
-       *sampass->private.nt_username = '\0';
-       if (!nt_username)
-               return False;
-
-       StrnCpy (sampass->private.nt_username, nt_username, strlen(nt_username));
-
-       return True;
-}
-
-/*********************************************************************
- Set the user's full name.
- ********************************************************************/
-
-BOOL pdb_set_fullname(SAM_ACCOUNT *sampass, const char *fullname)
-{
-       if (!sampass)
-               return False;
-
-       DEBUG(10, ("pdb_set_fullname: setting full name %s, was %s\n", 
-                  fullname, sampass->private.full_name));
-       *sampass->private.full_name = '\0';
-       if (!fullname)
-               return False;
-
-       StrnCpy (sampass->private.full_name, fullname, strlen(fullname));
-
-       return True;
-}
-
-/*********************************************************************
- Set the user's logon script.
- ********************************************************************/
-
-BOOL pdb_set_logon_script(SAM_ACCOUNT *sampass, const char *logon_script, BOOL store)
-{
-       if (!sampass)
-               return False;
-
-       DEBUG(10, ("pdb_set_logon_script: setting logon script (store:%d) %s, was %s\n", 
-                  store, logon_script, sampass->private.logon_script));
-       *sampass->private.logon_script = '\0';
-       if (!logon_script)
-               return False;
-
-       StrnCpy (sampass->private.logon_script, logon_script, strlen(logon_script));
-
-       if (store)
-               pdb_set_init_flag(sampass, FLAG_SAM_LOGONSCRIPT); 
-
-       return True;
-}
-
-/*********************************************************************
- Set the user's profile path.
- ********************************************************************/
-
-BOOL pdb_set_profile_path (SAM_ACCOUNT *sampass, const char *profile_path, BOOL store)
-{
-       if (!sampass)
-               return False;
-
-       DEBUG(10, ("pdb_set_profile_path: setting profile path (store:%d) %s, was %s\n", 
-                  store, profile_path, sampass->private.profile_path));
-       *sampass->private.profile_path = '\0';
-       if (!profile_path)
-               return False;
-       
-       StrnCpy (sampass->private.profile_path, profile_path, strlen(profile_path));
-
-       if (store)
-               pdb_set_init_flag(sampass, FLAG_SAM_PROFILE);
-       
-       return True;
-}
-
-/*********************************************************************
- Set the user's directory drive.
- ********************************************************************/
-
-BOOL pdb_set_dir_drive (SAM_ACCOUNT *sampass, const char *dir_drive, BOOL store)
-{
-       if (!sampass)
-               return False;
-       *sampass->private.dir_drive = '\0';
-       if (!dir_drive)
-               return False;
-
-       StrnCpy (sampass->private.dir_drive, dir_drive, strlen(dir_drive));
-
-       if (store)
-               pdb_set_init_flag(sampass, FLAG_SAM_DRIVE);
-
-       return True;
-}
-
-/*********************************************************************
- Set the user's home directory.
- ********************************************************************/
-
-BOOL pdb_set_homedir (SAM_ACCOUNT *sampass, const char *homedir, BOOL store)
-{
-       if (!sampass)
-               return False;
-       *sampass->private.home_dir = '\0';
-       if (!homedir)
-               return False;
-       
-       StrnCpy (sampass->private.home_dir, homedir, strlen(homedir));
-
-       if (store)
-               pdb_set_init_flag(sampass, FLAG_SAM_SMBHOME);
-
-       return True;
-}
-
-/*********************************************************************
- Set the user's account description.
- ********************************************************************/
-
-BOOL pdb_set_acct_desc (SAM_ACCOUNT *sampass, const char *acct_desc)
-{
-       if (!sampass)
-               return False;
-       *sampass->private.acct_desc = '\0';
-       if (!acct_desc)
-               return False;
-       
-       StrnCpy (sampass->private.acct_desc, acct_desc, strlen(acct_desc));
-
-       return True;
-}
-
-/*********************************************************************
- Set the user's workstation allowed list.
- ********************************************************************/
-
-BOOL pdb_set_workstations (SAM_ACCOUNT *sampass, const char *workstations)
-{
-       if (!sampass)
-               return False;
-       *sampass->private.workstations = '\0';
-       if (!workstations)
-               return False;
-
-       StrnCpy (sampass->private.workstations, workstations, strlen(workstations));
-
-       return True;
-}
-
-/*********************************************************************
- Set the user's 'unknown_str', whatever the heck this actually is...
- ********************************************************************/
-
-BOOL pdb_set_unknown_str (SAM_ACCOUNT *sampass, const char *unknown_str)
-{
-       if (!sampass)
-               return False;
-       *sampass->private.unknown_str = '\0';
-       if (!unknown_str)
-               return False;
-
-       StrnCpy (sampass->private.unknown_str, unknown_str, strlen(unknown_str));
-
-       return True;
-}
-
-/*********************************************************************
- Set the user's dial string.
- ********************************************************************/
-
-BOOL pdb_set_munged_dial (SAM_ACCOUNT *sampass, const char *munged_dial)
-{
-       if (!sampass)
-               return False;
-       *sampass->private.munged_dial = '\0';
-       if (!munged_dial)
-               return False;
-
-       StrnCpy (sampass->private.munged_dial, munged_dial, strlen(munged_dial));
-
-       return True;
-}
-
-/*********************************************************************
- Set the user's NT hash.
- ********************************************************************/
-
-BOOL pdb_set_nt_passwd (SAM_ACCOUNT *sampass, const uint8 *pwd)
-{
-       if (!sampass)
-               return False;
-
-       data_blob_clear_free(&(sampass->private.nt_pw));
-       
-       sampass->private.nt_pw = data_blob(pwd, NT_HASH_LEN);
-
-       return True;
-}
-
-/*********************************************************************
- Set the user's LM hash.
- ********************************************************************/
-
-BOOL pdb_set_lanman_passwd (SAM_ACCOUNT *sampass, const uint8 *pwd)
-{
-       if (!sampass)
-               return False;
-
-       data_blob_clear_free(&(sampass->private.lm_pw));
-       
-       sampass->private.lm_pw = data_blob(pwd, LM_HASH_LEN);
-
-       return True;
-}
-
-BOOL pdb_set_unknown_3 (SAM_ACCOUNT *sampass, uint32 unkn)
-{
-       if (!sampass)
-               return False;
-
-       sampass->private.unknown_3 = unkn;
-       return True;
-}
-
-BOOL pdb_set_unknown_5 (SAM_ACCOUNT *sampass, uint32 unkn)
-{
-       if (!sampass)
-               return False;
-
-       sampass->private.unknown_5 = unkn;
-       return True;
-}
-
-BOOL pdb_set_unknown_6 (SAM_ACCOUNT *sampass, uint32 unkn)
-{
-       if (!sampass)
-               return False;
-
-       sampass->private.unknown_6 = unkn;
-       return True;
-}
-
-BOOL pdb_set_hours (SAM_ACCOUNT *sampass, const uint8 *hours)
-{
-       if (!sampass)
-               return False;
-
-       if (!hours) {
-               memset ((char *)sampass->private.hours, 0, MAX_HOURS_LEN);
-               return True;
-       }
-       
-       memcpy (sampass->private.hours, hours, MAX_HOURS_LEN);
-
-       return True;
-}
-
-
-/* Helpful interfaces to the above */
-
-/*********************************************************************
- Sets the last changed times and must change times for a normal
- password change.
- ********************************************************************/
-
-BOOL pdb_set_pass_changed_now (SAM_ACCOUNT *sampass)
-{
-       uint32 expire;
-
-       if (!sampass)
-               return False;
-       
-       if (!pdb_set_pass_last_set_time (sampass, time(NULL)))
-               return False;
-
-       account_policy_get(AP_MAX_PASSWORD_AGE, &expire);
-
-       if (expire==(uint32)-1) {
-               if (!pdb_set_pass_must_change_time (sampass, 0))
-                       return False;
-       } else {
-               if (!pdb_set_pass_must_change_time (sampass, 
-                                           pdb_get_pass_last_set_time(sampass)
-                                           + expire))
-                       return False;
-       }
-       
-       return True;
-}
-
-/*********************************************************************
- Set the user's PLAINTEXT password.  Used as an interface to the above.
- Also sets the last change time to NOW.
- ********************************************************************/
-
-BOOL pdb_set_plaintext_passwd (SAM_ACCOUNT *sampass, const char *plaintext)
-{
-       uchar new_lanman_p16[16];
-       uchar new_nt_p16[16];
-
-       if (!sampass || !plaintext)
-               return False;
-       
-       nt_lm_owf_gen (plaintext, new_nt_p16, new_lanman_p16);
-
-       if (!pdb_set_nt_passwd (sampass, new_nt_p16)) 
-               return False;
-
-       if (!pdb_set_lanman_passwd (sampass, new_lanman_p16)) 
-               return False;
-       
-       if (!pdb_set_pass_changed_now (sampass))
-               return False;
-
-       return True;
-}
-
 /***************************************************************************
  Search by uid.  Wrapper around pdb_getsampwnam()
  **************************************************************************/
@@ -1910,13 +1178,15 @@ BOOL pdb_getsampwuid (SAM_ACCOUNT* user, uid_t uid)
         * and then lokup the user by name in the sam.
         */
         
-       if ((pw=sys_getpwuid(uid)) == NULL)  {
+       if ((pw=getpwuid_alloc(uid)) == NULL)  {
                DEBUG(0,("pdb_getsampwuid: getpwuid(%d) return NULL. User does not exist in Unix accounts!\n", uid));
                return False;
        }
        
        fstrcpy (name, pw->pw_name);
 
+       passwd_free(&pw);
+
        return pdb_getsampwnam (user, name);
 
 }