aboutsummaryrefslogtreecommitdiff
path: root/03/src/passwdctl.c
diff options
context:
space:
mode:
Diffstat (limited to '03/src/passwdctl.c')
-rw-r--r--03/src/passwdctl.c466
1 files changed, 466 insertions, 0 deletions
diff --git a/03/src/passwdctl.c b/03/src/passwdctl.c
new file mode 100644
index 0000000..b9e07a7
--- /dev/null
+++ b/03/src/passwdctl.c
@@ -0,0 +1,466 @@
1// src/passwdctl.c --- utility to manage access to protected directory
2//
3// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
4//
5// SPDX-License-Identifier: MIT
6//
7// Commentary:
8//
9// Code:
10
11#include <stdbool.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <assert.h>
16#include <getopt.h>
17#include <regex.h>
18#include <unistd.h>
19
20#include <openssl/evp.h>
21#include <openssl/err.h>
22
23#include "dynamic-array.h"
24#include "string-builder.h"
25#include "string-view.h"
26#include "common.h"
27
28#define SUBCOMMANDS() \
29 X (ADD, "add") \
30 X (MODIFY, "mod") \
31 X (DELETE, "del")
32
33#define X(SYM, STR) SUBCOMMAND_##SYM,
34enum subcommand {
35 SUBCOMMANDS ()
36};
37#undef X
38
39#define X(SYM, STR) [SUBCOMMAND_##SYM] = STR,
40char *const subcommand_tokens[] = {
41 SUBCOMMANDS ()
42 NULL
43};
44#undef X
45
46regex_t login_regex;
47regex_t comment_regex;
48regex_t password_regex;
49
50struct string_view
51parse_password (struct string_view sv)
52{
53 if (regexec (&password_regex, sv.begin, 0, NULL, 0) != 0)
54 DIE ("password doesn't match regex %s", PASSWORD_REGEX);
55
56 return sv;
57}
58
59int
60parse_id (const char *cstr)
61{
62 int id;
63 size_t read;
64 sscanf (cstr, "%d%ln", &id, &read);
65
66 if (id < 0) DIE ("invalid id");
67 if (read != strlen (cstr)) DIE ("invalid id");
68
69 return id;
70}
71
72enum security_level
73parse_security_level (const char *cstr)
74{
75 for (enum security_level sl = 0; sl < SL_COUNT; ++sl)
76 if (strcmp (cstr, security_level_names[sl]) == 0) return sl;
77
78 DIE ("invalid security level: %s", cstr);
79}
80
81struct string_view
82parse_comment (const char *cstr)
83{
84 struct string_view sv = SVCSTR (cstr);
85
86 if (SVLEN (sv) > COMMENT_MAX_LENGTH)
87 DIE ("comment too long");
88
89 if (regexec (&comment_regex, cstr, 0, NULL, 0) != 0)
90 DIE ("comment doesn't match regex " COMMENT_REGEX);
91
92 return sv;
93}
94
95struct string_view
96parse_login (const char *cstr)
97{
98 struct string_view sv = SVCSTR (cstr);
99
100 if (SVLEN (sv) > LOGIN_MAX_LENGTH)
101 DIE ("login too long");
102
103 if (regexec (&login_regex, cstr, 0, NULL, 0) != 0)
104 DIE ("login doesn't match regex " LOGIN_REGEX);
105
106 return sv;
107}
108
109const char *
110regex_error (const regex_t *comp, int errcode)
111{
112 static char *error = NULL;
113 size_t size = regerror (errcode, comp, NULL, 0);
114 error = realloc (error, size);
115 regerror (errcode, comp, error, size);
116 return error;
117}
118
119void
120usage (const char *progname)
121{
122 printf ("Usage: %s [options] COMMAND [ARGS]\n", progname);
123 puts ("");
124 puts ("Options:");
125 puts (" -h, --help display this help message and exit");
126 puts ("");
127 puts ("Commands:");
128 puts (" add LOGIN add new account");
129 puts (" mod LOGIN modify existing account");
130 puts (" del LOGIN delete account");
131 puts ("");
132}
133
134int
135main (int argc, char *argv[])
136{
137 ruid = getuid ();
138 euid = geteuid ();
139 seteuid (ruid);
140
141 ERR_load_crypto_strings ();
142
143 const char *progname = argv[0];
144 if (progname == NULL)
145 DIE ("unable to get program name");
146
147 char **subcommand = &argv[1];
148 if (*subcommand == NULL) {
149 usage (progname);
150 exit (1);
151 }
152
153 int errcode;
154 errcode = regcomp (&login_regex, "^" LOGIN_REGEX "$", REG_NOSUB);
155 if (errcode != 0) {
156 DIE ("unable to compile login regex: %s",
157 regex_error (&login_regex, errcode));
158 }
159
160 errcode = regcomp (&password_regex, "^" PASSWORD_REGEX "$", REG_NOSUB);
161 if (errcode != 0) {
162 DIE ("unable to compile password regex: %s",
163 regex_error (&password_regex, errcode));
164 }
165
166 errcode = regcomp (&comment_regex, "^" COMMENT_REGEX "$", REG_NOSUB);
167 if (errcode != 0) {
168 DIE ("unable to compile comment regex: %s",
169 regex_error (&comment_regex, errcode));
170 }
171
172 char *value;
173 switch (getsubopt (subcommand, subcommand_tokens, &value)) {
174 case SUBCOMMAND_ADD: {
175 static struct option longopts[] = {
176 { "help", no_argument, NULL, 'h' },
177 { "password", required_argument, NULL, 'p' },
178 { "id", required_argument, NULL, 'i' },
179 { "security-level", required_argument, NULL, 's' },
180 { "comment", required_argument, NULL, 'c' },
181 {0}
182 };
183 char ch;
184 struct passwd_entry new_entry = { .id = -1, .security_level = SL_COUNT - 1 };
185 optind = 2;
186 while ((ch = getopt_long (argc, argv, ":hp:i:s:c:", longopts, NULL)) != -1) {
187 switch (ch) {
188 case 'h':
189 printf ("Usage: %s add [options] LOGIN\n", progname);
190 puts ("");
191 puts ("Options:");
192 puts (" -h, --help display this help message and exit");
193 puts (" -p, --password PASSWORD password of the new account");
194 puts (" -i, --id ID ID of the new account");
195 puts (" -s, --security-level SECURITY-LEVEL security level of the new account");
196 puts (" -c, --comment COMMENT comment of the new account");
197 puts ("");
198 puts ("Security levels:");
199 puts (" top-secret");
200 puts (" secret");
201 puts (" confidential");
202 puts (" unclassified");
203 puts ("");
204 exit (0);
205 case 'p':
206 new_entry.encoded_password =
207 SVDUP (encode_password (parse_password (SVCSTR (optarg))));
208 break;
209 case 'i': new_entry.id = parse_id (optarg); break;
210 case 's': new_entry.security_level = parse_security_level (optarg); break;
211 case 'c': new_entry.comment = SVDUP (parse_comment (optarg)); break;
212 case '?': DIE ("unknown option -%c", optopt);
213 case ':': DIE ("missing argument for %s", argv[optind - 1]);
214 default: DIE ("unexpected opt error");
215 }
216 }
217
218 if (argv[optind] == NULL)
219 DIE ("no login provided");
220
221 if (argv[optind + 1] != NULL)
222 DIE ("extra trailing arguments starting with: %s", argv[optind + 1]);
223
224 if (getuid () != 0)
225 DIE ("permission denied");
226
227 new_entry.login = SVDUP (SVCSTR (argv[optind]));
228
229 if (new_entry.encoded_password.begin == NULL) {
230 fputs ("Password: ", stdout);
231 struct string_view password = duplicate_string_view_as_cstr (parse_password (read_password ()));
232 fputs ("Repeat password: ", stdout);
233 struct string_view repeated = read_password ();
234 if (string_view_compare (password, repeated) != 0)
235 DIE ("passwords don't match");
236
237 new_entry.encoded_password = SVDUP (encode_password (password));
238 }
239
240 struct passwd passwd = load_passwd_file ();
241
242 int max_id = -1;
243 for (struct passwd_entry *entry = passwd.items;
244 entry < passwd.items + passwd.count; ++entry) {
245 if (SVCMP (new_entry.login, entry->login) == 0)
246 DIE ("user %.*s already exists", SVFMT (entry->login));
247 if (new_entry.id == entry->id)
248 DIE ("user with id %d already exists", entry->id);
249 if (entry->id > max_id)
250 max_id = entry->id;
251 }
252
253 if (new_entry.id < 0)
254 new_entry.id = max_id + 1;
255
256 DYNAMIC_ARRAY_APPEND (passwd, new_entry);
257 save_passwd_file (passwd);
258
259 string_builder_printf (&passwd_log, "%u added user %.*s %d",
260 time (NULL), SVFMT (new_entry.login), new_entry.id);
261 if (new_entry.comment.begin)
262 string_builder_printf (&passwd_log, " (%.*s)", SVFMT (new_entry.comment));
263 string_builder_printf (&passwd_log, " with security level %s\n",
264 security_level_names[new_entry.security_level]);
265
266 close_passwd_file (passwd);
267 } break;
268
269 case SUBCOMMAND_MODIFY: { // [-p --password=] [-i -id=] [-c --comment=] login
270 static struct option longopts[] = {
271 { "help", no_argument, NULL, 'h' },
272 { "login", required_argument, NULL, 'l' },
273 { "password", required_argument, NULL, 'p' },
274 { "id", required_argument, NULL, 'i' },
275 { "security-level", required_argument, NULL, 's' },
276 { "comment", required_argument, NULL, 'c' },
277 {0}
278 };
279 char ch;
280 struct passwd_entry new_entry = { .id = -1, .security_level = SL_COUNT };
281 bool prompt_for_new_password = false;
282 optind = 2;
283 while ((ch = getopt_long (argc, argv, ":hl:p:Pi:s:c:", longopts, NULL)) != -1) {
284 switch (ch) {
285 case 'h':
286 printf ("Usage: %s mod [options] LOGIN\n", progname);
287 puts ("");
288 puts ("Options:");
289 puts (" -h, --help display this help message and exit");
290 puts (" -l, --login NEW_LOGIN new login for the account");
291 puts (" -p, --password PASSWORD new password for the account");
292 puts (" -P prompt for new password for the account");
293 puts (" -i, --id ID new ID for the account");
294 puts (" -s, --security-level SECURITY-LEVEL new access rights for the account");
295 puts (" -c, --comment COMMENT new comment for the account");
296 puts ("");
297 puts ("Security levels:");
298 puts (" top-secret");
299 puts (" secret");
300 puts (" confidential");
301 puts (" unclassified");
302 puts ("");
303 exit (0);
304 case 'l': new_entry.login = SVDUP (parse_login (optarg)); break;
305 case 'p':
306 new_entry.encoded_password =
307 SVDUP (encode_password (parse_password (SVCSTR (optarg))));
308 break;
309 case 'P': prompt_for_new_password = true; break;
310 case 'i': new_entry.id = parse_id (optarg); break;
311 case 's': new_entry.security_level = parse_security_level (optarg); break;
312 case 'c': new_entry.comment = SVDUP (parse_comment (optarg)); break;
313 case '?': DIE ("unknown option -%c", optopt);
314 case ':': DIE ("missing argument for %s", argv[optind - 1]);
315 default: DIE ("unexpected opt error");
316 }
317 }
318
319 if (argv[optind] == NULL)
320 DIE ("no login provided");
321
322 if (argv[optind + 1] != NULL)
323 DIE ("extra trailing arguments starting with: %s", argv[optind + 1]);
324
325 struct string_view login = SVCSTR (argv[optind]);
326 struct passwd_entry *entry;
327
328 struct passwd passwd = load_passwd_file ();
329
330 if (getuid () == 0) {
331 entry = lookup_login (passwd, login);
332 if (!entry)
333 DIE ("user %.*s not found", SVFMT (login));
334
335 string_builder_printf (&passwd_log, "%u modified user %.*s with id %d:",
336 time (NULL), SVFMT (entry->login), entry->id);
337
338 if (new_entry.id != -1) {
339 if (lookup_id (passwd, new_entry.id))
340 DIE ("user with id %d already exists", new_entry.id);
341 entry->id = new_entry.id;
342 string_builder_printf (&passwd_log, " new id %d,", entry->id);
343 }
344
345 if (new_entry.security_level != SL_COUNT) {
346 entry->security_level = new_entry.security_level;
347 string_builder_printf (&passwd_log, " new security level %s,",
348 security_level_names[entry->security_level]);
349 }
350 } else {
351 if (new_entry.id != -1)
352 DIE ("permission to change id denied");
353 if (new_entry.security_level != SL_COUNT)
354 DIE ("permission to change security level denied");
355
356 fputs ("Password: ", stdout);
357 struct string_view password = parse_password (read_password ());
358 entry = authenticate (passwd, login, password);
359 if (!entry) exit (1);
360 string_builder_printf (&passwd_log, "%u modified user %.*s with id %d:",
361 time (NULL), SVFMT (entry->login), entry->id);
362 }
363
364
365 if (prompt_for_new_password) {
366 fputs ("New password: ", stdout);
367 struct string_view password = SVDUP (parse_password (read_password ()));
368 fputs ("Repeat password: ", stdout);
369 struct string_view repeated = read_password ();
370 if (string_view_compare (password, repeated) != 0)
371 DIE ("passwords don't match");
372 entry->encoded_password = SVDUP (encode_password (password));
373 string_builder_printf (&passwd_log, " changed password,");
374 } else if (new_entry.encoded_password.begin != NULL) {
375 entry->encoded_password = new_entry.encoded_password;
376 string_builder_printf (&passwd_log, " changed password,");
377 }
378
379 if (new_entry.comment.begin != NULL) {
380 entry->comment = new_entry.comment;
381 string_builder_printf (&passwd_log, " new comment `%.*s',", SVFMT (entry->comment));
382 }
383
384 if (new_entry.login.begin != NULL) {
385 entry->login = new_entry.login;
386 string_builder_printf (&passwd_log, " new login %.*s,", SVFMT (entry->login));
387 }
388
389 if (DYNAMIC_ARRAY_LAST (passwd_log) != ',')
390 DIE ("no modifications supplied");
391 DYNAMIC_ARRAY_LAST (passwd_log) = '\n';
392
393 save_passwd_file (passwd);
394 close_passwd_file (passwd);
395 } break;
396
397 case SUBCOMMAND_DELETE: { // login
398 static struct option longopts[] = {
399 { "help", no_argument, NULL, 'h' },
400 {0}
401 };
402
403 char ch;
404 optind = 2;
405 while ((ch = getopt_long (argc, argv, ":h", longopts, NULL)) != -1) {
406 switch (ch) {
407 case 'h':
408 printf ("Usage: %s del [options] LOGIN\n", progname);
409 puts ("");
410 puts ("Options:");
411 puts (" -h, --help display this help message and exit");
412 puts ("");
413 exit (0);
414 case '?': DIE ("unknown option -%c", optopt);
415 case ':': DIE ("missing argument for %s", argv[optind - 1]);
416 default: DIE ("unexpected opt error");
417 }
418 }
419
420 if (argv[optind] == NULL)
421 DIE ("no login");
422
423 if (argv[optind + 1] != NULL)
424 DIE ("extra trailing arguments starting with: %s", argv[optind + 1]);
425
426 if (getuid () != 0)
427 DIE ("permission denied");
428
429 struct string_view login = SVDUP (SVCSTR (argv[optind]));
430 struct passwd passwd = load_passwd_file ();
431
432 struct passwd_entry *entry;
433 for (entry = passwd.items; entry < passwd.items + passwd.count; ++entry) {
434 if (SVCMP (login, entry->login) == 0)
435 break;
436 }
437
438 if (entry == passwd.items + passwd.count)
439 DIE ("user %.*s doesn't exist", SVFMT (login));
440
441 string_builder_printf (&passwd_log, "%u deleted user %.*s with id %d",
442 time (NULL), SVFMT (entry->login), entry->id);
443 if (entry->comment.begin)
444 string_builder_printf (&passwd_log, " (%.*s)", SVFMT (entry->comment));
445 DYNAMIC_ARRAY_APPEND (passwd_log, '\n');
446
447 if (entry + 1 != passwd.items + passwd.count) {
448 memmove (entry, entry + 1,
449 (passwd.count - (entry + 1 - passwd.items))*sizeof (*entry));
450 } passwd.count -= 1;
451
452 save_passwd_file (passwd);
453 close_passwd_file (passwd);
454 } break;
455
456 default:
457 usage (progname);
458 exit (strcmp (value, "-h") != 0 && strcmp (value, "--help") != 0);
459 }
460
461 append_passwd_log ();
462
463 return EXIT_SUCCESS;
464}
465
466// src/passwdctl.c ends here