summaryrefslogtreecommitdiffstats
path: root/lib/libaccess/aclparse.cpp
blob: 1ca1eae2350b6d4ccd13a8f9a7246d6a39fd71cc (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
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

/*
 * Description (aclparse.c)
 *
 *	This module provides functions for parsing a file containing
 *	Access Control List (ACL) definitions.  It builds a representation
 *	of the ACLs in memory, using the services of the aclbuild module.
 */

#include <base/systems.h>
#include <base/file.h>
#include <base/util.h>
#include <netsite.h>
#include <libaccess/nsadb.h>
#include <libaccess/aclerror.h>
#include <libaccess/aclparse.h>
#include <libaccess/symbols.h>

#ifdef XP_UNIX
#include <sys/types.h>
#include <netinet/in.h>  /* ntohl */
#include <arpa/inet.h>
#endif

void * aclChTab = 0;		/* character class table handle */

static char * classv[] = {
    " \t\r\f\013",		/* class 0 - whitespace */
    "\n",			/* class 1 - newline */
    ",.;@*()+{}\"\'",		/* class 2 - special characters */
    "0123456789",		/* class 3 - digits */
				/* class 4 - letters */
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
    "-",			/* class 5 - hyphen */
    "_",			/* class 6 - underscore */
    "/-_.:"			/* class 7 - filename special characters */
};

static int classc = sizeof(classv)/sizeof(char *);

/*
 * Description (aclAuthListParse)
 *
 *	This function parses an auth-list.  An auth-list specifies
 *	combinations of user/group names and host addresses/names.
 *	An auth-list entry can identify a collection of users and/or
 *	groups, a collection of hosts by IP addresses or DNS names,
 *	or a combination of the two.  Each auth-spec adds another
 *	ACClients_t structure to the specified list.
 *
 *	The syntax for an auth-list is:
 *
 *	auth-list ::= auth-spec | auth-list "," auth-spec
 *	auth-spec ::= auth-users [at-token auth-hosts]
 *	auth-users - see aclAuthUsersParse()
 *	auth-hosts - see aclAuthHostsParse()
 *	at-token ::= "at" | "@"
 *
 *	The caller provides a pointer to a ClientSpec_t structure,
 *	which is built up with new information as auth-specs are parsed.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	acf			- pointer to ACLFile_t for ACL file
 *	acc			- pointer to ACL context object
 *	rlm			- pointer to authentication realm object
 *	clsp			- pointer to returned ACClients_t list head
 *
 * Returns:
 *
 *	If successful, the return value is the token type of the token
 *	following the auth-list, i.e. the first token which is not
 *	recognized as the start of an auth-spec.  It is the caller's
 *	responsibility to validate this token as a legitimate terminator
 *	of an auth-list.  If a parsing error occurs in the middle of
 *	an auth-spec, the return value is ACLERRPARSE, and an error frame
 *	is generated if an error list is provided.  For other kinds of
 *	errors a negative error code (from aclerror.h) is returned.
 */

int aclAuthListParse(NSErr_t * errp, ACLFile_t * acf,
		     ACContext_t * acc, Realm_t * rlm, ACClients_t **clsp)
{
    void * token = acf->acf_token;	/* token handle */
    ACClients_t * csp;			/* client spec pointer */
    UserSpec_t * usp;			/* user spec pointer */
    HostSpec_t * hsp;			/* host spec pointer */
    int rv;				/* result value */
    int eid;				/* error id */

    /* Loop once for each auth-spec */
    for (rv = acf->acf_ttype; ; rv = aclGetToken(errp, acf, 0)) {

	usp = 0;
	hsp = 0;

	/* Parse auth-users into user and group lists in the ACClients_t */
	rv = aclAuthUsersParse(errp, acf, rlm, &usp, 0);
	if (rv < 0) break;

	/* Is the at-token there? */
	if ((rv == TOKEN_AT) || !strcasecmp(lex_token(token), KEYWORD_AT)) {

	    /* Step to the next token after the at-token */
	    rv = aclGetToken(errp, acf, 0);
	    if (rv < 0) break;

	    /* Parse auth-hosts part, adding information to the HostSpec_t */
	    rv = aclAuthHostsParse(errp, acf, acc, &hsp);
	    if (rv < 0) break;
	}

	/* Create a new ACClients_t structure for the parsed information */
	csp = (ACClients_t *)MALLOC(sizeof(ACClients_t));
	if (csp == 0) goto err_nomem;

	csp->cl_next = 0;
	csp->cl_user = usp;
	csp->cl_host = hsp;

	/* Add it to the end of the list referenced by clsp */
	while (*clsp != 0) clsp = &(*clsp)->cl_next;
	*clsp = csp;

	/* Need a "," to keep going */
	if (rv != TOKEN_COMMA) break;
    }

    return rv;

  err_nomem:
    eid = ACLERR1000;
    nserrGenerate(errp, ACLERRNOMEM, eid, ACL_Program, 0);
    return ACLERRNOMEM;
}

/*
 * Description (aclAuthHostsParse)
 *
 *	This function parses a list of IP address and/or DNS name
 *	specifications, adding information to the IP and DNS filters
 *	associated with a specified HostSpec_t.  The syntax of the
 *	auth-hosts construct is:
 *
 *	auth-hosts ::= auth-host-elem | "(" auth-host-list ")"
 *				      | "hosts" host-list-name
 *	auth-host-elem ::= auth-ip-spec | auth-dns-spec
 *	auth-ip-spec ::= ipaddr | ipaddr netmask
 *	auth-dns-spec ::= fqdn | dns-suffix
 *	auth-host-list ::= auth-host-elem | auth-host-list "," auth-host-elem
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	acf			- pointer to ACLFile_t for ACL file
 *	acc			- pointer to ACL context object
 *	hspp			- pointer to HostSpec_t pointer
 *
 * Returns:
 *
 *	If successful, the return value is the token type of the token
 *	following the auth-hosts, i.e. either the first token after a
 *	single auth-host-elem or the first token after the closing ")"
 *	of a list of auth-host-elems.  It is the caller's responsibility
 *	to validate this token as a legitimate successor of auth-hosts.
 *	If a parsing error occurs in the middle of auth-hosts, the return
 *	value is ACLERRPARSE, and an error frame is generated if an error
 *	list is provided.  For other kinds of errors a negative error
 *	code (from aclerror.h) is returned.
 */

int aclAuthHostsParse(NSErr_t * errp,
		      ACLFile_t * acf, ACContext_t * acc, HostSpec_t **hspp)
{
    void * token = acf->acf_token;	/* token handle */
    char * tokenstr;			/* token string pointer */
    int islist = 0;			/* true if auth-host-list */
    int fqdn;				/* fully qualified domain name */
    IPAddr_t ipaddr;			/* IP address value */
    IPAddr_t netmask;			/* IP netmask value */
    int arv;				/* alternate result value */
    int rv;				/* result value */
    int eid;				/* error id */
    char linestr[16];			/* line number string buffer */

    rv = acf->acf_ttype;

    /* Are we starting an auth-host-list? */
    if (rv == TOKEN_LPAREN) {

	/* Yes, it appears so */
	islist = 1;

	/* Step token to first auth-host-elem */
	rv = aclGetToken(errp, acf, 0);
	if (rv < 0) goto punt;
    }
    else if (rv == TOKEN_IDENT) {

	/* Could this be "hosts host-list-name"? */
	tokenstr = lex_token(token);

	if (!strcasecmp(tokenstr, KEYWORD_HOSTS)) {

	    /* We don't support lists of host lists yet */
	    if (*hspp != 0) goto err_unshl;

	    /* Get host-list-name */
	    rv = aclGetToken(errp, acf, 0);
	    if (rv < 0) goto punt;

	    if (rv != TOKEN_IDENT) goto err_hlname;

	    tokenstr = lex_token(token);

	    /* Look up the host-list-name in the ACL symbol table */
	    rv = symTableFindSym(acc->acc_stp,
				 tokenstr, ACLSYMHOST, (void **)hspp);
	    if (rv < 0) goto err_undefhl;

	    /* Step to token after the host-list-name */
	    rv = aclGetToken(errp, acf, 0);

	    return rv;
	}
    }

    /* Loop for each auth-host-elem */
    for (rv = acf->acf_ttype; ; rv = aclGetToken(errp, acf, 0)) {

	/* Does this look like an auth-ip-spec? */
	if (rv == TOKEN_NUMBER) {

	    /* Yes, go parse it */
	    rv = aclGetIPAddr(errp, acf, &ipaddr, &netmask);
	    if (rv < 0) goto punt;

	    arv = aclAuthIPAdd(hspp, ipaddr, netmask);
	    if (arv < 0) goto err_ipadd;
	}
	else if ((rv == TOKEN_STAR) || (rv == TOKEN_IDENT)) {

	    /* Get fully qualified DNS name indicator value */
	    fqdn = (rv == TOKEN_IDENT) ? 1 : 0;

	    /* This looks like the start of an auth-dns-spec */
	    rv = aclGetDNSString(errp, acf);
	    if (rv < 0) goto punt;

	    tokenstr = lex_token(token);

	    /* If the DNS spec begins with "*.", strip the "*" */
	    if (tokenstr && (tokenstr[0] == '*') && (tokenstr[1] == '.')) {
		tokenstr += 1;
	    }

	    arv = aclAuthDNSAdd(hspp, tokenstr, fqdn);
	    if (arv < 0) goto err_dnsadd;

	    /* Pick up the next token */
	    rv = aclGetToken(errp, acf, 0);
	}
	else break;

	/* If this is a list, we need a "," to keep going */
	if (!islist || (rv != TOKEN_COMMA)) break;
    }

    /* Were we parsing an auth-host-list? */
    if (islist) {

	/* Yes, check for closing ")" */
	if (acf->acf_ttype != TOKEN_RPAREN) goto err_norp;

	/* Got it.  Step to next token for caller. */
	rv = aclGetToken(errp, acf, 0);
    }

  punt:
    return rv;

  err_unshl:
    eid = ACLERR1100;
    goto err_parse;

  err_hlname:
    eid = ACLERR1120;
    goto err_parse;

  err_undefhl:
    eid = ACLERR1140;
    rv = ACLERRUNDEF;
    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program,
		  3, acf->acf_filename, linestr, tokenstr);
    goto punt;

  err_ipadd:
    eid = ACLERR1180;
    rv = arv;
    goto err_ret;

  err_dnsadd:
    eid = ACLERR1200;
    rv = arv;
    goto err_ret;

  err_ret:
    nserrGenerate(errp, rv, eid, ACL_Program, 0);
    goto punt;

  err_norp:
    eid = ACLERR1220;
  err_parse:
    rv = ACLERRPARSE;
    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program, 2, acf->acf_filename, linestr);
    goto punt;
}

/*
 * Description (aclAuthUsersParse)
 *
 *	This function parses a list of users and groups subject to
 *	authorization, adding the information to a specified UserSpec_t.
 *	The syntax it parses is:
 *
 *	auth-users ::= auth-user-elem | "(" auth-user-list ")"
 *	auth-user-elem ::= username | groupname
 *				    | "all" | "anyone"
 *	auth-user-list ::= auth-user-elem | auth-user-list "," auth-user-elem
 *
 *	If the 'elist' argument is non-null, an auth-user-list will be
 *	accepted without the enclosing parentheses.  Any invalid user
 *	or group names will not cause a fatal error, but will be returned
 *	in an array of strings via 'elist'.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	acf			- pointer to ACLFile_t for ACL file
 *	rlm			- pointer to authentication realm object
 *	uspp			- pointer to UserSpec_t pointer
 *	elist			- pointer to returned pointer to array
 *				  of strings containing invalid user or
 *				  group names (may be null)
 *
 * Returns:
 *
 *	If successful, the return value is the token type of the token
 *	following the auth-users, i.e. either the first token after a
 *	single auth-user-elem or the first token after the closing ")"
 *	of a list of auth-user-elems.  It is the caller's responsibility
 *	to validate this token as a legitimate successor of auth-users.
 *	If a parsing error occurs in the middle of auth-users, the return
 *	value is ACLERRPARSE, and an error frame is generated if an error
 *	list is provided.  For other kinds of errors a negative error
 *	code (from aclerror.h) is returned.
 */

int aclAuthUsersParse(NSErr_t * errp, ACLFile_t * acf,
		      Realm_t * rlm, UserSpec_t **uspp, char ***elist)
{
    void * token = acf->acf_token;	/* token handle */
    char * tokenstr;			/* token string pointer */
    UserSpec_t * usp;			/* user list head structure */
    int islist = 0;			/* true if auth-user-list */
    int inlist = 0;			/* true if UserSpec_t was supplied */
    int any = 0;			/* true if KEYWORD_ANY seen */
    int all = 0;			/* true if KEYWORD_ALL seen */
    int elemcnt = 0;			/* count of auth-user-elem seen */
    int elen = 0;			/* length of evec in (char *) */
    int ecnt = 0;			/* entries used in evec */
    char **evec = 0;			/* list of bad user/group names */
    int rv;				/* result value */
    int eid;				/* error id */
    char linestr[16];			/* line number string buffer */
    int errc = 2;

    usp = *uspp;
    if ((usp != 0) && (usp->us_flags & ACL_USALL)) all = 1;

    if (elist != 0) inlist = 1;
    else {

	/* Check for opening "(" */
	if (acf->acf_ttype == TOKEN_LPAREN) {

	    /* Looks like an auth-user-list */
	    islist = 1;

	    /* Step token to first auth-user-elem */
	    rv = aclGetToken(errp, acf, 0);
	    if (rv < 0) goto punt;
	}
    }

    /* Loop for each auth-user-elem */
    for (rv = acf->acf_ttype; ; rv = aclGetToken(errp, acf, 0)) {

	/* Looking for a user or group identifier */
	if ((rv == TOKEN_IDENT) || (rv == TOKEN_STRING)) {

	    /*
	     * If KEYWORD_ALL or KEYWORD_ANY has already appeared
	     * in this auth-spec, then return an error.
	     */
	    if (all | any) goto err_allany;

	    /* Check for reserved words */
	    tokenstr = lex_token(token);

	    /* KEYWORD_AT begins auth-hosts, but is invalid here */
	    if (!strcasecmp(tokenstr, KEYWORD_AT)) break;

	    /* Check for special group names */
	    if (!strcasecmp(tokenstr, KEYWORD_ANY)) {

		/*
		 * Any user, with no authentication needed.  This can
		 * only appear once in an auth-spec, and cannot be used
		 * in combination with KEYWORD_ALL (or any other user or
		 * group identifiers, but that will get checked before
		 * we return).
		 */

		if ((elemcnt > 0) || (usp != 0)) goto err_any;
		any = 1;
	    }
	    else if (!strcasecmp(tokenstr, KEYWORD_ALL)) {

		/*
		 * Any authenticated user.  This can only appear once in
		 * an auth-spec, and cannot be used in combination with
		 * KEYWORD_ANY (or any other user or group identifiers,
		 * but that will get checked before we return).
		 */

		if (elemcnt > 0) goto err_all;

		/* Create a UserSpec_t structure if we haven't got one yet */
		if (usp == 0) {
		    usp = aclUserSpecCreate();
		    if (usp == 0) goto err_nomem1;
		    *uspp = usp;
		}

		usp->us_flags |= ACL_USALL;
		all = 1;
	    }
	    else {

		/* Create a UserSpec_t structure if we haven't got one yet */
		if (usp == 0) {
		    usp = aclUserSpecCreate();
		    if (usp == 0) goto err_nomem2;
		    *uspp = usp;
		}

		/* This should be a user or group name */
		rv = aclAuthNameAdd(errp, usp, rlm, tokenstr);
		if (rv <= 0) {

		    /* The name was not found in the authentication DB */
		    if (elist != 0) {
			if (evec == 0) {
			    evec = (char **)MALLOC(4*sizeof(char *));
			    evec[0] = 0;
			    ecnt = 1;
			    elen = 4;
			}
			else if (ecnt >= elen) {
			    elen += 4;
			    evec = (char **)REALLOC(evec, elen*sizeof(char *));
			}
			evec[ecnt-1] = STRDUP(tokenstr);
			evec[ecnt] = 0;
			++ecnt;
			
		    }
		    else if (rv < 0) goto err_badgun;
		}

		/* Don't allow duplicate names */
		if (rv & ANA_DUP) {
		    if (elist == 0) goto err_dupgun;
		}
	    }

	    /* Count number of auth-user-elems seen */
	    elemcnt += 1;

	    /* Get the token after the auth-user-elem */
	    rv = aclGetToken(errp, acf, 0);
	    if (rv < 0) goto punt;
	}

	/* If this is a list, we need a "," to keep going */
	if (!(islist | inlist) || (rv != TOKEN_COMMA)) break;
    }

    /* Were we parsing an auth-user-list? */
    if (islist) {

	/* Yes, check for closing ")" */
	if (acf->acf_ttype != TOKEN_RPAREN) goto err_norp;

	/* Got it.  Step to next token for caller. */
	rv = aclGetToken(errp, acf, 0);
	if (rv < 0) goto punt;
    }

    /*
     * If we didn't see any auth-user-elems, then the auth-user we were
     * called to parse is missing.  We will forgive and forget if the
     * current token is a comma, however, so as to allow empty auth-specs.
     */
    if ((elemcnt <= 0) && (rv != TOKEN_COMMA)) {
	goto err_noelem;
    }

  punt:
    /* Return list of bad names if indicated */
    if (elist != 0) *elist = evec;

    return rv;

  err_badgun:
    /* Encountered an unknown user or group name */
    eid = ACLERR1360;
    rv = ACLERRUNDEF;
    goto err_retgun;

  err_dupgun:
    /* A user or group name was specified multiple times */
    eid = ACLERR1380;
    rv = ACLERRDUPSYM;
    goto err_retgun;

  err_retgun:
    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program,
		  3, acf->acf_filename, linestr, tokenstr);
    goto punt;

  err_norp:
    /* Missing ")" */
    eid = ACLERR1400;
    goto err_parse;

  err_noelem:
    eid = ACLERR1420;
    goto err_parse;

  err_all:
    eid = ACLERR1440;
    goto err_parse;

  err_any:
    eid = ACLERR1460;
    goto err_parse;

  err_allany:
    eid = ACLERR1480;
    goto err_parse;

  err_nomem1:
    eid = ACLERR1500;
    rv = ACLERRNOMEM;
    errc = 0;
    goto err_ret;

  err_nomem2:
    eid = ACLERR1520;
    rv = ACLERRNOMEM;
    errc = 0;
    goto err_ret;

  err_parse:
    rv = ACLERRPARSE;
  err_ret:
    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program, errc, acf->acf_filename, linestr);
    goto punt;
}

/*
 * Description (aclDirectivesParse)
 *
 *	This function parses the directives inside an ACL definition.
 *	The syntax for a directive list is:
 *
 *	dir-list ::= directive | dir-list ";" directive
 *	directive ::= auth-directive | access-directive | exec-directive
 *	auth-directive ::= dir-force "authenticate" ["in" realm-spec]
 *	access-directive ::= dir-force dir-access auth-list
 *	exec-directive ::= dir-force "execute" ["if" exec-optlist]
 *	exec-optlist ::= exec-condition | exec-optlist "," exec-condition
 *	exec-condition ::= dir-access | "authenticate"
 *	dir-force ::= "Always" | "Default"
 *	dir-access ::= "allow" | "deny"
 *
 *	See aclAuthListParse() for auth-list syntax.
 *	See aclRealmSpecParse() for realm-spec syntax.
 *
 *	The caller provides a pointer to an ACL structure, which is
 *	built up with new information as directives are parsed.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	acf			- pointer to ACLFile_t for ACL file
 *	acl			- pointer to ACL structure
 *
 * Returns:
 *
 *	If successful, the return value is the token type of the token
 *	following the directive list, i.e. the first token which is not
 *	recognized as the start of a directive.  It is the caller's
 *	responsibility to validate this token as a legitimate terminator
 *	of a directive list.  If a parsing error occurs in the middle of
 *	a directive, the return value is ACLERRPARSE, and an error frame
 *	is generated if an error list is provided.  For other kinds of
 *	errors a negative error code (from aclerror.h) is returned.
 */

int aclDirectivesParse(NSErr_t * errp, ACLFile_t * acf, ACL_t * acl)
{
    void * token = acf->acf_token;	/* token handle */
    char * tokenstr;			/* token string */
    Realm_t * rlm = 0;			/* current realm pointer */
    ACDirective_t * acd;		/* directive pointer */
    int action;				/* directive action code */
    int flags;				/* directive action flags */
    int arv;				/* alternate return value */
    int rv;				/* result value */
    int eid;				/* error id */
    char linestr[16];			/* line number string buffer */

    /* Look for top-level directives */
    for (rv = acf->acf_ttype; ; rv = aclGetToken(errp, acf, 0)) {

	action = 0;
	flags = 0;

	/* Check for beginning of directive */
	if (rv == TOKEN_IDENT) {

	    /* Check identifier for directive dir-force keywords */
	    tokenstr = lex_token(token);

	    if (!strcasecmp(tokenstr, KEYWORD_DEFAULT)) {
		flags = ACD_DEFAULT;
	    }
	    else if (!strcasecmp(tokenstr, "always")) {
		flags = ACD_ALWAYS;
	    }
	    else break;

	    /*
	     * Now we're looking for dir-access, "authenticate",
	     * or "execute".
	     */
	    rv = aclGetToken(errp, acf, 0);

	    /* An identifier would be nice ... */
	    if (rv != TOKEN_IDENT) goto err_access;

	    tokenstr = lex_token(token);

	    if (!strcasecmp(tokenstr, KEYWORD_AUTH)) {

		/* process auth-directive */
		action = ACD_AUTH;

		/* Create a new directive object */
		acd = aclDirectiveCreate();
		if (acd == 0) goto err_nomem1;

		/* Get the next token after KEYWORD_AUTH */
		rv = aclGetToken(errp, acf, 0);
		if (rv < 0) break;

		/* Could we have "in" realm-spec here? */
		if (rv == TOKEN_IDENT) {

		    tokenstr = lex_token(token);

		    if (!strcasecmp(tokenstr, KEYWORD_IN)) {

			/* Get the next token after KEYWORD_IN */
			rv = aclGetToken(errp, acf, 0);
			if (rv < 0) break;

			/* Parse the realm-spec */
			rv = aclRealmSpecParse(errp, acf, acl->acl_acc,
					       &acd->acd_auth.au_realm);
			if (rv < 0) break;

			/* Set current realm */
			if (acd->acd_auth.au_realm != 0) {

			    /* Close database in current realm if any */
			    if (rlm && rlm->rlm_authdb) {
				(*rlm->rlm_aif->aif_close)(rlm->rlm_authdb, 0);
				rlm->rlm_authdb = 0;
			    }

			    rlm = &acd->acd_auth.au_realm->rs_realm;
			}
		    }
		}

		/* Add this directive to the ACL */
		acd->acd_action = action;
		acd->acd_flags = flags;

		arv = aclDirectiveAdd(acl, acd);
		if (arv < 0) goto err_diradd1;
	    }
	    else if (!strcasecmp(tokenstr, KEYWORD_EXECUTE)) {

		/* process exec-directive */
		action = ACD_EXEC;

		/* Create a new directive object */
		acd = aclDirectiveCreate();
		if (acd == 0) goto err_nomem3;

		/* Get the next token after KEYWORD_EXECUTE */
		rv = aclGetToken(errp, acf, 0);
		if (rv < 0) break;

		/* Could we have "if" exec-optlist here? */
		if (rv == TOKEN_IDENT) {

		    tokenstr = lex_token(token);

		    if (!strcasecmp(tokenstr, KEYWORD_IF)) {

			for (;;) {

			    /* Get the next token after KEYWORD_IF or "," */
			    rv = aclGetToken(errp, acf, 0);
			    if (rv < 0) break;

			    /*
			     * Looking for "allow", "deny", or "authenticate"
			     */
			    if (rv == TOKEN_IDENT) {

				tokenstr = lex_token(token);

				if (!strcasecmp(tokenstr, KEYWORD_ALLOW)) {
				    flags |= ACD_EXALLOW;
				}
				else if (!strcasecmp(tokenstr, KEYWORD_DENY)) {
				    flags |= ACD_EXDENY;
				}
				else if (!strcasecmp(tokenstr, KEYWORD_AUTH)) {
				    flags |= ACD_EXAUTH;
				}
				else goto err_exarg;
			    }

			    /* End of directive if no comma */
			    rv = aclGetToken(errp, acf, 0);
			    if (rv < 0) break;

			    if (rv != TOKEN_COMMA) break;
			}
		    }
		}
		else flags = (ACD_EXALLOW|ACD_EXDENY|ACD_EXAUTH);

		if (rv < 0) break;

		/* Add this directive to the ACL */
		acd->acd_action = action;
		acd->acd_flags = flags;

		arv = aclDirectiveAdd(acl, acd);
		if (arv < 0) goto err_diradd3;
	    }
	    else {

		/* process access-directive */

		if (!strcasecmp(tokenstr, KEYWORD_ALLOW)) {
		    action = ACD_ALLOW;
		}
		else if (!strcasecmp(tokenstr, KEYWORD_DENY)) {
		    action = ACD_DENY;
		}
		else goto err_acctype;

		/* Get the next token after dir-access */
		rv = aclGetToken(errp, acf, 0);

		/* Create a new directive object */
		acd = aclDirectiveCreate();
		if (acd == 0) goto err_nomem2;

		/* Parse a list of auth-specs */
		rv = aclAuthListParse(errp, acf, acl->acl_acc, rlm,
				      &acd->acd_cl);
		if (rv < 0) break;

		/* Add this directive to the ACL */
		acd->acd_action = action;
		acd->acd_flags = flags;

		arv = aclDirectiveAdd(acl, acd);
		if (arv < 0) goto err_diradd2;
	    }
	}

	/* Need a ";" to keep going */
	if (rv != TOKEN_EOS) break;
    }

  punt:
    /* Close database in current realm if any */
    if (rlm && rlm->rlm_authdb) {
	(*rlm->rlm_aif->aif_close)(rlm->rlm_authdb, 0);
	rlm->rlm_authdb = 0;
    }

    return rv;

  err_access:
    /* dir-access not present */
    eid = ACLERR1600;
    rv = ACLERRPARSE;
    goto err_ret;

  err_acctype:
    /* dir-access identifier is invalid */
    eid = ACLERR1620;
    rv = ACLERRPARSE;
    goto err_ret;

  err_diradd1:
    eid = ACLERR1640;
    rv = arv;
    tokenstr = 0;
    goto err_ret;

  err_diradd2:
    eid = ACLERR1650;
    rv = arv;
    tokenstr = 0;
    goto err_ret;

  err_nomem1:
    eid = ACLERR1660;
    rv = ACLERRNOMEM;
    tokenstr = 0;
    goto err_ret;

  err_nomem2:
    eid = ACLERR1680;
    rv = ACLERRNOMEM;
    tokenstr = 0;
    goto err_ret;

  err_nomem3:
    eid = ACLERR1685;
    rv = ACLERRNOMEM;
    tokenstr = 0;
    goto err_ret;

  err_diradd3:
    eid = ACLERR1690;
    rv = arv;
    tokenstr = 0;
    goto err_ret;

  err_exarg:
    eid = ACLERR1695;
    rv = ACLERRSYNTAX;
    goto err_ret;

  err_ret:
    sprintf(linestr, "%d", acf->acf_lineno);
    if (tokenstr) {
	nserrGenerate(errp, rv, eid, ACL_Program,
		      3, acf->acf_filename, linestr, tokenstr);
    }
    else {
	nserrGenerate(errp, rv, eid, ACL_Program,
		      2, acf->acf_filename, linestr);
    }
    goto punt;
}

/*
 * Description (aclACLParse)
 *
 *	This function parses a data stream containing ACL definitions,
 *	and builds a representation of the ACLs in memory.  Each ACL
 *	has a user-specified name, and a pointer to the ACL structure
 *	is stored under the name in a symbol table provided by the caller.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	acf			- pointer to ACLFile_t for ACL file
 *	acc			- pointer to ACContext_t structure
 *	flags			- bit flags (unused - must be zero)
 *
 * Returns:
 *
 *	The return value is zero if the stream is parsed successfully.
 *	Otherwise it is a negative error code (ACLERRxxxx - see aclerror.h),
 *	and an error frame will be generated if an error list is provided.
 */

int aclACLParse(NSErr_t * errp, ACLFile_t * acf, ACContext_t * acc, int flags)
{
    void * token = acf->acf_token;	/* handle for current token */
    char * tokenstr;			/* current token string */
    char * aclname;			/* ACL name string */
    ACL_t * aclp;			/* pointer to ACL structure */
    int rv;				/* result value */
    int eid;				/* error id value */
    char linestr[16];			/* line number string buffer */

    /* Look for top-level statements */
    for (;;) {

	/* Get a token to begin a statement */
	rv = aclGetToken(errp, acf, 0);

	/* An identifier would be nice ... */
	if (rv != TOKEN_IDENT) {

	    /* Empty statements are ok, if pointless */
	    if (rv == TOKEN_EOS) continue;

	    /* EOF is valid here */
	    if (rv == TOKEN_EOF) break;

	    /* Anything else is unacceptable */
	    goto err_nostmt;
	}

	/* Check identifier for statement keywords */
	tokenstr = lex_token(token);

	if (!strcasecmp(tokenstr, KEYWORD_ACL)) {

	    /* ACL name rights-list { acl-def-list }; */

	    /* Get the name of the ACL */
	    rv = aclGetToken(errp, acf, 0);
	    if (rv != TOKEN_IDENT) goto err_aclname;
	    aclname = lex_token(token);

	    /* Create the ACL structure */
	    rv = aclCreate(errp, acc, aclname, &aclp);
	    if (rv < 0) goto punt;

	    /* Get the next token after the ACL name */
	    rv = aclGetToken(errp, acf, 0);

	    /* Parse the rights specification */
	    rv = aclRightsParse(errp, acf, acc, &aclp->acl_rights);

	    /* Want a "{" to open the ACL directive list */
	    if (rv != TOKEN_LBRACE) {
		if (rv < 0) goto punt;
		goto err_aclopen;
	    }

	    /* Get the first token in the ACL directive list */
	    rv = aclGetToken(errp, acf, 0);
	    if (rv < 0) goto punt;

	    /* Parse the ACL directive list */
	    rv = aclDirectivesParse(errp, acf, aclp);

	    /* Want a "}" to close the ACL directive list */
	    if (rv != TOKEN_RBRACE) {
		if (rv < 0) goto punt;
		goto err_aclclose;
	    }
	}
	else if (!strcasecmp(tokenstr, KEYWORD_INCLUDE)) {
	    /* Include "filename"; */
	}
	else if (!strcasecmp(tokenstr, KEYWORD_REALM)) {
	    /* Realm name realm-spec */
	}
	else if (!strcasecmp(tokenstr, KEYWORD_RIGHTS)) {
	    /* Rights name rights-def; */
	}
	else if (!strcasecmp(tokenstr, KEYWORD_HOSTS)) {
	    /* Hosts name auth-hosts; */
	}
	else goto err_syntax;
    }

    return 0;

  err_nostmt:
    eid = ACLERR1700;
    rv = ACLERRPARSE;
    goto err_ret;

  err_aclname:
    eid = ACLERR1720;
    rv = ACLERRPARSE;
    goto err_ret;

  err_aclopen:
    eid = ACLERR1740;
    rv = ACLERRPARSE;
    goto err_ret;

  err_aclclose:
    eid = ACLERR1760;
    rv = ACLERRPARSE;
    goto err_ret;

  err_ret:
    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program, 2, acf->acf_filename, linestr);
    goto punt;

  err_syntax:
    eid = ACLERR1780;
    rv = ACLERRPARSE;
    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program,
		  3, acf->acf_filename, linestr, tokenstr);

  punt:
    return rv;
}

/*
 * Description (aclFileClose)
 *
 *	This function closes an ACL file previously opened by aclFileOpen(),
 *	and frees any associated data structures.
 *
 * Arguments:
 *
 *	acf			- pointer to ACL file information
 *	flags			- bit flags (unused - must be zero)
 */

void aclFileClose(ACLFile_t * acf, int flags)
{
    if (acf != 0) {

	/* Destroy the associated lexer stream if any */
	if (acf->acf_lst != 0) {
	    lex_stream_destroy(acf->acf_lst);
	}

	/* Close the file if it's open */
	if (acf->acf_fd != SYS_ERROR_FD) {
	    system_fclose(acf->acf_fd);
	}

	/* Destroy any associated token */
	if (acf->acf_token != 0) {
	    lex_token_destroy(acf->acf_token);
	}

	/* Free the filename string if any */
	if (acf->acf_filename != 0) {
	    FREE(acf->acf_filename);
	}

	/* Free the ACLFile_t structure */
	FREE(acf);
    }
}

/*
 * Description (aclFileOpen)
 *
 *	This function opens a specified filename and creates a structure
 *	to contain information about the file during parsing.  This
 *	includes a handle for a LEX data stream for the file.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	filename		- name of file to be opened
 *	flags			- bit flags (unused - must be zero)
 *	pacf			- pointer to returned ACLFile_t pointer
 *
 * Returns:
 *
 *	The return value is zero if the file is opened successfully, and
 *	a pointer to the ACLFile_t is returned in the location specified
 *	by 'pacf'.  Otherwise a negative error code (ACLERRxxxx - see
 *	aclerror.h) is returned, and an error frame will be generated if
 *	an error list is provided.
 */

int aclFileOpen(NSErr_t * errp,
		char * filename, int flags, ACLFile_t **pacf)
{
    ACLFile_t * acf;		/* pointer to ACL file structure */
    int rv;			/* return value */
    int eid;			/* error identifier */
    char * errmsg;		/* system error message string */

    *pacf = 0;

    /* Allocate the ACLFile_t structure */
    acf = (ACLFile_t *)MALLOC(sizeof(ACLFile_t));
    if (acf == 0) goto err_nomem1;

    memset((void *)acf, 0, sizeof(ACLFile_t));
    acf->acf_filename = STRDUP(filename);
    acf->acf_lineno = 1;
    acf->acf_flags = flags;

    /* Create a LEX token object */
    rv = lex_token_new((pool_handle_t *)0, 32, 8, &acf->acf_token);
    if (rv < 0) goto err_nomem2;

    /* Open the file */
    acf->acf_fd = system_fopenRO(acf->acf_filename);
    if (acf->acf_fd == SYS_ERROR_FD) goto err_open;

    /* Create a LEX stream for the file */
    acf->acf_lst = lex_stream_create(aclStreamGet,
				     (void *)acf->acf_fd, 0, 8192);
    if (acf->acf_lst == 0) goto err_nomem3;

    *pacf = acf;
    return 0;

  err_open:				/* file open error */
    rv = ACLERROPEN;
    eid = ACLERR1900;
    errmsg = system_errmsg();
    nserrGenerate(errp, rv, eid, ACL_Program, 2, filename, errmsg);
    goto punt;

  err_nomem1:				/* MALLOC of ACLFile_t failed */
    rv = ACLERRNOMEM;
    eid = ACLERR1920;
    goto err_mem;

  err_nomem2:				/* lex_token_new() failed */
    rv = ACLERRNOMEM;
    eid = ACLERR1940;
    goto err_mem;

  err_nomem3:				/* lex_stream_create() failed */
    system_fclose(acf->acf_fd);
    rv = ACLERRNOMEM;
    eid = ACLERR1960;

  err_mem:
    nserrGenerate(errp, rv, eid, ACL_Program, 0);
    goto punt;

  punt:
    return rv;
}

/*
 * Description (aclGetDNSString)
 *
 *	This function parses a DNS name specification, which consists
 *	of a sequence of DNS name components separated by ".".  Each
 *	name component must start with a letter, and contains only
 *	letters, digits, and hyphens.  An exception is that the first
 *	component may be the wildcard indicator, "*".  This function
 *	assumes that the current token already contains a TOKEN_STAR
 *	or TOKEN_IDENT.  The complete DNS name specification is
 *	returned as the current token string.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	acf			- pointer to ACLFile_t for ACL file
 *
 * Returns:
 *
 *	The character terminating the DNS name specification is returned
 *	as the function value.  The current token type is unchanged, but
 *	the string associated with the current token contains the
 *	complete DNS name specification.  An error is indicated by a
 *	negative return value, and an error frame is generated if an
 *	error list is provided.
 */

int aclGetDNSString(NSErr_t * errp, ACLFile_t * acf)
{
    LEXStream_t * lst = acf->acf_lst;	/* LEX stream handle */
    void * token = acf->acf_token;	/* LEX token handle */
    int rv;				/* result value */
    int eid;				/* error id value */
    char linestr[16];			/* line number string buffer */

    /* The current token should be TOKEN_STAR or TOKEN_IDENT */
    rv = acf->acf_ttype;

    if ((rv != TOKEN_STAR) && (rv != TOKEN_IDENT)) goto err_dns1;

    /* Loop to parse [ "." dns-component ]+ */
    for (;;) {

	/* Try to step over a "." */
	rv = lex_next_char(lst, aclChTab, 0);

	/* End of DNS string if there's not one there */
	if (rv != '.') break;

	/* Append the "." to the token string */
	(void)lex_token_append(token, 1, ".");

	/* Advance the input stream past the "." */
	rv = lex_next_char(lst, aclChTab, CCM_SPECIAL);

	/* Next we want to see a letter */
	rv = lex_next_char(lst, aclChTab, 0);

	/* Error if it's not there */
	if (!lex_class_check(aclChTab, rv, CCM_LETTER)) goto err_dns2;

	/* Append a string of letters, digits, hyphens to token */
	rv = lex_scan_over(lst, aclChTab, (CCM_LETTER|CCM_DIGIT|CCM_HYPHEN),
			   token);
	if (rv < 0) goto err_dns3;
    }

  punt:
    return rv;

  err_dns1:
    eid = ACLERR2100;
    rv = ACLERRPARSE;
    goto err_ret;

  err_dns2:
    eid = ACLERR2120;
    rv = ACLERRPARSE;
    goto err_ret;

  err_dns3:
    eid = ACLERR2140;
    rv = ACLERRPARSE;
    goto err_ret;

  err_ret:
    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program, 2, acf->acf_filename, linestr);
    goto punt;
}

int aclGetFileSpec(NSErr_t * errp, ACLFile_t * acf, int flags)
{
    LEXStream_t * lst = acf->acf_lst;	/* LEX stream handle */
    void * token = acf->acf_token;	/* LEX token handle */
    char * tokenstr;			/* token string pointer */
    int rv;				/* result value */
    int eid;				/* error id value */
    char linestr[16];			/* line number string buffer */

    /* Skip whitespace */
    rv = lex_skip_over(lst, aclChTab, CCM_WS);
    if (rv < 0) goto err_lex1;

    /* Begin a new token string */
    rv = lex_token_start(token);

    rv = lex_scan_over(lst, aclChTab, CCM_FILENAME, token);
    if (rv < 0) goto err_lex2;

    tokenstr = lex_token(token);

    if (!tokenstr || !*tokenstr) goto err_nofn;

  punt:
    return rv;

  err_lex1:
    eid = ACLERR2900;
    goto err_parse;

  err_lex2:
    eid = ACLERR2920;
    goto err_parse;

  err_nofn:
    eid = ACLERR2940;

  err_parse:
    rv = ACLERRPARSE;
    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program, 2, acf->acf_filename, linestr);
    goto punt;
}

/*
 * Description (aclGetIPAddr)
 *
 *	This function retrieves an IP address specification from a given
 *	input stream.  The specification consists of an IP address expressed
 *	in the standard "." notation, possibly followed by whitespace and a
 *	netmask, also in "." form.  The IP address and netmask values are
 *	returned.  If no netmask is specified, a default value of 0xffffffff
 *	is returned.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	acf			- pointer to ACLFile_t for ACL file
 *	pip			- pointer to returned IP address value
 *	pmask			- pointer to returned IP netmask value
 *
 * Returns:
 *
 *	If successful, the return value identifies the type of the token
 *	following the IP address specification. This token type value is
 *	also returned in acf_ttype.  An error is indicated by a negative
 *	error code (ACLERRxxxx - see aclerror.h), and an error frame will
 *	be generated if an error list is provided. The token type code in
 *	acf_ttype is TOKEN_ERROR when an error code is returned.
 */

int aclGetIPAddr(NSErr_t * errp,
		 ACLFile_t * acf, IPAddr_t * pip, IPAddr_t * pmask)
{
    LEXStream_t * lst = acf->acf_lst;	/* LEX stream handle */
    void * token = acf->acf_token;	/* LEX token handle */
    char * tokenstr;			/* token string pointer */
    IPAddr_t ipaddr;			/* IP address */
    IPAddr_t netmask;			/* IP netmask */
    int dotcnt;				/* count of '.' seen in address */
    int rv;				/* result value */
    int eid;				/* error id value */
    char linestr[16];			/* line number string buffer */

    /* Set default return values */
    *pip = 0;
    *pmask = 0xffffffff;

    rv = acf->acf_ttype;

    /* The current token must be a number */
    if (rv != TOKEN_NUMBER) {

	/* No IP address present */
	return rv;
    }

    /* Assume no netmask */
    netmask = 0xffffffff;

    for (dotcnt = 0;;) {

	/* Append digits and letters to the current token */
	rv = lex_scan_over(lst, aclChTab, (CCM_DIGIT|CCM_LETTER), token);
	if (rv < 0) goto err_lex1;

	/* Stop when no "." follows the digits and letters */
	if (rv != '.') break;

	/* Stop if we've already seen three "." */
	if (++dotcnt > 3) break;

	/* Advance past the "." */
	(void)lex_next_char(lst, aclChTab, CCM_SPECIAL);

	/* Check the next character for a "*" */
	rv = lex_next_char(lst, aclChTab, 0);
	if (rv == '*') {

	    /* Advance past the "*" */
	    (void)lex_next_char(lst, aclChTab, CCM_SPECIAL);

	    netmask <<= ((4-dotcnt)*8);
	    netmask = htonl(netmask);

	    while (dotcnt < 4) {
		(void)lex_token_append(token, 2, ".0");
		++dotcnt;
	    }
	    break;
	}
	else {
	    /* Append the "." to the token string */
	    (void)lex_token_append(token, 1, ".");
	}
    }

    /* Get a pointer to the token string */
    tokenstr = lex_token(token);

    /* A NULL pointer or an empty string is an error */
    if (!tokenstr || !*tokenstr) goto err_noip;
	
    /* Convert IP address to binary */
    ipaddr = inet_addr(tokenstr);
    if (ipaddr == (unsigned long)-1) goto err_badip;

    /* Skip whitespace */
    rv = lex_skip_over(lst, aclChTab, CCM_WS);
    if (rv < 0) goto err_lex2;

    /* A digit is the start of a netmask */
    if ((netmask == 0xffffffff) && lex_class_check(aclChTab, rv, CCM_DIGIT)) {

	/* Initialize token for network mask */
	rv = lex_token_start(token);

	for (dotcnt = 0;;) {

	    /* Collect token including digits, letters, and periods */
	    rv = lex_scan_over(lst, aclChTab, (CCM_DIGIT|CCM_LETTER), token);
	    if (rv < 0) goto err_lex3;

	    /* Stop when no "." follows the digits and letters */
	    if (rv != '.') break;

	    /* Stop if we've already seen three "." */
	    if (++dotcnt > 3) break;

	    /* Append the "." to the token string */
	    (void)lex_token_append(token, 1, ".");

	    /* Advance past the "." */
	    (void)lex_next_char(lst, aclChTab, CCM_SPECIAL);
	}

	/* Get a pointer to the token string */
	tokenstr = lex_token(token);

	/* A NULL pointer or an empty string is an error */
	if (!tokenstr || !*tokenstr) goto err_nonm;

	/* Convert netmask to binary. */
	netmask = inet_addr(tokenstr);
	if (netmask == (unsigned long)-1) {
	    
	    /*
	     * Unfortunately inet_addr() doesn't distinguish between an
	     * error and a valid conversion of "255.255.255.255".  So
	     * we check for it explicitly.  Too bad if "0xff.0xff.0xff.0xff"
	     * is specified.  Don't do that!
	     */
	    if (strcmp(tokenstr, "255.255.255.255")) goto err_badnm;
	}
    }

    /* Return the IP address and netmask in host byte order */
    *pip = ntohl(ipaddr);
    *pmask = ntohl(netmask);

    /* Get the token following the IP address (and netmask) */
    rv = aclGetToken(errp, acf, 0);

  punt:
    acf->acf_ttype = (rv < 0) ? TOKEN_ERROR : rv;
    return rv;

  err_lex1:
    eid = ACLERR2200;
    rv = ACLERRPARSE;
    goto err_ret;

  err_lex2:
    eid = ACLERR2220;
    rv = ACLERRPARSE;
    goto err_ret;

  err_lex3:
    eid = ACLERR2240;
    rv = ACLERRPARSE;
    goto err_ret;

  err_noip:
    eid = ACLERR2260;
    rv = ACLERRPARSE;
    goto err_ret;

  err_badip:
    eid = ACLERR2280;
    rv = ACLERRPARSE;
    goto err_ret;

  err_nonm:
    eid = ACLERR2300;
    rv = ACLERRPARSE;
    goto err_ret;

  err_badnm:
    eid = ACLERR2320;
    rv = ACLERRPARSE;
    goto err_ret;

  err_ret:
    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program, 2, acf->acf_filename, linestr);
    goto punt;
}

/*
 * Description (aclGetToken)
 *
 *	This function retrieves the next token in an ACL definition file.
 *	It skips blank lines, comments, and white space.  It updates
 *	the current line number as newlines are encountered.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	acf			- pointer to ACLFile_t for ACL file
 *	flags			- bit flags:
 *				  AGT_NOSKIP - don't skip leading whitespace
 *				  AGT_APPEND - append to token buffer
 *					       (else start new token)
 *
 * Returns:
 *
 *	The return value is a code identifying the next token if successful.
 *	This token type value is also returned in acf_ttype.  An error
 *	is indicated by a negative error code (ACLERRxxxx - see aclerror.h),
 *	and an error frame will be generated if an error list is provided.
 *	The token type code in acf_ttype is TOKEN_ERROR when an error code
 *	is returned.
 */

int aclGetToken(NSErr_t * errp, ACLFile_t * acf, int flags)
{
    LEXStream_t * lst = acf->acf_lst;	/* LEX stream handle */
    void * token = acf->acf_token;	/* LEX token handle */
    int dospecial = 0;			/* handle CCM_SPECIAL character */
    int tv;				/* token value */
    int rv;				/* result value */
    int eid;				/* error id */
    char spech;
    char linestr[16];			/* line number string buffer */

    /* Begin a new token, unless AGT_APPEND is set */
    if (!(flags & AGT_APPEND)) {
	rv = lex_token_start(token);
    }

    /* Loop to read file */
    tv = 0;
    do {

	/*
	 * If the AGT_NOSKIP flag is not set, skip whitespace (but not
	 * newline).  If the flag is set, just get the next character.
	 */
	rv = lex_skip_over(lst, aclChTab, (flags & AGT_NOSKIP) ? 0 : CCM_WS);
	if (rv <= 0) {
	    if (rv < 0) goto err_lex1;

	    /* Exit loop if EOF */
	    if (rv == 0) {
		tv = TOKEN_EOF;
		break;
	    }
	}

	/* Analyze character after whitespace */
	switch (rv) {

	  case '\n':		/* newline */

	    /* Keep count of lines as we're skipping whitespace */
	    acf->acf_lineno += 1;
	    (void)lex_next_char(lst, aclChTab, CCM_NL);
	    break;

	  case '#':		/* Beginning of comment */

	    /* Skip to a newline if so */
	    rv = lex_skip_to(lst, aclChTab, CCM_NL);
	    break;

	  case ';':		/* End of statement */
	    tv = TOKEN_EOS;
	    dospecial = 1;
	    break;

	  case '@':		/* at sign */
	    tv = TOKEN_AT;
	    dospecial = 1;
	    break;

	  case '+':		/* plus sign */
	    tv = TOKEN_PLUS;
	    dospecial = 1;
	    break;

	  case '*':		/* asterisk */
	    tv = TOKEN_STAR;
	    dospecial = 1;
	    break;

	  case '.':		/* period */
	    tv = TOKEN_PERIOD;
	    dospecial = 1;
	    break;

	  case ',':		/* comma */
	    tv = TOKEN_COMMA;
	    dospecial = 1;
	    break;

	  case '(':		/* left parenthesis */
	    tv = TOKEN_LPAREN;
	    dospecial = 1;
	    break;

	  case ')':		/* right parenthesis */
	    tv = TOKEN_RPAREN;
	    dospecial = 1;
	    break;

	  case '{':		/* left brace */
	    tv = TOKEN_LBRACE;
	    dospecial = 1;
	    break;

	  case '}':		/* right brace */
	    tv = TOKEN_RBRACE;
	    dospecial = 1;
	    break;

	  case '\"':		/* double quote */
	  case '\'':		/* single quote */

	    /* Append string contents to token buffer */
	    rv = lex_scan_string(lst, token, 0);
	    tv = TOKEN_STRING;
	    break;

	  default:

	    /* Check for identifier, beginning with a letter */
	    if (lex_class_check(aclChTab, rv, CCM_LETTER)) {

		/* Append valid identifier characters to token buffer */
		rv = lex_scan_over(lst, aclChTab, CCM_IDENT, token);
		tv = TOKEN_IDENT;
		break;
	    }

	    /* Check for a number, beginning with a digit */
	    if (lex_class_check(aclChTab, rv, CCM_DIGIT)) {
		char digit;

		/* Save the first digit */
		digit = (char)rv;

		/* Append the first digit to the token */
		rv = lex_token_append(token, 1, &digit);

		/* Skip over the first digit */
		rv = lex_next_char(lst, aclChTab, CCM_DIGIT);

		/* If it's '0', we might have "0x.." */
		if (rv == '0') {

		    /* Pick up the next character */
		    rv = lex_next_char(lst, aclChTab, 0);

		    /* Is it 'x'? */
		    if (rv == 'x') {

			/* Yes, append it to the token */
			digit = (char)rv;
			rv = lex_token_append(token, 1, &digit);

			/* Step over it */
			rv = lex_next_char(lst, aclChTab, CCM_LETTER);
		    }
		}
		/* Get more digits, if any */
		rv = lex_scan_over(lst, aclChTab, CCM_DIGIT, token);
		tv = TOKEN_NUMBER;
		break;
	    }

	    /* Unrecognized character */

	    spech = *lst->lst_cp;
	    lex_token_append(token, 1, &spech);
	    lst->lst_cp += 1;
	    lst->lst_len -= 1;
	    tv = TOKEN_HUH;
	    break;
	}

	/* Handle CCM_SPECIAL character? */
	if (dospecial) {

	    /* Yes, clear the flag for next time */
	    dospecial = 0;

	    /* Get the character and advance past it */
	    rv = lex_next_char(lst, aclChTab, CCM_SPECIAL);

	    /* Append the character to the token buffer */
	    spech = (char)rv;
	    (void)lex_token_append(token, 1, &spech);
	}
    }
    while ((tv == 0) && (rv > 0));

    if (rv < 0) {
	tv = TOKEN_ERROR;
    }
    else rv = tv;

    acf->acf_ttype = tv;
    return rv;

  err_lex1:
    rv = ACLERRPARSE;
    eid = ACLERR2400;

    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program, 2, acf->acf_filename, linestr);

    acf->acf_ttype = TOKEN_ERROR;
    return rv;
}

/*
 * Description (aclParseInit)
 *
 *	This function is called to initialize the ACL parser.  It
 *	creates a LEX character class table to assist in parsing.
 *
 * Arguments:
 *
 *	None.
 *
 * Returns:
 *
 *	If successful, the return value is zero.  An error is indicated
 *	by a negative return value.
 */

int aclParseInit()
{
    int rv;				/* result value */

    /* Have we created the character class table yet? */
    if (aclChTab == 0) {

	/* No, initialize character classes for lexer processing */
	rv = lex_class_create(classc, classv, &aclChTab);
	if (rv < 0) goto err_nomem;
    }

    return 0;

  err_nomem:
    return ACLERRNOMEM;
}

/*
 * Description (aclRealmSpecParse)
 *
 *	This function parses an authentication realm specification.  An
 *	authentication realm includes an authentication database and
 *	an authentication method.  The syntax of a realm-spec is:
 *
 *	realm-spec ::= "{" realm-directive-list "}" | "realm" realm-name
 *	realm-directive-list ::= realm-directive |
 *				 realm-directive-list ";" realm-directive
 *	realm-directive ::= realm-db-directive | realm-meth-directive
 *				| realm-prompt-directive
 *	realm-db-directive ::= "database" db-file-path
 *	realm-meth-directive ::= "method" auth-method-name
 *	auth-method-name ::= "basic" | "SSL"
 *	realm-prompt-directive ::= "prompt" quote-char string quote-char
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	acf			- pointer to ACLFile_t for ACL file
 *	acc			- pointer to ACContext_t structure
 *	rspp			- pointer to RealmSpec_t pointer
 *
 * Returns:
 *
 *	If successful, the return value is the token type of the token
 *	following the realm-spec, i.e. either the first token after a
 *	realm-name or the first token after the closing "}".  It is the
 *	caller's responsibility to validate this token as a legitimate
 *	successor of a realm-spec.  If a parsing error occurs in the
 *	middle of a realm-spec, the return value is ACLERRPARSE, and an
 *	error frame is generated if an error list is provided.  For
 *	other kinds of errors a negative error code (from aclerror.h)
 *	is returned.
 */

int aclRealmSpecParse(NSErr_t * errp,
		      ACLFile_t * acf, ACContext_t * acc, RealmSpec_t **rspp)
{
    void * token = acf->acf_token;	/* handle for current token */
    char * tokenstr;			/* current token string */
    RealmSpec_t * rsp;			/* realm spec pointer */
    RealmSpec_t * nrsp;			/* named realm spec pointer */
    int rv;				/* result value */
    int eid;				/* error id value */
    char linestr[16];			/* line number string buffer */

    rv = acf->acf_ttype;

    /* Is the current token a "{" ? */
    if (rv != TOKEN_LBRACE) {

	/* No, could it be KEYWORD_REALM? */
	if (rv == TOKEN_IDENT) {

	    tokenstr = lex_token(token);

	    if (!strcasecmp(tokenstr, KEYWORD_REALM)) {

		/* Yes, step to the realm name */
		rv = aclGetToken(errp, acf, 0);
		if (rv != TOKEN_IDENT) {
		    if (rv < 0) goto punt;
		    goto err_rlmname;
		}

		tokenstr = lex_token(token);

		/* Look up the named realm specification */
		rv = symTableFindSym(acc->acc_stp, tokenstr, ACLSYMREALM,
				     (void **)&nrsp);
		if (rv < 0) goto err_undrlm;

		/* Return the named realm specification */
		*rspp = nrsp;

		/* Step to the token after the realm name */
		rv = aclGetToken(errp, acf, 0);
	    }
	}

	return rv;
    }

    /* Step to the token after the "{" */
    rv = aclGetToken(errp, acf, 0);
    if (rv < 0) goto punt;

    rsp = *rspp;
    if (rsp == 0) {
	rsp = (RealmSpec_t *)MALLOC(sizeof(RealmSpec_t));
	if (rsp == 0) goto err_nomem;
	memset((void *)rsp, 0, sizeof(RealmSpec_t));
	rsp->rs_sym.sym_type = ACLSYMREALM;
	*rspp = rsp;
    }

    /* Loop for each realm-directive */
    for (;; rv = aclGetToken(errp, acf, 0)) {

	if (rv != TOKEN_IDENT) {

	    /* Exit loop on "}" */
	    if (rv == TOKEN_RBRACE) break;

	    /* Ignore null directives */
	    if (rv == TOKEN_EOS) continue;

	    /* Otherwise need an identifier to start a directive */
	    goto err_nodir;
	}

	tokenstr = lex_token(token);

	/* Figure out which realm-directive this is */
	if (!strcasecmp(tokenstr, KEYWORD_DATABASE)) {

	    /* Get a file specification for the database */
	    rv = aclGetToken(errp, acf, 0);
	    if (rv != TOKEN_STRING) {
		if (rv < 0) goto punt;
		goto err_nodb;
	    }

	    rsp->rs_realm.rlm_dbname = lex_token_take(token);
	    rsp->rs_realm.rlm_aif = &NSADB_AuthIF;
	}
	else if (!strcasecmp(tokenstr, KEYWORD_METHOD)) {

	    /* Step to the method identifier */
	    rv = aclGetToken(errp, acf, 0);
	    if (rv != TOKEN_IDENT) {
		if (rv < 0) goto punt;
		goto err_nometh;
	    }

	    tokenstr = lex_token(token);

	    /* Interpret method name and set method in realm structure */
	    if (!strcasecmp(tokenstr, KEYWORD_BASIC)) {
		rsp->rs_realm.rlm_ameth = AUTH_METHOD_BASIC;
	    }
	    else if (!strcasecmp(tokenstr, KEYWORD_SSL) && server_enterprise) {
		rsp->rs_realm.rlm_ameth = AUTH_METHOD_SSL;
	    }
	    else goto err_badmeth;
	}
	else if (!strcasecmp(tokenstr, KEYWORD_PROMPT)) {

	    /* Step to the realm prompt string */
	    rv = aclGetToken(errp, acf, 0);
	    if ((rv != TOKEN_STRING) && (rv != TOKEN_IDENT)) {
		if (rv < 0) goto punt;
		goto err_noprompt;
	    }

	    /* Reference a copy of the prompt string from the realm */
	    rsp->rs_realm.rlm_prompt = lex_token_take(token);
	}
	else goto err_baddir;

	/* Get the token after the realm-directive */
	rv = aclGetToken(errp, acf, 0);

	/* Need a ";" to keep going */
	if (rv != TOKEN_EOS) break;
    }

    if (rv != TOKEN_RBRACE) goto err_rbrace;

    /* Get the token after the realm-spec */
    rv = aclGetToken(errp, acf, 0);

  punt:
    return rv;

  err_rlmname:
    eid = ACLERR2500;
    goto err_parse;

  err_undrlm:
    eid = ACLERR2520;
    rv = ACLERRUNDEF;
    goto err_sym;

  err_nomem:
    eid = ACLERR2540;
    rv = ACLERRNOMEM;
    goto ret_err;

  err_nodir:
    eid = ACLERR2560;
    goto err_parse;

  err_nodb:
    eid = ACLERR2570;
    goto err_parse;

  err_nometh:
    eid = ACLERR2580;
    goto err_parse;

  err_badmeth:
    eid = ACLERR2600;
    goto err_sym;

  err_noprompt:
    eid = ACLERR2605;
    goto err_parse;

  err_baddir:
    eid = ACLERR2610;
    goto err_sym;

  err_rbrace:
    eid = ACLERR2620;
    goto err_parse;

  err_sym:
    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program,
		  3, acf->acf_filename, linestr, tokenstr);
    goto punt;

  err_parse:
    rv = ACLERRPARSE;
  ret_err:
    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program, 2, acf->acf_filename, linestr);
    goto punt;
}

/*
 * Description (aclRightsParse)
 *
 *	This function parse an access rights list.  The syntax for an
 *	access rights list is:
 *
 *	rights-list ::= "(" list-of-rights ")"
 *	list-of-rights ::= rights-elem | list-of-rights "," rights-elem
 *	rights-elem ::= right-name | "+" rights-def-name
 *	right-name ::= identifier
 *	rights-def-name ::= identifier
 *
 *	An element of a rights list is either the name of a particular
 *	access right (e.g. Read), or the name associated with an
 *	external definition of an access rights list, preceded by "+"
 *	(e.g. +editor-rights).  The list is enclosed in parentheses,
 *	and the elements are separated by commas.
 *
 *	This function adds to a list of rights provided by the caller.
 *	Access rights are internally assigned unique integer identifiers,
 *	and a symbol table is maintained to map an access right name to
 *	its identifier.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	acf			- pointer to ACLFile_t for ACL file
 *	acc			- pointer to ACContext_t structure
 *	rights			- pointer to rights list head
 *
 * Returns:
 *
 *	The return value is a code identifying the next token if successful.
 *	End-of-stream is indicated by a return value of TOKEN_EOF.  An error
 *	is indicated by a negative error code (ACLERRxxxx - see aclerror.h),
 *	and an error frame will be generated if an error list is provided.
 */

int aclRightsParse(NSErr_t * errp, ACLFile_t * acf, ACContext_t * acc,
		   RightSpec_t **rights)
{
    void * token = acf->acf_token;	/* LEX token handle */
    char * ename;			/* element name string pointer */
    RightSpec_t * rsp;			/* rights specification pointer */
    RightSpec_t * nrsp;			/* named rights spec pointer */
    RightDef_t * rdp;			/* right definition pointer */
    int rv;				/* result value */
    int eid;				/* error id */
    char linestr[16];			/* line number string buffer */

    /* Look for a left parenthesis */
    if (acf->acf_ttype != TOKEN_LPAREN) {

	/* No rights list present */
	return 0;
    }

    rsp = *rights;

    /* Create a RightSpec_t if we don't have one */
    if (rsp == 0) {
	rsp = (RightSpec_t *)MALLOC(sizeof(RightSpec_t));
	if (rsp == 0) goto err_nomem1;
	memset((void *)rsp, 0, sizeof(RightSpec_t));
	rsp->rs_sym.sym_type = ACLSYMRDEF;
	*rights = rsp;
    }

    /* Parse list elements */
    for (;;) {

	/* Look for an identifier */
	rv = aclGetToken(errp, acf, 0);
	if (rv != TOKEN_IDENT) {

	    /* No, maybe a "+" preceding a rights definition name? */
	    if (rv != TOKEN_PLUS) {

		/* One more chance, we'll allow the closing ")" after "," */
		if (rv != TOKEN_RPAREN) {
		    /* No, bad news */
		    if (rv < 0) goto punt;
		    goto err_elem;
		}

		/* Got right paren after comma */
		break;
	    }

	    /* Got a "+", try for the rights definition name */
	    rv = aclGetToken(errp, acf, 0);
	    if (rv != TOKEN_IDENT) {
		if (rv < 0) goto punt;
		goto err_rdef;
	    }

	    /* Get a pointer to the token string */
	    ename = lex_token(token);

	    /* See if it matches a rights definition in the symbol table */
	    rv = symTableFindSym(acc->acc_stp, ename, ACLSYMRDEF,
				 (void **)&nrsp);
	    if (rv) goto err_undef;

	    /*
	     * Merge the rights from the named rights list into the
	     * current rights list.
	     */
	    rv = uilMerge(&rsp->rs_list, &nrsp->rs_list);
	    if (rv < 0) goto err_nomem2;
	}
	else {

	    /* The current token is an access right name */

	    /* Get a pointer to the token string */
	    ename = lex_token(token);


	    /* Find or create an access right definition */
	    rv = aclRightDef(errp, acc, ename, &rdp);
	    if (rv < 0) goto err_radd;

	    /* Add the id for this right to the current rights list */
	    rv = usiInsert(&rsp->rs_list, rdp->rd_id);
	    if (rv < 0) goto err_nomem3;
	}

	rv = aclGetToken(errp, acf, 0);

	/* Want a comma to continue the list */
	if (rv != TOKEN_COMMA) {

	    /* A right parenthesis will end the list nicely */
	    if (rv == TOKEN_RPAREN) {

		/* Get the first token after the rights list */
		rv = aclGetToken(errp, acf, 0);
		break;
	    }

	    /* Anything else is an error */
	    if (rv < 0) goto punt;
	    goto err_list;
	}
    }

    return rv;

  err_elem:
    eid = ACLERR2700;
    rv = ACLERRSYNTAX;
    goto err_ret;

  err_rdef:
    eid = ACLERR2720;
    rv = ACLERRSYNTAX;
    goto err_ret;

  err_undef:
    eid = ACLERR2740;
    rv = ACLERRUNDEF;
    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program,
		  3, acf->acf_filename, linestr, ename);
    return rv;

  err_nomem1:
    eid = ACLERR2760;
    goto err_nomem;

  err_nomem2:
    eid = ACLERR2780;
    goto err_nomem;

  err_radd:
    eid = ACLERR2800;
    goto err_ret;

  err_nomem3:
    eid = ACLERR2820;
    goto err_nomem;

  err_nomem:
    rv = ACLERRNOMEM;
    goto err_ret;

  err_list:

    eid = ACLERR2840;
    rv = ACLERRSYNTAX;

  err_ret:
    sprintf(linestr, "%d", acf->acf_lineno);
    nserrGenerate(errp, rv, eid, ACL_Program, 2, acf->acf_filename, linestr);

  punt:
    return rv;
}

/*
 * Description (aclStreamGet)
 *
 *	This function is the stream read function designated by
 *	aclFileOpen() to read the file associated with the LEX stream
 *	it creates.  It reads the next chunk of the file into the
 *	stream buffer.
 *
 * Arguments:
 *
 *	lst			- pointer to LEX stream structure
 *
 * Returns:
 *
 *	The return value is the number of bytes read if successful.
 *	A return value of zero indicates end-of-file.  An error is
 *	indicated by a negative return value.
 */

int aclStreamGet(LEXStream_t * lst)
{
    SYS_FILE fd = (SYS_FILE)(lst->lst_strmid);
    int len;

    len = system_fread(fd, lst->lst_buf, lst->lst_buflen);
    if (len >= 0) {
	lst->lst_len = len;
	lst->lst_cp = lst->lst_buf;
    }

    return len;
}