]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * canonicalize.c -- canonicalize pathname by removing symlinks | |
3 | * | |
4 | * This file may be distributed under the terms of the | |
5 | * GNU Lesser General Public License. | |
6 | * | |
7 | * Copyright (C) 2009-2013 Karel Zak <kzak@redhat.com> | |
8 | */ | |
9 | #include <stdio.h> | |
10 | #include <string.h> | |
11 | #include <ctype.h> | |
12 | #include <unistd.h> | |
13 | #include <errno.h> | |
14 | #include <stdlib.h> | |
15 | #include <sys/types.h> | |
16 | #include <sys/stat.h> | |
17 | ||
18 | #include "canonicalize.h" | |
19 | #include "pathnames.h" | |
20 | #include "all-io.h" | |
21 | #include "strutils.h" | |
22 | #include "fileutils.h" | |
23 | ||
24 | /* | |
25 | * Converts private "dm-N" names to "/dev/mapper/<name>" | |
26 | * | |
27 | * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs | |
28 | * provides the real DM device names in /sys/block/<ptname>/dm/name | |
29 | * | |
30 | * The @prefix allows /sys to be mounted or stored outside the system root | |
31 | * (/prefix/sys/...). | |
32 | */ | |
33 | char *ul_canonicalize_dm_name_prefixed(const char *prefix, const char *ptname) | |
34 | { | |
35 | FILE *f; | |
36 | size_t sz; | |
37 | char path[256], name[sizeof(path) - sizeof(_PATH_DEV_MAPPER)], *res = NULL; | |
38 | ||
39 | if (!ptname || !*ptname) | |
40 | return NULL; | |
41 | ||
42 | if (!prefix) | |
43 | prefix = ""; | |
44 | ||
45 | snprintf(path, sizeof(path), "%s/sys/block/%s/dm/name", prefix, ptname); | |
46 | if (!(f = fopen(path, "r" UL_CLOEXECSTR))) | |
47 | return NULL; | |
48 | ||
49 | /* read "<name>\n" from sysfs */ | |
50 | if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) { | |
51 | name[sz - 1] = '\0'; | |
52 | snprintf(path, sizeof(path), _PATH_DEV_MAPPER "/%s", name); | |
53 | ||
54 | if ((prefix && *prefix) || access(path, F_OK) == 0) | |
55 | res = strdup(path); | |
56 | } | |
57 | fclose(f); | |
58 | return res; | |
59 | } | |
60 | ||
61 | char *ul_canonicalize_dm_name(const char *ptname) | |
62 | { | |
63 | return ul_canonicalize_dm_name_prefixed(NULL, ptname); | |
64 | } | |
65 | ||
66 | static int is_dm_devname(char *canonical, char **name) | |
67 | { | |
68 | struct stat sb; | |
69 | char *p = strrchr(canonical, '/'); | |
70 | ||
71 | *name = NULL; | |
72 | ||
73 | if (!p | |
74 | || strncmp(p, "/dm-", 4) != 0 | |
75 | || !isdigit(*(p + 4)) | |
76 | || stat(canonical, &sb) != 0 | |
77 | || !S_ISBLK(sb.st_mode)) | |
78 | return 0; | |
79 | ||
80 | *name = p + 1; | |
81 | return 1; | |
82 | } | |
83 | ||
84 | /* | |
85 | * This function does not canonicalize the path! It just prepends CWD before a | |
86 | * relative path. If the path is no relative than returns NULL. The path does | |
87 | * not have to exist. | |
88 | */ | |
89 | char *ul_absolute_path(const char *path) | |
90 | { | |
91 | char cwd[PATH_MAX], *res, *p; | |
92 | size_t psz, csz; | |
93 | ||
94 | if (!ul_is_relative_path(path)) { | |
95 | errno = EINVAL; | |
96 | return NULL; | |
97 | } | |
98 | if (!getcwd(cwd, sizeof(cwd))) | |
99 | return NULL; | |
100 | ||
101 | /* simple clean up */ | |
102 | if (ul_startswith(path, "./")) | |
103 | path += 2; | |
104 | else if (strcmp(path, ".") == 0) | |
105 | path = NULL; | |
106 | ||
107 | if (!path || !*path) | |
108 | return strdup(cwd); | |
109 | ||
110 | csz = strlen(cwd); | |
111 | psz = strlen(path); | |
112 | ||
113 | p = res = malloc(csz + 1 + psz + 1); | |
114 | if (!res) | |
115 | return NULL; | |
116 | ||
117 | p = mempcpy(p, cwd, csz); | |
118 | *p++ = '/'; | |
119 | memcpy(p, path, psz + 1); | |
120 | ||
121 | return res; | |
122 | } | |
123 | ||
124 | /* | |
125 | * Returns: <0 on error, 1 is cannot be canonicalized (errno is set); 0 on success | |
126 | */ | |
127 | static int __attribute__((nonnull(2))) | |
128 | do_canonicalize(const char *path, char **result, | |
129 | void *data __attribute__((__unused__))) | |
130 | { | |
131 | char *canonical, *dmname; | |
132 | ||
133 | *result = NULL; | |
134 | ||
135 | if (!path || !*path) { | |
136 | errno = EINVAL; | |
137 | return -errno; | |
138 | } | |
139 | ||
140 | errno = 0; | |
141 | canonical = realpath(path, NULL); | |
142 | if (!canonical) | |
143 | return 1; | |
144 | ||
145 | if (is_dm_devname(canonical, &dmname)) { | |
146 | char *dm = ul_canonicalize_dm_name(dmname); | |
147 | if (dm) { | |
148 | free(canonical); | |
149 | canonical = dm; | |
150 | } | |
151 | } | |
152 | ||
153 | if (canonical) | |
154 | *result = canonical; | |
155 | return 0; | |
156 | } | |
157 | ||
158 | /* | |
159 | * Always returns a newly allocated string or NULL in case of an error. An | |
160 | * unreachable path is not an error (!), and in this case, it just duplicates | |
161 | * @path. | |
162 | */ | |
163 | char *ul_canonicalize_path(const char *path) | |
164 | { | |
165 | char *canonical = NULL; | |
166 | ||
167 | if (do_canonicalize(path, &canonical, NULL) == 1) | |
168 | return strdup(path); | |
169 | ||
170 | return canonical; | |
171 | } | |
172 | ||
173 | /* | |
174 | * Drop permissions (e.g., suid) and canonicalize the path. If the path is | |
175 | * unreadable (for example, due to missing permissions), it returns NULL. | |
176 | */ | |
177 | char *ul_canonicalize_path_restricted(const char *path) | |
178 | { | |
179 | return ul_restricted_path_oper(path, do_canonicalize, NULL); | |
180 | } | |
181 | ||
182 | #ifdef TEST_PROGRAM_CANONICALIZE | |
183 | int main(int argc, char **argv) | |
184 | { | |
185 | char *p; | |
186 | ||
187 | if (argc < 2) { | |
188 | fprintf(stderr, "usage: %s <device>\n", argv[0]); | |
189 | exit(EXIT_FAILURE); | |
190 | } | |
191 | ||
192 | fprintf(stdout, "orig: %s\n", argv[1]); | |
193 | ||
194 | p = ul_canonicalize_path(argv[1]); | |
195 | fprintf(stdout, "real: %s\n", p); | |
196 | free(p); | |
197 | ||
198 | p = ul_canonicalize_path_restricted(argv[1]); | |
199 | fprintf(stdout, "real-restricted: %s\n", p); | |
200 | free(p); | |
201 | ||
202 | exit(EXIT_SUCCESS); | |
203 | } | |
204 | #endif |