micro-utils/libmu/pw_check.c

35 lines
715 B
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
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] == '*') {
2024-07-31 10:58:31 +00:00
if (prog_name)
2024-07-01 10:23:00 +00:00
fprintf(stderr, "%s: Access denied\n", prog_name);
return 1;
}
2024-07-31 10:58:31 +00:00
char *cpass = crypt(pass, pw->pw_passwd);
if (cpass == NULL) {
if (prog_name)
fprintf(stderr, "%s: crypt: %s\n", prog_name, strerror(errno));
2024-07-01 10:23:00 +00:00
return 1;
2024-07-31 10:58:31 +00:00
}
2024-07-01 10:23:00 +00:00
if (!strcmp(pw->pw_passwd, cpass))
return 0;
2024-07-31 10:58:31 +00:00
if (prog_name)
2024-07-01 10:23:00 +00:00
fprintf(stderr, "%s: Incorrect password\n", prog_name);
return 1;
}