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
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
|
/*
* libsb2 -- scratchbox2 preload library
*
* Copyright (C) 2006,2007 Lauri Leukkunen <lle@rahina.org>
* parts contributed by
* Riku Voipio <riku.voipio@movial.com>
* Toni Timonen <toni.timonen@movial.com>
*
* Heavily based on the libfakechroot library by
* Piotr Roszatycki <dexter@debian.org>
*/
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "config_hardcoded.h"
#define _GNU_SOURCE
#define __BSD_VISIBLE
#include <unistd.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/syscall.h>
#include <sys/utsname.h>
#include <asm/unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdarg.h>
#include <dirent.h>
#include <string.h>
#include <glob.h>
#include <utime.h>
#ifdef HAVE_FTS_H
#include <fts.h>
#endif
#ifdef HAVE_FTW_H
#include <ftw.h>
#endif
#ifdef HAVE_SHADOW_H
#include <shadow.h>
#endif
#ifdef HAVE_SYS_XATTR_H
#include <sys/xattr.h>
#endif
#include <elf.h>
#include <sys/user.h>
#include <sys/mman.h>
#include <mapping.h>
#include <sb2.h>
#if defined(PATH_MAX)
#define SBOX_MAXPATH PATH_MAX
#elif defined(_POSIX_PATH_MAX)
#define SBOX_MAXPATH _POSIX_PATH_MAX
#elif defined(MAXPATHLEN)
#define SBOX_MAXPATH MAXPATHLEN
#else
#define SBOX_MAXPATH 2048
#endif
/* XXX: current code allows ./usr/bin/../../../../../ style escaping
* from chroot
* if ( amount /../ > amount /other/ ) ) remove extra /../
*/
#define SBOX_MAP_PROLOGUE() \
char *sbox_path;
#define SBOX_MAP_AT_PROLOGUE() \
char *sbox_path;
#define SBOX_MAP_PATH_NARROW(path, sbox_path) \
{ \
if ((path) != NULL && *((char *)(path)) != '\0') { \
sbox_path = scratchbox_path(__FUNCTION__, path); \
} \
}
#define SBOX_MAP_PATH(path, sbox_path) \
{ \
if ((path) != NULL) { \
sbox_path = scratchbox_path(__FUNCTION__, path); \
} \
}
#define SBOX_MAP_PATH_AT(dirfd, path, sbox_path) \
{ \
if ((path) != NULL) { \
if (path[0] == '/') { \
/* absolute path */ \
sbox_path = scratchbox_path(__FUNCTION__, path); \
} else { \
sbox_path = strdup(path); \
}\
} \
}
#define nextsym(function, name) \
{ \
char *msg; \
if (next_##function == NULL) { \
*(void **)(&next_##function) = dlsym(RTLD_NEXT, name); \
if ((msg = dlerror()) != NULL) { \
fprintf (stderr, "%s: dlsym(%s): %s\n", PACKAGE_NAME, name, msg); \
} \
} \
}
#ifndef __GLIBC__
extern char **environ;
#endif
#ifndef HAVE_STRCHRNUL
/* Find the first occurrence of C in S or the final NUL byte. */
static char *strchrnul (const char *s, int c_in)
{
const unsigned char *char_ptr;
const unsigned long int *longword_ptr;
unsigned long int longword, magic_bits, charmask;
unsigned char c;
c = (unsigned char) c_in;
/* Handle the first few characters by reading one character at a time.
Do this until CHAR_PTR is aligned on a longword boundary. */
for (char_ptr = s; ((unsigned long int) char_ptr
& (sizeof(longword) - 1)) != 0; ++char_ptr)
if (*char_ptr == c || *char_ptr == '\0')
return (void *) char_ptr;
/* All these elucidatory comments refer to 4-byte longwords,
but the theory applies equally well to 8-byte longwords. */
longword_ptr = (unsigned long int *) char_ptr;
/* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
* the "holes." Note that there is a hole just to the left of
* each byte, with an extra at the end:
*
* bits: 01111110 11111110 11111110 11111111
* bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
*
* The 1-bits make sure that carries propagate to the next 0-bit.
* The 0-bits provide holes for carries to fall into.
* */
switch (sizeof(longword)) {
case 4:
magic_bits = 0x7efefeffL;
break;
case 8:
magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL;
break;
default:
abort();
}
/* Set up a longword, each of whose bytes is C. */
charmask = c | (c << 8);
charmask |= charmask << 16;
if (sizeof(longword) > 4)
/* Do the shift in two steps to avoid a warning if long has 32 bits. */
charmask |= (charmask << 16) << 16;
if (sizeof(longword) > 8)
abort();
/* Instead of the traditional loop which tests each character,
we will test a longword at a time. The tricky part is testing
if *any of the four* bytes in the longword in question are zero. */
for (;;) {
/* We tentatively exit the loop if adding MAGIC_BITS to
LONGWORD fails to change any of the hole bits of LONGWORD.
1) Is this safe? Will it catch all the zero bytes?
Suppose there is a byte with all zeros. Any carry bits
propagating from its left will fall into the hole at its
least significant bit and stop. Since there will be no
carry from its most significant bit, the LSB of the
byte to the left will be unchanged, and the zero will be
detected.
2) Is this worthwhile? Will it ignore everything except
zero bytes? Suppose every byte of LONGWORD has a bit set
somewhere. There will be a carry into bit 8. If bit 8
is set, this will carry into bit 16. If bit 8 is clear,
one of bits 9-15 must be set, so there will be a carry
into bit 16. Similarly, there will be a carry into bit
24. If one of bits 24-30 is set, there will be a carry
into bit 31, so all of the hole bits will be changed.
The one misfire occurs when bits 24-30 are clear and bit
31 is set; in this case, the hole at bit 31 is not
changed. If we had access to the processor carry flag,
we could close this loophole by putting the fourth hole
at bit 32!
So it ignores everything except 128's, when they're aligned
properly.
3) But wait! Aren't we looking for C as well as zero?
Good point. So what we do is XOR LONGWORD with a longword,
each of whose bytes is C. This turns each byte that is C
into a zero. */
longword = *longword_ptr++;
/* Add MAGIC_BITS to LONGWORD. */
if ((((longword + magic_bits)
/* Set those bits that were unchanged by the addition. */
^ ~longword)
/* Look at only the hole bits. If any of the hole bits
* are unchanged, most likely one of the bytes was a
* zero.
*/
& ~magic_bits) != 0 ||
/* That caught zeroes. Now test for C. */
((((longword ^ charmask) +
magic_bits) ^ ~(longword ^ charmask))
& ~magic_bits) != 0) {
/* Which of the bytes was C or zero?
* If none of them were, it was a misfire; continue the search.
*/
const unsigned char *cp =
(const unsigned char *) (longword_ptr - 1);
if (*cp == c || *cp == '\0')
return (char *) cp;
if (*++cp == c || *cp == '\0')
return (char *) cp;
if (*++cp == c || *cp == '\0')
return (char *) cp;
if (*++cp == c || *cp == '\0')
return (char *) cp;
if (sizeof(longword) > 4) {
if (*++cp == c || *cp == '\0')
return (char *) cp;
if (*++cp == c || *cp == '\0')
return (char *) cp;
if (*++cp == c || *cp == '\0')
return (char *) cp;
if (*++cp == c || *cp == '\0')
return (char *) cp;
}
}
}
/* This should never happen. */
return NULL;
}
#endif
#ifdef HAVE___LXSTAT
static int (*next___lxstat) (int ver, const char *filename, struct stat *buf) = NULL;
#endif
#ifdef HAVE___LXSTAT64
static int (*next___lxstat64) (int ver, const char *filename, struct stat64 *buf) = NULL;
#endif
#ifdef HAVE___OPEN
static int (*next___open) (const char *pathname, int flags, ...) = NULL;
#endif
#ifdef HAVE___OPEN64
static int (*next___open64) (const char *pathname, int flags, ...) = NULL;
#endif
#ifdef HAVE___OPENDIR2
static DIR * (*next___opendir2) (const char *name, int flags) = NULL;
#endif
#ifdef HAVE___XMKNOD
static int (*next___xmknod) (int ver, const char *path, mode_t mode, dev_t *dev) = NULL;
#endif
#ifdef HAVE___XSTAT
static int (*next___xstat) (int ver, const char *filename, struct stat *buf) = NULL;
#endif
#ifdef HAVE___XSTAT64
static int (*next___xstat64) (int ver, const char *filename, struct stat64 *buf) = NULL;
#endif
#ifdef HAVE__XFTW
static int (*next__xftw) (int mode, const char *dir, int (*fn)(const char *file, const struct stat *sb, int flag), int nopenfd) = NULL;
#endif
#ifdef HAVE__XFTW64
static int (*next__xftw64) (int mode, const char *dir, int (*fn)(const char *file, const struct stat64 *sb, int flag), int nopenfd) = NULL;
#endif
static int (*next_access) (const char *pathname, int mode) = NULL;
static int (*next_acct) (const char *filename) = NULL;
#ifdef HAVE_CANONICALIZE_FILE_NAME
static char * (*next_canonicalize_file_name) (const char *name) = NULL;
#endif
static int (*next_chdir) (const char *path) = NULL;
static int (*next_chmod) (const char *path, mode_t mode) = NULL;
static int (*next_chown) (const char *path, uid_t owner, gid_t group) = NULL;
/* static int (*next_chroot) (const char *path) = NULL; */
static int (*next_creat) (const char *pathname, mode_t mode) = NULL;
static int (*next_creat64) (const char *pathname, mode_t mode) = NULL;
#ifdef HAVE_DLMOPEN
static void * (*next_dlmopen) (Lmid_t nsid, const char *filename, int flag) = NULL;
#endif
static void * (*next_dlopen) (const char *filename, int flag) = NULL;
#ifdef HAVE_EUIDACCESS
static int (*next_euidaccess) (const char *pathname, int mode) = NULL;
#endif
/* static int (*next_execl) (const char *path, const char *arg, ...) = NULL; */
/* static int (*next_execle) (const char *path, const char *arg, ...) = NULL; */
/* static int (*next_execlp) (const char *file, const char *arg, ...) = NULL; */
/* static int (*next_execv) (const char *path, char *const argv []) = NULL; */
int (*next_execve) (const char *filename, char *const argv [], char *const envp[]) = NULL;
static int (*next_execvp) (const char *file, char *const argv []) = NULL;
#ifdef HAVE_FACCESSAT
static int (*next_faccessat) (int dirfd, const char *pathname, int mode, int flags) = NULL;
#endif
#ifdef HAVE_FCHMODAT
static int (*next_fchmodat) (int dirfd, const char *pathname, mode_t mode, int flags) = NULL;
#endif
#ifdef HAVE_FCHOWNAT
static int (*next_fchownat) (int dirfd, const char *pathname, uid_t owner, gid_t group, int flags) = NULL;
#endif
static FILE * (*next_fopen) (const char *path, const char *mode) = NULL;
static FILE * (*next_fopen64) (const char *path, const char *mode) = NULL;
static FILE * (*next_freopen) (const char *path, const char *mode, FILE *stream) = NULL;
static FILE * (*next_freopen64) (const char *path, const char *mode, FILE *stream) = NULL;
#ifdef HAVE_FSTATAT
static int (*next_fstatat) (int dirfd, const char *pathname, struct stat *buf, int flags) = NULL;
#endif
#ifdef HAVE_FTS_OPEN
#if !defined(HAVE___OPENDIR2)
static FTS * (*next_fts_open) (char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **)) = NULL;
#endif
#endif
#ifdef HAVE_FTW
#if !defined(HAVE___OPENDIR2) && !defined(HAVE__XFTW)
static int (*next_ftw) (const char *dir, int (*fn)(const char *file, const struct stat *sb, int flag), int nopenfd) = NULL;
#endif
#endif
#ifdef HAVE_FTW64
#if !defined(HAVE___OPENDIR2) && !defined(HAVE__XFTW)
static int (*next_ftw64) (const char *dir, int (*fn)(const char *file, const struct stat64 *sb, int flag), int nopenfd) = NULL;
#endif
#endif
#ifdef HAVE_FUTIMESAT
static int (*next_futimesat) (int dirfd, const char *pathname, const struct timeval times[2]) = NULL;
#endif
#ifdef HAVE_GET_CURRENT_DIR_NAME
static char * (*next_get_current_dir_name) (void) = NULL;
#endif
static char * (*next_getcwd) (char *buf, size_t size) = NULL;
static char * (*next_getwd) (char *buf) = NULL;
#ifdef HAVE_GETXATTR
static ssize_t (*next_getxattr) (const char *path, const char *name, void *value, size_t size) = NULL;
#endif
static int (*next_glob) (const char *pattern, int flags, int (*errfunc) (const char *, int), glob_t *pglob) = NULL;
#ifdef HAVE_GLOB64
static int (*next_glob64) (const char *pattern, int flags, int (*errfunc) (const char *, int), glob64_t *pglob) = NULL;
#endif
#ifdef HAVE_GLOB_PATTERN_P
static int (*next_glob_pattern_p) (const char *pattern, int quote) = NULL;
#endif
#ifdef HAVE_LCHMOD
static int (*next_lchmod) (const char *path, mode_t mode) = NULL;
#endif
static int (*next_lchown) (const char *path, uid_t owner, gid_t group) = NULL;
#ifdef HAVE_LCKPWDF
/* static int (*next_lckpwdf) (void) = NULL; */
#endif
#ifdef HAVE_LGETXATTR
static ssize_t (*next_lgetxattr) (const char *path, const char *name, void *value, size_t size) = NULL;
#endif
static int (*next_link) (const char *oldpath, const char *newpath) = NULL;
#ifdef HAVE_LINKAT
static int (*next_linkat) (int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) = NULL;
#endif
#ifdef HAVE_LISTXATTR
static ssize_t (*next_listxattr) (const char *path, char *list, size_t size) = NULL;
#endif
#ifdef HAVE_LLISTXATTR
static ssize_t (*next_llistxattr) (const char *path, char *list, size_t size) = NULL;
#endif
#ifdef HAVE_LREMOVEXATTR
static int (*next_lremovexattr) (const char *path, const char *name) = NULL;
#endif
#ifdef HAVE_LSETXATTR
static int (*next_lsetxattr) (const char *path, const char *name, const void *value, size_t size, int flags) = NULL;
#endif
#if !defined(HAVE___LXSTAT)
static int (*next_lstat) (const char *file_name, struct stat *buf) = NULL;
#endif
#ifdef HAVE_LSTAT64
#if !defined(HAVE___LXSTAT64)
static int (*next_lstat64) (const char *file_name, struct stat64 *buf) = NULL;
#endif
#endif
#ifdef HAVE_LUTIMES
static int (*next_lutimes) (const char *filename, const struct timeval tv[2]) = NULL;
#endif
static int (*next_mkdir) (const char *pathname, mode_t mode) = NULL;
#ifdef HAVE_MKDIRAT
static int (*next_mkdirat) (int dirfd, const char *pathname, mode_t mode) = NULL;
#endif
#ifdef HAVE_MKDTEMP
static char * (*next_mkdtemp) (char *template) = NULL;
#endif
static int (*next_mknod) (const char *pathname, mode_t mode, dev_t dev) = NULL;
#ifdef HAVE_MKNODAT
static int (*next_mknodat) (int dirfd, const char *pathname, mode_t mode, dev_t dev) = NULL;
#endif
static int (*next_mkfifo) (const char *pathname, mode_t mode) = NULL;
#ifdef HAVE_MKFIFOAT
static int (*next_mkfifoat) (int dirfd, const char *pathname, mode_t mode) = NULL;
#endif
static int (*next_mkstemp) (char *template) = NULL;
static int (*next_mkstemp64) (char *template) = NULL;
static char * (*next_mktemp) (char *template) = NULL;
#ifdef HAVE_NFTW
static int (*next_nftw) (const char *dir, int (*fn)(const char *file, const struct stat *sb, int flag, struct FTW *s), int nopenfd, int flags) = NULL;
#endif
#ifdef HAVE_NFTW64
static int (*next_nftw64) (const char *dir, int (*fn)(const char *file, const struct stat64 *sb, int flag, struct FTW *s), int nopenfd, int flags) = NULL;
#endif
static int (*next_open) (const char *pathname, int flags, ...) = NULL;
static int (*next_open64) (const char *pathname, int flags, ...) = NULL;
#ifdef HAVE_OPENAT
static int (*next_openat) (int dirfd, const char *pathname, int flags, ...) = NULL;
#endif
#ifdef HAVE_OPENAT64
static int (*next_openat64) (int dirfd, const char *pathname, int flags, ...) = NULL;
#endif
#if !defined(HAVE___OPENDIR2)
static DIR * (*next_opendir) (const char *name) = NULL;
#endif
static long (*next_pathconf) (const char *path, int name) = NULL;
static int (*next_readlink) (const char *path, char *buf, READLINK_TYPE_ARG3) = NULL;
#ifdef HAVE_READLINKAT
static int (*next_readlinkat) (int dirfd, const char *pathname, char *buf, size_t bufsiz) = NULL;
#endif
static char * (*next_realpath) (const char *name, char *resolved) = NULL;
static int (*next_remove) (const char *pathname) = NULL;
#ifdef HAVE_REMOVEXATTR
static int (*next_removexattr) (const char *path, const char *name) = NULL;
#endif
static int (*next_rename) (const char *oldpath, const char *newpath) = NULL;
#ifdef HAVE_RENAMEAT
static int (*next_renameat) (int olddirfd, const char *oldpath, int newdirfd, const char *newpath) = NULL;
#endif
#ifdef HAVE_REVOKE
static int (*next_revoke) (const char *file) = NULL;
#endif
static int (*next_rmdir) (const char *pathname) = NULL;
#ifdef HAVE_SCANDIR
static int (*next_scandir) (const char *dir, struct dirent ***namelist, SCANDIR_TYPE_ARG3, int(*compar)(const void *, const void *)) = NULL;
#endif
#ifdef HAVE_SCANDIR64
static int (*next_scandir64) (const char *dir, struct dirent64 ***namelist, int(*filter)(const struct dirent64 *), int(*compar)(const void *, const void *)) = NULL;
#endif
#ifdef HAVE_SETXATTR
static int (*next_setxattr) (const char *path, const char *name, const void *value, size_t size, int flags) = NULL;
#endif
#if !defined(HAVE___XSTAT)
static int (*next_stat) (const char *file_name, struct stat *buf) = NULL;
#endif
#ifdef HAVE_STAT64
#if !defined(HAVE___XSTAT64)
static int (*next_stat64) (const char *file_name, struct stat64 *buf) = NULL;
#endif
#endif
static int (*next_symlink) (const char *oldpath, const char *newpath) = NULL;
#ifdef HAVE_SYMLINKAT
static int (*next_symlinkat) (const char *oldpath, int newdirfd, const char *newpath) = NULL;
#endif
static char * (*next_tempnam) (const char *dir, const char *pfx) = NULL;
static char * (*next_tmpnam) (char *s) = NULL;
static int (*next_truncate) (const char *path, off_t length) = NULL;
#ifdef HAVE_TRUNCATE64
static int (*next_truncate64) (const char *path, off64_t length) = NULL;
#endif
static int (*next_unlink) (const char *pathname) = NULL;
#ifdef HAVE_UNLINKAT
static int (*next_unlinkat) (int dirfd, const char *pathname, int flags) = NULL;
#endif
#ifdef HAVE_ULCKPWDF
/* static int (*next_ulckpwdf) (void) = NULL; */
#endif
static int (*next_utime) (const char *filename, const struct utimbuf *buf) = NULL;
static int (*next_utimes) (const char *filename, const struct timeval tv[2]) = NULL;
static int (*next_uname) (struct utsname *buf) = NULL;
void libsb2_init (void) __attribute((constructor));
void libsb2_init (void)
{
//DBGOUT("fakechroot init start: %i\n", getpid());
#ifdef HAVE___LXSTAT
nextsym(__lxstat, "__lxstat");
#endif
#ifdef HAVE___LXSTAT64
nextsym(__lxstat64, "__lxstat64");
#endif
#ifdef HAVE___OPEN
nextsym(__open, "__open");
#endif
#ifdef HAVE___OPEN64
nextsym(__open64, "__open64");
#endif
#ifdef HAVE___OPENDIR2
nextsym(__opendir2, "__opendir2");
#endif
#ifdef HAVE___XMKNOD
nextsym(__xmknod, "__xmknod");
#endif
#ifdef HAVE___XSTAT
nextsym(__xstat, "__xstat");
#endif
#ifdef HAVE___XSTAT64
nextsym(__xstat64, "__xstat64");
#endif
nextsym(access, "access");
nextsym(acct, "acct");
#ifdef HAVE_CANONICALIZE_FILE_NAME
nextsym(canonicalize_file_name, "canonicalize_file_name");
#endif
nextsym(chdir, "chdir");
nextsym(chmod, "chmod");
nextsym(chown, "chown");
nextsym(creat, "creat");
nextsym(creat64, "creat64");
#ifdef HAVE_DLMOPEN
nextsym(dlmopen, "dlmopen");
#endif
nextsym(dlopen, "dlopen");
#ifdef HAVE_EUIDACCESS
nextsym(euidaccess, "euidaccess");
#endif
/* nextsym(execl, "execl"); */
/* nextsym(execle, "execle"); */
/* nextsym(execlp, "execlp"); */
/* nextsym(execv, "execv"); */
nextsym(execve, "execve");
nextsym(execvp, "execvp");
#ifdef HAVE_FACCESSAT
nextsym(faccessat, "faccessat");
#endif
#ifdef HAVE_FCHMODAT
nextsym(fchmodat, "fchmodat");
#endif
#ifdef HAVE_FCHOWNAT
nextsym(fchownat, "fchownat");
#endif
nextsym(fopen, "fopen");
nextsym(fopen64, "fopen64");
nextsym(freopen, "freopen");
nextsym(freopen64, "freopen64");
#ifdef HAVE_FSTATAT
nextsym(fstatat, "fstatat");
#endif
#ifdef HAVE_FTS_OPEN
#if !defined(HAVE___OPENDIR2)
nextsym(fts_open, "fts_open");
#endif
#endif
#ifdef HAVE_FTW
#if !defined(HAVE___OPENDIR2) && !defined(HAVE__XFTW)
nextsym(ftw, "ftw");
#endif
#endif
#ifdef HAVE_FTW64
#if !defined(HAVE___OPENDIR2) && !defined(HAVE__XFTW)
nextsym(ftw64, "ftw64");
#endif
#endif
#ifdef HAVE_FUTIMESAT
nextsym(futimesat, "futimesat");
#endif
#ifdef HAVE_GET_CURRENT_DIR_NAME
nextsym(get_current_dir_name, "get_current_dir_name");
#endif
nextsym(getcwd, "getcwd");
nextsym(getwd, "getwd");
#ifdef HAVE_GETXATTR
nextsym(getxattr, "getxattr");
#endif
nextsym(glob, "glob");
#ifdef HAVE_GLOB64
nextsym(glob64, "glob64");
#endif
#ifdef HAVE_GLOB_PATTERN_P
nextsym(glob_pattern_p, "glob_pattern_p");
#endif
#ifdef HAVE_LCHMOD
nextsym(lchmod, "lchmod");
#endif
nextsym(lchown, "lchown");
#ifdef HAVE_LCKPWDF
/* nextsym(lckpwdf, "lckpwdf"); */
#endif
#ifdef HAVE_LGETXATTR
nextsym(lgetxattr, "lgetxattr");
#endif
nextsym(link, "link");
#ifdef HAVE_LINKAT
nextsym(linkat, "linkat");
#endif
#ifdef HAVE_LISTXATTR
nextsym(listxattr, "listxattr");
#endif
#ifdef HAVE_LLISTXATTR
nextsym(llistxattr, "llistxattr");
#endif
#ifdef HAVE_LREMOVEXATTR
nextsym(lremovexattr, "lremovexattr");
#endif
#ifdef HAVE_LSETXATTR
nextsym(lsetxattr, "lsetxattr");
#endif
#if !defined(HAVE___LXSTAT)
nextsym(lstat, "lstat");
#endif
#ifdef HAVE_LSTAT64
#if !defined(HAVE___LXSTAT64)
nextsym(lstat64, "lstat64");
#endif
#endif
#ifdef HAVE_LUTIMES
nextsym(lutimes, "lutimes");
#endif
nextsym(mkdir, "mkdir");
#ifdef HAVE_MKDIRAT
nextsym(mkdirat, "mkdirat");
#endif
#ifdef HAVE_MKDTEMP
nextsym(mkdtemp, "mkdtemp");
#endif
nextsym(mknod, "mknod");
#ifdef HAVE_MKNODAT
nextsym(mknodat, "mknodat");
#endif
nextsym(mkfifo, "mkfifo");
#ifdef HAVE_MKFIFOAT
nextsym(mkfifoat, "mkfifoat");
#endif
nextsym(mkstemp, "mkstemp");
nextsym(mkstemp64, "mkstemp64");
nextsym(mktemp, "mktemp");
#ifdef HAVE_NFTW
nextsym(nftw, "nftw");
#endif
#ifdef HAVE_NFTW64
nextsym(nftw64, "nftw64");
#endif
nextsym(open, "open");
#ifdef HAVE_OPENAT
nextsym(openat, "openat");
#endif
nextsym(open64, "open64");
#ifdef HAVE_OPENAT64
nextsym(openat64, "openat64");
#endif
#if !defined(HAVE___OPENDIR2)
nextsym(opendir, "opendir");
#endif
nextsym(pathconf, "pathconf");
nextsym(readlink, "readlink");
#ifdef HAVE_READLINKAT
nextsym(readlinkat, "readlinkat");
#endif
nextsym(realpath, "realpath");
nextsym(remove, "remove");
#ifdef HAVE_REMOVEXATTR
nextsym(removexattr, "removexattr");
#endif
nextsym(rename, "rename");
#ifdef HAVE_RENAMEAT
nextsym(renameat, "renameat");
#endif
#ifdef HAVE_REVOKE
nextsym(revoke, "revoke");
#endif
nextsym(rmdir, "rmdir");
#ifdef HAVE_SCANDIR
nextsym(scandir, "scandir");
#endif
#ifdef HAVE_SCANDIR64
nextsym(scandir64, "scandir64");
#endif
#ifdef HAVE_SETXATTR
nextsym(setxattr, "setxattr");
#endif
#if !defined(HAVE___XSTAT)
nextsym(stat, "stat");
#endif
#ifdef HAVE_STAT64
#if !defined(HAVE___XSTAT64)
nextsym(stat64, "stat64");
#endif
#endif
nextsym(symlink, "symlink");
#ifdef HAVE_SYMLINKAT
nextsym(symlinkat, "symlinkat");
#endif
nextsym(tempnam, "tempnam");
nextsym(tmpnam, "tmpnam");
nextsym(truncate, "truncate");
#ifdef HAVE_TRUNCATE64
nextsym(truncate64, "truncate64");
#endif
nextsym(unlink, "unlink");
#ifdef HAVE_UNLINKAT
nextsym(unlinkat, "unlinkat");
#endif
#ifdef HAVE_ULCKPWDF
/* nextsym(ulckpwdf, "ulckpwdf"); */
#endif
nextsym(utime, "utime");
nextsym(utimes, "utimes");
nextsym(uname, "uname");
}
int sb_next_execve(const char *file, char *const *argv, char *const *envp)
{
if (next_execve == NULL) libsb2_init();
return next_execve(file, argv, envp);
}
#ifdef HAVE___LXSTAT
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int __lxstat (int ver, const char *filename, struct stat *buf)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(filename, sbox_path);
if (next___lxstat == NULL) libsb2_init();
ret = next___lxstat(ver, sbox_path, buf);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE___LXSTAT64
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int __lxstat64 (int ver, const char *filename, struct stat64 *buf)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(filename, sbox_path);
if (next___lxstat64 == NULL) libsb2_init();
ret = next___lxstat64(ver, sbox_path, buf);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE___OPEN
/* Internal libc function */
int __open (const char *pathname, int flags, ...)
{
SBOX_MAP_PROLOGUE();
int ret;
int mode = 0;
SBOX_MAP_PATH(pathname, sbox_path);
if (flags & O_CREAT) {
va_list arg;
va_start (arg, flags);
mode = va_arg (arg, int);
va_end (arg);
}
if (next___open == NULL) libsb2_init();
ret = next___open(sbox_path, flags, mode);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE___OPEN64
/* Internal libc function */
int __open64 (const char *pathname, int flags, ...)
{
SBOX_MAP_PROLOGUE();
int ret;
int mode = 0;
SBOX_MAP_PATH(pathname, sbox_path);
if (flags & O_CREAT) {
va_list arg;
va_start (arg, flags);
mode = va_arg (arg, int);
va_end (arg);
}
if (next___open64 == NULL) libsb2_init();
ret = next___open64(sbox_path, flags, mode);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE___OPENDIR2
/* Internal libc function */
/* #include <dirent.h> */
DIR *__opendir2 (const char *name, int flags)
{
SBOX_MAP_PROLOGUE();
DIR *ret;
SBOX_MAP_PATH(name, sbox_path);
if (next___opendir2 == NULL) libsb2_init();
ret = next___opendir2(sbox_path, flags);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE___XMKNOD
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int __xmknod (int ver, const char *path, mode_t mode, dev_t *dev)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(path, sbox_path);
if (next___xmknod == NULL) libsb2_init();
ret = next___xmknod(ver, sbox_path, mode, dev);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE___XSTAT
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int __xstat (int ver, const char *filename, struct stat *buf)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(filename, sbox_path);
if (next___xstat == NULL) libsb2_init();
ret = next___xstat(ver, sbox_path, buf);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE___XSTAT64
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int __xstat64 (int ver, const char *filename, struct stat64 *buf)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(filename, sbox_path);
if (next___xstat64 == NULL) libsb2_init();
ret = next___xstat64(ver, sbox_path, buf);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE__XFTW
/* include <ftw.h> */
int _xftw (int mode, const char *dir, int (*fn)(const char *file, const struct stat *sb, int flag), int nopenfd)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(dir, sbox_path);
if (next__xftw == NULL) libsb2_init();
ret = next__xftw(mode, sbox_path, fn, nopenfd);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE__XFTW64
/* include <ftw.h> */
int _xftw64 (int mode, const char *dir, int (*fn)(const char *file, const struct stat64 *sb, int flag), int nopenfd)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(dir, sbox_path);
if (next__xftw64 == NULL) libsb2_init();
ret = next__xftw64(mode, sbox_path, fn, nopenfd);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <unistd.h> */
int access (const char *pathname, int mode)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(pathname, sbox_path);
if (next_access == NULL) libsb2_init();
ret = next_access(sbox_path, mode);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <unistd.h> */
int acct (const char *filename)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(filename, sbox_path);
if (next_acct == NULL) libsb2_init();
ret = next_acct(sbox_path);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_CANONICALIZE_FILE_NAME
/* #include <stdlib.h> */
char *canonicalize_file_name (const char *name)
{
SBOX_MAP_PROLOGUE();
char *ret;
SBOX_MAP_PATH(name, sbox_path);
if (next_canonicalize_file_name == NULL) libsb2_init();
ret = next_canonicalize_file_name(sbox_path);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <unistd.h> */
int chdir (const char *path)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_chdir == NULL) libsb2_init();
ret = next_chdir(sbox_path);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
int chmod (const char *path, mode_t mode)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_chmod == NULL) libsb2_init();
ret = next_chmod(sbox_path, mode);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <sys/types.h> */
/* #include <unistd.h> */
int chown (const char *path, uid_t owner, gid_t group)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_chown == NULL) libsb2_init();
ret = next_chown(sbox_path, owner, group);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
/* #include <fcntl.h> */
int creat (const char *pathname, mode_t mode)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(pathname, sbox_path);
if (next_creat == NULL) libsb2_init();
ret = next_creat(sbox_path, mode);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
/* #include <fcntl.h> */
int creat64 (const char *pathname, mode_t mode)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(pathname, sbox_path);
if (next_creat64 == NULL) libsb2_init();
ret = next_creat64(sbox_path, mode);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_DLMOPEN
/* #include <dlfcn.h> */
void *dlmopen (Lmid_t nsid, const char *filename, int flag)
{
SBOX_MAP_PROLOGUE();
void *ret;
SBOX_MAP_PATH(filename, sbox_path);
if (next_dlmopen == NULL) libsb2_init();
ret = next_dlmopen(nsid, sbox_path, flag);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <dlfcn.h> */
void *dlopen (const char *filename, int flag)
{
SBOX_MAP_PROLOGUE();
void *ret;
SBOX_MAP_PATH(filename, sbox_path);
if (next_dlopen == NULL) libsb2_init();
ret = next_dlopen(sbox_path, flag);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_EUIDACCESS
/* #include <unistd.h> */
int euidaccess (const char *pathname, int mode)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(pathname, sbox_path);
if (next_euidaccess == NULL) libsb2_init();
ret = next_euidaccess(sbox_path, mode);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <unistd.h> */
int execl (const char *path, const char *arg, ...)
{
size_t argv_max = 1024;
const char **argv = alloca (argv_max * sizeof (const char *));
unsigned int i;
va_list args;
argv[0] = arg;
va_start (args, arg);
i = 0;
while (argv[i++] != NULL)
{
if (i == argv_max)
{
const char **nptr = alloca ((argv_max *= 2) * sizeof (const char *));
if ((char *) argv + i == (char *) nptr)
/* Stack grows up. */
argv_max += i;
else
/* We have a hole in the stack. */
argv = (const char **) memcpy (nptr, argv,
i * sizeof (const char *));
}
argv[i] = va_arg (args, const char *);
}
va_end (args);
return execve (path, (char *const *) argv, environ);
}
/* #include <unistd.h> */
int execle (const char *path, const char *arg, ...)
{
size_t argv_max = 1024;
const char **argv = alloca (argv_max * sizeof (const char *));
const char *const *envp;
unsigned int i;
va_list args;
argv[0] = arg;
va_start (args, arg);
i = 0;
while (argv[i++] != NULL)
{
if (i == argv_max)
{
const char **nptr = alloca ((argv_max *= 2) * sizeof (const char *));
if ((char *) argv + i == (char *) nptr)
/* Stack grows up. */
argv_max += i;
else
/* We have a hole in the stack. */
argv = (const char **) memcpy (nptr, argv,
i * sizeof (const char *));
}
argv[i] = va_arg (args, const char *);
}
envp = va_arg (args, const char *const *);
va_end (args);
return execve (path, (char *const *) argv, (char *const *) envp);
}
/* Execute FILE, searching in the `PATH' environment variable if
it contains no slashes, with all arguments after FILE until a
NULL pointer and environment from `environ'. */
int
execlp (const char *file, const char *arg, ...)
{
size_t argv_max = 1024;
const char **argv = alloca (argv_max * sizeof (const char *));
unsigned int i;
va_list args;
argv[0] = arg;
va_start (args, arg);
i = 0;
while (argv[i++] != NULL)
{
if (i == argv_max)
{
const char **nptr = alloca ((argv_max *= 2) * sizeof (const char *));
#ifndef _STACK_GROWS_UP
if ((char *) nptr + argv_max == (char *) argv)
{
/* Stack grows down. */
argv = (const char **) memcpy (nptr, argv,
i * sizeof (const char *));
argv_max += i;
}
else
#endif
#ifndef _STACK_GROWS_DOWN
if ((char *) argv + i == (char *) nptr)
/* Stack grows up. */
argv_max += i;
else
#endif
/* We have a hole in the stack. */
argv = (const char **) memcpy (nptr, argv,
i * sizeof (const char *));
}
argv[i] = va_arg (args, const char *);
}
va_end (args);
return execvp (file, (char *const *) argv);
}
/* #include <unistd.h> */
int execv (const char *path, char *const argv [])
{
return execve (path, argv, environ);
}
/* #include <unistd.h> */
int execve (const char *filename, char *const argv [], char *const envp[])
{
SBOX_MAP_PROLOGUE();
char *hb_sbox_path;
int ret;
int file;
char hashbang[SBOX_MAXPATH];
size_t argv_max = 1024;
const char **newargv = alloca (argv_max * sizeof (const char *));
char newfilename[SBOX_MAXPATH], argv0[SBOX_MAXPATH];
char *ptr;
int k;
unsigned int i, j, n;
char c;
SBOX_MAP_PATH(filename, sbox_path);
if ((file = open(sbox_path, O_RDONLY)) == -1) {
errno = ENOENT;
if (sbox_path) free(sbox_path);
return -1;
}
k = read(file, hashbang, SBOX_MAXPATH-2);
close(file);
if (k == -1) {
errno = ENOENT;
if (sbox_path) free(sbox_path);
return -1;
}
if (hashbang[0] != '#' || hashbang[1] != '!') {
ret = do_exec(sbox_path, argv, envp);
if (sbox_path) free(sbox_path);
return ret;
}
/* if we're here we have a script */
//printf("hashbang: %s\n", hashbang);
for (i = j = 2; (hashbang[i] == ' ' || hashbang[i] == '\t') && i < SBOX_MAXPATH; i++, j++) {
//printf("looping\n");
}
//printf("hashbanging: i=%u\n",i);
//hashbang[i] = hashbang[i+1] = 0;
for (n = 0; i < SBOX_MAXPATH; i++) {
c = hashbang[i];
if (hashbang[i] == 0 || hashbang[i] == ' ' || hashbang[i] == '\t' || hashbang[i] == '\n') {
hashbang[i] = 0;
if (i > j) {
if (n == 0) {
ptr = &hashbang[j];
//printf("hashbanging ptr, sbox_path: %s, %s\n", ptr, sbox_path);
SBOX_MAP_PATH(ptr, hb_sbox_path);
strcpy(newfilename, hb_sbox_path);
strcpy(argv0, &hashbang[j]);
newargv[n++] = argv0;
free(hb_sbox_path);
hb_sbox_path = NULL;
} else {
newargv[n++] = &hashbang[j];
}
}
j = i + 1;
}
if (c == '\n' || c == 0) break;
}
//printf("hashbanging: %s, %s\n", filename, sbox_path);
SBOX_MAP_PATH(filename, hb_sbox_path);
newargv[n++] = hb_sbox_path;
for (i = 1; argv[i] != NULL && i < argv_max; ) {
newargv[n++] = argv[i++];
}
newargv[n] = 0;
ret = do_exec(newfilename, (char *const *)newargv, envp);
if (hb_sbox_path) free(hb_sbox_path);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <unistd.h> */
int execvp (const char *file, char *const argv [])
{
if (*file == '\0')
{
/* We check the simple case first. */
errno = ENOENT;
return -1;
}
if (strchr (file, '/') != NULL)
{
/* Don't search when it contains a slash. */
return execve (file, argv, environ);
}
else
{
int got_eacces = 0;
char *path, *p, *name;
size_t len;
size_t pathlen;
path = getenv ("PATH");
if (path) path = strdup(path);
if (path == NULL)
{
/* There is no `PATH' in the environment.
The default search path is the current directory
followed by the path `confstr' returns for `_CS_PATH'. */
len = confstr (_CS_PATH, (char *) NULL, 0);
path = (char *) alloca (1 + len);
path[0] = ':';
(void) confstr (_CS_PATH, path + 1, len);
}
len = strlen (file) + 1;
pathlen = strlen (path);
name = alloca (pathlen + len + 1);
/* Copy the file name at the top. */
name = (char *) memcpy (name + pathlen + 1, file, len);
/* And add the slash. */
*--name = '/';
p = path;
do
{
char *startp;
path = p;
p = strchrnul (path, ':');
if (p == path)
/* Two adjacent colons, or a colon at the beginning or the end
of `PATH' means to search the current directory. */
startp = name + 1;
else
startp = (char *) memcpy (name - (p - path), path, p - path);
/* Try to execute this name. If it works, execv will not return. */
execve (startp, argv, environ);
switch (errno)
{
case EACCES:
/* Record the we got a `Permission denied' error. If we end
up finding no executable we can use, we want to diagnose
that we did find one but were denied access. */
got_eacces = 1;
case ENOENT:
case ESTALE:
case ENOTDIR:
/* Those errors indicate the file is missing or not executable
by us, in which case we want to just try the next path
directory. */
break;
default:
/* Some other error means we found an executable file, but
something went wrong executing it; return the error to our
caller. */
return -1;
}
}
while (*p++ != '\0');
/* We tried every element and none of them worked. */
if (got_eacces)
/* At least one failure was due to permissions, so report that
error. */
errno = EACCES;
}
/* Return the error from the last attempt (probably ENOENT). */
return -1;
}
#ifdef HAVE_FACCESSAT
int faccessat(int dirfd, const char *pathname, int mode, int flags)
{
SBOX_MAP_AT_PROLOGUE();
int ret;
SBOX_MAP_PATH_AT(dirfd, pathname, sbox_path);
if (next_faccessat == NULL) libsb2_init();
ret = next_faccessat(dirfd, sbox_path, mode, flags);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_FCHMODAT
int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags)
{
SBOX_MAP_AT_PROLOGUE();
int ret;
SBOX_MAP_PATH_AT(dirfd, pathname, sbox_path);
if (next_fchmodat == NULL) libsb2_init();
ret = next_fchmodat(dirfd, sbox_path, mode, flags);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_FCHOWNAT
int fchownat (int dirfd, const char *pathname, uid_t owner, gid_t group, int flags)
{
SBOX_MAP_AT_PROLOGUE();
int ret;
SBOX_MAP_PATH_AT(dirfd, pathname, sbox_path);
if (next_fchownat == NULL) libsb2_init();
ret = next_fchownat(dirfd, sbox_path, owner, group, flags);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <stdio.h> */
FILE *fopen (const char *path, const char *mode)
{
SBOX_MAP_PROLOGUE();
FILE *ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_fopen == NULL) libsb2_init();
ret = next_fopen(sbox_path, mode);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <stdio.h> */
FILE *fopen64 (const char *path, const char *mode)
{
SBOX_MAP_PROLOGUE();
FILE *ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_fopen64 == NULL) libsb2_init();
ret = next_fopen64(sbox_path, mode);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <stdio.h> */
FILE *freopen (const char *path, const char *mode, FILE *stream)
{
SBOX_MAP_PROLOGUE();
FILE *ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_freopen == NULL) libsb2_init();
ret = next_freopen(sbox_path, mode, stream);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <stdio.h> */
FILE *freopen64 (const char *path, const char *mode, FILE *stream)
{
SBOX_MAP_PROLOGUE();
FILE *ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_freopen64 == NULL) libsb2_init();
ret = next_freopen64(sbox_path, mode, stream);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_FSTATAT
int fstatat(int dirfd, const char *pathname, struct stat *buf, int flags)
{
SBOX_MAP_AT_PROLOGUE();
int ret;
SBOX_MAP_PATH_AT(dirfd, pathname, sbox_path);
if (next_fstatat == NULL) libsb2_init();
ret = next_fstatat(dirfd, sbox_path, buf, flags);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_FTS_OPEN
#if !defined(HAVE___OPENDIR2)
/* #include <fts.h> */
FTS * fts_open (char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **)) {
SBOX_MAP_PROLOGUE();
char *path;
char * const *p;
char **new_path_argv;
char **np;
int n;
for (n=0, p=path_argv; *p; n++, p++);
if ((new_path_argv = malloc(n*(sizeof(char *)))) == NULL) {
return NULL;
}
for (n=0, p=path_argv, np=new_path_argv; *p; n++, p++, np++) {
path = *p;
SBOX_MAP_PATH(path, sbox_path);
*np = sbox_path;
}
if (next_fts_open == NULL) libsb2_init();
return next_fts_open(new_path_argv, options, compar);
}
#endif
#endif
#ifdef HAVE_FTW
#if !defined(HAVE___OPENDIR2) && !defined(HAVE__XFTW)
/* include <ftw.h> */
int ftw (const char *dir, int (*fn)(const char *file, const struct stat *sb, int flag), int nopenfd)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(dir, sbox_path);
if (next_ftw == NULL) libsb2_init();
ret = next_ftw(sbox_path, fn, nopenfd);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#endif
#ifdef HAVE_FTW64
#if !defined(HAVE___OPENDIR2) && !defined(HAVE__XFTW64)
/* include <ftw.h> */
int ftw64 (const char *dir, int (*fn)(const char *file, const struct stat64 *sb, int flag), int nopenfd)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(dir, sbox_path);
if (next_ftw64 == NULL) libsb2_init();
ret = next_ftw64(sbox_path, fn, nopenfd);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#endif
#ifdef HAVE_FUTIMESAT
int futimesat(int dirfd, const char *pathname, const struct timeval times[2])
{
SBOX_MAP_AT_PROLOGUE();
int ret;
SBOX_MAP_PATH_AT(dirfd, pathname, sbox_path);
if (next_futimesat == NULL) libsb2_init();
ret = next_futimesat(dirfd, sbox_path, times);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_GET_CURRENT_DIR_NAME
/* #include <unistd.h> */
char * get_current_dir_name (void)
{
SBOX_MAP_PROLOGUE();
char *cwd;
if (next_get_current_dir_name == NULL) libsb2_init();
if ((cwd = next_get_current_dir_name()) == NULL) {
return NULL;
}
SBOX_MAP_PATH_NARROW(cwd, sbox_path);
free(cwd);
return sbox_path;
}
#endif
/* #include <unistd.h> */
char * getcwd (char *buf, size_t size)
{
SBOX_MAP_PROLOGUE();
char *cwd;
if (next_getcwd == NULL) libsb2_init();
if ((cwd = next_getcwd(buf, size)) == NULL) {
return NULL;
}
SBOX_MAP_PATH_NARROW(cwd, sbox_path);
if (sbox_path) {
strncpy(buf, sbox_path, size);
free(sbox_path);
}
return cwd;
}
/* #include <unistd.h> */
char * getwd (char *buf)
{
SBOX_MAP_PROLOGUE();
char *cwd;
if (next_getwd == NULL) libsb2_init();
if ((cwd = next_getwd(buf)) == NULL) {
return NULL;
}
SBOX_MAP_PATH_NARROW(cwd, sbox_path);
if (sbox_path) {
strcpy(buf, sbox_path);
free(sbox_path);
}
return cwd;
}
#ifdef HAVE_GETXATTR
/* #include <sys/xattr.h> */
ssize_t getxattr (const char *path, const char *name, void *value, size_t size)
{
SBOX_MAP_PROLOGUE();
ssize_t ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_getxattr == NULL) libsb2_init();
ret = next_getxattr(sbox_path, name, value, size);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <glob.h> */
int glob (const char *pattern, int flags, int (*errfunc) (const char *, int), glob_t *pglob)
{
SBOX_MAP_PROLOGUE();
int rc;
unsigned int i;
char tmp[SBOX_MAXPATH];
SBOX_MAP_PATH(pattern, sbox_path);
if (next_glob == NULL) libsb2_init();
rc = next_glob(sbox_path, flags, errfunc, pglob);
if (sbox_path) free(sbox_path);
if (rc < 0) return rc;
for(i = 0; i < pglob->gl_pathc; i++) {
strcpy(tmp,pglob->gl_pathv[i]);
sbox_path = scratchbox_path(__FUNCTION__, tmp);
strcpy(pglob->gl_pathv[i], sbox_path);
if (sbox_path) free(sbox_path);
}
return rc;
}
#ifdef HAVE_GLOB64
/* #include <glob.h> */
int glob64 (const char *pattern, int flags, int (*errfunc) (const char *, int), glob64_t *pglob)
{
SBOX_MAP_PROLOGUE();
int rc;
unsigned int i;
char tmp[SBOX_MAXPATH];
if (next_glob64 == NULL) libsb2_init();
SBOX_MAP_PATH(pattern, sbox_path);
rc = next_glob64(sbox_path, flags, errfunc, pglob);
if (sbox_path) free(sbox_path);
if (rc < 0) return rc;
for(i = 0; i < pglob->gl_pathc; i++) {
strcpy(tmp,pglob->gl_pathv[i]);
sbox_path = scratchbox_path(__FUNCTION__, tmp);
strcpy(pglob->gl_pathv[i], sbox_path);
if (sbox_path) free(sbox_path);
}
return rc;
}
#endif
#ifdef HAVE_GLOB_PATTERN_P
/* #include <glob.h> */
int glob_pattern_p (const char *pattern, int quote)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(pattern, sbox_path);
if (next_glob_pattern_p == NULL) libsb2_init();
ret = next_glob_pattern_p(sbox_path, quote);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_LCHMOD
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
int lchmod (const char *path, mode_t mode)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_lchmod == NULL) libsb2_init();
ret = next_lchmod(sbox_path, mode);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <sys/types.h> */
/* #include <unistd.h> */
int lchown (const char *path, uid_t owner, gid_t group)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_lchown == NULL) libsb2_init();
ret = next_lchown(sbox_path, owner, group);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_LCKPWDF
/* #include <shadow.h> */
int lckpwdf (void)
{
return 0;
}
#endif
#ifdef HAVE_LGETXATTR
/* #include <sys/xattr.h> */
ssize_t lgetxattr (const char *path, const char *name, void *value, size_t size)
{
SBOX_MAP_PROLOGUE();
ssize_t ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_lgetxattr == NULL) libsb2_init();
ret = next_lgetxattr(sbox_path, name, value, size);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <unistd.h> */
int link(const char *oldpath, const char *newpath)
{
char *sbox_path_old, *sbox_path_new;
int ret;
SBOX_MAP_PATH(oldpath, sbox_path_old);
SBOX_MAP_PATH(newpath, sbox_path_new);
if (next_link == NULL) libsb2_init();
ret = next_link(sbox_path_old, sbox_path_new);
if (sbox_path_old) free(sbox_path_old);
if (sbox_path_new) free(sbox_path_new);
return ret;
}
#ifdef HAVE_LINKAT
int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags)
{
char *sbox_path_old, *sbox_path_new;
int ret;
SBOX_MAP_PATH_AT(olddirfd, oldpath, sbox_path_old);
SBOX_MAP_PATH_AT(newdirfd, newpath, sbox_path_new);
if (next_linkat == NULL) libsb2_init();
ret = next_linkat(olddirfd, sbox_path_old, newdirfd, sbox_path_new, flags);
if (sbox_path_old) free(sbox_path_old);
if (sbox_path_new) free(sbox_path_new);
return ret;
}
#endif
#ifdef HAVE_LISTXATTR
/* #include <sys/xattr.h> */
ssize_t listxattr (const char *path, char *list, size_t size)
{
SBOX_MAP_PROLOGUE();
ssize_t ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_listxattr == NULL) libsb2_init();
ret = next_listxattr(sbox_path, list, size);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_LLISTXATTR
/* #include <sys/xattr.h> */
ssize_t llistxattr (const char *path, char *list, size_t size)
{
SBOX_MAP_PROLOGUE();
ssize_t ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_llistxattr == NULL) libsb2_init();
ret = next_llistxattr(sbox_path, list, size);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_LREMOVEXATTR
/* #include <sys/xattr.h> */
int lremovexattr (const char *path, const char *name)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_lremovexattr == NULL) libsb2_init();
ret = next_lremovexattr(sbox_path, name);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_LSETXATTR
/* #include <sys/xattr.h> */
int lsetxattr (const char *path, const char *name, const void *value, size_t size, int flags)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_lsetxattr == NULL) libsb2_init();
ret = next_lsetxattr(sbox_path, name, value, size, flags);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#if !defined(HAVE___LXSTAT)
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int lstat (const char *file_name, struct stat *buf)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(file_name, sbox_path);
if (next_lstat == NULL) libsb2_init();
ret = next_lstat(sbox_path, buf);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_LSTAT64
#if !defined(HAVE___LXSTAT64)
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int lstat64 (const char *file_name, struct stat64 *buf)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(file_name, sbox_path);
if (next_lstat64 == NULL) libsb2_init();
ret = next_lstat64(sbox_path, buf);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#endif
#ifdef HAVE_LUTIMES
/* #include <sys/time.h> */
int lutimes(const char *filename, const struct timeval tv[2])
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(filename, sbox_path);
if (next_lutimes == NULL) libsb2_init();
ret = next_lutimes(sbox_path, tv);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <sys/stat.h> */
/* #include <sys/types.h> */
int mkdir(const char *pathname, mode_t mode)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(pathname, sbox_path);
if (next_mkdir == NULL) libsb2_init();
ret = next_mkdir(sbox_path, mode);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_MKDIRAT
int mkdirat(int dirfd, const char *pathname, mode_t mode)
{
SBOX_MAP_AT_PROLOGUE();
int ret;
SBOX_MAP_PATH_AT(dirfd, pathname, sbox_path);
if (next_mkdirat == NULL) libsb2_init();
ret = next_mkdirat(dirfd, sbox_path, mode);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_MKDTEMP
/* #include <stdlib.h> */
char *mkdtemp (char *template)
{
// char tmp[FAKECHROOT_MAXPATH], *oldtemplate, *ptr;
// SBOX_MAP_PROLOGUE();
// oldtemplate = template;
// SBOX_MAP_PATH(template, sbox_path);
if (next_mkdtemp == NULL) libsb2_init();
if (next_mkdtemp(template) == NULL) {
return NULL;
}
// ptr = tmp;
// strcpy(ptr, template);
// narrow_chroot_path(ptr, sbox_path);
// if (ptr == NULL) {
// return NULL;
// }
// strcpy(oldtemplate, ptr);
// DBGOUT("before returning from mkdtemp\n");
return template;
}
#endif
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
int mkfifo (const char *pathname, mode_t mode)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(pathname, sbox_path);
if (next_mkfifo == NULL) libsb2_init();
ret = next_mkfifo(sbox_path, mode);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_MKFIFOAT
int mkfifoat(int dirfd, const char *pathname, mode_t mode)
{
SBOX_MAP_AT_PROLOGUE();
int ret;
SBOX_MAP_PATH_AT(dirfd, pathname, sbox_path);
if (next_mkfifoat == NULL) libsb2_init();
ret = next_mkfifoat(dirfd, sbox_path, mode);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
/* #include <fcntl.h> */
/* #include <unistd.h> */
int mknod (const char *pathname, mode_t mode, dev_t dev)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(pathname, sbox_path);
if (next_mknod == NULL) libsb2_init();
ret = next_mknod(sbox_path, mode, dev);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_MKNODAT
int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev)
{
SBOX_MAP_AT_PROLOGUE();
int ret;
SBOX_MAP_PATH_AT(dirfd, pathname, sbox_path);
if (next_mknodat == NULL) libsb2_init();
ret = next_mknodat(dirfd, sbox_path, mode, dev);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <stdlib.h> */
int mkstemp (char *template)
{
if (next_mkstemp == NULL) libsb2_init();
return next_mkstemp(template);
}
/* #include <stdlib.h> */
int mkstemp64 (char *template)
{
if (next_mkstemp64 == NULL) libsb2_init();
return next_mkstemp64(template);
}
/* #include <stdlib.h> */
char *mktemp (char *template)
{
if (next_mktemp == NULL) libsb2_init();
return next_mktemp(template);
}
#ifdef HAVE_NFTW
/* #include <ftw.h> */
int nftw (const char *dir, int (*fn)(const char *file, const struct stat *sb, int flag, struct FTW *s), int nopenfd, int flags)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(dir, sbox_path);
if (next_nftw == NULL) libsb2_init();
ret = next_nftw(sbox_path, fn, nopenfd, flags);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_NFTW64
/* #include <ftw.h> */
int nftw64 (const char *dir, int (*fn)(const char *file, const struct stat64 *sb, int flag, struct FTW *s), int nopenfd, int flags)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(dir, sbox_path);
if (next_nftw64 == NULL) libsb2_init();
ret = next_nftw64(sbox_path, fn, nopenfd, flags);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
/* #include <fcntl.h> */
int open(const char *pathname, int flags, ...)
{
SBOX_MAP_PROLOGUE();
int ret;
int mode = 0;
SBOX_MAP_PATH(pathname, sbox_path);
if (flags & O_CREAT) {
va_list arg;
va_start (arg, flags);
mode = va_arg (arg, int);
va_end (arg);
}
if (next_open == NULL) libsb2_init();
ret = next_open(sbox_path, flags, mode);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
/* #include <fcntl.h> */
int open64 (const char *pathname, int flags, ...)
{
SBOX_MAP_PROLOGUE();
int ret;
int mode = 0;
SBOX_MAP_PATH(pathname, sbox_path);
if (flags & O_CREAT) {
va_list arg;
va_start (arg, flags);
mode = va_arg (arg, int);
va_end (arg);
}
if (next_open64 == NULL) libsb2_init();
ret = next_open64(sbox_path, flags, mode);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_OPENAT
int openat(int dirfd, const char *pathname, int flags, ...)
{
SBOX_MAP_AT_PROLOGUE();
int ret;
int mode = 0;
SBOX_MAP_PATH_AT(dirfd, pathname, sbox_path);
if (flags & O_CREAT) {
va_list arg;
va_start (arg, flags);
mode = va_arg (arg, int);
va_end (arg);
}
if (next_openat == NULL) libsb2_init();
ret = next_openat(dirfd, sbox_path, flags, mode);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_OPENAT64
int openat64(int dirfd, const char *pathname, int flags, ...)
{
SBOX_MAP_AT_PROLOGUE();
int ret;
int mode = 0;
SBOX_MAP_PATH_AT(dirfd, pathname, sbox_path);
if (flags & O_CREAT) {
va_list arg;
va_start (arg, flags);
mode = va_arg (arg, int);
va_end (arg);
}
if (next_openat64 == NULL) libsb2_init();
ret = next_openat64(dirfd, sbox_path, flags, mode);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#if !defined(HAVE___OPENDIR2)
/* #include <sys/types.h> */
/* #include <dirent.h> */
DIR *opendir (const char *name)
{
SBOX_MAP_PROLOGUE();
DIR *ret;
SBOX_MAP_PATH(name, sbox_path);
if (next_opendir == NULL) libsb2_init();
ret = next_opendir(sbox_path);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <unistd.h> */
long pathconf (const char *path, int name)
{
SBOX_MAP_PROLOGUE();
long ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_pathconf == NULL) libsb2_init();
ret = next_pathconf(sbox_path, name);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <unistd.h> */
/* XXX: add proc pid/exe wrapper from libsb to here */
READLINK_TYPE readlink (const char *path, char *buf, size_t bufsize)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_readlink == NULL) libsb2_init();
ret = next_readlink(sbox_path, buf, bufsize);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_READLINKAT
READLINK_TYPE readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsize)
{
SBOX_MAP_AT_PROLOGUE();
int ret;
SBOX_MAP_PATH_AT(dirfd, pathname, sbox_path);
if (next_readlinkat == NULL) libsb2_init();
ret = next_readlinkat(dirfd, sbox_path, buf, bufsize);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <stdlib.h> */
char *realpath (const char *name, char *resolved)
{
SBOX_MAP_PROLOGUE();
char *ret;
SBOX_MAP_PATH(name, sbox_path);
if (next_realpath == NULL) libsb2_init();
ret = next_realpath(sbox_path, resolved);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <stdio.h> */
int remove(const char *pathname)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(pathname, sbox_path);
if (next_remove == NULL) libsb2_init();
ret = next_remove(sbox_path);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_REMOVEXATTR
/* #include <sys/xattr.h> */
int removexattr(const char *path, const char *name)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_removexattr == NULL) libsb2_init();
ret = next_removexattr(sbox_path, name);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <stdio.h> */
int rename(const char *oldpath, const char *newpath)
{
char *sbox_path_old, *sbox_path_new;
int ret;
SBOX_MAP_PATH(oldpath, sbox_path_old);
SBOX_MAP_PATH(newpath, sbox_path_new);
if (next_rename == NULL) libsb2_init();
ret = next_rename(sbox_path_old, sbox_path_new);
if (sbox_path_old) free(sbox_path_old);
if (sbox_path_new) free(sbox_path_new);
return ret;
}
#ifdef HAVE_RENAMEAT
int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath)
{
char *sbox_path_old, *sbox_path_new;
int ret;
SBOX_MAP_PATH_AT(olddirfd, oldpath, sbox_path_old);
SBOX_MAP_PATH_AT(newdirfd, newpath, sbox_path_new);
if (next_renameat == NULL) libsb2_init();
ret = next_renameat(olddirfd, sbox_path_old, newdirfd, sbox_path_new);
if (sbox_path_old) free(sbox_path_old);
if (sbox_path_new) free(sbox_path_new);
return ret;
}
#endif
#ifdef HAVE_REVOKE
/* #include <unistd.h> */
int revoke(const char *file)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(file, sbox_path);
if (next_revoke == NULL) libsb2_init();
ret = next_revoke(sbox_path);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <unistd.h> */
int rmdir(const char *pathname)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(pathname, sbox_path);
if (next_rmdir == NULL) libsb2_init();
ret = next_rmdir(sbox_path);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_SCANDIR
/* #include <dirent.h> */
int scandir (const char *dir, struct dirent ***namelist, SCANDIR_TYPE_ARG3, int(*compar)(const void *, const void *))
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(dir, sbox_path);
if (next_scandir == NULL) libsb2_init();
ret = next_scandir(sbox_path, namelist, filter, compar);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_SCANDIR64
/* #include <dirent.h> */
int scandir64 (const char *dir, struct dirent64 ***namelist, int(*filter)(const struct dirent64 *), int(*compar)(const void *, const void *))
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(dir, sbox_path);
if (next_scandir64 == NULL) libsb2_init();
ret = next_scandir64(sbox_path, namelist, filter, compar);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_SETXATTR
/* #include <sys/xattr.h> */
int setxattr (const char *path, const char *name, const void *value, size_t size, int flags)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_setxattr == NULL) libsb2_init();
ret = next_setxattr(sbox_path, name, value, size, flags);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#if !defined(HAVE___XSTAT)
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int stat(const char *file_name, struct stat *buf)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(file_name, sbox_path);
if (next_stat == NULL) libsb2_init();
ret = next_stat(sbox_path, buf);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_STAT64
#if !defined(HAVE___XSTAT64)
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int stat64(const char *file_name, struct stat64 *buf)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(file_name, sbox_path);
if (next_stat64 == NULL) libsb2_init();
ret = next_stat64(sbox_path, buf);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#endif
/* #include <unistd.h> */
int symlink(const char *oldpath, const char *newpath)
{
char *sbox_path_old, *sbox_path_new;
int ret;
SBOX_MAP_PATH(oldpath, sbox_path_old);
SBOX_MAP_PATH(newpath, sbox_path_new);
if (next_symlink == NULL) libsb2_init();
ret = next_symlink(sbox_path_old, sbox_path_new);
if (sbox_path_old) free(sbox_path_old);
if (sbox_path_new) free(sbox_path_new);
return ret;
}
#ifdef HAVE_SYMLINKAT
int symlinkat(const char *oldpath, int newdirfd, const char *newpath)
{
char *sbox_path_old, *sbox_path_new;
int ret;
SBOX_MAP_PATH(oldpath, sbox_path_old);
SBOX_MAP_PATH_AT(newdirfd, newpath, sbox_path_new);
if (next_symlinkat == NULL) libsb2_init();
ret = next_symlinkat(sbox_path_old, newdirfd, sbox_path_new);
if (sbox_path_old) free(sbox_path_old);
if (sbox_path_new) free(sbox_path_new);
return ret;
}
#endif
/* #include <stdio.h> */
char *tempnam (const char *dir, const char *pfx)
{
SBOX_MAP_PROLOGUE();
char *ret;
SBOX_MAP_PATH(dir, sbox_path);
if (next_tempnam == NULL) libsb2_init();
ret = next_tempnam(sbox_path, pfx);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <stdio.h> */
char *tmpnam (char *s)
{
if (next_tmpnam == NULL) libsb2_init();
return next_tmpnam(s);
}
/* #include <unistd.h> */
/* #include <sys/types.h> */
int truncate (const char *path, off_t length)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_truncate == NULL) libsb2_init();
ret = next_truncate(sbox_path, length);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_TRUNCATE64
/* #include <unistd.h> */
/* #include <sys/types.h> */
int truncate64 (const char *path, off64_t length)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(path, sbox_path);
if (next_truncate64 == NULL) libsb2_init();
ret = next_truncate64(sbox_path, length);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
#ifdef HAVE_ULCKPWDF
/* #include <shadow.h> */
int ulckpwdf (void)
{
return 0;
}
#endif
/* #include <unistd.h> */
int unlink(const char *pathname)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(pathname, sbox_path);
if (next_unlink == NULL) libsb2_init();
ret = next_unlink(sbox_path);
if (sbox_path) free(sbox_path);
return ret;
}
#ifdef HAVE_UNLINKAT
int unlinkat(int dirfd, const char *pathname, int flags)
{
SBOX_MAP_AT_PROLOGUE();
int ret;
SBOX_MAP_PATH_AT(dirfd, pathname, sbox_path);
if (next_unlinkat == NULL) libsb2_init();
ret = next_unlinkat(dirfd, sbox_path, flags);
if (sbox_path) free(sbox_path);
return ret;
}
#endif
/* #include <sys/types.h> */
/* #include <utime.h> */
int utime (const char *filename, const struct utimbuf *buf)
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(filename, sbox_path);
if (next_utime == NULL) libsb2_init();
ret = next_utime(sbox_path, buf);
if (sbox_path) free(sbox_path);
return ret;
}
/* #include <sys/time.h> */
int utimes (const char *filename, const struct timeval tv[2])
{
SBOX_MAP_PROLOGUE();
int ret;
SBOX_MAP_PATH(filename, sbox_path);
if (next_utimes == NULL) libsb2_init();
ret = next_utimes(sbox_path, tv);
if (sbox_path) free(sbox_path);
return ret;
}
int uname(struct utsname *buf)
{
if (next_uname == NULL) libsb2_init();
if (next_uname(buf) < 0) {
return -1;
}
/* this may be called before environ is properly setup */
if (environ) {
strncpy(buf->machine, getenv("SBOX_UNAME_MACHINE"), sizeof(buf->machine));
}
return 0;
}
|