]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
util: add a crypt strerror helper
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Wed, 11 Dec 2024 01:30:04 +0000 (14:30 +1300)
committerAndreas Schneider <asn@cryptomilk.org>
Fri, 20 Dec 2024 07:04:31 +0000 (07:04 +0000)
This will be used by Python also.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15756

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andreas Schneider <asn@samba.org>
lib/util/util_crypt.c
lib/util/util_crypt.h
source4/dsdb/samdb/ldb_modules/password_hash.c

index 0f7b2d0fd31a634b2133bfc28b062b35a039e056..09cd47597d1b8da25c24711a58dd7db1eb157619 100644 (file)
@@ -88,3 +88,27 @@ int talloc_crypt_blob(TALLOC_CTX *mem_ctx,
        }
        return 0;
 }
+
+
+char *talloc_crypt_errstring(TALLOC_CTX *mem_ctx, int error)
+{
+       char buf[1024];
+       int err;
+       if (error == ERANGE) {
+               return talloc_strdup(
+                       mem_ctx,
+                       "Password exceeds maximum length allowed for crypt() hashing");
+       }
+       if (error == ENOTRECOVERABLE) {
+               /* probably weird RHEL7 crypt, see crypt_as_best_we_can() */
+               goto unknown;
+       }
+
+       err = strerror_r(error, buf, sizeof(buf));
+       if (err != 0) {
+               goto unknown;
+       }
+       return talloc_strndup(mem_ctx, buf, sizeof(buf));
+unknown:
+       return talloc_strdup(mem_ctx, "Unknown error");
+}
index 8c289e489e8f75e3eb34e8cbbe74657ede10896e..ca1a58e922c734fe72ab44fc3f13c7b5125d464c 100644 (file)
@@ -3,3 +3,5 @@ int talloc_crypt_blob(TALLOC_CTX *mem_ctx,
                      const char *phrase,
                      const char *cmd,
                      DATA_BLOB *blob);
+
+char *talloc_crypt_errstring(TALLOC_CTX *mem_ctx, int error);
index 7a7114c1caab064ab7015f66e23f99e5eaf94c2e..6949a92fc3e790490a66f4f653ade34fad26d2ea 100644 (file)
@@ -1661,21 +1661,7 @@ static int setup_primary_userPassword_hash(
                                cmd,
                                hash_blob);
        if (ret != 0) {
-               char buf[1024];
-               const char *reason = NULL;
-               if (ret == ERANGE) {
-                       reason = "Password exceeds maximum length allowed for crypt() hashing";
-               } else if (ret == ENOTRECOVERABLE) {
-                       /* probably weird RHEL7 crypt, see talloc_crypt_blob() */
-                       reason = "Unknown error";
-               } else {
-                       int err = strerror_r(ret, buf, sizeof(buf));
-                       if (err == 0) {
-                               reason = buf;
-                       } else {
-                               reason = "Unknown error";
-                       }
-               }
+               const char *reason = talloc_crypt_errstring(frame, ret);
                ldb_asprintf_errstring(
                        ldb,
                        "setup_primary_userPassword: generation of a %s "