-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenimg.c
More file actions
339 lines (311 loc) · 6.76 KB
/
Copy pathgenimg.c
File metadata and controls
339 lines (311 loc) · 6.76 KB
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
#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/xattr.h>
#include <unistd.h>
#define ModeMetaMask 1
#define UidMetaMask 2
#define GidMetaMask 4
typedef struct meta_path_t meta_path_t;
struct meta_path_t {
meta_path_t *next;
union {
mode_t mode;
uid_t uid;
gid_t gid;
};
short mask;
char path[];
};
static meta_path_t *meta_path_first = NULL;
static meta_path_t *meta_path_last = NULL;
typedef struct inode_path_t inode_path_t;
struct inode_path_t {
inode_path_t *next;
ino_t inode;
char path[];
};
static inode_path_t *inode_path_first = NULL;
static inode_path_t *inode_path_last = NULL;
static bool all_root = true;
static char target[BUFSIZ];
void
print_path(const char *path)
{
while(*path) {
if(*path == '"' || *path == '\\') {
putchar('\\');
}
putchar(*path++);
}
}
static int
num_from_xattr(const char *filename, const char *name, unsigned short *out_value)
{
ssize_t vsize;
char str_value[16];
long value;
char *b;
vsize = lgetxattr(filename, name, str_value, sizeof(str_value)-1);
if(vsize < 0) {
/*
ERROR("lgetxattr failed for %s in "
"num_from_xattr, because %s", filename,
strerror(errno));
ERROR_EXIT(". Ignoring");
*/
return 1;
}
str_value[vsize] = '\0';
value = strtoll(str_value, &b, 10);
if(*b !='\0') {
fprintf(stderr, "strtoll failed for xattr %s of file %s\n", name, filename);
return -1;
}
if(value < 0 || value > USHRT_MAX) {
// FIXME: proper error handling
fprintf(stderr, "strtoll out of range for xattr %s of file %s\n", name, filename);
return -1;
}
*out_value = (unsigned short)value;
return 0;
}
static void
meta_append(const char *path, int mask, short value)
{
size_t len = strlen(path);
meta_path_t *mp = malloc(sizeof(*mp) + len + 1);
mp->next = NULL;
mp->mask = mask;
switch(mask) {
case ModeMetaMask:
mp->mode = value;
break;
case UidMetaMask:
mp->uid = value;
break;
case GidMetaMask:
mp->gid = value;
break;
}
memcpy(mp->path, path, len);
mp->path[len] = '\0';
if(meta_path_first == NULL) {
meta_path_first = mp;
} else {
meta_path_last->next = mp;
}
meta_path_last = mp;
}
void
print_entry(const char *type, const char *path, const struct stat *sb)
{
unsigned short id;
uid_t uid = 0;
gid_t gid = 0;
if(!all_root) {
uid = sb->st_uid;
gid = sb->st_gid;
}
if(!strncmp(path, "home/tmtstr", strlen("home/tmtstr"))) {
uid = 1000;
gid = 1000;
}
if(num_from_xattr(path, "user.escape.uid", &id) == 0) {
lremovexattr(path, "user.escape.uid");
uid = id;
meta_append(path, UidMetaMask, id);
}
if(num_from_xattr(path, "user.escape.gid", &id) == 0) {
lremovexattr(path, "user.escape.gid");
gid = id;
meta_append(path, GidMetaMask, id);
}
printf("%s \"/", type);
print_path(path);
printf("\" %04o %u %u", sb->st_mode & 07777, uid, gid);
}
static int
step(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
(void)ftwbuf;
if(!strcmp(path, ".")) {
path++;
} else if(!strncmp(path, "./", strlen("./"))) {
path += strlen("./");
}
// FIXME: root directory has empty path ""
if(sb->st_nlink > 1) {
// search for inode in inode_path list
for(inode_path_t *ip = inode_path_first; ip; ip = ip->next) {
if(ip->inode != sb->st_ino) {
continue;
}
// if found emit: link path ... target
printf("link \"");
print_path(path);
// target path can't be quote wrapped, strange
printf("\" 0 0 0 %s\n", ip->path);
return 0;
}
// else append path to the list
size_t len = strlen(path);
inode_path_t *ip = malloc(sizeof(*ip) + len + 1);
ip->next = NULL;
ip->inode = sb->st_ino;
memcpy(ip->path, path, len);
ip->path[len] = '\0';
if(inode_path_first == NULL) {
inode_path_first = ip;
} else {
inode_path_last->next = ip;
}
inode_path_last = ip;
}
switch(typeflag) {
case FTW_F:
switch(sb->st_mode & S_IFMT) {
case S_IFREG:
print_entry("file", path, sb);
putchar('\n');
if(!(sb->st_mode & S_IRUSR) && sb->st_uid == getuid()) {
mode_t tmp_st_mode = sb->st_mode | S_IRUSR;
if(chmod(path, tmp_st_mode) != 0) {
perror("chmod");
// FIXME: try to restore previous modes on errors
return -1;
}
meta_append(path, ModeMetaMask, sb->st_mode);
}
break;
case S_IFIFO:
print_entry("pipe", path, sb);
putchar('\n');
break;
case S_IFSOCK:
print_entry("sock", path, sb);
putchar('\n');
break;
case S_IFBLK:
case S_IFCHR:
print_entry("nod", path, sb);
printf("%c %u %u\n", (sb->st_mode & S_IFMT) == S_IFBLK ? 'b' : 'c', major(sb->st_rdev), minor(sb->st_rdev));
break;
case S_IFLNK:
case S_IFDIR:
default:
return -1;
}
break;
case FTW_D:
print_entry("dir", path, sb);
putchar('\n');
break;
case FTW_SL: {
// Remember: readlink does not null terminate
ssize_t len = readlink(path, target, sizeof(target));
if(len < 0) {
perror("readlink");
} else if(len == sizeof(target)) {
// FIXME: extend buffer
fprintf(stderr, "Possible truncation of symlink's (%s) target\n", path);
return -1;
}
// We abort on len == sizeof(target) so it's always safe to put a null terminator
target[len] = '\0';
print_entry("slink", path, sb);
printf(" %s\n", target);
break;
}
case FTW_DNR:
perror("FTW_DNR");
return -1;
case FTW_NS:
perror("FTW_NS");
return -1;
case FTW_DP:
case FTW_SLN:
default:
return -1;
}
return 0;
}
// TODO: what about sorting?
int
main(int argc, char *argv[])
{
pid_t pid;
int wstatus;
char buf[16];
int siz;
int fd[2];
if(argc > 2) {
if(pipe(fd) < 0) {
perror("pipe");
return -1;
}
pid = fork();
if(pid < 0) {
perror("fork");
return -1;
} else if(pid == 0) {
dup2(fd[0], 0);
close(fd[1]);
close(fd[0]);
execvp(argv[2], argv + 2);
perror("execv");
return -1;
}
dup2(fd[1], 1);
close(fd[0]);
close(fd[1]);
}
if(chdir(argv[1]) != 0) {
perror("chdir");
return -1;
}
if(nftw(".", step, 16, FTW_PHYS) != 0) {
perror("nftw");
goto out_restore;
return -1;
}
fclose(stdout);
if(argc > 2) {
waitpid(pid, &wstatus, 0);
// FIXME: log errors
}
out_restore:
for(meta_path_t *mp = meta_path_first; mp; mp = mp->next) {
// FIXME: error handling
switch(mp->mask) {
case ModeMetaMask:
chmod(mp->path, mp->mode);
break;
case UidMetaMask:
siz = snprintf(buf, sizeof buf, "%hu", mp->uid);
if(siz <= 0) {
// FIXME: ERROR!
break;
}
lsetxattr(mp->path, "user.escape.uid", buf, siz, XATTR_CREATE);
break;
case GidMetaMask:
siz = snprintf(buf, sizeof buf, "%hu", mp->gid);
if(siz <= 0) {
// FIXME: ERROR!
break;
}
lsetxattr(mp->path, "user.escape.gid", buf, siz, XATTR_CREATE);
break;
}
}
return 0;
}