samdb: Rework samdb_connect_url() to return LDB error code and an error string
[samba.git] / source3 / passdb / pdb_smbpasswd.c
index 9feb9bfe3a5454ca6d6c1878510238f25d7fa17b..9c381471ddae384328541af58f401568f0a2f9e6 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"
+#include "passdb/pdb_smbpasswd.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_PASSDB
@@ -36,7 +39,7 @@
 
 struct smb_passwd
 {
-        uint32 smb_userid;        /* this is actually the unix uid_t */
+        uint32_t smb_userid;      /* this is actually the unix uid_t */
         const char *smb_name;     /* username string */
 
         const unsigned char *smb_passwd;    /* Null if no password */
@@ -84,7 +87,7 @@ static void gotalarm_sig(int signum)
 
 static bool do_file_lock(int fd, int waitsecs, int type)
 {
-       SMB_STRUCT_FLOCK lock;
+       struct flock lock;
        int             ret;
        void (*oldsig_handler)(int);
 
@@ -99,7 +102,7 @@ static bool do_file_lock(int fd, int waitsecs, int type)
 
        alarm(waitsecs);
        /* Note we must *NOT* use sys_fcntl here ! JRA */
-       ret = fcntl(fd, SMB_F_SETLKW, &lock);
+       ret = fcntl(fd, F_SETLKW, &lock);
        alarm(0);
        CatchSignal(SIGALRM, oldsig_handler);
 
@@ -211,10 +214,10 @@ static FILE *startsmbfilepwent(const char *pfile, enum pwf_access_type type, int
                                int i, fd = -1;
 
                                for(i = 0; i < 5; i++) {
-                                       if((fd = sys_open(pfile, O_CREAT|O_TRUNC|O_EXCL|O_RDWR, 0600))!=-1) {
+                                       if((fd = open(pfile, O_CREAT|O_TRUNC|O_EXCL|O_RDWR, 0600))!=-1) {
                                                break;
                                        }
-                                       sys_usleep(200); /* Spin, spin... */
+                                       usleep(200); /* Spin, spin... */
                                }
                                if(fd == -1) {
                                        DEBUG(0,("startsmbfilepwent_internal: too many race conditions \
@@ -234,14 +237,14 @@ creating file %s\n", pfile));
        for(race_loop = 0; race_loop < 5; race_loop++) {
                DEBUG(10, ("startsmbfilepwent_internal: opening file %s\n", pfile));
 
-               if((fp = sys_fopen(pfile, open_mode)) == NULL) {
+               if((fp = fopen(pfile, open_mode)) == NULL) {
 
                        /*
                         * If smbpasswd file doesn't exist, then create new one. This helps to avoid
                         * confusing error msg when adding user account first time.
                         */
                        if (errno == ENOENT) {
-                               if ((fp = sys_fopen(pfile, "a+")) != NULL) {
+                               if ((fp = fopen(pfile, "a+")) != NULL) {
                                        DEBUG(0, ("startsmbfilepwent_internal: file %s did not \
 exist. File successfully created.\n", pfile));
                                } else {
@@ -547,7 +550,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++) {
@@ -639,7 +642,7 @@ static NTSTATUS add_smbfilepwd_entry(struct smbpasswd_privates *smbpasswd_state,
        int fd;
        size_t new_entry_length;
        char *new_entry;
-       SMB_OFF_T offpos;
+       off_t offpos;
  
        /* Open the smbpassword file - for update. */
        fp = startsmbfilepwent(pfile, PWF_UPDATE, &smbpasswd_state->pw_file_lock_depth);
@@ -675,9 +678,9 @@ static NTSTATUS add_smbfilepwd_entry(struct smbpasswd_privates *smbpasswd_state,
         */
        fd = fileno(fp);
 
-       if((offpos = sys_lseek(fd, 0, SEEK_END)) == -1) {
+       if((offpos = lseek(fd, 0, SEEK_END)) == -1) {
                NTSTATUS result = map_nt_error_from_unix(errno);
-               DEBUG(0, ("add_smbfilepwd_entry(sys_lseek): Failed to add entry for user %s to file %s. \
+               DEBUG(0, ("add_smbfilepwd_entry(lseek): Failed to add entry for user %s to file %s. \
 Error was %s\n", newpwd->smb_name, pfile, strerror(errno)));
                endsmbfilepwent(fp, &smbpasswd_state->pw_file_lock_depth);
                return result;
@@ -703,7 +706,7 @@ Error was %s\n", newpwd->smb_name, pfile, strerror(errno)));
 Error was %s\n", wr_len, newpwd->smb_name, pfile, strerror(errno)));
 
                /* Remove the entry we just wrote. */
-               if(sys_ftruncate(fd, offpos) == -1) {
+               if(ftruncate(fd, offpos) == -1) {
                        DEBUG(0, ("add_smbfilepwd_entry: ERROR failed to ftruncate file %s. \
 Error was %s. Password file may be corrupt ! Please examine by hand !\n", 
                                newpwd->smb_name, strerror(errno)));
@@ -734,7 +737,8 @@ static bool mod_smbfilepwd_entry(struct smbpasswd_privates *smbpasswd_state, con
        fstring user_name;
 
        char *status;
-       char linebuf[256];
+#define LINEBUF_SIZE 255
+       char linebuf[LINEBUF_SIZE + 1];
        char readbuf[1024];
        int c;
        fstring ascii_p16;
@@ -747,7 +751,7 @@ static bool mod_smbfilepwd_entry(struct smbpasswd_privates *smbpasswd_state, con
        bool found_entry = False;
        bool got_pass_last_set_time = False;
 
-       SMB_OFF_T pwd_seekpos = 0;
+       off_t pwd_seekpos = 0;
 
        int i;
        int wr_len;
@@ -759,7 +763,7 @@ static bool mod_smbfilepwd_entry(struct smbpasswd_privates *smbpasswd_state, con
        }
        DEBUG(10, ("mod_smbfilepwd_entry: opening file %s\n", pfile));
 
-       fp = sys_fopen(pfile, "r+");
+       fp = fopen(pfile, "r+");
 
        if (fp == NULL) {
                DEBUG(0, ("mod_smbfilepwd_entry: unable to open file %s\n", pfile));
@@ -785,11 +789,11 @@ static bool mod_smbfilepwd_entry(struct smbpasswd_privates *smbpasswd_state, con
         */
        status = linebuf;
        while (status && !feof(fp)) {
-               pwd_seekpos = sys_ftell(fp);
+               pwd_seekpos = ftell(fp);
 
                linebuf[0] = '\0';
 
-               status = fgets(linebuf, sizeof(linebuf), fp);
+               status = fgets(linebuf, LINEBUF_SIZE, fp);
                if (status == NULL && ferror(fp)) {
                        pw_file_unlock(lockfd, &smbpasswd_state->pw_file_lock_depth);
                        fclose(fp);
@@ -973,7 +977,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])) {
@@ -988,7 +992,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 == '[' */
 
@@ -1015,10 +1019,10 @@ This is no longer supported.!\n", pwd->smb_name));
 
 #ifdef DEBUG_PASSWORD
        DEBUG(100,("mod_smbfilepwd_entry: "));
-       dump_data(100, (uint8 *)ascii_p16, wr_len);
+       dump_data(100, (uint8_t *)ascii_p16, wr_len);
 #endif
 
-       if(wr_len > sizeof(linebuf)) {
+       if(wr_len > LINEBUF_SIZE) {
                DEBUG(0, ("mod_smbfilepwd_entry: line to write (%d) is too long.\n", wr_len+1));
                pw_file_unlock(lockfd,&smbpasswd_state->pw_file_lock_depth);
                fclose(fp);
@@ -1036,7 +1040,7 @@ This is no longer supported.!\n", pwd->smb_name));
 
        fd = fileno(fp);
 
-       if (sys_lseek(fd, pwd_seekpos - 1, SEEK_SET) != pwd_seekpos - 1) {
+       if (lseek(fd, pwd_seekpos - 1, SEEK_SET) != pwd_seekpos - 1) {
                DEBUG(0, ("mod_smbfilepwd_entry: seek fail on file %s.\n", pfile));
                pw_file_unlock(lockfd,&smbpasswd_state->pw_file_lock_depth);
                fclose(fp);
@@ -1058,7 +1062,7 @@ This is no longer supported.!\n", pwd->smb_name));
                return False;
        }
  
-       if (sys_lseek(fd, pwd_seekpos, SEEK_SET) != pwd_seekpos) {
+       if (lseek(fd, pwd_seekpos, SEEK_SET) != pwd_seekpos) {
                DEBUG(0, ("mod_smbfilepwd_entry: seek fail on file %s.\n", pfile));
                pw_file_unlock(lockfd,&smbpasswd_state->pw_file_lock_depth);
                fclose(fp);
@@ -1092,7 +1096,7 @@ static bool del_smbfilepwd_entry(struct smbpasswd_privates *smbpasswd_state, con
 
        pfile2 = talloc_asprintf(talloc_tos(),
                        "%s.%u",
-                       pfile, (unsigned)sys_getpid());
+                       pfile, (unsigned)getpid());
        if (!pfile2) {
                return false;
        }
@@ -1202,9 +1206,9 @@ static bool build_smb_pass (struct smb_passwd *smb_pw, const struct samu *sampas
 
                /* If the user specified a RID, make sure its able to be both stored and retreived */
                if (rid == DOMAIN_RID_GUEST) {
-                       struct passwd *passwd = Get_Pwnam_alloc(NULL, lp_guestaccount());
+                       struct passwd *passwd = Get_Pwnam_alloc(NULL, lp_guest_account());
                        if (!passwd) {
-                               DEBUG(0, ("Could not find guest account via Get_Pwnam_alloc()! (%s)\n", lp_guestaccount()));
+                               DEBUG(0, ("Could not find guest account via Get_Pwnam_alloc()! (%s)\n", lp_guest_account()));
                                return False;
                        }
                        smb_pw->smb_userid=passwd->pw_uid;
@@ -1339,7 +1343,7 @@ static NTSTATUS smbpasswd_getsampwsid(struct pdb_methods *my_methods, struct sam
 
        /* More special case 'guest account' hacks... */
        if (rid == DOMAIN_RID_GUEST) {
-               const char *guest_account = lp_guestaccount();
+               const char *guest_account = lp_guest_account();
                if (!(guest_account && *guest_account)) {
                        DEBUG(1, ("Guest account not specfied!\n"));
                        return nt_status;
@@ -1445,7 +1449,7 @@ static NTSTATUS smbpasswd_rename_sam_account (struct pdb_methods *my_methods,
        TALLOC_CTX *ctx = talloc_tos();
        NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
 
-       if (!*(lp_renameuser_script()))
+       if (!*(lp_rename_user_script(talloc_tos())))
                goto done;
 
        if ( !(new_acct = samu_new( NULL )) ) {
@@ -1465,8 +1469,7 @@ static NTSTATUS smbpasswd_rename_sam_account (struct pdb_methods *my_methods,
        interim_account = True;
 
        /* rename the posix user */
-       rename_script = talloc_strdup(ctx,
-                               lp_renameuser_script());
+       rename_script = lp_rename_user_script(ctx);
        if (!rename_script) {
                ret = NT_STATUS_NO_MEMORY;
                goto done;
@@ -1498,7 +1501,7 @@ static NTSTATUS smbpasswd_rename_sam_account (struct pdb_methods *my_methods,
                        goto done;
                }
 
-               rename_ret = smbrun(rename_script, NULL);
+               rename_ret = smbrun(rename_script, NULL, NULL);
 
                DEBUG(rename_ret ? 0 : 3,("Running the command `%s' gave %d\n", rename_script, rename_ret));
 
@@ -1692,7 +1695,7 @@ static NTSTATUS pdb_init_smbpasswd( struct pdb_methods **pdb_method, const char
 
        /* 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;
        }
@@ -1717,7 +1720,7 @@ static NTSTATUS pdb_init_smbpasswd( struct pdb_methods **pdb_method, const char
        return NT_STATUS_OK;
 }
 
-NTSTATUS pdb_smbpasswd_init(void
+NTSTATUS pdb_smbpasswd_init(TALLOC_CTX *ctx
 {
        return smb_register_passdb(PASSDB_INTERFACE_VERSION, "smbpasswd", pdb_init_smbpasswd);
 }