This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
main.c
710 lines (589 loc) · 14.6 KB
/
main.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
* AUTHOR: Kris Moore <kris@pcbsd.org>
* LICENSE: BSD
* COMMENT: FUSE based filesystem for creating virtual PBI container chroot environments
*
*/
#define FUSE_USE_VERSION 26
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <fuse.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/types.h>
#include <unistd.h>
#define NOT_FOUND -1
// Set some globals
char hintsdir[MAXPATHLEN];
char pbidir[MAXPATHLEN];
char linuxdir[MAXPATHLEN];
int strpos(const char *haystack, char *needle)
{
char *p = strstr(haystack, needle);
if (p) {
return p - haystack;
}
return NOT_FOUND;
}
char *replace_str_all(const char *str, const char *old, const char *new)
{
char *ret, *r;
const char *p, *q;
size_t oldlen = strlen(old);
size_t count, retlen, newlen = strlen(new);
if (oldlen != newlen) {
for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
count++;
/* this is undefined if p - str > PTRDIFF_MAX */
retlen = p - str + strlen(p) + count * (newlen - oldlen);
} else
retlen = strlen(str);
if ((ret = malloc(retlen + 1)) == NULL)
return NULL;
for (r = ret, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen) {
/* this is undefined if q - p > PTRDIFF_MAX */
ptrdiff_t l = q - p;
memcpy(r, p, l);
r += l;
memcpy(r, new, newlen);
r += newlen;
}
strcpy(r, p);
return ret;
}
const char *replace_str(const char *str, char *orig, char *rep)
{
static char buffer[4096];
char *p;
if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'?
return str;
strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
buffer[p-str] = '\0';
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
return buffer;
}
/**********************************************************************************
* OK, this is where all the magic happens.
* We are going to be taking the current filesystem path, and checking it against
* a list of ones we want to "virtualize" so that it appears we have a different
* /usr/local namespace
***********************************************************************************/
int get_modified_path(char *npath, const char *opath)
{
// Get the real pathname we are using now
char rPath[MAXPATHLEN];
// Check if this is a relative path, or absolute
if ( strpos(opath, "/") == 0 ) {
strcpy(rPath, opath);
} else {
getwd(rPath);
strcat(rPath, opath);
// Debugging
/*
char testcmd[MAXPATHLEN];
strcpy(testcmd, "echo \"failedpath: ");
strcat(testcmd, opath);
strcat(testcmd, " -> ");
strcat(testcmd, rPath);
strcat(testcmd, "\" >> /tmp/failedpath");
system(testcmd);
*/
}
// First, lets look for calls to /var/run/ld-elf*.so.hints
// These need to be replaced with calls to our new hints file in hintsdir
if ( strpos(rPath, "/var/run/") == 0 )
{
if ( strcmp(rPath, "/var/run/ld-elf.so.hints") == 0 )
{
strcpy(npath, hintsdir);
strcat(npath, "/ld-elf.so.hints");
return 0;
}
if ( strcmp(rPath, "/var/run/ld-elf32.so.hints") == 0 )
{
strcpy(npath, hintsdir);
strcat(npath, "/ld-elf32.so.hints");
return 0;
}
}
if ( strcmp(rPath, "/etc/rc.d/ldconfig") == 0 )
{
strcpy(npath, "/usr/pbi/.ldconfig");
return 0;
}
// Check if we have a virtual linux /compat directory to fake as well
if ( strpos(rPath, "/compat/linux") == 0 )
{
// If this is /compat/linux/proc, lets use the systems
if ( strpos(rPath, "/compat/linux/proc") == 0 ) {
strcpy(npath, rPath);
return 0;
}
// If this is /compat/linux/sys, lets use the systems
if ( strpos(rPath, "/compat/linux/sys") == 0 ) {
strcpy(npath, rPath);
return 0;
}
if (0 == access(linuxdir, F_OK))
{
strcpy(npath, replace_str(rPath, "/compat/linux", linuxdir));
return 0;
}
strcpy(npath, rPath);
return 0;
}
// Lastly, lets do all the parsing for /usr/local matching
if ( strpos(rPath, "/usr/local") == 0 )
{
// Use the systems copies of some font / icon directories
if ( strpos(rPath, "/usr/local/etc/fonts") == 0 ) {
strcpy(npath, rPath);
return 0;
}
if ( strpos(rPath, "/usr/local/lib/X11/fonts") == 0 ) {
strcpy(npath, rPath);
return 0;
}
if ( strpos(rPath, "/usr/local/lib/X11/icons") == 0 ) {
strcpy(npath, rPath);
return 0;
}
if ( strpos(rPath, "/usr/local/share/icons") == 0 )
{
strcpy(npath, rPath);
return 0;
}
if ( strpos(rPath, "/usr/local/share/font-") == 0 ) {
strcpy(npath, rPath);
return 0;
}
// Look for some of our callback utils
if ( strpos(rPath, "/usr/local/bin/pbisyscmd") == 0 ) {
strcpy(npath, "/usr/pbi/.pbisyscmd");
return 0;
}
if ( strpos(rPath, "/usr/local/bin/openwith") == 0 ) {
strcpy(npath, "/usr/pbi/.pbisyscmd");
return 0;
}
if ( strpos(rPath, "/usr/local/bin/xdg-open") == 0 ) {
strcpy(npath, "/usr/pbi/.pbisyscmd");
return 0;
}
strcpy(npath, replace_str(rPath, "/usr/local", pbidir));
return 0;
}
strcpy(npath, rPath);
return 0;
}
int newfile_chown(const char *path) {
struct fuse_context *ctx = fuse_get_context();
if (ctx->uid != 0 && ctx->gid != 0) {
int res = lchown(path, ctx->uid, ctx->gid);
if (res)
return -errno;
}
return 0;
}
int cp(const char *to, const char *from)
{
int fd_to, fd_from;
char buf[4096];
ssize_t nread;
int saved_errno;
fd_from = open(from, O_RDONLY);
if (fd_from < 0)
return -1;
fd_to = open(to, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd_to < 0)
goto out_error;
while (nread = read(fd_from, buf, sizeof buf), nread > 0)
{
char *out_ptr = buf;
ssize_t nwritten;
do {
nwritten = write(fd_to, out_ptr, nread);
if (nwritten >= 0)
{
nread -= nwritten;
out_ptr += nwritten;
}
else if (errno != EINTR)
{
goto out_error;
}
} while (nread > 0);
}
if (nread == 0)
{
if (close(fd_to) < 0)
{
fd_to = -1;
goto out_error;
}
close(fd_from);
/* Success! */
return 0;
}
out_error:
saved_errno = errno;
close(fd_from);
if (fd_to >= 0)
close(fd_to);
errno = saved_errno;
return -1;
}
static int pbi_access(const char *path, int mask)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
int res = access(newpath, mask);
if (res == -1)
return -errno;
return 0;
}
static int pbi_getattr(const char *path, struct stat *stbuf)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
/*
char testcmd[MAXPATHLEN];
strcpy(testcmd, "echo \"getattr: ");
strcat(testcmd, newpath);
strcat(testcmd, "\" >> /tmp/newpath");
system(testcmd);
*/
int res = lstat(newpath, stbuf);
if (res == -1)
return -errno;
return res;
}
static int pbi_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
(void) offset;
(void) fi;
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
// Avoid a recursive directory tree
if ( strpos(newpath, "/usr/pbi/.mounts") == 0)
return 0;
DIR *openDir = opendir(newpath);
struct dirent *de;
while ((de = readdir(openDir)) != NULL) {
struct stat st;
memset(&st, 0, sizeof(st));
st.st_ino = de->d_ino;
st.st_mode = de->d_type << 12;
if (filler(buf, de->d_name, &st, 0)) break;
}
closedir(openDir);
return 0;
}
static int pbi_open(const char *path, struct fuse_file_info *fi)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
int fd = open(newpath, fi->flags);
if (fd == -1)
return -errno;
fi->fh = (unsigned long)fd;
return 0;
}
static int pbi_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
(void) fi;
int result = pread(fi->fh, buf, size, offset);
if (result == -1)
return -errno;
return result;
}
static int pbi_statfs(const char *path, struct statvfs *stbuf)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
return statvfs(newpath, stbuf);
}
static int pbi_symlink(const char *from, const char *to)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, from);
return symlink(newpath, to);
}
static int pbi_readlink(const char *path, char *buf, size_t size)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
int result= readlink(newpath, buf, size - 1);
if (result == -1)
return -errno;
buf[result] = '\0';
return 0;
}
static int pbi_chmod(const char *path, mode_t mode)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
int result = lchmod(newpath, mode);
if (result == -1)
return -errno;
return 0;
}
static int pbi_chown(const char *path, uid_t uid, gid_t gid)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
int result = lchown(newpath, uid, gid);
if (result == -1)
return -errno;
return 0;
}
static int pbi_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
int res = open(newpath, fi->flags, 0);
if (res == -1)
return -errno;
newfile_chown(newpath);
fchmod(res, mode);
fi->fh = res;
return 0;
}
static int pbi_flush(const char *path, struct fuse_file_info *fi)
{
int fd = dup(fi->fh);
if (fd == -1) {
if (fsync(fi->fh) == -1)
return -EIO;
return -errno;
}
int res = close(fd);
if (res == -1)
return -errno;
return 0;
}
static int pbi_fsync(const char *path, int isdatasync, struct fuse_file_info *fi)
{
(void) isdatasync;
int res = fsync(fi->fh);
if (res == -1)
return -errno;
return 0;
}
static int pbi_link(const char *from, const char *to)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, from);
int res = link(newpath, to);
if (res == -1)
return -errno;
return 0;
}
static int pbi_mkdir(const char *path, mode_t mode)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
int res = mkdir(newpath, 0);
if (res == -1)
return -errno;
newfile_chown(newpath);
chmod(newpath, mode);
return 0;
}
static int pbi_mknod(const char *path, mode_t mode, dev_t rdev)
{
int ftype = mode & S_IFMT;
int fpath = mode & (S_ISUID| S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
int res = mknod(newpath, ftype, rdev);
if (res == -1)
return -errno;
newfile_chown(newpath);
chmod(newpath, fpath);
return 0;
}
static int pbi_release(const char *path, struct fuse_file_info *fi)
{
(void) path;
int res = close(fi->fh);
if (res == -1)
return -errno;
return 0;
}
static int pbi_rename(const char *from, const char *to)
{
char newfrom[MAXPATHLEN];
char newto[MAXPATHLEN];
get_modified_path(newfrom, from);
get_modified_path(newto, to);
if ( strcmp(from, newfrom) != 0 || strcmp(to, newto) != 0 )
{
// Unlink the target first
if (0 == access(newto, F_OK)) {
if ( unlink(newto) == -1 )
return -errno;
}
// Doing a across file-system move, need to use copy instead of rename
if ( cp(newto, newfrom) != 0)
return -errno;
// Set owner / permissions now
struct stat fst;
stat(newfrom, &fst);
chown(newto, fst.st_uid,fst.st_gid);
chmod(newto, fst.st_mode);
if ( unlink(newfrom) == -1 )
return -errno;
} else {
int res = rename(newfrom, newto);
if (res == -1)
return -errno;
}
return 0;
}
int pbi_rmdir(const char *path)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
int res = rmdir(newpath);
if (res == -1)
return -errno;
return 0;
}
static int pbi_truncate(const char *path, off_t size)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
int res = truncate(newpath, size);
if (res == -1)
return -errno;
return 0;
}
int pbi_unlink(const char *path)
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
int res = unlink(newpath);
if (res == -1)
return -errno;
return 0;
}
static int pbi_utimens(const char *path, const struct timespec ts[2])
{
char newpath[MAXPATHLEN];
get_modified_path(newpath, path);
struct timeval tv[2];
tv[0].tv_sec = ts[0].tv_sec;
tv[0].tv_usec = ts[0].tv_nsec / 1000;
tv[1].tv_sec = ts[0].tv_sec;
tv[1].tv_usec = ts[0].tv_nsec / 1000;
int res = lutimes(newpath, tv);
if (res == -1)
return -errno;
return 0;
}
static int pbi_write(const char *path, const char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
(void) path;
int written = pwrite(fi->fh, buf, size, offset);
if (written == -1)
return -errno;
return written;
}
static struct fuse_operations pbi_oper = {
.access = pbi_access,
.chmod = pbi_chmod,
.chown = pbi_chown,
.create = pbi_create,
.flush = pbi_flush,
.fsync = pbi_fsync,
.getattr = pbi_getattr,
.link = pbi_link,
.mkdir = pbi_mkdir,
.mknod = pbi_mknod,
.open = pbi_open,
.read = pbi_read,
.readlink = pbi_readlink,
.readdir = pbi_readdir,
.release = pbi_release,
.rename = pbi_rename,
.rmdir = pbi_rmdir,
.statfs = pbi_statfs,
.symlink = pbi_symlink,
.truncate = pbi_truncate,
.unlink = pbi_unlink,
.utimens = pbi_utimens,
.write = pbi_write,
};
int main(int argc, char *argv[])
{
if ( argc != 4 )
{
printf("Usage: %s <pbidir> <hintsdir> <mountpoint>\n", argv[0]);
printf("Where: ");
printf(" <pbidir> = PBI directory with a new /local directory\n");
printf(" and optional /linux directory\n");
printf(" <hintsdir> = Directory with new ld-elf*.so.hints files\n");
printf(" <mountpoint> = Directory to mount PBI container\n");
return 1;
}
if ( strlen(argv[1]) >= MAXPATHLEN || strlen(argv[2]) >= MAXPATHLEN ) {
printf("MAXPATHLEN violation!\n");
return 1;
}
// Verify the pbidir exists
strcpy(pbidir, argv[1]);
strcat(pbidir, "/local");
if (0 != access(pbidir, F_OK)) {
if (ENOENT == errno) {
// does not exist
printf("ERROR: The directory %s does not exist!", pbidir);
return 1;
}
if (ENOTDIR == errno) {
// not a directory
printf("ERROR: %s is not a directory!", pbidir);
return 1;
}
}
// Add a linux dir
strcpy(linuxdir, argv[1]);
strcat(linuxdir, "/linux");
// Verify that hintsdir exists
strcpy(hintsdir, argv[2]);
if (0 != access(hintsdir, F_OK)) {
if (ENOENT == errno) {
// does not exist
printf("ERROR: The directory %s does not exist!", pbidir);
return 1;
}
if (ENOTDIR == errno) {
// not a directory
printf("ERROR: %s is not a directory!", pbidir);
return 1;
}
}
// Set the mountpoint before starting fuse
char *newargv[1024];
newargv[0] = argv[0];
newargv[1] = argv[3];
// Set some fuse options
struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
for ( int i = 0; i < argc ; i++ )
{
if ( i == 1 || i == 2 )
continue;
fuse_opt_add_arg(&args, argv[i]);
}
fuse_opt_add_arg(&args, "-oallow_other");
fuse_opt_add_arg(&args, "-s");
return fuse_main(args.argc, args.argv, &pbi_oper, NULL);
}