Recent

Author Topic: [Solved]Is it possible for a console program, to detect if being run as 'su'?!?  (Read 1198 times)

cdbc

  • Hero Member
  • *****
  • Posts: 1513
    • http://www.cdbc.dk
Hi
Is there any way, a console program can detect if it's being run as 'superuser'?
If so, how?
This question is mainly about Linux, but could be interesting to about winders too...
Regards Benny
« Last Edit: August 21, 2024, 02:15:10 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Zvoni

  • Hero Member
  • *****
  • Posts: 2690
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

cdbc

  • Hero Member
  • *****
  • Posts: 1513
    • http://www.cdbc.dk
Re: Is it possible for a console program, to detect if being run as 'su'?!?
« Reply #2 on: August 21, 2024, 01:50:01 pm »
Hi
Thanks @Zvoni  8)
...and the answer is:
Code: Pascal  [Select][+][-]
  1. uses ..., baseunix;
  2.   ...
  3.   IsRoot:= (FpGetuid = 0);
  4.   ...
  5.  
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0


Thaddy

  • Hero Member
  • *****
  • Posts: 15639
  • Censorship about opinions does not belong here.
Windows is a bit more cumbersome:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses windows;
  3.  
  4. function IsRunningAsAdmin: Boolean;
  5. var
  6.   hToken: THandle = 0;
  7.   pElevation: TOKEN_ELEVATION;
  8.   dwSize: DWORD = SizeOf(TOKEN_ELEVATION);
  9. begin
  10.   Result := False;
  11.   if OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hToken) then
  12.     if GetTokenInformation(hToken, TokenElevation, @pElevation, dwSize, dwSize) then
  13.     begin  
  14.       Result := pElevation.TokenIsElevated <> 0;
  15.       CloseHandle(hToken);
  16.     end;
  17. end;
  18. begin
  19.   writeln(isrunningasadmin);
  20. end.
« Last Edit: September 14, 2024, 07:14:07 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11758
  • FPC developer.
Windows is a bit more cumbersome:
Code: Pascal  [Select][+][-]
  1. [/quote]
  2.  
  3. No it is not.
  4.  
  5. [code=pascal]
  6. uses winutils;
  7.  
  8. begin
  9.   result:=IsWindowsAdmin();
  10. end;
  11.  

Thaddy

  • Hero Member
  • *****
  • Posts: 15639
  • Censorship about opinions does not belong here.
Maybe, but my code is much more concise than the convoluted code in winutils.
Has also less dependencies.
It is also a straight port from C on msdn.
« Last Edit: September 14, 2024, 11:34:18 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Kays

  • Hero Member
  • *****
  • Posts: 604
  • Whasup!?
    • KaiBurghardt.de
Re: Is it possible for a console program, to detect if being run as 'su'?!?
« Reply #7 on: September 14, 2024, 11:38:41 am »
You should not “detect” whether your console program is su’d, but simply access the resources that require elevated privileges. Specifically Linux has capabilities(7) that allow non‑root users to perform certain administrative tasks, so testing for a zero UID may give false negatives. Or it just may happen that the file permissions mode is very liberal, so being root isn’t required at all.
Yours Sincerely
Kai Burghardt

 

TinyPortal © 2005-2018