s3-talloc Change TALLOC_ZERO_P() to talloc_zero()
[nivanova/samba-autobuild/.git] / source3 / passdb / pdb_smbpasswd.c
index 6cf54fbdf66bda3206111b29bd8dd7a97e4e7fc1..2cfacd3a13628d7f81268fef80f9ca3b26481243 100644 (file)
  */
 
 #include "includes.h"
+#include "passdb.h"
+#include "system/passwd.h"
+#include "system/filesys.h"
+#include "../librpc/gen_ndr/samr.h"
+#include "../libcli/security/security.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_PASSDB
@@ -30,7 +35,7 @@
    else.  However, smb_passwd is limited to the information
    stored by an smbpasswd entry 
  */
+
 struct smb_passwd
 {
         uint32 smb_userid;        /* this is actually the unix uid_t */
@@ -39,7 +44,7 @@ struct smb_passwd
         const unsigned char *smb_passwd;    /* Null if no password */
         const unsigned char *smb_nt_passwd; /* Null if no password */
 
-        uint16 acct_ctrl;             /* account info (ACB_xxxx bit-mask) */
+        uint16_t acct_ctrl;             /* account info (ACB_xxxx bit-mask) */
         time_t pass_last_set_time;    /* password last set time */
 };
 
@@ -47,10 +52,10 @@ struct smbpasswd_privates
 {
        /* used for maintain locks on the smbpasswd file */
        int     pw_file_lock_depth;
-       
+
        /* Global File pointer */
        FILE    *pw_file;
-       
+
        /* formerly static variables */
        struct smb_passwd pw_buf;
        fstring user_name;
@@ -69,7 +74,7 @@ static SIG_ATOMIC_T gotalarm;
  Signal function to tell us we timed out.
 ****************************************************************/
 
-static void gotalarm_sig(void)
+static void gotalarm_sig(int signum)
 {
        gotalarm = 1;
 }
@@ -86,7 +91,7 @@ static bool do_file_lock(int fd, int waitsecs, int type)
        void (*oldsig_handler)(int);
 
        gotalarm = 0;
-       oldsig_handler = CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
+       oldsig_handler = CatchSignal(SIGALRM, gotalarm_sig);
 
        lock.l_type = type;
        lock.l_whence = SEEK_SET;
@@ -98,9 +103,9 @@ static bool do_file_lock(int fd, int waitsecs, int type)
        /* Note we must *NOT* use sys_fcntl here ! JRA */
        ret = fcntl(fd, SMB_F_SETLKW, &lock);
        alarm(0);
-       CatchSignal(SIGALRM, SIGNAL_CAST oldsig_handler);
+       CatchSignal(SIGALRM, oldsig_handler);
 
-       if (gotalarm) {
+       if (gotalarm && ret == -1) {
                DEBUG(0, ("do_file_lock: failed to %s file.\n",
                        type == F_UNLCK ? "unlock" : "lock"));
                return False;
@@ -168,7 +173,7 @@ static void pdb_init_smb(struct smb_passwd *user)
        if (user == NULL) 
                return;
        ZERO_STRUCTP (user);
-       
+
        user->pass_last_set_time = (time_t)0;
 }
 
@@ -223,8 +228,11 @@ creating file %s\n", pfile));
                                lock_type = F_WRLCK;
                                break;
                        }
+               default:
+                       DEBUG(10, ("Invalid open mode: %d\n", type));
+                       return NULL;
        }
-                      
+
        for(race_loop = 0; race_loop < 5; race_loop++) {
                DEBUG(10, ("startsmbfilepwent_internal: opening file %s\n", pfile));
 
@@ -276,7 +284,7 @@ Error was %s\n", pfile, strerror(errno) ));
                         * prevent infinate loops. JRA.
                         */
 
-                       if (sys_stat(pfile,&sbuf1) != 0) {
+                       if (sys_stat(pfile, &sbuf1, false) != 0) {
                                DEBUG(0, ("startsmbfilepwent_internal: unable to stat file %s. \
 Error was %s\n", pfile, strerror(errno)));
                                pw_file_unlock(fileno(fp), lock_depth);
@@ -284,7 +292,7 @@ Error was %s\n", pfile, strerror(errno)));
                                return NULL;
                        }
 
-                       if (sys_fstat(fileno(fp),&sbuf2) != 0) {
+                       if (sys_fstat(fileno(fp), &sbuf2, false) != 0) {
                                DEBUG(0, ("startsmbfilepwent_internal: unable to fstat file %s. \
 Error was %s\n", pfile, strerror(errno)));
                                pw_file_unlock(fileno(fp), lock_depth);
@@ -292,7 +300,7 @@ Error was %s\n", pfile, strerror(errno)));
                                return NULL;
                        }
 
-                       if( sbuf1.st_ino == sbuf2.st_ino) {
+                       if( sbuf1.st_ex_ino == sbuf2.st_ex_ino) {
                                /* No race. */
                                break;
                        }
@@ -541,7 +549,7 @@ static struct smb_passwd *getsmbfilepwent(struct smbpasswd_privates *smbpasswd_s
                        }
                        if(*p == ':') {
                                p++;
-                               if(*p && (StrnCaseCmp((char *)p, "LCT-", 4)==0)) {
+                               if(*p && (strncasecmp_m((char *)p, "LCT-", 4)==0)) {
                                        int i;
                                        p += 4;
                                        for(i = 0; i < 8; i++) {
@@ -614,7 +622,7 @@ static char *format_new_smbpasswd_entry(const struct smb_passwd *newpwd)
        /* Add the account encoding and the last change time. */
        slprintf((char *)p, new_entry_length - 1 - (p - new_entry),  "%s:LCT-%08X:\n",
                pdb_encode_acct_ctrl(newpwd->acct_ctrl, NEW_PW_FORMAT_SPACE_PADDED_LEN),
-               (uint32)newpwd->pass_last_set_time);
+               (uint32_t)newpwd->pass_last_set_time);
 
        return new_entry;
 }
@@ -967,7 +975,7 @@ This is no longer supported.!\n", pwd->smb_name));
                        p++;
 
                        /* We should be pointing at the LCT entry. */
-                       if((linebuf_len > (PTR_DIFF(p, linebuf) + 13)) && (StrnCaseCmp((char *)p, "LCT-", 4) == 0)) {
+                       if((linebuf_len > (PTR_DIFF(p, linebuf) + 13)) && (strncasecmp_m((char *)p, "LCT-", 4) == 0)) {
                                p += 4;
                                for(i = 0; i < 8; i++) {
                                        if(p[i] == '\0' || !isxdigit(p[i])) {
@@ -982,7 +990,7 @@ This is no longer supported.!\n", pwd->smb_name));
                                         */
                                        got_pass_last_set_time = True;
                                } /* i == 8 */
-                       } /* *p && StrnCaseCmp() */
+                       } /* *p && strncasecmp_m() */
                } /* p == ':' */
        } /* p == '[' */
 
@@ -1003,7 +1011,7 @@ This is no longer supported.!\n", pwd->smb_name));
                slprintf(&ascii_p16[strlen(ascii_p16)], 
                        sizeof(ascii_p16)-(strlen(ascii_p16)+1),
                        "%s:LCT-%08X:", 
-                       encode_bits, (uint32)pwd->pass_last_set_time );
+                       encode_bits, (uint32_t)pwd->pass_last_set_time );
                wr_len = strlen(ascii_p16);
        }
 
@@ -1185,7 +1193,7 @@ Error was %s\n", pwd->smb_name, pfile2, strerror(errno)));
 
 static bool build_smb_pass (struct smb_passwd *smb_pw, const struct samu *sampass)
 {
-       uint32 rid;
+       uint32_t rid;
 
        if (sampass == NULL) 
                return False;
@@ -1193,12 +1201,12 @@ static bool build_smb_pass (struct smb_passwd *smb_pw, const struct samu *sampas
 
        if (!IS_SAM_DEFAULT(sampass, PDB_USERSID)) {
                rid = pdb_get_user_rid(sampass);
-               
+
                /* If the user specified a RID, make sure its able to be both stored and retreived */
-               if (rid == DOMAIN_USER_RID_GUEST) {
-                       struct passwd *passwd = getpwnam_alloc(NULL, lp_guestaccount());
+               if (rid == DOMAIN_RID_GUEST) {
+                       struct passwd *passwd = Get_Pwnam_alloc(NULL, lp_guestaccount());
                        if (!passwd) {
-                               DEBUG(0, ("Could not find guest account via getpwnam()! (%s)\n", lp_guestaccount()));
+                               DEBUG(0, ("Could not find guest account via Get_Pwnam_alloc()! (%s)\n", lp_guestaccount()));
                                return False;
                        }
                        smb_pw->smb_userid=passwd->pw_uid;
@@ -1230,7 +1238,7 @@ static bool build_sam_account(struct smbpasswd_privates *smbpasswd_state,
                              struct samu *sam_pass, const struct smb_passwd *pw_buf)
 {
        struct passwd *pwfile;
-       
+
        if ( !sam_pass ) {
                DEBUG(5,("build_sam_account: struct samu is NULL\n"));
                return False;
@@ -1243,20 +1251,22 @@ static bool build_sam_account(struct smbpasswd_privates *smbpasswd_state,
                "%u is not in unix passwd database!\n", pw_buf->smb_name, pw_buf->smb_userid));
                        return False;
        }
-       
+
        if ( !NT_STATUS_IS_OK( samu_set_unix(sam_pass, pwfile )) )
                return False;
-               
+
        TALLOC_FREE(pwfile);
 
        /* set remaining fields */
-               
-       pdb_set_nt_passwd (sam_pass, pw_buf->smb_nt_passwd, PDB_SET);
-       pdb_set_lanman_passwd (sam_pass, pw_buf->smb_passwd, PDB_SET);                  
+
+       if (!pdb_set_nt_passwd (sam_pass, pw_buf->smb_nt_passwd, PDB_SET))
+               return False;
+       if (!pdb_set_lanman_passwd (sam_pass, pw_buf->smb_passwd, PDB_SET))
+               return False;
        pdb_set_acct_ctrl (sam_pass, pw_buf->acct_ctrl, PDB_SET);
        pdb_set_pass_last_set_time (sam_pass, pw_buf->pass_last_set_time, PDB_SET);
        pdb_set_pass_can_change_time (sam_pass, pw_buf->pass_last_set_time, PDB_SET);
-       
+
        return True;
 }
 
@@ -1292,21 +1302,21 @@ static NTSTATUS smbpasswd_getsampwnam(struct pdb_methods *my_methods,
 
        while ( ((smb_pw=getsmbfilepwent(smbpasswd_state, fp)) != NULL)&& (!strequal(smb_pw->smb_name, username)) )
                /* do nothing....another loop */ ;
-       
+
        endsmbfilepwent(fp, &(smbpasswd_state->pw_file_lock_depth));
 
 
        /* did we locate the username in smbpasswd  */
        if (smb_pw == NULL)
                return nt_status;
-       
+
        DEBUG(10, ("getsampwnam (smbpasswd): found by name: %s\n", smb_pw->smb_name));
 
        if (!sam_acct) {
                DEBUG(10,("getsampwnam (smbpasswd): struct samu is NULL\n"));
                return nt_status;
        }
-               
+
        /* now build the struct samu */
        if (!build_sam_account(smbpasswd_state, sam_acct, smb_pw))
                return nt_status;
@@ -1315,14 +1325,14 @@ static NTSTATUS smbpasswd_getsampwnam(struct pdb_methods *my_methods,
        return NT_STATUS_OK;
 }
 
-static NTSTATUS smbpasswd_getsampwsid(struct pdb_methods *my_methods, struct samu *sam_acct, const DOM_SID *sid)
+static NTSTATUS smbpasswd_getsampwsid(struct pdb_methods *my_methods, struct samu *sam_acct, const struct dom_sid *sid)
 {
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        struct smbpasswd_privates *smbpasswd_state = (struct smbpasswd_privates*)my_methods->private_data;
        struct smb_passwd *smb_pw;
        FILE *fp = NULL;
-       uint32 rid;
-       
+       uint32_t rid;
+
        DEBUG(10, ("smbpasswd_getsampwrid: search by sid: %s\n",
                   sid_string_dbg(sid)));
 
@@ -1330,7 +1340,7 @@ static NTSTATUS smbpasswd_getsampwsid(struct pdb_methods *my_methods, struct sam
                return NT_STATUS_UNSUCCESSFUL;
 
        /* More special case 'guest account' hacks... */
-       if (rid == DOMAIN_USER_RID_GUEST) {
+       if (rid == DOMAIN_RID_GUEST) {
                const char *guest_account = lp_guestaccount();
                if (!(guest_account && *guest_account)) {
                        DEBUG(1, ("Guest account not specfied!\n"));
@@ -1356,9 +1366,9 @@ static NTSTATUS smbpasswd_getsampwsid(struct pdb_methods *my_methods, struct sam
        /* did we locate the username in smbpasswd  */
        if (smb_pw == NULL)
                return nt_status;
-       
+
        DEBUG(10, ("getsampwrid (smbpasswd): found by name: %s\n", smb_pw->smb_name));
-               
+
        if (!sam_acct) {
                DEBUG(10,("getsampwrid: (smbpasswd) struct samu is NULL\n"));
                return nt_status;
@@ -1369,7 +1379,7 @@ static NTSTATUS smbpasswd_getsampwsid(struct pdb_methods *my_methods, struct sam
                return nt_status;
 
        /* build_sam_account might change the SID on us, if the name was for the guest account */
-       if (NT_STATUS_IS_OK(nt_status) && !sid_equal(pdb_get_user_sid(sam_acct), sid)) {
+       if (NT_STATUS_IS_OK(nt_status) && !dom_sid_equal(pdb_get_user_sid(sam_acct), sid)) {
                DEBUG(1, ("looking for user with sid %s instead returned %s "
                          "for account %s!?!\n", sid_string_dbg(sid),
                          sid_string_dbg(pdb_get_user_sid(sam_acct)),
@@ -1385,12 +1395,12 @@ static NTSTATUS smbpasswd_add_sam_account(struct pdb_methods *my_methods, struct
 {
        struct smbpasswd_privates *smbpasswd_state = (struct smbpasswd_privates*)my_methods->private_data;
        struct smb_passwd smb_pw;
-       
+
        /* convert the struct samu */
        if (!build_smb_pass(&smb_pw, sampass)) {
                return NT_STATUS_UNSUCCESSFUL;
        }
-       
+
        /* add the entry */
        return add_smbfilepwd_entry(smbpasswd_state, &smb_pw);
 }
@@ -1399,19 +1409,19 @@ static NTSTATUS smbpasswd_update_sam_account(struct pdb_methods *my_methods, str
 {
        struct smbpasswd_privates *smbpasswd_state = (struct smbpasswd_privates*)my_methods->private_data;
        struct smb_passwd smb_pw;
-       
+
        /* convert the struct samu */
        if (!build_smb_pass(&smb_pw, sampass)) {
                DEBUG(0, ("smbpasswd_update_sam_account: build_smb_pass failed!\n"));
                return NT_STATUS_UNSUCCESSFUL;
        }
-       
+
        /* update the entry */
        if(!mod_smbfilepwd_entry(smbpasswd_state, &smb_pw)) {
                DEBUG(0, ("smbpasswd_update_sam_account: mod_smbfilepwd_entry failed!\n"));
                return NT_STATUS_UNSUCCESSFUL;
        }
-       
+
        return NT_STATUS_OK;
 }
 
@@ -1443,7 +1453,7 @@ static NTSTATUS smbpasswd_rename_sam_account (struct pdb_methods *my_methods,
        if ( !(new_acct = samu_new( NULL )) ) {
                return NT_STATUS_NO_MEMORY;
        }
-       
+
        if ( !pdb_copy_sam_account( new_acct, old_acct ) 
                || !pdb_set_username(new_acct, newname, PDB_CHANGED)) 
        {
@@ -1514,21 +1524,21 @@ done:
 
        if (new_acct)
                TALLOC_FREE(new_acct);
-       
+
        return (ret);   
 }
 
-static bool smbpasswd_rid_algorithm(struct pdb_methods *methods)
+static uint32_t smbpasswd_capabilities(struct pdb_methods *methods)
 {
-       return True;
+       return 0;
 }
 
 static void free_private_data(void **vp) 
 {
        struct smbpasswd_privates **privates = (struct smbpasswd_privates**)vp;
-       
+
        endsmbfilepwent((*privates)->pw_file, &((*privates)->pw_file_lock_depth));
-       
+
        *privates = NULL;
        /* No need to free any further, as it is talloc()ed */
 }
@@ -1559,8 +1569,24 @@ static bool smbpasswd_search_next_entry(struct pdb_search *search,
                return false;
        }
 
-       *entry = state->entries[state->current++];
+       entry->idx = state->entries[state->current].idx;
+       entry->rid = state->entries[state->current].rid;
+       entry->acct_flags = state->entries[state->current].acct_flags;
+
+       entry->account_name = talloc_strdup(
+               search, state->entries[state->current].account_name);
+       entry->fullname = talloc_strdup(
+               search, state->entries[state->current].fullname);
+       entry->description = talloc_strdup(
+               search, state->entries[state->current].description);
 
+       if ((entry->account_name == NULL) || (entry->fullname == NULL)
+           || (entry->description == NULL)) {
+               DEBUG(0, ("talloc_strdup failed\n"));
+               return false;
+       }
+
+       state->current += 1;
        return true;
 }
 
@@ -1575,8 +1601,7 @@ static bool smbpasswd_search_users(struct pdb_methods *methods,
        struct smb_passwd *pwd;
        FILE *fp;
 
-       search_state = TALLOC_ZERO_P(search->mem_ctx,
-                                    struct smbpasswd_search_state);
+       search_state = talloc_zero(search, struct smbpasswd_search_state);
        if (search_state == NULL) {
                DEBUG(0, ("talloc failed\n"));
                return false;
@@ -1665,11 +1690,11 @@ static NTSTATUS pdb_init_smbpasswd( struct pdb_methods **pdb_method, const char
        (*pdb_method)->rename_sam_account = smbpasswd_rename_sam_account;
        (*pdb_method)->search_users = smbpasswd_search_users;
 
-       (*pdb_method)->rid_algorithm = smbpasswd_rid_algorithm;
+       (*pdb_method)->capabilities = smbpasswd_capabilities;
 
        /* Setup private data and free function */
 
-       if ( !(privates = TALLOC_ZERO_P( *pdb_method, struct smbpasswd_privates )) ) {
+       if ( !(privates = talloc_zero( *pdb_method, struct smbpasswd_privates )) ) {
                DEBUG(0, ("talloc() failed for smbpasswd private_data!\n"));
                return NT_STATUS_NO_MEMORY;
        }
@@ -1681,7 +1706,7 @@ static NTSTATUS pdb_init_smbpasswd( struct pdb_methods **pdb_method, const char
        } else {
                privates->smbpasswd_file = talloc_strdup(*pdb_method, lp_smb_passwd_file());
        }
-       
+
        if (!privates->smbpasswd_file) {
                DEBUG(0, ("talloc_strdp() failed for storing smbpasswd location!\n"));
                return NT_STATUS_NO_MEMORY;