]> CyberLeo.Net >> Repos - SourceForge/eyefi-config.git/blob - eyefi-unix.c
Don't clobber password.
[SourceForge/eyefi-config.git] / eyefi-unix.c
1 /*
2  * eyefi-unix.c
3  *
4  * Copyright (C) 2008 Dave Hansen <dave@sr71.net>
5  *
6  * This software may be redistributed and/or modified under the terms of
7  * the GNU General Public License ("GPL") version 2 as published by the
8  * Free Software Foundation.
9  */
10
11 #include "eyefi-config.h"
12
13 void print_pascal_string(struct pascal_string *str)
14 {
15         int i;
16         for (i = 0; i < str->length; i++)
17                 printf("%c", str->value[i]);
18 }
19
20 void print_mac(struct mac_address *mac)
21 {
22         int i;
23         for (i=0; i < MAC_BYTES-1; i++) {
24                 printf("%02x:", mac->mac[i]);
25         }
26         printf("%02x\n", mac->mac[i]);
27 }
28
29
30 void print_card_mac(void)
31 {
32         debug_printf(2, "%s()\n", __func__);
33         struct mac_address *mac;
34
35         card_info_cmd(MAC_ADDRESS);
36         mac = eyefi_response();
37         debug_printf(3, "%s() mac->length: %d\n", __func__, mac->length);
38         assert(mac->length == MAC_BYTES);
39         printf("card mac address: ");
40         print_mac(mac);
41 }
42
43 void print_card_firmware_info(void)
44 {
45         struct card_firmware_info *info = fetch_card_firmware_info();
46         printf("card firmware (len: %d): '", info->info.length);
47         print_pascal_string(&info->info);
48         printf("'\n");
49 }
50
51 void print_card_key(void)
52 {
53         debug_printf(2, "%s()\n", __func__);
54         struct card_info_rsp_key *foo = fetch_card_key();
55         printf("card key (len: %d): '", foo->key.length);
56         print_pascal_string(&foo->key);
57         printf("'\n");
58 }
59
60 void scan_print_nets(void)
61 {
62         int i;
63
64         debug_printf(2, "%s()\n", __func__);
65         struct scanned_net_list *scanned = scan_nets();
66         if (scanned->nr == 0) {
67                 printf("unable to detect any wireless networks\n");
68                 return;
69         }
70         printf("Scanned wireless networks:\n");
71         for (i=0; i < scanned->nr; i++) {
72                 struct scanned_net *net = &scanned->nets[i];
73                 printf("security: ");
74                 if (eyefi_debug_level > 1)
75                         printf("(%d)", net->type);
76                 printf("%4s, strength: %3d ", net_type_name(net->type),
77                                 net->strength);
78                 printf("essid: '%s'\n", net->essid);
79         }
80 }
81
82 void print_configured_nets(void)
83 {
84         int ret;
85         int i;
86         struct configured_net_list *configured = fetch_configured_nets();
87
88         debug_printf(2, "%s()\n", __func__);
89         ret = issue_noarg_command('l');
90         if (ret) {
91                 printf("error issuing print networks command: %d\n", ret);
92                 return;
93         }
94         configured = eyefi_response();
95         if (configured->nr == 0) {
96                 printf("No wireless networks configured on card\n");
97                 return;
98         }
99         printf("configured wireless networks:\n");
100         for (i=0; i < configured->nr; i++) {
101                 struct configured_net *net = &configured->nets[i];
102                 printf("'%s'\n", net->essid);
103         }
104 }
105
106 int try_connection_to(char *essid, char *ascii_password)
107 {
108         int i;
109         int ret = -1;
110         const char *type;
111
112         eyefi_printf("trying to connect to network: '%s'\n", essid);
113         if (ascii_password)
114                 eyefi_printf(" with passphrase: '%s'\n", ascii_password);
115         fflush(NULL);
116
117         // test network
118         ret = network_action('t', essid, ascii_password);
119         if (ret)
120                 return ret;
121         u8 last_rsp = -1;
122
123         char rsp = '\0';
124         ret = -1;
125         for (i=0; i < 200; i++) {
126                 struct byte_response *r;
127                 issue_noarg_command('s');
128                 r = eyefi_response();
129                 rsp = r->response;
130                 char *state = net_test_state_name(rsp);
131                 if (rsp == last_rsp) {
132                         eyefi_printf(".");
133                         fflush(NULL);;
134                 } else {
135                         if (rsp)
136                                 eyefi_printf("\nTesting connecion to '%s' (%d): %s", essid, rsp, state);
137                         last_rsp = rsp;
138                 }
139                 
140                 if (!strcmp("success", state)) {
141                         ret = 0;
142                         break;
143                 }
144                 if (!strcmp("not scanning", state))
145                         break;
146                 if (!strcmp("unknown", state))
147                         break;
148         }
149         eyefi_printf("\n");
150         if (!ret) {
151                 eyefi_printf("Succeeded connecting to: '%s'\n", essid);
152         } else {
153                 eyefi_printf("Unable to connect to: '%s' (final state: %d/'%s')\n", essid,
154                                 rsp, net_test_state_name(rsp));
155         }
156         return ret;
157 }
158
159 int print_log(void)
160 {
161         int i;
162         u8 *resbuf = malloc(EYEFI_BUF_SIZE*4);
163         int total_bytes;
164
165         total_bytes = get_log_into(resbuf);
166         if (total_bytes < 0) {
167                 debug_printf(1, "%s() error: %d\n", __func__, total_bytes);
168                 return total_bytes;
169         }
170         // The last byte *should* be a null, and the 
171         // official software does not print it.
172         for (i = 0; i < total_bytes-1; i++) {
173                 char c = resbuf[i];
174                 // the official software converts UNIX to DOS-style
175                 // line breaks, so we'll do the same
176                 if (c == '\n')
177                         printf("%c", '\r');
178                 printf("%c", c);
179         }
180         printf("\n");
181         // just some simple sanity checking to make sure what
182         // we are fetching looks valid
183         /* needs to be rethought for the new aligned logs
184         int null_bytes_left = 20;
185         if (resbuf[log_end] != 0) {
186                 debug_printf(2, "error: unexpected last byte (%ld/0x%lx) of log: %02x\n",
187                                 log_end, log_end, resbuf[log_end]);
188                 for (i=0; i<log_size; i++) {
189                         if (resbuf[i])
190                                 continue;
191                         if (null_bytes_left <= 0)
192                                 continue;
193                         null_bytes_left--;
194                         debug_printf(2, "null byte %d\n", i);
195                 }
196         }
197         */
198         return 0;
199 }
200
201 void open_error(char *file, int ret)
202 {
203         fprintf(stderr, "unable to open '%s' (%d)\n", file, ret);
204         fprintf(stderr, "Is the Eye-Fi card inserted and mounted at: %s ?\n", locate_eyefi_mount());
205         fprintf(stderr, "Do you have write permissions to it?\n");
206         fprintf(stderr, "debug information:\n");
207         if (eyefi_debug_level > 0)
208                 system("cat /proc/mounts >&2");
209         if (eyefi_debug_level > 1)
210                 perror("bad open");
211         exit(1);
212 }
213
214 void usage(void)
215 {
216         printf("Usage:\n");
217         printf("  eyefitest [OPTIONS]\n");
218         printf("  -a ESSID      add network (implies test unless --force)\n");
219         printf("  -t ESSID      test network\n");
220         printf("  -p KEY        set WPA key for add/test\n");
221         printf("  -r ESSID      remove network\n");
222         printf("  -s            scan for networks\n");
223         printf("  -c            list configured networks\n");
224         printf("  -b            reboot card\n");
225         printf("  -f            print information about card firmware\n");
226         printf("  -d level      set debugging level (default: 1)\n");
227         printf("  -k            print card unique key\n");
228         printf("  -l            dump card log\n");
229         printf("  -m            print card mac\n");
230         exit(4);
231 }
232
233 int main(int argc, char *argv[])
234 {
235         int option_index;
236         char c;
237         int cint;
238         char *essid = NULL;
239         char *passwd = NULL;
240         char network_action = 0;
241         static int force = 0;
242         static struct option long_options[] = {
243                 //{"wep", 'x', &passed_wep, 1},
244                 //{"wpa", 'y', &passed_wpa, 1},
245                 {"force", 0, &force, 1},
246                 {"help", 'h', NULL, 1},
247                 {0, 0, 0, 0}
248         };
249
250         if (argc == 1)
251                 usage();
252
253         debug_printf(3, "%s starting...\n", argv[0]);
254
255         debug_printf(3, "about to parse arguments\n");
256         debug_printf(4, "argc: %d\n", argc);
257         debug_printf(4, "argv: %p\n", argv);
258         while ((cint = getopt_long_only(argc, argv, "a:bcd:kflmp:r:st:z",
259                         &long_options[0], &option_index)) != -1) {
260                 c = cint;
261                 debug_printf(3, "argument: '%c' %d optarg: '%s'\n", c, c, optarg);
262                 switch (c) {
263                 case 0:
264                         // was a long argument
265                         break;
266                 case 'a':
267                 case 't':
268                 case 'r':
269                         essid = strdup(optarg);
270                         network_action = c;
271                         break;
272                 case 'b':
273                         reboot_card();
274                         break;
275                 case 'c':
276                         print_configured_nets();
277                         break;
278                 case 'd':
279                         eyefi_debug_level = atoi(optarg);
280                         fprintf(stderr, "set debug level to: %d\n", eyefi_debug_level);
281                         break;
282                 case 'f':
283                         print_card_firmware_info();
284                         break;
285                 case 'k':
286                         print_card_key();
287                         break;
288                 case 'l':
289                         print_log();
290                         break;
291                 case 'm':
292                         print_card_mac();
293                         break;
294                 case 'p':
295                         passwd = strdup(optarg);
296                         break;
297                 case 's':
298                         scan_print_nets();
299                         break;
300                 case 'z': {
301                         extern void testit0(void);
302                         testit0();
303                         break;
304                 }
305                 case 'h':
306                 default:
307                         usage();
308                         break;
309                 }
310         }
311
312         debug_printf(3, "after arguments1 essid: '%s' passwd: '%s'\n", essid, passwd);
313         if (network_action && essid) {
314                 int ret = 0;
315                 init_card();
316                 switch (network_action) {
317                 case 't':
318                         ret = try_connection_to(essid, passwd);
319                         break;
320                 case 'a':
321                         if (!force) {
322                                 ret = try_connection_to(essid, passwd);
323                         } else {
324                                 debug_printf(1, "forced: skipping network test\n");
325                         }
326                         if (ret) {
327                                 printf("Error connecting to network '%s', not adding.\n", essid);
328                                 printf("use --force to override\n");
329                                 break;
330                         }
331                         add_network(essid, passwd);
332                         break;
333                 case 'r':
334                         remove_network(essid);
335                         break;
336                 }
337         }
338
339         free(essid);
340         free(passwd);
341         return 0;
342 }
343
344