diff options
Diffstat (limited to '03/src/common.c')
| -rw-r--r-- | 03/src/common.c | 469 |
1 files changed, 469 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 | } | ||
