~ubuntu-branches/ubuntu/wily/apparmor/wily

« back to all changes in this revision

Viewing changes to debian/aa-update-browser

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2011-04-27 10:38:07 UTC
  • mfrom: (5.1.118 natty)
  • Revision ID: james.westby@ubuntu.com-20110427103807-ym3rhwys6o84ith0
Tags: 2.6.1-2
debian/copyright: clarify for some full organization names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
# Copyright (C) 2010 Canonical, Ltd.
 
4
# Author: Jamie Strandboge <jamie@canonical.com>
 
5
# License: GPLv2
 
6
#
 
7
# Program for updating browser abstractions in Ubuntu. The program will
 
8
# search the specified profile for an include directive for a file in
 
9
# abstractions/ubuntu-browsers.d and update this file with the specified
 
10
# browsers abstractions.
 
11
 
 
12
set -e
 
13
 
 
14
topdir="/etc/apparmor.d"
 
15
reldir="abstractions/ubuntu-browsers.d"
 
16
dir="$topdir/$reldir"
 
17
 
 
18
if [ ! -d "$dir" ]; then
 
19
    echo "'$dir' is not a directory" >&2
 
20
    exit 1
 
21
fi
 
22
 
 
23
help() {
 
24
    cat <<EOM
 
25
`basename $0`
 
26
 
 
27
Usage: `basename $0` [OPTIONS] <profile>
 
28
  -u            comma separated list of abstractions for profile to use
 
29
  -d            dry-run. Only show what would be done.
 
30
  -l            list available abstractions
 
31
  -h            this message
 
32
 
 
33
Eg:
 
34
$ aa-update-browser -l
 
35
# aa-update-browser -u multimedia,productivity /etc/apparmor.d/usr.bin.firefox
 
36
EOM
 
37
}
 
38
 
 
39
find_browser_include() {
 
40
    fn="$1"
 
41
    r=`egrep " *#include <$reldir/.*> *(|#.*)" "$fn" | cut -f 2 -d '<' | cut -f 1 -d '>'`
 
42
    if [ -z "$r" ]; then
 
43
        echo "Could not find '#include <$reldir/...>' in" >&2
 
44
        echo "$fn" >&2
 
45
        return
 
46
    fi
 
47
    basename "$r"
 
48
}
 
49
 
 
50
existing_abstractions=""
 
51
for i in $dir/* ; do
 
52
    if [ ! -s "$i" ]; then
 
53
        continue
 
54
    fi
 
55
 
 
56
    if head -1 "$i" | grep -q '^# This file is updated' ; then
 
57
        continue
 
58
    fi
 
59
 
 
60
    # This has a leading space, which we use below.
 
61
    existing_abstractions="$existing_abstractions `basename $i`"
 
62
done
 
63
 
 
64
updated=
 
65
dryrun=
 
66
while getopts "dhlu:" opt
 
67
do
 
68
    case "$opt" in
 
69
        d) dryrun="yes";;
 
70
        u) updated="$OPTARG";;
 
71
        l)
 
72
            echo "$existing_abstractions"
 
73
            exit 0
 
74
            ;;
 
75
        h)
 
76
            help
 
77
            exit 0
 
78
            ;;
 
79
        ?)
 
80
            help
 
81
            exit 1
 
82
            ;;
 
83
    esac
 
84
done
 
85
shift $(($OPTIND - 1))
 
86
 
 
87
if [ -z "$1" ]; then
 
88
    help
 
89
    exit 1
 
90
fi
 
91
 
 
92
for p in $* ; do
 
93
    if [ ! -s "$p" ]; then
 
94
        echo "Could not find '$p'" >&2
 
95
        exit 1
 
96
    fi
 
97
 
 
98
    include=`find_browser_include $p`
 
99
    if [ -z "$include" ]; then
 
100
        exit 1
 
101
    fi
 
102
 
 
103
    if echo "$existing_abstractions" | grep -q " $include" ; then
 
104
        echo "'$reldir/$include' is an existing abstraction" >&2
 
105
        exit 1
 
106
    fi
 
107
 
 
108
    tmp=`mktemp`
 
109
    plugins_common_path="$dir/plugins-common"
 
110
    cat > "$tmp" <<EOM
 
111
# This file is updated by '`basename $0`' and may be overwritten on
 
112
# upgrades.
 
113
#
 
114
# For site-specific adjustments, please see /etc/apparmor.d/local/<binary>
 
115
 
 
116
EOM
 
117
    for a in `echo "$updated" | tr [,] ' '`; do
 
118
        echo "$existing_abstractions" | egrep -q " $a( |$)" || {
 
119
            echo "'$a' is not an existing abstraction. Skipping." >&2
 
120
            continue
 
121
        }
 
122
        if [ -f "$dir/$a" ]; then
 
123
            # TODO: add $plugins_common_path only for those browser abstractions
 
124
            # that actually need it.
 
125
            if [ -n "$plugins_common_path" ] && [ -e "$plugins_common_path" ]; then
 
126
                echo "#include <$reldir/`basename $plugins_common_path`>" >> "$tmp"
 
127
                plugins_common_path=""
 
128
            fi
 
129
            echo "#include <$reldir/$a>" >> "$tmp"
 
130
        else
 
131
            echo "Skipping '$a' (not found in '$dir')" >&2
 
132
            continue
 
133
        fi
 
134
    done
 
135
 
 
136
    if [ "$dryrun" = "yes" ]; then
 
137
        echo "Skipping commit to '$dir/$include' (dry run)" >&2
 
138
        cat "$tmp"
 
139
        rm -f "$tmp"
 
140
        continue
 
141
    fi
 
142
    mv -f "$tmp" "$dir/$include" || {
 
143
        rm -f "$tmp"
 
144
        exit 1
 
145
    }
 
146
    chmod 644 "$dir/$include"
 
147
done
 
148