]> git.decadent.org.uk Git - ap-utils.git/blob - src/ap-auth.c
Imported Upstream version 1.4.2~pre2~a
[ap-utils.git] / src / ap-auth.c
1 /*
2  *      ap-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 #include <stdio.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
27 #if defined (__GLIBC__)
28 #include <libgen.h>
29 #endif
30 #include "ap-utils.h"
31
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")
37
38 short ap_type = ATMEL410;
39 char *community = NULL;
40 int sockfd;
41 struct in_addr ap_ip;
42
43 void usage()
44 {
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"
49         " addresses\n\n"));
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"
53         " to a file\n"));
54     printf(_("-u filename  - upload list of authorised MAC addresses from"
55         " a file to AP\n"));
56     printf(_("-h           - print this help screen\n\n"));
57     printf(_("ap-auth %s Copyright (c) 2002-2004 Roman Festchook\n\n"),
58            VERSION);
59 }
60
61 int write_addr (FILE *f, const struct MacListStat *n)
62 {
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])
66                                                 != 13)
67         return 1;
68
69     return 0;
70 }
71
72 int get_addr (struct MacListStat *ml, char *addr)
73 {
74     int i;
75     char tmp[3];
76     
77     if (strlen (addr) != 12)
78         return 1;
79     
80     tmp[2] = '\0';
81     
82     for (i = 0; i < 6 ; i++) {
83         tmp[0] = addr[2 * i];
84         tmp[1] = addr[2 * i + 1];
85         ml->addr[i] = strtol (tmp, NULL, 16);
86     }
87     
88     return 0;
89 }
90
91 int main(int argc, char **argv)
92 {
93     extern char *optarg;
94     extern int optind;
95     extern int opterr;
96     extern int optopt;
97     int opt = 0;
98     int mode = 0; /* 1 = download, 2 = upload */
99
100     FILE *f;
101     char *filename = NULL;
102     struct sockaddr_in client;
103     
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;
110     
111     struct MacListStat *first = NULL, *curr = NULL;
112     
113     char AutorizedMac[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
114         0x02, 0x06, 0x02, 0x00
115     };
116     
117     int total_mac, mac_num = 0;
118     varbind varbinds[1];
119     char mac_tmp[13];
120     struct MacListStat ml_tmp;
121     int i, tmp;
122         
123 #ifdef HAVE_GETTEXT
124     setlocale(LC_ALL, "");
125     bindtextdomain("ap-utils", LOCALEDIR);
126     textdomain("ap-utils");
127 #endif
128
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;
133     
134     do {
135         opterr = 0;
136         switch (opt = getopt(argc, argv, "i:c:d:u:")) {
137         case 'i':
138             if (inet_aton(optarg, &ap_ip) == 0) {
139                 printf(_("Invalid IP-address\n"));
140                 return 1;
141             }
142             break;
143         case 'c':
144             community = malloc(strlen(optarg) + 1);
145             strncpy(community, optarg, strlen(optarg) + 1);
146             break;
147         case 'd':
148             filename = malloc(strlen(optarg) + 1);
149             strncpy(filename, optarg, strlen(optarg) + 1);
150             mode = 1;
151             break;
152         case 'u':
153             filename = malloc(strlen(optarg) + 1);
154             strncpy(filename, optarg, strlen(optarg) + 1);
155             mode = 2;
156             break;
157         case -1:
158             break;
159         default:
160             usage();
161             return 1;
162         }
163     } while (opt != -1);
164     
165     if (!community) {
166         usage();
167         return 1;
168     }
169     
170     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
171         perror(_("Create socket error"));
172         return 1;
173     }
174     if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1) {
175         perror(_("Bind socket error"));
176         return 1;
177     }
178     
179     switch (mode) {
180     
181     case 1: /* download */
182     
183         total_mac = 0;
184         mac_num = 0;
185         
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);
190             
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;
196             
197             if (snmp(varbinds, 1, SET) <= 0) {
198                 printf(ERR_RET);
199                 printf("\n");
200                 return 1;
201             }
202             
203             if (varbinds[0].len_val == 12) {
204                 if (AuthMac)
205                     free(AuthMac);
206                 AuthMac = 
207                     (struct AuthorizedMacTableString *) malloc(varbinds[0].
208                                                                len_val);
209                 memcpy(AuthMac, varbinds[0].value, varbinds[0].len_val);
210             } else {
211                 printf(PACKET_ERROR);
212                 printf("\n");
213                 return 1;
214             }
215             
216             rshort(AuthMac->NumOfAllTableAddresses);
217             total_mac =
218                 (AuthMac->NumOfAllTableAddresses ==
219                  65535) ? 0 : AuthMac->NumOfAllTableAddresses;
220         
221             if (mac_num) {
222                 if (first == NULL) {
223                     first = (struct MacListStat *)
224                         malloc(sizeof(struct MacListStat));
225                     curr = first;
226                 } else {
227                     curr->next = (struct MacListStat *)
228                         malloc(sizeof(struct MacListStat));
229                     curr = curr->next;
230                 }
231                 memcpy(curr->addr, AuthMac->MacAddress, 6);
232                 curr->next = NULL;
233             }
234             mac_num++;
235         }
236         f = fopen(filename, "w");
237         if(f == NULL) {
238             perror(ERROR_FILE_OPEN);
239             return 1;
240         }
241         if (first == NULL) {
242             /* no authorised addresses */
243         } else {
244             curr = first;
245             while (curr != NULL) {
246                 if(write_addr (f, curr) != 0) {
247                     perror(ERROR_FILE_WRITE);
248                     return 1;
249                 }
250                 curr = curr->next;
251             }
252         }
253         if(fclose(f) != 0) {
254             perror(ERROR_FILE_CLOSE);
255             return 1;
256         }
257         break;
258     case 2: /* upload */
259         f = fopen(filename, "r");
260         if (f == NULL) {
261             perror(ERROR_FILE_OPEN);
262             return 1;
263         }
264         mac_num  = 0;
265         while (!feof (f)) {
266             tmp = fread (mac_tmp, 1, sizeof (mac_tmp), f);
267             
268             if (tmp == sizeof (mac_tmp)) {
269                 if (mac_tmp[12] != '\n') {
270                     printf(ERROR_DATA);
271                     printf("\n");
272                     return 1;
273                 }
274                 mac_tmp[12] = '\0';
275                 
276                 if (get_addr (&ml_tmp, mac_tmp) != 0) {
277                     printf(ERROR_DATA);
278                     printf("\n");
279                     return 1;
280                 }
281                 if (first == NULL) {
282                     first = (struct MacListStat *)
283                         malloc (sizeof (struct MacListStat));
284                     curr = first;
285                 } else {
286                     curr->next = (struct MacListStat *)
287                         malloc (sizeof (struct MacListStat));
288                     curr = curr->next;
289                 }
290                 memcpy (curr, &ml_tmp, sizeof (struct MacListStat));
291                 curr->next = NULL;
292                 mac_num++;
293             }
294         }
295         fclose(f);
296         
297         curr = first;
298         i = 1;
299         while (curr != NULL) {
300             get.Action = 0x01;
301             rshort(get.Action);
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) {
313                 printf(ERR_RET);
314                 printf("\n");
315                 return 1;
316             }
317             if (varbinds[0].len_val != 12) {
318                 printf(PACKET_ERROR);
319                 printf("\n");
320                 return 1;
321             }
322             curr = curr->next;
323             i++;
324         }
325         break;
326     default:
327         usage();
328         return 1;
329     }
330     
331     close(sockfd);
332     
333     curr = first;
334     while (curr != NULL) {
335         curr = curr->next;
336         free (first);
337         first = curr;
338     }
339     
340     if (community)
341         free(community);
342     if (filename)
343         free(filename);
344     return 0;
345 }