]>
Commit | Line | Data |
---|---|---|
227239b1 DW |
1 | /* |
2 | * xattrs.c --- Modify extended attributes via debugfs. | |
3 | * | |
4 | * Copyright (C) 2014 Oracle. This file may be redistributed | |
5 | * under the terms of the GNU Public License. | |
6 | */ | |
7 | ||
8 | #include "config.h" | |
9 | #include <stdio.h> | |
10 | #ifdef HAVE_GETOPT_H | |
11 | #include <getopt.h> | |
12 | #else | |
13 | extern int optind; | |
14 | extern char *optarg; | |
15 | #endif | |
16 | #include <ctype.h> | |
997e213b | 17 | #include "support/cstring.h" |
227239b1 DW |
18 | |
19 | #include "debugfs.h" | |
20 | ||
997e213b TT |
21 | #define PRINT_XATTR_HEX 0x01 |
22 | #define PRINT_XATTR_RAW 0x02 | |
23 | #define PRINT_XATTR_C 0x04 | |
24 | #define PRINT_XATTR_STATFMT 0x08 | |
25 | #define PRINT_XATTR_NOQUOTES 0x10 | |
26 | ||
227239b1 | 27 | /* Dump extended attributes */ |
997e213b | 28 | static void print_xattr_hex(FILE *f, const char *str, int len) |
227239b1 | 29 | { |
227239b1 DW |
30 | int i; |
31 | ||
227239b1 | 32 | for (i = 0; i < len; i++) |
997e213b TT |
33 | fprintf(f, "%02x ", (unsigned char)str[i]); |
34 | } | |
227239b1 | 35 | |
997e213b TT |
36 | /* Dump extended attributes */ |
37 | static void print_xattr_string(FILE *f, const char *str, int len, int flags) | |
38 | { | |
39 | int printable = 0; | |
40 | int i; | |
227239b1 | 41 | |
997e213b TT |
42 | if (flags & PRINT_XATTR_RAW) { |
43 | fwrite(str, len, 1, f); | |
44 | return; | |
45 | } | |
46 | ||
47 | if ((flags & PRINT_XATTR_C) == 0) { | |
48 | /* check: is string "printable enough?" */ | |
49 | for (i = 0; i < len; i++) | |
50 | if (isprint(str[i])) | |
51 | printable++; | |
52 | ||
53 | if (printable <= len*7/8) | |
54 | flags |= PRINT_XATTR_HEX; | |
55 | } | |
56 | ||
57 | if (flags & PRINT_XATTR_HEX) { | |
58 | print_xattr_hex(f, str, len); | |
59 | } else { | |
60 | if ((flags & PRINT_XATTR_NOQUOTES) == 0) | |
61 | fputc('\"', f); | |
62 | print_c_string(f, str, len); | |
63 | if ((flags & PRINT_XATTR_NOQUOTES) == 0) | |
64 | fputc('\"', f); | |
65 | } | |
66 | } | |
67 | ||
68 | static void print_xattr(FILE *f, char *name, char *value, size_t value_len, | |
69 | int print_flags) | |
70 | { | |
71 | print_xattr_string(f, name, strlen(name), PRINT_XATTR_NOQUOTES); | |
72 | fprintf(f, " (%zu)", value_len); | |
73 | if ((print_flags & PRINT_XATTR_STATFMT) && | |
74 | (strcmp(name, "system.data") == 0)) | |
75 | value_len = 0; | |
76 | if (value_len != 0 && | |
77 | (!(print_flags & PRINT_XATTR_STATFMT) || (value_len < 40))) { | |
78 | fprintf(f, " = "); | |
79 | print_xattr_string(f, value, value_len, print_flags); | |
80 | } | |
81 | fputc('\n', f); | |
227239b1 DW |
82 | } |
83 | ||
84 | static int dump_attr(char *name, char *value, size_t value_len, void *data) | |
85 | { | |
86 | FILE *out = data; | |
87 | ||
88 | fprintf(out, " "); | |
997e213b | 89 | print_xattr(out, name, value, value_len, PRINT_XATTR_STATFMT); |
227239b1 DW |
90 | return 0; |
91 | } | |
92 | ||
93 | void dump_inode_attributes(FILE *out, ext2_ino_t ino) | |
94 | { | |
95 | struct ext2_xattr_handle *h; | |
96 | size_t sz; | |
97 | errcode_t err; | |
98 | ||
99 | err = ext2fs_xattrs_open(current_fs, ino, &h); | |
100 | if (err) | |
101 | return; | |
102 | ||
103 | err = ext2fs_xattrs_read(h); | |
104 | if (err) | |
105 | goto out; | |
106 | ||
107 | err = ext2fs_xattrs_count(h, &sz); | |
108 | if (err || sz == 0) | |
109 | goto out; | |
110 | ||
111 | fprintf(out, "Extended attributes:\n"); | |
112 | err = ext2fs_xattrs_iterate(h, dump_attr, out); | |
113 | if (err) | |
114 | goto out; | |
115 | ||
116 | out: | |
117 | err = ext2fs_xattrs_close(&h); | |
118 | } | |
119 | ||
2fcbcb1b TT |
120 | void do_list_xattr(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)), |
121 | void *infop EXT2FS_ATTR((unused))) | |
227239b1 DW |
122 | { |
123 | ext2_ino_t ino; | |
124 | ||
125 | if (argc != 2) { | |
126 | printf("%s: Usage: %s <file>\n", argv[0], | |
127 | argv[0]); | |
128 | return; | |
129 | } | |
130 | ||
131 | if (check_fs_open(argv[0])) | |
132 | return; | |
133 | ||
134 | ino = string_to_inode(argv[1]); | |
135 | if (!ino) | |
136 | return; | |
137 | ||
138 | dump_inode_attributes(stdout, ino); | |
139 | } | |
140 | ||
2fcbcb1b TT |
141 | void do_get_xattr(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)), |
142 | void *infop EXT2FS_ATTR((unused))) | |
227239b1 DW |
143 | { |
144 | ext2_ino_t ino; | |
145 | struct ext2_xattr_handle *h; | |
146 | FILE *fp = NULL; | |
147 | char *buf = NULL; | |
148 | size_t buflen; | |
149 | int i; | |
997e213b | 150 | int print_flags = 0; |
686994eb | 151 | unsigned int handle_flags = 0; |
227239b1 DW |
152 | errcode_t err; |
153 | ||
154 | reset_getopt(); | |
0ee1eaf7 | 155 | while ((i = getopt(argc, argv, "Cf:rxV")) != -1) { |
227239b1 DW |
156 | switch (i) { |
157 | case 'f': | |
ec3a42b1 DW |
158 | if (fp) |
159 | fclose(fp); | |
227239b1 DW |
160 | fp = fopen(optarg, "w"); |
161 | if (fp == NULL) { | |
162 | perror(optarg); | |
163 | return; | |
164 | } | |
165 | break; | |
0ee1eaf7 TT |
166 | case 'r': |
167 | handle_flags |= XATTR_HANDLE_FLAG_RAW; | |
168 | break; | |
997e213b TT |
169 | case 'x': |
170 | print_flags |= PRINT_XATTR_HEX; | |
171 | break; | |
172 | case 'V': | |
84383515 TT |
173 | print_flags |= PRINT_XATTR_RAW| |
174 | PRINT_XATTR_NOQUOTES; | |
997e213b TT |
175 | break; |
176 | case 'C': | |
177 | print_flags |= PRINT_XATTR_C; | |
178 | break; | |
227239b1 | 179 | default: |
997e213b | 180 | goto usage; |
227239b1 DW |
181 | } |
182 | } | |
183 | ||
184 | if (optind != argc - 2) { | |
997e213b | 185 | usage: |
0ee1eaf7 | 186 | printf("%s: Usage: %s [-f outfile]|[-xVC] [-r] <file> <attr>\n", |
997e213b | 187 | argv[0], argv[0]); |
0ee1eaf7 | 188 | |
1bad6f46 | 189 | goto out2; |
227239b1 DW |
190 | } |
191 | ||
192 | if (check_fs_open(argv[0])) | |
1bad6f46 | 193 | goto out2; |
227239b1 DW |
194 | |
195 | ino = string_to_inode(argv[optind]); | |
196 | if (!ino) | |
1bad6f46 | 197 | goto out2; |
227239b1 DW |
198 | |
199 | err = ext2fs_xattrs_open(current_fs, ino, &h); | |
200 | if (err) | |
1bad6f46 | 201 | goto out2; |
227239b1 | 202 | |
0ee1eaf7 TT |
203 | err = ext2fs_xattrs_flags(h, &handle_flags, NULL); |
204 | if (err) | |
205 | goto out; | |
206 | ||
227239b1 DW |
207 | err = ext2fs_xattrs_read(h); |
208 | if (err) | |
209 | goto out; | |
210 | ||
211 | err = ext2fs_xattr_get(h, argv[optind + 1], (void **)&buf, &buflen); | |
212 | if (err) | |
213 | goto out; | |
214 | ||
215 | if (fp) { | |
216 | fwrite(buf, buflen, 1, fp); | |
227239b1 | 217 | } else { |
997e213b | 218 | if (print_flags & PRINT_XATTR_RAW) { |
84383515 | 219 | if (print_flags & (PRINT_XATTR_HEX|PRINT_XATTR_C)) |
997e213b TT |
220 | print_flags &= ~PRINT_XATTR_RAW; |
221 | print_xattr_string(stdout, buf, buflen, print_flags); | |
222 | } else { | |
223 | print_xattr(stdout, argv[optind + 1], | |
224 | buf, buflen, print_flags); | |
225 | } | |
227239b1 DW |
226 | printf("\n"); |
227 | } | |
228 | ||
1bad6f46 | 229 | ext2fs_free_mem(&buf); |
227239b1 DW |
230 | out: |
231 | ext2fs_xattrs_close(&h); | |
232 | if (err) | |
233 | com_err(argv[0], err, "while getting extended attribute"); | |
1bad6f46 DW |
234 | out2: |
235 | if (fp) | |
236 | fclose(fp); | |
227239b1 DW |
237 | } |
238 | ||
2fcbcb1b TT |
239 | void do_set_xattr(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)), |
240 | void *infop EXT2FS_ATTR((unused))) | |
227239b1 DW |
241 | { |
242 | ext2_ino_t ino; | |
243 | struct ext2_xattr_handle *h; | |
244 | FILE *fp = NULL; | |
245 | char *buf = NULL; | |
246 | size_t buflen; | |
686994eb | 247 | unsigned int handle_flags = 0; |
227239b1 DW |
248 | int i; |
249 | errcode_t err; | |
250 | ||
251 | reset_getopt(); | |
0ee1eaf7 | 252 | while ((i = getopt(argc, argv, "f:r")) != -1) { |
227239b1 DW |
253 | switch (i) { |
254 | case 'f': | |
ec3a42b1 DW |
255 | if (fp) |
256 | fclose(fp); | |
227239b1 DW |
257 | fp = fopen(optarg, "r"); |
258 | if (fp == NULL) { | |
259 | perror(optarg); | |
260 | return; | |
261 | } | |
262 | break; | |
0ee1eaf7 TT |
263 | case 'r': |
264 | handle_flags |= XATTR_HANDLE_FLAG_RAW; | |
265 | break; | |
227239b1 | 266 | default: |
be05f60e | 267 | goto print_usage; |
227239b1 DW |
268 | } |
269 | } | |
270 | ||
d0bc2c88 | 271 | if (!(fp && optind == argc - 2) && !(!fp && optind == argc - 3)) { |
be05f60e | 272 | print_usage: |
7600aa0f | 273 | printf("Usage:\t%s [-r] <file> <attr> <value>\n", argv[0]); |
0ee1eaf7 | 274 | printf("\t%s -f <value_file> [-r] <file> <attr>\n", argv[0]); |
1bad6f46 | 275 | goto out2; |
227239b1 DW |
276 | } |
277 | ||
278 | if (check_fs_open(argv[0])) | |
1bad6f46 | 279 | goto out2; |
227239b1 | 280 | if (check_fs_read_write(argv[0])) |
1bad6f46 | 281 | goto out2; |
227239b1 | 282 | if (check_fs_bitmaps(argv[0])) |
1bad6f46 | 283 | goto out2; |
227239b1 DW |
284 | |
285 | ino = string_to_inode(argv[optind]); | |
286 | if (!ino) | |
1bad6f46 | 287 | goto out2; |
227239b1 DW |
288 | |
289 | err = ext2fs_xattrs_open(current_fs, ino, &h); | |
290 | if (err) | |
1bad6f46 | 291 | goto out2; |
227239b1 | 292 | |
0ee1eaf7 TT |
293 | err = ext2fs_xattrs_flags(h, &handle_flags, NULL); |
294 | if (err) | |
295 | goto out; | |
296 | ||
227239b1 DW |
297 | err = ext2fs_xattrs_read(h); |
298 | if (err) | |
299 | goto out; | |
300 | ||
301 | if (fp) { | |
302 | err = ext2fs_get_mem(current_fs->blocksize, &buf); | |
303 | if (err) | |
304 | goto out; | |
305 | buflen = fread(buf, 1, current_fs->blocksize, fp); | |
306 | } else { | |
307 | buf = argv[optind + 2]; | |
997e213b | 308 | buflen = parse_c_string(buf); |
227239b1 DW |
309 | } |
310 | ||
311 | err = ext2fs_xattr_set(h, argv[optind + 1], buf, buflen); | |
227239b1 | 312 | out: |
1bad6f46 DW |
313 | ext2fs_xattrs_close(&h); |
314 | if (err) | |
315 | com_err(argv[0], err, "while setting extended attribute"); | |
316 | out2: | |
227239b1 DW |
317 | if (fp) { |
318 | fclose(fp); | |
319 | ext2fs_free_mem(&buf); | |
320 | } | |
227239b1 DW |
321 | } |
322 | ||
2fcbcb1b TT |
323 | void do_rm_xattr(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)), |
324 | void *infop EXT2FS_ATTR((unused))) | |
227239b1 DW |
325 | { |
326 | ext2_ino_t ino; | |
327 | struct ext2_xattr_handle *h; | |
328 | int i; | |
329 | errcode_t err; | |
330 | ||
331 | if (argc < 3) { | |
332 | printf("%s: Usage: %s <file> <attrs>...\n", argv[0], argv[0]); | |
333 | return; | |
334 | } | |
335 | ||
336 | if (check_fs_open(argv[0])) | |
337 | return; | |
338 | if (check_fs_read_write(argv[0])) | |
339 | return; | |
340 | if (check_fs_bitmaps(argv[0])) | |
341 | return; | |
342 | ||
343 | ino = string_to_inode(argv[1]); | |
344 | if (!ino) | |
345 | return; | |
346 | ||
347 | err = ext2fs_xattrs_open(current_fs, ino, &h); | |
348 | if (err) | |
349 | return; | |
350 | ||
351 | err = ext2fs_xattrs_read(h); | |
352 | if (err) | |
353 | goto out; | |
354 | ||
355 | for (i = 2; i < argc; i++) { | |
227239b1 DW |
356 | err = ext2fs_xattr_remove(h, argv[i]); |
357 | if (err) | |
358 | goto out; | |
359 | } | |
227239b1 DW |
360 | out: |
361 | ext2fs_xattrs_close(&h); | |
362 | if (err) | |
363 | com_err(argv[0], err, "while removing extended attribute"); | |
364 | } | |
c7c99af6 TT |
365 | |
366 | /* | |
367 | * Return non-zero if the string has a minimal number of non-printable | |
368 | * characters. | |
369 | */ | |
370 | static int is_mostly_printable(const char *cp, int len) | |
371 | { | |
372 | int np = 0; | |
373 | ||
374 | if (len < 0) | |
375 | len = strlen(cp); | |
376 | ||
377 | while (len--) { | |
378 | if (!isprint(*cp++)) { | |
379 | np++; | |
380 | if (np > 3) | |
381 | return 0; | |
382 | } | |
383 | } | |
384 | return 1; | |
385 | } | |
386 | ||
387 | static void safe_print(FILE *f, const char *cp, int len) | |
388 | { | |
389 | unsigned char ch; | |
390 | ||
391 | if (len < 0) | |
392 | len = strlen(cp); | |
393 | ||
394 | while (len--) { | |
395 | ch = *cp++; | |
396 | if (ch > 128) { | |
397 | fputs("M-", f); | |
398 | ch -= 128; | |
399 | } | |
400 | if ((ch < 32) || (ch == 0x7f)) { | |
401 | fputc('^', f); | |
402 | ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */ | |
403 | } | |
404 | fputc(ch, f); | |
405 | } | |
406 | } | |
407 | ||
408 | static void dump_xattr_raw_entries(FILE *f, unsigned char *buf, | |
409 | unsigned int start, unsigned int len, | |
410 | unsigned value_start) | |
411 | { | |
412 | struct ext2_ext_attr_entry ent; | |
c7c99af6 TT |
413 | unsigned int off = start; |
414 | unsigned int vstart; | |
415 | ||
416 | while (off < len) { | |
417 | if ((*(__u16 *) (buf + off)) == 0) { | |
418 | fprintf(f, "last entry found at offset %u (%04o)\n", | |
419 | off, off); | |
420 | break; | |
421 | } | |
422 | if ((off + sizeof(struct ext2_ext_attr_entry)) >= len) { | |
c7c99af6 TT |
423 | fprintf(f, "xattr buffer overrun at %u (len = %u)\n", |
424 | off, len); | |
425 | break; | |
426 | } | |
427 | #if WORDS_BIGENDIAN | |
428 | ext2fs_swap_ext_attr_entry(&ent, | |
429 | (struct ext2_ext_attr_entry *) (buf + off)); | |
430 | #else | |
431 | ent = *((struct ext2_ext_attr_entry *) (buf + off)); | |
432 | #endif | |
433 | fprintf(f, "offset = %d (%04o), name_len = %u, " | |
434 | "name_index = %u\n", | |
435 | off, off, ent.e_name_len, ent.e_name_index); | |
436 | vstart = value_start + ent.e_value_offs; | |
437 | fprintf(f, "value_offset = %d (%04o), value_inum = %u, " | |
438 | "value_size = %u\n", ent.e_value_offs, | |
439 | vstart, ent.e_value_inum, ent.e_value_size); | |
440 | off += sizeof(struct ext2_ext_attr_entry); | |
441 | fprintf(f, "name = "); | |
442 | if ((off + ent.e_name_len) >= len) | |
443 | fprintf(f, "<runs off end>"); | |
444 | else | |
445 | safe_print(f, (char *)(buf + off), ent.e_name_len); | |
446 | fputc('\n', f); | |
447 | if (ent.e_value_size == 0) | |
448 | goto skip_value; | |
449 | fprintf(f, "value = "); | |
450 | if (ent.e_value_inum) | |
451 | fprintf(f, "<ino %u>", ent.e_value_inum); | |
452 | else if (ent.e_value_offs >= len || | |
453 | (vstart + ent.e_value_size) > len) | |
454 | fprintf(f, "<runs off end>"); | |
cecc2bc7 | 455 | else if (is_mostly_printable((char *)(buf + vstart), |
c7c99af6 TT |
456 | ent.e_value_size)) |
457 | safe_print(f, (char *)(buf + vstart), | |
458 | ent.e_value_size); | |
459 | else { | |
460 | fprintf(f, "<hexdump>\n"); | |
ac3256fd | 461 | do_byte_hexdump(f, (unsigned char *)(buf + vstart), |
c7c99af6 TT |
462 | ent.e_value_size); |
463 | } | |
464 | fputc('\n', f); | |
465 | skip_value: | |
466 | fputc('\n', f); | |
467 | off += (ent.e_name_len + 3) & ~3; | |
468 | } | |
469 | } | |
470 | ||
471 | void raw_inode_xattr_dump(FILE *f, unsigned char *buf, unsigned int len) | |
472 | { | |
473 | __u32 magic = ext2fs_le32_to_cpu(*((__le32 *) buf)); | |
474 | ||
475 | fprintf(f, "magic = %08x, length = %u, value_start =4 \n\n", | |
476 | magic, len); | |
477 | if (magic == EXT2_EXT_ATTR_MAGIC) | |
478 | dump_xattr_raw_entries(f, buf, 4, len, 4); | |
479 | } | |
480 | ||
481 | void block_xattr_dump(FILE *f, unsigned char *buf, unsigned int len) | |
482 | { | |
483 | struct ext2_ext_attr_header header; | |
484 | ||
485 | #ifdef WORDS_BIGENDIAN | |
486 | ext2fs_swap_ext_attr_header(&header, | |
487 | (struct ext2_ext_attr_header *) buf); | |
488 | #else | |
489 | header = *((struct ext2_ext_attr_header *) buf); | |
490 | #endif | |
491 | fprintf(f, "magic = %08x, length = %u\n", header.h_magic, len); | |
492 | if (header.h_magic != EXT2_EXT_ATTR_MAGIC) | |
493 | return; | |
494 | fprintf(f, "refcount = %u, blocks = %u\n", header.h_refcount, | |
495 | header.h_blocks); | |
496 | fprintf(f, "hash = %08x, checksum = %08x\n", header.h_hash, | |
497 | header.h_checksum); | |
498 | fprintf(f, "reserved: %08x %08x %08x\n\n", header.h_reserved[0], | |
499 | header.h_reserved[1], header.h_reserved[2]); | |
500 | ||
501 | dump_xattr_raw_entries(f, buf, | |
502 | sizeof(struct ext2_ext_attr_header), len, 0); | |
503 | } |