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