aboutsummaryrefslogtreecommitdiff
path: root/02/src/common.h
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 05:31:06 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 06:14:42 +0000
commit9e1d304cac5367c3dbb1ca14700736680bd3a804 (patch)
tree31f328747acb934d365d423adfda144818ff5ad4 /02/src/common.h
downloadlab-01-05-9e1d304cac5367c3dbb1ca14700736680bd3a804.tar.gz
publishHEADmaster
Diffstat (limited to '02/src/common.h')
-rw-r--r--02/src/common.h107
1 files changed, 107 insertions, 0 deletions
diff --git a/02/src/common.h b/02/src/common.h
new file mode 100644
index 0000000..0edc629
--- /dev/null
+++ b/02/src/common.h
@@ -0,0 +1,107 @@
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/practice2"
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_OPT__ (,) __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
54struct passwd_entry {
55 struct string_view login;
56 struct string_view encoded_password;
57 int id;
58 enum access_rights {
59 AR_READ = 1 << 0,
60 AR_WRITE = 1 << 1,
61 AR_DELETE = 1 << 2,
62 } access_rights;
63 struct string_view comment;
64};
65
66struct passwd {
67 struct passwd_entry *items;
68 uint32_t count;
69 uint32_t capacity;
70 FILE *stream;
71};
72
73char encode6 (unsigned char b6);
74unsigned char decode6 (char c);
75void encode (char *into, const unsigned char* from, unsigned int size);
76void decode (unsigned char *into, struct string_view from);
77
78void generate_salt (unsigned char salt[PASSWORD_SALT_SIZE]);
79unsigned char *hash_password (unsigned char hash[SHA_DIGEST_LENGTH],
80 struct string_view password,
81 const unsigned char salt[PASSWORD_SALT_SIZE]);
82struct string_view encode_password (struct string_view password);
83bool check_password (struct string_view password,
84 struct string_view encoded_password);
85
86int fscan_passwd_entry (FILE *, struct passwd_entry *);
87void fprint_passwd_entry (FILE *, const struct passwd_entry *);
88struct passwd load_passwd_file (void);
89void save_passwd_file (struct passwd);
90void close_passwd_file (struct passwd);
91
92struct string_view read_password (void);
93struct string_view read_login (void);
94
95void append_passwd_log (void);
96
97struct passwd_entry *lookup_login (struct passwd, struct string_view login);
98struct passwd_entry *lookup_id (struct passwd, int id);
99
100struct passwd_entry *authenticate (struct passwd, struct string_view login, struct string_view password);
101
102void elevate (void);
103void drop (void);
104
105#endif // INCLUDED_COMMON_H
106
107// common.h ends here