aboutsummaryrefslogtreecommitdiff
path: root/02/src/passwdctl.c
diff options
context:
space:
mode:
Diffstat (limited to '02/src/passwdctl.c')
-rw-r--r--02/src/passwdctl.c493
1 files changed, 493 insertions, 0 deletions
diff --git a/02/src/passwdctl.c b/02/src/passwdctl.c
new file mode 100644
index 0000000..f0ab1a8
--- /dev/null
+++ b/02/src/passwdctl.c
@@ -0,0 +1,493 @@
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 access_rights
73parse_access_right (const char *cstr)
74{
75 enum access_rights ar = 0;
76
77 while (*cstr) {
78 switch (*cstr++) {
79 case 'r': ar |= AR_READ; break;
80 case 'w': ar |= AR_WRITE; break;
81 case 'd': ar |= AR_DELETE; break;
82 default: DIE ("invalid access right: %c", cstr[-1]);
83 }
84 }
85
86 return ar;
87}
88
89struct string_view
90parse_comment (const char *cstr)
91{
92 struct string_view sv = SVCSTR (cstr);
93
94 if (SVLEN (sv) > COMMENT_MAX_LENGTH)
95 DIE ("comment too long");
96
97 if (regexec (&comment_regex, cstr, 0, NULL, 0) != 0)
98 DIE ("comment doesn't match regex " COMMENT_REGEX);
99
100 return sv;
101}
102
103struct string_view
104parse_login (const char *cstr)
105{
106 struct string_view sv = SVCSTR (cstr);
107
108 if (SVLEN (sv) > LOGIN_MAX_LENGTH)
109 DIE ("login too long");
110
111 if (regexec (&login_regex, cstr, 0, NULL, 0) != 0)
112 DIE ("login doesn't match regex " LOGIN_REGEX);
113
114 return sv;
115}
116
117const char *
118regex_error (const regex_t *comp, int errcode)
119{
120 static char *error = NULL;
121 size_t size = regerror (errcode, comp, NULL, 0);
122 error = realloc (error, size);
123 regerror (errcode, comp, error, size);
124 return error;
125}
126
127void
128usage (const char *progname)
129{
130 printf ("Usage: %s [options] COMMAND [ARGS]\n", progname);
131 puts ("");
132 puts ("Options:");
133 puts (" -h, --help display this help message and exit");
134 puts ("");
135 puts ("Commands:");
136 puts (" add LOGIN add new account");
137 puts (" mod LOGIN modify existing account");
138 puts (" del LOGIN delete account");
139 puts ("");
140}
141
142void
143usage_add (const char *progname)
144{
145 printf ("Usage: %s add [options] LOGIN\n", progname);
146 puts ("");
147 puts ("Options:");
148 puts (" -h, --help display this help message and exit");
149 puts (" -p, --password PASSWORD password of the new account");
150 puts (" -i, --id ID ID of the new account");
151 puts (" -a, --access-rights {r|w|d}... access right of the new account");
152 puts (" -c, --comment COMMENT comment of the new account");
153 puts ("");
154}
155
156void
157usage_mod (const char *progname)
158{
159 printf ("Usage: %s mod [options] LOGIN\n", progname);
160 puts ("");
161 puts ("Options:");
162 puts (" -h, --help display this help message and exit");
163 puts (" -l, --login NEW_LOGIN new login for the account");
164 puts (" -p, --password PASSWORD new password for the account");
165 puts (" -P prompt for new password for the account");
166 puts (" -i, --id ID new ID for the account");
167 puts (" -a, --access-rights {r|w|d}... new access rights for the account");
168 puts (" -c, --comment COMMENT new comment for the account");
169 puts ("");
170}
171
172void
173usage_del (const char *progname)
174{
175 printf ("Usage: %s del [options] LOGIN\n", progname);
176 puts ("");
177 puts ("Options:");
178 puts (" -h, --help display this help message and exit");
179 puts ("");
180}
181
182int
183main (int argc, char *argv[])
184{
185 ruid = getuid ();
186 euid = geteuid ();
187 seteuid (ruid);
188
189 ERR_load_crypto_strings ();
190
191 const char *progname = argv[0];
192 if (progname == NULL)
193 DIE ("unable to get program name");
194
195 char **subcommand = &argv[1];
196 if (*subcommand == NULL) {
197 usage (progname);
198 exit (1);
199 }
200
201 int errcode;
202 errcode = regcomp (&login_regex, "^" LOGIN_REGEX "$", REG_NOSUB);
203 if (errcode != 0) {
204 DIE ("unable to compile login regex: %s",
205 regex_error (&login_regex, errcode));
206 }
207
208 errcode = regcomp (&password_regex, "^" PASSWORD_REGEX "$", REG_NOSUB);
209 if (errcode != 0) {
210 DIE ("unable to compile password regex: %s",
211 regex_error (&password_regex, errcode));
212 }
213
214 errcode = regcomp (&comment_regex, "^" COMMENT_REGEX "$", REG_NOSUB);
215 if (errcode != 0) {
216 DIE ("unable to compile comment regex: %s",
217 regex_error (&comment_regex, errcode));
218 }
219
220 char *value;
221 switch (getsubopt (subcommand, subcommand_tokens, &value)) {
222 case SUBCOMMAND_ADD: {
223 static struct option longopts[] = {
224 { "help", no_argument, NULL, 'h' },
225 { "password", required_argument, NULL, 'p' },
226 { "id", required_argument, NULL, 'i' },
227 { "access-rights", required_argument, NULL, 'a' },
228 { "comment", required_argument, NULL, 'c' },
229 {0}
230 };
231 char ch;
232 struct passwd_entry new_entry = { .id = -1 };
233 optind = 2;
234 while ((ch = getopt_long (argc, argv, ":hp:i:a:c:", longopts, NULL)) != -1) {
235 switch (ch) {
236 case 'h':
237 usage_add (progname);
238 exit (0);
239 case 'p':
240 new_entry.encoded_password =
241 SVDUP (encode_password (parse_password (SVCSTR (optarg))));
242 break;
243 case 'i': new_entry.id = parse_id (optarg); break;
244 case 'a': new_entry.access_rights = parse_access_right (optarg); break;
245 case 'c': new_entry.comment = SVDUP (parse_comment (optarg)); break;
246 case '?': DIE ("unknown option -%c", optopt);
247 case ':': DIE ("missing argument for %s", argv[optind - 1]);
248 default: DIE ("unexpected opt error");
249 }
250 }
251
252 if (argv[optind] == NULL) {
253 fprintf (stderr, "error: no login provided\n");
254 usage_add (progname);
255 exit (1);
256 }
257
258 if (argv[optind + 1] != NULL)
259 DIE ("extra trailing arguments starting with: %s", argv[optind + 1]);
260
261 if (getuid () != 0)
262 DIE ("permission denied");
263
264 new_entry.login = SVDUP (SVCSTR (argv[optind]));
265
266 if (new_entry.encoded_password.begin == NULL) {
267 fputs ("Password: ", stdout);
268 struct string_view password = duplicate_string_view_as_cstr (parse_password (read_password ()));
269 fputs ("Repeat password: ", stdout);
270 struct string_view repeated = read_password ();
271 if (string_view_compare (password, repeated) != 0)
272 DIE ("passwords don't match");
273
274 new_entry.encoded_password = SVDUP (encode_password (password));
275 }
276
277 struct passwd passwd = load_passwd_file ();
278
279 int max_id = -1;
280 for (struct passwd_entry *entry = passwd.items;
281 entry < passwd.items + passwd.count; ++entry) {
282 if (SVCMP (new_entry.login, entry->login) == 0)
283 DIE ("user %.*s already exists", SVFMT (entry->login));
284 if (new_entry.id == entry->id)
285 DIE ("user with id %d already exists", entry->id);
286 if (entry->id > max_id)
287 max_id = entry->id;
288 }
289
290 if (new_entry.id < 0)
291 new_entry.id = max_id + 1;
292
293 DYNAMIC_ARRAY_APPEND (passwd, new_entry);
294 save_passwd_file (passwd);
295
296 string_builder_printf (&passwd_log, "%u added user %.*s %d",
297 time (NULL), SVFMT (new_entry.login), new_entry.id);
298 if (new_entry.comment.begin)
299 string_builder_printf (&passwd_log, " (%.*s)", SVFMT (new_entry.comment));
300 string_builder_printf (&passwd_log, " with access rights %c%c%c\n",
301 new_entry.access_rights & AR_READ ? 'r' : '-',
302 new_entry.access_rights & AR_WRITE ? 'w' : '-',
303 new_entry.access_rights & AR_DELETE ? 'd' : '-');
304
305 close_passwd_file (passwd);
306 } break;
307
308 case SUBCOMMAND_MODIFY: { // [-p --password=] [-i -id=] [-c --comment=] login
309 static struct option longopts[] = {
310 { "help", no_argument, NULL, 'h' },
311 { "login", required_argument, NULL, 'l' },
312 { "password", required_argument, NULL, 'p' },
313 { "id", required_argument, NULL, 'i' },
314 { "access-rights", required_argument, NULL, 'a' },
315 { "comment", required_argument, NULL, 'c' },
316 {0}
317 };
318 char ch;
319 struct passwd_entry new_entry = { .id = -1 };
320 bool prompt_for_new_password = false;
321 optind = 2;
322 while ((ch = getopt_long (argc, argv, ":hl:p:Pi:a:c:", longopts, NULL)) != -1) {
323 switch (ch) {
324 case 'h':
325 usage_mod (progname);
326 exit (0);
327 case 'l': new_entry.login = SVDUP (parse_login (optarg)); break;
328 case 'p':
329 new_entry.encoded_password =
330 SVDUP (encode_password (parse_password (SVCSTR (optarg))));
331 break;
332 case 'P': prompt_for_new_password = true; break;
333 case 'i': new_entry.id = parse_id (optarg); break;
334 case 'a': new_entry.access_rights = parse_access_right (optarg); break;
335 case 'c': new_entry.comment = SVDUP (parse_comment (optarg)); break;
336 case '?': DIE ("unknown option -%c", optopt);
337 case ':': DIE ("missing argument for %s", argv[optind - 1]);
338 default: DIE ("unexpected opt error");
339 }
340 }
341
342 if (argv[optind] == NULL) {
343 fprintf (stderr, "error: no login provided\n");
344 usage_mod (progname);
345 exit (1);
346 }
347
348 if (argv[optind + 1] != NULL)
349 DIE ("extra trailing arguments starting with: %s", argv[optind + 1]);
350
351 struct string_view login = SVCSTR (argv[optind]);
352 struct passwd_entry *entry;
353
354 struct passwd passwd = load_passwd_file ();
355
356 if (getuid () == 0) {
357 entry = lookup_login (passwd, login);
358 if (!entry)
359 DIE ("user %.*s not found", SVFMT (login));
360
361 string_builder_printf (&passwd_log, "%u modified user %.*s with id %d:",
362 time (NULL), SVFMT (entry->login), entry->id);
363
364 if (new_entry.id != -1) {
365 if (lookup_id (passwd, new_entry.id))
366 DIE ("user with id %d already exists", new_entry.id);
367 entry->id = new_entry.id;
368 string_builder_printf (&passwd_log, " new id %d,", entry->id);
369 }
370
371 if (new_entry.access_rights != 0) {
372 entry->access_rights = new_entry.access_rights;
373 string_builder_printf (&passwd_log, " new access rights %c%c%c,",
374 entry->access_rights & AR_READ ? 'r' : '-',
375 entry->access_rights & AR_WRITE ? 'w' : '-',
376 entry->access_rights & AR_DELETE ? 'd' : '-');
377 }
378 } else {
379 if (new_entry.id != -1)
380 DIE ("permission to change id denied");
381 if (new_entry.access_rights != 0)
382 DIE ("permission to change access rights denied");
383
384 fputs ("Password: ", stdout);
385 struct string_view password = parse_password (read_password ());
386 entry = authenticate (passwd, login, password);
387 if (!entry) exit (1);
388 string_builder_printf (&passwd_log, "%u modified user %.*s with id %d:",
389 time (NULL), SVFMT (entry->login), entry->id);
390 }
391
392
393 if (prompt_for_new_password) {
394 fputs ("New password: ", stdout);
395 struct string_view password = SVDUP (parse_password (read_password ()));
396 fputs ("Repeat password: ", stdout);
397 struct string_view repeated = read_password ();
398 if (string_view_compare (password, repeated) != 0)
399 DIE ("passwords don't match");
400 entry->encoded_password = SVDUP (encode_password (password));
401 string_builder_printf (&passwd_log, " changed password,");
402 } else if (new_entry.encoded_password.begin != NULL) {
403 entry->encoded_password = new_entry.encoded_password;
404 string_builder_printf (&passwd_log, " changed password,");
405 }
406
407 if (new_entry.comment.begin != NULL) {
408 entry->comment = new_entry.comment;
409 string_builder_printf (&passwd_log, " new comment `%.*s',", SVFMT (entry->comment));
410 }
411
412 if (new_entry.login.begin != NULL) {
413 entry->login = new_entry.login;
414 string_builder_printf (&passwd_log, " new login %.*s,", SVFMT (entry->login));
415 }
416
417 if (DYNAMIC_ARRAY_LAST (passwd_log) != ',')
418 DIE ("no modifications supplied");
419 DYNAMIC_ARRAY_LAST (passwd_log) = '\n';
420
421 save_passwd_file (passwd);
422 close_passwd_file (passwd);
423 } break;
424
425 case SUBCOMMAND_DELETE: { // login
426 static struct option longopts[] = {
427 { "help", no_argument, NULL, 'h' },
428 {0}
429 };
430
431 char ch;
432 optind = 2;
433 while ((ch = getopt_long (argc, argv, ":h", longopts, NULL)) != -1) {
434 switch (ch) {
435 case 'h':
436 usage_del (progname);
437 exit (0);
438 case '?': DIE ("unknown option -%c", optopt);
439 case ':': DIE ("missing argument for %s", argv[optind - 1]);
440 default: DIE ("unexpected opt error");
441 }
442 }
443
444 if (argv[optind] == NULL) {
445 fprintf (stderr, "error: no login provided\n");
446 usage_del (progname);
447 exit (1);
448 }
449
450 if (argv[optind + 1] != NULL)
451 DIE ("extra trailing arguments starting with: %s", argv[optind + 1]);
452
453 if (getuid () != 0)
454 DIE ("permission denied");
455
456 struct string_view login = SVDUP (SVCSTR (argv[optind]));
457 struct passwd passwd = load_passwd_file ();
458
459 struct passwd_entry *entry;
460 for (entry = passwd.items; entry < passwd.items + passwd.count; ++entry) {
461 if (SVCMP (login, entry->login) == 0)
462 break;
463 }
464
465 if (entry == passwd.items + passwd.count)
466 DIE ("user %.*s doesn't exist", SVFMT (login));
467
468 string_builder_printf (&passwd_log, "%u deleted user %.*s with id %d",
469 time (NULL), SVFMT (entry->login), entry->id);
470 if (entry->comment.begin)
471 string_builder_printf (&passwd_log, " (%.*s)", SVFMT (entry->comment));
472 DYNAMIC_ARRAY_APPEND (passwd_log, '\n');
473
474 if (entry + 1 != passwd.items + passwd.count) {
475 memmove (entry, entry + 1,
476 (passwd.count - (entry + 1 - passwd.items))*sizeof (*entry));
477 } passwd.count -= 1;
478
479 save_passwd_file (passwd);
480 close_passwd_file (passwd);
481 } break;
482
483 default:
484 usage (progname);
485 exit (strcmp (value, "-h") != 0 && strcmp (value, "--help") != 0);
486 }
487
488 append_passwd_log ();
489
490 return EXIT_SUCCESS;
491}
492
493// src/passwdctl.c ends here