chiark / gitweb /
udev_selinux.c: include udev.h
[elogind.git] / udev_add.c
1 /*
2  * udev-add.c
3  *
4  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  *      This program is free software; you can redistribute it and/or modify it
8  *      under the terms of the GNU General Public License as published by the
9  *      Free Software Foundation version 2 of the License.
10  * 
11  *      This program 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  *      General Public License for more details.
15  * 
16  *      You should have received a copy of the GNU General Public License along
17  *      with this program; if not, write to the Free Software Foundation, Inc.,
18  *      675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <grp.h>
31 #include <net/if.h>
32 #include <sys/socket.h>
33 #include <sys/ioctl.h>
34 #include <linux/sockios.h>
35
36 #include "libsysfs/sysfs/libsysfs.h"
37 #include "udev_libc_wrapper.h"
38 #include "udev.h"
39 #include "udev_utils.h"
40 #include "udev_version.h"
41 #include "logging.h"
42 #include "udev_rules.h"
43 #include "udev_selinux.h"
44
45
46 int udev_make_node(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
47 {
48         struct stat stats;
49         int retval = 0;
50
51         switch (udev->type) {
52         case DEV_BLOCK:
53                 mode |= S_IFBLK;
54                 break;
55         case DEV_CLASS:
56                 mode |= S_IFCHR;
57                 break;
58         default:
59                 dbg("unknown node type %c\n", udev->type);
60                 return -EINVAL;
61         }
62
63         if (stat(file, &stats) != 0)
64                 goto create;
65
66         /* preserve node with already correct numbers, to not change the inode number */
67         if ((stats.st_mode & S_IFMT) == (mode & S_IFMT) && (stats.st_rdev == devt)) {
68                 info("preserve file '%s', cause it has correct dev_t", file);
69                 selinux_setfilecon(file, udev->kernel_name, stats.st_mode);
70                 goto perms;
71         }
72
73         if (unlink(file) != 0)
74                 err("unlink(%s) failed: %s", file, strerror(errno));
75         else
76                 dbg("already present file '%s' unlinked", file);
77
78 create:
79         selinux_setfscreatecon(file, udev->kernel_name, mode);
80         retval = mknod(file, mode, devt);
81         selinux_resetfscreatecon();
82         if (retval != 0) {
83                 err("mknod(%s, %#o, %u, %u) failed: %s",
84                     file, mode, major(devt), minor(devt), strerror(errno));
85                 goto exit;
86         }
87
88 perms:
89         dbg("chmod(%s, %#o)", file, mode);
90         if (chmod(file, mode) != 0) {
91                 err("chmod(%s, %#o) failed: %s", file, mode, strerror(errno));
92                 goto exit;
93         }
94
95         if (uid != 0 || gid != 0) {
96                 dbg("chown(%s, %u, %u)", file, uid, gid);
97                 if (chown(file, uid, gid) != 0) {
98                         err("chown(%s, %u, %u) failed: %s",
99                             file, uid, gid, strerror(errno));
100                         goto exit;
101                 }
102         }
103
104 exit:
105         return retval;
106 }
107
108 static int create_node(struct udevice *udev, struct sysfs_class_device *class_dev)
109 {
110         char filename[PATH_SIZE];
111         struct name_entry *name_loop;
112         uid_t uid;
113         gid_t gid;
114         int tail;
115         int i;
116
117         snprintf(filename, sizeof(filename), "%s/%s", udev_root, udev->name);
118         filename[sizeof(filename)-1] = '\0';
119
120         /* create parent directories if needed */
121         if (strchr(udev->name, '/'))
122                 create_path(filename);
123
124         if (strcmp(udev->owner, "root") == 0)
125                 uid = 0;
126         else {
127                 char *endptr;
128                 unsigned long id;
129
130                 id = strtoul(udev->owner, &endptr, 10);
131                 if (endptr[0] == '\0')
132                         uid = (uid_t) id;
133                 else
134                         uid = lookup_user(udev->owner);
135         }
136
137         if (strcmp(udev->group, "root") == 0)
138                 gid = 0;
139         else {
140                 char *endptr;
141                 unsigned long id;
142
143                 id = strtoul(udev->group, &endptr, 10);
144                 if (endptr[0] == '\0')
145                         gid = (gid_t) id;
146                 else
147                         gid = lookup_group(udev->group);
148         }
149
150         if (!udev->test_run) {
151                 info("creating device node '%s'", filename);
152                 if (udev_make_node(udev, filename, udev->devt, udev->mode, uid, gid) != 0)
153                         goto error;
154                 setenv("DEVNAME", filename, 1);
155         } else {
156                 info("creating device node '%s', major = '%d', minor = '%d', "
157                      "mode = '%#o', uid = '%d', gid = '%d'", filename,
158                      major(udev->devt), minor(udev->devt), udev->mode, uid, gid);
159         }
160
161         /* create all_partitions if requested */
162         if (udev->partitions) {
163                 char partitionname[PATH_SIZE];
164                 struct sysfs_attribute *attr;
165                 int range;
166
167                 /* take the maximum registered minor range */
168                 attr = sysfs_get_classdev_attr(class_dev, "range");
169                 if (attr) {
170                         range = atoi(attr->value);
171                         if (range > 1)
172                                 udev->partitions = range-1;
173                 }
174                 info("creating device partition nodes '%s[1-%i]'", filename, udev->partitions);
175                 if (!udev->test_run) {
176                         for (i = 1; i <= udev->partitions; i++) {
177                                 dev_t part_devt;
178
179                                 snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
180                                 partitionname[sizeof(partitionname)-1] = '\0';
181                                 part_devt = makedev(major(udev->devt), minor(udev->devt) + i);
182                                 udev_make_node(udev, partitionname, part_devt, udev->mode, uid, gid);
183                         }
184                 }
185         }
186
187         /* create symlink(s) if requested */
188         if (!list_empty(&udev->symlink_list)) {
189                 char symlinks[512] = "";
190
191                 list_for_each_entry(name_loop, &udev->symlink_list, node) {
192                         int retval;
193                         char linktarget[PATH_SIZE];
194
195                         snprintf(filename, sizeof(filename), "%s/%s", udev_root, name_loop->name);
196                         filename[sizeof(filename)-1] = '\0';
197
198                         dbg("symlink '%s' to node '%s' requested", filename, udev->name);
199                         if (!udev->test_run)
200                                 if (strchr(filename, '/'))
201                                         create_path(filename);
202
203                         /* optimize relative link */
204                         linktarget[0] = '\0';
205                         i = 0;
206                         tail = 0;
207                         while (udev->name[i] && (udev->name[i] == name_loop->name[i])) {
208                                 if (udev->name[i] == '/')
209                                         tail = i+1;
210                                 i++;
211                         }
212                         while (name_loop->name[i] != '\0') {
213                                 if (name_loop->name[i] == '/')
214                                         strlcat(linktarget, "../", sizeof(linktarget));
215                                 i++;
216                         }
217
218                         strlcat(linktarget, &udev->name[tail], sizeof(linktarget));
219
220                         info("creating symlink '%s' to '%s'", filename, linktarget);
221                         if (!udev->test_run) {
222                                 unlink(filename);
223                                 selinux_setfscreatecon(filename, NULL, S_IFLNK);
224                                 retval = symlink(linktarget, filename);
225                                 selinux_resetfscreatecon();
226                                 if (retval != 0)
227                                         err("symlink(%s, %s) failed: %s",
228                                             linktarget, filename, strerror(errno));
229                         }
230
231                         strlcat(symlinks, filename, sizeof(symlinks));
232                         strlcat(symlinks, " ", sizeof(symlinks));
233                 }
234
235                 remove_trailing_chars(symlinks, ' ');
236                 setenv("DEVLINKS", symlinks, 1);
237         }
238
239         return 0;
240 error:
241         return -1;
242 }
243
244 static int rename_net_if(struct udevice *udev)
245 {
246         int sk;
247         struct ifreq ifr;
248         int retval;
249
250         info("changing net interface name from '%s' to '%s'", udev->kernel_name, udev->name);
251         if (udev->test_run)
252                 return 0;
253
254         sk = socket(PF_INET, SOCK_DGRAM, 0);
255         if (sk < 0) {
256                 err("error opening socket: %s", strerror(errno));
257                 return -1;
258         }
259
260         memset(&ifr, 0x00, sizeof(struct ifreq));
261         strlcpy(ifr.ifr_name, udev->kernel_name, IFNAMSIZ);
262         strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
263
264         retval = ioctl(sk, SIOCSIFNAME, &ifr);
265         if (retval != 0)
266                 err("error changing net interface name: %s", strerror(errno));
267         close(sk);
268
269         return retval;
270 }
271
272 int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
273 {
274         char *pos;
275         int retval = 0;
276
277         dbg("adding name='%s'", udev->name);
278         selinux_init();
279
280         if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS) {
281                 retval = create_node(udev, class_dev);
282                 if (retval != 0)
283                         goto exit;
284
285                 if (udev_db_add_device(udev) != 0)
286                         dbg("udev_db_add_dev failed, remove might not work for custom names");
287         } else if (udev->type == DEV_NET) {
288                 /* look if we want to change the name of the netif */
289                 if (strcmp(udev->name, udev->kernel_name) != 0) {
290                         retval = rename_net_if(udev);
291                         if (retval != 0)
292                                 goto exit;
293
294                         info("renamed netif to '%s'", udev->name);
295                         /* we've changed the name, now fake the devpath, cause the
296                          * original kernel name sleeps with the fishes and we don't
297                          * get an event from the kernel with the new name
298                          */
299                         pos = strrchr(udev->devpath, '/');
300                         if (pos != NULL) {
301                                 pos[1] = '\0';
302                                 strlcat(udev->devpath, udev->name, sizeof(udev->devpath));
303                                 strlcpy(udev->kernel_name, udev->name, sizeof(udev->kernel_name));
304                                 setenv("DEVPATH", udev->devpath, 1);
305                                 setenv("INTERFACE", udev->name, 1);
306                         }
307                 }
308         }
309
310 exit:
311         selinux_exit();
312         return retval;
313 }