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
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
|
/*
* A mount(8) for Linux.
*
* Modifications by many people. Distributed under GPL.
*/
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <mntent.h>
#ifdef HAVE_LIBSELINUX
#include <selinux/selinux.h>
#include <selinux/context.h>
#endif
#include "pathnames.h"
#include "fsprobe.h"
#include "devname.h"
#include "mount_constants.h"
#include "sundries.h"
#include "xmalloc.h"
#include "mount_mntent.h"
#include "fstab.h"
#include "lomount.h"
#include "loop.h"
#include "getusername.h"
#include "env.h"
#include "nls.h"
#include "blkdev.h"
#include "strutils.h"
#define DO_PS_FIDDLING
#ifdef DO_PS_FIDDLING
#include "setproctitle.h"
#endif
/* True for fake mount (-f). */
static int fake = 0;
/* True if we are allowed to call /sbin/mount.${FSTYPE} */
static int external_allowed = 1;
/* Don't write an entry in /etc/mtab (-n). */
static int nomtab = 0;
/* True for explicit readonly (-r). */
static int readonly = 0;
/* Nonzero for chatty (-v). */
int verbose = 0;
/* Do we hash the password or not */
int hash_pass = 1;
/* Nonzero for sloppy (-s). */
static int sloppy = 0;
/* True for explicit read/write (-w). */
static int readwrite = 0;
/* True for all mount (-a). */
static int mount_all = 0;
/* True for fork() during all mount (-F). */
static int optfork = 0;
/* Add volumelabel in a listing of mounted devices (-l). */
static int list_with_volumelabel = 0;
/* Nonzero for mount {bind|move|make-shared|make-private|
* make-unbindable|make-slave}
*/
static int mounttype = 0;
/* True if (ruid != euid) or (0 != ruid), i.e. only "user" mounts permitted. */
static int restricted = 1;
/* Contains the fd to read the passphrase from, if any. */
static int pfd = -1;
/* Contains the preferred keysize in bits we want to use */
static int keysz = 0;
#ifdef HAVE_LIBMOUNT_MOUNT
static struct libmnt_update *mtab_update;
static char *mtab_opts;
static unsigned long mtab_flags;
static void prepare_mtab_entry(const char *spec, const char *node,
const char *type, const char *opts, unsigned long flags);
#endif
/* mount(2) options */
struct mountargs {
const char *spec;
const char *node;
const char *type;
int flags;
void *data;
};
/* Map from -o and fstab option strings to the flag argument to mount(2). */
struct opt_map {
const char *opt; /* option name */
int skip; /* skip in mtab option string */
int inv; /* true if flag value should be inverted */
int mask; /* flag mask value */
};
/* Custom mount options for our own purposes. */
/* Maybe these should now be freed for kernel use again */
#define MS_NOAUTO 0x80000000
#define MS_USERS 0x40000000
#define MS_USER 0x20000000
#define MS_OWNER 0x10000000
#define MS_GROUP 0x08000000
#define MS_COMMENT 0x02000000
#define MS_LOOP 0x00010000
/* Options that we keep the mount system call from seeing. */
#define MS_NOSYS (MS_NOAUTO|MS_USERS|MS_USER|MS_COMMENT|MS_LOOP)
/* Options that we keep from appearing in the options field in the mtab. */
#define MS_NOMTAB (MS_REMOUNT|MS_NOAUTO|MS_USERS|MS_USER)
#define MS_PROPAGATION (MS_SHARED|MS_SLAVE|MS_UNBINDABLE|MS_PRIVATE)
/* Options that we make ordinary users have by default. */
#define MS_SECURE (MS_NOEXEC|MS_NOSUID|MS_NODEV)
/* Options that we make owner-mounted devices have by default */
#define MS_OWNERSECURE (MS_NOSUID|MS_NODEV)
static const struct opt_map opt_map[] = {
{ "defaults", 0, 0, 0 }, /* default options */
{ "ro", 1, 0, MS_RDONLY }, /* read-only */
{ "rw", 1, 1, MS_RDONLY }, /* read-write */
{ "exec", 0, 1, MS_NOEXEC }, /* permit execution of binaries */
{ "noexec", 0, 0, MS_NOEXEC }, /* don't execute binaries */
{ "suid", 0, 1, MS_NOSUID }, /* honor suid executables */
{ "nosuid", 0, 0, MS_NOSUID }, /* don't honor suid executables */
{ "dev", 0, 1, MS_NODEV }, /* interpret device files */
{ "nodev", 0, 0, MS_NODEV }, /* don't interpret devices */
{ "sync", 0, 0, MS_SYNCHRONOUS}, /* synchronous I/O */
{ "async", 0, 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
{ "dirsync", 0, 0, MS_DIRSYNC}, /* synchronous directory modifications */
{ "remount", 0, 0, MS_REMOUNT}, /* Alter flags of mounted FS */
{ "bind", 0, 0, MS_BIND }, /* Remount part of tree elsewhere */
{ "rbind", 0, 0, MS_BIND|MS_REC }, /* Idem, plus mounted subtrees */
{ "auto", 0, 1, MS_NOAUTO }, /* Can be mounted using -a */
{ "noauto", 0, 0, MS_NOAUTO }, /* Can only be mounted explicitly */
{ "users", 0, 0, MS_USERS }, /* Allow ordinary user to mount */
{ "nousers", 0, 1, MS_USERS }, /* Forbid ordinary user to mount */
{ "user", 0, 0, MS_USER }, /* Allow ordinary user to mount */
{ "nouser", 0, 1, MS_USER }, /* Forbid ordinary user to mount */
{ "owner", 0, 0, MS_OWNER }, /* Let the owner of the device mount */
{ "noowner", 0, 1, MS_OWNER }, /* Device owner has no special privs */
{ "group", 0, 0, MS_GROUP }, /* Let the group of the device mount */
{ "nogroup", 0, 1, MS_GROUP }, /* Device group has no special privs */
{ "_netdev", 0, 0, MS_COMMENT}, /* Device requires network */
{ "comment", 0, 0, MS_COMMENT}, /* fstab comment only (kudzu,_netdev)*/
/* add new options here */
#ifdef MS_NOSUB
{ "sub", 0, 1, MS_NOSUB }, /* allow submounts */
{ "nosub", 0, 0, MS_NOSUB }, /* don't allow submounts */
#endif
#ifdef MS_SILENT
{ "quiet", 0, 0, MS_SILENT }, /* be quiet */
{ "loud", 0, 1, MS_SILENT }, /* print out messages. */
#endif
#ifdef MS_MANDLOCK
{ "mand", 0, 0, MS_MANDLOCK }, /* Allow mandatory locks on this FS */
{ "nomand", 0, 1, MS_MANDLOCK }, /* Forbid mandatory locks on this FS */
#endif
{ "loop", 1, 0, MS_LOOP }, /* use a loop device */
#ifdef MS_NOATIME
{ "atime", 0, 1, MS_NOATIME }, /* Update access time */
{ "noatime", 0, 0, MS_NOATIME }, /* Do not update access time */
#endif
#ifdef MS_I_VERSION
{ "iversion", 0, 0, MS_I_VERSION }, /* Update inode I_version time */
{ "noiversion", 0, 1, MS_I_VERSION }, /* Don't update inode I_version time */
#endif
#ifdef MS_NODIRATIME
{ "diratime", 0, 1, MS_NODIRATIME }, /* Update dir access times */
{ "nodiratime", 0, 0, MS_NODIRATIME },/* Do not update dir access times */
#endif
#ifdef MS_RELATIME
{ "relatime", 0, 0, MS_RELATIME }, /* Update access times relative to
mtime/ctime */
{ "norelatime", 0, 1, MS_RELATIME }, /* Update access time without regard
to mtime/ctime */
#endif
#ifdef MS_STRICTATIME
{ "strictatime", 0, 0, MS_STRICTATIME }, /* Strict atime semantics */
{ "nostrictatime", 0, 1, MS_STRICTATIME }, /* kernel default atime */
#endif
{ "nofail", 0, 0, MS_COMMENT}, /* Do not fail if ENOENT on dev */
{ NULL, 0, 0, 0 }
};
static int opt_nofail = 0;
static const char *opt_loopdev, *opt_vfstype, *opt_offset, *opt_sizelimit,
*opt_encryption, *opt_speed, *opt_comment, *opt_uhelper;
static const char *opt_keybits, *opt_nohashpass;
static int is_readonly(const char *node);
static int mounted (const char *spec0, const char *node0);
static int check_special_mountprog(const char *spec, const char *node,
const char *type, int flags, char *extra_opts, int *status);
static struct string_opt_map {
char *tag;
int skip;
const char **valptr;
} string_opt_map[] = {
{ "loop=", 0, &opt_loopdev },
{ "vfs=", 1, &opt_vfstype },
{ "offset=", 0, &opt_offset },
{ "sizelimit=", 0, &opt_sizelimit },
{ "encryption=", 0, &opt_encryption },
{ "keybits=", 0, &opt_keybits },
{ "nohashpass", 0, &opt_nohashpass },
{ "speed=", 0, &opt_speed },
{ "comment=", 1, &opt_comment },
{ "uhelper=", 0, &opt_uhelper },
{ NULL, 0, NULL }
};
static void
clear_string_opts(void) {
struct string_opt_map *m;
for (m = &string_opt_map[0]; m->tag; m++)
*(m->valptr) = NULL;
}
static int
parse_string_opt(char *s) {
struct string_opt_map *m;
int lth;
for (m = &string_opt_map[0]; m->tag; m++) {
lth = strlen(m->tag);
if (!strncmp(s, m->tag, lth)) {
*(m->valptr) = xstrdup(s + lth);
return 1;
}
}
return 0;
}
/* Report on a single mount. */
static void
print_one (const struct my_mntent *me) {
char *fsname = NULL;
if (mount_quiet)
return;
/* users assume backing file name rather than /dev/loopN in
* mount(8) output if the device has been initialized by mount(8).
*/
if (strncmp(me->mnt_fsname, "/dev/loop", 9) == 0 &&
is_loop_autoclear(me->mnt_fsname))
fsname = loopdev_get_loopfile(me->mnt_fsname);
if (!fsname)
fsname = (char *) me->mnt_fsname;
printf ("%s on %s", fsname, me->mnt_dir);
if (me->mnt_type != NULL && *(me->mnt_type) != '\0')
printf (" type %s", me->mnt_type);
if (me->mnt_opts != NULL)
printf (" (%s)", me->mnt_opts);
if (list_with_volumelabel && is_pseudo_fs(me->mnt_type) == 0) {
const char *devname = spec_to_devname(me->mnt_fsname);
if (devname) {
const char *label;
label = fsprobe_get_label_by_devname(devname);
my_free(devname);
if (label) {
printf (" [%s]", label);
my_free(label);
}
}
}
printf ("\n");
}
/* Report on everything in mtab (of the specified types if any). */
static int
print_all (char *types) {
struct mntentchn *mc, *mc0;
mc0 = mtab_head();
for (mc = mc0->nxt; mc && mc != mc0; mc = mc->nxt) {
if (matching_type (mc->m.mnt_type, types))
print_one (&(mc->m));
}
if (!mtab_does_not_exist() && !mtab_is_a_symlink() && is_readonly(_PATH_MOUNTED))
printf(_("\n"
"mount: warning: /etc/mtab is not writable (e.g. read-only filesystem).\n"
" It's possible that information reported by mount(8) is not\n"
" up to date. For actual information about system mount points\n"
" check the /proc/mounts file.\n\n"));
exit (0);
}
/* reallocates its first arg */
static char *
append_opt(char *s, const char *opt, const char *val)
{
if (!opt)
return s;
if (!s) {
if (!val)
return xstrdup(opt); /* opt */
return xstrconcat3(NULL, opt, val); /* opt=val */
}
if (!val)
return xstrconcat3(s, ",", opt); /* s,opt */
return xstrconcat4(s, ",", opt, val); /* s,opt=val */
}
static char *
append_numopt(char *s, const char *opt, unsigned int num)
{
char buf[32];
snprintf(buf, sizeof(buf), "%u", num);
return append_opt(s, opt, buf);
}
#ifdef HAVE_LIBSELINUX
/* strip quotes from a "string"
* Warning: This function modify the "str" argument.
*/
static char *
strip_quotes(char *str)
{
char *end = NULL;
if (*str != '"')
return str;
end = strrchr(str, '"');
if (end == NULL || end == str)
die (EX_USAGE, _("mount: improperly quoted option string '%s'"), str);
*end = '\0';
return str+1;
}
/* translates SELinux context from human to raw format and
* appends it to the mount extra options.
*
* returns -1 on error and 0 on success
*/
static int
append_context(const char *optname, char *optdata, char **extra_opts)
{
security_context_t raw = NULL;
char *data = NULL;
if (is_selinux_enabled() != 1)
/* ignore the option if we running without selinux */
return 0;
if (optdata==NULL || *optdata=='\0' || optname==NULL)
return -1;
/* TODO: use strip_quotes() for all mount options? */
data = *optdata =='"' ? strip_quotes(optdata) : optdata;
if (selinux_trans_to_raw_context(
(security_context_t) data, &raw) == -1 ||
raw == NULL)
return -1;
if (verbose)
printf(_("mount: translated %s '%s' to '%s'\n"),
optname, data, (char *) raw);
*extra_opts = append_opt(*extra_opts, optname, NULL);
*extra_opts = xstrconcat4(*extra_opts, "\"", (char *) raw, "\"");
freecon(raw);
return 0;
}
/* returns newly allocated string without *context= options */
static char *remove_context_options(char *opts)
{
char *begin = NULL, *end = NULL, *p;
int open_quote = 0, changed = 0;
if (!opts)
return NULL;
opts = xstrdup(opts);
for (p = opts; p && *p; p++) {
if (!begin)
begin = p; /* begin of the option item */
if (*p == '"')
open_quote ^= 1; /* reverse the status */
if (open_quote)
continue; /* still in quoted block */
if (*p == ',')
end = p; /* terminate the option item */
else if (*(p + 1) == '\0')
end = p + 1; /* end of optstr */
if (!begin || !end)
continue;
if (strncmp(begin, "context=", 8) == 0 ||
strncmp(begin, "fscontext=", 10) == 0 ||
strncmp(begin, "defcontext=", 11) == 0 ||
strncmp(begin, "rootcontext=", 12) == 0) {
size_t sz;
if ((begin == opts || *(begin - 1) == ',') && *end == ',')
end++;
sz = strlen(end);
memmove(begin, end, sz + 1);
if (!*begin && *(begin - 1) == ',')
*(begin - 1) = '\0';
p = begin;
changed = 1;
}
}
if (changed && verbose)
printf (_("mount: SELinux *context= options are ignore on remount.\n"));
return opts;
}
#endif
/*
* Look for OPT in opt_map table and return mask value.
* If OPT isn't found, tack it onto extra_opts (which is non-NULL).
* For the options uid= and gid= replace user or group name by its value.
*/
static inline void
parse_opt(char *opt, int *mask, char **extra_opts) {
const struct opt_map *om;
for (om = opt_map; om->opt != NULL; om++)
if (streq (opt, om->opt)) {
if (om->inv)
*mask &= ~om->mask;
else
*mask |= om->mask;
if ((om->mask == MS_USER || om->mask == MS_USERS)
&& !om->inv)
*mask |= MS_SECURE;
if ((om->mask == MS_OWNER || om->mask == MS_GROUP)
&& !om->inv)
*mask |= MS_OWNERSECURE;
#ifdef MS_SILENT
if (om->mask == MS_SILENT && om->inv) {
mount_quiet = 1;
verbose = 0;
}
#endif
if (streq(opt, "nofail"))
opt_nofail = 1;
return;
}
/* convert nonnumeric ids to numeric */
if (!strncmp(opt, "uid=", 4) && !isdigit(opt[4])) {
struct passwd *pw = getpwnam(opt+4);
if (pw) {
*extra_opts = append_numopt(*extra_opts,
"uid=", pw->pw_uid);
return;
}
}
if (!strncmp(opt, "gid=", 4) && !isdigit(opt[4])) {
struct group *gr = getgrnam(opt+4);
if (gr) {
*extra_opts = append_numopt(*extra_opts,
"gid=", gr->gr_gid);
return;
}
}
#ifdef HAVE_LIBSELINUX
if (strncmp(opt, "context=", 8) == 0 && *(opt+8)) {
if (append_context("context=", opt+8, extra_opts) == 0)
return;
}
if (strncmp(opt, "fscontext=", 10) == 0 && *(opt+10)) {
if (append_context("fscontext=", opt+10, extra_opts) == 0)
return;
}
if (strncmp(opt, "defcontext=", 11) == 0 && *(opt+11)) {
if (append_context("defcontext=", opt+11, extra_opts) == 0)
return;
}
if (strncmp(opt, "rootcontext=", 12) == 0 && *(opt+12)) {
if (append_context("rootcontext=", opt+12, extra_opts) == 0)
return;
}
#endif
*extra_opts = append_opt(*extra_opts, opt, NULL);
}
/* Take -o options list and compute 4th and 5th args to mount(2). flags
gets the standard options (indicated by bits) and extra_opts all the rest */
static void
parse_opts (const char *options, int *flags, char **extra_opts) {
*flags = 0;
*extra_opts = NULL;
clear_string_opts();
if (options != NULL) {
char *opts = xstrdup(options);
int open_quote = 0;
char *opt, *p;
for (p=opts, opt=NULL; p && *p; p++) {
if (!opt)
opt = p; /* begin of the option item */
if (*p == '"')
open_quote ^= 1; /* reverse the status */
if (open_quote)
continue; /* still in quoted block */
if (*p == ',')
*p = '\0'; /* terminate the option item */
/* end of option item or last item */
if (*p == '\0' || *(p+1) == '\0') {
if (!parse_string_opt(opt))
parse_opt(opt, flags, extra_opts);
opt = NULL;
}
}
free(opts);
}
if (readonly)
*flags |= MS_RDONLY;
if (readwrite)
*flags &= ~MS_RDONLY;
*flags |= mounttype;
/* The propagation flags should not be used together with any other flags */
if (*flags & MS_PROPAGATION)
*flags &= MS_PROPAGATION;
}
/* Try to build a canonical options string. */
static char *
fix_opts_string (int flags, const char *extra_opts, const char *user) {
const struct opt_map *om;
const struct string_opt_map *m;
char *new_opts;
new_opts = append_opt(NULL, (flags & MS_RDONLY) ? "ro" : "rw", NULL);
for (om = opt_map; om->opt != NULL; om++) {
if (om->skip)
continue;
if (om->inv || !om->mask || (flags & om->mask) != om->mask)
continue;
new_opts = append_opt(new_opts, om->opt, NULL);
flags &= ~om->mask;
}
for (m = &string_opt_map[0]; m->tag; m++) {
if (!m->skip && *(m->valptr))
new_opts = append_opt(new_opts, m->tag, *(m->valptr));
}
if (extra_opts && *extra_opts)
new_opts = append_opt(new_opts, extra_opts, NULL);
if (user)
new_opts = append_opt(new_opts, "user=", user);
return new_opts;
}
static int
already (const char *spec0, const char *node0) {
struct mntentchn *mc;
int ret = 1;
char *spec = canonicalize_spec(spec0);
char *node = canonicalize(node0);
if ((mc = getmntfile(node)) != NULL)
error (_("mount: according to mtab, "
"%s is already mounted on %s"),
mc->m.mnt_fsname, node);
else if (spec && strcmp (spec, "none") &&
(mc = getmntfile(spec)) != NULL)
error (_("mount: according to mtab, %s is mounted on %s"),
spec, mc->m.mnt_dir);
else
ret = 0;
free(spec);
free(node);
return ret;
}
/* Create mtab with a root entry. */
static void
create_mtab (void) {
struct mntentchn *fstab;
struct my_mntent mnt;
int flags;
mntFILE *mfp;
lock_mtab();
mfp = my_setmntent (_PATH_MOUNTED, "a+");
if (mfp == NULL || mfp->mntent_fp == NULL) {
int errsv = errno;
die (EX_FILEIO, _("mount: can't open %s for writing: %s"),
_PATH_MOUNTED, strerror (errsv));
}
/* Find the root entry by looking it up in fstab */
if ((fstab = getfs_by_dir ("/")) || (fstab = getfs_by_dir ("root"))) {
char *extra_opts;
parse_opts (fstab->m.mnt_opts, &flags, &extra_opts);
mnt.mnt_dir = "/";
mnt.mnt_fsname = spec_to_devname(fstab->m.mnt_fsname);
mnt.mnt_type = fstab->m.mnt_type;
mnt.mnt_opts = fix_opts_string (flags, extra_opts, NULL);
mnt.mnt_freq = mnt.mnt_passno = 0;
free(extra_opts);
if (mnt.mnt_fsname && my_addmntent (mfp, &mnt) == 1) {
int errsv = errno;
die (EX_FILEIO, _("mount: error writing %s: %s"),
_PATH_MOUNTED, strerror (errsv));
}
}
if (fchmod (fileno (mfp->mntent_fp), 0644) < 0)
if (errno != EROFS) {
int errsv = errno;
die (EX_FILEIO,
_("mount: error changing mode of %s: %s"),
_PATH_MOUNTED, strerror (errsv));
}
my_endmntent (mfp);
unlock_mtab();
reset_mtab_info();
}
/* count successful mount system calls */
static int mountcount = 0;
/*
* do_mount_syscall()
* Mount a single file system. Keep track of successes.
* returns: 0: OK, -1: error in errno
*/
static int
do_mount_syscall (struct mountargs *args) {
int flags = args->flags;
if ((flags & MS_MGC_MSK) == 0)
flags |= MS_MGC_VAL;
if (verbose > 2)
printf("mount: mount(2) syscall: source: \"%s\", target: \"%s\", "
"filesystemtype: \"%s\", mountflags: %d, data: %s\n",
args->spec, args->node, args->type, flags, (char *) args->data);
return mount (args->spec, args->node, args->type, flags, args->data);
}
/*
* do_mount()
* Mount a single file system, possibly invoking an external handler to
* do so. Keep track of successes.
* returns: 0: OK, -1: error in errno
*/
static int
do_mount (struct mountargs *args, int *special, int *status) {
int ret;
if (check_special_mountprog(args->spec, args->node, args->type,
args->flags, args->data, status)) {
*special = 1;
ret = 0;
} else {
#ifdef HAVE_LIBMOUNT_MOUNT
prepare_mtab_entry(args->spec, args->node, args->type,
mtab_opts, mtab_flags);
#endif
ret = do_mount_syscall(args);
}
if (ret == 0)
mountcount++;
return ret;
}
/*
* check_special_mountprog()
* If there is a special mount program for this type, exec it.
* returns: 0: no exec was done, 1: exec was done, status has result
*/
static int
check_special_mountprog(const char *spec, const char *node, const char *type, int flags,
char *extra_opts, int *status) {
char search_path[] = FS_SEARCH_PATH;
char *path, mountprog[150];
struct stat statbuf;
int res;
if (!external_allowed)
return 0;
if (type == NULL || strcmp(type, "none") == 0)
return 0;
path = strtok(search_path, ":");
while (path) {
int type_opt = 0;
res = snprintf(mountprog, sizeof(mountprog), "%s/mount.%s",
path, type);
path = strtok(NULL, ":");
if (res >= sizeof(mountprog) || res < 0)
continue;
res = stat(mountprog, &statbuf);
if (res == -1 && errno == ENOENT && strchr(type, '.')) {
/* If type ends with ".subtype" try without it */
*strrchr(mountprog, '.') = '\0';
type_opt = 1;
res = stat(mountprog, &statbuf);
}
if (res)
continue;
if (verbose)
fflush(stdout);
switch (fork()) {
case 0: { /* child */
char *oo, *mountargs[12];
int i = 0;
if (setgid(getgid()) < 0)
die(EX_FAIL, _("mount: cannot set group id: %s"), strerror(errno));
if (setuid(getuid()) < 0)
die(EX_FAIL, _("mount: cannot set user id: %s"), strerror(errno));
oo = fix_opts_string (flags, extra_opts, NULL);
mountargs[i++] = mountprog; /* 1 */
mountargs[i++] = (char *) spec; /* 2 */
mountargs[i++] = (char *) node; /* 3 */
if (sloppy && strncmp(type, "nfs", 3) == 0)
mountargs[i++] = "-s"; /* 4 */
if (fake)
mountargs[i++] = "-f"; /* 5 */
if (nomtab)
mountargs[i++] = "-n"; /* 6 */
if (verbose)
mountargs[i++] = "-v"; /* 7 */
if (oo && *oo) {
mountargs[i++] = "-o"; /* 8 */
mountargs[i++] = oo; /* 9 */
}
if (type_opt) {
mountargs[i++] = "-t"; /* 10 */
mountargs[i++] = (char *) type; /* 11 */
}
mountargs[i] = NULL; /* 12 */
if (verbose > 2) {
i = 0;
while (mountargs[i]) {
printf("mount: external mount: argv[%d] = \"%s\"\n",
i, mountargs[i]);
i++;
}
fflush(stdout);
}
execv(mountprog, mountargs);
exit(1); /* exec failed */
}
default: { /* parent */
int st;
wait(&st);
*status = (WIFEXITED(st) ? WEXITSTATUS(st) : EX_SYSERR);
return 1;
}
case -1: { /* error */
int errsv = errno;
error(_("mount: cannot fork: %s"), strerror(errsv));
}
}
}
return 0;
}
/* list of already tested filesystems by procfsloop_mount() */
static struct tried {
struct tried *next;
char *type;
} *tried = NULL;
static int
was_tested(const char *fstype) {
struct tried *t;
for (t = tried; t; t = t->next) {
if (!strcmp(t->type, fstype))
return 1;
}
return 0;
}
static void
set_tested(const char *fstype) {
struct tried *t = xmalloc(sizeof(struct tried));
t->next = tried;
t->type = xstrdup(fstype);
tried = t;
}
static void
free_tested(void) {
struct tried *t, *tt;
t = tried;
while(t) {
free(t->type);
tt = t->next;
free(t);
t = tt;
}
tried = NULL;
}
static char *
procfsnext(FILE *procfs) {
char line[100];
char fsname[100];
while (fgets(line, sizeof(line), procfs)) {
if (sscanf (line, "nodev %[^#\n]\n", fsname) == 1) continue;
if (sscanf (line, " %[^# \n]\n", fsname) != 1) continue;
return xstrdup(fsname);
}
return 0;
}
/* Only use /proc/filesystems here, this is meant to test what
the kernel knows about, so /etc/filesystems is irrelevant.
Return: 1: yes, 0: no, -1: cannot open procfs */
static int
known_fstype_in_procfs(const char *type)
{
FILE *procfs;
char *fsname;
int ret = -1;
procfs = fopen(_PATH_PROC_FILESYSTEMS, "r");
if (procfs) {
ret = 0;
while ((fsname = procfsnext(procfs)) != NULL)
if (!strcmp(fsname, type)) {
ret = 1;
break;
}
fclose(procfs);
procfs = NULL;
}
return ret;
}
/* Try all types in FILESYSTEMS, except those in *types,
in case *types starts with "no" */
/* return: 0: OK, -1: error in errno, 1: type not found */
/* when 0 or -1 is returned, *types contains the type used */
/* when 1 is returned, *types is NULL */
static int
procfsloop_mount(int (*mount_fn)(struct mountargs *, int *, int *),
struct mountargs *args,
const char **types,
int *special, int *status)
{
char *files[2] = { _PATH_FILESYSTEMS, _PATH_PROC_FILESYSTEMS };
FILE *procfs;
char *fsname;
const char *notypes = NULL;
int no = 0;
int ret = 1;
int errsv = 0;
int i;
if (*types && !strncmp(*types, "no", 2)) {
no = 1;
notypes = (*types) + 2;
}
*types = NULL;
/* Use _PATH_PROC_FILESYSTEMS only when _PATH_FILESYSTEMS
* (/etc/filesystems) does not exist. In some cases trying a
* filesystem that the kernel knows about on the wrong data will crash
* the kernel; in such cases _PATH_FILESYSTEMS can be used to list the
* filesystems that we are allowed to try, and in the order they should
* be tried. End _PATH_FILESYSTEMS with a line containing a single '*'
* only, if _PATH_PROC_FILESYSTEMS should be tried afterwards.
*/
for (i=0; i<2; i++) {
procfs = fopen(files[i], "r");
if (!procfs)
continue;
while ((fsname = procfsnext(procfs)) != NULL) {
if (!strcmp(fsname, "*")) {
fclose(procfs);
goto nexti;
}
if (was_tested (fsname))
continue;
if (no && matching_type(fsname, notypes))
continue;
set_tested (fsname);
args->type = fsname;
if (verbose)
printf(_("Trying %s\n"), fsname);
if ((*mount_fn) (args, special, status) == 0) {
*types = fsname;
ret = 0;
break;
} else if (errno != EINVAL &&
known_fstype_in_procfs(fsname) == 1) {
*types = "guess";
ret = -1;
errsv = errno;
break;
}
}
free_tested();
fclose(procfs);
errno = errsv;
return ret;
nexti:;
}
return 1;
}
static const char *
guess_fstype_by_devname(const char *devname, int *ambivalent)
{
const char *type = fsprobe_get_fstype_by_devname_ambi(devname, ambivalent);
if (verbose) {
printf (_("mount: you didn't specify a filesystem type for %s\n"), devname);
if (!type)
printf (_(" I will try all types mentioned in %s or %s\n"),
_PATH_FILESYSTEMS, _PATH_PROC_FILESYSTEMS);
else if (!strcmp(type, MNTTYPE_SWAP))
printf (_(" and it looks like this is swapspace\n"));
else
printf (_(" I will try type %s\n"), type);
}
return type;
}
/*
* guess_fstype_and_mount()
* Mount a single file system. Guess the type when unknown.
* returns: 0: OK, -1: error in errno, 1: other error
* don't exit on non-fatal errors.
* on return types is filled with the type used.
*/
static int
guess_fstype_and_mount(const char *spec, const char *node, const char **types,
int flags, char *mount_opts, int *special, int *status) {
struct mountargs args = { spec, node, NULL, flags & ~MS_NOSYS, mount_opts };
int ambivalent = 0;
if (*types && strcasecmp (*types, "auto") == 0)
*types = NULL;
if (!*types && !(flags & MS_REMOUNT)) {
*types = guess_fstype_by_devname(spec, &ambivalent);
if (*types) {
if (!strcmp(*types, MNTTYPE_SWAP)) {
error(_("%s looks like swapspace - not mounted"), spec);
*types = NULL;
return 1;
} else {
args.type = *types;
return do_mount (&args, special, status);
}
} else if (ambivalent) {
error(_("mount: %s: more filesystems detected. This should not happen,\n"
" use -t <type> to explicitly specify the filesystem type or\n"
" use wipefs(8) to clean up the device.\n"), spec);
return 1;
}
}
/* Accept a comma-separated list of types, and try them one by one */
/* A list like "nonfs,.." indicates types not to use */
if (*types && strncmp(*types, "no", 2) && strchr(*types,',')) {
char *t = strdup(*types);
char *p;
while((p = strchr(t,',')) != NULL) {
*p = 0;
args.type = *types = t;
if (do_mount (&args, special, status) == 0)
return 0;
t = p+1;
}
/* do last type below */
*types = t;
}
if (*types || (flags & MS_REMOUNT)) {
args.type = *types;
return do_mount (&args, special, status);
}
return procfsloop_mount(do_mount, &args, types, special, status);
}
/*
* restricted_check()
* Die if the user is not allowed to do this.
*/
static void
restricted_check(const char *spec, const char *node, int *flags, char **user) {
if (restricted) {
/*
* MS_OWNER: Allow owners to mount when fstab contains
* the owner option. Note that this should never be used
* in a high security environment, but may be useful to give
* people at the console the possibility of mounting a floppy.
* MS_GROUP: Allow members of device group to mount. (Martin Dickopp)
*/
if (*flags & (MS_OWNER | MS_GROUP)) {
struct stat sb;
if (!strncmp(spec, "/dev/", 5) && stat(spec, &sb) == 0) {
if (*flags & MS_OWNER) {
if (getuid() == sb.st_uid)
*flags |= MS_USER;
}
if (*flags & MS_GROUP) {
if (getgid() == sb.st_gid)
*flags |= MS_USER;
else {
int n = getgroups(0, NULL);
if (n > 0) {
gid_t *groups = xmalloc(n * sizeof(*groups));
if (getgroups(n, groups) == n) {
int i;
for (i = 0; i < n; i++) {
if (groups[i] == sb.st_gid) {
*flags |= MS_USER;
break;
}
}
}
free(groups);
}
}
}
}
}
/* James Kehl <mkehl@gil.com.au> came with a similar patch:
allow an arbitrary user to mount when he is the owner of
the mount-point and has write-access to the device.
This is even less secure. Let me skip it for the time being;
there should be an explicit fstab line allowing such things. */
if (!(*flags & (MS_USER | MS_USERS))) {
if (already (spec, node))
die (EX_USAGE, _("mount failed"));
else
die (EX_USAGE, _("mount: only root can mount %s on %s"), spec, node);
}
if (*flags & MS_USER)
*user = getusername();
}
*flags &= ~(MS_OWNER | MS_GROUP);
}
/* Check, if there already exists a mounted loop device on the mountpoint node
* with the same parameters.
*/
static int
is_mounted_same_loopfile(const char *node0, const char *loopfile, unsigned long long offset)
{
struct mntentchn *mnt = NULL;
char *node;
int res = 0;
node = canonicalize(node0);
/* Search for mountpoint node in mtab,
* procceed if any of these has the loop option set or
* the device is a loop device
*/
mnt = getmntdirbackward(node, mnt);
if (!mnt) {
free(node);
return 0;
}
for(; mnt && res == 0; mnt = getmntdirbackward(node, mnt)) {
char *p;
if (strncmp(mnt->m.mnt_fsname, "/dev/loop", 9) == 0)
res = loopfile_used_with((char *) mnt->m.mnt_fsname,
loopfile, offset);
else if ((p = strstr(mnt->m.mnt_opts, "loop="))) {
char *dev = xstrdup(p+5);
if ((p = strchr(dev, ',')))
*p = '\0';
res = loopfile_used_with(dev, loopfile, offset);
free(dev);
}
}
free(node);
return res;
}
static int
parse_offset(const char **opt, uintmax_t *val)
{
char *tmp;
if (strtosize(*opt, val))
return -1;
tmp = xmalloc(32);
snprintf(tmp, 32, "%jd", *val);
my_free(*opt);
*opt = tmp;
return 0;
}
static int
loop_check(const char **spec, const char **type, int *flags,
int *loop, const char **loopdev, const char **loopfile,
const char *node) {
int looptype;
uintmax_t offset = 0, sizelimit = 0;
/*
* In the case of a loop mount, either type is of the form lo@/dev/loop5
* or the option "-o loop=/dev/loop5" or just "-o loop" is given, or
* mount just has to figure things out for itself from the fact that
* spec is not a block device. We do not test for a block device
* immediately: maybe later other types of mountable objects will occur.
*/
*loopdev = opt_loopdev;
looptype = (*type && strncmp("lo@", *type, 3) == 0);
if (looptype) {
if (*loopdev)
error(_("mount: loop device specified twice"));
*loopdev = *type + 3;
*type = opt_vfstype;
} else if (opt_vfstype) {
if (*type)
error(_("mount: type specified twice"));
else
*type = opt_vfstype;
}
*loop = ((*flags & MS_LOOP) || *loopdev || opt_offset || opt_sizelimit || opt_encryption || opt_keybits);
*loopfile = *spec;
/* Automatically create a loop device from a regular file if a filesystem
* is not specified or the filesystem is known for libblkid (these
* filesystems work with block devices only).
*
* Note that there is not a restriction (on kernel side) that prevents regular
* file as a mount(2) source argument. A filesystem that is able to mount
* regular files could be implemented.
*/
if (!*loop && !(*flags & (MS_BIND | MS_MOVE | MS_PROPAGATION)) &&
(!*type || strcmp(*type, "auto") == 0 || fsprobe_known_fstype(*type))) {
struct stat st;
if (stat(*loopfile, &st) == 0)
*loop = S_ISREG(st.st_mode);
}
if (*loop) {
*flags |= MS_LOOP;
if (fake) {
if (verbose)
printf(_("mount: skipping the setup of a loop device\n"));
} else {
int loop_opts = 0;
int res;
/* since 2.6.37 we don't have to store backing filename to mtab
* because kernel provides the name in /sys
*/
if (get_linux_version() >= KERNEL_VERSION(2, 6, 37) ||
mtab_is_writable() == 0) {
if (verbose)
printf(_("mount: enabling autoclear loopdev flag\n"));
loop_opts = SETLOOP_AUTOCLEAR;
}
if (*flags & MS_RDONLY)
loop_opts |= SETLOOP_RDONLY;
if (opt_offset && parse_offset(&opt_offset, &offset)) {
error(_("mount: invalid offset '%s' specified"), opt_offset);
return EX_FAIL;
}
if (opt_sizelimit && parse_offset(&opt_sizelimit, &sizelimit)) {
error(_("mount: invalid sizelimit '%s' specified"), opt_sizelimit);
return EX_FAIL;
}
if (is_mounted_same_loopfile(node, *loopfile, offset)) {
error(_("mount: according to mtab %s is already mounted on %s as loop"), *loopfile, node);
return EX_FAIL;
}
do {
if (!*loopdev || !**loopdev)
*loopdev = find_unused_loop_device();
if (!*loopdev)
return EX_SYSERR; /* no more loop devices */
if (verbose)
printf(_("mount: going to use the loop device %s\n"), *loopdev);
if (!keysz && opt_keybits)
keysz = strtoul(opt_keybits, NULL, 0);
if (opt_nohashpass)
hash_pass=0;
if ((res = set_loop(*loopdev, *loopfile, offset, sizelimit,
opt_encryption, pfd, &loop_opts, keysz, hash_pass))) {
if (res == 2) {
/* loop dev has been grabbed by some other process,
try again, if not given explicitly */
if (!opt_loopdev) {
if (verbose)
printf(_("mount: stolen loop=%s ...trying again\n"), *loopdev);
my_free(*loopdev);
*loopdev = NULL;
continue;
}
error(_("mount: stolen loop=%s"), *loopdev);
return EX_FAIL;
} else {
if (verbose)
printf(_("mount: failed setting up loop device\n"));
if (!opt_loopdev) {
my_free(*loopdev);
*loopdev = NULL;
}
return EX_FAIL;
}
}
} while (!*loopdev);
if (verbose > 1)
printf(_("mount: setup loop device successfully\n"));
*spec = *loopdev;
if (loop_opts & SETLOOP_RDONLY)
*flags |= MS_RDONLY;
if (loop_opts & SETLOOP_AUTOCLEAR)
/* Prevent recording loop dev in mtab for cleanup on umount */
*loop = 0;
}
}
return 0;
}
#ifdef HAVE_LIBMOUNT_MOUNT
static void
verbose_mount_info(const char *spec, const char *node, const char *type,
const char *opts, int flags)
{
struct my_mntent mnt;
mnt.mnt_fsname = is_pseudo_fs(type) ? spec : canonicalize(spec);
mnt.mnt_dir = canonicalize (node);
mnt.mnt_type = type;
mnt.mnt_opts = opts;
print_one (&mnt);
if (spec != mnt.mnt_fsname)
my_free(mnt.mnt_fsname);
my_free(mnt.mnt_dir);
}
static void
prepare_mtab_entry(const char *spec, const char *node, const char *type,
const char *opts, unsigned long flags)
{
struct libmnt_fs *fs = mnt_new_fs();
int rc = -1;
if (!mtab_update)
mtab_update = mnt_new_update();
if (mtab_update && fs) {
const char *cn_spec = is_pseudo_fs(type) ? spec : canonicalize(spec);
const char *cn_node = canonicalize(node);
mnt_fs_set_source(fs, cn_spec);
mnt_fs_set_target(fs, cn_node);
mnt_fs_set_fstype(fs, type);
mnt_fs_set_options(fs, opts);
rc = mnt_update_set_fs(mtab_update, flags, NULL, fs);
if (spec != cn_spec)
my_free(cn_spec);
my_free(cn_node);
}
mnt_free_fs(fs);
if (rc) {
mnt_free_update(mtab_update);
mtab_update = NULL;
}
}
static void update_mtab_entry(int flags)
{
unsigned long fl;
struct libmnt_lock *lc;
if (!mtab_update)
return;
fl = mnt_update_get_mflags(mtab_update);
if ((flags & MS_RDONLY) != (fl & MS_RDONLY))
mnt_update_force_rdonly(mtab_update, flags & MS_RDONLY);
if (!nomtab) {
if (mtab_does_not_exist()) {
if (verbose > 1)
printf(_("mount: no %s found - creating it..\n"),
_PATH_MOUNTED);
create_mtab ();
}
lc = init_libmount_lock( mnt_update_get_filename(mtab_update) );
mnt_update_table(mtab_update, lc);
init_libmount_lock(NULL);
}
mnt_free_update(mtab_update);
mtab_update = NULL;
}
#else /*!HAVE_LIBMOUNT_MOUNT */
static void
update_mtab_entry(const char *spec, const char *node, const char *type,
const char *opts, int flags, int freq, int pass) {
struct my_mntent mnt;
mnt.mnt_fsname = is_pseudo_fs(type) ? xstrdup(spec) : canonicalize(spec);
mnt.mnt_dir = canonicalize (node);
mnt.mnt_type = type;
mnt.mnt_opts = opts;
mnt.mnt_freq = freq;
mnt.mnt_passno = pass;
/* We get chatty now rather than after the update to mtab since the
mount succeeded, even if the write to /etc/mtab should fail. */
if (verbose)
print_one (&mnt);
if (!nomtab && mtab_does_not_exist()) {
if (verbose > 1)
printf(_("mount: no %s found - creating it..\n"),
_PATH_MOUNTED);
create_mtab ();
}
if (!nomtab && mtab_is_writable()) {
if (flags & MS_REMOUNT)
update_mtab (mnt.mnt_dir, &mnt);
else if (flags & MS_MOVE)
update_mtab(mnt.mnt_fsname, &mnt);
else {
mntFILE *mfp;
lock_mtab();
mfp = my_setmntent(_PATH_MOUNTED, "a+");
if (mfp == NULL || mfp->mntent_fp == NULL) {
int errsv = errno;
error(_("mount: can't open %s: %s"), _PATH_MOUNTED,
strerror (errsv));
} else {
if ((my_addmntent (mfp, &mnt)) == 1) {
int errsv = errno;
error(_("mount: error writing %s: %s"),
_PATH_MOUNTED, strerror (errsv));
}
}
my_endmntent(mfp);
unlock_mtab();
}
}
my_free(mnt.mnt_fsname);
my_free(mnt.mnt_dir);
}
#endif /* !HAVE_LIBMOUNT_MOUNT */
static void
set_pfd(char *s) {
if (!isdigit(*s))
die(EX_USAGE,
_("mount: argument to -p or --pass-fd must be a number"));
pfd = atoi(optarg);
}
static void
cdrom_setspeed(const char *spec) {
#define CDROM_SELECT_SPEED 0x5322 /* Set the CD-ROM speed */
if (opt_speed) {
int cdrom;
int speed = atoi(opt_speed);
if ((cdrom = open(spec, O_RDONLY | O_NONBLOCK)) < 0)
die(EX_FAIL,
_("mount: cannot open %s for setting speed"),
spec);
if (ioctl(cdrom, CDROM_SELECT_SPEED, speed) < 0)
die(EX_FAIL, _("mount: cannot set speed: %s"),
strerror(errno));
close(cdrom);
}
}
/*
* Check if @path is on read-only filesystem independently on file permissions.
*/
static int
is_readonly(const char *path)
{
if (access(path, W_OK) == 0)
return 0;
if (errno == EROFS)
return 1;
if (errno != EACCES)
return 0;
#ifdef HAVE_FUTIMENS
/*
* access(2) returns EACCES on read-only FS:
*
* - for set-uid application if one component of the path is not
* accessible for the current rUID. (Note that euidaccess(2) does not
* check for EROFS at all).
*
* - for read-write filesystem with read-only VFS node (aka -o remount,ro,bind)
*/
{
struct timespec times[2];
times[0].tv_nsec = UTIME_NOW; /* atime */
times[1].tv_nsec = UTIME_OMIT; /* mtime */
if (utimensat(AT_FDCWD, path, times, 0) == -1)
return errno == EROFS;
}
#endif
return 0;
}
/*
* try_mount_one()
* Try to mount one file system.
*
* returns: 0: OK, EX_SYSERR, EX_FAIL, return code from nfsmount,
* return status from wait
*/
static int
try_mount_one (const char *spec0, const char *node0, const char *types0,
const char *opts0, int freq, int pass, int ro) {
int res = 0, status = 0, special = 0;
int mnt5_res = 0; /* only for gcc */
int mnt_err;
int flags;
char *extra_opts; /* written in mtab */
char *mount_opts; /* actually used on system call */
const char *opts, *spec, *node, *types;
char *user = 0;
int loop = 0;
const char *loopdev = 0, *loopfile = 0;
struct stat statbuf;
/* copies for freeing on exit */
const char *opts1, *spec1, *node1, *types1, *extra_opts1;
if (verbose > 2) {
printf("mount: spec: \"%s\"\n", spec0);
printf("mount: node: \"%s\"\n", node0);
printf("mount: types: \"%s\"\n", types0);
printf("mount: opts: \"%s\"\n", opts0);
}
spec = spec1 = xstrdup(spec0);
node = node1 = xstrdup(node0);
types = types1 = xstrdup(types0);
opts = opts1 = xstrdup(opts0);
parse_opts (opts, &flags, &extra_opts);
extra_opts1 = extra_opts;
mount_opts = extra_opts;
/* quietly succeed for fstab entries that don't get mounted automatically */
if (mount_all && (flags & MS_NOAUTO))
goto out;
restricted_check(spec, node, &flags, &user);
/* The "mount -f" checks for for existing record in /etc/mtab (with
* regular non-fake mount this is usually done by kernel)
*/
if (!(flags & MS_REMOUNT) && fake && mounted (spec, node))
die(EX_USAGE, _("mount: according to mtab, "
"%s is already mounted on %s\n"),
spec, node);
if (opt_speed)
cdrom_setspeed(spec);
if (!(flags & MS_REMOUNT)) {
/*
* Don't set up a (new) loop device if we only remount - this left
* stale assignments of files to loop devices. Nasty when used for
* encryption.
*/
res = loop_check(&spec, &types, &flags, &loop, &loopdev, &loopfile, node);
if (res)
goto out;
}
if (loop)
opt_loopdev = loopdev;
if (flags & (MS_BIND | MS_MOVE | MS_PROPAGATION))
types = "none";
#ifdef HAVE_LIBSELINUX
if ((flags & MS_REMOUNT) && mount_opts)
mount_opts = remove_context_options(mount_opts);
#endif
/*
* Call mount.TYPE for types that require a separate mount program.
* For the moment these types are ncpfs and smbfs. Maybe also vxfs.
* All such special things must occur isolated in the types string.
*/
if (check_special_mountprog(spec, node, types, flags, mount_opts, &status)) {
res = status;
goto out;
}
#ifdef HAVE_LIBMOUNT_MOUNT
mtab_opts = fix_opts_string(flags & ~MS_NOMTAB, extra_opts, user);
mtab_flags = flags;
#endif
block_signals (SIG_BLOCK);
if (!fake) {
mnt5_res = guess_fstype_and_mount (spec, node, &types, flags & ~MS_NOSYS,
mount_opts, &special, &status);
if (special) {
block_signals (SIG_UNBLOCK);
res = status;
goto out;
}
}
/* Kernel allows to use MS_RDONLY for bind mounts, but the read-only request
* could be silently ignored. Check it to avoid 'ro' in mtab and 'rw' in
* /proc/mounts.
*/
if (!fake && mnt5_res == 0 &&
(flags & MS_BIND) && (flags & MS_RDONLY) && !is_readonly(node)) {
printf(_("mount: warning: %s seems to be mounted read-write.\n"), node);
flags &= ~MS_RDONLY;
}
/* Kernel can silently add MS_RDONLY flag when mounting file system that
* does not have write support. Check this to avoid 'ro' in /proc/mounts
* and 'rw' in mtab.
*/
if (!fake && mnt5_res == 0 &&
!(flags & (MS_RDONLY | MS_PROPAGATION | MS_MOVE)) &&
is_readonly(node)) {
printf(_("mount: warning: %s seems to be mounted read-only.\n"), node);
flags |= MS_RDONLY;
}
if (fake || mnt5_res == 0) {
char *mo = fix_opts_string (flags & ~MS_NOMTAB, extra_opts, user);
const char *tp = types ? types : "unknown";
/* Mount succeeded, report this (if verbose) and write mtab entry. */
#ifdef HAVE_LIBMOUNT_MOUNT
update_mtab_entry(flags);
if (verbose)
verbose_mount_info(loop ? loopfile : spec, node, tp, mo, flags);
#else
if (!(mounttype & MS_PROPAGATION))
update_mtab_entry(loop ? loopfile : spec,
node,
tp,
mo,
flags,
freq,
pass);
#endif
block_signals (SIG_UNBLOCK);
free(mo);
res = 0;
goto out;
}
mnt_err = errno;
if (loop)
del_loop(spec);
block_signals (SIG_UNBLOCK);
/* Mount failed, complain, but don't die. */
if (types == 0) {
if (restricted)
error (_("mount: I could not determine the filesystem type, "
"and none was specified"));
else
error (_("mount: you must specify the filesystem type"));
} else if (mnt5_res != -1) {
/* should not happen */
error (_("mount: mount failed"));
} else {
switch (mnt_err) {
case EPERM:
if (geteuid() == 0) {
if (stat (node, &statbuf) || !S_ISDIR(statbuf.st_mode))
error (_("mount: mount point %s is not a directory"), node);
else
error (_("mount: permission denied"));
} else
error (_("mount: must be superuser to use mount"));
break;
case EBUSY:
if (flags & MS_REMOUNT) {
error (_("mount: %s is busy"), node);
} else if (!strcmp(types, "proc") && !strcmp(node, "/proc")) {
/* heuristic: if /proc/version exists, then probably proc is mounted */
if (stat ("/proc/version", &statbuf)) /* proc mounted? */
error (_("mount: %s is busy"), node); /* no */
else if (!mount_all || verbose) /* yes, don't mention it */
error (_("mount: proc already mounted"));
} else {
error (_("mount: %s already mounted or %s busy"), spec, node);
already (spec, node);
}
break;
case ENOENT:
if (lstat (node, &statbuf))
error (_("mount: mount point %s does not exist"), node);
else if (stat (node, &statbuf))
error (_("mount: mount point %s is a symbolic link to nowhere"),
node);
else if (stat (spec, &statbuf)) {
if (opt_nofail)
goto out;
error (_("mount: special device %s does not exist"), spec);
} else {
errno = mnt_err;
perror("mount");
}
break;
case ENOTDIR:
if (stat (node, &statbuf) || ! S_ISDIR(statbuf.st_mode))
error (_("mount: mount point %s is not a directory"), node);
else if (stat (spec, &statbuf) && errno == ENOTDIR) {
if (opt_nofail)
goto out;
error (_("mount: special device %s does not exist\n"
" (a path prefix is not a directory)\n"), spec);
} else {
errno = mnt_err;
perror("mount");
}
break;
case EINVAL:
{ int fd;
unsigned long long size = 0;
if (flags & MS_REMOUNT) {
error (_("mount: %s not mounted already, or bad option"), node);
} else {
error (_("mount: wrong fs type, bad option, bad superblock on %s,\n"
" missing codepage or helper program, or other error"),
spec);
if (stat(spec, &statbuf) < 0) {
if (errno == ENOENT) /* network FS? */
error(_(
" (for several filesystems (e.g. nfs, cifs) you might\n"
" need a /sbin/mount.<type> helper program)"));
} else if (S_ISBLK(statbuf.st_mode)
&& (fd = open(spec, O_RDONLY | O_NONBLOCK)) >= 0) {
if (blkdev_get_size(fd, &size) == 0) {
if (size == 0 && !loop)
error(_(
" (could this be the IDE device where you in fact use\n"
" ide-scsi so that sr0 or sda or so is needed?)"));
if (size && size <= 2)
error(_(
" (aren't you trying to mount an extended partition,\n"
" instead of some logical partition inside?)"));
close(fd);
}
}
error(_(
" In some cases useful info is found in syslog - try\n"
" dmesg | tail or so\n"));
}
break;
}
case EMFILE:
error (_("mount table full")); break;
case EIO:
error (_("mount: %s: can't read superblock"), spec); break;
case ENODEV:
{
int pfs = known_fstype_in_procfs(types);
if (pfs == 1 || !strcmp(types, "guess"))
error(_("mount: %s: unknown device"), spec);
else if (pfs == 0) {
char *lowtype, *p;
int u;
error (_("mount: unknown filesystem type '%s'"), types);
/* maybe this loser asked for FAT or ISO9660 or isofs */
lowtype = xstrdup(types);
u = 0;
for(p=lowtype; *p; p++) {
if(tolower(*p) != *p) {
*p = tolower(*p);
u++;
}
}
if (u && known_fstype_in_procfs(lowtype) == 1)
error (_("mount: probably you meant %s"), lowtype);
else if (!strncmp(lowtype, "iso", 3) &&
known_fstype_in_procfs("iso9660") == 1)
error (_("mount: maybe you meant 'iso9660'?"));
else if (!strncmp(lowtype, "fat", 3) &&
known_fstype_in_procfs("vfat") == 1)
error (_("mount: maybe you meant 'vfat'?"));
free(lowtype);
} else
error (_("mount: %s has wrong device number or fs type %s not supported"),
spec, types);
break;
}
case ENOTBLK:
if (opt_nofail)
goto out;
if (stat (spec, &statbuf)) /* strange ... */
error (_("mount: %s is not a block device, and stat fails?"), spec);
else if (S_ISBLK(statbuf.st_mode))
error (_("mount: the kernel does not recognize %s as a block device\n"
" (maybe `modprobe driver'?)"), spec);
else if (S_ISREG(statbuf.st_mode))
error (_("mount: %s is not a block device (maybe try `-o loop'?)"),
spec);
else
error (_("mount: %s is not a block device"), spec);
break;
case ENXIO:
if (opt_nofail)
goto out;
error (_("mount: %s is not a valid block device"), spec); break;
case EACCES: /* pre-linux 1.1.38, 1.1.41 and later */
case EROFS: /* linux 1.1.38 and later */
{ char *bd = (loop ? "" : _("block device "));
if (ro || (flags & MS_RDONLY)) {
error (_("mount: cannot mount %s%s read-only"),
bd, spec);
break;
} else if (readwrite) {
error (_("mount: %s%s is write-protected but explicit `-w' flag given"),
bd, spec);
break;
} else if (flags & MS_REMOUNT) {
error (_("mount: cannot remount %s%s read-write, is write-protected"),
bd, spec);
break;
} else {
opts = opts0;
types = types0;
if (opts) {
char *opts2 = append_opt(xstrdup(opts), "ro", NULL);
my_free(opts1);
opts = opts1 = opts2;
} else
opts = "ro";
if (types && !strcmp(types, "guess"))
types = 0;
error (_("mount: %s%s is write-protected, mounting read-only"),
bd, spec0);
res = try_mount_one (spec0, node0, types, opts, freq, pass, 1);
goto out;
}
break;
}
case ENOMEDIUM:
error(_("mount: no medium found on %s"), spec);
break;
default:
error ("mount: %s", strerror (mnt_err)); break;
}
}
res = EX_FAIL;
out:
#if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
if (res != EX_FAIL && verbose && is_selinux_enabled() > 0) {
security_context_t raw = NULL, def = NULL;
if (getfilecon(node, &raw) > 0 &&
security_get_initial_context("file", &def) == 0) {
if (!selinux_file_context_cmp(raw, def))
printf(_("mount: %s does not contain SELinux labels.\n"
" You just mounted an file system that supports labels which does not\n"
" contain labels, onto an SELinux box. It is likely that confined\n"
" applications will generate AVC messages and not be allowed access to\n"
" this file system. For more details see restorecon(8) and mount(8).\n"),
node);
}
freecon(raw);
freecon(def);
}
#endif
if (extra_opts1 != mount_opts)
my_free(mount_opts);
my_free(extra_opts1);
my_free(spec1);
my_free(node1);
my_free(opts1);
my_free(types1);
return res;
}
static char *
subst_string(const char *s, const char *sub, int sublen, const char *repl) {
char *n;
n = (char *) xmalloc(strlen(s)-sublen+strlen(repl)+1);
strncpy (n, s, sub-s);
strcpy (n + (sub-s), repl);
strcat (n, sub+sublen);
return n;
}
static char *
usersubst(const char *opts) {
char *s, *w;
char id[40];
if (!opts)
return NULL;
s = "uid=useruid";
if (opts && (w = strstr(opts, s)) != NULL) {
sprintf(id, "uid=%u", getuid());
opts = subst_string(opts, w, strlen(s), id);
}
s = "gid=usergid";
if (opts && (w = strstr(opts, s)) != NULL) {
sprintf(id, "gid=%u", getgid());
opts = subst_string(opts, w, strlen(s), id);
}
return xstrdup(opts);
}
static int
is_existing_file (const char *s) {
struct stat statbuf;
return (stat(s, &statbuf) == 0);
}
/*
* Return 0 for success (either mounted sth or -a and NOAUTO was given)
*/
static int
mount_one (const char *spec, const char *node, const char *types,
const char *fstabopts, char *cmdlineopts, int freq, int pass) {
const char *nspec;
char *opts;
/* Substitute values in opts, if required */
opts = usersubst(fstabopts);
/* Merge the fstab and command line options. */
opts = append_opt(opts, cmdlineopts, NULL);
if (types == NULL && !mounttype && !is_existing_file(spec)) {
if (strchr (spec, ':') != NULL) {
types = "nfs";
if (verbose)
printf(_("mount: no type was given - "
"I'll assume nfs because of "
"the colon\n"));
} else if(!strncmp(spec, "//", 2)) {
types = "cifs";
if (verbose)
printf(_("mount: no type was given - "
"I'll assume cifs because of "
"the // prefix\n"));
}
}
/* Handle possible LABEL= and UUID= forms of spec */
if (types == NULL || (strncmp(types, "9p", 2) &&
strncmp(types, "nfs", 3) &&
strncmp(types, "cifs", 4) &&
strncmp(types, "smbfs", 5))) {
nspec = spec_to_devname(spec);
if (nspec)
spec = nspec;
}
return try_mount_one (spec, node, types, opts, freq, pass, 0);
}
/* Check if an fsname/dir pair was already in the old mtab. */
static int
mounted (const char *spec0, const char *node0) {
struct mntentchn *mc, *mc0;
const char *spec, *node;
int ret = 0;
/* Handle possible UUID= and LABEL= in spec */
spec = spec_to_devname(spec0);
if (!spec)
return ret;
node = canonicalize(node0);
mc0 = mtab_head();
for (mc = mc0->nxt; mc && mc != mc0; mc = mc->nxt)
if (streq (spec, mc->m.mnt_fsname) &&
streq (node, mc->m.mnt_dir)) {
ret = 1;
break;
}
my_free(spec);
my_free(node);
return ret;
}
/* returns 0 if not mounted, 1 if mounted and -1 in case of error */
static int
is_fstab_entry_mounted(struct mntentchn *mc, int verbose)
{
struct stat st;
if (mounted(mc->m.mnt_fsname, mc->m.mnt_dir))
goto yes;
/* extra care for loop devices */
if ((strstr(mc->m.mnt_opts, "loop=") ||
(stat(mc->m.mnt_fsname, &st) == 0 && S_ISREG(st.st_mode)))) {
char *p = get_option_value(mc->m.mnt_opts, "offset=");
uintmax_t offset = 0;
if (p && strtosize(p, &offset) != 0) {
if (verbose)
printf(_("mount: ignore %s "
"(unparsable offset= option)\n"),
mc->m.mnt_fsname);
return -1;
}
free(p);
if (is_mounted_same_loopfile(mc->m.mnt_dir, mc->m.mnt_fsname, offset))
goto yes;
}
return 0;
yes:
if (verbose)
printf(_("mount: %s already mounted on %s\n"),
mc->m.mnt_fsname, mc->m.mnt_dir);
return 1;
}
/* avoid using stat() on things we are not going to mount anyway.. */
static int
has_noauto (const char *opts) {
char *s;
if (!opts)
return 0;
s = strstr(opts, "noauto");
if (!s)
return 0;
return (s == opts || s[-1] == ',') && (s[6] == 0 || s[6] == ',');
}
/* Mount all filesystems of the specified types except swap and root. */
/* With the --fork option: fork and let different incarnations of
mount handle different filesystems. However, try to avoid several
simultaneous mounts on the same physical disk, since that is very slow. */
#define DISKMAJOR(m) (((int) m) & ~0xf)
static int
do_mount_all (char *types, char *options, char *test_opts) {
struct mntentchn *mc, *mc0, *mtmp;
int status = 0;
struct stat statbuf;
struct child {
pid_t pid;
char *group;
struct mntentchn *mec;
struct mntentchn *meclast;
struct child *nxt;
} childhead, *childtail, *cp;
char major[22];
char *g, *colon;
/* build a chain of what we have to do, or maybe
several chains, one for each major or NFS host */
childhead.nxt = 0;
childtail = &childhead;
mc0 = fstab_head();
for (mc = mc0->nxt; mc && mc != mc0; mc = mc->nxt) {
if (has_noauto (mc->m.mnt_opts))
continue;
if (matching_type (mc->m.mnt_type, types)
&& matching_opts (mc->m.mnt_opts, test_opts)
&& !streq (mc->m.mnt_dir, "/")
&& !streq (mc->m.mnt_dir, "root")
&& !is_fstab_entry_mounted(mc, verbose)) {
mtmp = (struct mntentchn *) xmalloc(sizeof(*mtmp));
*mtmp = *mc;
mtmp->nxt = 0;
g = NULL;
if (optfork) {
if (stat(mc->m.mnt_fsname, &statbuf) == 0 &&
S_ISBLK(statbuf.st_mode)) {
sprintf(major, "#%x",
DISKMAJOR(statbuf.st_rdev));
g = major;
}
if (strcmp(mc->m.mnt_type, "nfs") == 0) {
g = xstrdup(mc->m.mnt_fsname);
colon = strchr(g, ':');
if (colon)
*colon = '\0';
}
}
if (g) {
for (cp = childhead.nxt; cp; cp = cp->nxt)
if (cp->group &&
strcmp(cp->group, g) == 0) {
cp->meclast->nxt = mtmp;
cp->meclast = mtmp;
goto fnd;
}
}
cp = (struct child *) xmalloc(sizeof *cp);
cp->nxt = 0;
cp->mec = cp->meclast = mtmp;
cp->group = xstrdup(g);
cp->pid = 0;
childtail->nxt = cp;
childtail = cp;
fnd:;
}
}
/* now do everything */
for (cp = childhead.nxt; cp; cp = cp->nxt) {
pid_t p = -1;
if (optfork) {
p = fork();
if (p == -1) {
int errsv = errno;
error(_("mount: cannot fork: %s"),
strerror (errsv));
}
else if (p != 0)
cp->pid = p;
}
/* if child, or not forked, do the mounting */
if (p == 0 || p == -1) {
for (mc = cp->mec; mc; mc = mc->nxt) {
status |= mount_one (mc->m.mnt_fsname,
mc->m.mnt_dir,
mc->m.mnt_type,
mc->m.mnt_opts,
options, 0, 0);
}
if (mountcount)
status |= EX_SOMEOK;
if (p == 0)
exit(status);
}
}
/* wait for children, if any */
while ((cp = childhead.nxt) != NULL) {
childhead.nxt = cp->nxt;
if (cp->pid) {
int ret;
keep_waiting:
if(waitpid(cp->pid, &ret, 0) == -1) {
if (errno == EINTR)
goto keep_waiting;
perror("waitpid");
} else if (WIFEXITED(ret))
status |= WEXITSTATUS(ret);
else
status |= EX_SYSERR;
}
}
if (mountcount)
status |= EX_SOMEOK;
return status;
}
static struct option longopts[] = {
{ "all", 0, 0, 'a' },
{ "fake", 0, 0, 'f' },
{ "fork", 0, 0, 'F' },
{ "help", 0, 0, 'h' },
{ "no-mtab", 0, 0, 'n' },
{ "read-only", 0, 0, 'r' },
{ "ro", 0, 0, 'r' },
{ "verbose", 0, 0, 'v' },
{ "version", 0, 0, 'V' },
{ "read-write", 0, 0, 'w' },
{ "rw", 0, 0, 'w' },
{ "options", 1, 0, 'o' },
{ "test-opts", 1, 0, 'O' },
{ "pass-fd", 1, 0, 'p' },
{ "keybits", 1, 0, 'k' },
{ "types", 1, 0, 't' },
{ "bind", 0, 0, 'B' },
{ "move", 0, 0, 'M' },
{ "guess-fstype", 1, 0, 134 },
{ "rbind", 0, 0, 'R' },
{ "make-shared", 0, 0, 136 },
{ "make-slave", 0, 0, 137 },
{ "make-private", 0, 0, 138 },
{ "make-unbindable", 0, 0, 139 },
{ "make-rshared", 0, 0, 140 },
{ "make-rslave", 0, 0, 141 },
{ "make-rprivate", 0, 0, 142 },
{ "make-runbindable", 0, 0, 143 },
{ "no-canonicalize", 0, 0, 144 },
{ "internal-only", 0, 0, 'i' },
{ NULL, 0, 0, 0 }
};
/* Keep the usage message at max 22 lines, each at most 70 chars long.
The user should not need a pager to read it. */
static void
usage (FILE *fp, int n) {
fprintf(fp, _(
"Usage: mount -V : print version\n"
" mount -h : print this help\n"
" mount : list mounted filesystems\n"
" mount -l : idem, including volume labels\n"
"So far the informational part. Next the mounting.\n"
"The command is `mount [-t fstype] something somewhere'.\n"
"Details found in /etc/fstab may be omitted.\n"
" mount -a [-t|-O] ... : mount all stuff from /etc/fstab\n"
" mount device : mount device at the known place\n"
" mount directory : mount known device here\n"
" mount -t type dev dir : ordinary mount command\n"
"Note that one does not really mount a device, one mounts\n"
"a filesystem (of the given type) found on the device.\n"
"One can also mount an already visible directory tree elsewhere:\n"
" mount --bind olddir newdir\n"
"or move a subtree:\n"
" mount --move olddir newdir\n"
"One can change the type of mount containing the directory dir:\n"
" mount --make-shared dir\n"
" mount --make-slave dir\n"
" mount --make-private dir\n"
" mount --make-unbindable dir\n"
"One can change the type of all the mounts in a mount subtree\n"
"containing the directory dir:\n"
" mount --make-rshared dir\n"
" mount --make-rslave dir\n"
" mount --make-rprivate dir\n"
" mount --make-runbindable dir\n"
"A device can be given by name, say /dev/hda1 or /dev/cdrom,\n"
"or by label, using -L label or by uuid, using -U uuid .\n"
"Other options: [-nfFrsvw] [-o options] [-p passwdfd].\n"
"For many more details, say man 8 mount .\n"
));
unlock_mtab();
exit (n);
}
/* returns mount entry from fstab */
static struct mntentchn *
getfs(const char *spec, const char *uuid, const char *label)
{
struct mntentchn *mc = NULL;
const char *devname = NULL;
if (!spec && !uuid && !label)
return NULL;
/*
* A) 99% of all cases, the spec on cmdline matches
* with spec in fstab
*/
if (uuid)
mc = getfs_by_uuid(uuid);
else if (label)
mc = getfs_by_label(label);
else {
mc = getfs_by_spec(spec);
if (!mc)
mc = getfs_by_dir(spec);
}
if (mc)
return mc;
/*
* B) UUID or LABEL on cmdline, but devname in fstab
*/
if (uuid)
devname = fsprobe_get_devname_by_uuid(uuid);
else if (label)
devname = fsprobe_get_devname_by_label(label);
else
devname = spec_to_devname(spec);
if (devname)
mc = getfs_by_devname(devname);
/*
* C) mixed
*/
if (!mc && devname) {
const char *id = NULL;
if (!label && (!spec || strncmp(spec, "LABEL=", 6))) {
id = fsprobe_get_label_by_devname(devname);
if (id)
mc = getfs_by_label(id);
}
if (!mc && !uuid && (!spec || strncmp(spec, "UUID=", 5))) {
id = fsprobe_get_uuid_by_devname(devname);
if (id)
mc = getfs_by_uuid(id);
}
my_free(id);
if (mc) {
/* use real device name to avoid repetitional
* conversion from LABEL/UUID to devname
*/
my_free(mc->m.mnt_fsname);
mc->m.mnt_fsname = xstrdup(devname);
}
}
/*
* D) remount -- try /etc/mtab
* Earlier mtab was tried first, but this would sometimes try the
* wrong mount in case mtab had the root device entry wrong. Try
* the last occurrence first, since that is the visible mount.
*/
if (!mc && (devname || spec))
mc = getmntfilebackward (devname ? devname : spec, NULL);
my_free(devname);
return mc;
}
static void
print_version(int rc) {
printf( "mount from %s (with "
#ifdef HAVE_LIBBLKID
"libblkid"
#else
"libvolume_id"
#endif
#ifdef HAVE_LIBSELINUX
" and selinux"
#endif
" support)\n", PACKAGE_STRING);
exit(rc);
}
int
main(int argc, char *argv[]) {
int c, result = 0, specseen;
char *options = NULL, *test_opts = NULL, *node;
const char *spec = NULL;
char *label = NULL;
char *keysize = NULL;
char *uuid = NULL;
char *types = NULL;
char *p;
struct mntentchn *mc;
int fd;
sanitize_env();
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
progname = argv[0];
if ((p = strrchr(progname, '/')) != NULL)
progname = p+1;
umask(022);
/* People report that a mount called from init without console
writes error messages to /etc/mtab
Let us try to avoid getting fd's 0,1,2 */
while((fd = open("/dev/null", O_RDWR)) == 0 || fd == 1 || fd == 2) ;
if (fd > 2)
close(fd);
fsprobe_init();
#ifdef DO_PS_FIDDLING
initproctitle(argc, argv);
#endif
while ((c = getopt_long (argc, argv, "aBfFhik:lL:Mno:O:p:rRsU:vVwt:",
longopts, NULL)) != -1) {
switch (c) {
case 'a': /* mount everything in fstab */
++mount_all;
break;
case 'B': /* bind */
mounttype = MS_BIND;
break;
case 'f': /* fake: don't actually call mount(2) */
++fake;
break;
case 'F':
++optfork;
break;
case 'h': /* help */
usage (stdout, 0);
break;
case 'i':
external_allowed = 0;
break;
case 'k':
keysize = optarg;
break;
case 'l':
list_with_volumelabel = 1;
break;
case 'L':
label = optarg;
break;
case 'M': /* move */
mounttype = MS_MOVE;
break;
case 'n': /* do not write /etc/mtab */
++nomtab;
break;
case 'o': /* specify mount options */
options = append_opt(options, optarg, NULL);
break;
case 'O': /* with -t: mount only if (not) opt */
test_opts = append_opt(test_opts, optarg, NULL);
break;
case 'p': /* fd on which to read passwd */
set_pfd(optarg);
break;
case 'r': /* mount readonly */
readonly = 1;
readwrite = 0;
break;
case 'R': /* rbind */
mounttype = (MS_BIND | MS_REC);
break;
case 's': /* allow sloppy mount options */
sloppy = 1;
break;
case 't': /* specify file system types */
types = optarg;
break;
case 'U':
uuid = optarg;
break;
case 'v': /* be chatty - more so if repeated */
++verbose;
break;
case 'V': /* version */
print_version(EXIT_SUCCESS);
break;
case 'w': /* mount read/write */
readwrite = 1;
readonly = 0;
break;
case 0:
break;
case 134:
/* undocumented, may go away again:
call: mount --guess-fstype device
use only for testing purposes -
the guessing is not reliable at all */
{
const char *fstype;
fstype = fsprobe_get_fstype_by_devname(optarg);
printf("%s\n", fstype ? fstype : "unknown");
exit(fstype ? 0 : EX_FAIL);
}
case 136:
mounttype = MS_SHARED;
break;
case 137:
mounttype = MS_SLAVE;
break;
case 138:
mounttype = MS_PRIVATE;
break;
case 139:
mounttype = MS_UNBINDABLE;
break;
case 140:
mounttype = (MS_SHARED | MS_REC);
break;
case 141:
mounttype = (MS_SLAVE | MS_REC);
break;
case 142:
mounttype = (MS_PRIVATE | MS_REC);
break;
case 143:
mounttype = (MS_UNBINDABLE | MS_REC);
break;
case 144:
nocanonicalize = 1;
break;
case '?':
default:
usage (stderr, EX_USAGE);
}
}
if (verbose > 2) {
printf("mount: fstab path: \"%s\"\n", _PATH_MNTTAB);
printf("mount: mtab path: \"%s\"\n", _PATH_MOUNTED);
printf("mount: lock path: \"%s\"\n", _PATH_MOUNTED_LOCK);
printf("mount: temp path: \"%s\"\n", _PATH_MOUNTED_TMP);
printf("mount: UID: %u\n", getuid());
printf("mount: eUID: %u\n", geteuid());
}
#ifdef HAVE_LIBMOUNT_MOUNT
mnt_init_debug(0);
#endif
argc -= optind;
argv += optind;
specseen = (uuid || label) ? 1 : 0; /* yes, .. i know */
if (argc+specseen == 0 && !mount_all) {
if (options || mounttype)
usage (stderr, EX_USAGE);
return print_all (types);
}
{
const uid_t ruid = getuid();
const uid_t euid = geteuid();
/* if we're really root and aren't running setuid */
if (((uid_t)0 == ruid) && (ruid == euid)) {
restricted = 0;
}
if (restricted &&
(types || options || readwrite || nomtab || mount_all ||
nocanonicalize || fake || mounttype ||
(argc + specseen) != 1)) {
if (ruid == 0 && euid != 0)
/* user is root, but setuid to non-root */
die (EX_USAGE, _("mount: only root can do that "
"(effective UID is %u)"), euid);
die (EX_USAGE, _("mount: only root can do that"));
}
}
if (keysize && sscanf(keysize,"%d",&keysz) != 1)
die (EX_USAGE, _("mount: argument to --keybits or -k must be a number"));
atexit(unlock_mtab);
switch (argc+specseen) {
case 0:
/* mount -a */
result = do_mount_all (types, options, test_opts);
if (result == 0 && verbose && !fake)
error(_("nothing was mounted"));
break;
case 1:
/* mount [-nfrvw] [-o options] special | node
* mount -L label (or -U uuid)
* (/etc/fstab is necessary)
*/
if (types != NULL)
usage (stderr, EX_USAGE);
if (uuid || label)
mc = getfs(NULL, uuid, label);
else
mc = getfs(*argv, NULL, NULL);
if (!mc) {
if (uuid || label)
die (EX_USAGE, _("mount: no such partition found"));
die (EX_USAGE,
_("mount: can't find %s in %s or %s"),
*argv, _PATH_MNTTAB, _PATH_MOUNTED);
}
result = mount_one (xstrdup (mc->m.mnt_fsname),
xstrdup (mc->m.mnt_dir),
xstrdup (mc->m.mnt_type),
mc->m.mnt_opts, options, 0, 0);
break;
case 2:
/* mount special node (/etc/fstab is not necessary) */
if (specseen) {
/* mount -L label node (or -U uuid) */
spec = uuid ? fsprobe_get_devname_by_uuid(uuid) :
fsprobe_get_devname_by_label(label);
node = argv[0];
} else {
/* mount special node */
spec = argv[0];
node = argv[1];
}
if (!spec)
die (EX_USAGE, _("mount: no such partition found"));
result = mount_one (spec, node, types, NULL, options, 0, 0);
break;
default:
usage (stderr, EX_USAGE);
}
if (result == EX_SOMEOK)
result = 0;
fsprobe_exit();
exit (result);
}
|