// 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" int id; 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 append_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); } } 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); } bool is_conf (struct string_view filename) { elevate (); char *canonical = realpath (filename.begin, NULL); if (!canonical) { 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; } return false; } bool authorize_conf (int id, struct string_view filename, enum access_rights ar) { bool ret = true; struct passwd passwd = load_passwd_file (); struct passwd_entry *entry = lookup_id (passwd, id); if (!entry) { fprintf (stderr, "user no longer exists, switch user"); ret = false; goto finish; } if ((ar & entry->access_rights) != ar) { 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)); ret = false; goto finish; } 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; } bool authorize_reg (struct string_view filename, enum access_rights ar) { if (ar == 0) return true; int how = 0; how |= ar & AR_READ ? R_OK : 0; how |= ar & (AR_WRITE | AR_DELETE) ? W_OK : 0; if (access (filename.begin, how) == 0) return true; struct string_view dirname = SVDUP0 (string_view_dirname (filename)); int ret = access (dirname.begin, how); free ((void *) dirname.begin); if (ret == 0) return true; fprintf (stderr, "access denied '%.*s': %s\n", SVFMT (filename), strerror (errno)); return false; } bool authorize (int id, struct string_view filename, enum access_rights ar) { return is_conf (filename) ? authorize_conf (id, filename, ar) : authorize_reg (filename, ar); } bool authorize_known (int id, struct string_view filename, enum access_rights ar, bool conf) { return conf ? authorize_conf (id, filename, ar) : authorize_reg (filename, ar); } bool copy_file (struct string_view source, struct string_view destination) { bool conf_source = is_conf (source); bool 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 (!authorize_known (id, source, AR_READ, conf_source )) return false; if (!authorize_known (id, destination, AR_WRITE, conf_destination)) 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); } 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) { bool conf_source = is_conf (source); bool 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 (!authorize_known (id, source, AR_READ | AR_DELETE, conf_source)) return false; if (!authorize_known (id, destination, AR_WRITE, conf_destination)) 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_source) { assert (canonical); string_builder_printf (&confdata_log, "%u user %d removed file '!%s'\n", time (NULL), id, canonical + strlen (CONFDATA)); free (canonical); } 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); if (conf) elevate (); glob_t globs; if (glob (arg.begin, GLOB_MARK | GLOB_NOSORT | GLOB_BRACE | GLOB_TILDE_CHECK, NULL, &globs) != 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)); } } if (conf) drop (); 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 (""); puts ("Aliases:"); puts (" read -> cat"); puts (" copy -> cp"); puts (" remove -> rm"); 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; } 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 ("%d %.*s (%.*s) %c%c%c\n", id, SVFMT (entry->login), SVFMT (entry->comment), entry->access_rights & AR_READ ? 'r' : '-', entry->access_rights & AR_WRITE ? 'w' : '-', entry->access_rights & AR_DELETE ? 'd' : '-'); 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) { 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])) { struct string_builder sb = {0}; 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); struct string_view destination = SVSB (sb); STRING_BUILDER_APPEND_NULL (sb); if (!rename_file (source, destination)) { fprintf (stderr, "mv: failed to rename '%.*s' -> '%.*s'\n", SVFMT (source), SVFMT (destination)); ret = 1; } } STRING_BUILDER_FREE (sb); } 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) { 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])) { struct string_builder sb = {0}; 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); struct string_view destination = SVSB (sb); STRING_BUILDER_APPEND_NULL (sb); if (!copy_file (source, destination)) { fprintf (stderr, "cp: failed to copy '%.*s' -> '%.*s'\n", SVFMT (source), SVFMT (destination)); ret = 1; } } STRING_BUILDER_FREE (sb); } 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; bool conf = is_conf (file); if (!authorize_known (id, file, AR_DELETE, conf)) { 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); } } return ret; } int command_mkdir (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) { const char *filename = cl.args[argind].begin; bool conf = is_conf (cl.args[argind]); if (!authorize_known (id, cl.args[argind], AR_WRITE, conf)) { 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) { 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) { 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) { const char *filename = cl.args[argind].begin; bool conf = is_conf (cl.args[argind]); if (!authorize_known (id, cl.args[argind], AR_DELETE, conf)) { 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); } } 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 (!authorize_known (id, file, AR_READ, conf)) 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; int res = fstat (fd, &st); if (res == -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]; bool conf = is_conf (filename); if (!authorize_known (id, filename, AR_WRITE, conf)) 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 (!authorize_known (id, filename, AR_WRITE, conf)) 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); } close (fd); } return 0; } int command_ls (struct command_line cl) { if (cl.argc == 0) DYNAMIC_ARRAY_APPEND (cl, SVCSTR (".")); 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 (!authorize_known (id, file, AR_READ, conf)) continue; 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)) { printf ("%s\n", filename); if (conf) { 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); } goto close; } 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'); printf ("%s:\n", filename); 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) printf ("%s/\n", entry->d_name); else printf ("%s\n", entry->d_name); } 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); } 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 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 (); append_confdata_log (); } putchar ('\n'); return EXIT_SUCCESS; } // src/shell.c ends here