summaryrefslogtreecommitdiff
path: root/debian/local/sendmail.in
blob: 36ccc7fe4b002a0915ad142c2fc3d916a46c38a6 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
#!/bin/sh -e
#-----------------------------------------------------------------------------
# $Sendmail: sm_helper.sh,v @sm_version@ @sm_date@ @sm_time@ cowboy Exp $
#
# Copyright (c) 1998-@SM_CPYRT@ Richard Nelson.  All Rights Reserved.
#
#	Debian helper function script for Debian Sendmail
#	  Note: this file supports @SM_MINVERS@ - @SM_MAXVERS@
#
# Notes (to all):
#	*
#
# Notes (to self):
#	* clean_queues fubar, esp wrt MSP
#
#-----------------------------------------------------------------------------
#
set -e;
DEBUG=0;

# LSB compliance (kinda)
if [ -f /lib/lsb/init-functions ]; then
	. /lib/lsb/init-functions;
else
	log_begin_msg()   { echo "$@"; };
	log_success_msg() { echo "$@"; };
	log_warning_msg() { echo "$@"; };
	fi;

#------------------------------------------------------------------------------
# Parameters for the sendmail daemon
# Do *NOT* touch these lines, instead, edit /etc/mail/sendmail.conf
# The _PARMS lines are listed in precedence order
#------------------------------------------------------------------------------
Get_Parameters () {

	# Main configuration parameters in /etc/mail/sendmail.conf
	DAEMON_MODE='Daemon';
	DAEMON_PARMS='';
	DAEMON_UID='root';
	QUEUE_MODE="$DAEMON_MODE";
	QUEUE_INTERVAL='10m';
	QUEUE_PARMS='';
	MSP_MODE="$QUEUE_MODE";
	MSP_INTERVAL="$QUEUE_INTERVAL";
	MSP_PARMS="";
	MISC_PARMS='';
	CRON_PARMS='';
	LOG_CMDS='No';

	# Secondary (non-documented) parameters in /etc/mail/sendmail.conf
	# Caveat Emptor: change these at your own risk - they impact several
	# disjoint pieces parts...
	SENDMAIL_ROOT='@localstatedir@/run/sendmail';
	MTA_DAEMON='@sbindir@/sendmail-mta';
	MTA_COMMAND='@sbindir@/sendmail-mta';
	MTA_A='-Am';
	MTAL_L='-L sm-mta';
	MTAL_L_QUEUE='-L sm-mta-queue';
	MTAL_L_RUNQ='-L sm-mta-runq';
	MTA_ROOT="${SENDMAIL_ROOT}/mta";
	MTAL_PIDFILE="${MTA_ROOT}/sendmail.pid";
	MTAL_SOCKET="${MTA_ROOT}/smsocket";
	MTAL_CNTL="${MTA_ROOT}/smcontrol";
	MTAQ_L='-L sm-que';
	MTAQ_L_RUNQ='-L sm-que-runq';
	MTAQ_PIDFILE="${MTA_ROOT}/queue.pid";
	MTAQ_SOCKET="${MTA_ROOT}/qusocket";
	MTAQ_CNTL="${MTA_ROOT}/qucontrol";
	MSP_DAEMON='@sbindir@/sendmail-msp';
	MSP_COMMAND='@sbindir@/sendmail-msp';
	MSP_A='-Ac';
	MSP_L='-L sm-msp';
	MSP_L_QUEUE='-L sm-msp-queue';
	MSP_ROOT="${SENDMAIL_ROOT}/msp";
	MSP_PIDFILE="${MSP_ROOT}/sendmail.pid";
	MSP_SOCKET="${MSP_ROOT}/smsocket";
	MSP_CNTL="${MSP_ROOT}/smcontrol";

	# Pull in any user modified variables
	if [ -f @sysconfdir@/mail/sendmail.conf ]; then
		. @sysconfdir@/mail/sendmail.conf;
		fi;

	# These can't be user customized
	SM_Get_Parameters='yes';
	PATH='/bin:/usr/bin:/sbin:/usr/sbin';
	STAMP_DIR="${SENDMAIL_ROOT}/stampdir";
	START_MTAL_CMD="start-stop-daemon \
		--pidfile $MTAL_PIDFILE \
		--exec $MTA_DAEMON \
		--startas $MTA_COMMAND \
		--start";
	STOP_MTAL_CMD="start-stop-daemon \
		--pidfile $MTAL_PIDFILE \
		--name sendmail-mta \
		--stop";
	SIGNAL_MTAL_CMD="start-stop-daemon \
		--pidfile $MTAL_PIDFILE \
		--name sendmail-mta \
		--stop";
	START_MTAQ_CMD="start-stop-daemon \
		--pidfile $MTAQ_PIDFILE \
		--make-pidfile \
		--exec $MTA_DAEMON \
		--startas $MTA_COMMAND \
		--start";
	STOP_MTAQ_CMD="start-stop-daemon \
		--pidfile $MTAQ_PIDFILE \
		--name sendmail-mta \
		--stop";
	SIGNAL_MTAQ_CMD="start-stop-daemon \
		--pidfile $MTAQ_PIDFILE \
		--name sendmail-mta \
		--stop";
	START_MSP_CMD="start-stop-daemon \
		--pidfile $MSP_PIDFILE \
		--exec $MSP_DAEMON \
		--startas $MSP_COMMAND \
		--chuid smmsp \
		--start";
	STOP_MSP_CMD="start-stop-daemon \
		--pidfile $MSP_PIDFILE \
		--name sendmail-msp \
		--stop";
	SIGNAL_MSP_CMD="start-stop-daemon \
		--pidfile $MSP_PIDFILE \
		--name sendmail-msp \
		--stop";
	NAME='sendmail';
	FLAGS='defaults 50';

	# Sanitize some keyword entries
	DAEMON_MODE=$(echo "$DAEMON_MODE" | tr '[:upper:]' '[:lower:]');
	QUEUE_MODE=$(echo  "$QUEUE_MODE"  | tr '[:upper:]' '[:lower:]');
	MSP_MODE=$(echo    "$MSP_MODE"    | tr '[:upper:]' '[:lower:]');

	# See if we can share the listener and queue-runner daemon:
	#	* Both must be in daemon mode
	#	* They must have the same (possibly empty) parameters
	if [ "$DAEMON_MODE" = "daemon" \
	   -a \( \( "$QUEUE_MODE" = "cron" -o "$QUEUE_MODE" = "none" \) \
		  -o \( "$QUEUE_MODE" != "cron" \
			 -a "$QUEUE_MODE" != "none" \
			 -a "$DAEMON_PARMS" = "$QUEUE_PARMS" \
			 \) \
		  \) ]; then
		SPLIT_DAEMON=0;
	else
		SPLIT_DAEMON=1;
		fi;

	# Version dependant support:
	# 8.12.0+ M{TA,MSP}_A
	if [ ! -f @datadir@/sendmail/cf/feature/msp.m4 ]; then
		MTA_A='';
		MTAL_L='';
		MTAL_L_QUEUE='';
		MSP_A='';
		MSP_L='';
		MSP_L_QUEUE='';
		fi;
	};
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# enhanced sendmail startup
#------------------------------------------------------------------------------
start_mta () {
	#
	# Make sure @localstatedir@/run/sendmail/ exists
	check_dirs;
	#
	# If already running, don't start it...
	if is_running mta; then
		log_warning_msg 'MTA is already running.';
		return;
		fi;
	#
	# Check if we're going to run a daemon (and how many):
	daemon_check;
	#
	# Mark restarted for communication betwixt here and /etc/mail/Makefile
	touch $STAMP_DIR/reload;
	#
	if [ "$DAEMON_MODE" = "daemon" ]; then
		#
		# Allow Unix (local) connections betwixt MSP/MTA:
		touch $MTAL_SOCKET;
		#
		# We can only afford to clean the MTA queues if running daemon mode,
		# otherwise, there is a chance that a cronjob might still be using
		# the queue...	Thats also why we don't clean the MSP queues herein.
		clean_queues;
		#
		# cd to a safe place to stash core files...
		cd $MTA_ROOT;
		CMD="$START_MTAL_CMD -- $MTAL_PARMS";
		if [ "$LOG_CMDS" = "Yes" ]; then
			logger -i -p mail.debug -- "$0 $CMD";
			fi;
		$CMD &
		#
		# Update permissions on smsocket
		sleep 2;
		chown $DAEMON_UID:smmsp $MTAL_SOCKET;
		chmod 0666 $MTAL_SOCKET;
		fi;
	#
	# Check for split daemon mode (separate listener/queue runner)
	if [ "$SPLIT_DAEMON" -eq 1 ]; then
		CMD="$START_MTAQ_CMD -- $MTAQ_PARMS";
		if [ "$LOG_CMDS" = "Yes" ]; then
			logger -i -p mail.debug -- "$0 $CMD";
			fi;
        $CMD &
		sleep 2;
		qp=`expr "${MTAQ_PARMS}" : '.*\(-qp[0-9]*[smhdw]\)'` || true;
		if [ -z "$qp" ]; then
			chmod 0664 $MTAQ_PIDFILE;
		else
			qc=`ps --no-headers -fCsendmail \
				| egrep -e 'Queue control' \
				| awk '{print $2}'`;
			if [ -z "$qc" ]; then
				rm -f $MTAQ_PIDFILE;
			else
				chmod 0664 $MTAQ_PIDFILE;
				printf "$qc\n$MTA_COMMAND $MTAQ_PARMS" > $MTAQ_PIDFILE;
				fi;
			fi;
		fi;
	#
	# if running split service, run the client queues (just to make sure)
	if check_msp; then
		$MSP_COMMAND -q $MSP_A $MSP_L_QUEUE $MSP_PARMS $MISC_PARMS;
		fi;
	};

start_msp () {
	#
	# Make sure @localstatedir@/run/sendmail/ exists
	check_dirs;
	#
	# If already running, don't start it...
	if is_running msp; then
		log_warning_msg 'MSP is already running.';
		return;
		fi;
	#
	# Check to see if MSP mode is indeed available
	if ! check_msp; then
		return;
		fi;
	#
	# Check if we're going to run a daemon:
	if [ "$MSP_MODE" != 'daemon' ]; then
		return;
		fi;
	#
	# We can only afford to clean the MSP queues if running daemon mode,
	# otherwise, there is a chance that a cronjob might still be using
	# the queue...	Thats also why we don't clean the MTA queues herein.
	#clean_queues /var/spool/mqueue-client;
	#
	# cd to a safe place to stash core files...
	cd $MSP_ROOT;
	$START_MSP_CMD -- \
		$MSP_A $MSP_L -q${MSP_INTERVAL} $MSP_PARMS $MISC_PARMS &
	};

start_sendmail () {
	start_mta;
	if check_msp; then
		start_msp;
		fi;
	};
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# enhanced sendmail reload
#------------------------------------------------------------------------------
reload_mta () {
	#
	# Make sure @localstatedir@/run/sendmail/ exists
	check_dirs;
	#
	# reload (signal -HUP) is *much* better/faster than stop/start
	#
	# Mark restarted for communication betwixt here and /etc/mail/Makefile
	touch $STAMP_DIR/reload;
	#
	# If not running, just start it...
	if ! is_running mta; then
		start_mta;
		fi;
	#
	# Is running, must signal it...
	$SIGNAL_MTAL_CMD --signal HUP --oknodo --quiet || true;
	sleep 2;
	chown $DAEMON_UID:smmta $MTAL_SOCKET;
	chmod 0666 $MTAL_SOCKET;
	#
	# Check for split daemon mode (separate listener/queue runner)
	if [ "$SPLIT_DAEMON" -eq 1 ]; then
		$SIGNAL_MTAQ_CMD --signal HUP --oknodo --quiet || true;
		fi;
	};

reload_msp () {
	#
	# Make sure @localstatedir@/run/sendmail/ exists
	check_dirs;
	#
	# reload (signal -HUP) is *much* better/faster than stop/start
	#
	# If not running, just start it...
	if ! is_running msp; then
		start_msp;
		fi;
	#
	# Is running, must signal it...
	$SIGNAL_MSP_CMD --signal HUP --oknodo --quiet || true;
	};

reload_sendmail () {
	reload_mta;
	if check_msp; then
		reload_msp;
		fi;
	};
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# enhanced sendmail shutdown
#------------------------------------------------------------------------------
stop_mta () {
	local cnt;
	stopped=0;
	#
	# Make sure @localstatedir@/run/sendmail/ exists
	check_dirs;
	#
	# If not running, don't stop it...
	if ! is_running mta; then
		return;
		fi;
	#
	# Is running, must stop it...
	$STOP_MTAL_CMD --signal TERM --quiet --oknodo > /dev/null;
	#
	#		Now we have to wait until sendmail has _really_ stopped.
	#
	sleep 2;
	if $STOP_MTAL_CMD --signal TERM --quiet > /dev/null; then
		echo -n ' Waiting .';
		cnt=0;
		while $STOP_MTAL_CMD --signal TERM --quiet > /dev/null; do
			cnt=`expr $cnt + 1`;
			if [ $cnt -gt 60 ]; then
				#
				#		Waited 120 seconds now. Fail.
				#
				echo -n ' Failed ';
				stopped=1;
				break;
				fi;
			sleep 2;
			echo -n '.';
			done;
		echo -n ' Done ';
		fi;
	# Remove pidfile iff stopped
	if ! $STOP_MTAL_CMD --signal TERM --quiet > /dev/null; then
		rm -f "$MTAL_PIDFILE";
		fi
	};

stop_queue () {
	local cnt;
	stopped=0;
	#
	# Make sure @localstatedir@/run/sendmail/ exists
	check_dirs;
	#
	# If not running, don't stop it...
	if ! is_running queue; then
		return;
		fi;
	#
	# Is running, must stop it...
	$STOP_MTAQ_CMD --signal TERM --quiet --oknodo > /dev/null;
	#
	#		Now we have to wait until sendmail has _really_ stopped.
	#
	sleep 2;
	if $STOP_MTAQ_CMD --signal TERM --quiet > /dev/null; then
		echo -n ' Waiting .';
		cnt=0;
		while $STOP_MTAQ_CMD --signal TERM --quiet > /dev/null; do
			cnt=`expr $cnt + 1`;
			if [ $cnt -gt 60 ]; then
				#
				#		Waited 120 seconds now. Fail.
				#
				echo -n ' Failed ';
				stopped=1;
				break;
				fi;
			sleep 2;
			echo -n '.';
			done;
		echo -n ' Done ';
		fi;
	# Remove pidfile iff stopped
	if ! $STOP_MTAQ_CMD --signal TERM --quiet > /dev/null; then
		rm -f "$MTAQ_PIDFILE";
		fi
	};

stop_msp () {
	local cnt;
	stopped=0;
	#
	# Make sure @localstatedir@/run/sendmail/ exists
	check_dirs;
	#
	# If not running, don't stop it...
	if ! is_running msp; then
		return;
		fi;
	#
	# Is running, must stop it...
	$STOP_MSP_CMD --signal TERM --quiet --oknodo > /dev/null;
	#
	#		Now we have to wait until sendmail has _really_ stopped.
	#
	sleep 2;
	if $STOP_MSP_CMD --signal TERM --quiet > /dev/null; then
		echo -n 'Waiting .';
		cnt=0;
		while $STOP_MSP_CMD --signal TERM --quiet > /dev/null; do
			cnt=`expr $cnt + 1`;
			if [ $cnt -gt 60 ]; then
				#
				#		Waited 120 seconds now. Fail.
				#
				echo -n ' Failed ';
				stopped=1;
				break;
				fi;
			sleep 2;
			echo -n '.';
			done;
		echo -n ' Done ';
		fi;
	# Remove pidfile iff stopped
	if ! $STOP_MSP_CMD --signal TERM --quiet > /dev/null; then
		rm -f "$MSP_PIDFILE";
		fi;
	};

stop_sendmail () {
	if check_msp; then
		stop_msp;
		fi;
	stop_mta;
	stop_queue;
	};
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Check to see if sendmail is running
#------------------------------------------------------------------------------
is_running () {
	local result;
	result=1;
	#
	# Make sure @localstatedir@/run/sendmail/ exists
	check_dirs;
	#
	# Determine proper pidfile to check
	who=$(echo	"$1"  | tr '[:upper:]' '[:lower:]');
	case $who in
		mta)
			PIDFILE="$MTAL_PIDFILE";
			;;
		queue)
			PIDFILE="$MTAQ_PIDFILE";
			;;
		msp)
			PIDFILE="$MSP_PIDFILE";
			;;
		*)
			PIDFILE="$1";
			;;
		esac;
	#
	# If no pidfile, not running
	# Extract pid/command and see if still running
	# Remove pidfile if app didn't
	# Also remove any control sockets
	if [ -s $PIDFILE ]; then
		PID=`head -n 1 $PIDFILE 2>/dev/null`;
		COMMAND=`tail -n 1 $PIDFILE`;
		if [ ! -z "`ps --no-heading $PID`" ]; then
			result=0;
		else
			rm -f $PIDFILE;
			case $who in
				mta)
					rm -f $MTAL_SOCKET $MTAL_CNTL;
					;;
				queue)
					rm -f $MTAQ_SOCKET $MTAQ_CNTL;
					;;
				msp)
					rm -f $MSP_SOCKET $MSP_CNTL;
				esac;
			fi;
		fi;
	return $result;
	};
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Check to see if running split service (MTA, MSP)
# Must be at 8.12.0+ for this support
#------------------------------------------------------------------------------
check_msp () {
	local result;
	result=1;
	if [ ! -f @datadir@/sendmail/cf/feature/msp.m4 ]; then
		result=1;
	elif [ -s @sysconfdir@/mail/submit.cf ] \
	&& [ -s @sysconfdir@/mail/submit.mc ]; then
		if grep -qEe "^[[:space:]]*\`?FEATURE\([[:space:]]*\`?msp" \
			@sysconfdir@/mail/submit.mc; then
			result=0;
			fi;
		fi;
	return $result;
	};
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Miscellaneous sendmail command support for MSP/MTA split
# mailstats, mailq, runq
#------------------------------------------------------------------------------
newaliases () {
	#
	# Obtain parameters IFF needed
	if [ -z "$SM_Get_Parameters" ]; then
		Get_Parameters;
		fi;
	#
	$MTA_COMMAND $MTA_A -bi $*;
	};

hoststat () {
	#
	# Obtain parameters IFF needed
	if [ -z "$SM_Get_Parameters" ]; then
		Get_Parameters;
		fi;
	#
	$MTA_COMMAND $MTA_A -bh $*;
	};

purgestat () {
	local parms;
	parms=$(echo "$1" | tr '[:upper:]' '[:lower:]');
	case $parms in
		n*)		# Now
			parms='-O Timeout.hoststatus=1s';
			shift;
			;;
		*)
			parms='';
			;;
		esac;
	#
	# Obtain parameters IFF needed
	if [ -z "$SM_Get_Parameters" ]; then
		Get_Parameters;
		fi;
	#
	$MTA_COMMAND $MTA_A -bH $parms $*;
	};

mailstats () {
	#
	# Obtain parameters IFF needed
	if [ -z "$SM_Get_Parameters" ]; then
		Get_Parameters;
		fi;
	#
	#if running split service, show the client status first
	if check_msp; then
		echo 'MSP statistics...';
		#check if we have a status file for the MSP
		statusfile=$(grep -Ee '^O StatusFile=/.*' \
			@sysconfdir@/mail/submit.cf | cut -d= -f2);
		if [ -n $statusfile ]; then
			@libexecdir@/mailstats -C \
				@sysconfdir@/mail/submit.cf $* || true;
			fi;
		echo 'MTA statistics...';
		fi;
	#check if we have a status file for the MTA
	statusfile=$(grep -Ee '^O StatusFile=/.*' \
		@sysconfdir@/mail/sendmail.cf | cut -d= -f2);
	if [ -n $statusfile ]; then
		@libexecdir@/mailstats $* || true;
		fi;
	};

mailq () {
	#
	# Obtain parameters IFF needed
	if [ -z "$SM_Get_Parameters" ]; then
		Get_Parameters;
		fi;
	#
	# if running split service, show the client queues first
	if check_msp; then
		echo 'MSP Queue status...';
		#
		# Check to see if shared memory is in use (8.12.0+)
		if grep -qEe "^[[:space:]]*\`?define\(\`?confSHAREDMEMORYKEY'?[[:space:]]*,[[:space:]]*\`?0*[1-9]+[0-9]*'?[[:space:]]*\)" \
			@sysconfdir@/mail/submit.mc; then
			$MSP_COMMAND -bP || true;
			fi;
		$MSP_COMMAND -bp $MSP_A $MISC_PARMS $* || true;
		echo 'MTA Queue status...';
		fi;
	#
	# Check to see if shared memory is in use (8.12.0+)
	if grep -qEe "^[[:space:]]*\`?define\(\`?confSHAREDMEMORYKEY'?[[:space:]]*,[[:space:]]*\`?0*[1-9]+[0-9]*'?[[:space:]]*\)" \
		@sysconfdir@/mail/sendmail.mc; then
		$MTA_COMMAND -bP || true;
		fi;
	$MTA_COMMAND -bp $MTA_A $MISC_PARMS $* || true;
	};

runq () {
	#
	# Obtain parameters IFF needed
	if [ -z "$SM_Get_Parameters" ]; then
		Get_Parameters;
		fi;
	#
	# if running split service, run the client queues first
	if check_msp; then
		echo 'Running the MSP queue...';
		$MSP_COMMAND -q $MSP_A \
			$MSP_L_QUEUE $MSP_PARMS $MISC_PARMS $* || true;
		echo 'Running the MTA queues...';
		fi;
	if [ "$SPLIT_DAEMON" -eq 0 ]; then
		$MTA_COMMAND -q $MTA_A \
			$MTAL_L_RUNQ $QUEUE_PARMS $MISC_PARMS $* || true;
	else
		$MTA_COMMAND -q $MTA_A \
			$MTAQ_L_RUNQ $QUEUE_PARMS $MISC_PARMS $* || true;
		fi;
	};

control () {
	#
	# Obtain parameters IFF needed
	if [ -z "$SM_Get_Parameters" ]; then
		Get_Parameters;
		fi;
	local parms;
	parms="$*";
	if [ -z "$parms" ]; then
		parms='help';
		fi;
	if is_running mta; then
		@datadir@/sendmail/smcontrol.pl $parms;
	else
		echo 'MTA: is not running';
		fi;
	};
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Print status of running job(s)
#------------------------------------------------------------------------------
status () {
	#
	# Make sure @localstatedir@/run/sendmail/ exists
	check_dirs;
	#
	# Obtain parameters IFF needed
	if [ -z "$SM_Get_Parameters" ]; then
		Get_Parameters;
		fi;
	#
	# if running split service, check the client status first
	if check_msp; then
		if is_running msp; then
			echo "MSP: $PID $COMMAND";
			ps -fwp $PID;
			if [ -S ${MSP_CNTL} ]; then
				@datadir@/sendmail/smcontrol.pl \
					-f ${MSP_CNTL} status || true;
				fi;
		elif [ $MSP_MODE = 'cron' ]; then
			echo "MSP: is run via cron ($MSP_INTERVAL)";
		elif [ $MSP_MODE = 'none' ]; then
			echo 'MSP: is disabled';
		else
			echo 'MSP: is not running';
			fi;
		fi;
	#
	# Check MTA listener
	if is_running mta; then
		echo "MTA: $PID $COMMAND";
		ps -fwp $PID;
		if [ -S ${MTAL_CNTL} ]; then
			@datadir@/sendmail/smcontrol.pl \
				-f ${MTAL_CNTL} status || true;
			fi;
	elif [ $DAEMON_MODE = 'inetd' ]; then
		echo 'MTA: is run via inetd';
	elif [ $DAEMON_MODE = 'none' ]; then
		echo 'MTA: is disabled';
	else
		echo 'MTA: is not running';
		fi;
	#
	# Check for split daemon mode (separate listener/queue runner)
	if is_running queue; then
		echo "QUE: $PID $COMMAND";
		ps -fwp $PID;
		if [ -S ${MTAQ_CNTL} ]; then
			@datadir@/sendmail/smcontrol.pl \
				-f ${MTAQ_CNTL} status || true;
			fi;
	elif [ $QUEUE_MODE = 'cron' ]; then
		echo "QUE: is run via cron ($QUEUE_INTERVAL)";
	elif [ $QUEUE_MODE = 'none' ]; then
		echo 'QUE: is disabled';
	elif [ "$SPLIT_DAEMON" -eq 0 ]; then
		echo "QUE: Same as MTA";
	else
		echo 'QUE: is not running';
		fi;
	};
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Cronjob handling
#------------------------------------------------------------------------------
cron_mta () {
	#
	# Make sure @localstatedir@/run/sendmail/ exists
	check_dirs;
	#
	# Obtain parameters IFF needed
	if [ -z "$SM_Get_Parameters" ]; then
		Get_Parameters;
		fi;
	#
	# If cron not needed, don't do queue running (though it wouldn't hurt)
	if [ $QUEUE_MODE = 'cron' ]; then
		#
		# If running a split (MTA/MSP) setup, we need to make sure that
		# messages not immediately accepted by the MTA get delivered.
		# Only run the MSP queue if MSP_MODE=none
		if check_msp; then
			if [ $MSP_MODE = 'none' ]; then
				# Make sure only *ONE* cronjob at a time
				if [ ! -f $STAMP_DIR/cron_msp ]; then
					touch $STAMP_DIR/cron_msp;
					#clean_queues /var/spool/mqueue-client;
					$MSP_COMMAND -q $MSP_A $MSP_L_QUEUE \
						$MSP_PARMS $MISC_PARMS \
						$CRON_PARMS || true;
					rm -f $STAMP_DIR/cron_msp;
					fi;
				fi;
			fi;

		# Make sure only *ONE* cronjob at a time
		if [ ! -f $STAMP_DIR/cron_mta ]; then
			touch $STAMP_DIR/cron_mta;
			$MTA_COMMAND -q $MTA_A $MTAL_L_QUEUE \
				$QUEUE_PARMS $MISC_PARMS $CRON_PARMS || true;
			rm -f $STAMP_DIR/cron_mta;
			fi;
		fi;
	};

cron_msp () {
	#
	# Make sure @localstatedir@/run/sendmail/ exists
	check_dirs;
	#
	# Obtain parameters IFF needed
	if [ -z "$SM_Get_Parameters" ]; then
		Get_Parameters;
		fi;
	#
	# If cron not needed, don't do queue running (though it wouldn't hurt)
	if [ $MSP_MODE = 'cron' ]; then
		#
		# If running a split (MTA/MSP) setup, we need to make sure that
		# messages not immediately accepted by the MTA get delivered.
		if check_msp; then
			# Make sure only *ONE* cronjob at a time
			if [ ! -f $STAMP_DIR/cron_msp ]; then
				touch $STAMP_DIR/cron_msp;
				#clean_queues /var/spool/mqueue-client;
				$MSP_COMMAND -q $MSP_A $MSP_L_QUEUE \
					$MSP_PARMS $MISC_PARMS \
					$CRON_PARMS || true;
				rm -f $STAMP_DIR/cron_msp;
				fi;
			fi;
		fi;
	};
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Determine how to run sendmail mta daemon
#	* No daemon
#	* As listener
#	* As queue runner
#	* As both listener and queue runner
#------------------------------------------------------------------------------
daemon_check () {
	local run_daemon;
	#
	# Skip daemon run for the following:
	#	* sendmail hasn't been configured
	#	* smptd, a firewall frontend for sendmail, is installed
	#	* DAEMON_MODE = (none | inet) & QUEUE_MODE = (none | cron)
	if [ ! -s @sysconfdir@/mail/sendmail.cf ] || \
	   [ ! -s @sysconfdir@/mail/sendmail.mc ]; then
		echo 'sendmail has not been configured, not started.';
		echo 'To configure sendmail, type sendmailconfig';
		exit 1;
		fi;

	MTAL_PARMS="$MTA_A $MTAL_L";
	MTAQ_PARMS="$MTA_A $MTAQ_L";
	run_daemon=3;

	case "$DAEMON_MODE" in
		none* | \
		inetd*)
			run_daemon=`expr $run_daemon - 1`;
			;;

		daemon* | \
		*)
			MTAL_PARMS="$MTAL_PARMS -bd $DAEMON_PARMS";
			;;
		esac;

	case "$QUEUE_MODE" in
		none* | \
		cron*)
			run_daemon=`expr $run_daemon - 1`;
			;;

		daemon* | \
		*)
			# Check for split daemon mode (separate listener/queue runner)
			if [ $SPLIT_DAEMON -eq 0 ]; then
				if [ ! -z "$QUEUE_INTERVAL" ]; then
					MTAL_PARMS="$MTAL_PARMS -q${QUEUE_INTERVAL}";
					fi;
				MTAL_PARMS="$MTAL_PARMS $QUEUE_PARMS";
			else
				if [ ! -z "$QUEUE_INTERVAL" ]; then
					MTAQ_PARMS="$MTAQ_PARMS -q${QUEUE_INTERVAL}";
					fi;
				MTAQ_PARMS="$MTAQ_PARMS $QUEUE_PARMS";
				fi;
			;;
		esac;

	# Add any miscellanous (ie debugging) parameters
	MTAL_PARMS="$MTAL_PARMS $MISC_PARMS";
	MTAQ_PARMS="$MTAQ_PARMS $MISC_PARMS";

	# Add PidFile override for MTA queue runner
	MTAQ_PARMS="$MTAQ_PARMS -O PidFile=$MTAQ_PIDFILE";
	MTAQ_PARMS="$MTAQ_PARMS -O ControlSocketName=$MTAQ_CNTL";

	if [ $run_daemon -lt 2 ]; then
		echo 'sendmail listen/queue daemon not desired.';
		exit 0;
		fi;
	};
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Clean sendmail queues (somewhat): does *not* support split qf/df/xf dirs.
# NOTE: This whole thing sucks with queue-groups, need to redo it!!!
# NOTE: Check for race conditions betwixt this code and queue-runners
#------------------------------------------------------------------------------
clean_queues () {
	local QUEUE_ROOT QUEUE QUIET
	# Obtain queue root directory
	if [ -z "$1" ]; then
		QUEUE_ROOT=/var/spool/mqueue
		QUEUE=/var/spool/mqueue
		QUIET=''
	else
		QUEUE_ROOT="$1"
		QUEUE="$1"
		QUIET='1'
		fi
	# remove lock files left because of kill/crash
	# rm -f $QUEUE/[lnx]f* doesn't work with a plethora of files ;-(
	for i in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \
		a b c d e f g h i j k l m n o p q r s t u v w x y z; do
		rm -f $QUEUE/[lnx]f${i}*
		done
	# remove zero length qf files
	#for qffile in $QUEUE/qf*; do
	for qffile in $(find $QUEUE_ROOT -type f -name 'qf*'); do
		if [ -r "$qffile" ] && [ ! -s "$qffile" ]; then
			if [ ! -z "$QUIET" ]; then
				echo -n "<zero: $qffile> "; fi
			rm -f "$qffile"
			fi
		done
	# rename tf files to be qf if the qf does not exist
	for tffile in $QUEUE/tf*; do
		qffile=`echo "$tffile" | sed 's/\/tf\([[:alnum:]]*\)$/\/qf\1/'`
		if [ -r "$tffile" ] && [ ! -f "$qffile" ]; then
			if [ ! -z "$QUIET" ]; then
				echo -n "<recovering: $tffile> "; fi
			mv "$tffile" "$qffile"
		elif [ -f "$tffile" ]; then
			echo -n "<extra: $tffile> "
			rm -f "$tffile"
			fi
		done
	# remove df files with no corresponding qf files
	for dffile in $QUEUE/df*; do
		qffile=`echo "$dffile" | sed 's/\/df\([[:alnum:]]*\)$/\/qf\1/'`
		Dffile=`echo "$dffile" | sed 's/\/df\([[:alnum:]]*\)$/\/Df\1/'`
		if [ -r "$dffile" ] && [ ! -f "$qffile" ]; then
			if [ ! -s "$dffile" ]; then
				rm -f "$dffile"
			else
				if [ ! -z "$QUIET" ]; then
					 echo -n "<incomplete: $dffile> "; fi
				mv "$dffile" "$Dffile";
				fi
			fi
		done
	# announce files that have been saved during disaster recovery
	for xffile in $QUEUE/[A-Z]f*; do
		if [ -f "$xffile" ]; then
			if [ ! -z "$QUIET" ]; then
				echo -n "<panic: $xffile> "; fi
			fi
		done
	}
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# check_dirs:  Make sure @localstatedir@/run/sendmail/{mta,msp,stampdir} exist
#------------------------------------------------------------------------------
check_dirs () {
	if [ ! -d "${SENDMAIL_ROOT}" ]; then
		mkdir -p "${SENDMAIL_ROOT}";
		chown root:smmta "${SENDMAIL_ROOT}";
		chmod 02755 "${SENDMAIL_ROOT}";
		fi;
	if [ ! -d "${MTA_ROOT}" ]; then
		mkdir -p "${MTA_ROOT}";
		chown $DAEMON_UID:smmta "${MTA_ROOT}";
		chmod 02755 "${MTA_ROOT}";
		fi;
	if [ ! -d "${MSP_ROOT}" ]; then
		mkdir -p "${MSP_ROOT}";
		chown smmsp:smmsp "${MSP_ROOT}";
		chmod 02775 "${MSP_ROOT}";
		fi;
	if [ ! -d "${STAMP_DIR}" ]; then
		mkdir -p "${STAMP_DIR}";
		chown root:smmsp "${STAMP_DIR}";
		chmod 02775 "${STAMP_DIR}";
		fi;
	}

#------------------------------------------------------------------------------
# Why are we here ?
#------------------------------------------------------------------------------
# Some requisite initialization
if [ -z "$SM_Get_Parameters" ]; then
	Get_Parameters;
	fi;
if [ "$LOG_CMDS" = "Yes" ]; then
	logger -i -p mail.debug -- "$0 $@";
	fi;

#------------------------------------------------------------------------------
# Handle being called via an alias
#------------------------------------------------------------------------------
case $(basename $0) in
	newaliases)
		newaliases $*;
		exit $?;
		;;
	hoststat)
		hoststat $*;
		exit $?;
		;;
	purgestat)
		purgestat $*;
		exit $?;
		;;
	mailstats)
		mailstats $*;
		exit $?;
		;;
	mailq)
		mailq $*;
		exit $?;
		;;
	runq)
		runq $*;
		exit $?;
		;;
	control|smcontrol)
		control $*;
		exit $?;
		;;
	status)
		status $*
		exit $?
		;;
	*)
		;;
	esac;

#------------------------------------------------------------------------------
# Handle being called via /etc/init.d/sendmail or directly
#------------------------------------------------------------------------------
# Ok, why are we here...
case "$1" in

	#-----------------------------------------------------------------------
	# Debian required/optional targets:
	#-----------------------------------------------------------------------
	start)
		echo -n 'Starting Mail Transport Agent: sendmail';
		start_sendmail;
		echo '.'
		;;

	stop|force-stop)
		echo -n 'Stopping Mail Transport Agent: sendmail';
		stop_sendmail;
		echo '.'
		;;

	restart)
		echo -n 'Restarting Transport Agent: sendmail';
		# reload is equivalent (but faster) than stop/start !
		# but... it doesn't honor changes to /etc/mail/sendmail.conf
		#reload_sendmail;
		stop_sendmail;
		start_sendmail;
		echo '.';
		;;

	restart-if-running)
		if ! is_running mta && ! is_running msp; then
			echo 'Mail Transport Agent: sendmail is not running';
		else
			$0 restart;
			fi;
		;;

	reload-if-running)
		if ! is_running mta && ! is_running msp; then
			echo 'Mail Transport Agent: sendmail is not running';
		else
			$0 reload;
			fi;
		;;

	reload|force-reload)
		echo -n 'Reloading Mail Transport Agent configuration: sendmail';
		reload_sendmail;
		echo '.';
		;;

	#-----------------------------------------------------------------------
	# Local targets (sendmail commands/aliases) for MSP/MTA split support
	# These targets will pass along any provided parameters
	#-----------------------------------------------------------------------
	newaliases)
		shift;
		newaliases $*;
		;;

	hoststat)
		shift;
		hoststat $*;
		;;

	purgestat)
		shift;
		purgestat $*;
		;;

	mailstats)
		shift;
		mailstats $*;
		;;

	mailq)
		shift;
		mailq $*;
		;;

	runq)
		shift;
		runq $*;
		;;

	control|smcontrol)
		shift;
		control $*;
		;;

	#-----------------------------------------------------------------------
	# Local targets for extended support/debugging
	#-----------------------------------------------------------------------
	status)
		shift;
		status $*;
		;;

	debug)
		#
		# If not running, can't debug
		if is_running msp; then
			echo -n 'Dumping MSP state...';
			$SIGNAL_MSP_CMD --signal USR1;
			echo 'done.';
			fi;
		if is_running mta; then
			echo -n 'Dumping MTA state...';
			$SIGNAL_MTAL_CMD --signal USR1;
			echo 'done.';
			fi;
		;;

	clean|clean_que*|clean-que*)
		#
		# If running, don't clean the queues...
		if is_running mta; then
			echo 'MTA is running, queue cleaning ill advised...';
		else
			echo -n 'Cleaning up the queues...';
			clean_queues;
			echo 'done.';
			fi;
		;;

	#-----------------------------------------------------------------------
	# Local targets for cronjob support
	#-----------------------------------------------------------------------
	cron-msp)
		cron_msp;
		;;

	cron-mta)
		cron_mta;
		;;

	#-----------------------------------------------------------------------
	# Default target - bitch and moan
	#-----------------------------------------------------------------------
	*)
		echo "Invalid command <$1>";
		echo "Usage: $0 <command>";
		echo '	Where <command> is one of the following';
		echo '	  start|stop|restart|restart-if-running';
		echo '	  reload-if-running|reload|force-reload';
		echo '	  newaliases|hoststat|purgestat|mailstats|mailq|runq|control';
		echo '	  status|debug|clean';
		exit 1;
		;;
	esac;

exit 0;