40c466130f03f645f2e69d6d76f4000b2c688346
[proxmark3-svn] / client / proxmark3.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 //
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
7 // the license.
8 //-----------------------------------------------------------------------------
9 // Main binary
10 //-----------------------------------------------------------------------------
11
12 #include "proxmark3.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <pthread.h>
18 #include <unistd.h>
19 #include <readline/readline.h>
20 #include <readline/history.h>
21
22 #include "util_posix.h"
23 #include "proxgui.h"
24 #include "cmdmain.h"
25 #include "ui.h"
26 #include "util.h"
27 #include "cmdparser.h"
28 #include "cmdhw.h"
29 #include "whereami.h"
30 #include "comms.h"
31
32 void
33 #ifdef __has_attribute
34 #if __has_attribute(force_align_arg_pointer)
35 __attribute__((force_align_arg_pointer))
36 #endif
37 #endif
38 main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
39 char *cmd = NULL;
40 bool execCommand = (script_cmd != NULL);
41 bool stdinOnPipe = !isatty(STDIN_FILENO);
42
43 if (usb_present) {
44 SetOffline(false);
45 // cache Version information now:
46 CmdVersion(NULL);
47 } else {
48 SetOffline(true);
49 }
50
51 // file with script
52 FILE *script_file = NULL;
53 char script_cmd_buf[256] = {0}; // iceman, needs lua script the same file_path_buffer as the rest
54
55 if (script_cmds_file) {
56 script_file = fopen(script_cmds_file, "r");
57 if (script_file) {
58 printf("executing commands from file: %s\n", script_cmds_file);
59 }
60 }
61
62 read_history(".history");
63
64 while (1) {
65 // If there is a script file
66 if (script_file)
67 {
68 memset(script_cmd_buf, 0, sizeof(script_cmd_buf));
69 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file)) {
70 fclose(script_file);
71 script_file = NULL;
72 } else {
73 strcleanrn(script_cmd_buf, sizeof(script_cmd_buf));
74
75 if ((cmd = strmcopy(script_cmd_buf)) != NULL) {
76 printf(PROXPROMPT"%s\n", cmd);
77 }
78 }
79 } else {
80 // If there is a script command
81 if (execCommand){
82 if ((cmd = strmcopy(script_cmd)) != NULL) {
83 printf(PROXPROMPT"%s\n", cmd);
84 }
85
86 execCommand = false;
87 } else {
88 // exit after exec command
89 if (script_cmd)
90 break;
91
92 // if there is a pipe from stdin
93 if (stdinOnPipe) {
94 memset(script_cmd_buf, 0, sizeof(script_cmd_buf));
95 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), stdin)) {
96 printf("\nStdin end. Exit...\n");
97 break;
98 }
99 strcleanrn(script_cmd_buf, sizeof(script_cmd_buf));
100
101 if ((cmd = strmcopy(script_cmd_buf)) != NULL) {
102 printf(PROXPROMPT"%s\n", cmd);
103 }
104
105 } else {
106 // read command from command prompt
107 cmd = readline(PROXPROMPT);
108 }
109 }
110 }
111
112 // execute command
113 if (cmd) {
114
115 while(cmd[strlen(cmd) - 1] == ' ')
116 cmd[strlen(cmd) - 1] = 0x00;
117
118 if (cmd[0] != 0x00) {
119 int ret = CommandReceived(cmd);
120 add_history(cmd);
121 if (ret == 99) { // exit or quit
122 break;
123 }
124 }
125 free(cmd);
126 cmd = NULL;
127 } else {
128 printf("\n");
129 break;
130 }
131 }
132
133 write_history(".history");
134
135 if (usb_present) {
136 CloseProxmark();
137 }
138
139 if (script_file) {
140 fclose(script_file);
141 script_file = NULL;
142 }
143 }
144
145 static void dumpAllHelp(int markdown)
146 {
147 printf("\n%sProxmark3 command dump%s\n\n",markdown?"# ":"",markdown?"":"\n======================");
148 printf("Some commands are available only if a Proxmark is actually connected.%s\n",markdown?" ":"");
149 printf("Check column \"offline\" for their availability.\n");
150 printf("\n");
151 command_t *cmds = getTopLevelCommandTable();
152 dumpCommandsRecursive(cmds, markdown);
153 }
154
155 static char *my_executable_path = NULL;
156 static char *my_executable_directory = NULL;
157
158 const char *get_my_executable_path(void)
159 {
160 return my_executable_path;
161 }
162
163 const char *get_my_executable_directory(void)
164 {
165 return my_executable_directory;
166 }
167
168 static void set_my_executable_path(void)
169 {
170 int path_length = wai_getExecutablePath(NULL, 0, NULL);
171 if (path_length != -1) {
172 my_executable_path = (char*)malloc(path_length + 1);
173 int dirname_length = 0;
174 if (wai_getExecutablePath(my_executable_path, path_length, &dirname_length) != -1) {
175 my_executable_path[path_length] = '\0';
176 my_executable_directory = (char *)malloc(dirname_length + 2);
177 strncpy(my_executable_directory, my_executable_path, dirname_length+1);
178 my_executable_directory[dirname_length+1] = '\0';
179 }
180 }
181 }
182
183 static void show_help(bool showFullHelp, char *command_line){
184 printf("syntax: %s <port> [-h|-help|-m|-f|-flush|-w|-wait|-c|-command|-l|-lua] [cmd_script_file_name] [command][lua_script_name]\n", command_line);
185 printf("\texample: %s "SERIAL_PORT_H"\n\n", command_line);
186
187 if (showFullHelp){
188 printf("help: <-h|-help> Dump all interactive command's help at once.\n");
189 printf("\t%s -h\n\n", command_line);
190 printf("markdown: <-m> Dump all interactive help at once in markdown syntax\n");
191 printf("\t%s -m\n\n", command_line);
192 printf("flush: <-f|-flush> Output will be flushed after every print.\n");
193 printf("\t%s -f\n\n", command_line);
194 printf("wait: <-w|-wait> 20sec waiting the serial port to appear in the OS\n");
195 printf("\t%s "SERIAL_PORT_H" -w\n\n", command_line);
196 printf("script: A script file with one proxmark3 command per line.\n\n");
197 printf("command: <-c|-command> Execute one proxmark3 command.\n");
198 printf("\t%s "SERIAL_PORT_H" -c \"hf mf chk 1* ?\"\n", command_line);
199 printf("\t%s "SERIAL_PORT_H" -command \"hf mf nested 1 *\"\n\n", command_line);
200 printf("lua: <-l|-lua> Execute lua script.\n");
201 printf("\t%s "SERIAL_PORT_H" -l hf_read\n\n", command_line);
202 }
203 }
204
205 int main(int argc, char* argv[]) {
206 srand(time(0));
207
208 bool usb_present = false;
209 bool waitCOMPort = false;
210 bool executeCommand = false;
211 bool addLuaExec = false;
212 char *script_cmds_file = NULL;
213 char *script_cmd = NULL;
214
215 if (argc < 2) {
216 show_help(true, argv[0]);
217 return 1;
218 }
219
220 for (int i = 1; i < argc; i++) {
221 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i],"-help") == 0) {
222 show_help(false, argv[0]);
223 dumpAllHelp(0);
224 return 0;
225 }
226
227 if (strcmp(argv[i], "-m") == 0) {
228 dumpAllHelp(1);
229 return 0;
230 }
231
232 if(strcmp(argv[i],"-f") == 0 || strcmp(argv[i],"-flush") == 0){
233 printf("Output will be flushed after every print.\n");
234 SetFlushAfterWrite(true);
235 }
236
237 if(strcmp(argv[i],"-w") == 0 || strcmp(argv[i],"-wait") == 0){
238 waitCOMPort = true;
239 }
240
241 if(strcmp(argv[i],"-c") == 0 || strcmp(argv[i],"-command") == 0){
242 executeCommand = true;
243 }
244
245 if(strcmp(argv[i],"-l") == 0 || strcmp(argv[i],"-lua") == 0){
246 executeCommand = true;
247 addLuaExec = true;
248 }
249 }
250
251 // If the user passed the filename of the 'script' to execute, get it from last parameter
252 if (argc > 2 && argv[argc - 1] && argv[argc - 1][0] != '-') {
253 if (executeCommand){
254 script_cmd = argv[argc - 1];
255
256 while(script_cmd[strlen(script_cmd) - 1] == ' ')
257 script_cmd[strlen(script_cmd) - 1] = 0x00;
258
259 if (strlen(script_cmd) == 0) {
260 script_cmd = NULL;
261 } else {
262 if (addLuaExec){
263 // add "script run " to command
264 char *ctmp = NULL;
265 int len = strlen(script_cmd) + 11 + 1;
266 if ((ctmp = (char*) malloc(len)) != NULL) {
267 memset(ctmp, 0, len);
268 strcpy(ctmp, "script run ");
269 strcpy(&ctmp[11], script_cmd);
270 script_cmd = ctmp;
271 }
272 }
273
274 printf("Execute command from commandline: %s\n", script_cmd);
275 }
276 } else {
277 script_cmds_file = argv[argc - 1];
278 }
279 }
280
281 // check command
282 if (executeCommand && (!script_cmd || strlen(script_cmd) == 0)){
283 printf("ERROR: execute command: command not found.\n");
284 return 2;
285 }
286
287 // set global variables
288 set_my_executable_path();
289
290 // try to open USB connection to Proxmark
291 usb_present = OpenProxmark(argv[1], waitCOMPort, 20, false);
292
293 #ifdef HAVE_GUI
294 #ifdef _WIN32
295 InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present);
296 MainGraphics();
297 #else
298 char* display = getenv("DISPLAY");
299
300 if (display && strlen(display) > 1)
301 {
302 InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present);
303 MainGraphics();
304 }
305 else
306 {
307 main_loop(script_cmds_file, script_cmd, usb_present);
308 }
309 #endif
310 #else
311 main_loop(script_cmds_file, script_cmd, usb_present);
312 #endif
313
314 // Clean up the port
315 if (usb_present) {
316 CloseProxmark();
317 }
318
319 exit(0);
320 }
Impressum, Datenschutz