17c478bd9Sstevel@tonic-gate#!/sbin/sh 27c478bd9Sstevel@tonic-gate# 3ead1f93eSLiane Praza# Copyright 2010 Sun Microsystems, Inc. All rights reserved. 47c478bd9Sstevel@tonic-gate# Use is subject to license terms. 57c478bd9Sstevel@tonic-gate# 6eb1a3463STruong Nguyen 7eb1a3463STruong Nguyen. /lib/svc/share/ipf_include.sh 8ead1f93eSLiane Praza. /lib/svc/share/smf_include.sh 97c478bd9Sstevel@tonic-gate 107c478bd9Sstevel@tonic-gateSSHDIR=/etc/ssh 117c478bd9Sstevel@tonic-gateKEYGEN="/usr/bin/ssh-keygen -q" 127c478bd9Sstevel@tonic-gatePIDFILE=/var/run/sshd.pid 137c478bd9Sstevel@tonic-gate 147c478bd9Sstevel@tonic-gate# Checks to see if RSA, and DSA host keys are available 157c478bd9Sstevel@tonic-gate# if any of these keys are not present, the respective keys are created. 167c478bd9Sstevel@tonic-gatecreate_key() 177c478bd9Sstevel@tonic-gate{ 187c478bd9Sstevel@tonic-gate keypath=$1 197c478bd9Sstevel@tonic-gate keytype=$2 207c478bd9Sstevel@tonic-gate 217c478bd9Sstevel@tonic-gate if [ ! -f $keypath ]; then 22ead1f93eSLiane Praza # 23ead1f93eSLiane Praza # HostKey keywords in sshd_config may be preceded or 24ead1f93eSLiane Praza # followed by a mix of any number of space or tabs, 25ead1f93eSLiane Praza # and optionally have an = between keyword and 26ead1f93eSLiane Praza # argument. We use two grep invocations such that we 27ead1f93eSLiane Praza # can match HostKey case insensitively but still have 28ead1f93eSLiane Praza # the case of the path name be significant, keeping 29ead1f93eSLiane Praza # the pattern somewhat more readable. 30ead1f93eSLiane Praza # 31ead1f93eSLiane Praza # The character classes below contain one literal 32ead1f93eSLiane Praza # space and one literal tab. 33ead1f93eSLiane Praza # 34ead1f93eSLiane Praza grep -i "^[ ]*HostKey[ ]*=\{0,1\}[ ]*$keypath" \ 35ead1f93eSLiane Praza $SSHDIR/sshd_config | grep "$keypath" > /dev/null 2>&1 36ead1f93eSLiane Praza 377c478bd9Sstevel@tonic-gate if [ $? -eq 0 ]; then 387c478bd9Sstevel@tonic-gate echo Creating new $keytype public/private host key pair 397c478bd9Sstevel@tonic-gate $KEYGEN -f $keypath -t $keytype -N '' 40ead1f93eSLiane Praza if [ $? -ne 0 ]; then 41ead1f93eSLiane Praza echo "Could not create $keytype key: $keypath" 42ead1f93eSLiane Praza exit $SMF_EXIT_ERR_CONFIG 437c478bd9Sstevel@tonic-gate fi 447c478bd9Sstevel@tonic-gate fi 45ead1f93eSLiane Praza fi 467c478bd9Sstevel@tonic-gate} 477c478bd9Sstevel@tonic-gate 48eb1a3463STruong Nguyencreate_ipf_rules() 49eb1a3463STruong Nguyen{ 50eb1a3463STruong Nguyen FMRI=$1 51eb1a3463STruong Nguyen ipf_file=`fmri_to_file ${FMRI} $IPF_SUFFIX` 52eb1a3463STruong Nguyen policy=`get_policy ${FMRI}` 53eb1a3463STruong Nguyen 54eb1a3463STruong Nguyen # 55eb1a3463STruong Nguyen # Get port from /etc/ssh/sshd_config 56eb1a3463STruong Nguyen # 57eb1a3463STruong Nguyen tports=`grep "^Port" /etc/ssh/sshd_config 2>/dev/null | \ 58eb1a3463STruong Nguyen awk '{print $2}'` 59eb1a3463STruong Nguyen 60eb1a3463STruong Nguyen echo "# $FMRI" >$ipf_file 61eb1a3463STruong Nguyen for port in $tports; do 62eb1a3463STruong Nguyen generate_rules $FMRI $policy "tcp" "any" $port $ipf_file 63eb1a3463STruong Nguyen done 64eb1a3463STruong Nguyen} 65eb1a3463STruong Nguyen 667c478bd9Sstevel@tonic-gate# This script is being used for two purposes: as part of an SMF 677c478bd9Sstevel@tonic-gate# start/stop/refresh method, and as a sysidconfig(1M)/sys-unconfig(1M) 687c478bd9Sstevel@tonic-gate# application. 697c478bd9Sstevel@tonic-gate# 707c478bd9Sstevel@tonic-gate# Both, the SMF methods and sysidconfig/sys-unconfig use different 717c478bd9Sstevel@tonic-gate# arguments.. 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gatecase $1 in 747c478bd9Sstevel@tonic-gate # sysidconfig/sys-unconfig arguments (-c and -u) 757c478bd9Sstevel@tonic-gate'-c') 76*75614fd9SAlexander Pyhalov /usr/bin/ssh-keygen -A 77*75614fd9SAlexander Pyhalov if [ $? -ne 0 ]; then 787c478bd9Sstevel@tonic-gate create_key $SSHDIR/ssh_host_rsa_key rsa 797c478bd9Sstevel@tonic-gate create_key $SSHDIR/ssh_host_dsa_key dsa 80*75614fd9SAlexander Pyhalov fi 817c478bd9Sstevel@tonic-gate ;; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate'-u') 847c478bd9Sstevel@tonic-gate # sys-unconfig(1M) knows how to remove ssh host keys, so there's 857c478bd9Sstevel@tonic-gate # nothing to do here. 867c478bd9Sstevel@tonic-gate : 877c478bd9Sstevel@tonic-gate ;; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate # SMF arguments (start and restart [really "refresh"]) 90eb1a3463STruong Nguyen 91eb1a3463STruong Nguyen'ipfilter') 92eb1a3463STruong Nguyen create_ipf_rules $2 93eb1a3463STruong Nguyen ;; 94eb1a3463STruong Nguyen 957c478bd9Sstevel@tonic-gate'start') 96ead1f93eSLiane Praza # 97ead1f93eSLiane Praza # If host keys don't exist when the service is started, create 98ead1f93eSLiane Praza # them; sysidconfig is not run in every situation (such as on 99ead1f93eSLiane Praza # the install media). 100ead1f93eSLiane Praza # 101*75614fd9SAlexander Pyhalov /usr/bin/ssh-keygen -A 102*75614fd9SAlexander Pyhalov if [ $? -ne 0 ]; then 103ead1f93eSLiane Praza create_key $SSHDIR/ssh_host_rsa_key rsa 104ead1f93eSLiane Praza create_key $SSHDIR/ssh_host_dsa_key dsa 105*75614fd9SAlexander Pyhalov fi 106ead1f93eSLiane Praza 1077c478bd9Sstevel@tonic-gate /usr/lib/ssh/sshd 1087c478bd9Sstevel@tonic-gate ;; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate'restart') 1117c478bd9Sstevel@tonic-gate if [ -f "$PIDFILE" ]; then 1127c478bd9Sstevel@tonic-gate /usr/bin/kill -HUP `/usr/bin/cat $PIDFILE` 1137c478bd9Sstevel@tonic-gate fi 1147c478bd9Sstevel@tonic-gate ;; 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate*) 1177c478bd9Sstevel@tonic-gate echo "Usage: $0 { start | restart }" 1187c478bd9Sstevel@tonic-gate exit 1 1197c478bd9Sstevel@tonic-gate ;; 1207c478bd9Sstevel@tonic-gateesac 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gateexit $? 123