summaryrefslogtreecommitdiff
path: root/usr/src/lib/libinetsvc/common/inetsvc.c
blob: 6c688c0c11e5be6e05e93a35350f33a0fa8e6d95 (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
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
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * This library contains a set of routines that are shared amongst inetd,
 * inetadm, inetconv and the formerly internal inetd services. Amongst the
 * routines are ones for reading and validating the configuration of an
 * inetd service, a routine for requesting inetd be refreshed, ones for
 * reading, calculating and writing the hash of an inetd.conf file, and
 * numerous utility routines shared amongst the formerly internal inetd
 * services.
 */


#include <string.h>
#include <rpc/rpcent.h>
#include <netdb.h>
#include <limits.h>
#include <errno.h>
#include <inetsvc.h>
#include <stdlib.h>
#include <unistd.h>
#include <nss_dbdefs.h>
#include <stdio.h>
#include <fcntl.h>
#include <pwd.h>
#include <md5.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>
#include <syslog.h>
#include <libintl.h>
#include <stdlib.h>
#include <assert.h>
#include <rpc/nettype.h>
#include <libuutil.h>

static inetd_prop_t inetd_properties[] = {
	{PR_SVC_NAME_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_STRING,
	    B_FALSE, IVE_UNSET, 0, B_FALSE},
	{PR_SOCK_TYPE_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_STRING,
	    B_FALSE, IVE_UNSET, 0, B_FALSE},
	{PR_PROTO_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_STRING_LIST,
	    B_FALSE, IVE_UNSET, 0, B_FALSE},
	{PR_ISRPC_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_BOOLEAN,
	    B_FALSE, IVE_UNSET, 0, B_FALSE},
	{PR_RPC_LW_VER_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_INTEGER,
	    B_FALSE, IVE_UNSET, 0, B_FALSE},
	{PR_RPC_HI_VER_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_INTEGER,
	    B_FALSE, IVE_UNSET, 0, B_FALSE},
	{PR_ISWAIT_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_BOOLEAN,
	    B_FALSE, IVE_UNSET, 0, B_FALSE},
	{PR_EXEC_NAME, START_METHOD_NAME, INET_TYPE_STRING,
	    B_FALSE, IVE_UNSET, 0, B_FALSE},
	{PR_ARG0_NAME, START_METHOD_NAME, INET_TYPE_STRING,
	    B_FALSE, IVE_UNSET, 0, B_FALSE},
	{PR_USER_NAME, START_METHOD_NAME, INET_TYPE_STRING,
	    B_FALSE, IVE_UNSET, 0, B_FALSE},
	{PR_BIND_ADDR_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_STRING,
	    B_TRUE, IVE_UNSET, 0, B_FALSE},
	{PR_BIND_FAIL_MAX_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_INTEGER,
	    B_TRUE, IVE_UNSET, 0, B_FALSE},
	{PR_BIND_FAIL_INTVL_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_INTEGER,
	    B_TRUE, IVE_UNSET, 0, B_FALSE},
	{PR_CON_RATE_MAX_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_INTEGER,
	    B_TRUE, IVE_UNSET, 0, B_FALSE},
	{PR_MAX_COPIES_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_INTEGER,
	    B_TRUE, IVE_UNSET, 0, B_FALSE},
	{PR_CON_RATE_OFFLINE_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_INTEGER,
	    B_TRUE, IVE_UNSET, 0, B_FALSE},
	{PR_MAX_FAIL_RATE_CNT_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_INTEGER,
	    B_TRUE, IVE_UNSET, 0, B_FALSE},
	{PR_MAX_FAIL_RATE_INTVL_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_INTEGER,
	    B_TRUE, IVE_UNSET, 0, B_FALSE},
	{PR_INHERIT_ENV_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_BOOLEAN,
	    B_TRUE, IVE_UNSET, 0, B_FALSE},
	{PR_DO_TCP_TRACE_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_BOOLEAN,
	    B_TRUE, IVE_UNSET, 0, B_FALSE},
	{PR_DO_TCP_WRAPPERS_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_BOOLEAN,
	    B_TRUE, IVE_UNSET, 0, B_FALSE},
	{PR_CONNECTION_BACKLOG_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_INTEGER,
	    B_TRUE, IVE_UNSET, 0, B_FALSE},
	{PR_DO_TCP_KEEPALIVE_NAME, PG_NAME_SERVICE_CONFIG, INET_TYPE_BOOLEAN,
	    B_TRUE, IVE_UNSET, 0, B_FALSE},
	{NULL},
};

#define	INETSVC_SVC_BUF_MAX (NSS_BUFLEN_RPC + sizeof (struct rpcent))

#define	DIGEST_LEN	16
#define	READ_BUFSIZ	8192
#define	HASH_PG		"hash"
#define	HASH_PROP	"md5sum"

/*
 * Inactivity timer used by dg_template(). After this many seconds of network
 * inactivity dg_template will cease listening for new datagrams and return.
 */
#define	DG_INACTIVITY_TIMEOUT	60

static boolean_t v6_proto(const char *);

boolean_t
is_tlx_service(inetd_prop_t *props)
{
	return ((strcmp(SOCKTYPE_TLI_STR,
	    props[PT_SOCK_TYPE_INDEX].ip_value.iv_string) == 0) ||
	    (strcmp(SOCKTYPE_XTI_STR,
	    props[PT_SOCK_TYPE_INDEX].ip_value.iv_string) == 0));
}

/*
 * Return a reference to the property table. Number of entries in table
 * are returned in num_elements argument.
 */
inetd_prop_t *
get_prop_table(size_t *num_elements)
{
	*num_elements = sizeof (inetd_properties) / sizeof (inetd_prop_t);
	return (&inetd_properties[0]);
}

/*
 * find_prop takes an array of inetd_prop_t's, the name of an inetd
 * property, the type expected, and returns a pointer to the matching member,
 * or NULL.
 */
inetd_prop_t *
find_prop(const inetd_prop_t *prop, const char *name, inet_type_t type)
{
	int		i = 0;

	while (prop[i].ip_name != NULL && strcmp(name, prop[i].ip_name) != 0)
		i++;

	if (prop[i].ip_name == NULL)
		return (NULL);

	if (prop[i].ip_type != type)
		return (NULL);

	return ((inetd_prop_t *)prop + i);
}

/*
 * get_prop_value_int takes an array of inetd_prop_t's together with the name of
 * an inetd property and returns the value of the property.  It's expected that
 * the property exists in the searched array.
 */
int64_t
get_prop_value_int(const inetd_prop_t *prop, const char *name)
{
	inetd_prop_t	*p;

	p = find_prop(prop, name, INET_TYPE_INTEGER);
	return (p->ip_value.iv_int);
}

/*
 * get_prop_value_count takes an array of inetd_prop_t's together with the name
 * of an inetd property and returns the value of the property.  It's expected
 * that the property exists in the searched array.
 */
uint64_t
get_prop_value_count(const inetd_prop_t *prop, const char *name)
{
	inetd_prop_t	*p;

	p = find_prop(prop, name, INET_TYPE_COUNT);
	return (p->ip_value.iv_cnt);
}

/*
 * get_prop_value_boolean takes an array of inetd_prop_t's together with the
 * name of an inetd property and returns the value of the property.  It's
 * expected that the property exists in the searched array.
 */
boolean_t
get_prop_value_boolean(const inetd_prop_t *prop, const char *name)
{
	inetd_prop_t	*p;

	p = find_prop(prop, name, INET_TYPE_BOOLEAN);
	return (p->ip_value.iv_boolean);
}

/*
 * get_prop_value_string takes an array of inetd_prop_t's together with
 * the name of an inetd property and returns the value of the property.
 * It's expected that the property exists in the searched array.
 */
const char *
get_prop_value_string(const inetd_prop_t *prop, const char *name)
{
	inetd_prop_t	*p;

	p = find_prop(prop, name, INET_TYPE_STRING);
	return (p->ip_value.iv_string);
}

/*
 * get_prop_value_string_list takes an array of inetd_prop_t's together
 * with the name of an inetd property and returns the value of the property.
 * It's expected that the property exists in the searched array.
 */
const char **
get_prop_value_string_list(const inetd_prop_t *prop, const char *name)
{
	inetd_prop_t	*p;

	p = find_prop(prop, name, INET_TYPE_STRING_LIST);
	return ((const char **)p->ip_value.iv_string_list);
}

/*
 * put_prop_value_int takes an array of inetd_prop_t's, a name of an inetd
 * property, and a value.  It copies the value into the property
 * in the array.  It's expected that the property exists in the searched array.
 */
void
put_prop_value_int(inetd_prop_t *prop, const char *name, int64_t value)
{
	inetd_prop_t	*p;

	p = find_prop(prop, name, INET_TYPE_INTEGER);
	p->ip_value.iv_int = value;
	p->ip_error = IVE_VALID;
}

/*
 * put_prop_value_count takes an array of inetd_prop_t's, a name of an inetd
 * property, and a value.  It copies the value into the property
 * in the array.  It's expected that the property exists in the searched array.
 */
void
put_prop_value_count(inetd_prop_t *prop, const char *name, uint64_t value)
{
	inetd_prop_t	*p;

	p = find_prop(prop, name, INET_TYPE_COUNT);
	p->ip_value.iv_cnt = value;
	p->ip_error = IVE_VALID;
}

/*
 * put_prop_value_boolean takes an array of inetd_prop_t's, a name of an inetd
 * property, and a value.  It copies the value into the property
 * in the array.  It's expected that the property exists in the searched array.
 */
void
put_prop_value_boolean(inetd_prop_t *prop, const char *name, boolean_t value)
{
	inetd_prop_t	*p;

	p = find_prop(prop, name, INET_TYPE_BOOLEAN);
	p->ip_value.iv_boolean = value;
	p->ip_error = IVE_VALID;
}

/*
 * put_prop_value_string takes an array of inetd_prop_t's, a name of an inetd
 * property, and a value.  It duplicates the value into the property
 * in the array, and returns B_TRUE for success and B_FALSE for failure.  It's
 * expected that the property exists in the searched array.
 */
boolean_t
put_prop_value_string(inetd_prop_t *prop, const char *name, const char *value)
{
	inetd_prop_t	*p;

	if (strlen(value) >= scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH)) {
		errno = E2BIG;
		return (B_FALSE);
	}
	p = find_prop(prop, name, INET_TYPE_STRING);
	if ((p->ip_value.iv_string = strdup(value)) == NULL)
		return (B_FALSE);
	p->ip_error = IVE_VALID;
	return (B_TRUE);
}

/*
 * put_prop_value_string_list takes an array of inetd_prop_t's, a name of an
 * inetd property, and a value.  It copies the value into the property
 * in the array.  It's expected that the property exists in the searched array.
 */
void
put_prop_value_string_list(inetd_prop_t *prop, const char *name, char **value)
{
	inetd_prop_t	*p;

	p = find_prop(prop, name, INET_TYPE_STRING_LIST);
	p->ip_value.iv_string_list = value;
	p->ip_error = IVE_VALID;
}

static void
destroy_rpc_info(rpc_info_t *rpc)
{
	if (rpc != NULL) {
		free(rpc->netbuf.buf);
		free(rpc->netid);
		free(rpc);
	}
}

/*
 * If 'proto' is a valid netid,  and no memory allocations fail, returns a
 * pointer to an allocated and initialized rpc_info_t, else NULL.
 */
static rpc_info_t *
create_rpc_info(const char *proto, int pnum, int low_ver, int high_ver)
{
	struct netconfig	*nconf;
	rpc_info_t		*ret;

	if ((ret = calloc(1, sizeof (rpc_info_t))) == NULL)
		return (NULL);

	ret->netbuf.maxlen = sizeof (struct sockaddr_storage);
	if ((ret->netbuf.buf = malloc(ret->netbuf.maxlen)) == NULL) {
		free(ret);
		return (NULL);
	}

	ret->prognum = pnum;
	ret->lowver = low_ver;
	ret->highver = high_ver;

	if ((ret->netid = strdup(proto)) == NULL) {
		destroy_rpc_info(ret);
		return (NULL);
	}

	/*
	 * Determine whether this is a loopback transport. If getnetconfigent()
	 * fails, we check to see whether it was the result of a v6 proto
	 * being specified and no IPv6 interface was configured on the system;
	 * if this holds, we know it must not be a loopback transport, else
	 * getnetconfigent() must be miss-behaving, so return an error.
	 */
	if ((nconf = getnetconfigent(proto)) != NULL) {
		if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
			ret->is_loopback = B_TRUE;
		freenetconfigent(nconf);
	} else if (!v6_proto(proto)) {
		destroy_rpc_info(ret);
		return (NULL);
	}

	return (ret);
}

void
destroy_tlx_info(tlx_info_t *tlx)
{
	tlx_conn_ind_t  *ci;
	void		*cookie = NULL;

	if (tlx == NULL)
		return;

	free(tlx->dev_name);

	if (tlx->conn_ind_queue != NULL) {
		/* free up conn ind queue */
		while ((ci = uu_list_teardown(tlx->conn_ind_queue, &cookie)) !=
		    NULL) {
			(void) t_free((char *)ci->call, T_CALL);
			free(ci);
		}
		uu_list_destroy(tlx->conn_ind_queue);
	}

	free(tlx->local_addr.buf);
	free(tlx);
}

/*
 * Allocate, initialize and return a pointer to a tlx_info_t structure.
 * On memory allocation failure NULL is returned.
 */
static tlx_info_t *
create_tlx_info(const char *proto, uu_list_pool_t *conn_ind_pool)
{
	size_t			sz;
	tlx_info_t		*ret;

	if ((ret = calloc(1, sizeof (tlx_info_t))) == NULL)
		return (NULL);

	ret->local_addr.maxlen = sizeof (struct sockaddr_storage);
	if ((ret->local_addr.buf = calloc(1, ret->local_addr.maxlen)) == NULL)
		goto fail;

	if ((ret->conn_ind_queue = uu_list_create(conn_ind_pool, NULL, 0)) ==
	    NULL)
		goto fail;

	ret->local_addr.len = sizeof (struct sockaddr_in);
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	((struct sockaddr_in *)(ret->local_addr.buf))->sin_family = AF_INET;
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	((struct sockaddr_in *)(ret->local_addr.buf))->sin_addr.s_addr =
	    htonl(INADDR_ANY);

	/* store device name, constructing if necessary */
	if (proto[0] != '/') {
		sz = strlen("/dev/") + strlen(proto) + 1;
		if ((ret->dev_name = malloc(sz)) == NULL)
			goto fail;
		(void) snprintf(ret->dev_name, sz, "/dev/%s", proto);
	} else if ((ret->dev_name = strdup(proto)) == NULL) {
			goto fail;
	}

	return (ret);

fail:
	destroy_tlx_info(ret);
	return (NULL);
}

/*
 * Returns B_TRUE if this is a v6 protocol valid for both TLI and socket
 * based services, else B_FALSE.
 */
static boolean_t
v6_proto(const char *proto)
{
	return ((strcmp(proto, SOCKET_PROTO_TCP6) == 0) ||
	    (strcmp(proto, SOCKET_PROTO_UDP6) == 0));
}

/*
 * Returns B_TRUE if this is a valid v6 protocol for a socket based service,
 * else B_FALSE.
 */
static boolean_t
v6_socket_proto(const char *proto)
{
	return ((strcmp(proto, SOCKET_PROTO_SCTP6) == 0) ||
	    v6_proto(proto));

}

static boolean_t
valid_socket_proto(const char *proto)
{
	return (v6_socket_proto(proto) ||
	    (strcmp(proto, SOCKET_PROTO_SCTP) == 0) ||
	    (strcmp(proto, SOCKET_PROTO_TCP) == 0) ||
	    (strcmp(proto, SOCKET_PROTO_UDP) == 0));
}

/*
 * Free all the memory consumed by 'pi' associated with the instance
 * with configuration 'cfg'.
 */
static void
destroy_proto_info(basic_cfg_t *cfg, proto_info_t *pi)
{
	if (pi == NULL)
		return;

	assert(pi->listen_fd == -1);

	free(pi->proto);
	if (pi->ri != NULL)
		destroy_rpc_info(pi->ri);
	if (cfg->istlx) {
		destroy_tlx_info((tlx_info_t *)pi);
	} else {
		free(pi);
	}
}

void
destroy_proto_list(basic_cfg_t *cfg)
{
	void		*cookie = NULL;
	proto_info_t	*pi;

	if (cfg->proto_list == NULL)
		return;

	while ((pi = uu_list_teardown(cfg->proto_list, &cookie)) != NULL)
		destroy_proto_info(cfg, pi);
	uu_list_destroy(cfg->proto_list);
	cfg->proto_list = NULL;
}

void
destroy_basic_cfg(basic_cfg_t *cfg)
{
	if (cfg == NULL)
		return;

	free(cfg->bind_addr);
	destroy_proto_list(cfg);
	free(cfg->svc_name);
	free(cfg);
}

/*
 * Overwrite the socket address with the address specified by the
 * bind_addr property.
 */
static int
set_bind_addr(struct sockaddr_storage *ss, char *bind_addr)
{
	struct addrinfo hints, *res;

	if (bind_addr == NULL || bind_addr[0] == '\0')
		return (0);

	(void) memset(&hints, 0, sizeof (hints));
	hints.ai_flags = AI_DEFAULT;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_family = ss->ss_family;
	if (getaddrinfo(bind_addr, "", &hints, &res) != 0) {
		return (-1);
	} else {
		void *p = res->ai_addr;
		struct sockaddr_storage *newss = p;

		(void) memcpy(SS_SINADDR(*ss), SS_SINADDR(*newss),
		    SS_ADDRLEN(*ss));
		freeaddrinfo(res);
		return (0);
	}
}

/*
 * valid_props validates all the properties in an array of inetd_prop_t's,
 * marking each property as valid or invalid.  If any properties are invalid,
 * it returns B_FALSE, otherwise it returns B_TRUE.  Note that some properties
 * are interdependent, so if one is invalid, it leaves others in an
 * indeterminate state (such as ISRPC and SVC_NAME).  In this case, the
 * indeterminate property will be marked valid.  IE, the only properties
 * marked invalid are those that are KNOWN to be invalid.
 *
 * Piggy-backed onto this validation if 'fmri' is non-NULL is the construction
 * of a structured configuration, a basic_cfg_t,  which is used by inetd.
 * If 'fmri' is set then the latter three parameters need to be set to
 * non-NULL values, and if the configuration is valid, the storage referenced
 * by cfgpp is set to point at an initialized basic_cfg_t.
 */
boolean_t
valid_props(inetd_prop_t *prop, const char *fmri, basic_cfg_t **cfgpp,
    uu_list_pool_t *proto_info_pool, uu_list_pool_t *tlx_ci_pool)
{
	char			*bufp, *cp;
	boolean_t		ret = B_TRUE;
	int			i;
	long			uidl;
	boolean_t		isrpc;
	int			sock_type_id;
	int			rpc_pnum;
	int			rpc_lv, rpc_hv;
	basic_cfg_t		*cfg;
	char			*proto = NULL;
	int			pi;
	char			**netids = NULL;
	int			ni = 0;

	if (fmri != NULL)
		assert((cfgpp != NULL) && (proto_info_pool != NULL) &&
		    (tlx_ci_pool != NULL));

	/*
	 * Set all checkable properties to valid as a baseline.  We'll be
	 * marking all invalid properties.
	 */
	for (i = 0; prop[i].ip_name != NULL; i++) {
		if (prop[i].ip_error != IVE_UNSET)
			prop[i].ip_error = IVE_VALID;
	}

	if (((cfg = calloc(1, sizeof (basic_cfg_t))) == NULL) ||
	    ((fmri != NULL) &&
	    ((cfg->proto_list = uu_list_create(proto_info_pool, NULL, 0)) ==
	    NULL))) {
		free(cfg);
		return (B_FALSE);
	}

	/* Check a service name was supplied */
	if ((prop[PT_SVC_NAME_INDEX].ip_error == IVE_UNSET) ||
	    ((cfg->svc_name =
	    strdup(prop[PT_SVC_NAME_INDEX].ip_value.iv_string)) == NULL))
		prop[PT_SVC_NAME_INDEX].ip_error = IVE_INVALID;

	/* Check that iswait and isrpc have valid boolean values */

	if ((prop[PT_ISWAIT_INDEX].ip_error == IVE_UNSET) ||
	    (((cfg->iswait = prop[PT_ISWAIT_INDEX].ip_value.iv_boolean) !=
	    B_TRUE) && (cfg->iswait != B_FALSE)))
		prop[PT_ISWAIT_INDEX].ip_error = IVE_INVALID;

	if ((prop[PT_ISRPC_INDEX].ip_error == IVE_UNSET) ||
	    (((isrpc = prop[PT_ISRPC_INDEX].ip_value.iv_boolean) != B_TRUE) &&
	    (isrpc != B_FALSE))) {
		prop[PT_ISRPC_INDEX].ip_error = IVE_INVALID;
	} else if (isrpc) {
		/*
		 * This is an RPC service, so ensure that the RPC version
		 * numbers are zero or greater, that the low version isn't
		 * greater than the high version and a valid program name
		 * is supplied.
		 */

		if ((prop[PT_RPC_LW_VER_INDEX].ip_error == IVE_UNSET) ||
		    ((rpc_lv = prop[PT_RPC_LW_VER_INDEX].ip_value.iv_int) <
		    0))
			prop[PT_RPC_LW_VER_INDEX].ip_error = IVE_INVALID;

		if ((prop[PT_RPC_HI_VER_INDEX].ip_error == IVE_UNSET) ||
		    ((rpc_hv = prop[PT_RPC_HI_VER_INDEX].ip_value.iv_int) <
		    0))
			prop[PT_RPC_HI_VER_INDEX].ip_error = IVE_INVALID;

		if ((prop[PT_RPC_LW_VER_INDEX].ip_error != IVE_INVALID) &&
		    (prop[PT_RPC_HI_VER_INDEX].ip_error != IVE_INVALID) &&
		    (rpc_lv > rpc_hv)) {
			prop[PT_RPC_LW_VER_INDEX].ip_error = IVE_INVALID;
			prop[PT_RPC_HI_VER_INDEX].ip_error = IVE_INVALID;
		}

		if ((cfg->svc_name != NULL) &&
		    ((rpc_pnum = get_rpc_prognum(cfg->svc_name)) == -1))
			prop[PT_SVC_NAME_INDEX].ip_error = IVE_INVALID;
	}

	/* Check that the socket type is one of the acceptable values. */
	cfg->istlx = B_FALSE;
	if ((prop[PT_SOCK_TYPE_INDEX].ip_error == IVE_UNSET) ||
	    ((sock_type_id = get_sock_type_id(
	    prop[PT_SOCK_TYPE_INDEX].ip_value.iv_string)) == -1) &&
	    !(cfg->istlx = is_tlx_service(prop)))
		prop[PT_SOCK_TYPE_INDEX].ip_error = IVE_INVALID;

	/* Get the bind address */
	if (!cfg->istlx && prop[PT_BIND_ADDR_INDEX].ip_error != IVE_UNSET &&
	    (cfg->bind_addr =
	    strdup(prop[PT_BIND_ADDR_INDEX].ip_value.iv_string)) == NULL)
		prop[PT_BIND_ADDR_INDEX].ip_error = IVE_INVALID;

	/*
	 * Iterate through all the different protos/netids resulting from the
	 * proto property and check that they're valid and perform checks on
	 * other fields that are tied-in with the proto.
	 */

	pi = 0;
	do {
		socket_info_t		*si = NULL;
		tlx_info_t		*ti = NULL;
		proto_info_t		*p_inf = NULL;
		boolean_t		v6only = B_FALSE;
		char			*only;
		boolean_t		invalid_proto = B_FALSE;
		char			**protos;
		struct protoent		pe;
		char			gpbuf[1024];
		struct netconfig	*nconf = NULL;

		/*
		 * If we don't know whether it's an rpc service or its
		 * endpoint type, we can't do any of the proto checks as we
		 * have no context; break out.
		 */
		if ((prop[PT_ISRPC_INDEX].ip_error != IVE_VALID) ||
		    (prop[PT_SOCK_TYPE_INDEX].ip_error != IVE_VALID))
			break;

		/* skip proto specific processing if the proto isn't set. */
		if (prop[PT_PROTO_INDEX].ip_error == IVE_UNSET) {
			invalid_proto = B_TRUE;
			goto past_proto_processing;
		}
		protos = prop[PT_PROTO_INDEX].ip_value.iv_string_list;

		/*
		 * Get the next netid/proto.
		 */

		if (!cfg->istlx || !isrpc) {
			proto = protos[pi++];
		/*
		 * This is a TLI/RPC service, so get the next netid, expanding
		 * any supplied nettype.
		 */
		} else if ((netids == NULL) ||
		    ((proto = netids[ni++]) == NULL)) {
			/*
			 * Either this is the first time around or
			 * we've exhausted the last set of netids, so
			 * try and get the next set using the currently
			 * indexed proto entry.
			 */

			if (netids != NULL) {
				destroy_strings(netids);
				netids = NULL;
			}

			if (protos[pi] != NULL) {
				if ((netids = get_netids(protos[pi++])) ==
				    NULL) {
					invalid_proto = B_TRUE;
					proto = protos[pi - 1];
				} else {
					ni = 0;
					proto = netids[ni++];
				}
			} else {
				proto = NULL;
			}
		}

		if (proto == NULL)
			break;

		if (invalid_proto)
			goto past_proto_processing;

		/* strip a trailing only to simplify further processing */
		only = proto + strlen(proto) - (sizeof ("6only") - 1);
		if ((only > proto) && (strcmp(only, "6only") == 0)) {
			*++only = '\0';
			v6only = B_TRUE;
		}

		/* validate the proto/netid */

		if (!cfg->istlx) {
			if (!valid_socket_proto(proto))
				invalid_proto = B_TRUE;
		} else {
			/*
			 * Check if we've got a valid netid. If
			 * getnetconfigent() fails, we check to see whether
			 * we've got a v6 netid that may have been rejected
			 * because no IPv6 interface was configured before
			 * flagging 'proto' as invalid. If the latter condition
			 * holds, we don't flag the proto as invalid, and
			 * leave inetd to handle the value appropriately
			 * when it tries to listen on behalf of the service.
			 */
			if (((nconf = getnetconfigent(proto)) == NULL) &&
			    !v6_proto(proto))
				invalid_proto = B_TRUE;
		}
		if (invalid_proto)
			goto past_proto_processing;

		/*
		 * dissallow datagram type nowait services
		 */
		if ((prop[PT_ISWAIT_INDEX].ip_error == IVE_VALID) &&
		    !cfg->iswait) {
			if (strncmp(proto, SOCKET_PROTO_UDP,
			    sizeof (SOCKET_PROTO_UDP) - 1) == 0) {
				invalid_proto = B_TRUE;
			} else if (cfg->istlx && (nconf != NULL) &&
			    (nconf->nc_semantics == NC_TPI_CLTS)) {
					invalid_proto = B_TRUE;
			}
			if (invalid_proto) {
				prop[PT_ISWAIT_INDEX].ip_error = IVE_INVALID;
				goto past_proto_processing;
			}
		}

		/*
		 * We're running in validate only mode. Don't bother creating
		 * any proto structures (they don't do any further validation).
		 */
		if (fmri == NULL)
			goto past_proto_processing;

		/*
		 * Create the apropriate transport info structure.
		 */
		if (cfg->istlx) {
			if ((ti = create_tlx_info(proto, tlx_ci_pool)) != NULL)
				p_inf = (proto_info_t *)ti;
		} else {
			struct sockaddr_storage *ss;

			if ((si = calloc(1, sizeof (socket_info_t))) != NULL) {
				p_inf = (proto_info_t *)si;
				si->type = sock_type_id;
				ss = &si->local_addr;

				if (v6_socket_proto(proto)) {
					ss->ss_family = AF_INET6;
					/* already in network order */
					((struct sockaddr_in6 *)ss)->sin6_addr =
					    in6addr_any;
				} else {
					ss->ss_family = AF_INET;
					((struct sockaddr_in *)ss)->sin_addr.
					    s_addr = htonl(INADDR_ANY);
				}
				if (set_bind_addr(ss, cfg->bind_addr) != 0) {
					prop[PT_BIND_ADDR_INDEX].ip_error =
					    IVE_INVALID;
				}
			}
		}
		if (p_inf == NULL) {
			invalid_proto = B_TRUE;
			goto past_proto_processing;
		}

		p_inf->v6only = v6only;

		/*
		 * Store the supplied proto string for error reporting,
		 * re-attaching the 'only' suffix if one was taken off.
		 */
		if ((p_inf->proto = malloc(strlen(proto) + 5)) == NULL) {
			invalid_proto = B_TRUE;
			goto past_proto_processing;
		} else {
			(void) strlcpy(p_inf->proto, proto, strlen(proto) + 5);
			if (v6only)
				(void) strlcat(p_inf->proto, "only",
				    strlen(proto) + 5);
		}

		/*
		 * Validate and setup RPC/non-RPC specifics.
		 */

		if (isrpc) {
			rpc_info_t *ri;

			if ((rpc_pnum != -1) && (rpc_lv != -1) &&
			    (rpc_hv != -1)) {
				if ((ri = create_rpc_info(proto, rpc_pnum,
				    rpc_lv, rpc_hv)) == NULL) {
					invalid_proto = B_TRUE;
				} else {
					p_inf->ri = ri;
				}
			}
		}

past_proto_processing:
		/* validate non-RPC service name */
		if (!isrpc && (cfg->svc_name != NULL)) {
			struct servent	se;
			char		gsbuf[NSS_BUFLEN_SERVICES];
			char		*gsproto = proto;

			if (invalid_proto) {
				/*
				 * Make getservbyname_r do its lookup without a
				 * proto.
				 */
				gsproto = NULL;
			} else if (gsproto != NULL) {
				/*
				 * Since getservbyname & getprotobyname don't
				 * support tcp6, udp6 or sctp6 take off the 6
				 * digit from protocol.
				 */
				if (v6_socket_proto(gsproto))
					gsproto[strlen(gsproto) - 1] = '\0';
			}

			if (getservbyname_r(cfg->svc_name, gsproto, &se, gsbuf,
			    sizeof (gsbuf)) == NULL) {
				if (gsproto != NULL)
					invalid_proto = B_TRUE;
				prop[PT_SVC_NAME_INDEX].ip_error = IVE_INVALID;
			} else if (cfg->istlx && (ti != NULL)) {
				/* LINTED E_BAD_PTR_CAST_ALIGN */
				SS_SETPORT(*(struct sockaddr_storage *)
				    ti->local_addr.buf, se.s_port);
			} else if (!cfg->istlx && (si != NULL)) {
				if ((gsproto != NULL) &&
				    getprotobyname_r(gsproto, &pe, gpbuf,
				    sizeof (gpbuf)) == NULL) {
					invalid_proto = B_TRUE;
				} else {
					si->protocol = pe.p_proto;
				}
				SS_SETPORT(si->local_addr, se.s_port);
			}

		}

		if (p_inf != NULL) {
			p_inf->listen_fd = -1;

			/* add new proto entry to proto_list */
			uu_list_node_init(p_inf, &p_inf->link, proto_info_pool);
			(void) uu_list_insert_after(cfg->proto_list, NULL,
			    p_inf);
		}

		if (nconf != NULL)
			freenetconfigent(nconf);
		if (invalid_proto)
			prop[PT_PROTO_INDEX].ip_error = IVE_INVALID;
	} while (proto != NULL);	/* while just processed a proto */

	/*
	 * Check that the exec string for the start method actually exists and
	 * that the user is either a valid username or uid. Note we don't
	 * mandate the setting of these fields, and don't do any checks
	 * for arg0, hence its absence.
	 */

	if (prop[PT_EXEC_INDEX].ip_error != IVE_UNSET) {
		/* Don't pass any arguments to access() */
		if ((bufp = strdup(
		    prop[PT_EXEC_INDEX].ip_value.iv_string)) == NULL) {
			prop[PT_EXEC_INDEX].ip_error = IVE_INVALID;
		} else {
			if ((cp = strpbrk(bufp, " \t")) != NULL)
				*cp = '\0';

			if ((access(bufp, F_OK) == -1) && (errno == ENOENT))
				prop[PT_EXEC_INDEX].ip_error = IVE_INVALID;
			free(bufp);
		}
	}

	if (prop[PT_USER_INDEX].ip_error != IVE_UNSET) {
		char		pw_buf[NSS_BUFLEN_PASSWD];
		struct passwd	pw;

		if (getpwnam_r(prop[PT_USER_INDEX].ip_value.iv_string, &pw,
		    pw_buf, NSS_BUFLEN_PASSWD) == NULL) {
			errno = 0;
			uidl = strtol(prop[PT_USER_INDEX].ip_value.iv_string,
			    &bufp, 10);
			if ((errno != 0) || (*bufp != '\0') ||
			    (getpwuid_r(uidl, &pw, pw_buf,
			    NSS_BUFLEN_PASSWD) == NULL))
				prop[PT_USER_INDEX].ip_error = IVE_INVALID;
		}
	}

	/*
	 * Iterate through the properties in the array verifying that any
	 * default properties are valid, and setting the return boolean
	 * according to whether any properties were marked invalid.
	 */

	for (i = 0; prop[i].ip_name != NULL; i++) {
		if (prop[i].ip_error == IVE_UNSET)
			continue;

		if (prop[i].ip_default &&
		    !valid_default_prop(prop[i].ip_name, &prop[i].ip_value))
			prop[i].ip_error = IVE_INVALID;

		if (prop[i].ip_error == IVE_INVALID)
			ret = B_FALSE;
	}

	/* pass back the basic_cfg_t if requested and it's a valid config */
	if ((cfgpp != NULL) && ret) {
		*cfgpp = cfg;
	} else {
		destroy_basic_cfg(cfg);
	}

	return (ret);
}

/*
 * validate_default_prop takes the name of an inetd property, and a value
 * for that property.  It returns B_TRUE if the property is valid, and B_FALSE
 * if the proposed value isn't valid for that property.
 */

boolean_t
valid_default_prop(const char *name, const void *value)
{
	int		i;

	for (i = 0; inetd_properties[i].ip_name != NULL; i++) {
		if (strcmp(name, inetd_properties[i].ip_name) != 0)
			continue;
		if (!inetd_properties[i].ip_default)
			return (B_FALSE);

		switch (inetd_properties[i].ip_type) {
		case INET_TYPE_INTEGER:
			if (*((int64_t *)value) >= -1)
				return (B_TRUE);
			else
				return (B_FALSE);
		case INET_TYPE_BOOLEAN:
			if ((*((boolean_t *)value) == B_FALSE) ||
			    (*((boolean_t *)value) == B_TRUE))
				return (B_TRUE);
			else
				return (B_FALSE);
		case INET_TYPE_COUNT:
		case INET_TYPE_STRING_LIST:
		case INET_TYPE_STRING:
			return (B_TRUE);
		}
	}

	return (B_FALSE);
}

/*ARGSUSED*/
scf_error_t
read_prop(scf_handle_t *h, inetd_prop_t *iprop, int index, const char *inst,
    const char *pg_name)
{
	scf_simple_prop_t	*sprop;
	uint8_t			*tmp_bool;
	int64_t			*tmp_int;
	uint64_t		*tmp_cnt;
	char			*tmp_char;

	if ((sprop = scf_simple_prop_get(h, inst, pg_name, iprop->ip_name)) ==
	    NULL)
		return (scf_error());

	switch (iprop->ip_type) {
	case INET_TYPE_STRING:
		if ((tmp_char = scf_simple_prop_next_astring(sprop)) == NULL)
			goto scf_error;
		if ((iprop->ip_value.iv_string = strdup(tmp_char)) == NULL) {
			scf_simple_prop_free(sprop);
			return (SCF_ERROR_NO_MEMORY);
		}
		break;
	case INET_TYPE_STRING_LIST:
		{
			int	j = 0;

			while ((tmp_char =
			    scf_simple_prop_next_astring(sprop)) != NULL) {
				char	**cpp;

				if ((cpp = realloc(
				    iprop->ip_value.iv_string_list,
				    (j + 2) * sizeof (char *))) == NULL) {
					scf_simple_prop_free(sprop);
					return (SCF_ERROR_NO_MEMORY);
				}
				iprop->ip_value.iv_string_list = cpp;
				if ((cpp[j] = strdup(tmp_char)) == NULL) {
					scf_simple_prop_free(sprop);
					return (SCF_ERROR_NO_MEMORY);
				}
				cpp[++j] = NULL;
			}
			if ((j == 0) || (scf_error() != SCF_ERROR_NONE))
				goto scf_error;
		}
		break;
	case INET_TYPE_BOOLEAN:
		if ((tmp_bool = scf_simple_prop_next_boolean(sprop)) == NULL)
			goto scf_error;
		iprop->ip_value.iv_boolean =
		    (*tmp_bool == 0) ? B_FALSE : B_TRUE;
		break;
	case INET_TYPE_COUNT:
		if ((tmp_cnt = scf_simple_prop_next_count(sprop)) == NULL)
			goto scf_error;
		iprop->ip_value.iv_cnt = *tmp_cnt;
		break;
	case INET_TYPE_INTEGER:
		if ((tmp_int = scf_simple_prop_next_integer(sprop)) == NULL)
			goto scf_error;
		iprop->ip_value.iv_int = *tmp_int;
		break;
	default:
		assert(0);
	}

	iprop->ip_error = IVE_VALID;
	scf_simple_prop_free(sprop);
	return (0);

scf_error:
	scf_simple_prop_free(sprop);
	if (scf_error() == SCF_ERROR_NONE)
		return (SCF_ERROR_NOT_FOUND);
	return (scf_error());
}

/*
 * read_props reads either the full set of properties for instance 'instance'
 * (including defaults - pulling them in from inetd where necessary) if
 * 'instance' is non-null, else just the defaults from inetd. The properties
 * are returned in an allocated inetd_prop_t array, which must be freed
 * using free_instance_props(). If an error occurs NULL is returned and 'err'
 * is set to indicate the cause, else a pointer to the read properties is
 * returned.
 */
static inetd_prop_t *
read_props(scf_handle_t *h, const char *instance, size_t *num_elements,
    scf_error_t *err)
{
	inetd_prop_t	*ret = NULL;
	int		i;
	boolean_t	defaults_only = (instance == NULL);

	if ((ret = malloc(sizeof (inetd_properties))) == NULL) {
		*err = SCF_ERROR_NO_MEMORY;
		return (NULL);
	}
	(void) memcpy(ret, &inetd_properties, sizeof (inetd_properties));

	if (defaults_only)
		instance = INETD_INSTANCE_FMRI;
	for (i = 0; ret[i].ip_name != NULL; i++) {
		if (defaults_only && !ret[i].ip_default)
			continue;

		switch (*err = read_prop(h, &ret[i], i, instance,
		    defaults_only ? PG_NAME_SERVICE_DEFAULTS : ret[i].ip_pg)) {
		case 0:
			break;
		case SCF_ERROR_INVALID_ARGUMENT:
			goto failure_cleanup;
		case SCF_ERROR_NOT_FOUND:
			/*
			 * In non-default-only mode where we're reading a
			 * default property, since the property wasn't
			 * found in the instance, try and read inetd's default
			 * value.
			 */
			if (!ret[i].ip_default || defaults_only)
				continue;
			switch (*err = read_prop(h, &ret[i], i,
			    INETD_INSTANCE_FMRI, PG_NAME_SERVICE_DEFAULTS)) {
			case 0:
				ret[i].from_inetd = B_TRUE;
				continue;
			case SCF_ERROR_NOT_FOUND:
				continue;
			default:
				goto failure_cleanup;
			}
		default:
			goto failure_cleanup;
		}
	}

	*num_elements = i;
	return (ret);

failure_cleanup:
	free_instance_props(ret);
	return (NULL);
}

/*
 * Read all properties applicable to 'instance' (including defaults).
 */
inetd_prop_t *
read_instance_props(scf_handle_t *h, const char *instance, size_t *num_elements,
    scf_error_t *err)
{
	return (read_props(h, instance, num_elements, err));
}

/*
 * Read the default properties from inetd's defaults property group.
 */
inetd_prop_t *
read_default_props(scf_handle_t *h, size_t *num_elements, scf_error_t *err)
{
	return (read_props(h, NULL, num_elements, err));
}

void
free_instance_props(inetd_prop_t *prop)
{
	int i;

	if (prop == NULL)
		return;

	for (i = 0; prop[i].ip_name != NULL; i++) {
		if (prop[i].ip_type == INET_TYPE_STRING) {
			free(prop[i].ip_value.iv_string);
		} else if (prop[i].ip_type == INET_TYPE_STRING_LIST) {
			destroy_strings(prop[i].ip_value.iv_string_list);
		}
	}
	free(prop);
}

int
connect_to_inetd(void)
{
	struct sockaddr_un	addr;
	int			fd;

	fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (fd < 0)
		return (-1);

	(void) memset(&addr, 0, sizeof (addr));
	addr.sun_family = AF_UNIX;
	/* CONSTCOND */
	assert(sizeof (INETD_UDS_PATH) <= sizeof (addr.sun_path));
	(void) strlcpy(addr.sun_path, INETD_UDS_PATH,
	    sizeof (addr.sun_path));

	if (connect(fd, (struct sockaddr *)&addr, sizeof (addr)) < 0) {
		(void) close(fd);
		return (-1);
	}

	return (fd);
}

/*
 * refresh_inetd requests that inetd re-read all of the information that it's
 * monitoring.
 */

int
refresh_inetd(void)
{
	uds_request_t   req;
	int		fd;

	if ((fd = connect_to_inetd()) < 0)
		return (-1);

	req = UR_REFRESH_INETD;
	if (send(fd, &req, sizeof (req), 0) < 0) {
		(void) close(fd);
		return (-1);
	}

	(void) close(fd);
	return (0);
}

/*
 * Returns the id of the socket type 'type_str' that can be used in a call
 * to socket(). If an unknown type string is passed returns -1, else the id.
 */

int
get_sock_type_id(const char *type_str)
{
	int	ret;

	if (strcmp(SOCKTYPE_STREAM_STR, type_str) == 0) {
		ret = SOCK_STREAM;
	} else if (strcmp(SOCKTYPE_DGRAM_STR, type_str) == 0) {
		ret = SOCK_DGRAM;
	} else if (strcmp(SOCKTYPE_RAW_STR, type_str) == 0) {
		ret = SOCK_RAW;
	} else if (strcmp(SOCKTYPE_SEQPKT_STR, type_str) == 0) {
		ret = SOCK_SEQPACKET;
	} else {
		ret = -1;
	}
	return (ret);
}

/*
 * Takes either an RPC service name or number in string form as 'svc_name', and
 * returns an integer format program number for the service. If the name isn't
 * recognized as a valid RPC service name or isn't a valid number, -1 is
 * returned, else the services program number.
 */

int
get_rpc_prognum(const char *svc_name)
{
	struct rpcent	rpc;
	char		buf[INETSVC_SVC_BUF_MAX];
	int		pnum;
	char		*endptr;

	if (getrpcbyname_r(svc_name, &rpc, buf, sizeof (buf)) != NULL)
		return (rpc.r_number);

	pnum = strtol(svc_name, &endptr, 0);
	if ((pnum == 0 && errno == EINVAL) ||
	    (pnum == LONG_MAX && errno == ERANGE) ||
	    pnum < 0 || *endptr != '\0') {
		return (-1);
	}

	return (pnum);
}

/*
 * calculate_hash calculates the MD5 message-digest of the file pathname.
 * On success, hash is modified to point to the digest string and 0 is returned.
 * Otherwise, -1 is returned and errno is set to indicate the error.
 * The space for the digest string is obtained using malloc(3C) and should be
 * freed by the caller.
 */
int
calculate_hash(const char *pathname, char **hash)
{
	int fd, i, serrno;
	size_t len;
	ssize_t n;
	char *digest;
	MD5_CTX md5_context;
	unsigned char md5_digest[DIGEST_LEN];
	unsigned char buf[READ_BUFSIZ];

	do {
		fd = open(pathname, O_RDONLY);
	} while (fd == -1 && errno == EINTR);

	if (fd == -1)
		return (-1);

	/* allocate space for a 16-byte MD5 digest as a string of hex digits */
	len = 2 * sizeof (md5_digest) + 1;
	if ((digest = malloc(len)) == NULL) {
		serrno = errno;
		(void) close(fd);
		errno = serrno;
		return (-1);
	}

	MD5Init(&md5_context);

	do {
		if ((n = read(fd, buf, sizeof (buf))) > 0)
			MD5Update(&md5_context, buf, n);
	} while ((n > 0) || (n == -1 && errno == EINTR));

	serrno = errno;
	MD5Final(md5_digest, &md5_context);

	(void) close(fd);

	if (n == -1) {
		errno = serrno;
		return (-1);
	}

	for (i = 0; i < sizeof (md5_digest); i++) {
		(void) snprintf(&digest[2 * i], len - (2 * i), "%02x",
		    md5_digest[i]);
	}
	*hash = digest;
	return (0);
}

/*
 * retrieve_inetd_hash retrieves inetd's configuration file hash from the
 * repository. On success, hash is modified to point to the hash string and
 * SCF_ERROR_NONE is returned. Otherwise, the scf_error value is returned.
 * The space for the hash string is obtained using malloc(3C) and should be
 * freed by the caller.
 */
scf_error_t
retrieve_inetd_hash(char **hash)
{
	scf_simple_prop_t *sp;
	char *hashstr, *s;
	scf_error_t scf_err;

	if ((sp = scf_simple_prop_get(NULL, INETD_INSTANCE_FMRI, HASH_PG,
	    HASH_PROP)) == NULL)
		return (scf_error());

	if ((hashstr = scf_simple_prop_next_astring(sp)) == NULL) {
		scf_err = scf_error();
		scf_simple_prop_free(sp);
		return (scf_err);
	}

	if ((s = strdup(hashstr)) == NULL) {
		scf_simple_prop_free(sp);
		return (SCF_ERROR_NO_MEMORY);
	}
	*hash = s;
	scf_simple_prop_free(sp);
	return (SCF_ERROR_NONE);
}

/*
 * store_inetd_hash stores the string hash in inetd's configuration file hash
 * in the repository. On success, SCF_ERROR_NONE is returned. Otherwise, the
 * scf_error value is returned.
 */
scf_error_t
store_inetd_hash(const char *hash)
{
	int ret;
	scf_error_t rval = SCF_ERROR_NONE;
	scf_handle_t *h;
	scf_propertygroup_t *pg = NULL;
	scf_instance_t *inst = NULL;
	scf_transaction_t *tx = NULL;
	scf_transaction_entry_t *txent = NULL;
	scf_property_t *prop = NULL;
	scf_value_t *val = NULL;

	if ((h = scf_handle_create(SCF_VERSION)) == NULL ||
	    scf_handle_bind(h) == -1)
		goto error;

	if ((pg = scf_pg_create(h)) == NULL ||
	    (inst = scf_instance_create(h)) == NULL ||
	    scf_handle_decode_fmri(h, INETD_INSTANCE_FMRI, NULL, NULL, inst,
	    NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1)
		goto error;

	if (scf_instance_get_pg(inst, HASH_PG, pg) == -1) {
		if (scf_error() != SCF_ERROR_NOT_FOUND ||
		    scf_instance_add_pg(inst, HASH_PG, SCF_GROUP_APPLICATION,
		    0, pg) == -1)
			goto error;
	}

	if ((tx = scf_transaction_create(h)) == NULL ||
	    (txent = scf_entry_create(h)) == NULL ||
	    (prop = scf_property_create(h)) == NULL ||
	    (val = scf_value_create(h)) == NULL)
		goto error;

	do {
		if (scf_transaction_start(tx, pg) == -1)
			goto error;

		if (scf_transaction_property_new(tx, txent, HASH_PROP,
		    SCF_TYPE_ASTRING) == -1 &&
		    scf_transaction_property_change_type(tx, txent, HASH_PROP,
		    SCF_TYPE_ASTRING) == -1)
			goto error;

		if (scf_value_set_astring(val, hash) == -1 ||
		    scf_entry_add_value(txent, val) == -1)
			goto error;

		if ((ret = scf_transaction_commit(tx)) == -1)
			goto error;

		if (ret == 0) {
			scf_transaction_reset(tx);
			if (scf_pg_update(pg) == -1)
				goto error;
		}
	} while (ret == 0);

	goto success;

error:
	rval = scf_error();

success:
	scf_value_destroy(val);
	scf_property_destroy(prop);
	scf_entry_destroy(txent);
	scf_transaction_destroy(tx);
	scf_instance_destroy(inst);
	scf_pg_destroy(pg);
	scf_handle_destroy(h);
	return (rval);
}

/*
 * This is a wrapper function for inet_ntop(). In case the af is AF_INET6
 * and the address pointed by src is a IPv4-mapped IPv6 address, it returns
 * a printable IPv4 address, not an IPv4-mapped IPv6 address. In other cases it
 * behaves just like inet_ntop().
 */
const char *
inet_ntop_native(int af, const void *addr, char *dst, size_t size)
{
	struct in_addr	v4addr;

	if ((af == AF_INET6) && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)addr)) {
		IN6_V4MAPPED_TO_INADDR((struct in6_addr *)addr, &v4addr);
		return (inet_ntop(AF_INET, &v4addr, dst, size));
	} else {
		return (inet_ntop(af, addr, dst, size));
	}
}

/*
 * inetd specific setproctitle. It sets the title so that it contains
 * 'svc_name' followed by, if obtainable, the address of the remote end of
 * socket 's'.
 * NOTE: The argv manipulation in this function should be replaced when a
 * common version of setproctitle is made available.
 */
void
setproctitle(const char *svc_name, int s, char *argv[])
{
	socklen_t		size;
	struct sockaddr_storage	ss;
	char			abuf[INET6_ADDRSTRLEN];

	static char		buf[80];

	size = (socklen_t)sizeof (ss);
	if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) {
		(void) snprintf(buf, sizeof (buf), "-%s [%s]", svc_name,
		    inet_ntop_native(ss.ss_family, (ss.ss_family == AF_INET6 ?
		    (void *)&((struct sockaddr_in6 *)(&ss))->sin6_addr :
		    (void *)&((struct sockaddr_in *)(&ss))->sin_addr), abuf,
		    sizeof (abuf)));
	} else {
		(void) snprintf(buf, sizeof (buf), "-%s", svc_name);
	}

	/* we set argv[0] to point at our static storage. */
	argv[0] = buf;
	argv[1] = NULL;
}

static boolean_t
inetd_builtin_srcport(in_port_t p)
{
	p = ntohs(p);

	if ((p == IPPORT_ECHO) ||
	    (p == IPPORT_DISCARD) ||
	    (p == IPPORT_DAYTIME) ||
	    (p == IPPORT_CHARGEN) ||
	    (p == IPPORT_TIMESERVER)) {
		return (B_TRUE);
	} else {
		return (B_FALSE);
	}
}

/* ARGSUSED0 */
static void
alarm_handler(int sig)
{
	exit(0);
}

/*
 * This function is a datagram service template. It acts as a datagram wait
 * type server, waiting for datagrams to come in, and when they do passing
 * their contents, as-well as the socket they came in on and the remote
 * address, in a call to the callback function 'cb'. If no datagrams are
 * received for DG_INACTIVITY_TIMEOUT seconds the function exits with code 0.
 */
void
dg_template(void (*cb)(int, const struct sockaddr *, int, const void *, size_t),
    int s, void *buf, size_t buflen)
{
	struct sockaddr_storage	sa;
	socklen_t		sa_size;
	ssize_t			i;
	char			tmp[BUFSIZ];

	(void) sigset(SIGALRM, alarm_handler);

	if (buf == NULL) {
		buf = tmp;
		buflen = sizeof (tmp);
	}
	for (;;) {
		(void) alarm(DG_INACTIVITY_TIMEOUT);
		sa_size = sizeof (sa);
		if ((i = recvfrom(s, buf, buflen, 0, (struct sockaddr *)&sa,
		    &sa_size)) < 0) {
			continue;
		} else if (inetd_builtin_srcport(
		    ((struct sockaddr_in *)(&sa))->sin_port)) {
			/* denial-of-service attack possibility - ignore it */
			syslog(LOG_WARNING,
	"Incoming datagram from internal inetd service received; ignoring.");
			continue;
		}
		(void) alarm(0);

		cb(s, (struct sockaddr *)&sa, sa_size, buf, i);
	}
}

/*
 * An extension of write() or sendto() that keeps trying until either the full
 * request has completed or a non-EINTR error occurs. If 'to' is set to a
 * non-NULL value, sendto() is extended, else write(). Returns 0 on success
 * else -1.
 */
int
safe_sendto_write(int fd, const void *buf, size_t sz, int flags,
    const struct sockaddr *to, int tolen)
{

	size_t		cnt = 0;
	ssize_t		ret;
	const char	*cp = buf;

	do {
		if (to == NULL) {
			ret = write(fd, cp + cnt, sz - cnt);
		} else {
			ret = sendto(fd, cp + cnt, sz - cnt, flags, to, tolen);
		}

		if (ret > 0)
			cnt += ret;
	} while ((cnt != sz) && (errno == EINTR));

	return ((cnt == sz) ? 0 : -1);
}

int
safe_sendto(int fd, const void *buf, size_t sz, int flags,
    const struct sockaddr *to, int tolen)
{
	return (safe_sendto_write(fd, buf, sz, flags, to, tolen));
}

int
safe_write(int fd, const void *buf, size_t sz)
{
	return (safe_sendto_write(fd, buf, sz, 0, NULL, 0));
}

/*
 * Free up the memory occupied by string array 'strs'.
 */
void
destroy_strings(char **strs)
{
	int i = 0;

	if (strs != NULL) {
		while (strs[i] != NULL)
			free(strs[i++]);
		free(strs);
	}
}

/*
 * Parse the proto list string into an allocated array of proto strings,
 * returning a pointer to this array. If one of the protos is too big
 * errno is set to E2BIG and NULL is returned; if memory allocation failure
 * occurs errno is set to ENOMEM and NULL is returned; else on success
 * a pointer the string array is returned.
 */
char **
get_protos(const char *pstr)
{
	char	*cp;
	int	i = 0;
	char	**ret = NULL;
	size_t	max_proto_len = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
	char	*str;

	/* copy the parameter as strtok modifies its parameters */
	if ((str = strdup(pstr)) == NULL)
		goto malloc_failure;

	for (cp = strtok(str, PROTO_DELIMITERS); cp != NULL;
	    cp = strtok(NULL, PROTO_DELIMITERS)) {
		char **cpp;

		if (strlen(cp) >= max_proto_len) {
			destroy_strings(ret);
			free(str);
			errno = E2BIG;
			return (NULL);
		}
		if ((cpp = realloc(ret, (i + 2) * sizeof (char *))) == NULL)
			goto malloc_failure;
		ret = cpp;
		if ((cpp[i] = strdup(cp)) == NULL)
			goto malloc_failure;
		cpp[++i] = NULL;
	}

	free(str);
	return (ret);

malloc_failure:
	destroy_strings(ret);
	free(str);
	errno = ENOMEM;
	return (NULL);
}

/*
 * Returns an allocated string array of netids corresponding with 'proto'. The
 * function first tries to interpret 'proto' as a nettype to get its netids.
 * If this fails it tries to interpret it as a netid. If 'proto' is neither
 * a nettype or a netid or a memory allocation failures occurs NULL is
 * returned, else a pointer to an array of netids associated with 'proto' is
 * returned.
 */
char **
get_netids(char *proto)
{
	void			*handle;
	struct netconfig	*nconf;
	char			**netids = NULL;
	char			**cpp;
	int			i = 0;

	if (strcmp(proto, "*") == 0)
		proto = "visible";

	if ((handle = __rpc_setconf(proto)) != NULL) {
		/* expand nettype */
		while ((nconf = __rpc_getconf(handle)) != NULL) {
			if ((cpp = realloc(netids,
			    (i + 2) * sizeof (char *))) == NULL)
				goto failure_cleanup;
			netids = cpp;
			if ((cpp[i] = strdup(nconf->nc_netid)) == NULL)
				goto failure_cleanup;
			cpp[++i] = NULL;
		}
		__rpc_endconf(handle);
	} else {
		if ((netids = malloc(2 * sizeof (char *))) == NULL)
			return (NULL);
		if ((netids[0] = strdup(proto)) == NULL) {
			free(netids);
			return (NULL);
		}
		netids[1] = NULL;
	}

	return (netids);

failure_cleanup:
	destroy_strings(netids);
	__rpc_endconf(handle);
	return (NULL);
}