]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
basic_ncsa_auth: reduce use of std::string::c_str()
authorAmos Jeffries <squid3@treenet.co.nz>
Wed, 6 Jan 2016 03:15:12 +0000 (16:15 +1300)
committerAmos Jeffries <squid3@treenet.co.nz>
Wed, 6 Jan 2016 03:15:12 +0000 (16:15 +1300)
This should also reduce issues with c_str() escaping and variable scope
"use after free" identified by Coverity Scan issue #1347000

helpers/basic_auth/NCSA/basic_ncsa_auth.cc

index 224fe200fed2b84bd1a957021f1165898f827517..df221c35dcf7c2af3f34e9ee463ad861b478161b 100644 (file)
@@ -116,30 +116,31 @@ main(int argc, char **argv)
             continue;
         }
         std::string stored_pass = userpassIterator->second;
+        const char *salted = stored_pass.c_str(); // locally stored version contains salt etc.
 
         char *crypted = NULL;
 #if HAVE_CRYPT
         size_t passwordLength = strlen(passwd);
         // Bug 3831: given algorithms more secure than DES crypt() does not truncate, so we can ignore the bug 3107 length checks below
         // '$1$' = MD5, '$2a$' = Blowfish, '$5$' = SHA256 (Linux), '$6$' = SHA256 (BSD) and SHA512
-        if (passwordLength > 1 && stored_pass[0] == '$' &&
-                (crypted = crypt(passwd, stored_pass.c_str())) && stored_pass == crypted) {
+        if (passwordLength > 1 && salted[0] == '$' &&
+                (crypted = crypt(passwd, salted)) && stored_pass == crypted) {
             SEND_OK("");
             continue;
         }
         // 'other' prefixes indicate DES algorithm.
-        if (passwordLength <= 8 && (crypted = crypt(passwd, stored_pass.c_str())) && stored_pass == crypted) {
+        if (passwordLength <= 8 && (crypted = crypt(passwd, salted)) && stored_pass == crypted) {
             SEND_OK("");
             continue;
         }
-        if (passwordLength > 8 && (crypted = crypt(passwd, stored_pass.c_str())) && stored_pass == crypted) {
+        if (passwordLength > 8 && (crypted = crypt(passwd, salted)) && stored_pass == crypted) {
             // Bug 3107: crypt() DES functionality silently truncates long passwords.
             SEND_ERR("Password too long. Only 8 characters accepted.");
             continue;
         }
 
 #endif
-        if ( (crypted = crypt_md5(passwd, stored_pass.c_str())) && stored_pass == crypted) {
+        if ( (crypted = crypt_md5(passwd, salted)) && stored_pass == crypted) {
             SEND_OK("");
             continue;
         }