Whamcloud - gitweb
misc: add e2mmpstatus utility via dumpe2fs
[tools/e2fsprogs.git] / debugfs / xattrs.c
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>
17 #include "support/cstring.h"
18
19 #include "debugfs.h"
20
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
27 /* Dump extended attributes */
28 static void print_xattr_hex(FILE *f, const char *str, int len)
29 {
30         int i;
31
32         for (i = 0; i < len; i++)
33                 fprintf(f, "%02x ", (unsigned char)str[i]);
34 }
35
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;
41
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);
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, "  ");
89         print_xattr(out, name, value, value_len, PRINT_XATTR_STATFMT);
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
120 void do_list_xattr(int argc, char **argv)
121 {
122         ext2_ino_t ino;
123
124         if (argc != 2) {
125                 printf("%s: Usage: %s <file>\n", argv[0],
126                        argv[0]);
127                 return;
128         }
129
130         if (check_fs_open(argv[0]))
131                 return;
132
133         ino = string_to_inode(argv[1]);
134         if (!ino)
135                 return;
136
137         dump_inode_attributes(stdout, ino);
138 }
139
140 void do_get_xattr(int argc, char **argv)
141 {
142         ext2_ino_t ino;
143         struct ext2_xattr_handle *h;
144         FILE *fp = NULL;
145         char *buf = NULL;
146         size_t buflen;
147         int i;
148         int print_flags = 0;
149         unsigned int handle_flags = 0;
150         errcode_t err;
151
152         reset_getopt();
153         while ((i = getopt(argc, argv, "Cf:rxV")) != -1) {
154                 switch (i) {
155                 case 'f':
156                         if (fp)
157                                 fclose(fp);
158                         fp = fopen(optarg, "w");
159                         if (fp == NULL) {
160                                 perror(optarg);
161                                 return;
162                         }
163                         break;
164                 case 'r':
165                         handle_flags |= XATTR_HANDLE_FLAG_RAW;
166                         break;
167                 case 'x':
168                         print_flags |= PRINT_XATTR_HEX;
169                         break;
170                 case 'V':
171                         print_flags |= PRINT_XATTR_RAW|
172                                 PRINT_XATTR_NOQUOTES;
173                         break;
174                 case 'C':
175                         print_flags |= PRINT_XATTR_C;
176                         break;
177                 default:
178                         goto usage;
179                 }
180         }
181
182         if (optind != argc - 2) {
183         usage:
184                 printf("%s: Usage: %s [-f outfile]|[-xVC] [-r] <file> <attr>\n",
185                                argv[0], argv[0]);
186
187                 goto out2;
188         }
189
190         if (check_fs_open(argv[0]))
191                 goto out2;
192
193         ino = string_to_inode(argv[optind]);
194         if (!ino)
195                 goto out2;
196
197         err = ext2fs_xattrs_open(current_fs, ino, &h);
198         if (err)
199                 goto out2;
200
201         err = ext2fs_xattrs_flags(h, &handle_flags, NULL);
202         if (err)
203                 goto out;
204
205         err = ext2fs_xattrs_read(h);
206         if (err)
207                 goto out;
208
209         err = ext2fs_xattr_get(h, argv[optind + 1], (void **)&buf, &buflen);
210         if (err)
211                 goto out;
212
213         if (fp) {
214                 fwrite(buf, buflen, 1, fp);
215         } else {
216                 if (print_flags & PRINT_XATTR_RAW) {
217                         if (print_flags & (PRINT_XATTR_HEX|PRINT_XATTR_C))
218                                 print_flags &= ~PRINT_XATTR_RAW;
219                         print_xattr_string(stdout, buf, buflen, print_flags);
220                 } else {
221                         print_xattr(stdout, argv[optind + 1],
222                                     buf, buflen, print_flags);
223                 }
224                 printf("\n");
225         }
226
227         ext2fs_free_mem(&buf);
228 out:
229         ext2fs_xattrs_close(&h);
230         if (err)
231                 com_err(argv[0], err, "while getting extended attribute");
232 out2:
233         if (fp)
234                 fclose(fp);
235 }
236
237 void do_set_xattr(int argc, char **argv)
238 {
239         ext2_ino_t ino;
240         struct ext2_xattr_handle *h;
241         FILE *fp = NULL;
242         char *buf = NULL;
243         size_t buflen;
244         unsigned int handle_flags = 0;
245         int i;
246         errcode_t err;
247
248         reset_getopt();
249         while ((i = getopt(argc, argv, "f:r")) != -1) {
250                 switch (i) {
251                 case 'f':
252                         if (fp)
253                                 fclose(fp);
254                         fp = fopen(optarg, "r");
255                         if (fp == NULL) {
256                                 perror(optarg);
257                                 return;
258                         }
259                         break;
260                 case 'r':
261                         handle_flags |= XATTR_HANDLE_FLAG_RAW;
262                         break;
263                 default:
264                         goto print_usage;
265                 }
266         }
267
268         if (!(fp && optind == argc - 2) && !(!fp && optind == argc - 3)) {
269         print_usage:
270                 printf("Usage:\t%s [-r] <file> <attr> <value>\n", argv[0]);
271                 printf("\t%s -f <value_file> [-r] <file> <attr>\n", argv[0]);
272                 goto out2;
273         }
274
275         if (check_fs_open(argv[0]))
276                 goto out2;
277         if (check_fs_read_write(argv[0]))
278                 goto out2;
279         if (check_fs_bitmaps(argv[0]))
280                 goto out2;
281
282         ino = string_to_inode(argv[optind]);
283         if (!ino)
284                 goto out2;
285
286         err = ext2fs_xattrs_open(current_fs, ino, &h);
287         if (err)
288                 goto out2;
289
290         err = ext2fs_xattrs_flags(h, &handle_flags, NULL);
291         if (err)
292                 goto out;
293
294         err = ext2fs_xattrs_read(h);
295         if (err)
296                 goto out;
297
298         if (fp) {
299                 err = ext2fs_get_mem(current_fs->blocksize, &buf);
300                 if (err)
301                         goto out;
302                 buflen = fread(buf, 1, current_fs->blocksize, fp);
303         } else {
304                 buf = argv[optind + 2];
305                 buflen = parse_c_string(buf);
306         }
307
308         err = ext2fs_xattr_set(h, argv[optind + 1], buf, buflen);
309 out:
310         ext2fs_xattrs_close(&h);
311         if (err)
312                 com_err(argv[0], err, "while setting extended attribute");
313 out2:
314         if (fp) {
315                 fclose(fp);
316                 ext2fs_free_mem(&buf);
317         }
318 }
319
320 void do_rm_xattr(int argc, char **argv)
321 {
322         ext2_ino_t ino;
323         struct ext2_xattr_handle *h;
324         int i;
325         errcode_t err;
326
327         if (argc < 3) {
328                 printf("%s: Usage: %s <file> <attrs>...\n", argv[0], argv[0]);
329                 return;
330         }
331
332         if (check_fs_open(argv[0]))
333                 return;
334         if (check_fs_read_write(argv[0]))
335                 return;
336         if (check_fs_bitmaps(argv[0]))
337                 return;
338
339         ino = string_to_inode(argv[1]);
340         if (!ino)
341                 return;
342
343         err = ext2fs_xattrs_open(current_fs, ino, &h);
344         if (err)
345                 return;
346
347         err = ext2fs_xattrs_read(h);
348         if (err)
349                 goto out;
350
351         for (i = 2; i < argc; i++) {
352                 err = ext2fs_xattr_remove(h, argv[i]);
353                 if (err)
354                         goto out;
355         }
356 out:
357         ext2fs_xattrs_close(&h);
358         if (err)
359                 com_err(argv[0], err, "while removing extended attribute");
360 }
361
362 /*
363  * Return non-zero if the string has a minimal number of non-printable
364  * characters.
365  */
366 static int is_mostly_printable(const char *cp, int len)
367 {
368         int     np = 0;
369
370         if (len < 0)
371                 len = strlen(cp);
372
373         while (len--) {
374                 if (!isprint(*cp++)) {
375                         np++;
376                         if (np > 3)
377                                 return 0;
378                 }
379         }
380         return 1;
381 }
382
383 static void safe_print(FILE *f, const char *cp, int len)
384 {
385         unsigned char   ch;
386
387         if (len < 0)
388                 len = strlen(cp);
389
390         while (len--) {
391                 ch = *cp++;
392                 if (ch > 128) {
393                         fputs("M-", f);
394                         ch -= 128;
395                 }
396                 if ((ch < 32) || (ch == 0x7f)) {
397                         fputc('^', f);
398                         ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
399                 }
400                 fputc(ch, f);
401         }
402 }
403
404 static void dump_xattr_raw_entries(FILE *f, unsigned char *buf,
405                                    unsigned int start, unsigned int len,
406                                    unsigned value_start)
407 {
408         struct ext2_ext_attr_entry ent;
409         char *name;
410         unsigned int off = start;
411         unsigned int vstart;
412
413         while (off < len) {
414                 if ((*(__u16 *) (buf + off)) == 0) {
415                         fprintf(f, "last entry found at offset %u (%04o)\n",
416                                 off, off);
417                         break;
418                 }
419                 if ((off + sizeof(struct ext2_ext_attr_entry)) >= len) {
420                 overrun:
421                         fprintf(f, "xattr buffer overrun at %u (len = %u)\n",
422                                 off, len);
423                         break;
424                 }
425 #if WORDS_BIGENDIAN
426                 ext2fs_swap_ext_attr_entry(&ent,
427                         (struct ext2_ext_attr_entry *) (buf + off));
428 #else
429                 ent = *((struct ext2_ext_attr_entry *) (buf + off));
430 #endif
431                 fprintf(f, "offset = %d (%04o), name_len = %u, "
432                         "name_index = %u\n",
433                         off, off, ent.e_name_len, ent.e_name_index);
434                 vstart = value_start + ent.e_value_offs;
435                 fprintf(f, "value_offset = %d (%04o), value_inum = %u, "
436                         "value_size = %u\n", ent.e_value_offs,
437                         vstart, ent.e_value_inum, ent.e_value_size);
438                 off += sizeof(struct ext2_ext_attr_entry);
439                 fprintf(f, "name = ");
440                 if ((off + ent.e_name_len) >= len)
441                         fprintf(f, "<runs off end>");
442                 else
443                         safe_print(f, (char *)(buf + off), ent.e_name_len);
444                 fputc('\n', f);
445                 if (ent.e_value_size == 0)
446                         goto skip_value;
447                 fprintf(f, "value = ");
448                 if (ent.e_value_inum)
449                         fprintf(f, "<ino %u>", ent.e_value_inum);
450                 else if (ent.e_value_offs >= len ||
451                          (vstart + ent.e_value_size) > len)
452                         fprintf(f, "<runs off end>");
453                 if (is_mostly_printable((char *)(buf + vstart),
454                                         ent.e_value_size))
455                         safe_print(f, (char *)(buf + vstart),
456                                    ent.e_value_size);
457                 else {
458                         fprintf(f, "<hexdump>\n");
459                         do_byte_hexdump(f, (char *)(buf + vstart),
460                                         ent.e_value_size);
461                 }
462                 fputc('\n', f);
463         skip_value:
464                 fputc('\n', f);
465                 off += (ent.e_name_len + 3) & ~3;
466         }
467 }
468
469 void raw_inode_xattr_dump(FILE *f, unsigned char *buf, unsigned int len)
470 {
471         __u32 magic = ext2fs_le32_to_cpu(*((__le32 *) buf));
472
473         fprintf(f, "magic = %08x, length = %u, value_start =4 \n\n",
474                 magic, len);
475         if (magic == EXT2_EXT_ATTR_MAGIC)
476                 dump_xattr_raw_entries(f, buf, 4, len, 4);
477 }
478
479 void block_xattr_dump(FILE *f, unsigned char *buf, unsigned int len)
480 {
481         struct ext2_ext_attr_header header;
482
483 #ifdef WORDS_BIGENDIAN
484         ext2fs_swap_ext_attr_header(&header,
485                                     (struct ext2_ext_attr_header *) buf);
486 #else
487         header = *((struct ext2_ext_attr_header *) buf);
488 #endif
489         fprintf(f, "magic = %08x, length = %u\n", header.h_magic, len);
490         if (header.h_magic != EXT2_EXT_ATTR_MAGIC)
491                 return;
492         fprintf(f, "refcount = %u, blocks = %u\n", header.h_refcount,
493                 header.h_blocks);
494         fprintf(f, "hash = %08x, checksum = %08x\n", header.h_hash,
495                 header.h_checksum);
496         fprintf(f, "reserved: %08x %08x %08x\n\n", header.h_reserved[0],
497                 header.h_reserved[1], header.h_reserved[2]);
498
499         dump_xattr_raw_entries(f, buf,
500                                sizeof(struct ext2_ext_attr_header), len, 0);
501 }