| 1 | @[has_globals] |
| 2 | module main |
| 3 | |
| 4 | import os |
| 5 | import vmakepkg |
| 6 | |
| 7 | // root_provider is how vpac gets root: the config's (options #:root "..."), |
| 8 | // sudo unless it says otherwise. vpac re-runs itself through it with the |
| 9 | // same arguments, so it gives the same pipes and exit status. it is a |
| 10 | // command line of the user's own: any program that runs the words after it |
| 11 | // as root (doas, run0, "sudo -A"), its own arguments parted by spaces. two |
| 12 | // are known by name, since they take the command another way: su wants one |
| 13 | // shell line after -c, and pkexec — which asks through the desktop's polkit |
| 14 | // agent, what a frontend without a terminal (--ui ipc) needs — must be told |
| 15 | // to stay in the working dir |
| 16 | __global root_provider = 'sudo' |
| 17 | |
| 18 | // env_invoker is how the root process learns who asked, so pkgbuild builds can run |
| 19 | // as that user. the provider may drop the environment, so it is passed through |
| 20 | // env, which every provider runs |
| 21 | const env_invoker = 'VPAC_INVOKER' |
| 22 | |
| 23 | // env_v hands the root process the invoker's own v compiler: PKGBUILD.vsh |
| 24 | // packages run through it, and the provider's sanitized path does not know |
| 25 | // where the user keeps theirs |
| 26 | const env_v = 'VPAC_V' |
| 27 | |
| 28 | // shell_words spells a command as the one line su -c hands root's shell: |
| 29 | // every word quoted whole, so the shell reads back exactly these words — |
| 30 | // the arguments are the user's, and nothing in them may become syntax |
| 31 | fn shell_words(words []string) string { |
| 32 | return words.map(vmakepkg.shell_quote).join(' ') |
| 33 | } |
| 34 | |
| 35 | // can_write tells whether this user may write path, or create it when it does |
| 36 | // not exist yet |
| 37 | fn can_write(path string) bool { |
| 38 | target := if os.exists(path) { path } else { os.dir(path) } |
| 39 | return os.is_writable(target) |
| 40 | } |
| 41 | |
| 42 | // root_argv is the words handed to the root provider after its own name: |
| 43 | // the provider's own arguments, then env carrying envs, then vpac and its |
| 44 | // arguments |
| 45 | fn root_argv(provider []string, envs []string, exe string, args []string) []string { |
| 46 | mut run := ['env'] |
| 47 | run << envs |
| 48 | run << exe |
| 49 | run << args |
| 50 | mut out := provider[1..].clone() |
| 51 | match os.base(provider[0]) { |
| 52 | 'su' { |
| 53 | out << ['-c', shell_words(run)] |
| 54 | } |
| 55 | 'pkexec' { |
| 56 | // pkexec starts in root's home otherwise, and a relative target |
| 57 | // (a package file, a repo dir) is relative to where vpac was run |
| 58 | out << '--keep-cwd' |
| 59 | out << run |
| 60 | } |
| 61 | else { |
| 62 | out << run |
| 63 | } |
| 64 | } |
| 65 | return out |
| 66 | } |
| 67 | |
| 68 | // reexec_as_root replaces this process with vpac running as root through the |
| 69 | // root provider. it only returns on error, and not when already root |
| 70 | fn reexec_as_root(args []string) ! { |
| 71 | if os.geteuid() == 0 { |
| 72 | return |
| 73 | } |
| 74 | invoker := current_user()! |
| 75 | words := root_provider.fields() |
| 76 | if words.len == 0 { |
| 77 | return error('config: options #:root names no command to get root with') |
| 78 | } |
| 79 | provider := os.find_abs_path_of_executable(words[0]) or { |
| 80 | return error('${words[0]} is needed to run this as root, but it was not found: name another with (options #:root "...") in the config') |
| 81 | } |
| 82 | // the compiler rides along, exactly like the invoker does |
| 83 | vbin := os.find_abs_path_of_executable('v') or { '' } |
| 84 | mut envs := ['${env_invoker}=${invoker}'] |
| 85 | if vbin != '' { |
| 86 | envs << '${env_v}=${vbin}' |
| 87 | } |
| 88 | // os.execvp spells argv[0] out of cmdpath itself, so the list handed |
| 89 | // here is the words after the program name, and the process list shows |
| 90 | // the provider correctly spelled rather than one word short |
| 91 | os.execvp(provider, root_argv(words, envs, os.executable(), args))! |
| 92 | } |
| 93 | |
| 94 | #include <pwd.h> |
| 95 | |
| 96 | struct C.passwd { |
| 97 | pw_name &char |
| 98 | pw_uid u32 |
| 99 | pw_dir &char |
| 100 | } |
| 101 | |
| 102 | fn C.getpwnam(name &char) &C.passwd |
| 103 | |
| 104 | fn C.getpwuid(uid u32) &C.passwd |
| 105 | |
| 106 | // current_user is the user this process runs as, named from its real uid: |
| 107 | // the user doas, sudo and pkexec report as their caller too. getlogin(3), |
| 108 | // which os.loginname is, answers the session's login instead — the user |
| 109 | // before an su, or nobody without a login session (a frontend started by a |
| 110 | // service) — and a build would land in another user's home, or nowhere |
| 111 | fn current_user() !string { |
| 112 | uid := os.getuid() |
| 113 | pw := C.getpwuid(u32(uid)) |
| 114 | if pw == unsafe { nil } || pw.pw_name == unsafe { nil } { |
| 115 | return error('could not tell which user is running vpac: uid ${uid} is not in the password database') |
| 116 | } |
| 117 | // getpwuid hands out a static buffer: the name is copied out of it |
| 118 | return unsafe { cstring_to_vstring(pw.pw_name) } |
| 119 | } |
| 120 | |
| 121 | // user_uid is a user's uid from the password database, none when there is |
| 122 | // no such user |
| 123 | fn user_uid(user string) ?u32 { |
| 124 | pw := C.getpwnam(&char(user.str)) |
| 125 | if pw == unsafe { nil } { |
| 126 | return none |
| 127 | } |
| 128 | return pw.pw_uid |
| 129 | } |
| 130 | |
| 131 | // invoker_of checks the user the escalation passed on before root builds |
| 132 | // as it: a user of the password database, and not one with root's uid. root |
| 133 | // is told by uid, not by name, since an alias of uid 0 is root to runuser |
| 134 | // and to every file a build or a .vsh generator touches. the name comes from |
| 135 | // vpac's own re-execution; the check keeps a stray or hand-set one from |
| 136 | // aiming the builds at root or at nobody |
| 137 | fn invoker_of(user string) !string { |
| 138 | if user == '' { |
| 139 | return error('pkgbuild packages are built as your user: run vpac as your user, it asks for root itself') |
| 140 | } |
| 141 | uid := user_uid(user) or { |
| 142 | return error('the invoking user "${user}" (${env_invoker}) is not in the password database') |
| 143 | } |
| 144 | if uid == 0 { |
| 145 | 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') |
| 146 | } |
| 147 | return user |
| 148 | } |
| 149 | |
| 150 | // user_home is a user's home dir from the password database, as getent |
| 151 | // passwd finds it: through nss, so a home outside /home (/home2, an ldap |
| 152 | // account) is right. the invoker's $HOME is not asked: the root process |
| 153 | // would then list and size a path the user can set to anything |
| 154 | fn user_home(user string) !string { |
| 155 | pw := C.getpwnam(&char(user.str)) |
| 156 | if pw == unsafe { nil } || pw.pw_dir == unsafe { nil } { |
| 157 | return error('no home directory for user ${user} in the password database') |
| 158 | } |
| 159 | // getpwnam hands out a static buffer: the path is copied out of it |
| 160 | return unsafe { cstring_to_vstring(pw.pw_dir) } |
| 161 | } |
| 162 | |
| 163 | // invoker_user is who asked for this run: the user the escalation passed |
| 164 | // on, or, in a run that never escalated (a --print preview), the user |
| 165 | // running it |
| 166 | fn invoker_user() !string { |
| 167 | if os.geteuid() != 0 { |
| 168 | // the same answer reexec_as_root passes on: a user that cannot be |
| 169 | // told is an error, not an empty user to build under |
| 170 | return current_user() |
| 171 | } |
| 172 | return invoker_of(os.getenv(env_invoker)) |
| 173 | } |
| 174 | |