Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1 | /** |
| 2 | * ntfscat - Part of the Linux-NTFS project. |
| 3 | * |
| 4 | * Copyright (c) 2003-2005 Richard Russon |
| 5 | * Copyright (c) 2003-2005 Anton Altaparmakov |
| 6 | * Copyright (c) 2003-2005 Szabolcs Szakacsits |
| 7 | * Copyright (c) 2007 Yura Pakhuchiy |
| 8 | * |
| 9 | * This utility will concatenate files and print on the standard output. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program (in the main directory of the Linux-NTFS |
| 23 | * distribution in the file COPYING); if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | |
| 29 | #ifdef HAVE_STDIO_H |
| 30 | #include <stdio.h> |
| 31 | #endif |
| 32 | #ifdef HAVE_GETOPT_H |
| 33 | #include <getopt.h> |
| 34 | #endif |
| 35 | #ifdef HAVE_STDLIB_H |
| 36 | #include <stdlib.h> |
| 37 | #endif |
| 38 | #ifdef HAVE_STRING_H |
| 39 | #include <string.h> |
| 40 | #endif |
| 41 | |
| 42 | #include "types.h" |
| 43 | #include "attrib.h" |
| 44 | #include "utils.h" |
| 45 | #include "volume.h" |
| 46 | #include "debug.h" |
| 47 | #include "dir.h" |
| 48 | #include "ntfscat.h" |
| 49 | /* #include "version.h" */ |
| 50 | #include "utils.h" |
| 51 | |
| 52 | static const char *EXEC_NAME = "ntfscat"; |
| 53 | static struct options opts; |
| 54 | |
| 55 | /** |
| 56 | * version - Print version information about the program |
| 57 | * |
| 58 | * Print a copyright statement and a brief description of the program. |
| 59 | * |
| 60 | * Return: none |
| 61 | */ |
| 62 | static void version(void) |
| 63 | { |
| 64 | ntfs_log_info("\n%s v%s (libntfs-3g) - Concatenate files and print " |
| 65 | "on the standard output.\n\n", EXEC_NAME, VERSION); |
| 66 | ntfs_log_info("Copyright (c) 2003-2005 Richard Russon\n"); |
| 67 | ntfs_log_info("Copyright (c) 2003-2005 Anton Altaparmakov\n"); |
| 68 | ntfs_log_info("Copyright (c) 2003-2005 Szabolcs Szakacsits\n"); |
| 69 | ntfs_log_info("Copyright (c) 2007 Yura Pakhuchiy\n"); |
| 70 | ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * usage - Print a list of the parameters to the program |
| 75 | * |
| 76 | * Print a list of the parameters and options for the program. |
| 77 | * |
| 78 | * Return: none |
| 79 | */ |
| 80 | static void usage(void) |
| 81 | { |
| 82 | ntfs_log_info("\nUsage: %s [options] device [file]\n\n" |
| 83 | " -a, --attribute TYPE Display this attribute type\n" |
| 84 | " -n, --attribute-name NAME Display this attribute name\n" |
| 85 | " -i, --inode NUM Display this inode\n\n" |
| 86 | " -f, --force Use less caution\n" |
| 87 | " -h, --help Print this help\n" |
| 88 | " -q, --quiet Less output\n" |
| 89 | " -V, --version Version information\n" |
| 90 | " -v, --verbose More output\n\n", |
| 91 | // Does not work for compressed files at present so leave undocumented... |
| 92 | // " -r --raw Display the raw data (e.g. for compressed or encrypted file)", |
| 93 | EXEC_NAME); |
| 94 | ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * parse_attribute - Read an attribute name, or number |
| 99 | * @value: String to be parsed |
| 100 | * @attr: Resulting attribute id (on success) |
| 101 | * |
| 102 | * Read a string representing an attribute. It may be a decimal, octal or |
| 103 | * hexadecimal number, or the attribute name in full. The leading $ sign is |
| 104 | * optional. |
| 105 | * |
| 106 | * Return: 1 Success, a valid attribute name or number |
| 107 | * 0 Error, not an attribute name or number |
| 108 | */ |
| 109 | static int parse_attribute(const char *value, ATTR_TYPES *attr) |
| 110 | { |
| 111 | static const char *attr_name[] = { |
| 112 | "$STANDARD_INFORMATION", |
| 113 | "$ATTRIBUTE_LIST", |
| 114 | "$FILE_NAME", |
| 115 | "$OBJECT_ID", |
| 116 | "$SECURITY_DESCRIPTOR", |
| 117 | "$VOLUME_NAME", |
| 118 | "$VOLUME_INFORMATION", |
| 119 | "$DATA", |
| 120 | "$INDEX_ROOT", |
| 121 | "$INDEX_ALLOCATION", |
| 122 | "$BITMAP", |
| 123 | "$REPARSE_POINT", |
| 124 | "$EA_INFORMATION", |
| 125 | "$EA", |
| 126 | "$PROPERTY_SET", |
| 127 | "$LOGGED_UTILITY_STREAM", |
| 128 | NULL |
| 129 | }; |
| 130 | |
| 131 | int i; |
| 132 | long num; |
| 133 | |
| 134 | for (i = 0; attr_name[i]; i++) { |
| 135 | if ((strcmp(value, attr_name[i]) == 0) || |
| 136 | (strcmp(value, attr_name[i] + 1) == 0)) { |
| 137 | *attr = (ATTR_TYPES)cpu_to_le32((i + 1) * 16); |
| 138 | return 1; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | num = strtol(value, NULL, 0); |
| 143 | if ((num > 0) && (num < 257)) { |
| 144 | *attr = (ATTR_TYPES)cpu_to_le32(num); |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * parse_options - Read and validate the programs command line |
| 153 | * |
| 154 | * Read the command line, verify the syntax and parse the options. |
| 155 | * This function is very long, but quite simple. |
| 156 | * |
| 157 | * Return: 1 Success |
| 158 | * 0 Error, one or more problems |
| 159 | */ |
| 160 | static int parse_options(int argc, char **argv) |
| 161 | { |
| 162 | static const char *sopt = "-a:fh?i:n:qVvr"; |
| 163 | static const struct option lopt[] = { |
| 164 | { "attribute", required_argument, NULL, 'a' }, |
| 165 | { "attribute-name", required_argument, NULL, 'n' }, |
| 166 | { "force", no_argument, NULL, 'f' }, |
| 167 | { "help", no_argument, NULL, 'h' }, |
| 168 | { "inode", required_argument, NULL, 'i' }, |
| 169 | { "quiet", no_argument, NULL, 'q' }, |
| 170 | { "version", no_argument, NULL, 'V' }, |
| 171 | { "verbose", no_argument, NULL, 'v' }, |
| 172 | { "raw", no_argument, NULL, 'r' }, |
| 173 | { NULL, 0, NULL, 0 } |
| 174 | }; |
| 175 | |
| 176 | int c = -1; |
| 177 | int err = 0; |
| 178 | int ver = 0; |
| 179 | int help = 0; |
| 180 | int levels = 0; |
| 181 | ATTR_TYPES attr = AT_UNUSED; |
| 182 | |
| 183 | opterr = 0; /* We'll handle the errors, thank you. */ |
| 184 | |
| 185 | opts.inode = -1; |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 186 | opts.attr = const_cpu_to_le32(-1); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 187 | opts.attr_name = NULL; |
| 188 | opts.attr_name_len = 0; |
| 189 | |
| 190 | while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { |
| 191 | switch (c) { |
| 192 | case 1: /* A non-option argument */ |
| 193 | if (!opts.device) { |
| 194 | opts.device = argv[optind - 1]; |
| 195 | } else if (!opts.file) { |
| 196 | opts.file = argv[optind - 1]; |
| 197 | } else { |
| 198 | ntfs_log_error("You must specify exactly one " |
| 199 | "file.\n"); |
| 200 | err++; |
| 201 | } |
| 202 | break; |
| 203 | case 'a': |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 204 | if (opts.attr != const_cpu_to_le32(-1)) { |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 205 | ntfs_log_error("You must specify exactly one " |
| 206 | "attribute.\n"); |
| 207 | } else if (parse_attribute(optarg, &attr) > 0) { |
| 208 | opts.attr = attr; |
| 209 | break; |
| 210 | } else { |
| 211 | ntfs_log_error("Couldn't parse attribute.\n"); |
| 212 | } |
| 213 | err++; |
| 214 | break; |
| 215 | case 'f': |
| 216 | opts.force++; |
| 217 | break; |
| 218 | case 'h': |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 219 | help++; |
| 220 | break; |
| 221 | case 'i': |
| 222 | if (opts.inode != -1) |
| 223 | ntfs_log_error("You must specify exactly one inode.\n"); |
| 224 | else if (utils_parse_size(optarg, &opts.inode, FALSE)) |
| 225 | break; |
| 226 | else |
| 227 | ntfs_log_error("Couldn't parse inode number.\n"); |
| 228 | err++; |
| 229 | break; |
| 230 | |
| 231 | case 'n': |
| 232 | opts.attr_name_len = ntfs_mbstoucs(optarg, |
| 233 | &opts.attr_name); |
| 234 | if (opts.attr_name_len < 0) { |
| 235 | ntfs_log_perror("Invalid attribute name '%s'", |
| 236 | optarg); |
| 237 | usage(); |
| 238 | } |
| 239 | |
| 240 | case 'q': |
| 241 | opts.quiet++; |
| 242 | ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); |
| 243 | break; |
| 244 | case 'V': |
| 245 | ver++; |
| 246 | break; |
| 247 | case 'v': |
| 248 | opts.verbose++; |
| 249 | ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); |
| 250 | break; |
| 251 | case 'r': |
| 252 | opts.raw = TRUE; |
| 253 | break; |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 254 | case '?': |
| 255 | if (strncmp (argv[optind-1], "--log-", 6) == 0) { |
| 256 | if (!ntfs_log_parse_option (argv[optind-1])) |
| 257 | err++; |
| 258 | break; |
| 259 | } |
| 260 | /* fall through */ |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 261 | default: |
| 262 | ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]); |
| 263 | err++; |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /* Make sure we're in sync with the log levels */ |
| 269 | levels = ntfs_log_get_levels(); |
| 270 | if (levels & NTFS_LOG_LEVEL_VERBOSE) |
| 271 | opts.verbose++; |
| 272 | if (!(levels & NTFS_LOG_LEVEL_QUIET)) |
| 273 | opts.quiet++; |
| 274 | |
| 275 | if (help || ver) { |
| 276 | opts.quiet = 0; |
| 277 | } else { |
| 278 | if (opts.device == NULL) { |
| 279 | ntfs_log_error("You must specify a device.\n"); |
| 280 | err++; |
| 281 | |
| 282 | } else if (opts.file == NULL && opts.inode == -1) { |
| 283 | ntfs_log_error("You must specify a file or inode " |
| 284 | "with the -i option.\n"); |
| 285 | err++; |
| 286 | |
| 287 | } else if (opts.file != NULL && opts.inode != -1) { |
| 288 | ntfs_log_error("You can't specify both a file and inode.\n"); |
| 289 | err++; |
| 290 | } |
| 291 | |
| 292 | if (opts.quiet && opts.verbose) { |
| 293 | ntfs_log_error("You may not use --quiet and --verbose at the " |
| 294 | "same time.\n"); |
| 295 | err++; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if (ver) |
| 300 | version(); |
| 301 | if (help || err) |
| 302 | usage(); |
| 303 | |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 304 | /* tri-state 0 : done, 1 : error, -1 : proceed */ |
| 305 | return (err ? 1 : (help || ver ? 0 : -1)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /** |
| 309 | * index_get_size - Find the INDX block size from the index root |
| 310 | * @inode: Inode of the directory to be checked |
| 311 | * |
| 312 | * Find the size of a directory's INDX block from the INDEX_ROOT attribute. |
| 313 | * |
| 314 | * Return: n Success, the INDX blocks are n bytes in size |
| 315 | * 0 Error, not a directory |
| 316 | */ |
| 317 | static int index_get_size(ntfs_inode *inode) |
| 318 | { |
| 319 | ATTR_RECORD *attr90; |
| 320 | INDEX_ROOT *iroot; |
| 321 | |
| 322 | attr90 = find_first_attribute(AT_INDEX_ROOT, inode->mrec); |
| 323 | if (!attr90) |
| 324 | return 0; // not a directory |
| 325 | |
| 326 | iroot = (INDEX_ROOT*)((u8*)attr90 + le16_to_cpu(attr90->value_offset)); |
| 327 | return le32_to_cpu(iroot->index_block_size); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * cat |
| 332 | */ |
| 333 | static int cat(ntfs_volume *vol, ntfs_inode *inode, ATTR_TYPES type, |
| 334 | ntfschar *name, int namelen) |
| 335 | { |
| 336 | const int bufsize = 4096; |
| 337 | char *buffer; |
| 338 | ntfs_attr *attr; |
| 339 | s64 bytes_read, written; |
| 340 | s64 offset; |
| 341 | u32 block_size; |
| 342 | |
| 343 | buffer = malloc(bufsize); |
| 344 | if (!buffer) |
| 345 | return 1; |
| 346 | |
| 347 | attr = ntfs_attr_open(inode, type, name, namelen); |
| 348 | if (!attr) { |
| 349 | ntfs_log_error("Cannot find attribute type 0x%x.\n", |
| 350 | le32_to_cpu(type)); |
| 351 | free(buffer); |
| 352 | return 1; |
| 353 | } |
| 354 | |
| 355 | if ((inode->mft_no < 2) && (attr->type == AT_DATA)) |
| 356 | block_size = vol->mft_record_size; |
| 357 | else if (attr->type == AT_INDEX_ALLOCATION) |
| 358 | block_size = index_get_size(inode); |
| 359 | else |
| 360 | block_size = 0; |
| 361 | |
| 362 | offset = 0; |
| 363 | for (;;) { |
| 364 | if (!opts.raw && block_size > 0) { |
| 365 | // These types have fixup |
| 366 | bytes_read = ntfs_attr_mst_pread(attr, offset, 1, block_size, buffer); |
| 367 | if (bytes_read > 0) |
| 368 | bytes_read *= block_size; |
| 369 | } else { |
| 370 | bytes_read = ntfs_attr_pread(attr, offset, bufsize, buffer); |
| 371 | } |
| 372 | //ntfs_log_info("read %lld bytes\n", bytes_read); |
| 373 | if (bytes_read == -1) { |
| 374 | ntfs_log_perror("ERROR: Couldn't read file"); |
| 375 | break; |
| 376 | } |
| 377 | if (!bytes_read) |
| 378 | break; |
| 379 | |
| 380 | written = fwrite(buffer, 1, bytes_read, stdout); |
| 381 | if (written != bytes_read) { |
| 382 | ntfs_log_perror("ERROR: Couldn't output all data!"); |
| 383 | break; |
| 384 | } |
| 385 | offset += bytes_read; |
| 386 | } |
| 387 | |
| 388 | ntfs_attr_close(attr); |
| 389 | free(buffer); |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * main - Begin here |
| 395 | * |
| 396 | * Start from here. |
| 397 | * |
| 398 | * Return: 0 Success, the program worked |
| 399 | * 1 Error, something went wrong |
| 400 | */ |
| 401 | int main(int argc, char *argv[]) |
| 402 | { |
| 403 | ntfs_volume *vol; |
| 404 | ntfs_inode *inode; |
| 405 | ATTR_TYPES attr; |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 406 | int res; |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 407 | int result = 1; |
| 408 | |
| 409 | ntfs_log_set_handler(ntfs_log_handler_stderr); |
| 410 | |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 411 | res = parse_options(argc, argv); |
| 412 | if (res >= 0) |
| 413 | return (res); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 414 | |
| 415 | utils_set_locale(); |
| 416 | |
| 417 | vol = utils_mount_volume(opts.device, NTFS_MNT_RDONLY | |
| 418 | (opts.force ? NTFS_MNT_RECOVER : 0)); |
| 419 | if (!vol) { |
| 420 | ntfs_log_perror("ERROR: couldn't mount volume"); |
| 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | if (opts.inode != -1) |
| 425 | inode = ntfs_inode_open(vol, opts.inode); |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 426 | else { |
| 427 | #ifdef HAVE_WINDOWS_H |
| 428 | char *unix_name; |
| 429 | |
| 430 | unix_name = ntfs_utils_unix_path(opts.file); |
| 431 | if (unix_name) { |
| 432 | inode = ntfs_pathname_to_inode(vol, NULL, |
| 433 | unix_name); |
| 434 | free(unix_name); |
| 435 | } else |
| 436 | inode = (ntfs_inode*)NULL; |
| 437 | #else |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 438 | inode = ntfs_pathname_to_inode(vol, NULL, opts.file); |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 439 | #endif |
| 440 | } |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 441 | |
| 442 | if (!inode) { |
| 443 | ntfs_log_perror("ERROR: Couldn't open inode"); |
| 444 | return 1; |
| 445 | } |
| 446 | |
| 447 | attr = AT_DATA; |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 448 | if (opts.attr != const_cpu_to_le32(-1)) |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 449 | attr = opts.attr; |
| 450 | |
| 451 | result = cat(vol, inode, attr, opts.attr_name, opts.attr_name_len); |
| 452 | |
| 453 | ntfs_inode_close(inode); |
| 454 | ntfs_umount(vol, FALSE); |
| 455 | |
| 456 | return result; |
| 457 | } |