Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build with OpenSSL 1.1. #93

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/users.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ struct mt_credentials *find_user(char *username) {
return NULL;
}

#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
/*
* Filter out colons from the decoded string.
* By default, the OPENSSL_buf2hexstr function in OpenSSL 1.1
* uses colons as a byte separator, and this cannot be overridden.
*/
static void remove_colons(char *s) {
const char *p = s;
char *q = s;
while (*p != '\0') {
*q = *p++;
q += (*q != ':');
}

*q = '\0';
}
#endif

int add_user(const char *username, const char *password) {
FILE *rfile;
FILE *wfile;
Expand Down Expand Up @@ -289,12 +307,27 @@ int add_user(const char *username, const char *password) {
continue;
}
fprintf(wfile, "%s:", username);
#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
char *output;
output = OPENSSL_buf2hexstr(newhash, MT_CRED_HASHLEN);
remove_colons(output);
#else
char output[MT_CRED_HASHLEN * 2 + 1];
OPENSSL_buf2hexstr_ex(output, sizeof(output), NULL, newhash, MT_CRED_HASHLEN, '\0');
#endif
fputs(output, wfile);
fputs(":", wfile);
#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
OPENSSL_free(output);
output = OPENSSL_buf2hexstr(newsalt, MT_CRED_SALTLEN);
remove_colons(output);
#else
OPENSSL_buf2hexstr_ex(output, sizeof(output), NULL, newsalt, MT_CRED_SALTLEN, '\0');
#endif
fputs(output, wfile);
#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
OPENSSL_free(output);
#endif
fputs("\n", wfile);
found = 1;
} else {
Expand All @@ -306,12 +339,27 @@ int add_user(const char *username, const char *password) {
if (!found && password != NULL) {
// Write username, salt, and hashed password to the file
fprintf(wfile, "%s:", username);
#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
char *output;
output = OPENSSL_buf2hexstr(newhash, MT_CRED_HASHLEN);
remove_colons(output);
#else
char output[MT_CRED_HASHLEN * 2 + 1];
OPENSSL_buf2hexstr_ex(output, sizeof(output), NULL, newhash, MT_CRED_HASHLEN, '\0');
#endif
fputs(output, wfile);
fputs(":", wfile);
#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
OPENSSL_free(output);
output = OPENSSL_buf2hexstr(newsalt, MT_CRED_SALTLEN);
remove_colons(output);
#else
OPENSSL_buf2hexstr_ex(output, sizeof(output), NULL, newsalt, MT_CRED_SALTLEN, '\0');
#endif
fputs(output, wfile);
#if OPENSSL_VERSION_NUMBER < 0x030000000 // less than 3.0.0
OPENSSL_free(output);
#endif
fputs("\n", wfile);
}

Expand All @@ -327,4 +375,4 @@ int add_user(const char *username, const char *password) {
}

return found ? 2 : 1;
}
}
Loading