aboutsummaryrefslogtreecommitdiff
path: root/utils/apparmor/patches/040-remove-bash-dep.patch
blob: 98ef107a8ea464304828dff7cbd12ac801f5e635 (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
--- a/utils/aa-decode
+++ b/utils/aa-decode
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 #
 #    Copyright (C) 2009-2010, 2012 Canonical Ltd.
 #    Copyright (C) 2012 Christian Boltz
@@ -16,8 +16,6 @@
 #    along with this program; if not, contact Canonical, Ltd.
 #
 
-set -e
-
 help() {
     cat <<EOM
 USAGE: aa-decode [OPTIONS] <encoded string>
@@ -36,13 +34,15 @@ $ cat /var/log/kern.log | aa-decode
 EOM
 }
 
-decode() {
-    if echo "$1" | egrep -q "^[0-9A-Fa-f]+$" ; then
-      python3 -c "import binascii; print(bytes.decode(binascii.unhexlify('$1'), errors='strict'));"
-    else
-      echo ""
-    fi
+match_re() {
+	local result=$(echo "$1" | grep -E "$2" )
+	[ -z "$result" ] && return 1 || return 0
+}
 
+
+decode() {
+	$(echo "$1" | egrep -q "^[0-9A-Fa-f]+$"); [ "$?" -eq 0 ] &&
+		python3 -c "import binascii; print(bytes.decode(binascii.unhexlify('$1'), errors='strict'));" || echo ""
 }
 
 if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
@@ -51,47 +51,61 @@ if [ "$1" = "-h" ] || [ "$1" = "--help"
 fi
 
 # if have an argument, then use it, otherwise process stdin
-if [ -n "$1" ]; then
-    e="$1"
-    if ! echo "$e" | egrep -q "^[0-9A-Fa-f]+$" ; then
-        echo "String should only contain hex characters (0-9, a-f, A-F)"
-        exit 1
-    fi
-
-    d=`decode $e`
-    if [ -z "$d" ]; then
-        echo "Could not decode string"
-        exit 1
-    fi
+[ -n "$1" ] && {
 
-    echo "Decoded: $d"
-    exit 0
-fi
+	e="$1"
 
-# For now just look at 'name=...' and 'profile=...',
-# so validate input against this and output based on it.
-# TODO: better handle other cases too
-while read line ; do
+	$(echo "$e" | egrep -q "^[0-9A-Fa-f]+$"); [ "$?" -ne 0 ] && {
+		echo "String should only contain hex characters (0-9, a-f, A-F)"
+		exit 1
+	}
 
-    # check if line contains encoded name= or profile=
-    if [[ "$line" =~ \ (name|profile|proctitle)=[0-9a-fA-F] ]]; then
+	d=$(decode $e)
 
-        # cut the encoded filename/profile name out of the line and decode it
-        ne=`echo "$line" | sed 's/.* name=\([^ ]*\).*$/\\1/g'`
-        nd="$(decode ${ne/\'/\\\'})"
+	[ -z "$d" ] && {
+		echo "Could not decode string"
+		exit 1
+	}
 
-        pe=`echo "$line" | sed 's/.* profile=\([^ ]*\).*$/\\1/g'`
-        pd="$(decode ${pe/\'/\\\'})"
+	echo "Decoded: $d"
+	exit 0
+}
 
-        pce=`echo "$line" | sed 's/.* proctitle=\([^ ]*\).*$/\\1/g'`
-        pcd="$(decode ${pce/\'/\\\'})"
+[ -t 0 ] && {
+	help
+	exit
+}
+
+while read line ; do
 
-        # replace encoded name and profile with its decoded counterparts (only if it was encoded)
-        test -n "$nd" && line="${line/name=$ne/name=\"$nd\"}"
-        test -n "$pd" && line="${line/profile=$pe/profile=\"$pd\"}"
-        test -n "$pcd" && line="${line/proctitle=$pce/proctitle=\"$pcd\"}"
+	# check if line contains encoded name= or profile=
 
-    fi
+	matches=0
+	match_re "$line" "^[[:blank:]](name|profile|proctitle)=[0-9a-fA-F]+"; [ "$?" -eq 0 ] && matches=1 || {
+		 match_re "$line" "^(name|profile|proctitle)=[0-9a-fA-F]+"; [ "$?" -eq 0 ] && matches=1
+	}
+
+	[ "$matches" -eq 0 ] || {
+
+		# cut the encoded filename/profile name out of the line and decode it
+		ne=$(echo "$line" | sed 's/.* name=\([^ ]*\).*$/\\1/g')
+		[ "$line" = "$ne" ] && ne=$(echo "$line" | sed 's/.*name=\([^ ]*\).*$/\\1/g')
+		echo var: $ne
+		nd="$(decode ${ne/\'/\\\'})"
+
+		pe=$(echo "$line" | sed 's/.* profile=\([^ ]*\).*$/\\1/g')
+		[ "$line" = "$pe" ] && pe=$(echo "$line" | sed 's/.*profile=\([^ ]*\).*$/\\1/g')
+		pd="$(decode ${pe/\'/\\\'})"
+
+		pce=$(echo "$line" | sed 's/.* proctitle=\([^ ]*\).*$/\\1/g')
+		[ "$line" = "$pce" ] && pce=$(echo "$line" | sed 's/.*proctitle=\([^ ]*\).*$/\\1/g')
+		pcd="$(decode ${pce/\'/\\\'})"
+
+		# replace encoded name and profile with its decoded counterparts (only if it was encoded)
+		test -n "$nd" && line="${line/name=$ne/name=\"$nd\"}"
+		test -n "$pd" && line="${line/profile=$pe/profile=\"$pd\"}"
+		test -n "$pcd" && line="${line/proctitle=$pce/proctitle=\"$pcd\"}"
+	}
 
     echo "$line"