itektas
asked on
Checking if user is root or normal user for linux
hi
I am building an application which i only want the root users to be able to use, so that if a normal user attempts to run the program they are not allowed in. I am guessing that I have to use If/else to check if the user is root, so that if they arent i can display a message something like Showmessage('You must be under root to run this application'); . Is this possible? thanks in advance
I am building an application which i only want the root users to be able to use, so that if a normal user attempts to run the program they are not allowed in. I am guessing that I have to use If/else to check if the user is root, so that if they arent i can display a message something like Showmessage('You must be under root to run this application'); . Is this possible? thanks in advance
Begin
If IsAdministrator = False Then
Begin
showmessage('error');
exit;
end;
End;
function IsAdministrator: Boolean;
var
psidAdmin: Pointer;
Token: THandle;
Count: DWORD;
TokenInfo: PTokenGroups;
HaveToken: Boolean;
I: Integer;
begin
Result := False;
psidAdmin := nil;
TokenInfo := nil;
HaveToken := False;
try
HaveToken := OpenThreadToken(GetCurrent Thread, TOKEN_QUERY, True, Token);
if (not HaveToken) and (GetLastError = ERROR_NO_TOKEN) then
HaveToken := OpenProcessToken(GetCurren tProcess, TOKEN_QUERY, Token);
if HaveToken then
begin
Win32Check(AllocateAndInit ializeSid( SECURITY_N T_AUTHORIT Y, 2,
SECURITY_BUILTIN_DOMAIN_RI D, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0,
psidAdmin));
if GetTokenInformation(Token, TokenGroups, nil, 0, Count) or
(GetLastError <> ERROR_INSUFFICIENT_BUFFER) then
RaiseLastOSError;
TokenInfo := PTokenGroups(AllocMem(Coun t));
Win32Check(GetTokenInforma tion(Token , TokenGroups, TokenInfo, Count, Count));
for I := 0 to TokenInfo^.GroupCount - 1 do
begin
{$RANGECHECKS OFF} // Groups is an array [0..0] of TSIDAndAttributes, ignore ERangeError
Result := EqualSid(psidAdmin, TokenInfo^.Groups[I].Sid);
{$IFDEF RANGECHECKS_ON}
{$RANGECHECKS ON}
{$ENDIF RANGECHECKS_ON}
if Result then
Break;
end;
end;
finally
if TokenInfo <> nil then
FreeMem(TokenInfo);
if HaveToken then
CloseHandle(Token);
if psidAdmin <> nil then
FreeSid(psidAdmin);
end;
end;
If IsAdministrator = False Then
Begin
showmessage('error');
exit;
end;
End;
function IsAdministrator: Boolean;
var
psidAdmin: Pointer;
Token: THandle;
Count: DWORD;
TokenInfo: PTokenGroups;
HaveToken: Boolean;
I: Integer;
begin
Result := False;
psidAdmin := nil;
TokenInfo := nil;
HaveToken := False;
try
HaveToken := OpenThreadToken(GetCurrent
if (not HaveToken) and (GetLastError = ERROR_NO_TOKEN) then
HaveToken := OpenProcessToken(GetCurren
if HaveToken then
begin
Win32Check(AllocateAndInit
SECURITY_BUILTIN_DOMAIN_RI
psidAdmin));
if GetTokenInformation(Token,
(GetLastError <> ERROR_INSUFFICIENT_BUFFER)
RaiseLastOSError;
TokenInfo := PTokenGroups(AllocMem(Coun
Win32Check(GetTokenInforma
for I := 0 to TokenInfo^.GroupCount - 1 do
begin
{$RANGECHECKS OFF} // Groups is an array [0..0] of TSIDAndAttributes, ignore ERangeError
Result := EqualSid(psidAdmin, TokenInfo^.Groups[I].Sid);
{$IFDEF RANGECHECKS_ON}
{$RANGECHECKS ON}
{$ENDIF RANGECHECKS_ON}
if Result then
Break;
end;
end;
finally
if TokenInfo <> nil then
FreeMem(TokenInfo);
if HaveToken then
CloseHandle(Token);
if psidAdmin <> nil then
FreeSid(psidAdmin);
end;
end;
thegetta stole the code from JEDI Code Library !!!!
ASKER
I can not find it on the website actually, when i go to the link "jedi code library" i can not see any links that talk about jcl security?! and isnt that code for windows? i can see lines in it where it says " win32"
ASKER
i have briefly tried it and it doesnt seem to work, i am using delphi under linux not windows?
ASKER
ok the code that you guys are talking about i have found the same on the borland developer support site;
http://community.borland.com/article/0,1410,26752,00.html
but it is only for windows! when i try to use it with delphi in linux i get loads of compiler errors.
:(
http://community.borland.com/article/0,1410,26752,00.html
but it is only for windows! when i try to use it with delphi in linux i get loads of compiler errors.
:(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Simply use the function (IsAdministrator)
function IsAdministrator: Boolean;
You can find this function at JclSecurity unit from Project JEDI Code Library (JCL).
JCL library can be found at:
http://projectjedi.sourceforge.net/
This library is free and come with full source code!!
Monir.