]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Auto-calculate the CHAP-Password
authorAlan T. DeKok <aland@freeradius.org>
Tue, 17 Apr 2012 16:07:07 +0000 (18:07 +0200)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 17 Apr 2012 16:07:47 +0000 (18:07 +0200)
As before, *unless* it's 17 hex digits.  In that case, leave it alone.

src/main/radclient.c

index edbb2decd2a69ee7f305189d31f716569555a3cb..2359de7b85146920e54cb47d1a6ef5218418f681 100644 (file)
@@ -605,20 +605,37 @@ static int send_one_packet(radclient_t *radclient)
                                vp->length = strlen(vp->vp_strvalue);
 
                        } else if ((vp = pairfind(radclient->request->vps, PW_CHAP_PASSWORD, 0)) != NULL) {
-                         /*
-                          *    FIXME: AND there's no CHAP-Challenge,
-                          *           AND vp->length != 17
-                          *           AND rad_chap_encode() != vp->vp_octets
-                          */
-                               strlcpy(vp->vp_strvalue, radclient->password,
-                                       sizeof(vp->vp_strvalue));
-                               vp->length = strlen(vp->vp_strvalue);
+                               int already_hex = 0;
 
-                               rad_chap_encode(radclient->request,
-                                               vp->vp_octets,
-                                               radclient->request->id, vp);
-                               vp->length = 17;
+                               /*
+                                *      If it's 17 octets, it *might* be already encoded.
+                                *      Or, it might just be a 17-character password (maybe UTF-8)
+                                *      Check it for non-printable characters.  The odds of ALL
+                                *      of the characters being 32..255 is (1-7/8)^17, or (1/8)^17,
+                                *      or 1/(2^51), which is pretty much zero.
+                                */
+                               if (vp->length == 17) {
+                                       for (i = 0; i < 17; i++) {
+                                               if (vp->vp_octets[i] < 32) {
+                                                       already_hex = 1;
+                                                       break;
+                                               }
+                                       }
+                               }
 
+                               /*
+                                *      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);
+                                       
+                                       rad_chap_encode(radclient->request,
+                                                       vp->vp_octets,
+                                                       fr_rand() & 0xff, vp);
+                                       vp->length = 17;
+                               }
                        } else if (pairfind(radclient->request->vps, PW_MSCHAP_PASSWORD, 0) != NULL) {
                                mschapv1_encode(&radclient->request->vps,
                                                radclient->password);