Setting tag nodemanager-1.8-28
[nodemanager.git] / plugins / specialaccounts.py
index ad4c2cf..545910f 100644 (file)
@@ -21,8 +21,11 @@ import tools
 def start(options, conf):
     logger.log("personkeys plugin starting up...")
 
-def GetSlivers(plc, data, conf):
-    if 'accounts' not in data: return
+def GetSlivers(data, conf = None, plc = None):
+    if 'accounts' not in data: 
+        logger.log("specialaccounts: No account information found.  DISABLED!")
+        return
+
     for account in data['accounts']:
         name = account['name']
         new_keys = account['keys']
@@ -30,24 +33,29 @@ def GetSlivers(plc, data, conf):
         # look up account name, which must exist
         pw_info = pwd.getpwnam(name)
         uid = pw_info[2]
-       gid = pw_info[3]
+        gid = pw_info[3]
         pw_dir = pw_info[5]
 
         # populate account's .ssh/authorized_keys file
-        dot_ssh = pw_dir + '/.ssh'
+        dot_ssh = os.path.join(pw_dir,'.ssh')
         if not os.access(dot_ssh, os.F_OK): os.mkdir(dot_ssh)
-        auth_keys = dot_ssh + '/authorized_keys'
-       logger.log("new keys = %s" % auth_keys)
-       auth_file = file(auth_keys,"w")
-       for key in new_keys:
-               auth_file.write(key)
-               auth_file.write("\n")
-       auth_file.close()
+        auth_keys = os.path.join(dot_ssh,'authorized_keys')
+
+        logger.log("new keys = %s" % auth_keys)
+        fd, fname = tempfile.mkstemp('','authorized_keys',dot_ssh)
+
+        for key in new_keys:
+            os.write(fd,key)
+            os.write(fd,'\n')
+
+        os.close(fd)
+        if os.path.exists(auth_keys): os.unlink(auth_keys)
+        os.rename(fname, auth_keys)
 
         # set permissions properly
         os.chmod(dot_ssh, 0700)
-        os.chmod(auth_keys, 0600)
         os.chown(dot_ssh, uid,gid)
+        os.chmod(auth_keys, 0600)
         os.chown(auth_keys, uid,gid)
 
         logger.log('specialacounts: installed ssh keys for %s' % name)