summaryrefslogtreecommitdiff
path: root/cloudinit/CloudConfig/cc_ssh.py
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit/CloudConfig/cc_ssh.py')
-rw-r--r--cloudinit/CloudConfig/cc_ssh.py42
1 files changed, 37 insertions, 5 deletions
diff --git a/cloudinit/CloudConfig/cc_ssh.py b/cloudinit/CloudConfig/cc_ssh.py
index c4603d2b..ee03de22 100644
--- a/cloudinit/CloudConfig/cc_ssh.py
+++ b/cloudinit/CloudConfig/cc_ssh.py
@@ -20,7 +20,15 @@ import os
import glob
import subprocess
+DISABLE_ROOT_OPTS="no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command=\"echo \'Please login as the user \\\"$USER\\\" rather than the user \\\"root\\\".\';echo;sleep 10\""
+
+
+global_log = None
+
def handle(name,cfg,cloud,log,args):
+ global global_log
+ global_log = log
+
# remove the static keys from the pristine image
for f in glob.glob("/etc/ssh/ssh_host_*_key*"):
try: os.unlink(f)
@@ -55,14 +63,17 @@ def handle(name,cfg,cloud,log,args):
try:
user = util.get_cfg_option_str(cfg,'user')
disable_root = util.get_cfg_option_bool(cfg, "disable_root", True)
+ disable_root_opts = util.get_cfg_option_str(cfg, "disable_root_opts",
+ DISABLE_ROOT_OPTS)
keys = cloud.get_public_ssh_keys()
if cfg.has_key("ssh_authorized_keys"):
cfgkeys = cfg["ssh_authorized_keys"]
keys.extend(cfgkeys)
- apply_credentials(keys,user,disable_root)
+ apply_credentials(keys,user,disable_root, disable_root_opts)
except:
+ util.logexc(log)
log.warn("applying credentials failed!\n")
send_ssh_keys_to_console()
@@ -70,13 +81,13 @@ def handle(name,cfg,cloud,log,args):
def send_ssh_keys_to_console():
subprocess.call(('/usr/lib/cloud-init/write-ssh-key-fingerprints',))
-def apply_credentials(keys, user, disable_root):
+def apply_credentials(keys, user, disable_root, disable_root_opts=DISABLE_ROOT_OPTS):
keys = set(keys)
if user:
setup_user_keys(keys, user, '')
if disable_root:
- key_prefix = 'command="echo \'Please login as the user \\\"%s\\\" rather than the user \\\"root\\\".\';echo;sleep 10" ' % user
+ key_prefix = disable_root_opts.replace('$USER', user)
else:
key_prefix = ''
@@ -93,13 +104,34 @@ def setup_user_keys(keys, user, key_prefix):
os.mkdir(ssh_dir)
os.chown(ssh_dir, pwent.pw_uid, pwent.pw_gid)
- authorized_keys = '%s/.ssh/authorized_keys' % pwent.pw_dir
+ try:
+ ssh_cfg = parse_ssh_config()
+ akeys = ssh_cfg.get("AuthorizedKeysFile","%h/.ssh/authorized_keys")
+ akeys = akeys.replace("%h", pwent.pw_dir)
+ akeys = akeys.replace("%u", user)
+ authorized_keys = akeys
+ except Exception as e:
+ authorized_keys = '%s/.ssh/authorized_keys' % pwent.pw_dir
+ util.logexc(global_log)
+
fp = open(authorized_keys, 'a')
- fp.write(''.join(['%s%s\n' % (key_prefix, key) for key in keys]))
+ key_prefix = key_prefix.replace("\n"," ")
+ fp.write(''.join(['%s %s\n' % (key_prefix.strip(), key) for key in keys]))
fp.close()
os.chown(authorized_keys, pwent.pw_uid, pwent.pw_gid)
os.umask(saved_umask)
+def parse_ssh_config(fname="/etc/ssh/sshd_config"):
+ ret = { }
+ fp=open(fname)
+ for l in fp.readlines():
+ l = l.strip()
+ if not l or l.startswith("#"):
+ continue
+ key,val = l.split(None,1)
+ ret[key]=val
+ fp.close()
+ return(ret)