Not including libgen.h breaks 64bit builds (probably because the compiler assumes...
[librfid] / utils / mifare-tool.c
1 /* mifare-tool - a small command-line tool for librfid mifare testing
2  *
3  * (C) 2006 by Harald Welte <laforge@gnumonks.org>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License version 2 
7  *  as published by the Free Software Foundation
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #ifndef __MINGW32__
26 #include <libgen.h>
27 #endif
28
29 #define _GNU_SOURCE
30 #include <getopt.h>
31
32 #include <librfid/rfid.h>
33 #include <librfid/rfid_scan.h>
34 #include <librfid/rfid_reader.h>
35 #include <librfid/rfid_layer2.h>
36 #include <librfid/rfid_protocol.h>
37
38 #include <librfid/rfid_protocol_mifare_classic.h>
39 #include <librfid/rfid_protocol_mifare_ul.h>
40
41 #include <librfid/rfid_access_mifare_classic.h>
42
43 #include "librfid-tool.h"
44
45 static char *program_name;
46
47 static void help(void)
48 {
49         printf( " -h    --help          Print this help message\n"
50                 " -r    --read          Read a mifare sector\n"
51                 " -l    --loop-read     Loop reading a mifare sector\n"
52                 " -w    --write         Write a mifare sector\n"
53                 " -k    --key           Specify mifare access key (in hex tuples)\n");
54 }
55
56 static struct option mifare_opts[] = {
57         { "key", 1, 0, 'k' },
58         { "read", 1, 0, 'r' },
59         { "loop-read", 1, 0, 'l' },
60         { "write", 1 ,0, 'w' },
61         { "help", 0, 0, 'h' },
62         { 0, 0, 0, 0 }
63 };
64
65 static int mifare_cl_auth(unsigned char *key, int page)
66 {
67         int rc;
68
69         rc = mfcl_set_key(ph, key);
70         if (rc < 0) {
71                 fprintf(stderr, "key format error\n");
72                 return rc;
73         }
74         rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, page);
75         if (rc < 0) {
76                 fprintf(stderr, "mifare auth error\n");
77                 return rc;
78         } else 
79                 printf("mifare auth succeeded!\n");
80         
81         return 0;
82 }
83
84 static void mifare_l3(void)
85 {
86         while (l2_init(RFID_LAYER2_ISO14443A) < 0) ;
87
88         printf("ISO14443-3A anticollision succeeded\n");
89
90         while (l3_init(RFID_PROTOCOL_MIFARE_CLASSIC) < 0) ;
91
92         printf("Mifare card available\n");
93 }
94
95 int main(int argc, char **argv)
96 {
97         int len, rc, c, option_index = 0;
98         unsigned int page,uid,uid_len;
99         char key[MIFARE_CL_KEY_LEN];
100         char buf[MIFARE_CL_PAGE_SIZE];
101
102 #ifdef  __MINGW32__
103         program_name = argv[0];
104 #else
105         program_name = basename(argv[0]);
106 #endif/*__MINGW32__*/
107
108         memcpy(key, MIFARE_CL_KEYA_DEFAULT_INFINEON, MIFARE_CL_KEY_LEN);
109
110         printf("%s - (C) 2006 by Harald Welte\n"
111                "This program is Free Software and has "
112                "ABSOLUTELY NO WARRANTY\n\n", program_name);
113
114         printf("initializing librfid\n");
115         rfid_init();
116
117         if (reader_init() < 0) {
118                 fprintf(stderr, "error opening reader\n");
119                 exit(1);
120         }
121
122         while (1) {
123                 c = getopt_long(argc, argv, "k:r:l:w:", mifare_opts,
124                                 &option_index);
125                 if (c == -1)
126                         break;
127
128                 switch (c) {
129                 case 'k':
130                         hexread(key, optarg, strlen(optarg));
131                         printf("key: %s\n", hexdump(key, MIFARE_CL_KEY_LEN));
132                         break;
133                 case 'r':
134                         page = atoi(optarg);
135                         printf("read(key='%s',page=%u):",
136                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
137                         len = MIFARE_CL_PAGE_SIZE;
138                         mifare_l3();
139                         if (mifare_cl_auth(key, page) < 0)
140                                 exit(1);
141
142                         uid_len=sizeof(uid);
143                         uid=0;
144                         if(rfid_layer2_getopt(l2h,RFID_OPT_LAYER2_UID,&uid,&uid_len)>=0)
145                             printf("UID=%08X (len=%u)\n",uid,uid_len);
146                                 
147                         len=MIFARE_CL_PAGE_SIZE;                                                                                                                                                                                                
148                         rc = rfid_protocol_read(ph, page, buf, &len);
149                         if (rc < 0) {
150                                 printf("\n");
151                                 fprintf(stderr, "error during read\n");
152                                 break;
153                         }
154                         printf("len=%u data=%s\n", len, hexdump(buf, len));
155
156                         if (page & 0x3 == 0x3) {
157                                 struct mfcl_access_sect s;
158                                 struct mfcl_access_exp_sect es;
159                                 int b;
160                                 u_int8_t recreated[4];
161                                 mfcl_parse_access(&s, buf+6);
162                                 printf("access b0:%u b1:%u b2:%u b3:%u\n",
163                                         s.block[0], s.block[1],
164                                         s.block[2], s.block[3]);
165                                 mfcl_access_to_exp(&es, &s);
166                                 for (b = 0; b < 3; b++)
167                                         printf("%u: %s\n", b, mfcl_access_exp_stringify(&es.block[b]));
168                                 printf("3: %s\n", mfcl_access_exp_acc_stringify(&es.acc));
169 #if 0
170                                 mfcl_compile_access(recreated, &s);
171                                 printf("recreated; %s\n", hexdump(recreated,4));
172 #endif
173                         }
174                         break;
175                 case 'l':
176                         page = atoi(optarg);
177                         printf("read_loop(key='%s',page=%u):\n",
178                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
179                         while (1) {
180                                 mifare_l3();
181                                 if (mifare_cl_auth(key, page) < 0)
182                                         continue;
183
184                                 uid_len=sizeof(uid);
185                                 uid=0;
186                                 if(rfid_layer2_getopt(l2h,RFID_OPT_LAYER2_UID,&uid,&uid_len)>=0)
187                                     printf("UID=%08X (len=%u)\n",uid,uid_len);
188
189                                 len=MIFARE_CL_PAGE_SIZE;                                                                                                                                                                                                
190                                 rc = rfid_protocol_read(ph, page, buf, &len);
191                                 if (rc < 0) {
192                                         printf("\n");
193                                         fprintf(stderr, "error during read\n");
194                                         continue;
195                                 }
196                                 printf("%s\n", hexdump(buf, len));
197                         }
198                         break;
199                 case 'w':
200                         page = atoi(optarg);
201                         len = strlen(argv[optind]);
202                         len = hexread(buf, argv[optind], len);
203                         printf("write(key='%s',page=%u):",
204                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
205                         printf(" '%s'(%u):", hexdump(buf, len), len);
206                         mifare_l3();
207                         if (mifare_cl_auth(key, page) < 0)
208                                 exit(1);
209                         rc = rfid_protocol_write(ph, page, buf, len); 
210                         if (rc < 0) {
211                                 printf("\n");
212                                 fprintf(stderr, "error during write\n");
213                                 break;
214                         }
215                         printf("success\n");
216                         break;
217                 case 'h':
218                 default:
219                         help();
220                 }
221         }
222
223 #if 0
224         rfid_protocol_close(ph);
225         rfid_protocol_fini(ph);
226
227         rfid_layer2_close(l2h);
228         rfid_layer2_fini(l2h);
229 #endif
230         rfid_reader_close(rh);
231         exit(0);
232 }
233