chiark / gitweb /
selinux: more context settings
[elogind.git] / udev_utils_file.c
1 /*
2  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
3  *
4  *      This program is free software; you can redistribute it and/or modify it
5  *      under the terms of the GNU General Public License as published by the
6  *      Free Software Foundation version 2 of the License.
7  * 
8  *      This program is distributed in the hope that it will be useful, but
9  *      WITHOUT ANY WARRANTY; without even the implied warranty of
10  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  *      General Public License for more details.
12  * 
13  *      You should have received a copy of the GNU General Public License along
14  *      with this program; if not, write to the Free Software Foundation, Inc.,
15  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16  *
17  */
18
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <dirent.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30
31 #include "udev.h"
32
33 int create_path(const char *path)
34 {
35         char p[PATH_SIZE];
36         char *pos;
37         struct stat stats;
38         int ret;
39
40         strlcpy(p, path, sizeof(p));
41         pos = strrchr(p, '/');
42         if (pos == p || pos == NULL)
43                 return 0;
44
45         while (pos[-1] == '/')
46                 pos--;
47         pos[0] = '\0';
48
49         dbg("stat '%s'", p);
50         if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
51                 return 0;
52
53         if (create_path(p) != 0)
54                 return -1;
55
56         dbg("mkdir '%s'", p);
57         selinux_setfscreatecon(p, NULL, S_IFDIR|0755);
58         ret = mkdir(p, 0755);
59         selinux_resetfscreatecon();
60         if (ret == 0)
61                 return 0;
62
63         if (errno == EEXIST)
64                 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
65                         return 0;
66         return -1;
67 }
68
69 int delete_path(const char *path)
70 {
71         char p[PATH_SIZE];
72         char *pos;
73         int retval;
74
75         strcpy (p, path);
76         pos = strrchr(p, '/');
77         if (pos == p || pos == NULL)
78                 return 0;
79
80         while (1) {
81                 *pos = '\0';
82                 pos = strrchr(p, '/');
83
84                 /* don't remove the last one */
85                 if ((pos == p) || (pos == NULL))
86                         break;
87
88                 /* remove if empty */
89                 retval = rmdir(p);
90                 if (errno == ENOENT)
91                         retval = 0;
92                 if (retval) {
93                         if (errno == ENOTEMPTY)
94                                 return 0;
95                         err("rmdir(%s) failed: %s", p, strerror(errno));
96                         break;
97                 }
98                 dbg("removed '%s'", p);
99         }
100         return 0;
101 }
102
103 /* Reset permissions on the device node, before unlinking it to make sure,
104  * that permisions of possible hard links will be removed too.
105  */
106 int unlink_secure(const char *filename)
107 {
108         int retval;
109
110         retval = chown(filename, 0, 0);
111         if (retval)
112                 err("chown(%s, 0, 0) failed: %s", filename, strerror(errno));
113
114         retval = chmod(filename, 0000);
115         if (retval)
116                 err("chmod(%s, 0000) failed: %s", filename, strerror(errno));
117
118         retval = unlink(filename);
119         if (errno == ENOENT)
120                 retval = 0;
121
122         if (retval)
123                 err("unlink(%s) failed: %s", filename, strerror(errno));
124
125         return retval;
126 }
127
128 int file_map(const char *filename, char **buf, size_t *bufsize)
129 {
130         struct stat stats;
131         int fd;
132
133         fd = open(filename, O_RDONLY);
134         if (fd < 0) {
135                 return -1;
136         }
137
138         if (fstat(fd, &stats) < 0) {
139                 close(fd);
140                 return -1;
141         }
142
143         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
144         if (*buf == MAP_FAILED) {
145                 close(fd);
146                 return -1;
147         }
148         *bufsize = stats.st_size;
149
150         close(fd);
151
152         return 0;
153 }
154
155 void file_unmap(void *buf, size_t bufsize)
156 {
157         munmap(buf, bufsize);
158 }
159
160 /* return number of chars until the next newline, skip escaped newline */
161 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
162 {
163         int escape = 0;
164         size_t count;
165
166         for (count = cur; count < buflen; count++) {
167                 if (!escape && buf[count] == '\n')
168                         break;
169
170                 if (buf[count] == '\\')
171                         escape = 1;
172                 else
173                         escape = 0;
174         }
175
176         return count - cur;
177 }