This repository has been archived by the owner on Jan 10, 2019. It is now read-only.
forked from CTU-OSP/mc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfs.c
447 lines (373 loc) · 8.81 KB
/
sfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* Single File fileSystem
*
* Copyright 1998 Pavel Machek, distribute under GPL
*
* $Id$
*
* This defines whole class of filesystems which contain single file
* inside. It is somehow similar to extfs, except that extfs makes
* whole virtual trees and we do only single virtual files.
*
* If you want to gunzip something, you should open it with #ugz
* suffix, DON'T try to gunzip it yourself.
*
* Namespace: exports vfs_sfs_ops */
#include <config.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include "utilvfs.h"
#include "vfs.h"
#include "local.h"
struct cachedfile {
char *name, *cache;
uid_t uid;
struct cachedfile *next;
};
static struct cachedfile *head;
#define MAXFS 32
static int sfs_no = 0;
static char *sfs_prefix[ MAXFS ];
static char *sfs_command[ MAXFS ];
static int sfs_flags[ MAXFS ];
#define F_1 1
#define F_2 2
#define F_NOLOCALCOPY 4
#define F_FULLMATCH 8
static int uptodate (char *name, char *cache)
{
return 1;
}
static int vfmake (vfs *me, char *name, char *cache)
{
char *inpath, *op;
int w;
char pad [10240];
char *s, *t = pad;
int was_percent = 0;
vfs_split (name, &inpath, &op);
if ((w = (*me->which) (me, op)) == -1)
vfs_die ("This cannot happen... Hopefully.\n");
if ((sfs_flags[w] & F_1) || (!strcmp (name, "/"))) ; else return -1;
/* if ((sfs_flags[w] & F_2) || (!inpath) || (!*inpath)); else return -1; */
if (!(sfs_flags[w] & F_NOLOCALCOPY)) {
s = mc_getlocalcopy (name);
if (!s)
return -1;
name = name_quote (s, 0);
g_free (s);
} else
name = name_quote (name, 0);
#define COPY_CHAR if (t-pad>sizeof(pad)) { g_free (name); return -1; } else *t++ = *s;
#define COPY_STRING(a) if ((t-pad)+strlen(a)>sizeof(pad)) { g_free (name); return -1; } else { strcpy (t, a); t+= strlen(a); }
for (s = sfs_command[w]; *s; s++) {
if (was_percent) {
char *ptr = NULL;
was_percent = 0;
switch (*s) {
case '1': ptr = name; break;
case '2': ptr = op + strlen (sfs_prefix[w]); break;
case '3': ptr = cache; break;
case '%': COPY_CHAR; continue;
}
COPY_STRING (ptr);
} else {
if (*s == '%')
was_percent = 1;
else
COPY_CHAR;
}
}
g_free (name);
if (my_system (EXECUTE_AS_SHELL | EXECUTE_SETUID | EXECUTE_WAIT, "/bin/sh", pad)) {
return -1;
}
return 0; /* OK */
}
static char *
redirect (vfs *me, char *name)
{
struct cachedfile *cur = head;
uid_t uid = vfs_uid;
char *cache;
int handle;
while (cur){
if ((!strcmp (name, cur->name)) &&
(uid == cur->uid) &&
(uptodate (cur->name, cur->cache)))
/* FIXME: when not uptodate, we might want to kill cache
* file immediately, not to wait until timeout. */ {
vfs_stamp (&vfs_sfs_ops, cur);
return cur->cache;
}
cur = cur->next;
}
handle = mc_mkstemps (&cache, "sfs", NULL);
if (handle == -1) {
return "/SOMEONE_PLAYING_DIRTY_TMP_TRICKS_ON_US";
}
close (handle);
if (!vfmake (me, name, cache)){
cur = g_new (struct cachedfile, 1);
cur->name = g_strdup (name);
cur->cache = cache;
cur->uid = uid;
cur->next = head;
head = cur;
vfs_add_noncurrent_stamps (&vfs_sfs_ops, (vfsid) head, NULL);
vfs_rm_parents (NULL);
return cache;
}
unlink (cache);
g_free (cache);
return "/I_MUST_NOT_EXIST";
}
static void *
sfs_open (vfs *me, char *path, int flags, int mode)
{
int *sfs_info;
int fd;
path = redirect (me, path);
fd = open (path, flags, mode);
if (fd == -1)
return 0;
sfs_info = g_new (int, 1);
*sfs_info = fd;
return sfs_info;
}
static int sfs_stat (vfs *me, char *path, struct stat *buf)
{
path = redirect (me, path);
return stat (path, buf);
}
static int sfs_lstat (vfs *me, char *path, struct stat *buf)
{
path = redirect (me, path);
#ifndef HAVE_STATLSTAT
return lstat (path, buf);
#else
return statlstat (path, buf);
#endif
}
static int sfs_chmod (vfs *me, char *path, int mode)
{
path = redirect (me, path);
return chmod (path, mode);
}
static int sfs_chown (vfs *me, char *path, int owner, int group)
{
path = redirect (me, path);
return chown (path, owner, group);
}
static int sfs_utime (vfs *me, char *path, struct utimbuf *times)
{
path = redirect (me, path);
return utime (path, times);
}
static int sfs_readlink (vfs *me, char *path, char *buf, int size)
{
path = redirect (me, path);
return readlink (path, buf, size);
}
static vfsid sfs_getid (vfs *me, char *path, struct vfs_stamping **parent)
{ /* FIXME: what should I do? */
vfs *v;
vfsid id;
struct vfs_stamping *par;
struct cachedfile *cur = head;
while (cur) {
if ((!strcmp( path, cur->name )) &&
(vfs_uid == cur->uid))
break;
cur = cur->next;
}
*parent = NULL;
if (!cur)
return (vfsid)(-1);
{
char *path2 = g_strdup (path);
v = vfs_split (path2, NULL, NULL); /* Strip suffix which led to this being sfs */
v = vfs_split (path2, NULL, NULL); /* ... and learn whoever was the parent system */
id = (*v->getid) (v, path2, &par);
g_free (path2);
}
if (id != (vfsid)-1) {
*parent = g_new (struct vfs_stamping, 1);
(*parent)->v = v;
(*parent)->id = id;
(*parent)->parent = par;
(*parent)->next = NULL;
}
return (vfsid) cur;
}
static void sfs_free (vfsid id)
{
struct cachedfile *which = (struct cachedfile *) id;
struct cachedfile *cur, *prev;
for (cur = head, prev = 0; cur && cur != which; prev = cur, cur = cur->next)
;
if (!cur)
vfs_die( "Free of thing which is unknown to me\n" );
unlink (cur->cache);
if (prev)
prev->next = cur->next;
else
head = cur->next;
}
static void sfs_fill_names (vfs *me, void (*func)(char *))
{
struct cachedfile *cur = head;
while (cur){
(*func)(cur->name);
cur = cur->next;
}
}
static int sfs_nothingisopen (vfsid id)
{
/* FIXME: Investigate whether have to guard this like in
the other VFSs (see fd_usage in extfs) -- Norbert */
return 1;
}
static char *sfs_getlocalcopy (vfs *me, char *path)
{
path = redirect (me, path);
return g_strdup (path);
}
static int sfs_ungetlocalcopy (vfs *me, char *path, char *local, int has_changed)
{
g_free(local);
return 0;
}
static int sfs_init (vfs *me)
{
char *mc_sfsini;
FILE *cfg;
mc_sfsini = concat_dir_and_file (mc_home, "extfs/sfs.ini");
cfg = fopen (mc_sfsini, "r");
if (!cfg){
fprintf (stderr, _("Warning: file %s not found\n"), mc_sfsini);
g_free (mc_sfsini);
return 0;
}
g_free (mc_sfsini);
sfs_no = 0;
while (sfs_no < MAXFS){
char key[256];
char *c, *semi = NULL, flags = 0;
if (!fgets (key, sizeof (key), cfg))
break;
if (*key == '#')
continue;
for (c = key; *c; c++)
if ((*c == ':') || (*c == '/')){
semi = c;
if (*c == '/'){
*c = 0;
flags |= F_FULLMATCH;
}
break;
}
if (!semi){
fprintf (stderr, _("Warning: Invalid line in sfs.ini:\n%s\n"), key);
continue;
}
c = semi + 1;
while ((*c != ' ') && (*c != '\t')) {
switch (*c) {
case '1': flags |= F_1; break;
case '2': flags |= F_2; break;
case 'R': flags |= F_NOLOCALCOPY; break;
default:
fprintf (stderr, _("Warning: Invalid flag %c in sfs.ini:\n%s\n"), *c, key);
}
c++;
}
c++;
*(semi+1) = 0;
if ((semi = strchr (c, '\n')))
*semi = 0;
sfs_prefix [sfs_no] = g_strdup (key);
sfs_command [sfs_no] = g_strdup (c);
sfs_flags [sfs_no] = flags;
sfs_no++;
}
fclose (cfg);
return 1;
}
static void
sfs_done (vfs *me)
{
int i;
for (i = 0; i < sfs_no; i++){
g_free (sfs_prefix [i]);
g_free (sfs_command [i]);
sfs_prefix [i] = sfs_command [i] = NULL;
}
sfs_no = 0;
}
static int
sfs_which (vfs *me, char *path)
{
int i;
for (i = 0; i < sfs_no; i++)
if (sfs_flags [i] & F_FULLMATCH) {
if (!strcmp (path, sfs_prefix [i]))
return i;
} else
if (!strncmp (path, sfs_prefix [i], strlen (sfs_prefix [i])))
return i;
return -1;
}
vfs vfs_sfs_ops = {
NULL, /* This is place of next pointer */
"sfs",
F_EXEC, /* flags */
NULL, /* prefix */
NULL, /* data */
0, /* errno */
sfs_init,
sfs_done,
sfs_fill_names,
sfs_which,
sfs_open,
local_close,
local_read,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
sfs_stat,
sfs_lstat,
local_fstat,
sfs_chmod,
sfs_chown,
sfs_utime,
sfs_readlink,
NULL,
NULL,
NULL,
NULL,
NULL,
local_errno,
local_lseek,
NULL,
sfs_getid,
sfs_nothingisopen,
sfs_free,
sfs_getlocalcopy,
sfs_ungetlocalcopy,
NULL,
NULL,
NULL,
NULL
#ifdef HAVE_MMAP
,local_mmap,
local_munmap
#endif
};