File: common.sh

package info (click to toggle)
courier-authlib 0.71.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 14,416 kB
  • sloc: ansic: 15,778; sh: 4,565; cpp: 4,205; makefile: 793; perl: 739
file content (146 lines) | stat: -rw-r--r-- 3,498 bytes parent folder | download | duplicates (5)
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
# autopkgtest check: helper routines for authdaemond tests
#
# Author: Markus Wanner <markus@bluegap.ch>

TEST_USERS="alice bob carol"

CONFIG_FILES="/etc/courier/authdaemonrc \
	    /etc/courier/authldaprc \
	    /etc/courier/authmysqlrc \
	    /etc/courier/authpgsqlrc \
	    /etc/courier/authsqliterc"

export PGOPTIONS='--client-min-messages=warning'

# exits with code 0 if the given user exists
user_exists() {
    id -u $1 > /dev/null 2>&1
}

# exits with code 0 if the postgresql client tools are installed
has_postgres_client() {
    which psql > /dev/null 2>&1
}

test_authentication() {
    user=$1
    password=$2
    TEST_OUTPUT="$AUTOPKGTEST_ARTIFACTS/testauth-$1.out"
    echo "testing: '$user' with password '$password'"
    /usr/sbin/authtest $user $password > $TEST_OUTPUT
}

authenumerate_as_courier() {
    su -c "/usr/sbin/authenumerate" -s /bin/sh courier
}

# emits a random (512bit, hex encoded) password on stdout
gen_random_password() {
    dd if=/dev/urandom bs=16 count=1 2> /dev/null | hexdump -e '"%x"'
}

# accepts SQL on stdin
postgres_superuser_exec() {
    su postgres -c "psql -X -q -v ON_ERROR_STOP=1 --pset pager=off"
}

create_test_users() {
    echo "== creating test users..."
    for USER in $TEST_USERS; do
        gen_random_password > $USER.password
        useradd --shell /bin/false --password $(cat $USER.password) $USER
    done
}

backup_config_files() {
    echo "== backup config files..."
    for f in $CONFIG_FILES; do
        if [ -f $f ]; then
            cp ${f} ${f}.autopkgtest.bak
        fi
    done
}

restore_config_files() {
    echo "== restore config files..."
    for f in $CONFIG_FILES; do
        if [ -f ${f}.autopkgtest.bak ]; then
            mv ${f}.autopkgtest.bak ${f}
        fi
    done
}

start_authdaemon() {
    echo "== starting authdameon..."
    service courier-authdaemon start
}

start_postgresql() {
    echo "== starting postgresql..."
    service postgresql start
}

# helper methods for dumping test status
dump_file_if_exists() {
    if [ -f $1 ]; then
        echo "===== BEGIN $1 ====="
        cat $1
        echo "===== END $1 ====="
    fi
}

dump_config_files() {
    for f in $CONFIG_FILES; do
        if [ -f ${f}.autopkgtest.bak ]; then
            dump_file_if_exists $f
        fi
    done

    for f in `ls $AUTOPKGTEST_ARTIFACTS/`; do
        dump_file_if_exists $AUTOPKGTEST_ARTIFACTS/$f
    done
}

# cleanup after running tests
finish() {
    echo "== dump..."
    # dump and then restore the config files
    dump_config_files

    echo "== finish..."

    # drop test users
    if user_exists alice; then
        echo "== dropping user alice"
        userdel alice
    fi
    if user_exists bob; then
        echo "== dropping user bob"
        userdel bob
    fi
    if user_exists carol; then
        echo "== dropping user carol"
        userdel carol
    fi

    # restore config files, then restart the authdaemon, so it
    # disconnects from the database. Otherwise authdaemon blocks the
    # database deletion.
    restore_config_files

    # cleanup Postgres databases
    if has_postgres_client; then
        postgres_superuser_exec <<EOSQL
DROP DATABASE IF EXISTS courier_authdaemon_test;
DROP ROLE IF EXISTS courier;
EOSQL
    fi

    for NAME in courier-authdaemon postgresql; do
        if [ -x /etc/init.d/$NAME ]; then
            echo "== stopping service $NAME..."
            service $NAME stop || /bin/true
        fi
    done
}
trap finish EXIT INT QUIT ABRT PIPE TERM