diff options
Diffstat (limited to '03/src')
| -rw-r--r-- | 03/src/common.c | 469 | ||||
| -rw-r--r-- | 03/src/common.h | 119 | ||||
| -rw-r--r-- | 03/src/dynamic-array.h | 68 | ||||
| -rw-r--r-- | 03/src/passwdctl.c | 466 | ||||
| -rw-r--r-- | 03/src/shell.c | 1762 | ||||
| -rw-r--r-- | 03/src/string-builder.c | 60 | ||||
| -rw-r--r-- | 03/src/string-builder.h | 56 | ||||
| -rw-r--r-- | 03/src/string-view.c | 147 | ||||
| -rw-r--r-- | 03/src/string-view.h | 46 |
9 files changed, 3193 insertions, 0 deletions
diff --git a/03/src/common.c b/03/src/common.c new file mode 100644 index 0000000..fd76a3e --- /dev/null +++ b/03/src/common.c | |||
| @@ -0,0 +1,469 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <termios.h> | ||
| 4 | #include <fcntl.h> | ||
| 5 | #include <unistd.h> | ||
| 6 | |||
| 7 | #include <openssl/rand.h> | ||
| 8 | #include <openssl/err.h> | ||
| 9 | |||
| 10 | struct string_builder passwd_log = {0}; | ||
| 11 | |||
| 12 | const char *security_level_names[SL_COUNT] = { | ||
| 13 | [SL_TOP_SECRET ] = "top-secret", | ||
| 14 | [SL_SECRET ] = "secret", | ||
| 15 | [SL_CONFIDENTIAL] = "confidential", | ||
| 16 | [SL_UNCLASSIFIED] = "unclassified", | ||
| 17 | }; | ||
| 18 | |||
| 19 | uid_t ruid; | ||
| 20 | gid_t rgid; | ||
| 21 | uid_t euid; | ||
| 22 | gid_t egid; | ||
| 23 | |||
| 24 | char | ||
| 25 | encode6 (unsigned char b6) | ||
| 26 | { | ||
| 27 | if (b6 < ('9' - '.' + 1) ) return '.' + b6; | ||
| 28 | if (b6 < ('9' - '.' + 1) + ('Z' - 'A' + 1) ) return 'A' + b6 - ('9' - '.' + 1); | ||
| 29 | if (b6 < ('9' - '.' + 1) + ('Z' - 'A' + 1) + ('z' - 'a' + 1)) return 'a' + b6 - ('9' - '.' + 1) - ('Z' - 'A' + 1); | ||
| 30 | printf ("0x%X\n", b6); | ||
| 31 | assert (!"out of range"); | ||
| 32 | return -1; | ||
| 33 | } | ||
| 34 | |||
| 35 | unsigned char | ||
| 36 | decode6 (char c) | ||
| 37 | { | ||
| 38 | if ('.' <= c && c <= '9') return c - '.'; | ||
| 39 | if ('A' <= c && c <= 'Z') return c - 'A' + ('9' - '.' + 1); | ||
| 40 | if ('a' <= c && c <= 'z') return c - 'a' + ('9' - '.' + 1) + ('Z' - 'A' + 1); | ||
| 41 | assert (!"out of range"); | ||
| 42 | return -1; | ||
| 43 | } | ||
| 44 | |||
| 45 | void | ||
| 46 | encode (char *into, const unsigned char* from, unsigned int size) | ||
| 47 | { | ||
| 48 | for (; size >= 3; size -= 3) { | ||
| 49 | // 012345670123456701234567 | ||
| 50 | // 012345012345012345012345 | ||
| 51 | into[0] = encode6 ( from[0] >> 2); | ||
| 52 | into[1] = encode6 ((from[0] & 0b00000011) << 4 | from[1] >> 4); | ||
| 53 | into[2] = encode6 ((from[1] & 0b00001111) << 2 | from[2] >> 6); | ||
| 54 | into[3] = encode6 ((from[2] & 0b00111111)); | ||
| 55 | |||
| 56 | from += 3; | ||
| 57 | into += 4; | ||
| 58 | } | ||
| 59 | if (size == 2) { | ||
| 60 | into[0] = encode6 ( from[0] >> 2); | ||
| 61 | into[1] = encode6 ((from[0] & 0b00000011) << 4 | from[1] >> 4); | ||
| 62 | into[2] = encode6 ((from[1] & 0b00001111) << 2); | ||
| 63 | } else if (size == 1) { | ||
| 64 | into[0] = encode6 ( from[0] >> 2); | ||
| 65 | into[1] = encode6 ((from[0] & 0b00000011) << 4); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | void | ||
| 70 | decode (unsigned char *into, struct string_view from) | ||
| 71 | { | ||
| 72 | size_t len; | ||
| 73 | |||
| 74 | for (len = SVLEN (from); len >= 4; len -= 4) { | ||
| 75 | // 012345012345012345012345 | ||
| 76 | // 012345670123456701234567 | ||
| 77 | |||
| 78 | unsigned char d1 = decode6 (from.begin[0]); | ||
| 79 | unsigned char d2 = decode6 (from.begin[1]); | ||
| 80 | unsigned char d3 = decode6 (from.begin[2]); | ||
| 81 | unsigned char d4 = decode6 (from.begin[3]); | ||
| 82 | |||
| 83 | into[0] = d1 << 2 | d2 >> 4; | ||
| 84 | into[1] = (d2 & 0b001111) << 4 | d3 >> 2; | ||
| 85 | into[2] = d3 << 6 | d4; | ||
| 86 | |||
| 87 | from.begin += 4; | ||
| 88 | into += 3; | ||
| 89 | } | ||
| 90 | if (len == 3) { | ||
| 91 | unsigned char d1 = decode6 (from.begin[0]); | ||
| 92 | unsigned char d2 = decode6 (from.begin[1]); | ||
| 93 | unsigned char d3 = decode6 (from.begin[2]); | ||
| 94 | |||
| 95 | into[0] = d1 << 2 | d2 >> 4; | ||
| 96 | into[1] = (d2 & 0b001111) << 4 | d3 >> 2; | ||
| 97 | } else if (len == 2) { | ||
| 98 | unsigned char d1 = decode6 (from.begin[0]); | ||
| 99 | unsigned char d2 = decode6 (from.begin[1]); | ||
| 100 | |||
| 101 | into[0] = d1 << 2 | d2 >> 4; | ||
| 102 | } else assert ("!unreachable"); | ||
| 103 | } | ||
| 104 | |||
| 105 | void | ||
| 106 | generate_salt (unsigned char salt[PASSWORD_SALT_SIZE]) | ||
| 107 | { | ||
| 108 | if (RAND_bytes (salt, PASSWORD_SALT_SIZE) != 1) { | ||
| 109 | DIE ("error generating salt: RAND_bytes: %s", | ||
| 110 | ERR_reason_error_string (ERR_get_error ())); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | unsigned char * | ||
| 115 | hash_password (unsigned char hash[SHA_DIGEST_LENGTH], | ||
| 116 | struct string_view password, | ||
| 117 | const unsigned char salt[PASSWORD_SALT_SIZE]) | ||
| 118 | { | ||
| 119 | EVP_MD_CTX *mdctx = EVP_MD_CTX_new (); | ||
| 120 | if (mdctx == NULL) { | ||
| 121 | DIE ("error hashing creating context: EVP_MD_CTX_new: %s", | ||
| 122 | ERR_reason_error_string (ERR_get_error ())); | ||
| 123 | } | ||
| 124 | |||
| 125 | if (EVP_DigestInit_ex (mdctx, EVP_sha1 (), NULL) != 1) { | ||
| 126 | DIE ("error initializing digest: EVP_DigestInit_ex: %s", | ||
| 127 | ERR_reason_error_string (ERR_get_error ())); | ||
| 128 | } | ||
| 129 | |||
| 130 | if (EVP_DigestUpdate (mdctx, password.begin, SVLEN (password)) != 1) { | ||
| 131 | DIE ("error updating digest with password: EVP_DigestUpdate: %s", | ||
| 132 | ERR_reason_error_string (ERR_get_error ())); | ||
| 133 | } | ||
| 134 | |||
| 135 | if (EVP_DigestUpdate (mdctx, salt, PASSWORD_SALT_SIZE) != 1) { | ||
| 136 | DIE ("error updating digest with salt: EVP_DigestUpdate: %s", | ||
| 137 | ERR_reason_error_string (ERR_get_error ())); | ||
| 138 | } | ||
| 139 | |||
| 140 | unsigned int hash_len; | ||
| 141 | if (EVP_DigestFinal_ex(mdctx, hash, &hash_len) != 1) { | ||
| 142 | DIE ("error finalizing digest: EVP_DigestFinal_ex: %s", | ||
| 143 | ERR_reason_error_string (ERR_get_error ())); | ||
| 144 | } | ||
| 145 | |||
| 146 | EVP_MD_CTX_free (mdctx); | ||
| 147 | |||
| 148 | return hash; | ||
| 149 | } | ||
| 150 | |||
| 151 | struct string_view | ||
| 152 | encode_password (struct string_view password) | ||
| 153 | { | ||
| 154 | static char encoded[PASSWORD_ENCODED_LENGTH + 1] = { | ||
| 155 | [0] = '$', | ||
| 156 | [1] = '1', | ||
| 157 | [2] = '$', | ||
| 158 | [3 + PASSWORD_SALT_LENGTH] = '$', | ||
| 159 | }; | ||
| 160 | |||
| 161 | char *salt_encoded = (void *) &encoded[3]; | ||
| 162 | char *hash_encoded = (void *) &encoded[3 + PASSWORD_SALT_LENGTH + 1]; | ||
| 163 | |||
| 164 | unsigned char salt[PASSWORD_SALT_SIZE]; | ||
| 165 | generate_salt (salt); | ||
| 166 | encode (salt_encoded, salt, sizeof (salt)); | ||
| 167 | |||
| 168 | unsigned char hash[SHA_DIGEST_LENGTH]; | ||
| 169 | hash_password (hash, password, salt); | ||
| 170 | encode (hash_encoded, hash, SHA_DIGEST_LENGTH); | ||
| 171 | |||
| 172 | return (struct string_view) { encoded, encoded + PASSWORD_ENCODED_LENGTH }; | ||
| 173 | } | ||
| 174 | |||
| 175 | bool | ||
| 176 | check_password (struct string_view password, | ||
| 177 | struct string_view encoded_password) | ||
| 178 | { | ||
| 179 | if (*encoded_password.begin++ != '$') return false; | ||
| 180 | if (*encoded_password.begin++ != '1') return false; | ||
| 181 | if (*encoded_password.begin++ != '$') return false; | ||
| 182 | |||
| 183 | struct string_view encoded_salt = { | ||
| 184 | .begin = encoded_password.begin, | ||
| 185 | .end = encoded_password.begin += PASSWORD_SALT_LENGTH, | ||
| 186 | }; | ||
| 187 | if (*encoded_password.begin++ != '$') return false; | ||
| 188 | |||
| 189 | struct string_view encoded_hash = { | ||
| 190 | .begin = encoded_password.begin, | ||
| 191 | .end = encoded_password.begin + PASSWORD_HASH_LENGTH, | ||
| 192 | }; | ||
| 193 | |||
| 194 | unsigned char decoded_salt[PASSWORD_SALT_SIZE]; | ||
| 195 | decode (decoded_salt, encoded_salt); | ||
| 196 | |||
| 197 | unsigned char hash[SHA_DIGEST_LENGTH]; | ||
| 198 | hash_password (hash, password, decoded_salt); | ||
| 199 | |||
| 200 | unsigned char decoded_hash[SHA_DIGEST_LENGTH]; | ||
| 201 | decode (decoded_hash, encoded_hash); | ||
| 202 | |||
| 203 | return memcmp (hash, decoded_hash, sizeof (hash)) == 0; | ||
| 204 | } | ||
| 205 | |||
| 206 | int | ||
| 207 | fscan_passwd_entry (FILE *stream, struct passwd_entry *entry) | ||
| 208 | { | ||
| 209 | *entry = (struct passwd_entry) {0}; | ||
| 210 | |||
| 211 | int c; | ||
| 212 | do c = getc (stream); | ||
| 213 | while (c == '\n'); | ||
| 214 | if (c == EOF) | ||
| 215 | return 0; | ||
| 216 | ungetc (c, stream); | ||
| 217 | |||
| 218 | char buffer[256 + 1]; | ||
| 219 | |||
| 220 | fscanf (stream, "%32[-_a-zA-Z0-9]", buffer); | ||
| 221 | if (getc (stream) != ':') return -1; | ||
| 222 | entry->login = string_view_strdup (buffer); | ||
| 223 | |||
| 224 | fread (buffer, 1, PASSWORD_ENCODED_LENGTH, stream); | ||
| 225 | if (getc (stream) != ':') return -1; | ||
| 226 | buffer[PASSWORD_ENCODED_LENGTH] = '\0'; | ||
| 227 | entry->encoded_password = string_view_strdup (buffer); | ||
| 228 | |||
| 229 | fscanf (stream, "%d", &entry->id); | ||
| 230 | if (getc (stream) != ':') return -1; | ||
| 231 | |||
| 232 | fscanf (stream, "%d", (int *) &entry->security_level); | ||
| 233 | if (getc (stream) != ':') return -1; | ||
| 234 | |||
| 235 | int len = 0; | ||
| 236 | fscanf (stream, "%256[^:\n]%n", buffer, &len); | ||
| 237 | if (len) | ||
| 238 | entry->comment = SVDUP0 ((struct string_view) { buffer, buffer + len }); | ||
| 239 | |||
| 240 | return 1; | ||
| 241 | } | ||
| 242 | |||
| 243 | void | ||
| 244 | fprint_passwd_entry (FILE *stream, const struct passwd_entry *entry) | ||
| 245 | { | ||
| 246 | if (entry->id < 0) | ||
| 247 | DIE ("id < 0"); | ||
| 248 | |||
| 249 | struct string_view comment; | ||
| 250 | if (entry->comment.begin) { | ||
| 251 | comment.begin = entry->comment.begin; | ||
| 252 | comment.end = entry->comment.end; | ||
| 253 | } else { | ||
| 254 | comment.begin = ""; | ||
| 255 | comment.end = comment.begin; | ||
| 256 | } | ||
| 257 | |||
| 258 | fprintf (stream, "%.*s:%.*s:%d:%d:%.*s\n", | ||
| 259 | SVFMT (entry->login), | ||
| 260 | SVFMT (entry->encoded_password), | ||
| 261 | entry->id, | ||
| 262 | entry->security_level, | ||
| 263 | SVFMT (comment)); | ||
| 264 | } | ||
| 265 | |||
| 266 | void | ||
| 267 | lock_passwd_file (struct passwd passwd) | ||
| 268 | { | ||
| 269 | if (lockf (fileno (passwd.stream), F_TLOCK, 0) != 0) | ||
| 270 | DIE ("unable to lock passwd file: %s", strerror (errno)); | ||
| 271 | } | ||
| 272 | |||
| 273 | void | ||
| 274 | unlock_passwd_file (struct passwd passwd) | ||
| 275 | { | ||
| 276 | if (lockf (fileno (passwd.stream), F_ULOCK, 0) != 0) | ||
| 277 | DIE ("unable to unlock passwd file: %s", strerror (errno)); | ||
| 278 | } | ||
| 279 | |||
| 280 | struct passwd | ||
| 281 | load_passwd_file (void) | ||
| 282 | { | ||
| 283 | elevate (); | ||
| 284 | struct passwd passwd = { | ||
| 285 | .stream = fopen (PREFIX"/etc/passwd", "r+"), | ||
| 286 | }; | ||
| 287 | drop (); | ||
| 288 | |||
| 289 | if (passwd.stream == NULL) | ||
| 290 | DIE ("unable to open passwd file: %s", strerror (errno)); | ||
| 291 | |||
| 292 | if (lockf (fileno (passwd.stream), F_TEST, 0) != 0) | ||
| 293 | DIE ("passwd file is locked: %s", strerror (errno)); | ||
| 294 | |||
| 295 | for (int i = 0;; ++i) { | ||
| 296 | struct passwd_entry entry; | ||
| 297 | int ret = fscan_passwd_entry (passwd.stream, &entry); | ||
| 298 | if (ret == -1) | ||
| 299 | DIE (PREFIX"/etc/passwd:%d: failed to parse entry", i); | ||
| 300 | if (ret == 0) | ||
| 301 | break; | ||
| 302 | DYNAMIC_ARRAY_APPEND (passwd, entry); | ||
| 303 | } | ||
| 304 | |||
| 305 | return passwd; | ||
| 306 | } | ||
| 307 | |||
| 308 | void | ||
| 309 | save_passwd_file (struct passwd passwd) | ||
| 310 | { | ||
| 311 | ftruncate (fileno (passwd.stream), 0); | ||
| 312 | rewind (passwd.stream); | ||
| 313 | for (struct passwd_entry *entry = passwd.items; | ||
| 314 | entry < passwd.items + passwd.count; ++entry) { | ||
| 315 | fprint_passwd_entry (passwd.stream, entry); | ||
| 316 | } fflush (passwd.stream); | ||
| 317 | } | ||
| 318 | |||
| 319 | void | ||
| 320 | close_passwd_file (struct passwd passwd) | ||
| 321 | { | ||
| 322 | for (uint32_t i = 0; i < passwd.count; ++i) { | ||
| 323 | free ((void *) passwd.items[i].login.begin); | ||
| 324 | free ((void *) passwd.items[i].encoded_password.begin); | ||
| 325 | free ((void *) passwd.items[i].comment.begin); | ||
| 326 | } | ||
| 327 | DYNAMIC_ARRAY_FREE (passwd); | ||
| 328 | |||
| 329 | if (lockf (fileno (passwd.stream), F_ULOCK, 0) != 0) | ||
| 330 | DIE ("unable to unlock passwd file: %s", strerror (errno)); | ||
| 331 | |||
| 332 | fclose (passwd.stream); | ||
| 333 | } | ||
| 334 | |||
| 335 | struct string_view | ||
| 336 | read_password () | ||
| 337 | { | ||
| 338 | static char password[PASSWORD_MAX_LENGTH + 2]; | ||
| 339 | |||
| 340 | struct termios term_orig; | ||
| 341 | tcgetattr (fileno (stdin), &term_orig); | ||
| 342 | struct termios term_noecho = term_orig; | ||
| 343 | term_noecho.c_lflag &= ~ECHO; | ||
| 344 | tcsetattr (fileno (stdin), 0, &term_noecho); | ||
| 345 | |||
| 346 | if (fgets (password, sizeof (password), stdin) == NULL) { | ||
| 347 | tcsetattr (fileno (stdin), 0, &term_orig); | ||
| 348 | putchar ('\n'); | ||
| 349 | exit (0); | ||
| 350 | } tcsetattr (fileno (stdin), 0, &term_orig); | ||
| 351 | |||
| 352 | putchar ('\n'); | ||
| 353 | size_t len = strlen (password) - 1; | ||
| 354 | if (password[len] != '\n') | ||
| 355 | DIE ("password too long"); | ||
| 356 | password[len] = '\0'; | ||
| 357 | |||
| 358 | return (struct string_view) { password, password + len }; | ||
| 359 | } | ||
| 360 | |||
| 361 | struct string_view | ||
| 362 | read_login () | ||
| 363 | { | ||
| 364 | static char login[LOGIN_MAX_LENGTH + 2]; | ||
| 365 | |||
| 366 | if (fgets (login, sizeof (login), stdin) == NULL) { | ||
| 367 | putchar ('\n'); | ||
| 368 | exit (0); | ||
| 369 | } | ||
| 370 | |||
| 371 | size_t len = strlen (login) - 1; | ||
| 372 | if (login[len] != '\n') | ||
| 373 | DIE ("login too long"); | ||
| 374 | login[len] = '\0'; | ||
| 375 | |||
| 376 | return (struct string_view) { login, login + len }; | ||
| 377 | } | ||
| 378 | |||
| 379 | struct passwd_entry * | ||
| 380 | authenticate (struct passwd passwd, | ||
| 381 | struct string_view login, | ||
| 382 | struct string_view password) | ||
| 383 | { | ||
| 384 | struct passwd_entry *entry = lookup_login (passwd, login); | ||
| 385 | |||
| 386 | if (!entry) { | ||
| 387 | fprintf (stderr, "user %.*s not found\n", SVFMT (login)); | ||
| 388 | return NULL; | ||
| 389 | } | ||
| 390 | |||
| 391 | if (!check_password (password, entry->encoded_password)) { | ||
| 392 | fputs ("authentication failure\n", stderr); | ||
| 393 | string_builder_printf (&passwd_log, "%u user %.*s supplied incorrect password\n", | ||
| 394 | time (NULL), SVFMT (entry->login)); | ||
| 395 | return NULL; | ||
| 396 | } | ||
| 397 | |||
| 398 | string_builder_printf (&passwd_log, "%u authenticated user %.*s %d\n", | ||
| 399 | time (NULL), SVFMT (entry->login), entry->id); | ||
| 400 | |||
| 401 | return entry; | ||
| 402 | } | ||
| 403 | |||
| 404 | void | ||
| 405 | append_passwd_log () | ||
| 406 | { | ||
| 407 | if (passwd_log.count) { | ||
| 408 | elevate (); | ||
| 409 | int fd = open (PREFIX"/log/passwd.log", O_CREAT | O_WRONLY | O_APPEND); | ||
| 410 | drop (); | ||
| 411 | write (fd, passwd_log.items, passwd_log.count); | ||
| 412 | close (fd); | ||
| 413 | DYNAMIC_ARRAY_RESET (passwd_log); | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | struct passwd_entry * | ||
| 418 | lookup_login (struct passwd passwd, struct string_view login) | ||
| 419 | { | ||
| 420 | for (struct passwd_entry *entry = passwd.items; | ||
| 421 | entry < passwd.items + passwd.count; ++entry) { | ||
| 422 | if (SVCMP (login, entry->login) != 0) | ||
| 423 | continue; | ||
| 424 | |||
| 425 | string_builder_printf (&passwd_log, "%u user '%.*s' (%d) was looked up\n", | ||
| 426 | time (NULL), SVFMT (entry->login), entry->id); | ||
| 427 | |||
| 428 | return entry; | ||
| 429 | } | ||
| 430 | |||
| 431 | string_builder_printf (&passwd_log, "%u user '%.*s' lookup failed\n", | ||
| 432 | time (NULL), SVFMT (login)); | ||
| 433 | |||
| 434 | return NULL; | ||
| 435 | } | ||
| 436 | |||
| 437 | struct passwd_entry * | ||
| 438 | lookup_id (struct passwd passwd, int id) | ||
| 439 | { | ||
| 440 | for (struct passwd_entry *entry = passwd.items; | ||
| 441 | entry < passwd.items + passwd.count; ++entry) { | ||
| 442 | if (id != entry->id) | ||
| 443 | continue; | ||
| 444 | |||
| 445 | string_builder_printf (&passwd_log, "%u user %d was looked up\n", | ||
| 446 | time (NULL), id); | ||
| 447 | |||
| 448 | return entry; | ||
| 449 | } | ||
| 450 | |||
| 451 | string_builder_printf (&passwd_log, "%u user %d lookup failed\n", | ||
| 452 | time (NULL), id); | ||
| 453 | |||
| 454 | return NULL; | ||
| 455 | } | ||
| 456 | |||
| 457 | void | ||
| 458 | elevate (void) | ||
| 459 | { | ||
| 460 | if (euid != ruid) seteuid (euid); | ||
| 461 | if (egid != rgid) setegid (egid); | ||
| 462 | } | ||
| 463 | |||
| 464 | void | ||
| 465 | drop (void) | ||
| 466 | { | ||
| 467 | if (euid != ruid) seteuid (ruid); | ||
| 468 | if (egid != rgid) setegid (rgid); | ||
| 469 | } | ||
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 | |||
| 47 | extern struct string_builder passwd_log; | ||
| 48 | |||
| 49 | extern uid_t ruid; | ||
| 50 | extern gid_t rgid; | ||
| 51 | extern uid_t euid; | ||
| 52 | extern gid_t egid; | ||
| 53 | |||
| 54 | enum access_rights { | ||
| 55 | AR_READ = 1 << 0, | ||
| 56 | AR_WRITE = 1 << 1, | ||
| 57 | AR_DELETE = 1 << 2, | ||
| 58 | }; | ||
| 59 | |||
| 60 | enum security_level { | ||
| 61 | SL_TOP_SECRET, | ||
| 62 | SL_SECRET, | ||
| 63 | SL_CONFIDENTIAL, | ||
| 64 | SL_UNCLASSIFIED, | ||
| 65 | SL_COUNT | ||
| 66 | }; | ||
| 67 | |||
| 68 | extern const char *security_level_names[SL_COUNT]; | ||
| 69 | |||
| 70 | struct 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 | |||
| 78 | struct passwd { | ||
| 79 | struct passwd_entry *items; | ||
| 80 | uint32_t count; | ||
| 81 | uint32_t capacity; | ||
| 82 | FILE *stream; | ||
| 83 | }; | ||
| 84 | |||
| 85 | char encode6 (unsigned char b6); | ||
| 86 | unsigned char decode6 (char c); | ||
| 87 | void encode (char *into, const unsigned char* from, unsigned int size); | ||
| 88 | void decode (unsigned char *into, struct string_view from); | ||
| 89 | |||
| 90 | void generate_salt (unsigned char salt[PASSWORD_SALT_SIZE]); | ||
| 91 | unsigned char *hash_password (unsigned char hash[SHA_DIGEST_LENGTH], | ||
| 92 | struct string_view password, | ||
| 93 | const unsigned char salt[PASSWORD_SALT_SIZE]); | ||
| 94 | struct string_view encode_password (struct string_view password); | ||
| 95 | bool check_password (struct string_view password, | ||
| 96 | struct string_view encoded_password); | ||
| 97 | |||
| 98 | int fscan_passwd_entry (FILE *, struct passwd_entry *); | ||
| 99 | void fprint_passwd_entry (FILE *, const struct passwd_entry *); | ||
| 100 | struct passwd load_passwd_file (void); | ||
| 101 | void save_passwd_file (struct passwd); | ||
| 102 | void close_passwd_file (struct passwd); | ||
| 103 | |||
| 104 | struct string_view read_password (void); | ||
| 105 | struct string_view read_login (void); | ||
| 106 | |||
| 107 | void append_passwd_log (void); | ||
| 108 | |||
| 109 | struct passwd_entry *lookup_login (struct passwd, struct string_view login); | ||
| 110 | struct passwd_entry *lookup_id (struct passwd, int id); | ||
| 111 | |||
| 112 | struct passwd_entry *authenticate (struct passwd, struct string_view login, struct string_view password); | ||
| 113 | |||
| 114 | void elevate (void); | ||
| 115 | void drop (void); | ||
| 116 | |||
| 117 | #endif // INCLUDED_COMMON_H | ||
| 118 | |||
| 119 | // common.h ends here | ||
diff --git a/03/src/dynamic-array.h b/03/src/dynamic-array.h new file mode 100644 index 0000000..ed5cb32 --- /dev/null +++ b/03/src/dynamic-array.h | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | #ifndef INCLUDED_DYNAMIC_ARRAY_H | ||
| 2 | #define INCLUDED_DYNAMIC_ARRAY_H | ||
| 3 | |||
| 4 | #include <stdlib.h> | ||
| 5 | #include <assert.h> | ||
| 6 | |||
| 7 | #define MALLOC malloc | ||
| 8 | #define REALLOC realloc | ||
| 9 | #define FREE free | ||
| 10 | #define ASSERT assert | ||
| 11 | |||
| 12 | #ifndef DYNAMIC_ARRAY_INIT_CAP | ||
| 13 | #define DYNAMIC_ARRAY_INIT_CAP 256 | ||
| 14 | #endif | ||
| 15 | |||
| 16 | #define DYNAMIC_ARRAY_ENSURE_CAPACITY(DYNAMIC_ARRAY, COUNT) \ | ||
| 17 | ({ \ | ||
| 18 | if ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) { \ | ||
| 19 | if ((DYNAMIC_ARRAY).capacity == 0) \ | ||
| 20 | (DYNAMIC_ARRAY).capacity = DYNAMIC_ARRAY_INIT_CAP; \ | ||
| 21 | while ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) \ | ||
| 22 | (DYNAMIC_ARRAY).capacity *= 2; \ | ||
| 23 | (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ | ||
| 24 | (DYNAMIC_ARRAY).capacity * \ | ||
| 25 | sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 26 | ASSERT((DYNAMIC_ARRAY).items); \ | ||
| 27 | } \ | ||
| 28 | }) | ||
| 29 | |||
| 30 | #define DYNAMIC_ARRAY_SHRINK(DYNAMIC_ARRAY) \ | ||
| 31 | ({ \ | ||
| 32 | (DYNAMIC_ARRAY).capacity = (DYNAMIC_ARRAY).count; \ | ||
| 33 | (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ | ||
| 34 | (DYNAMIC_ARRAY).capacity * \ | ||
| 35 | sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 36 | ASSERT((DYNAMIC_ARRAY).items); \ | ||
| 37 | \ | ||
| 38 | }) | ||
| 39 | |||
| 40 | #define DYNAMIC_ARRAY_APPEND(DYNAMIC_ARRAY, ITEM) \ | ||
| 41 | ({ \ | ||
| 42 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), 1); \ | ||
| 43 | (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count++] = (ITEM); \ | ||
| 44 | }) | ||
| 45 | |||
| 46 | #define DYNAMIC_ARRAY_APPEND_MANY(DYNAMIC_ARRAY, NEW_ITEMS, NEW_ITEMS_COUNT) \ | ||
| 47 | ({ \ | ||
| 48 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), (NEW_ITEMS_COUNT)); \ | ||
| 49 | memcpy ((DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count, (NEW_ITEMS), \ | ||
| 50 | (NEW_ITEMS_COUNT)*sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 51 | (DYNAMIC_ARRAY).count += (NEW_ITEMS_COUNT); \ | ||
| 52 | }) | ||
| 53 | |||
| 54 | #define DYNAMIC_ARRAY_LAST(DYNAMIC_ARRAY) \ | ||
| 55 | (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count - 1] | ||
| 56 | |||
| 57 | #define DYNAMIC_ARRAY_POP(DYNAMIC_ARRAY) \ | ||
| 58 | (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count--] | ||
| 59 | |||
| 60 | #define DYNAMIC_ARRAY_RESET(DYNAMIC_ARRAY) ({ (DYNAMIC_ARRAY).count = 0; }) | ||
| 61 | |||
| 62 | #define DYNAMIC_ARRAY_FREE(DYNAMIC_ARRAY) FREE((DYNAMIC_ARRAY).items) | ||
| 63 | |||
| 64 | #define DYNAMIC_ARRAY_FOREACH(ITEM, DYNAMIC_ARRAY) for ( \ | ||
| 65 | typeof ((DYNAMIC_ARRAY).items) ITEM = (DYNAMIC_ARRAY).items; \ | ||
| 66 | ITEM < (DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count; ++ITEM) | ||
| 67 | |||
| 68 | #endif | ||
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, | ||
| 34 | enum subcommand { | ||
| 35 | SUBCOMMANDS () | ||
| 36 | }; | ||
| 37 | #undef X | ||
| 38 | |||
| 39 | #define X(SYM, STR) [SUBCOMMAND_##SYM] = STR, | ||
| 40 | char *const subcommand_tokens[] = { | ||
| 41 | SUBCOMMANDS () | ||
| 42 | NULL | ||
| 43 | }; | ||
| 44 | #undef X | ||
| 45 | |||
| 46 | regex_t login_regex; | ||
| 47 | regex_t comment_regex; | ||
| 48 | regex_t password_regex; | ||
| 49 | |||
| 50 | struct string_view | ||
| 51 | parse_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 | |||
| 59 | int | ||
| 60 | parse_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 | |||
| 72 | enum security_level | ||
| 73 | parse_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 | |||
| 81 | struct string_view | ||
| 82 | parse_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 | |||
| 95 | struct string_view | ||
| 96 | parse_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 | |||
| 109 | const char * | ||
| 110 | regex_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 | |||
| 119 | void | ||
| 120 | usage (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 | |||
| 134 | int | ||
| 135 | main (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 | ||
diff --git a/03/src/shell.c b/03/src/shell.c new file mode 100644 index 0000000..0935ebc --- /dev/null +++ b/03/src/shell.c | |||
| @@ -0,0 +1,1762 @@ | |||
| 1 | // src/shell.c --- utility for providing access to protected folder | ||
| 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 <assert.h> | ||
| 12 | #include <getopt.h> | ||
| 13 | #include <stdlib.h> | ||
| 14 | #include <errno.h> | ||
| 15 | #include <unistd.h> | ||
| 16 | #include <fcntl.h> | ||
| 17 | #include <time.h> | ||
| 18 | #include <sys/stat.h> | ||
| 19 | #include <sys/mman.h> | ||
| 20 | #include <glob.h> | ||
| 21 | #include <stdio.h> | ||
| 22 | #include <dirent.h> | ||
| 23 | #include <readline/readline.h> | ||
| 24 | #include <readline/history.h> | ||
| 25 | |||
| 26 | #include "common.h" | ||
| 27 | |||
| 28 | #define CONFDATA PREFIX"/confdata" | ||
| 29 | #define ACL PREFIX"/etc/acl" | ||
| 30 | |||
| 31 | int id; | ||
| 32 | |||
| 33 | struct string_view | ||
| 34 | canonicalize (struct string_view filename) | ||
| 35 | { | ||
| 36 | static struct string_builder sb = {0}; | ||
| 37 | STRING_BUILDER_RESET (sb); | ||
| 38 | |||
| 39 | static struct { | ||
| 40 | struct string_view *items; | ||
| 41 | uint32_t count; | ||
| 42 | uint32_t capacity; | ||
| 43 | } parts = {0}; | ||
| 44 | DYNAMIC_ARRAY_RESET (parts); | ||
| 45 | |||
| 46 | bool ends_with_slash = filename.end[-1] == '/'; | ||
| 47 | |||
| 48 | if (*filename.begin != '/') { | ||
| 49 | DYNAMIC_ARRAY_ENSURE_CAPACITY (sb, 128); | ||
| 50 | for (;;) { | ||
| 51 | if (getcwd (sb.string, sb.capacity)) { | ||
| 52 | sb.length = strlen (sb.string); | ||
| 53 | break; | ||
| 54 | } | ||
| 55 | if (errno != ERANGE) | ||
| 56 | return SVNULL; | ||
| 57 | DYNAMIC_ARRAY_ENSURE_CAPACITY (sb, 2*sb.capacity); | ||
| 58 | } | ||
| 59 | STRING_BUILDER_APPEND_CHAR (sb, '/'); | ||
| 60 | } | ||
| 61 | |||
| 62 | STRING_BUILDER_APPEND_STRING_VIEW (sb, filename); | ||
| 63 | filename = SVSB (sb); | ||
| 64 | |||
| 65 | while (++filename.begin < filename.end) { | ||
| 66 | struct string_view part = string_view_pop_up_to (&filename, '/'); | ||
| 67 | if (SVLEN (part) == 0) continue; | ||
| 68 | if (SVCMP (part, SVCSTR (".")) == 0) continue; | ||
| 69 | if (SVCMP (part, SVCSTR ("..")) == 0) { | ||
| 70 | if (parts.count == 0) return SVNULL; | ||
| 71 | parts.count -= 1; | ||
| 72 | continue; | ||
| 73 | } | ||
| 74 | DYNAMIC_ARRAY_APPEND (parts, part); | ||
| 75 | } | ||
| 76 | |||
| 77 | STRING_BUILDER_RESET (sb); | ||
| 78 | for (struct string_view *part = parts.items; | ||
| 79 | part < parts.items + parts.count; ++part) { | ||
| 80 | STRING_BUILDER_APPEND_CHAR (sb, '/'); | ||
| 81 | STRING_BUILDER_APPEND_STRING_VIEW (sb, *part); | ||
| 82 | } | ||
| 83 | |||
| 84 | if (ends_with_slash) | ||
| 85 | STRING_BUILDER_APPEND_CHAR (sb, '/'); | ||
| 86 | |||
| 87 | return SVSB (sb); | ||
| 88 | } | ||
| 89 | |||
| 90 | struct acl_grant { | ||
| 91 | int id; | ||
| 92 | enum access_rights access_rights; | ||
| 93 | }; | ||
| 94 | |||
| 95 | struct acl { | ||
| 96 | struct string_view filename; | ||
| 97 | const char *path; | ||
| 98 | enum security_level security_level; | ||
| 99 | int owner; | ||
| 100 | int grant_count; | ||
| 101 | struct acl_grant grants[]; | ||
| 102 | }; | ||
| 103 | |||
| 104 | const char * | ||
| 105 | filename_to_acl_path (struct string_view filename) | ||
| 106 | { | ||
| 107 | static struct string_builder acl_path = {0}; | ||
| 108 | |||
| 109 | struct string_view canonical = canonicalize (filename); | ||
| 110 | ASSERT (strncmp (canonical.begin, CONFDATA, strlen (CONFDATA)) == 0); | ||
| 111 | |||
| 112 | STRING_BUILDER_RESET (acl_path); | ||
| 113 | STRING_BUILDER_APPEND_CSTR (acl_path, ACL); | ||
| 114 | STRING_BUILDER_APPEND_CHAR (acl_path, '/'); | ||
| 115 | if (SVLEN (canonical) == strlen (CONFDATA)) { | ||
| 116 | STRING_BUILDER_APPEND_CHAR (acl_path, '!'); | ||
| 117 | goto finish; | ||
| 118 | } | ||
| 119 | |||
| 120 | for (const char *head = canonical.begin + strlen (CONFDATA); head < canonical.end; ++head) { | ||
| 121 | if (*head == '/') STRING_BUILDER_APPEND_CHAR (acl_path, '!'); | ||
| 122 | else STRING_BUILDER_APPEND_CHAR (acl_path, *head); | ||
| 123 | } | ||
| 124 | |||
| 125 | finish: | ||
| 126 | STRING_BUILDER_TERMINATE (acl_path); | ||
| 127 | return acl_path.string; | ||
| 128 | } | ||
| 129 | |||
| 130 | void | ||
| 131 | init_acl_for_file (struct string_view filename, int owner) | ||
| 132 | { | ||
| 133 | struct passwd passwd = load_passwd_file (); | ||
| 134 | struct passwd_entry *entry = lookup_id (passwd, id); | ||
| 135 | close_passwd_file (passwd); | ||
| 136 | ASSERT (entry); | ||
| 137 | |||
| 138 | const char *path = filename_to_acl_path (filename); | ||
| 139 | ASSERT (path); | ||
| 140 | |||
| 141 | elevate (); | ||
| 142 | int fd = open (path, O_WRONLY | O_CREAT | O_EXCL); | ||
| 143 | drop (); | ||
| 144 | ASSERT (fd != -1); | ||
| 145 | |||
| 146 | FILE *stream = fdopen (fd, "w"); | ||
| 147 | ASSERT (stream); | ||
| 148 | fprintf (stream, "%d %d 0\n", entry->security_level, id); | ||
| 149 | fclose (stream); | ||
| 150 | } | ||
| 151 | |||
| 152 | /// File has to be in confdata | ||
| 153 | struct acl * | ||
| 154 | load_acl_for_file (struct string_view filename) | ||
| 155 | { | ||
| 156 | struct acl header = { | ||
| 157 | .filename = filename, | ||
| 158 | .path = filename_to_acl_path (filename), | ||
| 159 | }; | ||
| 160 | ASSERT (header.path); | ||
| 161 | |||
| 162 | elevate (); | ||
| 163 | int fd = open (header.path, O_RDONLY); | ||
| 164 | drop (); | ||
| 165 | if (fd == -1) return NULL; | ||
| 166 | struct stat st; | ||
| 167 | fstat (fd, &st); | ||
| 168 | if (S_ISDIR (st.st_mode)) | ||
| 169 | return NULL; | ||
| 170 | |||
| 171 | FILE *stream = fdopen (fd, "r"); | ||
| 172 | ASSERT (stream); | ||
| 173 | |||
| 174 | fscanf (stream, "%d %d %d", | ||
| 175 | (int *) &header.security_level, &header.owner, &header.grant_count); | ||
| 176 | struct acl *acl = malloc (sizeof (*acl) + | ||
| 177 | (header.grant_count + 1)*sizeof (*acl->grants)); | ||
| 178 | *acl = header; | ||
| 179 | for (int i = 0; i < acl->grant_count; ++i) | ||
| 180 | fscanf (stream, "%d %d", | ||
| 181 | &acl->grants[i].id, (int *) &acl->grants[i].access_rights); | ||
| 182 | fclose (stream); | ||
| 183 | |||
| 184 | return acl; | ||
| 185 | } | ||
| 186 | |||
| 187 | void | ||
| 188 | save_acl (struct acl *acl) | ||
| 189 | { | ||
| 190 | elevate (); | ||
| 191 | int fd = open (acl->path, O_WRONLY); | ||
| 192 | drop (); | ||
| 193 | ASSERT (fd != -1); | ||
| 194 | |||
| 195 | FILE *stream = fdopen (fd, "w"); | ||
| 196 | ASSERT (stream); | ||
| 197 | |||
| 198 | fprintf (stream, "%d %d %d\n", | ||
| 199 | acl->security_level, acl->owner, acl->grant_count); | ||
| 200 | for (int i = 0; i < acl->grant_count; ++i) | ||
| 201 | fprintf (stream, "%d %d\n", | ||
| 202 | acl->grants[i].id, acl->grants[i].access_rights); | ||
| 203 | fclose (stream); | ||
| 204 | } | ||
| 205 | |||
| 206 | struct command_line { | ||
| 207 | struct string_builder expanded; | ||
| 208 | struct string_view command; | ||
| 209 | union {struct string_view *items, *args; }; | ||
| 210 | union {uint32_t count, argc; }; | ||
| 211 | uint32_t capacity; | ||
| 212 | }; | ||
| 213 | |||
| 214 | struct string_builder confdata_log = {0}; | ||
| 215 | |||
| 216 | void | ||
| 217 | flush_confdata_log () | ||
| 218 | { | ||
| 219 | if (confdata_log.count) { | ||
| 220 | elevate (); | ||
| 221 | int fd = open (PREFIX"/log/confdata.log", O_CREAT | O_WRONLY | O_APPEND); | ||
| 222 | drop (); | ||
| 223 | write (fd, confdata_log.items, confdata_log.count); | ||
| 224 | close (fd); | ||
| 225 | DYNAMIC_ARRAY_RESET (confdata_log); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | int | ||
| 230 | is_conf (struct string_view filename) | ||
| 231 | { | ||
| 232 | int exists = 1; | ||
| 233 | elevate (); | ||
| 234 | char *canonical = realpath (filename.begin, NULL); | ||
| 235 | if (!canonical) { | ||
| 236 | exists = 0; | ||
| 237 | struct string_view dirname = SVDUP0 (string_view_dirname (filename)); | ||
| 238 | canonical = realpath (dirname.begin, NULL); | ||
| 239 | free ((void *) dirname.begin); | ||
| 240 | } | ||
| 241 | drop (); | ||
| 242 | |||
| 243 | if (canonical) { | ||
| 244 | bool starts_with = string_view_starts_with (SVCSTR (canonical), SVCSTR (CONFDATA)); | ||
| 245 | free (canonical); | ||
| 246 | return starts_with ? 1 + exists : 0; | ||
| 247 | } | ||
| 248 | |||
| 249 | return 0; | ||
| 250 | } | ||
| 251 | |||
| 252 | bool | ||
| 253 | authorize (int id, struct string_view filename, enum access_rights ar) | ||
| 254 | { | ||
| 255 | bool ret = false; | ||
| 256 | struct passwd passwd = load_passwd_file (); | ||
| 257 | struct passwd_entry *entry = lookup_id (passwd, id); | ||
| 258 | if (!entry) { | ||
| 259 | fprintf (stderr, "user no longer exists, switch user"); | ||
| 260 | goto finish; | ||
| 261 | } | ||
| 262 | |||
| 263 | struct acl *acl = load_acl_for_file (filename); | ||
| 264 | if (!acl) { | ||
| 265 | ret = true; | ||
| 266 | goto finish; | ||
| 267 | } | ||
| 268 | |||
| 269 | if (acl->owner == id) goto authorized; | ||
| 270 | for (int i = 0; i < acl->grant_count; ++i) { | ||
| 271 | if (acl->grants[i].id != id) continue; | ||
| 272 | enum access_rights acl_ar = acl->grants[i].access_rights; | ||
| 273 | if ((ar & acl_ar) == ar) goto authorized; | ||
| 274 | else break; | ||
| 275 | } | ||
| 276 | |||
| 277 | denied: | ||
| 278 | fprintf (stderr, "access denied '%.*s'\n", SVFMT (filename)); | ||
| 279 | string_builder_printf (&passwd_log, | ||
| 280 | "%u user '%.*s' (%d) was denied access %c%c%c to '%.*s'\n", | ||
| 281 | time (NULL), SVFMT (entry->login), entry->id, | ||
| 282 | ar & AR_READ ? 'r' : '-', | ||
| 283 | ar & AR_WRITE ? 'w' : '-', | ||
| 284 | ar & AR_DELETE ? 'd' : '-', SVFMT (filename)); | ||
| 285 | goto finish; | ||
| 286 | |||
| 287 | authorized:; | ||
| 288 | #ifdef MAC | ||
| 289 | if ((ar & AR_READ ) && entry->security_level > acl->security_level) goto denied; | ||
| 290 | if ((ar & AR_WRITE) && entry->security_level < acl->security_level) goto denied; | ||
| 291 | #endif | ||
| 292 | |||
| 293 | ret = true; | ||
| 294 | string_builder_printf (&passwd_log, | ||
| 295 | "%u user '%.*s' (%d) was granted access %c%c%c to '%.*s'\n", | ||
| 296 | time (NULL), SVFMT (entry->login), entry->id, | ||
| 297 | ar & AR_READ ? 'r' : '-', | ||
| 298 | ar & AR_WRITE ? 'w' : '-', | ||
| 299 | ar & AR_DELETE ? 'd' : '-', SVFMT (filename)); | ||
| 300 | |||
| 301 | finish: | ||
| 302 | close_passwd_file (passwd); | ||
| 303 | return ret; | ||
| 304 | } | ||
| 305 | |||
| 306 | void | ||
| 307 | created_conf_file (struct string_view filename) | ||
| 308 | { | ||
| 309 | // TODO(cmmm): log | ||
| 310 | init_acl_for_file (filename, id); | ||
| 311 | } | ||
| 312 | |||
| 313 | void | ||
| 314 | deleted_conf_file (struct string_view filename) | ||
| 315 | { | ||
| 316 | const char *path = filename_to_acl_path (filename); | ||
| 317 | elevate (); | ||
| 318 | remove (path); | ||
| 319 | drop (); | ||
| 320 | // TODO(cmmm): log | ||
| 321 | } | ||
| 322 | |||
| 323 | bool | ||
| 324 | copy_file (struct string_view source, struct string_view destination) | ||
| 325 | { | ||
| 326 | int conf_source = is_conf (source); | ||
| 327 | int conf_destination = is_conf (destination); | ||
| 328 | |||
| 329 | if (conf_source && !conf_destination) { | ||
| 330 | fprintf (stderr, "access denied: no one can copy out of confdata\n"); | ||
| 331 | return false; | ||
| 332 | } | ||
| 333 | |||
| 334 | if (conf_source && !authorize (id, source, AR_READ )) return false; | ||
| 335 | if (conf_destination && !authorize (id, destination, AR_WRITE)) return false; | ||
| 336 | |||
| 337 | bool ret = true; | ||
| 338 | if (conf_source) elevate (); | ||
| 339 | int source_fd = open (source.begin, O_RDONLY); | ||
| 340 | if (conf_source) drop (); | ||
| 341 | if (source_fd == -1) { | ||
| 342 | fprintf (stderr, "error opening file for reading '%.*s': %s\n", | ||
| 343 | SVFMT (source), strerror (errno)); | ||
| 344 | return false; | ||
| 345 | } | ||
| 346 | |||
| 347 | struct stat st; | ||
| 348 | if (fstat (source_fd, &st) == -1) { | ||
| 349 | fprintf (stderr, "error getting file stats for '%.*s': %s\n", | ||
| 350 | SVFMT (source), strerror (errno)); | ||
| 351 | ret = false; | ||
| 352 | goto close_source; | ||
| 353 | } | ||
| 354 | |||
| 355 | if (!S_ISREG (st.st_mode)) { | ||
| 356 | fprintf (stderr, "source file is not regular '%.*s'\n", SVFMT (source)); | ||
| 357 | ret = false; | ||
| 358 | goto close_source; | ||
| 359 | } | ||
| 360 | |||
| 361 | if (conf_destination) elevate (); | ||
| 362 | int destination_fd = open (destination.begin, O_RDWR | O_CREAT | O_EXCL | | ||
| 363 | O_TRUNC, S_IRUSR | S_IWUSR); | ||
| 364 | if (conf_destination) drop (); | ||
| 365 | if (destination_fd == -1) { | ||
| 366 | fprintf (stderr, "error opening file for writing '%.*s': %s\n", | ||
| 367 | SVFMT (destination), strerror (errno)); | ||
| 368 | ret = false; | ||
| 369 | goto close_source; | ||
| 370 | } | ||
| 371 | |||
| 372 | if (ftruncate (destination_fd, st.st_size) == -1) { | ||
| 373 | fprintf (stderr, "error setting file size for '%.*s': %s\n", | ||
| 374 | SVFMT (destination), strerror (errno)); | ||
| 375 | ret = false; | ||
| 376 | goto close_destination; | ||
| 377 | } | ||
| 378 | |||
| 379 | void *source_map = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, source_fd, 0); | ||
| 380 | if (source_map == MAP_FAILED) { | ||
| 381 | fprintf (stderr, "error mapping source file '%.*s': %s\n", | ||
| 382 | SVFMT (source), strerror (errno)); | ||
| 383 | ret = false; | ||
| 384 | goto close_destination; | ||
| 385 | } | ||
| 386 | |||
| 387 | void *destination_map = mmap (NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, destination_fd, 0); | ||
| 388 | if (destination_map == MAP_FAILED) { | ||
| 389 | fprintf (stderr, "error mapping destination file '%.*s': %s\n", | ||
| 390 | SVFMT (destination), strerror (errno)); | ||
| 391 | ret = false; | ||
| 392 | goto munmap_source; | ||
| 393 | } | ||
| 394 | |||
| 395 | memcpy (destination_map, source_map, st.st_size); | ||
| 396 | |||
| 397 | if (conf_destination) { | ||
| 398 | elevate (); | ||
| 399 | char *canonical = realpath (destination.begin, NULL); | ||
| 400 | drop (); | ||
| 401 | assert (canonical); | ||
| 402 | string_builder_printf (&confdata_log, "%u user %d wrote to file '!%s'\n", | ||
| 403 | time (NULL), id, canonical + strlen (CONFDATA)); | ||
| 404 | free (canonical); | ||
| 405 | |||
| 406 | if (conf_destination != 2) | ||
| 407 | created_conf_file (destination); | ||
| 408 | } | ||
| 409 | |||
| 410 | munmap (destination_map, st.st_size); | ||
| 411 | munmap_source: | ||
| 412 | munmap (source_map, st.st_size); | ||
| 413 | close_destination: | ||
| 414 | close (destination_fd); | ||
| 415 | close_source: | ||
| 416 | close (source_fd); | ||
| 417 | |||
| 418 | elevate (); | ||
| 419 | if (conf_destination) chown (destination.begin, euid, egid); | ||
| 420 | else chown (destination.begin, ruid, rgid); | ||
| 421 | drop (); | ||
| 422 | |||
| 423 | return ret; | ||
| 424 | } | ||
| 425 | |||
| 426 | bool | ||
| 427 | rename_file (struct string_view source, struct string_view destination) | ||
| 428 | { | ||
| 429 | int conf_source = is_conf (source); | ||
| 430 | int conf_destination = is_conf (destination); | ||
| 431 | |||
| 432 | if (conf_source && !conf_destination) { | ||
| 433 | fprintf (stderr, "access denied: no one can rename out of confdata\n"); | ||
| 434 | return false; | ||
| 435 | } | ||
| 436 | |||
| 437 | if (conf_source && !authorize (id, source, AR_READ | AR_DELETE)) return false; | ||
| 438 | if (conf_destination && !authorize (id, destination, AR_WRITE )) return false; | ||
| 439 | |||
| 440 | struct stat st; | ||
| 441 | if (stat (source.begin, &st) == -1) { | ||
| 442 | fprintf (stderr, "error getting file stats for '%.*s': %s\n", | ||
| 443 | SVFMT (source), strerror (errno)); | ||
| 444 | return false; | ||
| 445 | } | ||
| 446 | |||
| 447 | if (!S_ISREG (st.st_mode)) { | ||
| 448 | fprintf (stderr, "source file is not regular '%.*s'\n", SVFMT (source)); | ||
| 449 | return false; | ||
| 450 | } | ||
| 451 | |||
| 452 | elevate (); | ||
| 453 | int res = access (destination.begin, F_OK); | ||
| 454 | drop (); | ||
| 455 | if (res == 0) { | ||
| 456 | fprintf (stderr, "destination file already exists '%.*s'\n", SVFMT (destination)); | ||
| 457 | return false; | ||
| 458 | } | ||
| 459 | |||
| 460 | char *canonical = NULL; | ||
| 461 | if (conf_source || conf_destination) elevate (); | ||
| 462 | if (conf_source) canonical = realpath (source.begin, NULL); | ||
| 463 | res = rename (source.begin, destination.begin); | ||
| 464 | if (conf_source || conf_destination) drop (); | ||
| 465 | if (res == -1) { | ||
| 466 | fprintf (stderr, "error renaming '%.*s' to '%.*s': %s\n", | ||
| 467 | SVFMT (source), SVFMT (destination), strerror (errno)); | ||
| 468 | free (canonical); | ||
| 469 | return false; | ||
| 470 | } | ||
| 471 | |||
| 472 | if (conf_destination) { | ||
| 473 | elevate (); | ||
| 474 | char *canonical = realpath (destination.begin, NULL); | ||
| 475 | drop (); | ||
| 476 | assert (canonical); | ||
| 477 | string_builder_printf (&confdata_log, "%u user %d wrote to file '!%s'\n", | ||
| 478 | time (NULL), id, canonical + strlen (CONFDATA)); | ||
| 479 | free (canonical); | ||
| 480 | |||
| 481 | if (conf_destination != 2) | ||
| 482 | created_conf_file (destination); | ||
| 483 | } | ||
| 484 | |||
| 485 | if (conf_source) { | ||
| 486 | assert (canonical); | ||
| 487 | string_builder_printf (&confdata_log, "%u user %d removed file '!%s'\n", | ||
| 488 | time (NULL), id, canonical + strlen (CONFDATA)); | ||
| 489 | free (canonical); | ||
| 490 | |||
| 491 | deleted_conf_file (source); | ||
| 492 | } | ||
| 493 | |||
| 494 | elevate (); | ||
| 495 | if (conf_destination) chown (destination.begin, euid, egid); | ||
| 496 | else chown (destination.begin, ruid, rgid); | ||
| 497 | drop (); | ||
| 498 | |||
| 499 | return true; | ||
| 500 | } | ||
| 501 | |||
| 502 | int | ||
| 503 | parse_command_line (struct command_line *cl, struct string_view line) | ||
| 504 | { | ||
| 505 | DYNAMIC_ARRAY_RESET (cl->expanded); | ||
| 506 | DYNAMIC_ARRAY_RESET (*cl); | ||
| 507 | cl->command = SVNULL; | ||
| 508 | |||
| 509 | // printf ("line: %.*s\n", SVFMT (line)); | ||
| 510 | |||
| 511 | const char *head = line.begin; | ||
| 512 | const char *end = line.end; | ||
| 513 | for (; head < end; ++head) { | ||
| 514 | if (*head == ' ' || *head == '\t') { | ||
| 515 | do head += 1; | ||
| 516 | while (head < end && (*head == ' ' || *head == '\t')); | ||
| 517 | if (head == end) | ||
| 518 | break; | ||
| 519 | if (cl->expanded.length) | ||
| 520 | STRING_BUILDER_APPEND_CHAR (cl->expanded, '\0'); | ||
| 521 | } | ||
| 522 | |||
| 523 | if (*head == '\'') { | ||
| 524 | head += 1; | ||
| 525 | if (head == end) return 0; | ||
| 526 | while (head < end && *head != '\'') | ||
| 527 | STRING_BUILDER_APPEND_CHAR (cl->expanded, *head++); | ||
| 528 | if (head == end && (head[-1] != '\'' || head - 1 == line.begin)) return 0; | ||
| 529 | } else if (*head == '"') { | ||
| 530 | head += 1; | ||
| 531 | if (head == end) return 0; | ||
| 532 | if (*head == '!') { | ||
| 533 | STRING_BUILDER_APPEND_CSTR (cl->expanded, CONFDATA); | ||
| 534 | head += 1; | ||
| 535 | } | ||
| 536 | while (head < end && *head != '"') | ||
| 537 | STRING_BUILDER_APPEND_CHAR (cl->expanded, *head++); | ||
| 538 | if (head == end && (head[-1] != '"' || head - 1 == line.begin)) return 0; | ||
| 539 | } else if (*head == '!' && (cl->expanded.length == 0 || DYNAMIC_ARRAY_LAST (cl->expanded) == '\0')) { | ||
| 540 | STRING_BUILDER_APPEND_CSTR (cl->expanded, CONFDATA); | ||
| 541 | } else { | ||
| 542 | STRING_BUILDER_APPEND_CHAR (cl->expanded, *head); | ||
| 543 | } | ||
| 544 | } STRING_BUILDER_APPEND_CHAR (cl->expanded, '\0'); | ||
| 545 | |||
| 546 | head = cl->expanded.string; | ||
| 547 | end = cl->expanded.string + cl->expanded.length; | ||
| 548 | cl->command.begin = head; | ||
| 549 | while (head < end && *head != '\0') | ||
| 550 | head += 1; | ||
| 551 | cl->command.end = head++; | ||
| 552 | |||
| 553 | if (SVLEN (cl->command) == 0) return 0; | ||
| 554 | |||
| 555 | while (head < end) { | ||
| 556 | struct string_view arg; | ||
| 557 | arg.begin = head; | ||
| 558 | do head += 1; | ||
| 559 | while (head < end && *head != '\0'); | ||
| 560 | arg.end = head++; | ||
| 561 | |||
| 562 | const char *star = strchr (arg.begin, '*'); | ||
| 563 | struct string_view trimmed = arg; | ||
| 564 | if (star) { | ||
| 565 | if (star == arg.begin) { | ||
| 566 | trimmed = SVCSTR ("."); | ||
| 567 | } else { | ||
| 568 | trimmed = SVDUP0 ((struct string_view) { | ||
| 569 | .begin = arg.begin, | ||
| 570 | .end = star, | ||
| 571 | }); | ||
| 572 | } | ||
| 573 | } | ||
| 574 | |||
| 575 | bool conf = is_conf (trimmed); | ||
| 576 | glob_t globs; | ||
| 577 | if (conf) elevate (); | ||
| 578 | int ret = glob (arg.begin, GLOB_MARK | GLOB_NOSORT | GLOB_BRACE | GLOB_TILDE_CHECK, NULL, &globs); | ||
| 579 | if (conf) drop (); | ||
| 580 | if (ret != 0) { | ||
| 581 | DYNAMIC_ARRAY_APPEND (*cl, arg); | ||
| 582 | } else { | ||
| 583 | for (size_t i = 0; i < globs.gl_pathc; ++i) { | ||
| 584 | arg.begin = globs.gl_pathv[i]; | ||
| 585 | arg.end = arg.begin + strlen (arg.begin); | ||
| 586 | DYNAMIC_ARRAY_APPEND (*cl, SVDUP0 (arg)); | ||
| 587 | } | ||
| 588 | } | ||
| 589 | |||
| 590 | globfree (&globs); | ||
| 591 | } | ||
| 592 | |||
| 593 | // printf ("command: %.*s\n", SVFMT (cl->command)); | ||
| 594 | // for (uint32_t i = 0; i < cl->argc; ++i) | ||
| 595 | // printf ("arg: %.*s\n", SVFMT (cl->args[i])); | ||
| 596 | |||
| 597 | return 1; | ||
| 598 | } | ||
| 599 | |||
| 600 | int | ||
| 601 | command_exit (struct command_line cl) | ||
| 602 | { | ||
| 603 | exit (0); | ||
| 604 | } | ||
| 605 | |||
| 606 | int | ||
| 607 | command_help (struct command_line cl) | ||
| 608 | { | ||
| 609 | puts ("Commands:"); | ||
| 610 | puts (" help display information about builtin commands"); | ||
| 611 | puts (" su LOGIN change user to LOGIN"); | ||
| 612 | puts (" pwd print the name of the current working directory "); | ||
| 613 | puts (" whoami print effective user name"); | ||
| 614 | puts (" echo [ARG]... write ARG(s) to the standard output"); | ||
| 615 | puts (" ls [FILE]... list information about the FILE(s)"); | ||
| 616 | puts (" cd [DIR] change the shell working directory"); | ||
| 617 | puts (" mv SOURCE DEST rename SOURCE to DEST"); | ||
| 618 | puts (" mv SOURCE... DIRECTORY move SOURCE(s) to DIRECTORY"); | ||
| 619 | puts (" cp SOURCE DEST copy SOURCE to DEST"); | ||
| 620 | puts (" cp SOURCE... DIRECTORY copy SOURCE(s) to DIRECTORY"); | ||
| 621 | puts (" rm FILE... remove (unlink) the FILE(s)"); | ||
| 622 | puts (" mkdir DIR... create the DIRECTORY(ies), if they do not already exist"); | ||
| 623 | puts (" rmdir DIR... remove the DIRECTORY(ies), if they are empty"); | ||
| 624 | puts (" cat [FILE]... concatenate FILE(s) to standard output"); | ||
| 625 | puts (" append FILE append text from standard input to FILE"); | ||
| 626 | puts (" chown LOGIN FILE... change owner of FILE(s) to LOGIN (root only)"); | ||
| 627 | puts (" chsl SECURITY-LEVEL FILE... change security level of FILE(s) to SECURITY-LEVEL (root only)"); | ||
| 628 | puts (" grant LOGIN {r|w|d}... FILE... grant specified access to FILE(s) for LOGIN"); | ||
| 629 | puts (" revoke LOGIN {r|w|d}... FILE... revoke specified access to FILE(s) from LOGIN"); | ||
| 630 | puts (""); | ||
| 631 | puts ("Aliases:"); | ||
| 632 | puts (" read -> cat"); | ||
| 633 | puts (" copy -> cp"); | ||
| 634 | puts (" remove -> rm"); | ||
| 635 | puts (""); | ||
| 636 | puts ("Security levels:"); | ||
| 637 | puts (" top-secret"); | ||
| 638 | puts (" secret"); | ||
| 639 | puts (" confidential"); | ||
| 640 | puts (" unclassified"); | ||
| 641 | puts (""); | ||
| 642 | return 0; | ||
| 643 | } | ||
| 644 | |||
| 645 | int | ||
| 646 | command_su (struct command_line cl) | ||
| 647 | { | ||
| 648 | if (cl.argc != 1) { | ||
| 649 | fprintf (stderr, "usage: %.*s LOGIN\n", SVFMT (cl.command)); | ||
| 650 | return 1; | ||
| 651 | } | ||
| 652 | |||
| 653 | struct string_view login = cl.args[0]; | ||
| 654 | int ret = 0; | ||
| 655 | struct passwd passwd = load_passwd_file (); | ||
| 656 | struct passwd_entry *entry; | ||
| 657 | if (getuid () == 0) { | ||
| 658 | entry = lookup_login (passwd, login); | ||
| 659 | if (!entry) { | ||
| 660 | fprintf (stderr, "user %.*s doesn't exist\n", SVFMT (login)); | ||
| 661 | ret = 1; | ||
| 662 | goto close; | ||
| 663 | } | ||
| 664 | } else { | ||
| 665 | fputs ("Password: ", stdout); | ||
| 666 | struct string_view password = read_password (); | ||
| 667 | entry = authenticate (passwd, login, password); | ||
| 668 | if (!entry) { | ||
| 669 | ret = 1; | ||
| 670 | goto close; | ||
| 671 | } | ||
| 672 | } | ||
| 673 | id = entry->id; | ||
| 674 | |||
| 675 | close: | ||
| 676 | close_passwd_file (passwd); | ||
| 677 | return ret; | ||
| 678 | } | ||
| 679 | |||
| 680 | enum access_rights | ||
| 681 | parse_access_rights (struct string_view sv) | ||
| 682 | { | ||
| 683 | enum access_rights ar = 0; | ||
| 684 | for (const char *head = sv.begin; head < sv.end; ++head) { | ||
| 685 | switch (*head) { | ||
| 686 | case 'r': ar |= AR_READ; continue; | ||
| 687 | case 'w': ar |= AR_WRITE; continue; | ||
| 688 | case 'd': ar |= AR_DELETE; continue; | ||
| 689 | default: return 0; | ||
| 690 | } | ||
| 691 | } | ||
| 692 | return ar; | ||
| 693 | } | ||
| 694 | |||
| 695 | int | ||
| 696 | command_grant (struct command_line cl) | ||
| 697 | { | ||
| 698 | static struct string_builder sb = {0}; | ||
| 699 | |||
| 700 | if (cl.argc < 3) { | ||
| 701 | fprintf (stderr, "usage: %.*s LOGIN {r|w|d}... FILE...\n", SVFMT (cl.command)); | ||
| 702 | return 1; | ||
| 703 | } | ||
| 704 | |||
| 705 | struct string_view login = cl.args[0]; | ||
| 706 | struct passwd passwd = load_passwd_file (); | ||
| 707 | struct passwd_entry *entry = lookup_login (passwd, login); | ||
| 708 | int login_id = entry ? entry->id : -1; | ||
| 709 | close_passwd_file (passwd); | ||
| 710 | |||
| 711 | if (!entry) { | ||
| 712 | fprintf (stderr, "%.*s: user doesn't exist '%.*s'\n", | ||
| 713 | SVFMT (cl.command), SVFMT (login)); | ||
| 714 | return 1; | ||
| 715 | } | ||
| 716 | |||
| 717 | enum access_rights access_rights = parse_access_rights (cl.args[1]); | ||
| 718 | if (!access_rights) { | ||
| 719 | fprintf (stderr, "%.*s: invalid access rights '%.*s'\n", | ||
| 720 | SVFMT (cl.command), SVFMT (cl.args[1])); | ||
| 721 | return 1; | ||
| 722 | } | ||
| 723 | |||
| 724 | int ret = 0; | ||
| 725 | for (uint32_t i = 2; i < cl.argc; ++i) { | ||
| 726 | struct string_view file = cl.args[i]; | ||
| 727 | int conf = is_conf (file); | ||
| 728 | |||
| 729 | if (conf == 0) { | ||
| 730 | fprintf (stderr, "%.*s: warning: skipping file outside of confdata '%.*s'\n", | ||
| 731 | SVFMT (cl.command), SVFMT (file)); | ||
| 732 | continue; | ||
| 733 | } | ||
| 734 | |||
| 735 | if (conf == 1) { | ||
| 736 | fprintf (stderr, "%.*s: warning: skipping nonexistent file '%.*s'\n", | ||
| 737 | SVFMT (cl.command), SVFMT (file)); | ||
| 738 | continue; | ||
| 739 | } | ||
| 740 | |||
| 741 | struct acl *acl = load_acl_for_file (file); | ||
| 742 | ASSERT (acl); | ||
| 743 | if (acl->owner != id && ruid != euid) { | ||
| 744 | fprintf (stderr, "%.*s: error: permission denied: not owner of the file '%.*s'\n", | ||
| 745 | SVFMT (cl.command), SVFMT (file)); | ||
| 746 | ret = 1; | ||
| 747 | continue; | ||
| 748 | } | ||
| 749 | |||
| 750 | for (int i = 0; i < acl->grant_count; ++i) { | ||
| 751 | if (acl->grants[i].id != login_id) | ||
| 752 | continue; | ||
| 753 | |||
| 754 | acl->grants[i].access_rights |= access_rights; | ||
| 755 | goto save; | ||
| 756 | } | ||
| 757 | acl->grants[acl->grant_count++] = (struct acl_grant) { | ||
| 758 | .id = login_id, | ||
| 759 | .access_rights = access_rights, | ||
| 760 | }; | ||
| 761 | |||
| 762 | save: | ||
| 763 | save_acl (acl); | ||
| 764 | free (acl); | ||
| 765 | } | ||
| 766 | |||
| 767 | return ret; | ||
| 768 | } | ||
| 769 | |||
| 770 | int | ||
| 771 | command_revoke (struct command_line cl) | ||
| 772 | { | ||
| 773 | static struct string_builder sb = {0}; | ||
| 774 | |||
| 775 | if (cl.argc < 3) { | ||
| 776 | fprintf (stderr, "usage: %.*s LOGIN {r|w|d}... FILE...\n", SVFMT (cl.command)); | ||
| 777 | return 1; | ||
| 778 | } | ||
| 779 | |||
| 780 | struct string_view login = cl.args[0]; | ||
| 781 | struct passwd passwd = load_passwd_file (); | ||
| 782 | struct passwd_entry *entry = lookup_login (passwd, login); | ||
| 783 | int login_id = entry ? entry->id : -1; | ||
| 784 | close_passwd_file (passwd); | ||
| 785 | |||
| 786 | if (!entry) { | ||
| 787 | fprintf (stderr, "%.*s: user doesn't exist '%.*s'\n", | ||
| 788 | SVFMT (cl.command), SVFMT (login)); | ||
| 789 | return 1; | ||
| 790 | } | ||
| 791 | |||
| 792 | enum access_rights access_rights = parse_access_rights (cl.args[1]); | ||
| 793 | if (!access_rights) { | ||
| 794 | fprintf (stderr, "%.*s: invalid access rights '%.*s'\n", | ||
| 795 | SVFMT (cl.command), SVFMT (cl.args[1])); | ||
| 796 | return 1; | ||
| 797 | } | ||
| 798 | |||
| 799 | int ret = 0; | ||
| 800 | for (uint32_t i = 2; i < cl.argc; ++i) { | ||
| 801 | struct string_view file = cl.args[i]; | ||
| 802 | int conf = is_conf (file); | ||
| 803 | |||
| 804 | if (conf == 0) { | ||
| 805 | fprintf (stderr, "%.*s: warning: skipping file outside of confdata '%.*s'\n", | ||
| 806 | SVFMT (cl.command), SVFMT (file)); | ||
| 807 | continue; | ||
| 808 | } | ||
| 809 | |||
| 810 | if (conf == 1) { | ||
| 811 | fprintf (stderr, "%.*s: warning: skipping nonexistent file '%.*s'\n", | ||
| 812 | SVFMT (cl.command), SVFMT (file)); | ||
| 813 | continue; | ||
| 814 | } | ||
| 815 | |||
| 816 | struct acl *acl = load_acl_for_file (file); | ||
| 817 | ASSERT (acl); | ||
| 818 | if (acl->owner != id && ruid != euid) { | ||
| 819 | fprintf (stderr, "%.*s: error: permission denied: not owner of the file '%.*s'\n", | ||
| 820 | SVFMT (cl.command), SVFMT (file)); | ||
| 821 | ret = 1; | ||
| 822 | continue; | ||
| 823 | } | ||
| 824 | |||
| 825 | for (int i = 0; i < acl->grant_count; ++i) { | ||
| 826 | if (acl->grants[i].id != login_id) | ||
| 827 | continue; | ||
| 828 | |||
| 829 | acl->grants[i].access_rights &= ~access_rights; | ||
| 830 | if (acl->grants[i].access_rights == 0) | ||
| 831 | acl->grants[i] = acl->grants[--acl->grant_count]; | ||
| 832 | |||
| 833 | goto save; | ||
| 834 | } | ||
| 835 | |||
| 836 | save: | ||
| 837 | save_acl (acl); | ||
| 838 | free (acl); | ||
| 839 | } | ||
| 840 | |||
| 841 | return ret; | ||
| 842 | } | ||
| 843 | |||
| 844 | int | ||
| 845 | command_chown (struct command_line cl) | ||
| 846 | { | ||
| 847 | static struct string_builder sb = {0}; | ||
| 848 | |||
| 849 | if (cl.argc < 2) { | ||
| 850 | fprintf (stderr, "usage: %.*s LOGIN FILE...\n", SVFMT (cl.command)); | ||
| 851 | return 1; | ||
| 852 | } | ||
| 853 | |||
| 854 | if (euid != ruid) { | ||
| 855 | fprintf (stderr, "%.*s: error: only superuser can change file ownership\n", | ||
| 856 | SVFMT (cl.command)); | ||
| 857 | return 1; | ||
| 858 | } | ||
| 859 | |||
| 860 | struct string_view login = cl.args[0]; | ||
| 861 | struct passwd passwd = load_passwd_file (); | ||
| 862 | struct passwd_entry *entry = lookup_login (passwd, login); | ||
| 863 | int login_id = entry ? entry->id : -1; | ||
| 864 | close_passwd_file (passwd); | ||
| 865 | |||
| 866 | if (!entry) { | ||
| 867 | fprintf (stderr, "%.*s: user doesn't exist '%.*s'\n", | ||
| 868 | SVFMT (cl.command), SVFMT (login)); | ||
| 869 | return 1; | ||
| 870 | } | ||
| 871 | |||
| 872 | int ret = 0; | ||
| 873 | for (uint32_t i = 1; i < cl.argc; ++i) { | ||
| 874 | struct string_view file = cl.args[i]; | ||
| 875 | int conf = is_conf (file); | ||
| 876 | |||
| 877 | if (conf == 0) { | ||
| 878 | fprintf (stderr, "%.*s: warning: skipping file outside of confdata '%.*s'\n", | ||
| 879 | SVFMT (cl.command), SVFMT (file)); | ||
| 880 | continue; | ||
| 881 | } | ||
| 882 | |||
| 883 | if (conf == 1) { | ||
| 884 | fprintf (stderr, "%.*s: warning: skipping nonexistent file '%.*s'\n", | ||
| 885 | SVFMT (cl.command), SVFMT (file)); | ||
| 886 | continue; | ||
| 887 | } | ||
| 888 | |||
| 889 | struct acl *acl = load_acl_for_file (file); | ||
| 890 | ASSERT (acl); | ||
| 891 | acl->owner = login_id; | ||
| 892 | save_acl (acl); | ||
| 893 | free (acl); | ||
| 894 | } | ||
| 895 | |||
| 896 | return ret; | ||
| 897 | } | ||
| 898 | |||
| 899 | enum security_level | ||
| 900 | parse_security_level (struct string_view sv) | ||
| 901 | { | ||
| 902 | for (enum security_level sl = 0; sl < SL_COUNT; ++sl) { | ||
| 903 | if (SVCMP (sv, SVCSTR (security_level_names[sl])) == 0) | ||
| 904 | return sl; | ||
| 905 | } | ||
| 906 | |||
| 907 | return SL_COUNT; | ||
| 908 | } | ||
| 909 | |||
| 910 | int | ||
| 911 | command_chsl (struct command_line cl) | ||
| 912 | { | ||
| 913 | static struct string_builder sb = {0}; | ||
| 914 | |||
| 915 | if (cl.argc < 2) { | ||
| 916 | fprintf (stderr, "usage: %.*s LOGIN FILE...\n", SVFMT (cl.command)); | ||
| 917 | return 1; | ||
| 918 | } | ||
| 919 | |||
| 920 | if (euid != ruid) { | ||
| 921 | fprintf (stderr, "%.*s: error: only superuser can change file security level\n", | ||
| 922 | SVFMT (cl.command)); | ||
| 923 | return 1; | ||
| 924 | } | ||
| 925 | |||
| 926 | enum security_level security_level = parse_security_level (cl.args[0]); | ||
| 927 | if (security_level == SL_COUNT) { | ||
| 928 | fprintf (stderr, "%.*s: error: invalid security level, use command 'help' to get listing of valid security levels\n", | ||
| 929 | SVFMT (cl.command)); | ||
| 930 | return 1; | ||
| 931 | } | ||
| 932 | |||
| 933 | int ret = 0; | ||
| 934 | for (uint32_t i = 1; i < cl.argc; ++i) { | ||
| 935 | struct string_view file = cl.args[i]; | ||
| 936 | int conf = is_conf (file); | ||
| 937 | |||
| 938 | if (conf == 0) { | ||
| 939 | fprintf (stderr, "%.*s: warning: skipping file outside of confdata '%.*s'\n", | ||
| 940 | SVFMT (cl.command), SVFMT (file)); | ||
| 941 | continue; | ||
| 942 | } | ||
| 943 | |||
| 944 | if (conf == 1) { | ||
| 945 | fprintf (stderr, "%.*s: warning: skipping nonexistent file '%.*s'\n", | ||
| 946 | SVFMT (cl.command), SVFMT (file)); | ||
| 947 | continue; | ||
| 948 | } | ||
| 949 | |||
| 950 | struct acl *acl = load_acl_for_file (file); | ||
| 951 | ASSERT (acl); | ||
| 952 | acl->security_level = security_level; | ||
| 953 | save_acl (acl); | ||
| 954 | free (acl); | ||
| 955 | } | ||
| 956 | |||
| 957 | return ret; | ||
| 958 | } | ||
| 959 | |||
| 960 | int | ||
| 961 | command_pwd (struct command_line cl) | ||
| 962 | { | ||
| 963 | size_t size = 256; | ||
| 964 | |||
| 965 | for (;;) { | ||
| 966 | char *buffer = (char *) malloc (size); | ||
| 967 | if (getcwd (buffer, size) == buffer) { | ||
| 968 | puts (buffer); | ||
| 969 | free (buffer); | ||
| 970 | return 0; | ||
| 971 | } | ||
| 972 | free (buffer); | ||
| 973 | if (errno != ERANGE) | ||
| 974 | return errno; | ||
| 975 | size *= 2; | ||
| 976 | } | ||
| 977 | } | ||
| 978 | |||
| 979 | int | ||
| 980 | command_whoami (struct command_line cl) | ||
| 981 | { | ||
| 982 | int ret = 0; | ||
| 983 | struct passwd passwd = load_passwd_file (); | ||
| 984 | struct passwd_entry *entry = lookup_id (passwd, id); | ||
| 985 | if (!entry) { | ||
| 986 | fprintf (stderr, "user with id %d no longed exists\n", id); | ||
| 987 | ret = 1; | ||
| 988 | goto close; | ||
| 989 | } | ||
| 990 | |||
| 991 | printf ("%.*s", SVFMT (entry->login)); | ||
| 992 | |||
| 993 | #ifdef MAC | ||
| 994 | printf (" (%s)", security_level_names[entry->security_level]); | ||
| 995 | #endif | ||
| 996 | |||
| 997 | putchar ('\n'); | ||
| 998 | |||
| 999 | close: | ||
| 1000 | close_passwd_file (passwd); | ||
| 1001 | return ret; | ||
| 1002 | } | ||
| 1003 | |||
| 1004 | int | ||
| 1005 | command_echo (struct command_line cl) | ||
| 1006 | { | ||
| 1007 | if (cl.argc == 0) | ||
| 1008 | return 0; | ||
| 1009 | |||
| 1010 | fputs (cl.args[0].begin, stdout); | ||
| 1011 | for (uint32_t argind = 1; argind < cl.argc; ++argind) { | ||
| 1012 | putchar (' '); | ||
| 1013 | fputs (cl.args[argind].begin, stdout); | ||
| 1014 | } putchar ('\n'); | ||
| 1015 | |||
| 1016 | return 0; | ||
| 1017 | } | ||
| 1018 | |||
| 1019 | int | ||
| 1020 | command_cd (struct command_line cl) | ||
| 1021 | { | ||
| 1022 | struct string_view dir = cl.argc ? cl.args[0] : SVCSTR (CONFDATA); | ||
| 1023 | bool conf = string_view_starts_with (dir, SVCSTR (CONFDATA)); | ||
| 1024 | |||
| 1025 | if (conf) elevate (); | ||
| 1026 | if (chdir (dir.begin) == -1) { | ||
| 1027 | fprintf (stderr, "%.*s: cannot change directory to '%s': %s\n", | ||
| 1028 | SVFMT (cl.command), dir.begin, strerror (errno)); | ||
| 1029 | } | ||
| 1030 | if (conf) drop (); | ||
| 1031 | |||
| 1032 | return 0; | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | bool | ||
| 1036 | is_dir (struct string_view filename) | ||
| 1037 | { | ||
| 1038 | struct stat st; | ||
| 1039 | |||
| 1040 | elevate (); | ||
| 1041 | int res = stat (filename.begin, &st); | ||
| 1042 | drop (); | ||
| 1043 | |||
| 1044 | if (res == -1) | ||
| 1045 | return false; | ||
| 1046 | |||
| 1047 | return S_ISDIR (st.st_mode); | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | int | ||
| 1051 | command_mv (struct command_line cl) | ||
| 1052 | { | ||
| 1053 | static struct string_builder sb = {0}; | ||
| 1054 | |||
| 1055 | if (cl.argc < 2) { | ||
| 1056 | fprintf (stderr, "usage: %.*s SOURCE DEST\n", SVFMT (cl.command)); | ||
| 1057 | fprintf (stderr, "or %.*s SOURCE... DIRECTORY\n", SVFMT (cl.command)); | ||
| 1058 | return 1; | ||
| 1059 | } | ||
| 1060 | |||
| 1061 | int ret = 0; | ||
| 1062 | |||
| 1063 | if (is_dir (cl.args[cl.argc - 1])) { | ||
| 1064 | STRING_BUILDER_RESET (sb); | ||
| 1065 | |||
| 1066 | struct string_view directory = cl.args[cl.argc - 1]; | ||
| 1067 | STRING_BUILDER_APPEND_STRING_VIEW (sb, directory); | ||
| 1068 | if (DYNAMIC_ARRAY_LAST (sb) != '/') | ||
| 1069 | STRING_BUILDER_APPEND_CHAR (sb, '/'); | ||
| 1070 | |||
| 1071 | size_t directory_length = sb.length; | ||
| 1072 | for (uint32_t i = 0; i < cl.argc - 1; ++i) { | ||
| 1073 | sb.length = directory_length; | ||
| 1074 | struct string_view source = cl.args[i]; | ||
| 1075 | |||
| 1076 | struct string_view basename = string_view_basename (source); | ||
| 1077 | STRING_BUILDER_APPEND_STRING_VIEW (sb, basename); | ||
| 1078 | STRING_BUILDER_TERMINATE (sb); | ||
| 1079 | struct string_view destination = SVSB (sb); | ||
| 1080 | |||
| 1081 | if (!rename_file (source, destination)) { | ||
| 1082 | fprintf (stderr, "mv: failed to rename '%.*s' -> '%.*s'\n", | ||
| 1083 | SVFMT (source), SVFMT (destination)); | ||
| 1084 | ret = 1; | ||
| 1085 | } | ||
| 1086 | } | ||
| 1087 | } else { | ||
| 1088 | if (cl.argc != 2) { | ||
| 1089 | fprintf (stderr, "usage: %.*s SOURCE DEST\n", SVFMT (cl.command)); | ||
| 1090 | fprintf (stderr, "or %.*s SOURCE... DIRECTORY\n", SVFMT (cl.command)); | ||
| 1091 | return 1; | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | struct string_view source = cl.args[0]; | ||
| 1095 | struct string_view destination = cl.args[1]; | ||
| 1096 | if (!rename_file (source, destination)) { | ||
| 1097 | fprintf (stderr, "mv: failed to rename '%.*s' -> '%.*s'\n", | ||
| 1098 | SVFMT (source), SVFMT (destination)); | ||
| 1099 | ret = 1; | ||
| 1100 | } | ||
| 1101 | } | ||
| 1102 | |||
| 1103 | return ret; | ||
| 1104 | } | ||
| 1105 | |||
| 1106 | int | ||
| 1107 | command_cp (struct command_line cl) | ||
| 1108 | { | ||
| 1109 | static struct string_builder sb = {0}; | ||
| 1110 | |||
| 1111 | if (cl.argc < 2) { | ||
| 1112 | fprintf (stderr, "usage: %.*s SOURCE DEST\n", SVFMT (cl.command)); | ||
| 1113 | fprintf (stderr, "or %.*s SOURCE... DIRECTORY\n", SVFMT (cl.command)); | ||
| 1114 | return 1; | ||
| 1115 | } | ||
| 1116 | |||
| 1117 | int ret = 0; | ||
| 1118 | |||
| 1119 | if (is_dir (cl.args[cl.argc - 1])) { | ||
| 1120 | STRING_BUILDER_RESET (sb); | ||
| 1121 | |||
| 1122 | struct string_view directory = cl.args[cl.argc - 1]; | ||
| 1123 | STRING_BUILDER_APPEND_STRING_VIEW (sb, directory); | ||
| 1124 | if (DYNAMIC_ARRAY_LAST (sb) != '/') | ||
| 1125 | STRING_BUILDER_APPEND_CHAR (sb, '/'); | ||
| 1126 | |||
| 1127 | size_t directory_length = sb.length; | ||
| 1128 | for (uint32_t i = 0; i < cl.argc - 1; ++i) { | ||
| 1129 | sb.length = directory_length; | ||
| 1130 | struct string_view source = cl.args[i]; | ||
| 1131 | |||
| 1132 | struct string_view basename = string_view_basename (source); | ||
| 1133 | STRING_BUILDER_APPEND_STRING_VIEW (sb, basename); | ||
| 1134 | STRING_BUILDER_TERMINATE (sb); | ||
| 1135 | struct string_view destination = SVSB (sb); | ||
| 1136 | |||
| 1137 | if (!copy_file (source, destination)) { | ||
| 1138 | fprintf (stderr, "cp: failed to copy '%.*s' -> '%.*s'\n", | ||
| 1139 | SVFMT (source), SVFMT (destination)); | ||
| 1140 | ret = 1; | ||
| 1141 | } | ||
| 1142 | } | ||
| 1143 | } else { | ||
| 1144 | if (cl.argc != 2) { | ||
| 1145 | fprintf (stderr, "usage: %.*s SOURCE DEST\n", SVFMT (cl.command)); | ||
| 1146 | fprintf (stderr, "or %.*s SOURCE... DIRECTORY\n", SVFMT (cl.command)); | ||
| 1147 | return 1; | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | struct string_view source = cl.args[0]; | ||
| 1151 | struct string_view destination = cl.args[1]; | ||
| 1152 | if (!copy_file (source, destination)) { | ||
| 1153 | fprintf (stderr, "cp: failed to copy '%.*s' -> '%.*s'\n", | ||
| 1154 | SVFMT (source), SVFMT (destination)); | ||
| 1155 | ret = 1; | ||
| 1156 | } | ||
| 1157 | } | ||
| 1158 | |||
| 1159 | return ret; | ||
| 1160 | } | ||
| 1161 | |||
| 1162 | int | ||
| 1163 | command_rm (struct command_line cl) | ||
| 1164 | { | ||
| 1165 | if (cl.argc == 0) { | ||
| 1166 | fprintf (stderr, "usage: %.*s DIR...\n", SVFMT (cl.command)); | ||
| 1167 | return 1; | ||
| 1168 | } | ||
| 1169 | |||
| 1170 | int ret = 0; | ||
| 1171 | for (uint32_t argind = 0; argind < cl.argc; ++argind) { | ||
| 1172 | struct string_view file = cl.args[argind]; | ||
| 1173 | const char *filename = file.begin; | ||
| 1174 | |||
| 1175 | int conf = is_conf (file); | ||
| 1176 | if (conf && !authorize (id, file, AR_DELETE)) { | ||
| 1177 | ret = 1; | ||
| 1178 | continue; | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | char *canonical = NULL; | ||
| 1182 | if (conf) { | ||
| 1183 | elevate (); | ||
| 1184 | canonical = realpath (filename, NULL); | ||
| 1185 | } | ||
| 1186 | int res = remove (filename); | ||
| 1187 | if (conf) drop (); | ||
| 1188 | if (res == -1) { | ||
| 1189 | fprintf (stderr, "%.*s: cannot remove file '%s': %s\n", | ||
| 1190 | SVFMT (cl.command), filename, strerror (errno)); | ||
| 1191 | ret = 1; | ||
| 1192 | free (canonical); | ||
| 1193 | } else if (conf) { | ||
| 1194 | assert (canonical); | ||
| 1195 | string_builder_printf (&confdata_log, "%u user %d removed file '!%s'\n", | ||
| 1196 | time (NULL), id, canonical + strlen (CONFDATA)); | ||
| 1197 | free (canonical); | ||
| 1198 | deleted_conf_file (file); | ||
| 1199 | } | ||
| 1200 | } | ||
| 1201 | |||
| 1202 | return ret; | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | int | ||
| 1206 | command_mkdir (struct command_line cl) | ||
| 1207 | { | ||
| 1208 | static struct string_builder sb = {0}; | ||
| 1209 | |||
| 1210 | if (cl.argc == 0) { | ||
| 1211 | fprintf (stderr, "usage: %.*s DIR...\n", SVFMT (cl.command)); | ||
| 1212 | return 1; | ||
| 1213 | } | ||
| 1214 | |||
| 1215 | int ret = 0; | ||
| 1216 | for (uint32_t argind = 0; argind < cl.argc; ++argind) { | ||
| 1217 | struct string_view file = cl.args[argind]; | ||
| 1218 | const char *filename = file.begin; | ||
| 1219 | |||
| 1220 | bool conf = is_conf (file); | ||
| 1221 | if (conf) { | ||
| 1222 | STRING_BUILDER_RESET (sb); | ||
| 1223 | STRING_BUILDER_APPEND_STRING_VIEW (sb, string_view_dirname (canonicalize (file))); | ||
| 1224 | if (sb.string[sb.length - 1] != '/') | ||
| 1225 | STRING_BUILDER_APPEND_CHAR (sb, '/'); | ||
| 1226 | if (!authorize (id, SVSB (sb), AR_WRITE)) { | ||
| 1227 | ret = 1; | ||
| 1228 | continue; | ||
| 1229 | } | ||
| 1230 | } | ||
| 1231 | |||
| 1232 | if (conf) elevate (); | ||
| 1233 | int res = mkdir (filename, S_IRUSR | S_IWUSR | S_IXUSR); | ||
| 1234 | if (conf) drop (); | ||
| 1235 | if (res == -1) { | ||
| 1236 | fprintf (stderr, "%.*s: cannot create directory '%s': %s\n", | ||
| 1237 | SVFMT (cl.command), filename, strerror (errno)); | ||
| 1238 | ret = 1; | ||
| 1239 | } else if (conf) { | ||
| 1240 | STRING_BUILDER_RESET (sb); | ||
| 1241 | STRING_BUILDER_APPEND_STRING_VIEW (sb, file); | ||
| 1242 | if (sb.string[sb.length - 1] != '/') | ||
| 1243 | STRING_BUILDER_APPEND_CHAR (sb, '/'); | ||
| 1244 | created_conf_file (SVSB (sb)); | ||
| 1245 | elevate (); | ||
| 1246 | char *canonical = realpath (filename, NULL); | ||
| 1247 | drop (); | ||
| 1248 | assert (canonical); | ||
| 1249 | string_builder_printf (&confdata_log, "%u user %d created directory '!%s'\n", | ||
| 1250 | time (NULL), id, canonical + strlen (CONFDATA)); | ||
| 1251 | free (canonical); | ||
| 1252 | } | ||
| 1253 | } | ||
| 1254 | |||
| 1255 | return ret; | ||
| 1256 | } | ||
| 1257 | |||
| 1258 | int | ||
| 1259 | command_rmdir (struct command_line cl) | ||
| 1260 | { | ||
| 1261 | static struct string_builder sb = {0}; | ||
| 1262 | |||
| 1263 | if (cl.argc == 0) { | ||
| 1264 | fprintf (stderr, "usage: %.*s DIR...\n", SVFMT (cl.command)); | ||
| 1265 | return 1; | ||
| 1266 | } | ||
| 1267 | |||
| 1268 | int ret = 0; | ||
| 1269 | for (uint32_t argind = 0; argind < cl.argc; ++argind) { | ||
| 1270 | struct string_view file = cl.args[argind]; | ||
| 1271 | const char *filename = file.begin; | ||
| 1272 | |||
| 1273 | bool conf = is_conf (file); | ||
| 1274 | if (conf) { | ||
| 1275 | STRING_BUILDER_RESET (sb); | ||
| 1276 | STRING_BUILDER_APPEND_STRING_VIEW (sb, canonicalize (file)); | ||
| 1277 | if (sb.string[sb.length - 1] != '/') | ||
| 1278 | STRING_BUILDER_APPEND_CHAR (sb, '/'); | ||
| 1279 | if (!authorize (id, SVSB (sb), AR_DELETE)) { | ||
| 1280 | ret = 1; | ||
| 1281 | continue; | ||
| 1282 | } | ||
| 1283 | } | ||
| 1284 | char *canonical = NULL; | ||
| 1285 | if (conf) { | ||
| 1286 | elevate (); | ||
| 1287 | canonical = realpath (filename, NULL); | ||
| 1288 | } | ||
| 1289 | int res = rmdir (filename); | ||
| 1290 | if (conf) drop (); | ||
| 1291 | if (res == -1) { | ||
| 1292 | fprintf (stderr, "%.*s: cannot remove directory '%s': %s\n", | ||
| 1293 | SVFMT (cl.command), filename, strerror (errno)); | ||
| 1294 | ret = 1; | ||
| 1295 | free (canonical); | ||
| 1296 | } else if (conf) { | ||
| 1297 | assert (canonical); | ||
| 1298 | string_builder_printf (&confdata_log, "%u user %d removed directory '!%s'\n", | ||
| 1299 | time (NULL), id, canonical + strlen (CONFDATA)); | ||
| 1300 | free (canonical); | ||
| 1301 | |||
| 1302 | STRING_BUILDER_RESET (sb); | ||
| 1303 | STRING_BUILDER_APPEND_STRING_VIEW (sb, file); | ||
| 1304 | if (sb.string[sb.length - 1] != '/') | ||
| 1305 | STRING_BUILDER_APPEND_CHAR (sb, '/'); | ||
| 1306 | deleted_conf_file (SVSB (sb)); | ||
| 1307 | } | ||
| 1308 | } | ||
| 1309 | |||
| 1310 | return ret; | ||
| 1311 | } | ||
| 1312 | |||
| 1313 | int | ||
| 1314 | command_cat (struct command_line cl) | ||
| 1315 | { | ||
| 1316 | int ret = 0; | ||
| 1317 | for (uint32_t argind = 0; argind < cl.argc; ++argind) { | ||
| 1318 | struct string_view file = cl.args[argind]; | ||
| 1319 | bool conf = is_conf (file); | ||
| 1320 | if (conf && !authorize (id, file, AR_READ)) continue; | ||
| 1321 | |||
| 1322 | const char *filename = file.begin; | ||
| 1323 | if (conf) elevate (); | ||
| 1324 | int fd = open (filename, O_RDONLY); | ||
| 1325 | if (conf) drop (); | ||
| 1326 | if (fd == -1) { | ||
| 1327 | fprintf (stderr, "%.*s: cannot access '%s': %s\n", | ||
| 1328 | SVFMT (cl.command), filename, strerror (errno)); | ||
| 1329 | ret = 1; | ||
| 1330 | continue; | ||
| 1331 | } | ||
| 1332 | |||
| 1333 | struct stat st; | ||
| 1334 | if (fstat (fd, &st) == -1) { | ||
| 1335 | fprintf (stderr, "%.*s: cannot access '%s': %s\n", | ||
| 1336 | SVFMT (cl.command), filename, strerror (errno)); | ||
| 1337 | ret = 2; | ||
| 1338 | goto close; | ||
| 1339 | } | ||
| 1340 | |||
| 1341 | void *contents = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); | ||
| 1342 | if (contents == NULL) { | ||
| 1343 | ret = 3; | ||
| 1344 | goto close; | ||
| 1345 | } | ||
| 1346 | write (0, contents, st.st_size); | ||
| 1347 | |||
| 1348 | if (conf) { | ||
| 1349 | elevate (); | ||
| 1350 | char *canonical = realpath (file.begin, NULL); | ||
| 1351 | drop (); | ||
| 1352 | assert (canonical); | ||
| 1353 | string_builder_printf (&confdata_log, "%u user %d read file '!%s'\n", | ||
| 1354 | time (NULL), id, canonical + strlen (CONFDATA)); | ||
| 1355 | free (canonical); | ||
| 1356 | } | ||
| 1357 | |||
| 1358 | munmap (contents, st.st_size); | ||
| 1359 | close: | ||
| 1360 | close (fd); | ||
| 1361 | } | ||
| 1362 | |||
| 1363 | drop (); | ||
| 1364 | |||
| 1365 | return ret; | ||
| 1366 | } | ||
| 1367 | |||
| 1368 | int | ||
| 1369 | command_append (struct command_line cl) | ||
| 1370 | { | ||
| 1371 | static struct string_builder sb = {0}; | ||
| 1372 | |||
| 1373 | if (cl.argc != 1) { | ||
| 1374 | fprintf (stderr, "usage: %.*s FILE\n", SVFMT (cl.command)); | ||
| 1375 | return 1; | ||
| 1376 | } | ||
| 1377 | |||
| 1378 | struct string_view filename = cl.args[0]; | ||
| 1379 | |||
| 1380 | int conf = is_conf (filename); | ||
| 1381 | if (conf && !authorize (id, filename, AR_WRITE)) | ||
| 1382 | return 1; | ||
| 1383 | |||
| 1384 | STRING_BUILDER_RESET (sb); | ||
| 1385 | char *line; | ||
| 1386 | while ((line = readline ("append> ")) != NULL) { | ||
| 1387 | STRING_BUILDER_APPEND_CSTR (sb, line); | ||
| 1388 | STRING_BUILDER_APPEND_CHAR (sb, '\n'); | ||
| 1389 | free (line); | ||
| 1390 | } | ||
| 1391 | |||
| 1392 | int ret = 0; | ||
| 1393 | if (sb.length) { | ||
| 1394 | if (conf && !authorize (id, filename, AR_WRITE)) | ||
| 1395 | return 1; | ||
| 1396 | |||
| 1397 | if (conf) elevate (); | ||
| 1398 | int fd = open (filename.begin, O_WRONLY | O_CREAT | O_APPEND); | ||
| 1399 | if (conf) drop (); | ||
| 1400 | if (fd == -1) { | ||
| 1401 | fprintf (stderr, "failed to open '%.*s' for writing\n", SVFMT (filename)); | ||
| 1402 | return 1; | ||
| 1403 | } | ||
| 1404 | |||
| 1405 | write (fd, sb.string, sb.length); | ||
| 1406 | |||
| 1407 | if (conf) { | ||
| 1408 | elevate (); | ||
| 1409 | char *canonical = realpath (filename.begin, NULL); | ||
| 1410 | drop (); | ||
| 1411 | assert (canonical); | ||
| 1412 | string_builder_printf (&confdata_log, "%u user %d appended to file '!%s'\n", | ||
| 1413 | time (NULL), id, canonical + strlen (CONFDATA)); | ||
| 1414 | free (canonical); | ||
| 1415 | |||
| 1416 | if (conf != 2) created_conf_file (filename); | ||
| 1417 | } | ||
| 1418 | |||
| 1419 | close (fd); | ||
| 1420 | } | ||
| 1421 | |||
| 1422 | return ret; | ||
| 1423 | } | ||
| 1424 | |||
| 1425 | #define COMMAND_LS_ALIGNMENT 30 | ||
| 1426 | |||
| 1427 | int | ||
| 1428 | command_ls (struct command_line cl) | ||
| 1429 | { | ||
| 1430 | static struct string_builder sb = {0}; | ||
| 1431 | |||
| 1432 | if (cl.argc == 0) | ||
| 1433 | DYNAMIC_ARRAY_APPEND (cl, SVCSTR (".")); | ||
| 1434 | |||
| 1435 | struct passwd passwd = load_passwd_file (); | ||
| 1436 | |||
| 1437 | for (uint32_t argind = 0; argind < cl.argc; ++argind) { | ||
| 1438 | struct string_view file = cl.args[argind]; | ||
| 1439 | const char *filename = file.begin; | ||
| 1440 | |||
| 1441 | bool conf = is_conf (file); | ||
| 1442 | |||
| 1443 | if (conf) elevate (); | ||
| 1444 | int fd = open (filename, O_RDONLY); | ||
| 1445 | if (conf) drop (); | ||
| 1446 | if (fd == -1) { | ||
| 1447 | fprintf (stderr, "%.*s: cannot access '%s': %s\n", | ||
| 1448 | SVFMT (cl.command), filename, strerror (errno)); | ||
| 1449 | continue; | ||
| 1450 | } | ||
| 1451 | |||
| 1452 | struct stat st; | ||
| 1453 | if (fstat (fd, &st) == -1) { | ||
| 1454 | fprintf (stderr, "%.*s: cannot access '%s': %s\n", | ||
| 1455 | SVFMT (cl.command), filename, strerror (errno)); | ||
| 1456 | goto close; | ||
| 1457 | } | ||
| 1458 | |||
| 1459 | if (S_ISREG (st.st_mode)) { | ||
| 1460 | int n = printf ("%s", filename); | ||
| 1461 | if (conf) { | ||
| 1462 | while (n++ < COMMAND_LS_ALIGNMENT) | ||
| 1463 | putchar (' '); | ||
| 1464 | struct acl *acl = load_acl_for_file (file); | ||
| 1465 | |||
| 1466 | #ifdef MAC | ||
| 1467 | fputs (security_level_names[acl->security_level], stdout); | ||
| 1468 | #endif | ||
| 1469 | |||
| 1470 | struct passwd_entry *entry = lookup_id (passwd, acl->owner); | ||
| 1471 | printf (" %.*s", SVFMT (entry->login)); | ||
| 1472 | for (int i = 0; i < acl->grant_count; ++i) { | ||
| 1473 | entry = lookup_id (passwd, acl->grants[i].id); | ||
| 1474 | enum access_rights ar = acl->grants[i].access_rights; | ||
| 1475 | printf (" %.*s:%c%c%c", SVFMT (entry->login), | ||
| 1476 | ar & AR_READ ? 'r' : '-', | ||
| 1477 | ar & AR_WRITE ? 'w' : '-', | ||
| 1478 | ar & AR_DELETE ? 'd' : '-'); | ||
| 1479 | } | ||
| 1480 | free (acl); | ||
| 1481 | |||
| 1482 | elevate (); | ||
| 1483 | char *canonical = realpath (filename, NULL); | ||
| 1484 | drop (); | ||
| 1485 | ASSERT (canonical); | ||
| 1486 | string_builder_printf (&confdata_log, "%u user %d listed file '!%s'\n", | ||
| 1487 | time (NULL), id, canonical + strlen (CONFDATA)); | ||
| 1488 | free (canonical); | ||
| 1489 | } | ||
| 1490 | putchar ('\n'); | ||
| 1491 | goto close; | ||
| 1492 | } | ||
| 1493 | |||
| 1494 | STRING_BUILDER_RESET (sb); | ||
| 1495 | STRING_BUILDER_APPEND_STRING_VIEW (sb, canonicalize (file)); | ||
| 1496 | if (sb.string[sb.length - 1] != '/') | ||
| 1497 | STRING_BUILDER_APPEND_CHAR (sb, '/'); | ||
| 1498 | if (conf && !authorize (id, SVSB (sb), AR_READ)) | ||
| 1499 | continue; | ||
| 1500 | |||
| 1501 | DIR *dir = fdopendir (fd); | ||
| 1502 | if (dir == NULL) { | ||
| 1503 | fprintf (stderr, "%.*s: cannot access '%s': %s\n", | ||
| 1504 | SVFMT (cl.command), filename, strerror (errno)); | ||
| 1505 | goto close; | ||
| 1506 | } | ||
| 1507 | |||
| 1508 | if (argind != 0) putchar ('\n'); | ||
| 1509 | int n = printf ("%s:", filename); | ||
| 1510 | if (conf) { | ||
| 1511 | STRING_BUILDER_RESET (sb); | ||
| 1512 | STRING_BUILDER_APPEND_STRING_VIEW (sb, canonicalize (file)); | ||
| 1513 | if (sb.string[sb.length - 1] != '/') | ||
| 1514 | STRING_BUILDER_APPEND_CHAR (sb, '/'); | ||
| 1515 | struct acl *acl = load_acl_for_file (SVSB (sb)); | ||
| 1516 | if (acl) { | ||
| 1517 | while (n++ < COMMAND_LS_ALIGNMENT) | ||
| 1518 | putchar (' '); | ||
| 1519 | |||
| 1520 | #ifdef MAC | ||
| 1521 | fputs (security_level_names[acl->security_level], stdout); | ||
| 1522 | #endif | ||
| 1523 | |||
| 1524 | struct passwd_entry *entry = lookup_id (passwd, acl->owner); | ||
| 1525 | printf (" %.*s", SVFMT (entry->login)); | ||
| 1526 | for (int i = 0; i < acl->grant_count; ++i) { | ||
| 1527 | entry = lookup_id (passwd, acl->grants[i].id); | ||
| 1528 | enum access_rights ar = acl->grants[i].access_rights; | ||
| 1529 | printf (" %.*s:%c%c%c", SVFMT (entry->login), | ||
| 1530 | ar & AR_READ ? 'r' : '-', | ||
| 1531 | ar & AR_WRITE ? 'w' : '-', | ||
| 1532 | ar & AR_DELETE ? 'd' : '-'); | ||
| 1533 | } | ||
| 1534 | free (acl); | ||
| 1535 | } | ||
| 1536 | } | ||
| 1537 | putchar ('\n'); | ||
| 1538 | struct dirent *entry; | ||
| 1539 | while ((entry = readdir (dir)) != NULL) { | ||
| 1540 | if (strcmp (entry->d_name, ".") == 0 || | ||
| 1541 | strcmp (entry->d_name, "..") == 0) { | ||
| 1542 | continue; | ||
| 1543 | } | ||
| 1544 | |||
| 1545 | if (entry->d_type == DT_DIR) { | ||
| 1546 | n = printf ("%s/", entry->d_name); | ||
| 1547 | } else { | ||
| 1548 | n = printf ("%s", entry->d_name); | ||
| 1549 | } | ||
| 1550 | |||
| 1551 | if (conf) { | ||
| 1552 | STRING_BUILDER_RESET (sb); | ||
| 1553 | STRING_BUILDER_APPEND_STRING_VIEW (sb, file); | ||
| 1554 | STRING_BUILDER_APPEND_CHAR (sb, '/'); | ||
| 1555 | STRING_BUILDER_APPEND_CSTR (sb, entry->d_name); | ||
| 1556 | if (entry->d_type == DT_DIR) | ||
| 1557 | STRING_BUILDER_APPEND_CHAR (sb, '/'); | ||
| 1558 | struct acl *acl = load_acl_for_file (SVSB (sb)); | ||
| 1559 | if (acl) { | ||
| 1560 | while (n++ < COMMAND_LS_ALIGNMENT) | ||
| 1561 | putchar (' '); | ||
| 1562 | |||
| 1563 | #ifdef MAC | ||
| 1564 | fputs (security_level_names[acl->security_level], stdout); | ||
| 1565 | #endif | ||
| 1566 | |||
| 1567 | struct passwd_entry *entry = lookup_id (passwd, acl->owner); | ||
| 1568 | printf (" %.*s", SVFMT (entry->login)); | ||
| 1569 | for (int i = 0; i < acl->grant_count; ++i) { | ||
| 1570 | entry = lookup_id (passwd, acl->grants[i].id); | ||
| 1571 | enum access_rights ar = acl->grants[i].access_rights; | ||
| 1572 | printf (" %.*s:%c%c%c", SVFMT (entry->login), | ||
| 1573 | ar & AR_READ ? 'r' : '-', | ||
| 1574 | ar & AR_WRITE ? 'w' : '-', | ||
| 1575 | ar & AR_DELETE ? 'd' : '-'); | ||
| 1576 | free (acl); | ||
| 1577 | } | ||
| 1578 | } | ||
| 1579 | } | ||
| 1580 | |||
| 1581 | putchar ('\n'); | ||
| 1582 | } | ||
| 1583 | |||
| 1584 | if (conf) { | ||
| 1585 | elevate (); | ||
| 1586 | char *canonical = realpath (filename, NULL); | ||
| 1587 | drop (); | ||
| 1588 | assert (canonical); | ||
| 1589 | string_builder_printf (&confdata_log, "%u user %d listed directory '!%s'\n", | ||
| 1590 | time (NULL), id, canonical + strlen (CONFDATA)); | ||
| 1591 | free (canonical); | ||
| 1592 | } | ||
| 1593 | |||
| 1594 | closedir (dir); | ||
| 1595 | close: | ||
| 1596 | close (fd); | ||
| 1597 | } | ||
| 1598 | |||
| 1599 | close_passwd_file (passwd); | ||
| 1600 | |||
| 1601 | return 0; | ||
| 1602 | } | ||
| 1603 | |||
| 1604 | const char * | ||
| 1605 | prompt (int ret) | ||
| 1606 | { | ||
| 1607 | static struct string_builder sb = {0}; | ||
| 1608 | |||
| 1609 | STRING_BUILDER_RESET (sb); | ||
| 1610 | STRING_BUILDER_APPEND_CSTR (sb, "\e[36m[\e[34m"); | ||
| 1611 | |||
| 1612 | struct passwd passwd = load_passwd_file (); | ||
| 1613 | struct passwd_entry *entry = lookup_id (passwd, id); | ||
| 1614 | if (entry) { | ||
| 1615 | STRING_BUILDER_APPEND_STRING_VIEW (sb, entry->login); | ||
| 1616 | |||
| 1617 | #ifdef MAC | ||
| 1618 | STRING_BUILDER_APPEND_CSTR (sb, "\e[36m@"); | ||
| 1619 | STRING_BUILDER_APPEND_CSTR (sb, entry->security_level == SL_COUNT - 1 | ||
| 1620 | ? "\e[34m" | ||
| 1621 | : "\e[31m"); | ||
| 1622 | STRING_BUILDER_APPEND_CSTR (sb, security_level_names[entry->security_level]); | ||
| 1623 | #endif | ||
| 1624 | } else STRING_BUILDER_APPEND_CHAR (sb, '?'); | ||
| 1625 | close_passwd_file (passwd); | ||
| 1626 | |||
| 1627 | STRING_BUILDER_APPEND_CSTR (sb, "\e[36m:\e[34m"); | ||
| 1628 | struct string_view canonical = canonicalize (SVCSTR (".")); | ||
| 1629 | if (strncmp (canonical.begin, CONFDATA, strlen (CONFDATA)) == 0) { | ||
| 1630 | STRING_BUILDER_APPEND_CHAR (sb, '!'); | ||
| 1631 | canonical.begin += strlen (CONFDATA); | ||
| 1632 | } STRING_BUILDER_APPEND_STRING_VIEW (sb, canonical); | ||
| 1633 | |||
| 1634 | STRING_BUILDER_APPEND_CSTR (sb, "\e[36m]\e[34m"); | ||
| 1635 | |||
| 1636 | if (ret) string_builder_printf (&sb, " \e[31m%d", ret); | ||
| 1637 | |||
| 1638 | STRING_BUILDER_APPEND_CSTR (sb, ruid == euid ? " \e[31m#" : " \e[95m$"); | ||
| 1639 | STRING_BUILDER_APPEND_CSTR (sb, "\e[0m "); | ||
| 1640 | STRING_BUILDER_TERMINATE (sb); | ||
| 1641 | |||
| 1642 | return sb.string; | ||
| 1643 | } | ||
| 1644 | |||
| 1645 | int | ||
| 1646 | main (int argc, char *argv[]) | ||
| 1647 | { | ||
| 1648 | ruid = getuid (); | ||
| 1649 | rgid = getgid (); | ||
| 1650 | euid = geteuid (); | ||
| 1651 | egid = getegid (); | ||
| 1652 | drop (); | ||
| 1653 | |||
| 1654 | static struct option longopts[] = { | ||
| 1655 | { "help", no_argument, NULL, 'h' }, | ||
| 1656 | { "password", required_argument, NULL, 'p' }, | ||
| 1657 | {0} | ||
| 1658 | }; | ||
| 1659 | char ch; | ||
| 1660 | struct string_view password = {0}; | ||
| 1661 | while ((ch = getopt_long (argc, argv, ":hp:", longopts, NULL)) != -1) { | ||
| 1662 | switch (ch) { | ||
| 1663 | case 'h': | ||
| 1664 | printf ("Usage: %s [options] [LOGIN]\n", argv[0]); | ||
| 1665 | puts (""); | ||
| 1666 | puts ("Options:"); | ||
| 1667 | puts (" -h, --help display this help message and exit"); | ||
| 1668 | puts (" -p, --password PASSWORD password of the account (only if login was provided)"); | ||
| 1669 | puts (""); | ||
| 1670 | exit (0); | ||
| 1671 | case 'p': password = SVCSTR (optarg); break; | ||
| 1672 | case '?': DIE ("unknown option %s", argv[optind - 1]); | ||
| 1673 | case ':': DIE ("missing argument for %s", argv[optind - 1]); | ||
| 1674 | default: DIE ("unexpected opt error"); | ||
| 1675 | } | ||
| 1676 | } | ||
| 1677 | |||
| 1678 | struct passwd passwd = load_passwd_file (); | ||
| 1679 | struct passwd_entry *entry = NULL; | ||
| 1680 | if (argv[optind] != NULL) { | ||
| 1681 | if (argv[optind + 1] != NULL) | ||
| 1682 | DIE ("extra trailing arguments starting with: %s", argv[optind + 1]); | ||
| 1683 | |||
| 1684 | struct string_view login = SVCSTR (argv[optind]); | ||
| 1685 | if (ruid == 0) { | ||
| 1686 | entry = lookup_login (passwd, login); | ||
| 1687 | append_passwd_log (); | ||
| 1688 | if (!entry) { | ||
| 1689 | DIE ("user %.*s doesn't exist", SVFMT (login)); | ||
| 1690 | } | ||
| 1691 | } else { | ||
| 1692 | if (password.begin == NULL) { | ||
| 1693 | fputs ("Password: ", stdout); | ||
| 1694 | password = read_password (); | ||
| 1695 | } | ||
| 1696 | entry = authenticate (passwd, login, password); | ||
| 1697 | append_passwd_log (); | ||
| 1698 | if (entry == NULL) exit (1); | ||
| 1699 | } | ||
| 1700 | } else { | ||
| 1701 | do { | ||
| 1702 | fputs ("Login: ", stdout); fflush (stdout); | ||
| 1703 | struct string_view login = read_login (); | ||
| 1704 | fputs ("Password: ", stdout); fflush (stdout); | ||
| 1705 | struct string_view password = read_password (); | ||
| 1706 | entry = authenticate (passwd, login, password); | ||
| 1707 | } while (entry == NULL); | ||
| 1708 | } | ||
| 1709 | |||
| 1710 | id = entry->id; | ||
| 1711 | printf ("Hello, %.*s! (%d)\n", SVFMT (entry->comment), id); | ||
| 1712 | close_passwd_file (passwd); | ||
| 1713 | printf ("Run 'help', to get a list of available commands.\n"); | ||
| 1714 | |||
| 1715 | struct command_line cl = {0}; | ||
| 1716 | int ret = 0; | ||
| 1717 | for (;;) { | ||
| 1718 | char *line = readline (prompt (ret)); | ||
| 1719 | if (line == NULL) break; | ||
| 1720 | add_history (line); | ||
| 1721 | switch (parse_command_line (&cl, SVCSTR (line))) { | ||
| 1722 | case 1: | ||
| 1723 | if (SVCMP (cl.command, SVCSTR ("exit" )) == 0) ret = command_exit (cl); | ||
| 1724 | else if (SVCMP (cl.command, SVCSTR ("help" )) == 0) ret = command_help (cl); | ||
| 1725 | else if (SVCMP (cl.command, SVCSTR ("su" )) == 0) ret = command_su (cl); | ||
| 1726 | else if (SVCMP (cl.command, SVCSTR ("pwd" )) == 0) ret = command_pwd (cl); | ||
| 1727 | else if (SVCMP (cl.command, SVCSTR ("whoami")) == 0) ret = command_whoami (cl); | ||
| 1728 | else if (SVCMP (cl.command, SVCSTR ("echo" )) == 0) ret = command_echo (cl); | ||
| 1729 | else if (SVCMP (cl.command, SVCSTR ("ls" )) == 0) ret = command_ls (cl); | ||
| 1730 | else if (SVCMP (cl.command, SVCSTR ("cd" )) == 0) ret = command_cd (cl); | ||
| 1731 | else if (SVCMP (cl.command, SVCSTR ("mv" )) == 0) ret = command_mv (cl); | ||
| 1732 | else if (SVCMP (cl.command, SVCSTR ("cp" )) == 0) ret = command_cp (cl); | ||
| 1733 | else if (SVCMP (cl.command, SVCSTR ("rm" )) == 0) ret = command_rm (cl); | ||
| 1734 | else if (SVCMP (cl.command, SVCSTR ("mkdir" )) == 0) ret = command_mkdir (cl); | ||
| 1735 | else if (SVCMP (cl.command, SVCSTR ("rmdir" )) == 0) ret = command_rmdir (cl); | ||
| 1736 | else if (SVCMP (cl.command, SVCSTR ("cat" )) == 0) ret = command_cat (cl); | ||
| 1737 | else if (SVCMP (cl.command, SVCSTR ("read" )) == 0) ret = command_cat (cl); | ||
| 1738 | else if (SVCMP (cl.command, SVCSTR ("copy" )) == 0) ret = command_cp (cl); | ||
| 1739 | else if (SVCMP (cl.command, SVCSTR ("remove")) == 0) ret = command_rm (cl); | ||
| 1740 | else if (SVCMP (cl.command, SVCSTR ("append")) == 0) ret = command_append (cl); | ||
| 1741 | else if (SVCMP (cl.command, SVCSTR ("chown" )) == 0) ret = command_chown (cl); | ||
| 1742 | else if (SVCMP (cl.command, SVCSTR ("chsl" )) == 0) ret = command_chsl (cl); | ||
| 1743 | else if (SVCMP (cl.command, SVCSTR ("grant" )) == 0) ret = command_grant (cl); | ||
| 1744 | else if (SVCMP (cl.command, SVCSTR ("revoke")) == 0) ret = command_revoke (cl); | ||
| 1745 | else puts ("unknown command, run `help' to get the list of available commands"); | ||
| 1746 | break; | ||
| 1747 | case 0: break; | ||
| 1748 | case -1: | ||
| 1749 | putchar ('\n'); | ||
| 1750 | exit (0); | ||
| 1751 | default: | ||
| 1752 | fputs ("invalid command line", stderr); | ||
| 1753 | break; | ||
| 1754 | } free (line); | ||
| 1755 | append_passwd_log (); | ||
| 1756 | flush_confdata_log (); | ||
| 1757 | } putchar ('\n'); | ||
| 1758 | |||
| 1759 | return EXIT_SUCCESS; | ||
| 1760 | } | ||
| 1761 | |||
| 1762 | // src/shell.c ends here | ||
diff --git a/03/src/string-builder.c b/03/src/string-builder.c new file mode 100644 index 0000000..99d066c --- /dev/null +++ b/03/src/string-builder.c | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | #include "string-builder.h" | ||
| 2 | #include <stdarg.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | |||
| 5 | void | ||
| 6 | string_builder_append_ntimes (STRING_BUILDER sb, char c, size_t n) | ||
| 7 | { | ||
| 8 | DYNAMIC_ARRAY_ENSURE_CAPACITY (*sb, n); | ||
| 9 | memset (sb->items + sb->count, c, n); | ||
| 10 | sb->count += n; | ||
| 11 | } | ||
| 12 | |||
| 13 | void | ||
| 14 | string_builder_append_cstrs (STRING_BUILDER sb, ...) | ||
| 15 | { | ||
| 16 | va_list args; | ||
| 17 | |||
| 18 | va_start (args, sb); | ||
| 19 | const char *arg = va_arg (args, const char *); | ||
| 20 | while (arg) { | ||
| 21 | STRING_BUILDER_APPEND_CSTR(*sb, arg); | ||
| 22 | arg = va_arg (args, const char *); | ||
| 23 | } va_end (args); | ||
| 24 | } | ||
| 25 | |||
| 26 | void | ||
| 27 | string_builder_printf (STRING_BUILDER sb, const char *format, ...) | ||
| 28 | { | ||
| 29 | va_list args; | ||
| 30 | |||
| 31 | va_start (args, format); | ||
| 32 | int size = vsnprintf (NULL, 0, format, args); | ||
| 33 | va_end (args); | ||
| 34 | ASSERT (size >= 0); | ||
| 35 | |||
| 36 | DYNAMIC_ARRAY_ENSURE_CAPACITY (*sb, size + 1); | ||
| 37 | va_start (args, format); | ||
| 38 | vsnprintf (&sb->items[sb->count], size + 1, format, args); | ||
| 39 | va_end (args); | ||
| 40 | sb->count += size; | ||
| 41 | } | ||
| 42 | |||
| 43 | bool | ||
| 44 | string_builder_getline (STRING_BUILDER sb) | ||
| 45 | { | ||
| 46 | for (;;) { | ||
| 47 | int c = getchar (); | ||
| 48 | |||
| 49 | if (c == EOF) { | ||
| 50 | clearerr (stdin); | ||
| 51 | return false; | ||
| 52 | } | ||
| 53 | if (c == '\n') | ||
| 54 | break; | ||
| 55 | |||
| 56 | STRING_BUILDER_APPEND_CHAR (*sb, c); | ||
| 57 | } | ||
| 58 | |||
| 59 | return true; | ||
| 60 | } | ||
diff --git a/03/src/string-builder.h b/03/src/string-builder.h new file mode 100644 index 0000000..6cc6307 --- /dev/null +++ b/03/src/string-builder.h | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | #ifndef INCLUDED_STRING_BUILDER_H | ||
| 2 | #define INCLUDED_STRING_BUILDER_H | ||
| 3 | |||
| 4 | #include <stddef.h> | ||
| 5 | #include <string.h> | ||
| 6 | #include <stdbool.h> | ||
| 7 | #include "dynamic-array.h" | ||
| 8 | |||
| 9 | struct string_builder { | ||
| 10 | union { char *items, *string; }; | ||
| 11 | union { size_t count, length; }; | ||
| 12 | size_t capacity; | ||
| 13 | }; | ||
| 14 | #define STRING_BUILDER struct string_builder * | ||
| 15 | |||
| 16 | #define SBFMT(SB) (int) (SB).length, (SB).string | ||
| 17 | |||
| 18 | #define STRING_BUILDER_APPEND_CHAR(SB, CHAR) \ | ||
| 19 | DYNAMIC_ARRAY_APPEND ((SB), (char) (CHAR)) | ||
| 20 | |||
| 21 | #define STRING_BUILDER_APPEND_BUF(SB, BUF, SIZE) \ | ||
| 22 | DYNAMIC_ARRAY_APPEND_MANY (SB, BUF, SIZE) | ||
| 23 | |||
| 24 | #define STRING_BUILDER_APPEND_CSTR(SB, CSTR) \ | ||
| 25 | ({ \ | ||
| 26 | const char *s = (CSTR); \ | ||
| 27 | size_t n = strlen (s); \ | ||
| 28 | DYNAMIC_ARRAY_APPEND_MANY(SB, s, n); \ | ||
| 29 | }) | ||
| 30 | |||
| 31 | #define STRING_BUILDER_APPEND_CSTRS(SB, ...) \ | ||
| 32 | string_builder_append_cstrs (&(SB), __VA_ARGS__, NULL) | ||
| 33 | |||
| 34 | #define STRING_BUILDER_APPEND_STRING_VIEW(SB, SV) \ | ||
| 35 | DYNAMIC_ARRAY_APPEND_MANY(SB, (SV).begin, SVLEN (SV)); | ||
| 36 | |||
| 37 | #define STRING_BUILDER_APPEND_NULL(SB) \ | ||
| 38 | STRING_BUILDER_APPEND_CHAR (SB, '\0'); | ||
| 39 | |||
| 40 | #define STRING_BUILDER_TERMINATE(SB) ({ \ | ||
| 41 | DYNAMIC_ARRAY_ENSURE_CAPACITY (SB, 1); \ | ||
| 42 | (SB).string[(SB).length] = '\0'; \ | ||
| 43 | }) | ||
| 44 | |||
| 45 | #define STRING_BUILDER_RESET(SB) \ | ||
| 46 | DYNAMIC_ARRAY_RESET (SB) | ||
| 47 | |||
| 48 | #define STRING_BUILDER_FREE(SB) \ | ||
| 49 | DYNAMIC_ARRAY_FREE(SB) | ||
| 50 | |||
| 51 | void string_builder_append_ntimes (STRING_BUILDER, char c, size_t n); | ||
| 52 | void string_builder_append_cstrs (STRING_BUILDER, ...); | ||
| 53 | void string_builder_printf (STRING_BUILDER, const char *format, ...); | ||
| 54 | bool string_builder_getline (STRING_BUILDER); | ||
| 55 | |||
| 56 | #endif | ||
diff --git a/03/src/string-view.c b/03/src/string-view.c new file mode 100644 index 0000000..ef84f69 --- /dev/null +++ b/03/src/string-view.c | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | #include "string-view.h" | ||
| 2 | #include <string.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | |||
| 5 | size_t | ||
| 6 | string_view_length (struct string_view sv) | ||
| 7 | { | ||
| 8 | return sv.end - sv.begin; | ||
| 9 | } | ||
| 10 | |||
| 11 | struct string_view | ||
| 12 | string_view_from_cstr (const char *cstr) | ||
| 13 | { | ||
| 14 | return (struct string_view) { | ||
| 15 | .begin = cstr, | ||
| 16 | .end = cstr + strlen (cstr), | ||
| 17 | }; | ||
| 18 | } | ||
| 19 | |||
| 20 | struct string_view | ||
| 21 | duplicate_string_view (struct string_view sv) | ||
| 22 | { | ||
| 23 | size_t len = SVLEN (sv); | ||
| 24 | |||
| 25 | struct string_view result = { | ||
| 26 | .begin = malloc (len), | ||
| 27 | }; | ||
| 28 | result.end = result.begin + len; | ||
| 29 | |||
| 30 | memcpy ((void *) result.begin, sv.begin, len); | ||
| 31 | |||
| 32 | return result; | ||
| 33 | } | ||
| 34 | |||
| 35 | struct string_view | ||
| 36 | duplicate_string_view_as_cstr (struct string_view sv) | ||
| 37 | { | ||
| 38 | size_t len = SVLEN (sv); | ||
| 39 | |||
| 40 | struct string_view result = { | ||
| 41 | .begin = malloc (len + 1), | ||
| 42 | }; | ||
| 43 | result.end = result.begin + len; | ||
| 44 | |||
| 45 | memcpy ((void *) result.begin, sv.begin, len); | ||
| 46 | ((char *) result.begin)[len] = '\0'; | ||
| 47 | |||
| 48 | return result; | ||
| 49 | } | ||
| 50 | |||
| 51 | struct string_view | ||
| 52 | string_view_strdup (const char *cstr) | ||
| 53 | { | ||
| 54 | struct string_view sv; | ||
| 55 | size_t len = strlen (cstr); | ||
| 56 | sv.begin = malloc (len + 1), | ||
| 57 | sv.end = sv.begin + len; | ||
| 58 | |||
| 59 | memcpy ((void *) sv.begin, cstr, len); | ||
| 60 | ((char *) sv.begin)[len] = '\0'; | ||
| 61 | |||
| 62 | return sv; | ||
| 63 | } | ||
| 64 | |||
| 65 | int | ||
| 66 | string_view_compare (struct string_view lhs, struct string_view rhs) | ||
| 67 | { | ||
| 68 | if (SVLEN (lhs) != SVLEN (rhs)) | ||
| 69 | return SVLEN (lhs) - SVLEN (rhs); | ||
| 70 | |||
| 71 | return memcmp (lhs.begin, rhs.begin, SVLEN (lhs)); | ||
| 72 | } | ||
| 73 | |||
| 74 | bool | ||
| 75 | string_view_starts_with (struct string_view sv, struct string_view prefix) | ||
| 76 | { | ||
| 77 | if (SVLEN (sv) < SVLEN (prefix)) | ||
| 78 | return false; | ||
| 79 | |||
| 80 | return memcmp (sv.begin, prefix.begin, SVLEN (prefix)) == 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | struct string_view | ||
| 84 | string_view_dirname (struct string_view sv) | ||
| 85 | { | ||
| 86 | while (sv.end != sv.begin && sv.end[-1] == '/') | ||
| 87 | sv.end -= 1; | ||
| 88 | |||
| 89 | while (sv.end != sv.begin && sv.end[-1] != '/') | ||
| 90 | sv.end -= 1; | ||
| 91 | |||
| 92 | while (sv.end != sv.begin && sv.end[-1] == '/') | ||
| 93 | sv.end -= 1; | ||
| 94 | |||
| 95 | if (sv.end == sv.begin) | ||
| 96 | return SVCSTR ("."); | ||
| 97 | |||
| 98 | return sv; | ||
| 99 | } | ||
| 100 | |||
| 101 | struct string_view | ||
| 102 | string_view_basename (struct string_view sv) | ||
| 103 | { | ||
| 104 | const char *head = sv.end; | ||
| 105 | |||
| 106 | while (head > sv.begin && head[-1] == '/') | ||
| 107 | head -= 1; | ||
| 108 | |||
| 109 | while (head > sv.begin && head[-1] != '/') | ||
| 110 | head -= 1; | ||
| 111 | |||
| 112 | if (head != sv.begin) | ||
| 113 | sv.begin = head; | ||
| 114 | |||
| 115 | return sv; | ||
| 116 | } | ||
| 117 | |||
| 118 | // pops all if c was not found | ||
| 119 | struct string_view | ||
| 120 | string_view_pop_up_to (struct string_view *sv, char c) | ||
| 121 | { | ||
| 122 | const char *head = sv->begin; | ||
| 123 | |||
| 124 | while (head < sv->end && *head != c) | ||
| 125 | head += 1; | ||
| 126 | |||
| 127 | struct string_view result = { .begin = sv->begin }; | ||
| 128 | result.end = sv->begin = head; | ||
| 129 | |||
| 130 | return result; | ||
| 131 | } | ||
| 132 | |||
| 133 | struct string_view | ||
| 134 | string_view_pop_n (struct string_view *sv, size_t n) | ||
| 135 | { | ||
| 136 | struct string_view result = { | ||
| 137 | .begin = sv->begin, | ||
| 138 | .end = sv->begin + n, | ||
| 139 | }; | ||
| 140 | |||
| 141 | if (result.end > sv->end) | ||
| 142 | result.end = sv->end; | ||
| 143 | |||
| 144 | sv->begin = result.end; | ||
| 145 | |||
| 146 | return result; | ||
| 147 | } | ||
diff --git a/03/src/string-view.h b/03/src/string-view.h new file mode 100644 index 0000000..fa42d77 --- /dev/null +++ b/03/src/string-view.h | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | // src/string-view.h | ||
| 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_STRING_VIEW_H | ||
| 12 | #define INCLUDED_STRING_VIEW_H | ||
| 13 | |||
| 14 | #include <stddef.h> | ||
| 15 | #include <stdbool.h> | ||
| 16 | |||
| 17 | struct string_view { | ||
| 18 | const char *begin; | ||
| 19 | const char *end; | ||
| 20 | }; | ||
| 21 | |||
| 22 | #define SVNULL ((struct string_view) {0}) | ||
| 23 | #define SVLEN string_view_length | ||
| 24 | #define SVFMT(SV) (int) SVLEN (SV), (SV).begin | ||
| 25 | #define SVCSTR string_view_from_cstr | ||
| 26 | #define SVDUP duplicate_string_view | ||
| 27 | #define SVDUP0 duplicate_string_view_as_cstr | ||
| 28 | #define SVSTRDUP string_view_strdup | ||
| 29 | #define SVCMP string_view_compare | ||
| 30 | #define SVSB(SB) ((struct string_view) { (SB).string, (SB).string + (SB).length }) | ||
| 31 | |||
| 32 | size_t string_view_length (struct string_view sv); | ||
| 33 | struct string_view string_view_from_cstr (const char *cstr); | ||
| 34 | struct string_view duplicate_string_view (struct string_view sv); | ||
| 35 | struct string_view duplicate_string_view_as_cstr (struct string_view sv); | ||
| 36 | struct string_view string_view_strdup (const char *cstr); | ||
| 37 | int string_view_compare (struct string_view lhs, struct string_view rhs); | ||
| 38 | bool string_view_starts_with (struct string_view sv, struct string_view prefix); | ||
| 39 | struct string_view string_view_dirname (struct string_view); | ||
| 40 | struct string_view string_view_basename (struct string_view); | ||
| 41 | struct string_view string_view_pop_up_to (struct string_view *, char); | ||
| 42 | struct string_view string_view_pop_n (struct string_view *, size_t n); | ||
| 43 | |||
| 44 | #endif // INCLUDED_STRING_VIEW_H | ||
| 45 | |||
| 46 | // src/string-view.h ends here | ||
