sudo.nim

Search:
Group by:

Detect if you are running as root, restart self with sudo if needed or setup uid zero when running with the SUID flag set.

Requirements

  • The sudo program is required to be installed and setup correctly on the target system.

Thanks

β™₯ Idea from https://gitlab.com/dns2utf8/sudo.rs

Types

RunningAs = enum
  raRoot, raUser, raSuid

Procs

proc sudo(filter: (string, string) -> bool): R {.
    ...raises: [OSError, Exception, IOError, ValueError], tags: [ReadDirEffect,
    ReadEnvEffect, ReadIOEffect, RootEffect, ExecIOEffect], forbids: [].}

Escalate program priviledge using sudo as needed. Explicitly exports environment variables for filter(key, value) == true.

Parameters

  • filter: proc (key, value: string): bool When returns true, the key-value pair environment variable in concern will be propagated.
  • prefixes: openArray[string] = [] All envionment variables pairs, with the name of the key that start with any strings in prefixes, will be propagated.

Return

  • if priviledge escalation is unnecessary for the current process: results.Result[RunningAs, string]
  • if priviledge escalation is performed: does not return and quit() with exit code of sudo

Examples

import sudo, sugar
discard sudo()
# or…
discard sudo(["QT_"])  # exports envvars that start with QT_
discard sudo((k, _) => k.startsWith("QT_"))  # equivalent to above

proc sudo(prefixes: openArray[string] = []): R {.
    ...raises: [OSError, Exception, IOError, ValueError], tags: [ReadDirEffect,
    ReadEnvEffect, ReadIOEffect, RootEffect, ExecIOEffect], forbids: [].}
See documentations for sudo(filter).
proc sudo_check(): RunningAs {....raises: [], tags: [], forbids: [].}
Check posix.getuid() and posix.geteuid() to learn about the process configuration.