From 94a0c8c60cd6cd7705e5431ac5a88470ba2c1913 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 31 May 2024 10:52:57 +0000 Subject: [PATCH] fixed login/pw_chech added cryptpw --- config.h | 72 ++++++++++++++++++------------ configs/passwd | 2 +- include/libmu/pw_check.h | 33 +++++++++++++- src/coreutils/head/head.c | 2 + src/coreutils/id/id.c | 1 - src/coreutils/ls/ls.c | 4 +- src/coreutils/nohup/nohup.c | 4 +- src/coreutils/shred/shred.c | 2 + src/coreutils/uname/uname.c | 2 + src/loginutils/cryptpw/build.sh | 3 ++ src/loginutils/cryptpw/cryptpw.c | 20 +++++++++ src/miscutils/spark/spark.c | 4 +- src/miscutils/time/time.c | 4 +- src/sysutils-linux/mount/mount.c | 2 + src/sysutils-linux/umount/umount.c | 2 + 15 files changed, 119 insertions(+), 38 deletions(-) create mode 100755 src/loginutils/cryptpw/build.sh create mode 100644 src/loginutils/cryptpw/cryptpw.c diff --git a/config.h b/config.h index 01ce060..49d22e3 100644 --- a/config.h +++ b/config.h @@ -1,38 +1,54 @@ -#ifndef _CONFIG_H + #ifndef _CONFIG_H #define _CONFIG_H /* (cat tee wc xargs rev split) text buffer */ #define BUF_SIZE 2048 -/* Random source (shred) */ -#define RAND_SOURCE "/dev/urandom" - -/* format for printf (head) */ -#define HEAD_FMT "==> %s <==\n" - -/* mount config */ -#define MOUNT_CFG "/etc/fstab" -#define MOUNT_DEF_FS "ext4" -#define MOUNT_LIST "/proc/mounts" -#define MOUNT_OPT_SIZE 512 - -/* colors for ls */ -#define LS_DIR_COLOR "\033[1;34m" -#define LS_LINK_COLOR "\033[1;35m" -#define LS_SOCK_COLOR "\033[35m" -#define LS_FIFO_COLOR "\033[1;35m" -#define LS_BLOCK_COLOR "\033[1;33m" -#define LS_EXE_COLOR "\033[1;32m" - -/* Init scripts */ -#ifdef INIT -char *INIT_POWEROFF[] = {"/etc/rc.poweroff", NULL}; -char *INIT_START[] = {"/etc/rc.init", NULL}; -#define INIT_MSG "Starting micro-init..." +#ifdef _SHRED_C + /* Random source (shred) */ + #define RAND_SOURCE "/dev/urandom" #endif -/* Os name for uname */ -/* #define OS_NAME "unknow" */ +#ifdef _HEAD_C + /* format for printf (head) */ + #define HEAD_FMT "==> %s <==\n" +#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 + /* mount config */ + #define MOUNT_CFG "/etc/fstab" + #define MOUNT_DEF_FS "ext4" + #define MOUNT_LIST "/proc/mounts" + #define MOUNT_OPT_SIZE 512 +#endif + +#ifdef _LS_C + /* colors for ls */ + #define LS_DIR_COLOR "\033[1;34m" + #define LS_LINK_COLOR "\033[1;35m" + #define LS_SOCK_COLOR "\033[35m" + #define LS_FIFO_COLOR "\033[1;35m" + #define LS_BLOCK_COLOR "\033[1;33m" + #define LS_EXE_COLOR "\033[1;32m" +#endif + +#ifdef INIT + /* Init scripts */ + char *INIT_POWEROFF[] = {"/etc/rc.poweroff", NULL}; + char *INIT_START[] = {"/etc/rc.init", NULL}; + #define INIT_MSG "Starting micro-init..." +#endif + +#ifdef _UNAME_C + /* Os name for uname */ + /* #define OS_NAME "unknow" */ +#endif /* Options: To disable, comment line */ diff --git a/configs/passwd b/configs/passwd index 715c6e4..013ee3d 100644 --- a/configs/passwd +++ b/configs/passwd @@ -1,2 +1,2 @@ root::0:0:root:/usr/root:/bin/sh -nobody:*:65534:65534:nobody:/nonexistent:/bin/nologin +nobody:*:65534:65534:nobody:/nonexistent:/bin/false diff --git a/include/libmu/pw_check.h b/include/libmu/pw_check.h index b409c7d..7a6654f 100644 --- a/include/libmu/pw_check.h +++ b/include/libmu/pw_check.h @@ -3,7 +3,34 @@ #include #include +#include #include +#include +#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] = MU_SALT_ENC[i]; + + MU_SALT_BUF[i + 1] = '\0'; +} + +/* Using not only there */ +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') @@ -16,7 +43,11 @@ int pw_check(const char *prog_name, const struct passwd *pw, const char *pass) { return 1; } - if (!strcmp(pass, pw->pw_passwd)) + 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) diff --git a/src/coreutils/head/head.c b/src/coreutils/head/head.c index 5928409..92110a8 100644 --- a/src/coreutils/head/head.c +++ b/src/coreutils/head/head.c @@ -1,3 +1,5 @@ +#define _HEAD_C + #include #include #include diff --git a/src/coreutils/id/id.c b/src/coreutils/id/id.c index c6e3d5d..a2ea279 100644 --- a/src/coreutils/id/id.c +++ b/src/coreutils/id/id.c @@ -137,4 +137,3 @@ int main(int argc, char **argv) { return def_ids(uid, pwd); } - diff --git a/src/coreutils/ls/ls.c b/src/coreutils/ls/ls.c index 79a21e7..b93c7c3 100644 --- a/src/coreutils/ls/ls.c +++ b/src/coreutils/ls/ls.c @@ -1,3 +1,5 @@ +#define _LS_C + #include #include #include @@ -104,7 +106,7 @@ struct d_node **list(const char *path, size_t *nfiles, int *ret) { if (dn == NULL) return NULL; - struct d_node **dir = malloc(*nfiles * sizeof(struct d_node *)); + struct d_node **dir = malloc((*nfiles + 1) * sizeof(struct d_node *)); if (dir == NULL) { fprintf(stderr, "ls: malloc failed\n"); return NULL; diff --git a/src/coreutils/nohup/nohup.c b/src/coreutils/nohup/nohup.c index 9681faa..ae5a54d 100644 --- a/src/coreutils/nohup/nohup.c +++ b/src/coreutils/nohup/nohup.c @@ -8,8 +8,8 @@ int main(int argc, char **argv) { if (argc < 2) { - fprintf(stderr, "nohup: missing operand\n"); - return 1; + printf("nohup: missing operand\nnohup [cmd]\n"); + return 0; } if (fork() != 0) diff --git a/src/coreutils/shred/shred.c b/src/coreutils/shred/shred.c index 3706365..93c257a 100644 --- a/src/coreutils/shred/shred.c +++ b/src/coreutils/shred/shred.c @@ -1,3 +1,5 @@ +#define _SHRED_C + #include #include #include diff --git a/src/coreutils/uname/uname.c b/src/coreutils/uname/uname.c index 72e7615..7094803 100644 --- a/src/coreutils/uname/uname.c +++ b/src/coreutils/uname/uname.c @@ -1,3 +1,5 @@ +#define UNAME_C + #include #include #include diff --git a/src/loginutils/cryptpw/build.sh b/src/loginutils/cryptpw/build.sh new file mode 100755 index 0000000..6d8974b --- /dev/null +++ b/src/loginutils/cryptpw/build.sh @@ -0,0 +1,3 @@ +#!/bin/sh +project_dir=$(pwd) +echo ./*.c $CFLAGS -o $OUTPUT$(basename $project_dir) | xargs $CC diff --git a/src/loginutils/cryptpw/cryptpw.c b/src/loginutils/cryptpw/cryptpw.c new file mode 100644 index 0000000..74179bf --- /dev/null +++ b/src/loginutils/cryptpw/cryptpw.c @@ -0,0 +1,20 @@ +#include +#include "pw_check.h" + +int main(int argc, char **argv) { + if (argc < 2) { + printf("cryptpw: missing operand\ncryptpw [password] [own salt]\n"); + return 0; + } + + char *salt = NULL; + if (argc > 2) + salt = argv[2]; + + char *cpass = enc_password("cryptpw", argv[1], salt); + if (cpass == NULL) + return 1; + + puts(cpass); + return 0; +} diff --git a/src/miscutils/spark/spark.c b/src/miscutils/spark/spark.c index f9ccc4b..a2c6871 100644 --- a/src/miscutils/spark/spark.c +++ b/src/miscutils/spark/spark.c @@ -27,8 +27,8 @@ void print(double diff, char *str, double min) { int main(int argc, char **argv) { if (argc == 1) { - fprintf(stderr, "spark: missing operands\n"); - return 1; + printf("spark: missing operands\nspark [num1] [num2]...\n"); + return 0; } unsigned int r_flag = 0; diff --git a/src/miscutils/time/time.c b/src/miscutils/time/time.c index 862b5c2..5ef1e1b 100644 --- a/src/miscutils/time/time.c +++ b/src/miscutils/time/time.c @@ -9,8 +9,8 @@ int main(int argc, char **argv) { if (argc < 2) { - fprintf(stderr, "time: missing operand\n"); - return 1; + printf("time: missing operand\ntime [cmd]\n"); + return 0; } long ticks = sysconf(_SC_CLK_TCK); diff --git a/src/sysutils-linux/mount/mount.c b/src/sysutils-linux/mount/mount.c index 53d4e29..2ac0280 100644 --- a/src/sysutils-linux/mount/mount.c +++ b/src/sysutils-linux/mount/mount.c @@ -1,3 +1,5 @@ +#define _MOUNT_C + #include #include #include diff --git a/src/sysutils-linux/umount/umount.c b/src/sysutils-linux/umount/umount.c index 74a8def..ee7ec47 100644 --- a/src/sysutils-linux/umount/umount.c +++ b/src/sysutils-linux/umount/umount.c @@ -1,3 +1,5 @@ +#define _MOUNT_C + #include #include #include