From 9e1d304cac5367c3dbb1ca14700736680bd3a804 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Wed, 11 Nov 2026 08:31:06 +0300 Subject: publish --- 03/src/shell.c | 1762 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1762 insertions(+) create mode 100644 03/src/shell.c (limited to '03/src/shell.c') diff --git a/03/src/shell.c b/03/src/shell.c new file mode 100644 index 0000000..0935ebc --- /dev/null +++ b/03/src/shell.c @@ -0,0 +1,1762 @@ +// src/shell.c --- utility for providing access to protected folder +// +// Copyright (c) 2026 Martin Michalec +// +// SPDX-License-Identifier: MIT +// +// Commentary: +// +// Code: + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "common.h" + +#define CONFDATA PREFIX"/confdata" +#define ACL PREFIX"/etc/acl" + +int id; + +struct string_view +canonicalize (struct string_view filename) +{ + static struct string_builder sb = {0}; + STRING_BUILDER_RESET (sb); + + static struct { + struct string_view *items; + uint32_t count; + uint32_t capacity; + } parts = {0}; + DYNAMIC_ARRAY_RESET (parts); + + bool ends_with_slash = filename.end[-1] == '/'; + + if (*filename.begin != '/') { + DYNAMIC_ARRAY_ENSURE_CAPACITY (sb, 128); + for (;;) { + if (getcwd (sb.string, sb.capacity)) { + sb.length = strlen (sb.string); + break; + } + if (errno != ERANGE) + return SVNULL; + DYNAMIC_ARRAY_ENSURE_CAPACITY (sb, 2*sb.capacity); + } + STRING_BUILDER_APPEND_CHAR (sb, '/'); + } + + STRING_BUILDER_APPEND_STRING_VIEW (sb, filename); + filename = SVSB (sb); + + while (++filename.begin < filename.end) { + struct string_view part = string_view_pop_up_to (&filename, '/'); + if (SVLEN (part) == 0) continue; + if (SVCMP (part, SVCSTR (".")) == 0) continue; + if (SVCMP (part, SVCSTR ("..")) == 0) { + if (parts.count == 0) return SVNULL; + parts.count -= 1; + continue; + } + DYNAMIC_ARRAY_APPEND (parts, part); + } + + STRING_BUILDER_RESET (sb); + for (struct string_view *part = parts.items; + part < parts.items + parts.count; ++part) { + STRING_BUILDER_APPEND_CHAR (sb, '/'); + STRING_BUILDER_APPEND_STRING_VIEW (sb, *part); + } + + if (ends_with_slash) + STRING_BUILDER_APPEND_CHAR (sb, '/'); + + return SVSB (sb); +} + +struct acl_grant { + int id; + enum access_rights access_rights; +}; + +struct acl { + struct string_view filename; + const char *path; + enum security_level security_level; + int owner; + int grant_count; + struct acl_grant grants[]; +}; + +const char * +filename_to_acl_path (struct string_view filename) +{ + static struct string_builder acl_path = {0}; + + struct string_view canonical = canonicalize (filename); + ASSERT (strncmp (canonical.begin, CONFDATA, strlen (CONFDATA)) == 0); + + STRING_BUILDER_RESET (acl_path); + STRING_BUILDER_APPEND_CSTR (acl_path, ACL); + STRING_BUILDER_APPEND_CHAR (acl_path, '/'); + if (SVLEN (canonical) == strlen (CONFDATA)) { + STRING_BUILDER_APPEND_CHAR (acl_path, '!'); + goto finish; + } + + for (const char *head = canonical.begin + strlen (CONFDATA); head < canonical.end; ++head) { + if (*head == '/') STRING_BUILDER_APPEND_CHAR (acl_path, '!'); + else STRING_BUILDER_APPEND_CHAR (acl_path, *head); + } + + finish: + STRING_BUILDER_TERMINATE (acl_path); + return acl_path.string; +} + +void +init_acl_for_file (struct string_view filename, int owner) +{ + struct passwd passwd = load_passwd_file (); + struct passwd_entry *entry = lookup_id (passwd, id); + close_passwd_file (passwd); + ASSERT (entry); + + const char *path = filename_to_acl_path (filename); + ASSERT (path); + + elevate (); + int fd = open (path, O_WRONLY | O_CREAT | O_EXCL); + drop (); + ASSERT (fd != -1); + + FILE *stream = fdopen (fd, "w"); + ASSERT (stream); + fprintf (stream, "%d %d 0\n", entry->security_level, id); + fclose (stream); +} + +/// File has to be in confdata +struct acl * +load_acl_for_file (struct string_view filename) +{ + struct acl header = { + .filename = filename, + .path = filename_to_acl_path (filename), + }; + ASSERT (header.path); + + elevate (); + int fd = open (header.path, O_RDONLY); + drop (); + if (fd == -1) return NULL; + struct stat st; + fstat (fd, &st); + if (S_ISDIR (st.st_mode)) + return NULL; + + FILE *stream = fdopen (fd, "r"); + ASSERT (stream); + + fscanf (stream, "%d %d %d", + (int *) &header.security_level, &header.owner, &header.grant_count); + struct acl *acl = malloc (sizeof (*acl) + + (header.grant_count + 1)*sizeof (*acl->grants)); + *acl = header; + for (int i = 0; i < acl->grant_count; ++i) + fscanf (stream, "%d %d", + &acl->grants[i].id, (int *) &acl->grants[i].access_rights); + fclose (stream); + + return acl; +} + +void +save_acl (struct acl *acl) +{ + elevate (); + int fd = open (acl->path, O_WRONLY); + drop (); + ASSERT (fd != -1); + + FILE *stream = fdopen (fd, "w"); + ASSERT (stream); + + fprintf (stream, "%d %d %d\n", + acl->security_level, acl->owner, acl->grant_count); + for (int i = 0; i < acl->grant_count; ++i) + fprintf (stream, "%d %d\n", + acl->grants[i].id, acl->grants[i].access_rights); + fclose (stream); +} + +struct command_line { + struct string_builder expanded; + struct string_view command; + union {struct string_view *items, *args; }; + union {uint32_t count, argc; }; + uint32_t capacity; +}; + +struct string_builder confdata_log = {0}; + +void +flush_confdata_log () +{ + if (confdata_log.count) { + elevate (); + int fd = open (PREFIX"/log/confdata.log", O_CREAT | O_WRONLY | O_APPEND); + drop (); + write (fd, confdata_log.items, confdata_log.count); + close (fd); + DYNAMIC_ARRAY_RESET (confdata_log); + } +} + +int +is_conf (struct string_view filename) +{ + int exists = 1; + elevate (); + char *canonical = realpath (filename.begin, NULL); + if (!canonical) { + exists = 0; + struct string_view dirname = SVDUP0 (string_view_dirname (filename)); + canonical = realpath (dirname.begin, NULL); + free ((void *) dirname.begin); + } + drop (); + + if (canonical) { + bool starts_with = string_view_starts_with (SVCSTR (canonical), SVCSTR (CONFDATA)); + free (canonical); + return starts_with ? 1 + exists : 0; + } + + return 0; +} + +bool +authorize (int id, struct string_view filename, enum access_rights ar) +{ + bool ret = false; + struct passwd passwd = load_passwd_file (); + struct passwd_entry *entry = lookup_id (passwd, id); + if (!entry) { + fprintf (stderr, "user no longer exists, switch user"); + goto finish; + } + + struct acl *acl = load_acl_for_file (filename); + if (!acl) { + ret = true; + goto finish; + } + + if (acl->owner == id) goto authorized; + for (int i = 0; i < acl->grant_count; ++i) { + if (acl->grants[i].id != id) continue; + enum access_rights acl_ar = acl->grants[i].access_rights; + if ((ar & acl_ar) == ar) goto authorized; + else break; + } + + denied: + fprintf (stderr, "access denied '%.*s'\n", SVFMT (filename)); + string_builder_printf (&passwd_log, + "%u user '%.*s' (%d) was denied access %c%c%c to '%.*s'\n", + time (NULL), SVFMT (entry->login), entry->id, + ar & AR_READ ? 'r' : '-', + ar & AR_WRITE ? 'w' : '-', + ar & AR_DELETE ? 'd' : '-', SVFMT (filename)); + goto finish; + + authorized:; +#ifdef MAC + if ((ar & AR_READ ) && entry->security_level > acl->security_level) goto denied; + if ((ar & AR_WRITE) && entry->security_level < acl->security_level) goto denied; +#endif + + ret = true; + string_builder_printf (&passwd_log, + "%u user '%.*s' (%d) was granted access %c%c%c to '%.*s'\n", + time (NULL), SVFMT (entry->login), entry->id, + ar & AR_READ ? 'r' : '-', + ar & AR_WRITE ? 'w' : '-', + ar & AR_DELETE ? 'd' : '-', SVFMT (filename)); + + finish: + close_passwd_file (passwd); + return ret; +} + +void +created_conf_file (struct string_view filename) +{ + // TODO(cmmm): log + init_acl_for_file (filename, id); +} + +void +deleted_conf_file (struct string_view filename) +{ + const char *path = filename_to_acl_path (filename); + elevate (); + remove (path); + drop (); + // TODO(cmmm): log +} + +bool +copy_file (struct string_view source, struct string_view destination) +{ + int conf_source = is_conf (source); + int conf_destination = is_conf (destination); + + if (conf_source && !conf_destination) { + fprintf (stderr, "access denied: no one can copy out of confdata\n"); + return false; + } + + if (conf_source && !authorize (id, source, AR_READ )) return false; + if (conf_destination && !authorize (id, destination, AR_WRITE)) return false; + + bool ret = true; + if (conf_source) elevate (); + int source_fd = open (source.begin, O_RDONLY); + if (conf_source) drop (); + if (source_fd == -1) { + fprintf (stderr, "error opening file for reading '%.*s': %s\n", + SVFMT (source), strerror (errno)); + return false; + } + + struct stat st; + if (fstat (source_fd, &st) == -1) { + fprintf (stderr, "error getting file stats for '%.*s': %s\n", + SVFMT (source), strerror (errno)); + ret = false; + goto close_source; + } + + if (!S_ISREG (st.st_mode)) { + fprintf (stderr, "source file is not regular '%.*s'\n", SVFMT (source)); + ret = false; + goto close_source; + } + + if (conf_destination) elevate (); + int destination_fd = open (destination.begin, O_RDWR | O_CREAT | O_EXCL | + O_TRUNC, S_IRUSR | S_IWUSR); + if (conf_destination) drop (); + if (destination_fd == -1) { + fprintf (stderr, "error opening file for writing '%.*s': %s\n", + SVFMT (destination), strerror (errno)); + ret = false; + goto close_source; + } + + if (ftruncate (destination_fd, st.st_size) == -1) { + fprintf (stderr, "error setting file size for '%.*s': %s\n", + SVFMT (destination), strerror (errno)); + ret = false; + goto close_destination; + } + + void *source_map = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, source_fd, 0); + if (source_map == MAP_FAILED) { + fprintf (stderr, "error mapping source file '%.*s': %s\n", + SVFMT (source), strerror (errno)); + ret = false; + goto close_destination; + } + + void *destination_map = mmap (NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, destination_fd, 0); + if (destination_map == MAP_FAILED) { + fprintf (stderr, "error mapping destination file '%.*s': %s\n", + SVFMT (destination), strerror (errno)); + ret = false; + goto munmap_source; + } + + memcpy (destination_map, source_map, st.st_size); + + if (conf_destination) { + elevate (); + char *canonical = realpath (destination.begin, NULL); + drop (); + assert (canonical); + string_builder_printf (&confdata_log, "%u user %d wrote to file '!%s'\n", + time (NULL), id, canonical + strlen (CONFDATA)); + free (canonical); + + if (conf_destination != 2) + created_conf_file (destination); + } + + munmap (destination_map, st.st_size); + munmap_source: + munmap (source_map, st.st_size); + close_destination: + close (destination_fd); + close_source: + close (source_fd); + + elevate (); + if (conf_destination) chown (destination.begin, euid, egid); + else chown (destination.begin, ruid, rgid); + drop (); + + return ret; +} + +bool +rename_file (struct string_view source, struct string_view destination) +{ + int conf_source = is_conf (source); + int conf_destination = is_conf (destination); + + if (conf_source && !conf_destination) { + fprintf (stderr, "access denied: no one can rename out of confdata\n"); + return false; + } + + if (conf_source && !authorize (id, source, AR_READ | AR_DELETE)) return false; + if (conf_destination && !authorize (id, destination, AR_WRITE )) return false; + + struct stat st; + if (stat (source.begin, &st) == -1) { + fprintf (stderr, "error getting file stats for '%.*s': %s\n", + SVFMT (source), strerror (errno)); + return false; + } + + if (!S_ISREG (st.st_mode)) { + fprintf (stderr, "source file is not regular '%.*s'\n", SVFMT (source)); + return false; + } + + elevate (); + int res = access (destination.begin, F_OK); + drop (); + if (res == 0) { + fprintf (stderr, "destination file already exists '%.*s'\n", SVFMT (destination)); + return false; + } + + char *canonical = NULL; + if (conf_source || conf_destination) elevate (); + if (conf_source) canonical = realpath (source.begin, NULL); + res = rename (source.begin, destination.begin); + if (conf_source || conf_destination) drop (); + if (res == -1) { + fprintf (stderr, "error renaming '%.*s' to '%.*s': %s\n", + SVFMT (source), SVFMT (destination), strerror (errno)); + free (canonical); + return false; + } + + if (conf_destination) { + elevate (); + char *canonical = realpath (destination.begin, NULL); + drop (); + assert (canonical); + string_builder_printf (&confdata_log, "%u user %d wrote to file '!%s'\n", + time (NULL), id, canonical + strlen (CONFDATA)); + free (canonical); + + if (conf_destination != 2) + created_conf_file (destination); + } + + if (conf_source) { + assert (canonical); + string_builder_printf (&confdata_log, "%u user %d removed file '!%s'\n", + time (NULL), id, canonical + strlen (CONFDATA)); + free (canonical); + + deleted_conf_file (source); + } + + elevate (); + if (conf_destination) chown (destination.begin, euid, egid); + else chown (destination.begin, ruid, rgid); + drop (); + + return true; +} + +int +parse_command_line (struct command_line *cl, struct string_view line) +{ + DYNAMIC_ARRAY_RESET (cl->expanded); + DYNAMIC_ARRAY_RESET (*cl); + cl->command = SVNULL; + + // printf ("line: %.*s\n", SVFMT (line)); + + const char *head = line.begin; + const char *end = line.end; + for (; head < end; ++head) { + if (*head == ' ' || *head == '\t') { + do head += 1; + while (head < end && (*head == ' ' || *head == '\t')); + if (head == end) + break; + if (cl->expanded.length) + STRING_BUILDER_APPEND_CHAR (cl->expanded, '\0'); + } + + if (*head == '\'') { + head += 1; + if (head == end) return 0; + while (head < end && *head != '\'') + STRING_BUILDER_APPEND_CHAR (cl->expanded, *head++); + if (head == end && (head[-1] != '\'' || head - 1 == line.begin)) return 0; + } else if (*head == '"') { + head += 1; + if (head == end) return 0; + if (*head == '!') { + STRING_BUILDER_APPEND_CSTR (cl->expanded, CONFDATA); + head += 1; + } + while (head < end && *head != '"') + STRING_BUILDER_APPEND_CHAR (cl->expanded, *head++); + if (head == end && (head[-1] != '"' || head - 1 == line.begin)) return 0; + } else if (*head == '!' && (cl->expanded.length == 0 || DYNAMIC_ARRAY_LAST (cl->expanded) == '\0')) { + STRING_BUILDER_APPEND_CSTR (cl->expanded, CONFDATA); + } else { + STRING_BUILDER_APPEND_CHAR (cl->expanded, *head); + } + } STRING_BUILDER_APPEND_CHAR (cl->expanded, '\0'); + + head = cl->expanded.string; + end = cl->expanded.string + cl->expanded.length; + cl->command.begin = head; + while (head < end && *head != '\0') + head += 1; + cl->command.end = head++; + + if (SVLEN (cl->command) == 0) return 0; + + while (head < end) { + struct string_view arg; + arg.begin = head; + do head += 1; + while (head < end && *head != '\0'); + arg.end = head++; + + const char *star = strchr (arg.begin, '*'); + struct string_view trimmed = arg; + if (star) { + if (star == arg.begin) { + trimmed = SVCSTR ("."); + } else { + trimmed = SVDUP0 ((struct string_view) { + .begin = arg.begin, + .end = star, + }); + } + } + + bool conf = is_conf (trimmed); + glob_t globs; + if (conf) elevate (); + int ret = glob (arg.begin, GLOB_MARK | GLOB_NOSORT | GLOB_BRACE | GLOB_TILDE_CHECK, NULL, &globs); + if (conf) drop (); + if (ret != 0) { + DYNAMIC_ARRAY_APPEND (*cl, arg); + } else { + for (size_t i = 0; i < globs.gl_pathc; ++i) { + arg.begin = globs.gl_pathv[i]; + arg.end = arg.begin + strlen (arg.begin); + DYNAMIC_ARRAY_APPEND (*cl, SVDUP0 (arg)); + } + } + + globfree (&globs); + } + + // printf ("command: %.*s\n", SVFMT (cl->command)); + // for (uint32_t i = 0; i < cl->argc; ++i) + // printf ("arg: %.*s\n", SVFMT (cl->args[i])); + + return 1; +} + +int +command_exit (struct command_line cl) +{ + exit (0); +} + +int +command_help (struct command_line cl) +{ + puts ("Commands:"); + puts (" help display information about builtin commands"); + puts (" su LOGIN change user to LOGIN"); + puts (" pwd print the name of the current working directory "); + puts (" whoami print effective user name"); + puts (" echo [ARG]... write ARG(s) to the standard output"); + puts (" ls [FILE]... list information about the FILE(s)"); + puts (" cd [DIR] change the shell working directory"); + puts (" mv SOURCE DEST rename SOURCE to DEST"); + puts (" mv SOURCE... DIRECTORY move SOURCE(s) to DIRECTORY"); + puts (" cp SOURCE DEST copy SOURCE to DEST"); + puts (" cp SOURCE... DIRECTORY copy SOURCE(s) to DIRECTORY"); + puts (" rm FILE... remove (unlink) the FILE(s)"); + puts (" mkdir DIR... create the DIRECTORY(ies), if they do not already exist"); + puts (" rmdir DIR... remove the DIRECTORY(ies), if they are empty"); + puts (" cat [FILE]... concatenate FILE(s) to standard output"); + puts (" append FILE append text from standard input to FILE"); + puts (" chown LOGIN FILE... change owner of FILE(s) to LOGIN (root only)"); + puts (" chsl SECURITY-LEVEL FILE... change security level of FILE(s) to SECURITY-LEVEL (root only)"); + puts (" grant LOGIN {r|w|d}... FILE... grant specified access to FILE(s) for LOGIN"); + puts (" revoke LOGIN {r|w|d}... FILE... revoke specified access to FILE(s) from LOGIN"); + puts (""); + puts ("Aliases:"); + puts (" read -> cat"); + puts (" copy -> cp"); + puts (" remove -> rm"); + puts (""); + puts ("Security levels:"); + puts (" top-secret"); + puts (" secret"); + puts (" confidential"); + puts (" unclassified"); + puts (""); + return 0; +} + +int +command_su (struct command_line cl) +{ + if (cl.argc != 1) { + fprintf (stderr, "usage: %.*s LOGIN\n", SVFMT (cl.command)); + return 1; + } + + struct string_view login = cl.args[0]; + int ret = 0; + struct passwd passwd = load_passwd_file (); + struct passwd_entry *entry; + if (getuid () == 0) { + entry = lookup_login (passwd, login); + if (!entry) { + fprintf (stderr, "user %.*s doesn't exist\n", SVFMT (login)); + ret = 1; + goto close; + } + } else { + fputs ("Password: ", stdout); + struct string_view password = read_password (); + entry = authenticate (passwd, login, password); + if (!entry) { + ret = 1; + goto close; + } + } + id = entry->id; + + close: + close_passwd_file (passwd); + return ret; +} + +enum access_rights +parse_access_rights (struct string_view sv) +{ + enum access_rights ar = 0; + for (const char *head = sv.begin; head < sv.end; ++head) { + switch (*head) { + case 'r': ar |= AR_READ; continue; + case 'w': ar |= AR_WRITE; continue; + case 'd': ar |= AR_DELETE; continue; + default: return 0; + } + } + return ar; +} + +int +command_grant (struct command_line cl) +{ + static struct string_builder sb = {0}; + + if (cl.argc < 3) { + fprintf (stderr, "usage: %.*s LOGIN {r|w|d}... FILE...\n", SVFMT (cl.command)); + return 1; + } + + struct string_view login = cl.args[0]; + struct passwd passwd = load_passwd_file (); + struct passwd_entry *entry = lookup_login (passwd, login); + int login_id = entry ? entry->id : -1; + close_passwd_file (passwd); + + if (!entry) { + fprintf (stderr, "%.*s: user doesn't exist '%.*s'\n", + SVFMT (cl.command), SVFMT (login)); + return 1; + } + + enum access_rights access_rights = parse_access_rights (cl.args[1]); + if (!access_rights) { + fprintf (stderr, "%.*s: invalid access rights '%.*s'\n", + SVFMT (cl.command), SVFMT (cl.args[1])); + return 1; + } + + int ret = 0; + for (uint32_t i = 2; i < cl.argc; ++i) { + struct string_view file = cl.args[i]; + int conf = is_conf (file); + + if (conf == 0) { + fprintf (stderr, "%.*s: warning: skipping file outside of confdata '%.*s'\n", + SVFMT (cl.command), SVFMT (file)); + continue; + } + + if (conf == 1) { + fprintf (stderr, "%.*s: warning: skipping nonexistent file '%.*s'\n", + SVFMT (cl.command), SVFMT (file)); + continue; + } + + struct acl *acl = load_acl_for_file (file); + ASSERT (acl); + if (acl->owner != id && ruid != euid) { + fprintf (stderr, "%.*s: error: permission denied: not owner of the file '%.*s'\n", + SVFMT (cl.command), SVFMT (file)); + ret = 1; + continue; + } + + for (int i = 0; i < acl->grant_count; ++i) { + if (acl->grants[i].id != login_id) + continue; + + acl->grants[i].access_rights |= access_rights; + goto save; + } + acl->grants[acl->grant_count++] = (struct acl_grant) { + .id = login_id, + .access_rights = access_rights, + }; + + save: + save_acl (acl); + free (acl); + } + + return ret; +} + +int +command_revoke (struct command_line cl) +{ + static struct string_builder sb = {0}; + + if (cl.argc < 3) { + fprintf (stderr, "usage: %.*s LOGIN {r|w|d}... FILE...\n", SVFMT (cl.command)); + return 1; + } + + struct string_view login = cl.args[0]; + struct passwd passwd = load_passwd_file (); + struct passwd_entry *entry = lookup_login (passwd, login); + int login_id = entry ? entry->id : -1; + close_passwd_file (passwd); + + if (!entry) { + fprintf (stderr, "%.*s: user doesn't exist '%.*s'\n", + SVFMT (cl.command), SVFMT (login)); + return 1; + } + + enum access_rights access_rights = parse_access_rights (cl.args[1]); + if (!access_rights) { + fprintf (stderr, "%.*s: invalid access rights '%.*s'\n", + SVFMT (cl.command), SVFMT (cl.args[1])); + return 1; + } + + int ret = 0; + for (uint32_t i = 2; i < cl.argc; ++i) { + struct string_view file = cl.args[i]; + int conf = is_conf (file); + + if (conf == 0) { + fprintf (stderr, "%.*s: warning: skipping file outside of confdata '%.*s'\n", + SVFMT (cl.command), SVFMT (file)); + continue; + } + + if (conf == 1) { + fprintf (stderr, "%.*s: warning: skipping nonexistent file '%.*s'\n", + SVFMT (cl.command), SVFMT (file)); + continue; + } + + struct acl *acl = load_acl_for_file (file); + ASSERT (acl); + if (acl->owner != id && ruid != euid) { + fprintf (stderr, "%.*s: error: permission denied: not owner of the file '%.*s'\n", + SVFMT (cl.command), SVFMT (file)); + ret = 1; + continue; + } + + for (int i = 0; i < acl->grant_count; ++i) { + if (acl->grants[i].id != login_id) + continue; + + acl->grants[i].access_rights &= ~access_rights; + if (acl->grants[i].access_rights == 0) + acl->grants[i] = acl->grants[--acl->grant_count]; + + goto save; + } + + save: + save_acl (acl); + free (acl); + } + + return ret; +} + +int +command_chown (struct command_line cl) +{ + static struct string_builder sb = {0}; + + if (cl.argc < 2) { + fprintf (stderr, "usage: %.*s LOGIN FILE...\n", SVFMT (cl.command)); + return 1; + } + + if (euid != ruid) { + fprintf (stderr, "%.*s: error: only superuser can change file ownership\n", + SVFMT (cl.command)); + return 1; + } + + struct string_view login = cl.args[0]; + struct passwd passwd = load_passwd_file (); + struct passwd_entry *entry = lookup_login (passwd, login); + int login_id = entry ? entry->id : -1; + close_passwd_file (passwd); + + if (!entry) { + fprintf (stderr, "%.*s: user doesn't exist '%.*s'\n", + SVFMT (cl.command), SVFMT (login)); + return 1; + } + + int ret = 0; + for (uint32_t i = 1; i < cl.argc; ++i) { + struct string_view file = cl.args[i]; + int conf = is_conf (file); + + if (conf == 0) { + fprintf (stderr, "%.*s: warning: skipping file outside of confdata '%.*s'\n", + SVFMT (cl.command), SVFMT (file)); + continue; + } + + if (conf == 1) { + fprintf (stderr, "%.*s: warning: skipping nonexistent file '%.*s'\n", + SVFMT (cl.command), SVFMT (file)); + continue; + } + + struct acl *acl = load_acl_for_file (file); + ASSERT (acl); + acl->owner = login_id; + save_acl (acl); + free (acl); + } + + return ret; +} + +enum security_level +parse_security_level (struct string_view sv) +{ + for (enum security_level sl = 0; sl < SL_COUNT; ++sl) { + if (SVCMP (sv, SVCSTR (security_level_names[sl])) == 0) + return sl; + } + + return SL_COUNT; +} + +int +command_chsl (struct command_line cl) +{ + static struct string_builder sb = {0}; + + if (cl.argc < 2) { + fprintf (stderr, "usage: %.*s LOGIN FILE...\n", SVFMT (cl.command)); + return 1; + } + + if (euid != ruid) { + fprintf (stderr, "%.*s: error: only superuser can change file security level\n", + SVFMT (cl.command)); + return 1; + } + + enum security_level security_level = parse_security_level (cl.args[0]); + if (security_level == SL_COUNT) { + fprintf (stderr, "%.*s: error: invalid security level, use command 'help' to get listing of valid security levels\n", + SVFMT (cl.command)); + return 1; + } + + int ret = 0; + for (uint32_t i = 1; i < cl.argc; ++i) { + struct string_view file = cl.args[i]; + int conf = is_conf (file); + + if (conf == 0) { + fprintf (stderr, "%.*s: warning: skipping file outside of confdata '%.*s'\n", + SVFMT (cl.command), SVFMT (file)); + continue; + } + + if (conf == 1) { + fprintf (stderr, "%.*s: warning: skipping nonexistent file '%.*s'\n", + SVFMT (cl.command), SVFMT (file)); + continue; + } + + struct acl *acl = load_acl_for_file (file); + ASSERT (acl); + acl->security_level = security_level; + save_acl (acl); + free (acl); + } + + return ret; +} + +int +command_pwd (struct command_line cl) +{ + size_t size = 256; + + for (;;) { + char *buffer = (char *) malloc (size); + if (getcwd (buffer, size) == buffer) { + puts (buffer); + free (buffer); + return 0; + } + free (buffer); + if (errno != ERANGE) + return errno; + size *= 2; + } +} + +int +command_whoami (struct command_line cl) +{ + int ret = 0; + struct passwd passwd = load_passwd_file (); + struct passwd_entry *entry = lookup_id (passwd, id); + if (!entry) { + fprintf (stderr, "user with id %d no longed exists\n", id); + ret = 1; + goto close; + } + + printf ("%.*s", SVFMT (entry->login)); + +#ifdef MAC + printf (" (%s)", security_level_names[entry->security_level]); +#endif + + putchar ('\n'); + + close: + close_passwd_file (passwd); + return ret; +} + +int +command_echo (struct command_line cl) +{ + if (cl.argc == 0) + return 0; + + fputs (cl.args[0].begin, stdout); + for (uint32_t argind = 1; argind < cl.argc; ++argind) { + putchar (' '); + fputs (cl.args[argind].begin, stdout); + } putchar ('\n'); + + return 0; +} + +int +command_cd (struct command_line cl) +{ + struct string_view dir = cl.argc ? cl.args[0] : SVCSTR (CONFDATA); + bool conf = string_view_starts_with (dir, SVCSTR (CONFDATA)); + + if (conf) elevate (); + if (chdir (dir.begin) == -1) { + fprintf (stderr, "%.*s: cannot change directory to '%s': %s\n", + SVFMT (cl.command), dir.begin, strerror (errno)); + } + if (conf) drop (); + + return 0; +} + +bool +is_dir (struct string_view filename) +{ + struct stat st; + + elevate (); + int res = stat (filename.begin, &st); + drop (); + + if (res == -1) + return false; + + return S_ISDIR (st.st_mode); +} + +int +command_mv (struct command_line cl) +{ + static struct string_builder sb = {0}; + + if (cl.argc < 2) { + fprintf (stderr, "usage: %.*s SOURCE DEST\n", SVFMT (cl.command)); + fprintf (stderr, "or %.*s SOURCE... DIRECTORY\n", SVFMT (cl.command)); + return 1; + } + + int ret = 0; + + if (is_dir (cl.args[cl.argc - 1])) { + STRING_BUILDER_RESET (sb); + + struct string_view directory = cl.args[cl.argc - 1]; + STRING_BUILDER_APPEND_STRING_VIEW (sb, directory); + if (DYNAMIC_ARRAY_LAST (sb) != '/') + STRING_BUILDER_APPEND_CHAR (sb, '/'); + + size_t directory_length = sb.length; + for (uint32_t i = 0; i < cl.argc - 1; ++i) { + sb.length = directory_length; + struct string_view source = cl.args[i]; + + struct string_view basename = string_view_basename (source); + STRING_BUILDER_APPEND_STRING_VIEW (sb, basename); + STRING_BUILDER_TERMINATE (sb); + struct string_view destination = SVSB (sb); + + if (!rename_file (source, destination)) { + fprintf (stderr, "mv: failed to rename '%.*s' -> '%.*s'\n", + SVFMT (source), SVFMT (destination)); + ret = 1; + } + } + } else { + if (cl.argc != 2) { + fprintf (stderr, "usage: %.*s SOURCE DEST\n", SVFMT (cl.command)); + fprintf (stderr, "or %.*s SOURCE... DIRECTORY\n", SVFMT (cl.command)); + return 1; + } + + struct string_view source = cl.args[0]; + struct string_view destination = cl.args[1]; + if (!rename_file (source, destination)) { + fprintf (stderr, "mv: failed to rename '%.*s' -> '%.*s'\n", + SVFMT (source), SVFMT (destination)); + ret = 1; + } + } + + return ret; +} + +int +command_cp (struct command_line cl) +{ + static struct string_builder sb = {0}; + + if (cl.argc < 2) { + fprintf (stderr, "usage: %.*s SOURCE DEST\n", SVFMT (cl.command)); + fprintf (stderr, "or %.*s SOURCE... DIRECTORY\n", SVFMT (cl.command)); + return 1; + } + + int ret = 0; + + if (is_dir (cl.args[cl.argc - 1])) { + STRING_BUILDER_RESET (sb); + + struct string_view directory = cl.args[cl.argc - 1]; + STRING_BUILDER_APPEND_STRING_VIEW (sb, directory); + if (DYNAMIC_ARRAY_LAST (sb) != '/') + STRING_BUILDER_APPEND_CHAR (sb, '/'); + + size_t directory_length = sb.length; + for (uint32_t i = 0; i < cl.argc - 1; ++i) { + sb.length = directory_length; + struct string_view source = cl.args[i]; + + struct string_view basename = string_view_basename (source); + STRING_BUILDER_APPEND_STRING_VIEW (sb, basename); + STRING_BUILDER_TERMINATE (sb); + struct string_view destination = SVSB (sb); + + if (!copy_file (source, destination)) { + fprintf (stderr, "cp: failed to copy '%.*s' -> '%.*s'\n", + SVFMT (source), SVFMT (destination)); + ret = 1; + } + } + } else { + if (cl.argc != 2) { + fprintf (stderr, "usage: %.*s SOURCE DEST\n", SVFMT (cl.command)); + fprintf (stderr, "or %.*s SOURCE... DIRECTORY\n", SVFMT (cl.command)); + return 1; + } + + struct string_view source = cl.args[0]; + struct string_view destination = cl.args[1]; + if (!copy_file (source, destination)) { + fprintf (stderr, "cp: failed to copy '%.*s' -> '%.*s'\n", + SVFMT (source), SVFMT (destination)); + ret = 1; + } + } + + return ret; +} + +int +command_rm (struct command_line cl) +{ + if (cl.argc == 0) { + fprintf (stderr, "usage: %.*s DIR...\n", SVFMT (cl.command)); + return 1; + } + + int ret = 0; + for (uint32_t argind = 0; argind < cl.argc; ++argind) { + struct string_view file = cl.args[argind]; + const char *filename = file.begin; + + int conf = is_conf (file); + if (conf && !authorize (id, file, AR_DELETE)) { + ret = 1; + continue; + } + + char *canonical = NULL; + if (conf) { + elevate (); + canonical = realpath (filename, NULL); + } + int res = remove (filename); + if (conf) drop (); + if (res == -1) { + fprintf (stderr, "%.*s: cannot remove file '%s': %s\n", + SVFMT (cl.command), filename, strerror (errno)); + ret = 1; + free (canonical); + } else if (conf) { + assert (canonical); + string_builder_printf (&confdata_log, "%u user %d removed file '!%s'\n", + time (NULL), id, canonical + strlen (CONFDATA)); + free (canonical); + deleted_conf_file (file); + } + } + + return ret; +} + +int +command_mkdir (struct command_line cl) +{ + static struct string_builder sb = {0}; + + if (cl.argc == 0) { + fprintf (stderr, "usage: %.*s DIR...\n", SVFMT (cl.command)); + return 1; + } + + int ret = 0; + for (uint32_t argind = 0; argind < cl.argc; ++argind) { + struct string_view file = cl.args[argind]; + const char *filename = file.begin; + + bool conf = is_conf (file); + if (conf) { + STRING_BUILDER_RESET (sb); + STRING_BUILDER_APPEND_STRING_VIEW (sb, string_view_dirname (canonicalize (file))); + if (sb.string[sb.length - 1] != '/') + STRING_BUILDER_APPEND_CHAR (sb, '/'); + if (!authorize (id, SVSB (sb), AR_WRITE)) { + ret = 1; + continue; + } + } + + if (conf) elevate (); + int res = mkdir (filename, S_IRUSR | S_IWUSR | S_IXUSR); + if (conf) drop (); + if (res == -1) { + fprintf (stderr, "%.*s: cannot create directory '%s': %s\n", + SVFMT (cl.command), filename, strerror (errno)); + ret = 1; + } else if (conf) { + STRING_BUILDER_RESET (sb); + STRING_BUILDER_APPEND_STRING_VIEW (sb, file); + if (sb.string[sb.length - 1] != '/') + STRING_BUILDER_APPEND_CHAR (sb, '/'); + created_conf_file (SVSB (sb)); + elevate (); + char *canonical = realpath (filename, NULL); + drop (); + assert (canonical); + string_builder_printf (&confdata_log, "%u user %d created directory '!%s'\n", + time (NULL), id, canonical + strlen (CONFDATA)); + free (canonical); + } + } + + return ret; +} + +int +command_rmdir (struct command_line cl) +{ + static struct string_builder sb = {0}; + + if (cl.argc == 0) { + fprintf (stderr, "usage: %.*s DIR...\n", SVFMT (cl.command)); + return 1; + } + + int ret = 0; + for (uint32_t argind = 0; argind < cl.argc; ++argind) { + struct string_view file = cl.args[argind]; + const char *filename = file.begin; + + bool conf = is_conf (file); + if (conf) { + STRING_BUILDER_RESET (sb); + STRING_BUILDER_APPEND_STRING_VIEW (sb, canonicalize (file)); + if (sb.string[sb.length - 1] != '/') + STRING_BUILDER_APPEND_CHAR (sb, '/'); + if (!authorize (id, SVSB (sb), AR_DELETE)) { + ret = 1; + continue; + } + } + char *canonical = NULL; + if (conf) { + elevate (); + canonical = realpath (filename, NULL); + } + int res = rmdir (filename); + if (conf) drop (); + if (res == -1) { + fprintf (stderr, "%.*s: cannot remove directory '%s': %s\n", + SVFMT (cl.command), filename, strerror (errno)); + ret = 1; + free (canonical); + } else if (conf) { + assert (canonical); + string_builder_printf (&confdata_log, "%u user %d removed directory '!%s'\n", + time (NULL), id, canonical + strlen (CONFDATA)); + free (canonical); + + STRING_BUILDER_RESET (sb); + STRING_BUILDER_APPEND_STRING_VIEW (sb, file); + if (sb.string[sb.length - 1] != '/') + STRING_BUILDER_APPEND_CHAR (sb, '/'); + deleted_conf_file (SVSB (sb)); + } + } + + return ret; +} + +int +command_cat (struct command_line cl) +{ + int ret = 0; + for (uint32_t argind = 0; argind < cl.argc; ++argind) { + struct string_view file = cl.args[argind]; + bool conf = is_conf (file); + if (conf && !authorize (id, file, AR_READ)) continue; + + const char *filename = file.begin; + if (conf) elevate (); + int fd = open (filename, O_RDONLY); + if (conf) drop (); + if (fd == -1) { + fprintf (stderr, "%.*s: cannot access '%s': %s\n", + SVFMT (cl.command), filename, strerror (errno)); + ret = 1; + continue; + } + + struct stat st; + if (fstat (fd, &st) == -1) { + fprintf (stderr, "%.*s: cannot access '%s': %s\n", + SVFMT (cl.command), filename, strerror (errno)); + ret = 2; + goto close; + } + + void *contents = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if (contents == NULL) { + ret = 3; + goto close; + } + write (0, contents, st.st_size); + + if (conf) { + elevate (); + char *canonical = realpath (file.begin, NULL); + drop (); + assert (canonical); + string_builder_printf (&confdata_log, "%u user %d read file '!%s'\n", + time (NULL), id, canonical + strlen (CONFDATA)); + free (canonical); + } + + munmap (contents, st.st_size); + close: + close (fd); + } + + drop (); + + return ret; +} + +int +command_append (struct command_line cl) +{ + static struct string_builder sb = {0}; + + if (cl.argc != 1) { + fprintf (stderr, "usage: %.*s FILE\n", SVFMT (cl.command)); + return 1; + } + + struct string_view filename = cl.args[0]; + + int conf = is_conf (filename); + if (conf && !authorize (id, filename, AR_WRITE)) + return 1; + + STRING_BUILDER_RESET (sb); + char *line; + while ((line = readline ("append> ")) != NULL) { + STRING_BUILDER_APPEND_CSTR (sb, line); + STRING_BUILDER_APPEND_CHAR (sb, '\n'); + free (line); + } + + int ret = 0; + if (sb.length) { + if (conf && !authorize (id, filename, AR_WRITE)) + return 1; + + if (conf) elevate (); + int fd = open (filename.begin, O_WRONLY | O_CREAT | O_APPEND); + if (conf) drop (); + if (fd == -1) { + fprintf (stderr, "failed to open '%.*s' for writing\n", SVFMT (filename)); + return 1; + } + + write (fd, sb.string, sb.length); + + if (conf) { + elevate (); + char *canonical = realpath (filename.begin, NULL); + drop (); + assert (canonical); + string_builder_printf (&confdata_log, "%u user %d appended to file '!%s'\n", + time (NULL), id, canonical + strlen (CONFDATA)); + free (canonical); + + if (conf != 2) created_conf_file (filename); + } + + close (fd); + } + + return ret; +} + +#define COMMAND_LS_ALIGNMENT 30 + +int +command_ls (struct command_line cl) +{ + static struct string_builder sb = {0}; + + if (cl.argc == 0) + DYNAMIC_ARRAY_APPEND (cl, SVCSTR (".")); + + struct passwd passwd = load_passwd_file (); + + for (uint32_t argind = 0; argind < cl.argc; ++argind) { + struct string_view file = cl.args[argind]; + const char *filename = file.begin; + + bool conf = is_conf (file); + + if (conf) elevate (); + int fd = open (filename, O_RDONLY); + if (conf) drop (); + if (fd == -1) { + fprintf (stderr, "%.*s: cannot access '%s': %s\n", + SVFMT (cl.command), filename, strerror (errno)); + continue; + } + + struct stat st; + if (fstat (fd, &st) == -1) { + fprintf (stderr, "%.*s: cannot access '%s': %s\n", + SVFMT (cl.command), filename, strerror (errno)); + goto close; + } + + if (S_ISREG (st.st_mode)) { + int n = printf ("%s", filename); + if (conf) { + while (n++ < COMMAND_LS_ALIGNMENT) + putchar (' '); + struct acl *acl = load_acl_for_file (file); + +#ifdef MAC + fputs (security_level_names[acl->security_level], stdout); +#endif + + struct passwd_entry *entry = lookup_id (passwd, acl->owner); + printf (" %.*s", SVFMT (entry->login)); + for (int i = 0; i < acl->grant_count; ++i) { + entry = lookup_id (passwd, acl->grants[i].id); + enum access_rights ar = acl->grants[i].access_rights; + printf (" %.*s:%c%c%c", SVFMT (entry->login), + ar & AR_READ ? 'r' : '-', + ar & AR_WRITE ? 'w' : '-', + ar & AR_DELETE ? 'd' : '-'); + } + free (acl); + + elevate (); + char *canonical = realpath (filename, NULL); + drop (); + ASSERT (canonical); + string_builder_printf (&confdata_log, "%u user %d listed file '!%s'\n", + time (NULL), id, canonical + strlen (CONFDATA)); + free (canonical); + } + putchar ('\n'); + goto close; + } + + STRING_BUILDER_RESET (sb); + STRING_BUILDER_APPEND_STRING_VIEW (sb, canonicalize (file)); + if (sb.string[sb.length - 1] != '/') + STRING_BUILDER_APPEND_CHAR (sb, '/'); + if (conf && !authorize (id, SVSB (sb), AR_READ)) + continue; + + DIR *dir = fdopendir (fd); + if (dir == NULL) { + fprintf (stderr, "%.*s: cannot access '%s': %s\n", + SVFMT (cl.command), filename, strerror (errno)); + goto close; + } + + if (argind != 0) putchar ('\n'); + int n = printf ("%s:", filename); + if (conf) { + STRING_BUILDER_RESET (sb); + STRING_BUILDER_APPEND_STRING_VIEW (sb, canonicalize (file)); + if (sb.string[sb.length - 1] != '/') + STRING_BUILDER_APPEND_CHAR (sb, '/'); + struct acl *acl = load_acl_for_file (SVSB (sb)); + if (acl) { + while (n++ < COMMAND_LS_ALIGNMENT) + putchar (' '); + +#ifdef MAC + fputs (security_level_names[acl->security_level], stdout); +#endif + + struct passwd_entry *entry = lookup_id (passwd, acl->owner); + printf (" %.*s", SVFMT (entry->login)); + for (int i = 0; i < acl->grant_count; ++i) { + entry = lookup_id (passwd, acl->grants[i].id); + enum access_rights ar = acl->grants[i].access_rights; + printf (" %.*s:%c%c%c", SVFMT (entry->login), + ar & AR_READ ? 'r' : '-', + ar & AR_WRITE ? 'w' : '-', + ar & AR_DELETE ? 'd' : '-'); + } + free (acl); + } + } + putchar ('\n'); + struct dirent *entry; + while ((entry = readdir (dir)) != NULL) { + if (strcmp (entry->d_name, ".") == 0 || + strcmp (entry->d_name, "..") == 0) { + continue; + } + + if (entry->d_type == DT_DIR) { + n = printf ("%s/", entry->d_name); + } else { + n = printf ("%s", entry->d_name); + } + + if (conf) { + STRING_BUILDER_RESET (sb); + STRING_BUILDER_APPEND_STRING_VIEW (sb, file); + STRING_BUILDER_APPEND_CHAR (sb, '/'); + STRING_BUILDER_APPEND_CSTR (sb, entry->d_name); + if (entry->d_type == DT_DIR) + STRING_BUILDER_APPEND_CHAR (sb, '/'); + struct acl *acl = load_acl_for_file (SVSB (sb)); + if (acl) { + while (n++ < COMMAND_LS_ALIGNMENT) + putchar (' '); + +#ifdef MAC + fputs (security_level_names[acl->security_level], stdout); +#endif + + struct passwd_entry *entry = lookup_id (passwd, acl->owner); + printf (" %.*s", SVFMT (entry->login)); + for (int i = 0; i < acl->grant_count; ++i) { + entry = lookup_id (passwd, acl->grants[i].id); + enum access_rights ar = acl->grants[i].access_rights; + printf (" %.*s:%c%c%c", SVFMT (entry->login), + ar & AR_READ ? 'r' : '-', + ar & AR_WRITE ? 'w' : '-', + ar & AR_DELETE ? 'd' : '-'); + free (acl); + } + } + } + + putchar ('\n'); + } + + if (conf) { + elevate (); + char *canonical = realpath (filename, NULL); + drop (); + assert (canonical); + string_builder_printf (&confdata_log, "%u user %d listed directory '!%s'\n", + time (NULL), id, canonical + strlen (CONFDATA)); + free (canonical); + } + + closedir (dir); + close: + close (fd); + } + + close_passwd_file (passwd); + + return 0; +} + +const char * +prompt (int ret) +{ + static struct string_builder sb = {0}; + + STRING_BUILDER_RESET (sb); + STRING_BUILDER_APPEND_CSTR (sb, "\e[36m[\e[34m"); + + struct passwd passwd = load_passwd_file (); + struct passwd_entry *entry = lookup_id (passwd, id); + if (entry) { + STRING_BUILDER_APPEND_STRING_VIEW (sb, entry->login); + +#ifdef MAC + STRING_BUILDER_APPEND_CSTR (sb, "\e[36m@"); + STRING_BUILDER_APPEND_CSTR (sb, entry->security_level == SL_COUNT - 1 + ? "\e[34m" + : "\e[31m"); + STRING_BUILDER_APPEND_CSTR (sb, security_level_names[entry->security_level]); +#endif + } else STRING_BUILDER_APPEND_CHAR (sb, '?'); + close_passwd_file (passwd); + + STRING_BUILDER_APPEND_CSTR (sb, "\e[36m:\e[34m"); + struct string_view canonical = canonicalize (SVCSTR (".")); + if (strncmp (canonical.begin, CONFDATA, strlen (CONFDATA)) == 0) { + STRING_BUILDER_APPEND_CHAR (sb, '!'); + canonical.begin += strlen (CONFDATA); + } STRING_BUILDER_APPEND_STRING_VIEW (sb, canonical); + + STRING_BUILDER_APPEND_CSTR (sb, "\e[36m]\e[34m"); + + if (ret) string_builder_printf (&sb, " \e[31m%d", ret); + + STRING_BUILDER_APPEND_CSTR (sb, ruid == euid ? " \e[31m#" : " \e[95m$"); + STRING_BUILDER_APPEND_CSTR (sb, "\e[0m "); + STRING_BUILDER_TERMINATE (sb); + + return sb.string; +} + +int +main (int argc, char *argv[]) +{ + ruid = getuid (); + rgid = getgid (); + euid = geteuid (); + egid = getegid (); + drop (); + + static struct option longopts[] = { + { "help", no_argument, NULL, 'h' }, + { "password", required_argument, NULL, 'p' }, + {0} + }; + char ch; + struct string_view password = {0}; + while ((ch = getopt_long (argc, argv, ":hp:", longopts, NULL)) != -1) { + switch (ch) { + case 'h': + printf ("Usage: %s [options] [LOGIN]\n", argv[0]); + puts (""); + puts ("Options:"); + puts (" -h, --help display this help message and exit"); + puts (" -p, --password PASSWORD password of the account (only if login was provided)"); + puts (""); + exit (0); + case 'p': password = SVCSTR (optarg); break; + case '?': DIE ("unknown option %s", argv[optind - 1]); + case ':': DIE ("missing argument for %s", argv[optind - 1]); + default: DIE ("unexpected opt error"); + } + } + + struct passwd passwd = load_passwd_file (); + struct passwd_entry *entry = NULL; + if (argv[optind] != NULL) { + if (argv[optind + 1] != NULL) + DIE ("extra trailing arguments starting with: %s", argv[optind + 1]); + + struct string_view login = SVCSTR (argv[optind]); + if (ruid == 0) { + entry = lookup_login (passwd, login); + append_passwd_log (); + if (!entry) { + DIE ("user %.*s doesn't exist", SVFMT (login)); + } + } else { + if (password.begin == NULL) { + fputs ("Password: ", stdout); + password = read_password (); + } + entry = authenticate (passwd, login, password); + append_passwd_log (); + if (entry == NULL) exit (1); + } + } else { + do { + fputs ("Login: ", stdout); fflush (stdout); + struct string_view login = read_login (); + fputs ("Password: ", stdout); fflush (stdout); + struct string_view password = read_password (); + entry = authenticate (passwd, login, password); + } while (entry == NULL); + } + + id = entry->id; + printf ("Hello, %.*s! (%d)\n", SVFMT (entry->comment), id); + close_passwd_file (passwd); + printf ("Run 'help', to get a list of available commands.\n"); + + struct command_line cl = {0}; + int ret = 0; + for (;;) { + char *line = readline (prompt (ret)); + if (line == NULL) break; + add_history (line); + switch (parse_command_line (&cl, SVCSTR (line))) { + case 1: + if (SVCMP (cl.command, SVCSTR ("exit" )) == 0) ret = command_exit (cl); + else if (SVCMP (cl.command, SVCSTR ("help" )) == 0) ret = command_help (cl); + else if (SVCMP (cl.command, SVCSTR ("su" )) == 0) ret = command_su (cl); + else if (SVCMP (cl.command, SVCSTR ("pwd" )) == 0) ret = command_pwd (cl); + else if (SVCMP (cl.command, SVCSTR ("whoami")) == 0) ret = command_whoami (cl); + else if (SVCMP (cl.command, SVCSTR ("echo" )) == 0) ret = command_echo (cl); + else if (SVCMP (cl.command, SVCSTR ("ls" )) == 0) ret = command_ls (cl); + else if (SVCMP (cl.command, SVCSTR ("cd" )) == 0) ret = command_cd (cl); + else if (SVCMP (cl.command, SVCSTR ("mv" )) == 0) ret = command_mv (cl); + else if (SVCMP (cl.command, SVCSTR ("cp" )) == 0) ret = command_cp (cl); + else if (SVCMP (cl.command, SVCSTR ("rm" )) == 0) ret = command_rm (cl); + else if (SVCMP (cl.command, SVCSTR ("mkdir" )) == 0) ret = command_mkdir (cl); + else if (SVCMP (cl.command, SVCSTR ("rmdir" )) == 0) ret = command_rmdir (cl); + else if (SVCMP (cl.command, SVCSTR ("cat" )) == 0) ret = command_cat (cl); + else if (SVCMP (cl.command, SVCSTR ("read" )) == 0) ret = command_cat (cl); + else if (SVCMP (cl.command, SVCSTR ("copy" )) == 0) ret = command_cp (cl); + else if (SVCMP (cl.command, SVCSTR ("remove")) == 0) ret = command_rm (cl); + else if (SVCMP (cl.command, SVCSTR ("append")) == 0) ret = command_append (cl); + else if (SVCMP (cl.command, SVCSTR ("chown" )) == 0) ret = command_chown (cl); + else if (SVCMP (cl.command, SVCSTR ("chsl" )) == 0) ret = command_chsl (cl); + else if (SVCMP (cl.command, SVCSTR ("grant" )) == 0) ret = command_grant (cl); + else if (SVCMP (cl.command, SVCSTR ("revoke")) == 0) ret = command_revoke (cl); + else puts ("unknown command, run `help' to get the list of available commands"); + break; + case 0: break; + case -1: + putchar ('\n'); + exit (0); + default: + fputs ("invalid command line", stderr); + break; + } free (line); + append_passwd_log (); + flush_confdata_log (); + } putchar ('\n'); + + return EXIT_SUCCESS; +} + +// src/shell.c ends here -- cgit v1.3.1