users: Multiple SSH keys and better home creation

- Allow setting multiple SSH keys one per line (which is already
  allowed, but advertise it better).

- Use mkhomedir_helper to create the user's home directory.  Avoid
  security and accuracy complexities of creating a home directory.

- Allow homes that don't exist in /home.
This commit is contained in:
Sunil Mohan Adapa committed 2016-01-30 15:30:52 +05:30
1 parent ad7d6db968
commit 506bff5c7b
3 files changed
+48 -53

No files matched your search

+35 -42
View File
@@ -25,6 +25,7 @@ import os
import re
import shutil
import stat
import subprocess
import sys
@@ -33,64 +34,56 @@ def parse_arguments():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
get_key = subparsers.add_parser('get-key', help='Get SSH authorized key')
get_key.add_argument('--username')
get_keys = subparsers.add_parser('get-keys', help='Get SSH authorized keys')
get_keys.add_argument('--username')
set_key = subparsers.add_parser('set-key', help='Set SSH authorized key')
set_key.add_argument('--username')
set_key.add_argument('--key')
set_keys = subparsers.add_parser('set-keys', help='Set SSH authorized keys')
set_keys.add_argument('--username')
set_keys.add_argument('--keys')
return parser.parse_args()
def subcommand_get_key(arguments):
"""Get SSH authorized key."""
user = arguments.username
if not re.match(r'^[a-z][-a-z0-9_]*$', user):
def _assert_valid_username(username):
"""Verify that username is a valid one."""
if not re.match(r'^[a-z][-a-z0-9_]*$', username):
print('Bad username')
sys.exit(-1)
home = '/home/' + user
ssh_folder = home + '/.ssh'
keyfile_path = ssh_folder + '/authorized_keys'
if not os.path.exists(keyfile_path):
return
with open(keyfile_path, 'r') as keyfile:
key = keyfile.read()
print(key)
sys.exit(1)
def subcommand_set_key(arguments):
"""Set SSH authorized key."""
def subcommand_get_keys(arguments):
"""Get SSH authorized keys."""
user = arguments.username
if not re.match(r'^[a-z][-a-z0-9_]*$', user):
print('Bad username')
sys.exit(-1)
_assert_valid_username(user)
home = '/home/' + user
ssh_folder = home + '/.ssh'
keyfile_path = ssh_folder + '/authorized_keys'
path = os.path.join(os.path.expanduser('~' + user),
'.ssh', 'authorized_keys')
try:
with open(path, 'r') as file_handle:
print(file_handle.read())
except FileNotFoundError:
pass
if not os.path.exists(home):
shutil.copytree('/etc/skel', home)
shutil.chown(home, user, 'users')
for root, dirs, files in os.walk(home):
for directory in dirs:
shutil.chown(os.path.join(root, directory), user, 'users')
for filename in files:
shutil.chown(os.path.join(root, filename), user, 'users')
def subcommand_set_keys(arguments):
"""Set SSH authorized keys."""
user = arguments.username
_assert_valid_username(user)
subprocess.check_call(['mkhomedir_helper', user])
ssh_folder = os.path.join(os.path.expanduser('~' + user), '.ssh')
key_file_path = os.path.join(ssh_folder, 'authorized_keys')
if not os.path.exists(ssh_folder):
os.makedirs(ssh_folder)
shutil.chown(ssh_folder, user, 'users')
with open(keyfile_path, 'w') as keyfile:
keyfile.write(arguments.key)
shutil.chown(keyfile_path, user, 'users')
os.chmod(keyfile_path, stat.S_IRUSR | stat.S_IWUSR)
with open(key_file_path, 'w') as file_handle:
file_handle.write(arguments.keys)
shutil.chown(key_file_path, user, 'users')
os.chmod(key_file_path, stat.S_IRUSR | stat.S_IWUSR)
def main():