| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # Author: Steven Shiau <steven _at_ clonezilla org> |
| 3 | # License: GPL |
| 4 | # Description: This program is modified from Fedora Core 4 Init file for OpenSSH server daemon (/etc/init.d/sshd), so that it can generate the host key for DRBL Debian clients. |
| 5 | # |
| 6 | # config: /etc/ssh/ssh_host_key |
| 7 | # config: /etc/ssh/ssh_host_key.pub |
| 8 | # config: /etc/ssh/ssh_random_seed |
| 9 | # config: /etc/ssh/sshd_config |
| 10 | |
| 11 | # Load DRBL setting and functions |
| 12 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 13 | |
| 14 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 15 | |
| 16 | # |
| 17 | KEYGEN=/usr/bin/ssh-keygenscip-shell filesystem . . `/usr/bin/ssh-keygen`. |
| 18 | |
| 19 | export LC_ALL=C |
| 20 | |
| 21 | Usagescip-shell shell shell-project 0.0.0 Usage().() { |
| 22 | echoscip-shell system . . echo. "Usage: $0 [OPTION] {generate|clean|regenerate} Client_IP_LIST" |
| 23 | echoscip-shell system . . echo. "Options:" |
| 24 | echoscip-shell system . . echo. "-v, --verbose: verbose mode." |
| 25 | echoscip-shell system . . echo. "Ex: "$0 generate 192.168.0.1" will generate the OpenSSH host keys for DRBL client 192.168.0.1." |
| 26 | } |
| 27 | |
| 28 | do_rsa1_keygenscip-shell shell shell-project 0.0.0 do_rsa1_keygen().() { |
| 29 | if [scip-shell system . . `[`. ! -s $RSA1_KEY ]; then |
| 30 | [scip-shell system . . `[`. -n "$verbose" ] && echoscip-shell system . . echo. -n "Generating SSH1 RSA host key: " |
| 31 | if $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then |
| 32 | chmodscip-shell system . . chmod. 600 $RSA1_KEY |
| 33 | chmodscip-shell system . . chmod. 644 $RSA1_KEY.pub |
| 34 | [scip-shell system . . `[`. -n "$verbose" ] && echoscip-shell system . . echo. "RSA1 key generation done!" |
| 35 | else |
| 36 | echoscip-shell system . . echo. "RSA1 key generation failed!" |
| 37 | exit 1 |
| 38 | fi |
| 39 | fi |
| 40 | } |
| 41 | |
| 42 | do_rsa_keygenscip-shell shell shell-project 0.0.0 do_rsa_keygen().() { |
| 43 | if [scip-shell system . . `[`. ! -s $RSA_KEY ]; then |
| 44 | [scip-shell system . . `[`. -n "$verbose" ] && echoscip-shell system . . echo. -n "Generating SSH2 RSA host key: " |
| 45 | if $KEYGEN -q -t rsa -f $RSA_KEY -C '' -N '' >&/dev/null; then |
| 46 | chmodscip-shell system . . chmod. 600 $RSA_KEY |
| 47 | chmodscip-shell system . . chmod. 644 $RSA_KEY.pub |
| 48 | [scip-shell system . . `[`. -n "$verbose" ] && echoscip-shell system . . echo. "RSA key generation done!" |
| 49 | else |
| 50 | echoscip-shell system . . echo. "RSA key generation failed" |
| 51 | exit 1 |
| 52 | fi |
| 53 | fi |
| 54 | } |
| 55 | |
| 56 | do_dsa_keygenscip-shell shell shell-project 0.0.0 do_dsa_keygen().() { |
| 57 | if [scip-shell system . . `[`. ! -s $DSA_KEY ]; then |
| 58 | [scip-shell system . . `[`. -n "$verbose" ] && echoscip-shell system . . echo. -n "Generating SSH2 DSA host key: " |
| 59 | if $KEYGEN -q -t dsa -f $DSA_KEY -C '' -N '' >&/dev/null; then |
| 60 | chmodscip-shell system . . chmod. 600 $DSA_KEY |
| 61 | chmodscip-shell system . . chmod. 644 $DSA_KEY.pub |
| 62 | [scip-shell system . . `[`. -n "$verbose" ] && echoscip-shell system . . echo. "DSA key generation done!" |
| 63 | else |
| 64 | echoscip-shell system . . echo. "DSA key generation failed!" |
| 65 | exit 1 |
| 66 | fi |
| 67 | fi |
| 68 | } |
| 69 | |
| 70 | generatescip-shell shell shell-project 0.0.0 generate().() { |
| 71 | # Create keys if necessary |
| 72 | echoscip-shell system . . echo. -n "Generating SSH host keys for client $HOST_IP if they do not exist... " |
| 73 | if [scip-shell system . . `[`. -e /etc/ssh/ssh_host_keyscip-shell filesystem . . `/etc/ssh/ssh_host_key`. ]; then |
| 74 | # RSA1 exists on server, so create it in the clients, too. For newer GNU/Linux, RSA1 is no more. |
| 75 | do_rsa1_keygenscip-shell shell shell-project 0.0.0 do_rsa1_keygen(). |
| 76 | fi |
| 77 | do_rsa_keygenscip-shell shell shell-project 0.0.0 do_rsa_keygen(). |
| 78 | do_dsa_keygenscip-shell shell shell-project 0.0.0 do_dsa_keygen(). |
| 79 | echoscip-shell system . . echo. "done!" |
| 80 | } |
| 81 | |
| 82 | cleanscip-shell shell shell-project 0.0.0 clean().() { |
| 83 | echoscip-shell system . . echo. -n "Cleaning SSH host keys for client $HOST_IP..." |
| 84 | [scip-shell system . . `[`. -n "$verbose" ] && vop="-v" |
| 85 | [scip-shell system . . `[`. -f "$RSA1_KEY" ] && rmscip-shell system . . rm. -f $vop $RSA1_KEY |
| 86 | [scip-shell system . . `[`. -f "$RSA1_KEY.pub" ] && rmscip-shell system . . rm. -f $vop $RSA1_KEY.pub |
| 87 | [scip-shell system . . `[`. -f "$RSA_KEY" ] && rmscip-shell system . . rm. -f $vop $RSA_KEY |
| 88 | [scip-shell system . . `[`. -f "$RSA_KEY.pub" ] && rmscip-shell system . . rm. -f $vop $RSA_KEY.pub |
| 89 | [scip-shell system . . `[`. -f "$DSA_KEY" ] && rmscip-shell system . . rm. -f $vop $DSA_KEY |
| 90 | [scip-shell system . . `[`. -f "$DSA_KEY.pub" ] && rmscip-shell system . . rm. -f $vop $DSA_KEY.pub |
| 91 | echoscip-shell system . . echo. "done!" |
| 92 | } |
| 93 | |
| 94 | # |
| 95 | check_if_root |
| 96 | |
| 97 | # Parse command-line options |
| 98 | while [scip-shell system . . `[`. $# -gt 0 ]; do |
| 99 | case "$1" in |
| 100 | -v|--verbose) |
| 101 | shift; verbose="on" |
| 102 | ;; |
| 103 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 104 | Usagescip-shell shell shell-project 0.0.0 Usage(). >& 2 |
| 105 | exit 2 ;; |
| 106 | *) break ;; |
| 107 | esac |
| 108 | done |
| 109 | # |
| 110 | mode=$1 |
| 111 | shift |
| 112 | IP_LIST=$* |
| 113 | |
| 114 | [scip-shell system . . `[`. -z "$IP_LIST" ] && Usagescip-shell shell shell-project 0.0.0 Usage(). && exit 1 |
| 115 | # |
| 116 | for HOST_IP in $IP_LIST; do |
| 117 | [scip-shell system . . `[`. ! -d "$drblroot/$HOST_IP/etc/ssh" ] && echoscip-shell system . . echo. "No directory $drblroot/$HOST_IP/etc/ssh!!! Skip this client!!!" |
| 118 | |
| 119 | RETVAL=0 |
| 120 | |
| 121 | # Some functions to make the below more readable |
| 122 | RSA1_KEY="$drblroot/$HOST_IP/etc/ssh/ssh_host_key" |
| 123 | RSA_KEY="$drblroot/$HOST_IP/etc/ssh/ssh_host_rsa_key" |
| 124 | DSA_KEY="$drblroot/$HOST_IP/etc/ssh/ssh_host_dsa_key" |
| 125 | |
| 126 | case "$mode" in |
| 127 | generate) |
| 128 | generatescip-shell shell shell-project 0.0.0 generate(). |
| 129 | ;; |
| 130 | clean) |
| 131 | cleanscip-shell shell shell-project 0.0.0 clean(). |
| 132 | ;; |
| 133 | regenerate) |
| 134 | cleanscip-shell shell shell-project 0.0.0 clean(). |
| 135 | generatescip-shell shell shell-project 0.0.0 generate(). |
| 136 | ;; |
| 137 | *) |
| 138 | Usagescip-shell shell shell-project 0.0.0 Usage(). |
| 139 | RETVAL=1 |
| 140 | esac |
| 141 | done |
| 142 | exit $RETVAL |