From 51a362baa656c28c2bfc07d3aefdde1580e5dbd9 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Tue, 5 Apr 2016 09:34:09 +0300
Subject: [PATCH] - Added more checks (if init has been run, if hostname or
 username have been   passed, if public keys exist). - Finished work on
 handling different key types (entering testing phase).

---
 ssh-ca | 51 +++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 45 insertions(+), 6 deletions(-)

diff --git a/ssh-ca b/ssh-ca
index 5cc4cbe..cbcf63a 100755
--- a/ssh-ca
+++ b/ssh-ca
@@ -1,6 +1,13 @@
 #!/bin/sh -e
-test $(which ssh-keygen) || \
-    (echo "Can't find ssh-keygen. Is OpenSSH installed properly?"; exit 1)
+
+error () {
+    echo "$1"
+    exit 1
+}
+
+[ $(which ssh-keygen) ] || \
+    error "Can't find ssh-keygen. Is OpenSSH installed properly?"
+
 local key_types="dsa ecdsa ed25519 rsa"
 
 usage () {
@@ -13,17 +20,44 @@ init () {
     ssh-keygen -qf CA -P "" -C ssh-ca
 }
 
+is_initialized () {
+    if [ ! -r "CA" ] || [ ! -r "CA.pub" ] || [ -d "users" ] || [ -d "hosts" ]
+    then
+        error "Something seems wrong. Did you run $0 init?"
+    fi
+}
+
 signuser () {
-    echo "Signing user $1 key."
-    ssh-keygen -s CA -I "$1" -n "$1" "users/$1.pub"
+    [ -z "$1" ] && error "You must specify username."
+    for type in $key_types
+    do
+        if [ -r "users/$1/id_${type}.pub" ]
+        then
+            echo "Signing user $1 $type key."
+            ssh-keygen -s CA -I "$1" -n "$1" "users/$1/id_${type}.pub"
+            local flag="not empty"
+        fi
+    done
+    [ -z "$flag" ] && echo "Didn't find any public keys for $1."
 }
 
 signhost () {
-    echo "Signing host $1 key."
-    ssh-keygen -s CA -I "$1" -h -n "$1" "hosts/$1.pub"
+    [ -z "$1" ] && error "You must specify hostname."
+    for type in $key_types
+    do
+        if [ -r "hosts/$1/ssh_host_${type}_key.pub" ]
+        then
+            echo "Signing host $1 $type key."
+            ssh-keygen -s CA -I "$1" -h -n "$1" \
+                "hosts/$1/ssh_host_${type}_key.pub"
+            local flag="not empty"
+        fi
+    done
+    [ -z "$flag" ] && echo "Didn't find any public keys for $1."
 }
 
 newhost () {
+    [ -z "$1" ] && error "You must specify hostname."
     echo "Creating new host $1 keypair."
     mkdir -p "hosts/$1"
     for type in "$key_types"
@@ -34,6 +68,7 @@ newhost () {
 }
 
 newuser () {
+    [ -z "$1" ] && error "You must specify username."
     echo "Creating new user $1 keypair."
     mkdir -p "users/$1"
     for type in "$key_types"
@@ -54,15 +89,19 @@ case "$1" in
         init
         ;;
     signuser)
+        is_initialized
         signuser "$2"
         ;;
     signhost)
+        is_initialized
         signhost "$2"
         ;;
     newhost)
+        is_initialized
         newhost "$2"
         ;;
     newuser)
+        is_initialized
         newuser "$2"
         ;;
     *)
-- 
GitLab