]> git.decadent.org.uk Git - ap-utils.git/blob - ap-gl/auth_mac.c
Imported Upstream version 1.5~pre1
[ap-utils.git] / ap-gl / 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 #define MAC_RADIUS_HELP _("A - auth; IPSTF - set; W - write conf; Q - quit")
36 #define MAC_NONE_HELP _("A - auth; W - write conf; Q - quit")
37
38 #define RADIUS_IP _("[I] RADIUS SERVER IP: ")
39 #define RADIUS_DEST_PORT _("[P] RADIUS SERVER PORT: ")
40 #define RADIUS_SECRET _("[S] RADIUS SERVER SECRET: ")
41 #define RADIUS_TIME _("[T] REAUTHORIZATION TIME: ")
42 #define RADIUS_PORT _("[F] RADIUS SOURCE PORT: ")
43
44 extern WINDOW *main_sub;
45 extern int LINES;
46 extern short ap_type;
47
48 void atmel_auth_mac()
49 {
50     struct AuthorizedMacTableString {
51         unsigned int short Action;
52         unsigned int short NumOfAllTableAddresses;
53         unsigned int short NumOfCurrentAddress;
54         unsigned char MacAddress[6];
55     } *AuthMac = NULL, get;
56
57     struct MacListStat *pmac, *first = NULL, *curr = NULL;
58     uint32_t auth_mac_hw;
59
60     char EnableAuthMAC[] =
61         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
62         0x02, 0x06, 0x01, 0x00
63     };
64     char AutorizedMac[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
65         0x02, 0x06, 0x02, 0x00
66     };
67
68     char AuthRadiusIP[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
69         0x02, 0x06, 0x03, 0x00
70     };
71     char AuthRadiusSecret[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
72         0x02, 0x06, 0x04, 0x00
73     };
74     char AuthRadiusSecretLength[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
75         0x02, 0x06, 0x05, 0x00
76     };
77     char AuthRadiusPort[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
78         0x02, 0x06, 0x06, 0x00
79     };
80     char AuthRadiusReauthorizationTime[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
81         0x02, 0x06, 0x07, 0x00
82     };
83     char AuthRadiusDestinPort[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
84         0x02, 0x06, 0x08, 0x00
85     };
86
87     char *auth_types[] = { _("Internal"), OFF, "RADIUS" };
88
89     struct in_addr radius_ip;
90     char message[1024], m_authmac = 0, radius_secret[32], m_radius_secret=0, m_radius_ip=0;
91     int radius_port, radius_time, radius_dest_port, m_radius_port=0, m_radius_time=0, m_radius_dest_port=0;
92     int i, total_mac, auth_mac = 0, mac_num = 0, begin=0, end=0, c=0;
93     varbind varbinds[7];
94
95     memcpy(radius_secret, _("<hidden>"), 32); 
96
97     varbinds[0].oid = EnableAuthMAC;
98     varbinds[0].len_oid = sizeof(EnableAuthMAC);
99     varbinds[0].type = NULL_VALUE;
100     varbinds[0].len_val = 0;
101
102     varbinds[1].oid = AuthRadiusIP;
103     varbinds[1].len_oid = sizeof(AuthRadiusIP);
104     varbinds[1].type = NULL_VALUE;
105     varbinds[1].len_val = 0;
106
107     varbinds[3].oid = AuthRadiusPort;
108     varbinds[3].len_oid = sizeof(AuthRadiusPort);
109     varbinds[3].type = NULL_VALUE;
110     varbinds[3].len_val = 0;
111
112     varbinds[4].oid = AuthRadiusReauthorizationTime;
113     varbinds[4].len_oid = sizeof(AuthRadiusReauthorizationTime);
114     varbinds[4].type = NULL_VALUE;
115     varbinds[4].len_val = 0;
116
117     varbinds[2].oid = AuthRadiusDestinPort;
118     varbinds[2].len_oid = sizeof(AuthRadiusDestinPort);
119     varbinds[2].type = NULL_VALUE;
120     varbinds[2].len_val = 0;
121
122     print_help(WAIT_RET);
123     if (snmp(varbinds, 5, GET) <= 0) {
124         print_helperr(ERR_RET);
125         goto exit;
126     }
127     print_top(NULL, MAC_TITLE);
128
129     auth_mac = *(varbinds[0].value);
130     memcpy(&radius_ip.s_addr, varbinds[1].value, 4);
131     memcpy(&radius_dest_port, varbinds[2].value, 2);
132     radius_dest_port = ntohs(radius_dest_port);
133     memcpy(&radius_port, varbinds[3].value, 2);
134     radius_port = ntohs(radius_port);
135     memcpy(&radius_time, varbinds[4].value, 2);
136     radius_time = ntohs(radius_time);
137         
138     sprintf(message, "%s%s", MAC_AUTH, auth_types[auth_mac-1]);
139     mvwaddstr(main_sub, 0, 0, message);
140     wrefresh(main_sub);
141
142     total_mac = 0;
143     mac_num = 0;
144
145     while (mac_num <= total_mac) {
146         get.Action = 0x02; rshort(get.Action);
147         get.NumOfAllTableAddresses = total_mac; rshort(get.NumOfAllTableAddresses);
148         get.NumOfCurrentAddress = mac_num; rshort(get.NumOfCurrentAddress);
149
150         varbinds[0].oid = AutorizedMac;
151         varbinds[0].len_oid = sizeof(AutorizedMac);
152         varbinds[0].value = (char *) &get;
153         varbinds[0].len_val = 12;
154         varbinds[0].type = STRING_VALUE;
155
156         if (snmp(varbinds, 1, SET) <= 0) {
157             print_helperr(ERR_RET);
158             goto exit;
159         }
160
161         if (varbinds[0].len_val == 12) {
162             if (AuthMac)
163                 free(AuthMac);
164             AuthMac =
165                 (struct AuthorizedMacTableString *) malloc(varbinds[0].
166                                                            len_val);
167             memcpy(AuthMac, varbinds[0].value, varbinds[0].len_val);
168 /*          AuthMac =
169                 (struct AuthorizedMacTableString *) varbinds[0].value;*/
170         } else {
171             print_helperr(PACKET_ERROR);
172             goto exit;
173         }
174
175         rshort(AuthMac->NumOfAllTableAddresses);
176         total_mac =
177             (AuthMac->NumOfAllTableAddresses ==
178              65535) ? 0 : AuthMac->NumOfAllTableAddresses;
179
180         if (mac_num) {
181             if (first == NULL) {
182                 first = (struct MacListStat *)
183                     malloc(sizeof(struct MacListStat));
184                 curr = first;
185             } else {
186                 curr->next = (struct MacListStat *)
187                     malloc(sizeof(struct MacListStat));
188                 curr = curr->next;
189             }
190             memcpy(curr->addr, AuthMac->MacAddress, 6);
191             curr->next = NULL;
192         }
193         mac_num++;
194     }
195
196 choose_view:
197     clear_main(2);
198     if (auth_mac == 1) {
199         mvwaddstr(main_sub, 2, 1, MAC_HEADER);
200         begin = 1;
201         end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
202         scroll_rows(first, begin, end, 3, 0);
203         print_help(MAC_HELP);
204     } else if(auth_mac == 3) {
205         sprintf(message, "%s%s", RADIUS_IP, inet_ntoa(radius_ip));
206         mvwaddstr(main_sub, 2, 0, message);
207         sprintf(message, "%s%d", RADIUS_DEST_PORT, radius_dest_port);
208         mvwaddstr(main_sub, 3, 0, message);
209         sprintf(message, "%s%s", RADIUS_SECRET, radius_secret);
210         mvwaddstr(main_sub, 4, 0, message);
211         sprintf(message, "%s%d", RADIUS_TIME, radius_time);
212         mvwaddstr(main_sub, 5, 0, message);
213         sprintf(message, "%s%d", RADIUS_PORT, radius_port);
214         mvwaddstr(main_sub, 6, 0, message);
215
216         wrefresh(main_sub);
217         print_help(MAC_RADIUS_HELP);
218     } else {
219         print_help(MAC_NONE_HELP);
220     }
221     noecho();
222
223     while (1) {
224         switch (getch()) {
225         case 'q':
226         case 'Q':
227             goto quit;
228         case 'a':
229         case 'A':
230             auth_mac = menu_choose(0, strlen(MAC_AUTH), auth_types, 3)+1;
231             clear_main_new(0, 1);
232             print_menusel(0, 0, MAC_AUTH, auth_types[auth_mac-1]);
233             m_authmac = 1;
234             goto choose_view;
235         case 'S':
236         case 's':
237             if (auth_mac != 3) {
238                         continue;
239             }
240             get_value(radius_secret, 4, strlen(RADIUS_SECRET), 32, ANY_STRING, 0, 0, NULL);
241             m_radius_secret = 1;
242             continue;
243         case 'P':
244         case 'p':
245             if (auth_mac != 3) {
246                continue;
247             }
248             get_value(message, 3, strlen(RADIUS_DEST_PORT), 6, INT_STRING, 0, 65535, MAC_RADIUS_HELP);
249             radius_dest_port = atoi(message);
250             m_radius_dest_port = 1;
251             continue;
252         case 'T':
253         case 't':
254             if (auth_mac != 3) {
255                     continue;
256             }
257             get_value(message, 5, strlen(RADIUS_TIME), 6, INT_STRING, 0, 65535, MAC_RADIUS_HELP);
258             radius_time = atoi(message);
259             m_radius_time = 1;
260             continue;
261         case 'F':
262         case 'f':
263             if (auth_mac != 3) {
264                continue;
265             }
266             get_value(message, 6, strlen(RADIUS_PORT), 6, INT_STRING, 0, 65535, MAC_RADIUS_HELP);
267             radius_port = atoi(message);
268             m_radius_port = 1;
269             continue;
270         case 'I':
271         case 'i':
272             if (auth_mac != 3) {
273                 continue;
274             }
275             get_ip(&radius_ip, 2, strlen(RADIUS_IP), MAC_RADIUS_HELP);
276             m_radius_ip = 1;
277             continue;
278         case 'd':
279         case 'D':
280             if (auth_mac != 1) {
281                 continue;
282             }
283             mvwaddstr(main_sub, 1, 0, MAC_DEL);
284             get_value(message, 1, strlen(MAC_DEL), 5, INT_STRING,
285                 1, mac_num - 1, NULL);
286             i = atoi(message);
287             if (i == 1) {
288                 pmac = first;
289                 first = first->next;
290                 free(pmac);
291             } else {
292                 curr = first;
293                 while (--i > 1)
294                     curr = curr->next;
295                 pmac = curr->next;
296                 curr->next = pmac->next;
297                 free(pmac);
298             }
299             mac_num--;
300             begin = 1;
301             end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
302             scroll_rows(first, begin, end, 3, 0);
303             clear_main_new(1, 2);
304             continue;
305         case 'n':
306         case 'N':
307             if (auth_mac != 1) {
308                 continue;
309             }
310             mvwaddstr(main_sub, 1, 0, MAC_ADD);
311             curr = first;
312             while (curr && curr->next)
313                 curr = curr->next;
314             if (first == NULL) {
315                 first = (struct MacListStat *)
316                     malloc(sizeof(struct MacListStat));
317                 curr = first;
318             } else {
319                 curr->next = (struct MacListStat *)
320                     malloc(sizeof(struct MacListStat));
321                 curr = curr->next;
322             }
323             curr->next = NULL;
324             mac_num++;
325             get_mac(curr->addr, 1, strlen(MAC_ADD));
326             begin = 1;
327             end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
328             scroll_rows(first, begin, end, 3, 0);
329             clear_main_new(1, 2);
330             continue;
331         case 'w':
332         case 'W':
333             i=0;
334             if (m_authmac) {
335                 auth_mac_hw = swap4(auth_mac);
336                 varbinds[i].oid = EnableAuthMAC;
337                 varbinds[i].len_oid = sizeof(EnableAuthMAC);
338                 varbinds[i].type = INT_VALUE;
339                 varbinds[i].value = (char *) &auth_mac_hw;
340                 varbinds[i].len_val = 1;
341                 i++;
342             }
343             
344             if (m_radius_ip) {
345                 radius_ip.s_addr = htonl(radius_ip.s_addr);
346                 radius_ip.s_addr = swap4(radius_ip.s_addr);
347                 varbinds[i].oid = AuthRadiusIP;
348                 varbinds[i].len_oid = sizeof(AuthRadiusIP);
349                 varbinds[i].value = (char *) &radius_ip.s_addr;
350                 varbinds[i].len_val = 4;
351                 varbinds[i].type = INT_VALUE;
352                 i++;
353             }
354            
355             if (m_radius_dest_port) {
356                 varbinds[i].oid = AuthRadiusDestinPort;
357                 varbinds[i].len_oid = sizeof(AuthRadiusDestinPort);
358                 radius_dest_port = htons(radius_dest_port);
359                 varbinds[i].value = (char *) &radius_dest_port;
360                 varbinds[i].len_val = 2;
361                 varbinds[i].type = 0x02;
362                 i++;
363             }
364             
365             if (m_radius_time) {
366                 varbinds[i].oid = AuthRadiusReauthorizationTime;
367                 varbinds[i].len_oid = sizeof(AuthRadiusReauthorizationTime);
368                 radius_time = htons(radius_time);
369                 varbinds[i].value = (char *) &radius_time;
370                 varbinds[i].len_val = 2;
371                 varbinds[i].type = 0x02;
372                 i++;
373             }
374
375            if (m_radius_port) {
376                 varbinds[i].oid = AuthRadiusPort;
377                 varbinds[i].len_oid = sizeof(AuthRadiusPort);
378                 radius_port = htons(radius_port);
379                 varbinds[i].value = (char *) &radius_port;
380                 varbinds[i].len_val = 2;
381                 varbinds[i].type = 0x02;
382                 i++;
383             }
384            if (m_radius_secret) {
385                 c = strlen(radius_secret);
386                 varbinds[i].oid = AuthRadiusSecretLength;
387                 varbinds[i].len_oid = sizeof(AuthRadiusSecretLength);
388                 varbinds[i].value = (char *)&c;
389                 varbinds[i].len_val = 1;
390                 varbinds[i].type = INT_VALUE;
391                 i++;
392                 varbinds[i].oid = AuthRadiusSecret;
393                 varbinds[i].len_oid = sizeof(AuthRadiusSecret);
394                 varbinds[i].value = radius_secret;
395                 varbinds[i].len_val = c;
396                 varbinds[i].type = STRING_VALUE;
397                 i++;
398            }
399
400             print_help(WAIT_SET);
401             if (snmp(varbinds, i, SET) <= 0) {
402                 print_helperr(ERR_RET);
403                 goto exit;
404             }
405             
406             curr = first;
407             i = 1;
408             while (curr != NULL) {
409                 get.Action = 0x01; rshort(get.Action);
410                 get.NumOfAllTableAddresses = mac_num - 1; rshort(get.NumOfAllTableAddresses);
411                 get.NumOfCurrentAddress = i; rshort(get.NumOfCurrentAddress);
412                 memcpy(get.MacAddress, curr->addr, 6);
413                 varbinds[0].oid = AutorizedMac;
414                 varbinds[0].len_oid = sizeof(AutorizedMac);
415                 varbinds[0].value = (char *) &get;
416                 varbinds[0].len_val = 12;
417                 varbinds[0].type = STRING_VALUE;
418                 print_help(WAIT_SET);
419                 if (snmp(varbinds, 1, SET) <= 0) {
420                     print_helperr(ERR_RET);
421                     goto exit;
422                 }
423                 if (varbinds[0].len_val != 12) {
424                     print_helperr(PACKET_ERROR);
425                     goto exit;
426                 }
427                 curr = curr->next;
428                 i++;
429             }
430             print_help(DONE_SET);
431             goto exit;
432         case KEY_DOWN:
433         case KEY_RIGHT:
434             if (auth_mac != 1) {
435                 continue;
436             }
437             if (end < mac_num) {
438                 begin++;
439                 end++;
440                 scroll_rows(first, begin, end, 3, 0);
441             }
442             continue;
443         case KEY_UP:
444         case KEY_LEFT:
445             if (auth_mac != 1) {
446                 continue;
447             }
448             if (begin > 1) {
449                 begin--;
450                 end--;
451                 scroll_rows(first, begin, end, 3, 0);
452             }
453             continue;
454         }
455         continue;
456     }
457
458     print_help(ANY_KEY);
459   exit:
460     getch();
461   quit:
462     while ((curr = first)) {
463         first = curr->next;
464         free(curr);
465     }
466     if (AuthMac)
467         free(AuthMac);
468     print_top(NULL, NULL);
469     clear_main(0);
470 }