1 /***
2 This file is part of systemd.
3
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stddef.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <dirent.h>
30 #include <ctype.h>
31 #include <fcntl.h>
32 #include <time.h>
33 #include <pwd.h>
34 #include <grp.h>
35 #include <sys/stat.h>
36 #include <sys/param.h>
37
38 #include "device-nodes.h"
39 #include "libudev.h"
40 #include "libudev-private.h"
41 #include "utf8.h"
42 #include "MurmurHash2.h"
43
44 /**
45 * SECTION:libudev-util
46 * @short_description: utils
47 *
48 * Utilities useful when dealing with devices and device node names.
49 */
50
51 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
util_resolve_subsys_kernel(struct udev * udev,const char * string,char * result,size_t maxsize,int read_value)52 int util_resolve_subsys_kernel(struct udev *udev, const char *string,
53 char *result, size_t maxsize, int read_value)
54 {
55 char temp[UTIL_PATH_SIZE];
56 char *subsys;
57 char *sysname;
58 struct udev_device *dev;
59 char *attr;
60
61 if (string[0] != '[')
62 return -1;
63
64 strscpy(temp, sizeof(temp), string);
65
66 subsys = &temp[1];
67
68 sysname = strchr(subsys, '/');
69 if (sysname == NULL)
70 return -1;
71 sysname[0] = '\0';
72 sysname = &sysname[1];
73
74 attr = strchr(sysname, ']');
75 if (attr == NULL)
76 return -1;
77 attr[0] = '\0';
78 attr = &attr[1];
79 if (attr[0] == '/')
80 attr = &attr[1];
81 if (attr[0] == '\0')
82 attr = NULL;
83
84 if (read_value && attr == NULL)
85 return -1;
86
87 dev = udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
88 if (dev == NULL)
89 return -1;
90
91 if (read_value) {
92 const char *val;
93
94 val = udev_device_get_sysattr_value(dev, attr);
95 if (val != NULL)
96 strscpy(result, maxsize, val);
97 else
98 result[0] = '\0';
99 log_debug("value '[%s/%s]%s' is '%s'", subsys, sysname, attr, result);
100 } else {
101 size_t l;
102 char *s;
103
104 s = result;
105 l = strpcpyl(&s, maxsize, udev_device_get_syspath(dev), NULL);
106 if (attr != NULL)
107 strpcpyl(&s, l, "/", attr, NULL);
108 log_debug("path '[%s/%s]%s' is '%s'", subsys, sysname, attr, result);
109 }
110 udev_device_unref(dev);
111 return 0;
112 }
113
util_get_sys_core_link_value(struct udev * udev,const char * slink,const char * syspath,char * value,size_t size)114 ssize_t util_get_sys_core_link_value(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size)
115 {
116 char path[UTIL_PATH_SIZE];
117 char target[UTIL_PATH_SIZE];
118 ssize_t len;
119 const char *pos;
120
121 strscpyl(path, sizeof(path), syspath, "/", slink, NULL);
122 len = readlink(path, target, sizeof(target));
123 if (len <= 0 || len == (ssize_t)sizeof(target))
124 return -1;
125 target[len] = '\0';
126 pos = strrchr(target, '/');
127 if (pos == NULL)
128 return -1;
129 pos = &pos[1];
130 return strscpy(value, size, pos);
131 }
132
util_resolve_sys_link(struct udev * udev,char * syspath,size_t size)133 int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size)
134 {
135 char link_target[UTIL_PATH_SIZE];
136
137 ssize_t len;
138 int i;
139 int back;
140 char *base = NULL;
141
142 len = readlink(syspath, link_target, sizeof(link_target));
143 if (len <= 0 || len == (ssize_t)sizeof(link_target))
144 return -1;
145 link_target[len] = '\0';
146
147 for (back = 0; startswith(&link_target[back * 3], "../"); back++)
148 ;
149 for (i = 0; i <= back; i++) {
150 base = strrchr(syspath, '/');
151 if (base == NULL)
152 return -EINVAL;
153 base[0] = '\0';
154 }
155
156 strscpyl(base, size - (base - syspath), "/", &link_target[back * 3], NULL);
157 return 0;
158 }
159
util_log_priority(const char * priority)160 int util_log_priority(const char *priority)
161 {
162 char *endptr;
163 int prio;
164
165 prio = strtoul(priority, &endptr, 10);
166 if (endptr[0] == '\0' || isspace(endptr[0])) {
167 if (prio >= 0 && prio <= 7)
168 return prio;
169 else
170 return -ERANGE;
171 }
172
173 return log_level_from_string(priority);
174 }
175
util_path_encode(const char * src,char * dest,size_t size)176 size_t util_path_encode(const char *src, char *dest, size_t size)
177 {
178 size_t i, j;
179
180 for (i = 0, j = 0; src[i] != '\0'; i++) {
181 if (src[i] == '/') {
182 if (j+4 >= size) {
183 j = 0;
184 break;
185 }
186 memcpy(&dest[j], "\\x2f", 4);
187 j += 4;
188 } else if (src[i] == '\\') {
189 if (j+4 >= size) {
190 j = 0;
191 break;
192 }
193 memcpy(&dest[j], "\\x5c", 4);
194 j += 4;
195 } else {
196 if (j+1 >= size) {
197 j = 0;
198 break;
199 }
200 dest[j] = src[i];
201 j++;
202 }
203 }
204 dest[j] = '\0';
205 return j;
206 }
207
util_remove_trailing_chars(char * path,char c)208 void util_remove_trailing_chars(char *path, char c)
209 {
210 size_t len;
211
212 if (path == NULL)
213 return;
214 len = strlen(path);
215 while (len > 0 && path[len-1] == c)
216 path[--len] = '\0';
217 }
218
util_replace_whitespace(const char * str,char * to,size_t len)219 int util_replace_whitespace(const char *str, char *to, size_t len)
220 {
221 size_t i, j;
222
223 /* strip trailing whitespace */
224 len = strnlen(str, len);
225 while (len && isspace(str[len-1]))
226 len--;
227
228 /* strip leading whitespace */
229 i = 0;
230 while ((i < len) && isspace(str[i]))
231 i++;
232
233 j = 0;
234 while (i < len) {
235 /* substitute multiple whitespace with a single '_' */
236 if (isspace(str[i])) {
237 while (isspace(str[i]))
238 i++;
239 to[j++] = '_';
240 }
241 to[j++] = str[i++];
242 }
243 to[j] = '\0';
244 return j;
245 }
246
247 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
util_replace_chars(char * str,const char * white)248 int util_replace_chars(char *str, const char *white)
249 {
250 size_t i = 0;
251 int replaced = 0;
252
253 while (str[i] != '\0') {
254 int len;
255
256 if (whitelisted_char_for_devnode(str[i], white)) {
257 i++;
258 continue;
259 }
260
261 /* accept hex encoding */
262 if (str[i] == '\\' && str[i+1] == 'x') {
263 i += 2;
264 continue;
265 }
266
267 /* accept valid utf8 */
268 len = utf8_encoded_valid_unichar(&str[i]);
269 if (len > 1) {
270 i += len;
271 continue;
272 }
273
274 /* if space is allowed, replace whitespace with ordinary space */
275 if (isspace(str[i]) && white != NULL && strchr(white, ' ') != NULL) {
276 str[i] = ' ';
277 i++;
278 replaced++;
279 continue;
280 }
281
282 /* everything else is replaced with '_' */
283 str[i] = '_';
284 i++;
285 replaced++;
286 }
287 return replaced;
288 }
289
290 /**
291 * udev_util_encode_string:
292 * @str: input string to be encoded
293 * @str_enc: output string to store the encoded input string
294 * @len: maximum size of the output string, which may be
295 * four times as long as the input string
296 *
297 * Encode all potentially unsafe characters of a string to the
298 * corresponding 2 char hex value prefixed by '\x'.
299 *
300 * Returns: 0 if the entire string was copied, non-zero otherwise.
301 **/
udev_util_encode_string(const char * str,char * str_enc,size_t len)302 _public_ int udev_util_encode_string(const char *str, char *str_enc, size_t len)
303 {
304 return encode_devnode_name(str, str_enc, len);
305 }
306
util_string_hash32(const char * str)307 unsigned int util_string_hash32(const char *str)
308 {
309 return MurmurHash2(str, strlen(str), 0);
310 }
311
312 /* get a bunch of bit numbers out of the hash, and set the bits in our bit field */
util_string_bloom64(const char * str)313 uint64_t util_string_bloom64(const char *str)
314 {
315 uint64_t bits = 0;
316 unsigned int hash = util_string_hash32(str);
317
318 bits |= 1LLU << (hash & 63);
319 bits |= 1LLU << ((hash >> 6) & 63);
320 bits |= 1LLU << ((hash >> 12) & 63);
321 bits |= 1LLU << ((hash >> 18) & 63);
322 return bits;
323 }
324