#!/bin/sh # # z/VM IUCV HVC device driver -- Edit z/VM user ID filter # # Copyright IBM Corp. 2009, 2017 # # s390-tools is free software; you can redistribute it and/or modify # it under the terms of the MIT license. See LICENSE for details. # PRG=`basename $0` # sysfs file that contains the z/VM user ID filter for the # z/VM IUCV HVC device driver sysfs_hvc_iucv_allow="/sys/module/kernel/parameters/hvc_iucv_allow" # older kernel versions do not have the "kernel" directory test -f $sysfs_hvc_iucv_allow || \ sysfs_hvc_iucv_allow="/sys/module/hvc_iucv/parameters/hvc_iucv_allow" show_help(){ cat <<"EoHelp" Usage: chiucvallow [-h|--help] [-v|--version] chiucvallow -l|--list chiucvallow -c|--clear chiucvallow -e|--edit [] chiucvallow -s|--set chiucvallow -V|--verify Options: -h|--help Print help information, then exit. -v|--version Print version information, then exit. -l|--list List current z/VM user ID filter. -c|--clear Clear the z/VM user ID filter. -e|--edit [] Edit the z/VM user ID filter. -s|--set Set the z/VM user ID filter from a filter file. -V|--verify Verify the z/VM user ID filter file. The list, edit, set and clear options require root authority. EoHelp } show_version(){ cat <&2 exit ${2:-1} } hvciucv_available(){ test -r "$sysfs_hvc_iucv_allow" \ && cat "$sysfs_hvc_iucv_allow" >/dev/null 2>&1 if test $? -gt 0; then cat >&2 </dev/null |wc -c` if test "$fsize" -gt 4095; then printf "$PRG: The z/VM user ID filter exceeds the maximum size (%d of %d bytes)\n" \ $fsize 4095 >&2 return 1 fi while read userid; do # skip empty lines and lines starting with '#' echo "$userid" |grep -q -E '^(\s*$|#)' && continue printf "Verify z/VM user ID: %-8s : " "$userid" if echo -n "$userid" |grep -q -E -i "$regex"; then printf "OK\n" else printf "FAILED\n" failed=$((failed + 1)) fi count=$((count + 1)) done <$filename test $count -gt 500 && \ error "The z/VM user ID filter exceeds the maximum of 500 user IDs" printf "\n$PRG: Verification summary: verified=%d failed=%d size=%d bytes\n" \ $count $failed $fsize test $failed -eq 0 || return 2 } # # Edit the z/VM user ID filter edit_filter(){ local fromfile="$1" local context=$2 local tmpfile=`mktemp /tmp/hvc_iucv_allow.XXXXXX` local md5file=`mktemp /tmp/hvc_iucv_allow.md5.XXXXXX` if test -w $tmpfile && test -w $md5file; then :; else error "Creating temporary files failed" fi # save list in temp file if test -r "$fromfile"; then cat $fromfile; else list_filter; fi > $tmpfile # check whether to open editor if test "x$context" != xnoeditor; then md5sum $tmpfile > $md5file # save checksum to track changes ${EDITOR:-vi} $tmpfile # open editor if md5sum --status -c $md5file; then cat </dev/null; then :; else cat >&2 < $sysfs_hvc_iucv_allow if test x$? != x0; then cat >&2 </dev/null exit 0 } # # Clear z/VM user ID filter clear_filter(){ echo > $sysfs_hvc_iucv_allow } # # Check whether we run as root, otherwise complain and exit for_root_only(){ local euid=`id -u 2>/dev/null` test "x$euid" = x0 && return error "You need root authority to use option '$1'" } lock_operation(){ ( flock -nx 9 \ || error "The filter is currently being changed. Try again later." $@ ) 9>/var/lock/hvc_iucv_allow } # Common options case $1 in -h|--help) show_help exit 0 ;; -v|--version) show_version exit 0 ;; esac # check the name under which we have been called case `basename $0` in lsiucvallow) exec chiucvallow --list ;; esac # chiucvallow program options case $1 in -l|--list) for_root_only "$1" hvciucv_available list_filter ;; -e|--edit) for_root_only "$1" hvciucv_available lock_operation edit_filter "$2" ;; -c|--clear) for_root_only "$1" hvciucv_available lock_operation clear_filter ;; -s|--set) for_root_only "$1" test -n "$2" \ || error "This option requires a file as argument" test -r "$2" \ || error "The specified file must be readable" hvciucv_available lock_operation edit_filter "$2" noeditor ;; -V|--verify) test -n "$2" \ || error "This option requires a file as argument" verify_filter "$2" exit $? ;; '') echo "$PRG: One or more arguments are missing" >&2 echo "Try '$0 --help' for more information." >&2 exit 201 ;; *) echo "$PRG: Invalid option -- '$1'" >&2 echo "Try '$0 --help' for more information." >&2 exit 201 ;; esac exit 0