-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testdocker
executable file
·1461 lines (1423 loc) · 44.2 KB
/
testdocker
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
#
# The way this works is like this
# on the host we create a local domain $domain1 with user $user
# on the container we create a local domain $domain2 with alias $user
# on the container we create a virtual domain $domain3 with user $user
# on the host we create basedir=$testdir/home and share it as /home on container.
# on the host we have assign file for user $user with home as $testdir/$user
# on the host we run qmail-smtpd on $localip:$smtp_port
# on the container we create smtproute to route mails for $domain1 to $localip:$smtp_port
# on the container we create alias $user@$domain2 forwards mails to $user@$domain1
# so checking $testdir/$user/Maildir is good enough to test mail receipt
# since /home/mail on container is accessible in $testdir/home/mail, we can test
# mail receipt locally for user's Maildirs on the container.
#
localip=192.168.2.107
#
raw_url=https://raw.githubusercontent.com/mbhangui/indimail-virtualdomains/master/indimail-x/tests/testindimail-virtual
testdir=/tmp/qmail-test
user=$(whoami)
myuid=$(id -u)
mygid=$(id -g)
localuser=luser
maildir=$testdir/$user/Maildir
basedir=$testdir/home
servicedir=$testdir/service
sysconfdir=$testdir/etc/indimail
cntrldir=$sysconfdir/control
certdir=$sysconfdir/certs
logdir=$testdir/logs
bindir=/usr/bin
sbindir=/usr/sbin
qmail_newu=$sbindir/qmail-newu
qmail_start=$sbindir/qmail-start
qmail_smtpd=$sbindir/qmail-smtpd
tcpserver=$bindir/tcpserver
smtp_port=2050
sleep_int=2
HOSTNAME=$(uname -n)
domain1=local.indimail.org
domain2=podman.indimail.org
domain3=virtual.indimail.org
domain4=example.com
password="aBcdefgh12@34"
MYSQL_USER=indimail
MYSQL_PASS="ssh-1.5-"
MYSQL_DB="indimail"
MYSQL_HOST=localhost
MYSQL_PORT=3307
setup_localuser()
{
out=$(mktemp -t testdockXXXXXXX)
(
echo "#!/bin/sh"
echo "localuser=$localuser"
echo "password=$password"
echo "getent passwd \$localuser >/dev/null"
echo "if [ \$? -eq 2 ] ; then"
echo " if [ -f /etc/alpine-release ] ; then"
echo " adduser \$localuser -D"
echo " else"
echo " useradd \$localuser"
echo " fi"
echo " mkdir -p /home/\$localuser"
echo " if [ -f /etc/redhat-release ] ; then"
echo " echo \$password | passwd --stdin \$localuser"
echo " elif [ -f /etc/gentoo-release ] ; then"
echo " echo \"\$localuser:\$password\" | chpasswd"
echo " else"
echo " printf \"%s\\\n%s\\\n\" \$password \$password | passwd \$localuser"
echo " fi"
echo "fi"
echo "if [ ! -d /home/\$localuser/Maildir ] ; then"
echo " qmaildirmake /home/\$localuser/Maildir"
echo "fi"
echo "gid=\$(id -g $localuser)"
echo "chown -R \$localuser:\$gid /home/\$localuser"
echo "/usr/sbin/usermod -aG qcerts \$localuser"
echo "/usr/sbin/usermod -aG indimail \$localuser"
echo "if [ -d /etc/sudoers.d -a ! -f /etc/sudoers.d/01-\$localuser ] ; then"
echo " echo \"\$localuser ALL=(ALL) NOPASSWD: ALL\" > /etc/sudoers.d/01-\$localuser"
echo "fi"
echo "if [ -f /usr/bin/sudo ] ; then"
echo " exit 0"
echo "fi"
echo "if [ -x /usr/bin/dnf ] ; then"
echo " dnf -y install sudo"
echo "elif [ -x /usr/bin/yum ] ; then"
echo " yum -y install sudo"
echo "elif [ -x /usr/bin/apt-get ] ; then"
echo " env DEBIAN_FRONTEND=noninteractive apt-get -y install sudo"
echo "elif [ -x /sbin/apk ] ; then"
echo " /sbin/apk install sudo"
echo "fi"
echo "if [ -d /etc/sudoers.d -a ! -f /etc/sudoers.d/01-\$localuser ] ; then"
echo " echo \"\$localuser ALL=(ALL) NOPASSWD: ALL\" > /etc/sudoers.d/01-\$localuser"
echo "fi"
) > $out
mv $out /tmp/create_testuser
chmod +x /tmp/create_testuser
}
check_relay()
{
TM=$(expr $(date +%s) - 1800)
echo "SELECT email,ipaddr from relay where timestamp > $TM" | \
mysql -s -u $MYSQL_USER -p$MYSQL_PASS -h $MYSQL_HOST -P $MYSQL_PORT $MYSQL_DB 2>/dev/null
return $?
}
select_relay()
{
TM=$(expr $(date +%s) - 1800)
echo "SELECT email,ipaddr from relay where timestamp > $TM" | \
mysql -s -u $MYSQL_USER -p$MYSQL_PASS -h $MYSQL_HOST -P $MYSQL_PORT $MYSQL_DB 2>/dev/null
echo "------------------------------------------"
echo "SELECT email,ipaddr from relay" | \
mysql -s -u $MYSQL_USER -p$MYSQL_PASS -h $MYSQL_HOST -P $MYSQL_PORT $MYSQL_DB 2>/dev/null
}
delete_relay()
{
echo "DELETE from relay where 1=1" | \
mysql -s -u $MYSQL_USER -p$MYSQL_PASS -h $MYSQL_HOST -P $MYSQL_PORT $MYSQL_DB 2>/dev/null
return $?
}
check_service()
{
if [ $# -eq 3 ] ; then
min_up=$3
else
min_up=10
fi
count=1
while true
do
[ $verbose -eq 1 ] && echo "Checking Service $2 count=$count" 1>&2
str=$($podcmd exec $1 svstat /service/$2 2>&1)
if [ -z "$str" ] ; then
echo "Service $2 is not up. unable to get service status" 1>&2
return 2
fi
echo $str | grep -E -i "warning:|Error:" > /dev/null
if [ $? -ne 0 ] ; then
t=$(echo $str | awk '{print $2}')
s=$(echo $str | awk '{print $3}')
else
[ $verbose -eq 1 ] && echo "svscan maybe down or just starting: [$str]"
sleep 5;
count=$(expr $count + 1)
if [ $count -gt 12 ] ; then
break
fi
continue
fi
if [ "$t" = "up" ] ; then
if [ $s -ge $min_up ] ; then
[ $verbose -eq 1 ] && echo "Service $2 is up: [$str]"
return 0
else
sleep $(expr $min_up - $s)
count=$(expr $count + 1)
if [ $count -gt 12 ] ; then
break
fi
continue
fi
elif [ "$t" = "wait" -a $s -gt 0 ] ; then
[ $verbose -eq 1 ] && echo "Service $2 is waiting for $s secs: [$str]"
sleep $s
count=$(expr $count + 1)
if [ $count -gt 12 ] ; then
break
fi
continue
elif [ "$t" = "down" -o $s -eq 0 ] ; then
[ $verbose -eq 1 ] && echo "Service $2 is down: [$str]"
count=$(expr $count + 1)
else
echo "Service $2 is not up: [$str]" 1>&2
return 2
fi
sleep 5;
if [ $count -gt 12 ] ; then
break
fi
done
echo "Service $2 is not up: [$str]" 1>&2
return 1
}
test_pop3()
{
[ $# -eq 5 ] && do_relay=1 || do_relay=0
t=$(echo $1 | cut -d@ -f2)
if [ "$t" = "$1" ] ; then
from=$1@$domain2
to=$1@$domain2
else
from=$1
to=$1
fi
[ $verbose -eq 1 ] && echo swaks --tls --to $to --from $from --server 127.0.0.1 --port 2587 -a LOGIN -au $1 -ap "$2"
swaks --tls --to $to --from $from --server 127.0.0.1 --port 2587 -a LOGIN -au $1 -ap "$2" >> $logdir/pop3d/pop3d.log 2>&1
[ $verbose -eq 1 ] && echo "test_pop3: Checking mail receipt in $4"
check_mail $4 no-delete
if [ $? -ne 0 ] ; then
fcount=$(expr $fcount + 1)
printf " testing email to POP3 account %s failed\n" "$1"
cat $logdir/pop3d/pop3d.log
[ -z "$failed" ] && failed="swaks:2587-sendmail-$what:$image" || failed="$failed swaks:2587-sendmail-$what:$image"
[ $halt_on_error -eq 1 ] && echo "Press ENTER to continue" && read key
return 1
fi
(
echo "#!/bin/sh"
echo "exec 0<&6"
echo "exec 1>&7"
echo "read line"
echo "printf \"%s\n\" \"\$line\" 1>&2"
echo "printf \"user $1\r\n\" 1>&2"
echo "printf \"user $1\r\n\""
echo "read line"
echo "printf \"%s\n\" \"\$line\" 1>&2"
echo "printf \"%s\" \"\$line\" |grep \"+OK Password required.\" >/dev/null"
echo "if [ \$? -ne 0 ] ; then"
echo " printf \"%s\n\" \"\$line\" 1>&2"
echo " echo \"Failed to get password prompt\" 1>&2"
echo " exit 1"
echo "else"
echo " printf \"pass %s\n\" \"$2\" 1>&2"
echo " printf \"pass %s\n\" \"$2\""
echo "fi"
echo "read line"
echo "printf \"%s\n\" \"\$line\" 1>&2"
echo "printf \"%s\" \"\$line\" | grep \"+OK logged in.\" > /dev/null"
echo "if [ \$? -ne 0 ] ; then"
echo " echo \"Failed to login\" 1>&2"
echo " exit 1"
echo "fi"
echo "printf \"LIST\r\n\" 1>&2"
echo "printf \"LIST\r\n\""
echo "read line"
echo "printf \"%s\" \"\$line\" |grep \"+OK POP3 clients that break here, they violate STD53.\" > /dev/null"
echo "if [ \$? -ne 0 ] ; then"
echo " echo \"LIST command failed\" 1>&2"
echo " exit 1"
echo "fi"
echo "read line"
echo "printf \"%s\n\" \"\$line\" 1>&2"
echo "octets=\$(printf \"%s\n\" \"\$line\"|awk '{print \$2}')"
echo "while true"
echo "do"
echo " read line"
echo " if [ -z \"\$line\" ] ; then"
echo " break"
echo " fi"
echo " printf \"%s\n\" \"\$line\" 1>&2"
echo " line=\$(echo \"\$line\"|tr -d '\r')"
echo " if [ -z \"\$line\" -o \"\$line\" = \".\" ] ; then"
echo " break"
echo " fi"
echo "done"
echo "printf \"RETR 1\r\n\" 1>&2"
echo "printf \"RETR 1\r\n\""
echo "read line"
echo "printf \"%s\n\" \"\$line\" 1>&2"
echo "printf \"%s\" \"\$line\" | grep -E \"+OK .* octets follow.\" > /dev/null"
echo "if [ \$? -ne 0 ] ; then"
echo " echo \"RETR command failed\" 1>&2"
echo " exit 1"
echo "fi"
echo "found=0"
echo "while true"
echo "do"
echo " read line"
echo " if [ -z \"\$line\" ] ; then"
echo " break"
echo " fi"
echo " printf \"%s\n\" \"\$line\" 1>&2"
echo " line=\$(echo \"\$line\"|tr -d '\r')"
echo " if [ \"\$line\" = \".\" ] ; then"
echo " break"
echo " fi"
echo " echo \"\$line\" | grep \"This is a test mailing\" > /dev/null"
echo " if [ \$? -eq 0 ] ; then"
echo " found=1"
echo " fi"
echo "done"
echo "printf \"QUIT\r\n\""
echo "read line"
echo "printf \"%s\n\" \"\$line\" 1>&2"
echo "if [ \$found -eq 1 ] ; then"
echo " exit 0"
echo "else"
echo " exit 1"
echo "fi"
) > $testdir/tcpclient.pop3
chmod +x $testdir/tcpclient.pop3
check_service $name qmail-pop3d.110 3
tcpclient -a 10 -vDHR 127.0.0.1 2110 /tmp/qmail-test/tcpclient.pop3 2>$logdir/pop3d/pop3d.log
if [ $? -eq 0 ] ; then
tcount=$(expr $tcount + 1)
printf " testing POP3 login %s and mail retrieval succeeded\n" "$1"
else
/bin/rm -f $testdir/tcpclient.pop3
printf " testing POP3 login %s and mail retrieval failed\n" "$1"
cat $logdir/pop3d/pop3d.log
[ -z "$failed" ] && failed="pop3d:2110-$what:$image" || failed="$failed pop3d:2110-$what:$image"
[ $halt_on_error -eq 1 ] && echo "Press ENTER to continue" && read key
return 1
fi
if [ $do_relay -eq 1 ] ; then
check_relay > /dev/null
if [ $? -eq 0 ] ; then
tcount=$(expr $tcount + 1)
printf " testing record in relay table succeeded\n"
else
printf " testing record in relay table failed\n"
select_relay
delete_relay
cat $logdir/pop3d/pop3d.log
[ -z "$failed" ] && failed="relay-pop3d-$what:$image" || failed="$failed relay-pop3d-$what:$image"
[ $halt_on_error -eq 1 ] && echo "Press ENTER to continue" && read key
return 1
fi
swaks -S --to tuser01@$domain4 --from $from --server 127.0.0.1 --port 2025 >> $logdir/pop3d/pop3d.log 2>&1
ret1=$?
swaks -S --to tuser01@$domain4 --from $3 --server 127.0.0.1 --port 2025 >> $logdir/pop3d/pop3d.log 2>&1
ret2=$?
if [ $ret1 -eq 0 -a $ret2 -ne 0 ] ; then
tcount=$(expr $tcount + 1)
printf " testing Open RELAY using POP3 login succeeded\n"
else
/bin/rm -f $testdir/tcpclient.pop3
printf " testing Open RELAY using POP3 login failed ret1=%d ret2=%d\n" $ret1 $ret2
select_relay
delete_relay
cat $logdir/pop3d/pop3d.log
[ -z "$failed" ] && failed="relay-pop3d-$what:$image" || failed="$failed relay-pop3d-$what:$image"
[ $halt_on_error -eq 1 ] && echo "Press ENTER to continue" && read key
return 1
fi
fi
check_service $name qmail-pop3d-ssl.995 3
tcpclient -a 10 -n $certdir/clientcert.pem -vDHR 127.0.0.1 2995 /tmp/qmail-test/tcpclient.pop3 2>$logdir/pop3d-ssl/pop3d-ssl.log
if [ $? -eq 0 ] ; then
tcount=$(expr $tcount + 1)
printf " testing POP3S login %s and mail retrieval succeeded\n" "$1"
else
/bin/rm -f $testdir/tcpclient.pop3
if [ $do_relay -eq 1 ] ; then
delete_relay
fi
printf " testing POP3S login %s and mail retrieval failed\n" "$1"
cat $logdir/pop3d-ssl/pop3d-ssl.log
[ -z "$failed" ] && failed="pop3s:2995-$what:$image" || failed="$failed pop3s:2995-$what:$image"
[ $halt_on_error -eq 1 ] && echo "Press ENTER to continue" && read key
return 1
fi
/bin/rm -f $testdir/tcpclient.pop3
if [ $do_relay -eq 1 ] ; then
swaks -S --to tuser01@$domain4 --from $from --server 127.0.0.1 --port 2025 >> $logdir/pop3d/pop3d.log 2>&1
ret1=$?
swaks -S --to tuser01@$domain4 --from $3 --server 127.0.0.1 --port 2025 >> $logdir/pop3d/pop3d.log 2>&1
ret2=$?
if [ $ret1 -eq 0 -a $ret2 -ne 0 ] ; then
delete_relay
tcount=$(expr $tcount + 1)
printf " testing Open RELAY using POP3S login succeeded\n"
else
printf " testing Open RELAY using POP3S login failed ret1=%d ret2=%d\n" $ret1 $ret2
select_relay
delete_relay
cat $logdir/pop3d-ssl/pop3d-ssl.log
[ -z "$failed" ] && failed="relay-pop3d-$what:$image" || failed="$failed relay-pop3d-$what:$image"
[ $halt_on_error -eq 1 ] && echo "Press ENTER to continue" && read key
return 1
fi
fi
}
test_imap()
{
[ $# -eq 5 ] && do_relay=1 || do_relay=0
t=$(echo $1 | cut -d@ -f2)
if [ "$t" = "$1" ] ; then
from=$1@$domain2
to=$1@$domain2
else
from=$1
to=$1
fi
[ $verbose -eq 1 ] && echo swaks --tls --to $to --from $from --server 127.0.0.1 --port 2587 -a LOGIN -au $1 -ap "$2"
swaks -S --tls --to $to --from $from --server 127.0.0.1 --port 2587 -a LOGIN -au $1 -ap "$2" >> $logdir/imapd/imapd.log 2>&1
[ $verbose -eq 1 ] && echo "test_imap: Checking mail receipt in $4"
check_mail $4 no-delete
if [ $? -ne 0 ] ; then
fcount=$(expr $fcount + 1)
printf " testing email to IMAP account %s failed\n" "$1"
cat $logdir/imapd/imapd.log
[ -z "$failed" ] && failed="swaks:2587-sendmail-$what:$image" || failed="$failed swaks:2587-sendmail-$what:$image"
[ $halt_on_error -eq 1 ] && echo "Press ENTER to continue" && read key
return 1
fi
(
echo "#!/bin/sh"
echo "exec 0<&6"
echo "exec 1>&7"
echo "read key"
echo "printf \"%s\n\" \"\$key\" 1>&2"
echo "printf \"a1 login $1 $2\r\n\""
echo "read key"
echo "printf \"%s\" \"\$key\" |grep \"a1 OK LOGIN Ok.\" >/dev/null"
echo "if [ \$? -ne 0 ] ; then"
echo " printf \"%s\n\" \"\$key\" 1>&2"
echo " echo \"LOGIN Failed\" 1>&2"
echo " exit 1"
echo "else"
echo " printf \"%s\n\" \"\$key\" 1>&2"
echo "fi"
echo "printf \"a1 select inbox\r\n\""
echo "while true"
echo "do"
echo " read line"
echo " if [ -z \"\$line\" ] ; then"
echo " break"
echo " fi"
echo " t=\$(echo \"\$line\" | cut -d' ' -f1)"
echo " if [ \"\$t\" = \"*\" ] ; then"
echo " echo \"\$line\" 1>&2"
echo " continue"
echo " elif [ \"\$t\" = \"a1\" ] ; then"
echo " break"
echo " else"
echo " echo \"\$line\" 1>&2"
echo " fi"
echo "done"
echo ""
echo "printf \"a1 fetch 1 RFC822\r\n\""
echo "found=0"
echo "while true"
echo "do"
echo " read line"
echo " if [ -z \"\$line\" ] ; then"
echo " break"
echo " fi"
echo " echo \"\$line\" | grep \"a1 OK FETCH completed.\" > /dev/null"
echo " if [ \$? -eq 0 ] ; then"
echo " break"
echo " fi"
echo " echo \"\$line\" | grep \"This is a test mailing\" > /dev/null"
echo " if [ \$? -eq 0 ] ; then"
echo " found=1"
echo " fi"
echo " echo \"\$line\" 1>&2"
echo "done"
echo "printf \"a1 logout\r\n\""
echo "read line"
echo "printf \"%s\n\" \"\$line\""
echo "if [ \$found -eq 1 ] ; then"
echo " exit 0"
echo "else"
echo " exit 1"
echo "fi"
) > $testdir/tcpclient.imap
chmod +x $testdir/tcpclient.imap
check_service $name qmail-imapd.143 3
tcpclient -a 10 -vDHR 127.0.0.1 2143 /tmp/qmail-test/tcpclient.imap 2>$logdir/imapd/imapd.log
if [ $? -eq 0 ] ; then
tcount=$(expr $tcount + 1)
printf " testing IMAP login %s and mail retrieval succeeded\n" "$1"
else
/bin/rm -f $testdir/tcpclient.imap
fcount=$(expr $fcount + 1)
printf " testing IMAP login %s and mail retrieval failed\n" "$1"
cat $logdir/imapd/imapd.log
[ -z "$failed" ] && failed="imapd:2143-$what:$image" || failed="$failed imapd:2143-$what:$image"
[ $halt_on_error -eq 1 ] && echo "Press ENTER to continue" && read key
return 1
fi
if [ $do_relay -eq 1 ] ; then
check_relay > /dev/null
if [ $? -eq 0 ] ; then
tcount=$(expr $tcount + 1)
printf " testing record in relay table succeeded\n"
else
/bin/rm -f $testdir/tcpclient.imap
fcount=$(expr $fcount + 1)
printf " testing record in relay table failed\n"
select_relay
delete_relay
cat $logdir/imapd/imapd.log
[ -z "$failed" ] && failed="relay-imapd-$what:$image" || failed="$failed relay-imapd-$what:$image"
[ $halt_on_error -eq 1 ] && echo "Press ENTER to continue" && read key
return 1
fi
swaks -S --to tuser01@$domain4 --from $from --server 127.0.0.1 --port 2025 >> $logdir/imapd/imapd.log 2>&1
ret1=$?
swaks -S --to tuser01@$domain4 --from $3 --server 127.0.0.1 --port 2025 >> $logdir/imapd/imapd.log 2>&1
ret2=$?
if [ $ret1 -eq 0 -a $ret2 -ne 0 ] ; then
delete_relay
tcount=$(expr $tcount + 1)
printf " testing Open RELAY using IMAP login succeeded\n"
else
/bin/rm -f $testdir/tcpclient.imap
fcount=$(expr $fcount + 1)
printf " testing Open RELAY using IMAP login failed ret1=%d ret2=%d\n" $ret1 $ret2
select_relay
delete_relay
cat $logdir/imapd/imapd.log
[ -z "$failed" ] && failed="relay-imapd-$what:$image" || failed="$failed relay-imapd-$what:$image"
[ $halt_on_error -eq 1 ] && echo "Press ENTER to continue" && read key
return 1
fi
fi
check_service $name qmail-imapd-ssl.993 3
tcpclient -a 10 -n $certdir/clientcert.pem -vDHR 127.0.0.1 2993 /tmp/qmail-test/tcpclient.imap 2>$logdir/imapd-ssl/imapd-ssl.log
if [ $? -eq 0 ] ; then
tcount=$(expr $tcount + 1)
printf " testing IMAPS login %s and mail retrieval succeeded\n" "$1"
else
/bin/rm -f $testdir/tcpclient.imap
fcount=$(expr $fcount + 1)
if [ $do_relay -eq 1 ] ; then
delete_relay
fi
printf " testing IMAPS login %s and mail retrieval failed\n" "$1"
cat $logdir/imapd-ssl/imapd-ssl.log
[ -z "$failed" ] && failed="imaps:2993-$what:$image" || failed="$failed imaps:2993-$what:$image"
[ $halt_on_error -eq 1 ] && echo "Press ENTER to continue" && read key
return 1
fi
/bin/rm -f $testdir/tcpclient.imap
if [ $do_relay -eq 1 ] ; then
swaks -S --to tuser01@$domain4 --from $from --server 127.0.0.1 --port 2025 >> $logdir/imapd-ssl/imapd-ssl.log 2>&1
ret1=$?
swaks -S --to tuser01@$domain4 --from $3 --server 127.0.0.1 --port 2025 >> $logdir/imapd-ssl/imapd-ssl.log 2>&1
ret2=$?
if [ $ret1 -eq 0 -a $ret2 -ne 0 ] ; then
delete_relay
tcount=$(expr $tcount + 1)
printf " testing Open RELAY using IMAPS login succeeded\n"
else
fcount=$(expr $fcount + 1)
printf " testing Open RELAY using IMAPS login failed ret1=%d ret2=%d\n" $ret1 $ret2
select_relay
delete_relay
cat $logdir/imapd-ssl/imapd-ssl.log
[ -z "$failed" ] && failed="relay-imapd-$what:$image" || failed="$failed relay-imapd-$what:$image"
[ $halt_on_error -eq 1 ] && echo "Press ENTER to continue" && read key
return 1
fi
fi
}
check_mail()
{
count=0
ret=1
(
while true
do
mcount=$(sudo ls $1/new | wc -l)
if [ $mcount -gt 0 ] ; then
mail_file=$(sudo ls -lt $1/new|head -2|tail -1|awk '{print $9}')
if [ $# -eq 1 ] ; then
sudo /bin/rm -f $1/new/$mail_file
fi
return 0
fi
sleep 1
count=$(expr "$count" + 1)
echo count=$count
if [ $count -gt 40 ] ; then
echo "Failed to receive Mail" 1>&2
break
fi
done
return 1
) >> $logdir/mail/mail.log 2>&1
}
setup_maildir()
{
if [ -d $maildir/new ] ; then
find $maildir/new -type f -exec /bin/rm -f {} \;
fi
mkdir -p $cntrldir
mkdir -p $sysconfdir/users
echo "Creating $maildir" 1>&2
for i in cur new tmp
do
mkdir -p $maildir/$i
done
}
shutdown_svscan()
{
[ $verbose -eq 1 ] && echo "Shutting down svscan"
if [ -d $servicedir/.svscan/log ] ; then
sudo kill $1
sleep 5
sudo svc -dx $servicedir/* $servicedir/*/log $servicedir/.svscan/log
else
sudo kill $1
sleep 5
sudo svc -dx $servicedir/* $servicedir/*/log
fi
}
setup_assign()
{
echo "Creating $sysconfdir/users/assign" 1>&2
(
echo "=$user:$user:$myuid:$mygid:$testdir/$user:::"
echo "+$user-:$user:$myuid:$mygid:$testdir/$user:-::"
echo "."
) > $sysconfdir/users/assign
echo "Creating $sysconfdir/users/cdb" 1>&2
$qmail_newu $sysconfdir/users
}
setup_queue()
{
echo "Creating queue in $testdir/queue" 1>&2
sudo queue-fix -s 23 -b 0 $testdir/queue
}
setup_config()
{
[ $verbose -eq 1 ] && echo "Creating default config files in $cntrldir" 1>&3
env CONTROLDIR=$cntrldir config-fast $domain1
[ $verbose -eq 1 ] && echo "Creating qmail config" 1>&3
sudo /usr/sbin/svctool --cntrldir=$cntrldir --config=qmail
[ $verbose -eq 1 ] && echo "Creating openssl certificates" 1>&3
if [ ! -f $certdir/servercert.pem ] ; then
sudo /usr/sbin/svctool --certdir=$certdir --config=cert \
--postmaster=postmaster@$HOSTNAME --common_name=$HOSTNAME
fi
sudo chown -R $user $cntrldir $certdir
echo ./Maildir/ > $cntrldir/defaultdelivery
echo TLSv1_2:TLSv1_3 > $cntrldir/tlsclientmethod
echo TLSv1_2:TLSv1_3 > $cntrldir/tlsservermethod
echo blocked@$HOSTNAME > $cntrldir/badmailfrom
echo blocked@$HOSTNAME > $cntrldir/badrcptto
echo @$HOSTNAME > $cntrldir/nodnscheck
echo "$domain2:127.0.0.1:2025" > $cntrldir/smtproute
echo "$domain1" >>$cntrldir/rcpthosts
echo "$domain1" >>$cntrldir/locals
sudo chown -R $user $cntrldir $certdir
sudo /bin/rm -f $cntrldir/libmysql $cntrldir/libindimail
}
setup_svscan_without_svscanlog()
{
[ $verbose -eq 1 ] && echo "Starting svscan services without svscanlog"
sudo /bin/rm -rf $servicedir
sudo /bin/rm -rf $logdir/smtpd
sudo /bin/rm -rf $logdir/qmail-send
mkdir -p $servicedir/smtpd/variables
mkdir -p $servicedir/smtpd/log
echo 1 > $servicedir/smtpd/variables/USE_QPWGR
echo 0 > $servicedir/smtpd/variables/BIGTODO
echo 23 > $servicedir/smtpd/variables/CONFSPLIT
echo $cntrldir > $servicedir/smtpd/variables/CONTROLDIR
echo $testdir/queue > $servicedir/smtpd/variables/QUEUEDIR
echo $certdir > $servicedir/smtpd/variables/CERTDIR
echo 1 > $servicedir/smtpd/variables/DISABLE_PLUGIN
printf "#!/bin/sh\nexec envdir ./variables $tcpserver -u qmaild -g qmail -v -HR 0 $smtp_port $qmail_smtpd 2>&1\n" > $servicedir/smtpd/run
printf "#!/bin/sh\nexec /usr/sbin/multilog t $logdir/smtpd\n" > $servicedir/smtpd/log/run
chmod +x $servicedir/smtpd/run
chmod +x $servicedir/smtpd/log/run
mkdir -p $servicedir/qmail-send/variables
mkdir -p $servicedir/qmail-send/log
echo /bin:/usr/sbin > $servicedir/qmail-send/variables/PATH
echo 0 > $servicedir/qmail-send/variables/BIGTODO
echo 1 > $servicedir/qmail-send/variables/QPWGR
echo 23 > $servicedir/qmail-send/variables/CONFSPLIT
echo $cntrldir > $servicedir/qmail-send/variables/CONTROLDIR
echo $testdir/queue > $servicedir/qmail-send/variables/QUEUEDIR
echo $sysconfdir/users > $servicedir/qmail-send/variables/ASSIGNDIR
echo $certdir > $servicedir/qmail-send/variables/CERTDIR
printf "#!/bin/sh\nexec envdir ./variables $qmail_start -s ./Maildir/ 2>&1\n" > $servicedir/qmail-send/run
printf "#!/bin/sh\nexec /usr/sbin/multilog t $logdir/qmail-send\n" > $servicedir/qmail-send/log/run
chmod +x $servicedir/qmail-send/run
chmod +x $servicedir/qmail-send/log/run
}
start_svscan_without_svscanlog()
{
(
sudo env - \
DISABLE_RUN=1 \
SILENT=1 \
PATH=/bin:/usr/sbin \
/usr/sbin/svscan $servicedir
) > $logdir/svscan/svscan.log 2>&1 &
sleep 1
svpid=$(sed -n '$p' $servicedir/.svscan.pid)
sudo kill -0 $svpid
if [ $? -ne 0 ] ; then
echo " testing svscan (without svscanlog) startup failed" 1>&2
shutdown_svscan $svpid
exit 1
fi
for i in smtpd smtpd/log qmail-send qmail-send/log
do
sudo svok $servicedir/smtpd
if [ $? -ne 0 ] ; then
echo " testing service $i startup failed" 1>&2
shutdown_svscan $svpid
exit 1
fi
done
}
do_setup()
{
# basic setup for maildir, assign config and queue
sudo /bin/rm -rf $basedir/$localuser
mkdir -p $basedir
sudo /bin/rm -rf $logdir
mkdir -p $logdir/setup
mkdir -p $logdir/mail
mkdir -p $logdir/imapd
mkdir -p $logdir/imapd-ssl
mkdir -p $logdir/pop3d
mkdir -p $logdir/pop3d-ssl
mkdir -p $logdir/qmail-send
mkdir -p $logdir/svscan
mkdir -p $logdir/smtpd
mkdir -p $logdir/$podcmd
(
[ $verbose -eq 1 ] && echo "Creating maildir" 1>&3
setup_maildir
[ $verbose -eq 1 ] && echo "Creating assign" 1>&3
setup_assign
[ $verbose -eq 1 ] && echo "Creating config" 1>&3
setup_config
[ $verbose -eq 1 ] && echo "Creating queue" 1>&3
setup_queue
) > $logdir/setup/setup.log 2>&1
[ $verbose -eq 1 ] && echo "--- setup.log ---" && cat $logdir/setup/setup.log
}
do_svscan_without_svscanlog()
{
# start svscan without svscanlog
[ $verbose -eq 1 ] && echo "Setting svscan service" 1>&3
setup_svscan_without_svscanlog
start_svscan_without_svscanlog
svpid=$(sed -n '$p' $servicedir/.svscan.pid)
}
test_iwebadmin()
{
u=$1
d=$2
p=$3
if [ $verbose -eq 1 ] ; then
if [ "$u" = "postmaster" ] ; then
echo "Executing curl to test iwebadmin admin login"
else
echo "Executing curl to test iwebadmin user login"
fi
fi
$podcmd exec -ti $name /usr/bin/curl "http://localhost/cgi-bin/iwebadmin" -d \
"username="$u"&domain="$d"&password="$p"&returnhttp=&returntext=" \
>> $logdir/$podcmd/roundcube.log 2>&1
if [ $? -ne 0 ] ; then
fcount=$(expr $fcount + 1)
if [ "$u" = "postmaster" ] ; then
echo " testing iwebadmin admin login via $podcmd container $what.$image failed"
else
echo " testing iwebadmin user login via $podcmd container $what.$image failed"
fi
[ -z "$failed" ] && failed="iwebadmin-$what:$image" || failed="$failed iwebadmin-$what:$image"
if [ $prompt -eq 1 ] ; then
echo -n "Press ENTER or type quit to exit "
read key
if [ "$key" = "quit" ] ; then
doquit=1
fi
fi
fi
t1=0
t2=0
grep "<h2>Invalid Login" $logdir/$podcmd/roundcube.log >/dev/null
t1=$?
if [ "$u" = "postmaster" ] ; then
grep "background=\"/images/iwebadmin/main.png\"" $logdir/$podcmd/roundcube.log > /dev/null
else
grep "<b>Modify User: $u@$d</b>" $logdir/$podcmd/roundcube.log >/dev/null
fi
t2=$?
if [ $t2 -eq 0 -a $t1 -ne 0 ] ; then
tcount=$(expr $tcount + 1)
if [ "$u" = "postmaster" ] ; then
echo " testing iwebadmin admin login via $podcmd container $what.$image succeeded"
else
echo " testing iwebadmin user login via $podcmd container $what.$image succeeded"
fi
elif [ $t1 -eq 0 -a $t2 -ne 0 ] ; then
fcount=$(expr $fcount + 1)
if [ "$u" = "postmaster" ] ; then
echo " testing iwebadmin admin login via $podcmd container $what.$image failed"
else
echo " testing iwebadmin user login via $podcmd container $what.$image failed"
fi
[ -z "$failed" ] && failed="iwebadmin-$what:$image" || failed="$failed iwebadmin-$what:$image"
cat $logdir/$podcmd/roundcube.log
if [ $prompt -eq 1 ] ; then
echo -n "Press ENTER or type quit to exit "
read key
if [ "$key" = "quit" ] ; then
doquit=1
fi
fi
else
cat $logdir/$podcmd/roundcube.log
fi
logout=$(grep logout $logdir/$podcmd/roundcube.log | sed -e "s{.*a href={{" -e "s{>Log Out.*{{" -e "s{\"{{g")
$podcmd exec -ti $name /usr/bin/curl -X GET $url > /dev/null
/bin/rm -f $logdir/$podcmd/roundcube.log
}
print_logs()
{
(
echo "------ local $what.$image.log ------"
cat $logdir/$podcmd/$what.$image.log
echo "------ container qmail-smtpd.25 log ------"
$podcmd exec -ti $name cat /var/log/svc/smtpd.25/current
echo
echo "------ container qmail-smtpd.587 log ------"
$podcmd exec -ti $name cat /var/log/svc/smtpd.587/current
echo
echo "------ container qmail-send log ------"
$podcmd exec -ti $name cat /var/log/svc/deliver.25/current
echo
echo "------ local qmail-smtpd log ------"
cat $logdir/smtpd/current
echo
echo "------ local qmail-send log ------"
cat $logdir/qmail-send/current
echo
echo "------ $what.$image.log ------"
cat $logdir/$podcmd/$what.$image.log
)
}
test_docker()
{
if [ $doquit -eq 1 ] ; then
return 0
fi
image=$1
what=$2
if [ $prompt -eq 1 ] ; then
echo -n "Test $what:$image (Y/N) - "
read key
if [ "$key" = "quit" ] ; then
doquit=1
return 0
fi
if [ "$key" != "Y" -a "$key" != "y" ] ; then
return 0
fi
fi
start=$(date +'%s')
update=0
if [ $do_update -eq 0 ] ; then
if [ "$podcmd" = "podman" ] ; then
$podcmd image exists cprogrammer/$what:$image
if [ $? -ne 0 ] ; then
update=1
fi
else
z=$($podcmd image ls cprogrammer/$what:$image|sed -n '2,$p')
if [ -z "$z" ] ; then
update=1
fi
fi
else
update=1
fi
if [ $update -eq 1 ] ; then
[ $verbose -eq 1 ] && echo "$podcmd pull cprogrammer/$what:$image"
$podcmd pull cprogrammer/$what:$image > /dev/null
fi
id=$($podcmd image list -q cprogrammer/$what:$image)
echo "Testing $what:$image id=$id"
if [ $selinux_enabled -eq 1 ] ; then
vol="$basedir:/home:z"
else
vol="$basedir:/home"
fi
if [ "$what" = "indimail-web" -o "$what" = "indimail-mta-web" ] ; then
name="webmail"
else
name="$what"
fi
ports="-p 8000:80 -p 2025:25 -p 2587:587 -p 2110:110"
ports="$ports -p 2143:143 -p 2993:993 -p 2995:995 -p 3307:3306"
if [ $verbose -eq 1 ] ; then
extra="--cap-add IPC_LOCK --cap-add SYS_RESOURCE --cap-add=NET_ADMIN --cap-add=CAP_NET_RAW --cap-add=SYS_NICE --cap-add=SYS_PTRACE"
echo "$podcmd run $extra -d --rm -h $domain2 --name $name $ports -v $vol $id -d $domain2 $name"
$podcmd run $extra -d --rm -h $domain2 --name $name $ports -v $vol $id -d $domain2 $name
ret=$?
else
extra="-q --cap-add IPC_LOCK --cap-add SYS_RESOURCE --cap-add=NET_ADMIN --cap-add=CAP_NET_RAW --cap-add=SYS_NICE --cap-add=SYS_PTRACE"
$podcmd run $extra -d --rm -h $domain2 --name $name $ports -v $vol $id -d $domain2 $name >/dev/null
ret=$?
fi
if [ "$podcmd" = "podman" ] ; then
pid=$($podcmd ps --noheading)
else
pid=$($podcmd ps -q)
fi
if [ $ret -ne 0 -o -z "$pid" ] ; then
echo "Failed to run $what:$image"
[ -z "$failed" ] && failed="mail-$what:$image" || failed="$failed mail-$what:$image"
[ $halt_on_error -eq 1 ] && echo "Press ENTER to continue" && read key
return 1
fi
$podcmd exec -ti $name /bin/rm -f /etc/indimail/control/global_vars/TLS_CIPHER_SUITE # remove this later
if [ "$name" = "indimail" -o "$name" = "webmail" ] ; then
case $image in
jammy|noble)
for i in qmail-send.25 slowq-send inlookup.infifo indisrvr.4000
do
$podcmd exec -ti $name /bin/rm -f /service/$i/variables/MYSQL_OPT_RECONNECT
$podcmd exec -ti $name svc -r /service/$i
done
;;
#stream9)
#$podcmd exec -ti $name setcap -r /usr/libexec/mysqld
#;;
esac
fi
case $image in
focal)
t1=$(cat $cntrldir/servercipherlist)
t2=$(cat $cntrldir/serverciphersuite)
$podcmd exec -ti $name sh -c "echo $t1:$t2 > /etc/indimail/control/serverciphersuite"
$podcmd exec -ti $name svc -r /service/qmail-smtpd.25 /service/qmail-smtpd.587
;;
bionic)
t1=$(cat $cntrldir/servercipherlist)
t2=$(cat $cntrldir/serverciphersuite)
$podcmd exec -ti $name sh -c "echo 209715200 > /service/qmail-smtpd.25/variables/SOFT_MEM"
$podcmd exec -ti $name sh -c "echo 209715200 > /service/qmail-smtpd.587/variables/SOFT_MEM"
$podcmd exec -ti $name sh -c "echo $t1:$t2 > /etc/indimail/control/serverciphersuite"
$podcmd exec -ti $name svc -r /service/qmail-smtpd.25 /service/qmail-smtpd.587
;;
esac
[ $verbose -eq 1 ] && echo "adding user $localuser"
(
setup_localuser
$podcmd cp /tmp/create_testuser $name:/usr/bin
/bin/rm -f /tmp/create_testuser
$podcmd exec -ti $name touch /etc/indimail/control/chkrcptdomains
$podcmd exec -ti $name svc -h /service/qmail-smtpd.25 /service/qmail-smtpd.587 # remove this later
$podcmd exec -ti $name sh -c "/usr/bin/create_testuser"
$podcmd exec -ti $name svc -h /service/qmail-send.25
) > $logdir/$podcmd/$what.$image.log 2>&1
check_service $name qmail-smtpd.587 3
test_pop3 $localuser $password $user@$domain1 $basedir/$localuser/Maildir
sudo sh -c "/bin/rm -rf $basedir/$localuser/Maildir/new/* $basedir/$localuser/Maildir/cur/*"
test_imap $localuser $password $user@$domain1 $basedir/$localuser/Maildir
sudo sh -c "/bin/rm -rf $basedir/$localuser/Maildir/new/* $basedir/$localuser/Maildir/cur/*"
if [ "$what" = "indimail-mta" ] ; then
ret=0
check_service $name qmail-smtpd.25 3
(
$podcmd exec -ti $name sh -c "printf \"&$user@$domain1\n/var/indimail/alias/Maildir/\n\" > /var/indimail/alias/.qmail-$user"
$podcmd exec -ti $name sh -c "printf \"$domain1:$localip:$smtp_port\n\" > /etc/indimail/control/smtproutes"
) >> $logdir/$podcmd/$what.$image.log 2>&1
[ $verbose -eq 1 ] && echo "sending mail to user $user@$domain2 from testuser01@example.com"
[ $verbose -eq 1 ] && echo swaks --to $user@$domain2 --from testuser01@example.com -s 127.0.0.1 -p 2025
(
if [ $verbose -eq 1 ] ; then
(
echo "This is a test mail"