fixed pw_check, cryptpw

This commit is contained in:
pups 2024-07-31 13:58:31 +03:00
parent 1925d32c40
commit ff94d81541
5 changed files with 29 additions and 43 deletions

View File

@ -14,12 +14,6 @@
#define HEAD_FMT "==> %s <==\n" #define HEAD_FMT "==> %s <==\n"
#endif #endif
#ifdef _PW_CHECK_H
/* Pw_check. Salt for crypt() */
int MU_SALT_ENC[] = {'s', 'a', 'l', 't'};
char MU_SALT_BUF[sizeof(MU_SALT_ENC) + 1];
#endif
#ifdef _MOUNT_C #ifdef _MOUNT_C
/* mount config */ /* mount config */
#define MOUNT_CFG "/etc/fstab" #define MOUNT_CFG "/etc/fstab"

View File

@ -5,48 +5,29 @@
#include "pw_check.h" #include "pw_check.h"
#include "config.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) { int pw_check(const char *prog_name, const struct passwd *pw, const char *pass) {
if (pw->pw_passwd[0] == '\0' && pass[0] == '\0') if (pw->pw_passwd[0] == '\0' && pass[0] == '\0')
return 0; return 0;
if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*') { if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*') {
if (prog_name != NULL) if (prog_name)
fprintf(stderr, "%s: Access denied\n", prog_name); fprintf(stderr, "%s: Access denied\n", prog_name);
return 1; return 1;
} }
char *cpass = enc_password(prog_name, pass, NULL); char *cpass = crypt(pass, pw->pw_passwd);
if (cpass == NULL) if (cpass == NULL) {
if (prog_name)
fprintf(stderr, "%s: crypt: %s\n", prog_name, strerror(errno));
return 1; return 1;
}
if (!strcmp(pw->pw_passwd, cpass)) if (!strcmp(pw->pw_passwd, cpass))
return 0; return 0;
if (prog_name != NULL) if (prog_name)
fprintf(stderr, "%s: Incorrect password\n", prog_name); fprintf(stderr, "%s: Incorrect password\n", prog_name);
return 1; return 1;

View File

@ -2,8 +2,5 @@
#define _PW_CHECK_H #define _PW_CHECK_H
#include <pwd.h> #include <pwd.h>
void dec_salt(void);
char *enc_password(const char *prog_name, const char *pass, const char *salt);
int pw_check(const char *prog_name, const struct passwd *pw, const char *pass); int pw_check(const char *prog_name, const struct passwd *pw, const char *pass);
#endif #endif

View File

@ -1,5 +1,20 @@
#include <stdio.h> #include <stdio.h>
#include "pw_check.h" #include <stdlib.h>
#include <unistd.h>
static char *gen_salt(void) {
srand(getpid());
static char salt[15];
char abc[] = "asdfghjklzxcvbnmqwertyuiop1234567890";
size_t i = 0;
for (; i < sizeof(salt); i++)
salt[i] = abc[rand() % sizeof(abc)];
salt[i] = '\0';
return salt;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc < 2) { if (argc < 2) {
@ -7,11 +22,11 @@ int main(int argc, char **argv) {
return 0; return 0;
} }
char *salt = NULL; char *salt_ptr = gen_salt();
if (argc > 2) if (argc > 2)
salt = argv[2]; salt_ptr = argv[2];
char *cpass = enc_password("cryptpw", argv[1], salt); char *cpass = crypt(argv[1], salt_ptr);
if (cpass == NULL) if (cpass == NULL)
return 1; return 1;

View File

@ -7,8 +7,8 @@
#include <sys/klog.h> #include <sys/klog.h>
int main(int argc, char **argv) { int main(int argc, char **argv) {
unsigned int s_size = 0; int s_size = 0;
unsigned int n_level = 0; int n_level = 0;
int opt; int opt;
while ((opt = getopt(argc, argv, "s:n:")) != -1) { while ((opt = getopt(argc, argv, "s:n:")) != -1) {
@ -65,4 +65,3 @@ int main(int argc, char **argv) {
free(buf); free(buf);
return 0; return 0;
} }