aboutsummaryrefslogtreecommitdiff
path: root/contrib/sendmail/src/mci.c
blob: a50fd8ed0fe29b19773e2a1ab93e409e888eabff (plain) (blame)
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
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
/*
 * Copyright (c) 1998-2005, 2010 Proofpoint, Inc. and its suppliers.
 *	All rights reserved.
 * Copyright (c) 1995-1997 Eric P. Allman.  All rights reserved.
 * Copyright (c) 1988, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 *
 */

#include <sendmail.h>

SM_RCSID("@(#)$Id: mci.c,v 8.225 2013-11-22 20:51:56 ca Exp $")

#if NETINET || NETINET6
# include <arpa/inet.h>
#endif

#include <dirent.h>
#if STARTTLS
# include <tls.h>
#endif

static int	mci_generate_persistent_path __P((const char *, char *,
						  int, bool));
static bool	mci_load_persistent __P((MCI *));
static void	mci_uncache __P((MCI **, bool));
static void	mci_clear __P((MCI *));
static int	mci_lock_host_statfile __P((MCI *));
static int	mci_read_persistent __P((SM_FILE_T *, MCI *));

/*
**  Mail Connection Information (MCI) Caching Module.
**
**	There are actually two separate things cached.  The first is
**	the set of all open connections -- these are stored in a
**	(small) list.  The second is stored in the symbol table; it
**	has the overall status for all hosts, whether or not there
**	is a connection open currently.
**
**	There should never be too many connections open (since this
**	could flood the socket table), nor should a connection be
**	allowed to sit idly for too long.
**
**	MaxMciCache is the maximum number of open connections that
**	will be supported.
**
**	MciCacheTimeout is the time (in seconds) that a connection
**	is permitted to survive without activity.
**
**	We actually try any cached connections by sending a RSET
**	before we use them; if the RSET fails we close down the
**	connection and reopen it (see smtpprobe()).
**
**	The persistent MCI code is donated by Mark Lovell and Paul
**	Vixie.  It is based on the long term host status code in KJS
**	written by Paul but has been adapted by Mark to fit into the
**	MCI structure.
*/

static MCI	**MciCache;		/* the open connection cache */

/*
**  MCI_CACHE -- enter a connection structure into the open connection cache
**
**	This may cause something else to be flushed.
**
**	Parameters:
**		mci -- the connection to cache.
**
**	Returns:
**		none.
*/

void
mci_cache(mci)
	register MCI *mci;
{
	register MCI **mcislot;

	/*
	**  Find the best slot.  This may cause expired connections
	**  to be closed.
	*/

	mcislot = mci_scan(mci);
	if (mcislot == NULL)
	{
		/* we don't support caching */
		return;
	}

	if (mci->mci_host == NULL)
		return;

	/* if this is already cached, we are done */
	if (bitset(MCIF_CACHED, mci->mci_flags))
		return;

	/* otherwise we may have to clear the slot */
	if (*mcislot != NULL)
		mci_uncache(mcislot, true);

	if (tTd(42, 5))
		sm_dprintf("mci_cache: caching %p (%s) in slot %d\n",
			(void *)mci, mci->mci_host, (int) (mcislot - MciCache));
	if (tTd(91, 100))
		sm_syslog(LOG_DEBUG, CurEnv->e_id,
			  "mci_cache: caching %lx (%.100s) in slot %d",
			  (unsigned long) mci, mci->mci_host,
			  (int) (mcislot - MciCache));

	*mcislot = mci;
	mci->mci_flags |= MCIF_CACHED;
}
/*
**  MCI_SCAN -- scan the cache, flush junk, and return best slot
**
**	Parameters:
**		savemci -- never flush this one.  Can be null.
**
**	Returns:
**		The LRU (or empty) slot.
*/

MCI **
mci_scan(savemci)
	MCI *savemci;
{
	time_t now;
	register MCI **bestmci;
	register MCI *mci;
	register int i;

	if (MaxMciCache <= 0)
	{
		/* we don't support caching */
		return NULL;
	}

	if (MciCache == NULL)
	{
		/* first call */
		MciCache = (MCI **) sm_pmalloc_x(MaxMciCache * sizeof(*MciCache));
		memset((char *) MciCache, '\0', MaxMciCache * sizeof(*MciCache));
		return &MciCache[0];
	}

	now = curtime();
	bestmci = &MciCache[0];
	for (i = 0; i < MaxMciCache; i++)
	{
		mci = MciCache[i];
		if (mci == NULL || mci->mci_state == MCIS_CLOSED)
		{
			bestmci = &MciCache[i];
			continue;
		}
		if ((mci->mci_lastuse + MciCacheTimeout <= now ||
		     (mci->mci_mailer != NULL &&
		      mci->mci_mailer->m_maxdeliveries > 0 &&
		      mci->mci_deliveries + 1 >= mci->mci_mailer->m_maxdeliveries))&&
		    mci != savemci)
		{
			/* connection idle too long or too many deliveries */
			bestmci = &MciCache[i];

			/* close it */
			mci_uncache(bestmci, true);
			continue;
		}
		if (*bestmci == NULL)
			continue;
		if (mci->mci_lastuse < (*bestmci)->mci_lastuse)
			bestmci = &MciCache[i];
	}
	return bestmci;
}
/*
**  MCI_UNCACHE -- remove a connection from a slot.
**
**	May close a connection.
**
**	Parameters:
**		mcislot -- the slot to empty.
**		doquit -- if true, send QUIT protocol on this connection.
**			  if false, we are assumed to be in a forked child;
**				all we want to do is close the file(s).
**
**	Returns:
**		none.
*/

static void
mci_uncache(mcislot, doquit)
	register MCI **mcislot;
	bool doquit;
{
	register MCI *mci;
	extern ENVELOPE BlankEnvelope;

	mci = *mcislot;
	if (mci == NULL)
		return;
	*mcislot = NULL;
	if (mci->mci_host == NULL)
		return;

	mci_unlock_host(mci);

	if (tTd(42, 5))
		sm_dprintf("mci_uncache: uncaching %p (%s) from slot %d (%d)\n",
			(void *)mci, mci->mci_host, (int) (mcislot - MciCache),
			   doquit);
	if (tTd(91, 100))
		sm_syslog(LOG_DEBUG, CurEnv->e_id,
			  "mci_uncache: uncaching %lx (%.100s) from slot %d (%d)",
			  (unsigned long) mci, mci->mci_host,
			  (int) (mcislot - MciCache), doquit);

	mci->mci_deliveries = 0;
	if (doquit)
	{
		message("Closing connection to %s", mci->mci_host);

		mci->mci_flags &= ~MCIF_CACHED;

		/* only uses the envelope to flush the transcript file */
		if (mci->mci_state != MCIS_CLOSED)
			smtpquit(mci->mci_mailer, mci, &BlankEnvelope);
#if XLA
		xla_host_end(mci->mci_host);
#endif
	}
	else
	{
		if (mci->mci_in != NULL)
			(void) sm_io_close(mci->mci_in, SM_TIME_DEFAULT);
		if (mci->mci_out != NULL)
			(void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
		mci->mci_in = mci->mci_out = NULL;
		mci->mci_state = MCIS_CLOSED;
		mci->mci_exitstat = EX_OK;
		mci->mci_errno = 0;
		mci->mci_flags = 0;

		mci->mci_retryrcpt = false;
		mci->mci_tolist = NULL;
#if PIPELINING
		mci->mci_okrcpts = 0;
#endif
	}

	SM_FREE(mci->mci_status);
	SM_FREE(mci->mci_rstatus);
	SM_FREE(mci->mci_heloname);
	mci_clear(mci);
	if (mci->mci_rpool != NULL)
	{
		sm_rpool_free(mci->mci_rpool);
		mci->mci_macro.mac_rpool = NULL;
		mci->mci_rpool = NULL;
	}
}
/*
**  MCI_FLUSH -- flush the entire cache
**
**	Parameters:
**		doquit -- if true, send QUIT protocol.
**			  if false, just close the connection.
**		allbut -- but leave this one open.
**
**	Returns:
**		none.
*/

void
mci_flush(doquit, allbut)
	bool doquit;
	MCI *allbut;
{
	register int i;

	if (MciCache == NULL)
		return;

	for (i = 0; i < MaxMciCache; i++)
	{
		if (allbut != MciCache[i])
			mci_uncache(&MciCache[i], doquit);
	}
}

/*
**  MCI_CLR_EXTENSIONS -- clear knowledge about SMTP extensions
**
**	Parameters:
**		mci -- the connection to clear.
**
**	Returns:
**		none.
*/

void
mci_clr_extensions(mci)
	MCI *mci;
{
	if (mci == NULL)
		return;

	mci->mci_flags &= ~MCIF_EXTENS;
	mci->mci_maxsize = 0;
	mci->mci_min_by = 0;
#if SASL
	mci->mci_saslcap = NULL;
#endif
}

/*
**  MCI_CLEAR -- clear mci
**
**	Parameters:
**		mci -- the connection to clear.
**
**	Returns:
**		none.
*/

static void
mci_clear(mci)
	MCI *mci;
{
	if (mci == NULL)
		return;

	mci->mci_maxsize = 0;
	mci->mci_min_by = 0;
	mci->mci_deliveries = 0;
#if SASL
	if (bitset(MCIF_AUTHACT, mci->mci_flags))
		sasl_dispose(&mci->mci_conn);
#endif
#if STARTTLS
	if (bitset(MCIF_TLSACT, mci->mci_flags) && mci->mci_ssl != NULL)
		SM_SSL_FREE(mci->mci_ssl);
#endif

	/* which flags to preserve? */
	mci->mci_flags &= MCIF_CACHED;
	mactabclear(&mci->mci_macro);
}


/*
**  MCI_GET -- get information about a particular host
**
**	Parameters:
**		host -- host to look for.
**		m -- mailer.
**
**	Returns:
**		mci for this host (might be new).
*/

MCI *
mci_get(host, m)
	char *host;
	MAILER *m;
{
	register MCI *mci;
	register STAB *s;
	extern SOCKADDR CurHostAddr;

	/* clear CurHostAddr so we don't get a bogus address with this name */
	memset(&CurHostAddr, '\0', sizeof(CurHostAddr));

	/* clear out any expired connections */
	(void) mci_scan(NULL);

	if (m->m_mno < 0)
		syserr("!negative mno %d (%s)", m->m_mno, m->m_name);

	s = stab(host, ST_MCI + m->m_mno, ST_ENTER);
	mci = &s->s_mci;

	/* initialize per-message data */
	mci->mci_retryrcpt = false;
	mci->mci_tolist = NULL;
#if PIPELINING
	mci->mci_okrcpts = 0;
#endif
	mci->mci_flags &= ~MCIF_NOTSTICKY;

	if (mci->mci_rpool == NULL)
		mci->mci_rpool = sm_rpool_new_x(NULL);

	if (mci->mci_macro.mac_rpool == NULL)
		mci->mci_macro.mac_rpool = mci->mci_rpool;

	/*
	**  We don't need to load the persistent data if we have data
	**  already loaded in the cache.
	*/

	if (mci->mci_host == NULL &&
	    (mci->mci_host = s->s_name) != NULL &&
	    !mci_load_persistent(mci))
	{
		if (tTd(42, 2))
			sm_dprintf("mci_get(%s %s): lock failed\n",
				host, m->m_name);
		mci->mci_exitstat = EX_TEMPFAIL;
		mci->mci_state = MCIS_CLOSED;
		mci->mci_statfile = NULL;
		return mci;
	}

	if (tTd(42, 2))
	{
		sm_dprintf("mci_get(%s %s): mci_state=%d, _flags=%lx, _exitstat=%d, _errno=%d\n",
			host, m->m_name, mci->mci_state, mci->mci_flags,
			mci->mci_exitstat, mci->mci_errno);
	}

	if (mci->mci_state == MCIS_OPEN)
	{
		/* poke the connection to see if it's still alive */
		(void) smtpprobe(mci);

		/* reset the stored state in the event of a timeout */
		if (mci->mci_state != MCIS_OPEN)
		{
			mci->mci_errno = 0;
			mci->mci_exitstat = EX_OK;
			mci->mci_state = MCIS_CLOSED;
		}
		else
		{
			/* get peer host address */
			/* (this should really be in the mci struct) */
			SOCKADDR_LEN_T socklen = sizeof(CurHostAddr);

			(void) getpeername(sm_io_getinfo(mci->mci_in,
							 SM_IO_WHAT_FD, NULL),
				(struct sockaddr *) &CurHostAddr, &socklen);
		}
	}
	if (mci->mci_state == MCIS_CLOSED)
	{
		time_t now = curtime();

		/* if this info is stale, ignore it */
		if (mci->mci_lastuse + MciInfoTimeout <= now)
		{
			mci->mci_lastuse = now;
			mci->mci_errno = 0;
			mci->mci_exitstat = EX_OK;
		}
		mci_clear(mci);
	}

	return mci;
}

/*
**  MCI_CLOSE -- (forcefully) close files used for a connection.
**	Note: this is a last resort, usually smtpquit() or endmailer()
**		should be used to close a connection.
**
**	Parameters:
**		mci -- the connection to close.
**		where -- where has this been called?
**
**	Returns:
**		none.
*/

void
mci_close(mci, where)
	MCI *mci;
	char *where;
{
	bool dumped;

	if (mci == NULL)
		return;
	dumped = false;
	if (mci->mci_out != NULL)
	{
		if (tTd(56, 1))
		{
			sm_dprintf("mci_close: mci_out!=NULL, where=%s\n",
				where);
			mci_dump(sm_debug_file(), mci, false);
			dumped = true;
		}
		(void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
		mci->mci_out = NULL;
	}
	if (mci->mci_in != NULL)
	{
		if (tTd(56, 1))
		{
			sm_dprintf("mci_close: mci_in!=NULL, where=%s\n",
				where);
			if (!dumped)
				mci_dump(sm_debug_file(), mci, false);
		}
		(void) sm_io_close(mci->mci_in, SM_TIME_DEFAULT);
		mci->mci_in = NULL;
	}
	mci->mci_state = MCIS_CLOSED;
}

/*
**  MCI_NEW -- allocate new MCI structure
**
**	Parameters:
**		rpool -- if non-NULL: allocate from that rpool.
**
**	Returns:
**		mci (new).
*/

MCI *
mci_new(rpool)
	SM_RPOOL_T *rpool;
{
	register MCI *mci;

	if (rpool == NULL)
		mci = (MCI *) sm_malloc_x(sizeof(*mci));
	else
		mci = (MCI *) sm_rpool_malloc_x(rpool, sizeof(*mci));
	memset((char *) mci, '\0', sizeof(*mci));
	mci->mci_rpool = sm_rpool_new_x(NULL);
	mci->mci_macro.mac_rpool = mci->mci_rpool;
	return mci;
}
/*
**  MCI_MATCH -- check connection cache for a particular host
**
**	Parameters:
**		host -- host to look for.
**		m -- mailer.
**
**	Returns:
**		true iff open connection exists.
*/

bool
mci_match(host, m)
	char *host;
	MAILER *m;
{
	register MCI *mci;
	register STAB *s;

	if (m->m_mno < 0 || m->m_mno > MAXMAILERS)
		return false;
	s = stab(host, ST_MCI + m->m_mno, ST_FIND);
	if (s == NULL)
		return false;

	mci = &s->s_mci;
	return mci->mci_state == MCIS_OPEN;
}
/*
**  MCI_SETSTAT -- set status codes in MCI structure.
**
**	Parameters:
**		mci -- the MCI structure to set.
**		xstat -- the exit status code.
**		dstat -- the DSN status code.
**		rstat -- the SMTP status code.
**
**	Returns:
**		none.
*/

void
mci_setstat(mci, xstat, dstat, rstat)
	MCI *mci;
	int xstat;
	char *dstat;
	char *rstat;
{
	/* protocol errors should never be interpreted as sticky */
	if (xstat != EX_NOTSTICKY && xstat != EX_PROTOCOL)
		mci->mci_exitstat = xstat;

	SM_FREE(mci->mci_status);
	if (dstat != NULL)
		mci->mci_status = sm_strdup_x(dstat);

	SM_FREE(mci->mci_rstatus);
	if (rstat != NULL)
		mci->mci_rstatus = sm_strdup_x(rstat);
}
/*
**  MCI_DUMP -- dump the contents of an MCI structure.
**
**	Parameters:
**		fp -- output file pointer
**		mci -- the MCI structure to dump.
**
**	Returns:
**		none.
**
**	Side Effects:
**		none.
*/

struct mcifbits
{
	int	mcif_bit;	/* flag bit */
	char	*mcif_name;	/* flag name */
};
static struct mcifbits	MciFlags[] =
{
	{ MCIF_CACHED,		"CACHED"	},
	{ MCIF_ESMTP,		"ESMTP"		},
	{ MCIF_EXPN,		"EXPN"		},
	{ MCIF_SIZE,		"SIZE"		},
	{ MCIF_8BITMIME,	"8BITMIME"	},
	{ MCIF_7BIT,		"7BIT"		},
	{ MCIF_INHEADER,	"INHEADER"	},
	{ MCIF_CVT8TO7,		"CVT8TO7"	},
	{ MCIF_DSN,		"DSN"		},
	{ MCIF_8BITOK,		"8BITOK"	},
	{ MCIF_CVT7TO8,		"CVT7TO8"	},
	{ MCIF_INMIME,		"INMIME"	},
	{ MCIF_AUTH,		"AUTH"		},
	{ MCIF_AUTH2,		"AUTH2"		},
	{ MCIF_AUTHACT,		"AUTHACT"	},
	{ MCIF_ENHSTAT,		"ENHSTAT"	},
	{ MCIF_PIPELINED,	"PIPELINED"	},
	{ MCIF_VERB,		"VERB"	},
#if STARTTLS
	{ MCIF_TLS,		"TLS"		},
	{ MCIF_TLSACT,		"TLSACT"	},
#endif
	{ MCIF_DLVR_BY,		"DLVR_BY"	},
	{ MCIF_INLONGLINE,	"INLONGLINE"	},
	{ MCIF_NOTSTICKY,	"NOTSTICKY"	},
	{ 0,			NULL		}
};

void
mci_dump(fp, mci, logit)
	SM_FILE_T *fp;
	register MCI *mci;
	bool logit;
{
	register char *p;
	char *sep;
	char buf[4000];

	sep = logit ? " " : "\n\t";
	p = buf;
	(void) sm_snprintf(p, SPACELEFT(buf, p), "MCI@%p: ", (void *)mci);
	p += strlen(p);
	if (mci == NULL)
	{
		(void) sm_snprintf(p, SPACELEFT(buf, p), "NULL");
		goto printit;
	}
	(void) sm_snprintf(p, SPACELEFT(buf, p), "flags=%lx", mci->mci_flags);
	p += strlen(p);

	/*
	**  The following check is just for paranoia.  It protects the
	**  assignment in the if() clause. If there's not some minimum
	**  amount of space we can stop right now. The check will not
	**  trigger as long as sizeof(buf)=4000.
	*/

	if (p >= buf + sizeof(buf) - 4)
		goto printit;
	if (mci->mci_flags != 0)
	{
		struct mcifbits *f;

		*p++ = '<';	/* protected above */
		for (f = MciFlags; f->mcif_bit != 0; f++)
		{
			if (!bitset(f->mcif_bit, mci->mci_flags))
				continue;
			(void) sm_strlcpyn(p, SPACELEFT(buf, p), 2,
					   f->mcif_name, ",");
			p += strlen(p);
		}
		p[-1] = '>';
	}

	/* Note: sm_snprintf() takes care of NULL arguments for %s */
	(void) sm_snprintf(p, SPACELEFT(buf, p),
		",%serrno=%d, herrno=%d, exitstat=%d, state=%d, pid=%d,%s",
		sep, mci->mci_errno, mci->mci_herrno,
		mci->mci_exitstat, mci->mci_state, (int) mci->mci_pid, sep);
	p += strlen(p);
	(void) sm_snprintf(p, SPACELEFT(buf, p),
		"maxsize=%ld, phase=%s, mailer=%s,%s",
		mci->mci_maxsize, mci->mci_phase,
		mci->mci_mailer == NULL ? "NULL" : mci->mci_mailer->m_name,
		sep);
	p += strlen(p);
	(void) sm_snprintf(p, SPACELEFT(buf, p),
		"status=%s, rstatus=%s,%s",
		mci->mci_status, mci->mci_rstatus, sep);
	p += strlen(p);
	(void) sm_snprintf(p, SPACELEFT(buf, p),
		"host=%s, lastuse=%s",
		mci->mci_host, ctime(&mci->mci_lastuse));
printit:
	if (logit)
		sm_syslog(LOG_DEBUG, CurEnv->e_id, "%.1000s", buf);
	else
		(void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s\n", buf);
}
/*
**  MCI_DUMP_ALL -- print the entire MCI cache
**
**	Parameters:
**		fp -- output file pointer
**		logit -- if set, log the result instead of printing
**			to stdout.
**
**	Returns:
**		none.
*/

void
mci_dump_all(fp, logit)
	SM_FILE_T *fp;
	bool logit;
{
	register int i;

	if (MciCache == NULL)
		return;

	for (i = 0; i < MaxMciCache; i++)
		mci_dump(fp, MciCache[i], logit);
}
/*
**  MCI_LOCK_HOST -- Lock host while sending.
**
**	If we are contacting a host, we'll need to
**	update the status information in the host status
**	file, and if we want to do that, we ought to have
**	locked it. This has the (according to some)
**	desirable effect of serializing connectivity with
**	remote hosts -- i.e.: one connection to a given
**	host at a time.
**
**	Parameters:
**		mci -- containing the host we want to lock.
**
**	Returns:
**		EX_OK	    -- got the lock.
**		EX_TEMPFAIL -- didn't get the lock.
*/

int
mci_lock_host(mci)
	MCI *mci;
{
	if (mci == NULL)
	{
		if (tTd(56, 1))
			sm_dprintf("mci_lock_host: NULL mci\n");
		return EX_OK;
	}

	if (!SingleThreadDelivery)
		return EX_OK;

	return mci_lock_host_statfile(mci);
}

static int
mci_lock_host_statfile(mci)
	MCI *mci;
{
	int save_errno = errno;
	int retVal = EX_OK;
	char fname[MAXPATHLEN];

	if (HostStatDir == NULL || mci->mci_host == NULL)
		return EX_OK;

	if (tTd(56, 2))
		sm_dprintf("mci_lock_host: attempting to lock %s\n",
			   mci->mci_host);

	if (mci_generate_persistent_path(mci->mci_host, fname, sizeof(fname),
					 true) < 0)
	{
		/* of course this should never happen */
		if (tTd(56, 2))
			sm_dprintf("mci_lock_host: Failed to generate host path for %s\n",
				   mci->mci_host);

		retVal = EX_TEMPFAIL;
		goto cleanup;
	}

	mci->mci_statfile = safefopen(fname, O_RDWR, FileMode,
				      SFF_NOLOCK|SFF_NOLINK|SFF_OPENASROOT|SFF_REGONLY|SFF_SAFEDIRPATH|SFF_CREAT);

	if (mci->mci_statfile == NULL)
	{
		syserr("mci_lock_host: cannot create host lock file %s", fname);
		goto cleanup;
	}

	if (!lockfile(sm_io_getinfo(mci->mci_statfile, SM_IO_WHAT_FD, NULL),
		      fname, "", LOCK_EX|LOCK_NB))
	{
		if (tTd(56, 2))
			sm_dprintf("mci_lock_host: couldn't get lock on %s\n",
				fname);
		(void) sm_io_close(mci->mci_statfile, SM_TIME_DEFAULT);
		mci->mci_statfile = NULL;
		retVal = EX_TEMPFAIL;
		goto cleanup;
	}

	if (tTd(56, 12) && mci->mci_statfile != NULL)
		sm_dprintf("mci_lock_host: Sanity check -- lock is good\n");

cleanup:
	errno = save_errno;
	return retVal;
}
/*
**  MCI_UNLOCK_HOST -- unlock host
**
**	Clean up the lock on a host, close the file, let
**	someone else use it.
**
**	Parameters:
**		mci -- us.
**
**	Returns:
**		nothing.
*/

void
mci_unlock_host(mci)
	MCI *mci;
{
	int save_errno = errno;

	if (mci == NULL)
	{
		if (tTd(56, 1))
			sm_dprintf("mci_unlock_host: NULL mci\n");
		return;
	}

	if (HostStatDir == NULL || mci->mci_host == NULL)
		return;

	if (!SingleThreadDelivery && mci_lock_host_statfile(mci) == EX_TEMPFAIL)
	{
		if (tTd(56, 1))
			sm_dprintf("mci_unlock_host: stat file already locked\n");
	}
	else
	{
		if (tTd(56, 2))
			sm_dprintf("mci_unlock_host: store prior to unlock\n");
		mci_store_persistent(mci);
	}

	if (mci->mci_statfile != NULL)
	{
		(void) sm_io_close(mci->mci_statfile, SM_TIME_DEFAULT);
		mci->mci_statfile = NULL;
	}

	errno = save_errno;
}
/*
**  MCI_LOAD_PERSISTENT -- load persistent host info
**
**	Load information about host that is kept
**	in common for all running sendmails.
**
**	Parameters:
**		mci -- the host/connection to load persistent info for.
**
**	Returns:
**		true -- lock was successful
**		false -- lock failed
*/

static bool
mci_load_persistent(mci)
	MCI *mci;
{
	int save_errno = errno;
	bool locked = true;
	SM_FILE_T *fp;
	char fname[MAXPATHLEN];

	if (mci == NULL)
	{
		if (tTd(56, 1))
			sm_dprintf("mci_load_persistent: NULL mci\n");
		return true;
	}

	if (IgnoreHostStatus || HostStatDir == NULL || mci->mci_host == NULL)
		return true;

	/* Already have the persistent information in memory */
	if (SingleThreadDelivery && mci->mci_statfile != NULL)
		return true;

	if (tTd(56, 1))
		sm_dprintf("mci_load_persistent: Attempting to load persistent information for %s\n",
			   mci->mci_host);

	if (mci_generate_persistent_path(mci->mci_host, fname, sizeof(fname),
					 false) < 0)
	{
		/* Not much we can do if the file isn't there... */
		if (tTd(56, 1))
			sm_dprintf("mci_load_persistent: Couldn't generate host path\n");
		goto cleanup;
	}

	fp = safefopen(fname, O_RDONLY, FileMode,
		       SFF_NOLOCK|SFF_NOLINK|SFF_OPENASROOT|SFF_REGONLY|SFF_SAFEDIRPATH);
	if (fp == NULL)
	{
		/* I can't think of any reason this should ever happen */
		if (tTd(56, 1))
			sm_dprintf("mci_load_persistent: open(%s): %s\n",
				fname, sm_errstring(errno));
		goto cleanup;
	}

	FileName = fname;
	locked = lockfile(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), fname, "",
			  LOCK_SH|LOCK_NB);
	if (locked)
	{
		(void) mci_read_persistent(fp, mci);
		(void) lockfile(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), fname,
				"", LOCK_UN);
	}
	FileName = NULL;
	(void) sm_io_close(fp, SM_TIME_DEFAULT);

cleanup:
	errno = save_errno;
	return locked;
}
/*
**  MCI_READ_PERSISTENT -- read persistent host status file
**
**	Parameters:
**		fp -- the file pointer to read.
**		mci -- the pointer to fill in.
**
**	Returns:
**		-1 -- if the file was corrupt.
**		0 -- otherwise.
**
**	Warning:
**		This code makes the assumption that this data
**		will be read in an atomic fashion, and that the data
**		was written in an atomic fashion.  Any other functioning
**		may lead to some form of insanity.  This should be
**		perfectly safe due to underlying stdio buffering.
*/

static int
mci_read_persistent(fp, mci)
	SM_FILE_T *fp;
	register MCI *mci;
{
	int ver;
	register char *p;
	int saveLineNumber = LineNumber;
	char buf[MAXLINE];

	if (fp == NULL)
	{
		syserr("mci_read_persistent: NULL fp");
		/* NOTREACHED */
		return -1;
	}
	if (mci == NULL)
	{
		syserr("mci_read_persistent: NULL mci");
		/* NOTREACHED */
		return -1;
	}
	if (tTd(56, 93))
	{
		sm_dprintf("mci_read_persistent: fp=%lx, mci=",
			   (unsigned long) fp);
	}

	SM_FREE(mci->mci_status);
	SM_FREE(mci->mci_rstatus);

	sm_io_rewind(fp, SM_TIME_DEFAULT);
	ver = -1;
	LineNumber = 0;
	while (sm_io_fgets(fp, SM_TIME_DEFAULT, buf, sizeof(buf)) >= 0)
	{
		LineNumber++;
		p = strchr(buf, '\n');
		if (p != NULL)
			*p = '\0';
		switch (buf[0])
		{
		  case 'V':		/* version stamp */
			ver = atoi(&buf[1]);
			if (ver < 0 || ver > 0)
				syserr("Unknown host status version %d: %d max",
					ver, 0);
			break;

		  case 'E':		/* UNIX error number */
			mci->mci_errno = atoi(&buf[1]);
			break;

		  case 'H':		/* DNS error number */
			mci->mci_herrno = atoi(&buf[1]);
			break;

		  case 'S':		/* UNIX exit status */
			mci->mci_exitstat = atoi(&buf[1]);
			break;

		  case 'D':		/* DSN status */
			mci->mci_status = newstr(&buf[1]);
			break;

		  case 'R':		/* SMTP status */
			mci->mci_rstatus = newstr(&buf[1]);
			break;

		  case 'U':		/* last usage time */
			mci->mci_lastuse = atol(&buf[1]);
			break;

		  case '.':		/* end of file */
			if (tTd(56, 93))
				mci_dump(sm_debug_file(), mci, false);
			return 0;

		  default:
			sm_syslog(LOG_CRIT, NOQID,
				  "%s: line %d: Unknown host status line \"%s\"",
				  FileName == NULL ? mci->mci_host : FileName,
				  LineNumber, buf);
			LineNumber = saveLineNumber;
			return -1;
		}
	}
	LineNumber = saveLineNumber;
	if (tTd(56, 93))
		sm_dprintf("incomplete (missing dot for EOF)\n");
	if (ver < 0)
		return -1;
	return 0;
}
/*
**  MCI_STORE_PERSISTENT -- Store persistent MCI information
**
**	Store information about host that is kept
**	in common for all running sendmails.
**
**	Parameters:
**		mci -- the host/connection to store persistent info for.
**
**	Returns:
**		none.
*/

void
mci_store_persistent(mci)
	MCI *mci;
{
	int save_errno = errno;

	if (mci == NULL)
	{
		if (tTd(56, 1))
			sm_dprintf("mci_store_persistent: NULL mci\n");
		return;
	}

	if (HostStatDir == NULL || mci->mci_host == NULL)
		return;

	if (tTd(56, 1))
		sm_dprintf("mci_store_persistent: Storing information for %s\n",
			   mci->mci_host);

	if (mci->mci_statfile == NULL)
	{
		if (tTd(56, 1))
			sm_dprintf("mci_store_persistent: no statfile\n");
		return;
	}

	sm_io_rewind(mci->mci_statfile, SM_TIME_DEFAULT);
#if !NOFTRUNCATE
	(void) ftruncate(sm_io_getinfo(mci->mci_statfile, SM_IO_WHAT_FD, NULL),
			 (off_t) 0);
#endif

	(void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "V0\n");
	(void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "E%d\n",
			     mci->mci_errno);
	(void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "H%d\n",
			     mci->mci_herrno);
	(void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "S%d\n",
			     mci->mci_exitstat);
	if (mci->mci_status != NULL)
		(void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT,
				     "D%.80s\n",
				     denlstring(mci->mci_status, true, false));
	if (mci->mci_rstatus != NULL)
		(void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT,
				     "R%.80s\n",
				     denlstring(mci->mci_rstatus, true, false));
	(void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, "U%ld\n",
			     (long)(mci->mci_lastuse));
	(void) sm_io_fprintf(mci->mci_statfile, SM_TIME_DEFAULT, ".\n");

	(void) sm_io_flush(mci->mci_statfile, SM_TIME_DEFAULT);

	errno = save_errno;
	return;
}
/*
**  MCI_TRAVERSE_PERSISTENT -- walk persistent status tree
**
**	Recursively find all the mci host files in `pathname'.  Default to
**		main host status directory if no path is provided.
**	Call (*action)(pathname, host) for each file found.
**
**	Note: all information is collected in a list before it is processed.
**	This may not be the best way to do it, but it seems safest, since
**	the file system would be touched while we are attempting to traverse
**	the directory tree otherwise (during purges).
**
**	Parameters:
**		action -- function to call on each node.  If returns < 0,
**			return immediately.
**		pathname -- root of tree.  If null, use main host status
**			directory.
**
**	Returns:
**		< 0 -- if any action routine returns a negative value, that
**			value is returned.
**		0 -- if we successfully went to completion.
**		> 0 -- return status from action()
*/

int
mci_traverse_persistent(action, pathname)
	int (*action)__P((char *, char *));
	char *pathname;
{
	struct stat statbuf;
	DIR *d;
	int ret;

	if (pathname == NULL)
		pathname = HostStatDir;
	if (pathname == NULL)
		return -1;

	if (tTd(56, 1))
		sm_dprintf("mci_traverse: pathname is %s\n", pathname);

	ret = stat(pathname, &statbuf);
	if (ret < 0)
	{
		if (tTd(56, 2))
			sm_dprintf("mci_traverse: Failed to stat %s: %s\n",
				pathname, sm_errstring(errno));
		return ret;
	}
	if (S_ISDIR(statbuf.st_mode))
	{
		bool leftone, removedone;
		size_t len;
		char *newptr;
		struct dirent *e;
		char newpath[MAXPATHLEN];
#if MAXPATHLEN <= MAXNAMLEN - 3
 ERROR "MAXPATHLEN <= MAXNAMLEN - 3"
#endif

		if ((d = opendir(pathname)) == NULL)
		{
			if (tTd(56, 2))
				sm_dprintf("mci_traverse: opendir %s: %s\n",
					pathname, sm_errstring(errno));
			return -1;
		}

		/*
		**  Reserve space for trailing '/', at least one
		**  character, and '\0'
		*/

		len = sizeof(newpath) - 3;
		if (sm_strlcpy(newpath, pathname, len) >= len)
		{
			int save_errno = errno;

			if (tTd(56, 2))
				sm_dprintf("mci_traverse: path \"%s\" too long",
					pathname);
			(void) closedir(d);
			errno = save_errno;
			return -1;
		}
		newptr = newpath + strlen(newpath);
		*newptr++ = '/';
		len = sizeof(newpath) - (newptr - newpath);

		/*
		**  repeat until no file has been removed
		**  this may become ugly when several files "expire"
		**  during these loops, but it's better than doing
		**  a rewinddir() inside the inner loop
		*/

		do
		{
			leftone = removedone = false;
			while ((e = readdir(d)) != NULL)
			{
				if (e->d_name[0] == '.')
					continue;

				if (sm_strlcpy(newptr, e->d_name, len) >= len)
				{
					/* Skip truncated copies */
					if (tTd(56, 4))
					{
						*newptr = '\0';
						sm_dprintf("mci_traverse: path \"%s%s\" too long",
							   newpath, e->d_name);
					}
					continue;
				}

				if (StopRequest)
					stop_sendmail();
				ret = mci_traverse_persistent(action, newpath);
				if (ret < 0)
					break;
				if (ret == 1)
					leftone = true;
				if (!removedone && ret == 0 &&
				    action == mci_purge_persistent)
					removedone = true;
			}
			if (ret < 0)
				break;

			/*
			**  The following appears to be
			**  necessary during purges, since
			**  we modify the directory structure
			*/

			if (removedone)
				rewinddir(d);
			if (tTd(56, 40))
				sm_dprintf("mci_traverse: path %s: ret %d removed %d left %d\n",
					pathname, ret, removedone, leftone);
		} while (removedone);

		/* purge (or whatever) the directory proper */
		if (!leftone)
		{
			*--newptr = '\0';
			ret = (*action)(newpath, NULL);
		}
		(void) closedir(d);
	}
	else if (S_ISREG(statbuf.st_mode))
	{
		char *end = pathname + strlen(pathname) - 1;
		char *start;
		char *scan;
		char host[MAXHOSTNAMELEN];
		char *hostptr = host;

		/*
		**  Reconstruct the host name from the path to the
		**  persistent information.
		*/

		do
		{
			if (hostptr != host)
				*(hostptr++) = '.';
			start = end;
			while (start > pathname && *(start - 1) != '/')
				start--;

			if (*end == '.')
				end--;

			for (scan = start; scan <= end; scan++)
				*(hostptr++) = *scan;

			end = start - 2;
		} while (end > pathname && *end == '.');

		*hostptr = '\0';

		/*
		**  Do something with the file containing the persistent
		**  information.
		*/

		ret = (*action)(pathname, host);
	}

	return ret;
}
/*
**  MCI_PRINT_PERSISTENT -- print persistent info
**
**	Dump the persistent information in the file 'pathname'
**
**	Parameters:
**		pathname -- the pathname to the status file.
**		hostname -- the corresponding host name.
**
**	Returns:
**		0
*/

int
mci_print_persistent(pathname, hostname)
	char *pathname;
	char *hostname;
{
	static bool initflag = false;
	SM_FILE_T *fp;
	int width = Verbose ? 78 : 25;
	bool locked;
	MCI mcib;

	/* skip directories */
	if (hostname == NULL)
		return 0;

	if (StopRequest)
		stop_sendmail();

	if (!initflag)
	{
		initflag = true;
		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
				     " -------------- Hostname --------------- How long ago ---------Results---------\n");
	}

	fp = safefopen(pathname, O_RDONLY, FileMode,
		       SFF_NOLOCK|SFF_NOLINK|SFF_OPENASROOT|SFF_REGONLY|SFF_SAFEDIRPATH);

	if (fp == NULL)
	{
		if (tTd(56, 1))
			sm_dprintf("mci_print_persistent: cannot open %s: %s\n",
				pathname, sm_errstring(errno));
		return 0;
	}

	FileName = pathname;
	memset(&mcib, '\0', sizeof(mcib));
	if (mci_read_persistent(fp, &mcib) < 0)
	{
		syserr("%s: could not read status file", pathname);
		(void) sm_io_close(fp, SM_TIME_DEFAULT);
		FileName = NULL;
		return 0;
	}

	locked = !lockfile(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), pathname,
			   "", LOCK_SH|LOCK_NB);
	(void) sm_io_close(fp, SM_TIME_DEFAULT);
	FileName = NULL;

	(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%c%-39s %12s ",
			     locked ? '*' : ' ', hostname,
			     pintvl(curtime() - mcib.mci_lastuse, true));
	if (mcib.mci_rstatus != NULL)
		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%.*s\n", width,
				     mcib.mci_rstatus);
	else if (mcib.mci_exitstat == EX_TEMPFAIL && mcib.mci_errno != 0)
		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
				     "Deferred: %.*s\n", width - 10,
				     sm_errstring(mcib.mci_errno));
	else if (mcib.mci_exitstat != 0)
	{
		char *exmsg = sm_sysexmsg(mcib.mci_exitstat);

		if (exmsg == NULL)
		{
			char buf[80];

			(void) sm_snprintf(buf, sizeof(buf),
				"Unknown mailer error %d",
				mcib.mci_exitstat);
			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%.*s\n",
					     width, buf);
		}
		else
			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%.*s\n",
					     width, &exmsg[5]);
	}
	else if (mcib.mci_errno == 0)
		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "OK\n");
	else
		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "OK: %.*s\n",
				     width - 4, sm_errstring(mcib.mci_errno));

	return 0;
}
/*
**  MCI_PURGE_PERSISTENT -- Remove a persistence status file.
**
**	Parameters:
**		pathname -- path to the status file.
**		hostname -- name of host corresponding to that file.
**			NULL if this is a directory (domain).
**
**	Returns:
**		0 -- ok
**		1 -- file not deleted (too young, incorrect format)
**		< 0 -- some error occurred
*/

int
mci_purge_persistent(pathname, hostname)
	char *pathname;
	char *hostname;
{
	struct stat statbuf;
	char *end = pathname + strlen(pathname) - 1;
	int ret;

	if (tTd(56, 1))
		sm_dprintf("mci_purge_persistent: purging %s\n", pathname);

	ret = stat(pathname, &statbuf);
	if (ret < 0)
	{
		if (tTd(56, 2))
			sm_dprintf("mci_purge_persistent: Failed to stat %s: %s\n",
				pathname, sm_errstring(errno));
		return ret;
	}
	if (curtime() - statbuf.st_mtime <= MciInfoTimeout)
		return 1;
	if (hostname != NULL)
	{
		/* remove the file */
		ret = unlink(pathname);
		if (ret < 0)
		{
			if (LogLevel > 8)
				sm_syslog(LOG_ERR, NOQID,
					  "mci_purge_persistent: failed to unlink %s: %s",
					  pathname, sm_errstring(errno));
			if (tTd(56, 2))
				sm_dprintf("mci_purge_persistent: failed to unlink %s: %s\n",
					pathname, sm_errstring(errno));
			return ret;
		}
	}
	else
	{
		/* remove the directory */
		if (*end != '.')
			return 1;

		if (tTd(56, 1))
			sm_dprintf("mci_purge_persistent: dpurge %s\n", pathname);

		ret = rmdir(pathname);
		if (ret < 0)
		{
			if (tTd(56, 2))
				sm_dprintf("mci_purge_persistent: rmdir %s: %s\n",
					pathname, sm_errstring(errno));
			return ret;
		}
	}

	return 0;
}
/*
**  MCI_GENERATE_PERSISTENT_PATH -- generate path from hostname
**
**	Given `host', convert from a.b.c to $HostStatDir/c./b./a,
**	putting the result into `path'.  if `createflag' is set, intervening
**	directories will be created as needed.
**
**	Parameters:
**		host -- host name to convert from.
**		path -- place to store result.
**		pathlen -- length of path buffer.
**		createflag -- if set, create intervening directories as
**			needed.
**
**	Returns:
**		0 -- success
**		-1 -- failure
*/

static int
mci_generate_persistent_path(host, path, pathlen, createflag)
	const char *host;
	char *path;
	int pathlen;
	bool createflag;
{
	char *elem, *p, *x, ch;
	int ret = 0;
	int len;
	char t_host[MAXHOSTNAMELEN];
#if NETINET6
	struct in6_addr in6_addr;
#endif

	/*
	**  Rationality check the arguments.
	*/

	if (host == NULL)
	{
		syserr("mci_generate_persistent_path: null host");
		return -1;
	}
	if (path == NULL)
	{
		syserr("mci_generate_persistent_path: null path");
		return -1;
	}

	if (tTd(56, 80))
		sm_dprintf("mci_generate_persistent_path(%s): ", host);

	if (*host == '\0' || *host == '.')
		return -1;

	/* make certain this is not a bracketed host number */
	if (strlen(host) > sizeof(t_host) - 1)
		return -1;
	if (host[0] == '[')
		(void) sm_strlcpy(t_host, host + 1, sizeof(t_host));
	else
		(void) sm_strlcpy(t_host, host, sizeof(t_host));

	/*
	**  Delete any trailing dots from the hostname.
	**  Leave 'elem' pointing at the \0.
	*/

	elem = t_host + strlen(t_host);
	while (elem > t_host &&
	       (elem[-1] == '.' || (host[0] == '[' && elem[-1] == ']')))
		*--elem = '\0';

	/* check for bogus bracketed address */
	if (host[0] == '[')
	{
		bool good = false;
# if NETINET6
		if (anynet_pton(AF_INET6, t_host, &in6_addr) == 1)
			good = true;
# endif
# if NETINET
		if (inet_addr(t_host) != INADDR_NONE)
			good = true;
# endif
		if (!good)
			return -1;
	}

	/* check for what will be the final length of the path */
	len = strlen(HostStatDir) + 2;
	for (p = (char *) t_host; *p != '\0'; p++)
	{
		if (*p == '.')
			len++;
		len++;
		if (p[0] == '.' && p[1] == '.')
			return -1;
	}
	if (len > pathlen || len < 1)
		return -1;
	(void) sm_strlcpy(path, HostStatDir, pathlen);
	p = path + strlen(path);
	while (elem > t_host)
	{
		if (!path_is_dir(path, createflag))
		{
			ret = -1;
			break;
		}
		elem--;
		while (elem >= t_host && *elem != '.')
			elem--;
		*p++ = '/';
		x = elem + 1;
		while ((ch = *x++) != '\0' && ch != '.')
		{
			if (isascii(ch) && isupper(ch))
				ch = tolower(ch);
			if (ch == '/')
				ch = ':';	/* / -> : */
			*p++ = ch;
		}
		if (elem >= t_host)
			*p++ = '.';
		*p = '\0';
	}
	if (tTd(56, 80))
	{
		if (ret < 0)
			sm_dprintf("FAILURE %d\n", ret);
		else
			sm_dprintf("SUCCESS %s\n", path);
	}
	return ret;
}