blob: 2d811c98821989a1833eca5570cd448473811123 [file] [log] [blame]
Dan Walshe5a81c72012-03-06 10:45:29 -05001# This file is part of systemd.
2#
Dan Walsh1886d462013-10-11 08:39:07 -04003# Copyright 2011-2013 Dan Walsh
Dan Walshe5a81c72012-03-06 10:45:29 -05004#
5# systemd is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# systemd is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with systemd; If not, see <http://www.gnu.org/licenses/>.
17
18__contains_word () {
19 local word=$1; shift
20 for w in $*; do [[ $w = $word ]] && return 0; done
21 return 1
22}
23
24ALL_OPTS='-l --list -S -o -n --noheading -h --help'
25MANAGED_OPTS='-a --add -m --modify -d --delete -D --deleteall -C --locallist '
26
27__get_all_stores () {
28 dir -1 -F /etc/selinux/ | grep '/' | cut -d'/' -f 1
29}
30__get_all_ftypes () {
31 echo '-- -d -c -b -s -l -p'
32}
33__get_all_users () {
34 seinfo -u 2> /dev/null | tail -n +3
35}
36__get_all_types () {
37 seinfo -t 2> /dev/null | tail -n +3
38}
39__get_all_port_types () {
40 seinfo -aport_type -x 2>/dev/null | tail -n +2
41}
42__get_all_domains () {
43 seinfo -adomain -x 2>/dev/null | tail -n +2
44}
45__get_all_node_types () {
46 seinfo -anode_type -x 2>/dev/null | tail -n +2
47}
48__get_all_file_types () {
49 seinfo -afile_type -x 2>/dev/null | tail -n +2
50}
51__get_all_roles () {
Dan Walsh1886d462013-10-11 08:39:07 -040052 seinfo -r 2> /dev/null | tail -n +3
Dan Walshe5a81c72012-03-06 10:45:29 -050053}
54__get_all_stores () {
55 dir -1 -F /etc/selinux/ | grep '/' | cut -d'/' -f 1
56}
Dan Walsh1886d462013-10-11 08:39:07 -040057__get_import_opts () { echo '$ALL_OPTS --f --input_file' ; }
58__get_export_opts () { echo '$ALL_OPTS --f --output_file' ; }
59__get_boolean_opts () { echo '$ALL_OPTS --on -off -1 -0' ; }
60__get_user_opts () { echo '$ALL_OPTS $MANAGED_OPTS -L --level -r --range -R --role '; }
61__get_login_opts () { echo '$ALL_OPTS $MANAGED_OPTS -s --seuser -r --range'; }
Lee Stubbse4348942018-02-18 17:21:41 -050062__get_port_opts () { echo '$ALL_OPTS $MANAGED_OPTS -t --type -r --range -p --proto'; }
Dan Walshe5a81c72012-03-06 10:45:29 -050063__get_interface_opts () { echo '$ALL_OPTS $MANAGED_OPTS -t --type '; }
64__get_node_opts () { echo '$ALL_OPTS $MANAGED_OPTS -t --type -M --mask -p --proto'; }
65__get_fcontext_opts () { echo '$ALL_OPTS $MANAGED_OPTS -t --type -e --equal -f --ftype '; }
Dan Walshb68435f2012-03-29 12:43:25 -040066__get_module_opts () { echo '$ALL_OPTS $MANAGED_OPTS --enable --disable '; }
Dan Walshe5a81c72012-03-06 10:45:29 -050067__get_dontaudit_opts () { echo '-S on off' ; }
68__get_permissive_opts () { echo '$ALL_OPTS -a --add -d --delete' ; }
69
70_semanage () {
71 local command=${COMP_WORDS[1]}
72 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
73 local verb comps
74 local -A VERBS=(
Dan Walsh1886d462013-10-11 08:39:07 -040075 [BOOLEAN]='boolean'
76 [DONTAUDIT]='dontaudit'
77 [EXPORT]='export'
78 [FCONTEXT]='fcontext'
79 [IMPORT]='import'
Dan Walshe5a81c72012-03-06 10:45:29 -050080 [INTERFACE]='interface'
Dan Walsh1886d462013-10-11 08:39:07 -040081 [LOGIN]='login'
Dan Walshe5a81c72012-03-06 10:45:29 -050082 [MODULE]='module'
83 [NODE]='node'
Dan Walshe5a81c72012-03-06 10:45:29 -050084 [PERMISSIVE]='permissive'
Dan Walsh1886d462013-10-11 08:39:07 -040085 [PORT]='port'
86 [USER]='user'
Dan Walshe5a81c72012-03-06 10:45:29 -050087 )
Dan Walshe5a81c72012-03-06 10:45:29 -050088 if [ "$prev" = "-a" -a "$command" = "permissive" ]; then
89 COMPREPLY=( $(compgen -W "$( __get_all_domains ) " -- "$cur") )
90 return 0
91 fi
92 if [ "$verb" = "" -a "$prev" = "semanage" ]; then
93 comps="${VERBS[*]}"
94 elif [ "$verb" = "" -a "$prev" = "-S" -o "$prev" = "--store" ]; then
95 COMPREPLY=( $(compgen -W "$( __get_all_stores ) " -- "$cur") )
96 return 0
97 elif [ "$verb" = "" -a "$prev" = "-p" -o "$prev" = "--proto" ]; then
98 COMPREPLY=( $(compgen -W "tcp udp" -- "$cur") )
99 return 0
Dan Walsh1886d462013-10-11 08:39:07 -0400100 elif [ "$verb" = "" -a "$prev" = "-R" -o "$prev" = "-r" -o "$prev" = "--role" ]; then
101 if [ "$command" != "user" -o "$prev" != "-r" ]; then
Dan Walshe5a81c72012-03-06 10:45:29 -0500102 COMPREPLY=( $(compgen -W "$( __get_all_roles ) " -- "$cur") )
103 return 0
Dan Walsh1886d462013-10-11 08:39:07 -0400104 else
105 return 0
106 fi
Dan Walshe5a81c72012-03-06 10:45:29 -0500107 elif [ "$verb" = "" -a "$prev" = "-s" -o "$prev" = "--seuser" ]; then
108 COMPREPLY=( $(compgen -W "$( __get_all_users ) " -- "$cur") )
109 return 0
110 elif [ "$verb" = "" -a "$prev" = "-f" -o "$prev" = "--ftype" ]; then
111 COMPREPLY=( $(compgen -W "$( __get_all_ftypes ) " -- "$cur") )
112 return 0
113 elif [ "$verb" = "" -a "$prev" = "-t" -o "$prev" = "--types" ]; then
114 if [ "$command" = "port" ]; then
115 COMPREPLY=( $(compgen -W "$( __get_all_port_types ) " -- "$cur") )
116 return 0
117 fi
118 if [ "$command" = "fcontext" ]; then
119 COMPREPLY=( $(compgen -W "$( __get_all_file_types ) " -- "$cur") )
120 return 0
121 fi
122 COMPREPLY=( $(compgen -W "$( __get_all_types ) " -- "$cur") )
123 return 0
124 elif __contains_word "$command" ${VERBS[LOGIN]} ; then
125 COMPREPLY=( $(compgen -W "$( __get_login_opts ) " -- "$cur") )
126 return 0
127 elif __contains_word "$command" ${VERBS[USER]} ; then
128 COMPREPLY=( $(compgen -W "$( __get_user_opts ) " -- "$cur") )
129 return 0
130 elif __contains_word "$command" ${VERBS[PORT]} ; then
131 COMPREPLY=( $(compgen -W "$( __get_port_opts ) " -- "$cur") )
132 return 0
133 elif __contains_word "$command" ${VERBS[INTERFACE]} ; then
134 COMPREPLY=( $(compgen -W "$( __get_interface_opts ) " -- "$cur") )
Dan Walsh1886d462013-10-11 08:39:07 -0400135 return 0
Dan Walshe5a81c72012-03-06 10:45:29 -0500136 elif __contains_word "$command" ${VERBS[MODULE]} ; then
137 COMPREPLY=( $(compgen -W "$( __get_module_opts ) " -- "$cur") )
138 return 0
139 elif __contains_word "$command" ${VERBS[NODE]} ; then
140 COMPREPLY=( $(compgen -W "$( __get_node_opts ) " -- "$cur") )
141 return 0
142 elif __contains_word "$command" ${VERBS[FCONTEXT]} ; then
143 COMPREPLY=( $(compgen -W "$( __get_fcontext_opts ) " -- "$cur") )
144 return 0
145 elif __contains_word "$command" ${VERBS[BOOLEAN]} ; then
146 COMPREPLY=( $(compgen -W "$( __get_boolean_opts ) " -- "$cur") )
147 return 0
148 elif __contains_word "$command" ${VERBS[PERMISSIVE]} ; then
149 COMPREPLY=( $(compgen -W "$( __get_permissive_opts ) " -- "$cur") )
150 return 0
151 elif __contains_word "$command" ${VERBS[DONTAUDIT]} ; then
152 COMPREPLY=( $(compgen -W "$( __get_dontaudit_opts ) " -- "$cur") )
153 return 0
Dan Walsh1886d462013-10-11 08:39:07 -0400154 elif __contains_word "$command" ${VERBS[IMPORT]} ; then
155 COMPREPLY=( $(compgen -W "$( __get_import_opts ) " -- "$cur") )
156 return 0
157 elif __contains_word "$command" ${VERBS[EXPORT]} ; then
158 COMPREPLY=( $(compgen -W "$( __get_export_opts ) " -- "$cur") )
159 return 0
Dan Walshe5a81c72012-03-06 10:45:29 -0500160 fi
161 COMPREPLY=( $(compgen -W "$comps" -- "$cur") )
162 return 0
163}
164complete -F _semanage semanage