fixed login/pw_chech added cryptpw

This commit is contained in:
Your Name 2024-05-31 10:52:57 +00:00
parent b752f9d46a
commit 94a0c8c60c
15 changed files with 119 additions and 38 deletions

View File

@ -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 */

View File

@ -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

View File

@ -3,7 +3,34 @@
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.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] = 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)

View File

@ -1,3 +1,5 @@
#define _HEAD_C
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>

View File

@ -137,4 +137,3 @@ int main(int argc, char **argv) {
return def_ids(uid, pwd);
}

View File

@ -1,3 +1,5 @@
#define _LS_C
#include <pwd.h>
#include <grp.h>
#include <time.h>
@ -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;

View File

@ -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)

View File

@ -1,3 +1,5 @@
#define _SHRED_C
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

View File

@ -1,3 +1,5 @@
#define UNAME_C
#include <stdio.h>
#include <errno.h>
#include <string.h>

View File

@ -0,0 +1,3 @@
#!/bin/sh
project_dir=$(pwd)
echo ./*.c $CFLAGS -o $OUTPUT$(basename $project_dir) | xargs $CC

View File

@ -0,0 +1,20 @@
#include <stdio.h>
#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;
}

View File

@ -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;

View File

@ -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);

View File

@ -1,3 +1,5 @@
#define _MOUNT_C
#include <stdio.h>
#include <errno.h>
#include <string.h>

View File

@ -1,3 +1,5 @@
#define _MOUNT_C
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>