]> git.decadent.org.uk Git - ap-utils.git/blobdiff - src/ap-auth.c
Merge commit 'upstream/1.5'
[ap-utils.git] / src / ap-auth.c
index fbd72ef7ff96f2db71ed44fed3af4cde82c3ea82..9375c3d568cb964207e4055959365e51e9cf4716 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *      ap-auth.c from Access Point SNMP Utils for Linux
  *
- * Copyright (c) 2002 Roman Festchook <roma at polesye dot net>
+ * Copyright (c) 2004 Teemu Kiviniemi <teemuki at fotokone.fi>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License Version 2 from
 #endif
 #include "ap-utils.h"
 
-#define PACKET_ERROR _("AuthorizedMacTableString packet error")
+#define ERROR_PACKET _("AuthorizedMacTableString packet error")
 #define ERROR_DATA _("Invalid data in source file")
 #define ERROR_FILE_OPEN _("Can't open file")
 #define ERROR_FILE_WRITE _("Can't write to file")
 #define ERROR_FILE_CLOSE _("Error closing file")
 
-short ap_type = ATMEL410;
+short ap_type;
 char *community = NULL;
-int sockfd;
 struct in_addr ap_ip;
 
 void usage()
@@ -45,16 +44,15 @@ void usage()
     printf(_("\nUsage:\n"));
     printf(_("\tap-auth -i ip -c community -d filename [-h]\n"));
     printf(_("\tap-auth -i ip -c community -u filename [-h]\n\n"));
-    printf(_("Change accesspoint's list of authorised MAC"
-        " addresses\n\n"));
+    printf(_("Change accesspoint's list of authorised MAC addresses\n\n"));
     printf(_("-i ip        - AP ip address\n"));
     printf(_("-c community - SNMP community string\n"));
     printf(_("-d filename  - download list of authorised MAC addresses from AP"
-        " to a file\n"));
+       " to a file\n"));
     printf(_("-u filename  - upload list of authorised MAC addresses from"
-        " a file to AP\n"));
+       " a file to AP\n"));
     printf(_("-h           - print this help screen\n\n"));
-    printf(_("ap-auth %s Copyright (c) 2002-2004 Roman Festchook\n\n"),
+    printf(_("ap-auth %s Copyright (c) 2004 Teemu Kiviniemi\n\n"),
           VERSION);
 }
 
@@ -73,18 +71,18 @@ int get_addr (struct MacListStat *ml, char *addr)
 {
     int i;
     char tmp[3];
-    
+
     if (strlen (addr) != 12)
         return 1;
-    
+
     tmp[2] = '\0';
-    
+
     for (i = 0; i < 6 ; i++) {
         tmp[0] = addr[2 * i];
        tmp[1] = addr[2 * i + 1];
        ml->addr[i] = strtol (tmp, NULL, 16);
     }
-    
+
     return 0;
 }
 
@@ -96,47 +94,48 @@ int main(int argc, char **argv)
     extern int optopt;
     int opt = 0;
     int mode = 0; /* 1 = download, 2 = upload */
-
     FILE *f;
     char *filename = NULL;
-    struct sockaddr_in client;
-    
+
     struct AuthorizedMacTableString {
        unsigned int short Action;
        unsigned int short NumOfAllTableAddresses;
        unsigned int short NumOfCurrentAddress;
        unsigned char MacAddress[6];
     } *AuthMac = NULL, get;
-    
+
     struct MacListStat *first = NULL, *curr = NULL;
-    
-    char AutorizedMac[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01,
-       0x02, 0x06, 0x02, 0x00
+
+    char sysDescr_NWN[] = {
+       0x2B, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00
+    };
+    char sysDescr_ATMEL[] = {
+       0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x01, 0x01, 0x00
+    };
+    char AutorizedMac_ATMEL[] = {
+       0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x06, 0x02, 0x00
     };
-    
+
     int total_mac, mac_num = 0;
     varbind varbinds[1];
     char mac_tmp[13];
+    char *cp;
     struct MacListStat ml_tmp;
     int i, tmp;
-        
+
 #ifdef HAVE_GETTEXT
     setlocale(LC_ALL, "");
     bindtextdomain("ap-utils", LOCALEDIR);
     textdomain("ap-utils");
 #endif
 
-    memset(&client, 0, sizeof client);
-    client.sin_family = AF_INET;
-    client.sin_port = INADDR_ANY;
-    client.sin_addr.s_addr = INADDR_ANY;
-    
     do {
        opterr = 0;
        switch (opt = getopt(argc, argv, "i:c:d:u:")) {
        case 'i':
-           if (inet_aton(optarg, &ap_ip) == 0) {
-               printf(_("Invalid IP-address\n"));
+           for (cp = optarg, i = 0; *cp && (cp = index(cp, '.')); cp++, i++);
+           if (i < 3 || inet_aton(optarg, &ap_ip) == 0) {
+               printf(_("Error: invalid IP-address.\n"));
                return 1;
            }
            break;
@@ -161,35 +160,77 @@ int main(int argc, char **argv)
            return 1;
        }
     } while (opt != -1);
-    
+
     if (!community) {
        usage();
        return 1;
     }
-    
-    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
-       perror(_("Create socket error"));
-       return 1;
+
+    /*
+     * Part detecting ap_type (ATMEL AP MIB type) follows.
+     * We could use get_mib_details() here with advantage, but it would
+     * have to involve 1. putting it to separate file in lib/ and
+     * 2. patch it so it would not contain curses-related commands (TODO)
+     */
+
+    /* determine private MIB type according to enterprises ID */
+    varbinds[0].oid = sysDescr_NWN;
+    varbinds[0].len_oid = sizeof(sysDescr_NWN);
+    varbinds[0].value = NULL;
+    varbinds[0].len_val = 0;
+    varbinds[0].type = NULL_VALUE;
+    if (snmp(varbinds, 1, GET) > 0) {
+        ap_type = NWN;
+    } else {
+       varbinds[0].oid = sysDescr_ATMEL;
+       varbinds[0].len_oid = sizeof(sysDescr_ATMEL);
+       varbinds[0].value = NULL;
+       varbinds[0].len_val = 0;
+       varbinds[0].type = NULL_VALUE;
+       if (snmp(varbinds, 1, GET) > 0) {
+           ap_type = ATMEL410;
+       } else {
+           sysDescr_ATMEL[5] = 0xE0;
+           sysDescr_ATMEL[6] = 0x3E;
+           varbinds[0].oid = sysDescr_ATMEL;
+           varbinds[0].len_oid = sizeof(sysDescr_ATMEL);
+           varbinds[0].value = NULL;
+           varbinds[0].len_val = 0;
+           varbinds[0].type = NULL_VALUE;
+           if (snmp(varbinds, 1, GET) > 0) {
+               ap_type = ATMEL12350;
+           } else {
+               printf(_("Unable to determine AP MIB type "
+                   "(no response from AP)."));
+               return 1;
+           }
+       }
     }
-    if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1) {
-       perror(_("Bind socket error"));
+
+    if (ap_type == NWN) {
+       printf(_("NWN devices are not yet supported."));
        return 1;
     }
-    
+
+    if (ap_type == ATMEL12350) {
+       AutorizedMac_ATMEL[5] = 0xE0;
+       AutorizedMac_ATMEL[6] = 0x3E;
+    }
+
     switch (mode) {
-    
+
     case 1: /* download */
-    
+
        total_mac = 0;
        mac_num = 0;
-       
+
        while (mac_num <= total_mac) {
            get.Action = 0x02; rshort(get.Action);
            get.NumOfAllTableAddresses = total_mac; rshort(get.NumOfAllTableAddresses);
            get.NumOfCurrentAddress = mac_num; rshort(get.NumOfCurrentAddress);
            
-           varbinds[0].oid = AutorizedMac;
-           varbinds[0].len_oid = sizeof(AutorizedMac);
+           varbinds[0].oid = AutorizedMac_ATMEL;
+           varbinds[0].len_oid = sizeof(AutorizedMac_ATMEL);
            varbinds[0].value = (char *) &get;
            varbinds[0].len_val = 12;
            varbinds[0].type = STRING_VALUE;
@@ -199,7 +240,7 @@ int main(int argc, char **argv)
                printf("\n");
                return 1;
            }
-           
+
            if (varbinds[0].len_val == 12) {
                if (AuthMac)
                    free(AuthMac);
@@ -208,16 +249,16 @@ int main(int argc, char **argv)
                                                               len_val);
                memcpy(AuthMac, varbinds[0].value, varbinds[0].len_val);
            } else {
-               printf(PACKET_ERROR);
+               printf(ERROR_PACKET);
                printf("\n");
                return 1;
            }
-           
+
            rshort(AuthMac->NumOfAllTableAddresses);
            total_mac =
                (AuthMac->NumOfAllTableAddresses ==
                 65535) ? 0 : AuthMac->NumOfAllTableAddresses;
-       
+
            if (mac_num) {
                if (first == NULL) {
                    first = (struct MacListStat *)
@@ -264,7 +305,7 @@ int main(int argc, char **argv)
        mac_num  = 0;
        while (!feof (f)) {
            tmp = fread (mac_tmp, 1, sizeof (mac_tmp), f);
-           
+
            if (tmp == sizeof (mac_tmp)) {
                if (mac_tmp[12] != '\n') {
                    printf(ERROR_DATA);
@@ -272,7 +313,7 @@ int main(int argc, char **argv)
                    return 1;
                }
                mac_tmp[12] = '\0';
-               
+
                if (get_addr (&ml_tmp, mac_tmp) != 0) {
                    printf(ERROR_DATA);
                    printf("\n");
@@ -293,7 +334,7 @@ int main(int argc, char **argv)
            }
        }
        fclose(f);
-       
+
        curr = first;
        i = 1;
        while (curr != NULL) {
@@ -304,8 +345,8 @@ int main(int argc, char **argv)
            get.NumOfCurrentAddress = i;
            rshort(get.NumOfCurrentAddress);
            memcpy(get.MacAddress, curr->addr, 6);
-           varbinds[0].oid = AutorizedMac;
-           varbinds[0].len_oid = sizeof(AutorizedMac);
+           varbinds[0].oid = AutorizedMac_ATMEL;
+           varbinds[0].len_oid = sizeof(AutorizedMac_ATMEL);
            varbinds[0].value = (char *) &get;
            varbinds[0].len_val = 12;
            varbinds[0].type = STRING_VALUE;
@@ -315,7 +356,7 @@ int main(int argc, char **argv)
                return 1;
            }
            if (varbinds[0].len_val != 12) {
-               printf(PACKET_ERROR);
+               printf(ERROR_PACKET);
                printf("\n");
                return 1;
            }
@@ -327,16 +368,14 @@ int main(int argc, char **argv)
        usage();
        return 1;
     }
-    
-    close(sockfd);
-    
+
     curr = first;
     while (curr != NULL) {
         curr = curr->next;
        free (first);
        first = curr;
     }
-    
+
     if (community)
        free(community);
     if (filename)