// src/passwdctl.c --- utility to manage access to protected directory // // Copyright (c) 2026 Martin Michalec // // SPDX-License-Identifier: MIT // // Commentary: // // Code: #include #include #include #include #include #include #include #include #include #include #include "dynamic-array.h" #include "string-builder.h" #include "string-view.h" #include "common.h" #define SUBCOMMANDS() \ X (ADD, "add") \ X (MODIFY, "mod") \ X (DELETE, "del") #define X(SYM, STR) SUBCOMMAND_##SYM, enum subcommand { SUBCOMMANDS () }; #undef X #define X(SYM, STR) [SUBCOMMAND_##SYM] = STR, char *const subcommand_tokens[] = { SUBCOMMANDS () NULL }; #undef X regex_t login_regex; regex_t comment_regex; regex_t password_regex; struct string_view parse_password (struct string_view sv) { if (regexec (&password_regex, sv.begin, 0, NULL, 0) != 0) DIE ("password doesn't match regex %s", PASSWORD_REGEX); return sv; } int parse_id (const char *cstr) { int id; size_t read; sscanf (cstr, "%d%ln", &id, &read); if (id < 0) DIE ("invalid id"); if (read != strlen (cstr)) DIE ("invalid id"); return id; } enum access_rights parse_access_right (const char *cstr) { enum access_rights ar = 0; while (*cstr) { switch (*cstr++) { case 'r': ar |= AR_READ; break; case 'w': ar |= AR_WRITE; break; case 'd': ar |= AR_DELETE; break; default: DIE ("invalid access right: %c", cstr[-1]); } } return ar; } struct string_view parse_comment (const char *cstr) { struct string_view sv = SVCSTR (cstr); if (SVLEN (sv) > COMMENT_MAX_LENGTH) DIE ("comment too long"); if (regexec (&comment_regex, cstr, 0, NULL, 0) != 0) DIE ("comment doesn't match regex " COMMENT_REGEX); return sv; } struct string_view parse_login (const char *cstr) { struct string_view sv = SVCSTR (cstr); if (SVLEN (sv) > LOGIN_MAX_LENGTH) DIE ("login too long"); if (regexec (&login_regex, cstr, 0, NULL, 0) != 0) DIE ("login doesn't match regex " LOGIN_REGEX); return sv; } const char * regex_error (const regex_t *comp, int errcode) { static char *error = NULL; size_t size = regerror (errcode, comp, NULL, 0); error = realloc (error, size); regerror (errcode, comp, error, size); return error; } void usage (const char *progname) { printf ("Usage: %s [options] COMMAND [ARGS]\n", progname); puts (""); puts ("Options:"); puts (" -h, --help display this help message and exit"); puts (""); puts ("Commands:"); puts (" add LOGIN add new account"); puts (" mod LOGIN modify existing account"); puts (" del LOGIN delete account"); puts (""); } void usage_add (const char *progname) { printf ("Usage: %s add [options] LOGIN\n", progname); puts (""); puts ("Options:"); puts (" -h, --help display this help message and exit"); puts (" -p, --password PASSWORD password of the new account"); puts (" -i, --id ID ID of the new account"); puts (" -a, --access-rights {r|w|d}... access right of the new account"); puts (" -c, --comment COMMENT comment of the new account"); puts (""); } void usage_mod (const char *progname) { printf ("Usage: %s mod [options] LOGIN\n", progname); puts (""); puts ("Options:"); puts (" -h, --help display this help message and exit"); puts (" -l, --login NEW_LOGIN new login for the account"); puts (" -p, --password PASSWORD new password for the account"); puts (" -P prompt for new password for the account"); puts (" -i, --id ID new ID for the account"); puts (" -a, --access-rights {r|w|d}... new access rights for the account"); puts (" -c, --comment COMMENT new comment for the account"); puts (""); } void usage_del (const char *progname) { printf ("Usage: %s del [options] LOGIN\n", progname); puts (""); puts ("Options:"); puts (" -h, --help display this help message and exit"); puts (""); } int main (int argc, char *argv[]) { ruid = getuid (); euid = geteuid (); seteuid (ruid); ERR_load_crypto_strings (); const char *progname = argv[0]; if (progname == NULL) DIE ("unable to get program name"); char **subcommand = &argv[1]; if (*subcommand == NULL) { usage (progname); exit (1); } int errcode; errcode = regcomp (&login_regex, "^" LOGIN_REGEX "$", REG_NOSUB); if (errcode != 0) { DIE ("unable to compile login regex: %s", regex_error (&login_regex, errcode)); } errcode = regcomp (&password_regex, "^" PASSWORD_REGEX "$", REG_NOSUB); if (errcode != 0) { DIE ("unable to compile password regex: %s", regex_error (&password_regex, errcode)); } errcode = regcomp (&comment_regex, "^" COMMENT_REGEX "$", REG_NOSUB); if (errcode != 0) { DIE ("unable to compile comment regex: %s", regex_error (&comment_regex, errcode)); } char *value; switch (getsubopt (subcommand, subcommand_tokens, &value)) { case SUBCOMMAND_ADD: { static struct option longopts[] = { { "help", no_argument, NULL, 'h' }, { "password", required_argument, NULL, 'p' }, { "id", required_argument, NULL, 'i' }, { "access-rights", required_argument, NULL, 'a' }, { "comment", required_argument, NULL, 'c' }, {0} }; char ch; struct passwd_entry new_entry = { .id = -1 }; optind = 2; while ((ch = getopt_long (argc, argv, ":hp:i:a:c:", longopts, NULL)) != -1) { switch (ch) { case 'h': usage_add (progname); exit (0); case 'p': new_entry.encoded_password = SVDUP (encode_password (parse_password (SVCSTR (optarg)))); break; case 'i': new_entry.id = parse_id (optarg); break; case 'a': new_entry.access_rights = parse_access_right (optarg); break; case 'c': new_entry.comment = SVDUP (parse_comment (optarg)); break; case '?': DIE ("unknown option -%c", optopt); case ':': DIE ("missing argument for %s", argv[optind - 1]); default: DIE ("unexpected opt error"); } } if (argv[optind] == NULL) { fprintf (stderr, "error: no login provided\n"); usage_add (progname); exit (1); } if (argv[optind + 1] != NULL) DIE ("extra trailing arguments starting with: %s", argv[optind + 1]); if (getuid () != 0) DIE ("permission denied"); new_entry.login = SVDUP (SVCSTR (argv[optind])); if (new_entry.encoded_password.begin == NULL) { fputs ("Password: ", stdout); struct string_view password = duplicate_string_view_as_cstr (parse_password (read_password ())); fputs ("Repeat password: ", stdout); struct string_view repeated = read_password (); if (string_view_compare (password, repeated) != 0) DIE ("passwords don't match"); new_entry.encoded_password = SVDUP (encode_password (password)); } struct passwd passwd = load_passwd_file (); int max_id = -1; for (struct passwd_entry *entry = passwd.items; entry < passwd.items + passwd.count; ++entry) { if (SVCMP (new_entry.login, entry->login) == 0) DIE ("user %.*s already exists", SVFMT (entry->login)); if (new_entry.id == entry->id) DIE ("user with id %d already exists", entry->id); if (entry->id > max_id) max_id = entry->id; } if (new_entry.id < 0) new_entry.id = max_id + 1; DYNAMIC_ARRAY_APPEND (passwd, new_entry); save_passwd_file (passwd); string_builder_printf (&passwd_log, "%u added user %.*s %d", time (NULL), SVFMT (new_entry.login), new_entry.id); if (new_entry.comment.begin) string_builder_printf (&passwd_log, " (%.*s)", SVFMT (new_entry.comment)); string_builder_printf (&passwd_log, " with access rights %c%c%c\n", new_entry.access_rights & AR_READ ? 'r' : '-', new_entry.access_rights & AR_WRITE ? 'w' : '-', new_entry.access_rights & AR_DELETE ? 'd' : '-'); close_passwd_file (passwd); } break; case SUBCOMMAND_MODIFY: { // [-p --password=] [-i -id=] [-c --comment=] login static struct option longopts[] = { { "help", no_argument, NULL, 'h' }, { "login", required_argument, NULL, 'l' }, { "password", required_argument, NULL, 'p' }, { "id", required_argument, NULL, 'i' }, { "access-rights", required_argument, NULL, 'a' }, { "comment", required_argument, NULL, 'c' }, {0} }; char ch; struct passwd_entry new_entry = { .id = -1 }; bool prompt_for_new_password = false; optind = 2; while ((ch = getopt_long (argc, argv, ":hl:p:Pi:a:c:", longopts, NULL)) != -1) { switch (ch) { case 'h': usage_mod (progname); exit (0); case 'l': new_entry.login = SVDUP (parse_login (optarg)); break; case 'p': new_entry.encoded_password = SVDUP (encode_password (parse_password (SVCSTR (optarg)))); break; case 'P': prompt_for_new_password = true; break; case 'i': new_entry.id = parse_id (optarg); break; case 'a': new_entry.access_rights = parse_access_right (optarg); break; case 'c': new_entry.comment = SVDUP (parse_comment (optarg)); break; case '?': DIE ("unknown option -%c", optopt); case ':': DIE ("missing argument for %s", argv[optind - 1]); default: DIE ("unexpected opt error"); } } if (argv[optind] == NULL) { fprintf (stderr, "error: no login provided\n"); usage_mod (progname); exit (1); } if (argv[optind + 1] != NULL) DIE ("extra trailing arguments starting with: %s", argv[optind + 1]); struct string_view login = SVCSTR (argv[optind]); struct passwd_entry *entry; struct passwd passwd = load_passwd_file (); if (getuid () == 0) { entry = lookup_login (passwd, login); if (!entry) DIE ("user %.*s not found", SVFMT (login)); string_builder_printf (&passwd_log, "%u modified user %.*s with id %d:", time (NULL), SVFMT (entry->login), entry->id); if (new_entry.id != -1) { if (lookup_id (passwd, new_entry.id)) DIE ("user with id %d already exists", new_entry.id); entry->id = new_entry.id; string_builder_printf (&passwd_log, " new id %d,", entry->id); } if (new_entry.access_rights != 0) { entry->access_rights = new_entry.access_rights; string_builder_printf (&passwd_log, " new access rights %c%c%c,", entry->access_rights & AR_READ ? 'r' : '-', entry->access_rights & AR_WRITE ? 'w' : '-', entry->access_rights & AR_DELETE ? 'd' : '-'); } } else { if (new_entry.id != -1) DIE ("permission to change id denied"); if (new_entry.access_rights != 0) DIE ("permission to change access rights denied"); fputs ("Password: ", stdout); struct string_view password = parse_password (read_password ()); entry = authenticate (passwd, login, password); if (!entry) exit (1); string_builder_printf (&passwd_log, "%u modified user %.*s with id %d:", time (NULL), SVFMT (entry->login), entry->id); } if (prompt_for_new_password) { fputs ("New password: ", stdout); struct string_view password = SVDUP (parse_password (read_password ())); fputs ("Repeat password: ", stdout); struct string_view repeated = read_password (); if (string_view_compare (password, repeated) != 0) DIE ("passwords don't match"); entry->encoded_password = SVDUP (encode_password (password)); string_builder_printf (&passwd_log, " changed password,"); } else if (new_entry.encoded_password.begin != NULL) { entry->encoded_password = new_entry.encoded_password; string_builder_printf (&passwd_log, " changed password,"); } if (new_entry.comment.begin != NULL) { entry->comment = new_entry.comment; string_builder_printf (&passwd_log, " new comment `%.*s',", SVFMT (entry->comment)); } if (new_entry.login.begin != NULL) { entry->login = new_entry.login; string_builder_printf (&passwd_log, " new login %.*s,", SVFMT (entry->login)); } if (DYNAMIC_ARRAY_LAST (passwd_log) != ',') DIE ("no modifications supplied"); DYNAMIC_ARRAY_LAST (passwd_log) = '\n'; save_passwd_file (passwd); close_passwd_file (passwd); } break; case SUBCOMMAND_DELETE: { // login static struct option longopts[] = { { "help", no_argument, NULL, 'h' }, {0} }; char ch; optind = 2; while ((ch = getopt_long (argc, argv, ":h", longopts, NULL)) != -1) { switch (ch) { case 'h': usage_del (progname); exit (0); case '?': DIE ("unknown option -%c", optopt); case ':': DIE ("missing argument for %s", argv[optind - 1]); default: DIE ("unexpected opt error"); } } if (argv[optind] == NULL) { fprintf (stderr, "error: no login provided\n"); usage_del (progname); exit (1); } if (argv[optind + 1] != NULL) DIE ("extra trailing arguments starting with: %s", argv[optind + 1]); if (getuid () != 0) DIE ("permission denied"); struct string_view login = SVDUP (SVCSTR (argv[optind])); struct passwd passwd = load_passwd_file (); struct passwd_entry *entry; for (entry = passwd.items; entry < passwd.items + passwd.count; ++entry) { if (SVCMP (login, entry->login) == 0) break; } if (entry == passwd.items + passwd.count) DIE ("user %.*s doesn't exist", SVFMT (login)); string_builder_printf (&passwd_log, "%u deleted user %.*s with id %d", time (NULL), SVFMT (entry->login), entry->id); if (entry->comment.begin) string_builder_printf (&passwd_log, " (%.*s)", SVFMT (entry->comment)); DYNAMIC_ARRAY_APPEND (passwd_log, '\n'); if (entry + 1 != passwd.items + passwd.count) { memmove (entry, entry + 1, (passwd.count - (entry + 1 - passwd.items))*sizeof (*entry)); } passwd.count -= 1; save_passwd_file (passwd); close_passwd_file (passwd); } break; default: usage (progname); exit (strcmp (value, "-h") != 0 && strcmp (value, "--help") != 0); } append_passwd_log (); return EXIT_SUCCESS; } // src/passwdctl.c ends here