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