Navigation


Changeset 550:4c935aecee6c in freeDiameter


Ignore:
Timestamp:
Sep 15, 2010, 2:24:45 PM (14 years ago)
Author:
Sebastien Decugis <sdecugis@nict.go.jp>
Branch:
default
Phase:
public
Message:

Hide and automate the Proxy-State attributes management in RADIUS gateway

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • doc/echodrop.rgwx.conf.sample

    r272 r550  
    2626#              This option should only be used with "CODE 26 VENDOR 0".
    2727#               
     28# Note that the Proxy-State (code 33) attribute is handled directly as an ECHO parameter by the gateway core.
    2829
    2930# Examples:
     
    3132# DROP code 26 vendor 9 ; # Drop any Cisco-specific attribute
    3233# ECHO code 26 vendor 0 ext 256 ; # Echo any extended attribute with the type 256.
    33 
    34 ECHO code 33 ; # RADIUS Proxy-State attribute
  • extensions/app_radgw/radius.c

    r540 r550  
    1 /*********************************************************************************/
     1/*********************************************************************************************************
     2* Software License Agreement (BSD License)                                                               *
     3* Author: Sebastien Decugis <sdecugis@nict.go.jp>                                                        *
     4*                                                                                                        *
     5* Copyright (c) 2010, WIDE Project and NICT                                                              *
     6* All rights reserved.                                                                                   *
     7*                                                                                                        *
     8* Redistribution and use of this software in source and binary forms, with or without modification, are  *
     9* permitted provided that the following conditions are met:                                              *
     10*                                                                                                        *
     11* * Redistributions of source code must retain the above                                                 *
     12*   copyright notice, this list of conditions and the                                                    *
     13*   following disclaimer.                                                                                *
     14*                                                                                                        *
     15* * Redistributions in binary form must reproduce the above                                              *
     16*   copyright notice, this list of conditions and the                                                    *
     17*   following disclaimer in the documentation and/or other                                               *
     18*   materials provided with the distribution.                                                            *
     19*                                                                                                        *
     20* * Neither the name of the WIDE Project or NICT nor the                                                 *
     21*   names of its contributors may be used to endorse or                                                  *
     22*   promote products derived from this software without                                                  *
     23*   specific prior written permission of WIDE Project and                                                *
     24*   NICT.                                                                                                *
     25*                                                                                                        *
     26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
     27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
     28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
     29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT     *
     30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS    *
     31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
     32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
     33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                                             *
     34*********************************************************************************************************/
     35
    236/* freeDiameter author note:
    3  *  The content from this file comes directly from the hostap project.
     37 *  The content from this file comes for the main part from the hostap project.
    438 * It is redistributed under the terms of the BSD license, as allowed
    539 * by the original copyright reproduced bellow.
    6  *  In addition to this notice, the following changes have been done:
    7  *   - created the radius_msg_dump_attr_val function
     40 * The modifications to this file are placed under the copyright of the freeDiameter project.
    841 */
    9 #include "rgw_common.h"
    10 
    11 /* Overwrite printf */
    12 #define printf(args...) fd_log_debug(args)
    13 
    14 /*********************************************************************************/
    15 
    1642
    1743/*
     
    2955 */
    3056
     57/*********************************************************************************/
     58#include "rgw.h"
     59
     60/* Overwrite printf */
     61#define printf(args...) fd_log_debug(args)
     62
     63
    3164static struct radius_attr_hdr *
    3265radius_get_attr_hdr(struct radius_msg *msg, int idx)
     
    104137}
    105138
    106 
    107 static const char *radius_code_string(u8 code)
    108 {
    109         switch (code) {
    110         case RADIUS_CODE_ACCESS_REQUEST: return "Access-Request";
    111         case RADIUS_CODE_ACCESS_ACCEPT: return "Access-Accept";
    112         case RADIUS_CODE_ACCESS_REJECT: return "Access-Reject";
    113         case RADIUS_CODE_ACCOUNTING_REQUEST: return "Accounting-Request";
    114         case RADIUS_CODE_ACCOUNTING_RESPONSE: return "Accounting-Response";
    115         case RADIUS_CODE_ACCESS_CHALLENGE: return "Access-Challenge";
    116         case RADIUS_CODE_STATUS_SERVER: return "Status-Server";
    117         case RADIUS_CODE_STATUS_CLIENT: return "Status-Client";
    118         case RADIUS_CODE_RESERVED: return "Reserved";
    119         default: return "?Unknown?";
    120         }
    121 }
     139/* Destroy a message */
     140void rgw_msg_free(struct rgw_radius_msg_meta ** msg)
     141{
     142        if (!msg || !*msg)
     143                return;
     144       
     145        radius_msg_free(&(*msg)->radius);
     146        free(*msg);
     147        *msg = NULL;
     148}
     149
    122150
    123151
     
    219247
    220248
    221 void radius_msg_dump_attr_val(struct radius_attr_hdr *hdr)
     249static void radius_msg_dump_attr_val(struct radius_attr_hdr *hdr)
    222250{
    223251        struct radius_attr_type *attr;
     
    282310}
    283311
    284 static void radius_msg_dump_attr(struct radius_attr_hdr *hdr)
    285 {
    286         struct radius_attr_type *attr;
    287 
    288         attr = radius_get_attr_type(hdr->type);
    289 
    290         printf("   Attribute %d (%s) length=%d\n",
    291                hdr->type, attr ? attr->name : "?Unknown?", hdr->length);
    292        
    293         radius_msg_dump_attr_val(hdr);
    294 }
    295 
    296 
    297 void radius_msg_dump(struct radius_msg *msg)
    298 {
     312/* Dump a message  -- can be used safely with a struct radius_msg as parameter (we don't dump the metadata) */
     313void rgw_msg_dump(struct rgw_radius_msg_meta * msg)
     314{
     315        unsigned char *auth;
    299316        size_t i;
    300 
    301         printf("RADIUS message: code=%d (%s) identifier=%d length=%d\n",
    302                msg->hdr->code, radius_code_string(msg->hdr->code),
    303                msg->hdr->identifier, ntohs(msg->hdr->length));
    304 
    305         for (i = 0; i < msg->attr_used; i++) {
    306                 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
    307                 radius_msg_dump_attr(attr);
    308         }
     317        if (! TRACE_BOOL(FULL) )
     318                return;
     319       
     320        auth =  &(msg->radius.hdr->authenticator[0]);
     321       
     322        fd_log_debug("------ RADIUS msg dump -------\n");
     323        fd_log_debug(" id  : 0x%02hhx, code : %hhd (%s), length : %d\n", msg->radius.hdr->identifier, msg->radius.hdr->code, rgw_msg_code_str(msg->radius.hdr->code), ntohs(msg->radius.hdr->length));
     324        fd_log_debug(" auth: %02hhx %02hhx %02hhx %02hhx  %02hhx %02hhx %02hhx %02hhx\n",
     325                        auth[0], auth[1], auth[2], auth[3], auth[4], auth[5], auth[6], auth[7]);
     326        fd_log_debug("       %02hhx %02hhx %02hhx %02hhx  %02hhx %02hhx %02hhx %02hhx\n",
     327                        auth[8],  auth[9],  auth[10], auth[11], auth[12], auth[13], auth[14], auth[15]);
     328        for (i = 0; i < msg->radius.attr_used; i++) {
     329                struct radius_attr_hdr *attr = (struct radius_attr_hdr *)(msg->radius.buf + msg->radius.attr_pos[i]);
     330                fd_log_debug("    - Type: 0x%02hhx (%s)\n       Len: %-3hhu", attr->type, rgw_msg_attrtype_str(attr->type), attr->length);
     331                radius_msg_dump_attr_val(attr);
     332        }
     333        if (msg->ps_nb) {
     334                fd_log_debug("---- hidden attributes:\n");
     335                for (i = msg->ps_first; i < msg->ps_first + msg->ps_nb; i++) {
     336                        struct radius_attr_hdr *attr = (struct radius_attr_hdr *)(msg->radius.buf + msg->radius.attr_pos[i]);
     337                        fd_log_debug("    - Type: 0x%02hhx (%s)\n       Len: %-3hhu", attr->type, rgw_msg_attrtype_str(attr->type), attr->length);
     338                        radius_msg_dump_attr_val(attr);
     339                }
     340        }
     341        fd_log_debug("-----------------------------\n");
    309342}
    310343
     
    407440
    408441
    409 static int radius_msg_add_attr_to_array(struct radius_msg *msg,
     442int radius_msg_add_attr_to_array(struct radius_msg *msg,
    410443                                        struct radius_attr_hdr *attr)
    411444{
     
    474507
    475508
    476 struct radius_msg *radius_msg_parse(const u8 *data, size_t len)
    477 {
    478         struct radius_msg *msg;
     509/* Modified version of radius_msg_parse */
     510int rgw_msg_parse(unsigned char * buf, size_t len, struct rgw_radius_msg_meta ** msg)
     511{
     512        struct rgw_radius_msg_meta * temp_msg = NULL;
    479513        struct radius_hdr *hdr;
    480514        struct radius_attr_hdr *attr;
    481515        size_t msg_len;
    482516        unsigned char *pos, *end;
    483 
    484         if (data == NULL || len < sizeof(*hdr))
    485                 return NULL;
    486 
    487         hdr = (struct radius_hdr *) data;
    488 
     517        int ret = 0;
     518       
     519        TRACE_ENTRY("%p %g %p", buf, len, msg);
     520       
     521        CHECK_PARAMS( buf && len >= sizeof(*hdr) && msg );
     522       
     523        *msg = NULL;
     524       
     525        /* Parse the RADIUS message */
     526        hdr = (struct radius_hdr *) buf;
    489527        msg_len = ntohs(hdr->length);
    490528        if (msg_len < sizeof(*hdr) || msg_len > len) {
    491                 printf("Invalid RADIUS message length\n");
    492                 return NULL;
     529                TRACE_DEBUG(INFO, "Invalid RADIUS message length\n");
     530                return EINVAL;
    493531        }
    494532
    495533        if (msg_len < len) {
    496                 printf("Ignored %lu extra bytes after RADIUS message\n",
     534                TRACE_DEBUG(INFO, "Ignored %lu extra bytes after RADIUS message\n",
    497535                       (unsigned long) len - msg_len);
    498536        }
    499537
    500         msg = os_malloc(sizeof(*msg));
    501         if (msg == NULL)
    502                 return NULL;
    503 
    504         if (radius_msg_initialize(msg, msg_len)) {
    505                 os_free(msg);
    506                 return NULL;
    507         }
    508 
    509         os_memcpy(msg->buf, data, msg_len);
    510         msg->buf_size = msg->buf_used = msg_len;
    511 
     538        CHECK_MALLOC( temp_msg = malloc(sizeof(struct rgw_radius_msg_meta)) );
     539        memset(temp_msg, 0, sizeof(struct rgw_radius_msg_meta));
     540       
     541        if (radius_msg_initialize(&temp_msg->radius, msg_len)) {
     542                TRACE_DEBUG(INFO, "Error in radius_msg_initialize, returning ENOMEM.");
     543                free(temp_msg);
     544                return ENOMEM;
     545        }
     546       
     547        /* Store the received data in the alloc'd buffer */
     548        memcpy(temp_msg->radius.buf, buf, msg_len);
     549        temp_msg->radius.buf_size = temp_msg->radius.buf_used = msg_len;
     550       
    512551        /* parse attributes */
    513         pos = (unsigned char *) (msg->hdr + 1);
    514         end = msg->buf + msg->buf_used;
     552        pos = (unsigned char *) (temp_msg->radius.hdr + 1);
     553        end = temp_msg->radius.buf + temp_msg->radius.buf_used;
     554       
    515555        while (pos < end) {
    516                 if ((size_t) (end - pos) < sizeof(*attr))
    517                         goto fail;
    518 
     556                if ((size_t) (end - pos) < sizeof(*attr)) {
     557                        TRACE_DEBUG(INFO, "Trucated attribute found in RADIUS buffer, EINVAL.");
     558                        ret = EINVAL;
     559                        break;
     560                }
     561                       
    519562                attr = (struct radius_attr_hdr *) pos;
    520 
    521                 if (pos + attr->length > end || attr->length < sizeof(*attr))
    522                         goto fail;
    523 
    524                 /* TODO: check that attr->length is suitable for attr->type */
    525 
    526                 if (radius_msg_add_attr_to_array(msg, attr))
    527                         goto fail;
     563       
     564                if (pos + attr->length > end || attr->length < sizeof(*attr)) {
     565                        TRACE_DEBUG(INFO, "Trucated attribute found in RADIUS buffer, EINVAL.");
     566                        ret = EINVAL;
     567                        break;
     568                }
     569
     570                if (radius_msg_add_attr_to_array(&temp_msg->radius, attr)) {
     571                        TRACE_DEBUG(INFO, "Error in radius_msg_add_attr_to_array, ENOMEM");
     572                        ret = ENOMEM;
     573                        break;
     574                }
     575               
     576                if (attr->type == RADIUS_ATTR_PROXY_STATE)
     577                        temp_msg->ps_nb += 1;
    528578
    529579                pos += attr->length;
    530580        }
    531 
    532         return msg;
    533 
    534  fail:
    535         radius_msg_free(msg);
    536         os_free(msg);
    537         return NULL;
    538 }
     581       
     582        if (ret != 0) {
     583                radius_msg_free(&temp_msg->radius);
     584                free(temp_msg);
     585                return ret;
     586        }
     587       
     588        /* Now move all the proxy-state attributes at the end of the attr_pos array */
     589        if (temp_msg->ps_nb) {
     590                size_t *temp_ps = NULL;
     591                int n, new_n = 0, p = 0;
     592               
     593                CHECK_MALLOC( temp_ps = calloc(temp_msg->ps_nb, sizeof(size_t *)) );
     594               
     595                /* Move all the Proxy-State attributes into the temp_ps array */
     596                for (n=0; n < temp_msg->radius.attr_used; n++) {
     597                        struct radius_attr_hdr * attr = (struct radius_attr_hdr *)(temp_msg->radius.buf + temp_msg->radius.attr_pos[n]);
     598                       
     599                        if (attr->type == RADIUS_ATTR_PROXY_STATE) {
     600                                temp_ps[p++] = temp_msg->radius.attr_pos[n];
     601                        } else {
     602                                temp_msg->radius.attr_pos[new_n++] = temp_msg->radius.attr_pos[n];
     603                        }
     604                }
     605                temp_msg->radius.attr_used = new_n; /* hide the proxy-state to other modules */
     606                temp_msg->ps_first = new_n;
     607               
     608                /* And back into the array */
     609                memcpy(temp_msg->radius.attr_pos + new_n, temp_ps, p * sizeof(size_t *));
     610                free(temp_ps);
     611        }
     612       
     613        *msg = temp_msg;
     614        return 0;
     615}
     616
    539617
    540618
  • extensions/app_radgw/radius.h

    r516 r550  
     1/*********************************************************************************************************
     2* Software License Agreement (BSD License)                                                               *
     3* Author: Sebastien Decugis <sdecugis@nict.go.jp>                                                        *
     4*                                                                                                        *
     5* Copyright (c) 2010, WIDE Project and NICT                                                              *
     6* All rights reserved.                                                                                   *
     7*                                                                                                        *
     8* Redistribution and use of this software in source and binary forms, with or without modification, are  *
     9* permitted provided that the following conditions are met:                                              *
     10*                                                                                                        *
     11* * Redistributions of source code must retain the above                                                 *
     12*   copyright notice, this list of conditions and the                                                    *
     13*   following disclaimer.                                                                                *
     14*                                                                                                        *
     15* * Redistributions in binary form must reproduce the above                                              *
     16*   copyright notice, this list of conditions and the                                                    *
     17*   following disclaimer in the documentation and/or other                                               *
     18*   materials provided with the distribution.                                                            *
     19*                                                                                                        *
     20* * Neither the name of the WIDE Project or NICT nor the                                                 *
     21*   names of its contributors may be used to endorse or                                                  *
     22*   promote products derived from this software without                                                  *
     23*   specific prior written permission of WIDE Project and                                                *
     24*   NICT.                                                                                                *
     25*                                                                                                        *
     26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
     27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
     28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
     29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT     *
     30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS    *
     31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
     32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
     33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                                             *
     34*********************************************************************************************************/
     35
    136/*********************************************************************************/
    237/* freeDiameter author note:
    3  *  The content from this file comes directly from the hostap project.
     38 *  The content from this file comes mostly from the hostap project.
    439 * It is redistributed under the terms of the BSD license, as allowed
    540 * by the original copyright reproduced bellow.
    6  *  The file has not been modified, except for this notice and
    7  * declaration of:
    8  *  void radius_msg_dump_attr_val(struct radius_attr_hdr *hdr);
     41 * The changes to this file are placed under the copyright of the freeDiameter project.
    942 */
    10 
    11 /*********************************************************************************/
    1243
    1344/*
     
    2455 * See README and COPYING for more details.
    2556 */
     57/*********************************************************************************/
     58
    2659
    2760#ifndef RADIUS_H
     
    219252void radius_msg_set_hdr(struct radius_msg *msg, u8 code, u8 identifier);
    220253void radius_msg_free(struct radius_msg *msg);
    221 void radius_msg_dump_attr_val(struct radius_attr_hdr *hdr);
    222 void radius_msg_dump(struct radius_msg *msg);
    223254int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
    224255                      size_t secret_len);
     
    229260struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type,
    230261                                            const u8 *data, size_t data_len);
    231 struct radius_msg *radius_msg_parse(const u8 *data, size_t len);
    232262int radius_msg_add_eap(struct radius_msg *msg, const u8 *data,
    233263                       size_t data_len);
     
    267297}
    268298
     299int radius_msg_add_attr_to_array(struct radius_msg *msg, struct radius_attr_hdr *attr);
    269300static inline int radius_msg_get_attr_int32(struct radius_msg *msg, u8 type,
    270301                                            u32 *value)
  • extensions/app_radgw/rgw.h

    r548 r550  
    6060        };
    6161       
     62        /* For Proxy-State attributes: */
     63        int     ps_first;       /* The index of the first Proxy-State attribute in radius.attr_pos. It is always >= radius.attr_used */
     64        int     ps_nb;          /* The number of Proxy-State attributes. The real radius.attr_pos size is attr_used + ps_nb */
    6265};
    6366void rgw_msg_free(struct rgw_radius_msg_meta ** msg);
  • extensions/app_radgw/rgw_clients.c

    r548 r550  
    975975        }
    976976       
     977        /* Add all the Proxy-States back in the message */
     978        for (p = 0; p < req->ps_nb; p++) {
     979                struct radius_attr_hdr * attr = (struct radius_attr_hdr *)(req->radius.buf + req->radius.attr_pos[req->ps_first + p]);
     980               
     981                if (radius_msg_add_attr_to_array(*msg, attr)) {
     982                        TRACE_DEBUG(INFO, "Error in radius_msg_add_attr_to_array, ENOMEM");
     983                        radius_msg_free(*msg);
     984                        free(*msg);
     985                        *msg = NULL;
     986                        return ENOMEM;
     987                }
     988        }
     989       
     990        /* Add the Message-Authenticator if needed, and other final tasks */
    977991        if (radius_msg_finish_srv(*msg, cli->key.data, cli->key.len, req->radius.hdr->authenticator)) {
    978992                TRACE_DEBUG(INFO, "An error occurred while preparing the RADIUS answer");
  • extensions/app_radgw/rgw_msg.c

    r518 r550  
    1 /*********************************************************************************************************
    2 * Software License Agreement (BSD License)                                                               *
    3 * Author: Sebastien Decugis <sdecugis@nict.go.jp>                                                        *
    4 *                                                                                                        *
    5 * Copyright (c) 2010, WIDE Project and NICT                                                              *
    6 * All rights reserved.                                                                                   *
    7 *                                                                                                        *
    8 * Redistribution and use of this software in source and binary forms, with or without modification, are  *
    9 * permitted provided that the following conditions are met:                                              *
    10 *                                                                                                        *
    11 * * Redistributions of source code must retain the above                                                 *
    12 *   copyright notice, this list of conditions and the                                                    *
    13 *   following disclaimer.                                                                                *
    14 *                                                                                                        *
    15 * * Redistributions in binary form must reproduce the above                                              *
    16 *   copyright notice, this list of conditions and the                                                    *
    17 *   following disclaimer in the documentation and/or other                                               *
    18 *   materials provided with the distribution.                                                            *
    19 *                                                                                                        *
    20 * * Neither the name of the WIDE Project or NICT nor the                                                 *
    21 *   names of its contributors may be used to endorse or                                                  *
    22 *   promote products derived from this software without                                                  *
    23 *   specific prior written permission of WIDE Project and                                                *
    24 *   NICT.                                                                                                *
    25 *                                                                                                        *
    26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
    27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
    28 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
    29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT     *
    30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS    *
    31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
    32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
    33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                                             *
    34 *********************************************************************************************************/
    35 
    36 /* This file contains all support functions to parse, create, and manipulate RADIUS messages. Other
    37 modules do not need to "know" the actual representation of RADIUS messages on the network. They only
    38 receive the logical view as exposed in the rgw.h file. This file extends the content of the radius.c
    39 file functions (from hostap project).*/
    40 
    41 #include "rgw.h"
    42 
    43 /* Destroy a message */
    44 void rgw_msg_free(struct rgw_radius_msg_meta ** msg)
    45 {
    46         if (!msg || !*msg)
    47                 return;
    48        
    49         radius_msg_free(&(*msg)->radius);
    50         free(*msg);
    51         *msg = NULL;
    52 }
    53 
    54 /* This function creates a rgw_radius_msg_meta structure after parsing a RADIUS buffer */
    55 int rgw_msg_parse(unsigned char * buf, size_t len, struct rgw_radius_msg_meta ** msg)
    56 {
    57         struct radius_msg * temp_msg = NULL;
    58        
    59         TRACE_ENTRY("%p %g %p", buf, len, msg);
    60        
    61         CHECK_PARAMS( buf && len && msg );
    62        
    63         *msg = NULL;
    64        
    65         /* Parse the RADIUS message */
    66         temp_msg = radius_msg_parse(buf, len);
    67         if (temp_msg == NULL) {
    68                 TRACE_DEBUG(INFO, "Error parsing the RADIUS message, discarding");
    69                 return EINVAL;
    70         }
    71        
    72         /* Now alloc space for the meta-data */
    73         CHECK_MALLOC( *msg = realloc(temp_msg, sizeof(struct rgw_radius_msg_meta)) );
    74        
    75         /* Clear memory after the parsed data */
    76         memset( &(*msg)->radius + 1, 0, sizeof(struct rgw_radius_msg_meta) - sizeof(struct radius_msg) );
    77        
    78         return 0;
    79 }
    80 
    81 /* Dump a message (inspired from radius_msg_dump) -- can be used safely with a struct radius_msg as parameter (we don't dump the metadata) */
    82 void rgw_msg_dump(struct rgw_radius_msg_meta * msg)
    83 {
    84         unsigned char *auth;
    85         size_t i;
    86         if (! TRACE_BOOL(FULL) )
    87                 return;
    88        
    89         auth =  &(msg->radius.hdr->authenticator[0]);
    90        
    91         fd_log_debug("------ RADIUS msg dump -------\n");
    92         fd_log_debug(" id  : 0x%02hhx, code : %hhd (%s)\n", msg->radius.hdr->identifier, msg->radius.hdr->code, rgw_msg_code_str(msg->radius.hdr->code));
    93         fd_log_debug(" auth: %02hhx %02hhx %02hhx %02hhx  %02hhx %02hhx %02hhx %02hhx\n",
    94                         auth[0], auth[1], auth[2], auth[3],
    95                         auth[4], auth[5], auth[6], auth[7]);
    96         fd_log_debug("       %02hhx %02hhx %02hhx %02hhx  %02hhx %02hhx %02hhx %02hhx\n",
    97                         auth[8],  auth[9],  auth[10], auth[11],
    98                         auth[12], auth[13], auth[14], auth[15]);
    99         for (i = 0; i < msg->radius.attr_used; i++) {
    100                 struct radius_attr_hdr *attr = (struct radius_attr_hdr *)(msg->radius.buf + msg->radius.attr_pos[i]);
    101                 fd_log_debug("    - Type: 0x%02hhx (%s)\n       Len: %-3hhu", attr->type, rgw_msg_attrtype_str(attr->type), attr->length);
    102                 radius_msg_dump_attr_val(attr);
    103         }
    104         fd_log_debug("-----------------------------\n");
    105 }
    106 
Note: See TracChangeset for help on using the changeset viewer.