]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdscript.c
b2c892d10b93eda4fc3d5def641292e7f5b53b69
[proxmark3-svn] / client / cmdscript.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Data and Graph commands
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <limits.h>
15 #include <sys/types.h>
16 #include <dirent.h>
17
18 #include "proxmark3.h"
19 #include "data.h"
20 #include "ui.h"
21 #include "graph.h"
22 #include "cmdparser.h"
23 #include "cmdmain.h"
24 #include "cmdscript.h"
25 #include "cmdhfmf.h"
26
27 #include <lua.h>
28 #include <lualib.h>
29 #include <lauxlib.h>
30
31
32
33 static int CmdHelp(const char *Cmd);
34 static int CmdList(const char *Cmd);
35 static int CmdRun(const char *Cmd);
36
37 command_t CommandTable[] =
38 {
39 {"help", CmdHelp, 1, "This help"},
40 {"list", CmdList, 1, "<name> -- List available scripts"},
41 {"run", CmdRun, 1, "Execute a script"},
42 {NULL, NULL, 0, NULL}
43 };
44
45 int str_ends_with(const char * str, const char * suffix) {
46
47 if( str == NULL || suffix == NULL )
48 return 0;
49
50 size_t str_len = strlen(str);
51 size_t suffix_len = strlen(suffix);
52
53 if(suffix_len > str_len)
54 return 0;
55
56 return 0 == strncmp( str + str_len - suffix_len, suffix, suffix_len );
57 }
58 /**
59 * Shows some basic help
60 * @brief CmdHelp
61 * @param Cmd
62 * @return
63 */
64 int CmdHelp(const char * Cmd)
65 {
66 PrintAndLog("This is a feature to run Lua-scripts. You can place lua-scripts within the scripts/-folder. ");
67 }
68
69 /**
70 * Generate list of available commands, what it does is
71 * generate a file listing of the script-directory for files
72 * ending with .lua
73 */
74 int CmdList(const char *Cmd)
75 {
76 DIR *dp;
77 struct dirent *ep;
78 dp = opendir ("./scripts/");
79 if (dp != NULL)
80 {
81 while (ep = readdir (dp))
82 {
83 if(ep->d_name != NULL && str_ends_with(ep->d_name, ".lua"))
84 PrintAndLog("%-16s %s", ep->d_name, "A script file");
85 }
86 (void) closedir (dp);
87 }
88 else
89 PrintAndLog ("Couldn't open the directory");
90 return 0;
91 }
92 /**
93 * Finds a matching script-file
94 * @brief CmdScript
95 * @param Cmd
96 * @return
97 */
98 int CmdScript(const char *Cmd)
99 {
100 CmdsParse(CommandTable, Cmd);
101 return 0;
102 }
103 /*
104 static int l_hfmf (lua_State *L) {
105 return CmdHFMF("wohoo");
106
107 }
108 */
109 static int l_CmdHelp(lua_State *L){ return CmdHelp('foo');}
110 static int l_CmdHF14AMfDbg(lua_State *L){ return CmdHF14AMfDbg('foo');}
111 static int l_CmdHF14AMfRdBl(lua_State *L){ return CmdHF14AMfRdBl('foo');}
112 static int l_CmdHF14AMfRdSc(lua_State *L){ return CmdHF14AMfRdSc('foo');}
113 static int l_CmdHF14AMfDump(lua_State *L){ return CmdHF14AMfDump('foo');}
114 static int l_CmdHF14AMfRestore(lua_State *L){ return CmdHF14AMfRestore('foo');}
115 static int l_CmdHF14AMfWrBl(lua_State *L){ return CmdHF14AMfWrBl('foo');}
116 static int l_CmdHF14AMfChk(lua_State *L){ return CmdHF14AMfChk('foo');}
117 static int l_CmdHF14AMifare(lua_State *L){ return CmdHF14AMifare(luaL_checkstring(L, 1));}
118 static int l_CmdHF14AMfNested(lua_State *L){ return CmdHF14AMfNested(luaL_checkstring(L, 1));}
119 static int l_CmdHF14AMfSniff(lua_State *L){ return CmdHF14AMfSniff('foo');}
120 static int l_CmdHF14AMf1kSim(lua_State *L){ return CmdHF14AMf1kSim('foo');}
121 static int l_CmdHF14AMfEClear(lua_State *L){ return CmdHF14AMfEClear('foo');}
122 static int l_CmdHF14AMfEGet(lua_State *L){ return CmdHF14AMfEGet('foo');}
123 static int l_CmdHF14AMfESet(lua_State *L){ return CmdHF14AMfESet('foo');}
124 static int l_CmdHF14AMfELoad(lua_State *L){ return CmdHF14AMfELoad('foo');}
125 static int l_CmdHF14AMfESave(lua_State *L){ return CmdHF14AMfESave('foo');}
126 static int l_CmdHF14AMfECFill(lua_State *L){ return CmdHF14AMfECFill('foo');}
127 static int l_CmdHF14AMfEKeyPrn(lua_State *L){ return CmdHF14AMfEKeyPrn('foo');}
128 static int l_CmdHF14AMfCSetUID(lua_State *L){ return CmdHF14AMfCSetUID('foo');}
129 static int l_CmdHF14AMfCSetBlk(lua_State *L){ return CmdHF14AMfCSetBlk('foo');}
130 static int l_CmdHF14AMfCGetBlk(lua_State *L){ return CmdHF14AMfCGetBlk('foo');}
131 static int l_CmdHF14AMfCGetSc(lua_State *L){ return CmdHF14AMfCGetSc('foo');}
132 static int l_CmdHF14AMfCLoad(lua_State *L){ return CmdHF14AMfCLoad('foo');}
133 static int l_CmdHF14AMfCSave(lua_State *L){ return CmdHF14AMfCSave('foo');}
134
135 int luaopen_hfmf(lua_State *L)
136 {
137 static const luaL_Reg hfmf_lib[] = {
138 {"help", l_CmdHelp},
139 {"dbg", l_CmdHF14AMfDbg},
140 {"rdbl", l_CmdHF14AMfRdBl},
141 {"rdsc", l_CmdHF14AMfRdSc},
142 {"dump", l_CmdHF14AMfDump},
143 {"restore", l_CmdHF14AMfRestore},
144 {"wrbl", l_CmdHF14AMfWrBl},
145 {"chk", l_CmdHF14AMfChk},
146 {"mifare", l_CmdHF14AMifare},
147 {"nested", l_CmdHF14AMfNested},
148 {"sniff", l_CmdHF14AMfSniff},
149 {"sim", l_CmdHF14AMf1kSim},
150 {"eclr", l_CmdHF14AMfEClear},
151 {"eget", l_CmdHF14AMfEGet},
152 {"eset", l_CmdHF14AMfESet},
153 {"eload", l_CmdHF14AMfELoad},
154 {"esave", l_CmdHF14AMfESave},
155 {"ecfill", l_CmdHF14AMfECFill},
156 {"ekeyprn", l_CmdHF14AMfEKeyPrn},
157 {"csetuid", l_CmdHF14AMfCSetUID},
158 {"csetblk", l_CmdHF14AMfCSetBlk},
159 {"cgetblk", l_CmdHF14AMfCGetBlk},
160 {"cgetsc", l_CmdHF14AMfCGetSc},
161 {"cload", l_CmdHF14AMfCLoad},
162 {"csave", l_CmdHF14AMfCSave},
163 {NULL, NULL}
164 };
165 luaL_newlib(L, hfmf_lib);
166 return 1;
167 }
168
169 static void set_libraries(lua_State *L)
170 {
171 static const luaL_Reg hfmf_lib[] = {
172 {"help", l_CmdHelp},
173 {"dbg", l_CmdHF14AMfDbg},
174 {"rdbl", l_CmdHF14AMfRdBl},
175 {"rdsc", l_CmdHF14AMfRdSc},
176 {"dump", l_CmdHF14AMfDump},
177 {"restore", l_CmdHF14AMfRestore},
178 {"wrbl", l_CmdHF14AMfWrBl},
179 {"chk", l_CmdHF14AMfChk},
180 {"mifare", l_CmdHF14AMifare},
181 {"nested", l_CmdHF14AMfNested},
182 {"sniff", l_CmdHF14AMfSniff},
183 {"sim", l_CmdHF14AMf1kSim},
184 {"eclr", l_CmdHF14AMfEClear},
185 {"eget", l_CmdHF14AMfEGet},
186 {"eset", l_CmdHF14AMfESet},
187 {"eload", l_CmdHF14AMfELoad},
188 {"esave", l_CmdHF14AMfESave},
189 {"ecfill", l_CmdHF14AMfECFill},
190 {"ekeyprn", l_CmdHF14AMfEKeyPrn},
191 {"csetuid", l_CmdHF14AMfCSetUID},
192 {"csetblk", l_CmdHF14AMfCSetBlk},
193 {"cgetblk", l_CmdHF14AMfCGetBlk},
194 {"cgetsc", l_CmdHF14AMfCGetSc},
195 {"cload", l_CmdHF14AMfCLoad},
196 {"csave", l_CmdHF14AMfCSave},
197 {NULL, NULL}
198 };
199
200 lua_pushglobaltable(L);
201 // Core library is in this table. Contains 'hf'
202
203 //this is 'hf' table
204 lua_newtable(L);
205
206 //this is the mf table
207 lua_newtable(L);
208
209 //Put the function into the hash table.
210 for (int i = 0; hfmf_lib[i].name; i++) {
211 lua_pushcfunction(L, hfmf_lib[i].func);
212 lua_setfield(L, -2, hfmf_lib[i].name);//set the name, pop stack
213 }
214 //Name of 'mf'
215 lua_setfield(L, -2, "mf");
216
217 //Name of 'hf'
218 lua_setfield(L, -2, "hf");
219
220 //-- remove the global environment table from the stack
221 lua_pop(L, 1);
222 return 1;
223 }
224
225 /**
226 * @brief CmdRun - executes a script file.
227 * @param argc
228 * @param argv
229 * @return
230 */
231 int CmdRun(const char *Cmd)
232 {
233 // create new Lua state
234 lua_State *lua_state;
235 lua_state = luaL_newstate();
236
237 // load Lua libraries
238 static const luaL_Reg lualibs[] =
239 {
240 {"base", luaopen_base },
241 { NULL, NULL}
242 };
243
244 const luaL_Reg *lib = lualibs;
245 for(; lib->func != NULL; lib++)
246 {
247 lib->func(lua_state);
248 lua_settop(lua_state, 0);
249 }
250
251 set_libraries(lua_state);
252 char cmd_name[32];
253 int len = 0;
254 memset(cmd_name, 0, 32);
255 sscanf(Cmd, "%31s%n", cmd_name, &len);
256
257 char buf[256];
258 snprintf(buf, sizeof buf, "./scripts/%s", cmd_name);
259
260 printf("Executing file '%s'\n---------------------------\n" , cmd_name);
261 // run the Lua script
262 luaL_dofile(lua_state, buf);
263 // close the Lua state
264 lua_close(lua_state);
265 printf("-----------------Finished\n");
266 }
267
Impressum, Datenschutz