// SPDX-License-Identifier: GPL-2.0-only // Bananarchy shadow passcode verifier for the fullscreen phone locker. #ifndef _DEFAULT_SOURCE #define _DEFAULT_SOURCE #endif #ifndef _XOPEN_SOURCE #define _XOPEN_SOURCE 700 #endif #include #include #include #include #include #include #include static int constant_time_eq(const char *a, const char *b) { if (!a || !b) { return 0; } size_t la = strlen(a); size_t lb = strlen(b); if (la == 0 || la != lb) { return 0; } unsigned char diff = 0; for (size_t i = 0; i < la; ++i) { diff |= (unsigned char)(a[i] ^ b[i]); } return diff == 0; } int main(int argc, char **argv) { int print_hash = 0; const char *username = "user"; for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "--print-hash") == 0) { print_hash = 1; } else if (argv[i][0] != '-') { username = argv[i]; } } if (strcmp(username, "user") != 0 && strcmp(username, "root") != 0) { return 2; } struct spwd *sp = getspnam(username); const char *hash = (sp && sp->sp_pwdp) ? sp->sp_pwdp : NULL; if (!hash || !hash[0]) { struct passwd *pw = getpwnam(username); hash = (pw && pw->pw_passwd) ? pw->pw_passwd : NULL; } if (!hash || !hash[0] || hash[0] == '!' || hash[0] == '*') { return 1; } if (print_hash) { puts(hash); return 0; } char pass[128]; if (!fgets(pass, sizeof(pass), stdin)) { return 1; } size_t len = strlen(pass); while (len > 0 && (pass[len - 1] == '\n' || pass[len - 1] == '\r')) { pass[--len] = '\0'; } if (len == 0) { return 1; } char *calc = crypt(pass, hash); memset(pass, 0, sizeof(pass)); if (!calc) { return 1; } return constant_time_eq(calc, hash) ? 0 : 1; }