]> git.decadent.org.uk Git - ap-utils.git/blob - ap-gl/auth.c
Imported Upstream version 1.5~pre3~a
[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        if (i < 0)
163             break;
164
165         
166         if (varbinds[0].len_val == 12) {
167             if (AuthMac)
168                 free(AuthMac);
169             AuthMac =
170                 (struct AuthorizedMacTableString *) malloc(varbinds[0].
171                                                            len_val);
172             memcpy(AuthMac, varbinds[0].value, varbinds[0].len_val);
173 /*          AuthMac =
174                 (struct AuthorizedMacTableString *) varbinds[0].value;*/
175         } else {
176             print_helperr(PACKET_ERROR);
177             goto exit;
178         }
179
180         rshort(AuthMac->NumOfAllTableAddresses);
181         total_mac =
182             (AuthMac->NumOfAllTableAddresses ==
183              65535) ? 0 : AuthMac->NumOfAllTableAddresses;
184
185         if (mac_num) {
186             if (first == NULL) {
187                 first = (struct MacListStat *)
188                     malloc(sizeof(struct MacListStat));
189                 curr = first;
190             } else {
191                 curr->next = (struct MacListStat *)
192                     malloc(sizeof(struct MacListStat));
193                 curr = curr->next;
194             }
195             memcpy(curr->addr, AuthMac->MacAddress, 6);
196             curr->next = NULL;
197         }
198         mac_num++;
199     }
200
201 choose_view:
202     clear_main(2);
203     if (auth_mac == 1) {
204         mvwaddstr(main_sub, 2, 1, MAC_HEADER);
205         begin = 1;
206         end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
207         scroll_rows(first, begin, end, 3, 0);
208         print_help(MAC_HELP);
209     } else if(auth_mac == 3) {
210         sprintf(message, "%s%s", RADIUS_IP, inet_ntoa(radius_ip));
211         mvwaddstr(main_sub, 2, 0, message);
212         sprintf(message, "%s%d", RADIUS_DEST_PORT, radius_dest_port);
213         mvwaddstr(main_sub, 3, 0, message);
214         sprintf(message, "%s%s", RADIUS_SECRET, radius_secret);
215         mvwaddstr(main_sub, 4, 0, message);
216         sprintf(message, "%s%d", RADIUS_TIME, radius_time);
217         mvwaddstr(main_sub, 5, 0, message);
218         sprintf(message, "%s%d", RADIUS_PORT, radius_port);
219         mvwaddstr(main_sub, 6, 0, message);
220
221         wrefresh(main_sub);
222         print_help(MAC_RADIUS_HELP);
223     } else {
224         print_help(MAC_NONE_HELP);
225     }
226
227     if (i < 0) {
228         mvwaddstr(main_sub, 3, 1, _("(insufficient community used)"));
229         wrefresh(main_sub);
230     }
231
232     noecho();
233
234     while (1) {
235         switch (getch()) {
236         case 'q':
237         case 'Q':
238             goto quit;
239         case 'a':
240         case 'A':
241             auth_mac = menu_choose(0, strlen(MAC_AUTH), auth_types, 3)+1;
242             clear_main_new(0, 1);
243             print_menusel(0, 0, MAC_AUTH, auth_types[auth_mac-1]);
244             m_authmac = 1;
245             goto choose_view;
246         case 'S':
247         case 's':
248             if (auth_mac != 3) {
249                         continue;
250             }
251             get_value(radius_secret, 4, strlen(RADIUS_SECRET), 32, ANY_STRING, 0, 0, NULL);
252             m_radius_secret = 1;
253             continue;
254         case 'P':
255         case 'p':
256             if (auth_mac != 3) {
257                continue;
258             }
259             get_value(message, 3, strlen(RADIUS_DEST_PORT), 6, INT_STRING, 0, 65535, MAC_RADIUS_HELP);
260             radius_dest_port = atoi(message);
261             m_radius_dest_port = 1;
262             continue;
263         case 'T':
264         case 't':
265             if (auth_mac != 3) {
266                     continue;
267             }
268             get_value(message, 5, strlen(RADIUS_TIME), 6, INT_STRING, 0, 65535, MAC_RADIUS_HELP);
269             radius_time = atoi(message);
270             m_radius_time = 1;
271             continue;
272         case 'F':
273         case 'f':
274             if (auth_mac != 3) {
275                continue;
276             }
277             get_value(message, 6, strlen(RADIUS_PORT), 6, INT_STRING, 0, 65535, MAC_RADIUS_HELP);
278             radius_port = atoi(message);
279             m_radius_port = 1;
280             continue;
281         case 'I':
282         case 'i':
283             if (auth_mac != 3) {
284                 continue;
285             }
286             get_ip(&radius_ip, 2, strlen(RADIUS_IP), MAC_RADIUS_HELP);
287             m_radius_ip = 1;
288             continue;
289         case 'd':
290         case 'D':
291             if (auth_mac != 1) {
292                 continue;
293             }
294             mvwaddstr(main_sub, 1, 0, MAC_DEL);
295             get_value(message, 1, strlen(MAC_DEL), 5, INT_STRING,
296                 1, mac_num - 1, NULL);
297             i = atoi(message);
298             if (i == 1) {
299                 pmac = first;
300                 first = first->next;
301                 free(pmac);
302             } else {
303                 curr = first;
304                 while (--i > 1)
305                     curr = curr->next;
306                 pmac = curr->next;
307                 curr->next = pmac->next;
308                 free(pmac);
309             }
310             mac_num--;
311             begin = 1;
312             end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
313             scroll_rows(first, begin, end, 3, 0);
314             clear_main_new(1, 2);
315             continue;
316         case 'n':
317         case 'N':
318             if (auth_mac != 1) {
319                 continue;
320             }
321             mvwaddstr(main_sub, 1, 0, MAC_ADD);
322             curr = first;
323             while (curr && curr->next)
324                 curr = curr->next;
325             if (first == NULL) {
326                 first = (struct MacListStat *)
327                     malloc(sizeof(struct MacListStat));
328                 curr = first;
329             } else {
330                 curr->next = (struct MacListStat *)
331                     malloc(sizeof(struct MacListStat));
332                 curr = curr->next;
333             }
334             curr->next = NULL;
335             mac_num++;
336             get_mac(curr->addr, 1, strlen(MAC_ADD));
337             begin = 1;
338             end = (MAX_LINES < mac_num) ? MAX_LINES : mac_num;
339             scroll_rows(first, begin, end, 3, 0);
340             clear_main_new(1, 2);
341             continue;
342         case 'w':
343         case 'W':
344             i=0;
345             if (m_authmac) {
346                 auth_mac_hw = swap4(auth_mac);
347                 varbinds[i].oid = EnableAuthMAC;
348                 varbinds[i].len_oid = sizeof(EnableAuthMAC);
349                 varbinds[i].type = INT_VALUE;
350                 varbinds[i].value = (char *) &auth_mac_hw;
351                 varbinds[i].len_val = 1;
352                 i++;
353             }
354             
355             if (m_radius_ip) {
356                 radius_ip.s_addr = htonl(radius_ip.s_addr);
357                 radius_ip.s_addr = swap4(radius_ip.s_addr);
358                 varbinds[i].oid = AuthRadiusIP;
359                 varbinds[i].len_oid = sizeof(AuthRadiusIP);
360                 varbinds[i].value = (char *) &radius_ip.s_addr;
361                 varbinds[i].len_val = 4;
362                 varbinds[i].type = INT_VALUE;
363                 i++;
364             }
365            
366             if (m_radius_dest_port) {
367                 varbinds[i].oid = AuthRadiusDestinPort;
368                 varbinds[i].len_oid = sizeof(AuthRadiusDestinPort);
369                 radius_dest_port = htons(radius_dest_port);
370                 varbinds[i].value = (char *) &radius_dest_port;
371                 varbinds[i].len_val = 2;
372                 varbinds[i].type = 0x02;
373                 i++;
374             }
375             
376             if (m_radius_time) {
377                 varbinds[i].oid = AuthRadiusReauthorizationTime;
378                 varbinds[i].len_oid = sizeof(AuthRadiusReauthorizationTime);
379                 radius_time = htons(radius_time);
380                 varbinds[i].value = (char *) &radius_time;
381                 varbinds[i].len_val = 2;
382                 varbinds[i].type = 0x02;
383                 i++;
384             }
385
386            if (m_radius_port) {
387                 varbinds[i].oid = AuthRadiusPort;
388                 varbinds[i].len_oid = sizeof(AuthRadiusPort);
389                 radius_port = htons(radius_port);
390                 varbinds[i].value = (char *) &radius_port;
391                 varbinds[i].len_val = 2;
392                 varbinds[i].type = 0x02;
393                 i++;
394             }
395            if (m_radius_secret) {
396                 c = strlen(radius_secret);
397                 varbinds[i].oid = AuthRadiusSecretLength;
398                 varbinds[i].len_oid = sizeof(AuthRadiusSecretLength);
399                 varbinds[i].value = (char *)&c;
400                 varbinds[i].len_val = 1;
401                 varbinds[i].type = INT_VALUE;
402                 i++;
403                 varbinds[i].oid = AuthRadiusSecret;
404                 varbinds[i].len_oid = sizeof(AuthRadiusSecret);
405                 varbinds[i].value = radius_secret;
406                 varbinds[i].len_val = c;
407                 varbinds[i].type = STRING_VALUE;
408                 i++;
409            }
410
411             print_help(WAIT_SET);
412             if (snmp(varbinds, i, SET) <= 0) {
413                 print_helperr(ERR_RET);
414                 goto exit;
415             }
416             
417             curr = first;
418             i = 1;
419             while (curr != NULL) {
420                 get.Action = 0x01; rshort(get.Action);
421                 get.NumOfAllTableAddresses = mac_num - 1; rshort(get.NumOfAllTableAddresses);
422                 get.NumOfCurrentAddress = i; rshort(get.NumOfCurrentAddress);
423                 memcpy(get.MacAddress, curr->addr, 6);
424                 varbinds[0].oid = AutorizedMac;
425                 varbinds[0].len_oid = sizeof(AutorizedMac);
426                 varbinds[0].value = (char *) &get;
427                 varbinds[0].len_val = 12;
428                 varbinds[0].type = STRING_VALUE;
429                 print_help(WAIT_SET);
430                 if (snmp(varbinds, 1, SET) <= 0) {
431                     print_helperr(ERR_RET);
432                     goto exit;
433                 }
434                 if (varbinds[0].len_val != 12) {
435                     print_helperr(PACKET_ERROR);
436                     goto exit;
437                 }
438                 curr = curr->next;
439                 i++;
440             }
441             print_help(DONE_SET);
442             goto exit;
443         case KEY_DOWN:
444         case KEY_RIGHT:
445             if (auth_mac != 1) {
446                 continue;
447             }
448             if (end < mac_num) {
449                 begin++;
450                 end++;
451                 scroll_rows(first, begin, end, 3, 0);
452             }
453             continue;
454         case KEY_UP:
455         case KEY_LEFT:
456             if (auth_mac != 1) {
457                 continue;
458             }
459             if (begin > 1) {
460                 begin--;
461                 end--;
462                 scroll_rows(first, begin, end, 3, 0);
463             }
464             continue;
465         }
466         continue;
467     }
468
469     print_help(ANY_KEY);
470   exit:
471     getch();
472   quit:
473     while ((curr = first)) {
474         first = curr->next;
475         free(curr);
476     }
477     if (AuthMac)
478         free(AuthMac);
479     print_top(NULL, NULL);
480     clear_main(0);
481 }