SAM database "set user info".
[samba.git] / source3 / passdb / smbpass.c
index e15a9b1dd703fd8ae6581742ccb3120c75f9ab43..464cec015c504844f18ddff4a077f2c74b0aebb7 100644 (file)
@@ -23,8 +23,6 @@
 
 static int pw_file_lock_depth = 0;
 extern int DEBUGLEVEL;
-extern pstring samlogon_user;
-extern BOOL sam_logon_in_ssb;
 
 static char s_readbuf[1024];
 
@@ -33,7 +31,7 @@ static char s_readbuf[1024];
  to ensure no modification outside this module.
 ****************************************************************/
 
-void *startsmbfilepwent(BOOL update)
+static void *startsmbfilepwent(BOOL update)
 {
        return startfilepwent(lp_smb_passwd_file(), s_readbuf, sizeof(s_readbuf),
                              &pw_file_lock_depth, update);
@@ -43,7 +41,7 @@ void *startsmbfilepwent(BOOL update)
  End enumeration of the smbpasswd list.
 ****************************************************************/
 
-void endsmbfilepwent(void *vp)
+static void endsmbfilepwent(void *vp)
 {
        endfilepwent(vp, &pw_file_lock_depth);
 }
@@ -53,7 +51,7 @@ void endsmbfilepwent(void *vp)
  This must be treated as an opaque token.
 *************************************************************************/
 
-SMB_BIG_UINT getsmbfilepwpos(void *vp)
+static SMB_BIG_UINT getsmbfilepwpos(void *vp)
 {
        return getfilepwpos(vp);
 }
@@ -63,22 +61,24 @@ SMB_BIG_UINT getsmbfilepwpos(void *vp)
  This must be treated as an opaque token.
 *************************************************************************/
 
-BOOL setsmbfilepwpos(void *vp, SMB_BIG_UINT tok)
+static BOOL setsmbfilepwpos(void *vp, SMB_BIG_UINT tok)
 {
        return setfilepwpos(vp, tok);
 }
 
 /*************************************************************************
  Routine to return the next entry in the smbpasswd list.
+
+ this function is non-static as it is called (exclusively and only)
+ from getsamfile21pwent().
  *************************************************************************/
 struct smb_passwd *getsmbfilepwent(void *vp)
 {
        /* Static buffers we will return. */
        static struct smb_passwd pw_buf;
-       static pstring  user_name;
+       static pstring  unix_name;
        static unsigned char smbpwd[16];
        static unsigned char smbntpwd[16];
-       struct passwd *pwfile;
        char            linebuf[256];
        char  *p;
        int            uidval;
@@ -118,7 +118,7 @@ struct smb_passwd *getsmbfilepwent(void *vp)
                 * As 256 is shorter than a pstring we don't need to check
                 * length here - if this ever changes....
                 */
-               p = strncpyn(user_name, linebuf, sizeof(user_name), ':');
+               p = strncpyn(unix_name, linebuf, sizeof(unix_name), ':');
 
                /* Go past ':' */
                p++;
@@ -127,8 +127,8 @@ struct smb_passwd *getsmbfilepwent(void *vp)
 
                p = Atoic( p, &uidval, ":");
 
-               pw_buf.smb_name = user_name;
-               pw_buf.smb_userid = uidval;
+               pw_buf.unix_name = unix_name;
+               pw_buf.unix_uid = uidval;
 
                /*
                 * Now get the password value - this should be 32 hex digits
@@ -139,16 +139,6 @@ struct smb_passwd *getsmbfilepwent(void *vp)
                /* Skip the ':' */
                p++;
 
-               if (*p == '*' || *p == 'X')
-               {
-                       /* Password deliberately invalid - end here. */
-                       DEBUG(10, ("getsmbfilepwent: entry invalidated for user %s\n", user_name));
-                       pw_buf.smb_nt_passwd = NULL;
-                       pw_buf.smb_passwd = NULL;
-                       pw_buf.acct_ctrl |= ACB_DISABLED;
-                       return &pw_buf;
-               }
-
                if (linebuf_len < (PTR_DIFF(p, linebuf) + 33))
                {
                        DEBUG(0, ("getsmbfilepwent: malformed password entry (passwd too short)\n"));
@@ -168,7 +158,7 @@ struct smb_passwd *getsmbfilepwent(void *vp)
                }
                else
                {
-                       if (!pwdb_gethexpwd(p, (char *)smbpwd))
+                       if (!pwdb_gethexpwd(p, (char *)smbpwd, NULL))
                        {
                                DEBUG(0, ("getsmbfilepwent: Malformed Lanman password entry (non hex chars)\n"));
                                continue;
@@ -188,7 +178,7 @@ struct smb_passwd *getsmbfilepwent(void *vp)
                {
                        if (*p != '*' && *p != 'X')
                        {
-                               if(pwdb_gethexpwd(p,(char *)smbntpwd))
+                               if(pwdb_gethexpwd(p,(char *)smbntpwd, NULL))
                                {
                                        pw_buf.smb_nt_passwd = smbntpwd;
                                }
@@ -197,8 +187,8 @@ struct smb_passwd *getsmbfilepwent(void *vp)
                        p += 33;
                }
 
-               DEBUG(5,("getsmbfilepwent: returning passwd entry for user %s, uid %d\n",
-                         user_name, uidval));
+               DEBUG(5,("getsmbfilepwent: returning passwd entry for unix user %s, unix uid %d\n",
+                         unix_name, uidval));
 
                if (*p == '[')
                {
@@ -211,6 +201,10 @@ struct smb_passwd *getsmbfilepwent(void *vp)
                        }
 
                        /* Now try and get the last change time. */
+                        while (*p != ']' && *p != ':') 
+                        {
+                                p++;
+                        }
                        if (*p == ']')
                        {
                                p++;
@@ -229,21 +223,25 @@ struct smb_passwd *getsmbfilepwent(void *vp)
                         * password file as 'normal accounts'. If this changes
                         * we will have to fix this code. JRA.
                         */
-                       if (pw_buf.smb_name[strlen(pw_buf.smb_name) - 1] == '$')        
+                       if (pw_buf.unix_name[strlen(pw_buf.unix_name) - 1] == '$')      
                        {
                                pw_buf.acct_ctrl &= ~ACB_NORMAL;
                                pw_buf.acct_ctrl |= ACB_WSTRUST;
                        }
                }
 
-               pwfile = Get_Pwnam(pw_buf.smb_name, False);
-               if (pwfile == NULL)
+               if (*p == '*' || *p == 'X')
                {
-                       DEBUG(0,("getsmbfilepwent: smbpasswd database is corrupt!\n"));
-                       DEBUG(0,("getsmbfilepwent: username %s not in unix passwd database!\n", pw_buf.smb_name));
-                       return NULL;
+                       /* Password deliberately invalid - end here. */
+                       DEBUG(10, ("getsmbfilepwent: entry invalidated for unix user %s\n", unix_name));
+                       pw_buf.smb_nt_passwd = NULL;
+                       pw_buf.smb_passwd = NULL;
+                       pw_buf.acct_ctrl |= ACB_DISABLED;
                }
 
+               DEBUG(6,("unixuser:%s uid:%d acb:%x\n",
+                         pw_buf.unix_name, pw_buf.unix_uid, pw_buf.acct_ctrl));
+
                return &pw_buf;
        }
 
@@ -251,110 +249,6 @@ struct smb_passwd *getsmbfilepwent(void *vp)
        return NULL;
 }
 
-/*************************************************************************
- Routine to return the next entry in the smbpasswd list.
- this function is a nice, messy combination of reading:
- - the smbpasswd file
- - the unix password database
- - smb.conf options (not done at present).
- *************************************************************************/
-
-static struct sam_passwd *getsmbfile21pwent(void *vp)
-{
-       struct smb_passwd *pw_buf = getsmbfilepwent(vp);
-       static struct sam_passwd user;
-       struct passwd *pwfile;
-
-       static pstring full_name;
-       static pstring home_dir;
-       static pstring home_drive;
-       static pstring logon_script;
-       static pstring profile_path;
-       static pstring acct_desc;
-       static pstring workstations;
-       
-       DEBUG(5,("getsmbfile21pwent\n"));
-
-       if (pw_buf == NULL) return NULL;
-
-       pwdb_init_sam(&user);
-
-       pwfile = Get_Pwnam(pw_buf->smb_name, False);
-       if (pwfile == NULL)
-       {
-               DEBUG(0,("getsmbfile21pwent: smbpasswd database is corrupt!\n"));
-               DEBUG(0,("getsmbfile21pwent: username %s not in unix passwd database!\n", pw_buf->smb_name));
-               return NULL;
-       }
-
-       pstrcpy(samlogon_user, pw_buf->smb_name);
-
-       if (samlogon_user[strlen(samlogon_user)-1] != '$')
-       {
-               /* XXXX hack to get standard_sub_basic() to use sam logon username */
-               /* possibly a better way would be to do a become_user() call */
-               sam_logon_in_ssb = True;
-
-               user.smb_userid    = pw_buf->smb_userid;
-               user.smb_grpid     = pwfile->pw_gid;
-
-               user.user_rid  = pwdb_uid_to_user_rid (user.smb_userid);
-               user.group_rid = pwdb_gid_to_group_rid(user.smb_grpid );
-
-               pstrcpy(full_name    , pwfile->pw_gecos        );
-               pstrcpy(logon_script , lp_logon_script       ());
-               pstrcpy(profile_path , lp_logon_path         ());
-               pstrcpy(home_drive   , lp_logon_drive        ());
-               pstrcpy(home_dir     , lp_logon_home         ());
-               pstrcpy(acct_desc    , "");
-               pstrcpy(workstations , "");
-
-               sam_logon_in_ssb = False;
-       }
-       else
-       {
-               user.smb_userid    = pw_buf->smb_userid;
-               user.smb_grpid     = pwfile->pw_gid;
-
-               user.user_rid  = pwdb_uid_to_user_rid (user.smb_userid);
-               user.group_rid = DOMAIN_GROUP_RID_USERS; /* lkclXXXX this is OBSERVED behaviour by NT PDCs, enforced here. */
-
-               pstrcpy(full_name    , "");
-               pstrcpy(logon_script , "");
-               pstrcpy(profile_path , "");
-               pstrcpy(home_drive   , "");
-               pstrcpy(home_dir     , "");
-               pstrcpy(acct_desc    , "");
-               pstrcpy(workstations , "");
-       }
-
-       user.smb_name     = pw_buf->smb_name;
-       user.full_name    = full_name;
-       user.home_dir     = home_dir;
-       user.dir_drive    = home_drive;
-       user.logon_script = logon_script;
-       user.profile_path = profile_path;
-       user.acct_desc    = acct_desc;
-       user.workstations = workstations;
-
-       user.unknown_str = NULL; /* don't know, yet! */
-       user.munged_dial = NULL; /* "munged" dial-back telephone number */
-
-       user.smb_nt_passwd = pw_buf->smb_nt_passwd;
-       user.smb_passwd    = pw_buf->smb_passwd;
-                       
-       user.acct_ctrl = pw_buf->acct_ctrl;
-
-       user.unknown_3 = 0xffffff; /* don't know */
-       user.logon_divs = 168; /* hours per week */
-       user.hours_len = 21; /* 21 times 8 bits = 168 */
-       memset(user.hours, 0xff, user.hours_len); /* available at all hours */
-       user.unknown_5 = 0x00020000; /* don't know */
-       user.unknown_5 = 0x000004ec; /* don't know */
-
-       return &user;
-}
-
 /************************************************************************
  Routine to add an entry to the smbpasswd file.
 *************************************************************************/
@@ -387,8 +281,8 @@ static BOOL add_smbfilepwd_entry(struct smb_passwd *newpwd)
    */
 
   while ((pwd = getsmbfilepwent(fp)) != NULL) {
-    if (strequal(newpwd->smb_name, pwd->smb_name)) {
-      DEBUG(0, ("add_smbfilepwd_entry: entry with name %s already exists\n", pwd->smb_name));
+    if (strequal(newpwd->unix_name, pwd->unix_name)) {
+      DEBUG(0, ("add_smbfilepwd_entry: entry with unix name %s already exists\n", pwd->unix_name));
       endsmbfilepwent(fp);
       return False;
     }
@@ -405,21 +299,21 @@ static BOOL add_smbfilepwd_entry(struct smb_passwd *newpwd)
 
   if((offpos = sys_lseek(fd, 0, SEEK_END)) == -1) {
     DEBUG(0, ("add_smbfilepwd_entry(sys_lseek): Failed to add entry for user %s to file %s. \
-Error was %s\n", newpwd->smb_name, pfile, strerror(errno)));
+Error was %s\n", newpwd->unix_name, pfile, strerror(errno)));
     endsmbfilepwent(fp);
     return False;
   }
 
-  new_entry_length = strlen(newpwd->smb_name) + 1 + 15 + 1 + 32 + 1 + 32 + 1 + NEW_PW_FORMAT_SPACE_PADDED_LEN + 1 + 13 + 2;
+  new_entry_length = strlen(newpwd->unix_name) + 1 + 15 + 1 + 32 + 1 + 32 + 1 + NEW_PW_FORMAT_SPACE_PADDED_LEN + 1 + 13 + 2;
 
   if((new_entry = (char *)malloc( new_entry_length )) == NULL) {
     DEBUG(0, ("add_smbfilepwd_entry(malloc): Failed to add entry for user %s to file %s. \
-Error was %s\n", newpwd->smb_name, pfile, strerror(errno)));
+Error was %s\n", newpwd->unix_name, pfile, strerror(errno)));
     endsmbfilepwent(fp);
     return False;
   }
 
-  slprintf(new_entry, new_entry_length - 1, "%s:%u:", newpwd->smb_name, (unsigned)newpwd->smb_userid);
+  slprintf(new_entry, new_entry_length - 1, "%s:%u:", newpwd->unix_name, (unsigned)newpwd->unix_uid);
   p = &new_entry[strlen(new_entry)];
 
   if(newpwd->smb_passwd != NULL) {
@@ -464,13 +358,13 @@ Error was %s\n", newpwd->smb_name, pfile, strerror(errno)));
 
   if ((wr_len = write(fd, new_entry, strlen(new_entry))) != strlen(new_entry)) {
     DEBUG(0, ("add_smbfilepwd_entry(write): %d Failed to add entry for user %s to file %s. \
-Error was %s\n", wr_len, newpwd->smb_name, pfile, strerror(errno)));
+Error was %s\n", wr_len, newpwd->unix_name, pfile, strerror(errno)));
 
     /* Remove the entry we just wrote. */
     if(sys_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)));
+             newpwd->unix_name, strerror(errno)));
     }
 
     endsmbfilepwent(fp);
@@ -495,7 +389,7 @@ Error was %s. Password file may be corrupt ! Please examine by hand !\n",
 static BOOL mod_smbfilepwd_entry(struct smb_passwd* pwd, BOOL override)
 {
   /* Static buffers we will return. */
-  static pstring  user_name;
+  static pstring  unix_name;
 
   char            linebuf[256];
   char            readbuf[1024];
@@ -516,6 +410,17 @@ static BOOL mod_smbfilepwd_entry(struct smb_passwd* pwd, BOOL override)
   int wr_len;
   int fd;
 
+#ifdef DEBUG_PASSWORD
+       DEBUG(100,("mod_smbfilepwd_entry: password entries\n"));
+       if (pwd->smb_passwd != NULL)
+       {
+               dump_data(100, pwd->smb_passwd, 16);
+       }
+       if (pwd->smb_nt_passwd != NULL)
+       {
+               dump_data(100, pwd->smb_nt_passwd, 16);
+       }
+#endif
   if (!*pfile) {
     DEBUG(0, ("No SMB password file set\n"));
     return False;
@@ -613,9 +518,9 @@ static BOOL mod_smbfilepwd_entry(struct smb_passwd* pwd, BOOL override)
      * As 256 is shorter than a pstring we don't need to check
      * length here - if this ever changes....
      */
-    strncpy(user_name, linebuf, PTR_DIFF(p, linebuf));
-    user_name[PTR_DIFF(p, linebuf)] = '\0';
-    if (strequal(user_name, pwd->smb_name)) {
+    strncpy(unix_name, linebuf, PTR_DIFF(p, linebuf));
+    unix_name[PTR_DIFF(p, linebuf)] = '\0';
+    if (strequal(unix_name, pwd->unix_name)) {
       found_entry = True;
       break;
     }
@@ -660,7 +565,7 @@ static BOOL mod_smbfilepwd_entry(struct smb_passwd* pwd, BOOL override)
 
   if (!override && (*p == '*' || *p == 'X')) {
     /* Password deliberately invalid - end here. */
-    DEBUG(10, ("mod_smbfilepwd_entry: entry invalidated for user %s\n", user_name));
+    DEBUG(10, ("mod_smbfilepwd_entry: entry invalidated for unix user %s\n", unix_name));
     file_unlock(lockfd, &pw_file_lock_depth);
     fclose(fp);
     return False;
@@ -884,58 +789,19 @@ static BOOL mod_smbfilepwd_entry(struct smb_passwd* pwd, BOOL override)
   return True;
 }
 
-/*
- * Stub functions - implemented in terms of others.
- */
-
-static BOOL mod_smbfile21pwd_entry(struct sam_passwd* pwd, BOOL override)
-{
-       return mod_smbfilepwd_entry(pwdb_sam_to_smb(pwd), override);
-}
-
-static BOOL add_smbfile21pwd_entry(struct sam_passwd *newpwd)
-{
-       return add_smbfilepwd_entry(pwdb_sam_to_smb(newpwd));
-}
-
-static struct sam_disp_info *getsmbfiledispnam(const char *name)
-{
-       return pwdb_sam_to_dispinfo(getsam21pwnam(name));
-}
-
-static struct sam_disp_info *getsmbfiledisprid(uint32 rid)
-{
-       return pwdb_sam_to_dispinfo(getsam21pwrid(rid));
-}
-
-static struct sam_disp_info *getsmbfiledispent(void *vp)
-{
-       return pwdb_sam_to_dispinfo(getsam21pwent(vp));
-}
-
-static struct passdb_ops file_ops = {
+static struct smb_passdb_ops file_ops = {
   startsmbfilepwent,
   endsmbfilepwent,
   getsmbfilepwpos,
   setsmbfilepwpos,
   iterate_getsmbpwnam,          /* In passdb.c */
   iterate_getsmbpwuid,          /* In passdb.c */
-  iterate_getsmbpwrid,          /* In passdb.c */
   getsmbfilepwent,
   add_smbfilepwd_entry,
-  mod_smbfilepwd_entry,
-  getsmbfile21pwent,
-  iterate_getsam21pwnam,
-  iterate_getsam21pwuid,
-  iterate_getsam21pwrid, 
-  add_smbfile21pwd_entry,
-  mod_smbfile21pwd_entry,
-  getsmbfiledispnam,
-  getsmbfiledisprid,
-  getsmbfiledispent
+  mod_smbfilepwd_entry
 };
 
-struct passdb_ops *file_initialise_password_db(void)
+struct smb_passdb_ops *file_initialise_password_db(void)
 {    
   return &file_ops;
 }