File: fakechroot.c

package info (click to toggle)
pure-ftpd 1.0.47-3
  • links: PTS
  • area: main
  • in suites: buster
  • size: 3,212 kB
  • sloc: ansic: 29,132; sh: 1,632; makefile: 500; perl: 280
file content (512 lines) | stat: -rw-r--r-- 11,437 bytes parent folder | download | duplicates (2)
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include <config.h>

#define FAKECHROOT_FUNCS_DEFINITION 1

#ifdef WITH_VIRTUAL_CHROOT

# include "ftpd.h"

# ifdef WITH_DMALLOC
#  include <dmalloc.h>
# endif

static char curdir[PATH_MAX];
static char *chroot_base;
static size_t chroot_len;

int fakechroot(const char *path)
{
    char *z;

    if (path == NULL || *path == 0) {
# ifdef EINVAL
        errno = EINVAL;
# endif
        return -1;
    }
    free(chroot_base);
    chroot_base = NULL;
    if (path[0] == '/' && path[1] == 0) {   /* chroot("/") => no chroot */
        return 0;
    }
    if ((chroot_base = strdup(path)) == NULL) {
        return -1;
    }
    simplify(chroot_base);
    z = chroot_base;
    while (*z != 0) {
        z++;
    }
    for (;;) {
        z--;
        if (z == chroot_base || *z != '/') {
            break;
        }
        *z = 0;
    }
    if ((chroot_len = strlen(chroot_base)) >= sizeof curdir) {
# ifdef ENAMETOOLONG
        errno = ENAMETOOLONG;
# endif
        return -1;
    }

    return 0;
}

char *fakegetcwd(char *dir, size_t size)
{
    char *curdirchr;
    size_t s;

    if (chroot_base == NULL) {
        return getcwd(dir, size);
    }
    if (strncmp(curdir, chroot_base, chroot_len) != 0) {
        abort();
    }
    curdirchr = curdir + chroot_len;
    s = strlen(curdirchr);
    if (s <= (size_t) 0U) {
# ifdef EFAULT
        errno = EFAULT;
# endif
        return NULL;
    }
    {
        char *sp = curdirchr + s - 1U;

        while (sp != curdirchr && *sp == '/') {
            *sp = 0;
            s--;
        }
    }
    s++;
    if (s > size || s < (size_t) 2U) {
# ifdef ENAMETOOLONG
        errno = ENAMETOOLONG;
# endif
        return NULL;
    }
    memcpy(dir, curdirchr, s);

    return curdirchr;
}

static int fakexlate(char *curdirtmp, size_t sizeof_curdirtmp, const char *dir)
{
    char *sl;
    size_t curdirlen;

    if ((curdirlen = strlen(curdir)) >= sizeof_curdirtmp) {
        return -1;
    }
    memcpy(curdirtmp, curdir, curdirlen + (size_t) 1U);
    simplify(curdirtmp);
    loop:
    if (dir[0] == '.' && dir[1] == '.' &&
        (dir[2] == 0 || dir[2] == '/')) {
        if ((sl = strrchr(curdirtmp, '/')) != NULL) {
            *sl = 0;
        } else {
            *curdirtmp = 0;
        }
        if (strncmp(curdirtmp, chroot_base, chroot_len) != 0 ||
            curdirtmp[chroot_len] != '/') {
            snprintf(curdirtmp, sizeof_curdirtmp, "%s/", chroot_base);
        }
        if (dir[0] == '.' && dir[1] == '.' && dir[2] == '/') {
            dir += 3;
            goto loop;
        }
    } else if (*dir == '/') {
        snprintf(curdirtmp, sizeof_curdirtmp, "%s/", chroot_base);
        dir++;
        goto loop;
    } else if (*dir != 0) {
        size_t dirlen;
        size_t curdirtmplen;

        if ((dir[0] == '.' && dir[1] == '.' &&
             (dir[2] == 0 || dir[2] == '/')) ||
            strstr(dir, "/../") != NULL) {
            perm:
# ifdef EPERM
            errno = EPERM;
# endif
            return -1;
        }
        dirlen = strlen(dir) + (size_t) 1U;
        if (dirlen >= (size_t) 4U &&
            (dir[dirlen - 2U] == '.' && dir[dirlen - 3U] == '.' &&
             dir[dirlen - 4U] == '/')) {
            goto perm;
        }
        curdirtmplen = strlen(curdirtmp);
        if (curdirtmplen + dirlen >= sizeof_curdirtmp) {
# ifdef ENAMETOOLONG
            errno = ENAMETOOLONG;
# endif
            return -1;
        }
        curdirtmp[curdirtmplen] = '/';
        memcpy(curdirtmp + curdirtmplen + 1U, dir, dirlen);
    }
    simplify(curdirtmp);

    return 0;
}

int fakechdir(const char *dir)
{
    char curdirtmp[PATH_MAX];
    size_t curdirtmplen;

    if (chroot_base == NULL) {
        return chdir(dir);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, dir) != 0) {
        return -1;
    }
    if (chdir(curdirtmp) != 0) {
        return -1;
    }
    if ((curdirtmplen = strlen(curdirtmp)) >= sizeof curdir) {
        return -1;
    }
    memcpy(curdir, curdirtmp, curdirtmplen + (size_t) 1U);

    return 0;
}

int fakestat(const char *file, struct stat *st)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return stat(file, st);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return -1;
    }
    return stat(curdirtmp, st);
}

int fakelstat(const char *file, struct stat *st)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return lstat(file, st);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return -1;
    }
    return lstat(curdirtmp, st);
}

FILE *fakefopen(const char *file, const char *mode)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return fopen(file, mode);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return NULL;
    }
    return fopen(curdirtmp, mode);
}

int fakeaccess(const char *file, mode_t mode)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return access(file, mode);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return -1;
    }
    return access(curdirtmp, mode);
}

int fakeunlink(const char *file)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return unlink(file);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return -1;
    }
    return unlink(curdirtmp);
}

DIR *fakeopendir(const char *file)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return opendir(file);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return NULL;
    }
    return opendir(curdirtmp);
}

int fakechmod(const char *file, mode_t mode)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return chmod(file, mode);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return -1;
    }
    return chmod(curdirtmp, mode);
}

int fakemkdir(const char *file, mode_t mode)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return mkdir(file, mode);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return -1;
    }
    return mkdir(curdirtmp, mode);
}

int fakermdir(const char *file)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return rmdir(file);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return -1;
    }
    return rmdir(curdirtmp);
}

# ifdef HAVE_UTIME
int fakeutime(const char *file, struct utimbuf *buf)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return utime(file, buf);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return -1;
    }
    return utime(curdirtmp, buf);
}
# endif

# ifdef HAVE_UTIMES
int fakeutimes(const char *file, struct timeval *buf)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return utimes(file, buf);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return -1;
    }
    return utimes(curdirtmp, buf);
}
# endif

int fakechown(const char *file, uid_t uid, gid_t gid)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return chown(file, uid, gid);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return -1;
    }
    return chown(file, uid, gid);
}

# ifdef HAVE_MKFIFO
int fakemkfifo(const char *file, mode_t mode)
{
     char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return mkfifo(file, mode);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return -1;
    }
    return mkfifo(file, mode);
}
# endif

# ifdef HAVE_MKNOD
int fakemknod(const char *file, mode_t mode, dev_t dev)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return mknod(file, mode, dev);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return -1;
    }
    return mknod(file, mode, dev);
}
# endif

int fakelink(const char *oldpath, const char *newpath)
{
    char curdirtmp[PATH_MAX];
    char curdirtmp2[PATH_MAX];

    if (chroot_base == NULL) {
        return link(oldpath, newpath);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, oldpath) != 0 ||
        fakexlate(curdirtmp2, sizeof curdirtmp2, newpath) != 0) {
        return -1;
    }
    return link(curdirtmp, curdirtmp2);
}

int fakesymlink(const char *oldpath, const char *newpath)
{
    char curdirtmp[PATH_MAX];
    char curdirtmp2[PATH_MAX];

    if (chroot_base == NULL) {
        return symlink(oldpath, newpath);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, oldpath) != 0 ||
        fakexlate(curdirtmp2, sizeof curdirtmp2, newpath) != 0) {
        return -1;
    }
    return symlink(curdirtmp, curdirtmp2);
}

int fakereadlink(const char *file, char *buf, size_t bufsiz)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return readlink(file, buf, bufsiz);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return -1;
    }
    return readlink(file, buf, bufsiz);
}

int fakerename(const char *oldpath, const char *newpath)
{
    char curdirtmp[PATH_MAX];
    char curdirtmp2[PATH_MAX];

    if (chroot_base == NULL) {
        return rename(oldpath, newpath);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, oldpath) != 0 ||
        fakexlate(curdirtmp2, sizeof curdirtmp2, newpath) != 0) {
        return -1;
    }
    return rename(curdirtmp, curdirtmp2);
}

/*
 * Promotion of mode_t is problematic. For instance, on MacOS X,
 * mode_t is an unsigned short.
 */

# if SIZEOF_MODE_T <= 0
#  define VA_ARG_MODE_T unsigned int
# elif SIZEOF_MODE_T <= SIZEOF_INT
#  define VA_ARG_MODE_T unsigned int
# elif SIZEOF_MODE_T <= SIZEOF_LONG
#  define VA_ARG_MODE_T unsigned long
# elif SIZEOF_MODE_T <= SIZEOF_LONG_LONG
#  define VA_ARG_MODE_T unsigned long long
# else
#  define VA_ARG_MODE_T mode_t
# endif

int fakeopen(const char *file, int flags, ...)
{
    va_list va;
    mode_t mode;
    char curdirtmp[PATH_MAX];

    va_start(va, flags);
    if (chroot_base == NULL) {
        if ((flags & O_CREAT) != 0) {
            mode = va_arg(va, VA_ARG_MODE_T);
            va_end(va);
            return open(file, flags, mode);
        }
        va_end(va);
        return open(file, flags);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        va_end(va);
        return -1;
    }
    if ((flags & O_CREAT) != 0) {
        mode = va_arg(va, VA_ARG_MODE_T);
        va_end(va);
        return open(curdirtmp, flags, mode);
    }
    va_end(va);

    return open(curdirtmp, flags);
}

char *fakerealpath(const char *file, char *resolved_path)
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return realpath(file, resolved_path);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, file) != 0) {
        return NULL;
    }
    return realpath(curdirtmp, resolved_path);
}

# if STATFS_TYPE == 1
int fakestatvfs64(const char *path, STATFS_STRUCT *str)
# elif STATFS_TYPE == 2
int fakestatvfs(const char *path, STATFS_STRUCT *str)
# elif STATFS_TYPE == 3
int fakestatfs(const char *path, STATFS_STRUCT *str)
# endif
# if STATFS_TYPE > 0
{
    char curdirtmp[PATH_MAX];

    if (chroot_base == NULL) {
        return STATFS(path, str);
    }
    if (fakexlate(curdirtmp, sizeof curdirtmp, path) != 0) {
        return -1;
    }
    return STATFS(curdirtmp, str);
}
# endif
#else
extern signed char v6ready;
#endif