Run as a different user lazarus / 21-General ---------------------------------------------------------------------- LemonParty · 2025-11-16 21:18 Hello. Is there a method to run program as a different user? Interested Windows and Linux OS. ---------------------------------------------------------------------- Warfley · 2025-11-16 21:34 On Windows I don't know, on Linux yes ---------------------------------------------------------------------- LemonParty · 2025-11-16 22:51 How do I get TUid if I know user name? By the way, I found the solution for Windows. It is CreateProcessWithLogonW from JwaWinBase. ---------------------------------------------------------------------- H₂SO₄ · 2025-11-17 07:08 Quote from: LemonParty on November 16, 2025, 11:51:37 pmHow do I get TUid if I know user name? You could look at  /etc/passwd  or run  id -u ---------------------------------------------------------------------- MarkMLl · 2025-11-17 08:30 Quote from: Warfley on November 16, 2025, 10:34:50 pmOn Windows I don't know, on Linux yes The problem there is that that requires that the program be started as root, which some widget sets (I forget which) explicitly refuse to accept. A better way would be to bless the binary with the required capabilities, and then relinquish them as soon as they are no longer needed. Unfortunately that can't be automated at least in Lazarus, since the IDE doesn't have a "run as" option for the "Execute after" operation. Thread at https://forum.lazarus.freepascal.org/index.php/topic,72090.m sg563702.html#msg563702 is relevant. MarkMLl ---------------------------------------------------------------------- Thaddy · 2025-11-17 12:05 Quote from: MarkMLl on November 17, 2025, 09:30:49 amThe problem there is that that requires that the program be started as root, which some widget sets (I forget which) explicitly refuse to accept. No, that is not correct. like su/sudo you can use the underlying for a different user with different credentials and group. That not necessarily means root, can be any other user. But that was an oversight? man su. ---------------------------------------------------------------------- MarkMLl · 2025-11-17 15:45 Quote from: Thaddy on November 17, 2025, 01:05:39 pmQuote from: MarkMLl on November 17, 2025, 09:30:49 amThe problem there is that that requires that the program be started as root, which some widget sets (I forget which) explicitly refuse to accept. No, that is not correct. like su/sudo you can use the underlying for a different user with different credentials and group. That not necessarily means root, can be any other user. But that was an oversight? man su. Thaddy, what part of "fpSetUID sets the user ID of the current process. This call will only work if it is executed as root, or the program is setuid root." do you find ambiguous? MarkMLl ---------------------------------------------------------------------- LemonParty · 2025-11-17 16:43 Quote from: Warfley on November 16, 2025, 10:34:50 pmOn Windows I don't know, on Linux yes How exactly should I use this function? Should I call it before runing TProcess? ---------------------------------------------------------------------- rdxdt · 2025-11-21 09:36 Quote from: LemonParty on November 17, 2025, 05:43:26 pmQuote from: Warfley on November 16, 2025, 10:34:50 pmOn Windows I don't know, on Linux yes How exactly should I use this function? Should I call it before runing TProcess? If your Linux based system has pkexec i've wrote a simple example, it's not elegant but depending on your needs it can work, please include the units BaseUnix and Process and then Code: [Select]if FpGetUID() <> 0 then   begin     RunCommand('pkexec',[ParamStr(0)],Dummy);     Terminate;   end   else   begin     // Insert your code that must run as root   end; ---------------------------------------------------------------------- LemonParty · 2025-11-21 14:24 I wrote this code that uses sudo: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function RunAsUser(User, Executable: String; Parameters: TStringDynArray): Boolean;var  P: TProcess;  i: SizeInt;begin  Result:= False;  P:= TProcess.Create(nil);  try    P.Executable:= 'sudo';    P.Parameters.Append('-H');    P.Parameters.Append('-u');    P.Parameters.Append(User);    P.Parameters.Append(Executable);    for i:= 0 to High(Parameters) do      P.Parameters.Append(Parameters[i]);    P.Execute;    Result:= True;  finally    P.Free;  end;end; ---------------------------------------------------------------------- MarkMLl · 2025-11-22 18:21 Quote from: LemonParty on November 21, 2025, 03:24:28 pmI wrote this code that uses sudo: As I've said, you will get into trouble if trying to invoke arbitrary GUI-oriented programs because some widget sets explicitly refuse to run as root (rationale: far too much code and far too difficult logic paths which make the maintainers worried about security). /What/ are you trying to do? In a lot of cases "the right way" is simply to make sure that you're a member of a specific group, or failing that to write a udev rule. MarkMLl ---------------------------------------------------------------------- LemonParty · 2025-11-23 14:15 I just wrote a function for Windows that runs program as a different user because when I run program under administrator rights it is not working good. Then I thought it would be good to have a crossplatform version of this function. ---------------------------------------------------------------------- MarkMLl · 2025-11-24 08:27 Quote from: LemonParty on November 23, 2025, 03:15:29 pmI just wrote a function for Windows that runs program as a different user because when I run program under administrator rights it is not working good. Then I thought it would be good to have a crossplatform version of this function. On Linux with reference to the patch etc. described in the link I posted earlier, the contributed file runexecuteafterasuser.pas contains this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i (* The user has requested that the command passed as the parameter be run as a  different (usually more-privileged) user. Construct an appropriate prefix.*)function Prefix(const command, asUser: string): string; platform; experimental; begin  if (asUser = '') or (asUser = 'root') then    result := 'sudo --askpass --reset-timestamp ' + command  else    result := 'sudo --askpass --reset-timestamp --user=' + asUser + ' ' + commandend { Prefix } ;  I did it like that after a lot of fiddling around because one can almost always rely on sudo being installed, and the --askpass option is generally set up to call into whatever graphical environment is running to bring up a dialog(ue), although this might require a line in /etc/sudo.conf or that the SUDO_ASKPASS shell variable has to be set somewhere. However as I've said: making sure that the user is a member of the appropriate group, and/or that there are suitable udev rules in place, is almost always to be preferred. If that doesn't work, for example if a program is to listen on a low-numbered port, then use an appropriate POSIX capability. There's problems there, which I'm sure I've touched on before (i.e. cited thread): in order to set a capability, you either need to be root or the capability-setting program needs to be blessed with the "this can set capabilities" capability. However if you bless something like an IDE with "this can set capabilities", then it can subsequently bless any program it generates with "this can set capabilities": if the POSIX people had really thought things through then there'd be a special "this can set any capability except for the 'this can set capabilities' capability". MarkMLl ---------------------------------------------------------------------- AlexTP · 2025-11-24 16:02 Is this what you need? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function RunElevated(const AProgram, AParameters: UnicodeString; AHideWindow: boolean): boolean; ---------------------------------------------------------------------- LemonParty · 2025-11-24 17:27 Not exactly. I need to run from a different user, not Administrator. CreateProcessWithLogonW works fine with this. I guess for now this topic may be closed. ---------------------------------------------------------------------- d2010 · 2025-12-21 05:30 Here you see, same code ofn Registry very nice. Line26  purpose more interesting facts. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i [HKEY_CLASS ES_ROOT\exefile\shell\open]"EditFlags"=hex:00,00,00,00 [HKEY_CLASSES_R OOT\exefile\shell\open\command]@="\"%1\" %*""IsolatedCommand"="\"%1\" %*" [HKEY_CLASSES_ROOT\exefile\shell\runas]"HasLUAShield"="" [HKEY_CLA SSES_ROOT\exefile\shell\runas\command]@="\"%1\" %*""IsolatedCommand"="\"%1\" %*" [HKEY_CLASSES_ROOT\exefile\shell\runa suser]@="@shell32.dll,- 50944""Extended"="""SuppressionPolicyEx"="{F211AA05-D4DF-4370-A2A0- 9F19C09756A7}" [HKEY_CLASSES_ROOT\exefile\shell\runasuser\command]"Del egateExecute"="{ea72d00e-4960-42fa-ba92-7792a7944c1d}" Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{ea72d00e-4960-42fa- ba92-7792a7944c1d}]@="Run as different user context menu handler" [HKE Y_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{ea72d00e-4960-42fa-ba92- 7792a7944c1d}\InProcServer32]@=hex(2):25,00,53,00,79,00,73,00,74,00,65 ,00,6d,00,52,00,6f,00,6f,00,74,00,25,\  00,5c,00,53,00,79,00,73,00,74, 00,65,00,6d,00,33,00,32,00,5c,00,65,00,78,00,\  70,00,6c,00,6f,00,72,0 0,65,00,72,00,66,00,72,00,61,00,6d,00,65,00,2e,00,64,\  00,6c,00,6c,00,00,00"ThreadingModel"="Apartment" ---------------------------------------------------------------------- LemonParty · 2025-12-21 13:32 d2010, that is nice. But the idea was to run something as a different user from code where you can directly specify username, password. And CreateProcessWithLogonW do the trick on Windows. ---------------------------------------------------------------------- Raskaton · 2025-12-23 18:18 I think this is an architectural issue. It's better to split the application into two parts: 1. A service/daemon running under a specific user/group 2. A GUI that gives any user access to manage the service via IPC or edit its configuration files. Changing permissions at runtime contradicts the very idea of ​​permissions in the OS. This is a bad idea. OS administrators will hate you. If you need access to a serial port or USB in Linux, then "udev rules" are simply added (TAG+="uaccess"). Or service added to stanard "dialout" user group. ---------------------------------------------------------------------- myisjwj · 2025-12-26 02:11 You can refer to the following website to obtain the user name https://www.cnblogs.com/jwjss/p/17914594.html