Added pairstrcpy()
authorAlan T. DeKok <aland@freeradius.org>
Tue, 30 Apr 2013 21:56:05 +0000 (17:56 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 30 Apr 2013 22:47:31 +0000 (18:47 -0400)
which copies a 'char const *' to a VALUE_PAIR

12 files changed:
src/include/libradius.h
src/lib/valuepair.c
src/main/radclient.c
src/main/session.c
src/modules/rlm_eap/radeapclient.c
src/modules/rlm_eap/rlm_eap.c
src/modules/rlm_expr/paircmp.c
src/modules/rlm_ldap/rlm_ldap.c
src/modules/rlm_preprocess/rlm_preprocess.c
src/modules/rlm_realm/rlm_realm.c
src/modules/rlm_sql/rlm_sql.c
src/modules/rlm_yubikey/rlm_yubikey.c

index f4529b7..5a87b4d 100644 (file)
@@ -429,6 +429,7 @@ VALUE_PAIR  *paircopyvpdata(TALLOC_CTX *ctx, DICT_ATTR const *da, VALUE_PAIR cons
 VALUE_PAIR     *paircopy(TALLOC_CTX *ctx, VALUE_PAIR *vp);
 VALUE_PAIR     *paircopy2(TALLOC_CTX *ctx, VALUE_PAIR *vp, unsigned int attr, unsigned int vendor, int8_t tag);
 void           pairmemcpy(VALUE_PAIR *vp, uint8_t const * src, size_t len);
+void           pairstrcpy(VALUE_PAIR *vp, char const * src);
 void           pairmove(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from);
 void           pairfilter(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
                          unsigned int attr, unsigned int vendor, int8_t tag);
index 60f5595..b047b3b 100644 (file)
@@ -997,12 +997,13 @@ int pairparsevalue(VALUE_PAIR *vp, char const *value)
        VERIFY(vp);
 
        /*
-        *      Even for integers, dates and ip addresses we
-        *      keep the original string in vp->vp_strvalue.
+        *      Even for integers, dates and ip addresses we
+        *      keep the original string in vp->vp_strvalue.
+        *
+        *      @todo: too many things depend on this!
         */
        if (vp->da->type != PW_TYPE_TLV) {
-               strlcpy(vp->vp_strvalue, value, sizeof(vp->vp_strvalue));
-               vp->length = strlen(vp->vp_strvalue);
+               pairstrcpy(vp, value);
        }
 
        switch(vp->da->type) {
@@ -2275,3 +2276,21 @@ void pairmemcpy(VALUE_PAIR *vp, uint8_t const *src, size_t size)
        memcpy(vp->vp_octets, src, size);
        vp->length = size;
 }
+
+
+/** Copy data into an "string" data type.
+ *
+ * @param[in,out] vp to update
+ * @param[in] src data to copy
+ * @param[in] size of the data
+ */
+void pairstrcpy(VALUE_PAIR *vp, char const *src)
+{
+       size_t size = strlen(src);
+
+       if (size >= sizeof(vp->vp_strvalue)) size = sizeof(vp->vp_strvalue) - 1;
+
+       memcpy(vp->vp_strvalue, src, size);
+       vp->vp_strvalue[size] = '\0';
+       vp->length = size;
+}
index 1d70ced..65d685d 100644 (file)
@@ -619,9 +619,7 @@ static int send_one_packet(radclient_t *radclient)
                        VALUE_PAIR *vp;
 
                        if ((vp = pairfind(radclient->request->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
-                               strlcpy(vp->vp_strvalue, radclient->password,
-                                       sizeof(vp->vp_strvalue));
-                               vp->length = strlen(vp->vp_strvalue);
+                               pairstrcpy(vp, radclient->password);
 
                        } else if ((vp = pairfind(radclient->request->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
                                int already_hex = 0;
@@ -646,9 +644,7 @@ static int send_one_packet(radclient_t *radclient)
                                 *      Allow the user to specify ASCII or hex CHAP-Password
                                 */
                                if (!already_hex) {
-                                       strlcpy(vp->vp_strvalue, radclient->password,
-                                               sizeof(vp->vp_strvalue));
-                                       vp->length = strlen(vp->vp_strvalue);
+                                       pairstrcpy(vp, radclient->password);
                                        
                                        rad_chap_encode(radclient->request,
                                                        vp->vp_octets,
index f0d5e6b..2f5fca6 100644 (file)
@@ -68,8 +68,7 @@ int session_zap(REQUEST *request, uint32_t nasaddr, unsigned int port,
                pairfree(&(stopreq->packet->vps)); \
                return 0; \
        } \
-       strlcpy((char *)vp->vp_strvalue, v, sizeof vp->vp_strvalue); \
-       vp->length = strlen(v); \
+       pairstrcpy(vp, v);      \
        pairadd(&(stopreq->packet->vps), vp); \
        } while(0)
 
index c8fd438..145f9d4 100644 (file)
@@ -903,16 +903,13 @@ static int sendrecv_eap(RADIUS_PACKET *rep)
 
        if (*password != '\0') {
                if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) != NULL) {
-                       strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
-                       vp->length = strlen(password);
+                       pairstrcpy(vp, password);
 
                } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
-                       strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
-                       vp->length = strlen(password);
+                       pairstrcpy(vp, password);
 
                } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
-                       strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
-                       vp->length = strlen(password);
+                       pairstrcpy(vp, password);
 
                        rad_chap_encode(rep, vp->vp_octets, rep->id, vp);
                        vp->length = 17;
index a8dbbcc..bb0c155 100644 (file)
@@ -443,11 +443,8 @@ static rlm_rcode_t mod_authenticate(void *instance, REQUEST *request)
                 */
                vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
                if (!vp) {
-                       vp = pairmake_reply("User-Name", "",
-                                     T_OP_EQ);
-                       strlcpy(vp->vp_strvalue, request->username->vp_strvalue,
-                               sizeof(vp->vp_strvalue));
-                       vp->length = request->username->length;
+                       vp = paircopyvp(request->reply, request->username);
+                       pairadd(&request->reply->vps, vp);
                }
 
                /*
index 37c2a20..2bc1d53 100644 (file)
@@ -162,8 +162,7 @@ static int presufcmp(UNUSED void *instance,
                req->username = vp;
        }
 
-       strlcpy((char *)vp->vp_strvalue, rest, sizeof(vp->vp_strvalue));
-       vp->length = strlen(vp->vp_strvalue);
+       pairstrcpy(vp, rest);
 
        return ret;
 }
index 8a42776..856f28a 100644 (file)
@@ -834,7 +834,7 @@ static rlm_rcode_t mod_authorize(void *instance, REQUEST *request)
                 *      Add Cleartext-Password attribute to the request
                 */
                vp = radius_paircreate(request, &request->config_items, PW_CLEARTEXT_PASSWORD, 0);
-               strlcpy(vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
+               pairstrcpy(vp, password);
                vp->length = pass_size;
                
                RDEBUG2("Added eDirectory password in check items as %s = %s", vp->da->name, vp->vp_strvalue);
index 814e7bb..805c4b7 100644 (file)
@@ -292,8 +292,7 @@ static void rad_mangle(rlm_preprocess_t *inst, REQUEST *request)
                if ((ptr = strchr(namepair->vp_strvalue, '\\')) != NULL) {
                        strlcpy(newname, ptr + 1, sizeof(newname));
                        /* Same size */
-                       strcpy(namepair->vp_strvalue, newname);
-                       namepair->length = strlen(newname);
+                       pairstrcpy(namepair, newname);
                }
        }
 
@@ -479,8 +478,7 @@ static int huntgroup_access(REQUEST *request, PAIR_LIST *huntgroups)
                        vp = pairfind(request_pairs, PW_HUNTGROUP_NAME, 0, TAG_ANY);
                        if (!vp) {
                                vp = radius_paircreate(request, &request->packet->vps, PW_HUNTGROUP_NAME, 0);
-                               strlcpy(vp->vp_strvalue, i->name, sizeof(vp->vp_strvalue));
-                               vp->length = strlen(vp->vp_strvalue);
+                               pairstrcpy(vp, i->name);
                        }
                        r = RLM_MODULE_OK;
                }
index 4c212dc..865dbd9 100644 (file)
@@ -189,8 +189,7 @@ static int check_for_realm(void *instance, REQUEST *request, REALM **returnrealm
                        RDEBUG2("Setting Stripped-User-Name = \"%s\"", username);
                }
 
-               strlcpy(vp->vp_strvalue, username, sizeof(vp->vp_strvalue));
-               vp->length = strlen((char *)vp->vp_strvalue);
+               pairstrcpy(vp, username);
                request->username = vp;
        }
 
index 9ee23aa..de66e9a 100644 (file)
@@ -464,12 +464,11 @@ int sql_set_user(rlm_sql_t *inst, REQUEST *request, char const *username)
        vp = pairalloc(request->packet, inst->sql_user);
        vp->op = T_OP_SET;
        
-       strlcpy(vp->vp_strvalue, expanded, sizeof(vp->vp_strvalue));
-       talloc_free(expanded);
-       
-       vp->length = strlen(vp->vp_strvalue);
+       pairstrcpy(vp, expanded); /* FIXME: pairsteal */
        pairadd(&request->packet->vps, vp);
 
+       talloc_free(expanded);
+
        RDEBUG2("SQL-User-Name updated");
 
        return 0;
index 2b34049..a6b29f3 100644 (file)
@@ -252,6 +252,9 @@ static rlm_rcode_t mod_authorize(void *instance, REQUEST *request)
         *      needs to verify it's associated with the user.
         *
         *      It's left up to the user if they want to decode it or not.
+        *
+        *      @todo: is this even right?  Is there anything in passcode other than
+        *      the public ID?  Why pairmake(...NULL) instead of pairmake(..., passcode) ?
         */
        if (inst->id_len) {
                vp = pairmake(request, &request->packet->vps, "Yubikey-Public-ID", NULL, T_OP_SET);
@@ -261,9 +264,7 @@ static rlm_rcode_t mod_authorize(void *instance, REQUEST *request)
                        return RLM_MODULE_FAIL;
                }
                
-               strlcpy(vp->vp_strvalue, passcode, inst->id_len + 1);
-               
-               vp->length = inst->id_len;
+               pairstrcpy(vp, passcode);
        }
 
        return RLM_MODULE_OK;