Sun, 17 Mar 2013 19:19:57 +0100
some fixes
54 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | #include <unistd.h> | |
32 | #include <sys/types.h> | |
33 | ||
34 | #include "../util/pool.h" | |
35 | #include "vfs.h" | |
36 | ||
37 | static VFS_IO sys_file_io = { | |
38 | sys_file_read, | |
39 | sys_file_write, | |
40 | sys_file_close | |
41 | }; | |
42 | ||
55
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
43 | static VFS_DIRIO sys_dir_io = { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
44 | sys_dir_read, |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
45 | sys_dir_close |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
46 | }; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
47 | |
54 | 48 | VFSContext* vfs_request_context(Session *sn, Request *rq) { |
49 | VFSContext *ctx = pool_malloc(sn->pool, sizeof(VFSContext)); | |
50 | ctx->sn = sn; | |
51 | ctx->rq = rq; | |
52 | ctx->vfs = rq->vfs; | |
53 | ctx->user = acllist_getuser(sn, rq, rq->acllist); | |
54 | ctx->acllist = rq->acllist; | |
55 | ctx->aclreqaccess = rq->aclreqaccess; | |
56 | ctx->pool = sn->pool; | |
57 | ctx->vfs_errno = 0; | |
58 | return ctx; | |
59 | } | |
60 | ||
61 | SYS_FILE vfs_open(VFSContext *ctx, char *path, int oflags) { | |
62 | Session *sn; | |
63 | Request *rq; | |
64 | pool_handle_t *pool; | |
65 | uint32_t access_mask; | |
66 | ||
67 | if(ctx) { | |
68 | access_mask = ctx->aclreqaccess; | |
69 | access_mask |= acl_oflag2mask(oflags); | |
70 | if(!ctx->pool) { | |
71 | // TODO: log warning | |
72 | // broken VFSContext | |
73 | } | |
74 | if(ctx->vfs) { | |
75 | // ctx->aclreqaccess should be the complete access mask | |
76 | uint32_t m = ctx->aclreqaccess; // save original access mask | |
77 | ctx->aclreqaccess = access_mask; // set mask for vfs->open call | |
78 | SYS_FILE file = ctx->vfs->open(ctx, path, oflags); | |
79 | ctx->aclreqaccess = m; // restore original access mask | |
80 | return file; | |
81 | } else { | |
82 | pool = ctx->pool; | |
83 | } | |
84 | } else { | |
85 | sn = NULL; | |
86 | rq = NULL; | |
87 | pool = NULL; | |
88 | access_mask = acl_oflag2mask(oflags); | |
89 | } | |
90 | ||
91 | // check ACLs | |
92 | uid_t uid; // uid and gid will be initialized by sys_acl_check | |
93 | gid_t gid; | |
94 | if(sys_acl_check(ctx, access_mask, &uid, &gid)) { | |
95 | return NULL; | |
96 | } | |
97 | ||
98 | // open file | |
99 | int fd = open(path, oflags); | |
100 | if(fd == -1) { | |
101 | if(ctx) { | |
102 | ctx->vfs_errno = errno; | |
103 | sys_set_error_status(ctx); | |
104 | } | |
105 | return NULL; | |
106 | } | |
107 | ||
108 | VFSFile *file = pool ? | |
109 | pool_malloc(pool, sizeof(VFSFile)) : malloc(sizeof(VFSFile)); | |
110 | if(!file) { | |
55
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
111 | close(fd); |
54 | 112 | return NULL; |
113 | } | |
114 | file->ctx = ctx; | |
115 | file->data = NULL; | |
116 | file->fd = fd; | |
117 | file->io = &sys_file_io; | |
118 | return file; | |
119 | } | |
120 | ||
121 | SYS_FILE vfs_openRO(VFSContext *ctx, char *path) { | |
122 | return vfs_open(ctx, path, O_RDONLY); | |
123 | } | |
124 | ||
125 | SYS_FILE vfs_openWO(VFSContext *ctx, char *path) { | |
56
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
126 | return vfs_open(ctx, path, O_WRONLY | O_CREAT); |
54 | 127 | } |
128 | ||
129 | SYS_FILE vfs_openRW(VFSContext *ctx, char *path) { | |
56
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
130 | return vfs_open(ctx, path, O_RDONLY | O_WRONLY | O_CREAT); |
54 | 131 | } |
132 | ||
133 | int vfs_stat(VFSContext *ctx, char *path, struct stat *buf) { | |
134 | Session *sn; | |
135 | Request *rq; | |
136 | uint32_t access_mask; | |
137 | ||
138 | if(ctx) { | |
139 | access_mask = ctx->aclreqaccess; | |
140 | access_mask |= ACL_READ_ATTRIBUTES; | |
141 | if(!ctx->pool) { | |
142 | // TODO: log warning | |
143 | // broken VFSContext | |
144 | } | |
145 | if(ctx->vfs) { | |
146 | // ctx->aclreqaccess should be the complete access mask | |
147 | uint32_t m = ctx->aclreqaccess; // save original access mask | |
148 | ctx->aclreqaccess = access_mask; // set mask for vfs->fstat call | |
149 | int ret = ctx->vfs->stat(ctx, path, buf); | |
150 | ctx->aclreqaccess = m; // restore original access mask | |
151 | return ret; | |
152 | } | |
153 | } else { | |
154 | sn = NULL; | |
155 | rq = NULL; | |
156 | access_mask = ACL_READ_ATTRIBUTES; | |
157 | } | |
158 | ||
159 | // check ACLs | |
160 | uid_t uid; // uid and gid will be initialized by sys_acl_check | |
161 | gid_t gid; | |
162 | if(sys_acl_check(ctx, access_mask, &uid, &gid)) { | |
57 | 163 | return -1; |
54 | 164 | } |
165 | ||
166 | // stat | |
167 | if(stat(path, buf)) { | |
168 | if(ctx) { | |
169 | ctx->vfs_errno = errno; | |
170 | sys_set_error_status(ctx); | |
171 | } | |
172 | return -1; | |
173 | } | |
174 | ||
175 | return 0; | |
176 | } | |
177 | ||
178 | int vfs_fstat(VFSContext *ctx, SYS_FILE fd, struct stat *buf) { | |
179 | if(ctx) { | |
180 | if(!ctx->pool) { | |
181 | // TODO: log warning | |
182 | // broken VFSContext | |
183 | } | |
184 | if(ctx->vfs) { | |
185 | return ctx->vfs->fstat(ctx, fd, buf); | |
186 | } | |
187 | } | |
188 | ||
189 | // stat | |
190 | if(fstat(fd->fd, buf)) { | |
191 | if(ctx) { | |
192 | ctx->vfs_errno = errno; | |
193 | } | |
194 | return -1; | |
195 | } | |
196 | ||
197 | return 0; | |
198 | } | |
199 | ||
200 | void vfs_close(SYS_FILE fd) { | |
201 | fd->io->close(fd); | |
202 | if(fd->ctx) { | |
203 | pool_free(fd->ctx->pool, fd); | |
204 | } else { | |
205 | free(fd); | |
206 | } | |
207 | } | |
208 | ||
55
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
209 | VFS_DIR vfs_opendir(VFSContext *ctx, char *path) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
210 | Session *sn; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
211 | Request *rq; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
212 | pool_handle_t *pool; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
213 | uint32_t access_mask; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
214 | |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
215 | if(ctx) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
216 | access_mask = ctx->aclreqaccess; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
217 | access_mask |= ACL_LIST; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
218 | if(!ctx->pool) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
219 | // TODO: log warning |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
220 | // broken VFSContext |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
221 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
222 | if(ctx->vfs) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
223 | // ctx->aclreqaccess should be the complete access mask |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
224 | uint32_t m = ctx->aclreqaccess; // save original access mask |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
225 | ctx->aclreqaccess = access_mask; // set mask for vfs->opendir call |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
226 | VFS_DIR dir = ctx->vfs->opendir(ctx, path); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
227 | ctx->aclreqaccess = m; // restore original access mask |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
228 | return dir; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
229 | } else { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
230 | pool = ctx->pool; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
231 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
232 | } else { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
233 | sn = NULL; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
234 | rq = NULL; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
235 | pool = NULL; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
236 | access_mask = ACL_LIST; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
237 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
238 | |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
239 | // check ACLs |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
240 | uid_t uid; // uid and gid will be initialized by sys_acl_check |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
241 | gid_t gid; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
242 | if(sys_acl_check(ctx, access_mask, &uid, &gid)) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
243 | return NULL; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
244 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
245 | |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
246 | // open file |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
247 | int sys_fd = open(path, O_RDONLY); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
248 | if(sys_fd == -1) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
249 | if(ctx) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
250 | ctx->vfs_errno = errno; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
251 | sys_set_error_status(ctx); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
252 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
253 | return NULL; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
254 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
255 | |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
256 | DIR *sys_dir = fdopendir(sys_fd); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
257 | if(!sys_dir) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
258 | if(ctx) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
259 | ctx->vfs_errno = errno; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
260 | sys_set_error_status(ctx); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
261 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
262 | return NULL; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
263 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
264 | |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
265 | VFSDir *dir = pool ? |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
266 | pool_malloc(pool, sizeof(VFSDir)) : malloc(sizeof(VFSDir)); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
267 | if(!dir) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
268 | closedir(sys_dir); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
269 | return NULL; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
270 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
271 | dir->ctx = ctx; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
272 | dir->data = sys_dir; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
273 | dir->fd = sys_fd; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
274 | dir->io = &sys_dir_io; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
275 | return dir; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
276 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
277 | |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
278 | int vfs_readdir(VFS_DIR dir, VFS_ENTRY *entry) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
279 | return dir->io->readdir(dir, entry, 0); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
280 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
281 | |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
282 | int vfs_readdir_stat(VFS_DIR dir, VFS_ENTRY *entry) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
283 | return dir->io->readdir(dir, entry, 1); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
284 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
285 | |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
286 | void vfs_closedir(VFS_DIR dir) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
287 | dir->io->close(dir); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
288 | if(dir->ctx) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
289 | pool_free(dir->ctx->pool, dir); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
290 | } else { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
291 | free(dir); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
292 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
293 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
294 | |
56
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
295 | int vfs_mkdir(VFSContext *ctx, char *path) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
296 | if(ctx && ctx->vfs) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
297 | return vfs_path_op(ctx, path, ctx->vfs->mkdir, ACL_ADD_FILE); |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
298 | } else { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
299 | return vfs_path_op(ctx, path, sys_mkdir, ACL_ADD_FILE); |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
300 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
301 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
302 | |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
303 | int vfs_unlink(VFSContext *ctx, char *path) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
304 | if(ctx && ctx->vfs) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
305 | return vfs_path_op(ctx, path, ctx->vfs->unlink, ACL_DELETE); |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
306 | } else { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
307 | return vfs_path_op(ctx, path, sys_unlink, ACL_DELETE); |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
308 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
309 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
310 | |
54 | 311 | |
312 | // private | |
56
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
313 | int vfs_path_op(VFSContext *ctx, char *path, vfs_op_f op, uint32_t access) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
314 | Session *sn; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
315 | Request *rq; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
316 | uint32_t access_mask; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
317 | |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
318 | if(ctx) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
319 | access_mask = ctx->aclreqaccess; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
320 | access_mask |= access; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
321 | if(!ctx->pool) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
322 | // TODO: log warning |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
323 | // broken VFSContext |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
324 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
325 | if(ctx->vfs) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
326 | // ctx->aclreqaccess should be the complete access mask |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
327 | uint32_t m = ctx->aclreqaccess; // save original access mask |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
328 | ctx->aclreqaccess = access_mask; // set mask for vfs->fstat call |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
329 | int ret = op(ctx, path); |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
330 | ctx->aclreqaccess = m; // restore original access mask |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
331 | return ret; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
332 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
333 | } else { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
334 | sn = NULL; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
335 | rq = NULL; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
336 | access_mask = access; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
337 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
338 | |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
339 | // check ACLs |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
340 | uid_t uid; // uid and gid will be initialized by sys_acl_check |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
341 | gid_t gid; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
342 | if(sys_acl_check(ctx, access_mask, &uid, &gid)) { |
57 | 343 | return -1; |
56
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
344 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
345 | |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
346 | // do path operation |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
347 | if(op(ctx, path)) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
348 | // error |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
349 | if(ctx) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
350 | ctx->vfs_errno = errno; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
351 | sys_set_error_status(ctx); |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
352 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
353 | return -1; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
354 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
355 | |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
356 | return 0; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
357 | } |
54 | 358 | |
359 | int sys_acl_check(VFSContext *ctx, uint32_t acm, uid_t *uid, gid_t *gid) { | |
360 | /* | |
361 | * we don't allow remote root access, so a uid of 0 means that | |
362 | * no file system acl check is needed | |
363 | */ | |
364 | *uid = 0; | |
365 | *gid = 0; | |
366 | if(!ctx) { | |
367 | return 0; | |
368 | } | |
369 | ||
370 | ACLListHandle *acllist = ctx->acllist; | |
371 | if(acllist) { | |
372 | ACLListElm *elm = acllist->listhead; | |
373 | while(elm) { | |
374 | ACLList *acl = elm->acl; | |
375 | if(acl->isextern) { | |
376 | // TODO | |
377 | } else if(!acl->check(acl, ctx->user, acm)) { | |
378 | // access denied | |
379 | if(ctx->sn && ctx->rq) { | |
380 | acl_set_error_status(ctx->sn, ctx->rq, acl, ctx->user); | |
381 | } | |
382 | return 1; | |
383 | } | |
384 | elm = elm->next; | |
385 | } | |
386 | } | |
387 | ||
388 | return 0; | |
389 | } | |
390 | ||
391 | void sys_set_error_status(VFSContext *ctx) { | |
392 | if(ctx->sn && ctx->rq) { | |
56
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
393 | int status = util_errno2status(ctx->vfs_errno); |
54 | 394 | protocol_status(ctx->sn, ctx->rq, status, NULL); |
395 | } | |
396 | } | |
397 | ||
398 | ssize_t sys_file_read(SYS_FILE fd, void *buf, size_t nbyte) { | |
399 | return read(fd->fd, buf, nbyte); | |
400 | } | |
401 | ||
402 | ssize_t sys_file_write(SYS_FILE fd, const void *buf, size_t nbyte) { | |
403 | return write(fd->fd, buf, nbyte); | |
404 | } | |
405 | ||
406 | void sys_file_close(SYS_FILE fd) { | |
407 | close(fd->fd); | |
408 | } | |
55
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
409 | |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
410 | int sys_dir_read(VFS_DIR dir, VFS_ENTRY *entry, int getstat) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
411 | struct dirent *e = readdir(dir->data); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
412 | if(e) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
413 | entry->name = e->d_name; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
414 | if(getstat) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
415 | // TODO: check ACLs again for new path |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
416 | if(fstatat(dir->fd, e->d_name, &entry->stat, 0)) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
417 | entry->stat_errno = errno; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
418 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
419 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
420 | return 1; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
421 | } else { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
422 | return 0; |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
423 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
424 | } |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
425 | |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
426 | void sys_dir_close(VFS_DIR dir) { |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
427 | closedir(dir->data); |
b7908bf38f9f
vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
428 | } |
56
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
429 | |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
430 | int sys_mkdir(VFSContext *ctx, char *path) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
431 | mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
432 | return mkdir(path, mode); |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
433 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
434 | |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
435 | int sys_unlink(VFSContext *ctx, char *path) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
436 | return unlink(path); |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
437 | } |