0

When providing code to another, it appears to be good practice to remove all elevation-specific code — that is, provide apt update rather than sudo apt update (or su -c 'apt update'). However, this causes all kinds of problems for those new to UNIX-based permission elevation — I and many others I have met used to merely run everything as the superuser using the aforementioned utilities.

Additionally, when invoking certain commands programmatically, superuser elevation shall be necessary on some systems, but not on others. This means that including the permission elevator can break configurations on some systems, because things shall begin to be owned by the superuser when they shouldn't be. flatpak is an example of some of this (except the configuration corruption) — 50% of the installations I've used necessitated elevation to invoke most of its commands, the other 50% didn't, by default.

Considering that UAC elevation on Windows 7+ appears to work incredibly well even in edge cases in my experience with it, I am seriously surprised that no comprehensive methods of remediating this issue appear to exist.

Consequently, I ask whether anyone knows of any methods of programmatically determining whether a command necessitates user elevation to invoke it. I am aware that switching users when invoking certain commands can cause its behaviour to affect that user, but in those instances, I cannot imagine that the users able to invoke the command would be restricted (lest it be rendered useless).


https://unix.stackexchange.com/search?q=determine+whether+superuser+elevation+required returns nothing, and although how to determine through code if command needs root elevation? and How to check if the "sudo" permission will be necessary to run a command? appear as if they're asking what I want know, the few responses available at both appear to be focussed upon shell script code to sidestep the issue rather than providing language-agnostic solutions.

  • What do you mean by "language-agnostic solution"? The apt command you use as an example is executed via the shell, so "shell script code" seems appropriate to me. – Kusalananda Mar 12 '24 at 14:32
  • 1
    Is checking a program's exit status ($?) too wonky? For example, apt update returns 100 when executed with insufficient permission, whereas apt and other programs return 0 on success. – Christopher Mar 12 '24 at 14:38
  • @Kusalananda, "shell script code" doesn't merely refer to binaries executed via a shell. .sh code follows the POSIX specification. For instance, try using /usr/bin/env -S sh or bash in POSIX compatibility mode — not all of its features way work in your bash files. I mentioned this due to the usage of comparator operators in the aforementioned answers. – RokeJulianLockhart Mar 12 '24 at 14:44
  • @Christopher, thanks. That's certainly assiative, but I've tried that, and the results are too inconsistent. It's also fundamentally not a confident determiner. – RokeJulianLockhart Mar 12 '24 at 14:44
  • 1
    So "language agnostic" means "POSIX shell"? The -S option to env does not seem to be a POSIX option, is it something that Linux adds? (I'm not ordinarily a Linux user; Busybox env does not understand -S) – Kusalananda Mar 12 '24 at 15:07
  • @Kusalananda Linux and MacOS env each has a -S option, which splits the string specified after -S in the shebang into multiple strings to pass as arguments to env. But... I don't see how /usr/bin/env -S sh is any different than /usr/bin/env sh. You? – Christopher Mar 12 '24 at 15:42
  • 1
    @Christopher Thanks for reminding me about that "feature" (maybe one should say "coreutils and macOS"; there are clearly Linuxes without env -S). No, there is no difference between using env with or without -S in that case, apart from being non-portable with -S. – Kusalananda Mar 12 '24 at 16:10
  • @Kusalananda I knew there was something wrong with "follows the POSIX specification" and "/usr/bin/env -S sh". That's it: -S is not POSIX. Thank you. Yes, coreutils and MacOS is a much better description. – Christopher Mar 12 '24 at 16:51
  • Indeed, I use -S because it means that when I'm using a shebang with multiple space-delimited arguments, I don't forget it. It's also a useful way to ensure that they are solely ever invoked on a recent machine using the GNU core utilities. – RokeJulianLockhart Mar 12 '24 at 20:23

1 Answers1

3

Consequently, I ask whether anyone knows of any methods of programmatically determining whether a command necessitates user elevation to invoke it. I am aware that switching users when invoking certain commands can cause its behaviour to affect that user, but in those instances, I cannot imagine that the users able to invoke the command would be restricted (lest it be rendered useless).

No such way exists; it's "administrator contextual knowledge" that you need to run apt as root, and flatpak not. Other programs will ask for privilege elevation when they need it.

Considering that UAC elevation on Windows 7+ appears to work incredibly well even in edge cases in my experience with it, I am seriously surprised that no comprehensive methods of remediating this issue appear to exist.

UAC elevation is the windows GUI equivalent of sudo or policykit on linux / freedesktop.

You can't tell whether a program will require admin privileges under windows, either, until it requests them.

The windows graphical shell has a way to mark links to executables as "run as different user", though. But that's not the same as saying the executable can be spotted as in need of privilege elevation.

  • Thanks for the answer. Indeed, UAC's counterpart is definitely PolicyKit. I hadn't considered that. I suppose I'll just have to continue requesting that PolicyKit be used by program authors. – RokeJulianLockhart Mar 12 '24 at 14:47
  • 2
    @RokeJulianLockhart AFAIK, PolicyKit only exists on Linux, so it's pretty useless for portable solutions. – Kusalananda Mar 12 '24 at 15:01
  • @Kusalananda, I didn't expect any solution to work cross-platform. If I need that, I'll write a wrapper which determines the OS and then performs the necessary steps. That was why I asked here. – RokeJulianLockhart Mar 12 '24 at 20:21