Skip to content

Commit 8e43dfd

Browse files
committed
Replace more variables on privileges drop
1 parent 03c0242 commit 8e43dfd

File tree

3 files changed

+49
-32
lines changed

3 files changed

+49
-32
lines changed

src/feature/syncinstall.nim

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,12 @@ proc editLoop(config: Config, base: string, repoPath: string, gitPath: Option[st
252252
('a', tr"abort operation"))
253253
editFileLoop(file)
254254
elif res == 'y':
255-
let visualEnv = getenv("VISUAL")
256-
let editorEnv = getenv("EDITOR")
257-
let editor = if visualEnv != nil and visualEnv.len > 0:
258-
$visualEnv
259-
elif editorEnv != nil and editorEnv.len > 0:
260-
$editorEnv
255+
let visualEnv = getEnv("VISUAL")
256+
let editorEnv = getEnv("EDITOR")
257+
let editor = if visualEnv.len > 0:
258+
visualEnv
259+
elif editorEnv.len > 0:
260+
editorEnv
261261
else:
262262
printColonUserInput(config.color, tr"Enter editor executable name" & ":",
263263
noconfirm, "", "")
@@ -304,11 +304,11 @@ proc buildLoop(config: Config, pkgInfos: seq[PackageInfo], noconfirm: bool,
304304
let gitPath = pkgInfos[0].gitPath
305305
let buildPath = buildPath(repoPath, gitPath)
306306

307-
let confFileEnv = getenv("MAKEPKG_CONF")
308-
let confFile = if confFileEnv == nil or confFileEnv.len == 0:
307+
let confFileEnv = getEnv("MAKEPKG_CONF")
308+
let confFile = if confFileEnv.len == 0:
309309
sysConfDir & "/makepkg.conf"
310310
else:
311-
$confFileEnv
311+
confFileEnv
312312

313313
let workConfFile = config.tmpRoot & "/makepkg.conf"
314314

@@ -334,20 +334,20 @@ proc buildLoop(config: Config, pkgInfos: seq[PackageInfo], noconfirm: bool,
334334
printError(config.color, tr"failed to copy config file '$#'" % [confFile])
335335
(none(BuildResult), 1)
336336
else:
337-
let envExt = getenv("PKGEXT")
338-
let confExt = if envExt == nil or envExt.len == 0:
337+
let envExt = getEnv("PKGEXT")
338+
let confExt = if envExt.len == 0:
339339
forkWaitRedirect(() => (block:
340340
dropPrivileges()
341341
execResult(bashCmd, "-c",
342342
"source \"$@\" && echo \"$PKGEXT\"",
343343
"bash", workConfFile)))
344344
.output.optFirst.get("")
345345
else:
346-
$envExt
346+
envExt
347347

348348
let buildCode = forkWait(proc: int =
349349
if chdir(buildPath) == 0:
350-
discard unsetenv("MAKEPKG_CONF")
350+
discard cunsetenv("MAKEPKG_CONF")
351351
dropPrivileges()
352352

353353
if not noextract:

src/main.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ proc handleHelp(operation: OperationType) =
184184
discard
185185

186186
const
187-
version = $getenv("PROG_VERSION")
188-
copyright = $getenv("PROG_COPYRIGHT")
187+
version = getEnv("PROG_VERSION")
188+
copyright = getEnv("PROG_COPYRIGHT")
189189

190190
proc handleVersion(): int =
191191
echo()

src/utils.nim

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,19 @@ type
1818
shell: string
1919
]
2020

21+
proc cgetenv*(name: cstring): cstring
22+
{.importc: "getenv", header: "<stdlib.h>".}
23+
24+
proc csetenv*(name: cstring, value: cstring, override: cint): cint
25+
{.importc: "setenv", header: "<stdlib.h>".}
26+
27+
proc cunsetenv*(name: cstring): cint
28+
{.importc: "unsetenv", header: "<stdlib.h>".}
29+
2130
const
22-
pkgLibDir* = getenv("PROG_PKGLIBDIR")
23-
localStateDir* = getenv("PROG_LOCALSTATEDIR")
24-
sysConfDir* = getenv("PROG_SYSCONFDIR")
31+
pkgLibDir* = getEnv("PROG_PKGLIBDIR")
32+
localStateDir* = getEnv("PROG_LOCALSTATEDIR")
33+
sysConfDir* = getEnv("PROG_SYSCONFDIR")
2534

2635
bashCmd* = "/bin/bash"
2736
suCmd* = "/usr/bin/su"
@@ -199,12 +208,6 @@ proc forkWaitRedirect*(call: () -> int): tuple[output: seq[string], code: int] =
199208

200209
(lines, code)
201210

202-
proc setenv*(name: cstring, value: cstring, override: cint): cint
203-
{.importc, header: "<stdlib.h>".}
204-
205-
proc unsetenv*(name: cstring): cint
206-
{.importc, header: "<stdlib.h>".}
207-
208211
proc getgrouplist*(user: cstring, group: Gid, groups: ptr cint, ngroups: var cint): cint
209212
{.importc, header: "<grp.h>".}
210213

@@ -232,13 +235,13 @@ proc getUser(uid: int): User =
232235
let currentUser* = getUser(getuid().int)
233236

234237
let initialUser* = try:
235-
let sudoUid = getenv("SUDO_UID")
236-
let polkitUid = getenv("PKEXEC_UID")
238+
let sudoUid = getEnv("SUDO_UID")
239+
let polkitUid = getEnv("PKEXEC_UID")
237240

238-
let uidString = if sudoUid != nil and sudoUid.len > 0:
239-
some($sudoUid)
240-
elif polkitUid != nil and polkitUid.len > 0:
241-
some($polkitUid)
241+
let uidString = if sudoUid.len > 0:
242+
some(sudoUid)
243+
elif polkitUid.len > 0:
244+
some(polkitUid)
242245
else:
243246
none(string)
244247

@@ -257,8 +260,22 @@ proc dropPrivileges*() =
257260
discard setgroups(user.groups.len, addr(groups[0]));
258261
discard setgid((Gid) user.gid)
259262
discard setuid((Uid) user.uid)
260-
discard setenv("HOME", user.home, 1)
261-
discard setenv("SHELL", user.shell, 1)
263+
264+
template replaceExisting(name: string, value: string) =
265+
if cgetenv(name) != nil:
266+
discard csetenv(name, value, 1)
267+
268+
replaceExisting("USER", user.name)
269+
replaceExisting("USERNAME", user.name)
270+
replaceExisting("LOGNAME", user.name)
271+
replaceExisting("HOME", user.home)
272+
replaceExisting("SHELL", user.shell)
273+
274+
discard cunsetenv("SUDO_COMMAND")
275+
discard cunsetenv("SUDO_USER")
276+
discard cunsetenv("SUDO_UID")
277+
discard cunsetenv("SUDO_GID")
278+
discard cunsetenv("PKEXEC_UID")
262279

263280
proc toString*[T](arr: array[T, char], length: Option[int]): string =
264281
var workLength = length.get(T.high + 1)

0 commit comments

Comments
 (0)