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
|
/*
* libsb2 -- scratchbox2 preload library
*
* Copyright (C) 2006 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 "sb_env.h"
#include "lua_bindings.h"
#include "sb2.h"
#if defined(PATH_MAX)
#define FAKECHROOT_MAXPATH PATH_MAX
#elif defined(_POSIX_PATH_MAX)
#define FAKECHROOT_MAXPATH _POSIX_PATH_MAX
#elif defined(MAXPATHLEN)
#define FAKECHROOT_MAXPATH MAXPATHLEN
#else
#define FAKECHROOT_MAXPATH 2048
#endif
/* XXX: current code allows ./usr/bin/../../../../../ style escaping
* from chroot
* if ( amount /../ > amount /other/ ) ) remove extra /../
*/
#define narrow_chroot_path(path, fakechroot_path) \
{ \
if ((path) != NULL && *((char *)(path)) != '\0') { \
fakechroot_path = scratchbox_path(__FUNCTION__, path); \
(path) = fakechroot_path; \
} \
}
#define expand_chroot_path(path, fakechroot_path) \
{ \
if ((path) != NULL) { \
fakechroot_path = scratchbox_path(__FUNCTION__, path); \
(path) = fakechroot_path; \
} \
}
#define expand_chroot_path_malloc(path, fakechroot_path) \
{ \
if ((path) != NULL) { \
fakechroot_path = scratchbox_path(__FUNCTION__, path); \
(path) = fakechroot_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;
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_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_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_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_MKDTEMP
static char * (*next_mkdtemp) (char *template) = NULL;
#endif
static int (*next_mknod) (const char *pathname, mode_t mode, dev_t dev) = NULL;
static int (*next_mkfifo) (const char *pathname, mode_t mode) = NULL;
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;
#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;
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_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;
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_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 fakechroot_init (void) __attribute((constructor));
void fakechroot_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(chroot, "chroot"); */
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");
nextsym(fopen, "fopen");
nextsym(fopen64, "fopen64");
nextsym(freopen, "freopen");
nextsym(freopen64, "freopen64");
#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_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_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_MKDTEMP
nextsym(mkdtemp, "mkdtemp");
#endif
nextsym(mknod, "mknod");
nextsym(mkfifo, "mkfifo");
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");
nextsym(open64, "open64");
#if !defined(HAVE___OPENDIR2)
nextsym(opendir, "opendir");
#endif
nextsym(pathconf, "pathconf");
nextsym(readlink, "readlink");
nextsym(realpath, "realpath");
nextsym(remove, "remove");
#ifdef HAVE_REMOVEXATTR
nextsym(removexattr, "removexattr");
#endif
nextsym(rename, "rename");
#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");
nextsym(tempnam, "tempnam");
nextsym(tmpnam, "tmpnam");
nextsym(truncate, "truncate");
#ifdef HAVE_TRUNCATE64
nextsym(truncate64, "truncate64");
#endif
nextsym(unlink, "unlink");
#ifdef HAVE_ULCKPWDF
/* nextsym(ulckpwdf, "ulckpwdf"); */
#endif
nextsym(utime, "utime");
nextsym(utimes, "utimes");
nextsym(uname, "uname");
}
#define RPATH_PREFIX "/scratchbox/"
#define LDPATH "/scratchbox/lib/ld.so"
#define LIBCPATH "/scratchbox/lib/libc.so.6"
#if __BYTE_ORDER == __BIG_ENDIAN
# define elf_endianness ELFDATA2MSB
#elif __BYTE_ORDER == __LITTLE_ENDIAN
# define elf_endianness ELFDATA2LSB
#else
# error Invalid __BYTE_ORDER
#endif
enum binary_type { BIN_NONE, BIN_UNKNOWN, BIN_FOREIGN, BIN_STATIC, BIN_DYNAMIC, BIN_SCRATCHBOX, BIN_TARGET };
static enum binary_type inspect_binary(const char *filename)
{
enum binary_type retval;
int fd, phnum, j;
struct stat status;
char *region;
int reloc0;
unsigned int ph_base, ph_frag;
Elf32_Ehdr *ehdr;
Elf32_Phdr *phdr;
retval = BIN_NONE;
fd = syscall(__NR_open, filename, O_RDONLY, 0);
if (fd < 0) {
goto _out;
}
retval = BIN_UNKNOWN;
if (fstat(fd, &status) < 0) {
goto _out_close;
}
if (!S_ISREG(status.st_mode) && !S_ISLNK(status.st_mode)) {
goto _out_close;
}
region = mmap(0, status.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (!region) {
goto _out_close;
}
ehdr = (Elf32_Ehdr *) region;
switch (ehdr->e_machine) {
case EM_ARM:
case EM_PPC:
retval = BIN_TARGET;
goto _out_munmap;
}
if (strncmp((char *) ehdr, ELFMAG, SELFMAG) != 0) {
goto _out_munmap;
}
retval = BIN_FOREIGN;
if (ehdr->e_ident[EI_DATA] != elf_endianness ||
ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
ehdr->e_machine != ELF_ARCH) {
goto _out_munmap;
}
retval = BIN_STATIC;
phnum = ehdr->e_phnum;
reloc0 = ~0;
ph_base = ehdr->e_phoff & PAGE_MASK;
ph_frag = ehdr->e_phoff - ph_base;
phdr = (Elf32_Phdr *) (region + ph_base + ph_frag);
for (j = phnum; --j >= 0; ++phdr) {
if (PT_LOAD == phdr->p_type && ~0 == reloc0) {
reloc0 = phdr->p_vaddr - phdr->p_offset;
}
}
phdr -= phnum;
for (j = phnum; --j >= 0; ++phdr) {
int szDyn;
unsigned int pt_base, pt_frag;
Elf32_Dyn *dp_rpath, *dp_strsz, *dp_strtab, *dp;
if (PT_DYNAMIC != phdr->p_type) {
continue;
}
retval = BIN_DYNAMIC;
pt_base = phdr->p_offset & PAGE_MASK;
pt_frag = phdr->p_offset - pt_base;
szDyn = phdr->p_filesz;
dp_rpath = NULL;
dp_strsz = NULL;
dp_strtab = NULL;
dp = (Elf32_Dyn *) ((char *) region + pt_base + pt_frag);
for (; 0 <= (szDyn -= sizeof (*dp)); ++dp) {
if (DT_RPATH == dp->d_tag) {
dp_rpath = dp;
} else if (DT_STRTAB == dp->d_tag) {
dp_strtab = dp;
} else if (DT_STRSZ == dp->d_tag) {
dp_strsz = dp;
}
}
if (dp_rpath && dp_strtab && dp_strsz) {
char *rpathstr, *strtab, *match;
unsigned int unloc, str_base, str_frag;
unloc = dp_strtab->d_un.d_val - reloc0;
str_base = unloc & PAGE_MASK;
str_frag = unloc - str_base;
strtab = (char *) (region + str_base + str_frag);
rpathstr = dp_rpath->d_un.d_val + strtab;
match = strstr(rpathstr, RPATH_PREFIX);
if (match && (match == rpathstr || match[-1] == ':')) {
retval = BIN_SCRATCHBOX;
break;
}
}
}
_out_munmap:
munmap(region, status.st_size);
_out_close:
close(fd);
_out:
return retval;
}
static int is_gcc_tool(char *fname)
{
unsigned int i, index, start, c;
char *t;
char **gcc_prefixes;
char **p;
char *gcc_tools[] = {
"addr2line",
"ar",
"as",
"cc",
"c++",
"c++filt",
"cpp",
"g++",
"gcc",
"gcov",
"gdb",
"gdbtui",
"gprof",
"ld",
"nm",
"objcopy",
"objdump",
"ranlib",
"rdi-stub",
"readelf",
"run",
"size",
"strings",
"strip",
NULL
};
char **tmp;
t = getenv("SBOX_CROSS_GCC_PREFIX_LIST");
if (!t) {
return 0;
}
t = strdup(t);
for (i = 0, c = 0; i < strlen(t); i++) {
if (t[i] == ':') {
c++;
}
}
gcc_prefixes = (char **)calloc(c + 2, sizeof(char *));
p = gcc_prefixes;
for (start = 0, index = 0; index < strlen(t); index++) {
if (t[index] == ':') {
*p = malloc((index - start + 1) * sizeof(char));
strncpy(*p, &t[start], index - start);
(*p)[index - start] = '\0';
p++;
start = index + 1;
}
}
p = NULL;
for (tmp = gcc_tools; *tmp; tmp++) {
for (p = gcc_prefixes; *p; p++) {
char s[PATH_MAX];
strcpy(s, *p);
strcat(s, *tmp);
if (!strcmp(*tmp, fname) || (!strcmp(s, fname))) {
return 1;
}
}
}
return 0;
}
static char const* const*drop_preload(char *const oldenv[])
{
char const* *newenv;
int i, j;
for (i = 0; oldenv[i]; ++i)
;
newenv = (char const**) calloc(i + 1, sizeof (char *));
if (!newenv) {
fprintf(stderr, "libsb: %s\n", strerror(errno));
return (char const*const*)oldenv;
}
for (i = 0, j = 0; oldenv[i]; ++i) {
if (strncmp(oldenv[i], "LD_PRELOAD=", 11) != 0) {
newenv[j++] = oldenv[i];
}
}
return newenv;
}
static int do_exec(const char *file, char *const *argv, char *const *envp)
{
char **my_envp, **my_argv, **p;
char *binaryname, *tmp, *my_file;
int envc=0, argc=0, i;
if (next_execve == NULL) fakechroot_init();
enum binary_type type = inspect_binary(file);
binaryname = strdup(basename(file));
/* count the environment variables and arguments */
for (p=(char **)envp; *p; p++, envc++)
;
for (p=(char **)argv; *p; p++, argc++)
;
//printf("envc: %i\n", envc);
my_envp = (char **)calloc(envc + 2, sizeof(char *));
i = strlen(file) + strlen("__SB2_BINARYNAME") + 1;
tmp = malloc(i * sizeof(char *));
strcpy(tmp, "__SB2_BINARYNAME=");
strcat(tmp, binaryname);
i = 0;
for (p=(char **)envp; *p; p++) {
/* DBGOUT("ENV: [%s]\n", *p); */
if (strncmp(*p, "__SB2_BINARYNAME=", strlen("__SB2_BINARYNAME=")) == 0) {
/* already set, skip it */
continue;
}
if (strncmp(*p, "__SBOX_GCCWRAPPER_RUN=", strlen("__SBOX_GCCWRAPPER_RUN")) == 0) {
/* don't pass this onwards */
continue;
}
my_envp[i++] = *p;
}
my_envp[i++] = strdup(tmp);
free(tmp);
char const* const* new_env=(char const* const*)my_envp;
my_argv = (char **)calloc(argc + 1, sizeof(char *));
i = 0;
my_file = strdup(file);
if (!getenv("__SBOX_GCCWRAPPER_RUN") && is_gcc_tool(binaryname)) {
/* unset the env variable */
char *sb_gcc_wrapper;
sb_gcc_wrapper = getenv("SBOX_GCCWRAPPER");
if (!sb_gcc_wrapper) {
my_file = "/usr/bin/sb_gcc_wrapper";
} else {
my_file = strdup(sb_gcc_wrapper);
}
/*DBGOUT("we've a gcc tool!\n");*/
my_argv[i++] = strdup(binaryname);
}
/* printf("type: %i\n", type);*/
switch (type) {
case BIN_FOREIGN:
new_env = drop_preload(my_envp);
break;
case BIN_STATIC:
new_env = override_sbox_env(drop_preload(my_envp));
break;
case BIN_DYNAMIC:
{
if (getenv("SBOX_TOOLS_ROOT")) {
return ld_so_run_app((char *)my_file, (char **)argv, my_envp);
} else {
return run_app((char *)my_file, (char **)argv, my_envp);
}
}
case BIN_TARGET:
/* DBGOUT("cpu transparency needed\n"); */
return run_cputransparency(my_file, (char **)argv, my_envp);
case BIN_NONE:
case BIN_UNKNOWN:
case BIN_SCRATCHBOX:
DBGOUT("unknown\n");
break;
}
return next_execve(my_file, argv, (char *const*)new_env);
}
#if 0
void scratchbox_init (void) __attribute((constructor));
void scratchbox_init (void)
{
/* currently doing nothing */
DBGOUT("in scratchbox_init\n");
}
#endif
#ifdef HAVE___LXSTAT
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int __lxstat (int ver, const char *filename, struct stat *buf)
{
char *fakechroot_path;
expand_chroot_path(filename, fakechroot_path);
if (next___lxstat == NULL) fakechroot_init();
return next___lxstat(ver, filename, buf);
}
#endif
#ifdef HAVE___LXSTAT64
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int __lxstat64 (int ver, const char *filename, struct stat64 *buf)
{
int r;
char *fakechroot_path;
expand_chroot_path(filename, fakechroot_path);
if (next___lxstat64 == NULL) fakechroot_init();
r = next___lxstat64(ver, filename, buf);
return r;
}
#endif
#ifdef HAVE___OPEN
/* Internal libc function */
int __open (const char *pathname, int flags, ...)
{
int mode = 0;
char *fakechroot_path;
printf("Avataan filu %s\n",pathname);
expand_chroot_path(pathname, fakechroot_path);
if (flags & O_CREAT) {
va_list arg;
va_start (arg, flags);
mode = va_arg (arg, int);
va_end (arg);
}
if (next___open == NULL) fakechroot_init();
return next___open(pathname, flags, mode);
}
#endif
#ifdef HAVE___OPEN64
/* Internal libc function */
int __open64 (const char *pathname, int flags, ...)
{
int mode = 0;
char *fakechroot_path;
printf("Avataan filu64 %s\n",pathname);
expand_chroot_path(pathname, fakechroot_path);
if (flags & O_CREAT) {
va_list arg;
va_start (arg, flags);
mode = va_arg (arg, int);
va_end (arg);
}
if (next___open64 == NULL) fakechroot_init();
return next___open64(pathname, flags, mode);
}
#endif
#ifdef HAVE___OPENDIR2
/* Internal libc function */
/* #include <dirent.h> */
DIR *__opendir2 (const char *name, int flags)
{
char *fakechroot_path;
expand_chroot_path(name, fakechroot_path);
if (next___opendir2 == NULL) fakechroot_init();
return next___opendir2(name, flags);
}
#endif
#ifdef HAVE___XMKNOD
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int __xmknod (int ver, const char *path, mode_t mode, dev_t *dev)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next___xmknod == NULL) fakechroot_init();
return next___xmknod(ver, path, mode, dev);
}
#endif
#ifdef HAVE___XSTAT
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int __xstat (int ver, const char *filename, struct stat *buf)
{
char *fakechroot_path;
expand_chroot_path(filename, fakechroot_path);
if (next___xstat == NULL) fakechroot_init();
return next___xstat(ver, filename, buf);
}
#endif
#ifdef HAVE___XSTAT64
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int __xstat64 (int ver, const char *filename, struct stat64 *buf)
{
char *fakechroot_path;
expand_chroot_path(filename, fakechroot_path);
if (next___xstat64 == NULL) fakechroot_init();
return next___xstat64(ver, filename, buf);
}
#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)
{
char *fakechroot_path;
expand_chroot_path(dir, fakechroot_path);
if (next__xftw == NULL) fakechroot_init();
return next__xftw(mode, dir, fn, nopenfd);
}
#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)
{
char *fakechroot_path;
expand_chroot_path(dir, fakechroot_path);
if (next__xftw64 == NULL) fakechroot_init();
return next__xftw64(mode, dir, fn, nopenfd);
}
#endif
/* #include <unistd.h> */
int access (const char *pathname, int mode)
{
char *fakechroot_path;
expand_chroot_path(pathname, fakechroot_path);
if (next_access == NULL) fakechroot_init();
return next_access(pathname, mode);
}
/* #include <unistd.h> */
int acct (const char *filename)
{
char *fakechroot_path;
expand_chroot_path(filename, fakechroot_path);
if (next_acct == NULL) fakechroot_init();
return next_acct(filename);
}
#ifdef HAVE_CANONICALIZE_FILE_NAME
/* #include <stdlib.h> */
char *canonicalize_file_name (const char *name)
{
char *fakechroot_path;
expand_chroot_path(name, fakechroot_path);
if (next_canonicalize_file_name == NULL) fakechroot_init();
return next_canonicalize_file_name(name);
}
#endif
/* #include <unistd.h> */
int chdir (const char *path)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_chdir == NULL) fakechroot_init();
return next_chdir(path);
}
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
int chmod (const char *path, mode_t mode)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_chmod == NULL) fakechroot_init();
return next_chmod(path, mode);
}
/* #include <sys/types.h> */
/* #include <unistd.h> */
int chown (const char *path, uid_t owner, gid_t group)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_chown == NULL) fakechroot_init();
return next_chown(path, owner, group);
}
/* #include <unistd.h> */
/* XXX: fixing needed for scratchbox2 */
int chroot (const char *path)
{
char *ptr, *ld_library_path, *tmp, *fakechroot_path;
int status, len;
char dir[FAKECHROOT_MAXPATH];
#if !defined(HAVE_SETENV)
char *envbuf;
#endif
fakechroot_path = getenv("FAKECHROOT_BASE");
if (fakechroot_path != NULL) {
return EFAULT;
}
if ((status = chdir(path)) != 0) {
return status;
}
if (getcwd(dir, FAKECHROOT_MAXPATH) == NULL) {
return EFAULT;
}
ptr = rindex(dir, 0);
if (ptr > dir) {
ptr--;
while (*ptr == '/') {
*ptr-- = 0;
}
}
#if defined(HAVE_SETENV)
setenv("FAKECHROOT_BASE", dir, 1);
#else
envbuf = malloc(FAKECHROOT_MAXPATH+16);
snprintf(envbuf, FAKECHROOT_MAXPATH+16, "FAKECHROOT_BASE=%s", dir);
putenv(envbuf);
#endif
fakechroot_path = getenv("FAKECHROOT_BASE");
ld_library_path = getenv("LD_LIBRARY_PATH");
if (ld_library_path == NULL) {
ld_library_path = "";
}
if ((len = strlen(ld_library_path)+strlen(dir)*2+sizeof(":/usr/lib:/lib")) > FAKECHROOT_MAXPATH) {
return ENAMETOOLONG;
}
if ((tmp = malloc(len)) == NULL) {
return ENOMEM;
}
snprintf(tmp, len, "%s:%s/usr/lib:%s/lib", ld_library_path, dir, dir);
#if defined(HAVE_SETENV)
setenv("LD_LIBRARY_PATH", tmp, 1);
#else
envbuf = malloc(FAKECHROOT_MAXPATH+16);
snprintf(envbuf, FAKECHROOT_MAXPATH+16, "LD_LIBRARY_PATH=%s", tmp);
putenv(envbuf);
#endif
free(tmp);
return 0;
}
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
/* #include <fcntl.h> */
int creat (const char *pathname, mode_t mode)
{
char *fakechroot_path;
expand_chroot_path(pathname, fakechroot_path);
if (next_creat == NULL) fakechroot_init();
return next_creat(pathname, mode);
}
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
/* #include <fcntl.h> */
int creat64 (const char *pathname, mode_t mode)
{
char *fakechroot_path;
expand_chroot_path(pathname, fakechroot_path);
if (next_creat64 == NULL) fakechroot_init();
return next_creat64(pathname, mode);
}
#ifdef HAVE_DLMOPEN
/* #include <dlfcn.h> */
void *dlmopen (Lmid_t nsid, const char *filename, int flag)
{
char *fakechroot_path;
expand_chroot_path(filename, fakechroot_path);
if (next_dlmopen == NULL) fakechroot_init();
return next_dlmopen(nsid, filename, flag);
}
#endif
/* #include <dlfcn.h> */
void *dlopen (const char *filename, int flag)
{
char *fakechroot_path;
expand_chroot_path(filename, fakechroot_path);
if (next_dlopen == NULL) fakechroot_init();
return next_dlopen(filename, flag);
}
#ifdef HAVE_EUIDACCESS
/* #include <unistd.h> */
int euidaccess (const char *pathname, int mode)
{
char *fakechroot_path;
expand_chroot_path(pathname, fakechroot_path);
if (next_euidaccess == NULL) fakechroot_init();
return next_euidaccess(pathname, mode);
}
#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[])
{
int file;
char hashbang[FAKECHROOT_MAXPATH];
size_t argv_max = 1024;
const char **newargv = alloca (argv_max * sizeof (const char *));
char tmp[FAKECHROOT_MAXPATH],
newfilename[FAKECHROOT_MAXPATH],
argv0[FAKECHROOT_MAXPATH];
char *ptr;
int k;
unsigned int i, j, n;
char c;
char *fakechroot_path;
expand_chroot_path(filename, fakechroot_path);
strcpy(tmp, filename);
filename = tmp;
if ((file = open(filename, O_RDONLY)) == -1) {
errno = ENOENT;
return -1;
}
k = read(file, hashbang, FAKECHROOT_MAXPATH-2);
close(file);
if (k == -1) {
errno = ENOENT;
return -1;
}
if (hashbang[0] != '#' || hashbang[1] != '!')
return do_exec(filename, argv, envp);
/* if we're here we have a script */
//printf("hashbang: %s\n", hashbang);
for (i = j = 2; (hashbang[i] == ' ' || hashbang[i] == '\t') && i < FAKECHROOT_MAXPATH; i++, j++) {
//printf("looping\n");
}
//printf("hashbanging: i=%u\n",i);
//hashbang[i] = hashbang[i+1] = 0;
for (n = 0; i < FAKECHROOT_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, fakechroot_path: %s, %s\n", ptr, fakechroot_path);
expand_chroot_path(ptr, fakechroot_path);
strcpy(newfilename, ptr);
strcpy(argv0, &hashbang[j]);
newargv[n++] = argv0;
} else {
newargv[n++] = &hashbang[j];
}
}
j = i + 1;
}
if (c == '\n' || c == 0)
break;
}
//printf("hashbanging: %s, %s\n", filename, fakechroot_path);
expand_chroot_path(filename, fakechroot_path);
newargv[n++] = filename;
for (i = 1; argv[i] != NULL && i < argv_max; ) {
newargv[n++] = argv[i++];
}
newargv[n] = 0;
return do_exec(newfilename, (char *const *)newargv, envp);
}
/* #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;
}
/* #include <stdio.h> */
FILE *fopen (const char *path, const char *mode)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_fopen == NULL) fakechroot_init();
return next_fopen(path, mode);
}
/* #include <stdio.h> */
FILE *fopen64 (const char *path, const char *mode)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_fopen64 == NULL) fakechroot_init();
return next_fopen64(path, mode);
}
/* #include <stdio.h> */
FILE *freopen (const char *path, const char *mode, FILE *stream)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_freopen == NULL) fakechroot_init();
return next_freopen(path, mode, stream);
}
/* #include <stdio.h> */
FILE *freopen64 (const char *path, const char *mode, FILE *stream)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_freopen64 == NULL) fakechroot_init();
return next_freopen64(path, mode, stream);
}
#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 **)) {
char *fakechroot_path;
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;
expand_chroot_path_malloc(path, fakechroot_path);
*np = path;
}
if (next_fts_open == NULL) fakechroot_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)
{
char *fakechroot_path;
expand_chroot_path(dir, fakechroot_path);
if (next_ftw == NULL) fakechroot_init();
return next_ftw(dir, fn, nopenfd);
}
#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)
{
char *fakechroot_path;
expand_chroot_path(dir, fakechroot_path);
if (next_ftw64 == NULL) fakechroot_init();
return next_ftw64(dir, fn, nopenfd);
}
#endif
#endif
#ifdef HAVE_GET_CURRENT_DIR_NAME
/* #include <unistd.h> */
char * get_current_dir_name (void) {
char *cwd, *oldptr, *newptr;
char *fakechroot_path;
if (next_get_current_dir_name == NULL) fakechroot_init();
if ((cwd = next_get_current_dir_name()) == NULL) {
return NULL;
}
oldptr = cwd;
narrow_chroot_path(cwd, fakechroot_path);
if (cwd == NULL) {
return NULL;
}
if ((newptr = malloc(strlen(cwd)+1)) == NULL) {
free(oldptr);
return NULL;
}
strcpy(newptr, cwd);
free(oldptr);
return newptr;
}
#endif
/* #include <unistd.h> */
char * getcwd (char *buf, size_t size)
{
char *cwd;
char *fakechroot_path;
if (next_getcwd == NULL) fakechroot_init();
if ((cwd = next_getcwd(buf, size)) == NULL) {
return NULL;
}
narrow_chroot_path(cwd, fakechroot_path);
return cwd;
}
/* #include <unistd.h> */
char * getwd (char *buf)
{
char *cwd;
char *fakechroot_path;
if (next_getwd == NULL) fakechroot_init();
if ((cwd = next_getwd(buf)) == NULL) {
return NULL;
}
narrow_chroot_path(cwd, fakechroot_path);
return cwd;
}
#ifdef HAVE_GETXATTR
/* #include <sys/xattr.h> */
ssize_t getxattr (const char *path, const char *name, void *value, size_t size)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_getxattr == NULL) fakechroot_init();
return next_getxattr(path, name, value, size);
}
#endif
/* #include <glob.h> */
int glob (const char *pattern, int flags, int (*errfunc) (const char *, int), glob_t *pglob)
{
int rc;
unsigned int i;
char tmp[FAKECHROOT_MAXPATH];
char *fakechroot_path;
expand_chroot_path(pattern, fakechroot_path);
if (next_glob == NULL) fakechroot_init();
rc = next_glob(pattern, flags, errfunc, pglob);
if (rc < 0)
return rc;
for(i = 0; i < pglob->gl_pathc; i++) {
strcpy(tmp,pglob->gl_pathv[i]);
fakechroot_path = scratchbox_path(__FUNCTION__, tmp);
strcpy(pglob->gl_pathv[i], fakechroot_path);
}
return rc;
}
#ifdef HAVE_GLOB64
/* #include <glob.h> */
int glob64 (const char *pattern, int flags, int (*errfunc) (const char *, int), glob64_t *pglob)
{
int rc;
unsigned int i;
char tmp[FAKECHROOT_MAXPATH];
char *fakechroot_path;
if (next_glob64 == NULL) fakechroot_init();
expand_chroot_path(pattern, fakechroot_path);
rc = next_glob64(pattern, flags, errfunc, pglob);
if (rc < 0)
return rc;
for(i = 0; i < pglob->gl_pathc; i++) {
strcpy(tmp,pglob->gl_pathv[i]);
fakechroot_path = scratchbox_path(__FUNCTION__, tmp);
strcpy(pglob->gl_pathv[i], fakechroot_path);
}
return rc;
}
#endif
#ifdef HAVE_GLOB_PATTERN_P
/* #include <glob.h> */
int glob_pattern_p (const char *pattern, int quote)
{
char *fakechroot_path;
expand_chroot_path(pattern, fakechroot_path);
if (next_glob_pattern_p == NULL) fakechroot_init();
return next_glob_pattern_p(pattern, quote);
}
#endif
#ifdef HAVE_LCHMOD
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
int lchmod (const char *path, mode_t mode)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_lchmod == NULL) fakechroot_init();
return next_lchmod(path, mode);
}
#endif
/* #include <sys/types.h> */
/* #include <unistd.h> */
int lchown (const char *path, uid_t owner, gid_t group)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_lchown == NULL) fakechroot_init();
return next_lchown(path, owner, group);
}
#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)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_lgetxattr == NULL) fakechroot_init();
return next_lgetxattr(path, name, value, size);
}
#endif
/* #include <unistd.h> */
int link (const char *oldpath, const char *newpath)
{
char tmp[FAKECHROOT_MAXPATH];
char *fakechroot_path;
expand_chroot_path(oldpath, fakechroot_path);
strcpy(tmp, oldpath); oldpath=tmp;
expand_chroot_path(newpath, fakechroot_path);
if (next_link == NULL) fakechroot_init();
return next_link(oldpath, newpath);
}
#ifdef HAVE_LISTXATTR
/* #include <sys/xattr.h> */
ssize_t listxattr (const char *path, char *list, size_t size)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_listxattr == NULL) fakechroot_init();
return next_listxattr(path, list, size);
}
#endif
#ifdef HAVE_LLISTXATTR
/* #include <sys/xattr.h> */
ssize_t llistxattr (const char *path, char *list, size_t size)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_llistxattr == NULL) fakechroot_init();
return next_llistxattr(path, list, size);
}
#endif
#ifdef HAVE_LREMOVEXATTR
/* #include <sys/xattr.h> */
int lremovexattr (const char *path, const char *name)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_lremovexattr == NULL) fakechroot_init();
return next_lremovexattr(path, name);
}
#endif
#ifdef HAVE_LSETXATTR
/* #include <sys/xattr.h> */
int lsetxattr (const char *path, const char *name, const void *value, size_t size, int flags)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_lsetxattr == NULL) fakechroot_init();
return next_lsetxattr(path, name, value, size, flags);
}
#endif
#if !defined(HAVE___LXSTAT)
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int lstat (const char *file_name, struct stat *buf)
{
char *fakechroot_path;
expand_chroot_path(file_name, fakechroot_path);
if (next_lstat == NULL) fakechroot_init();
return next_lstat(file_name, buf);
}
#endif
#ifdef HAVE_LSTAT64
#if !defined(HAVE___LXSTAT64)
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int lstat64 (const char *file_name, struct stat64 *buf)
{
char *fakechroot_path;
expand_chroot_path(file_name, fakechroot_path);
if (next_lstat64 == NULL) fakechroot_init();
return next_lstat64(file_name, buf);
}
#endif
#endif
#ifdef HAVE_LUTIMES
/* #include <sys/time.h> */
int lutimes (const char *filename, const struct timeval tv[2])
{
char *fakechroot_path;
expand_chroot_path(filename, fakechroot_path);
if (next_lutimes == NULL) fakechroot_init();
return next_lutimes(filename, tv);
}
#endif
/* #include <sys/stat.h> */
/* #include <sys/types.h> */
int mkdir (const char *pathname, mode_t mode)
{
char *fakechroot_path;
expand_chroot_path(pathname, fakechroot_path);
if (next_mkdir == NULL) fakechroot_init();
return next_mkdir(pathname, mode);
}
#ifdef HAVE_MKDTEMP
/* #include <stdlib.h> */
char *mkdtemp (char *template)
{
char tmp[FAKECHROOT_MAXPATH], *oldtemplate, *ptr;
char *fakechroot_path;
// oldtemplate = template;
// expand_chroot_path(template, fakechroot_path);
if (next_mkdtemp == NULL) fakechroot_init();
if (next_mkdtemp(template) == NULL) {
return NULL;
}
// ptr = tmp;
// strcpy(ptr, template);
// narrow_chroot_path(ptr, fakechroot_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)
{
char *fakechroot_path;
expand_chroot_path(pathname, fakechroot_path);
if (next_mkfifo == NULL) fakechroot_init();
return next_mkfifo(pathname, mode);
}
/* #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)
{
char *fakechroot_path;
expand_chroot_path(pathname, fakechroot_path);
if (next_mknod == NULL) fakechroot_init();
return next_mknod(pathname, mode, dev);
}
/* #include <stdlib.h> */
int mkstemp (char *template)
{
char tmp[FAKECHROOT_MAXPATH], *oldtemplate, *ptr;
int fd;
char *fakechroot_path;
oldtemplate = template;
expand_chroot_path(template, fakechroot_path);
if (next_mkstemp == NULL) fakechroot_init();
if ((fd = next_mkstemp(template)) == -1) {
return -1;
}
ptr = tmp;
strcpy(ptr, template);
narrow_chroot_path(ptr, fakechroot_path);
if (ptr != NULL) {
strcpy(oldtemplate, ptr);
}
return fd;
}
/* #include <stdlib.h> */
int mkstemp64 (char *template)
{
char tmp[FAKECHROOT_MAXPATH], *oldtemplate, *ptr;
int fd;
char *fakechroot_path;
oldtemplate = template;
expand_chroot_path(template, fakechroot_path);
if (next_mkstemp64 == NULL) fakechroot_init();
if ((fd = next_mkstemp64(template)) == -1) {
return -1;
}
ptr = tmp;
strcpy(ptr, template);
narrow_chroot_path(ptr, fakechroot_path);
if (ptr != NULL) {
strcpy(oldtemplate, ptr);
}
return fd;
}
/* #include <stdlib.h> */
char *mktemp (char *template)
{
char *fakechroot_path;
expand_chroot_path(template, fakechroot_path);
if (next_mktemp == NULL) fakechroot_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)
{
char *fakechroot_path;
expand_chroot_path(dir, fakechroot_path);
if (next_nftw == NULL) fakechroot_init();
return next_nftw(dir, fn, nopenfd, flags);
}
#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)
{
char *fakechroot_path;
expand_chroot_path(dir, fakechroot_path);
if (next_nftw64 == NULL) fakechroot_init();
return next_nftw64(dir, fn, nopenfd, flags);
}
#endif
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
/* #include <fcntl.h> */
int open (const char *pathname, int flags, ...) {
int mode = 0;
char *fakechroot_path;
expand_chroot_path(pathname, fakechroot_path);
if (flags & O_CREAT) {
va_list arg;
va_start (arg, flags);
mode = va_arg (arg, int);
va_end (arg);
}
if (next_open == NULL) fakechroot_init();
return next_open(pathname, flags, mode);
}
/* #include <sys/types.h> */
/* #include <sys/stat.h> */
/* #include <fcntl.h> */
int open64 (const char *pathname, int flags, ...)
{
int mode = 0;
char *fakechroot_path;
expand_chroot_path(pathname, fakechroot_path);
if (flags & O_CREAT) {
va_list arg;
va_start (arg, flags);
mode = va_arg (arg, int);
va_end (arg);
}
if (next_open64 == NULL) fakechroot_init();
return next_open64(pathname, flags, mode);
}
#if !defined(HAVE___OPENDIR2)
/* #include <sys/types.h> */
/* #include <dirent.h> */
DIR *opendir (const char *name)
{
char *fakechroot_path;
expand_chroot_path(name, fakechroot_path);
if (next_opendir == NULL) fakechroot_init();
return next_opendir(name);
}
#endif
/* #include <unistd.h> */
long pathconf (const char *path, int name)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_pathconf == NULL) fakechroot_init();
return next_pathconf(path, name);
}
/* #include <unistd.h> */
/* XXX: add proc pid/exe wrapper from libsb to here */
int readlink (const char *path, char *buf, READLINK_TYPE_ARG3)
{
int status;
char tmp[FAKECHROOT_MAXPATH];
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_readlink == NULL) fakechroot_init();
if ((status = next_readlink(path, tmp, bufsiz)) == -1) {
return status;
}
/* TODO: shouldn't end with \000 */
tmp[status] = '\0';
strcpy(buf, tmp);
return strlen(tmp);
}
/* #include <stdlib.h> */
char *realpath (const char *name, char *resolved)
{
char *ptr;
char *fakechroot_path;
if (next_realpath == NULL) fakechroot_init();
if ((ptr = next_realpath(name, resolved)) != NULL) {
narrow_chroot_path(ptr, fakechroot_path);
}
return ptr;
}
/* #include <stdio.h> */
int remove (const char *pathname)
{
char *fakechroot_path;
expand_chroot_path(pathname, fakechroot_path);
if (next_remove == NULL) fakechroot_init();
return next_remove(pathname);
}
#ifdef HAVE_REMOVEXATTR
/* #include <sys/xattr.h> */
int removexattr (const char *path, const char *name)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_removexattr == NULL) fakechroot_init();
return next_removexattr(path, name);
}
#endif
/* #include <stdio.h> */
int rename (const char *oldpath, const char *newpath)
{
char tmp[FAKECHROOT_MAXPATH];
char *fakechroot_path;
expand_chroot_path(oldpath, fakechroot_path);
strcpy(tmp, oldpath); oldpath=tmp;
expand_chroot_path(newpath, fakechroot_path);
if (next_rename == NULL) fakechroot_init();
return next_rename(oldpath, newpath);
}
#ifdef HAVE_REVOKE
/* #include <unistd.h> */
int revoke (const char *file)
{
char *fakechroot_path;
expand_chroot_path(file, fakechroot_path);
if (next_revoke == NULL) fakechroot_init();
return next_revoke(file);
}
#endif
/* #include <unistd.h> */
int rmdir (const char *pathname)
{
char *fakechroot_path;
expand_chroot_path(pathname, fakechroot_path);
if (next_rmdir == NULL) fakechroot_init();
return next_rmdir(pathname);
}
#ifdef HAVE_SCANDIR
/* #include <dirent.h> */
int scandir (const char *dir, struct dirent ***namelist, SCANDIR_TYPE_ARG3, int(*compar)(const void *, const void *))
{
char *fakechroot_path;
expand_chroot_path(dir, fakechroot_path);
if (next_scandir == NULL) fakechroot_init();
return next_scandir(dir, namelist, filter, compar);
}
#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 *))
{
char *fakechroot_path;
expand_chroot_path(dir, fakechroot_path);
if (next_scandir64 == NULL) fakechroot_init();
return next_scandir64(dir, namelist, filter, compar);
}
#endif
#ifdef HAVE_SETXATTR
/* #include <sys/xattr.h> */
int setxattr (const char *path, const char *name, const void *value, size_t size, int flags)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_setxattr == NULL) fakechroot_init();
return next_setxattr(path, name, value, size, flags);
}
#endif
#if !defined(HAVE___XSTAT)
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int stat (const char *file_name, struct stat *buf)
{
char *fakechroot_path;
expand_chroot_path(file_name, fakechroot_path);
if (next_stat == NULL) fakechroot_init();
return next_stat(file_name, buf);
}
#endif
#ifdef HAVE_STAT64
#if !defined(HAVE___XSTAT64)
/* #include <sys/stat.h> */
/* #include <unistd.h> */
int stat64 (const char *file_name, struct stat64 *buf)
{
char *fakechroot_path;
expand_chroot_path(file_name, fakechroot_path);
if (next_stat64 == NULL) fakechroot_init();
return next_stat64(file_name, buf);
}
#endif
#endif
/* #include <unistd.h> */
int symlink (const char *oldpath, const char *newpath)
{
char tmp[FAKECHROOT_MAXPATH];
char *fakechroot_path;
expand_chroot_path(oldpath, fakechroot_path);
strcpy(tmp, oldpath); oldpath=tmp;
expand_chroot_path(newpath, fakechroot_path);
if (next_symlink == NULL) fakechroot_init();
return next_symlink(oldpath, newpath);
}
/* #include <stdio.h> */
char *tempnam (const char *dir, const char *pfx)
{
char *fakechroot_path;
expand_chroot_path(dir, fakechroot_path);
if (next_tempnam == NULL) fakechroot_init();
return next_tempnam(dir, pfx);
}
/* #include <stdio.h> */
char *tmpnam (char *s)
{
char *ptr;
char *fakechroot_path;
if (next_tmpnam == NULL) fakechroot_init();
if (s != NULL) {
return next_tmpnam(s);
}
ptr = next_tmpnam(NULL);
expand_chroot_path_malloc(ptr, fakechroot_path);
return ptr;
}
/* #include <unistd.h> */
/* #include <sys/types.h> */
int truncate (const char *path, off_t length)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_truncate == NULL) fakechroot_init();
return next_truncate(path, length);
}
#ifdef HAVE_TRUNCATE64
/* #include <unistd.h> */
/* #include <sys/types.h> */
int truncate64 (const char *path, off64_t length)
{
char *fakechroot_path;
expand_chroot_path(path, fakechroot_path);
if (next_truncate64 == NULL) fakechroot_init();
return next_truncate64(path, length);
}
#endif
#ifdef HAVE_ULCKPWDF
/* #include <shadow.h> */
int ulckpwdf (void)
{
return 0;
}
#endif
/* #include <unistd.h> */
int unlink (const char *pathname)
{
char *fakechroot_path;
expand_chroot_path(pathname, fakechroot_path);
if (next_unlink == NULL) fakechroot_init();
return next_unlink(pathname);
}
/* #include <sys/types.h> */
/* #include <utime.h> */
int utime (const char *filename, const struct utimbuf *buf)
{
char *fakechroot_path;
expand_chroot_path(filename, fakechroot_path);
if (next_utime == NULL) fakechroot_init();
return next_utime(filename, buf);
}
/* #include <sys/time.h> */
int utimes (const char *filename, const struct timeval tv[2])
{
char *fakechroot_path;
expand_chroot_path(filename, fakechroot_path);
if (next_utimes == NULL) fakechroot_init();
return next_utimes(filename, tv);
}
int uname(struct utsname *buf)
{
if (next_uname == NULL) fakechroot_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;
}
|