micro-utils/include/libmu/pw_check.h

34 lines
640 B
C

#ifndef _PW_CHECK_H
#define _PW_CHECK_H
#include <pwd.h>
#include <stdio.h>
#include <crypt.h>
#include <unistd.h>
int pw_check(const char *prog_name, const struct passwd *pw, const char *pass) {
if (pw->pw_passwd[0] == '\0' || pass[0] == '\0') {
if (pass[0] == '\0')
return 0;
fprintf(stderr, "%s: Incorrect password\n", prog_name);
return 1;
}
if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*') {
fprintf(stderr, "%s: Denied\n", prog_name);
return 1;
}
char *cryptpass = crypt(pass, pw->pw_passwd);
if (cryptpass == NULL)
return 1;
if (!strcmp(pw->pw_passwd, cryptpass))
return 0;
return 1;
}
#endif