diff options
| author | 2026-11-11 05:31:06 +0000 | |
|---|---|---|
| committer | 2026-11-11 06:14:42 +0000 | |
| commit | 9e1d304cac5367c3dbb1ca14700736680bd3a804 (patch) | |
| tree | 31f328747acb934d365d423adfda144818ff5ad4 /03/src/shell.c | |
| download | lab-01-05-9e1d304cac5367c3dbb1ca14700736680bd3a804.tar.gz | |
Diffstat (limited to '03/src/shell.c')
| -rw-r--r-- | 03/src/shell.c | 1762 |
1 files changed, 1762 insertions, 0 deletions
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 | ||
