2 * ap-auth.c from Access Point SNMP Utils for Linux
4 * Copyright (c) 2002 Roman Festchook <roma at polesye dot net>
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 PACKET_ERROR _("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")
38 short ap_type = ATMEL410;
39 char *community = NULL;
45 printf(_("\nUsage:\n"));
46 printf(_("\tap-auth -i ip -c community -d filename [-h]\n"));
47 printf(_("\tap-auth -i ip -c community -u filename [-h]\n\n"));
48 printf(_("Change accesspoint's list of authorised MAC"
50 printf(_("-i ip - AP ip address\n"));
51 printf(_("-c community - SNMP community string\n"));
52 printf(_("-d filename - download list of authorised MAC addresses from AP"
54 printf(_("-u filename - upload list of authorised MAC addresses from"
56 printf(_("-h - print this help screen\n\n"));
57 printf(_("ap-auth %s Copyright (c) 2002-2004 Roman Festchook\n\n"),
61 int write_addr (FILE *f, const struct MacListStat *n)
63 if (fprintf (f, "%02x%02x%02x%02x%02x%02x\n", n->addr[0], n->addr[1],
64 n->addr[2], n->addr[3],
65 n->addr[4], n->addr[5])
72 int get_addr (struct MacListStat *ml, char *addr)
77 if (strlen (addr) != 12)
82 for (i = 0; i < 6 ; i++) {
84 tmp[1] = addr[2 * i + 1];
85 ml->addr[i] = strtol (tmp, NULL, 16);
91 int main(int argc, char **argv)
98 int mode = 0; /* 1 = download, 2 = upload */
101 char *filename = NULL;
102 struct sockaddr_in client;
104 struct AuthorizedMacTableString {
105 unsigned int short Action;
106 unsigned int short NumOfAllTableAddresses;
107 unsigned int short NumOfCurrentAddress;
108 unsigned char MacAddress[6];
109 } *AuthMac = NULL, get;
111 struct MacListStat *first = NULL, *curr = NULL;
113 char AutorizedMac[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
114 0x02, 0x06, 0x02, 0x00
117 int total_mac, mac_num = 0;
120 struct MacListStat ml_tmp;
124 setlocale(LC_ALL, "");
125 bindtextdomain("ap-utils", LOCALEDIR);
126 textdomain("ap-utils");
129 memset(&client, 0, sizeof client);
130 client.sin_family = AF_INET;
131 client.sin_port = INADDR_ANY;
132 client.sin_addr.s_addr = INADDR_ANY;
136 switch (opt = getopt(argc, argv, "i:c:d:u:")) {
138 if (inet_aton(optarg, &ap_ip) == 0) {
139 printf(_("Invalid IP-address\n"));
144 community = malloc(strlen(optarg) + 1);
145 strncpy(community, optarg, strlen(optarg) + 1);
148 filename = malloc(strlen(optarg) + 1);
149 strncpy(filename, optarg, strlen(optarg) + 1);
153 filename = malloc(strlen(optarg) + 1);
154 strncpy(filename, optarg, strlen(optarg) + 1);
170 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
171 perror(_("Create socket error"));
174 if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1) {
175 perror(_("Bind socket error"));
181 case 1: /* download */
186 while (mac_num <= total_mac) {
187 get.Action = 0x02; rshort(get.Action);
188 get.NumOfAllTableAddresses = total_mac; rshort(get.NumOfAllTableAddresses);
189 get.NumOfCurrentAddress = mac_num; rshort(get.NumOfCurrentAddress);
191 varbinds[0].oid = AutorizedMac;
192 varbinds[0].len_oid = sizeof(AutorizedMac);
193 varbinds[0].value = (char *) &get;
194 varbinds[0].len_val = 12;
195 varbinds[0].type = STRING_VALUE;
197 if (snmp(varbinds, 1, SET) <= 0) {
203 if (varbinds[0].len_val == 12) {
207 (struct AuthorizedMacTableString *) malloc(varbinds[0].
209 memcpy(AuthMac, varbinds[0].value, varbinds[0].len_val);
211 printf(PACKET_ERROR);
216 rshort(AuthMac->NumOfAllTableAddresses);
218 (AuthMac->NumOfAllTableAddresses ==
219 65535) ? 0 : AuthMac->NumOfAllTableAddresses;
223 first = (struct MacListStat *)
224 malloc(sizeof(struct MacListStat));
227 curr->next = (struct MacListStat *)
228 malloc(sizeof(struct MacListStat));
231 memcpy(curr->addr, AuthMac->MacAddress, 6);
236 f = fopen(filename, "w");
238 perror(ERROR_FILE_OPEN);
242 /* no authorised addresses */
245 while (curr != NULL) {
246 if(write_addr (f, curr) != 0) {
247 perror(ERROR_FILE_WRITE);
254 perror(ERROR_FILE_CLOSE);
259 f = fopen(filename, "r");
261 perror(ERROR_FILE_OPEN);
266 tmp = fread (mac_tmp, 1, sizeof (mac_tmp), f);
268 if (tmp == sizeof (mac_tmp)) {
269 if (mac_tmp[12] != '\n') {
276 if (get_addr (&ml_tmp, mac_tmp) != 0) {
282 first = (struct MacListStat *)
283 malloc (sizeof (struct MacListStat));
286 curr->next = (struct MacListStat *)
287 malloc (sizeof (struct MacListStat));
290 memcpy (curr, &ml_tmp, sizeof (struct MacListStat));
299 while (curr != NULL) {
302 get.NumOfAllTableAddresses = mac_num;
303 rshort(get.NumOfAllTableAddresses);
304 get.NumOfCurrentAddress = i;
305 rshort(get.NumOfCurrentAddress);
306 memcpy(get.MacAddress, curr->addr, 6);
307 varbinds[0].oid = AutorizedMac;
308 varbinds[0].len_oid = sizeof(AutorizedMac);
309 varbinds[0].value = (char *) &get;
310 varbinds[0].len_val = 12;
311 varbinds[0].type = STRING_VALUE;
312 if (snmp(varbinds, 1, SET) <= 0) {
317 if (varbinds[0].len_val != 12) {
318 printf(PACKET_ERROR);
334 while (curr != NULL) {