[anaconda] Add sshkey kickstart command

Brian C. Lane bcl at redhat.com
Sat Dec 13 02:33:49 UTC 2014


sshkey --username=<user> "ssh key"

This adds the sshkey command, used to set the ssh key for users on the
installed system. The user must exist or be root and the key must be
quoted.
---
 pyanaconda/install.py   |  1 +
 pyanaconda/kickstart.py |  6 ++++++
 pyanaconda/users.py     | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+)

diff --git a/pyanaconda/install.py b/pyanaconda/install.py
index 6f7dc8f..6246fd2 100644
--- a/pyanaconda/install.py
+++ b/pyanaconda/install.py
@@ -98,6 +98,7 @@ def doConfiguration(storage, payload, ksdata, instClass):
         ksdata.rootpw.execute(storage, ksdata, instClass, u)
         ksdata.group.execute(storage, ksdata, instClass, u)
         ksdata.user.execute(storage, ksdata, instClass, u)
+        ksdata.sshkey.execute(storage, ksdata, instClass, u)
 
     with progress_report(_("Configuring addons")):
         ksdata.addons.execute(storage, ksdata, instClass, u)
diff --git a/pyanaconda/kickstart.py b/pyanaconda/kickstart.py
index c09d4d7..1038853 100644
--- a/pyanaconda/kickstart.py
+++ b/pyanaconda/kickstart.py
@@ -1556,6 +1556,11 @@ class Services(commands.services.FC6_Services):
 
             iutil.execInSysroot("systemctl", ["enable", svc])
 
+class SshKey(commands.sshkey.F22_SshKey):
+    def execute(self, storage, ksdata, instClass, users):
+        for usr in self.sshUserList:
+            users.setUserSshKey(usr.username, usr.key)
+
 class Timezone(commands.timezone.F18_Timezone):
     def __init__(self, *args):
         commands.timezone.F18_Timezone.__init__(self, *args)
@@ -1800,6 +1805,7 @@ commandMap = {
         "rootpw": RootPw,
         "selinux": SELinux,
         "services": Services,
+        "sshkey": SshKey,
         "skipx": SkipX,
         "timezone": Timezone,
         "upgrade": Upgrade,
diff --git a/pyanaconda/users.py b/pyanaconda/users.py
index 7cabbe1..3363a42 100644
--- a/pyanaconda/users.py
+++ b/pyanaconda/users.py
@@ -426,3 +426,37 @@ class Users:
 
     def setRootPassword(self, password, isCrypted=False, isLocked=False, algo=None):
         return self.setUserPassword("root", password, isCrypted, isLocked, algo)
+
+    def setUserSshKey(self, username, key, **kwargs):
+        childpid = self._prepareChroot(kwargs.get("root", iutil.getSysroot()))
+
+        if childpid == 0:
+            user = self.admin.lookupUserByName(username)
+            if not user:
+                log.error("setUserSshKey: user %s does not exist", username)
+                os._exit(1)
+
+            homedir = user.get(libuser.HOMEDIRECTORY)[0]
+            if not os.path.exists(homedir):
+                log.error("setUserSshKey: home directory for %s does not exist", username)
+                os._exit(1)
+
+            sshdir = os.path.join(homedir, ".ssh")
+            if not os.path.isdir(sshdir):
+                os.mkdir(sshdir, 0o700)
+                iutil.eintr_retry_call(os.chown, sshdir, user.get(libuser.UIDNUMBER)[0], user.get(libuser.GIDNUMBER)[0])
+
+            authfile = os.path.join(sshdir, "authorized_keys")
+            authfile_existed = os.path.exists(authfile)
+            with open(authfile, "a") as f:
+                f.write(key + "\n")
+
+            # Only change mode and ownership if we created it
+            if not authfile_existed:
+                iutil.eintr_retry_call(os.chmod, authfile, 0o600)
+                iutil.eintr_retry_call(os.chown, authfile, user.get(libuser.UIDNUMBER)[0], user.get(libuser.GIDNUMBER)[0])
+
+            iutil.execWithRedirect("restorecon", [authfile])
+            os._exit(0)
+        else:
+            return self._finishChroot(childpid)
-- 
1.9.3



More information about the anaconda-patches mailing list