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