summaryrefslogtreecommitdiffstats
path: root/libexec/bootpd/readfile.c
blob: 4a9d9937dd5c9a3a927f801bca418a421f71c578 (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
/************************************************************************
          Copyright 1988, 1991 by Carnegie Mellon University

                          All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation, and that the name of Carnegie Mellon University not be used
in advertising or publicity pertaining to distribution of the software
without specific, written prior permission.

CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
************************************************************************/

#ifndef lint
static char rcsid[] = "$Id: readfile.c,v 1.1.1.1 1994/09/10 14:44:55 csgr Exp $";
#endif


/*
 * bootpd configuration file reading code.
 *
 * The routines in this file deal with reading, interpreting, and storing
 * the information found in the bootpd configuration file (usually
 * /etc/bootptab).
 */


#include <sys/errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/time.h>
#include <netinet/in.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <syslog.h>

#ifndef USE_BFUNCS
#include <memory.h>
/* Yes, memcpy is OK here (no overlapped copies). */
#define	bcopy(a,b,c)	memcpy(b,a,c)
#define	bzero(p,l)	memset(p,0,l)
#define	bcmp(a,b,c)	memcmp(a,b,c)
#endif

#include "bootp.h"
#include "hash.h"
#include "hwaddr.h"
#include "lookup.h"
#include "readfile.h"
#include "report.h"
#include "tzone.h"
#include "bootpd.h"

#define HASHTABLESIZE		257	/* Hash table size (prime) */

/* Non-standard hardware address type (see bootp.h) */
#define HTYPE_DIRECT	0

/* Error codes returned by eval_symbol: */
#define SUCCESS			  0
#define E_END_OF_ENTRY		(-1)
#define E_SYNTAX_ERROR		(-2)
#define E_UNKNOWN_SYMBOL	(-3)
#define E_BAD_IPADDR		(-4)
#define E_BAD_HWADDR		(-5)
#define E_BAD_LONGWORD		(-6)
#define E_BAD_HWATYPE		(-7)
#define E_BAD_PATHNAME		(-8)
#define E_BAD_VALUE 		(-9)

/* Tag idendities. */
#define SYM_NULL		  0
#define SYM_BOOTFILE		  1
#define SYM_COOKIE_SERVER	  2
#define SYM_DOMAIN_SERVER	  3
#define SYM_GATEWAY		  4
#define SYM_HWADDR		  5
#define SYM_HOMEDIR		  6
#define SYM_HTYPE		  7
#define SYM_IMPRESS_SERVER	  8
#define SYM_IPADDR		  9
#define SYM_LOG_SERVER		 10
#define SYM_LPR_SERVER		 11
#define SYM_NAME_SERVER		 12
#define SYM_RLP_SERVER		 13
#define SYM_SUBNET_MASK		 14
#define SYM_TIME_OFFSET		 15
#define SYM_TIME_SERVER		 16
#define SYM_VENDOR_MAGIC	 17
#define SYM_SIMILAR_ENTRY	 18
#define SYM_NAME_SWITCH		 19
#define SYM_BOOTSIZE		 20
#define SYM_BOOT_SERVER		 22
#define SYM_TFTPDIR		 23
#define SYM_DUMP_FILE		 24
#define SYM_DOMAIN_NAME          25
#define SYM_SWAP_SERVER          26
#define SYM_ROOT_PATH            27
#define SYM_EXTEN_FILE           28
#define SYM_REPLY_ADDR           29
#define SYM_NIS_DOMAIN           30	/* RFC 1533 */
#define SYM_NIS_SERVER           31	/* RFC 1533 */
#define SYM_NTP_SERVER           32	/* RFC 1533 */
#define SYM_EXEC_FILE		 33	/* YORK_EX_OPTION */
#define SYM_MSG_SIZE 		 34
#define SYM_MIN_WAIT		 35
/* XXX - Add new tags here */

#define OP_ADDITION		  1	/* Operations on tags */
#define OP_DELETION		  2
#define OP_BOOLEAN		  3

#define MAXINADDRS		 16	/* Max size of an IP address list */
#define MAXBUFLEN		256	/* Max temp buffer space */
#define MAXENTRYLEN	       2048	/* Max size of an entire entry */



/*
 * Structure used to map a configuration-file symbol (such as "ds") to a
 * unique integer.
 */

struct symbolmap {
	char *symbol;
	int symbolcode;
};


struct htypename {
	char *name;
	byte htype;
};


PRIVATE int nhosts;				/* Number of hosts (/w hw or IP address) */
PRIVATE int nentries;			/* Total number of entries */
PRIVATE int32 modtime = 0;		/* Last modification time of bootptab */
PRIVATE char *current_hostname;	/* Name of the current entry. */
PRIVATE char current_tagname[8];

/*
 * List of symbolic names used in the bootptab file.  The order and actual
 * values of the symbol codes (SYM_. . .) are unimportant, but they must
 * all be unique.
 */

PRIVATE struct symbolmap symbol_list[] = {
	{"bf", SYM_BOOTFILE},
	{"bs", SYM_BOOTSIZE},
	{"cs", SYM_COOKIE_SERVER},
	{"df", SYM_DUMP_FILE},
	{"dn", SYM_DOMAIN_NAME},
	{"ds", SYM_DOMAIN_SERVER},
	{"ef", SYM_EXTEN_FILE},
	{"ex", SYM_EXEC_FILE},		/* YORK_EX_OPTION */
	{"gw", SYM_GATEWAY},
	{"ha", SYM_HWADDR},
	{"hd", SYM_HOMEDIR},
	{"hn", SYM_NAME_SWITCH},
	{"ht", SYM_HTYPE},
	{"im", SYM_IMPRESS_SERVER},
	{"ip", SYM_IPADDR},
	{"lg", SYM_LOG_SERVER},
	{"lp", SYM_LPR_SERVER},
	{"ms", SYM_MSG_SIZE},
	{"mw", SYM_MIN_WAIT},
	{"ns", SYM_NAME_SERVER},
	{"nt", SYM_NTP_SERVER},
	{"ra", SYM_REPLY_ADDR},
	{"rl", SYM_RLP_SERVER},
	{"rp", SYM_ROOT_PATH},
	{"sa", SYM_BOOT_SERVER},
	{"sm", SYM_SUBNET_MASK},
	{"sw", SYM_SWAP_SERVER},
	{"tc", SYM_SIMILAR_ENTRY},
	{"td", SYM_TFTPDIR},
	{"to", SYM_TIME_OFFSET},
	{"ts", SYM_TIME_SERVER},
	{"vm", SYM_VENDOR_MAGIC},
	{"yd", SYM_NIS_DOMAIN},
	{"ys", SYM_NIS_SERVER},
	/* XXX - Add new tags here */
};


/*
 * List of symbolic names for hardware types.  Name translates into
 * hardware type code listed with it.  Names must begin with a letter
 * and must be all lowercase.  This is searched linearly, so put
 * commonly-used entries near the beginning.
 */

PRIVATE struct htypename htnamemap[] = {
	{"ethernet", HTYPE_ETHERNET},
	{"ethernet3", HTYPE_EXP_ETHERNET},
	{"ether", HTYPE_ETHERNET},
	{"ether3", HTYPE_EXP_ETHERNET},
	{"ieee802", HTYPE_IEEE802},
	{"tr", HTYPE_IEEE802},
	{"token-ring", HTYPE_IEEE802},
	{"pronet", HTYPE_PRONET},
	{"chaos", HTYPE_CHAOS},
	{"arcnet", HTYPE_ARCNET},
	{"ax.25", HTYPE_AX25},
	{"direct", HTYPE_DIRECT},
	{"serial", HTYPE_DIRECT},
	{"slip", HTYPE_DIRECT},
	{"ppp", HTYPE_DIRECT}
};



/*
 * Externals and forward declarations.
 */

#ifdef	__STDC__
#define P(args) args
#else
#define P(args) ()
#endif

extern boolean iplookcmp();
boolean nmcmp P((hash_datum *, hash_datum *));

PRIVATE void
	adjust P((char **));
PRIVATE void
	del_string P((struct shared_string *));
PRIVATE void
	del_bindata P((struct shared_bindata *));
PRIVATE void
	del_iplist P((struct in_addr_list *));
PRIVATE void
	eat_whitespace P((char **));
PRIVATE int
	eval_symbol P((char **, struct host *));
PRIVATE void
	fill_defaults P((struct host *, char **));
PRIVATE void
	free_host P((hash_datum *));
PRIVATE struct in_addr_list *
	get_addresses P((char **));
PRIVATE struct shared_string *
	get_shared_string P((char **));
PRIVATE char *
	get_string P((char **, char *, u_int *));
PRIVATE u_int32
	get_u_long P((char **));
PRIVATE boolean
	goodname P((char *));
PRIVATE boolean
	hwinscmp P((hash_datum *, hash_datum *));
PRIVATE int
	interp_byte P((char **, byte *));
PRIVATE void
	makelower P((char *));
PRIVATE boolean
        nullcmp P((hash_datum *, hash_datum *));
PRIVATE int
	process_entry P((struct host *, char *));
PRIVATE int
	process_generic P((char **, struct shared_bindata **, u_int));
PRIVATE byte *
	prs_haddr P((char **, u_int));
PRIVATE int
	prs_inetaddr P((char **, u_int32 *));
PRIVATE void
	read_entry P((FILE *, char *, u_int *));
PRIVATE char *
	smalloc P((u_int));

#undef P


/*
 * Vendor magic cookies for CMU and RFC1048
 */
u_char vm_cmu[4] = VM_CMU;
u_char vm_rfc1048[4] = VM_RFC1048;

/*
 * Main hash tables
 */
hash_tbl *hwhashtable;
hash_tbl *iphashtable;
hash_tbl *nmhashtable;

/*
 * Allocate hash tables for hardware address, ip address, and hostname
 * (shared by bootpd and bootpef)
 */
void
rdtab_init()
{
	hwhashtable = hash_Init(HASHTABLESIZE);
	iphashtable = hash_Init(HASHTABLESIZE);
	nmhashtable = hash_Init(HASHTABLESIZE);
	if (!(hwhashtable && iphashtable && nmhashtable)) {
		report(LOG_ERR, "Unable to allocate hash tables.");
		exit(1);
	}
}


/*
 * Read bootptab database file.  Avoid rereading the file if the
 * write date hasn't changed since the last time we read it.
 */

void
readtab(force)
	int force;
{
	struct host *hp;
	FILE *fp;
	struct stat st;
	unsigned hashcode, buflen;
	static char buffer[MAXENTRYLEN];

	/*
	 * Check the last modification time.
	 */
	if (stat(bootptab, &st) < 0) {
		report(LOG_ERR, "stat on \"%s\": %s",
			   bootptab, get_errmsg());
		return;
	}
#ifdef DEBUG
	if (debug > 3) {
		char timestr[28];
		strcpy(timestr, ctime(&(st.st_mtime)));
		/* zap the newline */
		timestr[24] = '\0';
		report(LOG_INFO, "bootptab mtime: %s",
			   timestr);
	}
#endif
	if ((force == 0) &&
		(st.st_mtime == modtime) &&
		st.st_nlink) {
		/*
		 * hasn't been modified or deleted yet.
		 */
		return;
	}
	if (debug)
		report(LOG_INFO, "reading %s\"%s\"",
			   (modtime != 0L) ? "new " : "",
			   bootptab);

	/*
	 * Open bootptab file.
	 */
	if ((fp = fopen(bootptab, "r")) == NULL) {
		report(LOG_ERR, "error opening \"%s\": %s", bootptab, get_errmsg());
		return;
	}
	/*
	 * Record file modification time.
	 */
	if (fstat(fileno(fp), &st) < 0) {
		report(LOG_ERR, "fstat: %s", get_errmsg());
		fclose(fp);
		return;
	}
	modtime = st.st_mtime;

	/*
	 * Entirely erase all hash tables.
	 */
	hash_Reset(hwhashtable, free_host);
	hash_Reset(iphashtable, free_host);
	hash_Reset(nmhashtable, free_host);

	nhosts = 0;
	nentries = 0;
	while (TRUE) {
		buflen = sizeof(buffer);
		read_entry(fp, buffer, &buflen);
		if (buflen == 0) {		/* More entries? */
			break;
		}
		hp = (struct host *) smalloc(sizeof(struct host));
		bzero((char *) hp, sizeof(*hp));
		/* the link count it zero */

		/*
		 * Get individual info
		 */
		if (process_entry(hp, buffer) < 0) {
			hp->linkcount = 1;
			free_host((hash_datum *) hp);
			continue;
		}
		/*
		 * If this is not a dummy entry, and the IP or HW
		 * address is not yet set, try to get them here.
		 * Dummy entries have . as first char of name.
		 */
		if (goodname(hp->hostname->string)) {
			char *hn = hp->hostname->string;
			u_int32 value;
			if (hp->flags.iaddr == 0) {
				if (lookup_ipa(hn, &value)) {
					report(LOG_ERR, "can not get IP addr for %s", hn);
					report(LOG_ERR, "(dummy names should start with '.')");
				} else {
					hp->iaddr.s_addr = value;
					hp->flags.iaddr = TRUE;
				}
			}
			/* Set default subnet mask. */
			if (hp->flags.subnet_mask == 0) {
				if (lookup_netmask(hp->iaddr.s_addr, &value)) {
					report(LOG_ERR, "can not get netmask for %s", hn);
				} else {
					hp->subnet_mask.s_addr = value;
					hp->flags.subnet_mask = TRUE;
				}
			}
		}
		if (hp->flags.iaddr) {
			nhosts++;
		}
		/* Register by HW addr if known. */
		if (hp->flags.htype && hp->flags.haddr) {
			/* We will either insert it or free it. */
			hp->linkcount++;
			hashcode = hash_HashFunction(hp->haddr, haddrlength(hp->htype));
			if (hash_Insert(hwhashtable, hashcode, hwinscmp, hp, hp) < 0) {
				report(LOG_NOTICE, "duplicate %s address: %s",
					   netname(hp->htype),
					   haddrtoa(hp->haddr, hp->htype));
				free_host((hash_datum *) hp);
				continue;
			}
		}
		/* Register by IP addr if known. */
		if (hp->flags.iaddr) {
			hashcode = hash_HashFunction((u_char *) & (hp->iaddr.s_addr), 4);
			if (hash_Insert(iphashtable, hashcode, nullcmp, hp, hp) < 0) {
				report(LOG_ERR,
					   "hash_Insert() failed on IP address insertion");
			} else {
				/* Just inserted the host struct in a new hash list. */
				hp->linkcount++;
			}
		}
		/* Register by Name (always known) */
		hashcode = hash_HashFunction((u_char *) hp->hostname->string,
									 strlen(hp->hostname->string));
		if (hash_Insert(nmhashtable, hashcode, nullcmp,
						hp->hostname->string, hp) < 0) {
			report(LOG_ERR,
				 "hash_Insert() failed on insertion of hostname: \"%s\"",
				   hp->hostname->string);
		} else {
			/* Just inserted the host struct in a new hash list. */
			hp->linkcount++;
		}

		nentries++;
	}

	fclose(fp);
	if (debug)
		report(LOG_INFO, "read %d entries (%d hosts) from \"%s\"",
			   nentries, nhosts, bootptab);
	return;
}



/*
 * Read an entire host entry from the file pointed to by "fp" and insert it
 * into the memory pointed to by "buffer".  Leading whitespace and comments
 * starting with "#" are ignored (removed).  Backslashes (\) always quote
 * the next character except that newlines preceeded by a backslash cause
 * line-continuation onto the next line.  The entry is terminated by a
 * newline character which is not preceeded by a backslash.  Sequences
 * surrounded by double quotes are taken literally (including newlines, but
 * not backslashes).
 *
 * The "bufsiz" parameter points to an unsigned int which specifies the
 * maximum permitted buffer size.  Upon return, this value will be replaced
 * with the actual length of the entry (not including the null terminator).
 *
 * This code is a little scary. . . .  I don't like using gotos in C
 * either, but I first wrote this as an FSM diagram and gotos seemed like
 * the easiest way to implement it.  Maybe later I'll clean it up.
 */

PRIVATE void
read_entry(fp, buffer, bufsiz)
	FILE *fp;
	char *buffer;
	unsigned *bufsiz;
{
	int c, length;

	length = 0;

	/*
	 * Eat whitespace, blank lines, and comment lines.
	 */
  top:
	c = fgetc(fp);
	if (c < 0) {
		goto done;				/* Exit if end-of-file */
	}
	if (isspace(c)) {
		goto top;				/* Skip over whitespace */
	}
	if (c == '#') {
		while (TRUE) {			/* Eat comments after # */
			c = fgetc(fp);
			if (c < 0) {
				goto done;		/* Exit if end-of-file */
			}
			if (c == '\n') {
				goto top;		/* Try to read the next line */
			}
		}
	}
	ungetc(c, fp);				/* Other character, push it back to reprocess it */


	/*
	 * Now we're actually reading a data entry.  Get each character and
	 * assemble it into the data buffer, processing special characters like
	 * double quotes (") and backslashes (\).
	 */

  mainloop:
	c = fgetc(fp);
	switch (c) {
	case EOF:
	case '\n':
		goto done;				/* Exit on EOF or newline */
	case '\\':
		c = fgetc(fp);			/* Backslash, read a new character */
		if (c < 0) {
			goto done;			/* Exit on EOF */
		}
		*buffer++ = c;			/* Store the literal character */
		length++;
		if (length < *bufsiz - 1) {
			goto mainloop;
		} else {
			goto done;
		}
	case '"':
		*buffer++ = '"';		/* Store double-quote */
		length++;
		if (length >= *bufsiz - 1) {
			goto done;
		}
		while (TRUE) {			/* Special quote processing loop */
			c = fgetc(fp);
			switch (c) {
			case EOF:
				goto done;		/* Exit on EOF . . . */
			case '"':
				*buffer++ = '"';/* Store matching quote */
				length++;
				if (length < *bufsiz - 1) {
					goto mainloop;	/* And continue main loop */
				} else {
					goto done;
				}
			case '\\':
				if ((c = fgetc(fp)) < 0) {	/* Backslash */
					goto done;	/* EOF. . . .*/
				}				/* else fall through */
			default:
				*buffer++ = c;	/* Other character, store it */
				length++;
				if (length >= *bufsiz - 1) {
					goto done;
				}
			}
		}
	case ':':
		*buffer++ = c;			/* Store colons */
		length++;
		if (length >= *bufsiz - 1) {
			goto done;
		}
		do {					/* But remove whitespace after them */
			c = fgetc(fp);
			if ((c < 0) || (c == '\n')) {
				goto done;
			}
		} while (isspace(c));	/* Skip whitespace */

		if (c == '\\') {		/* Backslash quotes next character */
			c = fgetc(fp);
			if (c < 0) {
				goto done;
			}
			if (c == '\n') {
				goto top;		/* Backslash-newline continuation */
			}
		}
		/* fall through if "other" character */
	default:
		*buffer++ = c;			/* Store other characters */
		length++;
		if (length >= *bufsiz - 1) {
			goto done;
		}
	}
	goto mainloop;				/* Keep going */

  done:
	*buffer = '\0';				/* Terminate string */
	*bufsiz = length;			/* Tell the caller its length */
}



/*
 * Parse out all the various tags and parameters in the host entry pointed
 * to by "src".  Stuff all the data into the appropriate fields of the
 * host structure pointed to by "host".  If there is any problem with the
 * entry, an error message is reported via report(), no further processing
 * is done, and -1 is returned.  Successful calls return 0.
 *
 * (Some errors probably shouldn't be so completely fatal. . . .)
 */

PRIVATE int
process_entry(host, src)
	struct host *host;
	char *src;
{
	int retval;
	char *msg;

	if (!host || *src == '\0') {
		return -1;
	}
	host->hostname = get_shared_string(&src);
#if 0
	/* Be more liberal for the benefit of dummy tag names. */
	if (!goodname(host->hostname->string)) {
		report(LOG_ERR, "bad hostname: \"%s\"", host->hostname->string);
		del_string(host->hostname);
		return -1;
	}
#endif
	current_hostname = host->hostname->string;
	adjust(&src);
	while (TRUE) {
		retval = eval_symbol(&src, host);
		if (retval == SUCCESS) {
			adjust(&src);
			continue;
		}
		if (retval == E_END_OF_ENTRY) {
			/* The default subnet mask is set in readtab() */
			return 0;
		}
		/* Some kind of error. */
		switch (retval) {
		case E_SYNTAX_ERROR:
			msg = "bad syntax";
			break;
		case E_UNKNOWN_SYMBOL:
			msg = "unknown symbol";
			break;
		case E_BAD_IPADDR:
			msg = "bad INET address";
			break;
		case E_BAD_HWADDR:
			msg = "bad hardware address";
			break;
		case E_BAD_LONGWORD:
			msg = "bad longword value";
			break;
		case E_BAD_HWATYPE:
			msg = "bad HW address type";
			break;
		case E_BAD_PATHNAME:
			msg = "bad pathname (need leading '/')";
		case E_BAD_VALUE:
			msg = "bad value";
		default:
			msg = "unkown error";
			break;
		}						/* switch */
		report(LOG_ERR, "in entry named \"%s\", symbol \"%s\": %s",
			   current_hostname, current_tagname, msg);
		return -1;
	}
}


/*
 * Macros for use in the function below:
 */

/* Parse one INET address stored directly in MEMBER. */
#define PARSE_IA1(MEMBER) do \
{ \
	if (optype == OP_BOOLEAN) \
		return E_SYNTAX_ERROR; \
	hp->flags.MEMBER = FALSE; \
	if (optype == OP_ADDITION) { \
		if (prs_inetaddr(symbol, &value) < 0) \
			return E_BAD_IPADDR; \
		hp->MEMBER.s_addr = value; \
		hp->flags.MEMBER = TRUE; \
	} \
} while (0)

/* Parse a list of INET addresses pointed to by MEMBER */
#define PARSE_IAL(MEMBER) do \
{ \
	if (optype == OP_BOOLEAN) \
		return E_SYNTAX_ERROR; \
	if (hp->flags.MEMBER) { \
		hp->flags.MEMBER = FALSE; \
		assert(hp->MEMBER); \
		del_iplist(hp->MEMBER); \
		hp->MEMBER = NULL; \
	} \
	if (optype == OP_ADDITION) { \
		hp->MEMBER = get_addresses(symbol); \
		if (hp->MEMBER == NULL) \
			return E_SYNTAX_ERROR; \
		hp->flags.MEMBER = TRUE; \
	} \
} while (0)

/* Parse a shared string pointed to by MEMBER */
#define PARSE_STR(MEMBER) do \
{ \
	if (optype == OP_BOOLEAN) \
		return E_SYNTAX_ERROR; \
	if (hp->flags.MEMBER) { \
		hp->flags.MEMBER = FALSE; \
		assert(hp->MEMBER); \
		del_string(hp->MEMBER); \
		hp->MEMBER = NULL; \
	} \
	if (optype == OP_ADDITION) { \
		hp->MEMBER = get_shared_string(symbol); \
		if (hp->MEMBER == NULL) \
			return E_SYNTAX_ERROR; \
		hp->flags.MEMBER = TRUE; \
	} \
} while (0)

/* Parse an integer value for MEMBER */
#define PARSE_INT(MEMBER) do \
{ \
	if (optype == OP_BOOLEAN) \
		return E_SYNTAX_ERROR; \
	hp->flags.MEMBER = FALSE; \
	if (optype == OP_ADDITION) { \
		value = get_u_long(symbol); \
		hp->MEMBER = value; \
		hp->flags.MEMBER = TRUE; \
	} \
} while (0)

/*
 * Evaluate the two-character tag symbol pointed to by "symbol" and place
 * the data in the structure pointed to by "hp".  The pointer pointed to
 * by "symbol" is updated to point past the source string (but may not
 * point to the next tag entry).
 *
 * Obviously, this need a few more comments. . . .
 */
PRIVATE int
eval_symbol(symbol, hp)
	char **symbol;
	struct host *hp;
{
	char tmpstr[MAXSTRINGLEN];
	byte *tmphaddr;
	struct shared_string *ss;
	struct symbolmap *symbolptr;
	u_int32 value;
	int32 timeoff;
	int i, numsymbols;
	unsigned len;
	int optype;					/* Indicates boolean, addition, or deletion */

	eat_whitespace(symbol);

	/* Make sure this is set before returning. */
	current_tagname[0] = (*symbol)[0];
	current_tagname[1] = (*symbol)[1];
	current_tagname[2] = 0;

	if ((*symbol)[0] == '\0') {
		return E_END_OF_ENTRY;
	}
	if ((*symbol)[0] == ':') {
		return SUCCESS;
	}
	if ((*symbol)[0] == 'T') {	/* generic symbol */
		(*symbol)++;
		value = get_u_long(symbol);
		sprintf(current_tagname, "T%d", value);
		eat_whitespace(symbol);
		if ((*symbol)[0] != '=') {
			return E_SYNTAX_ERROR;
		}
		(*symbol)++;
		if (!(hp->generic)) {
			hp->generic = (struct shared_bindata *)
				smalloc(sizeof(struct shared_bindata));
		}
		if (process_generic(symbol, &(hp->generic), (byte) (value & 0xFF)))
			return E_SYNTAX_ERROR;
		hp->flags.generic = TRUE;
		return SUCCESS;
	}
	/*
	 * Determine the type of operation to be done on this symbol
	 */
	switch ((*symbol)[2]) {
	case '=':
		optype = OP_ADDITION;
		break;
	case '@':
		optype = OP_DELETION;
		break;
	case ':':
	case '\0':
		optype = OP_BOOLEAN;
		break;
	default:
		return E_SYNTAX_ERROR;
	}

	symbolptr = symbol_list;
	numsymbols = sizeof(symbol_list) / sizeof(struct symbolmap);
	for (i = 0; i < numsymbols; i++) {
		if (((symbolptr->symbol)[0] == (*symbol)[0]) &&
			((symbolptr->symbol)[1] == (*symbol)[1])) {
			break;
		}
		symbolptr++;
	}
	if (i >= numsymbols) {
		return E_UNKNOWN_SYMBOL;
	}
	/*
	 * Skip past the = or @ character (to point to the data) if this
	 * isn't a boolean operation.  For boolean operations, just skip
	 * over the two-character tag symbol (and nothing else. . . .).
	 */
	(*symbol) += (optype == OP_BOOLEAN) ? 2 : 3;

	eat_whitespace(symbol);

	/* The cases below are in order by symbolcode value. */
	switch (symbolptr->symbolcode) {

	case SYM_BOOTFILE:
		PARSE_STR(bootfile);
		break;

	case SYM_COOKIE_SERVER:
		PARSE_IAL(cookie_server);
		break;

	case SYM_DOMAIN_SERVER:
		PARSE_IAL(domain_server);
		break;

	case SYM_GATEWAY:
		PARSE_IAL(gateway);
		break;

	case SYM_HWADDR:
		if (optype == OP_BOOLEAN)
			return E_SYNTAX_ERROR;
		hp->flags.haddr = FALSE;
		if (optype == OP_ADDITION) {
			/* Default the HW type to Ethernet */
			if (hp->flags.htype == 0) {
				hp->flags.htype = TRUE;
				hp->htype = HTYPE_ETHERNET;
			}
			tmphaddr = prs_haddr(symbol, hp->htype);
			if (!tmphaddr)
				return E_BAD_HWADDR;
			bcopy(tmphaddr, hp->haddr, haddrlength(hp->htype));
			hp->flags.haddr = TRUE;
		}
		break;

	case SYM_HOMEDIR:
		PARSE_STR(homedir);
		break;

	case SYM_HTYPE:
		if (optype == OP_BOOLEAN)
			return E_SYNTAX_ERROR;
		hp->flags.htype = FALSE;
		if (optype == OP_ADDITION) {
			value = 0L;			/* Assume an illegal value */
			eat_whitespace(symbol);
			if (isdigit(**symbol)) {
				value = get_u_long(symbol);
			} else {
				len = sizeof(tmpstr);
				(void) get_string(symbol, tmpstr, &len);
				makelower(tmpstr);
				numsymbols = sizeof(htnamemap) /
					sizeof(struct htypename);
				for (i = 0; i < numsymbols; i++) {
					if (!strcmp(htnamemap[i].name, tmpstr)) {
						break;
					}
				}
				if (i < numsymbols) {
					value = htnamemap[i].htype;
				}
			}
			if (value >= hwinfocnt) {
				return E_BAD_HWATYPE;
			}
			hp->htype = (byte) (value & 0xFF);
			hp->flags.htype = TRUE;
		}
		break;

	case SYM_IMPRESS_SERVER:
		PARSE_IAL(impress_server);
		break;

	case SYM_IPADDR:
		PARSE_IA1(iaddr);
		break;

	case SYM_LOG_SERVER:
		PARSE_IAL(log_server);
		break;

	case SYM_LPR_SERVER:
		PARSE_IAL(lpr_server);
		break;

	case SYM_NAME_SERVER:
		PARSE_IAL(name_server);
		break;

	case SYM_RLP_SERVER:
		PARSE_IAL(rlp_server);
		break;

	case SYM_SUBNET_MASK:
		PARSE_IA1(subnet_mask);
		break;

	case SYM_TIME_OFFSET:
		if (optype == OP_BOOLEAN)
			return E_SYNTAX_ERROR;
		hp->flags.time_offset = FALSE;
		if (optype == OP_ADDITION) {
			len = sizeof(tmpstr);
			(void) get_string(symbol, tmpstr, &len);
			if (!strncmp(tmpstr, "auto", 4)) {
				hp->time_offset = secondswest;
			} else {
				if (sscanf(tmpstr, "%d", &timeoff) != 1)
					return E_BAD_LONGWORD;
				hp->time_offset = timeoff;
			}
			hp->flags.time_offset = TRUE;
		}
		break;

	case SYM_TIME_SERVER:
		PARSE_IAL(time_server);
		break;

	case SYM_VENDOR_MAGIC:
		if (optype == OP_BOOLEAN)
			return E_SYNTAX_ERROR;
		hp->flags.vm_cookie = FALSE;
		if (optype == OP_ADDITION) {
			if (strncmp(*symbol, "auto", 4)) {
				/* The string is not "auto" */
				if (!strncmp(*symbol, "rfc", 3)) {
					bcopy(vm_rfc1048, hp->vm_cookie, 4);
				} else if (!strncmp(*symbol, "cmu", 3)) {
					bcopy(vm_cmu, hp->vm_cookie, 4);
				} else {
					if (!isdigit(**symbol))
						return E_BAD_IPADDR;
					if (prs_inetaddr(symbol, &value) < 0)
						return E_BAD_IPADDR;
					bcopy(&value, hp->vm_cookie, 4);
				}
				hp->flags.vm_cookie = TRUE;
			}
		}
		break;

	case SYM_SIMILAR_ENTRY:
		switch (optype) {
		case OP_ADDITION:
			fill_defaults(hp, symbol);
			break;
		default:
			return E_SYNTAX_ERROR;
		}
		break;

	case SYM_NAME_SWITCH:
		switch (optype) {
		case OP_ADDITION:
			return E_SYNTAX_ERROR;
		case OP_DELETION:
			hp->flags.send_name = FALSE;
			hp->flags.name_switch = FALSE;
			break;
		case OP_BOOLEAN:
			hp->flags.send_name = TRUE;
			hp->flags.name_switch = TRUE;
			break;
		}
		break;

	case SYM_BOOTSIZE:
		switch (optype) {
		case OP_ADDITION:
			if (!strncmp(*symbol, "auto", 4)) {
				hp->flags.bootsize = TRUE;
				hp->flags.bootsize_auto = TRUE;
			} else {
				hp->bootsize = (unsigned int) get_u_long(symbol);
				hp->flags.bootsize = TRUE;
				hp->flags.bootsize_auto = FALSE;
			}
			break;
		case OP_DELETION:
			hp->flags.bootsize = FALSE;
			break;
		case OP_BOOLEAN:
			hp->flags.bootsize = TRUE;
			hp->flags.bootsize_auto = TRUE;
			break;
		}
		break;

	case SYM_BOOT_SERVER:
		PARSE_IA1(bootserver);
		break;

	case SYM_TFTPDIR:
		PARSE_STR(tftpdir);
		if ((hp->tftpdir != NULL) &&
			(hp->tftpdir->string[0] != '/'))
			return E_BAD_PATHNAME;
		break;

	case SYM_DUMP_FILE:
		PARSE_STR(dump_file);
		break;

	case SYM_DOMAIN_NAME:
		PARSE_STR(domain_name);
		break;

	case SYM_SWAP_SERVER:
		PARSE_IA1(swap_server);
		break;

	case SYM_ROOT_PATH:
		PARSE_STR(root_path);
		break;

	case SYM_EXTEN_FILE:
		PARSE_STR(exten_file);
		break;

	case SYM_REPLY_ADDR:
		PARSE_IA1(reply_addr);
		break;

	case SYM_NIS_DOMAIN:
		PARSE_STR(nis_domain);
		break;

	case SYM_NIS_SERVER:
		PARSE_IAL(nis_server);
		break;

	case SYM_NTP_SERVER:
		PARSE_IAL(ntp_server);
		break;

#ifdef	YORK_EX_OPTION
	case SYM_EXEC_FILE:
		PARSE_STR(exec_file);
		break;
#endif

	case SYM_MSG_SIZE:
		PARSE_INT(msg_size);
		if (hp->msg_size < BP_MINPKTSZ ||
			hp->msg_size > MAX_MSG_SIZE)
			return E_BAD_VALUE;
		break;

	case SYM_MIN_WAIT:
		PARSE_INT(min_wait);
		if (hp->min_wait < 0)
			return E_BAD_VALUE;
		break;

		/* XXX - Add new tags here */

	default:
		return E_UNKNOWN_SYMBOL;

	}							/* switch symbolcode */

	return SUCCESS;
}
#undef	PARSE_IA1
#undef	PARSE_IAL
#undef	PARSE_STR




/*
 * Read a string from the buffer indirectly pointed to through "src" and
 * move it into the buffer pointed to by "dest".  A pointer to the maximum
 * allowable length of the string (including null-terminator) is passed as
 * "length".  The actual length of the string which was read is returned in
 * the unsigned integer pointed to by "length".  This value is the same as
 * that which would be returned by applying the strlen() function on the
 * destination string (i.e the terminating null is not counted as a
 * character).  Trailing whitespace is removed from the string.  For
 * convenience, the function returns the new value of "dest".
 *
 * The string is read until the maximum number of characters, an unquoted
 * colon (:), or a null character is read.  The return string in "dest" is
 * null-terminated.
 */

PRIVATE char *
get_string(src, dest, length)
	char **src, *dest;
	unsigned *length;
{
	int n, len, quoteflag;

	quoteflag = FALSE;
	n = 0;
	len = *length - 1;
	while ((n < len) && (**src)) {
		if (!quoteflag && (**src == ':')) {
			break;
		}
		if (**src == '"') {
			(*src)++;
			quoteflag = !quoteflag;
			continue;
		}
		if (**src == '\\') {
			(*src)++;
			if (!**src) {
				break;
			}
		}
		*dest++ = *(*src)++;
		n++;
	}

	/*
	 * Remove that troublesome trailing whitespace. . .
	 */
	while ((n > 0) && isspace(dest[-1])) {
		dest--;
		n--;
	}

	*dest = '\0';
	*length = n;
	return dest;
}



/*
 * Read the string indirectly pointed to by "src", update the caller's
 * pointer, and return a pointer to a malloc'ed shared_string structure
 * containing the string.
 *
 * The string is read using the same rules as get_string() above.
 */

PRIVATE struct shared_string *
get_shared_string(src)
	char **src;
{
	char retstring[MAXSTRINGLEN];
	struct shared_string *s;
	unsigned length;

	length = sizeof(retstring);
	(void) get_string(src, retstring, &length);

	s = (struct shared_string *) smalloc(sizeof(struct shared_string)
										 + length);
	s->linkcount = 1;
	strcpy(s->string, retstring);

	return s;
}



/*
 * Load RFC1048 generic information directly into a memory buffer.
 *
 * "src" indirectly points to the ASCII representation of the generic data.
 * "dest" points to a string structure which is updated to point to a new
 * string with the new data appended to the old string.  The old string is
 * freed.
 *
 * The given tag value is inserted with the new data.
 *
 * The data may be represented as either a stream of hexadecimal numbers
 * representing bytes (any or all bytes may optionally start with '0x' and
 * be separated with periods ".") or as a quoted string of ASCII
 * characters (the quotes are required).
 */

PRIVATE int
process_generic(src, dest, tagvalue)
	char **src;
	struct shared_bindata **dest;
	u_int tagvalue;
{
	byte tmpbuf[MAXBUFLEN];
	byte *str;
	struct shared_bindata *bdata;
	u_int newlength, oldlength;

	str = tmpbuf;
	*str++ = (tagvalue & 0xFF);	/* Store tag value */
	str++;						/* Skip over length field */
	if ((*src)[0] == '"') {		/* ASCII data */
		newlength = sizeof(tmpbuf) - 2;	/* Set maximum allowed length */
		(void) get_string(src, (char *) str, &newlength);
		newlength++;			/* null terminator */
	} else {					/* Numeric data */
		newlength = 0;
		while (newlength < sizeof(tmpbuf) - 2) {
			if (interp_byte(src, str++) < 0)
				break;
			newlength++;
			if (**src == '.') {
				(*src)++;
			}
		}
	}
	if ((*src)[0] != ':')
		return -1;

	tmpbuf[1] = (newlength & 0xFF);
	oldlength = ((*dest)->length);
	bdata = (struct shared_bindata *) smalloc(sizeof(struct shared_bindata)
											+ oldlength + newlength + 1);
	if (oldlength > 0) {
		bcopy((*dest)->data, bdata->data, oldlength);
	}
	bcopy(tmpbuf, bdata->data + oldlength, newlength + 2);
	bdata->length = oldlength + newlength + 2;
	bdata->linkcount = 1;
	if (*dest) {
		del_bindata(*dest);
	}
	*dest = bdata;
	return 0;
}



/*
 * Verify that the given string makes sense as a hostname (according to
 * Appendix 1, page 29 of RFC882).
 *
 * Return TRUE for good names, FALSE otherwise.
 */

PRIVATE boolean
goodname(hostname)
	register char *hostname;
{
	do {
		if (!isalpha(*hostname++)) {	/* First character must be a letter */
			return FALSE;
		}
		while (isalnum(*hostname) ||
			   (*hostname == '-') ||
			   (*hostname == '_') )
		{
			hostname++;			/* Alphanumeric or a hyphen */
		}
		if (!isalnum(hostname[-1])) {	/* Last must be alphanumeric */
			return FALSE;
		}
		if (*hostname == '\0') {/* Done? */
			return TRUE;
		}
	} while (*hostname++ == '.');	/* Dot, loop for next label */

	return FALSE;				/* If it's not a dot, lose */
}



/*
 * Null compare function -- always returns FALSE so an element is always
 * inserted into a hash table (i.e. there is never a collision with an
 * existing element).
 */

PRIVATE boolean
nullcmp(d1, d2)
	hash_datum *d1, *d2;
{
	return FALSE;
}


/*
 * Function for comparing a string with the hostname field of a host
 * structure.
 */

boolean
nmcmp(d1, d2)
	hash_datum *d1, *d2;
{
	char *name = (char *) d1;	/* XXX - OK? */
	struct host *hp = (struct host *) d2;

	return !strcmp(name, hp->hostname->string);
}


/*
 * Compare function to determine whether two hardware addresses are
 * equivalent.  Returns TRUE if "host1" and "host2" are equivalent, FALSE
 * otherwise.
 *
 * If the hardware addresses of "host1" and "host2" are identical, but
 * they are on different IP subnets, this function returns FALSE.
 *
 * This function is used when inserting elements into the hardware address
 * hash table.
 */

PRIVATE boolean
hwinscmp(d1, d2)
	hash_datum *d1, *d2;
{
	struct host *host1 = (struct host *) d1;
	struct host *host2 = (struct host *) d2;

	if (host1->htype != host2->htype) {
		return FALSE;
	}
	if (bcmp(host1->haddr, host2->haddr, haddrlength(host1->htype))) {
		return FALSE;
	}
	/* XXX - Is the subnet_mask field set yet? */
	if ((host1->subnet_mask.s_addr) == (host2->subnet_mask.s_addr)) {
		if (((host1->iaddr.s_addr) & (host1->subnet_mask.s_addr)) !=
			((host2->iaddr.s_addr) & (host2->subnet_mask.s_addr)))
		{
			return FALSE;
		}
	}
	return TRUE;
}


/*
 * Macros for use in the function below:
 */

#define DUP_COPY(MEMBER) do \
{ \
	if (!hp->flags.MEMBER) { \
		if ((hp->flags.MEMBER = hp2->flags.MEMBER) != 0) { \
			hp->MEMBER = hp2->MEMBER; \
		} \
	} \
} while (0)

#define DUP_LINK(MEMBER) do \
{ \
	if (!hp->flags.MEMBER) { \
		if ((hp->flags.MEMBER = hp2->flags.MEMBER) != 0) { \
			assert(hp2->MEMBER); \
			hp->MEMBER = hp2->MEMBER; \
			(hp->MEMBER->linkcount)++; \
		} \
	} \
} while (0)

/*
 * Process the "similar entry" symbol.
 *
 * The host specified as the value of the "tc" symbol is used as a template
 * for the current host entry.  Symbol values not explicitly set in the
 * current host entry are inferred from the template entry.
 */
PRIVATE void
fill_defaults(hp, src)
	struct host *hp;
	char **src;
{
	unsigned int tlen, hashcode;
	struct host *hp2;
	char tstring[MAXSTRINGLEN];

	tlen = sizeof(tstring);
	(void) get_string(src, tstring, &tlen);
	hashcode = hash_HashFunction((u_char *) tstring, tlen);
	hp2 = (struct host *) hash_Lookup(nmhashtable, hashcode, nmcmp, tstring);

	if (hp2 == NULL) {
		report(LOG_ERR, "can't find tc=\"%s\"", tstring);
		return;
	}
	DUP_LINK(bootfile);
	DUP_LINK(cookie_server);
	DUP_LINK(domain_server);
	DUP_LINK(gateway);
	/* haddr not copied */
	DUP_LINK(homedir);
	DUP_COPY(htype);

	DUP_LINK(impress_server);
	/* iaddr not copied */
	DUP_LINK(log_server);
	DUP_LINK(lpr_server);
	DUP_LINK(name_server);
	DUP_LINK(rlp_server);

	DUP_COPY(subnet_mask);
	DUP_COPY(time_offset);
	DUP_LINK(time_server);

	if (!hp->flags.vm_cookie) {
		if ((hp->flags.vm_cookie = hp2->flags.vm_cookie)) {
			bcopy(hp2->vm_cookie, hp->vm_cookie, 4);
		}
	}
	if (!hp->flags.name_switch) {
		if ((hp->flags.name_switch = hp2->flags.name_switch)) {
			hp->flags.send_name = hp2->flags.send_name;
		}
	}
	if (!hp->flags.bootsize) {
		if ((hp->flags.bootsize = hp2->flags.bootsize)) {
			hp->flags.bootsize_auto = hp2->flags.bootsize_auto;
			hp->bootsize = hp2->bootsize;
		}
	}
	DUP_COPY(bootserver);

	DUP_LINK(tftpdir);
	DUP_LINK(dump_file);
	DUP_LINK(domain_name);

	DUP_COPY(swap_server);
	DUP_LINK(root_path);
	DUP_LINK(exten_file);

	DUP_COPY(reply_addr);

	DUP_LINK(nis_domain);
	DUP_LINK(nis_server);
	DUP_LINK(ntp_server);

#ifdef	YORK_EX_OPTION
	DUP_LINK(exec_file);
#endif

	DUP_COPY(msg_size);
	DUP_COPY(min_wait);

	/* XXX - Add new tags here */

	DUP_LINK(generic);

}
#undef	DUP_COPY
#undef	DUP_LINK



/*
 * This function adjusts the caller's pointer to point just past the
 * first-encountered colon.  If it runs into a null character, it leaves
 * the pointer pointing to it.
 */

PRIVATE void
adjust(s)
	char **s;
{
	register char *t;

	t = *s;
	while (*t && (*t != ':')) {
		t++;
	}
	if (*t) {
		t++;
	}
	*s = t;
}




/*
 * This function adjusts the caller's pointer to point to the first
 * non-whitespace character.  If it runs into a null character, it leaves
 * the pointer pointing to it.
 */

PRIVATE void
eat_whitespace(s)
	char **s;
{
	register char *t;

	t = *s;
	while (*t && isspace(*t)) {
		t++;
	}
	*s = t;
}



/*
 * This function converts the given string to all lowercase.
 */

PRIVATE void
makelower(s)
	char *s;
{
	while (*s) {
		if (isupper(*s)) {
			*s = tolower(*s);
		}
		s++;
	}
}



/*
 *
 *	N O T E :
 *
 *	In many of the functions which follow, a parameter such as "src" or
 *	"symbol" is passed as a pointer to a pointer to something.  This is
 *	done for the purpose of letting the called function update the
 *	caller's copy of the parameter (i.e. to effect call-by-reference
 *	parameter passing).  The value of the actual parameter is only used
 *	to locate the real parameter of interest and then update this indirect
 *	parameter.
 *
 *	I'm sure somebody out there won't like this. . . .
 *  (Yea, because it usually makes code slower... -gwr)
 *
 */



/*
 * "src" points to a character pointer which points to an ASCII string of
 * whitespace-separated IP addresses.  A pointer to an in_addr_list
 * structure containing the list of addresses is returned.  NULL is
 * returned if no addresses were found at all.  The pointer pointed to by
 * "src" is updated to point to the first non-address (illegal) character.
 */

PRIVATE struct in_addr_list *
get_addresses(src)
	char **src;
{
	struct in_addr tmpaddrlist[MAXINADDRS];
	struct in_addr *address1, *address2;
	struct in_addr_list *result;
	unsigned addrcount, totalsize;

	address1 = tmpaddrlist;
	for (addrcount = 0; addrcount < MAXINADDRS; addrcount++) {
		while (isspace(**src) || (**src == ',')) {
			(*src)++;
		}
		if (!**src) {			/* Quit if nothing more */
			break;
		}
		if (prs_inetaddr(src, &(address1->s_addr)) < 0) {
			break;
		}
		address1++;				/* Point to next address slot */
	}
	if (addrcount < 1) {
		result = NULL;
	} else {
		totalsize = sizeof(struct in_addr_list)
		+			(addrcount - 1) * sizeof(struct in_addr);
		result = (struct in_addr_list *) smalloc(totalsize);
		result->linkcount = 1;
		result->addrcount = addrcount;
		address1 = tmpaddrlist;
		address2 = result->addr;
		for (; addrcount > 0; addrcount--) {
			address2->s_addr = address1->s_addr;
			address1++;
			address2++;
		}
	}
	return result;
}



/*
 * prs_inetaddr(src, result)
 *
 * "src" is a value-result parameter; the pointer it points to is updated
 * to point to the next data position.   "result" points to an unsigned long
 * in which an address is returned.
 *
 * This function parses the IP address string in ASCII "dot notation" pointed
 * to by (*src) and places the result (in network byte order) in the unsigned
 * long pointed to by "result".  For malformed addresses, -1 is returned,
 * (*src) points to the first illegal character, and the unsigned long pointed
 * to by "result" is unchanged.  Successful calls return 0.
 */

PRIVATE int
prs_inetaddr(src, result)
	char **src;
	u_int32 *result;
{
	char tmpstr[MAXSTRINGLEN];
	register u_int32 value;
	u_int32 parts[4], *pp;
	int n;
	char *s, *t;

#if 1	/* XXX - experimental */
	/* Leading alpha char causes IP addr lookup. */
	if (isalpha(**src)) {
		/* Lookup IP address. */
		s = *src;
		t = tmpstr;
		while ((isalnum(*s) || (*s == '.') ||
				(*s == '-') || (*s == '_') ) &&
			   (t < &tmpstr[MAXSTRINGLEN - 1]) )
			*t++ = *s++;
		*t = '\0';
		*src = s;

		n = lookup_ipa(tmpstr, result);
		if (n < 0)
			report(LOG_ERR, "can not get IP addr for %s", tmpstr);
		return n;
	}
#endif

	/*
	 * Parse an address in Internet format:
	 *	a.b.c.d
	 *	a.b.c	(with c treated as 16-bits)
	 *	a.b	(with b treated as 24 bits)
	 */
	pp = parts;
  loop:
	/* If it's not a digit, return error. */
	if (!isdigit(**src))
		return -1;
	*pp++ = get_u_long(src);
	if (**src == '.') {
		if (pp < (parts + 4)) {
			(*src)++;
			goto loop;
		}
		return (-1);
	}
#if 0
	/* This is handled by the caller. */
	if (**src && !(isspace(**src) || (**src == ':'))) {
		return (-1);
	}
#endif

	/*
	 * Construct the address according to
	 * the number of parts specified.
	 */
	n = pp - parts;
	switch (n) {
	case 1:					/* a -- 32 bits */
		value = parts[0];
		break;
	case 2:					/* a.b -- 8.24 bits */
		value = (parts[0] << 24) | (parts[1] & 0xFFFFFF);
		break;
	case 3:					/* a.b.c -- 8.8.16 bits */
		value = (parts[0] << 24) | ((parts[1] & 0xFF) << 16) |
			(parts[2] & 0xFFFF);
		break;
	case 4:					/* a.b.c.d -- 8.8.8.8 bits */
		value = (parts[0] << 24) | ((parts[1] & 0xFF) << 16) |
			((parts[2] & 0xFF) << 8) | (parts[3] & 0xFF);
		break;
	default:
		return (-1);
	}
	*result = htonl(value);
	return (0);
}



/*
 * "src" points to a pointer which in turn points to a hexadecimal ASCII
 * string.  This string is interpreted as a hardware address and returned
 * as a pointer to the actual hardware address, represented as an array of
 * bytes.
 *
 * The ASCII string must have the proper number of digits for the specified
 * hardware type (e.g. twelve digits for a 48-bit Ethernet address).
 * Two-digit sequences (bytes) may be separated with periods (.)  and/or
 * prefixed with '0x' for readability, but this is not required.
 *
 * For bad addresses, the pointer which "src" points to is updated to point
 * to the start of the first two-digit sequence which was bad, and the
 * function returns a NULL pointer.
 */

PRIVATE byte *
prs_haddr(src, htype)
	char **src;
	u_int htype;
{
	static byte haddr[MAXHADDRLEN];
	byte *hap;
	char tmpstr[MAXSTRINGLEN];
	u_int tmplen;
	unsigned hal;
	char *p;

	hal = haddrlength(htype);	/* Get length of this address type */
	if (hal <= 0) {
		report(LOG_ERR, "Invalid addr type for HW addr parse");
		return NULL;
	}
	tmplen = sizeof(tmpstr);
	get_string(src, tmpstr, &tmplen);
	p = tmpstr;

#if 1	/* XXX - experimental */
	/* If it's a valid host name, try to lookup the HW address. */
	if (goodname(p)) {
		/* Lookup Hardware Address for hostname. */
		if ((hap = lookup_hwa(p, htype)) != NULL)
			return hap; /* success */
		report(LOG_ERR, "Add 0x prefix if hex value starts with A-F");
		/* OK, assume it must be numeric. */
	}
#endif

	hap = haddr;
	while (hap < haddr + hal) {
		if (*p == '.')
			p++;
		if (interp_byte(&p, hap++) < 0) {
			return NULL;
		}
	}
	return haddr;
}



/*
 * "src" is a pointer to a character pointer which in turn points to a
 * hexadecimal ASCII representation of a byte.  This byte is read, the
 * character pointer is updated, and the result is deposited into the
 * byte pointed to by "retbyte".
 *
 * The usual '0x' notation is allowed but not required.  The number must be
 * a two digit hexadecimal number.  If the number is invalid, "src" and
 * "retbyte" are left untouched and -1 is returned as the function value.
 * Successful calls return 0.
 */

PRIVATE int
interp_byte(src, retbyte)
	char **src;
	byte *retbyte;
{
	int v;

	if ((*src)[0] == '0' &&
		((*src)[1] == 'x' ||
		 (*src)[1] == 'X')) {
		(*src) += 2;			/* allow 0x for hex, but don't require it */
	}
	if (!isxdigit((*src)[0]) || !isxdigit((*src)[1])) {
		return -1;
	}
	if (sscanf(*src, "%2x", &v) != 1) {
		return -1;
	}
	(*src) += 2;
	*retbyte = (byte) (v & 0xFF);
	return 0;
}



/*
 * The parameter "src" points to a character pointer which points to an
 * ASCII string representation of an unsigned number.  The number is
 * returned as an unsigned long and the character pointer is updated to
 * point to the first illegal character.
 */

PRIVATE u_int32
get_u_long(src)
	char **src;
{
	register u_int32 value, base;
	char c;

	/*
	 * Collect number up to first illegal character.  Values are specified
	 * as for C:  0x=hex, 0=octal, other=decimal.
	 */
	value = 0;
	base = 10;
	if (**src == '0') {
		base = 8;
		(*src)++;
	}
	if (**src == 'x' || **src == 'X') {
		base = 16;
		(*src)++;
	}
	while ((c = **src)) {
		if (isdigit(c)) {
			value = (value * base) + (c - '0');
			(*src)++;
			continue;
		}
		if (base == 16 && isxdigit(c)) {
			value = (value << 4) + ((c & ~32) + 10 - 'A');
			(*src)++;
			continue;
		}
		break;
	}
	return value;
}



/*
 * Routines for deletion of data associated with the main data structure.
 */


/*
 * Frees the entire host data structure given.  Does nothing if the passed
 * pointer is NULL.
 */

PRIVATE void
free_host(hmp)
	hash_datum *hmp;
{
	struct host *hostptr = (struct host *) hmp;
	if (hostptr == NULL)
		return;
	assert(hostptr->linkcount > 0);
	if (--(hostptr->linkcount))
		return;					/* Still has references */
	del_iplist(hostptr->cookie_server);
	del_iplist(hostptr->domain_server);
	del_iplist(hostptr->gateway);
	del_iplist(hostptr->impress_server);
	del_iplist(hostptr->log_server);
	del_iplist(hostptr->lpr_server);
	del_iplist(hostptr->name_server);
	del_iplist(hostptr->rlp_server);
	del_iplist(hostptr->time_server);
	del_iplist(hostptr->nis_server);
	del_iplist(hostptr->ntp_server);

	/*
	 * XXX - Add new tags here
	 * (if the value is an IP list)
	 */

	del_string(hostptr->hostname);
	del_string(hostptr->homedir);
	del_string(hostptr->bootfile);
	del_string(hostptr->tftpdir);
	del_string(hostptr->root_path);
	del_string(hostptr->domain_name);
	del_string(hostptr->dump_file);
	del_string(hostptr->exten_file);
	del_string(hostptr->nis_domain);

#ifdef	YORK_EX_OPTION
	del_string(hostptr->exec_file);
#endif

	/*
	 * XXX - Add new tags here
	 * (if it is a shared string)
	 */

	del_bindata(hostptr->generic);
	free((char *) hostptr);
}



/*
 * Decrements the linkcount on the given IP address data structure.  If the
 * linkcount goes to zero, the memory associated with the data is freed.
 */

PRIVATE void
del_iplist(iplist)
	struct in_addr_list *iplist;
{
	if (iplist) {
		if (!(--(iplist->linkcount))) {
			free((char *) iplist);
		}
	}
}



/*
 * Decrements the linkcount on a string data structure.  If the count
 * goes to zero, the memory associated with the string is freed.  Does
 * nothing if the passed pointer is NULL.
 */

PRIVATE void
del_string(stringptr)
	struct shared_string *stringptr;
{
	if (stringptr) {
		if (!(--(stringptr->linkcount))) {
			free((char *) stringptr);
		}
	}
}



/*
 * Decrements the linkcount on a shared_bindata data structure.  If the
 * count goes to zero, the memory associated with the data is freed.  Does
 * nothing if the passed pointer is NULL.
 */

PRIVATE void
del_bindata(dataptr)
	struct shared_bindata *dataptr;
{
	if (dataptr) {
		if (!(--(dataptr->linkcount))) {
			free((char *) dataptr);
		}
	}
}




/* smalloc()  --  safe malloc()
 *
 * Always returns a valid pointer (if it returns at all).  The allocated
 * memory is initialized to all zeros.  If malloc() returns an error, a
 * message is printed using the report() function and the program aborts
 * with a status of 1.
 */

PRIVATE char *
smalloc(nbytes)
	unsigned nbytes;
{
	char *retvalue;

	retvalue = malloc(nbytes);
	if (!retvalue) {
		report(LOG_ERR, "malloc() failure -- exiting");
		exit(1);
	}
	bzero(retvalue, nbytes);
	return retvalue;
}


/*
 * Compare function to determine whether two hardware addresses are
 * equivalent.  Returns TRUE if "host1" and "host2" are equivalent, FALSE
 * otherwise.
 *
 * This function is used when retrieving elements from the hardware address
 * hash table.
 */

boolean
hwlookcmp(d1, d2)
	hash_datum *d1, *d2;
{
	struct host *host1 = (struct host *) d1;
	struct host *host2 = (struct host *) d2;

	if (host1->htype != host2->htype) {
		return FALSE;
	}
	if (bcmp(host1->haddr, host2->haddr, haddrlength(host1->htype))) {
		return FALSE;
	}
	return TRUE;
}


/*
 * Compare function for doing IP address hash table lookup.
 */

boolean
iplookcmp(d1, d2)
	hash_datum *d1, *d2;
{
	struct host *host1 = (struct host *) d1;
	struct host *host2 = (struct host *) d2;

	return (host1->iaddr.s_addr == host2->iaddr.s_addr);
}

/*
 * Local Variables:
 * tab-width: 4
 * c-indent-level: 4
 * c-argdecl-indent: 4
 * c-continued-statement-offset: 4
 * c-continued-brace-offset: -4
 * c-label-offset: -4
 * c-brace-offset: 0
 * End:
 */
OpenPOWER on IntegriCloud