]> git.ipfire.org Git - thirdparty/systemd.git/blob - namedev_parse.c
[PATCH] udev - safer string handling all over the place
[thirdparty/systemd.git] / namedev_parse.c
1 /*
2 * namedev_parse.c
3 *
4 * Userspace devfs
5 *
6 * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 */
23
24 #ifdef DEBUG
25 /* define this to enable parsing debugging also */
26 /* #define DEBUG_PARSER */
27 #endif
28
29 #include <stddef.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <fcntl.h>
34 #include <ctype.h>
35 #include <unistd.h>
36 #include <sys/stat.h>
37 #include <dirent.h>
38 #include <errno.h>
39
40 #include "udev.h"
41 #include "logging.h"
42 #include "namedev.h"
43
44 LIST_HEAD(file_list);
45
46 static int add_config_dev(struct config_device *new_dev)
47 {
48 struct config_device *tmp_dev;
49
50 tmp_dev = malloc(sizeof(*tmp_dev));
51 if (tmp_dev == NULL)
52 return -ENOMEM;
53 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
54 list_add_tail(&tmp_dev->node, &config_device_list);
55 //dump_config_dev(tmp_dev);
56 return 0;
57 }
58
59 void dump_config_dev(struct config_device *dev)
60 {
61 /*FIXME dump all sysfs's */
62 dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', "
63 "sysfs_file[0]='%s', sysfs_value[0]='%s', "
64 "kernel='%s', program='%s', result='%s'",
65 dev->name, dev->symlink, dev->bus, dev->place, dev->id,
66 dev->sysfs_pair[0].file, dev->sysfs_pair[0].value,
67 dev->kernel, dev->program, dev->result);
68 }
69
70 void dump_config_dev_list(void)
71 {
72 struct config_device *dev;
73
74 list_for_each_entry(dev, &config_device_list, node)
75 dump_config_dev(dev);
76 }
77
78 void dump_perm_dev(struct perm_device *dev)
79 {
80 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
81 dev->name, dev->owner, dev->group, dev->mode);
82 }
83
84 void dump_perm_dev_list(void)
85 {
86 struct perm_device *dev;
87
88 list_for_each_entry(dev, &perm_device_list, node)
89 dump_perm_dev(dev);
90 }
91
92 /* extract possible KEY{attr} or KEY_attr */
93 static char *get_key_attribute(char *str)
94 {
95 char *pos;
96 char *attr;
97
98 attr = strchr(str, '{');
99 if (attr != NULL) {
100 attr++;
101 pos = strchr(attr, '}');
102 if (pos == NULL) {
103 dbg("missing closing brace for format");
104 return NULL;
105 }
106 pos[0] = '\0';
107 dbg("attribute='%s'", attr);
108 return attr;
109 }
110
111 attr = strchr(str, '_');
112 if (attr != NULL) {
113 attr++;
114 dbg("attribute='%s'", attr);
115 return attr;
116 }
117
118 return NULL;
119 }
120
121 static int namedev_parse_rules(char *filename)
122 {
123 char line[255];
124 int lineno;
125 char *temp;
126 char *temp2;
127 char *temp3;
128 char *attr;
129 FILE *fd;
130 int program_given = 0;
131 int retval = 0;
132 struct config_device dev;
133
134 fd = fopen(filename, "r");
135 if (fd != NULL) {
136 dbg("reading '%s' as rules file", filename);
137 } else {
138 dbg("can't open '%s' as a rules file", filename);
139 return -ENODEV;
140 }
141
142 /* loop through the whole file */
143 lineno = 0;
144 while (1) {
145 /* get a line */
146 temp = fgets(line, sizeof(line), fd);
147 if (temp == NULL)
148 goto exit;
149 lineno++;
150 dbg_parse("read '%s'", temp);
151
152 /* eat the whitespace */
153 while (isspace(*temp))
154 ++temp;
155
156 /* empty line? */
157 if ((*temp == '\0') || (*temp == '\n'))
158 continue;
159
160 /* see if this is a comment */
161 if (*temp == COMMENT_CHARACTER)
162 continue;
163
164 memset(&dev, 0x00, sizeof(struct config_device));
165
166 /* get all known keys */
167 while (1) {
168 retval = parse_get_pair(&temp, &temp2, &temp3);
169 if (retval)
170 break;
171
172 if (strcasecmp(temp2, FIELD_BUS) == 0) {
173 strfieldcpy(dev.bus, temp3);
174 continue;
175 }
176
177 if (strcasecmp(temp2, FIELD_ID) == 0) {
178 strfieldcpy(dev.id, temp3);
179 continue;
180 }
181
182 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
183 strfieldcpy(dev.place, temp3);
184 continue;
185 }
186
187 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
188 struct sysfs_pair *pair = &dev.sysfs_pair[0];
189 int sysfs_pair_num = 0;
190
191 /* find first unused pair */
192 while (pair->file[0] != '\0') {
193 ++sysfs_pair_num;
194 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
195 pair = NULL;
196 break;
197 }
198 ++pair;
199 }
200 if (pair) {
201 attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
202 if (attr == NULL) {
203 dbg("error parsing " FIELD_SYSFS " attribute");
204 continue;
205 }
206 strfieldcpy(pair->file, attr);
207 strfieldcpy(pair->value, temp3);
208 }
209 continue;
210 }
211
212 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
213 strfieldcpy(dev.kernel, temp3);
214 continue;
215 }
216
217 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
218 program_given = 1;
219 strfieldcpy(dev.program, temp3);
220 continue;
221 }
222
223 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
224 strfieldcpy(dev.result, temp3);
225 continue;
226 }
227
228 if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
229 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
230 if (attr != NULL)
231 if (strcasecmp(attr, ATTR_PARTITIONS) == 0) {
232 dbg_parse("creation of partition nodes requested");
233 dev.partitions = PARTITIONS_COUNT;
234 }
235 strfieldcpy(dev.name, temp3);
236 continue;
237 }
238
239 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
240 strfieldcpy(dev.symlink, temp3);
241 continue;
242 }
243
244 dbg("unknown type of field '%s'", temp2);
245 dbg("You might be using a rules file in the old format, please fix.");
246 goto error;
247 }
248
249 /* simple plausibility check for given keys */
250 if ((dev.sysfs_pair[0].file[0] == '\0') ^
251 (dev.sysfs_pair[0].value[0] == '\0')) {
252 dbg("inconsistency in " FIELD_SYSFS " key");
253 goto error;
254 }
255
256 if ((dev.result[0] != '\0') && (program_given == 0)) {
257 dbg(FIELD_RESULT " is only useful when "
258 FIELD_PROGRAM " is called in any rule before");
259 goto error;
260 }
261
262 dev.config_line = lineno;
263 retval = add_config_dev(&dev);
264 if (retval) {
265 dbg("add_config_dev returned with error %d", retval);
266 continue;
267 error:
268 dbg("%s:%d:%d: parse error, rule skipped",
269 filename, lineno, temp - line);
270 }
271 }
272 exit:
273 fclose(fd);
274 return retval;
275 }
276
277 static int namedev_parse_permissions(char *filename)
278 {
279 char line[255];
280 char *temp;
281 char *temp2;
282 FILE *fd;
283 int retval = 0;
284 struct perm_device dev;
285
286 fd = fopen(filename, "r");
287 if (fd != NULL) {
288 dbg("reading '%s' as permissions file", filename);
289 } else {
290 dbg("can't open '%s' as permissions file", filename);
291 return -ENODEV;
292 }
293
294 /* loop through the whole file */
295 while (1) {
296 temp = fgets(line, sizeof(line), fd);
297 if (temp == NULL)
298 break;
299
300 dbg_parse("read '%s'", temp);
301
302 /* eat the whitespace at the beginning of the line */
303 while (isspace(*temp))
304 ++temp;
305
306 /* empty line? */
307 if ((*temp == '\0') || (*temp == '\n'))
308 continue;
309
310 /* see if this is a comment */
311 if (*temp == COMMENT_CHARACTER)
312 continue;
313
314 memset(&dev, 0x00, sizeof(dev));
315
316 /* parse the line */
317 temp2 = strsep(&temp, ":");
318 if (!temp2) {
319 dbg("cannot parse line '%s'", line);
320 continue;
321 }
322 strfieldcpy(dev.name, temp2);
323
324 temp2 = strsep(&temp, ":");
325 if (!temp2) {
326 dbg("cannot parse line '%s'", line);
327 continue;
328 }
329 strfieldcpy(dev.owner, temp2);
330
331 temp2 = strsep(&temp, ":");
332 if (!temp2) {
333 dbg("cannot parse line '%s'", line);
334 continue;
335 }
336 strfieldcpy(dev.group, temp2);
337
338 if (!temp) {
339 dbg("cannot parse line: %s", line);
340 continue;
341 }
342 dev.mode = strtol(temp, NULL, 8);
343
344 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
345 dev.name, dev.owner, dev.group,
346 dev.mode);
347 retval = add_perm_dev(&dev);
348 if (retval) {
349 dbg("add_perm_dev returned with error %d", retval);
350 goto exit;
351 }
352 }
353
354 exit:
355 fclose(fd);
356 return retval;
357 }
358
359 struct files {
360 struct list_head list;
361 char name[NAME_SIZE];
362 };
363
364 /* sort files in lexical order */
365 static int file_list_insert(char *filename)
366 {
367 struct files *loop_file;
368 struct files *new_file;
369
370 list_for_each_entry(loop_file, &file_list, list) {
371 if (strcmp(loop_file->name, filename) > 0) {
372 break;
373 }
374 }
375
376 new_file = malloc(sizeof(struct files));
377 if (new_file == NULL) {
378 dbg("error malloc");
379 return -ENOMEM;
380 }
381
382 strfieldcpy(new_file->name, filename);
383 list_add_tail(&new_file->list, &loop_file->list);
384 return 0;
385 }
386
387 /* calls function for file or every file found in directory */
388 static int call_foreach_file(int parser (char *f) , char *filename, char *extension)
389 {
390 struct dirent *ent;
391 DIR *dir;
392 char *ext;
393 char file[NAME_SIZE];
394 struct stat stats;
395 struct files *loop_file;
396 struct files *tmp_file;
397
398 /* look if we have a plain file or a directory to scan */
399 stat(filename, &stats);
400 if ((stats.st_mode & S_IFMT) != S_IFDIR)
401 return parser(filename);
402
403 /* sort matching filename into list */
404 dbg("open config as directory '%s'", filename);
405 dir = opendir(filename);
406 while (1) {
407 ent = readdir(dir);
408 if (ent == NULL || ent->d_name[0] == '\0')
409 break;
410
411 dbg("found file '%s'", ent->d_name);
412 ext = strrchr(ent->d_name, '.');
413 if (ext == NULL)
414 continue;
415
416 if (strcmp(ext, extension) == 0) {
417 dbg("put file in list '%s'", ent->d_name);
418 file_list_insert(ent->d_name);
419 }
420 }
421
422 /* parse every file in the list */
423 list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
424 strfieldcpy(file, filename);
425 strfieldcat(file, loop_file->name);
426 parser(file);
427 list_del(&loop_file->list);
428 free(loop_file);
429 }
430
431 closedir(dir);
432 return 0;
433 }
434
435 int namedev_init_rules()
436 {
437 return call_foreach_file(namedev_parse_rules, udev_rules_filename, RULEFILE_EXT);
438 }
439
440 int namedev_init_permissions()
441 {
442 return call_foreach_file(namedev_parse_permissions, udev_permissions_filename, PERMFILE_EXT);
443 }