salt icon indicating copy to clipboard operation
salt copied to clipboard

salt.states.user generates a traceback when setting passwords on SmartOS

Open njones11 opened this issue 12 years ago • 6 comments

I'm getting the following error when creating user accounts on SmartOS with Salt 0.17.4. Although the accounts get created successfully, this traceback is still getting generated.

 Name: wheel - Function: group.present - Result: True
 Name: py27-pip - Function: pkg.installed - Result: True
 Name: foo - Function: group.present - Result: True
 Name: bzip2 - Function: pkg.installed - Result: True
 Name: autoconf - Function: pkg.installed - Result: True
----------
    State: - user
    Name:     foo
    Function:  present
        Result:    False
        Comment:   An exception occurred in this state: Traceback (most recent call last):
  File "salt/state.py", line 1307, in call
  File "salt/states/user.py", line 373, in present
  File "salt/modules/solaris_shadow.py", line 208, in set_password
  TypeError: 'NoneType' object has no attribute '__getitem__'

        Changes:   

Looking at the code here: https://github.com/saltstack/salt/blob/v0.17.4/salt/modules/solaris_shadow.py

It appears that the uinfo = info(name) call at line 207 is failing. I think this is triggered by the code here at 118-121 in solaris_shadow.py:

   if len(fields) == 2:
        # For example:
        #   root      NL
        return

This return should return something, even if it is an empty dict.

I think this issue is related to the fact that passwd on SmartOS can produce variable output. See this snippet from 'man passwd' on SmartOS

     The format of the display is:

       name status mm/dd/yy min max warn

     or, if password aging information is not present,

       name status

Basically, on a new account creation, there may not be password age information present, and thus len==2 and nothing gets returned.

njones11 avatar Jan 21 '14 21:01 njones11

Additionally I noticed that when the state tries to set a password hash it will always fail because solaris_shadow.py doesn't currently read in the password hash from the shadow file.

nshalman avatar Jan 22 '14 14:01 nshalman

I'm downlaoding a SmartOS ISO right now, I'll see if there is anything different between how SmartOS and Solaris handle passwords.

Thanks for reporting.

terminalmage avatar Jan 22 '14 19:01 terminalmage

I think the fix should look something like this (I'll try to confirm and open a PR tomorrow...):

diff --git a/salt/modules/solaris_shadow.py b/salt/modules/solaris_shadow.py
index fb751bf..5de358d 100644
--- a/salt/modules/solaris_shadow.py
+++ b/salt/modules/solaris_shadow.py
@@ -94,8 +94,7 @@ def info(name):
     try:
         data = pwd.getpwnam(name)
         ret.update({
-            'name': name,
-            'passwd': data.pw_dir
+            'name': name
         })
     except KeyError:
         return ret
@@ -123,12 +122,11 @@ def info(name):
     if len(fields) == 2:
         # For example:
         #   root      NL
-        return
+        return ret
     # We have all fields:
     #   buildbot L 05/09/2013 0 99999 7
     ret.update({
         'name': data.pw_name,
-        'passwd': data.pw_dir,
         'lstchg': fields[2],
         'min': int(fields[3]),
         'max': int(fields[4]),
@@ -136,6 +134,14 @@ def info(name):
         'inact': '',
         'expire': ''
     })
+    s_file = '/etc/shadow'
+    if not os.path.isfile(s_file):
+        return ret
+    with salt.utils.fopen(s_file, 'rb') as ifile:
+        for line in ifile:
+            comps = line.strip().split(':')
+            if comps[0] == name:
+                ret.update({'passwd': comps[1]})
     return ret


nshalman avatar Jan 23 '14 01:01 nshalman

@njones11 The tarball for testing this patch is here: https://us-east.manta.joyent.com/nahamu/public/salt/testing/salt-0.17.4-87-g8eead32-esky-smartos.tar.gz

I've prepared branches off of develop, 2014.1, and 0.17 to turn into PR's assuming this works (same commit cherry-picked from the develop one onto the other two): https://github.com/nshalman/salt/tree/smartos-shadow-develop https://github.com/nshalman/salt/tree/smartos-shadow-2014.1 https://github.com/nshalman/salt/tree/smartos-shadow-0.17

nshalman avatar Jan 23 '14 17:01 nshalman

I've also filed https://github.com/joyent/pkgsrc/issues/164 so that future versions of Python in SmartOS will hopefully have a working spwd module.

nshalman avatar Jan 23 '14 18:01 nshalman

Patch is wrong. Will respin.

nshalman avatar Jan 23 '14 19:01 nshalman