-
Notifications
You must be signed in to change notification settings - Fork 13
/
losetup.c
67 lines (59 loc) · 1.33 KB
/
losetup.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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool check_args(int argc, char **argv) {
if (argc == 3) {
if (strlen(argv[2]) > 8) {
printf("Usage: %s <m/u/f> <id>\n", argv[0]);
printf("id must be 8 characters or less\n");
return false;
}
return true;
} else {
printf("Usage: %s <m/u/f> <id>\n", argv[0]);
return false;
}
}
int main(int argc, char *argv[]) {
setuid(0);
setgid(0);
if (argc < 2 || argc > 3) {
printf("Usage: %s <m/u/f> <id?>\n", argv[0]);
return 1;
}
if (argv[1][1] != 0) {
printf("Usage: %s <m/u/f> <id>\n", argv[0]);
return 1;
}
char buffer[256];
switch (argv[1][0]) {
case 'm':
if (!check_args(argc, argv)) {
return 1;
}
sprintf(buffer, "losetup /dev/loop%s foxos.img -P", argv[2]);
system(buffer);
sprintf(buffer, "chown $USER:$USER /dev/loop%s", argv[2]);
system(buffer);
sprintf(buffer, "chown $USER:$USER /dev/loop%sp1", argv[2]);
system(buffer);
break;
case 'u':
if (!check_args(argc, argv)) {
return 1;
}
sprintf(buffer, "losetup -d /dev/loop%s", argv[2]);
system(buffer);
sprintf(buffer, "chown root:root /dev/loop%s", argv[2]);
system(buffer);
break;
case 'f':
system("losetup -f");
break;
default:
printf("Usage: %s <m/u/f> <id>\n", argv[0]);
return 1;
}
}