| 1 | |
| 2 | |
| 3 | |
| 4 | #import <Foundation/Foundation.h> |
| 5 | #include <sys/ioctl.h> |
| 6 | #include <sys/socket.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <sys/un.h> |
| 9 | #include <util.h> |
| 10 | #include <poll.h> |
| 11 | #include <signal.h> |
| 12 | #include <fcntl.h> |
| 13 | #include <errno.h> |
| 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | #include <unistd.h> |
| 17 | |
| 18 | static volatile sig_atomic_t signalWrite = -1; |
| 19 | static struct termios original; |
| 20 | static int changedTerminal; |
| 21 | |
| 22 | static void restoreTerminal(void) { |
| 23 | if (changedTerminal) tcsetattr(STDIN_FILENO, TCSANOW, &original); |
| 24 | } |
| 25 | |
| 26 | static void signalHandler(int number) { |
| 27 | int saved = errno; |
| 28 | unsigned char value = number; |
| 29 | if (signalWrite >= 0) (void)write(signalWrite, &value, 1); |
| 30 | errno = saved; |
| 31 | } |
| 32 | |
| 33 | static int writeAll(int fd, const void *data, size_t size) { |
| 34 | const unsigned char *bytes = data; |
| 35 | while (size) { |
| 36 | ssize_t n = write(fd, bytes, size); |
| 37 | if (n < 0 && errno == EINTR) continue; |
| 38 | if (n <= 0) return -1; |
| 39 | bytes += n; |
| 40 | size -= (size_t)n; |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static int sendTerminal(int control, int slave, NSData *request) { |
| 46 | struct iovec iov = {(void *)request.bytes, request.length}; |
| 47 | union { struct cmsghdr align; char bytes[CMSG_SPACE(sizeof(int))]; } rights = {0}; |
| 48 | struct msghdr msg = {0}; |
| 49 | msg.msg_iov = &iov; |
| 50 | msg.msg_iovlen = 1; |
| 51 | msg.msg_control = rights.bytes; |
| 52 | msg.msg_controllen = sizeof(rights.bytes); |
| 53 | struct cmsghdr *header = CMSG_FIRSTHDR(&msg); |
| 54 | header->cmsg_level = SOL_SOCKET; |
| 55 | header->cmsg_type = SCM_RIGHTS; |
| 56 | header->cmsg_len = CMSG_LEN(sizeof(int)); |
| 57 | memcpy(CMSG_DATA(header), &slave, sizeof(slave)); |
| 58 | ssize_t n; |
| 59 | do { n = sendmsg(control, &msg, 0); } while (n < 0 && errno == EINTR); |
| 60 | if (n <= 0) return -1; |
| 61 | return writeAll(control, (const unsigned char *)request.bytes + n, request.length - (size_t)n); |
| 62 | } |
| 63 | |
| 64 | static int relay(int control, int master, int signals) { |
| 65 | struct pollfd fds[] = {{STDIN_FILENO, POLLIN, 0}, {master, POLLIN, 0}, |
| 66 | {signals, POLLIN, 0}, {control, POLLIN, 0}}; |
| 67 | char buffer[16384]; |
| 68 | for (;;) { |
| 69 | if (poll(fds, 4, -1) < 0) { |
| 70 | if (errno == EINTR) continue; |
| 71 | return 1; |
| 72 | } |
| 73 | if (fds[2].revents) { |
| 74 | ssize_t n = read(signals, buffer, sizeof(buffer)); |
| 75 | for (ssize_t j = 0; j < n; j++) { |
| 76 | if (buffer[j] != SIGWINCH) return 0; |
| 77 | } |
| 78 | struct winsize size; |
| 79 | if (ioctl(STDIN_FILENO, TIOCGWINSZ, &size) == 0) |
| 80 | ioctl(master, TIOCSWINSZ, &size); |
| 81 | } |
| 82 | if (fds[1].revents) { |
| 83 | ssize_t n = read(master, buffer, sizeof(buffer)); |
| 84 | if (n < 0 && errno == EINTR) continue; |
| 85 | if (n <= 0) return (n == 0 || errno == EIO) ? 0 : 1; |
| 86 | if (writeAll(STDOUT_FILENO, buffer, (size_t)n) < 0) return 1; |
| 87 | |
| 88 | |
| 89 | if (fds[3].revents) continue; |
| 90 | } |
| 91 | if (fds[3].revents) return 0; |
| 92 | if (fds[0].revents) { |
| 93 | ssize_t n = read(STDIN_FILENO, buffer, sizeof(buffer)); |
| 94 | if (n < 0 && errno == EINTR) continue; |
| 95 | if (n <= 0) return 0; |
| 96 | if (writeAll(master, buffer, (size_t)n) < 0) return 1; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | int main(int argc, char **argv) { |
| 102 | @autoreleasepool { |
| 103 | BOOL editor = argc == 3 && strcmp(argv[1], "--editor") == 0; |
| 104 | BOOL claude = argc == 4 && strcmp(argv[1], "--claude") == 0; |
| 105 | BOOL interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO); |
| 106 | if ((argc != 2 && !editor && !claude) || (!editor && !interactive)) { |
| 107 | fprintf(stderr, "A project directory and an interactive guest terminal are required.\n"); |
| 108 | return 2; |
| 109 | } |
| 110 | NSString *project = [NSString stringWithUTF8String:argv[editor || claude ? 2 : 1]]; |
| 111 | if (!project || !project.isAbsolutePath) return 2; |
| 112 | NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Cage/project-terminal.sock"]; |
| 113 | struct sockaddr_un addr = {.sun_family = AF_UNIX}; |
| 114 | if (strlcpy(addr.sun_path, path.fileSystemRepresentation, sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) return 2; |
| 115 | addr.sun_len = SUN_LEN(&addr); |
| 116 | int control = socket(AF_UNIX, SOCK_STREAM, 0); |
| 117 | if (control < 0 || connect(control, (struct sockaddr *)&addr, addr.sun_len) != 0) { |
| 118 | fprintf(stderr, "The project helper is unavailable in this guest login session.\n"); |
| 119 | return 1; |
| 120 | } |
| 121 | uid_t peer; gid_t group; |
| 122 | if (getpeereid(control, &peer, &group) < 0 || peer != geteuid()) { |
| 123 | fprintf(stderr, "The project helper belongs to a different guest user.\n"); |
| 124 | return 1; |
| 125 | } |
| 126 | signal(SIGPIPE, SIG_IGN); |
| 127 | int master, slave; |
| 128 | struct winsize size = {.ws_row = 32, .ws_col = 100}; |
| 129 | if (interactive && (tcgetattr(STDIN_FILENO, &original) < 0 || ioctl(STDIN_FILENO, TIOCGWINSZ, &size) < 0)) return 1; |
| 130 | |
| 131 | |
| 132 | if (openpty(&master, &slave, NULL, interactive ? &original : NULL, &size) != 0 || fchmod(slave, 0600) < 0) return 1; |
| 133 | NSMutableDictionary *operation = [@{@"action": editor ? @"editor" : claude ? @"claude" : @"terminal", @"path": project} mutableCopy]; |
| 134 | if (claude) { |
| 135 | NSString *launch = [NSString stringWithUTF8String:argv[3]]; |
| 136 | if (!launch || [launch lengthOfBytesUsingEncoding:NSUTF8StringEncoding] > 4096) return 2; |
| 137 | operation[@"launch"] = launch; |
| 138 | } |
| 139 | NSMutableData *request = [[NSJSONSerialization dataWithJSONObject:operation options:0 error:nil] mutableCopy]; |
| 140 | [request appendBytes:"\n" length:1]; |
| 141 | if (sendTerminal(control, slave, request) < 0) return 1; |
| 142 | close(slave); |
| 143 | char reply[3]; |
| 144 | if (recv(control, reply, sizeof(reply), MSG_WAITALL) != sizeof(reply) || memcmp(reply, "OK\n", 3) != 0) { |
| 145 | fprintf(stderr, "The guest user has not approved this mounted project.\n"); |
| 146 | return 1; |
| 147 | } |
| 148 | int signals[2]; |
| 149 | if (pipe(signals) < 0 || fcntl(signals[1], F_SETFL, O_NONBLOCK) < 0) return 1; |
| 150 | signalWrite = signals[1]; |
| 151 | const int events[] = {SIGWINCH, SIGHUP, SIGINT, SIGTERM, SIGQUIT}; |
| 152 | struct sigaction action = {.sa_handler = signalHandler}; |
| 153 | sigemptyset(&action.sa_mask); |
| 154 | for (size_t i = 0; i < sizeof(events)/sizeof(events[0]); i++) sigaction(events[i], &action, NULL); |
| 155 | if (interactive) { |
| 156 | struct termios raw = original; |
| 157 | cfmakeraw(&raw); |
| 158 | atexit(restoreTerminal); |
| 159 | if (tcsetattr(STDIN_FILENO, TCSANOW, &raw) < 0) return 1; |
| 160 | changedTerminal = 1; |
| 161 | } |
| 162 | if (ioctl(STDIN_FILENO, TIOCGWINSZ, &size) == 0) ioctl(master, TIOCSWINSZ, &size); |
| 163 | int code = relay(control, master, signals[0]); |
| 164 | close(master); |
| 165 | close(control); |
| 166 | return code; |
| 167 | } |
| 168 | } |