]> PatchyGit - patchyircd.git/commitdiff
job: move hash retrieval here
authorEgor <egor@patchysicp.xyz>
Thu, 22 Jan 2026 11:10:53 +0000 (13:10 +0200)
committerEgor <egor@patchysicp.xyz>
Thu, 22 Jan 2026 11:10:53 +0000 (13:10 +0200)
src/db.cpp
src/db.hpp
src/job.cpp
src/passwd.cpp
src/passwd.hpp

index 944b25f..fc94a55 100644 (file)
@@ -95,27 +95,3 @@ InitLock::~InitLock() {
        memory = Database();
 }
 }
-
-// deprecated old APIs
-static char *db_read_internal(const char *key, db::Statement stmt) {
-       static char buf[1024];
-       stmt.bind(1, key);
-       if (!stmt.step())
-               return nullptr;
-       auto sv = stmt.text(0);
-       if (sv.size()+1 > 1024)
-               return nullptr;
-       memcpy(buf, sv.data(), sv.size());
-       buf[sv.size()] = '\0';
-       return buf;
-}
-char *password_get(const char *key, const char *fn) {
-       if (fn == "ops"sv)
-               return db_read_internal(key, db::disk("SELECT hash FROM ops WHERE account = ?;"));
-       else if (fn == "users"sv)
-               return db_read_internal(key, db::disk("SELECT hash FROM users WHERE account = ?;"));
-       else if (fn == "webirc"sv)
-               return db_read_internal(key, db::disk("SELECT hash FROM webirc WHERE account = ?;"));
-       else
-               return nullptr;
-}
index ebf2444..6402af1 100644 (file)
@@ -59,7 +59,4 @@ namespace db {
        };
        [[nodiscard]] InitLock init();
 }
-
-// deprecated old APIs from passwd.hpp
-char *password_get(const char *key, const char *fn);
 #endif
index 763ad41..37a45c4 100644 (file)
@@ -120,10 +120,25 @@ struct SecureStringWrapper {
 };
 void job_password_check(Client *client, const char *key, const char *pw, const char *fn, contproc_t&& then)
 {
-       char *salt = password_get(key, fn);
+       db::Statement stmt;
+       std::string_view salt;
+       if (fn == "ops"sv)
+               stmt = db::disk("SELECT hash FROM ops WHERE account = ?;");
+       else if (fn == "users"sv)
+               stmt = db::disk("SELECT hash FROM users WHERE account = ?;");
+       else if (fn == "webirc"sv)
+               stmt = db::disk("SELECT hash FROM webirc WHERE account = ?;");
+       else
+               goto no_table;
+
+       stmt.bind(1, key);
+       if (stmt.step())
+               salt = stmt.text(0);
+
+no_table:
        // FIXME: make_shared only really needed here cuz std::function wants a copyable object
        // C++23 adds a std::move_only_function
-       job_add(client, [pw=std::make_shared<SecureStringWrapper>(pw), salt=std::make_shared<SecureStringWrapper>(salt)]() -> int {
+       job_add(client, [pw=std::make_shared<SecureStringWrapper>(pw), salt=std::make_shared<SecureStringWrapper>(salt.data())]() -> int {
                if (!*salt)
                        return 1;
                char *hash = password_crypt(*pw, *salt);
index 1ab9034..95804b5 100644 (file)
@@ -44,19 +44,3 @@ char *password_hash(const char *pw)
 {
        return password_hash2(pw, 2, 12);
 }
-#if 0
-int password_check(const char *key, const char *pw, const char *fn)
-{
-       char *salt = password_get(key, fn);
-       if (salt) {
-               char *hash = password_crypt(pw, salt);
-               int rv = hash && !strcmp(hash, salt);
-               memset(salt, 0, strlen(salt));
-               if (hash)
-                       memset(hash, 0, strlen(hash));
-               return -!rv;
-       } else {
-               return -1;
-       }
-}
-#endif
index 690c2af..e04e845 100644 (file)
@@ -6,5 +6,4 @@
 char *password_crypt(const char *key, const char *setting);
 char *password_hash2(const char *pw, int algo, int strength);
 char *password_hash(const char *pw);
-//int password_check(const char *key, const char *pw, const char *fn);
 #endif