summaryrefslogtreecommitdiff
path: root/usr/src/cmd/dcs/sparc/sun4u/dcs.c
blob: a7147d262f27bdb9b4f3cdf92e1469bf82719cb8 (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
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
/*
 * 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 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * This is the main file for the Domain Configuration Server (DCS).
 *
 * The DCS is a server that runs on a domain and communicates with
 * a Domain Configuration Agent (DCA) running on a remote host. The
 * DCA initiates DR requests that the DCS performs by calling the
 * appropriate libcfgadm(3LIB) function.
 *
 * This file contains functions that receive and process the messages
 * received from the DCA. It also handles the initialization of the
 * server and is responsible for starting a concurrent session to
 * handle each DR request.
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <syslog.h>
#include <assert.h>
#include <signal.h>
#include <netdb.h>
#include <config_admin.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <strings.h>

#include "dcs.h"
#include "remote_cfg.h"
#include "rdr_param_types.h"
#include "rdr_messages.h"
#include "rsrc_info.h"


typedef struct {
	ushort_t	major;
	ushort_t	minor;
} dcs_ver_t;


/* initialization functions */
static int init_server(struct pollfd *pfd, uint8_t ah_auth_alg,
    uint8_t esp_encr_alg, uint8_t esp_auth_alg);
static void init_signals(void);

/* message processing functions */
static int invalid_msg(rdr_msg_hdr_t *hdr);

/* message handling functions */
static int dcs_ses_req(rdr_msg_hdr_t *hdr, cfga_params_t *param);
static int dcs_ses_estbl(rdr_msg_hdr_t *hdr, cfga_params_t *param);
static int dcs_ses_end(rdr_msg_hdr_t *hdr, cfga_params_t *param);
static int dcs_change_state(rdr_msg_hdr_t *hdr, cfga_params_t *param);
static int dcs_private_func(rdr_msg_hdr_t *hdr, cfga_params_t *param);
static int dcs_test(rdr_msg_hdr_t *hdr, cfga_params_t *param);
static int dcs_list_ext(rdr_msg_hdr_t *hdr, cfga_params_t *param);
static int dcs_help(rdr_msg_hdr_t *hdr, cfga_params_t *param);
static int dcs_ap_id_cmp(rdr_msg_hdr_t *hdr, cfga_params_t *param);
static int dcs_abort_cmd(rdr_msg_hdr_t *hdr, cfga_params_t *param);
static int dcs_rsrc_info(rdr_msg_hdr_t *hdr, cfga_params_t *param);
static int dcs_unknown_op(rdr_msg_hdr_t *hdr, cfga_params_t *param);

/* local callback functions */
static int dcs_confirm_callback(void *appdata_ptr, const char *message);
static int dcs_message_callback(void *appdata_ptr, const char *message);

/* utility functions */
static dcs_ver_t resolve_version(ushort_t req_major, ushort_t req_minor);
static void filter_list_data(int perm, int *nlistp, cfga_list_data_t *linfo);
static rdr_list_t *generate_sort_order(cfga_list_data_t *listp, int nlist);
static int ldata_compare(const void *ap1, const void *ap2);
static int invalid_msg(rdr_msg_hdr_t *hdr);
static char *basename(char *path);
static boolean_t is_socket(int fd);
static uint8_t dcs_get_alg(dcs_alg_t *algs, char *arg, dcs_err_code *error);
static void dcs_log_bad_alg(char optopt, char *optarg);
static boolean_t dcs_global_policy(void);


/*
 * Lookup table for handling different message types. This
 * assumes the ordering of rdr_msg_opcode_t in remote_cfg.h.
 * If this enum changes, the lookup table must be updated.
 *
 * The lookup table handles all _known_ opcodes >= 0. Unsupported
 * opcodes, or opcodes that should not be received by the
 * dispatcher are handled by the dcs_unknown_op() function.
 */
int (*dcs_cmd[])(rdr_msg_hdr_t *, cfga_params_t *) = {
	dcs_unknown_op,		/* 0 is an invalid opcode	*/
	dcs_ses_req,		/* RDR_SES_REQ			*/
	dcs_ses_estbl,		/* RDR_SES_ESTBL		*/
	dcs_ses_end,		/* RDR_SES_END			*/
	dcs_change_state,	/* RDR_CONF_CHANGE_STATE	*/
	dcs_private_func,	/* RDR_CONF_PRIVATE_FUNC	*/
	dcs_test,		/* RDR_CONF_TEST		*/
	dcs_list_ext,		/* RDR_CONF_LIST_EXT		*/
	dcs_help,		/* RDR_CONF_HELP		*/
	dcs_ap_id_cmp,		/* RDR_CONF_AP_ID_CMP		*/
	dcs_abort_cmd,		/* RDR_CONF_ABORT_CMD		*/
	dcs_unknown_op,		/* RDR_CONF_CONFIRM_CALLBACK	*/
	dcs_unknown_op,		/* RDR_CONF_MSG_CALLBACK	*/
	dcs_rsrc_info		/* RDR_RSRC_INFO		*/
};


/*
 * ver_supp[] is an array of the supported versions for the network
 * transport protocol used by the DCA and DCS. Each item in the array
 * is a pair: { major_version, minor_version }.
 *
 * The order of the array is significant. The first element should be
 * the highest supported version and all successive elements should be
 * strictly decreasing.
 */
dcs_ver_t ver_supp[] = {
	{ 1, 1 },
	{ 1, 0 }
};

#define	DCS_CURR_VER		ver_supp[0]


/*
 * Global Data
 */
char	*cmdname = NULL;		 /* the name of the executable	    */
ulong_t	dcs_debug = 0;			 /* control the amount of debugging */
int	standalone = 0;			 /* control standalone mode	    */
boolean_t inetd = B_FALSE;		 /* control daemon mode		    */
ulong_t	max_sessions = DCS_MAX_SESSIONS; /* control maximum active sessions */
int	dcsfd = STDIN_FILENO;		 /* fd for the DCS reserved port    */
int	use_libdscp = 0;		 /* control use of libdscp */
sa_family_t use_family = AF_INET6;	/* control use of AF_INET/AF_INET6 */

/*
 * Array of acceptable -a, -e and -u arguments.
 */
static dcs_alg_t auth_algs_array[] = {
	{ "none",	SADB_AALG_NONE },	/* -a none or -u none */
	{ "md5",	SADB_AALG_MD5HMAC },	/* -a md5  or -u md5 */
	{ "sha1",	SADB_AALG_SHA1HMAC },	/* -a sha1 or -u sha1 */
	{ NULL,		0x0 }
}, esp_algs_array[] = {
	{ "none",	SADB_EALG_NONE },	/* -e none */
	{ "des",	SADB_EALG_DESCBC },	/* -e des  */
	{ "3des",	SADB_EALG_3DESCBC },	/* -e 3des */
	{ NULL,		0x0 }
};


/*
 * main:
 *
 * Initialize the DCS and then enter an infinite loop. This loop waits
 * for connection requests to come and then establishes a connection.
 * It dispatches the connection to be handled in a concurrent session.
 */
int
main(int argc, char **argv)
{
	int		opt;
	struct timeval	tv;
	struct pollfd	dcs_rcv;
	int		newfd;
	uint8_t		ah_auth_alg	= SADB_AALG_NONE;
	uint8_t		esp_encr_alg	= SADB_EALG_NONE;
	uint8_t		esp_auth_alg	= SADB_AALG_NONE;
	dcs_err_code	alg_ec		= DCS_NO_ERR;


	/* initialize globals */
	dcs_debug = DBG_NONE;
	cmdname = basename(argv[0]);

	/* open log file with unique prefix */
	openlog(cmdname, LOG_CONS | LOG_NDELAY, LOG_DAEMON);

	/*
	 * Process command line args
	 */
	opterr = 0;	/* disable getopt error messages */
	while ((opt = getopt(argc, argv, OPT_STR)) != EOF) {

		switch (opt) {

		case 'd': {
			int	usr_debug;
			char	*err_str;

			usr_debug = strtol(optarg, &err_str, 0);

			/*
			 * The err_str parameter will be an
			 * empty string if successful.
			 */
			if (*err_str != '\0') {
				dcs_log_msg(LOG_ERR, DCS_BAD_OPT_ARG, optopt,
				    optarg, "exiting");
				(void) rdr_reject(dcsfd);
				exit(1);
			}

			dcs_debug = usr_debug;
			break;
		}

		case 'S':
			standalone++;
			break;

		case 's': {
			int	usr_ses;
			char	*err_str;

			usr_ses = strtol(optarg, &err_str, 0);

			if (usr_ses >= 1) {
				max_sessions = usr_ses;
			} else {
				char	behavior_str[MAX_MSG_LEN];

				snprintf(behavior_str, MAX_MSG_LEN,
				    "using default value (%d)", max_sessions);

				dcs_log_msg(LOG_NOTICE, DCS_BAD_OPT_ARG, optopt,
				    optarg, behavior_str);
			}

			break;
		}

		case 'a':
		case 'u':
			if (opt == 'a')
				ah_auth_alg = dcs_get_alg(auth_algs_array,
				    optarg, &alg_ec);
			else /* opt == 'u' */
				esp_auth_alg = dcs_get_alg(auth_algs_array,
				    optarg, &alg_ec);

			if (alg_ec == DCS_BAD_OPT_ARG) {
				dcs_log_bad_alg(optopt, optarg);
				(void) rdr_reject(dcsfd);
				exit(1);
			}

			break;

		case 'e':
			esp_encr_alg = dcs_get_alg(esp_algs_array, optarg,
			    &alg_ec);

			if (alg_ec == DCS_BAD_OPT_ARG) {
				dcs_log_bad_alg(optopt, optarg);
				(void) rdr_reject(dcsfd);
				exit(1);
			}

			break;

		case 'l':
			use_libdscp = 1;
			use_family = AF_INET;
			break;

		default:
			if (optopt == 'a' || optopt == 'e' || optopt == 'u')
				dcs_log_bad_alg(optopt, optarg);
			else
				dcs_log_msg(LOG_ERR, DCS_BAD_OPT, optopt);
			(void) rdr_reject(dcsfd);
			exit(1);

			/* NOTREACHED */
			break;
		}
	}

	/*
	 * In the future if inetd supports per-socket IPsec dcs can be run
	 * under inetd.
	 * Daemonize if we were not started by inetd unless running standalone.
	 */
	inetd = is_socket(STDIN_FILENO);
	if (inetd == B_FALSE && standalone == 0) {
		closefrom(0);
		(void) chdir("/");
		(void) umask(0);

		if (fork() != 0)
			exit(0);

		(void) setsid();

		/* open log again after all files were closed */
		openlog(cmdname, LOG_CONS | LOG_NDELAY, LOG_DAEMON);
	}

	DCS_DBG(DBG_ALL, "initializing %s...", cmdname);

	init_signals();

	/* must be root */
	if (geteuid() != 0) {
		dcs_log_msg(LOG_ERR, DCS_NO_PRIV);
		(void) rdr_reject(dcsfd);
		exit(1);
	}

	/*
	 * Seed the random number generator for
	 * generating random session identifiers.
	 */
	gettimeofday(&tv, NULL);
	srand48(tv.tv_usec);

	/* initialize our transport endpoint */
	if (init_server(&dcs_rcv, ah_auth_alg, esp_encr_alg, esp_auth_alg) ==
	    -1) {
		dcs_log_msg(LOG_ERR, DCS_INIT_ERR);
		(void) rdr_reject(dcsfd);
		exit(1);
	}


	DCS_DBG(DBG_ALL, "%s initialized, debug level = 0x%X, "
	    "max sessions = %d", cmdname, dcs_debug, max_sessions);

	/*
	 * Main service loop
	 */
	for (;;) {

		/* wait for a connection request */
		if (ses_poll(&dcs_rcv, 1, BLOCKFOREVER) == -1) {
			if (errno != EINTR) {
				dcs_log_msg(LOG_ERR, DCS_INT_ERR, "poll",
				    strerror(errno));
			}
			continue;
		}

		/* attempt to connect */
		newfd = rdr_connect_srv(dcs_rcv.fd);

		if ((newfd == RDR_ERROR) || (newfd == RDR_NET_ERR)) {
			dcs_log_msg(LOG_ERR, DCS_CONNECT_ERR);
			continue;
		}


		/* process the session concurrently */
		if (ses_start(newfd) == -1) {
			dcs_log_msg(LOG_ERR, DCS_SES_HAND_ERR);
			(void) rdr_close(newfd);
			break;
		}
	}

	close(dcs_rcv.fd);
	return (1);
}


/*
 * dcs_get_alg:
 *
 * Returns the ID of the first algorithm found in the 'algs' array
 * with a name matching 'arg'. If there is no matching algorithm,
 * 'error' is set to DCS_BAD_OPT_ARG, otherwise it is set to DCS_NO_ERR.
 * The 'algs' array must be terminated by an entry containing a NULL
 * 'arg_name' field. The 'error' argument must be a valid pointer.
 */
static uint8_t
dcs_get_alg(dcs_alg_t *algs, char *arg, dcs_err_code *error)
{
	dcs_alg_t *alg;

	*error = DCS_NO_ERR;

	for (alg = algs; alg->arg_name != NULL && arg != NULL; alg++) {
		if (strncmp(alg->arg_name, arg, strlen(alg->arg_name) + 1)
		    == 0) {
			return (alg->alg_id);
		}
	}

	*error = DCS_BAD_OPT_ARG;

	return (0);
}


/*
 * dcs_log_bad_alg:
 *
 * Logs an appropriate message when an invalid command line argument
 * was provided.  'optarg' is the invalid argument string for the
 * command line option 'optopt', where 'optopt' = 'a' for the '-a'
 * option. A NULL 'optarg' indicates the required option was not
 * provided.
 */
static void
dcs_log_bad_alg(char optopt, char *optarg)
{
	if (optarg == NULL) {
		dcs_log_msg(LOG_ERR, DCS_BAD_OPT_ARG, optopt,
		    "empty string", "an argument is required, exiting");
	} else {
		dcs_log_msg(LOG_ERR, DCS_BAD_OPT_ARG, optopt,
		    optarg, "exiting");
	}
}


/*
 * init_server:
 *
 * Perform all the operations that are required to initialize the
 * transport endpoint used by the DCS. After this routine succeeds,
 * the DCS is ready to accept session requests on its well known
 * port.
 */
static int
init_server(struct pollfd *pfd, uint8_t ah_auth_alg, uint8_t esp_encr_alg,
	uint8_t esp_auth_alg)
{
	struct servent		*se;
	struct sockaddr_storage	ss;
	struct sockaddr_in	*sin;
	struct sockaddr_in6	*sin6;
	struct linger		ling;
	ipsec_req_t		ipsec_req;
	int			req_port;
	int			act_port;
	int			init_status;
	int			num_sock_opts;
	int			sock_opts[] = { SO_REUSEADDR };


	assert(pfd);
	pfd->fd = dcsfd;
	pfd->events = POLLIN | POLLPRI;
	pfd->revents = 0;


	/*
	 * In standalone mode, we have to initialize the transport
	 * endpoint for our reserved port. In daemon mode, inetd
	 * starts the DCS and hands off STDIN_FILENO connected to
	 * our reserved port.
	 */

	if (inetd == B_FALSE || standalone) {
		/* in standalone mode, init fd for reserved port */
		if ((dcsfd = rdr_open(use_family)) == -1) {
			DCS_DBG(DBG_ALL, "rdr_open failed");
			return (-1);
		}
		pfd->fd = dcsfd;

		/*
		 * Enable per-socket IPsec if the user specified an
		 * AH or ESP algorithm to use and global policy is not in
		 * effect.
		 */
		if (!dcs_global_policy() &&
		    (ah_auth_alg != SADB_AALG_NONE ||
		    esp_encr_alg != SADB_EALG_NONE ||
		    esp_auth_alg != SADB_AALG_NONE)) {
			int err;

			bzero(&ipsec_req, sizeof (ipsec_req));

			/* Hardcoded values */
			ipsec_req.ipsr_self_encap_req	= SELF_ENCAP_REQ;
			/* User defined */
			ipsec_req.ipsr_auth_alg		= ah_auth_alg;
			ipsec_req.ipsr_esp_alg		= esp_encr_alg;
			if (ah_auth_alg != SADB_AALG_NONE)
				ipsec_req.ipsr_ah_req = AH_REQ;
			if (esp_encr_alg != SADB_EALG_NONE ||
			    esp_auth_alg != SADB_AALG_NONE) {
				ipsec_req.ipsr_esp_req		= ESP_REQ;
				ipsec_req.ipsr_esp_auth_alg	= esp_auth_alg;
			}

			err = rdr_setsockopt(pfd->fd, IPPROTO_IPV6,
			    IPV6_SEC_OPT, (void *)&ipsec_req,
			    sizeof (ipsec_req));

			if (err != RDR_OK) {
				DCS_DBG(DBG_ALL, "rdr_setsockopt failed");
				return (-1);
			}
		}
	}

	/*
	 * Look up our service to get the reserved port number
	 */
	if ((se = getservbyname(DCS_SERVICE, "tcp")) == NULL) {
		dcs_log_msg(LOG_NOTICE, DCS_NO_SERV, DCS_SERVICE);

		/* use the known port if service wasn't found */
		req_port = SUN_DR_PORT;
	} else {
		req_port = se->s_port;
	}

	(void) memset(&ss, 0, sizeof (ss));
	if (use_family == AF_INET) {
		/* initialize our local address */
		sin = (struct sockaddr_in *)&ss;
		sin->sin_family = AF_INET;
		sin->sin_port = htons(req_port);
		sin->sin_addr.s_addr = htonl(INADDR_ANY);
	} else {
		/* initialize our local address */
		sin6 = (struct sockaddr_in6 *)&ss;
		sin6->sin6_family = AF_INET6;
		sin6->sin6_port = htons(req_port);
		sin6->sin6_addr = in6addr_any;
	}

	num_sock_opts = sizeof (sock_opts) / sizeof (*sock_opts);

	init_status = rdr_init(pfd->fd, (struct sockaddr *)&ss,
	    sock_opts, num_sock_opts, DCS_BACKLOG);

	if (init_status != RDR_OK) {
		return (-1);
	}

	/*
	 * Set the SO_LINGER socket option so that TCP aborts the connection
	 * when the socket is closed.  This avoids encountering a TIME_WAIT
	 * state if the daemon ever crashes and is instantly restarted.
	 */
	ling.l_onoff = 1;
	ling.l_linger = 0;
	if (setsockopt(pfd->fd, SOL_SOCKET, SO_LINGER, &ling, sizeof (ling))) {
		return (-1);
	}

	switch (ss.ss_family) {
	case AF_INET:
		DCS_DBG(DBG_ALL, "using AF_INET socket");
		sin = (struct sockaddr_in *)&ss;
		act_port = ntohs(sin->sin_port);
		break;
	case AF_INET6:
		DCS_DBG(DBG_ALL, "using AF_INET6 socket");
		/* sin6 already set correctly */
		act_port = ntohs(sin6->sin6_port);
		break;
	default:
		DCS_DBG(DBG_ALL, "unknown socket type");
		return (-1);
	}

	/* check that we got the requested port */
	if (req_port != act_port) {
		dcs_log_msg(LOG_ERR, DCS_NO_PORT, req_port);
		return (-1);
	}

	return (0);
}


/*
 * init_signals:
 *
 * Initialize signals for the current session. All signals will be
 * blocked with two possible exceptions. SIGINT is not blocked in
 * standalone mode, and ses_init_signals() is called to selectively
 * unblock any signals required to handle concurrent sessions.
 */
static void
init_signals(void)
{
	sigset_t		mask;


	/* block all signals */
	sigfillset(&mask);

	/* in standalone, allow user to abort */
	if (standalone) {
		sigdelset(&mask, SIGINT);
	}

	ses_init_signals(&mask);

	(void) sigprocmask(SIG_BLOCK, &mask, NULL);
}


/*
 * dcs_dispatch_message:
 *
 * This function dispatches a message to the correct function. The
 * correct handler is determined by the opcode field of the message
 * header.
 */
int
dcs_dispatch_message(rdr_msg_hdr_t *hdr, cfga_params_t *params)
{
	session_t	*sp;


	assert(hdr);
	assert(params);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	/* check the message */
	if (invalid_msg(hdr)) {
		dcs_log_msg(LOG_ERR, DCS_MSG_INVAL);
		ses_close(DCS_MSG_INVAL);
		return (-1);
	}

	/* save the current message */
	sp->curr_msg.hdr = hdr;
	sp->curr_msg.params = params;

	/*
	 * hdr->message_opcode is unsigned so don't need
	 * to check for values less than zero
	 */
	if (hdr->message_opcode >= RDR_NUM_OPS) {
		dcs_unknown_op(hdr, params);
		ses_close(DCS_MSG_INVAL);
		return (-1);
	}

	PRINT_MSG_DBG(DCS_RECEIVE, hdr);

	/* dispatch the message */
	if ((*dcs_cmd[hdr->message_opcode])(hdr, params) == -1) {
		dcs_log_msg(LOG_ERR, DCS_OP_FAILED);
		ses_close(DCS_ERROR);
		return (-1);
	}

	return (0);
}


/*
 * init_msg:
 *
 * Initialize the message header with information from the current
 * session. Fields not set directly are initialized to zero.
 */
void
init_msg(rdr_msg_hdr_t *hdr)
{
	session_t	*sp;


	assert(hdr);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return;
	}

	(void) memset(hdr, 0, sizeof (rdr_msg_hdr_t));

	/* set the session information */
	hdr->random_req = sp->random_req;
	hdr->random_resp = sp->random_resp;

	/* set the version being used */
	hdr->major_version = sp->major_version;
	hdr->minor_version = sp->minor_version;
}


/*
 * invalid_msg:
 *
 * Check if the message is valid for the current session. This
 * is accomplished by checking various information in the header
 * against the information for the current session.
 */
static int
invalid_msg(rdr_msg_hdr_t *hdr)
{
	session_t	*sp;


	assert(hdr);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	/*
	 * Only perform the following checks if the message
	 * is not a session request. The information to check
	 * will not be set at the time a session request is
	 * received.
	 */
	if (hdr->message_opcode != RDR_SES_REQ) {

		/* check major and minor version */
		if ((sp->major_version != hdr->major_version) ||
		    (sp->minor_version != hdr->minor_version)) {
			DCS_DBG(DBG_MSG, "unsupported version %d.%d",
			    hdr->major_version, hdr->minor_version);
			return (-1);
		}

		/* check session identifiers */
		if ((sp->random_req != hdr->random_req) ||
		    (sp->random_resp != hdr->random_resp)) {
			DCS_DBG(DBG_MSG, "invalid session identifiers: "
			    "<%d, %d>", hdr->random_req, hdr->random_resp);
			return (-1);
		}
	}

	return (0);
}


/*
 * dcs_ses_req:
 *
 * Handle a session request message (RDR_SES_REQ).
 */
static int
dcs_ses_req(rdr_msg_hdr_t *hdr, cfga_params_t *param)
{
	session_t	*sp;
	rdr_msg_hdr_t	reply_hdr;
	cfga_params_t	reply_param;
	dcs_ver_t	act_ver;
	int		snd_status;
	static char 	*op_name = "session request";


	assert(hdr);
	assert(param);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	/* make sure that a session hasn't been requested yet */
	if (sp->state != DCS_CONNECTED) {
		dcs_log_msg(LOG_ERR, DCS_SES_SEQ_INVAL);
		ses_close(DCS_SES_SEQ_INVAL);
		return (-1);
	}

	ses_setlocale(param->req.locale_str);

	/* get the best matching version supported */
	act_ver = resolve_version(hdr->major_version, hdr->minor_version);

	/* initialize session information */
	sp->random_req = hdr->random_req;
	sp->major_version = act_ver.major;
	sp->minor_version = act_ver.minor;

	/* prepare header information */
	init_msg(&reply_hdr);
	reply_hdr.message_opcode = RDR_SES_REQ;
	reply_hdr.data_type = RDR_REPLY;
	reply_hdr.status = DCS_OK;

	/* prepare session request specific data */
	(void) memset(&reply_param, 0, sizeof (cfga_params_t));
	reply_param.req.session_id = sp->id;

	PRINT_MSG_DBG(DCS_SEND, &reply_hdr);

	/* send the message */
	snd_status = rdr_snd_msg(sp->fd, &reply_hdr, &reply_param,
	    DCS_SND_TIMEOUT);

	if (snd_status == RDR_ABORTED) {
		abort_handler();
	}

	if (snd_status != RDR_OK) {
		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
		return (-1);
	}

	sp->state = DCS_SES_REQ;
	return (0);
}


/*
 * dcs_ses_estbl:
 *
 * Handle a session establishment message (RDR_SES_ESTBL).
 */
/* ARGSUSED */
static int
dcs_ses_estbl(rdr_msg_hdr_t *hdr, cfga_params_t *param)
{
	session_t	*sp;
	dcs_ver_t	act_ver;


	assert(hdr);
	assert(param);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	/*
	 * Make sure that a session has not been
	 * established yet, and that a session
	 * request has already been processed.
	 */
	if (sp->state != DCS_SES_REQ) {
		dcs_log_msg(LOG_ERR, DCS_SES_SEQ_INVAL);
		ses_close(DCS_SES_SEQ_INVAL);
		return (-1);
	}

	/* get the best matching version supported */
	act_ver = resolve_version(hdr->major_version, hdr->minor_version);

	if ((act_ver.major != hdr->major_version) ||
	    (act_ver.minor != hdr->minor_version)) {

		/* end the session because protocol not supported */
		dcs_log_msg(LOG_ERR, DCS_VER_INVAL, hdr->major_version,
		    hdr->minor_version);
		ses_close(DCS_VER_INVAL);
		return (-1);
	}

	DCS_DBG(DBG_SES, "Session Established");
	sp->state = DCS_SES_ESTBL;

	return (0);
}


/*
 * dcs_ses_end:
 *
 * Handle a session end message (RDR_SES_END).
 */
static int
dcs_ses_end(rdr_msg_hdr_t *hdr, cfga_params_t *param)
{
	session_t	*sp;
	rdr_msg_hdr_t	reply_hdr;
	cfga_params_t	reply_param;
	int		snd_status;
	static char	*op_name = "session end";


	assert(hdr);
	assert(param);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	/*
	 * Session end is valid from any state. However, only
	 * send back a reply if the error code is zero. A non-zero
	 * error code indicates that the session is being terminated
	 * under an error condition, and no acknowledgement is
	 * required.
	 */
	if (param->end.error_code == 0) {

		/* prepare header information */
		init_msg(&reply_hdr);
		reply_hdr.message_opcode = RDR_SES_END;
		reply_hdr.data_type = RDR_REPLY;
		reply_hdr.status = DCS_OK;

		/* return empty data - no information needed in reply */
		(void) memset(&reply_param, 0, sizeof (cfga_params_t));

		PRINT_MSG_DBG(DCS_SEND, &reply_hdr);

		snd_status = rdr_snd_msg(sp->fd, &reply_hdr, &reply_param,
		    DCS_SND_TIMEOUT);

		if (snd_status == RDR_ABORTED) {
			abort_handler();
		}

		if (snd_status != RDR_OK) {
			dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
		}
	}

	sp->state = DCS_SES_END;

	return ((snd_status != RDR_OK) ? -1 : 0);
}


/*
 * dcs_change_state:
 *
 * Handle a change state request message (RDR_CONF_CHANGE_STATE).
 */
static int
dcs_change_state(rdr_msg_hdr_t *hdr, cfga_params_t *param)
{
	session_t		*sp;
	rdr_msg_hdr_t 		reply_hdr;
	change_state_params_t	*op_data;
	struct cfga_confirm 	local_conf_cb;
	struct cfga_msg		local_msg_cb;
	int			cfga_status = 0;
	int			snd_status;
	char			*err_str;
	unsigned int		curr_attempt;
	unsigned int		num_attempts;
	char			retry_msg[MAX_MSG_LEN];
	static char		*op_name = "config_change_state";


	assert(hdr);
	assert(param);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	op_data = &param->change;

	/* make sure we have a session established */
	if (sp->state != DCS_SES_ESTBL) {
		dcs_log_msg(LOG_ERR, DCS_NO_SES_ESTBL, op_name);
		ses_close(DCS_NO_SES_ERR);
		return (-1);
	}

	/* initialize local confirm callback */
	local_conf_cb.confirm = dcs_confirm_callback;
	local_conf_cb.appdata_ptr = (void *)op_data->confp;

	/* initialize local message callback */
	local_msg_cb.message_routine = dcs_message_callback;
	local_msg_cb.appdata_ptr = (void *)op_data->msgp;

	/* verify retry value */
	if (op_data->retries < 0) {
		dcs_log_msg(LOG_NOTICE, DCS_BAD_RETRY_VAL, op_data->retries);
		op_data->retries = 0;
	}

	/* verify timeout value */
	if (op_data->timeval < 0) {
		dcs_log_msg(LOG_NOTICE, DCS_BAD_TIME_VAL, op_data->timeval);
		op_data->timeval = 0;
	}

	num_attempts = 1 + op_data->retries;
	curr_attempt = 0;

	while (curr_attempt < num_attempts) {

		/* don't sleep the first time around */
		if (curr_attempt != 0) {

			/* log the error message and alert the user */
			err_str = dcs_cfga_str(op_data->errstring, cfga_status);
			if (err_str) {
				dcs_log_msg(LOG_ERR, DCS_CFGA_ERR, op_name,
				    err_str);
				dcs_message_callback((void *)op_data->msgp,
				    err_str);
				free((void *)err_str);
			} else {
				dcs_log_msg(LOG_ERR, DCS_CFGA_UNKNOWN);
				dcs_message_callback((void *)op_data->msgp,
				    dcs_strerror(DCS_CFGA_UNKNOWN));
			}

			if (op_data->errstring && *op_data->errstring) {
				free((void *)*op_data->errstring);
				*op_data->errstring = NULL;
			}

			/* sleep with abort enabled */
			ses_sleep(op_data->timeval);

			/* log the retry attempt and alert the user */
			dcs_log_msg(LOG_INFO, DCS_RETRY, curr_attempt);
			snprintf(retry_msg, MAX_MSG_LEN,
			    dcs_strerror(DCS_RETRY), curr_attempt);
			dcs_message_callback((void *)op_data->msgp, retry_msg);
		}

		sp->state = DCS_CONF_PENDING;

		/*
		 * Call into libcfgadm
		 */
		ses_abort_enable();

		cfga_status = config_change_state(op_data->state_change,
		    op_data->num_ap_ids, op_data->ap_ids, op_data->options,
		    &local_conf_cb, &local_msg_cb, op_data->errstring,
		    op_data->flags);

		ses_abort_disable();

		/*
		 * Retry only the operations that have a chance to
		 * succeed if retried. All libcfgadm errors not
		 * included below will always fail, regardless of
		 * a retry.
		 */
		if ((cfga_status != CFGA_BUSY) &&
		    (cfga_status != CFGA_SYSTEM_BUSY) &&
		    (cfga_status != CFGA_ERROR)) {
			break;
		}

		/* prepare for another attempt */
		++curr_attempt;
	}

	sp->state = DCS_CONF_DONE;

	/* log any libcfgadm errors */
	if (cfga_status != CFGA_OK) {
		err_str = dcs_cfga_str(op_data->errstring, cfga_status);
		if (err_str) {
			dcs_log_msg(LOG_ERR, DCS_CFGA_ERR, op_name, err_str);
			free((void *)err_str);
		}
	}

	/* prepare header information */
	init_msg(&reply_hdr);
	reply_hdr.message_opcode = RDR_CONF_CHANGE_STATE;
	reply_hdr.data_type = RDR_REPLY;
	reply_hdr.status = cfga_status;

	PRINT_MSG_DBG(DCS_SEND, &reply_hdr);

	/* send the message */
	snd_status = rdr_snd_msg(sp->fd, &reply_hdr, param, DCS_SND_TIMEOUT);

	if (snd_status == RDR_ABORTED) {
		abort_handler();
	}

	if (snd_status != RDR_OK) {
		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
	}

	/* clean up */
	if (op_data->errstring && *op_data->errstring) {
		free((void *)*op_data->errstring);
		*op_data->errstring = NULL;
	}

	return ((snd_status != RDR_OK) ? -1 : 0);
}


/*
 * dcs_private_func:
 *
 * Handle a private function request message (RDR_CONF_PRIVATE_FUNC).
 */
static int
dcs_private_func(rdr_msg_hdr_t *hdr, cfga_params_t *param)
{
	session_t		*sp;
	rdr_msg_hdr_t 		reply_hdr;
	private_func_params_t	*op_data;
	struct cfga_confirm 	local_conf_cb;
	struct cfga_msg		local_msg_cb;
	int			cfga_status;
	int			snd_status;
	char			*err_str;
	static char		*op_name = "config_private_func";


	assert(hdr);
	assert(param);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	op_data = &param->priv;

	/* make sure we have a session established */
	if (sp->state != DCS_SES_ESTBL) {
		dcs_log_msg(LOG_ERR, DCS_NO_SES_ESTBL, op_name);
		ses_close(DCS_NO_SES_ERR);
		return (-1);
	}

	/* initialize local confirm callback */
	local_conf_cb.confirm = dcs_confirm_callback;
	local_conf_cb.appdata_ptr = (void *)op_data->confp;

	/* initialize local message callback */
	local_msg_cb.message_routine = dcs_message_callback;
	local_msg_cb.appdata_ptr = (void *)op_data->msgp;

	sp->state = DCS_CONF_PENDING;

	/*
	 * Call into libcfgadm
	 */
	ses_abort_enable();

	cfga_status = config_private_func(op_data->function,
	    op_data->num_ap_ids, op_data->ap_ids, op_data->options,
	    &local_conf_cb, &local_msg_cb, op_data->errstring, op_data->flags);

	ses_abort_disable();

	sp->state = DCS_CONF_DONE;

	/* log any libcfgadm errors */
	if (cfga_status != CFGA_OK) {
		err_str = dcs_cfga_str(op_data->errstring, cfga_status);
		if (err_str) {
			dcs_log_msg(LOG_ERR, DCS_CFGA_ERR, op_name, err_str);
			free((void *)err_str);
		}
	}

	/* prepare header information */
	init_msg(&reply_hdr);
	reply_hdr.message_opcode = RDR_CONF_PRIVATE_FUNC;
	reply_hdr.data_type = RDR_REPLY;
	reply_hdr.status = cfga_status;

	PRINT_MSG_DBG(DCS_SEND, &reply_hdr);

	/* send the message */
	snd_status = rdr_snd_msg(sp->fd, &reply_hdr, param, DCS_SND_TIMEOUT);

	if (snd_status == RDR_ABORTED) {
		abort_handler();
	}

	if (snd_status != RDR_OK) {
		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
	}

	if (op_data->errstring && *op_data->errstring) {
		free((void *)*op_data->errstring);
		*op_data->errstring = NULL;
	}

	return ((snd_status != RDR_OK) ? -1 : 0);
}


/*
 * dcs_test:
 *
 * Handle a test request message (RDR_CONF_TEST).
 */
static int
dcs_test(rdr_msg_hdr_t *hdr, cfga_params_t *param)
{
	session_t		*sp;
	rdr_msg_hdr_t 		reply_hdr;
	test_params_t		*op_data;
	struct cfga_msg		local_msg_cb;
	int			cfga_status;
	int			snd_status;
	char			*err_str;
	static char		*op_name = "config_test";


	assert(hdr);
	assert(param);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	op_data = &param->test;

	/* make sure we have a session established */
	if (sp->state != DCS_SES_ESTBL) {
		dcs_log_msg(LOG_ERR, DCS_NO_SES_ESTBL, op_name);
		ses_close(DCS_NO_SES_ERR);
		return (-1);
	}

	/* initialize local message callback */
	local_msg_cb.message_routine = dcs_message_callback;
	local_msg_cb.appdata_ptr = op_data->msgp;

	sp->state = DCS_CONF_PENDING;

	/*
	 * Call into libcfgadm
	 */
	ses_abort_enable();

	cfga_status = config_test(op_data->num_ap_ids, op_data->ap_ids,
	    op_data->options, &local_msg_cb, op_data->errstring,
	    op_data->flags);

	ses_abort_disable();

	sp->state = DCS_CONF_DONE;

	/* log any libcfgadm errors */
	if (cfga_status != CFGA_OK) {
		err_str = dcs_cfga_str(op_data->errstring, cfga_status);
		if (err_str) {
			dcs_log_msg(LOG_ERR, DCS_CFGA_ERR, op_name, err_str);
			free((void *)err_str);
		}
	}

	/* prepare header information */
	init_msg(&reply_hdr);
	reply_hdr.message_opcode = RDR_CONF_TEST;
	reply_hdr.data_type = RDR_REPLY;
	reply_hdr.status = cfga_status;

	PRINT_MSG_DBG(DCS_SEND, &reply_hdr);

	/* send the message */
	snd_status = rdr_snd_msg(sp->fd, &reply_hdr, param, DCS_SND_TIMEOUT);

	if (snd_status == RDR_ABORTED) {
		abort_handler();
	}

	if (snd_status != RDR_OK) {
		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
	}

	if (op_data->errstring && *op_data->errstring) {
		free((void *)*op_data->errstring);
		*op_data->errstring = NULL;
	}

	return ((snd_status != RDR_OK) ? -1 : 0);
}


/*
 * dcs_list_ext:
 *
 * Handle a list request message (RDR_CONF_LIST_EXT).
 */
static int
dcs_list_ext(rdr_msg_hdr_t *hdr, cfga_params_t *param)
{
	session_t		*sp;
	rdr_msg_hdr_t 		reply_hdr;
	list_ext_params_t	*op_data;
	int			cfga_status;
	int			snd_status;
	char			*err_str;
	static char		*op_name = "config_list_ext";
	cfga_list_data_t	*ap_ids;


	assert(hdr);
	assert(param);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	op_data = &param->list_ext;

	/* make sure we have a session established */
	if (sp->state != DCS_SES_ESTBL) {
		dcs_log_msg(LOG_ERR, DCS_NO_SES_ESTBL, op_name);
		ses_close(DCS_NO_SES_ERR);
		return (-1);
	}

	/*
	 * Make sure that we can retrieve the data
	 * from libcfgadm. If not, report the error.
	 */
	if (op_data->ap_id_list == NULL) {
		dcs_log_msg(LOG_ERR, DCS_MSG_INVAL);
		ses_close(DCS_MSG_INVAL);
		return (-1);
	}

	sp->state = DCS_CONF_PENDING;

	/*
	 * Call into libcfgadm
	 */
	ses_abort_enable();

	cfga_status = config_list_ext(op_data->num_ap_ids, op_data->ap_ids,
	    &ap_ids, op_data->nlist, op_data->options, op_data->listopts,
	    op_data->errstring, op_data->flags);

	ses_abort_disable();

	sp->state = DCS_CONF_DONE;

	/*
	 * Log any libcfgadm errors at a low priority level.
	 * Since a status request does not modify the system
	 * in any way, we do not need to worry about these
	 * errors here on the host.
	 */
	if (cfga_status != CFGA_OK) {
		err_str = dcs_cfga_str(op_data->errstring, cfga_status);
		if (err_str) {
			dcs_log_msg(LOG_INFO, DCS_CFGA_ERR, op_name, err_str);
			free((void *)err_str);
		}
	}

	/*
	 * Filter ap ids to return only appropriate information
	 */
	filter_list_data(op_data->permissions, op_data->nlist, ap_ids);

	/* if all aps were filtered out, return an error */
	if ((cfga_status == CFGA_OK) && (*op_data->nlist == 0)) {
		cfga_status = CFGA_APID_NOEXIST;
	}

	/* calculate the sort order */
	if (cfga_status == CFGA_OK) {

		*op_data->ap_id_list = generate_sort_order(ap_ids,
		    *op_data->nlist);

		if (*op_data->ap_id_list == NULL) {
			cfga_status = CFGA_LIB_ERROR;
		}
	}

	/* ensure that nlist is 0 for errors */
	if (cfga_status != CFGA_OK) {
		*op_data->nlist = 0;
	}

	/* prepare header information */
	init_msg(&reply_hdr);
	reply_hdr.message_opcode = RDR_CONF_LIST_EXT;
	reply_hdr.data_type = RDR_REPLY;
	reply_hdr.status = cfga_status;

	PRINT_MSG_DBG(DCS_SEND, &reply_hdr);

	/* send the message */
	snd_status = rdr_snd_msg(sp->fd, &reply_hdr, param, DCS_SND_TIMEOUT);

	if (snd_status == RDR_ABORTED) {
		abort_handler();
	}

	if (snd_status != RDR_OK) {
		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
	}

	if (op_data->errstring && *op_data->errstring) {
		free((void *)*op_data->errstring);
		*op_data->errstring = NULL;
	}

	if (ap_ids != NULL) {
		free((void *)ap_ids);
	}

	return ((snd_status != RDR_OK) ? -1 : 0);
}


/*
 * dcs_help:
 *
 * Handle a help request message (RDR_CONF_HELP).
 */
static int
dcs_help(rdr_msg_hdr_t *hdr, cfga_params_t *param)
{
	session_t		*sp;
	rdr_msg_hdr_t 		reply_hdr;
	help_params_t		*op_data;
	struct cfga_msg		local_msg_cb;
	int			cfga_status;
	int			snd_status;
	char			*err_str;
	static char		*op_name = "config_help";


	assert(hdr);
	assert(param);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	op_data = &param->help;

	/* make sure we have a session established */
	if (sp->state != DCS_SES_ESTBL) {
		dcs_log_msg(LOG_ERR, DCS_NO_SES_ESTBL, op_name);
		ses_close(DCS_NO_SES_ERR);
		return (-1);
	}

	/* initialize local message callback */
	local_msg_cb.message_routine = dcs_message_callback;
	local_msg_cb.appdata_ptr = op_data->msgp;

	sp->state = DCS_CONF_PENDING;

	/*
	 * Call into libcfgadm
	 */
	ses_abort_enable();

	cfga_status = config_help(op_data->num_ap_ids, op_data->ap_ids,
	    &local_msg_cb, op_data->options, op_data->flags);

	ses_abort_disable();

	sp->state = DCS_CONF_DONE;

	/*
	 * Log any libcfgadm errors at a low priority level.
	 * Since a help request does not modify the system
	 * in any way, we do not need to worry about these
	 * errors here on the host.
	 */
	if (cfga_status != CFGA_OK) {
		err_str = dcs_cfga_str(NULL, cfga_status);
		if (err_str) {
			dcs_log_msg(LOG_INFO, DCS_CFGA_ERR, op_name, err_str);
			free((void *)err_str);
		}
	}

	/* prepare header information */
	init_msg(&reply_hdr);
	reply_hdr.message_opcode = RDR_CONF_HELP;
	reply_hdr.data_type = RDR_REPLY;
	reply_hdr.status = cfga_status;

	PRINT_MSG_DBG(DCS_SEND, &reply_hdr);

	/* send the message */
	snd_status = rdr_snd_msg(sp->fd, &reply_hdr, param, DCS_SND_TIMEOUT);

	if (snd_status == RDR_ABORTED) {
		abort_handler();
	}

	if (snd_status != RDR_OK) {
		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
	}

	return ((snd_status != RDR_OK) ? -1 : 0);
}


/*
 * dcs_ap_id_cmp:
 *
 * Handle an attachment point comparison request message (RDR_AP_ID_CMP).
 */
static int
dcs_ap_id_cmp(rdr_msg_hdr_t *hdr, cfga_params_t *param)
{
	session_t		*sp;
	rdr_msg_hdr_t 		reply_hdr;
	ap_id_cmp_params_t	*op_data;
	int			snd_status;
	int			cmp_result;
	static char		*op_name = "config_ap_id_cmp";


	assert(hdr);
	assert(param);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	op_data = &param->cmp;

	/* make sure we have a session established */
	if (sp->state != DCS_SES_ESTBL) {
		dcs_log_msg(LOG_ERR, DCS_NO_SES_ESTBL, op_name);
		ses_close(DCS_NO_SES_ERR);
		return (-1);
	}

	sp->state = DCS_CONF_PENDING;

	/*
	 * Call into libcfgadm
	 */
	ses_abort_enable();

	cmp_result = config_ap_id_cmp(op_data->ap_log_id1, op_data->ap_log_id2);

	ses_abort_disable();

	sp->state = DCS_CONF_DONE;

	/* prepare header information */
	init_msg(&reply_hdr);
	reply_hdr.message_opcode = RDR_CONF_AP_ID_CMP;
	reply_hdr.data_type = RDR_REPLY;

	/*
	 * Return result of comparison as error code.
	 * Since all values are valid, it is impossible
	 * to report an error.
	 */
	reply_hdr.status = cmp_result;

	PRINT_MSG_DBG(DCS_SEND, &reply_hdr);

	/* send the message */
	snd_status = rdr_snd_msg(sp->fd, &reply_hdr, param, DCS_SND_TIMEOUT);

	if (snd_status == RDR_ABORTED) {
		abort_handler();
	}

	if (snd_status != RDR_OK) {
		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
	}

	return ((snd_status != RDR_OK) ? -1 : 0);
}


/*
 * dcs_abort_cmd:
 *
 * Handle an abort request message (RDR_CONF_ABORT_CMD).
 */
/* ARGSUSED */
static int
dcs_abort_cmd(rdr_msg_hdr_t *hdr, cfga_params_t *param)
{
	session_t		*sp;
	rdr_msg_hdr_t		reply_hdr;
	abort_cmd_params_t	*op_data;
	int			op_status = RDR_SUCCESS;
	int			snd_status;
	static char		*op_name = "abort command";


	assert(hdr);
	assert(param);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	op_data = (abort_cmd_params_t *)param;

	op_status = ses_abort(op_data->session_id);

	if (op_status == -1) {
		dcs_log_msg(LOG_ERR, DCS_ABORT_ERR, op_data->session_id);
	}

	/* prepare header information */
	init_msg(&reply_hdr);
	reply_hdr.message_opcode = RDR_CONF_ABORT_CMD;
	reply_hdr.data_type = RDR_REPLY;
	reply_hdr.status = op_status;

	PRINT_MSG_DBG(DCS_SEND, &reply_hdr);

	/* send the message */
	snd_status = rdr_snd_msg(sp->fd, &reply_hdr, param, DCS_SND_TIMEOUT);

	if (snd_status == RDR_ABORTED) {
		abort_handler();
	}

	if (snd_status != RDR_OK) {
		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
	}

	sp->state = DCS_CONF_DONE;

	return ((snd_status != RDR_OK) ? -1 : 0);
}


/*
 * dcs_rsrc_info:
 *
 * Handle a resource info request message (RDR_RSRC_INFO).
 */
static int
dcs_rsrc_info(rdr_msg_hdr_t *hdr, cfga_params_t *param)
{
	session_t		*sp;
	rdr_msg_hdr_t		reply_hdr;
	rsrc_info_params_t	*op_data;
	int			rsrc_status;
	int			snd_status;
	static char		*op_name = "resource info init";

	assert(hdr);
	assert(param);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	op_data = (rsrc_info_params_t *)&param->rsrc_info;

	/* make sure we have a session established */
	if (sp->state != DCS_SES_ESTBL) {
		dcs_log_msg(LOG_ERR, DCS_NO_SES_ESTBL, op_name);
		ses_close(DCS_NO_SES_ERR);
		return (-1);
	}

	sp->state = DCS_CONF_PENDING;

	/*
	 * Request resource info data.
	 */
	ses_abort_enable();

	rsrc_status = ri_init(op_data->num_ap_ids, op_data->ap_ids,
	    op_data->flags, &op_data->hdl);

	ses_abort_disable();

	sp->state = DCS_CONF_DONE;

	/* log errors */
	if (rsrc_status != RI_SUCCESS) {
		dcs_log_msg(LOG_ERR, DCS_RSRC_ERR, rsrc_status);
	}

	/* prepare header information */
	init_msg(&reply_hdr);
	reply_hdr.message_opcode = RDR_RSRC_INFO;
	reply_hdr.data_type = RDR_REPLY;
	reply_hdr.status = rsrc_status;

	PRINT_MSG_DBG(DCS_SEND, &reply_hdr);

	/* send the message */
	snd_status = rdr_snd_msg(sp->fd, &reply_hdr, param, DCS_SND_TIMEOUT);

	if (snd_status == RDR_ABORTED) {
		abort_handler();
	}

	if (snd_status != RDR_OK) {
		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
	}

	ri_fini(op_data->hdl);

	return ((snd_status != RDR_OK) ? -1 : 0);
}


/*
 * dcs_unknown_op:
 *
 * Handle all unknown requests.
 */
/* ARGSUSED */
static int
dcs_unknown_op(rdr_msg_hdr_t *hdr, cfga_params_t *param)
{
	session_t	*sp;


	assert(hdr);
	assert(param);

	assert(hdr);

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		ses_close(DCS_ERROR);
		return (-1);
	}

	dcs_log_msg(LOG_ERR, DCS_UNKNOWN_OP, hdr->message_opcode);

	sp->state = DCS_CONF_DONE;

	return (-1);
}


/*
 * dcs_confirm_callback:
 *
 * Perform a confirm callback and wait for the reply. As defined
 * in the config_admin(3CFGADM) man page, 1 is returned if the
 * operation should be allowed to continue and 0 otherwise.
 */
static int
dcs_confirm_callback(void *appdata_ptr, const char *message)
{
	session_t		*sp;
	rdr_msg_hdr_t		req_hdr;
	cfga_params_t		req_data;
	struct cfga_confirm	*cb_data;
	rdr_msg_hdr_t		reply_hdr;
	cfga_params_t		reply_data;
	int			snd_status;
	int			rcv_status;
	static char		*op_name = "confirm callback";


	/* sanity check */
	if (appdata_ptr == NULL) {
		dcs_log_msg(LOG_NOTICE, DCS_CONF_CB_ERR);
		return (0);
	}

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		dcs_log_msg(LOG_NOTICE, DCS_CONF_CB_ERR);
		return (0);
	}

	cb_data = (struct cfga_confirm *)appdata_ptr;

	/* prepare header information */
	init_msg(&req_hdr);
	req_hdr.message_opcode = RDR_CONF_CONFIRM_CALLBACK;
	req_hdr.data_type = RDR_REQUEST;

	/* prepare confirm callback specific data */
	(void) memset(&req_data, 0, sizeof (req_data));
	req_data.conf_cb.confp = cb_data;
	req_data.conf_cb.message = (char *)message;

	PRINT_MSG_DBG(DCS_SEND, &req_hdr);

	/* send the message */
	snd_status = rdr_snd_msg(sp->fd, &req_hdr, &req_data, DCS_SND_TIMEOUT);

	if (snd_status == RDR_ABORTED) {
		abort_handler();
	}

	if (snd_status != RDR_OK) {
		dcs_log_msg(LOG_NOTICE, DCS_CONF_CB_ERR);
		return (0);
	}

	/*
	 * Wait for response
	 */
	rcv_status = rdr_rcv_msg(sp->fd, &reply_hdr, &reply_data,
	    DCS_RCV_CB_TIMEOUT);

	if (rcv_status != RDR_OK) {
		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
		dcs_log_msg(LOG_NOTICE, DCS_CONF_CB_ERR);
		return (0);
	}

	/*
	 * Perform several checks to see if we have a
	 * valid response to the confirm callback.
	 */
	if (invalid_msg(&reply_hdr)) {
		dcs_log_msg(LOG_ERR, DCS_MSG_INVAL);
		dcs_log_msg(LOG_NOTICE, DCS_CONF_CB_ERR);
		return (0);
	}

	/* check the opcode and type */
	if ((reply_hdr.message_opcode != RDR_CONF_CONFIRM_CALLBACK) ||
	    (reply_hdr.data_type != RDR_REPLY)) {
		DCS_DBG(DBG_MSG, "bad opcode or message type");
		dcs_log_msg(LOG_ERR, DCS_MSG_INVAL);
		dcs_log_msg(LOG_NOTICE, DCS_CONF_CB_ERR);
		return (0);
	}

	PRINT_MSG_DBG(DCS_RECEIVE, &reply_hdr);

	/* check for incorrect callback id */
	if (reply_data.conf_cb.confp->confirm != cb_data->confirm) {
		dcs_log_msg(LOG_ERR, DCS_MSG_INVAL);
		dcs_log_msg(LOG_NOTICE, DCS_CONF_CB_ERR);
		return (0);
	}

	/*
	 * Got back valid response: return the user's answer
	 */
	return (reply_data.conf_cb.response);
}


/*
 * dcs_message_callback:
 *
 * Perform a message callback to display a string to the user.
 *
 * Note: There is no documentation about possible return values
 * for the message callback. It is assumed that the value returned
 * is ignored, so 0 is returned for all cases.
 */
static int
dcs_message_callback(void *appdata_ptr, const char *message)
{
	session_t	*sp;
	rdr_msg_hdr_t	req_hdr;
	cfga_params_t	req_data;
	struct cfga_msg	*cb_data;
	int		snd_status;
	static char	*op_name = "message callback";


	/* sanity check */
	if (appdata_ptr == NULL) {
		dcs_log_msg(LOG_NOTICE, DCS_MSG_CB_ERR);
		return (0);
	}

	/* get the current session information */
	if ((sp = curr_ses()) == NULL) {
		dcs_log_msg(LOG_NOTICE, DCS_MSG_CB_ERR);
		return (0);
	}

	cb_data = (struct cfga_msg *)appdata_ptr;

	/* prepare header information */
	init_msg(&req_hdr);
	req_hdr.message_opcode = RDR_CONF_MSG_CALLBACK;
	req_hdr.data_type = RDR_REQUEST;

	/* prepare message callback specific data */
	(void) memset(&req_data, 0, sizeof (req_data));
	req_data.msg_cb.msgp = cb_data;
	req_data.msg_cb.message = (char *)message;

	PRINT_MSG_DBG(DCS_SEND, &req_hdr);

	/* send the message */
	snd_status = rdr_snd_msg(sp->fd, &req_hdr, (cfga_params_t *)&req_data,
	    DCS_SND_TIMEOUT);

	if (snd_status == RDR_ABORTED) {
		abort_handler();
	}

	if (snd_status != RDR_OK) {
		dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
		dcs_log_msg(LOG_NOTICE, DCS_MSG_CB_ERR);
	}

	return (0);
}


/*
 * resolve_version:
 *
 * Consult the list of supported versions and find the highest supported
 * version that is less than or equal to the version requested in the
 * parameters. This assumes that the list of supported versions is ordered
 * so that the highest supported version is the first element, and that
 * the versions are strictly decreasing.
 */
static dcs_ver_t
resolve_version(ushort_t req_major, ushort_t req_minor)
{
	int		i;
	dcs_ver_t	act_ver;
	int		num_vers;


	num_vers = sizeof (ver_supp) / sizeof (*ver_supp);

	/* default to the lowest version */
	act_ver = ver_supp[num_vers - 1];

	for (i = 0; i < num_vers; i++) {

		if (req_major == ver_supp[i].major) {

			if (req_minor >= ver_supp[i].minor) {
				/*
				 * The major version matches and the
				 * minor version either matches, or
				 * is the best match that we have.
				 */
				act_ver = ver_supp[i];
				break;
			}

		} else if (req_major > ver_supp[i].major) {
			/*
			 * The requested major version is larger than
			 * the current version we are checking. There
			 * is not going to be a better match.
			 */
			act_ver = ver_supp[i];
			break;
		}
	}

	DCS_DBG(DBG_SES, "requested ver: %d.%d, closest match: %d.%d",
	    req_major, req_minor, act_ver.major, act_ver.minor);

	return (act_ver);
}


/*
 * filter_list_data:
 *
 * Check a list of cfga_list_data_t structures to filter out the ones
 * that don't have other-read permissions. All valid entries are placed
 * at the beginning of the array and the count of entries is updated.
 */
static void
filter_list_data(int perm, int *nlistp, cfga_list_data_t *linfo)
{
	int		num_aps;
	int		num_aps_ret;
	int		curr_ap;
	int		next_aval;
	int		end_block;
	int		block_size;
	struct stat	ap_info;


	DCS_DBG(DBG_MSG, "list access = %s", (perm == RDR_PRIVILEGED) ?
	    "RDR_PRIVILEGED" : "RDR_NOT_PRIVILEGED");

	/*
	 * Check if the user has priviledged access
	 * to view all attachment points
	 */
	if (perm == RDR_PRIVILEGED) {
		return;
	}

	if (*nlistp < 0) {
		*nlistp = 0;
	}

	/*
	 * No priviledged access, check each attachment point to
	 * see if the user has access (other:read) to view it.
	 */
	num_aps = *nlistp;
	next_aval = 0;
	num_aps_ret = 0;
	curr_ap = 0;

	/*
	 * Use a simple algorithm to compact the array so that
	 * all attachment points that can be viewed are at the
	 * beginning of the array. Adjust the count of the
	 * attachment points accordingly.
	 */
	while (curr_ap < num_aps) {

		stat(linfo[curr_ap].ap_phys_id, &ap_info);

		/* check for unrestricted read permission */
		if (ap_info.st_mode & S_IROTH) {

			end_block = curr_ap + 1;

			/*
			 * Check if this is the beginning of a
			 * block of consecutive ap ids that can
			 * be returned.
			 */
			while (end_block < num_aps) {

				stat(linfo[end_block].ap_phys_id, &ap_info);

				/* search until the end of the block */
				if (ap_info.st_mode & S_IROTH) {
					end_block++;
				} else {
					break;
				}
			}

			block_size = end_block - curr_ap;

			/* make sure a copy is necessary */
			if (curr_ap != next_aval) {

				/* copy the block of ap ids all at once */
				(void) memmove(&linfo[next_aval],
				    &linfo[curr_ap],
				    block_size * sizeof (cfga_list_data_t));
			}

			/* move past the copied block */
			next_aval += block_size;
			curr_ap = end_block;

			num_aps_ret += block_size;
		} else {
			curr_ap++;
		}
	}

	DCS_DBG(DBG_ALL, "filtered %d of %d ap ids", (*nlistp - num_aps_ret),
	    *nlistp);

	/*
	 * return the number of aps that have the correct
	 * access permissions.
	 */
	*nlistp = num_aps_ret;
}


/*
 * generate_sort_order:
 *
 * Determine the sort order of an array of cfga_list_data_t structures
 * and create an array of rdr_list_t structures that contain the original
 * elements tagged with the sort order.
 *
 * This function is used to eliminate unnecessary network traffic that
 * might occur if the client needs the output of config_list_ext(3CFGADM)
 * sorted. Since a comparison is performed in a platform specific manner
 * using config_ap_id_cmp(3CFGADM), a client must establish a new session
 * for each comparison. For a long lists of attachment points, this can
 * slow down a simple list_ext operation significantly. With the sort
 * information included in the array of rdr_list_t structures, the client
 * can perform the sort operation locally, thus eliminating a great deal
 * of network traffic.
 */
static rdr_list_t *
generate_sort_order(cfga_list_data_t *listp, int nlist)
{
	int			curr_ap;
	rdr_list_t		*datalp;
	cfga_list_data_t	*sortlp;
	cfga_list_data_t	*match;


	assert(listp);

	if (nlist <= 0) {
		return (NULL);
	}

	/* create our new array */
	datalp = (rdr_list_t *)malloc(nlist * sizeof (rdr_list_t));

	if (datalp == NULL) {
		return (NULL);
	}


	/* copy over the elements, preserving the original order */
	for (curr_ap = 0; curr_ap < nlist; curr_ap++) {
		datalp[curr_ap].ap_id_info = listp[curr_ap];
	}

	/* handle a one element list */
	if (nlist == 1) {
		datalp[0].sort_order = 0;
		return (datalp);
	}

	/* sort the cfga_list_data_t array */
	qsort(listp, nlist, sizeof (listp[0]), ldata_compare);

	sortlp = listp;

	/* process each item in the original list */
	for (curr_ap = 0; curr_ap < nlist; curr_ap++) {

		/* look up the sort order in the sorted list */
		match = bsearch(&datalp[curr_ap].ap_id_info, sortlp,
		    nlist, sizeof (cfga_list_data_t), ldata_compare);

		/* found a match */
		if (match != NULL) {
			datalp[curr_ap].sort_order = match - sortlp;
		} else {
			/*
			 * Should never get here. Since we did a
			 * direct copy of the array, we should always
			 * be able to find the ap id that we were
			 * looking for.
			 */
			DCS_DBG(DBG_ALL, "could not find a matching "
			    "ap id in the sorted list");
			datalp[curr_ap].sort_order = 0;
		}
	}

	return (datalp);
}


/*
 * ldata_compare:
 *
 * Compare the two inputs to produce a strcmp(3C) style result. It uses
 * config_ap_id_cmp(3CFGADM) to perform the comparison.
 *
 * This function is passed to qsort(3C) in generate_sort_order() to sort a
 * list of attachment points.
 */
static int
ldata_compare(const void *ap1, const void *ap2)
{
	cfga_list_data_t *ap_id1;
	cfga_list_data_t *ap_id2;

	ap_id1 = (cfga_list_data_t *)ap1;
	ap_id2 = (cfga_list_data_t *)ap2;

	return (config_ap_id_cmp(ap_id1->ap_log_id, ap_id2->ap_log_id));
}


/*
 * basename:
 *
 * Find short path name of a full path name. If a short path name
 * is passed in, the original pointer is returned.
 */
static char *
basename(char *cp)
{
	char *sp;

	if ((sp = strrchr(cp, '/')) != NULL) {
		return (sp + 1);
	}

	return (cp);
}

/*
 * is_socket:
 *
 * determine if fd represents a socket file type.
 */
static boolean_t
is_socket(int fd)
{
	struct stat statb;
	if (fstat(fd, &statb) < 0) {
		return (B_FALSE);
	}
	return (S_ISSOCK(statb.st_mode));
}

/*
 * has_dcs_token
 *
 * Look for "?port [sun-dr|665]" in input buf.
 * Assume only a single thread calls here.
 */
static boolean_t
has_dcs_token(char *buf)
{
	char 		*token;
	char		*delims = "{} \t\n";
	boolean_t 	port = B_FALSE;

	while ((token = strtok(buf, delims)) != NULL) {
		buf = NULL;
		if (port == B_TRUE) {
			if (strcmp(token, "sun-dr") == 0 ||
			    strcmp(token, "665") == 0) {
				return (B_TRUE);
			} else {
				return (B_FALSE);
			}
		}
		if (strlen(token) == 5) {
			token++;
			if (strcmp(token, "port") == 0) {
				port = B_TRUE;
				continue;
			}
		}
	}
	return (B_FALSE);
}

/*
 * dcs_global_policy
 *
 * Check global policy file for dcs entry. Just covers common cases.
 */
static boolean_t
dcs_global_policy()
{
	FILE		*fp;
	char		buf[256];
	boolean_t	rv = B_FALSE;

	fp = fopen("/etc/inet/ipsecinit.conf", "r");
	if (fp == NULL)
		return (B_FALSE);
	while (fgets(buf, sizeof (buf), fp) != NULL) {
		if (buf[0] == '#')
			continue;
		if (has_dcs_token(buf)) {
			rv = B_TRUE;
			syslog(LOG_NOTICE, "dcs using global policy");
			break;
		}
	}
	(void) fclose(fp);
	return (rv);
}