reguire gnupg1 on f>=31; sense the system to use gpg1 when installed
[nodemanager.git] / plugins / specialaccounts.py
1 #!/usr/bin/python3 -tt
2 # vim:set ts=4 sw=4 expandtab:
3 #
4 #
5 # NodeManager plugin to create special accounts
6
7 """
8 create/populate accounts/ssh keys for special persons such as root, site_admin, etc.
9
10 """
11
12 import errno
13 import os
14 import random
15 import string
16 import tempfile
17 import grp
18 import pwd
19
20 import logger
21 import tools
22
23 # right after conf_files
24 priority = 3
25
26 def start():
27     logger.log("specialaccounts: plugin starting up...")
28
29 def GetSlivers(data, conf = None, plc = None):
30     if 'accounts' not in data:
31         logger.log_missing_data("specialaccounts.GetSlivers", 'accounts')
32         return
33
34     for account in data['accounts']:
35         name = account['name']
36         new_keys = account['keys']
37
38         logger.log('specialaccounts: dealing with account %s'%name)
39
40         # look up account name, which must exist
41         pw_info = pwd.getpwnam(name)
42         uid = pw_info[2]
43         gid = pw_info[3]
44         pw_dir = pw_info[5]
45
46         # populate account's .ssh/authorized_keys file
47         dot_ssh = os.path.join(pw_dir, '.ssh')
48         if not os.access(dot_ssh, os.F_OK): os.mkdir(dot_ssh)
49         auth_keys = os.path.join(dot_ssh, 'authorized_keys')
50
51         # catenate all keys in string, add newlines just in case (looks like keys already have this, but)
52         auth_keys_contents = '\n'.join(new_keys)+'\n'
53
54         changes = tools.replace_file_with_string(auth_keys, auth_keys_contents)
55         if changes:
56             logger.log("specialaccounts: keys file changed: %s" % auth_keys)
57
58         # always set permissions properly
59         os.chmod(dot_ssh, 0o700)
60         os.chown(dot_ssh, uid, gid)
61         os.chmod(auth_keys, 0o600)
62         os.chown(auth_keys, uid, gid)
63
64         logger.log('specialaccounts: installed ssh keys for %s' % name)