]> git.decadent.org.uk Git - ap-utils.git/blob - src/auth_mac.c
Imported Upstream version 1.5~pre1
[ap-utils.git] / src / auth_mac.c
1 /*
2  *      auth.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 <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include "ap-utils.h"
25
26 #define MAX_LINES LINES-6
27 #define PACKET_ERROR _("AuthorizedMacTableString packet error")
28
29 #define MAC_AUTH _("[A] MAC authorization: ")
30 #define MAC_ADD  _("Enter MAC: ")
31 #define MAC_DEL  _("Delete Num: ")
32 #define MAC_TITLE _("Authorized MAC addresses")
33 #define MAC_HEADER _("NUM     MAC address")
34 #define MAC_HELP _("A - auth; N - new; D - del; arrows - scroll; W - write conf; Q - quit")
35
36 extern WINDOW *main_sub;
37 extern int LINES;
38 extern short ap_type;
39
40 void atmel_auth_mac()
41 {
42     struct AuthorizedMacTableString {
43         unsigned int short Action;
44         unsigned int short NumOfAllTableAddresses;
45         unsigned int short NumOfCurrentAddress;
46         unsigned char MacAddress[6];
47     } *AuthMac = NULL, get;
48
49     struct MacListStat *pmac, *first = NULL, *curr = NULL;
50     uint32_t auth_mac_hw;
51
52     char EnableAuthMAC[] =
53         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
54         0x02, 0x06, 0x01, 0x00
55     };
56     char AutorizedMac[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
57         0x02, 0x06, 0x02, 0x00
58     };
59
60     char message[1024], m_authmac = 0;
61     int i, total_mac, auth_mac = 0, mac_num = 0, begin, end;
62     varbind varbinds[1];
63
64     if (ap_type == ATMEL12350) {
65         EnableAuthMAC[5] = 0xE0;
66         EnableAuthMAC[6] = 0x3E;
67         AutorizedMac[5] = 0xE0;
68         AutorizedMac[6] = 0x3E;
69     }
70
71     varbinds[0].oid = EnableAuthMAC;
72     varbinds[0].len_oid = sizeof(EnableAuthMAC);
73     varbinds[0].type = NULL_VALUE;
74     varbinds[0].len_val = 0;
75     print_help(WAIT_RET);
76     if (snmp(varbinds, 1, GET) <= 0) {
77         print_helperr(ERR_RET);
78         goto exit;
79     }
80     print_top(NULL, MAC_TITLE);
81
82     auth_mac = *(varbinds[0].value);
83     sprintf(message, "%s%s", MAC_AUTH, (auth_mac == 1) ? ON : OFF);
84     mvwaddstr(main_sub, 0, 0, message);
85     mvwaddstr(main_sub, 2, 1, MAC_HEADER);
86     wrefresh(main_sub);
87
88     total_mac = 0;
89     mac_num = 0;
90
91     while (mac_num <= total_mac) {
92         get.Action = 0x02; rshort(get.Action);
93         get.NumOfAllTableAddresses = total_mac; rshort(get.NumOfAllTableAddresses);
94         get.NumOfCurrentAddress = mac_num; rshort(get.NumOfCurrentAddress);
95
96         varbinds[0].oid = AutorizedMac;
97         varbinds[0].len_oid = sizeof(AutorizedMac);
98         varbinds[0].value = (char *) &get;
99         varbinds[0].len_val = 12;
100         varbinds[0].type = STRING_VALUE;
101
102         if (snmp(varbinds, 1, SET) <= 0) {
103             print_helperr(ERR_RET);
104             goto exit;
105         }
106
107         if (varbinds[0].len_val == 12) {
108             if (AuthMac)
109                 free(AuthMac);
110             AuthMac =
111                 (struct AuthorizedMacTableString *) malloc(varbinds[0].
112                                                            len_val);
113             memcpy(AuthMac, varbinds[0].value, varbinds[0].len_val);
114 /*          AuthMac =
115                 (struct AuthorizedMacTableString *) varbinds[0].value;*/
116         } else {
117             print_helperr(PACKET_ERROR);
118             goto exit;
119         }
120
121         rshort(AuthMac->NumOfAllTableAddresses);
122         total_mac =
123             (AuthMac->NumOfAllTableAddresses ==
124              65535) ? 0 : AuthMac->NumOfAllTableAddresses;
125
126         if (mac_num) {
127             if (first == NULL) {
128                 first = (struct MacListStat *)
129                     malloc(sizeof(struct MacListStat));
130                 curr = first;
131             } else {
132                 curr->next = (struct MacListStat *)
133                     malloc(sizeof(struct MacListStat));
134                 curr = curr->next;
135             }
136             memcpy(curr->addr, AuthMac->MacAddress, 6);
137             curr->next = NULL;
138         }
139         mac_num++;
140     }
141
142     begin = 1;
143     end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
144     scroll_rows(first, begin, end, 3, 0);
145
146     noecho();
147
148     print_help(MAC_HELP);
149     while (1) {
150         switch (getch()) {
151         case 'q':
152         case 'Q':
153             goto quit;
154         case 'a':
155         case 'A':
156             auth_mac = on_off(0, strlen(MAC_AUTH));
157             clear_main_new(0, 1);
158             print_menusel(0, 0, MAC_AUTH, (auth_mac == 1) ? ON : OFF);
159             m_authmac = 1;
160             continue;
161         case 'd':
162         case 'D':
163             mvwaddstr(main_sub, 1, 0, MAC_DEL);
164             get_value(message, 1, strlen(MAC_DEL), 5, INT_STRING,
165                 1, mac_num - 1, NULL);
166             i = atoi(message);
167             if (i == 1) {
168                 pmac = first;
169                 first = first->next;
170                 free(pmac);
171             } else {
172                 curr = first;
173                 while (--i > 1)
174                     curr = curr->next;
175                 pmac = curr->next;
176                 curr->next = pmac->next;
177                 free(pmac);
178             }
179             mac_num--;
180             begin = 1;
181             end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
182             scroll_rows(first, begin, end, 3, 0);
183             clear_main_new(1, 2);
184             continue;
185         case 'n':
186         case 'N':
187             mvwaddstr(main_sub, 1, 0, MAC_ADD);
188             curr = first;
189             while (curr && curr->next)
190                 curr = curr->next;
191             if (first == NULL) {
192                 first = (struct MacListStat *)
193                     malloc(sizeof(struct MacListStat));
194                 curr = first;
195             } else {
196                 curr->next = (struct MacListStat *)
197                     malloc(sizeof(struct MacListStat));
198                 curr = curr->next;
199             }
200             curr->next = NULL;
201             mac_num++;
202 /*          get_value(message, 1, strlen(MAC_ADD), 13, ANY_STRING, 0, 0,
203                 NULL);
204
205             for (i = 0; i < 6; i++) {
206                 mess[0] = message[2 * i];
207                 mess[1] = message[2 * i + 1];
208                 mess[2] = '\0';
209                 curr->addr[i] = strtol(mess, NULL, 16);
210             }
211             clear_main_new(1, 2);
212 */
213             get_mac(curr->addr, 1, strlen(MAC_ADD));
214             begin = 1;
215             end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
216             scroll_rows(first, begin, end, 3, 0);
217             clear_main_new(1, 2);
218             continue;
219         case 'w':
220         case 'W':
221             if (m_authmac) {
222                 auth_mac_hw = swap4(auth_mac);
223                 varbinds[0].oid = EnableAuthMAC;
224                 varbinds[0].len_oid = sizeof(EnableAuthMAC);
225                 varbinds[0].type = INT_VALUE;
226                 varbinds[0].value = (char *) &auth_mac_hw;
227                 varbinds[0].len_val = 1;
228                 print_help(WAIT_SET);
229                 if (snmp(varbinds, 1, SET) <= 0) {
230                     print_helperr(ERR_RET);
231                     goto exit;
232                 }
233             }
234             curr = first;
235             i = 1;
236             while (curr != NULL) {
237                 get.Action = 0x01; rshort(get.Action);
238                 get.NumOfAllTableAddresses = mac_num - 1; rshort(get.NumOfAllTableAddresses);
239                 get.NumOfCurrentAddress = i; rshort(get.NumOfCurrentAddress);
240                 memcpy(get.MacAddress, curr->addr, 6);
241                 varbinds[0].oid = AutorizedMac;
242                 varbinds[0].len_oid = sizeof(AutorizedMac);
243                 varbinds[0].value = (char *) &get;
244                 varbinds[0].len_val = 12;
245                 varbinds[0].type = STRING_VALUE;
246                 print_help(WAIT_SET);
247                 if (snmp(varbinds, 1, SET) <= 0) {
248                     print_helperr(ERR_RET);
249                     goto exit;
250                 }
251                 if (varbinds[0].len_val != 12) {
252                     print_helperr(PACKET_ERROR);
253                     goto exit;
254                 }
255                 curr = curr->next;
256                 i++;
257             }
258             print_help(DONE_SET);
259             goto exit;
260         case KEY_DOWN:
261         case KEY_RIGHT:
262             if (end < mac_num) {
263                 begin++;
264                 end++;
265                 scroll_rows(first, begin, end, 3, 0);
266             }
267             continue;
268         case KEY_UP:
269         case KEY_LEFT:
270             if (begin > 1) {
271                 begin--;
272                 end--;
273                 scroll_rows(first, begin, end, 3, 0);
274             }
275             continue;
276         }
277         continue;
278     }
279
280     print_help(ANY_KEY);
281   exit:
282     getch();
283   quit:
284     while ((curr = first)) {
285         first = curr->next;
286         free(curr);
287     }
288     if (AuthMac)
289         free(AuthMac);
290     print_top(NULL, NULL);
291     clear_main(0);
292 }
293
294 void nwn_auth_mac()
295 {
296     struct MacListStat *pmac, *first = NULL, *curr = NULL;
297     char Mac[] =
298         { 0x2b, 0x06, 0x01, 0x04, 0x01, 0x87, 0x29, 0x03, 0x01, 0x03, 0x02,
299         0x02, 0x01, 0x02, 0x00
300     };
301     char MacAllow[] =
302         { 0x2b, 0x06, 0x01, 0x04, 0x01, 0x87, 0x29, 0x03, 0x01, 0x03, 0x02,
303         0x02, 0x01, 0x03, 0x00
304     };
305     char MacRowStatus[] =
306         { 0x2b, 0x06, 0x01, 0x04, 0x01, 0x87, 0x29, 0x03, 0x01, 0x03, 0x02,
307         0x02, 0x01, 0x04, 0x00
308     };
309     char message[1024], auth_enable[] =
310         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
311     char destroy = 6, create = 4, allow = 1;
312     int i, auth_mac = 0, mac_num = 0, begin, end;
313     varbind varbinds[3];
314     print_top(NULL, MAC_TITLE);
315
316     mvwaddstr(main_sub, 2, 5, MAC_HEADER);
317     wrefresh(main_sub);
318     print_help(WAIT_RET);
319
320     mac_num = 1;
321     varbinds[0].oid = Mac;
322     varbinds[0].len_oid = sizeof(Mac);
323     varbinds[0].value = Mac;
324     varbinds[0].len_val = 0;
325     varbinds[0].type = NULL_VALUE;
326     if (snmp(varbinds, 1, GET_NEXT) <= 0) {
327         print_helperr(ERR_RET);
328         goto exit;
329     }
330
331     while (memcmp(varbinds[0].oid, Mac, sizeof(Mac) - 1) == 0) {
332
333         Mac[sizeof(Mac) - 1] = varbinds[0].oid[sizeof(Mac) - 1];
334         MacAllow[sizeof(MacAllow) - 1] = varbinds[0].oid[sizeof(Mac) - 1];
335         MacRowStatus[sizeof(MacRowStatus) - 1] =
336             varbinds[0].oid[sizeof(Mac) - 1];
337
338         varbinds[0].oid = Mac;
339         varbinds[0].len_oid = sizeof(Mac);
340         varbinds[0].value = Mac;
341         varbinds[0].len_val = 0;
342         varbinds[0].type = NULL_VALUE;
343         varbinds[1].oid = MacAllow;
344         varbinds[1].len_oid = sizeof(MacAllow);
345         varbinds[1].value = Mac;
346         varbinds[1].len_val = 0;
347         varbinds[1].type = NULL_VALUE;
348         varbinds[2].oid = MacRowStatus;
349         varbinds[2].len_oid = sizeof(MacRowStatus);
350         varbinds[2].value = Mac;
351         varbinds[2].len_val = 0;
352         varbinds[2].type = NULL_VALUE;
353         if (snmp(varbinds, 3, GET) <= 0) {
354             print_helperr(ERR_RET);
355             goto exit;
356         }
357         if (memcmp(auth_enable, varbinds[0].value, 6)) {
358             if (first == NULL) {
359                 first = (struct MacListStat *)
360                     malloc(sizeof(struct MacListStat));
361                 curr = first;
362             } else {
363                 curr->next = (struct MacListStat *)
364                     malloc(sizeof(struct MacListStat));
365                 curr = curr->next;
366             }
367             memcpy(curr->addr, varbinds[0].value, 6);
368             curr->next = NULL;
369             mac_num++;
370         } else
371             auth_mac = 1;
372
373
374         varbinds[0].oid = Mac;
375         varbinds[0].len_oid = sizeof(Mac);
376         varbinds[0].value = Mac;
377         varbinds[0].len_val = 0;
378         varbinds[0].type = NULL_VALUE;
379         if (snmp(varbinds, 1, GET_NEXT) <= 0) {
380             print_helperr(ERR_RET);
381             goto exit;
382         }
383     }
384
385     begin = 1;
386     end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
387
388     sprintf(message, "%s%s", MAC_AUTH, (auth_mac) ? OFF : ON);
389     mvwaddstr(main_sub, 0, 0, message);
390
391     scroll_rows(first, begin, end, 3, 0);
392
393     noecho();
394
395     print_help(MAC_HELP);
396     while (1) {
397         switch (getch()) {
398         case 'Q':
399         case 'q':
400             goto quit;
401         case 'a':
402             auth_mac = on_off(0, strlen(MAC_AUTH)) - 1;
403             clear_main_new(0, 1);
404             print_menusel(0, 0, MAC_AUTH, (auth_mac) ? OFF : ON);
405             continue;
406         case 'd':
407             mvwaddstr(main_sub, 1, 0, MAC_DEL);
408             get_value(message, 1, strlen(MAC_DEL), 5, INT_STRING,
409                 1, mac_num - 1, NULL);
410             i = atoi(message);
411             if (i == 1) {
412                 pmac = first;
413                 first = first->next;
414                 free(pmac);
415             } else {
416                 curr = first;
417                 while (--i > 1)
418                     curr = curr->next;
419                 pmac = curr->next;
420                 curr->next = pmac->next;
421                 free(pmac);
422             }
423             mac_num--;
424             begin = 1;
425             end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
426             scroll_rows(first, begin, end, 3, 0);
427             clear_main_new(1, 2);
428             continue;
429         case 'n':
430         case 'N':
431             mvwaddstr(main_sub, 1, 0, MAC_ADD);
432             curr = first;
433             while (curr && curr->next)
434                 curr = curr->next;
435             if (first == NULL) {
436                 first = (struct MacListStat *)
437                     malloc(sizeof(struct MacListStat));
438                 curr = first;
439             } else {
440                 curr->next = (struct MacListStat *)
441                     malloc(sizeof(struct MacListStat));
442                 curr = curr->next;
443             }
444             curr->next = NULL;
445             mac_num++;
446             get_mac(curr->addr, 1, strlen(MAC_ADD));
447 /*
448             for (i = 0; i < 6; i++) {
449                 get_value(message, 1, 20 + i * 3, 3, ANY_STRING, 0, 0, NULL);
450                 curr->addr[i] = strtol(message, NULL, 16);
451             }
452             clear_main_new(1, 2);
453 */
454             begin = 1;
455             end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
456             scroll_rows(first, begin, end, 3, 0);
457             continue;
458         case 'w':
459         case 'W':
460             print_help(WAIT_SET);
461
462             Mac[sizeof(Mac) - 1] = 0;
463
464             varbinds[0].oid = Mac;
465             varbinds[0].len_oid = sizeof(Mac);
466             varbinds[0].value = Mac;
467             varbinds[0].len_val = 0;
468             varbinds[0].type = NULL_VALUE;
469             if (snmp(varbinds, 1, GET_NEXT) <= 0) {
470                 print_helperr(ERR_RET);
471                 goto exit;
472             }
473             while (memcmp(varbinds[0].oid, Mac, sizeof(Mac) - 1) == 0) {
474
475                 MacRowStatus[sizeof(MacRowStatus) - 1] =
476                     varbinds[0].oid[sizeof(Mac) - 1];
477                 varbinds[0].oid = MacRowStatus;
478                 varbinds[0].len_oid = sizeof(MacRowStatus);
479                 varbinds[0].value = &destroy;
480                 varbinds[0].len_val = 1;
481                 varbinds[0].type = INT_VALUE;
482                 if (snmp(varbinds, 1, SET) <= 0) {
483                     print_helperr(ERR_RET);
484                     goto exit;
485                 }
486                 varbinds[0].oid = Mac;
487                 varbinds[0].len_oid = sizeof(Mac);
488                 varbinds[0].value = Mac;
489                 varbinds[0].len_val = 0;
490                 varbinds[0].type = NULL_VALUE;
491                 if (snmp(varbinds, 1, GET_NEXT) <= 0) {
492                     print_helperr(ERR_RET);
493                     goto exit;
494                 }
495             }
496
497
498             curr = first;
499             i = 1;
500
501             if (auth_mac) {
502                 Mac[sizeof(Mac) - 1] = i;
503                 MacAllow[sizeof(MacAllow) - 1] = i;
504                 MacRowStatus[sizeof(MacRowStatus) - 1] = i;
505
506                 varbinds[0].oid = MacRowStatus;
507                 varbinds[0].len_oid = sizeof(MacRowStatus);
508                 varbinds[0].value = &create;
509                 varbinds[0].len_val = 1;
510                 varbinds[0].type = INT_VALUE;
511                 varbinds[1].oid = Mac;
512                 varbinds[1].len_oid = sizeof(Mac);
513                 varbinds[1].value = auth_enable;
514                 varbinds[1].len_val = 6;
515                 varbinds[1].type = STRING_VALUE;
516                 varbinds[2].oid = MacAllow;
517                 varbinds[2].len_oid = sizeof(MacAllow);
518                 varbinds[2].value = &allow;
519                 varbinds[2].len_val = 1;
520                 varbinds[2].type = INT_VALUE;
521                 print_help(WAIT_SET);
522                 if (snmp(varbinds, 3, SET) <= 0) {
523                     print_helperr(ERR_RET);
524                     goto exit;
525                 }
526                 i++;
527             }
528
529
530
531
532             while (curr != NULL) {
533                 Mac[sizeof(Mac) - 1] = i;
534                 MacAllow[sizeof(MacAllow) - 1] = i;
535                 MacRowStatus[sizeof(MacRowStatus) - 1] = i;
536
537                 varbinds[0].oid = MacRowStatus;
538                 varbinds[0].len_oid = sizeof(MacRowStatus);
539                 varbinds[0].value = &create;
540                 varbinds[0].len_val = 1;
541                 varbinds[0].type = INT_VALUE;
542                 varbinds[1].oid = Mac;
543                 varbinds[1].len_oid = sizeof(Mac);
544                 varbinds[1].value = curr->addr;
545                 varbinds[1].len_val = 6;
546                 varbinds[1].type = STRING_VALUE;
547                 varbinds[2].oid = MacAllow;
548                 varbinds[2].len_oid = sizeof(MacAllow);
549                 varbinds[2].value = &allow;
550                 varbinds[2].len_val = 1;
551                 varbinds[2].type = INT_VALUE;
552                 print_help(WAIT_SET);
553                 if (snmp(varbinds, 3, SET) <= 0) {
554                     print_helperr(ERR_RET);
555                     goto exit;
556                 }
557                 curr = curr->next;
558                 i++;
559             }
560             print_help(DONE_SET);
561             goto exit;
562         case KEY_DOWN:
563         case KEY_RIGHT:
564             if (end < mac_num) {
565                 begin++;
566                 end++;
567                 scroll_rows(first, begin, end, 3, 0);
568             }
569
570             continue;
571         case KEY_UP:
572         case KEY_LEFT:
573             if (begin > 1) {
574                 begin--;
575                 end--;
576                 scroll_rows(first, begin, end, 3, 0);
577             }
578             continue;
579         }
580         continue;
581     }
582
583     print_help(ANY_KEY);
584   exit:
585     getch();
586   quit:
587     while ((curr = first)) {
588         first = curr->next;
589         free(curr);
590     }
591     print_top(NULL, NULL);
592     clear_main(0);
593 }