#!/usr/bin/env bash # Test matching of Unix peer credentials with NACM users # Use raw unix socket instead of clients (cli/netconf/restconf) since they do # magic things with the username and here it needs to be handled explicitly. # test matrix: # - mode: none, exact, except # - username: olof, admin, null, sudo # - socket family: unix|ip # Magic line must be first in script (see README.md) s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi # Raw unit tester of backend unix socket. : ${clixon_util_socket:=$(which clixon_util_socket)} APPNAME=example # Common NACM scripts . ./nacm.sh cfg=$dir/conf_yang.xml fyang=$dir/nacm-example.yang cat < $fyang module nacm-example{ yang-version 1.1; namespace "urn:example:nacm"; prefix nex; import ietf-netconf-acm { prefix nacm; } leaf x{ type int32; description "something to edit"; } } EOF # The groups are slightly modified from RFC8341 A.1 # The rule-list is from A.2 RULES=$(cat < false permit deny deny $NGROUPS guest-acl guest deny-ncm * * deny Do not allow guests any access to the NETCONF monitoring information. limited-acl limited permit-get get * exec permit Allow get permit-get-config get-config * exec permit Allow get-config $NADMIN 0 EOF ) # Set cred mode and run nacm operations # Arguments: # - mode (none,exact,except) # - xml/nacm-username # - socket family # - socket file/addr # - precommand /(eg sudo to raise to root) function testrun(){ mode=$1 username=$2 family=$3 sock=$4 exp=$5 precmd=$6 cat < $cfg $cfg ${YANG_INSTALLDIR} $fyang $family $sock /usr/local/lib/$APPNAME/backend /usr/local/var/$APPNAME/$APPNAME.pidfile /usr/local/var/$APPNAME internal true $mode EOF new "test params: -f $cfg" if [ $BE -ne 0 ]; then new "kill old backend" sudo clixon_backend -zf $cfg if [ $? -ne 0 ]; then err fi new "start backend -s init -f $cfg" start_backend -s init -f $cfg fi new "wait backend" wait_backend # First push in nacm rules via regular means new "auth set authentication config" expecteof_netconf "$clixon_netconf -qf $cfg" 0 "$DEFAULTHELLO" "$RULES" "" "" new "enable nacm" expecteof_netconf "$clixon_netconf -qf $cfg" 0 "$DEFAULTHELLO" "true" "" "" new "commit it" expecteof_netconf "$clixon_netconf -qf $cfg" 0 "$DEFAULTHELLO" "" "" "" # raw socket test if [ -n "$username" ]; then XML="" else XML="" fi new "get-config mode:$mode user:$username $family $precmd" expecteof_netconf "$precmd $clixon_util_socket -a $family -s $sock -D $DBG" 0 "" "$XML" "$ex" if [ $BE -ne 0 ]; then # Bring your own backend new "Kill backend" # Check if premature kill pid=$(pgrep -u root -f clixon_backend) if [ -z "$pid" ]; then err "backend already dead" fi # kill backend stop_backend -f $cfg fi } # testrun OK='^0$' ERROR='^applicationaccess-deniederror' # UNIX socket, no user, loop mode. All fail since null user cant access anything new "Credentials: mode=none, fam=UNIX user=none" testrun none "" UNIX $dir/backend.sock "$OK" "" new "Credentials: mode=exact, fam=UNIX user=none" testrun exact "" UNIX $dir/backend.sock "$OK" "" new "Credentials: mode=except, fam=UNIX user=none" testrun except "" UNIX $dir/backend.sock "$OK" "" # UNIX socket, myuser, loop mode. All should work new "Credentials: mode=none, fam=UNIX user=me" testrun none "$USER" UNIX $dir/backend.sock "$OK" "" new "Credentials: mode=exact, fam=UNIX user=me" testrun exact "$USER" UNIX $dir/backend.sock "$OK" "" new "Credentials: mode=except, fam=UNIX user=me" testrun except "$USER" UNIX $dir/backend.sock "$OK" "" # UNIX socket, admin user. First should work new "Credentials: mode=none, fam=UNIX user=admin" testrun none admin UNIX $dir/backend.sock "$OK" "" new "Credentials: mode=exact, fam=UNIX user=admin" testrun exact admin UNIX $dir/backend.sock "$ERROR" "" new "Credentials: mode=except, fam=UNIX user=admin" testrun except admin UNIX $dir/backend.sock "$ERROR" "" # UNIX socket, admin user. sudo self to root. First and last should work new "Credentials: mode=none, fam=UNIX user=admin sudo" testrun none admin UNIX $dir/backend.sock "$OK" sudo new "Credentials: mode=exact, fam=UNIX user=admin sudo" testrun exact admin UNIX $dir/backend.sock "$ERROR" sudo new "Credentials: mode=except, fam=UNIX user=admin sudo" testrun except admin UNIX $dir/backend.sock "$OK" sudo # IPv4 socket, admin user. First should work new "Credentials: mode=none, fam=UNIX user=admin sudo" testrun none $USER IPv4 127.0.0.1 "$OK" "" new "Credentials: mode=exact, fam=UNIX user=admin sudo" testrun exact $USER IPv4 127.0.0.1 "$ERROR" "" new "Credentials: mode=except, fam=UNIX user=admin sudo" testrun except $USER IPv4 127.0.0.1 "$ERROR" "" rm -rf $dir new "endtest" endtest