• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
version(void)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  */
usage(void)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  */
parse_attribute(const char * value,ATTR_TYPES * attr)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  */
parse_options(int argc,char ** argv)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;
186 	opts.attr = const_cpu_to_le32(-1);
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':
204 			if (opts.attr != const_cpu_to_le32(-1)) {
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':
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 			break;
240 
241 		case 'q':
242 			opts.quiet++;
243 			ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET);
244 			break;
245 		case 'V':
246 			ver++;
247 			break;
248 		case 'v':
249 			opts.verbose++;
250 			ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE);
251 			break;
252 		case 'r':
253 			opts.raw = TRUE;
254 			break;
255 		case '?':
256 			if (strncmp (argv[optind-1], "--log-", 6) == 0) {
257 				if (!ntfs_log_parse_option (argv[optind-1]))
258 					err++;
259 				break;
260 			}
261 			/* fall through */
262 		default:
263 			ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]);
264 			err++;
265 			break;
266 		}
267 	}
268 
269 	/* Make sure we're in sync with the log levels */
270 	levels = ntfs_log_get_levels();
271 	if (levels & NTFS_LOG_LEVEL_VERBOSE)
272 		opts.verbose++;
273 	if (!(levels & NTFS_LOG_LEVEL_QUIET))
274 		opts.quiet++;
275 
276 	if (help || ver) {
277 		opts.quiet = 0;
278 	} else {
279 		if (opts.device == NULL) {
280 			ntfs_log_error("You must specify a device.\n");
281 			err++;
282 
283 		} else if (opts.file == NULL && opts.inode == -1) {
284 			ntfs_log_error("You must specify a file or inode "
285 				 "with the -i option.\n");
286 			err++;
287 
288 		} else if (opts.file != NULL && opts.inode != -1) {
289 			ntfs_log_error("You can't specify both a file and inode.\n");
290 			err++;
291 		}
292 
293 		if (opts.quiet && opts.verbose) {
294 			ntfs_log_error("You may not use --quiet and --verbose at the "
295 					"same time.\n");
296 			err++;
297 		}
298 	}
299 
300 	if (ver)
301 		version();
302 	if (help || err)
303 		usage();
304 
305 		/* tri-state 0 : done, 1 : error, -1 : proceed */
306 	return (err ? 1 : (help || ver ? 0 : -1));
307 }
308 
309 /**
310  * index_get_size - Find the INDX block size from the index root
311  * @inode:  Inode of the directory to be checked
312  *
313  * Find the size of a directory's INDX block from the INDEX_ROOT attribute.
314  *
315  * Return:  n  Success, the INDX blocks are n bytes in size
316  *	    0  Error, not a directory
317  */
index_get_size(ntfs_inode * inode)318 static int index_get_size(ntfs_inode *inode)
319 {
320 	ATTR_RECORD *attr90;
321 	INDEX_ROOT *iroot;
322 
323 	attr90 = find_first_attribute(AT_INDEX_ROOT, inode->mrec);
324 	if (!attr90)
325 		return 0;	// not a directory
326 
327 	iroot = (INDEX_ROOT*)((u8*)attr90 + le16_to_cpu(attr90->value_offset));
328 	return le32_to_cpu(iroot->index_block_size);
329 }
330 
331 /**
332  * cat
333  */
cat(ntfs_volume * vol,ntfs_inode * inode,ATTR_TYPES type,ntfschar * name,int namelen)334 static int cat(ntfs_volume *vol, ntfs_inode *inode, ATTR_TYPES type,
335 		ntfschar *name, int namelen)
336 {
337 	const int bufsize = 4096;
338 	char *buffer;
339 	ntfs_attr *attr;
340 	s64 bytes_read, written;
341 	s64 offset;
342 	u32 block_size;
343 
344 	buffer = malloc(bufsize);
345 	if (!buffer)
346 		return 1;
347 
348 	attr = ntfs_attr_open(inode, type, name, namelen);
349 	if (!attr) {
350 		ntfs_log_error("Cannot find attribute type 0x%x.\n",
351 				le32_to_cpu(type));
352 		free(buffer);
353 		return 1;
354 	}
355 
356 	if ((inode->mft_no < 2) && (attr->type == AT_DATA))
357 		block_size = vol->mft_record_size;
358 	else if (attr->type == AT_INDEX_ALLOCATION)
359 		block_size = index_get_size(inode);
360 	else
361 		block_size = 0;
362 
363 	offset = 0;
364 	for (;;) {
365 		if (!opts.raw && block_size > 0) {
366 			// These types have fixup
367 			bytes_read = ntfs_attr_mst_pread(attr, offset, 1, block_size, buffer);
368 			if (bytes_read > 0)
369 				bytes_read *= block_size;
370 		} else {
371 			bytes_read = ntfs_attr_pread(attr, offset, bufsize, buffer);
372 		}
373 		//ntfs_log_info("read %lld bytes\n", bytes_read);
374 		if (bytes_read == -1) {
375 			ntfs_log_perror("ERROR: Couldn't read file");
376 			break;
377 		}
378 		if (!bytes_read)
379 			break;
380 
381 		written = fwrite(buffer, 1, bytes_read, stdout);
382 		if (written != bytes_read) {
383 			ntfs_log_perror("ERROR: Couldn't output all data!");
384 			break;
385 		}
386 		offset += bytes_read;
387 	}
388 
389 	ntfs_attr_close(attr);
390 	free(buffer);
391 	return 0;
392 }
393 
394 /**
395  * main - Begin here
396  *
397  * Start from here.
398  *
399  * Return:  0  Success, the program worked
400  *	    1  Error, something went wrong
401  */
main(int argc,char * argv[])402 int main(int argc, char *argv[])
403 {
404 	ntfs_volume *vol;
405 	ntfs_inode *inode;
406 	ATTR_TYPES attr;
407 	int res;
408 	int result = 1;
409 
410 	ntfs_log_set_handler(ntfs_log_handler_stderr);
411 
412 	res = parse_options(argc, argv);
413 	if (res >= 0)
414 		return (res);
415 
416 	utils_set_locale();
417 
418 	vol = utils_mount_volume(opts.device, NTFS_MNT_RDONLY |
419 			(opts.force ? NTFS_MNT_RECOVER : 0));
420 	if (!vol) {
421 		ntfs_log_perror("ERROR: couldn't mount volume");
422 		return 1;
423 	}
424 
425 	if (opts.inode != -1)
426 		inode = ntfs_inode_open(vol, opts.inode);
427 	else {
428 #ifdef HAVE_WINDOWS_H
429 		char *unix_name;
430 
431 		unix_name = ntfs_utils_unix_path(opts.file);
432 		if (unix_name) {
433 			inode = ntfs_pathname_to_inode(vol, NULL,
434 					unix_name);
435 			free(unix_name);
436 		} else
437 			inode = (ntfs_inode*)NULL;
438 #else
439 		inode = ntfs_pathname_to_inode(vol, NULL, opts.file);
440 #endif
441 	}
442 
443 	if (!inode) {
444 		ntfs_log_perror("ERROR: Couldn't open inode");
445 		return 1;
446 	}
447 
448 	attr = AT_DATA;
449 	if (opts.attr != const_cpu_to_le32(-1))
450 		attr = opts.attr;
451 
452 	result = cat(vol, inode, attr, opts.attr_name, opts.attr_name_len);
453 
454 	ntfs_inode_close(inode);
455 	ntfs_umount(vol, FALSE);
456 
457 	return result;
458 }
459