micro-utils/libmu/pw_check.c

54 lines
1.1 KiB
C
Raw Normal View History

2024-07-01 10:23:00 +00:00
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
2024-07-09 19:33:45 +00:00
#include "pw_check.h"
2024-07-01 10:23:00 +00:00
#include "config.h"
2024-07-09 19:33:45 +00:00
void dec_salt(void) {
2024-07-01 10:23:00 +00:00
size_t i;
for (i = 0; i < sizeof(MU_SALT_ENC) / sizeof(int); i++)
2024-07-02 19:48:16 +00:00
MU_SALT_BUF[i] = (char)MU_SALT_ENC[i];
2024-07-01 10:23:00 +00:00
MU_SALT_BUF[i + 1] = '\0';
}
2024-07-09 19:33:45 +00:00
char *enc_password(const char *prog_name, const char *pass, const char *salt) {
2024-07-01 10:23:00 +00:00
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;
}
2024-07-09 19:33:45 +00:00
int pw_check(const char *prog_name, const struct passwd *pw, const char *pass) {
2024-07-01 10:23:00 +00:00
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;
}