-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
294 lines (266 loc) · 5.46 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <argp.h>
#include <fcntl.h>
#include <unistd.h>
#include "libscsi.h"
#define MAX_PWD_LEN 16
#define MIN_PWD_LEN 4
struct arguments
{
int cmd42_para;
char *pwd;
// char *new_pwd;
int cmd13;
char *device;
int arg_count;
};
int is_transcend_reader(char *device)
{
int ret = 1;
FILE *ptr = NULL;
char readbuf[256];
char cmd[100];
snprintf(cmd, 100, "udevadm info --query=property -n %s | grep -E 'ID_USB_VENDOR|ID_VENDOR'", device);
if((ptr = popen(cmd, "r")) != NULL)
{
while(fgets(readbuf,256,ptr) != NULL)
{
if(strstr(readbuf, TS_VID) != NULL)
{
ret=0;
break;
}
}
pclose(ptr);
}
return ret;
}
int is_os_devcie(char *device)
{
int ret = 0;
FILE *ptr = NULL;
char readbuf[256];
char *df_cmd = "df / ";
if((ptr = popen(df_cmd, "r")) != NULL)
{
while(fgets(readbuf,256,ptr) != NULL)
{
// strncpy(os_device, readbuf, strlen(readbuf)-1);
if(strstr(readbuf, device) != NULL) // os is installed on device
{
ret=1;
break;
}
}
pclose(ptr);
}
return ret;
}
int match_pwd_rule(char *pwd, char **note)
{
if(strlen(pwd) > MAX_PWD_LEN)
{
*note = "Password need 4~16 characters";
return 1;
}
else if(strlen(pwd) < MIN_PWD_LEN)
{
*note = "Password need 4~16 characters";
return 1;
}
return 0;
}
static int parse_opt(int key, char *arg, struct argp_state *state)
{
struct arguments *argument = state->input;
switch(key){
case 's':{
if(argument->cmd42_para < 0)
argument->cmd42_para = CMD42_SET_PWD;
else
goto Confused;
break;
}
case 'q':{
if(argument->cmd42_para < 0)
argument->cmd42_para = CMD42_SET_LOCK;
else
goto Confused;
break;
}
case 'c':{
if(argument->cmd42_para < 0)
argument->cmd42_para = CMD42_CLR_PWD;
else
goto Confused;
break;
}
case 'l':{
if(argument->cmd42_para < 0)
argument->cmd42_para = CMD42_LOCK;
else
goto Confused;
break;
}
case 'u':{
if(argument->cmd42_para < 0)
argument->cmd42_para = CMD42_UNLOCK;
else
goto Confused;
break;
}
case 'e':{
if(argument->cmd42_para < 0)
argument->cmd42_para = CMD42_ERASE;
else
goto Confused;
break;
}
case 't':{
argument->cmd13 = 1;
break;
}
case ARGP_KEY_ARG:{
argument->device = arg;
argument->arg_count++;
// printf("%s\n", arg);
break;
}
case ARGP_KEY_END:{
if(argument->arg_count < 1)
argp_error(state, "requires minimum 1 device path");
break;
}
Confused:{
argp_error(state, "the command is confuseing");
return -1;
}
}
switch (key)
{
case 's': case 'q': case 'c': case 'l': case 'u':{
char *ret_note;
argument->pwd = arg;
if(match_pwd_rule(argument->pwd, &ret_note) == 1)
{
argp_failure(state, 0, 0, "%s", ret_note);
return -1;
}
break;
}
default:
break;
}
return 0;
error:
return -1;
}
int main(int argc, char **argv )
{
struct argp_option options[] = {
{0, 0, 0, 0, "Security Options:", 1},
{"set-pwd", 's', "Password[New Password]", 0, "Set SD password or change password"},
{"quick-lock", 'q', "\"Password [New Password]\"", 0, "Set SD password and lock the card"},
{"clear", 'c', "Password", 0, "Clear SD password"},
{"lock" , 'l', "Password", 0, "Lock SD by password"},
{"unlock", 'u', "Password", 0, "Unlock SD by password"},
{"erase", 'e', 0, 0, "Force erase SD card"},
{0, 0, 0, 0, "Status Options:", 2},
{"status", 't', 0, 0, "Check SD state"},
{0}
};
struct arguments argument = {
.cmd42_para = -1,
.pwd = NULL,
.cmd13 = 0,
.device = NULL,
};
struct argp argp = {options, parse_opt, "Device", 0};
if(!argp_parse(&argp, argc, argv, 0, 0, &argument))
{
// if(strstr(argument.device, "mmc"))
// {
// int fd;
// fd = open(argument.device, O_RDWR);
// if (fd < 0) {
// perror("open");
// exit(1);
// }
// unsigned *response;
// send_status(fd, response);
// }
if(is_os_devcie(argument.device) == 1)
{
printf("Lock function cannot work on OS device\n");
return 0;
}
if(is_transcend_reader(argument.device) == 1)
{
printf("Please use Transcend SD card reader\n");
return 0;
}
// printf("\n"
// "Device is %s\n"
// "cmd42 is %d\n"
// "pwd is %s\n"
// "cmd13 is %d\n"
// "\n",
// argument.device,
// argument.cmd42_para,
// argument.pwd,
// argument.cmd13);
if(argument.cmd42_para != -1)
{
int ret = 0;
ret = do_lock_unlock(argument.device, argument.cmd42_para, argument.pwd);
if(ret == 1)
{
int fd;
int *status;
fd = open(argument.device, O_RDWR);
if (fd < 0) {
perror("open");
exit(1);
}
status = read_status(&fd);
// printf("lock/unlock: %d, result: %d\n", status[0], status[1]);
if(status[1]==1)
show_cmd42_error_msg(argument.cmd42_para, status[0]);
else
{
if(status[0]==1)
printf("Status: Lock\n");
else
{
if(argument.cmd42_para==CMD42_ERASE)
disk_format(argument.device);
printf("Status: Unlock\n");
}
}
close(fd);
}
}
else if(argument.cmd13 == 1)
{
int fd, ret;
int *status;
fd = open(argument.device, O_RDWR);
if (fd < 0) {
perror("open");
exit(1);
}
status = read_status(&fd);
ret = status[0];
// printf("lock/unlock: %d, result: %d\n", status[0],status[1]);
if(ret)
printf("Device is lock\n");
else
printf("Device is unlock\n");
close(fd);
}
return 0;
}
return 1;
}