Add an auth "counter" function for tracking auth changes
[blerg.git] / common / auth.c
index 240ac47..e2f7407 100644 (file)
@@ -388,3 +388,28 @@ int auth_check_token(const char *username, const char *given_token) {
 
        return ret;
 }
+
+/* Return a 32-bit integer "counter" that will change when the password is
+ * updated.  Used to invalidate password recovery schemes after the password is
+ * updated.  Returns the counter in the "counter" argument, and returns
+ * true/false on success/failure. */
+int auth_get_counter(const char *username, uint32_t *counter) {
+       struct auth_v2 auth;
+       struct MD5Context ctx;
+       uint8_t md5hash[MD5_DIGEST_SIZE];
+
+       if (auth_get_data(username, (void *) &auth, sizeof(struct auth_v2)) == 0)
+               return 0;
+
+       /* There's probably going to be some question about using MD5 here.
+        * All I really need is to quickly and repeatably scramble some bits.
+        * MD5 can still do that. */
+       MD5Init(&ctx);
+       MD5Update(&ctx, auth.password, SCRYPT_OUTPUT_SIZE);
+       MD5Update(&ctx, auth.salt, SCRYPT_SALT_SIZE);
+       MD5Final((unsigned char *)md5hash, &ctx);
+
+       *counter = ((uint32_t *)md5hash)[0];
+
+       return 1;
+}