#!/bin/ksh93 # # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License (the "License"). # You may not use this file except in compliance with the License. # # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at usr/src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # Copyright 2009 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # # Portions Copyright 2009, 2014, Jens Elkner. DEFAULT_FILE="/etc/default/sendmail" SENDMAIL="/usr/lib/sendmail" PATH="/usr/bin:/usr/sbin" export PATH # trailing slash is important here! CF_BASEDIR=/usr/lib/mail/ # Actually the following 3 function should go into /lib/svc/share/smf_include.sh # since almost every svc method script needs them ... # Get a single value property. # $1 .. Name of the property to lookup # $2 .. vName, where to store the unescaped value # $3 .. Optional: SMF_FMRI to use (default: ${SMF_FMRI}) # return: 0 .. non-empty value found; 1 .. prop does not exist or is empty # 2 .. missing FMRI 3 .. usage error function getSvcProp { if [[ -z $1 || -z $2 ]]; then print -u2 "getSvcProp() requires 2 arguments" return 3 fi typeset -n VAL=$2 typeset FMRI=$3 if [[ -z ${FMRI} ]]; then [[ -n ${SMF_FMRI} ]] && FMRI=${SMF_FMRI} || { VAL='' ; return 2 ; } fi VAL="${ svcprop -p $1 ${FMRI} 2>/dev/null ; }" [[ ${VAL} == '""' ]] && { VAL='' ; return 1 ; } # unescape VAL="${VAL//\\(['"''`';&()|^<>'\n''\t''\'\047])/\1}" # \047 .. squote return 0 } # Get a multi-valued property. # $1 .. Name of the property to lookup # $2 .. vName of the array, where to append the unescaped values # $3 .. Optional: SMF_FMRI to use (default: ${SMF_FMRI}) # return: 0 .. non-empty value found; 1 .. prop does not exist or is empty # 2 .. missing FMRI 3 .. usage error function getSvcProps { if [[ -z $1 || -z $2 ]]; then print -u2 "getSvcProp() requires 2 arguments" return 3 fi typeset -n VAL=$2 typeset FMRI=$3 X if [[ -z ${FMRI} ]]; then [[ -n ${SMF_FMRI} ]] && FMRI=${SMF_FMRI} || return 2 fi typeset V="${ svcprop -p $1 ${FMRI} 2>/dev/null ; }" X [[ $V == '""' ]] && return 1 # svcprop emits a multi-valued prop by separating each value with a ' ' # Therefore ' ' within a single value are escaped with a '\'. However, # since the shell splits on ' ' we need to re-concatenate the adjacent # parts. integer I=${#VAL[@]} CONCAT=0 COUNT=I for X in $V ; do # is escaped, so no harm if [[ ${X: -1:1} == '\' ]]; then if (( CONCAT )); then VAL[I]+=" ${X:0:${#X}-1}" else VAL+=( "${X:0:${#X}-1}" ) (( I++ )) CONCAT=1 fi else VAL+=( "$X" ) (( I++ )) CONCAT=0 fi done # finally unescape - we use X instead of VAL[I] to keep vim highlight happy for (( ; I > COUNT; I-- )); do X="${VAL[I]}" X="${X//\\(['"''`';&()|^<>'\n''\t''\'\047])/\1}" VAL[I]="$X" done return 0 } # $1 .. pid file to use for killing a corresponding process # $2 .. optional SIGNAL to send (no leading dash!) function check_and_kill { [[ -z $1 || ! -f $1 ]] && return typeset PID=$(<$1) PID=${PID%%$'\n'*} [[ -z ${PID} ]] && return kill -0 ${PID} > /dev/null 2>&1 # check, whether the process exists (( ! $? )) && kill -${2:-TERM} ${PID} } # $1 .. function exist_or_exit { [[ -n $1 && -f $1 ]] && return print -u2 "file '$1' does not exist" exit ${SMF_EXIT_ERR_CONFIG} } # $1 .. Interval to check # $2 .. Optional: Name of the variable, where to store the answer as well. # Sets the Variable 'answer' to $1 if it is a valid value, otherwise to a # default of '15m'. function check_queue_interval_syntax { [[ $1 =~ ^([0-9]*[1-9][0-9]*[smhdw])+$ ]] && answer=$1 || answer='15m' # this is the right way to do it (i.e. not using global vars) if [[ -n $2 ]]; then typeset -n VAL=$2 VAL=${answer} fi } # $1 .. path name of the *.cf file, where to store the generated output # $2 .. absolute path name of the m4 aka *.mc file, from which to read the input function turn_m4_crank { (( $# < 2 )) && return typeset CF="$1" MC="$2" if [[ ${MC} == '_DONT_TOUCH_THIS' ]]; then if [[ -f ${CF}.old ]]; then mv "${CF}" "${CF}.new" || exit ${SMF_EXIT_ERR_CONFIG} mv "${CF}.old" "$CF" || exit ${SMF_EXIT_ERR_CONFIG} fi # If ${CF}.old does not exist, assume it was taken care # of on a previous run. return elif [[ ${MC:0:1} != '/' ]]; then # absolute path name required return fi exist_or_exit "${MC}" # If CF exists and has the same timestamp as MC, there is nothing to do [[ ${CF} -nt ${MC} || ${CF} -ot ${MC} ]] || return # just in case MC tries to include files with a relative path cd "${MC%/*}" || print -u2 "Warning: Can't change directory to '${MC%/*}'" \ '(this may produce unexpected results)' # create the new CF typeset PROP='' PARAM='' getSvcProp config/include_info PROP [[ ${PROP} != 'true' ]] && PARAM='-DSUN_HIDE_INTERNAL_DETAILS' m4 -D_CF_DIR_="${CF_BASEDIR}" ${PARAM} "${CF_BASEDIR}m4/cf.m4" "${MC}" \ > "${CF}.new" # if failed or m4 did not output anything, don't replace the current CF # we don't remove screwed output to allow an admin to find the root cause # a little bit easier. [[ $? != 0 || ! -s "${CF}.new" ]] && exit ${SMF_EXIT_ERR_CONFIG} cmp -s "${CF}.new" "$CF" || ( chown root:bin "${CF}.new" && chmod 444 "${CF}.new" && mv "${CF}.new" "$CF" ) (( $? )) && exit ${SMF_EXIT_ERR_CONFIG} # finally make sure both files have the same timestamp touch -r "${MC}" "${CF}" }