Delete trailing whitespace.
[freeradius.git] / src / modules / rlm_attr_rewrite / rlm_attr_rewrite.c
index ca06e50..eb55588 100644 (file)
  *
  *   You should have received a copy of the GNU General Public License
  *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  *
- * Copyright 2002  The FreeRADIUS server project
+ * Copyright 2002,2006  The FreeRADIUS server project
  * Copyright 2002  Kostas Kalevras <kkalev@noc.ntua.gr>
  */
 
-#include "autoconf.h"
+#include <freeradius-devel/ident.h>
+RCSID("$Id$")
+
+#include <freeradius-devel/radiusd.h>
+#include <freeradius-devel/modules.h>
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 #ifdef HAVE_REGEX_H
 #      include <regex.h>
 #endif
 
-#include "radiusd.h"
-#include "modules.h"
-#include "conffile.h"
-
 #define RLM_REGEX_INPACKET 0
 #define RLM_REGEX_INCONFIG 1
 #define RLM_REGEX_INREPLY  2
 #define RLM_REGEX_INPROXY 3
 #define RLM_REGEX_INPROXYREPLY 4
 
-static const char rcsid[] = "$Id$";
-
 typedef struct rlm_attr_rewrite_t {
        char *attribute;        /* The attribute to search for */
        int  attr_num;          /* The attribute number */
@@ -58,7 +53,6 @@ typedef struct rlm_attr_rewrite_t {
        char *name;             /* The module name */
 } rlm_attr_rewrite_t;
 
-
 static const CONF_PARSER module_config[] = {
   { "attribute", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,attribute), NULL, NULL },
   { "searchfor", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,search), NULL, NULL },
@@ -71,12 +65,11 @@ static const CONF_PARSER module_config[] = {
   { NULL, -1, 0, NULL, NULL }
 };
 
-
 static int attr_rewrite_instantiate(CONF_SECTION *conf, void **instance)
 {
        rlm_attr_rewrite_t *data;
        DICT_ATTR *dattr;
-       char *instance_name = NULL;
+       const char *instance_name = NULL;
 
        /*
         *      Set up a storage area for instance data
@@ -182,7 +175,7 @@ static int do_attr_rewrite(void *instance, REQUEST *request)
        int replace_len = 0;
 
        if ((attr_vp = pairfind(request->config_items, PW_REWRITE_RULE)) != NULL){
-               if (data->name == NULL || strcmp(data->name,attr_vp->strvalue))
+               if (data->name == NULL || strcmp(data->name,attr_vp->vp_strvalue))
                        return RLM_MODULE_NOOP;
        }
 
@@ -237,7 +230,7 @@ static int do_attr_rewrite(void *instance, REQUEST *request)
                        case RLM_REGEX_INPACKET:
                                if (data->attr_num == PW_USER_NAME)
                                        attr_vp = request->username;
-                               else if (data->attr_num == PW_PASSWORD)
+                               else if (data->attr_num == PW_USER_PASSWORD)
                                        attr_vp = request->password;
                                else
                                        tmp = request->packet->vps;
@@ -271,7 +264,7 @@ do_again:
                        DEBUG2("rlm_attr_rewrite: Could not find value pair for attribute %s",data->attribute);
                        return ret;
                }
-               if (attr_vp->strvalue == NULL || attr_vp->length == 0){
+               if (attr_vp->vp_strvalue == NULL || attr_vp->length == 0){
                        DEBUG2("rlm_attr_rewrite: Attribute %s string value NULL or of zero length",data->attribute);
                        return ret;
                }
@@ -289,8 +282,16 @@ do_again:
                        DEBUG2("rlm_attr_rewrite: regcomp() returned error: %s",err_msg);
                        return ret;
                }
+
+               if ((attr_vp->type == PW_TYPE_IPADDR) &&
+                   (attr_vp->vp_strvalue[0] == '\0')) {
+                       inet_ntop(AF_INET, &(attr_vp->vp_ipaddr),
+                                 attr_vp->vp_strvalue,
+                                 sizeof(attr_vp->vp_strvalue));
+               }
+
                ptr = new_str;
-               ptr2 = attr_vp->strvalue;
+               ptr2 = attr_vp->vp_strvalue;
                counter = 0;
 
                for ( i = 0 ;i < (unsigned)data->num_matches; i++) {
@@ -298,7 +299,7 @@ do_again:
                        if (err == REG_NOMATCH) {
                                if (i == 0) {
                                        DEBUG2("rlm_attr_rewrite: No match found for attribute %s with value '%s'",
-                                                       data->attribute, attr_vp->strvalue);
+                                                       data->attribute, attr_vp->vp_strvalue);
                                        regfree(&preg);
                                        goto to_do_again;
                                } else
@@ -307,7 +308,7 @@ do_again:
                        if (err != 0) {
                                regfree(&preg);
                                radlog(L_ERR, "rlm_attr_rewrite: match failure for attribute %s with value '%s'",
-                                               data->attribute, attr_vp->strvalue);
+                                               data->attribute, attr_vp->vp_strvalue);
                                return ret;
                        }
                        if (pmatch[0].rm_so == -1)
@@ -320,12 +321,13 @@ do_again:
                        if (counter >= MAX_STRING_LEN) {
                                regfree(&preg);
                                DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
-                                               data->attribute, attr_vp->strvalue);
+                                               data->attribute, attr_vp->vp_strvalue);
                                return ret;
                        }
 
-                       strncpy(ptr, ptr2,len);
+                       memcpy(ptr, ptr2,len);
                        ptr += len;
+                       *ptr = '\0';
                        ptr2 += pmatch[0].rm_eo;
 
                        if (i == 0){
@@ -334,10 +336,10 @@ do_again:
                                 */
                                for(j = 0; j <= REQUEST_MAX_REGEX; j++){
                                        char *p;
-                                       char buffer[sizeof(attr_vp->strvalue)];
+                                       char buffer[sizeof(attr_vp->vp_strvalue)];
 
                                        /*
-                                        * Stolen from src/main/valuepair.c, paircmp()
+                                        * Stolen from src/main/valuepair.c, paircompare()
                                         */
 
                                        /*
@@ -353,7 +355,7 @@ do_again:
                                                break;
                                        }
                                        memcpy(buffer,
-                                              attr_vp->strvalue + pmatch[j].rm_so,
+                                              attr_vp->vp_strvalue + pmatch[j].rm_so,
                                               pmatch[j].rm_eo - pmatch[j].rm_so);
                                        buffer[pmatch[j].rm_eo - pmatch[j].rm_so] = '\0';
                                        p = strdup(buffer);
@@ -375,12 +377,13 @@ do_again:
                        if (counter >= MAX_STRING_LEN) {
                                regfree(&preg);
                                DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
-                                               data->attribute, attr_vp->strvalue);
+                                               data->attribute, attr_vp->vp_strvalue);
                                return ret;
                        }
                        if (replace_len){
-                               strncpy(ptr, replace_STR, replace_len);
+                               memcpy(ptr, replace_STR, replace_len);
                                ptr += replace_len;
+                               *ptr = '\0';
                        }
                }
                regfree(&preg);
@@ -388,13 +391,14 @@ do_again:
                counter += len;
                if (counter >= MAX_STRING_LEN){
                        DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
-                                       data->attribute, attr_vp->strvalue);
+                                       data->attribute, attr_vp->vp_strvalue);
                        return ret;
                }
-               strncpy(ptr, ptr2, len);
+               memcpy(ptr, ptr2, len);
+               ptr[len] = '\0';
 
                DEBUG2("rlm_attr_rewrite: Changed value for attribute %s from '%s' to '%s'",
-                               data->attribute, attr_vp->strvalue, new_str);
+                               data->attribute, attr_vp->vp_strvalue, new_str);
                if (pairparsevalue(attr_vp, new_str) == NULL) {
                        DEBUG2("rlm_attr_rewrite: Could not write value '%s' into attribute %s: %s", new_str, data->attribute, librad_errstr);
                        return ret;
@@ -413,7 +417,6 @@ to_do_again:
        return ret;
 }
 
-
 static int attr_rewrite_accounting(void *instance, REQUEST *request)
 {
        return do_attr_rewrite(instance, request);
@@ -423,15 +426,18 @@ static int attr_rewrite_authorize(void *instance, REQUEST *request)
 {
        return do_attr_rewrite(instance, request);
 }
+
 static int attr_rewrite_authenticate(void *instance, REQUEST *request)
 {
        return do_attr_rewrite(instance, request);
 }
+
 static int attr_rewrite_preacct(void *instance, REQUEST *request)
 {
        return do_attr_rewrite(instance, request);
 }
-static int attr_rewrite_ismul(void *instance, REQUEST *request)
+
+static int attr_rewrite_checksimul(void *instance, REQUEST *request)
 {
        return do_attr_rewrite(instance, request);
 }
@@ -455,12 +461,6 @@ static int attr_rewrite_detach(void *instance)
 {
        rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
 
-       if (data->attribute)
-               free(data->attribute);
-       if (data->search)
-               free(data->search);
-       if (data->replace)
-               free(data->replace);
        if (data->name)
                free(data->name);
 
@@ -478,20 +478,19 @@ static int attr_rewrite_detach(void *instance)
  *     is single-threaded.
  */
 module_t rlm_attr_rewrite = {
+       RLM_MODULE_INIT,
        "attr_rewrite",
        RLM_TYPE_THREAD_UNSAFE,         /* type */
-       NULL,                           /* initialization */
        attr_rewrite_instantiate,               /* instantiation */
+       attr_rewrite_detach,                    /* detach */
        {
                attr_rewrite_authenticate,      /* authentication */
                attr_rewrite_authorize,         /* authorization */
                attr_rewrite_preacct,           /* preaccounting */
                attr_rewrite_accounting,        /* accounting */
-               attr_rewrite_ismul,             /* checksimul */
+               attr_rewrite_checksimul,        /* checksimul */
                attr_rewrite_preproxy,          /* pre-proxy */
                attr_rewrite_postproxy,         /* post-proxy */
                attr_rewrite_postauth           /* post-auth */
        },
-       attr_rewrite_detach,                    /* detach */
-       NULL,                           /* destroy */
 };