-
Notifications
You must be signed in to change notification settings - Fork 2
/
desflua.c
168 lines (137 loc) · 4.11 KB
/
desflua.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* DESFire-Shell: Modify MIFARE DESFire Cards
*
* Copyright (C) 2015-2021 Mario Haustein
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see https://www.gnu.org/licenses/.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include "buffer.h"
#include "cmd.h"
#include "debug.h"
#include "desflua.h"
int desflua_get_comm(lua_State *l, int idx, uint8_t *comm)
{
const char *commstr;
if(comm == NULL)
{
lua_checkstack(l, 1);
lua_pushfstring(l, "internal error (%s:%d): comm=%p", __FILE__, __LINE__, comm);
return -1;
}
if(lua_isnumber(l, idx))
*comm = lua_tonumber(l, idx);
else if(lua_isstring(l, idx))
{
commstr = lua_tostring(l, idx);
if(!strcasecmp(commstr, "PLAIN")) { *comm = MDCM_PLAIN; }
else if(!strcasecmp(commstr, "MAC")) { *comm = MDCM_MACED; }
else if(!strcasecmp(commstr, "CRYPT")) { *comm = MDCM_ENCIPHERED; }
else
{
lua_checkstack(l, 1);
lua_pushfstring(l, "unkown communication mode '%s'", commstr);
return -1;
}
}
else
{
lua_checkstack(l, 1);
lua_pushfstring(l, "number or string expected");
return -1;
}
return 0;
}
int desflua_get_acl(lua_State *l, int idx, uint16_t *acl)
{
uint16_t rd, wr, rw, ca;
if(acl == NULL)
{
lua_checkstack(l, 1);
lua_pushfstring(l, "internal error (%s:%d): acl=%p", __FILE__, __LINE__, acl);
return -1;
}
if(lua_isnumber(l, idx))
*acl = lua_tointeger(l, idx);
else if(lua_istable(l, idx))
{
lua_checkstack(l, 4);
lua_getfield(l, idx, "rd");
lua_getfield(l, idx, "wr");
lua_getfield(l, idx, "rw");
lua_getfield(l, idx, "ca");
if(!lua_isnumber(l, -4) ||
!lua_isnumber(l, -3) ||
!lua_isnumber(l, -2) ||
!lua_isnumber(l, -1))
{
lua_checkstack(l, 1);
if(!lua_isnumber(l, -4)) { lua_pushstring(l, "read access: number expected"); }
else if(!lua_isnumber(l, -3)) { lua_pushstring(l, "write access: number expected"); }
else if(!lua_isnumber(l, -2)) { lua_pushstring(l, "read/write access: number expected"); }
else if(!lua_isnumber(l, -1)) { lua_pushstring(l, "change access: number expected"); }
lua_remove(l, -2);
lua_remove(l, -2);
lua_remove(l, -2);
lua_remove(l, -2);
return -1;
}
rd = lua_tointeger(l, -4);
wr = lua_tointeger(l, -3);
rw = lua_tointeger(l, -2);
ca = lua_tointeger(l, -1);
lua_pop(l, 4);
*acl = MDAR(rd, wr, rw, ca);
}
else
{
lua_checkstack(l, 1);
lua_pushstring(l, "number or table expected");
return -1;
}
return 0;
}
void desflua_push_acl(lua_State *l, uint16_t acl)
{
lua_checkstack(l, 2);
lua_newtable(l);
lua_pushinteger(l, MDAR_READ(acl)); lua_setfield(l, -2, "rd");
lua_pushinteger(l, MDAR_WRITE(acl)); lua_setfield(l, -2, "wr");
lua_pushinteger(l, MDAR_READ_WRITE(acl)); lua_setfield(l, -2, "rw");
lua_pushinteger(l, MDAR_CHANGE_AR(acl)); lua_setfield(l, -2, "ca");
}
void desflua_handle_result(lua_State *l, int result, FreefareTag tag)
{
uint8_t err;
const char *str;
lua_settop(l, 0);
lua_checkstack(l, 2);
err = mifare_desfire_last_picc_error(tag);
str = result >= 0 ? "OK" : freefare_strerror(tag);
lua_pushinteger(l, err);
lua_pushstring(l, str);
debug_result(err, str);
}
void desflua_argerror(lua_State *l, int argnr, const char *prefix)
{
lua_checkstack(l, 1);
lua_pushfstring(l, "%s: %s", prefix, lua_tostring(l, -1));
lua_remove(l, -2);
luaL_argerror(l, argnr, lua_tostring(l, -1));
}