@[has_globals] module main import os import vmakepkg // root_provider is how vpac gets root: the config's (options #:root "..."), // sudo unless it says otherwise. vpac re-runs itself through it with the // same arguments, so it gives the same pipes and exit status. it is a // command line of the user's own: any program that runs the words after it // as root (doas, run0, "sudo -A"), its own arguments parted by spaces. two // are known by name, since they take the command another way: su wants one // shell line after -c, and pkexec — which asks through the desktop's polkit // agent, what a frontend without a terminal (--ui ipc) needs — must be told // to stay in the working dir __global root_provider = 'sudo' // env_invoker is how the root process learns who asked, so pkgbuild builds can run // as that user. the provider may drop the environment, so it is passed through // env, which every provider runs const env_invoker = 'VPAC_INVOKER' // env_v hands the root process the invoker's own v compiler: PKGBUILD.vsh // packages run through it, and the provider's sanitized path does not know // where the user keeps theirs const env_v = 'VPAC_V' // shell_words spells a command as the one line su -c hands root's shell: // every word quoted whole, so the shell reads back exactly these words — // the arguments are the user's, and nothing in them may become syntax fn shell_words(words []string) string { return words.map(vmakepkg.shell_quote).join(' ') } // can_write tells whether this user may write path, or create it when it does // not exist yet fn can_write(path string) bool { target := if os.exists(path) { path } else { os.dir(path) } return os.is_writable(target) } // root_argv is the words handed to the root provider after its own name: // the provider's own arguments, then env carrying envs, then vpac and its // arguments fn root_argv(provider []string, envs []string, exe string, args []string) []string { mut run := ['env'] run << envs run << exe run << args mut out := provider[1..].clone() match os.base(provider[0]) { 'su' { out << ['-c', shell_words(run)] } 'pkexec' { // pkexec starts in root's home otherwise, and a relative target // (a package file, a repo dir) is relative to where vpac was run out << '--keep-cwd' out << run } else { out << run } } return out } // reexec_as_root replaces this process with vpac running as root through the // root provider. it only returns on error, and not when already root fn reexec_as_root(args []string) ! { if os.geteuid() == 0 { return } invoker := current_user()! words := root_provider.fields() if words.len == 0 { return error('config: options #:root names no command to get root with') } provider := os.find_abs_path_of_executable(words[0]) or { return error('${words[0]} is needed to run this as root, but it was not found: name another with (options #:root "...") in the config') } // the compiler rides along, exactly like the invoker does vbin := os.find_abs_path_of_executable('v') or { '' } mut envs := ['${env_invoker}=${invoker}'] if vbin != '' { envs << '${env_v}=${vbin}' } // os.execvp spells argv[0] out of cmdpath itself, so the list handed // here is the words after the program name, and the process list shows // the provider correctly spelled rather than one word short os.execvp(provider, root_argv(words, envs, os.executable(), args))! } #include struct C.passwd { pw_name &char pw_uid u32 pw_dir &char } fn C.getpwnam(name &char) &C.passwd fn C.getpwuid(uid u32) &C.passwd // current_user is the user this process runs as, named from its real uid: // the user doas, sudo and pkexec report as their caller too. getlogin(3), // which os.loginname is, answers the session's login instead — the user // before an su, or nobody without a login session (a frontend started by a // service) — and a build would land in another user's home, or nowhere fn current_user() !string { uid := os.getuid() pw := C.getpwuid(u32(uid)) if pw == unsafe { nil } || pw.pw_name == unsafe { nil } { return error('could not tell which user is running vpac: uid ${uid} is not in the password database') } // getpwuid hands out a static buffer: the name is copied out of it return unsafe { cstring_to_vstring(pw.pw_name) } } // user_uid is a user's uid from the password database, none when there is // no such user fn user_uid(user string) ?u32 { pw := C.getpwnam(&char(user.str)) if pw == unsafe { nil } { return none } return pw.pw_uid } // invoker_of checks the user the escalation passed on before root builds // as it: a user of the password database, and not one with root's uid. root // is told by uid, not by name, since an alias of uid 0 is root to runuser // and to every file a build or a .vsh generator touches. the name comes from // vpac's own re-execution; the check keeps a stray or hand-set one from // aiming the builds at root or at nobody fn invoker_of(user string) !string { if user == '' { return error('pkgbuild packages are built as your user: run vpac as your user, it asks for root itself') } uid := user_uid(user) or { return error('the invoking user "${user}" (${env_invoker}) is not in the password database') } if uid == 0 { return error('pkgbuild packages are built as your user, not as root ("${user}" has uid 0): run vpac as your user, it asks for root itself') } return user } // user_home is a user's home dir from the password database, as getent // passwd finds it: through nss, so a home outside /home (/home2, an ldap // account) is right. the invoker's $HOME is not asked: the root process // would then list and size a path the user can set to anything fn user_home(user string) !string { pw := C.getpwnam(&char(user.str)) if pw == unsafe { nil } || pw.pw_dir == unsafe { nil } { return error('no home directory for user ${user} in the password database') } // getpwnam hands out a static buffer: the path is copied out of it return unsafe { cstring_to_vstring(pw.pw_dir) } } // invoker_user is who asked for this run: the user the escalation passed // on, or, in a run that never escalated (a --print preview), the user // running it fn invoker_user() !string { if os.geteuid() != 0 { // the same answer reexec_as_root passes on: a user that cannot be // told is an error, not an empty user to build under return current_user() } return invoker_of(os.getenv(env_invoker)) }