diff options
| author | 2026-11-11 05:31:06 +0000 | |
|---|---|---|
| committer | 2026-11-11 06:14:42 +0000 | |
| commit | 9e1d304cac5367c3dbb1ca14700736680bd3a804 (patch) | |
| tree | 31f328747acb934d365d423adfda144818ff5ad4 /03/src/common.h | |
| download | lab-01-05-9e1d304cac5367c3dbb1ca14700736680bd3a804.tar.gz | |
Diffstat (limited to '03/src/common.h')
| -rw-r--r-- | 03/src/common.h | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/03/src/common.h b/03/src/common.h new file mode 100644 index 0000000..c94763d --- /dev/null +++ b/03/src/common.h | |||
| @@ -0,0 +1,119 @@ | |||
| 1 | // common.h --- common functions | ||
| 2 | // | ||
| 3 | // Copyright (c) 2026 Martin Michalec <martin@michalec.dev> | ||
| 4 | // | ||
| 5 | // SPDX-License-Identifier: MIT | ||
| 6 | // | ||
| 7 | // Commentary: | ||
| 8 | // | ||
| 9 | // Code: | ||
| 10 | |||
| 11 | #ifndef INCLUDED_COMMON_H | ||
| 12 | #define INCLUDED_COMMON_H | ||
| 13 | |||
| 14 | #include <stddef.h> | ||
| 15 | #include <stdint.h> | ||
| 16 | #include <stdio.h> | ||
| 17 | #include <stdbool.h> | ||
| 18 | |||
| 19 | #include <openssl/sha.h> | ||
| 20 | |||
| 21 | #include "string-view.h" | ||
| 22 | #include "string-builder.h" | ||
| 23 | |||
| 24 | #define PREFIX "/usr/local/practice3" | ||
| 25 | |||
| 26 | #define STRINGIFY(ARG) #ARG | ||
| 27 | #define TO_STRING(ARG) STRINGIFY (ARG) | ||
| 28 | |||
| 29 | #define DIE(FMT, ...) do { \ | ||
| 30 | fprintf (stderr, "fatal: " FMT "\n", ## __VA_ARGS__); \ | ||
| 31 | exit (EXIT_FAILURE); \ | ||
| 32 | } while (0) | ||
| 33 | |||
| 34 | #define LOGIN_MAX_LENGTH 32 | ||
| 35 | #define LOGIN_REGEX "[a-zA-Z][-_a-zA-Z0-9]*" | ||
| 36 | |||
| 37 | #define COMMENT_MAX_LENGTH 256 | ||
| 38 | #define COMMENT_REGEX "[^:\n]*" | ||
| 39 | |||
| 40 | #define PASSWORD_MAX_LENGTH 256 | ||
| 41 | #define PASSWORD_SALT_SIZE 12 | ||
| 42 | #define PASSWORD_SALT_LENGTH 16 | ||
| 43 | #define PASSWORD_HASH_LENGTH 27 | ||
| 44 | #define PASSWORD_ENCODED_LENGTH (3 + PASSWORD_SALT_LENGTH + 1 + PASSWORD_HASH_LENGTH) | ||
| 45 | #define PASSWORD_REGEX "[a-zA-Z][a-zA-Z0-9!@#$%^&*()]*" | ||
| 46 | |||
| 47 | extern struct string_builder passwd_log; | ||
| 48 | |||
| 49 | extern uid_t ruid; | ||
| 50 | extern gid_t rgid; | ||
| 51 | extern uid_t euid; | ||
| 52 | extern gid_t egid; | ||
| 53 | |||
| 54 | enum access_rights { | ||
| 55 | AR_READ = 1 << 0, | ||
| 56 | AR_WRITE = 1 << 1, | ||
| 57 | AR_DELETE = 1 << 2, | ||
| 58 | }; | ||
| 59 | |||
| 60 | enum security_level { | ||
| 61 | SL_TOP_SECRET, | ||
| 62 | SL_SECRET, | ||
| 63 | SL_CONFIDENTIAL, | ||
| 64 | SL_UNCLASSIFIED, | ||
| 65 | SL_COUNT | ||
| 66 | }; | ||
| 67 | |||
| 68 | extern const char *security_level_names[SL_COUNT]; | ||
| 69 | |||
| 70 | struct passwd_entry { | ||
| 71 | struct string_view login; | ||
| 72 | struct string_view encoded_password; | ||
| 73 | enum security_level security_level; | ||
| 74 | int id; | ||
| 75 | struct string_view comment; | ||
| 76 | }; | ||
| 77 | |||
| 78 | struct passwd { | ||
| 79 | struct passwd_entry *items; | ||
| 80 | uint32_t count; | ||
| 81 | uint32_t capacity; | ||
| 82 | FILE *stream; | ||
| 83 | }; | ||
| 84 | |||
| 85 | char encode6 (unsigned char b6); | ||
| 86 | unsigned char decode6 (char c); | ||
| 87 | void encode (char *into, const unsigned char* from, unsigned int size); | ||
| 88 | void decode (unsigned char *into, struct string_view from); | ||
| 89 | |||
| 90 | void generate_salt (unsigned char salt[PASSWORD_SALT_SIZE]); | ||
| 91 | unsigned char *hash_password (unsigned char hash[SHA_DIGEST_LENGTH], | ||
| 92 | struct string_view password, | ||
| 93 | const unsigned char salt[PASSWORD_SALT_SIZE]); | ||
| 94 | struct string_view encode_password (struct string_view password); | ||
| 95 | bool check_password (struct string_view password, | ||
| 96 | struct string_view encoded_password); | ||
| 97 | |||
| 98 | int fscan_passwd_entry (FILE *, struct passwd_entry *); | ||
| 99 | void fprint_passwd_entry (FILE *, const struct passwd_entry *); | ||
| 100 | struct passwd load_passwd_file (void); | ||
| 101 | void save_passwd_file (struct passwd); | ||
| 102 | void close_passwd_file (struct passwd); | ||
| 103 | |||
| 104 | struct string_view read_password (void); | ||
| 105 | struct string_view read_login (void); | ||
| 106 | |||
| 107 | void append_passwd_log (void); | ||
| 108 | |||
| 109 | struct passwd_entry *lookup_login (struct passwd, struct string_view login); | ||
| 110 | struct passwd_entry *lookup_id (struct passwd, int id); | ||
| 111 | |||
| 112 | struct passwd_entry *authenticate (struct passwd, struct string_view login, struct string_view password); | ||
| 113 | |||
| 114 | void elevate (void); | ||
| 115 | void drop (void); | ||
| 116 | |||
| 117 | #endif // INCLUDED_COMMON_H | ||
| 118 | |||
| 119 | // common.h ends here | ||
