]> git.decadent.org.uk Git - ap-utils.git/blob - lib/wep.c
Imported Upstream version 1.5~pre1
[ap-utils.git] / lib / wep.c
1 /*
2  *      wep.c from Access Point SNMP Utils for Linux
3  *
4  * Copyright (c) 2002 Roman Festchook <roma at polesye dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License Version 2 from
8  * June 1991 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "ap-utils.h"
27
28 #define PRIVSETTINGS _("Privacy Settings")
29 #define ENCRYPT _("[E] Standard encryption mechanism: ")
30 #define ALLOW _("[A] Allow unencrypted: ")
31 #define DEFAULT _("[K] Default WEP key: ")
32 #define PUBLIC _("[P] Public key: ")
33
34 extern WINDOW *main_sub;
35 extern short ap_type;
36
37 void wep()
38 {
39     char defaultWEPKey[4][12] = {
40         {0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x02, 0x01, 0x00},
41         {0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x02, 0x02, 0x00},
42         {0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x02, 0x03, 0x00},
43         {0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x02, 0x04, 0x00}
44     };
45     char privacyWEPEnable[] =
46         {0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x02, 0x05, 0x00
47     };
48     char privacyDefaultWEPKeyID[] =
49         {0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x02, 0x06, 0x00
50     };
51
52     char message[1024], m_wep = 0, m_wepid = 0, wepkey[4][13],
53         m_wepkey[4] = { 0, 0, 0, 0 };
54     char *weps[3] = { "WEP64", OFF, "WEP128" }, *keys[4] = {"1", "2", "3", "4"};
55     int wep, wepid, i, c = 0;
56     varbind varbinds[6];
57     unsigned int wepid_hw, wep_hw;
58
59     if (ap_type == ATMEL12350) {
60         privacyDefaultWEPKeyID[5] = 0xE0;
61         privacyDefaultWEPKeyID[6] = 0x3E;
62         privacyWEPEnable[5] = 0xE0;
63         privacyWEPEnable[6] = 0x3E;
64         for (i = 0; i < 4; i++) {
65             defaultWEPKey[i][5] = 0xE0;
66             defaultWEPKey[i][6] = 0x3E;
67         }
68     }
69
70     varbinds[0].oid = privacyWEPEnable;
71     varbinds[0].len_oid = sizeof(privacyWEPEnable);
72     varbinds[0].type = NULL_VALUE;
73     varbinds[0].len_val = 0;
74     varbinds[1].oid = privacyDefaultWEPKeyID;
75     varbinds[1].len_oid = sizeof(privacyDefaultWEPKeyID);
76     varbinds[1].type = NULL_VALUE;
77     varbinds[1].len_val = 0;
78     print_help(WAIT_RET);
79     if (snmp(varbinds, 2, GET) <= 0) {
80         print_helperr(ERR_RET);
81         goto exit;
82     }
83     wep = *(varbinds[0].value);
84     wepid = *(varbinds[1].value);
85     print_help(_("EK1234 - set; W - write conf; Q - quit to menu"));
86
87     print_top(NULL, PRIVSETTINGS);
88
89     sprintf(message, "%s%s", ENCRYPT, weps[wep - 1]);
90     mvwaddstr(main_sub, 0, 0, message);
91     sprintf(message, "%s%d", DEFAULT, wepid);
92     mvwaddstr(main_sub, 1, 0, message);
93
94     mvwaddstr(main_sub, 3, 2, _("Key  WEP"));
95
96     for (i = 0; i < 4; i++) {
97         sprintf(message, "[%d]  00.00.00.00.00.00.00.00.00.00.00.00.00",
98                 i + 1);
99         mvwaddstr(main_sub, i + 4, 2, message);
100     }
101     mvwaddstr(main_sub, LAST_ROW, 1, _("Hint! Confused by WEP key values? See man ap-config for info..."));
102
103     wrefresh(main_sub);
104     noecho();
105
106     while (1) {
107         c = getch();
108         switch (c) {
109         case 'q':
110         case 'Q':
111             goto quit;
112         case 'e':
113         case 'E':
114             wep = menu_choose(0, strlen(ENCRYPT), weps, 3) + 1;
115             clear_main_new(0, 1);
116             print_menusel(0, 0, ENCRYPT, weps[wep - 1]);
117             m_wep = 1;
118             continue;
119         case 'k':
120         case 'K':
121             wepid = menu_choose(1, strlen(DEFAULT), keys, 4) + 1;
122             clear_main_new(1, 2);
123             sprintf(message, "%d", wepid);
124             print_menusel(1, 0, DEFAULT, message);
125             wrefresh(main_sub);
126             m_wepid = 1;
127             continue;
128         case '1':
129         case '2':
130         case '3':
131         case '4':
132             c = c -'0';
133 /*
134             message[1] = '\0';
135             c = strtol(message, NULL, 16);
136             for (i = 0; i < 13; i++)
137 */
138                 for (i = 0; i < 13; i++) {
139                     get_value(message, c + 3, 7 + i * 3, 3, HEX_STRING,
140                         0, 0, NULL);
141                     wepkey[c - 1][i] = strtol(message, NULL, 16);
142                 }
143             m_wepkey[c - 1] = 1;
144             continue;
145         case 'w':
146         case 'W':
147             i = 0;
148             if (m_wep) {
149                 wep_hw = swap4(wep);
150                 varbinds[i].oid = privacyWEPEnable;
151                 varbinds[i].len_oid = sizeof(privacyWEPEnable);
152                 varbinds[i].type = INT_VALUE;
153                 varbinds[i].value = (char *) &wep_hw;
154                 varbinds[i].len_val = 1;
155                 i++;
156             }
157             if (m_wepid) {
158                 wepid_hw = swap4(wepid);
159                 varbinds[i].oid = privacyDefaultWEPKeyID;
160                 varbinds[i].len_oid = sizeof(privacyDefaultWEPKeyID);
161                 varbinds[i].type = INT_VALUE;
162                 varbinds[i].value = (char *) &wepid_hw;
163                 varbinds[i].len_val = 1;
164                 i++;
165             }
166             for (c = 0; c < 4; c++)
167                 if (m_wepkey[c]) {
168                     varbinds[i].oid = defaultWEPKey[c];
169                     varbinds[i].len_oid = sizeof(defaultWEPKey[c]);
170                     varbinds[i].type = STRING_VALUE;
171                     varbinds[i].value = wepkey[c];
172                     varbinds[i].len_val = 13;
173                     i++;
174                 }
175             print_help(WAIT_SET);
176             if (snmp(varbinds, i, SET) <= 0) {
177                 print_helperr(ERR_SET);
178                 goto exit;
179             }
180             print_help(DONE_SET);
181             goto exit;
182         }
183         continue;
184     }
185
186   exit:
187     getch();
188   quit:
189     print_top(NULL, NULL);
190     clear_main(0);
191 }
192
193
194 void nwn_wep()
195 {
196
197     char defaultWEPKey[4][11] = {       
198         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x01, 0x03, 0x01, 0x02, 0x01, 0x01},
199         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x01, 0x03, 0x01, 0x02, 0x01, 0x02},
200         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x01, 0x03, 0x01, 0x02, 0x01, 0x03},
201         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x01, 0x03, 0x01, 0x02, 0x01, 0x04},
202 };
203
204     char privacyWEPEnable[] =
205         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x01, 0x05, 0x01, 0x01, 0x01 };
206     char privacyDefaultWEPKeyID[] =
207         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x01, 0x05, 0x01, 0x02, 0x01 };
208     
209     char oid_dot11ExcludeUnencrypted[] =
210         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x01, 0x05, 0x01, 0x04, 0x01 };
211     char oid_smtPublicKeyEnable[] =
212         { 0x2b, 0x06, 0x01, 0x04, 0x01, 0x87, 0x29, 0x03, 0x01, 0x02, 0x01,
213         0x0a, 0x00 };
214
215     char message[1024], m_wep = 0, m_wepid = 0, wepkey[4][5],
216             m_wepkey[4] = { 0, 0, 0, 0 }, exclude, m_exclude = 0;
217     char *weps[2] = { "WEP64", OFF }, publickey, m_publickey = 0, *keys[4] = {"1", "2", "3", "4"}, wepid=0, wepid_hw;
218     int wep, i, c=0;
219     varbind varbinds[15];
220
221     varbinds[0].oid = privacyWEPEnable;
222     varbinds[0].len_oid = sizeof(privacyWEPEnable);
223     varbinds[0].type = NULL_VALUE;
224     varbinds[0].len_val = 0;
225     varbinds[1].oid = oid_smtPublicKeyEnable;
226     varbinds[1].len_oid = sizeof(oid_smtPublicKeyEnable);
227     varbinds[1].type = NULL_VALUE;
228     varbinds[1].len_val = 0;
229     varbinds[2].oid = oid_dot11ExcludeUnencrypted;
230     varbinds[2].len_oid = sizeof(oid_dot11ExcludeUnencrypted);
231     varbinds[2].type = NULL_VALUE;
232     varbinds[2].len_val = 0;
233     varbinds[3].oid = privacyDefaultWEPKeyID;
234     varbinds[3].len_oid = sizeof(privacyDefaultWEPKeyID);
235     varbinds[3].type = NULL_VALUE;
236     varbinds[3].len_val = 0;
237                         
238     print_help(WAIT_RET);
239     if (snmp(varbinds, 4, GET) <= 0) {
240         print_helperr(ERR_RET);
241         goto exit;
242     }
243     wep = *(varbinds[0].value);
244     exclude = *(varbinds[2].value);
245     publickey = *(varbinds[1].value);
246     wepid=*(varbinds[3].value);
247     print_help(_("AEPK1234 - set options; W - write conf; Q - quit to menu"));
248
249     print_top(NULL, PRIVSETTINGS);
250
251     sprintf(message, "%s%s", ENCRYPT, weps[wep - 1]);
252     mvwaddstr(main_sub, 1, 0, message);
253     sprintf(message, "%s%s", ALLOW, (exclude == 2) ? ON : OFF);
254     mvwaddstr(main_sub, 2, 0, message);
255    
256     sprintf(message, "%s%d", DEFAULT, wepid+1);
257     mvwaddstr(main_sub, 3, 0, message);
258
259         
260     sprintf(message, "%s%s", PUBLIC, (publickey == 1) ? ON : OFF);
261     mvwaddstr(main_sub, 10, 0, message);
262
263     mvwaddstr(main_sub, 4, 2, _("Key  WEP"));
264     for (i = 0; i < 4; i++) {
265         sprintf(message, "[%d]  00.00.00.00.00", i + 1);
266        mvwaddstr(main_sub, i + 5, 2, message);
267    }
268
269
270     wrefresh(main_sub);
271     noecho();
272
273     while (1) {
274         switch (c = getch()) {
275         case 'Q':
276         case 'q':
277             goto quit;
278         case 'E':
279         case 'e':
280             wep = menu_choose(1, strlen(ENCRYPT), weps, 2) + 1;
281             clear_main_new(1, 2);
282             print_menusel(1, 0, ENCRYPT, weps[wep - 1]);
283             m_wep = 1;
284             continue;
285         case 'a':
286         case 'A':
287             exclude = on_off(2, strlen(ALLOW));
288             exclude = (exclude == 2) ? 1 : 2;
289             clear_main_new(2, 3);
290             print_menusel(2, 0, ALLOW, (exclude == 2) ? ON : OFF);
291             m_exclude = 1;
292             continue;
293         case 'p':
294         case 'P':
295             publickey = on_off(10, strlen(PUBLIC));
296             clear_main_new(10, 11);
297             print_menusel(10, 0, PUBLIC, (publickey == 1) ? ON : OFF);
298             m_publickey = 1;
299             continue;
300         case 'k':
301         case 'K':
302             wepid = menu_choose(3, strlen(DEFAULT), keys, 4);
303             clear_main_new(3, 4);
304             sprintf(message, "%d", wepid+1);
305             print_menusel(3, 0, DEFAULT, message);
306             m_wepid = 1;
307             continue;
308         case '1':
309         case '2':
310         case '3':
311         case '4':
312 //            message[0] = c;
313   //          message[1] = '\0';
314 //            c = strtol(message, NULL, 16);
315             c = c - '0';
316 /*          for (i = 0; i < 5; i++)*/
317                 for (i = 0; i < 5; i++) {
318                     get_value(message, c + 4, 7 + i * 3, 3, HEX_STRING,
319                         0, 0, NULL);
320                     wepkey[c - 1][i] = strtol(message, NULL, 16);
321                  }
322             m_wepkey[c - 1] = 1;
323             continue;
324 /*          
325         case '1':
326             for (i = 0; i < 5; i++) {
327                 get_value(message, 5, 7 + i * 3, 3, HEX_STRING, 0, 0, NULL);
328                 wepkey[i] = strtol(message, NULL, 16);
329             }
330             m_wepkey = 1;
331             continue;
332 */      case 'w':
333         case 'W':
334             i = 0;
335             if (m_wep) {
336                 varbinds[i].oid = privacyWEPEnable;
337                 varbinds[i].len_oid = sizeof(privacyWEPEnable);
338                 varbinds[i].type = 0x02;
339                 varbinds[i].value = (char *) &wep;
340                 varbinds[i].len_val = 1;
341                 i++;
342             }
343
344 /*          m_wep = 0;
345             varbinds[i].oid = privacyDefaultWEPKeyID;
346             varbinds[i].len_oid = sizeof(privacyDefaultWEPKeyID);
347             varbinds[i].type = INT_VALUE;
348             varbinds[i].value = &m_wep;
349             varbinds[i].len_val = 1;
350             i++;
351 */
352             if (m_exclude) {
353                 varbinds[i].oid = oid_dot11ExcludeUnencrypted;
354                 varbinds[i].len_oid = sizeof(oid_dot11ExcludeUnencrypted);
355                 varbinds[i].type = 0x02;
356                 varbinds[i].value = &exclude;
357                 varbinds[i].len_val = 1;
358                 i++;
359             }
360             if (m_publickey) {
361                 varbinds[i].oid = oid_smtPublicKeyEnable;
362                 varbinds[i].len_oid = sizeof(oid_smtPublicKeyEnable);
363                 varbinds[i].type = 0x02;
364                 varbinds[i].value = &publickey;
365                 varbinds[i].len_val = 1;
366                 i++;
367             }
368 /*          if (m_wepkey) {
369                 varbinds[i].oid = defaultWEPKey;
370                 varbinds[i].len_oid = sizeof(defaultWEPKey);
371                 varbinds[i].type = STRING_VALUE;
372                 varbinds[i].value = wepkey;
373                 varbinds[i].len_val = 5;
374                 i++;
375             }
376 */
377            if (m_wepid) {
378              wepid_hw = swap4(wepid);
379              varbinds[i].oid = privacyDefaultWEPKeyID;
380              varbinds[i].len_oid = sizeof(privacyDefaultWEPKeyID);
381              varbinds[i].type = INT_VALUE;
382              varbinds[i].value = (char *) &wepid_hw;
383              varbinds[i].len_val = 1;
384              i++;
385            }
386             
387
388                     print_help(WAIT_SET);
389                 if (snmp(varbinds, i, SET) <= 0) {
390                     print_helperr(ERR_SET);
391                     goto exit;
392                 }
393         i=0;    
394             for (c = 0; c < 4; c++)
395                 if (m_wepkey[c]) {
396                     varbinds[i].oid = defaultWEPKey[c];
397                     varbinds[i].len_oid = sizeof(defaultWEPKey[c]);
398                     varbinds[i].type = STRING_VALUE;
399                     varbinds[i].value = wepkey[c];
400                     varbinds[i].len_val = 5;
401                     i++;
402              }
403          if (snmp(varbinds, i, SET) <= 0) {
404                print_helperr(ERR_SET);
405                goto exit;
406          }
407                 
408                 print_help(DONE_SET);
409             goto exit;
410         }
411         continue;
412     }
413
414     print_help(ANY_KEY);
415   exit:
416     getch();
417   quit:
418     print_top(NULL, NULL);
419     clear_main(0);
420 }