]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev_add.c
[PATCH] make udev-test.pl test for root permissions before running
[thirdparty/systemd.git] / udev_add.c
1 /*
2 * udev-add.c
3 *
4 * Userspace devfs
5 *
6 * Copyright (C) 2003 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 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <grp.h>
33 #include <net/if.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <linux/sockios.h>
37 #include <pwd.h>
38
39 #include "libsysfs/sysfs/libsysfs.h"
40 #include "udev.h"
41 #include "udev_lib.h"
42 #include "udev_version.h"
43 #include "logging.h"
44 #include "namedev.h"
45 #include "udevdb.h"
46
47 #include "selinux.h"
48
49 /*
50 * the major/minor of a device is stored in a file called "dev"
51 * The number is stored in decimal values in the format: M:m
52 */
53 static int get_major_minor(struct sysfs_class_device *class_dev, struct udevice *udev)
54 {
55 struct sysfs_attribute *attr = NULL;
56
57 attr = sysfs_get_classdev_attr(class_dev, "dev");
58 if (attr == NULL)
59 goto error;
60 dbg("dev='%s'", attr->value);
61
62 if (sscanf(attr->value, "%u:%u", &udev->major, &udev->minor) != 2)
63 goto error;
64 dbg("found major=%d, minor=%d", udev->major, udev->minor);
65
66 return 0;
67 error:
68 return -1;
69 }
70
71 static int create_path(char *file)
72 {
73 char p[NAME_SIZE];
74 char *pos;
75 int retval;
76 struct stat stats;
77
78 strfieldcpy(p, file);
79 pos = strchr(p+1, '/');
80 while (1) {
81 pos = strchr(pos+1, '/');
82 if (pos == NULL)
83 break;
84 *pos = 0x00;
85 if (stat(p, &stats)) {
86 selinux_setfscreatecon(p, S_IFDIR);
87 retval = mkdir(p, 0755);
88 if (retval != 0) {
89 dbg("mkdir(%s) failed with error '%s'",
90 p, strerror(errno));
91 return retval;
92 }
93 dbg("created '%s'", p);
94 } else {
95 selinux_setfilecon(p, S_IFDIR);
96 }
97 *pos = '/';
98 }
99 return 0;
100 }
101
102 static int make_node(char *file, int major, int minor, unsigned int mode, uid_t uid, gid_t gid)
103 {
104 struct stat stats;
105 int retval = 0;
106
107 if (stat(file, &stats) != 0)
108 goto create;
109
110 /* preserve node with already correct numbers, to not change the inode number */
111 if (((stats.st_mode & S_IFMT) == S_IFBLK || (stats.st_mode & S_IFMT) == S_IFCHR) &&
112 (stats.st_rdev == makedev(major, minor))) {
113 dbg("preserve file '%s', cause it has correct dev_t", file);
114 selinux_setfilecon(file,stats.st_mode);
115 goto perms;
116 }
117
118 if (unlink(file) != 0)
119 dbg("unlink(%s) failed with error '%s'", file, strerror(errno));
120 else
121 dbg("already present file '%s' unlinked", file);
122
123 create:
124 selinux_setfscreatecon(file, mode);
125 retval = mknod(file, mode, makedev(major, minor));
126 if (retval != 0) {
127 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
128 file, mode, major, minor, strerror(errno));
129 goto exit;
130 }
131
132 perms:
133 dbg("chmod(%s, %#o)", file, mode);
134 if (chmod(file, mode) != 0) {
135 dbg("chmod(%s, %#o) failed with error '%s'", file, mode, strerror(errno));
136 goto exit;
137 }
138
139 if (uid != 0 || gid != 0) {
140 dbg("chown(%s, %u, %u)", file, uid, gid);
141 if (chown(file, uid, gid) != 0) {
142 dbg("chown(%s, %u, %u) failed with error '%s'",
143 file, uid, gid, strerror(errno));
144 goto exit;
145 }
146 }
147
148 exit:
149 return retval;
150 }
151
152 static int create_node(struct udevice *udev)
153 {
154 char filename[NAME_SIZE];
155 char linkname[NAME_SIZE];
156 char linktarget[NAME_SIZE];
157 char partitionname[NAME_SIZE];
158 uid_t uid = 0;
159 gid_t gid = 0;
160 int i;
161 int tail;
162 char *pos;
163 int len;
164
165 strfieldcpy(filename, udev_root);
166 strfieldcat(filename, udev->name);
167
168 switch (udev->type) {
169 case 'b':
170 udev->mode |= S_IFBLK;
171 break;
172 case 'c':
173 case 'u':
174 udev->mode |= S_IFCHR;
175 break;
176 case 'p':
177 udev->mode |= S_IFIFO;
178 break;
179 default:
180 dbg("unknown node type %c\n", udev->type);
181 return -EINVAL;
182 }
183
184 /* create parent directories if needed */
185 if (strrchr(udev->name, '/'))
186 create_path(filename);
187
188 if (udev->owner[0] != '\0') {
189 char *endptr;
190 unsigned long id = strtoul(udev->owner, &endptr, 10);
191 if (endptr[0] == '\0')
192 uid = (uid_t) id;
193 else {
194 struct passwd *pw;
195
196 pw = getpwnam(udev->owner);
197 if (pw == NULL)
198 dbg("specified user unknown '%s'", udev->owner);
199 else
200 uid = pw->pw_uid;
201 }
202 }
203
204 if (udev->group[0] != '\0') {
205 char *endptr;
206 unsigned long id = strtoul(udev->group, &endptr, 10);
207 if (endptr[0] == '\0')
208 gid = (gid_t) id;
209 else {
210 struct group *gr = getgrnam(udev->group);
211 if (gr == NULL)
212 dbg("specified group unknown '%s'", udev->group);
213 else
214 gid = gr->gr_gid;
215 }
216 }
217
218 if (!udev->test_run) {
219 info("creating device node '%s'", filename);
220 if (make_node(filename, udev->major, udev->minor, udev->mode, uid, gid) != 0)
221 goto error;
222 } else {
223 info("creating device node '%s', major = '%d', minor = '%d', "
224 "mode = '%#o', uid = '%d', gid = '%d'", filename,
225 udev->major, udev->minor, (mode_t)udev->mode, uid, gid);
226 }
227
228 /* create all_partitions if requested */
229 if (udev->partitions > 0) {
230 info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
231 if (!udev->test_run) {
232 for (i = 1; i <= udev->partitions; i++) {
233 strfieldcpy(partitionname, filename);
234 strintcat(partitionname, i);
235 make_node(partitionname, udev->major, udev->minor + i, udev->mode, uid, gid);
236 }
237 }
238 }
239
240 /* create symlink(s) if requested */
241 foreach_strpart(udev->symlink, " ", pos, len) {
242 strfieldcpymax(linkname, pos, len+1);
243 strfieldcpy(filename, udev_root);
244 strfieldcat(filename, linkname);
245 dbg("symlink '%s' to node '%s' requested", filename, udev->name);
246 if (!udev->test_run)
247 if (strrchr(linkname, '/'))
248 create_path(filename);
249
250 /* optimize relative link */
251 linktarget[0] = '\0';
252 i = 0;
253 tail = 0;
254 while ((udev->name[i] == linkname[i]) && udev->name[i]) {
255 if (udev->name[i] == '/')
256 tail = i+1;
257 i++;
258 }
259 while (linkname[i] != '\0') {
260 if (linkname[i] == '/')
261 strfieldcat(linktarget, "../");
262 i++;
263 }
264
265 strfieldcat(linktarget, &udev->name[tail]);
266
267 dbg("symlink(%s, %s)", linktarget, filename);
268 if (!udev->test_run) {
269 selinux_setfscreatecon(filename, S_IFLNK);
270 unlink(filename);
271 if (symlink(linktarget, filename) != 0)
272 dbg("symlink(%s, %s) failed with error '%s'",
273 linktarget, filename, strerror(errno));
274 }
275 }
276
277 return 0;
278 error:
279 return -1;
280 }
281
282 static int rename_net_if(struct udevice *udev)
283 {
284 int sk;
285 struct ifreq ifr;
286 int retval;
287
288 dbg("changing net interface name from '%s' to '%s'", udev->kernel_name, udev->name);
289 if (udev->test_run)
290 return 0;
291
292 sk = socket(PF_INET, SOCK_DGRAM, 0);
293 if (sk < 0) {
294 dbg("error opening socket");
295 return -1;
296 }
297
298 memset(&ifr, 0x00, sizeof(struct ifreq));
299 strfieldcpy(ifr.ifr_name, udev->kernel_name);
300 strfieldcpy(ifr.ifr_newname, udev->name);
301
302 retval = ioctl(sk, SIOCSIFNAME, &ifr);
303 if (retval != 0)
304 dbg("error changing net interface name");
305 close(sk);
306
307 return retval;
308 }
309
310 int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
311 {
312 char *pos;
313 int retval = 0;
314
315 if (udev->type == 'b' || udev->type == 'c') {
316 retval = get_major_minor(class_dev, udev);
317 if (retval != 0) {
318 dbg("no dev-file found, do nothing");
319 return 0;
320 }
321 }
322
323 if (namedev_name_device(udev, class_dev) != 0)
324 goto exit;
325
326 dbg("adding name='%s'", udev->name);
327
328 selinux_init();
329
330 if (udev->type == 'b' || udev->type == 'c') {
331 retval = create_node(udev);
332 if (retval != 0)
333 goto exit;
334
335 if (udevdb_add_dev(udev) != 0)
336 dbg("udevdb_add_dev failed, but we create the node anyway, "
337 "remove might not work for custom names");
338
339 /* use full path to the environment */
340 snprintf(udev->devname, NAME_SIZE-1, "%s%s", udev_root, udev->name);
341
342 } else if (udev->type == 'n') {
343 /* look if we want to change the name of the netif */
344 if (strcmp(udev->name, udev->kernel_name) != 0) {
345 retval = rename_net_if(udev);
346 if (retval != 0)
347 goto exit;
348
349 /* we've changed the name, now fake the devpath,
350 * cause original kernel name sleeps with the fishes
351 * and we don't get any event from the kernel now
352 */
353 pos = strrchr(udev->devpath, '/');
354 if (pos != NULL) {
355 pos[1] = '\0';
356 strfieldcat(udev->devpath, udev->name);
357 setenv("DEVPATH", udev->devpath, 1);
358 }
359
360 /* use netif name for the environment */
361 strfieldcpy(udev->devname, udev->name);
362 }
363 }
364
365 exit:
366 selinux_restore();
367
368 return retval;
369 }