Solved

Checking if user is root or normal user for linux

Posted on 2003-11-28
7
194 Views
Last Modified: 2010-04-05
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

0
Comment
Question by:itektas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:monir
ID: 9843026

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.
0
 

Expert Comment

by:thegetta
ID: 9843638
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(GetCurrentThread, TOKEN_QUERY, True, Token);
    if (not HaveToken) and (GetLastError = ERROR_NO_TOKEN) then
      HaveToken := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, Token);
    if HaveToken then
    begin
      Win32Check(AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2,
        SECURITY_BUILTIN_DOMAIN_RID, 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(Count));
      Win32Check(GetTokenInformation(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;
0
 
LVL 2

Expert Comment

by:monir
ID: 9844767


 thegetta stole the code from JEDI Code Library !!!!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:itektas
ID: 9845335
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"
0
 

Author Comment

by:itektas
ID: 9845339
i have briefly tried it and it doesnt seem to work, i am using delphi under linux not windows?
0
 

Author Comment

by:itektas
ID: 9845519
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.

:(
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 9883862
Question closed and points refunded

PashaMod
Cs Moderator
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Making delphi communicate with a c# service 16 123
Dev express lookupcombo 3 45
firemonkey Android Listview Sort items 7 73
delphi popmenu non latine charcters 3 34
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question