File: gsasl-dovecot-gssapi.sh

package info (click to toggle)
gsasl 2.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,600 kB
  • sloc: ansic: 16,551; sh: 1,739; makefile: 677; xml: 301; php: 172; perl: 4
file content (209 lines) | stat: -rwxr-xr-x 4,987 bytes parent folder | download | duplicates (2)
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
#!/bin/sh

# Copyright (C) 2022-2025 Simon Josefsson
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e
set -u
set -x

# Set up a local MIT Kerberos KDC, configure Dovecot, and then
# authenticate with GSS-API to the server using 'gsasl' as the client.

# No root privileges required, but listens on hard-coded ports 17643
# (KDC) and 17436 (dovecot).

# Environment variables GSASL specify tool to use, which may include
# valgrind/libtool or other profiling commands.

: ${GSASL=gsasl}

if ! $GSASL --version 2> /dev/null | grep '^gsasl (GNU SASL'; then
    echo FAIL: $0: GNU SASL gsasl missing...
    exit 1
fi

if ! $GSASL --client-mechanisms 2>&1 | grep ' GSSAPI '; then
    echo SKIP: $0: No GSSAPI support detected...
    exit 77
fi

if test "${GNUGSS:-no}" = yes; then
    echo SKIP: $0: Not ported to Shishi/GSS ccache yet...
    exit 77
fi

export PATH=$PATH:/sbin:/usr/sbin

if ! command -v ss && ! command -v netstat; then
    echo SKIP: $0: Required tools 'ss' or 'netstat' missing...
    exit 77
fi

if ! command -v id || ! command -v hostname || ! command -v dovecot || ! command -v kinit || ! command -v kdb5_util || ! command -v kadmin.local || ! command -v krb5kdc; then
    echo SKIP: $0: Required tools missing...
    exit 77
fi

WORKDIR=$(mktemp -d)

trap 'set +e; test -f $WORKDIR/pid && kill `cat $WORKDIR/pid`; dovecot -c $WORKDIR/d/dovecot.conf stop; tail -v -n +0 $WORKDIR/out-* $WORKDIR/dovecot.log $WORKDIR/kdc.log; rm -rfv $WORKDIR' 0 INT QUIT ABRT PIPE TERM

: ${USER=`id -un`}
: ${GROUP=`id -gn`}

mkdir $WORKDIR/k  $WORKDIR/d

cat<<EOF > $WORKDIR/k/kdc.conf
[realms]
    GSASL.EXAMPLE = {
        database_name = $WORKDIR/principal
        key_stash_file = $WORKDIR/stash
        kdc_ports = 17643
        kdc_tcp_ports = 17643
        default_principal_flags = +preauth
    }
[logging]
   kdc = FILE:$WORKDIR/kdc.log
EOF

cat<<EOF > $WORKDIR/k/krb5.conf
[libdefaults]
	default_realm = GSASL.EXAMPLE

[domain_realm]
	.`hostname -d` = GSASL.EXAMPLE

[realms]
	GSASL.EXAMPLE = {
		kdc = `hostname -f`:17643
	}
EOF

cat <<EOF > $WORKDIR/d/dovecot.conf
dovecot_config_version = 2.4.0
dovecot_storage_version = 2.4.0

protocols = imap

auth_gssapi_hostname = $(hostname -f)
auth_krb5_keytab = $WORKDIR/d/dovecot.keytab

auth_verbose=yes
auth_debug=yes

auth_allow_cleartext = yes
auth_mechanisms {
  gssapi = yes
}
base_dir = $WORKDIR/b
state_dir = $WORKDIR/state

passdb static {
  passdb_static_password=pencil
}

log_path = $WORKDIR/dovecot.log

# https://doc.dovecot.org/2.4.1/core/config/rootless.html

default_internal_user = $USER
default_internal_group = $GROUP

service anvil {
  chroot =
}
service imap-login {
  chroot =
}
service imap-login {
  inet_listener imap {
    port = 17436
  }
  inet_listener imaps {
    port = 0
  }
}
mail_driver = mbox
EOF

if test "$USER" = "root"; then
    cat <<EOF >> $WORKDIR/d/dovecot.conf
default_login_user = nobody
userdb static {
  fields {
    uid = 4711
    gid = 4711
    home = $WORKDIR
  }
}
EOF
else
    cat <<EOF >> $WORKDIR/d/dovecot.conf
default_login_user = $USER
userdb static {
  fields {
    uid = $USER
    gid = $GROUP
    home = $WORKDIR
  }
}
EOF
fi

export KRB5CCNAME=$WORKDIR/cc
export KRB5_CONFIG=$WORKDIR/k/krb5.conf
export KRB5_KDC_PROFILE=$WORKDIR/k

kdb5_util -P foo create -s
kadmin.local addprinc -randkey imap/`hostname -f`
kadmin.local addprinc -pw bar $USER
kadmin.local ktadd -k $WORKDIR/d/dovecot.keytab imap/`hostname -f`

krb5kdc -n -P $WORKDIR/pid &

i=0
while ! (ss -na || netstat -na) | grep 0.0.0.0:17643 | grep LISTEN; do
    i=`expr $i + 1`
    test "$i" = "10" && exit 1
    sleep 1
done

dovecot -c $WORKDIR/d/dovecot.conf || \
    {
	if grep -q 'Fatal: service(imap) access' $WORKDIR/dovecot.log; then
	    echo "Dovecot installed without imap support..."
	    echo "Try 'apt-get install dovecot-imapd dovecot-gssapi'"
	    exit 77
	fi
	exit 1
    }

! $GSASL -m GSSAPI -d --no-starttls --imap `hostname -f` 17436 > $WORKDIR/out-err 2>&1

grep -q 'gss_init_sec_context' $WORKDIR/out-err

echo bar | kinit $USER

# The -z "" is for gsasl < 2.0.1 which set the authorization identity
# to the authentication identity by default.  Dovecot rejects that.
# For gsasl >= 2.0.1 the authorization identity is empty by default.

$GSASL -z "" -m GSSAPI -d --no-starttls --imap `hostname -f` 17436 > $WORKDIR/out-ok 2>&1

grep -q 'OK Logged in' $WORKDIR/out-ok

echo PASS: $0
exit 0