Something went wrong. Try again.
Cider Isn't Darwin Emulation, Really
Something went wrong. Try again.
C
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212/*This file is part of Darling.
Copyright (C) 2016-2023 Lubos Dolezel
Darling is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.
Darling is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.
You should have received a copy of the GNU General Public Licensealong with Darling. If not, see <http://www.gnu.org/licenses/>.*/
#include <stdio.h>#include <sys/types.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <alloca.h>#include <errno.h>#include <fcntl.h>#include <sys/stat.h>#include <signal.h>#include <stdbool.h>#include <sched.h>#include <sys/poll.h>#include <sys/socket.h>#include <sys/un.h>#include <getopt.h>#include <termios.h>#include <pty.h>#include <pwd.h>#include "../shellspawn/shellspawn.h"#include "darling.h"#include "darling-config.h"
// Between Linux 4.9 and 4.11, a strange bug has been introduced// which prevents connecting to Unix sockets if the socket was// created in a different mount namespace or under overlayfs// (dunno which one is really responsible for this).#define USE_LINUX_4_11_HACK 1
char *prefix;uid_t g_originalUid, g_originalGid;bool g_fixPermissions = false;char g_workingDirectory[4096];
int main(int argc, char ** argv){ pid_t pidInit;
if (argc <= 1) { showHelp(argv[0]); return 1; }
if (geteuid() != 0) { missingSetuidRoot(); return 1; }
g_originalUid = getuid(); g_originalGid = getgid();
setuid(0); setgid(0);
prefix = getenv("DPREFIX"); if (!prefix) prefix = defaultPrefixPath(); if (!prefix) return 1; if (strlen(prefix) > 255) { fprintf(stderr, "Prefix path too long\n"); return 1; } unsetenv("DPREFIX"); getcwd(g_workingDirectory, sizeof(g_workingDirectory));
if (!checkPrefixDir()) { setupPrefix(); g_fixPermissions = true; } checkPrefixOwner();
int c; while (1) { static struct option long_options[] = { {"help", no_argument, 0, 0}, {"version", no_argument, 0, 0}, {0, 0, 0, 0} }; int option_index = 0;
c = getopt_long(argc, argv, "+", long_options, &option_index);
if (c == -1) { break; }
switch (c) { case 0: if (strcmp(long_options[option_index].name, "help") == 0) { showHelp(argv[0]); exit(EXIT_SUCCESS); } else if (strcmp(long_options[option_index].name, "version") == 0) { showVersion(argv[0]); exit(EXIT_SUCCESS); } break; case '?': break; default: abort(); } }
pidInit = getInitProcess();
if (strcmp(argv[1], "shutdown") == 0) { if (pidInit == 0) { fprintf(stderr, "Darling container is not running\n"); return 1; }
// TODO: when we have a working launchd, // this is where we ask it to shut down nicely
char path_buf[128]; FILE* file; pid_t launchd_pid; snprintf(path_buf, sizeof(path_buf), "/proc/%d/task/%d/children", pidInit, pidInit); file = fopen(path_buf, "r"); if (!file || fscanf(file, "%d", &launchd_pid) != 1) { fprintf(stderr, "Failed to shutdown Darling container\n"); if (file) { fclose(file); } return 1; } fclose(file);
kill(launchd_pid, SIGKILL); kill(pidInit, SIGKILL); return 0; }
// If prefix's init is not running, start it up if (pidInit == 0) { char socketPath[4096]; snprintf(socketPath, sizeof(socketPath), "%s" SHELLSPAWN_SOCKPATH, prefix); unlink(socketPath); setupWorkdir(); pidInit = spawnInitProcess(); putInitPid(pidInit); // Wait until shellspawn starts for (int i = 0; i < 15; i++) { if (access(socketPath, F_OK) == 0) break; sleep(1); } }
#if USE_LINUX_4_11_HACK joinNamespace(pidInit, CLONE_NEWNS, "mnt");#endif
seteuid(g_originalUid);
if (strcmp(argv[1], "shell") == 0) { // Spawn the shell if (argc > 2) spawnShell((const char**) &argv[2]); else spawnShell(NULL); } else { bool doExec = strcmp(argv[1], "exec") == 0; int argvIndex = doExec ? 2 : 1;
if (doExec && argc <= 2) { printf("'exec' subcommand requires a binary to execute.\n"); return 1; }
char *fullPath; char *path = realpath(argv[argvIndex], NULL);
if (path == NULL) { printf("'%s' is not a supported command or a file.\n", argv[argvIndex]); return 1; }
fullPath = malloc(strlen(SYSTEM_ROOT) + strlen(path) + 1); strcpy(fullPath, SYSTEM_ROOT); strcat(fullPath, path); free(path);
argv[argvIndex] = fullPath;
if (doExec) spawnBinary(argv[argvIndex], (const char**) &argv[argvIndex]); else spawnShell((const char**) &argv[argvIndex]); }
return 0;}
void joinNamespace(pid_t pid, int type, const char* typeName){ int fdNS; char pathNS[4096]; snprintf(pathNS, sizeof(pathNS), "/proc/%d/ns/%s", pid, typeName);
fdNS = open(pathNS, O_RDONLY);
if (fdNS < 0) { fprintf(stderr, "Cannot open %s namespace file: %s\n", typeName, strerror(errno)); exit(1); }
// Calling setns() with a PID namespace doesn't move our process into it, // but our child process will be spawned inside the namespace if (setns(fdNS, type) != 0) { fprintf(stderr, "Cannot join %s namespace: %s\n", typeName, strerror(errno)); exit(1); } close(fdNS);}
static void pushShellspawnCommandData(int sockfd, shellspawn_cmd_type_t type, const void* data, size_t data_length){ struct shellspawn_cmd* cmd; size_t length;
length = sizeof(*cmd) + data_length;
cmd = (struct shellspawn_cmd*) malloc(length); cmd->cmd = type; cmd->data_length = data_length;
if (data != NULL) memcpy(cmd->data, data, data_length);
if (write(sockfd, cmd, length) != length) { fprintf(stderr, "Error sending command to shellspawn: %s\n", strerror(errno)); exit(EXIT_FAILURE); }
free(cmd);}
static void pushShellspawnCommand(int sockfd, shellspawn_cmd_type_t type, const char* value){ if (!value) pushShellspawnCommandData(sockfd, type, NULL, 0); else pushShellspawnCommandData(sockfd, type, value, strlen(value) + 1);}
static void pushShellspawnCommandFDs(int sockfd, shellspawn_cmd_type_t type, const int fds[3]){ struct shellspawn_cmd cmd; char cmsgbuf[CMSG_SPACE(sizeof(int) * 3)]; struct msghdr msg; struct iovec iov; struct cmsghdr *cmptr;
cmd.cmd = type; cmd.data_length = 0;
iov.iov_base = &cmd;
memset(&msg, 0, sizeof(msg)); msg.msg_control = cmsgbuf; msg.msg_controllen = sizeof(cmsgbuf);
iov.iov_base = &cmd; iov.iov_len = sizeof(cmd); msg.msg_iov = &iov; msg.msg_iovlen = 1;
cmptr = CMSG_FIRSTHDR(&msg); cmptr->cmsg_len = CMSG_LEN(sizeof(int) * 3); cmptr->cmsg_level = SOL_SOCKET; cmptr->cmsg_type = SCM_RIGHTS; memcpy(CMSG_DATA(cmptr), fds, sizeof(fds[0])*3);
if (sendmsg(sockfd, &msg, 0) < 0) { fprintf(stderr, "Error sending command to shellspawn: %s\n", strerror(errno)); exit(EXIT_FAILURE); }}
static int _shSockfd = -1;static struct termios orig_termios;static int pty_master;static void signalHandler(int signo){ // printf("Received signal %d\n", signo);
// Forward window size changes if (signo == SIGWINCH && pty_master != -1) { struct winsize win;
ioctl(0, TIOCGWINSZ, &win); ioctl(pty_master, TIOCSWINSZ, &win); } // Foreground process loopkup in shellspawn doesn't work // if we're not running in TTY mode, so shellspawn falls back // to forwarding signals to the Bash subprocess. // // Hence we translate SIGINT to SIGTERM for user convenience, // because Bash will not terminate on SIGINT. if (pty_master == -1 && signo == SIGINT) signo = SIGTERM;
pushShellspawnCommandData(_shSockfd, SHELLSPAWN_SIGNAL, &signo, sizeof(signo));}
static void shellLoop(int sockfd, int master){ struct sigaction sa; struct pollfd pfds[3]; const int fdcount = (master != -1) ? 3 : 1; // do we do pty proxying?
// Vars for signal handler _shSockfd = sockfd; pty_master = master;
memset(&sa, 0, sizeof(sa)); sa.sa_handler = signalHandler; sigfillset(&sa.sa_mask);
for (int i = 1; i < 32; i++) sigaction(i, &sa, NULL);
pfds[2].fd = master; pfds[2].events = POLLIN; pfds[2].revents = 0; pfds[1].fd = STDIN_FILENO; pfds[1].events = POLLIN; pfds[1].revents = 0; pfds[0].fd = sockfd; pfds[0].events = POLLIN; pfds[0].revents = 0;
if (master != -1) fcntl(master, F_SETFL, O_NONBLOCK); //fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); fcntl(sockfd, F_SETFL, O_NONBLOCK);
while (1) { char buf[4096];
if (poll(pfds, fdcount, -1) < 0) { if (errno != EINTR) { perror("poll"); break; } }
if (pfds[2].revents & POLLIN) { int rd; do { rd = read(master, buf, sizeof(buf)); if (rd > 0) write(STDOUT_FILENO, buf, rd); } while (rd == sizeof(buf)); }
if (pfds[1].revents & POLLIN) { int rd = 0; do { if (ioctl(STDIN_FILENO, FIONREAD, &rd) < 0) { perror("ioctl"); exit(1); } if (rd > sizeof(buf)) { rd = sizeof(buf); } rd = read(STDIN_FILENO, buf, rd); if (rd > 0) write(master, buf, rd); else { perror("read"); exit(1); } } while (rd == sizeof(buf)); }
if (pfds[0].revents & (POLLHUP | POLLIN)) { int exitStatus; if (read(sockfd, &exitStatus, sizeof(int)) == sizeof(int)) exit(exitStatus); else exit(1); } }}
static void restoreTermios(void){ tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);}
// Glibc openpty() fails for me on Debian, because grantpty() fails to chown() the pty node with error code EPERM.// This is a more lenient version of openpty() that just works.static int openpty_darling(int* amaster, int* aslave, char* name_unused, const struct termios* tos, const struct winsize* wsz){ const char* slave_name;
*amaster = posix_openpt(O_RDWR); if (*amaster == -1) return -1;
grantpt(*amaster); if (unlockpt(*amaster) < 0) return -1;
slave_name = ptsname(*amaster); *aslave = open(slave_name, O_RDWR | O_NOCTTY); if (*aslave == -1) return -1;
if (tos != NULL) tcsetattr(*amaster, TCSANOW, tos); if (wsz != NULL) ioctl(*amaster, TIOCSWINSZ, wsz);
return 0;}
static void setupPtys(int fds[3], int* master){ struct winsize win; struct termios termios; bool tty = true;
if (tcgetattr(STDIN_FILENO, &termios) < 0) tty = false;
if (openpty_darling(master, &fds[0], NULL, &termios, NULL) < 0) { perror("openpty"); exit(1); } fds[2] = fds[1] = fds[0];
if (tty) { orig_termios = termios;
ioctl(0, TIOCGWINSZ, &win);
termios.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO); termios.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR | INPCK | ISTRIP | IXON | PARMRK); termios.c_oflag &= ~OPOST; termios.c_cc[VMIN] = 1; termios.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSANOW, &termios) < 0) { perror("tcsetattr"); exit(1); } ioctl(*master, TIOCSWINSZ, &win);
atexit(restoreTermios); }}
// Replace each quote character (') with the sequence '\''// (copying the result to dest, if non-null)// and return the length of the result.static size_t escapeQuotes(char *dest, const char *src){ size_t len = 0;
for (; *src != 0; src++) { if (*src == '\'') { if (dest) memcpy(&dest[len], "'\\''", 4); len += 4; } else { if (dest) dest[len] = *src; len++; } } if (dest) dest[len] = 0;
return len;}
int connectToShellspawn(void){ struct sockaddr_un addr; int sockfd;
// Connect to the shellspawn daemon in the container addr.sun_family = AF_UNIX;#if USE_LINUX_4_11_HACK addr.sun_path[0] = '\0'; strcpy(addr.sun_path, prefix); strcat(addr.sun_path, SHELLSPAWN_SOCKPATH);#else snprintf(addr.sun_path, sizeof(addr.sun_path), "%s" SHELLSPAWN_SOCKPATH, prefix);#endif
sockfd = socket(AF_UNIX, SOCK_STREAM, 0); if (sockfd == -1) { fprintf(stderr, "Error creating a unix domain socket: %s\n", strerror(errno)); exit(1); }
if (connect(sockfd, (struct sockaddr*) &addr, sizeof(addr)) == -1) { fprintf(stderr, "Error connecting to shellspawn in the container (%s): %s\n", addr.sun_path, strerror(errno)); exit(1); }
return sockfd;}
void setupShellspawnEnv(int sockfd){ static const char* skip_vars[] = { "PATH", "TMPDIR", "HOME", };
char buffer2[4096];
// Push environment variables pushShellspawnCommand(sockfd, SHELLSPAWN_SETENV, "PATH=/usr/bin:" "/bin:" "/usr/sbin:" "/sbin:" "/usr/local/bin"); pushShellspawnCommand(sockfd, SHELLSPAWN_SETENV, "TMPDIR=/private/tmp");
const char* login = NULL; struct passwd* pw = getpwuid(geteuid());
if (pw != NULL) login = pw->pw_name;
if (!login) login = getlogin(); if (!login) { fprintf(stderr, "Cannot determine your user name\n"); exit(1); }
snprintf(buffer2, sizeof(buffer2), "HOME=/Users/%s", login); pushShellspawnCommand(sockfd, SHELLSPAWN_SETENV, buffer2);
for (char** var_ptr = environ; *var_ptr != NULL; ++var_ptr) { const char* var = *var_ptr; const char* equal = strchr(var, '='); size_t name_len = (equal != NULL) ? (size_t)(equal - var) : strlen(var); bool skip_it = false;
for (size_t i = 0; i < sizeof(skip_vars) / sizeof(*skip_vars); ++i) { if (strlen(skip_vars[i]) == name_len && strncmp(var, skip_vars[i], name_len) == 0) { skip_it = true; break; } }
if (skip_it) { continue; }
pushShellspawnCommand(sockfd, SHELLSPAWN_SETENV, var); }}
void setupWorkingDir(int sockfd){ char buffer2[4096]; snprintf(buffer2, sizeof(buffer2), SYSTEM_ROOT "%s", g_workingDirectory); pushShellspawnCommand(sockfd, SHELLSPAWN_CHDIR, buffer2);}
void setupIDs(int sockfd){ int ids[2] = { g_originalUid, g_originalGid }; pushShellspawnCommandData(sockfd, SHELLSPAWN_SETUIDGID, ids, sizeof(ids));}
void setupFDs(int fds[3], int* master){ *master = -1;
if (isatty(STDIN_FILENO)) setupPtys(fds, master); else fds[0] = dup(STDIN_FILENO); // dup() because we close() after spawning if (*master == -1 || !isatty(STDOUT_FILENO)) fds[1] = STDOUT_FILENO; if (*master == -1 || !isatty(STDERR_FILENO)) fds[2] = STDERR_FILENO;}
void spawnGo(int sockfd, int fds[3], int master){ pushShellspawnCommandFDs(sockfd, SHELLSPAWN_GO, fds); close(fds[0]);
shellLoop(sockfd, master); if (master != -1) close(master); close(sockfd);}
void spawnShell(const char** argv){ size_t total_len = 0; int count; int sockfd; char* buffer; int fds[3], master;
if (argv != NULL) { for (count = 0; argv[count] != NULL; count++) total_len += escapeQuotes(NULL, argv[count]);
buffer = malloc(total_len + count*3);
char *to = buffer; for (int i = 0; argv[i] != NULL; i++) { if (to != buffer) to = stpcpy(to, " "); to = stpcpy(to, "'"); to += escapeQuotes(to, argv[i]); to = stpcpy(to, "'"); } } else buffer = NULL;
sockfd = connectToShellspawn();
setupShellspawnEnv(sockfd);
// Push shell arguments if (buffer != NULL) { pushShellspawnCommand(sockfd, SHELLSPAWN_ADDARG, "-c"); pushShellspawnCommand(sockfd, SHELLSPAWN_ADDARG, buffer);
free(buffer); }
setupWorkingDir(sockfd); setupIDs(sockfd); setupFDs(fds, &master); spawnGo(sockfd, fds, master);}
void spawnBinary(const char* binary, const char** argv){ int fds[3], master; int sockfd;
sockfd = connectToShellspawn(); setupShellspawnEnv(sockfd);
pushShellspawnCommand(sockfd, SHELLSPAWN_SETEXEC, binary);
for (; *argv != NULL; ++argv) pushShellspawnCommand(sockfd, SHELLSPAWN_ADDARG, *argv);
setupWorkingDir(sockfd); setupIDs(sockfd); setupFDs(fds, &master); spawnGo(sockfd, fds, master);}
void showHelp(const char* argv0){ fprintf(stderr, "This is Darling, translation layer for macOS software.\n\n"); fprintf(stderr, "Copyright (C) 2012-2023 Lubos Dolezel\n\n");
fprintf(stderr, "Usage:\n"); fprintf(stderr, "\t%s <program-path> [arguments...]\n", argv0); fprintf(stderr, "\t%s shell [arguments...]\n", argv0); fprintf(stderr, "\t%s exec <program-path> [arguments...]\n", argv0); fprintf(stderr, "\t%s shutdown\n", argv0); fprintf(stderr, "\n"); fprintf(stderr, "Environment variables:\n" "DPREFIX - specifies the location of Darling prefix, defaults to ~/.darling\n");}
void showVersion(const char* argv0) { fprintf(stderr, "%s " GIT_BRANCH " @ " GIT_COMMIT_HASH "\n", argv0); fprintf(stderr, "Copyright (C) 2012-2023 Lubos Dolezel\n");}
void missingSetuidRoot(void){ char path[4096]; int len;
len = readlink("/proc/self/exe", path, sizeof(path)-1); if (len < 0) strcpy(path, "darling"); else path[len] = '\0';
fprintf(stderr, "Sorry, the `%s' binary is not setuid root, which is mandatory.\n", path); fprintf(stderr, "Darling needs this in order to create mount and PID namespaces and to perform mounts.\n");}
pid_t spawnInitProcess(void){ pid_t pid; int pipefd[2]; char buffer[1];
if (pipe(pipefd) == -1) { fprintf(stderr, "Cannot create a pipe for synchronization: %s\n", strerror(errno)); exit(1); }
if (unshare(CLONE_NEWUTS | CLONE_NEWIPC) != 0) { fprintf(stderr, "Cannot unshare UTS and IPC namespaces to create darling-init: %s\n", strerror(errno)); exit(1); }
pid = fork();
if (pid < 0) { fprintf(stderr, "Cannot fork() to create darling-init: %s\n", strerror(errno)); exit(1); }
if (pid == 0) { // The child
char uid_str[21]; char gid_str[21]; char pipefd_str[21];
snprintf(uid_str, sizeof(uid_str), "%d", g_originalUid); snprintf(gid_str, sizeof(gid_str), "%d", g_originalGid); snprintf(pipefd_str, sizeof(pipefd_str), "%d", pipefd[1]);
close(pipefd[0]);
execl(INSTALL_PREFIX "/bin/darlingserver", "darlingserver", prefix, uid_str, gid_str, pipefd_str, g_fixPermissions ? "1" : "0", NULL);
fprintf(stderr, "Failed to start darlingserver\n"); exit(1); }
// Wait for the child to drop UID/GIDs and unshare stuff close(pipefd[1]); read(pipefd[0], buffer, 1); close(pipefd[0]);
/* snprintf(idmap, sizeof(idmap), "/proc/%d/uid_map", pid);
file = fopen(idmap, "w"); if (file != NULL) { fprintf(file, "0 %d 1\n", g_originalUid); // all users map to our user on the outside fclose(file); } else { fprintf(stderr, "Cannot set uid_map for the init process: %s\n", strerror(errno)); }
snprintf(idmap, sizeof(idmap), "/proc/%d/gid_map", pid);
file = fopen(idmap, "w"); if (file != NULL) { fprintf(file, "0 %d 1\n", g_originalGid); // all groups map to our group on the outside fclose(file); } else { fprintf(stderr, "Cannot set gid_map for the init process: %s\n", strerror(errno)); } */
// Here's where we resume the child // if we enable user namespaces
return pid;}
void putInitPid(pid_t pidInit){ const char pidFile[] = "/.init.pid"; char* pidPath; FILE *fp;
pidPath = (char*) alloca(strlen(prefix) + sizeof(pidFile)); strcpy(pidPath, prefix); strcat(pidPath, pidFile);
seteuid(g_originalUid); setegid(g_originalGid);
fp = fopen(pidPath, "w");
seteuid(0); setegid(0);
if (fp == NULL) { fprintf(stderr, "Cannot write out PID of the init process: %s\n", strerror(errno)); return; } fprintf(fp, "%d", (int) pidInit); fclose(fp);}
char* defaultPrefixPath(void){ const char defaultPath[] = "/.darling"; const char* home = getenv("HOME"); char* buf;
if (!home) { fprintf(stderr, "Cannot detect your home directory!\n"); return NULL; }
buf = (char*) malloc(strlen(home) + sizeof(defaultPath)); strcpy(buf, home); strcat(buf, defaultPath);
return buf;}
void createDir(const char* path){ struct stat st;
if (stat(path, &st) == 0) { if (!S_ISDIR(st.st_mode)) { fprintf(stderr, "%s already exists and is a file. Remove the file.\n", path); exit(1); } } else { if (errno == ENOENT) { if (mkdir(path, 0755) != 0) { fprintf(stderr, "Cannot create %s: %s\n", path, strerror(errno)); exit(1); } } else { fprintf(stderr, "Cannot access %s: %s\n", path, strerror(errno)); exit(1); } }}
void setupWorkdir(){ char* workdir; const char suffix[] = ".workdir"; size_t len;
len = strlen(prefix); workdir = (char*) alloca(len + sizeof(suffix)); strcpy(workdir, prefix);
// Remove trailing / while (workdir[len-1] == '/') len--; workdir[len] = '\0';
strcat(workdir, suffix);
createDir(workdir);}
int checkPrefixDir(){ struct stat st;
if (stat(prefix, &st) == 0) { if (!S_ISDIR(st.st_mode)) { fprintf(stderr, "%s is a file. Remove the file.\n", prefix); exit(1); } return 1; // OK } if (errno == ENOENT) return 0; // not found fprintf(stderr, "Cannot access %s: %s\n", prefix, strerror(errno)); exit(1);}
void setupPrefix(){ char path[4096]; size_t plen; FILE* file; struct passwd* passwd_entry; const char* dirs[] = { "/Volumes", "/Applications", "/usr", "/usr/local", "/usr/local/share", "/private", "/private/var", "/private/var/log", "/private/var/db", "/private/etc", "/var", "/var/run", "/var/tmp", "/var/log" };
fprintf(stderr, "Setting up a new Darling prefix at %s\n", prefix);
seteuid(g_originalUid); setegid(g_originalGid);
createDir(prefix); strcpy(path, prefix); strcat(path, "/"); plen = strlen(path);
for (size_t i = 0; i < sizeof(dirs)/sizeof(dirs[0]); i++) { path[plen] = '\0'; strcat(path, dirs[i]); createDir(path); }
// create passwd, master.passwd, and group
passwd_entry = getpwuid(g_originalUid); if (!passwd_entry) { fprintf(stderr, "Failed to find Linux /etc/passwd entry for current user\n"); exit(1); }
path[plen] = '\0'; strcat(path, "/private/etc/passwd"); file = fopen(path, "w"); if (!file) { fprintf(stderr, "Failed to open /private/etc/passwd within the prefix\n"); exit(1); }
fprintf(file, "root:*:0:0:System Administrator:/var/root:/bin/sh\n" "%s:*:%d:%d:Darling User:/Users/%s:/bin/bash\n", passwd_entry->pw_name, passwd_entry->pw_uid, passwd_entry->pw_gid, passwd_entry->pw_name ); fclose(file);
path[plen] = '\0'; strcat(path, "/private/etc/master.passwd"); file = fopen(path, "w"); if (!file) { fprintf(stderr, "Failed to open /private/etc/master.passwd within the prefix\n"); exit(1); }
fprintf(file, "root:*:0:0::0:0:System Administrator:/var/root:/bin/sh\n" "%s:*:%d:%d::0:0:Darling User:/Users/%s:/bin/bash\n", passwd_entry->pw_name, passwd_entry->pw_uid, passwd_entry->pw_gid, passwd_entry->pw_name ); fclose(file);
path[plen] = '\0'; strcat(path, "/private/etc/group"); file = fopen(path, "w"); if (!file) { fprintf(stderr, "Failed to open /private/etc/group within the prefix\n"); exit(1); }
fprintf(file, "wheel:*:0:root,%s\n" "%s:*:%d:%s\n", passwd_entry->pw_name, passwd_entry->pw_name, passwd_entry->pw_gid, passwd_entry->pw_name ); fclose(file); seteuid(0); setegid(0);}
pid_t getInitProcess(){ const char pidFile[] = "/.init.pid"; char* pidPath; pid_t pid; int pid_i; FILE *fp; char procBuf[100]; char *exeBuf, *statusBuf; int uidMatch = 0, gidMatch = 0;
pidPath = (char*) alloca(strlen(prefix) + sizeof(pidFile)); strcpy(pidPath, prefix); strcat(pidPath, pidFile);
fp = fopen(pidPath, "r"); if (fp == NULL) return 0;
if (fscanf(fp, "%d", &pid_i) != 1) { fclose(fp); unlink(pidPath); return 0; } fclose(fp); pid = (pid_t) pid_i;
// Does the process exist? if (kill(pid, 0) == -1) { unlink(pidPath); return 0; }
// Is it actually an init process? snprintf(procBuf, sizeof(procBuf), "/proc/%d/comm", pid); fp = fopen(procBuf, "r"); if (fp == NULL) { unlink(pidPath); return 0; }
if (fscanf(fp, "%ms", &exeBuf) != 1) { fclose(fp); unlink(pidPath); return 0; } fclose(fp);
if (strcmp(exeBuf, "darlingserver") != 0) { unlink(pidPath); return 0; } free(exeBuf);
// Is it owned by the current user? if (g_originalUid != 0) { snprintf(procBuf, sizeof(procBuf), "/proc/%d/status", pid); fp = fopen(procBuf, "r"); if (fp == NULL) { unlink(pidPath); return 0; }
while (1) { statusBuf = NULL; size_t len; if (getline(&statusBuf, &len, fp) == -1) break; int rid, eid, sid, fid; if (sscanf(statusBuf, "Uid: %d %d %d %d", &rid, &eid, &sid, &fid) == 4) { uidMatch = 1; uidMatch &= rid == g_originalUid; uidMatch &= eid == g_originalUid; uidMatch &= sid == g_originalUid; uidMatch &= fid == g_originalUid; } if (sscanf(statusBuf, "Gid: %d %d %d %d", &rid, &eid, &sid, &fid) == 4) { gidMatch = 1; gidMatch &= rid == g_originalGid; gidMatch &= eid == g_originalGid; gidMatch &= sid == g_originalGid; gidMatch &= fid == g_originalGid; } free(statusBuf); } fclose(fp);
if (!uidMatch || !gidMatch) { unlink(pidPath); return 0; } }
return pid;}
void checkPrefixOwner(){ struct stat st;
if (stat(prefix, &st) == 0) { if (g_originalUid != 0 && st.st_uid != g_originalUid) { fprintf(stderr, "You do not own the prefix directory.\n"); exit(1); } } else if (errno == EACCES) { fprintf(stderr, "You do not own the prefix directory.\n"); exit(1); }}