aboutsummaryrefslogtreecommitdiff
path: root/03/src/common.h
diff options
context:
space:
mode:
Diffstat (limited to '03/src/common.h')
-rw-r--r--03/src/common.h119
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
47extern struct string_builder passwd_log;
48
49extern uid_t ruid;
50extern gid_t rgid;
51extern uid_t euid;
52extern gid_t egid;
53
54enum access_rights {
55 AR_READ = 1 << 0,
56 AR_WRITE = 1 << 1,
57 AR_DELETE = 1 << 2,
58};
59
60enum security_level {
61 SL_TOP_SECRET,
62 SL_SECRET,
63 SL_CONFIDENTIAL,
64 SL_UNCLASSIFIED,
65 SL_COUNT
66};
67
68extern const char *security_level_names[SL_COUNT];
69
70struct 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
78struct passwd {
79 struct passwd_entry *items;
80 uint32_t count;
81 uint32_t capacity;
82 FILE *stream;
83};
84
85char encode6 (unsigned char b6);
86unsigned char decode6 (char c);
87void encode (char *into, const unsigned char* from, unsigned int size);
88void decode (unsigned char *into, struct string_view from);
89
90void generate_salt (unsigned char salt[PASSWORD_SALT_SIZE]);
91unsigned char *hash_password (unsigned char hash[SHA_DIGEST_LENGTH],
92 struct string_view password,
93 const unsigned char salt[PASSWORD_SALT_SIZE]);
94struct string_view encode_password (struct string_view password);
95bool check_password (struct string_view password,
96 struct string_view encoded_password);
97
98int fscan_passwd_entry (FILE *, struct passwd_entry *);
99void fprint_passwd_entry (FILE *, const struct passwd_entry *);
100struct passwd load_passwd_file (void);
101void save_passwd_file (struct passwd);
102void close_passwd_file (struct passwd);
103
104struct string_view read_password (void);
105struct string_view read_login (void);
106
107void append_passwd_log (void);
108
109struct passwd_entry *lookup_login (struct passwd, struct string_view login);
110struct passwd_entry *lookup_id (struct passwd, int id);
111
112struct passwd_entry *authenticate (struct passwd, struct string_view login, struct string_view password);
113
114void elevate (void);
115void drop (void);
116
117#endif // INCLUDED_COMMON_H
118
119// common.h ends here