~ubuntu-branches/ubuntu/wily/foremost/wily-proposed

« back to all changes in this revision

Viewing changes to main.c

  • Committer: Bazaar Package Importer
  • Author(s): Gürkan Sengün
  • Date: 2006-09-03 19:57:55 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 edgy)
  • Revision ID: james.westby@ubuntu.com-20060903195755-v0pk4s2n4d9fjxar
Tags: 1.3-1
* New upstream version.
* Bump standards version (no changes needed).
* Bump DH compat level to 5.
* Updated debian/copyright.
* Removed co-maintainer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
 
 
4
/* FOREMOST
 
5
 *
 
6
 * By Jesse Kornblum and Kris Kendall
 
7
 * 
 
8
 * This is a work of the US Government. In accordance with 17 USC 105,
 
9
 * copyright protection is not available for any work of the US Government.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful, but
 
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
14
 *
 
15
 * Modification by Nick Mikus 11-04-05
 
16
 *
 
17
 */
 
18
#include "main.h"
 
19
 
 
20
#ifdef __WIN32
 
21
 
 
22
/* Allows us to open standard input in binary mode by default 
 
23
   See http://gnuwin32.sourceforge.net/compile.html for more */
 
24
int _CRT_fmode = _O_BINARY;
 
25
#endif
 
26
 
 
27
void catch_alarm(int signum)
 
28
{
 
29
        signal_caught = signum;
 
30
        signal(signum, catch_alarm);
 
31
}
 
32
 
 
33
void register_signal_handler(void)
 
34
{
 
35
        signal_caught = 0;
 
36
 
 
37
        if (signal(SIGINT, catch_alarm) == SIG_IGN)
 
38
                signal(SIGINT, SIG_IGN);
 
39
        if (signal(SIGTERM, catch_alarm) == SIG_IGN)
 
40
                signal(SIGTERM, SIG_IGN);
 
41
 
 
42
#ifndef __WIN32
 
43
 
 
44
        /* Note: I haven't found a way to get notified of
 
45
     console resize events in Win32.  Right now the statusbar
 
46
     will be too long or too short if the user decides to resize
 
47
     their console window while foremost runs.. */
 
48
 
 
49
        /* RBF - Handle TTY events  */
 
50
 
 
51
        // The function setttywidth is in the old helpers.c
 
52
        // signal(SIGWINCH, setttywidth);
 
53
#endif
 
54
}
 
55
 
 
56
void try_msg(void)
 
57
{
 
58
        fprintf(stderr, "Try `%s -h` for more information.%s", __progname, NEWLINE);
 
59
}
 
60
 
 
61
/* The usage function should, at most, display 22 lines of text to fit
 
62
   on a single screen */
 
63
void usage(void)
 
64
{
 
65
        fprintf(stderr, "%s version %s by %s.%s", __progname, VERSION, AUTHOR, NEWLINE);
 
66
        fprintf(stderr,
 
67
                        "%s %s [-v|-V|-h|-T|-Q|-q|-a|-w-d] [-t <type>] [-s <blocks>] [-k <size>] \n\t[-b <size>] [-c <file>] [-o <dir>] [-i <file] %s%s",
 
68
                CMD_PROMPT,
 
69
                        __progname,
 
70
                        NEWLINE,
 
71
                        NEWLINE);
 
72
        fprintf(stderr, "-V  - display copyright information and exit%s", NEWLINE);
 
73
        fprintf(stderr, "-t  - specify file type.  (-t jpeg pdf ...) %s", NEWLINE);
 
74
        fprintf(stderr, "-d  - turn on indirect block detection (for UNIX file-systems) %s", NEWLINE);
 
75
        fprintf(stderr, "-i  - specify input file (default is stdin) %s", NEWLINE);
 
76
        fprintf(stderr,
 
77
                        "-a  - Write all headers, perform no error detection (corrupted files) %s",
 
78
                        NEWLINE);
 
79
        fprintf(stderr,
 
80
                        "-w  - Only write the audit file, do not write any detected files to the disk %s",
 
81
                        NEWLINE);
 
82
        fprintf(stderr,
 
83
                        "-o  - set output directory (defaults to %s)%s",
 
84
                        DEFAULT_OUTPUT_DIRECTORY,
 
85
                        NEWLINE);
 
86
        fprintf(stderr,
 
87
                        "-c  - set configuration file to use (defaults to %s)%s",
 
88
                        DEFAULT_CONFIG_FILE,
 
89
                        NEWLINE);
 
90
        fprintf(stderr,
 
91
                        "-q  - enables quick mode. Search are performed on 512 byte boundaries.%s",
 
92
                        NEWLINE);
 
93
        fprintf(stderr, "-Q  - enables quiet mode. Suppress output messages. %s", NEWLINE);
 
94
 
 
95
        /* RBF - What should verbose mode be? */
 
96
        fprintf(stderr, "-v  - verbose mode. Logs all messages to screen%s", NEWLINE);
 
97
}
 
98
 
 
99
void process_command_line(int argc, char **argv, f_state *s)
 
100
{
 
101
 
 
102
        int             i;
 
103
        char    *ptr1, *ptr2;
 
104
 
 
105
        while ((i = getopt(argc, argv, "o:b:c:t:s:i:k:hqmQTadvVw")) != -1)
 
106
                {
 
107
                switch (i)
 
108
                        {
 
109
 
 
110
                        case 'v':
 
111
                                set_mode(s, mode_verbose);
 
112
                                break;
 
113
 
 
114
                        case 'd':
 
115
                                set_mode(s, mode_ind_blk);
 
116
                                break;
 
117
 
 
118
                        case 'w':
 
119
                                set_mode(s, mode_write_audit);  /*Only write audit*/
 
120
                                break;
 
121
 
 
122
                        case 'a':
 
123
                                set_mode(s, mode_write_all);    /*Write all headers*/
 
124
                                break;
 
125
 
 
126
                        case 'b':
 
127
                                set_block(s, atoi(optarg));
 
128
                                break;
 
129
 
 
130
                        case 'o':
 
131
                                set_output_directory(s, optarg);
 
132
                                break;
 
133
 
 
134
                        case 'q':
 
135
                                set_mode(s, mode_quick);
 
136
                                break;
 
137
 
 
138
                        case 'Q':
 
139
                                set_mode(s, mode_quiet);
 
140
                                break;
 
141
 
 
142
                        case 'c':
 
143
                                set_config_file(s, optarg);
 
144
                                break;
 
145
 
 
146
                        case 'm':
 
147
                                set_mode(s, mode_multi_file);
 
148
 
 
149
                        case 'k':
 
150
                                set_chunk(s, atoi(optarg));
 
151
                                break;
 
152
 
 
153
                        case 's':
 
154
                                set_skip(s, atoi(optarg));
 
155
                                break;
 
156
 
 
157
                        case 'i':
 
158
                                set_input_file(s, optarg);
 
159
                                break;
 
160
 
 
161
                        case 'T':
 
162
                                s->time_stamp = TRUE;
 
163
                                break;
 
164
 
 
165
                        case 't':
 
166
 
 
167
                                /*See if we have multiple file types to define*/
 
168
                                ptr1 = ptr2 = optarg;
 
169
                                while (1)
 
170
                                        {
 
171
                                        if (!*ptr2)
 
172
                                                {
 
173
                                                if (!set_search_def(s, ptr1, 0))
 
174
                                                        {
 
175
                                                        usage();
 
176
                                                        exit(EXIT_SUCCESS);
 
177
                                                        }
 
178
                                                break;
 
179
                                                }
 
180
 
 
181
                                        if (*ptr2 == ',')
 
182
                                                {
 
183
                                                *ptr2 = '\0';
 
184
                                                if (!set_search_def(s, ptr1, 0))
 
185
                                                        {
 
186
                                                        usage();
 
187
                                                        exit(EXIT_SUCCESS);
 
188
                                                        }
 
189
 
 
190
                                                *ptr2++ = ',';
 
191
                                                ptr1 = ptr2;
 
192
                                                }
 
193
                                        else
 
194
                                                {
 
195
                                                ptr2++;
 
196
                                                }
 
197
                                        }
 
198
                                break;
 
199
 
 
200
                        case 'h':
 
201
                                usage();
 
202
                                exit(EXIT_SUCCESS);
 
203
 
 
204
                        case 'V':
 
205
                                printf("%s%s", VERSION, NEWLINE);
 
206
 
 
207
                                /* We could just say printf(COPYRIGHT), but that's a good way
 
208
         to introduce a format string vulnerability. Better to always
 
209
         use good programming practice... */
 
210
                                printf("%s", COPYRIGHT);
 
211
                                exit(EXIT_SUCCESS);
 
212
 
 
213
                        default:
 
214
                                try_msg();
 
215
                                exit(EXIT_FAILURE);
 
216
 
 
217
                        }
 
218
 
 
219
                }
 
220
 
 
221
#ifdef __DEBUG
 
222
        dump_state(s);
 
223
#endif
 
224
 
 
225
}
 
226
 
 
227
int main(int argc, char **argv)
 
228
{
 
229
 
 
230
        FILE    *testFile = NULL;
 
231
        f_state *s = (f_state *)malloc(sizeof(f_state));
 
232
        int             input_files = 0;
 
233
        char    **temp = argv;
 
234
        DIR*    dir;
 
235
 
 
236
#ifndef __GLIBC__
 
237
        __progname = basename(argv[0]);
 
238
#endif
 
239
 
 
240
        /*Initialize the global state struct*/
 
241
        if (initialize_state(s, argc, argv))
 
242
                fatal_error(s, "Unable to initialize state");
 
243
 
 
244
        register_signal_handler();
 
245
        process_command_line(argc, argv, s);
 
246
 
 
247
        load_config_file(s);
 
248
 
 
249
        if (s->num_builtin == 0)
 
250
                {
 
251
 
 
252
                /*Nothing specified via the command line or the conf
 
253
        file so default to all builtin search types*/
 
254
                set_search_def(s, "all", 0);
 
255
                }
 
256
 
 
257
        if (create_output_directory(s))
 
258
                fatal_error(s, "Unable to open output directory");
 
259
 
 
260
        if (!get_mode(s, mode_write_audit))
 
261
                {
 
262
                create_sub_dirs(s);
 
263
                }
 
264
 
 
265
        if (open_audit_file(s))
 
266
                fatal_error(s, "Can't open audit file");
 
267
 
 
268
        /* Scan for valid files to open */
 
269
        while (*argv != NULL)
 
270
        {
 
271
                testFile = fopen(*argv, "rb");
 
272
                if (testFile)
 
273
                {
 
274
                        fclose(testFile);
 
275
                        dir = opendir(*argv);
 
276
                        if(!strstr(s->config_file,*argv)!=0 && !dir)
 
277
                        {
 
278
                                //fprintf(stderr,"Testing %s against %s\n",*argv,s->config_file);
 
279
                                input_files++;
 
280
                        }
 
281
                        
 
282
                        if(dir) closedir(dir);          
 
283
                }
 
284
 
 
285
                ++argv;
 
286
        }
 
287
 
 
288
        argv = temp;
 
289
        if (input_files > 1)
 
290
                {
 
291
                set_mode(s, mode_multi_file);
 
292
                }
 
293
 
 
294
        ++argv;
 
295
        while (*argv != NULL)
 
296
                {
 
297
 
 
298
                //printf("*argv=%s\n",*argv);
 
299
                testFile = fopen(*argv, "rb");
 
300
 
 
301
                if (testFile)
 
302
                        {
 
303
                                fclose(testFile);
 
304
                                dir = opendir(*argv);
 
305
                                if(!strstr(s->config_file,*argv)!=0 && !dir)
 
306
                                {
 
307
                                        set_input_file(s, *argv);
 
308
                                        process_file(s);
 
309
                                }
 
310
                                if(dir) closedir(dir);  
 
311
                        }
 
312
 
 
313
                ++argv;
 
314
                }
 
315
 
 
316
        if (input_files == 0)
 
317
                {
 
318
 
 
319
                //printf("using stdin\n");
 
320
                process_stdin(s);
 
321
                }
 
322
 
 
323
        print_stats(s);
 
324
 
 
325
        /*Lets try to clean up some of the extra sub_dirs*/
 
326
        cleanup_output(s);
 
327
 
 
328
        if (close_audit_file(s))
 
329
                {
 
330
 
 
331
                /* Hells bells. This is bad, but really, what can we do about it? 
 
332
       Let's just report the error and try to get out of here! */
 
333
                print_error(s, AUDIT_FILE_NAME, "Error closing audit file");
 
334
                }
 
335
 
 
336
        free_state(s);
 
337
        free(s);
 
338
        return EXIT_SUCCESS;
 
339
}