17c478bd9Sstevel@tonic-gate#!/sbin/sh 27c478bd9Sstevel@tonic-gate# 3*ead1f93eSLiane 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 8*ead1f93eSLiane 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 22*ead1f93eSLiane Praza # 23*ead1f93eSLiane Praza # HostKey keywords in sshd_config may be preceded or 24*ead1f93eSLiane Praza # followed by a mix of any number of space or tabs, 25*ead1f93eSLiane Praza # and optionally have an = between keyword and 26*ead1f93eSLiane Praza # argument. We use two grep invocations such that we 27*ead1f93eSLiane Praza # can match HostKey case insensitively but still have 28*ead1f93eSLiane Praza # the case of the path name be significant, keeping 29*ead1f93eSLiane Praza # the pattern somewhat more readable. 30*ead1f93eSLiane Praza # 31*ead1f93eSLiane Praza # The character classes below contain one literal 32*ead1f93eSLiane Praza # space and one literal tab. 33*ead1f93eSLiane Praza # 34*ead1f93eSLiane Praza grep -i "^[ ]*HostKey[ ]*=\{0,1\}[ ]*$keypath" \ 35*ead1f93eSLiane Praza $SSHDIR/sshd_config | grep "$keypath" > /dev/null 2>&1 36*ead1f93eSLiane 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 '' 40*ead1f93eSLiane Praza if [ $? -ne 0 ]; then 41*ead1f93eSLiane Praza echo "Could not create $keytype key: $keypath" 42*ead1f93eSLiane Praza exit $SMF_EXIT_ERR_CONFIG 437c478bd9Sstevel@tonic-gate fi 447c478bd9Sstevel@tonic-gate fi 45*ead1f93eSLiane 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') 767c478bd9Sstevel@tonic-gate create_key $SSHDIR/ssh_host_rsa_key rsa 777c478bd9Sstevel@tonic-gate create_key $SSHDIR/ssh_host_dsa_key dsa 787c478bd9Sstevel@tonic-gate ;; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate'-u') 817c478bd9Sstevel@tonic-gate # sys-unconfig(1M) knows how to remove ssh host keys, so there's 827c478bd9Sstevel@tonic-gate # nothing to do here. 837c478bd9Sstevel@tonic-gate : 847c478bd9Sstevel@tonic-gate ;; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate # SMF arguments (start and restart [really "refresh"]) 87eb1a3463STruong Nguyen 88eb1a3463STruong Nguyen'ipfilter') 89eb1a3463STruong Nguyen create_ipf_rules $2 90eb1a3463STruong Nguyen ;; 91eb1a3463STruong Nguyen 927c478bd9Sstevel@tonic-gate'start') 93*ead1f93eSLiane Praza # 94*ead1f93eSLiane Praza # If host keys don't exist when the service is started, create 95*ead1f93eSLiane Praza # them; sysidconfig is not run in every situation (such as on 96*ead1f93eSLiane Praza # the install media). 97*ead1f93eSLiane Praza # 98*ead1f93eSLiane Praza create_key $SSHDIR/ssh_host_rsa_key rsa 99*ead1f93eSLiane Praza create_key $SSHDIR/ssh_host_dsa_key dsa 100*ead1f93eSLiane Praza 1017c478bd9Sstevel@tonic-gate /usr/lib/ssh/sshd 1027c478bd9Sstevel@tonic-gate ;; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate'restart') 1057c478bd9Sstevel@tonic-gate if [ -f "$PIDFILE" ]; then 1067c478bd9Sstevel@tonic-gate /usr/bin/kill -HUP `/usr/bin/cat $PIDFILE` 1077c478bd9Sstevel@tonic-gate fi 1087c478bd9Sstevel@tonic-gate ;; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate*) 1117c478bd9Sstevel@tonic-gate echo "Usage: $0 { start | restart }" 1127c478bd9Sstevel@tonic-gate exit 1 1137c478bd9Sstevel@tonic-gate ;; 1147c478bd9Sstevel@tonic-gateesac 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gateexit $? 117