summaryrefslogtreecommitdiffstats
path: root/third_party/heimdal/tests/kdc/check-iprop.in
blob: 524379393faaa8e1706b38314a06cd309da780df (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#!/bin/sh
#
# Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
# (Royal Institute of Technology, Stockholm, Sweden). 
# All rights reserved. 
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met: 
#
# 1. Redistributions of source code must retain the above copyright 
#    notice, this list of conditions and the following disclaimer. 
#
# 2. Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in the 
#    documentation and/or other materials provided with the distribution. 
#
# 3. Neither the name of the Institute nor the names of its contributors 
#    may be used to endorse or promote products derived from this software 
#    without specific prior written permission. 
#
# THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
# ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
# SUCH DAMAGE. 

top_builddir="@top_builddir@"
env_setup="@env_setup@"
objdir="@objdir@"

db_type=@db_type@

. ${env_setup}

# If there is no useful db support compiled in, disable test
${have_db} || exit 77


# Don't run this test in AFS, since it lacks support for AF_UNIX
expr "X`/bin/pwd || pwd`" : "X/afs/.*" > /dev/null 2>/dev/null && exit 77

R=TEST.H5L.SE

port=@port@
ipropport=@ipropport@
ipropport2=@ipropport2@

cache="FILE:${objdir}/cache.krb5"
keytabfile=${objdir}/iprop.keytab
keytab="FILE:${keytabfile}"

kdc="${kdc} --addresses=localhost -P $port"
kadmin="${kadmin} -r $R"
kinit="${kinit} -c $cache ${afs_no_afslog}"

# We'll test iprop, and in particular, hierarchical iprop.  This means we'll
# have a setup like:
#
#  ipropd-master -> ipropd-slave -> 2nd ipropd-master -> 2nd ipropd-slave

# Waiting for incremental propagation is inherently difficult because we don't
# have a way for ipropd-slave to signal this script that it has received
# updates.  Well, it does have a way to signal a possible ipropd-master for
# hierarchical iprop, but we don't have a way to get that signal here.
#
# FIXME: Add a private interface for async waiting for iprop.
#
# What we do is we have a set of utility functions:
#
#  - get_iprop_ver   [N] -> checks that N (default to 1) ops have made it over
#  - get_iprop_ver2  [N] -> same, but for second ipropd-slave instance
#
#  - wait_for            -> repeat a command until it succeeds or too many tries
#  - wait_for_slave  [N] -> wait for N ops to make it over (calls get_iprop_ver)
#  - wait_for_slave2 [N] -> same, but for second ipropd-slave instance
#
# In particular the wait_for* functions busy-wait for a max amount of time,
# with sleeps in between.
#
# NOTE: get_iprop_ver and get_iprop_ver2 keep hidden state.
#
# When first called, get_iprop_ver / get_iprop_ver2 save the current version
# numbers.  Thereafter they check that N ops have been received.
#
# It is critical to account for every incremental op via get_iprop_ver /
# get_iprop_ver2, or wait_for_slave / wait_for_slave2, otherwise this test will
# be racy and will have spurious failures!
#
# The pattern should be something like this:
#
#   echo "Add host"
#   ${kadmin} -l add --random-key --use-defaults host/foo@${R} || exit 1
#   wait_for_slave
#   ^^^^^^^^^^^^^^
#      waits for 1 operation
#
# or
#
#   echo "Rollover host keys"
#   ${kadmin} -l cpw -r --keepold host/foo@${R} || exit 1
#   ${kadmin} -l cpw -r --keepold host/foo@${R} || exit 1
#   ${kadmin} -l cpw -r --keepold host/foo@${R} || exit 1
#   wait_for_slave 3
#   ^^^^^^^^^^^^^^^^
#      waits for the three operations
#
# So though all operations must be accounted for, they need not be accounted
# one by one.

slave_ver_from_master_old=
slave_ver_from_master_new=
slave_ver_old=
slave_ver_new=
get_iprop_ver () {
    min_change=${1:-1}
    slave_ver_from_master_new=`grep '^iprop/' iprop-stats | head -1 | awk '{print $3}'`
    slave_ver_new=`grep 'up-to-date with version:' iprop-slave-status | awk '{print $4}'`
    if [ -z "$slave_ver_from_master_new" -o -z "$slave_ver_new" ]; then
        return 1
    fi
    if [ x"$slave_ver_from_master_new" != x"$slave_ver_new" ]; then
        return 1
    fi
    if [ x"$slave_ver_from_master_old" != x ]; then
        change=`expr "$slave_ver_from_master_new" - "$slave_ver_from_master_old"`
        if [ "$change" -lt "$min_change" ]; then
            return 1
        fi
    fi
    slave_ver_from_master_old=$slave_ver_from_master_new
    slave_ver_old=$slave_ver_new
    return 0
}

slave_ver_from_master_old2=
slave_ver_from_master_new2=
slave_ver_old2=
slave_ver_new2=
get_iprop_ver2 () {
    min_change=${1:-1}
    slave_ver_from_master_new2=`grep '^iprop/' iprop-stats2 | head -1 | awk '{print $3}'`
    slave_ver_new2=`grep 'up-to-date with version:' iprop-slave-status2 | awk '{print $4}'`
    if [ -z "$slave_ver_from_master_new2" -o -z "$slave_ver_new2" ]; then
        return 1
    fi
    if [ x"$slave_ver_from_master_new2" != x"$slave_ver_new2" ]; then
        return 1
    fi
    if [ x"$slave_ver_from_master_old2" != x ]; then
        change=`expr "$slave_ver_from_master_new2" - "$slave_ver_from_master_old2"`
        if [ "$change" -lt "$min_change" ]; then
            return 1
        fi
    fi
    slave_ver_from_master_old2=$slave_ver_from_master_new2
    slave_ver_old2=$slave_ver_new2
    return 0
}

waitsec=65
sleeptime=2
wait_for () {
    msg=$1
    shift
    t=0
    while ! "$@"; do
        sleep $sleeptime;
        t=`expr $t + $sleeptime`
        if [ $t -gt $waitsec ]; then
            echo "Waited too long for $msg"
            exit 1
        fi
    done
    return 0
}

check_pidfile_is_dead () {
    if test ! -f lt-${1}.pid -a ! -f ${1}.pid; then
        return 0
    fi
    _pid=`cat lt-${1}.pid ${1}.pid 2>/dev/null`
    if [ -z "$_pid" ]; then
        return 0
    fi
    if kill -0 $_pid 2>/dev/null; then
        return 1
    fi
    return 0
}

wait_for_slave () {
    wait_for "iprop versions to change and/or slave to catch up" get_iprop_ver "$@"
}

wait_for_slave2 () {
    wait_for "iprop versions to change and/or second slave to catch up" get_iprop_ver2 "$@"
}

wait_for_master_down () {
    wait_for "master to exit" check_pidfile_is_dead ipropd-master
}

wait_for_slave_down () {
    wait_for "slave to exit" check_pidfile_is_dead ipropd-slave
}

KRB5_CONFIG="${objdir}/krb5.conf"
export KRB5_CONFIG

rm -f ${keytabfile}
rm -f current-db*
rm -f current*.log
rm -f out-*
rm -f mkey.file*
rm -f messages.log messages.log

> messages.log
> messages.log2

echo Creating database
${kadmin} -l \
    init \
    --realm-max-ticket-life=1day \
    --realm-max-renewable-life=1month \
    ${R} || exit 1

${kadmin} -l add -p foo --use-defaults user@${R} || exit 1

${kadmin} -l add --random-key --use-defaults iprop/localhost@${R} || exit 1
${kadmin} -l ext -k ${keytab} iprop/localhost@${R} || exit 1
${kadmin} -l add --random-key --use-defaults iprop/slave.test.h5l.se@${R} || exit 1
${kadmin} -l ext -k ${keytab} iprop/slave.test.h5l.se@${R} || exit 1

echo foo > ${objdir}/foopassword

echo "Test log recovery"
${kadmin} -l add --random-key --use-defaults recovtest@${R} || exit 1
# Test theory: save the log, make a change and save the record it
# produced, restore the log, append to it the saved record, then add dummy
# record.

# Save the log
cp current.log current.log.tmp
ls -l current.log.tmp | awk '{print $5}' > tmp
read sz < tmp
# Make a change
${kadmin} -l mod -a requires-pre-auth recovtest@${R} || exit 1
${kadmin} -l get recovtest@${R} | grep 'Attributes: requires-pre-auth$' > /dev/null || exit 1
# Save the resulting log record
ls -l current.log | awk '{print $5}' > tmp
read nsz < tmp
rm tmp
dd bs=1 if=current.log skip=$sz of=current.log.tmp.saved-record count=`expr $nsz - $sz` 2>/dev/null
# Undo the change
${kadmin} -l mod -a -requires-pre-auth recovtest@${R} || exit 1
${kadmin} -l get recovtest@${R} | grep 'Attributes:.$' > /dev/null || exit 1
# Restore the log
cp current.log current.log.save
mv current.log.tmp current.log
# Append the saved record
cat current.log.tmp.saved-record >> current.log
rm current.log.tmp.saved-record
# Check that we still see the principal as modified after another write forcing
# log recovery.
${kadmin} -l add --random-key --use-defaults dummy@${R} || exit 1
${kadmin} -l del dummy@${R} || exit 1
${kadmin} -l get recovtest@${R} | grep 'Attributes: requires-pre-auth$' > /dev/null || exit 1

# -- foo
ipds=
ipdm=
kdcpid=

> iprop-stats
> iprop-stats2
rm -f iprop-slave-status iprop-slave-status2

ipropd_slave2=$ipropd_slave
ipropd_master2=$ipropd_master
ipropd_slave="${ipropd_slave} --status-file=iprop-slave-status --port=$ipropport"
ipropd_slave="${ipropd_slave} --hostname=slave.test.h5l.se -k ${keytab}"
ipropd_slave="${ipropd_slave} --detach localhost"
ipropd_master="${ipropd_master} --hostname=localhost -k ${keytab}"
ipropd_master="${ipropd_master} --port=$ipropport"
ipropd_master="${ipropd_master} --database=${objdir}/current-db --detach"

ipropd_slave2="${ipropd_slave2} --status-file=iprop-slave-status2 --port=$ipropport2"
ipropd_slave2="${ipropd_slave2} --hostname=slave.test.h5l.se -k ${keytab}"
ipropd_slave2="${ipropd_slave2} --pidfile-basename=ipropd-slave2"
ipropd_slave2="${ipropd_slave2} --detach localhost"
ipropd_master2="${ipropd_master2} --hostname=localhost -k ${keytab}"
ipropd_master2="${ipropd_master2} --port=$ipropport2"
ipropd_master2="${ipropd_master2} --pidfile-basename=ipropd-master2"
ipropd_master2="${ipropd_master2} --database=${objdir}/current-db.slave --detach"

cleanup() {
    echo 'killing ipropd s + m + kdc'
    test -n "$ipdm" && kill -9 $ipdm        >/dev/null 2>/dev/null
    test -n "$ipdm2" && kill -9 $ipdm2      >/dev/null 2>/dev/null
    test -n "$ipds" && kill -9 $ipds        >/dev/null 2>/dev/null
    test -n "$ipds2" && kill -9 $ipds2      >/dev/null 2>/dev/null
    test -n "$kdcpid" && kill -9 $kdcpid    >/dev/null 2>/dev/null
    tail messages.log
    tail iprop-stats
    exit 1
}
trap cleanup EXIT

echo Starting kdc ; > messages.log
${kdc} --detach --testing || { echo "kdc failed to start"; cat messages.log; exit 1; }
kdcpid=`getpid kdc`

echo "starting master" ; > messages.log
env ${HEIM_MALLOC_DEBUG} \
${ipropd_master} || { echo "ipropd-master failed to start"; exit 1; }
ipdm=`getpid ipropd-master`

echo "starting slave" ; > messages.log
env ${HEIM_MALLOC_DEBUG} \
KRB5_CONFIG="${objdir}/krb5-slave.conf" \
${ipropd_slave} || { echo "ipropd-slave failed to start"; exit 1; }
ipds=`getpid ipropd-slave`
sh ${wait_kdc} ipropd-slave messages.log 'slave status change: up-to-date' || exit 1
get_iprop_ver || exit 1

echo "checking slave is up"
${EGREP} 'iprop/slave.test.h5l.se@TEST.H5L.SE.*Up' iprop-stats >/dev/null || exit 1
${EGREP} 'up-to-date with version' iprop-slave-status >/dev/null || { echo "slave to up to date" ; cat iprop-slave-status ; exit 1; }

# Also setup a second master on the slave, then a second slave to pull from the
# second master.
echo "starting master2" ; > messages.log
env ${HEIM_MALLOC_DEBUG} \
KRB5_CONFIG="${objdir}/krb5-master2.conf" \
${ipropd_master2} || { echo "second ipropd-master failed to start"; exit 1; }
ipdm2=`getpid ipropd-master2`

echo "starting slave2" ; > messages.log
env ${HEIM_MALLOC_DEBUG} \
KRB5_CONFIG="${objdir}/krb5-slave2.conf" \
${ipropd_slave2} || { echo "ipropd-slave failed to start"; exit 1; }
ipds2=`getpid ipropd-slave2`
sh ${wait_kdc} ipropd-slave messages2.log 'slave status change: up-to-date' || exit 1
wait_for "Slave sees new host" get_iprop_ver2 0 || exit 1

# ----------------- checking: pushing lives changes

slave_get() { KRB5_CONFIG="${objdir}/krb5-slave.conf" ${kadmin} -l get "$@"; }
slave_check_exists() {
    # Creation with a random key is not atomic, there are at present
    # 3 log entries to create a random key principal, the entry is
    # "invalid" for the first two of these.  We wait for the entry to
    # exist and not be invalid
    #
    attrs=`slave_get -o attributes "$@" 2>/dev/null` || return 1
    echo $attrs | egrep 'Attributes:' | egrep -v invalid >/dev/null || return 1
    get_iprop_ver 0
}

echo "Add host"
${kadmin} -l add --random-key --use-defaults host/foo@${R} || exit 1
wait_for_slave
wait_for "Slave sees new host" slave_check_exists "host/foo@${R}"

echo "Rollover host keys"
${kadmin} -l cpw -r --keepold host/foo@${R} || exit 1
${kadmin} -l cpw -r --keepold host/foo@${R} || exit 1
${kadmin} -l cpw -r --keepold host/foo@${R} || exit 1
wait_for_slave 3
slave_get host/foo@${R} | \
    ${EGREP} Keytypes: | cut -d: -f2 | tr ' ' '
' | sed 's/^.*[[]\(.*\)[]].*$/\1/' | grep '[0-9]' | sort -nu | tr -d '
' | ${EGREP} 1234 > /dev/null || exit 1

wait_for_slave2 4

echo "Delete 3DES keys"
${kadmin} -l del_enctype host/foo@${R} des3-cbc-sha1
wait_for_slave
KRB5_CONFIG="${objdir}/krb5-slave.conf" \
${kadmin} -l get host/foo@${R} | \
    ${EGREP} Keytypes: | cut -d: -f2 | tr ' ' '
' | sed 's/^.*[[]\(.*\)[]].*$/\1/' | grep '[0-9]' | sort -nu | tr -d '
' | ${EGREP} 1234 > /dev/null || exit 1
KRB5_CONFIG="${objdir}/krb5-slave.conf" \
${kadmin} -l get host/foo@${R} | \
    ${EGREP} 'Keytypes:.*des3-cbc-sha1' > /dev/null && exit 1

echo "Change policy host"
${kadmin} -l modify --policy=default host/foo@${R} || exit 1
wait_for_slave
KRB5_CONFIG="${objdir}/krb5-slave.conf" \
${kadmin} -l get host/foo@${R} > /dev/null 2>/dev/null || exit 1

echo "Rename host"
${kadmin} -l rename host/foo@${R} host/bar@${R} || exit 1
wait_for_slave
KRB5_CONFIG="${objdir}/krb5-slave.conf" \
${kadmin} -l get host/foo@${R} > /dev/null 2>/dev/null && exit 1
KRB5_CONFIG="${objdir}/krb5-slave.conf" \
${kadmin} -l get host/bar@${R} > /dev/null || exit 1

wait_for_slave2 3

echo "Delete host"
${kadmin} -l delete host/bar@${R} || exit 1
wait_for_slave
KRB5_CONFIG="${objdir}/krb5-slave.conf" \
${kadmin} -l get host/bar@${R} > /dev/null 2>/dev/null && exit 1

# See note below in LMDB sanity checking
echo "Re-add host"
${kadmin} -l add --random-key --use-defaults host/foo@${R} || exit 1
${kadmin} -l add --random-key --use-defaults host/bar@${R} || exit 1
wait_for_slave 2
wait_for "Slave sees re-added host" slave_check_exists "host/bar@${R}"

wait_for_slave2 3

echo "kill slave and remove log and database"
> iprop-stats
sh ${leaks_kill} ipropd-slave $ipds || exit 1
rm -f iprop-slave-status

wait_for_slave_down
${EGREP} 'iprop/slave.test.h5l.se@TEST.H5L.SE.*Down' iprop-stats >/dev/null || exit 1

# ----------------- checking: slave is missing changes while down

rm current.slave.log current-db.slave* || exit 1

echo "doing changes while slave is down"
${kadmin} -l cpw --random-password user@${R} > /dev/null || exit 1
${kadmin} -l cpw --random-password user@${R} > /dev/null || exit 1

echo "Making a copy of the master log file"
cp ${objdir}/current.log ${objdir}/current.log.tmp

# ----------------- checking: checking that master and slaves resyncs

echo "starting slave again" ; > messages.log
> iprop-stats
env ${HEIM_MALLOC_DEBUG} \
KRB5_CONFIG="${objdir}/krb5-slave.conf" \
${ipropd_slave} || { echo "ipropd-slave failed to start"; exit 1; }
ipds=`getpid ipropd-slave`

echo "checking slave is up again"
wait_for "slave to start and connect to master" \
    ${EGREP} 'iprop/slave.test.h5l.se@TEST.H5L.SE.*Up' iprop-stats >/dev/null
wait_for_slave 2
wait_for_slave2 2
${EGREP} 'up-to-date with version' iprop-slave-status >/dev/null || { echo "slave not up to date" ; cat iprop-slave-status ; exit 1; }
echo "checking for replay problems"
${EGREP} 'Entry already exists in database' messages.log && exit 1

echo "compare versions on master and slave logs (no lock)"
KRB5_CONFIG=${objdir}/krb5-slave.conf \
${iprop_log} last-version -n > slave-last.tmp
${iprop_log} last-version -n > master-last.tmp
cmp master-last.tmp slave-last.tmp || exit 1

echo "kill slave and remove log and database"
sh ${leaks_kill} ipropd-slave $ipds || exit 1
wait_for_slave_down

rm current.slave.log current-db.slave* || exit 1
> iprop-stats
rm -f iprop-slave-status
echo "starting slave" ; > messages.log
env ${HEIM_MALLOC_DEBUG} \
KRB5_CONFIG="${objdir}/krb5-slave.conf" \
${ipropd_slave} || { echo "ipropd-slave failed to start"; exit 1; }
ipds=`getpid ipropd-slave`
wait_for_slave 0

echo "checking slave is up again"
wait_for "slave to start and connect to master" \
    ${EGREP} 'iprop/slave.test.h5l.se@TEST.H5L.SE.*Up' iprop-stats >/dev/null
${EGREP} 'up-to-date with version' iprop-slave-status >/dev/null || { echo "slave not up to date" ; cat iprop-slave-status ; exit 1; }
echo "checking for replay problems"
${EGREP} 'Entry already exists in database' messages.log && exit 1

# ----------------- checking: checking live truncation of master log

${kadmin} -l cpw --random-password user@${R} > /dev/null || exit 1
wait_for_slave
wait_for_slave2

echo "live truncate on master log"
${iprop_log} truncate -K 5 || exit 1
wait_for_slave 0

echo "Killing master and slave"
sh ${leaks_kill} ipropd-master $ipdm || exit 1
sh ${leaks_kill} ipropd-slave $ipds || exit 1

rm -f iprop-slave-status

wait_for_slave_down
wait_for_master_down

echo "compare versions on master and slave logs"
KRB5_CONFIG=${objdir}/krb5-slave.conf \
${iprop_log} last-version > slave-last.tmp
${iprop_log} last-version > master-last.tmp
cmp master-last.tmp slave-last.tmp || exit 1

# ----------------- checking: master going backward
> iprop-stats
> messages.log

echo "Going back to old version of the master log file"
cp ${objdir}/current.log.tmp ${objdir}/current.log

echo "starting master"  ; > messages.log
env ${HEIM_MALLOC_DEBUG} \
${ipropd_master} || { echo "ipropd-master failed to start"; exit 1; }
ipdm=`getpid ipropd-master`

echo "starting slave" ; > messages.log
env ${HEIM_MALLOC_DEBUG} \
KRB5_CONFIG="${objdir}/krb5-slave.conf" \
${ipropd_slave} || { echo "ipropd-slave failed to start"; exit 1; }
ipds=`getpid ipropd-slave`
wait_for_slave -1

echo "checking slave is up again"
wait_for "slave to start and connect to master" \
    ${EGREP} 'iprop/slave.test.h5l.se@TEST.H5L.SE.*Up' iprop-stats >/dev/null
${EGREP} 'up-to-date with version' iprop-slave-status >/dev/null || { echo "slave to up to date" ; cat iprop-slave-status ; exit 1; }
echo "checking for replay problems"
${EGREP} 'Entry already exists in database' messages.log && exit 1

echo "pushing one change"
${kadmin} -l cpw --random-password user@${R} > /dev/null || exit 1
wait_for_slave
wait_for_slave2 0

echo "Killing master"
sh ${leaks_kill} ipropd-master $ipdm || exit 1

wait_for_master_down

wait_for "slave to disconnect" \
  ${EGREP} 'disconnected' iprop-slave-status >/dev/null

if ! tail -30 messages.log | grep 'disconnected for server' > /dev/null; then
    echo "client didnt disconnect"
    exit 1
fi

echo "probing for slave pid"
kill -0 ${ipds}  || { echo "slave no longer there"; exit 1; }

> messages.log

echo "Staring master again" ; > messages.log
env ${HEIM_MALLOC_DEBUG} \
${ipropd_master} || { echo "ipropd-master failed to start"; exit 1; }
ipdm=`getpid ipropd-master`

echo "probing for slave pid"
kill -0 ${ipds}  || { echo "slave no longer there"; exit 1; }


echo "pushing one change"
${kadmin} -l cpw --random-password user@${R} > /dev/null || exit 1
wait_for_slave
wait_for_slave2

echo "shutting down all services"

leaked=false
sh ${leaks_kill} kdc $kdcpid || leaked=true
sh ${leaks_kill} ipropd-master $ipdm || leaked=true
sh ${leaks_kill} ipropd-slave $ipds || leaked=true
sh ${leaks_kill} ipropd-master $ipdm2 || leaked=true
sh ${leaks_kill} ipropd-slave $ipds2 || leaked=true
rm -f iprop-slave-status
trap "" EXIT
$leaked && exit 1

echo "compare versions on master and slave logs"
KRB5_CONFIG=${objdir}/krb5-slave.conf \
${iprop_log} last-version > slave-last.tmp
${iprop_log} last-version > master-last.tmp
cmp master-last.tmp slave-last.tmp || exit 1

if [ "$db_type" = lmdb ] && type mdb_stat > /dev/null 2>&1; then
    # Sanity check that we have the same number of principals at the HDB
    # and LMDB levels.
    #
    # We should also do this for the sqlite backend, but that would
    # require a sqlite3(1) shell that is capable of opening our HDB
    # files.
    echo "checking that principals in DB == entries in LMDB"
    # Add one to match lmdb overhead
    princs=`(echo; ${kadmin} -l list '*') | wc -l`
    entries=`mdb_stat -n current-db.mdb | grep 'Entries:' | awk '{print $2}'`
    [ "$princs" -eq "$entries" ] || exit 1
fi

exit 0