Inital commit
[rarcrack.git] / src / main.c
bloba211de7b34951708f546c459f77001aadf984701
1 /*
2 * Copyright (C) 2009 by David Kedves <kedazo@gmail.com>
4 * rarcrack is a password cracker, it's supports lot of types of archives.
6 * This file is part of rarcrack.
8 * Rarcrack is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Rarcrack is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with rarcrack. If not, see <http://www.gnu.org/licenses/>.
23 #define VERSION "0.3"
25 #include "rarcrack.h"
26 #include "state.h"
27 #include "file.h"
29 params parameters; /* main parameters */
30 gint minimum_length = 1;
31 gboolean just_temp = TRUE;
32 gboolean list_supported_archives = FALSE;
34 static GOptionEntry entries[] = {
35 { "bruteforce", 'b', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
36 &just_temp, "Bruteforce cracking [Default]", NULL },
37 { "dictionary", 'd', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
38 &parameters.file_dict, "Dictionary attack (- means from stdin)", "dict" },
39 { "current", 'c', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
40 &parameters.current, "Starting password", "firstpass" },
41 { "type", 't', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
42 &parameters.file_type, "Archive file type", "type" },
43 { "list", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
44 &list_supported_archives, "List of supported archive types", NULL},
45 { "charset", 's', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
46 &parameters.charset, "Character set (only on bruteforce)", "set" },
47 { "minlen", 'm', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
48 &minimum_length, "Minimum password length (only on bruteforce)", "X"},
49 { "threads", 'p', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
50 &parameters.threads, "Number of parallel cracking threads", "N" },
51 { NULL }
54 int main(int argc, char** argv) {
55 GError *error = NULL;
56 GOptionContext *context;
57 /* some GLibrary initialization: */
58 if (!g_thread_supported ()) g_thread_init (NULL);
60 parameters.file_dict = NULL;
61 parameters.current = NULL;
62 parameters.file_type = NULL;
63 parameters.charset = NULL;
64 parameters.threads = 1;
66 /* print program name and some version info */
67 g_printf("RarCrack! %s\n\n", VERSION);
69 context = g_option_context_new("filename - crack archive files");
70 g_option_context_add_main_entries(context, entries, NULL);
71 g_option_context_set_description(context,
72 "Web:\n http://rarcrack.sourceforge.net\n\n"
73 "Author:\n David Kedves <kedazo@gmail.com>\n");
74 if (!g_option_context_parse(context, &argc, &argv, &error)) {
75 g_print("Error: %s\n", error->message);
76 return 1; /* exiting with error code 1 */
79 if (list_supported_archives) {
80 file_packer_list_supported();
81 return 0;
84 if (argc == 2) { /* we got only one filename */
85 if (! g_file_test(argv[1], G_FILE_TEST_IS_REGULAR | G_FILE_TEST_EXISTS)) {
86 g_printf("Error: No such file: '%s'\n\n", argv[1]);
87 return 1; /* return with error */
89 parameters.file_ext = g_strdup(argv[1]);
90 parameters.file_status = g_strdup_printf("%s.rcs", argv[1]);
91 g_printf("Status filename: '%s'\n", parameters.file_status);
92 } else {
93 if (argc > 2)
94 g_printf("Error: Multiple files specified in parameter list.\n"
95 " The program can crack only one file.\n");
96 else
97 g_printf("Error: Missing filename!\n");
98 g_printf("Info: See '%s --help'\n\n", argv[0]);
99 return 1; /* returning with error */
102 if (parameters.file_type) {
103 g_printf("Info: forced archive type: %s\n", parameters.file_type);
104 parameters.file_type_id = file_packer_get_id(parameters.file_type,
105 FALSE);
106 } else
107 parameters.file_type_id = file_identify(parameters.file_ext, FALSE);
108 switch (parameters.file_type_id) {
109 case FILE_ERROR:
110 g_printf("Error: I can't open archive file\n"
111 " Please check the permissions!\n\n");
112 break;
113 case FILE_NOTYPE:
114 if (parameters.file_type)
115 g_printf("Error: RarCrack! isn't support this file type!\n\n");
116 else
117 g_printf("Error: RarCrack! can't detect the archive type!\n"
118 "Info: If you sure about type, you may try forcing it...\n"
119 " See help (%s --help) for more info!\n\n", argv[0]);
120 return 1;
121 break; /* Control never reach this line ...*/
122 case FILE_NOPACKER: {
123 gchar* you_need_this;
124 if (parameters.file_type) {
125 you_need_this = file_packer_get_command(
126 file_packer_get_id(parameters.file_type, TRUE), TRUE);
127 } else {
128 you_need_this = file_packer_get_command(
129 file_identify(parameters.file_ext, TRUE), TRUE);
131 g_printf("Error: RarCrack! can't run '%s' command, please ensure of\n"
132 "it is installed and available in you PATH\n\n", you_need_this);
133 return 1;
135 break;
136 default:
137 if (!parameters.file_type) {
138 parameters.file_type = file_packer_get_type(
139 parameters.file_type_id);
140 g_printf("Info: archive type: '%s'\n",
141 parameters.file_type);
143 break;
147 return 0;