Annotation of embedaddon/coova-chilli/src/main-radconfig.c, revision 1.1.1.1

1.1       misho       1: /* 
                      2:  *
                      3:  * chilli - ChilliSpot.org. A Wireless LAN Access Point Controller.
                      4:  * Copyright (C) 2003, 2004, 2005 Mondru AB.
                      5:  * Copyright (C) 2006 PicoPoint B.V.
                      6:  * Copyright (c) 2006-2007 David Bird <david@coova.com>
                      7:  *
                      8:  * The contents of this file may be used under the terms of the GNU
                      9:  * General Public License Version 2, provided that the above copyright
                     10:  * notice and this permission notice is included in all copies or
                     11:  * substantial portions of the software.
                     12:  * 
                     13:  */
                     14: 
                     15: #include "system.h"
                     16: #include "syserr.h"
                     17: #include "cmdline.h"
                     18: #include "dhcp.h"
                     19: #include "radius.h"
                     20: #include "radius_chillispot.h"
                     21: #include "radius_wispr.h"
                     22: #include "redir.h"
                     23: #include "chilli.h"
                     24: #include "options.h"
                     25: 
                     26: #define ADMIN_TIMEOUT 10
                     27: 
                     28: static int chilliauth_cb(struct radius_t *radius,
                     29:                         struct radius_packet_t *pack,
                     30:                         struct radius_packet_t *pack_req, void *cbp) {
                     31:   struct radius_attr_t *attr = NULL;
                     32:   /*char attrs[RADIUS_ATTR_VLEN+1];*/
                     33:   size_t offset = 0;
                     34: 
                     35:   if (!pack) { 
                     36:     sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Radius request timed out");
                     37:     return 0;
                     38:   }
                     39: 
                     40:   if ((pack->code != RADIUS_CODE_ACCESS_REJECT) && 
                     41:       (pack->code != RADIUS_CODE_ACCESS_CHALLENGE) &&
                     42:       (pack->code != RADIUS_CODE_ACCESS_ACCEPT)) {
                     43:     sys_err(LOG_ERR, __FILE__, __LINE__, 0, 
                     44:            "Unknown radius access reply code %d", pack->code);
                     45:     return 0;
                     46:   }
                     47: 
                     48:   /* ACCESS-ACCEPT */
                     49:   if (pack->code != RADIUS_CODE_ACCESS_ACCEPT) {
                     50:     sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Administrative-User Login Failed");
                     51:     return 0;
                     52:   }
                     53: 
                     54:   while (!radius_getnextattr(pack, &attr, 
                     55:                             RADIUS_ATTR_VENDOR_SPECIFIC,
                     56:                             RADIUS_VENDOR_CHILLISPOT,
                     57:                             RADIUS_ATTR_CHILLISPOT_CONFIG, 
                     58:                             0, &offset)) {
                     59:     char value[RADIUS_ATTR_VLEN+1] = "";
                     60:     strncpy(value, (const char *)attr->v.t, attr->l - 2);
                     61:     printf("%s\n", value);
                     62:   }
                     63: 
                     64:   return 0;
                     65:   
                     66: }
                     67: 
                     68: int static chilliauth() {
                     69:   unsigned char hwaddr[6];
                     70:   struct radius_t *radius=0;
                     71:   struct timeval idleTime;
                     72:   int endtime, now;
                     73:   int maxfd = 0;
                     74:   fd_set fds;
                     75:   int status;
                     76:   int ret=-1;
                     77: 
                     78:   if (!options.adminuser || !options.adminpasswd) return 1;
                     79: 
                     80:   if (radius_new(&radius, &options.radiuslisten, 0, 0, NULL, 0, NULL, NULL, NULL)) {
                     81:     log_err(0, "Failed to create radius");
                     82:     return ret;
                     83:   }
                     84: 
                     85:   /* get dhcpif mac */
                     86:   memset(hwaddr, 0, sizeof(hwaddr));
                     87: 
                     88: #ifdef SIOCGIFHWADDR
                     89:   if (!options.nasmac && options.dhcpif) {
                     90:     struct ifreq ifr;
                     91:     int fd;
                     92:     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
                     93:       memset(&ifr, 0, sizeof(ifr));
                     94:       strncpy(ifr.ifr_name, options.dhcpif, IFNAMSIZ);
                     95:       if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
                     96:        log_err(errno, "ioctl(d=%d, request=%d) failed", fd, SIOCGIFHWADDR);
                     97:       }
                     98:       memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, PKT_ETH_ALEN);
                     99:       close(fd);
                    100:     }
                    101:   }
                    102: #endif
                    103: 
                    104:   radius_set(radius, hwaddr, (options.debug & DEBUG_RADIUS));
                    105:   radius_set_cb_auth_conf(radius, chilliauth_cb); 
                    106: 
                    107:   ret = chilliauth_radius(radius);
                    108: 
                    109:   if (radius->fd <= 0) {
                    110:     log_err(0, "not a valid socket!");
                    111:     return ret;
                    112:   } 
                    113: 
                    114:   maxfd = radius->fd;
                    115: 
                    116:   now = time(NULL);
                    117:   endtime = now + ADMIN_TIMEOUT; 
                    118: 
                    119:   while (endtime > now) {
                    120: 
                    121:     FD_ZERO(&fds);
                    122:     FD_SET(radius->fd, &fds);
                    123:     
                    124:     idleTime.tv_sec = 0;
                    125:     idleTime.tv_usec = REDIR_RADIUS_SELECT_TIME;
                    126:     radius_timeleft(radius, &idleTime);
                    127: 
                    128:     switch (status = select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
                    129:     case -1:
                    130:       sys_err(LOG_ERR, __FILE__, __LINE__, errno, "select() returned -1!");
                    131:       break;  
                    132:     case 0:
                    133:       radius_timeout(radius);
                    134:     default:
                    135:       break;
                    136:     }
                    137: 
                    138:     if (status > 0) {
                    139:       if (FD_ISSET(radius->fd, &fds)) {
                    140:        if (radius_decaps(radius) < 0) {
                    141:          sys_err(LOG_ERR, __FILE__, __LINE__, 0, "radius_ind() failed!");
                    142:        }
                    143:        else {
                    144:          ret = 0;
                    145:        }
                    146:        break;
                    147:       }
                    148:     }
                    149: 
                    150:     now = time(NULL);
                    151:   }  
                    152: 
                    153:   radius_free(radius);
                    154:   return ret;
                    155: }
                    156: 
                    157: int main(int argc, char **argv)
                    158: {
                    159:   if (process_options(argc, argv, 1))
                    160:     exit(1);
                    161:   
                    162:   return chilliauth();
                    163: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>