xref: /aosp_15_r20/external/selinux/python/semanage/semanage-bash-completion.sh (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1# This file is part of systemd.
2#
3# Copyright 2011-2013 Dan Walsh
4#
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 () {
52    seinfo -r 2> /dev/null | tail -n +3
53}
54__get_all_stores () {
55    dir -1 -F /etc/selinux/ | grep '/' | cut -d'/' -f 1
56}
57__get_all_modules () {
58    semodule -l
59}
60__get_import_opts () { echo '$ALL_OPTS --f --input_file' ; }
61__get_export_opts () { echo '$ALL_OPTS --f --output_file' ; }
62__get_boolean_opts () { echo '$ALL_OPTS --on -off -1 -0' ; }
63__get_user_opts () { echo '$ALL_OPTS $MANAGED_OPTS -L --level -r --range -R --role '; }
64__get_login_opts () { echo '$ALL_OPTS $MANAGED_OPTS -s --seuser -r --range'; }
65__get_port_opts () { echo '$ALL_OPTS $MANAGED_OPTS -t --type -r --range -p --proto'; }
66__get_interface_opts () { echo '$ALL_OPTS $MANAGED_OPTS -t --type '; }
67__get_node_opts () { echo '$ALL_OPTS $MANAGED_OPTS -t --type -M --mask -p --proto'; }
68__get_fcontext_opts () { echo '$ALL_OPTS $MANAGED_OPTS -t --type -e --equal -f --ftype '; }
69__get_module_opts () { echo '$ALL_OPTS $MANAGED_OPTS --enable --disable '; }
70__get_dontaudit_opts () { echo '-S on off' ; }
71__get_permissive_opts () { echo '$ALL_OPTS -a --add -d --delete' ; }
72
73_semanage () {
74        local command=${COMP_WORDS[1]}
75        local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
76        local verb comps
77        local -A VERBS=(
78	       [BOOLEAN]='boolean'
79	       [DONTAUDIT]='dontaudit'
80	       [EXPORT]='export'
81	       [FCONTEXT]='fcontext'
82	       [IMPORT]='import'
83	       [INTERFACE]='interface'
84	       [LOGIN]='login'
85	       [MODULE]='module'
86	       [NODE]='node'
87	       [PERMISSIVE]='permissive'
88	       [PORT]='port'
89	       [USER]='user'
90        )
91	if   [ "$prev" = "-a" -a "$command" = "permissive" ]; then
92	        COMPREPLY=( $(compgen -W "$( __get_all_domains ) " -- "$cur") )
93		return 0
94	elif [ "$command" = "module" ]; then
95		if [ "$prev" = "-d" ] || [ "$prev" = "--disable" ] \
96		    || [ "$prev" = "-e" ] || [ "$prev" = "--enable" ] \
97		    || [ "$prev" = "-r" ] || [ "$prev" = "--remove" ]; then
98	            COMPREPLY=( $(compgen -W "$( __get_all_modules ) " -- "$cur") )
99		    return 0
100		fi
101	fi
102	if   [ "$verb" = "" -a "$prev" = "semanage" ]; then
103                comps="${VERBS[*]}"
104	elif [ "$verb" = "" -a "$prev" = "-S" -o "$prev" = "--store" ]; then
105	        COMPREPLY=( $(compgen -W "$( __get_all_stores ) " -- "$cur") )
106		return 0
107	elif [ "$verb" = "" -a "$prev" = "-p" -o "$prev" = "--proto" ]; then
108	        COMPREPLY=( $(compgen -W "tcp udp" -- "$cur") )
109		return 0
110	elif [ "$verb" = "" -a "$prev" = "-R" -o "$prev" = "-r" -o "$prev" = "--role" ]; then
111	    if [ "$command" != "user" -o "$prev" != "-r" ]; then
112	        COMPREPLY=( $(compgen -W "$( __get_all_roles ) " -- "$cur") )
113		return 0
114	    else
115		return 0
116	    fi
117	elif [ "$verb" = "" -a "$prev" = "-s" -o "$prev" = "--seuser" ]; then
118	        COMPREPLY=( $(compgen -W "$( __get_all_users ) " -- "$cur") )
119		return 0
120	elif [ "$verb" = "" -a "$prev" = "-f" -o "$prev" = "--ftype" ]; then
121	        COMPREPLY=( $(compgen -W "$( __get_all_ftypes ) " -- "$cur") )
122		return 0
123	elif [ "$verb" = "" -a "$prev" = "-t" -o "$prev" = "--types" ]; then
124	    if [ "$command" = "port" ]; then
125	        COMPREPLY=( $(compgen -W "$( __get_all_port_types ) " -- "$cur") )
126		return 0
127	    fi
128	    if [ "$command" = "fcontext" ]; then
129	        COMPREPLY=( $(compgen -W "$( __get_all_file_types ) " -- "$cur") )
130		return 0
131	    fi
132	    COMPREPLY=( $(compgen -W "$( __get_all_types ) " -- "$cur") )
133	    return 0
134        elif __contains_word "$command" ${VERBS[LOGIN]} ; then
135                COMPREPLY=( $(compgen -W "$( __get_login_opts ) " -- "$cur") )
136		return 0
137        elif __contains_word "$command" ${VERBS[USER]} ; then
138                COMPREPLY=( $(compgen -W "$( __get_user_opts ) " -- "$cur") )
139		return 0
140        elif __contains_word "$command" ${VERBS[PORT]} ; then
141                COMPREPLY=( $(compgen -W "$( __get_port_opts ) " -- "$cur") )
142		return 0
143        elif __contains_word "$command" ${VERBS[INTERFACE]} ; then
144                COMPREPLY=( $(compgen -W "$( __get_interface_opts ) " -- "$cur") )
145		return 0
146        elif __contains_word "$command" ${VERBS[MODULE]} ; then
147                COMPREPLY=( $(compgen -W "$( __get_module_opts ) " -- "$cur") )
148		return 0
149        elif __contains_word "$command" ${VERBS[NODE]} ; then
150                COMPREPLY=( $(compgen -W "$( __get_node_opts ) " -- "$cur") )
151		return 0
152        elif __contains_word "$command" ${VERBS[FCONTEXT]} ; then
153                COMPREPLY=( $(compgen -W "$( __get_fcontext_opts ) " -- "$cur") )
154		return 0
155        elif __contains_word "$command" ${VERBS[BOOLEAN]} ; then
156                COMPREPLY=( $(compgen -W "$( __get_boolean_opts ) " -- "$cur") )
157		return 0
158        elif __contains_word "$command" ${VERBS[PERMISSIVE]} ; then
159                COMPREPLY=( $(compgen -W "$( __get_permissive_opts ) " -- "$cur") )
160		return 0
161        elif __contains_word "$command" ${VERBS[DONTAUDIT]} ; then
162                COMPREPLY=( $(compgen -W "$( __get_dontaudit_opts ) " -- "$cur") )
163		return 0
164        elif __contains_word "$command" ${VERBS[IMPORT]} ; then
165                COMPREPLY=( $(compgen -W "$( __get_import_opts ) " -- "$cur") )
166		return 0
167        elif __contains_word "$command" ${VERBS[EXPORT]} ; then
168                COMPREPLY=( $(compgen -W "$( __get_export_opts ) " -- "$cur") )
169		return 0
170        fi
171        COMPREPLY=( $(compgen -W "$comps" -- "$cur") )
172        return 0
173}
174complete -F _semanage semanage
175