]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/canonicalize.c
lib: [loopdev.c] add module for work loop devices
[thirdparty/util-linux.git] / lib / canonicalize.c
1 /*
2 * canonicalize.c -- canonicalize pathname by removing symlinks
3 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library Public License for more details.
14 *
15 */
16
17 /*
18 * This routine is part of libc. We include it nevertheless,
19 * since the libc version has some security flaws.
20 *
21 * TODO: use canonicalize_file_name() when exist in glibc
22 */
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <stdlib.h>
29
30 #include "canonicalize.h"
31
32 #ifndef MAXSYMLINKS
33 # define MAXSYMLINKS 256
34 #endif
35
36 static char *
37 myrealpath(const char *path, char *resolved_path, int maxreslth) {
38 int readlinks = 0;
39 char *npath;
40 char link_path[PATH_MAX+1];
41 int n;
42 char *buf = NULL;
43
44 npath = resolved_path;
45
46 /* If it's a relative pathname use getcwd for starters. */
47 if (*path != '/') {
48 if (!getcwd(npath, maxreslth-2))
49 return NULL;
50 npath += strlen(npath);
51 if (npath[-1] != '/')
52 *npath++ = '/';
53 } else {
54 *npath++ = '/';
55 path++;
56 }
57
58 /* Expand each slash-separated pathname component. */
59 while (*path != '\0') {
60 /* Ignore stray "/" */
61 if (*path == '/') {
62 path++;
63 continue;
64 }
65 if (*path == '.' && (path[1] == '\0' || path[1] == '/')) {
66 /* Ignore "." */
67 path++;
68 continue;
69 }
70 if (*path == '.' && path[1] == '.' &&
71 (path[2] == '\0' || path[2] == '/')) {
72 /* Backup for ".." */
73 path += 2;
74 while (npath > resolved_path+1 &&
75 (--npath)[-1] != '/')
76 ;
77 continue;
78 }
79 /* Safely copy the next pathname component. */
80 while (*path != '\0' && *path != '/') {
81 if (npath-resolved_path > maxreslth-2) {
82 errno = ENAMETOOLONG;
83 goto err;
84 }
85 *npath++ = *path++;
86 }
87
88 /* Protect against infinite loops. */
89 if (readlinks++ > MAXSYMLINKS) {
90 errno = ELOOP;
91 goto err;
92 }
93
94 /* See if last pathname component is a symlink. */
95 *npath = '\0';
96 n = readlink(resolved_path, link_path, PATH_MAX);
97 if (n < 0) {
98 /* EINVAL means the file exists but isn't a symlink. */
99 if (errno != EINVAL)
100 goto err;
101 } else {
102 int m;
103 char *newbuf;
104
105 /* Note: readlink doesn't add the null byte. */
106 link_path[n] = '\0';
107 if (*link_path == '/')
108 /* Start over for an absolute symlink. */
109 npath = resolved_path;
110 else
111 /* Otherwise back up over this component. */
112 while (*(--npath) != '/')
113 ;
114
115 /* Insert symlink contents into path. */
116 m = strlen(path);
117 newbuf = malloc(m + n + 1);
118 if (!newbuf)
119 goto err;
120 memcpy(newbuf, link_path, n);
121 memcpy(newbuf + n, path, m + 1);
122 free(buf);
123 path = buf = newbuf;
124 }
125 *npath++ = '/';
126 }
127 /* Delete trailing slash but don't whomp a lone slash. */
128 if (npath != resolved_path+1 && npath[-1] == '/')
129 npath--;
130 /* Make sure it's null terminated. */
131 *npath = '\0';
132
133 free(buf);
134 return resolved_path;
135
136 err:
137 free(buf);
138 return NULL;
139 }
140
141 /*
142 * Converts private "dm-N" names to "/dev/mapper/<name>"
143 *
144 * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs
145 * provides the real DM device names in /sys/block/<ptname>/dm/name
146 */
147 char *
148 canonicalize_dm_name(const char *ptname)
149 {
150 FILE *f;
151 size_t sz;
152 char path[256], name[256], *res = NULL;
153
154 snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname);
155 if (!(f = fopen(path, "r")))
156 return NULL;
157
158 /* read "<name>\n" from sysfs */
159 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
160 name[sz - 1] = '\0';
161 snprintf(path, sizeof(path), "/dev/mapper/%s", name);
162 res = strdup(path);
163 }
164 fclose(f);
165 return res;
166 }
167
168 char *
169 canonicalize_path(const char *path)
170 {
171 char canonical[PATH_MAX+2];
172 char *p;
173
174 if (path == NULL)
175 return NULL;
176
177 if (!myrealpath(path, canonical, PATH_MAX+1))
178 return strdup(path);
179
180
181 p = strrchr(canonical, '/');
182 if (p && strncmp(p, "/dm-", 4) == 0 && isdigit(*(p + 4))) {
183 p = canonicalize_dm_name(p+1);
184 if (p)
185 return p;
186 }
187
188 return strdup(canonical);
189 }
190
191
192 #ifdef TEST_PROGRAM_CANONICALIZE
193 int main(int argc, char **argv)
194 {
195 if (argc < 2) {
196 fprintf(stderr, "usage: %s <device>\n", argv[0]);
197 exit(EXIT_FAILURE);
198 }
199
200 fprintf(stdout, "orig: %s\n", argv[1]);
201 fprintf(stdout, "real: %s\n", canonicalize_path(argv[1]));
202
203 exit(EXIT_SUCCESS);
204 }
205 #endif