From 9e1d304cac5367c3dbb1ca14700736680bd3a804 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Wed, 11 Nov 2026 08:31:06 +0300 Subject: publish --- 02/src/common.c | 463 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 463 insertions(+) create mode 100644 02/src/common.c (limited to '02/src/common.c') diff --git a/02/src/common.c b/02/src/common.c new file mode 100644 index 0000000..05a0e49 --- /dev/null +++ b/02/src/common.c @@ -0,0 +1,463 @@ +#include "common.h" + +#include +#include +#include + +#include +#include + +struct string_builder passwd_log = {0}; + +uid_t ruid; +gid_t rgid; +uid_t euid; +gid_t egid; + +char +encode6 (unsigned char b6) +{ + if (b6 < ('9' - '.' + 1) ) return '.' + b6; + if (b6 < ('9' - '.' + 1) + ('Z' - 'A' + 1) ) return 'A' + b6 - ('9' - '.' + 1); + if (b6 < ('9' - '.' + 1) + ('Z' - 'A' + 1) + ('z' - 'a' + 1)) return 'a' + b6 - ('9' - '.' + 1) - ('Z' - 'A' + 1); + printf ("0x%X\n", b6); + assert (!"out of range"); + return -1; +} + +unsigned char +decode6 (char c) +{ + if ('.' <= c && c <= '9') return c - '.'; + if ('A' <= c && c <= 'Z') return c - 'A' + ('9' - '.' + 1); + if ('a' <= c && c <= 'z') return c - 'a' + ('9' - '.' + 1) + ('Z' - 'A' + 1); + assert (!"out of range"); + return -1; +} + +void +encode (char *into, const unsigned char* from, unsigned int size) +{ + for (; size >= 3; size -= 3) { + // 012345670123456701234567 + // 012345012345012345012345 + into[0] = encode6 ( from[0] >> 2); + into[1] = encode6 ((from[0] & 0b00000011) << 4 | from[1] >> 4); + into[2] = encode6 ((from[1] & 0b00001111) << 2 | from[2] >> 6); + into[3] = encode6 ((from[2] & 0b00111111)); + + from += 3; + into += 4; + } + if (size == 2) { + into[0] = encode6 ( from[0] >> 2); + into[1] = encode6 ((from[0] & 0b00000011) << 4 | from[1] >> 4); + into[2] = encode6 ((from[1] & 0b00001111) << 2); + } else if (size == 1) { + into[0] = encode6 ( from[0] >> 2); + into[1] = encode6 ((from[0] & 0b00000011) << 4); + } +} + +void +decode (unsigned char *into, struct string_view from) +{ + size_t len; + + for (len = SVLEN (from); len >= 4; len -= 4) { + // 012345012345012345012345 + // 012345670123456701234567 + + unsigned char d1 = decode6 (from.begin[0]); + unsigned char d2 = decode6 (from.begin[1]); + unsigned char d3 = decode6 (from.begin[2]); + unsigned char d4 = decode6 (from.begin[3]); + + into[0] = d1 << 2 | d2 >> 4; + into[1] = (d2 & 0b001111) << 4 | d3 >> 2; + into[2] = d3 << 6 | d4; + + from.begin += 4; + into += 3; + } + if (len == 3) { + unsigned char d1 = decode6 (from.begin[0]); + unsigned char d2 = decode6 (from.begin[1]); + unsigned char d3 = decode6 (from.begin[2]); + + into[0] = d1 << 2 | d2 >> 4; + into[1] = (d2 & 0b001111) << 4 | d3 >> 2; + } else if (len == 2) { + unsigned char d1 = decode6 (from.begin[0]); + unsigned char d2 = decode6 (from.begin[1]); + + into[0] = d1 << 2 | d2 >> 4; + } else assert ("!unreachable"); +} + +void +generate_salt (unsigned char salt[PASSWORD_SALT_SIZE]) +{ + if (RAND_bytes (salt, PASSWORD_SALT_SIZE) != 1) { + DIE ("error generating salt: RAND_bytes: %s", + ERR_reason_error_string (ERR_get_error ())); + } +} + +unsigned char * +hash_password (unsigned char hash[SHA_DIGEST_LENGTH], + struct string_view password, + const unsigned char salt[PASSWORD_SALT_SIZE]) +{ + EVP_MD_CTX *mdctx = EVP_MD_CTX_new (); + if (mdctx == NULL) { + DIE ("error hashing creating context: EVP_MD_CTX_new: %s", + ERR_reason_error_string (ERR_get_error ())); + } + + if (EVP_DigestInit_ex (mdctx, EVP_sha1 (), NULL) != 1) { + DIE ("error initializing digest: EVP_DigestInit_ex: %s", + ERR_reason_error_string (ERR_get_error ())); + } + + if (EVP_DigestUpdate (mdctx, password.begin, SVLEN (password)) != 1) { + DIE ("error updating digest with password: EVP_DigestUpdate: %s", + ERR_reason_error_string (ERR_get_error ())); + } + + if (EVP_DigestUpdate (mdctx, salt, PASSWORD_SALT_SIZE) != 1) { + DIE ("error updating digest with salt: EVP_DigestUpdate: %s", + ERR_reason_error_string (ERR_get_error ())); + } + + unsigned int hash_len; + if (EVP_DigestFinal_ex(mdctx, hash, &hash_len) != 1) { + DIE ("error finalizing digest: EVP_DigestFinal_ex: %s", + ERR_reason_error_string (ERR_get_error ())); + } + + EVP_MD_CTX_free (mdctx); + + return hash; +} + +struct string_view +encode_password (struct string_view password) +{ + static char encoded[PASSWORD_ENCODED_LENGTH + 1] = { + [0] = '$', + [1] = '1', + [2] = '$', + [3 + PASSWORD_SALT_LENGTH] = '$', + }; + + char *salt_encoded = (void *) &encoded[3]; + char *hash_encoded = (void *) &encoded[3 + PASSWORD_SALT_LENGTH + 1]; + + unsigned char salt[PASSWORD_SALT_SIZE]; + generate_salt (salt); + encode (salt_encoded, salt, sizeof (salt)); + + unsigned char hash[SHA_DIGEST_LENGTH]; + hash_password (hash, password, salt); + encode (hash_encoded, hash, SHA_DIGEST_LENGTH); + + return (struct string_view) { encoded, encoded + PASSWORD_ENCODED_LENGTH }; +} + +bool +check_password (struct string_view password, + struct string_view encoded_password) +{ + if (*encoded_password.begin++ != '$') return false; + if (*encoded_password.begin++ != '1') return false; + if (*encoded_password.begin++ != '$') return false; + + struct string_view encoded_salt = { + .begin = encoded_password.begin, + .end = encoded_password.begin += PASSWORD_SALT_LENGTH, + }; + if (*encoded_password.begin++ != '$') return false; + + struct string_view encoded_hash = { + .begin = encoded_password.begin, + .end = encoded_password.begin + PASSWORD_HASH_LENGTH, + }; + + unsigned char decoded_salt[PASSWORD_SALT_SIZE]; + decode (decoded_salt, encoded_salt); + + unsigned char hash[SHA_DIGEST_LENGTH]; + hash_password (hash, password, decoded_salt); + + unsigned char decoded_hash[SHA_DIGEST_LENGTH]; + decode (decoded_hash, encoded_hash); + + return memcmp (hash, decoded_hash, sizeof (hash)) == 0; +} + +int +fscan_passwd_entry (FILE *stream, struct passwd_entry *entry) +{ + *entry = (struct passwd_entry) {0}; + + int c; + do c = getc (stream); + while (c == '\n'); + if (c == EOF) + return 0; + ungetc (c, stream); + + char buffer[256 + 1]; + + fscanf (stream, "%32[a-zA-Z0-9]", buffer); + if (getc (stream) != ':') return -1; + entry->login = string_view_strdup (buffer); + + fread (buffer, 1, PASSWORD_ENCODED_LENGTH, stream); + if (getc (stream) != ':') return -1; + buffer[PASSWORD_ENCODED_LENGTH] = '\0'; + entry->encoded_password = string_view_strdup (buffer); + + fscanf (stream, "%d", &entry->id); + if (getc (stream) != ':') return -1; + + fscanf (stream, "%d", (int *) &entry->access_rights); + if (entry->access_rights > 0b111) return -1; + if (getc (stream) != ':') return -1; + + int len = 0; + fscanf (stream, "%256[^:\n]%n", buffer, &len); + if (len) + entry->comment = SVDUP0 ((struct string_view) { buffer, buffer + len }); + + return 1; +} + +void +fprint_passwd_entry (FILE *stream, const struct passwd_entry *entry) +{ + if (entry->id < 0) + DIE ("id < 0"); + + struct string_view comment; + if (entry->comment.begin) { + comment.begin = entry->comment.begin; + comment.end = entry->comment.end; + } else { + comment.begin = ""; + comment.end = comment.begin; + } + + fprintf (stream, "%.*s:%.*s:%d:%d:%.*s\n", + SVFMT (entry->login), + SVFMT (entry->encoded_password), + entry->id, + entry->access_rights, + SVFMT (comment)); +} + +void +lock_passwd_file (struct passwd passwd) +{ + if (lockf (fileno (passwd.stream), F_TLOCK, 0) != 0) + DIE ("unable to lock passwd file: %s", strerror (errno)); +} + +void +unlock_passwd_file (struct passwd passwd) +{ + if (lockf (fileno (passwd.stream), F_ULOCK, 0) != 0) + DIE ("unable to unlock passwd file: %s", strerror (errno)); +} + +struct passwd +load_passwd_file (void) +{ + elevate (); + struct passwd passwd = { + .stream = fopen (PREFIX"/etc/passwd", "r+"), + }; + drop (); + + if (passwd.stream == NULL) + DIE ("unable to open passwd file: %s", strerror (errno)); + + if (lockf (fileno (passwd.stream), F_TEST, 0) != 0) + DIE ("passwd file is locked: %s", strerror (errno)); + + for (int i = 0;; ++i) { + struct passwd_entry entry; + int ret = fscan_passwd_entry (passwd.stream, &entry); + if (ret == -1) + DIE (PREFIX"/etc/passwd:%d: failed to parse entry", i); + if (ret == 0) + break; + DYNAMIC_ARRAY_APPEND (passwd, entry); + } + + return passwd; +} + +void +save_passwd_file (struct passwd passwd) +{ + ftruncate (fileno (passwd.stream), 0); + rewind (passwd.stream); + for (struct passwd_entry *entry = passwd.items; + entry < passwd.items + passwd.count; ++entry) { + fprint_passwd_entry (passwd.stream, entry); + } fflush (passwd.stream); +} + +void +close_passwd_file (struct passwd passwd) +{ + for (uint32_t i = 0; i < passwd.count; ++i) { + free ((void *) passwd.items[i].login.begin); + free ((void *) passwd.items[i].encoded_password.begin); + free ((void *) passwd.items[i].comment.begin); + } + DYNAMIC_ARRAY_FREE (passwd); + + if (lockf (fileno (passwd.stream), F_ULOCK, 0) != 0) + DIE ("unable to unlock passwd file: %s", strerror (errno)); + + fclose (passwd.stream); +} + +struct string_view +read_password () +{ + static char password[PASSWORD_MAX_LENGTH + 2]; + + struct termios term_orig; + tcgetattr (fileno (stdin), &term_orig); + struct termios term_noecho = term_orig; + term_noecho.c_lflag &= ~ECHO; + tcsetattr (fileno (stdin), 0, &term_noecho); + + if (fgets (password, sizeof (password), stdin) == NULL) { + tcsetattr (fileno (stdin), 0, &term_orig); + putchar ('\n'); + exit (0); + } tcsetattr (fileno (stdin), 0, &term_orig); + + putchar ('\n'); + size_t len = strlen (password) - 1; + if (password[len] != '\n') + DIE ("password too long"); + password[len] = '\0'; + + return (struct string_view) { password, password + len }; +} + +struct string_view +read_login () +{ + static char login[LOGIN_MAX_LENGTH + 2]; + + if (fgets (login, sizeof (login), stdin) == NULL) { + putchar ('\n'); + exit (0); + } + + size_t len = strlen (login) - 1; + if (login[len] != '\n') + DIE ("login too long"); + login[len] = '\0'; + + return (struct string_view) { login, login + len }; +} + +struct passwd_entry * +authenticate (struct passwd passwd, + struct string_view login, + struct string_view password) +{ + struct passwd_entry *entry = lookup_login (passwd, login); + + if (!entry) { + fprintf (stderr, "user %.*s not found\n", SVFMT (login)); + return NULL; + } + + if (!check_password (password, entry->encoded_password)) { + fputs ("authentication failure\n", stderr); + string_builder_printf (&passwd_log, "%u user %.*s supplied incorrect password\n", + time (NULL), SVFMT (entry->login)); + return NULL; + } + + string_builder_printf (&passwd_log, "%u authenticated user %.*s %d\n", + time (NULL), SVFMT (entry->login), entry->id); + + return entry; +} + +void +append_passwd_log () +{ + if (passwd_log.count) { + elevate (); + int fd = open (PREFIX"/log/passwd.log", O_CREAT | O_WRONLY | O_APPEND); + drop (); + write (fd, passwd_log.items, passwd_log.count); + close (fd); + DYNAMIC_ARRAY_RESET (passwd_log); + } +} + +struct passwd_entry * +lookup_login (struct passwd passwd, struct string_view login) +{ + for (struct passwd_entry *entry = passwd.items; + entry < passwd.items + passwd.count; ++entry) { + if (SVCMP (login, entry->login) != 0) + continue; + + string_builder_printf (&passwd_log, "%u user '%.*s' (%d) was looked up\n", + time (NULL), SVFMT (entry->login), entry->id); + + return entry; + } + + string_builder_printf (&passwd_log, "%u user '%.*s' lookup failed\n", + time (NULL), SVFMT (login)); + + return NULL; +} + +struct passwd_entry * +lookup_id (struct passwd passwd, int id) +{ + for (struct passwd_entry *entry = passwd.items; + entry < passwd.items + passwd.count; ++entry) { + if (id != entry->id) + continue; + + string_builder_printf (&passwd_log, "%u user %d was looked up\n", + time (NULL), id); + + return entry; + } + + string_builder_printf (&passwd_log, "%u user %d lookup failed\n", + time (NULL), id); + + return NULL; +} + +void +elevate (void) +{ + if (euid != ruid) seteuid (euid); + if (egid != rgid) setegid (egid); +} + +void +drop (void) +{ + if (euid != ruid) seteuid (ruid); + if (egid != rgid) setegid (rgid); +} -- cgit v1.3.1