Add su, add process group handling in shell

This commit is contained in:
Mark 2020-01-20 15:40:26 +02:00
parent bf6085b377
commit 76adf35512
7 changed files with 60 additions and 9 deletions

View File

@ -21,7 +21,8 @@ STAGE_BIN=$(STAGE)/init \
$(STAGE)/bin/rm \
$(STAGE)/bin/mkdir \
$(STAGE)/bin/login \
$(STAGE)/bin/ase
$(STAGE)/bin/ase \
$(STAGE)/bin/su
sh_OBJS=$(O)/sh/sh.o \
$(O)/sh/readline.o \
$(O)/sh/builtin.o \
@ -45,6 +46,7 @@ clean:
rm -rf $(O)
$(O)/initrd.img: mkstage-etc $(STAGE_BIN)
chmod 04711 $(STAGE)/bin/su
cd $(STAGE) && tar czf $(abspath $@) *
mkdirs:

View File

@ -1,4 +1,5 @@
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <errno.h>
#include <pwd.h>
@ -8,6 +9,9 @@ struct spwd {
char *sp_pwdp;
};
static int attempt = 0;
static char line_buf[64];
static ssize_t getline(char *buf, size_t lim, char pwchr) {
size_t c = 0;
char chr;
@ -124,6 +128,10 @@ static int loginas(const char *name) {
return loginuid(res->pw_uid, res->pw_gid, shell);
}
static void signal_handler(int signum) {
// TODO: syscalls interruptible by singals
}
int main(int argc, char **argv) {
// Arguments are ignored
if (getuid() != 0) {
@ -131,8 +139,8 @@ int main(int argc, char **argv) {
return -1;
}
int attempt = 0;
char line_buf[64];
signal(SIGINT, signal_handler);
char spnam_buf[128];
struct spwd sp;
@ -162,10 +170,14 @@ int main(int argc, char **argv) {
continue;
}
printf("password: ");
if (getline(line_buf, sizeof(line_buf), '*') < 0) {
++attempt;
continue;
if (sp.sp_pwdp[0] != 0) {
printf("password: ");
if (getline(line_buf, sizeof(line_buf), '*') < 0) {
++attempt;
continue;
}
} else {
line_buf[0] = 0;
}
// No hashing yet, so just compare passwords
@ -175,6 +187,7 @@ int main(int argc, char **argv) {
attempt = 3;
continue;
} else {
printf("Login failed\n");
++attempt;
}
}

View File

@ -66,7 +66,7 @@ static int ls_dir(const char *path, int flags) {
t,
(ent_stat.st_mode & S_IRUSR) ? 'r' : '-',
(ent_stat.st_mode & S_IWUSR) ? 'w' : '-',
(ent_stat.st_mode & S_IXUSR) ? 'x' : '-',
(ent_stat.st_mode & S_ISUID ? 's' : (ent_stat.st_mode & S_IXUSR) ? 'x' : '-'),
(ent_stat.st_mode & S_IRGRP) ? 'r' : '-',
(ent_stat.st_mode & S_IWGRP) ? 'w' : '-',
(ent_stat.st_mode & S_IXGRP) ? 'x' : '-',

19
core/bin/su.c Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
int main(int argc, char **argv) {
if (getuid() != 0) {
printf("su binary must have setuid bit on\n");
return -1;
}
if (setgid(0) != 0) {
perror("setgid()");
return -1;
}
const char *shell = "/bin/sh";
const char *argp[] = {
shell, NULL
};
return execve(shell, argp, NULL);
}

View File

@ -1,2 +1,2 @@
root:toor:
alnyan:password:
alnyan::

View File

@ -1,3 +1,5 @@
#include <sys/termios.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
@ -57,11 +59,19 @@ static int cmd_spawn(const char *path, const struct cmd_exec *cmd, int *cmd_res)
}
if (pid == 0) {
pid = getpid();
ioctl(STDIN_FILENO, TIOCSPGRP, &pid);
setpgid(0, 0);
exit(execve(path, (const char *const *) cmd->args, NULL));
} else {
if (waitpid(pid, cmd_res) != 0) {
perror("waitpid()");
}
// Regain control of foreground group
pid = getpgid(0);
ioctl(STDIN_FILENO, TIOCSPGRP, &pid);
return 0;
}
}

View File

@ -1,3 +1,4 @@
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <pwd.h>
@ -72,11 +73,17 @@ static void display_prompt(void) {
}
}
static void signal_handle(int signum) {
printf("\n^C\n");
}
int main(int argc, char **argv) {
int fd = STDIN_FILENO;
char linebuf[256];
int res;
signal(SIGINT, signal_handle);
if (argc == 2) {
fd = open(argv[1], O_RDONLY, 0);