]>
Commit | Line | Data |
---|---|---|
69b7e41e KZ |
1 | /* |
2 | * Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com> | |
3 | * | |
4 | * This file may be redistributed under the terms of the | |
5 | * GNU Lesser General Public License. | |
6 | */ | |
7 | ||
192c6aad KZ |
8 | /** |
9 | * SECTION: utils | |
10 | * @title: Utils | |
11 | * @short_description: misc utils. | |
12 | */ | |
69b7e41e KZ |
13 | #include <unistd.h> |
14 | #include <errno.h> | |
15 | #include <stdlib.h> | |
16 | #include <string.h> | |
17 | #ifdef HAVE_SYS_PRCTL_H | |
18 | #include <sys/prctl.h> | |
19 | #else | |
20 | #define PR_GET_DUMPABLE 3 | |
21 | #endif | |
22 | #if (!defined(HAVE_PRCTL) && defined(linux)) | |
23 | #include <sys/syscall.h> | |
24 | #endif | |
25 | #include <sys/stat.h> | |
26 | #include <ctype.h> | |
27 | #include <sys/types.h> | |
28 | #include <fcntl.h> | |
29 | #include <pwd.h> | |
a1e8af75 | 30 | #include <grp.h> |
69b7e41e | 31 | |
b49103ed | 32 | #include "strutils.h" |
0532ba1d | 33 | #include "pathnames.h" |
69b7e41e | 34 | #include "mountP.h" |
3c5e4ef8 | 35 | #include "mangle.h" |
0bb44be3 | 36 | #include "canonicalize.h" |
69b7e41e KZ |
37 | |
38 | char *mnt_getenv_safe(const char *arg) | |
39 | { | |
40 | if ((getuid() != geteuid()) || (getgid() != getegid())) | |
41 | return NULL; | |
42 | #if HAVE_PRCTL | |
43 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) | |
44 | return NULL; | |
45 | #else | |
46 | #if (defined(linux) && defined(SYS_prctl)) | |
47 | if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) | |
48 | return NULL; | |
49 | #endif | |
50 | #endif | |
51 | ||
52 | #ifdef HAVE___SECURE_GETENV | |
53 | return __secure_getenv(arg); | |
54 | #else | |
55 | return getenv(arg); | |
56 | #endif | |
57 | } | |
58 | ||
b49103ed KZ |
59 | int endswith(const char *s, const char *sx) |
60 | { | |
61 | ssize_t off; | |
62 | ||
63 | assert(s); | |
64 | assert(sx); | |
65 | ||
66 | off = strlen(s); | |
67 | if (!off) | |
68 | return 0; | |
69 | off -= strlen(sx); | |
70 | if (off < 0) | |
71 | return 0; | |
72 | ||
73 | return !strcmp(s + off, sx); | |
74 | } | |
75 | ||
76 | int startswith(const char *s, const char *sx) | |
77 | { | |
78 | size_t off; | |
79 | ||
80 | assert(s); | |
81 | assert(sx); | |
82 | ||
83 | off = strlen(sx); | |
84 | if (!off) | |
85 | return 0; | |
86 | ||
87 | return !strncmp(s, sx, off); | |
88 | } | |
89 | ||
3c5e4ef8 KZ |
90 | /** |
91 | * mnt_mangle: | |
92 | * @str: string | |
93 | * | |
94 | * Encode @str to be compatible with fstab/mtab | |
95 | * | |
96 | * Returns: new allocated string or NULL in case of error. | |
97 | */ | |
98 | char *mnt_mangle(const char *str) | |
99 | { | |
100 | return mangle(str); | |
101 | } | |
102 | ||
103 | /** | |
104 | * mnt_unmangle: | |
105 | * @str: string | |
106 | * | |
107 | * Decode @str from fstab/mtab | |
108 | * | |
109 | * Returns: new allocated string or NULL in case of error. | |
110 | */ | |
111 | char *mnt_unmangle(const char *str) | |
112 | { | |
113 | return unmangle(str); | |
114 | } | |
115 | ||
69b7e41e KZ |
116 | /** |
117 | * mnt_fstype_is_pseudofs: | |
118 | * @type: filesystem name | |
119 | * | |
120 | * Returns: 1 for filesystems like proc, sysfs, ... or 0. | |
121 | */ | |
122 | int mnt_fstype_is_pseudofs(const char *type) | |
123 | { | |
124 | if (!type) | |
125 | return 0; | |
126 | if (strcmp(type, "none") == 0 || | |
127 | strcmp(type, "proc") == 0 || | |
128 | strcmp(type, "tmpfs") == 0 || | |
129 | strcmp(type, "sysfs") == 0 || | |
130 | strcmp(type, "devpts") == 0|| | |
131 | strcmp(type, "cgroups") == 0 || | |
132 | strcmp(type, "devfs") == 0 || | |
133 | strcmp(type, "dlmfs") == 0 || | |
134 | strcmp(type, "cpuset") == 0 || | |
1bbe1bc7 | 135 | strcmp(type, "securityfs") == 0 || |
c5a6f7a9 KZ |
136 | strcmp(type, "rpc_pipefs") == 0 || |
137 | strcmp(type, "fusectl") == 0 || | |
138 | strcmp(type, "binfmt_misc") == 0 || | |
139 | strcmp(type, "fuse.gvfs-fuse-daemon") == 0 || | |
1bbe1bc7 | 140 | strcmp(type, "debugfs") == 0 || |
69b7e41e KZ |
141 | strcmp(type, "spufs") == 0) |
142 | return 1; | |
143 | return 0; | |
144 | } | |
145 | ||
146 | /** | |
147 | * mnt_fstype_is_netfs: | |
148 | * @type: filesystem name | |
149 | * | |
150 | * Returns: 1 for filesystems like cifs, nfs, ... or 0. | |
151 | */ | |
152 | int mnt_fstype_is_netfs(const char *type) | |
153 | { | |
154 | if (!type) | |
155 | return 0; | |
156 | if (strcmp(type, "cifs") == 0 || | |
157 | strcmp(type, "smbfs") == 0 || | |
158 | strncmp(type, "nfs", 3) == 0 || | |
159 | strcmp(type, "afs") == 0 || | |
160 | strcmp(type, "ncpfs") == 0) | |
161 | return 1; | |
162 | return 0; | |
163 | } | |
164 | ||
abc9c0f7 KZ |
165 | /** |
166 | * mnt_match_fstype: | |
167 | * @type: filesystem type | |
168 | * @pattern: filesystem name or comma delimitted list of names | |
169 | * | |
170 | * The @pattern list of filesystem can be prefixed with a global | |
171 | * "no" prefix to invert matching of the whole list. The "no" could | |
3d735589 KZ |
172 | * also used for individual items in the @pattern list. So, |
173 | * "nofoo,bar" has the same meaning as "nofoo,nobar". | |
abc9c0f7 | 174 | * |
3d735589 KZ |
175 | * "bar" : "nofoo,bar" -> False (global "no" prefix) |
176 | * | |
177 | * "bar" : "foo,bar" -> True | |
abc9c0f7 | 178 | * |
abc9c0f7 KZ |
179 | * "bar" : "foo,nobar" -> False |
180 | * | |
181 | * Returns: 1 if type is matching, else 0. This function also returns | |
182 | * 0 if @pattern is NULL and @type is non-NULL. | |
183 | */ | |
184 | int mnt_match_fstype(const char *type, const char *pattern) | |
185 | { | |
186 | int no = 0; /* negated types list */ | |
187 | int len; | |
188 | const char *p; | |
189 | ||
190 | if (!pattern && !type) | |
191 | return 1; | |
192 | if (!pattern) | |
193 | return 0; | |
194 | ||
195 | if (!strncmp(pattern, "no", 2)) { | |
196 | no = 1; | |
197 | pattern += 2; | |
198 | } | |
199 | ||
200 | /* Does type occur in types, separated by commas? */ | |
201 | len = strlen(type); | |
202 | p = pattern; | |
203 | while(1) { | |
204 | if (!strncmp(p, "no", 2) && !strncmp(p+2, type, len) && | |
205 | (p[len+2] == 0 || p[len+2] == ',')) | |
206 | return 0; | |
207 | if (strncmp(p, type, len) == 0 && (p[len] == 0 || p[len] == ',')) | |
208 | return !no; | |
209 | p = strchr(p,','); | |
210 | if (!p) | |
211 | break; | |
212 | p++; | |
213 | } | |
214 | return no; | |
215 | } | |
216 | ||
217 | ||
218 | /* Returns 1 if needle found or noneedle not found in haystack | |
219 | * Otherwise returns 0 | |
220 | */ | |
221 | static int check_option(const char *haystack, size_t len, | |
222 | const char *needle, size_t needle_len) | |
223 | { | |
224 | const char *p; | |
225 | int no = 0; | |
226 | ||
227 | if (needle_len >= 2 && !strncmp(needle, "no", 2)) { | |
228 | no = 1; | |
229 | needle += 2; | |
230 | needle_len -= 2; | |
231 | } | |
232 | ||
233 | for (p = haystack; p && p < haystack + len; p++) { | |
234 | char *sep = strchr(p, ','); | |
235 | size_t plen = sep ? sep - p : len - (p - haystack); | |
236 | ||
237 | if (plen == needle_len) { | |
238 | if (!strncmp(p, needle, plen)) | |
239 | return !no; /* foo or nofoo was found */ | |
240 | } | |
241 | p += plen; | |
242 | } | |
243 | ||
244 | return no; /* foo or nofoo was not found */ | |
245 | } | |
246 | ||
247 | /** | |
248 | * mnt_match_options: | |
249 | * @optstr: options string | |
250 | * @pattern: comma delimitted list of options | |
251 | * | |
252 | * The "no" could used for individual items in the @options list. The "no" | |
253 | * prefix does not have a global meanning. | |
254 | * | |
255 | * Unlike fs type matching, nonetdev,user and nonetdev,nouser have | |
256 | * DIFFERENT meanings; each option is matched explicitly as specified. | |
257 | * | |
3d735589 KZ |
258 | * "xxx,yyy,zzz" : "nozzz" -> False |
259 | * | |
260 | * "xxx,yyy,zzz" : "xxx,noeee" -> True | |
abc9c0f7 KZ |
261 | * |
262 | * Returns: 1 if pattern is matching, else 0. This function also returns 0 | |
263 | * if @pattern is NULL and @optstr is non-NULL. | |
264 | */ | |
265 | int mnt_match_options(const char *optstr, const char *pattern) | |
266 | { | |
267 | const char *p; | |
268 | size_t len, optstr_len = 0; | |
269 | ||
270 | if (!pattern && !optstr) | |
271 | return 1; | |
272 | if (!pattern) | |
273 | return 0; | |
274 | ||
275 | len = strlen(pattern); | |
276 | if (optstr) | |
277 | optstr_len = strlen(optstr); | |
278 | ||
279 | for (p = pattern; p < pattern + len; p++) { | |
280 | char *sep = strchr(p, ','); | |
281 | size_t plen = sep ? sep - p : len - (p - pattern); | |
282 | ||
283 | if (!plen) | |
284 | continue; /* if two ',' appear in a row */ | |
285 | ||
286 | if (!check_option(optstr, optstr_len, p, plen)) | |
287 | return 0; /* any match failure means failure */ | |
288 | ||
289 | p += plen; | |
290 | } | |
291 | ||
292 | /* no match failures in list means success */ | |
293 | return 1; | |
294 | } | |
295 | ||
69b7e41e KZ |
296 | /* |
297 | * Returns allocated string with username or NULL. | |
298 | */ | |
299 | char *mnt_get_username(const uid_t uid) | |
300 | { | |
301 | struct passwd pwd; | |
302 | struct passwd *res; | |
303 | size_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); | |
304 | char *buf, *username = NULL; | |
305 | ||
306 | if (sz <= 0) | |
307 | sz = 16384; /* Should be more than enough */ | |
308 | ||
309 | buf = malloc(sz); | |
310 | if (!buf) | |
311 | return NULL; | |
312 | ||
313 | if (!getpwuid_r(uid, &pwd, buf, sz, &res) && res) | |
314 | username = strdup(pwd.pw_name); | |
315 | ||
316 | free(buf); | |
317 | return username; | |
318 | } | |
abc9c0f7 | 319 | |
a1e8af75 KZ |
320 | int mnt_get_uid(const char *username, uid_t *uid) |
321 | { | |
322 | struct passwd pwd; | |
323 | struct passwd *pw; | |
324 | size_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); | |
325 | char *buf; | |
326 | ||
327 | if (sz <= 0) | |
328 | sz = 16384; /* Should be more than enough */ | |
329 | ||
330 | buf = malloc(sz); | |
331 | if (!buf) | |
332 | return -ENOMEM; | |
333 | ||
334 | if (!getpwnam_r(username, &pwd, buf, sz, &pw) && pw) | |
335 | *uid= pw->pw_uid; | |
336 | ||
337 | free(buf); | |
338 | return 0; | |
339 | } | |
340 | ||
341 | int mnt_get_gid(const char *groupname, gid_t *gid) | |
342 | { | |
343 | struct group grp; | |
344 | struct group *gr; | |
345 | size_t sz = sysconf(_SC_GETGR_R_SIZE_MAX); | |
346 | char *buf; | |
347 | ||
348 | if (sz <= 0) | |
349 | sz = 16384; /* Should be more than enough */ | |
350 | ||
351 | buf = malloc(sz); | |
352 | if (!buf) | |
353 | return -ENOMEM; | |
354 | ||
355 | if (!getgrnam_r(groupname, &grp, buf, sz, &gr) && gr) | |
356 | *gid= gr->gr_gid; | |
357 | ||
358 | free(buf); | |
359 | return 0; | |
360 | } | |
361 | ||
0532ba1d KZ |
362 | /* |
363 | * Returns 1 if /etc/mtab is a reqular file. | |
364 | */ | |
365 | int mnt_has_regular_mtab(void) | |
366 | { | |
367 | struct stat st; | |
368 | ||
369 | if (lstat(_PATH_MOUNTED, &st) == 0 && S_ISREG(st.st_mode)) | |
370 | return 1; | |
371 | return 0; | |
372 | } | |
373 | ||
d1be0c34 KZ |
374 | /** |
375 | * mnt_get_writable_mtab_path: | |
376 | * | |
be1a5180 KZ |
377 | * It's not error if this function return NULL and errno is not set. In case of |
378 | * error the errno is set by open(2). | |
379 | * | |
380 | * Returns: pointer to the static string with path to mtab or NULL. | |
d1be0c34 KZ |
381 | */ |
382 | const char *mnt_get_writable_mtab_path(void) | |
383 | { | |
384 | struct stat mst, ist; | |
385 | int mtab, info; | |
386 | ||
387 | mtab = !lstat(_PATH_MOUNTED, &mst); | |
388 | info = !stat(MNT_PATH_RUNDIR, &ist); | |
389 | ||
be1a5180 KZ |
390 | errno = 0; |
391 | ||
d1be0c34 KZ |
392 | /* A) mtab is symlink, /var/run/mount is available */ |
393 | if (mtab && S_ISLNK(mst.st_mode) && info) { | |
394 | int fd = open(MNT_PATH_MOUNTINFO, O_RDWR | O_CREAT, 0644); | |
395 | if (fd >= 0) { | |
396 | close(fd); | |
397 | return MNT_PATH_MOUNTINFO; | |
398 | } | |
399 | return NULL; /* probably EACCES */ | |
400 | } | |
401 | ||
402 | /* B) classis system with /etc/mtab */ | |
403 | if (mtab && S_ISREG(mst.st_mode)) { | |
404 | int fd = open(_PATH_MOUNTED, O_RDWR, 0644); | |
405 | if (fd >= 0) { | |
406 | close(fd); | |
407 | return _PATH_MOUNTED; | |
408 | } | |
409 | return NULL; /* probably EACCES */ | |
410 | } | |
411 | ||
412 | return NULL; | |
413 | } | |
414 | ||
0bb44be3 KZ |
415 | |
416 | /* returns basename and keeps dirname in the @path, if @path is "/" (root) | |
417 | * then returns empty string */ | |
418 | static char *stripoff_last_component(char *path) | |
419 | { | |
420 | char *p = strrchr(path, '/'); | |
421 | ||
422 | if (!p) | |
423 | return NULL; | |
424 | *p = '\0'; | |
425 | return ++p; | |
426 | } | |
427 | ||
428 | char *mnt_get_mountpoint(const char *path) | |
429 | { | |
430 | char *mnt = strdup(path); | |
431 | struct stat st; | |
432 | dev_t dir, base; | |
433 | ||
434 | if (!mnt) | |
435 | return NULL; | |
436 | if (*mnt == '/' && *(mnt + 1) == '\0') | |
9758c88a | 437 | goto done; |
0bb44be3 KZ |
438 | |
439 | if (stat(mnt, &st)) | |
440 | goto err; | |
441 | base = st.st_dev; | |
442 | ||
443 | do { | |
444 | char *p = stripoff_last_component(mnt); | |
445 | ||
446 | if (!p) | |
447 | break; | |
448 | if (stat(*mnt ? mnt : "/", &st)) | |
449 | goto err; | |
450 | dir = st.st_dev; | |
451 | if (dir != base) { | |
452 | *(p - 1) = '/'; | |
9758c88a | 453 | goto done; |
0bb44be3 KZ |
454 | } |
455 | base = dir; | |
456 | } while (mnt && *(mnt + 1) != '\0'); | |
457 | ||
458 | memcpy(mnt, "/", 2); | |
9758c88a | 459 | done: |
3f31a959 | 460 | DBG(UTILS, mnt_debug("fs-root for %s is %s", path, mnt)); |
9758c88a | 461 | return mnt; |
0bb44be3 KZ |
462 | err: |
463 | free(mnt); | |
464 | return NULL; | |
465 | } | |
466 | ||
9758c88a | 467 | char *mnt_get_fs_root(const char *path, const char *mnt) |
0bb44be3 | 468 | { |
9758c88a | 469 | char *m = (char *) mnt; |
0bb44be3 KZ |
470 | const char *p; |
471 | size_t sz; | |
472 | ||
9758c88a KZ |
473 | if (!m) |
474 | m = mnt_get_mountpoint(path); | |
475 | if (!m) | |
0bb44be3 KZ |
476 | return NULL; |
477 | ||
9758c88a | 478 | sz = strlen(m); |
0bb44be3 KZ |
479 | p = sz > 1 ? path + sz : path; |
480 | ||
9758c88a KZ |
481 | if (m != mnt) |
482 | free(m); | |
0bb44be3 KZ |
483 | |
484 | return *p ? strdup(p) : strdup("/"); | |
485 | } | |
486 | ||
abc9c0f7 KZ |
487 | #ifdef TEST_PROGRAM |
488 | int test_match_fstype(struct mtest *ts, int argc, char *argv[]) | |
489 | { | |
490 | char *type = argv[1]; | |
491 | char *pattern = argv[2]; | |
492 | ||
493 | printf("%s\n", mnt_match_fstype(type, pattern) ? "MATCH" : "NOT-MATCH"); | |
494 | return 0; | |
495 | } | |
496 | ||
497 | int test_match_options(struct mtest *ts, int argc, char *argv[]) | |
498 | { | |
499 | char *optstr = argv[1]; | |
500 | char *pattern = argv[2]; | |
501 | ||
502 | printf("%s\n", mnt_match_options(optstr, pattern) ? "MATCH" : "NOT-MATCH"); | |
503 | return 0; | |
504 | } | |
505 | ||
b49103ed KZ |
506 | int test_startswith(struct mtest *ts, int argc, char *argv[]) |
507 | { | |
508 | char *optstr = argv[1]; | |
509 | char *pattern = argv[2]; | |
510 | ||
511 | printf("%s\n", startswith(optstr, pattern) ? "YES" : "NOT"); | |
512 | return 0; | |
513 | } | |
514 | ||
515 | int test_endswith(struct mtest *ts, int argc, char *argv[]) | |
516 | { | |
517 | char *optstr = argv[1]; | |
518 | char *pattern = argv[2]; | |
519 | ||
520 | printf("%s\n", endswith(optstr, pattern) ? "YES" : "NOT"); | |
521 | return 0; | |
522 | } | |
523 | ||
0bb44be3 KZ |
524 | int test_mountpoint(struct mtest *ts, int argc, char *argv[]) |
525 | { | |
526 | char *path = canonicalize_path(argv[1]), | |
527 | *mnt = path ? mnt_get_mountpoint(path) : NULL; | |
528 | ||
529 | printf("%s: %s\n", argv[1], mnt ? : "unknown"); | |
530 | free(mnt); | |
531 | free(path); | |
532 | return 0; | |
533 | } | |
534 | ||
535 | int test_fsroot(struct mtest *ts, int argc, char *argv[]) | |
536 | { | |
537 | char *path = canonicalize_path(argv[1]), | |
d672fb26 | 538 | *mnt = path ? mnt_get_fs_root(path, NULL) : NULL; |
0bb44be3 KZ |
539 | |
540 | printf("%s: %s\n", argv[1], mnt ? : "unknown"); | |
541 | free(mnt); | |
542 | free(path); | |
543 | return 0; | |
544 | } | |
abc9c0f7 KZ |
545 | |
546 | int main(int argc, char *argv[]) | |
547 | { | |
548 | struct mtest tss[] = { | |
549 | { "--match-fstype", test_match_fstype, "<type> <pattern> FS types matching" }, | |
550 | { "--match-options", test_match_options, "<options> <pattern> options matching" }, | |
b49103ed KZ |
551 | { "--starts-with", test_startswith, "<string> <prefix>" }, |
552 | { "--ends-with", test_endswith, "<string> <prefix>" }, | |
0bb44be3 KZ |
553 | { "--mountpoint", test_mountpoint, "<path>" }, |
554 | { "--fs-root", test_fsroot, "<path>" }, | |
abc9c0f7 KZ |
555 | { NULL } |
556 | }; | |
557 | ||
558 | return mnt_run_test(tss, argc, argv); | |
559 | } | |
560 | ||
561 | #endif /* TEST_PROGRAM */ |