2 * ap-auth.c from Access Point SNMP Utils for Linux
4 * Copyright (c) 2004 Teemu Kiviniemi <teemuki at fotokone.fi>
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.
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.
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
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
27 #if defined (__GLIBC__)
32 #define ERROR_PACKET _("AuthorizedMacTableString packet error")
33 #define ERROR_DATA _("Invalid data in source file")
34 #define ERROR_FILE_OPEN _("Can't open file")
35 #define ERROR_FILE_WRITE _("Can't write to file")
36 #define ERROR_FILE_CLOSE _("Error closing file")
39 char *community = NULL;
44 printf(_("\nUsage:\n"));
45 printf(_("\tap-auth -i ip -c community -d filename [-h]\n"));
46 printf(_("\tap-auth -i ip -c community -u filename [-h]\n\n"));
47 printf(_("Change accesspoint's list of authorised MAC addresses\n\n"));
48 printf(_("-i ip - AP ip address\n"));
49 printf(_("-c community - SNMP community string\n"));
50 printf(_("-d filename - download list of authorised MAC addresses from AP"
52 printf(_("-u filename - upload list of authorised MAC addresses from"
54 printf(_("-h - print this help screen\n\n"));
55 printf(_("ap-auth %s Copyright (c) 2004 Teemu Kiviniemi\n\n"),
59 int write_addr (FILE *f, const struct MacListStat *n)
61 if (fprintf (f, "%02x%02x%02x%02x%02x%02x\n", n->addr[0], n->addr[1],
62 n->addr[2], n->addr[3],
63 n->addr[4], n->addr[5])
70 int get_addr (struct MacListStat *ml, char *addr)
75 if (strlen (addr) != 12)
80 for (i = 0; i < 6 ; i++) {
82 tmp[1] = addr[2 * i + 1];
83 ml->addr[i] = strtol (tmp, NULL, 16);
89 int main(int argc, char **argv)
96 int mode = 0; /* 1 = download, 2 = upload */
98 char *filename = NULL;
100 struct AuthorizedMacTableString {
101 unsigned int short Action;
102 unsigned int short NumOfAllTableAddresses;
103 unsigned int short NumOfCurrentAddress;
104 unsigned char MacAddress[6];
105 } *AuthMac = NULL, get;
107 struct MacListStat *first = NULL, *curr = NULL;
109 char sysDescr_NWN[] = {
110 0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00
112 char sysDescr_ATMEL[] = {
113 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x01, 0x01, 0x00
115 char AutorizedMac_ATMEL[] = {
116 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x06, 0x02, 0x00
119 int total_mac, mac_num = 0;
123 struct MacListStat ml_tmp;
127 setlocale(LC_ALL, "");
128 bindtextdomain("ap-utils", LOCALEDIR);
129 textdomain("ap-utils");
134 switch (opt = getopt(argc, argv, "i:c:d:u:")) {
136 for (cp = optarg, i = 0; *cp && (cp = index(cp, '.')); cp++, i++);
137 if (i < 3 || inet_aton(optarg, &ap_ip) == 0) {
138 printf(_("Error: invalid IP-address.\n"));
143 community = malloc(strlen(optarg) + 1);
144 strncpy(community, optarg, strlen(optarg) + 1);
147 filename = malloc(strlen(optarg) + 1);
148 strncpy(filename, optarg, strlen(optarg) + 1);
152 filename = malloc(strlen(optarg) + 1);
153 strncpy(filename, optarg, strlen(optarg) + 1);
170 * Part detecting ap_type (ATMEL AP MIB type) follows.
171 * We could use get_mib_details() here with advantage, but it would
172 * have to involve 1. putting it to separate file in lib/ and
173 * 2. patch it so it would not contain curses-related commands (TODO)
176 /* determine private MIB type according to enterprises ID */
177 varbinds[0].oid = sysDescr_NWN;
178 varbinds[0].len_oid = sizeof(sysDescr_NWN);
179 varbinds[0].value = NULL;
180 varbinds[0].len_val = 0;
181 varbinds[0].type = NULL_VALUE;
182 if (snmp(varbinds, 1, GET) > 0) {
185 varbinds[0].oid = sysDescr_ATMEL;
186 varbinds[0].len_oid = sizeof(sysDescr_ATMEL);
187 varbinds[0].value = NULL;
188 varbinds[0].len_val = 0;
189 varbinds[0].type = NULL_VALUE;
190 if (snmp(varbinds, 1, GET) > 0) {
193 sysDescr_ATMEL[5] = 0xE0;
194 sysDescr_ATMEL[6] = 0x3E;
195 varbinds[0].oid = sysDescr_ATMEL;
196 varbinds[0].len_oid = sizeof(sysDescr_ATMEL);
197 varbinds[0].value = NULL;
198 varbinds[0].len_val = 0;
199 varbinds[0].type = NULL_VALUE;
200 if (snmp(varbinds, 1, GET) > 0) {
201 ap_type = ATMEL12350;
203 printf(_("Unable to determine AP MIB type "
204 "(no response from AP)."));
210 if (ap_type == NWN) {
211 printf(_("NWN devices are not yet supported."));
215 if (ap_type == ATMEL12350) {
216 AutorizedMac_ATMEL[5] = 0xE0;
217 AutorizedMac_ATMEL[6] = 0x3E;
222 case 1: /* download */
227 while (mac_num <= total_mac) {
228 get.Action = 0x02; rshort(get.Action);
229 get.NumOfAllTableAddresses = total_mac; rshort(get.NumOfAllTableAddresses);
230 get.NumOfCurrentAddress = mac_num; rshort(get.NumOfCurrentAddress);
232 varbinds[0].oid = AutorizedMac_ATMEL;
233 varbinds[0].len_oid = sizeof(AutorizedMac_ATMEL);
234 varbinds[0].value = (char *) &get;
235 varbinds[0].len_val = 12;
236 varbinds[0].type = STRING_VALUE;
238 if (snmp(varbinds, 1, SET) <= 0) {
244 if (varbinds[0].len_val == 12) {
248 (struct AuthorizedMacTableString *) malloc(varbinds[0].
250 memcpy(AuthMac, varbinds[0].value, varbinds[0].len_val);
252 printf(ERROR_PACKET);
257 rshort(AuthMac->NumOfAllTableAddresses);
259 (AuthMac->NumOfAllTableAddresses ==
260 65535) ? 0 : AuthMac->NumOfAllTableAddresses;
264 first = (struct MacListStat *)
265 malloc(sizeof(struct MacListStat));
268 curr->next = (struct MacListStat *)
269 malloc(sizeof(struct MacListStat));
272 memcpy(curr->addr, AuthMac->MacAddress, 6);
277 f = fopen(filename, "w");
279 perror(ERROR_FILE_OPEN);
283 /* no authorised addresses */
286 while (curr != NULL) {
287 if(write_addr (f, curr) != 0) {
288 perror(ERROR_FILE_WRITE);
295 perror(ERROR_FILE_CLOSE);
300 f = fopen(filename, "r");
302 perror(ERROR_FILE_OPEN);
307 tmp = fread (mac_tmp, 1, sizeof (mac_tmp), f);
309 if (tmp == sizeof (mac_tmp)) {
310 if (mac_tmp[12] != '\n') {
317 if (get_addr (&ml_tmp, mac_tmp) != 0) {
323 first = (struct MacListStat *)
324 malloc (sizeof (struct MacListStat));
327 curr->next = (struct MacListStat *)
328 malloc (sizeof (struct MacListStat));
331 memcpy (curr, &ml_tmp, sizeof (struct MacListStat));
340 while (curr != NULL) {
343 get.NumOfAllTableAddresses = mac_num;
344 rshort(get.NumOfAllTableAddresses);
345 get.NumOfCurrentAddress = i;
346 rshort(get.NumOfCurrentAddress);
347 memcpy(get.MacAddress, curr->addr, 6);
348 varbinds[0].oid = AutorizedMac_ATMEL;
349 varbinds[0].len_oid = sizeof(AutorizedMac_ATMEL);
350 varbinds[0].value = (char *) &get;
351 varbinds[0].len_val = 12;
352 varbinds[0].type = STRING_VALUE;
353 if (snmp(varbinds, 1, SET) <= 0) {
358 if (varbinds[0].len_val != 12) {
359 printf(ERROR_PACKET);
373 while (curr != NULL) {