(*) the various modules have a priority; lower gets invoked first
[nodemanager.git] / plugins / specialaccounts.py
1 #!/usr/bin/python -tt
2 # vim:set ts=4 sw=4 expandtab:
3 #
4 # $Id$
5 # $URL$
6 #
7 # NodeManager plugin to create special accounts
8
9 """
10 Have NM create/populate accounts/ssh keys for special persons such as root, site_admin, etc.
11
12 """
13
14 import errno
15 import os
16 import random
17 import string
18 import tempfile
19 import grp
20 import pwd
21
22 import logger
23 import tools
24
25 # right after conf_files
26 priority = 3
27
28 def start(options, conf):
29     logger.log("personkeys: plugin starting up...")
30
31 def GetSlivers(data, conf = None, plc = None):
32     if 'accounts' not in data: 
33         logger.log_missing_data("specialaccounts.GetSlivers",'accounts')
34         return
35
36     for account in data['accounts']:
37         name = account['name']
38         new_keys = account['keys']
39
40         logger.log('specialaccounts: dealing with account %s'%name)
41
42         # look up account name, which must exist
43         pw_info = pwd.getpwnam(name)
44         uid = pw_info[2]
45         gid = pw_info[3]
46         pw_dir = pw_info[5]
47
48         # populate account's .ssh/authorized_keys file
49         dot_ssh = os.path.join(pw_dir,'.ssh')
50         if not os.access(dot_ssh, os.F_OK): os.mkdir(dot_ssh)
51         auth_keys = os.path.join(dot_ssh,'authorized_keys')
52
53         logger.log("specialaccounts: new keys = %s" % auth_keys)
54         fd, fname = tempfile.mkstemp('','authorized_keys',dot_ssh)
55
56         for key in new_keys:
57             os.write(fd,key)
58             os.write(fd,'\n')
59
60         os.close(fd)
61         if os.path.exists(auth_keys): os.unlink(auth_keys)
62         os.rename(fname, auth_keys)
63
64         # set permissions properly
65         os.chmod(dot_ssh, 0700)
66         os.chown(dot_ssh, uid,gid)
67         os.chmod(auth_keys, 0600)
68         os.chown(auth_keys, uid,gid)
69
70         logger.log('specialacounts: installed ssh keys for %s' % name)