code60-transmission-server 0.0.0-stage → 0.1.0-staging.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/LICENSE +180 -0
  2. package/README.engine.md +463 -0
  3. package/README.md +376 -2
  4. package/dist/src/broker/filesystem.js +385 -0
  5. package/dist/src/broker/index.js +777 -0
  6. package/dist/src/broker/native/README.md +207 -0
  7. package/dist/src/broker/native/build.js +46 -0
  8. package/dist/src/broker/native/command-helper.c +93 -0
  9. package/dist/src/broker/native/fs-helper.c +529 -0
  10. package/dist/src/broker/native/git-helper.c +205 -0
  11. package/dist/src/broker/policy.js +149 -0
  12. package/dist/src/broker/startup-lock.js +14 -0
  13. package/dist/src/broker/types.js +11 -0
  14. package/dist/src/cli/configure.js +208 -0
  15. package/dist/src/cli/daemon.js +369 -0
  16. package/dist/src/cli/helpers.js +112 -0
  17. package/dist/src/cli/main.js +189 -0
  18. package/dist/src/cli/runtime-build.js +38 -0
  19. package/dist/src/config/coding-policy.js +4 -0
  20. package/dist/src/config/deployment.js +183 -0
  21. package/dist/src/config/documents.js +29 -0
  22. package/dist/src/config/profile-validation.js +129 -0
  23. package/dist/src/config/schema-graph.js +237 -0
  24. package/dist/src/config/schema-loader.js +185 -0
  25. package/dist/src/config/validation-pool.js +105 -0
  26. package/dist/src/config/validation-protocol.js +1 -0
  27. package/dist/src/config/validation-worker.js +106 -0
  28. package/dist/src/config/workflow-language.js +9 -0
  29. package/dist/src/config/workflow-schema.json +545 -0
  30. package/dist/src/config/workflow-types.js +1 -0
  31. package/dist/src/config/workflow-validation.js +95 -0
  32. package/dist/src/engine/budgets.js +50 -0
  33. package/dist/src/engine/clocks.js +27 -0
  34. package/dist/src/engine/commands.js +477 -0
  35. package/dist/src/engine/contracts.js +11 -0
  36. package/dist/src/engine/drain-state.js +31 -0
  37. package/dist/src/engine/effective-permissions.js +120 -0
  38. package/dist/src/engine/instruction-pins.js +205 -0
  39. package/dist/src/engine/retries.js +50 -0
  40. package/dist/src/engine/state.js +1 -0
  41. package/dist/src/engine/timing.js +23 -0
  42. package/dist/src/engine/tool-observations.js +87 -0
  43. package/dist/src/engine/workflow-engine.js +1864 -0
  44. package/dist/src/main.js +28 -0
  45. package/dist/src/mcp/catalog.js +71 -0
  46. package/dist/src/mcp/main.js +26 -0
  47. package/dist/src/mcp/server.js +177 -0
  48. package/dist/src/runtime/codex-app-server.js +91 -0
  49. package/dist/src/runtime/codex-executable.js +50 -0
  50. package/dist/src/runtime/containment.js +82 -0
  51. package/dist/src/runtime/engine-driver.js +221 -0
  52. package/dist/src/runtime/json-line-writer.js +192 -0
  53. package/dist/src/runtime/pinned-instruction-sources.js +24 -0
  54. package/dist/src/runtime/protocol-client.js +264 -0
  55. package/dist/src/runtime/protocol-pool.js +183 -0
  56. package/dist/src/runtime/protocol-projection.js +393 -0
  57. package/dist/src/runtime/protocol-types.js +1 -0
  58. package/dist/src/runtime/protocol-worker.js +18 -0
  59. package/dist/src/runtime/provider-bridge.js +290 -0
  60. package/dist/src/runtime/provider-credentials.js +41 -0
  61. package/dist/src/runtime/provider-transport.js +406 -0
  62. package/dist/src/runtime/runtime-broker.js +213 -0
  63. package/dist/src/runtime/workspace-instructions.js +120 -0
  64. package/dist/src/runtime/workspace-view.js +74 -0
  65. package/dist/src/server/api-contract.json +11637 -0
  66. package/dist/src/server/api-types.js +1 -0
  67. package/dist/src/server/application.js +44 -0
  68. package/dist/src/server/auth.js +67 -0
  69. package/dist/src/server/broker-handlers.js +242 -0
  70. package/dist/src/server/coding.js +281 -0
  71. package/dist/src/server/control-plane.js +50 -0
  72. package/dist/src/server/deployment.js +249 -0
  73. package/dist/src/server/errors.js +22 -0
  74. package/dist/src/server/history-handlers.js +130 -0
  75. package/dist/src/server/http.js +124 -0
  76. package/dist/src/server/paging.js +56 -0
  77. package/dist/src/server/publication.js +119 -0
  78. package/dist/src/server/tool-event-handler.js +77 -0
  79. package/dist/src/server/workspace-handlers.js +34 -0
  80. package/dist/src/storage/coordinator.js +224 -0
  81. package/dist/src/storage/startup-lock.js +71 -0
  82. package/dist/src/storage/store.js +167 -0
  83. package/dist/src/tools/check-worker.js +81 -0
  84. package/dist/src/tools/checks.js +117 -0
  85. package/dist/src/tools/command-check.js +34 -0
  86. package/dist/src/tools/commands.js +312 -0
  87. package/dist/src/tools/developer.js +355 -0
  88. package/dist/src/tools/dns.js +248 -0
  89. package/dist/src/tools/http-contract.js +38 -0
  90. package/dist/src/tools/http.js +358 -0
  91. package/dist/src/tools/index.js +194 -0
  92. package/dist/src/tools/worker-check.js +73 -0
  93. package/dist/src/workspaces/git.js +227 -0
  94. package/dist/src/workspaces/index.js +937 -0
  95. package/dist/src/workspaces/source.js +295 -0
  96. package/examples/deployment.local.json +22 -0
  97. package/package.json +71 -4
  98. package/scripts/postinstall.mjs +26 -0
@@ -0,0 +1,529 @@
1
+ #define _DARWIN_C_SOURCE
2
+ #define _POSIX_C_SOURCE 200809L
3
+ #include <sys/types.h>
4
+ #include <sys/stat.h>
5
+ #include <sys/file.h>
6
+ #include <dirent.h>
7
+ #include <errno.h>
8
+ #include <fcntl.h>
9
+ #include <inttypes.h>
10
+ #include <stdarg.h>
11
+ #include <stdint.h>
12
+ #include <stdio.h>
13
+ #include <stdlib.h>
14
+ #include <string.h>
15
+ #include <strings.h>
16
+ #include <unistd.h>
17
+
18
+ /* Unprivileged, single-request helper. fd 3 is an engine-entrusted directory,
19
+ * never a path/flag/command selected by model input. See README.md for bounds
20
+ * and the explicit external-writer threat boundary. No shell or network code. */
21
+ enum { ROOT_FD = 3, MAX_PROTECTED = 64, MAX_PATH_BYTES = 4096, MAX_ID = 8192, MAX_DATA = 1048576, MAX_DEPTH = 64, MAX_ENTRIES = 1024 };
22
+ enum { PROBE_READ = 1, READ_FILE = 2, PROBE_WRITE = 3, WRITE_FILE = 4, VALIDATE_ROOT = 5,
23
+ PROBE_TREE = 6, READ_TREE = 7, PROBE_DELETE = 8, DELETE_FILE = 9, PROBE_MOVE = 10, MOVE_FILE = 11,
24
+ PROBE_DELETE_DIRECTORY = 13, DELETE_DIRECTORY = 14, MAKE_DIRECTORY = 15, SET_MODE = 16, PROBE_MODE = 17 };
25
+ enum { OK = 0, BAD_REQUEST = 1, DENIED = 2, STALE = 3, NOT_FOUND = 4, TOO_LARGE = 5, IO_ERROR = 6, UNCERTAIN = 7 };
26
+ static int status = IO_ERROR;
27
+ static const char *reason = "io_error";
28
+ static uint8_t *reply;
29
+ static size_t reply_length;
30
+ static int quarantine = -1;
31
+ static int fail(int code, const char *message) { status = code; reason = message; return -1; }
32
+ static uint32_t u32(const uint8_t *p) {
33
+ return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | (uint32_t)p[3];
34
+ }
35
+ static void put32(uint8_t *p, uint32_t n) {
36
+ p[0] = (uint8_t)(n >> 24); p[1] = (uint8_t)(n >> 16); p[2] = (uint8_t)(n >> 8); p[3] = (uint8_t)n;
37
+ }
38
+ static int read_exact(int fd, void *buffer, size_t length) {
39
+ uint8_t *p = buffer;
40
+ while (length) {
41
+ ssize_t n = read(fd, p, length);
42
+ if (n < 0 && errno == EINTR) continue;
43
+ if (n <= 0) return -1;
44
+ p += (size_t)n; length -= (size_t)n;
45
+ }
46
+ return 0;
47
+ }
48
+ static int write_exact(int fd, const void *buffer, size_t length) {
49
+ const uint8_t *p = buffer;
50
+ while (length) {
51
+ ssize_t n = write(fd, p, length);
52
+ if (n < 0 && errno == EINTR) continue;
53
+ if (n <= 0) return -1;
54
+ p += (size_t)n; length -= (size_t)n;
55
+ }
56
+ return 0;
57
+ }
58
+ static int append(char *buffer, size_t capacity, const char *format, ...) {
59
+ size_t used = strlen(buffer);
60
+ va_list args;
61
+ va_start(args, format);
62
+ int n = vsnprintf(buffer + used, capacity - used, format, args);
63
+ va_end(args);
64
+ if (n < 0 || (size_t)n >= capacity - used) return fail(TOO_LARGE, "identity_too_large");
65
+ return 0;
66
+ }
67
+ static int safe_directory(int fd, struct stat *s) {
68
+ if (fstat(fd, s) < 0) return fail(IO_ERROR, "directory_stat_failed");
69
+ if (!S_ISDIR(s->st_mode) || s->st_uid != geteuid() || (s->st_mode & (S_IWGRP | S_IWOTH)))
70
+ return fail(DENIED, "untrusted_directory");
71
+ return 0;
72
+ }
73
+ static int safe_file(const struct stat *s) {
74
+ if (!S_ISREG(s->st_mode) || s->st_nlink != 1 || s->st_uid != geteuid() ||
75
+ (s->st_mode & (S_IWGRP | S_IWOTH))) return fail(DENIED, "unsupported_file_identity");
76
+ if (s->st_size < 0 || s->st_size > MAX_DATA) return fail(TOO_LARGE, "file_too_large");
77
+ return 0;
78
+ }
79
+ static int ancestor_of(int ancestor, int descendant) {
80
+ struct stat expected, current;
81
+ if (fstat(ancestor, &expected) < 0 || !S_ISDIR(expected.st_mode)) return fail(DENIED, "invalid_protected_root");
82
+ int cursor = fcntl(descendant, F_DUPFD_CLOEXEC, 4 + MAX_PROTECTED);
83
+ if (cursor < 0) return fail(DENIED, "invalid_protected_root");
84
+ for (unsigned depth = 0; depth < 256; depth++) {
85
+ if (fstat(cursor, &current) < 0 || !S_ISDIR(current.st_mode)) { close(cursor); return fail(DENIED, "invalid_root_ancestry"); }
86
+ if (current.st_dev == expected.st_dev && current.st_ino == expected.st_ino) { close(cursor); return 1; }
87
+ int parent = openat(cursor, "..", O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK);
88
+ if (parent < 0) { close(cursor); return fail(DENIED, "unverifiable_root_ancestry"); }
89
+ struct stat parent_stat;
90
+ if (fstat(parent, &parent_stat) < 0) { close(parent); close(cursor); return fail(DENIED, "unverifiable_root_ancestry"); }
91
+ if (parent_stat.st_dev == current.st_dev && parent_stat.st_ino == current.st_ino) {
92
+ close(parent); close(cursor); return 0;
93
+ }
94
+ close(cursor); cursor = parent;
95
+ }
96
+ close(cursor);
97
+ return fail(DENIED, "root_ancestry_too_deep");
98
+ }
99
+ static int protect_roots(uint8_t count) {
100
+ for (int fd = 4; fd < 4 + count; fd++) {
101
+ int contains = ancestor_of(ROOT_FD, fd);
102
+ if (contains < 0) return -1;
103
+ int inside = ancestor_of(fd, ROOT_FD);
104
+ if (inside < 0) return -1;
105
+ if (contains || inside) return fail(DENIED, "protected_root_overlap");
106
+ }
107
+ return 0;
108
+ }
109
+ /* On case-insensitive/normalizing filesystems openat alone can resolve an alias.
110
+ * Require the exact directory-entry bytes, rather than classify an alias using
111
+ * the model's requested spelling. Never follow an entry during this check. */
112
+ static int exact_name(int parent, const char *name, const struct stat *expected) {
113
+ int copy = openat(parent, ".", O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
114
+ if (copy < 0) return fail(IO_ERROR, "directory_open_failed");
115
+ DIR *dir = fdopendir(copy);
116
+ if (!dir) { close(copy); return fail(IO_ERROR, "directory_scan_failed"); }
117
+ int found = 0;
118
+ struct dirent *entry;
119
+ errno = 0;
120
+ while ((entry = readdir(dir))) {
121
+ if (strcmp(entry->d_name, name) == 0) { found = 1; break; }
122
+ }
123
+ int scan_error = errno;
124
+ closedir(dir);
125
+ if (!found) return fail(scan_error ? IO_ERROR : DENIED, scan_error ? "directory_scan_failed" : "noncanonical_alias");
126
+ struct stat actual;
127
+ if (fstatat(parent, name, &actual, AT_SYMLINK_NOFOLLOW) < 0) return fail(STALE, "entry_changed");
128
+ if (actual.st_dev != expected->st_dev || actual.st_ino != expected->st_ino ||
129
+ (actual.st_mode & S_IFMT) != (expected->st_mode & S_IFMT)) return fail(STALE, "entry_changed");
130
+ return 0;
131
+ }
132
+ static int validate_path(const char *path, size_t length) {
133
+ if (!length || length > MAX_PATH_BYTES || path[0] == '/' || path[length - 1] == '/' ||
134
+ memchr(path, 0, length) || memchr(path, '\\', length)) return fail(BAD_REQUEST, "invalid_relative_path");
135
+ size_t start = 0, depth = 0;
136
+ for (size_t i = 0; i <= length; i++) {
137
+ if (i != length && path[i] != '/') continue;
138
+ size_t count = i - start;
139
+ if (!count || count > 255 || (count == 1 && path[start] == '.') ||
140
+ (count == 2 && path[start] == '.' && path[start + 1] == '.') ||
141
+ (count == 4 && strncasecmp(path + start, ".git", 4) == 0)) return fail(DENIED, "forbidden_component");
142
+ if (++depth > MAX_DEPTH) return fail(TOO_LARGE, "path_too_deep");
143
+ start = i + 1;
144
+ }
145
+ return 0;
146
+ }
147
+ /* Returns pinned parent and an identity of every actual traversed directory. */
148
+ static int parent_fd(const char *path, char *leaf, char *identity) {
149
+ int parent = fcntl(ROOT_FD, F_DUPFD_CLOEXEC, 4);
150
+ if (parent < 0) return fail(DENIED, "missing_root_descriptor");
151
+ struct stat s;
152
+ if (safe_directory(parent, &s) < 0) { close(parent); return -1; }
153
+ if (append(identity, MAX_ID, "v1|d:%ju:%ju", (uintmax_t)s.st_dev, (uintmax_t)s.st_ino) < 0) {
154
+ close(parent); return -1;
155
+ }
156
+ char copy[MAX_PATH_BYTES + 1];
157
+ memcpy(copy, path, strlen(path) + 1);
158
+ char *part = copy;
159
+ for (;;) {
160
+ char *slash = strchr(part, '/');
161
+ if (!slash) { memcpy(leaf, part, strlen(part) + 1); return parent; }
162
+ *slash = 0;
163
+ int next = openat(parent, part, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK);
164
+ if (next < 0) { close(parent); return fail(DENIED, "unsafe_parent_component"); }
165
+ if (safe_directory(next, &s) < 0 || exact_name(parent, part, &s) < 0) {
166
+ close(next); close(parent); return -1;
167
+ }
168
+ close(parent); parent = next;
169
+ if (append(identity, MAX_ID, "|d:%ju:%ju", (uintmax_t)s.st_dev, (uintmax_t)s.st_ino) < 0) {
170
+ close(parent); return -1;
171
+ }
172
+ part = slash + 1;
173
+ }
174
+ }
175
+ static int file_identity(char *identity, const struct stat *s) {
176
+ #ifdef __APPLE__
177
+ struct timespec modified = s->st_mtimespec, changed = s->st_ctimespec;
178
+ #else
179
+ struct timespec modified = s->st_mtim, changed = s->st_ctim;
180
+ #endif
181
+ return append(identity, MAX_ID, "|f:%ju:%ju:%ju:%ju:%ju:%ju:%jd:%jd:%ld:%jd:%ld",
182
+ (uintmax_t)s->st_dev, (uintmax_t)s->st_ino, (uintmax_t)s->st_mode,
183
+ (uintmax_t)s->st_uid, (uintmax_t)s->st_gid, (uintmax_t)s->st_nlink, (intmax_t)s->st_size,
184
+ (intmax_t)modified.tv_sec, modified.tv_nsec, (intmax_t)changed.tv_sec, changed.tv_nsec);
185
+ }
186
+ static int same_inode(const struct stat *a, const struct stat *b) {
187
+ return a->st_dev == b->st_dev && a->st_ino == b->st_ino;
188
+ }
189
+ static int output(const char *text) {
190
+ size_t length = strlen(text);
191
+ if (reply_length + length > MAX_DATA) return fail(TOO_LARGE, "directory_too_large");
192
+ uint8_t *next = realloc(reply, reply_length + length + 1);
193
+ if (!next) return fail(IO_ERROR, "allocation_failed");
194
+ reply = next; memcpy(reply + reply_length, text, length + 1); reply_length += length; return 0;
195
+ }
196
+ static int quoted(const char *text) {
197
+ if (output("\"") < 0) return -1;
198
+ for (const unsigned char *p = (const unsigned char *)text; *p; p++) {
199
+ char escaped[8] = {0};
200
+ if (*p < 32 || *p == '"' || *p == '\\') snprintf(escaped, sizeof(escaped), "\\u%04x", *p);
201
+ else { escaped[0] = (char)*p; }
202
+ if (output(escaped) < 0) return -1;
203
+ }
204
+ return output("\"");
205
+ }
206
+ static int name_order(const void *a, const void *b) { return strcmp(*(const char *const *)a, *(const char *const *)b); }
207
+ static int tree(int directory, const char *path, const char *ancestry, unsigned depth, unsigned *entries) {
208
+ if (depth > MAX_DEPTH) return fail(TOO_LARGE, "path_too_deep");
209
+ struct stat before;
210
+ if (safe_directory(directory, &before) < 0) return -1;
211
+ char id[MAX_ID] = {0};
212
+ if (append(id, MAX_ID, "%s", ancestry) < 0) return -1;
213
+ if (file_identity(id, &before) < 0) return -1;
214
+ if ((*entries)++ >= MAX_ENTRIES) return fail(TOO_LARGE, "too_many_entries");
215
+ if (reply_length > 1 && output(",") < 0) return -1;
216
+ if (output("{\"path\":") < 0 || quoted(path) < 0 || output(",\"type\":\"directory\",\"identity\":") < 0 ||
217
+ quoted(id) < 0 || output("}") < 0) return -1;
218
+ int copy = openat(directory, ".", O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
219
+ if (copy < 0) return fail(IO_ERROR, "directory_open_failed");
220
+ DIR *scan = fdopendir(copy);
221
+ if (!scan) { close(copy); return fail(IO_ERROR, "directory_scan_failed"); }
222
+ char *names[MAX_ENTRIES]; size_t count = 0; int result = -1;
223
+ struct dirent *entry; errno = 0;
224
+ while ((entry = readdir(scan))) {
225
+ if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..") || !strcasecmp(entry->d_name, ".git")) continue;
226
+ if (count >= MAX_ENTRIES || *entries + count >= MAX_ENTRIES) { fail(TOO_LARGE, "too_many_entries"); goto done; }
227
+ names[count] = strdup(entry->d_name);
228
+ if (!names[count]) { fail(IO_ERROR, "allocation_failed"); goto done; }
229
+ count++;
230
+ }
231
+ if (errno) { fail(IO_ERROR, "directory_scan_failed"); goto done; }
232
+ qsort(names, count, sizeof(names[0]), name_order);
233
+ for (size_t i = 0; i < count; i++) {
234
+ char child[MAX_PATH_BYTES + 1], identity[MAX_ID] = {0};
235
+ int n = !strcmp(path, ".") ? snprintf(child, sizeof(child), "%s", names[i]) :
236
+ snprintf(child, sizeof(child), "%s/%s", path, names[i]);
237
+ if (n < 0 || (size_t)n >= sizeof(child) || validate_path(child, (size_t)n) < 0) goto done;
238
+ struct stat s;
239
+ if (fstatat(directory, names[i], &s, AT_SYMLINK_NOFOLLOW) < 0) { fail(STALE, "entry_changed"); goto done; }
240
+ if (exact_name(directory, names[i], &s) < 0) goto done;
241
+ if (S_ISDIR(s.st_mode)) {
242
+ int next = openat(directory, names[i], O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK);
243
+ if (next < 0) { fail(DENIED, "unsafe_parent_component"); goto done; }
244
+ struct stat opened; char next_id[MAX_ID] = {0};
245
+ if (fstat(next, &opened) < 0 || !same_inode(&opened, &s) ||
246
+ append(next_id, MAX_ID, "%s|d:%ju:%ju", ancestry, (uintmax_t)s.st_dev, (uintmax_t)s.st_ino) < 0 ||
247
+ tree(next, child, next_id, depth + 1, entries) < 0) { close(next); goto done; }
248
+ close(next);
249
+ } else {
250
+ if (safe_file(&s) < 0 || append(identity, MAX_ID, "%s", ancestry) < 0 || file_identity(identity, &s) < 0) goto done;
251
+ if ((*entries)++ >= MAX_ENTRIES) { fail(TOO_LARGE, "too_many_entries"); goto done; }
252
+ if (output(",{\"path\":") < 0 || quoted(child) < 0 || output(",\"type\":\"file\",\"identity\":") < 0 ||
253
+ quoted(identity) < 0 || output("}") < 0) goto done;
254
+ }
255
+ }
256
+ struct stat after; char after_id[MAX_ID] = {0};
257
+ if (fstat(directory, &after) < 0 || append(after_id, MAX_ID, "%s", ancestry) < 0 ||
258
+ file_identity(after_id, &after) < 0 || strcmp(id, after_id)) { fail(STALE, "directory_changed"); goto done; }
259
+ result = 0;
260
+ done:
261
+ for (size_t i = 0; i < count; i++) free(names[i]);
262
+ closedir(scan); return result;
263
+ }
264
+ static int run_tree(const char *path, const char *expected, int executing) {
265
+ int directory = -1, parent = -1, result = -1; char leaf[256], identity[MAX_ID] = {0};
266
+ if (!strcmp(path, ".")) directory = fcntl(ROOT_FD, F_DUPFD_CLOEXEC, 4 + MAX_PROTECTED);
267
+ else {
268
+ parent = parent_fd(path, leaf, identity);
269
+ if (parent < 0) goto done;
270
+ directory = openat(parent, leaf, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK);
271
+ if (directory < 0) { fail(DENIED, "unsafe_directory"); goto done; }
272
+ struct stat s;
273
+ if (safe_directory(directory, &s) < 0 || exact_name(parent, leaf, &s) < 0) goto done;
274
+ }
275
+ if (directory < 0) { fail(DENIED, "missing_root_descriptor"); goto done; }
276
+ struct stat root;
277
+ if (safe_directory(directory, &root) < 0 || append(identity, MAX_ID, "%s%ju:%ju", !strcmp(path, ".") ? "v1|d:" : "|d:",
278
+ (uintmax_t)root.st_dev, (uintmax_t)root.st_ino) < 0) goto done;
279
+ unsigned entries = 0;
280
+ if (output("[") < 0 || tree(directory, path, identity, 0, &entries) < 0 || output("]") < 0) goto done;
281
+ if (executing && strcmp((const char *)reply, expected)) { fail(STALE, "directory_changed"); goto done; }
282
+ result = 0;
283
+ done:
284
+ if (directory >= 0) close(directory);
285
+ if (parent >= 0) close(parent);
286
+ return result;
287
+ }
288
+ static int no_replace(int from, const char *source, int to, const char *destination) {
289
+ #ifdef __APPLE__
290
+ return renameatx_np(from, source, to, destination, RENAME_EXCL);
291
+ #else
292
+ (void)from; (void)source; (void)to; (void)destination;
293
+ errno = ENOTSUP; return -1;
294
+ #endif
295
+ }
296
+ /* Namespace effects are NOT unlink-by-file-FD. Atomically capture the selected
297
+ * parent/name into an engine-private same-device quarantine, inspect that actual
298
+ * captured object before destruction/publication, and preserve unexpected objects.
299
+ * Restoring an unexpected capture never overwrites a new source entry. */
300
+ static int namespace_effect(uint8_t operation, const char *path, const char *expected, const uint8_t *data, size_t length) {
301
+ int moving = operation == PROBE_MOVE || operation == MOVE_FILE;
302
+ int deleting_directory = operation == PROBE_DELETE_DIRECTORY || operation == DELETE_DIRECTORY;
303
+ int executing = operation == DELETE_FILE || operation == MOVE_FILE || operation == DELETE_DIRECTORY;
304
+ char destination[MAX_PATH_BYTES + 1] = {0}, holding[64] = {0};
305
+ if (quarantine < 0) return fail(DENIED, "quarantine_required");
306
+ struct stat q, root;
307
+ if (safe_directory(quarantine, &q) < 0 || fstat(ROOT_FD, &root) < 0 ||
308
+ (q.st_mode & 077) || q.st_dev != root.st_dev) return fail(DENIED, "unsupported_quarantine");
309
+ size_t destination_length = 0;
310
+ if (moving) {
311
+ destination_length = executing ? (length >= 4 ? u32(data) : MAX_PATH_BYTES + 1) : length;
312
+ if (!destination_length || destination_length > MAX_PATH_BYTES ||
313
+ (executing && length != 4 + destination_length + 38)) return fail(BAD_REQUEST, "invalid_namespace_request");
314
+ memcpy(destination, data + (executing ? 4 : 0), destination_length);
315
+ if (validate_path(destination, destination_length) < 0 || !strcmp(path, destination)) return fail(BAD_REQUEST, "invalid_namespace_destination");
316
+ }
317
+ if (executing) {
318
+ const uint8_t *token = data + (moving ? 4 + destination_length : 0);
319
+ if ((!moving && length != 38) || token[0] != 'q' || token[1] != '-') return fail(BAD_REQUEST, "invalid_namespace_token");
320
+ for (size_t i = 2; i < 38; i++) if (!((token[i] >= '0' && token[i] <= '9') ||
321
+ (token[i] >= 'a' && token[i] <= 'f') || token[i] == '-')) return fail(BAD_REQUEST, "invalid_namespace_token");
322
+ memcpy(holding, token, 38);
323
+ }
324
+ char leaf[256], target_leaf[256], identity[MAX_ID] = {0}, target_identity[MAX_ID] = {0};
325
+ int parent = parent_fd(path, leaf, identity), target_parent = -1, file = -1, result = -1, captured = 0;
326
+ if (parent < 0) return -1;
327
+ struct stat before, opened, captured_stat;
328
+ if (fstatat(parent, leaf, &before, AT_SYMLINK_NOFOLLOW) < 0) { fail(NOT_FOUND, "file_not_found"); goto done; }
329
+ if ((!deleting_directory && safe_file(&before) < 0) || exact_name(parent, leaf, &before) < 0 || file_identity(identity, &before) < 0) goto done;
330
+ file = openat(parent, leaf, O_RDONLY | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK | (deleting_directory ? O_DIRECTORY : 0));
331
+ if (file < 0 || fstat(file, &opened) < 0 || !same_inode(&before, &opened)) { fail(STALE, "entry_changed"); goto done; }
332
+ if (deleting_directory && safe_directory(file, &opened) < 0) goto done;
333
+ if (moving) {
334
+ target_parent = parent_fd(destination, target_leaf, target_identity);
335
+ if (target_parent < 0) goto done;
336
+ struct stat exists;
337
+ if (fstatat(target_parent, target_leaf, &exists, AT_SYMLINK_NOFOLLOW) == 0 || errno != ENOENT) { fail(DENIED, "destination_exists"); goto done; }
338
+ if (append(identity, MAX_ID, "\n%s|absent", target_identity) < 0) goto done;
339
+ }
340
+ if (!executing) {
341
+ reply_length = strlen(identity); reply = malloc(reply_length + 1);
342
+ if (!reply) { fail(IO_ERROR, "allocation_failed"); goto done; }
343
+ memcpy(reply, identity, reply_length + 1); result = 0; goto done;
344
+ }
345
+ if (strcmp(identity, expected)) { fail(STALE, "identity_changed"); goto done; }
346
+ if (no_replace(parent, leaf, quarantine, holding) < 0) { fail(STALE, "namespace_capture_failed"); goto done; }
347
+ captured = 1;
348
+ if (fstatat(quarantine, holding, &captured_stat, AT_SYMLINK_NOFOLLOW) < 0 ||
349
+ !same_inode(&before, &captured_stat) || (!deleting_directory && safe_file(&captured_stat) < 0) ||
350
+ captured_stat.st_size != before.st_size || captured_stat.st_mode != before.st_mode ||
351
+ #ifdef __APPLE__
352
+ captured_stat.st_mtimespec.tv_sec != before.st_mtimespec.tv_sec || captured_stat.st_mtimespec.tv_nsec != before.st_mtimespec.tv_nsec) {
353
+ #else
354
+ captured_stat.st_mtim.tv_sec != before.st_mtim.tv_sec || captured_stat.st_mtim.tv_nsec != before.st_mtim.tv_nsec) {
355
+ #endif
356
+ fail(STALE, "namespace_target_changed"); goto done;
357
+ }
358
+ if (moving) {
359
+ if (no_replace(quarantine, holding, target_parent, target_leaf) < 0) { fail(STALE, "destination_conflict"); goto done; }
360
+ } else if (unlinkat(quarantine, holding, deleting_directory ? AT_REMOVEDIR : 0) < 0) { fail(UNCERTAIN, "quarantine_delete_failed"); goto done; }
361
+ captured = 0;
362
+ if (fsync(parent) < 0 || fsync(quarantine) < 0 || (moving && fsync(target_parent) < 0)) { fail(UNCERTAIN, "namespace_sync_failed"); goto done; }
363
+ result = 0;
364
+ done:
365
+ if (captured && no_replace(quarantine, holding, parent, leaf) < 0) fail(UNCERTAIN, "namespace_conflict_preserved");
366
+ if (file >= 0) close(file);
367
+ if (target_parent >= 0) close(target_parent);
368
+ close(parent); return result;
369
+ }
370
+ static int run_request(uint8_t operation, const char *path, const char *expected,
371
+ const uint8_t *data, size_t data_length) {
372
+ char leaf[256], identity[MAX_ID] = {0};
373
+ int parent = parent_fd(path, leaf, identity);
374
+ if (parent < 0) return -1;
375
+ int file = -1, absent = 0, result = -1, mutated = 0;
376
+ struct stat before, entry;
377
+ if (fstatat(parent, leaf, &entry, AT_SYMLINK_NOFOLLOW) < 0) {
378
+ if (errno != ENOENT) { fail(IO_ERROR, "target_stat_failed"); goto done; }
379
+ absent = 1;
380
+ if (operation == READ_FILE || operation == PROBE_READ || operation == SET_MODE || operation == PROBE_MODE) { fail(NOT_FOUND, "file_not_found"); goto done; }
381
+ if (append(identity, MAX_ID, "|absent") < 0) goto done;
382
+ } else {
383
+ int directory_mode = (operation == SET_MODE || operation == PROBE_MODE) && S_ISDIR(entry.st_mode);
384
+ if ((!directory_mode && safe_file(&entry) < 0) || exact_name(parent, leaf, &entry) < 0) goto done;
385
+ int flags = operation == WRITE_FILE ? O_RDWR : O_RDONLY;
386
+ file = openat(parent, leaf, flags | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK | (directory_mode ? O_DIRECTORY : 0));
387
+ if (file < 0) { fail(DENIED, "target_open_failed"); goto done; }
388
+ if (fstat(file, &before) < 0) { fail(IO_ERROR, "target_stat_failed"); goto done; }
389
+ if ((directory_mode ? safe_directory(file, &before) : safe_file(&before)) < 0 || !same_inode(&before, &entry)) { fail(STALE, "target_changed"); goto done; }
390
+ if (exact_name(parent, leaf, &before) < 0 || file_identity(identity, &before) < 0) goto done;
391
+ }
392
+ if (operation == PROBE_READ || operation == PROBE_WRITE || operation == PROBE_MODE) {
393
+ reply_length = strlen(identity); reply = malloc(reply_length ? reply_length : 1);
394
+ if (!reply) { fail(IO_ERROR, "allocation_failed"); goto done; }
395
+ memcpy(reply, identity, reply_length); result = 0; goto done;
396
+ }
397
+ if (strcmp(identity, expected) != 0) { fail(STALE, "identity_changed"); goto done; }
398
+ if (operation == MAKE_DIRECTORY) {
399
+ if (!absent || mkdirat(parent, leaf, 0700) < 0) { fail(STALE, "directory_creation_conflict"); goto done; }
400
+ if (fsync(parent) < 0) { fail(UNCERTAIN, "directory_sync_failed"); goto done; }
401
+ result = 0; goto done;
402
+ }
403
+ if (operation == SET_MODE) {
404
+ uint32_t mode = u32(data);
405
+ if (data_length != 4 || mode > 0777 || (mode & 022)) { fail(BAD_REQUEST, "unsafe_mode"); goto done; }
406
+ if (fchmod(file, (mode_t)mode) < 0 || fsync(file) < 0) { fail(UNCERTAIN, "mode_change_uncertain"); goto done; }
407
+ result = 0; goto done;
408
+ }
409
+ if (operation == READ_FILE) {
410
+ reply_length = (size_t)before.st_size; reply = malloc(reply_length ? reply_length : 1);
411
+ if (!reply) { fail(IO_ERROR, "allocation_failed"); goto done; }
412
+ size_t offset = 0;
413
+ while (offset < reply_length) {
414
+ ssize_t n = pread(file, reply + offset, reply_length - offset, (off_t)offset);
415
+ if (n < 0 && errno == EINTR) continue;
416
+ if (n <= 0) { fail(STALE, "file_changed_during_read"); goto done; }
417
+ offset += (size_t)n;
418
+ }
419
+ struct stat after;
420
+ char check[MAX_ID] = {0};
421
+ if (fstat(file, &after) < 0 || file_identity(check, &before) < 0) { fail(IO_ERROR, "target_stat_failed"); goto done; }
422
+ char after_id[MAX_ID] = {0};
423
+ if (file_identity(after_id, &after) < 0) goto done;
424
+ if (strcmp(check, after_id) || exact_name(parent, leaf, &after) < 0) { fail(STALE, "file_changed_during_read"); goto done; }
425
+ result = 0; goto done;
426
+ }
427
+ if (absent) {
428
+ file = openat(parent, leaf, O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK | O_RDWR, 0600);
429
+ if (file < 0) { fail(errno == EEXIST ? STALE : IO_ERROR, "creation_conflict"); goto done; }
430
+ mutated = 1;
431
+ if (fstat(file, &before) < 0 || safe_file(&before) < 0) { fail(UNCERTAIN, "created_identity_uncertain"); goto done; }
432
+ }
433
+ if (flock(file, LOCK_EX | LOCK_NB) < 0) { fail(mutated ? UNCERTAIN : STALE, "file_locked"); goto done; }
434
+ size_t offset = 0;
435
+ while (offset < data_length) {
436
+ struct stat current;
437
+ if (fstat(file, &current) < 0 || !same_inode(&current, &before) || safe_file(&current) < 0) {
438
+ fail(mutated ? UNCERTAIN : STALE, "write_identity_changed"); goto done;
439
+ }
440
+ ssize_t n = pwrite(file, data + offset, data_length - offset, (off_t)offset);
441
+ if (n < 0 && errno == EINTR) continue;
442
+ if (n <= 0) { fail(UNCERTAIN, "partial_write"); goto done; }
443
+ mutated = 1; offset += (size_t)n;
444
+ }
445
+ if (ftruncate(file, (off_t)data_length) < 0) { fail(UNCERTAIN, "truncate_failed"); goto done; }
446
+ mutated = 1;
447
+ if (fsync(file) < 0 || (absent && fsync(parent) < 0)) { fail(UNCERTAIN, "sync_failed"); goto done; }
448
+ struct stat final;
449
+ if (fstat(file, &final) < 0 || safe_file(&final) < 0 || !same_inode(&before, &final) ||
450
+ exact_name(parent, leaf, &final) < 0 || final.st_size != (off_t)data_length) {
451
+ fail(UNCERTAIN, "write_result_uncertain"); goto done;
452
+ }
453
+ uint8_t check[8192];
454
+ offset = 0;
455
+ while (offset < data_length) {
456
+ size_t count = data_length - offset < sizeof(check) ? data_length - offset : sizeof(check);
457
+ ssize_t n = pread(file, check, count, (off_t)offset);
458
+ if (n < 0 && errno == EINTR) continue;
459
+ if (n <= 0 || memcmp(check, data + offset, (size_t)n) != 0) { fail(UNCERTAIN, "write_content_uncertain"); goto done; }
460
+ offset += (size_t)n;
461
+ }
462
+ result = 0;
463
+ done:
464
+ if (file >= 0) close(file);
465
+ close(parent);
466
+ return result;
467
+ }
468
+ int main(void) {
469
+ uint8_t header[24], response[16] = {'C','6','0','R',1,0,0,0,0,0,0,0,0,0,0,0};
470
+ char *path = NULL, *expected = NULL;
471
+ uint8_t *data = NULL;
472
+ if (read_exact(STDIN_FILENO, header, sizeof(header)) < 0) { fail(BAD_REQUEST, "truncated_header"); goto respond; }
473
+ uint8_t operation = header[5];
474
+ uint32_t path_length = u32(header + 8), identity_length = u32(header + 12), data_length = u32(header + 16);
475
+ uint8_t protected_count = header[6];
476
+ if (memcmp(header, "C60F", 4) || header[4] != 1 || !protected_count || protected_count > MAX_PROTECTED || header[7] > 1 || u32(header + 20) ||
477
+ operation < PROBE_READ || operation > PROBE_MODE || operation == 12 || (operation != VALIDATE_ROOT && !path_length) || path_length > MAX_PATH_BYTES ||
478
+ identity_length > MAX_DATA || (operation != READ_TREE && identity_length >= MAX_ID) || data_length > MAX_DATA ||
479
+ ((operation == PROBE_READ || operation == PROBE_WRITE) && (identity_length || data_length)) ||
480
+ ((operation == READ_FILE || operation == WRITE_FILE) && !identity_length) ||
481
+ (operation == READ_FILE && data_length) ||
482
+ ((operation == PROBE_TREE || operation == PROBE_DELETE) && (identity_length || data_length)) ||
483
+ (operation == PROBE_MOVE && (identity_length || !data_length)) ||
484
+ ((operation == READ_TREE || operation == DELETE_FILE || operation == MOVE_FILE) && !identity_length) ||
485
+ (operation == READ_TREE && data_length) ||
486
+ ((operation == PROBE_DELETE_DIRECTORY || operation == PROBE_MODE) && (identity_length || data_length)) ||
487
+ ((operation == DELETE_DIRECTORY || operation == MAKE_DIRECTORY || operation == SET_MODE) && !identity_length) ||
488
+ (operation == MAKE_DIRECTORY && data_length) || (operation == SET_MODE && data_length != 4) ||
489
+ (operation == VALIDATE_ROOT && (path_length || identity_length || data_length))) { fail(BAD_REQUEST, "invalid_header"); goto respond; }
490
+ path = calloc((size_t)path_length + 1, 1); expected = calloc((size_t)identity_length + 1, 1);
491
+ data = malloc(data_length ? (size_t)data_length : 1);
492
+ if (!path || !expected || !data) { fail(IO_ERROR, "allocation_failed"); goto respond; }
493
+ if (read_exact(STDIN_FILENO, path, path_length) < 0 || read_exact(STDIN_FILENO, expected, identity_length) < 0 ||
494
+ read_exact(STDIN_FILENO, data, data_length) < 0) { fail(BAD_REQUEST, "truncated_body"); goto respond; }
495
+ uint8_t extra;
496
+ ssize_t trailing;
497
+ do { trailing = read(STDIN_FILENO, &extra, 1); } while (trailing < 0 && errno == EINTR);
498
+ if (trailing != 0 || memchr(expected, 0, identity_length)) { fail(BAD_REQUEST, "invalid_body"); goto respond; }
499
+ if (operation != VALIDATE_ROOT && !((operation == PROBE_TREE || operation == READ_TREE) && !strcmp(path, ".")) &&
500
+ validate_path(path, path_length) < 0) goto respond;
501
+ if (protect_roots(protected_count) < 0) goto respond;
502
+ if (header[7]) quarantine = 3 + protected_count;
503
+ if (operation == VALIDATE_ROOT) {
504
+ struct stat root;
505
+ if (safe_directory(ROOT_FD, &root) == 0) {
506
+ struct stat q;
507
+ if (quarantine >= 0 && (safe_directory(quarantine, &q) < 0 || (q.st_mode & 077) || q.st_dev != root.st_dev))
508
+ fail(DENIED, "unsupported_quarantine");
509
+ else status = OK;
510
+ }
511
+ goto respond;
512
+ }
513
+ if (operation == PROBE_TREE || operation == READ_TREE) {
514
+ if (run_tree(path, expected, operation == READ_TREE) == 0) status = OK;
515
+ goto respond;
516
+ }
517
+ if ((operation >= PROBE_DELETE && operation <= MOVE_FILE) || operation == PROBE_DELETE_DIRECTORY || operation == DELETE_DIRECTORY) {
518
+ if (namespace_effect(operation, path, expected, data, data_length) == 0) status = OK;
519
+ goto respond;
520
+ }
521
+ if (run_request(operation, path, expected, data, data_length) == 0) status = OK;
522
+ respond:
523
+ if (status != OK) { free(reply); reply = NULL; reply_length = strlen(reason); }
524
+ response[5] = (uint8_t)status; put32(response + 8, (uint32_t)reply_length);
525
+ int output_failed = write_exact(STDOUT_FILENO, response, sizeof(response)) < 0 ||
526
+ write_exact(STDOUT_FILENO, status == OK ? (const void *)reply : (const void *)reason, reply_length) < 0;
527
+ free(path); free(expected); free(data); free(reply);
528
+ return output_failed ? 1 : 0;
529
+ }