1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
|
/*
Samba CIFS implementation
Registry backend for REGF files
Copyright (C) 2005-2007 Jelmer Vernooij, jelmer@samba.org
Copyright (C) 2006-2010 Wilco Baan Hofman, wilco@baanhofman.nl
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "includes.h"
#include "system/filesys.h"
#include "system/time.h"
#include "lib/registry/tdr_regf.h"
#include "librpc/gen_ndr/ndr_security.h"
#include "librpc/gen_ndr/winreg.h"
#include "lib/registry/registry.h"
#include "libcli/security/security.h"
static struct hive_operations reg_backend_regf;
/**
* There are several places on the web where the REGF format is explained;
*
* TODO: Links
*/
/* TODO:
* - Return error codes that make more sense
* - Locking
* - do more things in-memory
*/
/*
* Read HBIN blocks into memory
*/
struct regf_data {
int fd;
struct hbin_block **hbins;
struct regf_hdr *header;
time_t last_write;
};
static WERROR regf_save_hbin(struct regf_data *data, bool flush);
struct regf_key_data {
struct hive_key key;
struct regf_data *hive;
uint32_t offset;
struct nk_block *nk;
};
static struct hbin_block *hbin_by_offset(const struct regf_data *data,
uint32_t offset, uint32_t *rel_offset)
{
unsigned int i;
for (i = 0; data->hbins[i]; i++) {
if (offset >= data->hbins[i]->offset_from_first &&
offset < data->hbins[i]->offset_from_first+
data->hbins[i]->offset_to_next) {
if (rel_offset != NULL)
*rel_offset = offset - data->hbins[i]->offset_from_first - 0x20;
return data->hbins[i];
}
}
return NULL;
}
/**
* Validate a regf header
* For now, do nothing, but we should check the checksum
*/
static uint32_t regf_hdr_checksum(const uint8_t *buffer)
{
uint32_t checksum = 0, x;
unsigned int i;
for (i = 0; i < 0x01FB; i+= 4) {
x = IVAL(buffer, i);
checksum ^= x;
}
return checksum;
}
/**
* Obtain the contents of a HBIN block
*/
static DATA_BLOB hbin_get(const struct regf_data *data, uint32_t offset)
{
DATA_BLOB ret;
struct hbin_block *hbin;
uint32_t rel_offset;
ret.data = NULL;
ret.length = 0;
hbin = hbin_by_offset(data, offset, &rel_offset);
if (hbin == NULL) {
DEBUG(1, ("Can't find HBIN at 0x%04x\n", offset));
return ret;
}
ret.length = IVAL(hbin->data, rel_offset);
if (!(ret.length & 0x80000000)) {
DEBUG(0, ("Trying to use dirty block at 0x%04x\n", offset));
return ret;
}
/* remove high bit */
ret.length = (ret.length ^ 0xffffffff) + 1;
ret.length -= 4; /* 4 bytes for the length... */
ret.data = hbin->data +
(offset - hbin->offset_from_first - 0x20) + 4;
return ret;
}
static bool hbin_get_tdr(struct regf_data *regf, uint32_t offset,
TALLOC_CTX *ctx, tdr_pull_fn_t pull_fn, void *p)
{
struct tdr_pull *pull = tdr_pull_init(regf);
pull->data = hbin_get(regf, offset);
if (!pull->data.data) {
DEBUG(1, ("Unable to get data at 0x%04x\n", offset));
talloc_free(pull);
return false;
}
if (NT_STATUS_IS_ERR(pull_fn(pull, ctx, p))) {
DEBUG(1, ("Error parsing record at 0x%04x using tdr\n",
offset));
talloc_free(pull);
return false;
}
talloc_free(pull);
return true;
}
/* Allocate some new data */
static DATA_BLOB hbin_alloc(struct regf_data *data, uint32_t size,
uint32_t *offset)
{
DATA_BLOB ret;
uint32_t rel_offset = (uint32_t) -1; /* Relative offset ! */
struct hbin_block *hbin = NULL;
unsigned int i;
*offset = 0;
if (size == 0)
return data_blob(NULL, 0);
size += 4; /* Need to include int32 for the length */
/* Allocate as a multiple of 8 */
size = (size + 7) & ~7;
ret.data = NULL;
ret.length = 0;
for (i = 0; (hbin = data->hbins[i]); i++) {
int j;
int32_t my_size;
for (j = 0; j < hbin->offset_to_next-0x20; j+= my_size) {
my_size = IVALS(hbin->data, j);
if (my_size == 0x0) {
DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
return ret;
}
if (my_size % 8 != 0) {
DEBUG(0, ("Encountered non-aligned block!\n"));
}
if (my_size < 0) { /* Used... */
my_size = -my_size;
} else if (my_size == size) { /* exact match */
rel_offset = j;
DEBUG(4, ("Found free block of exact size %d in middle of HBIN\n",
size));
break;
} else if (my_size > size) { /* data will remain */
rel_offset = j;
/* Split this block and mark the next block as free */
SIVAL(hbin->data, rel_offset+size, my_size-size);
DEBUG(4, ("Found free block of size %d (needing %d) in middle of HBIN\n",
my_size, size));
break;
}
}
if (rel_offset != -1)
break;
}
/* No space available in previous hbins,
* allocate new one */
if (data->hbins[i] == NULL) {
DEBUG(4, ("No space available in other HBINs for block of size %d, allocating new HBIN\n",
size));
/* Add extra hbin block */
data->hbins = talloc_realloc(data, data->hbins,
struct hbin_block *, i+2);
hbin = talloc(data->hbins, struct hbin_block);
SMB_ASSERT(hbin != NULL);
data->hbins[i] = hbin;
data->hbins[i+1] = NULL;
/* Set hbin data */
hbin->HBIN_ID = talloc_strdup(hbin, "hbin");
hbin->offset_from_first = (i == 0?0:data->hbins[i-1]->offset_from_first+data->hbins[i-1]->offset_to_next);
hbin->offset_to_next = 0x1000;
hbin->unknown[0] = 0;
hbin->unknown[1] = 0;
unix_to_nt_time(&hbin->last_change, time(NULL));
hbin->block_size = hbin->offset_to_next;
hbin->data = talloc_zero_array(hbin, uint8_t, hbin->block_size - 0x20);
/* Update the regf header */
data->header->last_block += hbin->offset_to_next;
/* Set the next block to it's proper size and set the
* rel_offset for this block */
SIVAL(hbin->data, size, hbin->block_size - size - 0x20);
rel_offset = 0x0;
}
/* Set size and mark as used */
SIVAL(hbin->data, rel_offset, -size);
ret.data = hbin->data + rel_offset + 0x4; /* Skip past length */
ret.length = size - 0x4;
if (offset) {
uint32_t new_rel_offset;
*offset = hbin->offset_from_first + rel_offset + 0x20;
SMB_ASSERT(hbin_by_offset(data, *offset, &new_rel_offset) == hbin);
SMB_ASSERT(new_rel_offset == rel_offset);
}
return ret;
}
/* Store a data blob. Return the offset at which it was stored */
static uint32_t hbin_store (struct regf_data *data, DATA_BLOB blob)
{
uint32_t ret;
DATA_BLOB dest = hbin_alloc(data, blob.length, &ret);
memcpy(dest.data, blob.data, blob.length);
/* Make sure that we have no tailing garbage in the block */
if (dest.length > blob.length) {
memset(dest.data + blob.length, 0, dest.length - blob.length);
}
return ret;
}
static uint32_t hbin_store_tdr(struct regf_data *data,
tdr_push_fn_t push_fn, void *p)
{
struct tdr_push *push = tdr_push_init(data);
uint32_t ret;
if (NT_STATUS_IS_ERR(push_fn(push, p))) {
DEBUG(0, ("Error during push\n"));
return -1;
}
ret = hbin_store(data, push->data);
talloc_free(push);
return ret;
}
/* Free existing data */
static void hbin_free (struct regf_data *data, uint32_t offset)
{
int32_t size;
uint32_t rel_offset;
int32_t next_size;
struct hbin_block *hbin;
SMB_ASSERT (offset > 0);
hbin = hbin_by_offset(data, offset, &rel_offset);
if (hbin == NULL)
return;
/* Get original size */
size = IVALS(hbin->data, rel_offset);
if (size > 0) {
DEBUG(1, ("Trying to free already freed block at 0x%04x\n",
offset));
return;
}
/* Mark as unused */
size = -size;
/* If the next block is free, merge into big free block */
if (rel_offset + size < hbin->offset_to_next - 0x20) {
next_size = IVALS(hbin->data, rel_offset+size);
if (next_size > 0) {
size += next_size;
}
}
/* Write block size */
SIVALS(hbin->data, rel_offset, size);
}
/**
* Store a data blob data was already stored, but has changed in size
* Will try to save it at the current location if possible, otherwise
* does a free + store */
static uint32_t hbin_store_resize(struct regf_data *data,
uint32_t orig_offset, DATA_BLOB blob)
{
uint32_t rel_offset;
struct hbin_block *hbin = hbin_by_offset(data, orig_offset,
&rel_offset);
int32_t my_size;
int32_t orig_size;
int32_t needed_size;
int32_t possible_size;
unsigned int i;
SMB_ASSERT(orig_offset > 0);
if (!hbin)
return hbin_store(data, blob);
/* Get original size */
orig_size = -IVALS(hbin->data, rel_offset);
needed_size = blob.length + 4; /* Add int32 containing length */
needed_size = (needed_size + 7) & ~7; /* Align */
/* Fits into current allocated block */
if (orig_size >= needed_size) {
memcpy(hbin->data + rel_offset + 0x4, blob.data, blob.length);
/* If the difference in size is greater than 0x4, split the block
* and free/merge it */
if (orig_size - needed_size > 0x4) {
SIVALS(hbin->data, rel_offset, -needed_size);
SIVALS(hbin->data, rel_offset + needed_size,
needed_size-orig_size);
hbin_free(data, orig_offset + needed_size);
}
return orig_offset;
}
possible_size = orig_size;
/* Check if it can be combined with the next few free records */
for (i = rel_offset; i < hbin->offset_to_next - 0x20; i += my_size) {
if (IVALS(hbin->data, i) < 0) /* Used */
break;
my_size = IVALS(hbin->data, i);
if (my_size == 0x0) {
DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
break;
} else {
possible_size += my_size;
}
if (possible_size >= blob.length) {
SIVAL(hbin->data, rel_offset, -possible_size);
memcpy(hbin->data + rel_offset + 0x4,
blob.data, blob.length);
return orig_offset;
}
}
hbin_free(data, orig_offset);
return hbin_store(data, blob);
}
static uint32_t hbin_store_tdr_resize(struct regf_data *regf,
tdr_push_fn_t push_fn,
uint32_t orig_offset, void *p)
{
struct tdr_push *push = tdr_push_init(regf);
uint32_t ret;
if (NT_STATUS_IS_ERR(push_fn(push, p))) {
DEBUG(0, ("Error during push\n"));
return -1;
}
ret = hbin_store_resize(regf, orig_offset, push->data);
talloc_free(push);
return ret;
}
static uint32_t regf_create_lh_hash(const char *name)
{
char *hash_name;
uint32_t ret = 0;
uint16_t i;
hash_name = strupper_talloc(NULL, name);
for (i = 0; *(hash_name + i) != 0; i++) {
ret *= 37;
ret += *(hash_name + i);
}
talloc_free(hash_name);
return ret;
}
static WERROR regf_get_info(TALLOC_CTX *mem_ctx,
const struct hive_key *key,
const char **classname,
uint32_t *num_subkeys,
uint32_t *num_values,
NTTIME *last_mod_time,
uint32_t *max_subkeynamelen,
uint32_t *max_valnamelen,
uint32_t *max_valbufsize)
{
const struct regf_key_data *private_data =
(const struct regf_key_data *)key;
if (num_subkeys != NULL)
*num_subkeys = private_data->nk->num_subkeys;
if (num_values != NULL)
*num_values = private_data->nk->num_values;
if (classname != NULL) {
if (private_data->nk->clsname_offset != -1) {
DATA_BLOB data = hbin_get(private_data->hive,
private_data->nk->clsname_offset);
*classname = talloc_strndup(mem_ctx,
(char*)data.data,
private_data->nk->clsname_length);
W_ERROR_HAVE_NO_MEMORY(*classname);
} else
*classname = NULL;
}
/* TODO: Last mod time */
/* TODO: max valnamelen */
/* TODO: max valbufsize */
/* TODO: max subkeynamelen */
return WERR_OK;
}
static struct regf_key_data *regf_get_key(TALLOC_CTX *ctx,
struct regf_data *regf,
uint32_t offset)
{
struct nk_block *nk;
struct regf_key_data *ret;
ret = talloc_zero(ctx, struct regf_key_data);
ret->key.ops = ®_backend_regf;
ret->hive = talloc_reference(ret, regf);
ret->offset = offset;
nk = talloc(ret, struct nk_block);
if (nk == NULL)
return NULL;
ret->nk = nk;
if (!hbin_get_tdr(regf, offset, nk,
(tdr_pull_fn_t)tdr_pull_nk_block, nk)) {
DEBUG(0, ("Unable to find HBIN data for offset 0x%x\n", offset));
return NULL;
}
if (strcmp(nk->header, "nk") != 0) {
DEBUG(0, ("Expected nk record, got %s\n", nk->header));
talloc_free(ret);
return NULL;
}
return ret;
}
static WERROR regf_get_value(TALLOC_CTX *ctx, struct hive_key *key,
uint32_t idx, const char **name,
uint32_t *data_type, DATA_BLOB *data)
{
const struct regf_key_data *private_data =
(const struct regf_key_data *)key;
struct vk_block *vk;
struct regf_data *regf = private_data->hive;
uint32_t vk_offset;
DATA_BLOB tmp;
if (idx >= private_data->nk->num_values)
return WERR_NO_MORE_ITEMS;
tmp = hbin_get(regf, private_data->nk->values_offset);
if (!tmp.data) {
DEBUG(0, ("Unable to find value list at 0x%x\n",
private_data->nk->values_offset));
return WERR_GENERAL_FAILURE;
}
if (tmp.length < private_data->nk->num_values * 4) {
DEBUG(1, ("Value counts mismatch\n"));
}
vk_offset = IVAL(tmp.data, idx * 4);
vk = talloc(NULL, struct vk_block);
W_ERROR_HAVE_NO_MEMORY(vk);
if (!hbin_get_tdr(regf, vk_offset, vk,
(tdr_pull_fn_t)tdr_pull_vk_block, vk)) {
DEBUG(0, ("Unable to get VK block at 0x%x\n", vk_offset));
talloc_free(vk);
return WERR_GENERAL_FAILURE;
}
/* FIXME: name character set ?*/
if (name != NULL) {
*name = talloc_strndup(ctx, vk->data_name, vk->name_length);
W_ERROR_HAVE_NO_MEMORY(*name);
}
if (data_type != NULL)
*data_type = vk->data_type;
if (vk->data_length & 0x80000000) {
/* this is data of type "REG_DWORD" or "REG_DWORD_BIG_ENDIAN" */
data->data = talloc_size(ctx, sizeof(uint32_t));
W_ERROR_HAVE_NO_MEMORY(data->data);
SIVAL(data->data, 0, vk->data_offset);
data->length = sizeof(uint32_t);
} else {
*data = hbin_get(regf, vk->data_offset);
}
if (data->length < vk->data_length) {
DEBUG(1, ("Read data less than indicated data length!\n"));
}
talloc_free(vk);
return WERR_OK;
}
static WERROR regf_get_value_by_name(TALLOC_CTX *mem_ctx,
struct hive_key *key, const char *name,
uint32_t *type, DATA_BLOB *data)
{
unsigned int i;
const char *vname;
WERROR error;
/* FIXME: Do binary search? Is this list sorted at all? */
for (i = 0; W_ERROR_IS_OK(error = regf_get_value(mem_ctx, key, i,
&vname, type, data));
i++) {
if (!strcmp(vname, name))
return WERR_OK;
}
if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
return WERR_BADFILE;
return error;
}
static WERROR regf_get_subkey_by_index(TALLOC_CTX *ctx,
const struct hive_key *key,
uint32_t idx, const char **name,
const char **classname,
NTTIME *last_mod_time)
{
DATA_BLOB data;
struct regf_key_data *ret;
const struct regf_key_data *private_data = (const struct regf_key_data *)key;
struct nk_block *nk = private_data->nk;
uint32_t key_off=0;
if (idx >= nk->num_subkeys)
return WERR_NO_MORE_ITEMS;
/* Make sure that we don't crash if the key is empty */
if (nk->subkeys_offset == -1) {
return WERR_NO_MORE_ITEMS;
}
data = hbin_get(private_data->hive, nk->subkeys_offset);
if (!data.data) {
DEBUG(0, ("Unable to find subkey list at 0x%x\n",
nk->subkeys_offset));
return WERR_GENERAL_FAILURE;
}
if (!strncmp((char *)data.data, "li", 2)) {
struct li_block li;
struct tdr_pull *pull = tdr_pull_init(private_data->hive);
DEBUG(10, ("Subkeys in LI list\n"));
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, nk, &li))) {
DEBUG(0, ("Error parsing LI list\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
talloc_free(pull);
SMB_ASSERT(!strncmp(li.header, "li", 2));
if (li.key_count != nk->num_subkeys) {
DEBUG(0, ("Subkey counts don't match\n"));
return WERR_GENERAL_FAILURE;
}
key_off = li.nk_offset[idx];
} else if (!strncmp((char *)data.data, "lf", 2)) {
struct lf_block lf;
struct tdr_pull *pull = tdr_pull_init(private_data->hive);
DEBUG(10, ("Subkeys in LF list\n"));
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, nk, &lf))) {
DEBUG(0, ("Error parsing LF list\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
talloc_free(pull);
SMB_ASSERT(!strncmp(lf.header, "lf", 2));
if (lf.key_count != nk->num_subkeys) {
DEBUG(0, ("Subkey counts don't match\n"));
return WERR_GENERAL_FAILURE;
}
key_off = lf.hr[idx].nk_offset;
} else if (!strncmp((char *)data.data, "lh", 2)) {
struct lh_block lh;
struct tdr_pull *pull = tdr_pull_init(private_data->hive);
DEBUG(10, ("Subkeys in LH list\n"));
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, nk, &lh))) {
DEBUG(0, ("Error parsing LH list\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
talloc_free(pull);
SMB_ASSERT(!strncmp(lh.header, "lh", 2));
if (lh.key_count != nk->num_subkeys) {
DEBUG(0, ("Subkey counts don't match\n"));
return WERR_GENERAL_FAILURE;
}
key_off = lh.hr[idx].nk_offset;
} else if (!strncmp((char *)data.data, "ri", 2)) {
struct ri_block ri;
struct tdr_pull *pull = tdr_pull_init(ctx);
uint16_t i;
uint16_t sublist_count = 0;
DEBUG(10, ("Subkeys in RI list\n"));
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull, nk, &ri))) {
DEBUG(0, ("Error parsing RI list\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
SMB_ASSERT(!strncmp(ri.header, "ri", 2));
for (i = 0; i < ri.key_count; i++) {
DATA_BLOB list_data;
/* Get sublist data blob */
list_data = hbin_get(private_data->hive, ri.offset[i]);
if (!list_data.data) {
DEBUG(0, ("Error getting RI list."));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
pull->data = list_data;
if (!strncmp((char *)list_data.data, "li", 2)) {
struct li_block li;
DEBUG(10, ("Subkeys in RI->LI list\n"));
if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull,
nk,
&li))) {
DEBUG(0, ("Error parsing LI list from RI\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
SMB_ASSERT(!strncmp(li.header, "li", 2));
/* Advance to next sublist if necessary */
if (idx >= sublist_count + li.key_count) {
sublist_count += li.key_count;
continue;
}
key_off = li.nk_offset[idx - sublist_count];
sublist_count += li.key_count;
break;
} else if (!strncmp((char *)list_data.data, "lh", 2)) {
struct lh_block lh;
DEBUG(10, ("Subkeys in RI->LH list\n"));
if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull,
nk,
&lh))) {
DEBUG(0, ("Error parsing LH list from RI\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
SMB_ASSERT(!strncmp(lh.header, "lh", 2));
/* Advance to next sublist if necessary */
if (idx >= sublist_count + lh.key_count) {
sublist_count += lh.key_count;
continue;
}
key_off = lh.hr[idx - sublist_count].nk_offset;
sublist_count += lh.key_count;
break;
} else {
DEBUG(0,("Unknown sublist in ri block\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
}
talloc_free(pull);
if (idx > sublist_count) {
return WERR_NO_MORE_ITEMS;
}
} else {
DEBUG(0, ("Unknown type for subkey list (0x%04x): %c%c\n",
nk->subkeys_offset, data.data[0], data.data[1]));
return WERR_GENERAL_FAILURE;
}
ret = regf_get_key (ctx, private_data->hive, key_off);
if (classname != NULL) {
if (ret->nk->clsname_offset != -1) {
DATA_BLOB db = hbin_get(ret->hive,
ret->nk->clsname_offset);
*classname = talloc_strndup(ctx,
(char*)db.data,
ret->nk->clsname_length);
W_ERROR_HAVE_NO_MEMORY(*classname);
} else
*classname = NULL;
}
if (last_mod_time != NULL)
*last_mod_time = ret->nk->last_change;
if (name != NULL)
*name = talloc_steal(ctx, ret->nk->key_name);
talloc_free(ret);
return WERR_OK;
}
static WERROR regf_match_subkey_by_name(TALLOC_CTX *ctx,
const struct hive_key *key,
uint32_t offset,
const char *name, uint32_t *ret)
{
DATA_BLOB subkey_data;
struct nk_block subkey;
struct tdr_pull *pull;
const struct regf_key_data *private_data =
(const struct regf_key_data *)key;
subkey_data = hbin_get(private_data->hive, offset);
if (!subkey_data.data) {
DEBUG(0, ("Unable to retrieve subkey HBIN\n"));
return WERR_GENERAL_FAILURE;
}
pull = tdr_pull_init(ctx);
pull->data = subkey_data;
if (NT_STATUS_IS_ERR(tdr_pull_nk_block(pull, ctx, &subkey))) {
DEBUG(0, ("Error parsing NK structure.\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
talloc_free(pull);
if (strncmp(subkey.header, "nk", 2)) {
DEBUG(0, ("Not an NK structure.\n"));
return WERR_GENERAL_FAILURE;
}
if (!strcasecmp(subkey.key_name, name)) {
*ret = offset;
} else {
*ret = 0;
}
return WERR_OK;
}
static WERROR regf_get_subkey_by_name(TALLOC_CTX *ctx,
const struct hive_key *key,
const char *name,
struct hive_key **ret)
{
DATA_BLOB data;
const struct regf_key_data *private_data =
(const struct regf_key_data *)key;
struct nk_block *nk = private_data->nk;
uint32_t key_off = 0;
/* Make sure that we don't crash if the key is empty */
if (nk->subkeys_offset == -1) {
return WERR_BADFILE;
}
data = hbin_get(private_data->hive, nk->subkeys_offset);
if (!data.data) {
DEBUG(0, ("Unable to find subkey list\n"));
return WERR_GENERAL_FAILURE;
}
if (!strncmp((char *)data.data, "li", 2)) {
struct li_block li;
struct tdr_pull *pull = tdr_pull_init(ctx);
uint16_t i;
DEBUG(10, ("Subkeys in LI list\n"));
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, nk, &li))) {
DEBUG(0, ("Error parsing LI list\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
talloc_free(pull);
SMB_ASSERT(!strncmp(li.header, "li", 2));
if (li.key_count != nk->num_subkeys) {
DEBUG(0, ("Subkey counts don't match\n"));
return WERR_GENERAL_FAILURE;
}
for (i = 0; i < li.key_count; i++) {
W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
li.nk_offset[i],
name,
&key_off));
if (key_off != 0)
break;
}
if (key_off == 0)
return WERR_BADFILE;
} else if (!strncmp((char *)data.data, "lf", 2)) {
struct lf_block lf;
struct tdr_pull *pull = tdr_pull_init(ctx);
uint16_t i;
DEBUG(10, ("Subkeys in LF list\n"));
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, nk, &lf))) {
DEBUG(0, ("Error parsing LF list\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
talloc_free(pull);
SMB_ASSERT(!strncmp(lf.header, "lf", 2));
if (lf.key_count != nk->num_subkeys) {
DEBUG(0, ("Subkey counts don't match\n"));
return WERR_GENERAL_FAILURE;
}
for (i = 0; i < lf.key_count; i++) {
if (strncmp(lf.hr[i].hash, name, 4)) {
continue;
}
W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
key,
lf.hr[i].nk_offset,
name,
&key_off));
if (key_off != 0)
break;
}
if (key_off == 0)
return WERR_BADFILE;
} else if (!strncmp((char *)data.data, "lh", 2)) {
struct lh_block lh;
struct tdr_pull *pull = tdr_pull_init(ctx);
uint16_t i;
uint32_t hash;
DEBUG(10, ("Subkeys in LH list\n"));
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, nk, &lh))) {
DEBUG(0, ("Error parsing LH list\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
talloc_free(pull);
SMB_ASSERT(!strncmp(lh.header, "lh", 2));
if (lh.key_count != nk->num_subkeys) {
DEBUG(0, ("Subkey counts don't match\n"));
return WERR_GENERAL_FAILURE;
}
hash = regf_create_lh_hash(name);
for (i = 0; i < lh.key_count; i++) {
if (lh.hr[i].base37 != hash) {
continue;
}
W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
key,
lh.hr[i].nk_offset,
name,
&key_off));
if (key_off != 0)
break;
}
if (key_off == 0)
return WERR_BADFILE;
} else if (!strncmp((char *)data.data, "ri", 2)) {
struct ri_block ri;
struct tdr_pull *pull = tdr_pull_init(ctx);
uint16_t i, j;
DEBUG(10, ("Subkeys in RI list\n"));
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull, nk, &ri))) {
DEBUG(0, ("Error parsing RI list\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
SMB_ASSERT(!strncmp(ri.header, "ri", 2));
for (i = 0; i < ri.key_count; i++) {
DATA_BLOB list_data;
/* Get sublist data blob */
list_data = hbin_get(private_data->hive, ri.offset[i]);
if (list_data.data == NULL) {
DEBUG(0, ("Error getting RI list."));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
pull->data = list_data;
if (!strncmp((char *)list_data.data, "li", 2)) {
struct li_block li;
if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull,
nk,
&li))) {
DEBUG(0, ("Error parsing LI list from RI\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
SMB_ASSERT(!strncmp(li.header, "li", 2));
for (j = 0; j < li.key_count; j++) {
W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
li.nk_offset[j],
name,
&key_off));
if (key_off)
break;
}
} else if (!strncmp((char *)list_data.data, "lh", 2)) {
struct lh_block lh;
uint32_t hash;
if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull,
nk,
&lh))) {
DEBUG(0, ("Error parsing LH list from RI\n"));
talloc_free(pull);
return WERR_GENERAL_FAILURE;
}
SMB_ASSERT(!strncmp(lh.header, "lh", 2));
hash = regf_create_lh_hash(name);
for (j = 0; j < lh.key_count; j++) {
if (lh.hr[j].base37 != hash) {
continue;
}
W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
lh.hr[j].nk_offset,
name,
&key_off));
if (key_off)
break;
}
}
if (key_off)
break;
}
talloc_free(pull);
if (!key_off)
return WERR_BADFILE;
} else {
DEBUG(0, ("Unknown subkey list type.\n"));
return WERR_GENERAL_FAILURE;
}
*ret = (struct hive_key *)regf_get_key(ctx, private_data->hive,
key_off);
return WERR_OK;
}
static WERROR regf_set_sec_desc(struct hive_key *key,
const struct security_descriptor *sec_desc)
{
const struct regf_key_data *private_data =
(const struct regf_key_data *)key;
struct sk_block cur_sk, sk, new_sk;
struct regf_data *regf = private_data->hive;
struct nk_block root;
DATA_BLOB data;
uint32_t sk_offset, cur_sk_offset;
bool update_cur_sk = false;
/* Get the root nk */
hbin_get_tdr(regf, regf->header->data_offset, regf,
(tdr_pull_fn_t) tdr_pull_nk_block, &root);
/* Push the security descriptor to a blob */
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf,
sec_desc, (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
DEBUG(0, ("Unable to push security descriptor\n"));
return WERR_GENERAL_FAILURE;
}
/* Get the current security descriptor for the key */
if (!hbin_get_tdr(regf, private_data->nk->sk_offset, regf,
(tdr_pull_fn_t) tdr_pull_sk_block, &cur_sk)) {
DEBUG(0, ("Unable to find security descriptor for current key\n"));
return WERR_BADFILE;
}
/* If there's no change, change nothing. */
if (memcmp(data.data, cur_sk.sec_desc,
MIN(data.length, cur_sk.rec_size)) == 0) {
return WERR_OK;
}
/* Delete the current sk if only this key is using it */
if (cur_sk.ref_cnt == 1) {
/* Get the previous security descriptor for the key */
if (!hbin_get_tdr(regf, cur_sk.prev_offset, regf,
(tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
DEBUG(0, ("Unable to find prev security descriptor for current key\n"));
return WERR_BADFILE;
}
/* Change and store the previous security descriptor */
sk.next_offset = cur_sk.next_offset;
hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
cur_sk.prev_offset, &sk);
/* Get the next security descriptor for the key */
if (!hbin_get_tdr(regf, cur_sk.next_offset, regf,
(tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
DEBUG(0, ("Unable to find next security descriptor for current key\n"));
return WERR_BADFILE;
}
/* Change and store the next security descriptor */
sk.prev_offset = cur_sk.prev_offset;
hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
cur_sk.next_offset, &sk);
hbin_free(regf, private_data->nk->sk_offset);
} else {
/* This key will no longer be referring to this sk */
cur_sk.ref_cnt--;
update_cur_sk = true;
}
sk_offset = root.sk_offset;
do {
cur_sk_offset = sk_offset;
if (!hbin_get_tdr(regf, sk_offset, regf,
(tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
DEBUG(0, ("Unable to find security descriptor\n"));
return WERR_BADFILE;
}
if (memcmp(data.data, sk.sec_desc, MIN(data.length, sk.rec_size)) == 0) {
private_data->nk->sk_offset = sk_offset;
sk.ref_cnt++;
hbin_store_tdr_resize(regf,
(tdr_push_fn_t) tdr_push_sk_block,
sk_offset, &sk);
hbin_store_tdr_resize(regf,
(tdr_push_fn_t) tdr_push_nk_block,
private_data->offset,
private_data->nk);
return WERR_OK;
}
sk_offset = sk.next_offset;
} while (sk_offset != root.sk_offset);
ZERO_STRUCT(new_sk);
new_sk.header = "sk";
new_sk.prev_offset = cur_sk_offset;
new_sk.next_offset = root.sk_offset;
new_sk.ref_cnt = 1;
new_sk.rec_size = data.length;
new_sk.sec_desc = data.data;
sk_offset = hbin_store_tdr(regf,
(tdr_push_fn_t) tdr_push_sk_block,
&new_sk);
if (sk_offset == -1) {
DEBUG(0, ("Error storing sk block\n"));
return WERR_GENERAL_FAILURE;
}
private_data->nk->sk_offset = sk_offset;
if (update_cur_sk) {
hbin_store_tdr_resize(regf,
(tdr_push_fn_t) tdr_push_sk_block,
private_data->nk->sk_offset, &cur_sk);
}
/* Get the previous security descriptor for the key */
if (!hbin_get_tdr(regf, new_sk.prev_offset, regf,
(tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
DEBUG(0, ("Unable to find security descriptor for previous key\n"));
return WERR_BADFILE;
}
/* Change and store the previous security descriptor */
sk.next_offset = sk_offset;
hbin_store_tdr_resize(regf,
(tdr_push_fn_t) tdr_push_sk_block,
cur_sk.prev_offset, &sk);
/* Get the next security descriptor for the key (always root, as we append) */
if (!hbin_get_tdr(regf, new_sk.next_offset, regf,
(tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
DEBUG(0, ("Unable to find security descriptor for current key\n"));
return WERR_BADFILE;
}
/* Change and store the next security descriptor (always root, as we append) */
sk.prev_offset = sk_offset;
hbin_store_tdr_resize(regf,
(tdr_push_fn_t) tdr_push_sk_block,
root.sk_offset, &sk);
/* Store the nk. */
hbin_store_tdr_resize(regf,
(tdr_push_fn_t) tdr_push_sk_block,
private_data->offset, private_data->nk);
return WERR_OK;
}
static WERROR regf_get_sec_desc(TALLOC_CTX *ctx, const struct hive_key *key,
struct security_descriptor **sd)
{
const struct regf_key_data *private_data =
(const struct regf_key_data *)key;
struct sk_block sk;
struct regf_data *regf = private_data->hive;
DATA_BLOB data;
if (!hbin_get_tdr(regf, private_data->nk->sk_offset, ctx,
(tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
DEBUG(0, ("Unable to find security descriptor\n"));
return WERR_GENERAL_FAILURE;
}
if (strcmp(sk.header, "sk") != 0) {
DEBUG(0, ("Expected 'sk', got '%s'\n", sk.header));
return WERR_GENERAL_FAILURE;
}
*sd = talloc(ctx, struct security_descriptor);
W_ERROR_HAVE_NO_MEMORY(*sd);
data.data = sk.sec_desc;
data.length = sk.rec_size;
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_struct_blob(&data, ctx, *sd,
(ndr_pull_flags_fn_t)ndr_pull_security_descriptor))) {
DEBUG(0, ("Error parsing security descriptor\n"));
return WERR_GENERAL_FAILURE;
}
return WERR_OK;
}
static WERROR regf_sl_add_entry(struct regf_data *regf, uint32_t list_offset,
const char *name,
uint32_t key_offset, uint32_t *ret)
{
DATA_BLOB data;
/* Create a new key if necessary */
if (list_offset == -1) {
if (regf->header->version.major != 1) {
DEBUG(0, ("Can't store keys in unknown registry format\n"));
return WERR_NOT_SUPPORTED;
}
if (regf->header->version.minor < 3) {
/* Store LI */
struct li_block li;
ZERO_STRUCT(li);
li.header = "li";
li.key_count = 1;
li.nk_offset = talloc_array(regf, uint32_t, 1);
W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
li.nk_offset[0] = key_offset;
*ret = hbin_store_tdr(regf,
(tdr_push_fn_t) tdr_push_li_block,
&li);
talloc_free(li.nk_offset);
} else if (regf->header->version.minor == 3 ||
regf->header->version.minor == 4) {
/* Store LF */
struct lf_block lf;
ZERO_STRUCT(lf);
lf.header = "lf";
lf.key_count = 1;
lf.hr = talloc_array(regf, struct hash_record, 1);
W_ERROR_HAVE_NO_MEMORY(lf.hr);
lf.hr[0].nk_offset = key_offset;
lf.hr[0].hash = talloc_strndup(lf.hr, name, 4);
W_ERROR_HAVE_NO_MEMORY(lf.hr[0].hash);
*ret = hbin_store_tdr(regf,
(tdr_push_fn_t) tdr_push_lf_block,
&lf);
talloc_free(lf.hr);
} else if (regf->header->version.minor == 5) {
/* Store LH */
struct lh_block lh;
ZERO_STRUCT(lh);
lh.header = "lh";
lh.key_count = 1;
lh.hr = talloc_array(regf, struct lh_hash, 1);
W_ERROR_HAVE_NO_MEMORY(lh.hr);
lh.hr[0].nk_offset = key_offset;
lh.hr[0].base37 = regf_create_lh_hash(name);
*ret = hbin_store_tdr(regf,
(tdr_push_fn_t) tdr_push_lh_block,
&lh);
talloc_free(lh.hr);
}
return WERR_OK;
}
data = hbin_get(regf, list_offset);
if (!data.data) {
DEBUG(0, ("Unable to find subkey list\n"));
return WERR_BADFILE;
}
if (!strncmp((char *)data.data, "li", 2)) {
struct tdr_pull *pull = tdr_pull_init(regf);
struct li_block li;
struct nk_block sub_nk;
int32_t i, j;
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, regf, &li))) {
DEBUG(0, ("Error parsing LI list\n"));
talloc_free(pull);
return WERR_BADFILE;
}
talloc_free(pull);
if (strncmp(li.header, "li", 2) != 0) {
abort();
DEBUG(0, ("LI header corrupt\n"));
return WERR_BADFILE;
}
/*
* Find the position to store the pointer
* Extensive testing reveils that at least on windows 7 subkeys
* *MUST* be stored in alphabetical order
*/
for (i = 0; i < li.key_count; i++) {
/* Get the nk */
hbin_get_tdr(regf, li.nk_offset[i], regf,
(tdr_pull_fn_t) tdr_pull_nk_block, &sub_nk);
if (strcasecmp(name, sub_nk.key_name) < 0) {
break;
}
}
li.nk_offset = talloc_realloc(regf, li.nk_offset,
uint32_t, li.key_count+1);
W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
/* Move everything behind this offset */
for (j = li.key_count - 1; j >= i; j--) {
li.nk_offset[j+1] = li.nk_offset[j];
}
li.nk_offset[i] = key_offset;
li.key_count++;
*ret = hbin_store_tdr_resize(regf,
(tdr_push_fn_t)tdr_push_li_block,
list_offset, &li);
talloc_free(li.nk_offset);
} else if (!strncmp((char *)data.data, "lf", 2)) {
struct tdr_pull *pull = tdr_pull_init(regf);
struct lf_block lf;
struct nk_block sub_nk;
int32_t i, j;
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, regf, &lf))) {
DEBUG(0, ("Error parsing LF list\n"));
talloc_free(pull);
return WERR_BADFILE;
}
talloc_free(pull);
SMB_ASSERT(!strncmp(lf.header, "lf", 2));
/*
* Find the position to store the hash record
* Extensive testing reveils that at least on windows 7 subkeys
* *MUST* be stored in alphabetical order
*/
for (i = 0; i < lf.key_count; i++) {
/* Get the nk */
hbin_get_tdr(regf, lf.hr[i].nk_offset, regf,
(tdr_pull_fn_t) tdr_pull_nk_block, &sub_nk);
if (strcasecmp(name, sub_nk.key_name) < 0) {
break;
}
}
lf.hr = talloc_realloc(regf, lf.hr, struct hash_record,
lf.key_count+1);
W_ERROR_HAVE_NO_MEMORY(lf.hr);
/* Move everything behind this hash record */
for (j = lf.key_count - 1; j >= i; j--) {
lf.hr[j+1] = lf.hr[j];
}
lf.hr[i].nk_offset = key_offset;
lf.hr[i].hash = talloc_strndup(lf.hr, name, 4);
W_ERROR_HAVE_NO_MEMORY(lf.hr[lf.key_count].hash);
lf.key_count++;
*ret = hbin_store_tdr_resize(regf,
(tdr_push_fn_t)tdr_push_lf_block,
list_offset, &lf);
talloc_free(lf.hr);
} else if (!strncmp((char *)data.data, "lh", 2)) {
struct tdr_pull *pull = tdr_pull_init(regf);
struct lh_block lh;
struct nk_block sub_nk;
int32_t i, j;
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, regf, &lh))) {
DEBUG(0, ("Error parsing LH list\n"));
talloc_free(pull);
return WERR_BADFILE;
}
talloc_free(pull);
SMB_ASSERT(!strncmp(lh.header, "lh", 2));
/*
* Find the position to store the hash record
* Extensive testing reveils that at least on windows 7 subkeys
* *MUST* be stored in alphabetical order
*/
for (i = 0; i < lh.key_count; i++) {
/* Get the nk */
hbin_get_tdr(regf, lh.hr[i].nk_offset, regf,
(tdr_pull_fn_t) tdr_pull_nk_block, &sub_nk);
if (strcasecmp(name, sub_nk.key_name) < 0) {
break;
}
}
lh.hr = talloc_realloc(regf, lh.hr, struct lh_hash,
lh.key_count+1);
W_ERROR_HAVE_NO_MEMORY(lh.hr);
/* Move everything behind this hash record */
for (j = lh.key_count - 1; j >= i; j--) {
lh.hr[j+1] = lh.hr[j];
}
lh.hr[i].nk_offset = key_offset;
lh.hr[i].base37 = regf_create_lh_hash(name);
lh.key_count++;
*ret = hbin_store_tdr_resize(regf,
(tdr_push_fn_t)tdr_push_lh_block,
list_offset, &lh);
talloc_free(lh.hr);
} else if (!strncmp((char *)data.data, "ri", 2)) {
/* FIXME */
DEBUG(0, ("Adding to 'ri' subkey list is not supported yet.\n"));
return WERR_NOT_SUPPORTED;
} else {
DEBUG(0, ("Cannot add to unknown subkey list\n"));
return WERR_BADFILE;
}
return WERR_OK;
}
static WERROR regf_sl_del_entry(struct regf_data *regf, uint32_t list_offset,
uint32_t key_offset, uint32_t *ret)
{
DATA_BLOB data;
data = hbin_get(regf, list_offset);
if (!data.data) {
DEBUG(0, ("Unable to find subkey list\n"));
return WERR_BADFILE;
}
if (strncmp((char *)data.data, "li", 2) == 0) {
struct li_block li;
struct tdr_pull *pull = tdr_pull_init(regf);
uint16_t i;
bool found_offset = false;
DEBUG(10, ("Subkeys in LI list\n"));
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, regf, &li))) {
DEBUG(0, ("Error parsing LI list\n"));
talloc_free(pull);
return WERR_BADFILE;
}
talloc_free(pull);
SMB_ASSERT(!strncmp(li.header, "li", 2));
for (i = 0; i < li.key_count; i++) {
if (found_offset) {
li.nk_offset[i-1] = li.nk_offset[i];
}
if (li.nk_offset[i] == key_offset) {
found_offset = true;
continue;
}
}
if (!found_offset) {
DEBUG(2, ("Subkey not found\n"));
return WERR_BADFILE;
}
li.key_count--;
/* If the there are no entries left, free the subkey list */
if (li.key_count == 0) {
hbin_free(regf, list_offset);
*ret = -1;
}
/* Store li block */
*ret = hbin_store_tdr_resize(regf,
(tdr_push_fn_t) tdr_push_li_block,
list_offset, &li);
} else if (strncmp((char *)data.data, "lf", 2) == 0) {
struct lf_block lf;
struct tdr_pull *pull = tdr_pull_init(regf);
uint16_t i;
bool found_offset = false;
DEBUG(10, ("Subkeys in LF list\n"));
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, regf, &lf))) {
DEBUG(0, ("Error parsing LF list\n"));
talloc_free(pull);
return WERR_BADFILE;
}
talloc_free(pull);
SMB_ASSERT(!strncmp(lf.header, "lf", 2));
for (i = 0; i < lf.key_count; i++) {
if (found_offset) {
lf.hr[i-1] = lf.hr[i];
continue;
}
if (lf.hr[i].nk_offset == key_offset) {
found_offset = 1;
continue;
}
}
if (!found_offset) {
DEBUG(2, ("Subkey not found\n"));
return WERR_BADFILE;
}
lf.key_count--;
/* If the there are no entries left, free the subkey list */
if (lf.key_count == 0) {
hbin_free(regf, list_offset);
*ret = -1;
return WERR_OK;
}
/* Store lf block */
*ret = hbin_store_tdr_resize(regf,
(tdr_push_fn_t) tdr_push_lf_block,
list_offset, &lf);
} else if (strncmp((char *)data.data, "lh", 2) == 0) {
struct lh_block lh;
struct tdr_pull *pull = tdr_pull_init(regf);
uint16_t i;
bool found_offset = false;
DEBUG(10, ("Subkeys in LH list\n"));
pull->data = data;
if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, regf, &lh))) {
DEBUG(0, ("Error parsing LF list\n"));
talloc_free(pull);
return WERR_BADFILE;
}
talloc_free(pull);
SMB_ASSERT(!strncmp(lh.header, "lh", 2));
for (i = 0; i < lh.key_count; i++) {
if (found_offset) {
lh.hr[i-1] = lh.hr[i];
continue;
}
if (lh.hr[i].nk_offset == key_offset) {
found_offset = 1;
continue;
}
}
if (!found_offset) {
DEBUG(0, ("Subkey not found\n"));
return WERR_BADFILE;
}
lh.key_count--;
/* If the there are no entries left, free the subkey list */
if (lh.key_count == 0) {
hbin_free(regf, list_offset);
*ret = -1;
return WERR_OK;
}
/* Store lh block */
*ret = hbin_store_tdr_resize(regf,
(tdr_push_fn_t) tdr_push_lh_block,
list_offset, &lh);
} else if (strncmp((char *)data.data, "ri", 2) == 0) {
/* FIXME */
DEBUG(0, ("Sorry, deletion from ri block is not supported yet.\n"));
return WERR_NOT_SUPPORTED;
} else {
DEBUG (0, ("Unknown header found in subkey list.\n"));
return WERR_BADFILE;
}
return WERR_OK;
}
static WERROR regf_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
const char *name)
{
struct regf_key_data *private_data = (struct regf_key_data *)key;
struct regf_data *regf = private_data->hive;
struct nk_block *nk = private_data->nk;
struct vk_block vk;
uint32_t vk_offset;
bool found_offset = false;
DATA_BLOB values;
unsigned int i;
if (nk->values_offset == -1) {
return WERR_BADFILE;
}
values = hbin_get(regf, nk->values_offset);
for (i = 0; i < nk->num_values; i++) {
if (found_offset) {
((uint32_t *)values.data)[i-1] = ((uint32_t *) values.data)[i];
} else {
vk_offset = IVAL(values.data, i * 4);
if (!hbin_get_tdr(regf, vk_offset, private_data,
(tdr_pull_fn_t)tdr_pull_vk_block,
&vk)) {
DEBUG(0, ("Unable to get VK block at %d\n",
vk_offset));
return WERR_BADFILE;
}
if (strcmp(vk.data_name, name) == 0) {
hbin_free(regf, vk_offset);
found_offset = true;
}
}
}
if (!found_offset) {
return WERR_BADFILE;
} else {
nk->num_values--;
values.length = (nk->num_values)*4;
}
/* Store values list and nk */
if (nk->num_values == 0) {
hbin_free(regf, nk->values_offset);
nk->values_offset = -1;
} else {
nk->values_offset = hbin_store_resize(regf,
nk->values_offset,
values);
}
hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
private_data->offset, nk);
return regf_save_hbin(private_data->hive, 0);
}
static WERROR regf_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
const char *name)
{
const struct regf_key_data *private_data =
(const struct regf_key_data *)parent;
struct regf_key_data *key;
struct nk_block *parent_nk;
WERROR error;
SMB_ASSERT(private_data);
parent_nk = private_data->nk;
if (parent_nk->subkeys_offset == -1) {
DEBUG(4, ("Subkey list is empty, this key cannot contain subkeys.\n"));
return WERR_BADFILE;
}
/* Find the key */
if (!W_ERROR_IS_OK(regf_get_subkey_by_name(parent_nk, parent, name,
(struct hive_key **)&key))) {
DEBUG(2, ("Key '%s' not found\n", name));
return WERR_BADFILE;
}
if (key->nk->subkeys_offset != -1) {
char *sk_name;
struct hive_key *sk = (struct hive_key *)key;
unsigned int i = key->nk->num_subkeys;
while (i--) {
/* Get subkey information. */
error = regf_get_subkey_by_index(parent_nk, sk, 0,
(const char **)&sk_name,
NULL, NULL);
if (!W_ERROR_IS_OK(error)) {
DEBUG(0, ("Can't retrieve subkey by index.\n"));
return error;
}
/* Delete subkey. */
error = regf_del_key(NULL, sk, sk_name);
if (!W_ERROR_IS_OK(error)) {
DEBUG(0, ("Can't delete key '%s'.\n", sk_name));
return error;
}
talloc_free(sk_name);
}
}
if (key->nk->values_offset != -1) {
char *val_name;
struct hive_key *sk = (struct hive_key *)key;
DATA_BLOB data;
unsigned int i = key->nk->num_values;
while (i--) {
/* Get value information. */
error = regf_get_value(parent_nk, sk, 0,
(const char **)&val_name,
NULL, &data);
if (!W_ERROR_IS_OK(error)) {
DEBUG(0, ("Can't retrieve value by index.\n"));
return error;
}
/* Delete value. */
error = regf_del_value(NULL, sk, val_name);
if (!W_ERROR_IS_OK(error)) {
DEBUG(0, ("Can't delete value '%s'.\n", val_name));
return error;
}
talloc_free(val_name);
}
}
/* Delete it from the subkey list. */
error = regf_sl_del_entry(private_data->hive, parent_nk->subkeys_offset,
key->offset, &parent_nk->subkeys_offset);
if (!W_ERROR_IS_OK(error)) {
DEBUG(0, ("Can't store new subkey list for parent key. Won't delete.\n"));
return error;
}
/* Re-store parent key */
parent_nk->num_subkeys--;
hbin_store_tdr_resize(private_data->hive,
(tdr_push_fn_t) tdr_push_nk_block,
private_data->offset, parent_nk);
if (key->nk->clsname_offset != -1) {
hbin_free(private_data->hive, key->nk->clsname_offset);
}
hbin_free(private_data->hive, key->offset);
return regf_save_hbin(private_data->hive, 0);
}
static WERROR regf_add_key(TALLOC_CTX *ctx, const struct hive_key *parent,
const char *name, const char *classname,
struct security_descriptor *sec_desc,
struct hive_key **ret)
{
const struct regf_key_data *private_data =
(const struct regf_key_data *)parent;
struct nk_block *parent_nk = private_data->nk, nk;
struct nk_block *root;
struct regf_data *regf = private_data->hive;
uint32_t offset;
WERROR error;
nk.header = "nk";
nk.type = REG_SUB_KEY;
unix_to_nt_time(&nk.last_change, time(NULL));
nk.uk1 = 0;
nk.parent_offset = private_data->offset;
nk.num_subkeys = 0;
nk.uk2 = 0;
nk.subkeys_offset = -1;
nk.unknown_offset = -1;
nk.num_values = 0;
nk.values_offset = -1;
memset(nk.unk3, 0, sizeof(nk.unk3));
nk.clsname_offset = -1; /* FIXME: fill in */
nk.clsname_length = 0;
nk.key_name = name;
/* Get the security descriptor of the root key */
root = talloc_zero(ctx, struct nk_block);
W_ERROR_HAVE_NO_MEMORY(root);
if (!hbin_get_tdr(regf, regf->header->data_offset, root,
(tdr_pull_fn_t)tdr_pull_nk_block, root)) {
DEBUG(0, ("Unable to find HBIN data for offset 0x%x\n",
regf->header->data_offset));
return WERR_GENERAL_FAILURE;
}
nk.sk_offset = root->sk_offset;
talloc_free(root);
/* Store the new nk key */
offset = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_nk_block, &nk);
error = regf_sl_add_entry(regf, parent_nk->subkeys_offset, name, offset,
&parent_nk->subkeys_offset);
if (!W_ERROR_IS_OK(error)) {
hbin_free(regf, offset);
return error;
}
parent_nk->num_subkeys++;
/* Since the subkey offset of the parent can change, store it again */
hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
nk.parent_offset, parent_nk);
*ret = (struct hive_key *)regf_get_key(ctx, regf, offset);
DEBUG(9, ("Storing key %s\n", name));
return regf_save_hbin(private_data->hive, 0);
}
static WERROR regf_set_value(struct hive_key *key, const char *name,
uint32_t type, const DATA_BLOB data)
{
struct regf_key_data *private_data = (struct regf_key_data *)key;
struct regf_data *regf = private_data->hive;
struct nk_block *nk = private_data->nk;
struct vk_block vk;
uint32_t i;
uint32_t tmp_vk_offset, vk_offset, old_vk_offset = (uint32_t) -1;
DATA_BLOB values;
ZERO_STRUCT(vk);
/* find the value offset, if it exists */
if (nk->values_offset != -1) {
values = hbin_get(regf, nk->values_offset);
for (i = 0; i < nk->num_values; i++) {
tmp_vk_offset = IVAL(values.data, i * 4);
if (!hbin_get_tdr(regf, tmp_vk_offset, private_data,
(tdr_pull_fn_t)tdr_pull_vk_block,
&vk)) {
DEBUG(0, ("Unable to get VK block at 0x%x\n",
tmp_vk_offset));
return WERR_GENERAL_FAILURE;
}
if (strcmp(vk.data_name, name) == 0) {
old_vk_offset = tmp_vk_offset;
break;
}
}
}
/* If it's new, create the vk struct, if it's old, free the old data. */
if (old_vk_offset == -1) {
vk.header = "vk";
vk.name_length = strlen(name);
if (name != NULL && name[0] != 0) {
vk.flag = 1;
vk.data_name = name;
} else {
vk.data_name = NULL;
vk.flag = 0;
}
} else {
/* Free data, if any */
if (!(vk.data_length & 0x80000000)) {
hbin_free(regf, vk.data_offset);
}
}
/* Set the type and data */
vk.data_length = data.length;
vk.data_type = type;
if ((type == REG_DWORD) || (type == REG_DWORD_BIG_ENDIAN)) {
if (vk.data_length != sizeof(uint32_t)) {
DEBUG(0, ("DWORD or DWORD_BIG_ENDIAN value with size other than 4 byte!\n"));
return WERR_NOT_SUPPORTED;
}
vk.data_length |= 0x80000000;
vk.data_offset = IVAL(data.data, 0);
} else {
/* Store data somewhere */
vk.data_offset = hbin_store(regf, data);
}
if (old_vk_offset == -1) {
/* Store new vk */
vk_offset = hbin_store_tdr(regf,
(tdr_push_fn_t) tdr_push_vk_block,
&vk);
} else {
/* Store vk at offset */
vk_offset = hbin_store_tdr_resize(regf,
(tdr_push_fn_t) tdr_push_vk_block,
old_vk_offset ,&vk);
}
/* Re-allocate the value list */
if (nk->values_offset == -1) {
nk->values_offset = hbin_store_tdr(regf,
(tdr_push_fn_t) tdr_push_uint32,
&vk_offset);
nk->num_values = 1;
} else {
/* Change if we're changing, otherwise we're adding the value */
if (old_vk_offset != -1) {
/* Find and overwrite the offset. */
for (i = 0; i < nk->num_values; i++) {
if (IVAL(values.data, i * 4) == old_vk_offset) {
SIVAL(values.data, i * 4, vk_offset);
break;
}
}
} else {
/* Create a new value list */
DATA_BLOB value_list;
value_list.length = (nk->num_values+1)*4;
value_list.data = (uint8_t *)talloc_array(private_data,
uint32_t,
nk->num_values+1);
W_ERROR_HAVE_NO_MEMORY(value_list.data);
memcpy(value_list.data, values.data, nk->num_values * 4);
SIVAL(value_list.data, nk->num_values * 4, vk_offset);
nk->num_values++;
nk->values_offset = hbin_store_resize(regf,
nk->values_offset,
value_list);
}
}
hbin_store_tdr_resize(regf,
(tdr_push_fn_t) tdr_push_nk_block,
private_data->offset, nk);
return regf_save_hbin(private_data->hive, 0);
}
static WERROR regf_save_hbin(struct regf_data *regf, bool flush)
{
struct tdr_push *push = tdr_push_init(regf);
unsigned int i;
W_ERROR_HAVE_NO_MEMORY(push);
/* Only write once every 5 seconds, or when flush is set */
if (!flush && regf->last_write + 5 >= time(NULL)) {
return WERR_OK;
}
regf->last_write = time(NULL);
if (lseek(regf->fd, 0, SEEK_SET) == -1) {
DEBUG(0, ("Error lseeking in regf file\n"));
return WERR_GENERAL_FAILURE;
}
/* Recompute checksum */
if (NT_STATUS_IS_ERR(tdr_push_regf_hdr(push, regf->header))) {
DEBUG(0, ("Failed to push regf header\n"));
return WERR_GENERAL_FAILURE;
}
regf->header->chksum = regf_hdr_checksum(push->data.data);
talloc_free(push);
if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
(tdr_push_fn_t)tdr_push_regf_hdr,
regf->header))) {
DEBUG(0, ("Error writing registry file header\n"));
return WERR_GENERAL_FAILURE;
}
if (lseek(regf->fd, 0x1000, SEEK_SET) == -1) {
DEBUG(0, ("Error lseeking to 0x1000 in regf file\n"));
return WERR_GENERAL_FAILURE;
}
for (i = 0; regf->hbins[i]; i++) {
if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
(tdr_push_fn_t)tdr_push_hbin_block,
regf->hbins[i]))) {
DEBUG(0, ("Error writing HBIN block\n"));
return WERR_GENERAL_FAILURE;
}
}
return WERR_OK;
}
WERROR reg_create_regf_file(TALLOC_CTX *parent_ctx,
const char *location,
int minor_version, struct hive_key **key)
{
struct regf_data *regf;
struct regf_hdr *regf_hdr;
struct nk_block nk;
struct sk_block sk;
WERROR error;
DATA_BLOB data;
struct security_descriptor *sd;
uint32_t sk_offset;
regf = (struct regf_data *)talloc_zero(NULL, struct regf_data);
W_ERROR_HAVE_NO_MEMORY(regf);
DEBUG(5, ("Attempting to create registry file\n"));
/* Get the header */
regf->fd = creat(location, 0644);
if (regf->fd == -1) {
DEBUG(0,("Could not create file: %s, %s\n", location,
strerror(errno)));
talloc_free(regf);
return WERR_GENERAL_FAILURE;
}
regf_hdr = talloc_zero(regf, struct regf_hdr);
W_ERROR_HAVE_NO_MEMORY(regf_hdr);
regf_hdr->REGF_ID = "regf";
unix_to_nt_time(®f_hdr->modtime, time(NULL));
regf_hdr->version.major = 1;
regf_hdr->version.minor = minor_version;
regf_hdr->last_block = 0x1000; /* Block size */
regf_hdr->description = talloc_strdup(regf_hdr,
"Registry created by Samba 4");
W_ERROR_HAVE_NO_MEMORY(regf_hdr->description);
regf_hdr->chksum = 0;
regf->header = regf_hdr;
/* Create all hbin blocks */
regf->hbins = talloc_array(regf, struct hbin_block *, 1);
W_ERROR_HAVE_NO_MEMORY(regf->hbins);
regf->hbins[0] = NULL;
nk.header = "nk";
nk.type = REG_ROOT_KEY;
unix_to_nt_time(&nk.last_change, time(NULL));
nk.uk1 = 0;
nk.parent_offset = -1;
nk.num_subkeys = 0;
nk.uk2 = 0;
nk.subkeys_offset = -1;
nk.unknown_offset = -1;
nk.num_values = 0;
nk.values_offset = -1;
memset(nk.unk3, 0, 5);
nk.clsname_offset = -1;
nk.clsname_length = 0;
nk.sk_offset = 0x80;
nk.key_name = "SambaRootKey";
/*
* It should be noted that changing the key_name to something shorter
* creates a shorter nk block, which makes the position of the sk block
* change. All Windows registries I've seen have the sk at 0x80.
* I therefore recommend that our regf files share that offset -- Wilco
*/
/* Create a security descriptor. */
sd = security_descriptor_dacl_create(regf,
0,
NULL, NULL,
SID_NT_AUTHENTICATED_USERS,
SEC_ACE_TYPE_ACCESS_ALLOWED,
SEC_GENERIC_ALL,
SEC_ACE_FLAG_OBJECT_INHERIT,
NULL);
/* Push the security descriptor to a blob */
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf,
sd, (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
DEBUG(0, ("Unable to push security descriptor\n"));
return WERR_GENERAL_FAILURE;
}
ZERO_STRUCT(sk);
sk.header = "sk";
sk.prev_offset = 0x80;
sk.next_offset = 0x80;
sk.ref_cnt = 1;
sk.rec_size = data.length;
sk.sec_desc = data.data;
/* Store the new nk key */
regf->header->data_offset = hbin_store_tdr(regf,
(tdr_push_fn_t)tdr_push_nk_block,
&nk);
/* Store the sk block */
sk_offset = hbin_store_tdr(regf,
(tdr_push_fn_t) tdr_push_sk_block,
&sk);
if (sk_offset != 0x80) {
DEBUG(0, ("Error storing sk block, should be at 0x80, stored at 0x%x\n", nk.sk_offset));
return WERR_GENERAL_FAILURE;
}
*key = (struct hive_key *)regf_get_key(parent_ctx, regf,
regf->header->data_offset);
error = regf_save_hbin(regf, 1);
if (!W_ERROR_IS_OK(error)) {
return error;
}
/* We can drop our own reference now that *key will have created one */
talloc_unlink(NULL, regf);
return WERR_OK;
}
static WERROR regf_flush_key(struct hive_key *key)
{
struct regf_key_data *private_data = (struct regf_key_data *)key;
struct regf_data *regf = private_data->hive;
WERROR error;
error = regf_save_hbin(regf, 1);
if (!W_ERROR_IS_OK(error)) {
DEBUG(0, ("Failed to flush regf to disk\n"));
return error;
}
return WERR_OK;
}
static int regf_destruct(struct regf_data *regf)
{
WERROR error;
/* Write to disk */
error = regf_save_hbin(regf, 1);
if (!W_ERROR_IS_OK(error)) {
DEBUG(0, ("Failed to flush registry to disk\n"));
return -1;
}
/* Close file descriptor */
close(regf->fd);
return 0;
}
WERROR reg_open_regf_file(TALLOC_CTX *parent_ctx, const char *location,
struct hive_key **key)
{
struct regf_data *regf;
struct regf_hdr *regf_hdr;
struct tdr_pull *pull;
unsigned int i;
regf = (struct regf_data *)talloc_zero(parent_ctx, struct regf_data);
W_ERROR_HAVE_NO_MEMORY(regf);
talloc_set_destructor(regf, regf_destruct);
DEBUG(5, ("Attempting to load registry file\n"));
/* Get the header */
regf->fd = open(location, O_RDWR);
if (regf->fd == -1) {
DEBUG(0,("Could not load file: %s, %s\n", location,
strerror(errno)));
talloc_free(regf);
return WERR_GENERAL_FAILURE;
}
pull = tdr_pull_init(regf);
pull->data.data = (uint8_t*)fd_load(regf->fd, &pull->data.length, 0, regf);
if (pull->data.data == NULL) {
DEBUG(0, ("Error reading data from file: %s\n", location));
talloc_free(regf);
return WERR_GENERAL_FAILURE;
}
regf_hdr = talloc(regf, struct regf_hdr);
W_ERROR_HAVE_NO_MEMORY(regf_hdr);
if (NT_STATUS_IS_ERR(tdr_pull_regf_hdr(pull, regf_hdr, regf_hdr))) {
DEBUG(0, ("Failed to pull regf header from file: %s\n", location));
talloc_free(regf);
return WERR_GENERAL_FAILURE;
}
regf->header = regf_hdr;
if (strcmp(regf_hdr->REGF_ID, "regf") != 0) {
DEBUG(0, ("Unrecognized NT registry header id: %s, %s\n",
regf_hdr->REGF_ID, location));
talloc_free(regf);
return WERR_GENERAL_FAILURE;
}
/* Validate the header ... */
if (regf_hdr_checksum(pull->data.data) != regf_hdr->chksum) {
DEBUG(0, ("Registry file checksum error: %s: %d,%d\n",
location, regf_hdr->chksum,
regf_hdr_checksum(pull->data.data)));
talloc_free(regf);
return WERR_GENERAL_FAILURE;
}
pull->offset = 0x1000;
i = 0;
/* Read in all hbin blocks */
regf->hbins = talloc_array(regf, struct hbin_block *, 1);
W_ERROR_HAVE_NO_MEMORY(regf->hbins);
regf->hbins[0] = NULL;
while (pull->offset < pull->data.length &&
pull->offset <= regf->header->last_block) {
struct hbin_block *hbin = talloc(regf->hbins,
struct hbin_block);
W_ERROR_HAVE_NO_MEMORY(hbin);
if (NT_STATUS_IS_ERR(tdr_pull_hbin_block(pull, hbin, hbin))) {
DEBUG(0, ("[%d] Error parsing HBIN block\n", i));
talloc_free(regf);
return WERR_FOOBAR;
}
if (strcmp(hbin->HBIN_ID, "hbin") != 0) {
DEBUG(0, ("[%d] Expected 'hbin', got '%s'\n",
i, hbin->HBIN_ID));
talloc_free(regf);
return WERR_FOOBAR;
}
regf->hbins[i] = hbin;
i++;
regf->hbins = talloc_realloc(regf, regf->hbins,
struct hbin_block *, i+2);
regf->hbins[i] = NULL;
}
talloc_free(pull);
DEBUG(1, ("%d HBIN blocks read\n", i));
*key = (struct hive_key *)regf_get_key(parent_ctx, regf,
regf->header->data_offset);
/* We can drop our own reference now that *key will have created one */
talloc_unlink(parent_ctx, regf);
return WERR_OK;
}
static struct hive_operations reg_backend_regf = {
.name = "regf",
.get_key_info = regf_get_info,
.enum_key = regf_get_subkey_by_index,
.get_key_by_name = regf_get_subkey_by_name,
.get_value_by_name = regf_get_value_by_name,
.enum_value = regf_get_value,
.get_sec_desc = regf_get_sec_desc,
.set_sec_desc = regf_set_sec_desc,
.add_key = regf_add_key,
.set_value = regf_set_value,
.del_key = regf_del_key,
.delete_value = regf_del_value,
.flush_key = regf_flush_key
};
|