Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Owner of File

Status
Not open for further replies.

areric

Programmer
Jul 13, 2003
47
US
Hey everyone,

I posted a thread earlier called Opening Files, doesnt matter anymore cause i found the problem. But i have a new question for you all.

Im trying to identify the Owner of a file based on NT Permissions. The code that i have (taken of MSDN) opens the file to do this. I was wondering if anyone has ever done this without opening the file. I figured out how to close the file, which helps emensely but when you open the file it counts as an access and kills the lastaccessdate. I need that to stay in tact if possible.

p.s. my current function is below.

Thanks.

CODE:
int getOwnerName(const char* filename, string &rAcctName){
DWORD dwRtnCode = 0;
PSID pSidOwner;
BOOL bRtnBool = TRUE;
LPTSTR AcctName, DomainName;
DWORD dwAcctName = 1, dwDomainName = 1;
SID_NAME_USE eUse = SidTypeUnknown;
HANDLE hFile;
PSECURITY_DESCRIPTOR pSD;

// Get the handle of the file object.
hFile = CreateFile(
filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);

// Check GetLastError for CreateFile error code.
if (hFile == INVALID_HANDLE_VALUE) {
DWORD dwErrorCode = 0;

dwErrorCode = GetLastError();
//_tprintf(TEXT("CreateFile error = %d-%s\n"), dwErrorCode, filename);
rAcctName="unknown";
return -1;
}

// Allocate memory for the SID structure.
pSidOwner = (PSID)GlobalAlloc(
GMEM_FIXED,
sizeof(PSID));

// Allocate memory for the security descriptor structure.
pSD = (PSECURITY_DESCRIPTOR)GlobalAlloc(
GMEM_FIXED,
sizeof(PSECURITY_DESCRIPTOR));

// Get the owner SID of the file.
dwRtnCode = GetSecurityInfo(
hFile,
SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION,
&pSidOwner,
NULL,
NULL,
NULL,
&pSD);

// Check GetLastError for GetSecurityInfo error condition.
if (dwRtnCode != ERROR_SUCCESS) {
DWORD dwErrorCode = 0;

dwErrorCode = GetLastError();
//_tprintf(TEXT("GetSecurityInfo error = %d\n"), dwErrorCode);
rAcctName="unknown";
return -1;
}

// First call to LookupAccountSid to get the buffer sizes.
bRtnBool = LookupAccountSid(
NULL, // local computer
pSidOwner,
AcctName,
(LPDWORD)&dwAcctName,
DomainName,
(LPDWORD)&dwDomainName,
&eUse);

// Reallocate memory for the buffers.
AcctName = (char *)GlobalAlloc(
GMEM_FIXED,
dwAcctName);

// Check GetLastError for GlobalAlloc error condition.
if (AcctName == NULL) {
DWORD dwErrorCode = 0;

dwErrorCode = GetLastError();
//_tprintf(TEXT("GlobalAlloc error = %d\n"), dwErrorCode);
rAcctName="unknown";
return -1;
}

DomainName = (char *)GlobalAlloc(
GMEM_FIXED,
dwDomainName);

// Check GetLastError for GlobalAlloc error condition.
if (DomainName == NULL) {
DWORD dwErrorCode = 0;

dwErrorCode = GetLastError();
//_tprintf(TEXT("GlobalAlloc error = %d\n"), dwErrorCode);
rAcctName="unknown";
return -1;

}

// Second call to LookupAccountSid to get the account name.
bRtnBool = LookupAccountSid(
NULL, // name of local or remote computer
pSidOwner, // security identifier
AcctName, // account name buffer
(LPDWORD)&dwAcctName, // size of account name buffer
DomainName, // domain name
(LPDWORD)&dwDomainName, // size of domain name buffer
&eUse); // SID type

// Check GetLastError for LookupAccountSid error condition.
if (bRtnBool == FALSE) {
DWORD dwErrorCode = 0;

dwErrorCode = GetLastError();

if (dwErrorCode == ERROR_NONE_MAPPED) {
// _tprintf(TEXT("Account owner not found for specified SID.\n"));
}
else {
// _tprintf(TEXT("Error in LookupAccountSid.\n"));
}
rAcctName = "unknown";
return -1;

}
else if (bRtnBool == TRUE) {
// Print the account name.
//_tprintf(TEXT("Account owner = %s\n"), AcctName);
rAcctName = AcctName;
return 1;
}
CloseHandle(hFile);
}
 
Try this:
Code:
bool GetOwner(const char* fname)
{
  PSID	psid;
  char aname[256];
  DWORD saname = sizeof aname;
  char dname[256];
  DWORD sdname = sizeof dname;
  SID_NAME_USE sidnuse;
  char  sinfo[1024];
  DWORD len = sizeof sinfo;
  BOOL  own = 0;

  if (!GetFileSecurity(fname,OWNER_SECURITY_INFORMATION,
		(PSECURITY_DESCRIPTOR)sinfo,sizeof sinfo,&len))
    return 0;

  if (!GetSecurityDescriptorOwner((PSECURITY_DESCRIPTOR)sinfo,&psid,&own))
    return 0;

  if (!LookupAccountSid(0,psid,aname,&saname,dname,&sdname,&sidnuse))
    return false;

  cout << aname << "@" << dname << endl;

  return true;
}
Warnings:
1. It's an improvisation (see also MSDN etc).
2. Modify return value as you wish.
3. Your program must have proper permissions to get owner info...
4. Win NT++ only, of course...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top