micro-utils/libmu/pw_check.c
2024-07-10 18:51:30 +03:00

54 lines
1.1 KiB
C

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "pw_check.h"
#include "config.h"
void dec_salt(void) {
size_t i;
for (i = 0; i < sizeof(MU_SALT_ENC) / sizeof(int); i++)
MU_SALT_BUF[i] = (char)MU_SALT_ENC[i];
MU_SALT_BUF[i + 1] = '\0';
}
char *enc_password(const char *prog_name, const char *pass, const char *salt) {
if (salt == NULL)
dec_salt();
char *cpass = crypt(pass, (salt == NULL) ? MU_SALT_BUF : salt);
if (cpass == NULL) {
if (prog_name != NULL)
fprintf(stderr, "%s: %s\n", prog_name, strerror(errno));
return NULL;
}
return cpass;
}
int pw_check(const char *prog_name, const struct passwd *pw, const char *pass) {
if (pw->pw_passwd[0] == '\0' && pass[0] == '\0')
return 0;
if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*') {
if (prog_name != NULL)
fprintf(stderr, "%s: Access denied\n", prog_name);
return 1;
}
char *cpass = enc_password(prog_name, pass, NULL);
if (cpass == NULL)
return 1;
if (!strcmp(pw->pw_passwd, cpass))
return 0;
if (prog_name != NULL)
fprintf(stderr, "%s: Incorrect password\n", prog_name);
return 1;
}