login add

This commit is contained in:
Your Name 2024-01-14 01:18:21 +03:00
parent d33fe7b787
commit f8dd93020d
8 changed files with 155 additions and 10 deletions

5
TODO
View File

@ -46,7 +46,7 @@ Loginutils:
getty
Modutils (linux only):
modprobe
insmod
rmmod
lsmod
@ -54,9 +54,6 @@ Findutils:
grep
find
Shell:
rc - run command (1 2 3 ~ | <> <<>> & * " parsing) (sig handler)
BUGS:
ls (unicode strlen, -l flag col)
xargs (getopt with glibc)

View File

@ -26,7 +26,7 @@ for project in $projects; do
cd $PROJECT_DIR/$i
echo " * Compile" $i
chmod -v +x build.sh
env CC=$CC CFLAGS="$CFLAGS -I$PROJECT_DIR -I$PROJECT_DIR/include/libmu -lm" OUTPUT="$PROJECT_DIR"/bin/ ./build.sh
env CC=$CC CFLAGS="$CFLAGS -I$PROJECT_DIR -I$PROJECT_DIR/include/libmu" OUTPUT="$PROJECT_DIR"/bin/ ./build.sh
cd $PROJECT_DIR
done
echo

View File

@ -0,0 +1,21 @@
#ifndef _GET_STRING_H
#define _GET_STRING_H
#include <stdio.h>
#include <string.h>
#include <errno.h>
int mu_get_string(const char *prog_name, char *buf, const size_t len) {
off_t rbytes = read(STDIN_FILENO, buf, len);
if (rbytes <= 0) {
fprintf(stderr, "%s: %s\n", prog_name, strerror(errno));
return 1;
}
if (rbytes > 1)
buf[rbytes - 1] = '\0';
return 0;
}
#endif

30
include/libmu/pw_check.h Normal file
View File

@ -0,0 +1,30 @@
#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') {
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

View File

@ -179,14 +179,14 @@ int main(int argc, char **argv) {
}
}
if (argv[optind] == NULL) {
argv += optind;
argc -= optind;
if (argc < 2) {
fprintf(stderr, "cp: missing operand\n");
return 1;
}
argv += optind;
argc -= optind;
int ret = 0;
if (argc == 2)
ret = cptree(argv[0], argv[argc - 1]);

3
src/loginutils/login/build.sh Executable file
View File

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

View File

@ -0,0 +1,94 @@
#include <pwd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <signal.h>
#include "get_string.h"
#include "pw_check.h"
void login(const struct passwd *pw) {
char *shell = pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell;
setenv("HOME", pw->pw_dir, 1);
setenv("SHELL", shell, 1);
setenv("USER", pw->pw_name, 1);
setenv("LOGNAME", pw->pw_name, 1);
if (chdir(pw->pw_dir) < 0) {
fprintf(stderr, "login: %s\n", strerror(errno));
return;
}
execlp(shell, shell, "-l", NULL);
}
struct passwd *proccess_input(char *hostname) {
static char user[512];
static char psswd[512];
/* Username */
printf("Login on %s:\n", hostname);
if (mu_get_string("login", user, sizeof(user)))
return NULL;
struct passwd *pw = getpwnam(user);
if (!pw) {
fprintf(stderr, "login: Incorrent username\n");
return NULL;
}
/* Password */
printf("\nPassword:\n");
if (mu_get_string("login", psswd, sizeof(psswd)))
return NULL;
if (pw_check("login", pw, psswd)) {
memset(psswd, '\0', sizeof(psswd));
return NULL;
}
memset(psswd, '\0', sizeof(psswd));
return pw;
}
int main(void) {
if ((!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO))) {
fprintf(stderr, "login: no tty\n");
return 1;
}
/* For prompt */
char hostname[HOST_NAME_MAX + 1];
if (gethostname(hostname, sizeof(hostname)) < 0) {
fprintf(stderr, "login: %s\n", strerror(errno));
return 1;
}
signal(SIGQUIT, SIG_IGN);
signal(SIGINT, SIG_IGN);
signal(SIGHUP, SIG_IGN);
struct passwd *pw = proccess_input(hostname);
if (!pw)
return 1;
/* Start */
if (setgid(pw->pw_gid) < 0) {
fprintf(stderr, "login: %s\n", strerror(errno));
return 1;
}
if (setuid(pw->pw_uid) < 0) {
fprintf(stderr, "login: %s\n", strerror(errno));
return 1;
}
login(pw);
return 0;
}

View File

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