Annotation of embedaddon/coova-chilli/src/test-radius.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (c) 2006-2007 David Bird <david@coova.com>
        !             3:  *
        !             4:  */
        !             5: #include "system.h"
        !             6: #include "syserr.h"
        !             7: #include "cmdline.h"
        !             8: #include "dhcp.h"
        !             9: #include "radius.h"
        !            10: #include "radius_chillispot.h"
        !            11: #include "radius_wispr.h"
        !            12: #include "redir.h"
        !            13: #include "chilli.h"
        !            14: #include "options.h"
        !            15: 
        !            16: static int chilliauth_cb(struct radius_t *radius,
        !            17:                         struct radius_packet_t *pack,
        !            18:                         struct radius_packet_t *pack_req, void *cbp) {
        !            19:   struct radius_attr_t *attr = NULL;
        !            20:   /*char attrs[RADIUS_ATTR_VLEN+1];*/
        !            21:   size_t offset = 0;
        !            22:   
        !            23:   if (!pack) { 
        !            24:     sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Radius request timed out");
        !            25:     return 0;
        !            26:   }
        !            27: 
        !            28:   if ((pack->code != RADIUS_CODE_ACCESS_REJECT) && 
        !            29:       (pack->code != RADIUS_CODE_ACCESS_CHALLENGE) &&
        !            30:       (pack->code != RADIUS_CODE_ACCESS_ACCEPT)) {
        !            31:     sys_err(LOG_ERR, __FILE__, __LINE__, 0, 
        !            32:            "Unknown radius access reply code %d", pack->code);
        !            33:     return 0;
        !            34:   }
        !            35: 
        !            36:   /* ACCESS-ACCEPT */
        !            37:   if (pack->code != RADIUS_CODE_ACCESS_ACCEPT) {
        !            38:     sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Administrative-User Login Failed");
        !            39:     return 0;
        !            40:   }
        !            41: 
        !            42:   while (!radius_getnextattr(pack, &attr, 
        !            43:                             RADIUS_ATTR_VENDOR_SPECIFIC,
        !            44:                             RADIUS_VENDOR_CHILLISPOT,
        !            45:                             RADIUS_ATTR_CHILLISPOT_CONFIG, 
        !            46:                             0, &offset)) {
        !            47:     char value[RADIUS_ATTR_VLEN+1] = "";
        !            48:     strncpy(value, (const char *)attr->v.t, attr->l - 2);
        !            49:     printf("%s\n", value);
        !            50:   }
        !            51: 
        !            52:   return 0;
        !            53:   
        !            54: }
        !            55: 
        !            56: int static test_radius() {
        !            57:   struct radius_t *radius;
        !            58:   struct radius_packet_t radius_pack;
        !            59:   struct timeval idleTime;
        !            60:   int starttime;
        !            61:   int maxfd = 0;
        !            62:   fd_set fds;
        !            63:   int status;
        !            64: 
        !            65:   if (!options.adminuser || !options.adminpasswd) return 1;
        !            66: 
        !            67:   if (radius_new(&radius, &options.radiuslisten, 0, 0, NULL, 0, NULL, NULL, NULL)) {
        !            68:     sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Failed to create radius");
        !            69:     return -1;
        !            70:   }
        !            71: 
        !            72:   radius_set(radius, 0, (options.debug & DEBUG_RADIUS));
        !            73: 
        !            74:   radius_set_cb_auth_conf(radius, chilliauth_cb); 
        !            75: 
        !            76:   {int cnt=0; for (; cnt < RADIUS_QUEUESIZE * 2; cnt++) {
        !            77: 
        !            78:   if (radius_default_pack(radius, &radius_pack, RADIUS_CODE_ACCESS_REQUEST)) {
        !            79:     sys_err(LOG_ERR, __FILE__, __LINE__, 0, "radius_default_pack() failed");
        !            80:     return -1;
        !            81:   }
        !            82:   
        !            83:   radius_addattr(radius, &radius_pack, RADIUS_ATTR_USER_NAME, 0, 0, 0,
        !            84:                 (uint8_t *)options.adminuser, strlen(options.adminuser));
        !            85: 
        !            86:   radius_addattr(radius, &radius_pack, RADIUS_ATTR_USER_PASSWORD, 0, 0, 0,
        !            87:                 (uint8_t *)options.adminpasswd, strlen(options.adminpasswd));
        !            88: 
        !            89:   radius_addnasip(radius, &radius_pack);
        !            90: 
        !            91:   radius_addattr(radius, &radius_pack, RADIUS_ATTR_SERVICE_TYPE, 0, 0,
        !            92:                 RADIUS_SERVICE_TYPE_ADMIN_USER, NULL, 0); 
        !            93:   
        !            94:   if (options.radiusnasid)
        !            95:     radius_addattr(radius, &radius_pack, RADIUS_ATTR_NAS_IDENTIFIER, 0, 0, 0,
        !            96:                   (uint8_t *)options.radiusnasid, strlen(options.radiusnasid));
        !            97:   
        !            98:   if (options.nasmac)
        !            99:     radius_addattr(radius, &radius_pack, RADIUS_ATTR_CALLED_STATION_ID, 0, 0, 0,
        !           100:                   (uint8_t *)options.nasmac, strlen(options.nasmac)); 
        !           101: 
        !           102:   radius_addattr(radius, &radius_pack, RADIUS_ATTR_NAS_PORT_TYPE, 0, 0,
        !           103:                 options.radiusnasporttype, NULL, 0);
        !           104: 
        !           105:   if (options.radiuslocationid)
        !           106:     radius_addattr(radius, &radius_pack, RADIUS_ATTR_VENDOR_SPECIFIC,
        !           107:                   RADIUS_VENDOR_WISPR, RADIUS_ATTR_WISPR_LOCATION_ID, 0,
        !           108:                   (uint8_t *)options.radiuslocationid, strlen(options.radiuslocationid));
        !           109: 
        !           110:   if (options.radiuslocationname)
        !           111:     radius_addattr(radius, &radius_pack, RADIUS_ATTR_VENDOR_SPECIFIC,
        !           112:                   RADIUS_VENDOR_WISPR, RADIUS_ATTR_WISPR_LOCATION_NAME, 0,
        !           113:                   (uint8_t *)options.radiuslocationname, 
        !           114:                   strlen(options.radiuslocationname));
        !           115:   
        !           116:   radius_addattr(radius, &radius_pack, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, 
        !           117:                 0, 0, 0, NULL, RADIUS_MD5LEN);
        !           118: 
        !           119:   radius_req(radius, &radius_pack, NULL); 
        !           120: 
        !           121:   if (radius->fd <= 0) {
        !           122:     sys_err(LOG_ERR, __FILE__, __LINE__, 0, "not a valid socket!");
        !           123:     return -1;
        !           124:   } 
        !           125: 
        !           126:   maxfd = radius->fd;
        !           127: 
        !           128:   starttime = time(NULL);
        !           129:   while ((starttime + REDIR_RADIUS_MAX_TIME) > time(NULL)) {
        !           130:     FD_ZERO(&fds);
        !           131:     FD_SET(radius->fd, &fds);
        !           132:     
        !           133:     idleTime.tv_sec = 0;
        !           134:     idleTime.tv_usec = REDIR_RADIUS_SELECT_TIME;
        !           135:     radius_timeleft(radius, &idleTime);
        !           136: 
        !           137:     switch (status = select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
        !           138:     case -1:
        !           139:       sys_err(LOG_ERR, __FILE__, __LINE__, errno, "select() returned -1!");
        !           140:       break;  
        !           141:     case 0:
        !           142:       radius_timeout(radius);
        !           143:     default:
        !           144:       break;
        !           145:     }
        !           146: 
        !           147:     if (status > 0) {
        !           148:       if (FD_ISSET(radius->fd, &fds)) {
        !           149:        if (radius_decaps(radius) < 0) {
        !           150:          sys_err(LOG_ERR, __FILE__, __LINE__, 0, "radius_ind() failed!");
        !           151:        }
        !           152:        break;
        !           153:       }
        !           154:     }
        !           155:   }  
        !           156: 
        !           157:    }};
        !           158: 
        !           159:   radius_free(radius);
        !           160:   return 0;
        !           161: }
        !           162: 
        !           163: int main(int argc, char **argv)
        !           164: {
        !           165:   if (process_options(argc, argv, 1))
        !           166:     exit(1);
        !           167:   
        !           168:   return test_radius();
        !           169: }

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