summaryrefslogtreecommitdiff
blob: 0d408eadc96ab7364ffe60a81fabead92e020330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/sbin/runscript
# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

extra_started_commands="reload"

start() {
	ebegin "Starting ${SVCNAME}"
	eindent

	if ! check_config || ! remove_profiles || ! load_profiles load ; then
		eend 1
		return 1
	fi

	eoutdent
	eend 0
}

stop() {
	ebegin "Stopping ${SVCNAME}"

	if ! check_config ; then
		eend 1
		return 1
	fi

	eindent
	ebegin "Removing profiles"
	remove_profiles
	eoutdent
	rc=$?
	eend $rc
	return $rc
}

reload() {
	ebegin "Reloading ${SVCNAME} profiles"

	if ! ( check_config && load_profiles reload && unload_obsolete_profiles ); then
		eend 1
		return 1
	fi

	eend 0
	return 0
}

check_config() {

	if [ -z "$PARSER" -o -z "$PROFILE_DIR" -o -z "$SECURITYFS" ]; then
		eerror "/etc/conf.d/${SVCNAME} has missing variables"
		return 1
	fi

	if ! is_securityfs_mounted ; then
		if ! mount_securityfs ; then
			eerror "Failed to mount securityfs"
			return 1
		fi
	fi

	if ! is_apparmor_present ; then
		modprobe -q apparmor
		if ! is_apparmor_present ; then
			eerror "${SVCNAME} kernel support is not present"
			return 1
		fi
	fi

	if ! check_apparmor_compat ; then
		eerror "${SVCNAME} compatibility is not present in the kernel"
		return 1
	fi
}

get_profile_files() {
	ARGS=""
	IGNORES="$( find ${PROFILE_DIR} -name .ignore )"
	LENGTH=$( echo "$IGNORES" | wc -l )

	I=1;
	for IGNORE in $IGNORES; do
		ARGS="${ARGS} -path $( dirname ${IGNORE} ) -prune"
		if [ $LENGTH -gt 1 -a $I -lt $LENGTH ]; then
			ARGS="${ARGS} -o"
		fi
		I=$(($I+1))
	done

	if [ -z "$ARGS" ]; then
		PROFILES="find /etc/apparmor.d"
	else
		PROFILES="find /etc/apparmor.d "$ARGS" -o -type f -print"
	fi

	echo $( $PROFILES )
}

get_active_profiles() {
	PROFILES=`sed -e "s/ (\(enforce\|complain\))//" "${SECURITYFS}/profiles"`
	echo $PROFILES
}

load_profiles() {

	case "$1" in
		load)
			PARSER_ARGS="--add"
			MESSAGE="Loading ${SVCNAME} profiles"
			;;
		reload)
			PARSER_ARGS="--replace"
			MESSAGE="Reloading ${SVCNAME} profiles"
			;;
		*)
			eerror "Invalid load_profile argument"
			exit 1
			;;
	esac	

	if [ ! -x "${PARSER}" ]; then
		eerror "Could not find apparmor_parser"
		return 1
	fi

	if [ ! -d "${PROFILE_DIR}" -o -z "$(ls $PROFILE_DIR 2> /dev/null)" ]; then
		ewarn "No profiles found"
		return 0
	fi

	PROFILES="$(get_profile_files)"

	for PROFILE in $PROFILES; do
		$PARSER $PARSER_ARGS $PROFILE
		if [ $? -ne 0 ]; then
			if [ "${PARSER_ARGS}" = "replace" ]; then
				ewarn "Error loading '${PROFILE}', continuing"
			else
				eerror "Error loading '${PROFILE}', aborting"
				remove_profiles
				return 1
			fi
		fi
	done

	return 0
}

remove_profiles() {
	PROFILES=$(get_active_profiles)
	for PROFILE in $PROFILES; do
		#use printf instead of echo -n for POSIX compatibility
		printf '%s' "$PROFILE" > "${SECURITYFS}/.remove"
	done
	return 0
}

is_securityfs_mounted() {
	grep -q securityfs /proc/filesystems && grep -q securityfs /proc/mounts
	return $?
}

mount_securityfs() {
	if [ grep -q securityfs /proc/filesystems ]; then
		mount -t securityfs securityfs "${SECURITYFS}"
		return $?
	else
		return 1
	fi
}

is_apparmor_present() {
	grep -q "^apparmor" /proc/modules
	[ $? -ne 0 -a -d /sys/module/apparmor ]
	return $?
}

check_apparmor_compat() {
	if [ -f "${SECURITYFS}/profiles" ]; then
		return 0
	else
		return 1
	fi
}

unload_obsolete_profiles() {

	TEMPDIR=$(umask 0077 && mktemp -d)

	if [ ! -d "${TEMPDIR}" ]; then
		eerror "Failed to create temporary directory"
		return 1
	fi

	cd $TEMPDIR

	echo $(get_active_profiles) | tr ' ' '\n' | sort > old

	for PROFILE in $(get_profile_files); do
		echo $(${PARSER} -N "$PROFILE") >> new
	done

	sort new > new_sorted

	for PROFILE in $(comm -2 -3 old new_sorted); do
		#use printf instead of echo -n for POSIX compatibility
		printf '%s' "$PROFILE" > "${SECURITYFS}/.remove"
	done

	rm -rf "${TEMPDIR}"

	return 0
}