PSID vncImportACL::GetSID(const TCHAR *domainaccount){ PSID pSid = NULL; unsigned long ulDomLen = 0; unsigned long ulSidLen = 0; SID_NAME_USE peUse; TCHAR *domain = NULL; // Get accounts's SID // First call to LookupAccountName is expected to fail. // Sets pSid and domain size LookupAccountName(NULL, domainaccount, pSid, &ulSidLen, domain, &ulDomLen, &peUse); pSid = (PSID)new TCHAR[ulSidLen]; domain = new TCHAR[ulDomLen]; LookupAccountName(NULL, domainaccount, pSid, &ulSidLen, domain, &ulDomLen, &peUse); if (!IsValidSid(pSid)){ _ftprintf(stderr, _T("%s: SID not valid.\n"), domainaccount); delete [] pSid; pSid = NULL; } delete [] domain; return pSid; }
/*----------------------------------------------------------------------------- Kill any Dr. Watson windows that are open (we already killed the browser process) -----------------------------------------------------------------------------*/ void CurlBlastDlg::KillProcs(void) { #ifndef _DEBUG WTS_PROCESS_INFO * proc = NULL; DWORD count = 0; if( WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, &proc, &count) ) { for( DWORD i = 0; i < count; i++ ) { bool terminate = false; // check for Dr. Watson if( !lstrcmpi(PathFindFileName(proc[i].pProcessName), _T("dwwin.exe")) ) { if( !bDrWatson ) { log.LogEvent(event_KilledDrWatson); bDrWatson = true; } terminate = true; } else if(lstrcmpi(PathFindFileName(proc[i].pProcessName), _T("iexplore.exe"))) { if (worker) { EnterCriticalSection( &(worker->cs) ); // make sure it's not the browser we launched if( proc[i].ProcessId != worker->browserPID && worker->userSID && proc[i].pUserSid && IsValidSid(worker->userSID) && IsValidSid(proc[i].pUserSid) ) { // see if the SID matches if( EqualSid(proc[i].pUserSid, worker->userSID ) ) terminate = true; } LeaveCriticalSection( &(worker->cs) ); } } if( terminate ) { HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, proc[i].ProcessId); if( hProc ) { TerminateProcess(hProc, 0); CloseHandle(hProc); } } } WTSFreeMemory(proc); } #endif }
APR_DECLARE(apr_status_t) apr_uid_compare(apr_uid_t left, apr_uid_t right) { if (!left || !right) return APR_EINVAL; #ifndef _WIN32_WCE if (!IsValidSid(left) || !IsValidSid(right)) return APR_EINVAL; if (!EqualSid(left, right)) return APR_EMISMATCH; #endif return APR_SUCCESS; }
BOOL ValidateSecurity(uch *securitydata) { PSECURITY_DESCRIPTOR sd = (PSECURITY_DESCRIPTOR)securitydata; PACL pAcl; PSID pSid; BOOL bAclPresent; BOOL bDefaulted; if(!IsWinNT()) return TRUE; /* don't do anything if not on WinNT */ if(!IsValidSecurityDescriptor(sd)) return FALSE; /* verify Dacl integrity */ if(!GetSecurityDescriptorDacl(sd, &bAclPresent, &pAcl, &bDefaulted)) return FALSE; if(bAclPresent && pAcl!=NULL) { if(!IsValidAcl(pAcl)) return FALSE; } /* verify Sacl integrity */ if(!GetSecurityDescriptorSacl(sd, &bAclPresent, &pAcl, &bDefaulted)) return FALSE; if(bAclPresent && pAcl!=NULL) { if(!IsValidAcl(pAcl)) return FALSE; } /* verify owner integrity */ if(!GetSecurityDescriptorOwner(sd, &pSid, &bDefaulted)) return FALSE; if(pSid != NULL) { if(!IsValidSid(pSid)) return FALSE; } /* verify group integrity */ if(!GetSecurityDescriptorGroup(sd, &pSid, &bDefaulted)) return FALSE; if(pSid != NULL) { if(!IsValidSid(pSid)) return FALSE; } return TRUE; }
QByteArray WindowsPlatform::getLocalStorageKey() { HANDLE hToken = NULL; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) { return QByteArray(); } DWORD dwBufferSize = 0; GetTokenInformation(hToken, TokenUser, NULL, 0, &dwBufferSize); if (!dwBufferSize) { CloseHandle(hToken); return QByteArray(); } PTOKEN_USER userToken = (PTOKEN_USER)new char[dwBufferSize]; if (!GetTokenInformation(hToken, TokenUser, userToken, dwBufferSize, &dwBufferSize) || !IsValidSid(userToken->User.Sid)) { CloseHandle(hToken); delete userToken; return QByteArray(); } DWORD dwLength = GetLengthSid(userToken->User.Sid); QByteArray result((char *)userToken->User.Sid, dwLength); CloseHandle(hToken); delete userToken; return result; }
BOOL make_relative_sid(PSID* answer, PSID base, ULONG relative_id) { int count; int i; if (answer == NULL) return print_error(L"Error in make_relative_sid: answer is NULL.\n"); if (base == NULL) return print_error(L"Error in make_relative_sid: base is NULL.\n"); if (!IsValidSid(base)) return print_error(L"Error in make_relative_sid: base is not a valid SID.\n"); count = *GetSidSubAuthorityCount(base); if (count > 7) return print_error(L"Error in make_relative_sid: base has too many sub-authorities.\n"); if (!AllocateAndInitializeSid( GetSidIdentifierAuthority(base) , 1 + count , 0, 0, 0, 0, 0, 0, 0, 0, answer)) return win_error(GetLastError(), L"AllocateAndInitializeSid"); for(i=0; i<count; i++) { *GetSidSubAuthority(*answer, i) = *GetSidSubAuthority(base, i); } *GetSidSubAuthority(*answer, count) = relative_id; return TRUE; }
uid_t getuid() { const int UNLEN = 256; PSID sid = NULL; TCHAR* domain = NULL; DWORD sid_size = 0; DWORD domain_size = 0; SID_NAME_USE use = SidTypeUnknown; TCHAR buffer[257]; DWORD buffer_len = _countof(buffer); if (!GetUserName(buffer, &buffer_len)) { return 0; } //Called once to set the size of the sid pointer and domain LookupAccountName(NULL, buffer, NULL, &sid_size, NULL, &domain_size, &use); //Allocate memory based on sid and domain size sid = (PSID)LocalAlloc(LMEM_FIXED, sid_size); domain = (TCHAR*)malloc(domain_size * sizeof(TCHAR)); //Initialize sid and domain if (!LookupAccountName(NULL, buffer, sid, &sid_size, domain, &domain_size, &use)) { return 0; } if (!IsValidSid(sid)) { return 0; } return (uid_t)sid; }
bool GetFileOwner(const string& Computer,const string& Name, string &strOwner) { bool Result=false; strOwner.clear(); SECURITY_INFORMATION si=OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION;; DWORD LengthNeeded=0; NTPath strName(Name); static char sddata[64 * 1024]; PSECURITY_DESCRIPTOR sd = reinterpret_cast<PSECURITY_DESCRIPTOR>(sddata); if (GetFileSecurity(strName.data(),si,sd,sizeof(sddata),&LengthNeeded) && LengthNeeded<=sizeof(sddata)) { PSID pOwner; BOOL OwnerDefaulted; if (GetSecurityDescriptorOwner(sd,&pOwner,&OwnerDefaulted)) { if (IsValidSid(pOwner)) { Result = SidToNameCached(pOwner, strOwner, Computer); } } } return Result; }
static int iwin32_file_get_group (const char *file, group_id_t *gid) { char file_sd_buf [256]; DWORD sd_size = 256; PSECURITY_DESCRIPTOR file_sd = (PSECURITY_DESCRIPTOR) &file_sd_buf; PSID sid; BOOL dummy; assert (file != NULL); assert (gid != NULL); if (!InitializeSecurityDescriptor (file_sd, SECURITY_DESCRIPTOR_REVISION)) return 0; if (!GetFileSecurity (file, (SECURITY_INFORMATION)(GROUP_SECURITY_INFORMATION), file_sd, sizeof (file_sd_buf), &sd_size)) return 0; if (!GetSecurityDescriptorGroup (file_sd, &sid, &dummy)) return 0; if (!IsValidSid (sid)) return 0; gid->value = malloc (sd_size); if (!gid->value) return 0; if (!CopySid (sd_size, gid->value, sid)) return 0; return 1; }
static void print_sid (const char *prefix, int idx, PISID sid) { DWORD i; cyglsa_printf ("%s", prefix); if (idx >= 0) cyglsa_printf ("[%d] ", idx); cyglsa_printf ("(0x%08x) ", (INT_PTR) sid); if (!sid) cyglsa_printf ("NULL\n"); else if (IsBadReadPtr (sid, 8)) cyglsa_printf ("INVALID POINTER\n"); else if (!IsValidSid ((PSID) sid)) cyglsa_printf ("INVALID SID\n"); else if (IsBadReadPtr (sid, 8 + sizeof (DWORD) * sid->SubAuthorityCount)) cyglsa_printf ("INVALID POINTER SPACE\n"); else { cyglsa_printf ("S-%d-%d", sid->Revision, sid->IdentifierAuthority.Value[5]); for (i = 0; i < sid->SubAuthorityCount; ++i) cyglsa_printf ("-%lu", sid->SubAuthority[i]); cyglsa_printf ("\n"); } }
LPTSTR CDialupass::GetLocalSid() { union { SID s; char c[256]; }Sid; DWORD sizeSid=sizeof(Sid); char DomainName[256]; DWORD sizeDomainName=sizeof(DomainName); SID_NAME_USE peUse; LPSTR pSid; if (m_lpCurrentUser == NULL) return NULL; if(!LookupAccountName(NULL,m_lpCurrentUser,(SID*)&Sid,&sizeSid,DomainName,&sizeDomainName,&peUse))return NULL; if(!IsValidSid(&Sid))return NULL; typedef BOOL (WINAPI *ConvertSid2StringSid)(PSID , LPTSTR *); ConvertSid2StringSid proc; HINSTANCE hLibrary = LoadLibrary("advapi32.dll"); proc = (ConvertSid2StringSid) GetProcAddress(hLibrary, "ConvertSidToStringSidA"); if(proc) proc((SID*)&Sid.s,&pSid); FreeLibrary(hLibrary); return pSid; }
/** * @brief * add_privilege - add_privilege: returns 0 if privname has been added for account * referenced by sid; otherwise, 1. * * @param[in] sid - The security identifier (SID) structure is a variable-length structure * used to uniquely identify users or groups. * @param[in] privname - privilege name * * @return int * @retval 1 : if privname has not been added for account referenced by sid * @retval 0 : if privname has been added for account referenced by sid */ int add_privilege(SID *sid, char *privname) { LSA_UNICODE_STRING rights; LSA_HANDLE h_policy = INVALID_HANDLE_VALUE; LSA_OBJECT_ATTRIBUTES obj_attrs; NTSTATUS lsa_stat; BOOL rval = 1; WCHAR *privnameW = NULL; int priv_len = 0; if (privname == NULL) { fprintf(stderr, "add_privilege: NULL privname\n"); return (1); } if (!IsValidSid(sid)) { fprintf(stderr, "add_privilege: Not a valid sid\n"); return (1); } priv_len = strlen(privname) + 1; privnameW = (WCHAR *)malloc(priv_len * sizeof(WCHAR)); if (privnameW == NULL) { fprintf(stderr, "add_privilege: malloc failed\n"); return (1); } mbstowcs(privnameW, privname, priv_len); init_lsa_string(&rights, privnameW); ZeroMemory(&obj_attrs, sizeof(obj_attrs)); if( LsaOpenPolicy(NULL, &obj_attrs, POLICY_ALL_ACCESS, &h_policy) \ != ERROR_SUCCESS ) { fprintf(stderr, "add_privilege: Unable to open policy!\n"); goto add_privilege_end; } if( (lsa_stat=LsaAddAccountRights( h_policy, sid, &rights, 1 )) != \ ERROR_SUCCESS ) { fprintf(stderr, "add_privilege: adding privilege %s failed! - err %d\n", privname, LsaNtStatusToWinError(lsa_stat)); goto add_privilege_end; } printf("\tadded %s\n", privname); rval = 0; add_privilege_end: if (h_policy != INVALID_HANDLE_VALUE) LsaClose(h_policy); if (privnameW != NULL) (void)free(privnameW); return (rval); }
PSID vncImportACL::CopySID(PSID pSID){ PSID pNewSID = NULL; DWORD sidLength = 0; if (IsValidSid(pSID)){ sidLength = GetLengthSid(pSID); pNewSID = (PSID) new char[sidLength]; CopySid(sidLength, pNewSID, pSID); } return pNewSID; }
void GetUserNameEx(wchar_t *computer,PSID sid,bool full,wchar_t **username) { *username=(wchar_t*)L"Account Unknown"; if(IsValidSid(sid)) { get_sid_cache(sid,full,username); if(!(*username)) { add_sid_cache(computer,sid,full,username); } } }
VOID IDMFreeSid( PSID pSid ) { if (!pSid || !IsValidSid(pSid)) { return; } FreeSid(pSid); return; }
/* (in) PSID (in/out) char (in) size_t return 0 - success -1 - error */ int get_textual_sid(PSID pSid,char * buf,size_t len) { /*FROM MSDN Converting a Binary SID to String Format in C++*/ PSID_IDENTIFIER_AUTHORITY psia; DWORD dwSubAuthorities; DWORD dwSidRev = SID_REVISION; DWORD dwCounter; DWORD dwSidSize; if (!IsValidSid(pSid)){ SetLastError(ERROR_INVALID_DATA); return -1; } psia = GetSidIdentifierAuthority(pSid); dwSubAuthorities = *GetSidSubAuthorityCount(pSid); dwSidSize = (15 + 12 + (12 * dwSubAuthorities) +1) * sizeof(char); if (len < dwSidSize){ SetLastError(ERROR_INSUFFICIENT_BUFFER); return -1; } _snprintf(buf,len,"S-%lu-",dwSidRev); if ( (psia->Value[0] != 0) || (psia->Value[1] != 0) ){ _snprintf(buf + strlen(buf),len-(strlen(buf)+1), "0x%02hx%02hx%02hx%02hx%02hx%02hx", (USHORT) psia->Value[0], (USHORT) psia->Value[1], (USHORT) psia->Value[2], (USHORT) psia->Value[3], (USHORT) psia->Value[4], (USHORT) psia->Value[5]); }else { _snprintf(buf + strlen(buf),len - (strlen(buf)+1), "%lu", (USHORT) (psia->Value[5]) + (USHORT) (psia->Value[4] << 8)+ (USHORT) (psia->Value[3] << 16)+ (USHORT) (psia->Value[2] << 24)); } for (dwCounter = 0; dwCounter < dwSubAuthorities; dwCounter++){ _snprintf(buf + strlen(buf),len - (strlen(buf)+1),"-%lu", *GetSidSubAuthority(pSid,dwCounter)); } return 0; }
HRESULT COpcSecurity::SetGroup(PSID pGroupSid, BOOL bDefaulted) { OPCASSERT(m_pSD); // Mark the SD as having no Group if (!SetSecurityDescriptorGroup(m_pSD, NULL, bDefaulted)) { HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); OPCASSERT(FALSE); return hr; } if (m_pGroup) { free(m_pGroup); m_pGroup = NULL; } // If they asked for no Group don't do the copy if (pGroupSid == NULL) return S_OK; // Make a copy of the Sid for the return value DWORD dwSize = GetLengthSid(pGroupSid); m_pGroup = (PSID) malloc(dwSize); if (m_pGroup == NULL) return E_OUTOFMEMORY; if (!CopySid(dwSize, m_pGroup, pGroupSid)) { HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); OPCASSERT(FALSE); free(m_pGroup); m_pGroup = NULL; return hr; } OPCASSERT(IsValidSid(m_pGroup)); if (!SetSecurityDescriptorGroup(m_pSD, m_pGroup, bDefaulted)) { HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); OPCASSERT(FALSE); free(m_pGroup); m_pGroup = NULL; return hr; } return S_OK; }
///////////////////////////////////////////////////////////////////////// // // GetStoredApplicationOrder() // // Retrieves the gadget order from the property store. The memory must // be CoTaskMemFree'd by the caller. // // Parameters: // ppAppIds [out] // A pointer to an array of APPLICATION_IDs that upon return will // contain the application IDs in the order that they should be // presented on the device // pcAppIds [out] // A pointer to a DWORD that upon return will contain the count of // APPLICATION_ID contained in the array pointed to by ppAppIds. // // Return Values: // S_OK: Success // ///////////////////////////////////////////////////////////////////////// HRESULT CWssBasicDDI::GetStoredApplicationOrder(APPLICATION_ID** ppAppIds, DWORD* pcAppIds) { if ((NULL == m_pUserSID) || (0 == IsValidSid(m_pUserSID)) || (NULL == m_pPropertyStore)) { return E_UNEXPECTED; } HRESULT hr = S_OK; LPTSTR wszKey = NULL; // Initialize out parameters *ppAppIds = NULL; *pcAppIds = 0; // // Get the key to use for the property store // hr = GetApplicationOrderString(&wszKey); // // Put the data into a PROPVARIANT, and store it in the property store // if ((SUCCEEDED(hr)) && (NULL != wszKey)) { PROPVARIANT pvBlob = {0}; PropVariantInit(&pvBlob); hr = m_pPropertyStore->GetNamedValue(wszKey, &pvBlob); if (SUCCEEDED(hr) && (VT_BLOB == pvBlob.vt) && (0 == (pvBlob.blob.cbSize % sizeof(APPLICATION_ID)))) { *pcAppIds = pvBlob.blob.cbSize / sizeof(APPLICATION_ID); *ppAppIds = (APPLICATION_ID*)pvBlob.blob.pBlobData; } // Don't clear the PROPVARIANT because we don't want to erase the memory that we pass out of this method } if (NULL != wszKey) { ::CoTaskMemFree(wszKey); } return hr; }
NTSTATUS kuhl_m_sid_lookup(int argc, wchar_t * argv[]) { PWSTR name, domain; PSID pSid; SID_NAME_USE nameUse; PCWCHAR szName, szSystem = NULL; kull_m_string_args_byName(argc, argv, L"system", &szSystem, NULL); if(kull_m_string_args_byName(argc, argv, L"sid", &szName, NULL)) { if(ConvertStringSidToSid(szName, &pSid)) { kprintf(L"SID : %s\n", szName); if(IsValidSid(pSid)) { if(kull_m_token_getNameDomainFromSID(pSid, &name, &domain, &nameUse, szSystem)) { kprintf(L"Type : %s\n" L"Domain: %s\n" L"Name : %s\n", kull_m_token_getSidNameUse(nameUse), domain, name); LocalFree(name); LocalFree(domain); } else PRINT_ERROR_AUTO(L"kull_m_token_getNameDomainFromSID"); } else PRINT_ERROR(L"Invalid SID\n"); LocalFree(pSid); } else PRINT_ERROR_AUTO(L"ConvertStringSidToSid"); } else if(kull_m_string_args_byName(argc, argv, L"name", &szName, NULL)) { kprintf(L"Name : %s\n", szName); if(kull_m_token_getSidDomainFromName(szName, &pSid, &domain, &nameUse, szSystem)) { kprintf(L"Type : %s\n" L"Domain: %s\n" L"SID : ", kull_m_token_getSidNameUse(nameUse), domain); kull_m_string_displaySID(pSid); kprintf(L"\n"); LocalFree(pSid); LocalFree(domain); } else PRINT_ERROR_AUTO(L"kull_m_token_getSidDomainFromName"); } else PRINT_ERROR(L"/sid or /name is missing\n"); return STATUS_SUCCESS; }
//------------------------------------------------------------------------------ //local function part !!! //------------------------------------------------------------------------------ void GetSIDFromUser(char *user, char* rid, char *sid, unsigned int max_size) { BYTE BSid[MAX_PATH]; PSID psid = (PSID)BSid; DWORD sid_size = MAX_PATH; SID_NAME_USE TypeSid; char domain[MAX_PATH]; DWORD domain_size = MAX_PATH; if(LookupAccountName(NULL,(LPCTSTR)user,psid,(LPDWORD)&sid_size,(LPTSTR)domain,(LPDWORD)&domain_size,(PSID_NAME_USE)&TypeSid)) { if (IsValidSid(psid))SidtoUser(psid, user, rid, sid, max_size); } }
///////////////////////////////////////////////////////////////////////// // // OnGetApplicationOrder() // // Retrieves the order that applications should be listed in by the CPL. // // Parameters: // ppAppIds [out] // A pointer to an array of APPLICATION_IDs that upon return will // contain the application IDs in the order that they should be // presented in the WSS CPL. // pcAppIds [out] // A pointer to a DWORD that upon return will contain the count of // APPLICATION_ID contained in the array pointed to by ppAppIds. // // Return Values: // S_OK: Success // ///////////////////////////////////////////////////////////////////////// __override HRESULT CWssBasicDDI::OnGetApplicationOrder(_Outptr_result_maybenull_ APPLICATION_ID** ppAppIds, _Out_ DWORD* pcAppIds) { if ((NULL == m_pUserSID) || (0 == IsValidSid(m_pUserSID)) || (NULL == m_pPropertyStore) || (NULL == g_pDataManager)) { return E_UNEXPECTED; } // // Get the gadget order from the DataManager // return g_pDataManager->GetApplications(ppAppIds, pcAppIds); }
bool get_user_sid(SCP_string& outStr) { HANDLE hToken = NULL; if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken) == FALSE) { mprintf(("Failed to get process token! Error Code: %d", GetLastError())); return false; } DWORD dwBufferSize; GetTokenInformation(hToken, TokenUser, NULL, 0, &dwBufferSize); PTOKEN_USER ptkUser = (PTOKEN_USER) new byte[dwBufferSize]; if (GetTokenInformation(hToken, TokenUser, ptkUser, dwBufferSize, &dwBufferSize)) { CloseHandle(hToken); } if (IsValidSid(ptkUser->User.Sid) == FALSE) { mprintf(("Invalid SID structure detected!")); delete[] ptkUser; return false; } LPTSTR sidName = NULL; if (ConvertSidToStringSid(ptkUser->User.Sid, &sidName) == 0) { mprintf(("Failed to convert SID structure to string! Error Code: %d", GetLastError())); delete[] ptkUser; return false; } outStr.assign(sidName); LocalFree(sidName); delete[](byte*) ptkUser; return true; }
NTSTATUS kuhl_m_sid_add(int argc, wchar_t * argv[]) { PLDAP ld; DWORD dwErr; PCWCHAR szName; PWCHAR domain = NULL; PLDAPMessage pMessage = NULL; BERVAL NewSid; PBERVAL pNewSid[2] = {&NewSid, NULL}; LDAPMod Modification = {LDAP_MOD_ADD | LDAP_MOD_BVALUES, L"sIDHistory"}; PLDAPMod pModification[2] = {&Modification, NULL}; Modification.mod_vals.modv_bvals = pNewSid; if(kull_m_string_args_byName(argc, argv, L"new", &szName, NULL)) { if(ConvertStringSidToSid(szName, (PSID *) &NewSid.bv_val) || kull_m_token_getSidDomainFromName(szName, (PSID *) &NewSid.bv_val, &domain, NULL, NULL)) { if(IsValidSid((PSID) NewSid.bv_val)) { NewSid.bv_len = GetLengthSid((PSID) NewSid.bv_val); if(kuhl_m_sid_quickSearch(argc, argv, TRUE, NULL, &ld, &pMessage)) { kprintf(L"\n * Will try to add \'%s\' this new SID:\'", Modification.mod_type); kull_m_string_displaySID(NewSid.bv_val); kprintf(L"\': "); dwErr = ldap_modify_s(ld, ldap_get_dn(ld, pMessage), pModification); if(dwErr == LDAP_SUCCESS) kprintf(L"OK!\n"); else PRINT_ERROR(L"ldap_modify_s 0x%x (%u)\n", dwErr, dwErr); if(pMessage) ldap_msgfree(pMessage); ldap_unbind(ld); } } else PRINT_ERROR(L"Invalid SID\n"); LocalFree(NewSid.bv_val); if(domain) LocalFree(domain); } else PRINT_ERROR_AUTO(L"ConvertStringSidToSid / kull_m_token_getSidDomainFromName"); } else PRINT_ERROR(L"/new:sid or /new:resolvable_name is needed"); return STATUS_SUCCESS; }
///////////////////////////////////////////////////////////////////////// // // SetStoredApplicationOrder() // // Stores the application order in the property store for the device // // Parameters: // pApps [in] // An array of APPLICATION_IDs. // cApps [out] // A DWORD that is the count of application IDs in the array, pApps. // // Return Values: // S_OK: Success // E_INVALIDARG: cApps was == 0 or > 1024 // ///////////////////////////////////////////////////////////////////////// HRESULT CWssBasicDDI::SetStoredApplicationOrder(const APPLICATION_ID* pApps, const DWORD cApps) { if ((NULL == m_pUserSID) || (0 == IsValidSid(m_pUserSID)) || (NULL == m_pPropertyStore)) { return E_UNEXPECTED; } HRESULT hr = S_OK; LPTSTR wszKey = NULL; // // Get the key to use for the property store // hr = GetApplicationOrderString(&wszKey); // // Put the data into a PROPVARIANT, and store it in the property store // if ((SUCCEEDED(hr)) && (NULL != wszKey)) { PROPVARIANT pvBlob = {0}; PropVariantInit(&pvBlob); pvBlob.vt = VT_BLOB; pvBlob.blob.cbSize = cApps * sizeof(APPLICATION_ID); pvBlob.blob.pBlobData = (BYTE*)pApps; hr = m_pPropertyStore->SetNamedValue(wszKey, &pvBlob); // Don't clear the PROPVARIANT because we don't want to erase the memory that we don't own. } if (NULL != wszKey) { ::CoTaskMemFree(wszKey); } return hr; }
PWCHAR kuhl_m_sid_filterFromArgs(int argc, wchar_t * argv[]) { PWCHAR filter = NULL; PCWCHAR szName; DWORD i, sidLen; size_t buffLen; PSID pSid; if(kull_m_string_args_byName(argc, argv, L"sam", &szName, NULL)) { buffLen = wcslen(L"(sAMAccountName=") + wcslen(szName) + wcslen(L")") + 1; if(filter = (PWCHAR) LocalAlloc(LPTR, buffLen * sizeof(wchar_t))) { if(swprintf_s(filter, buffLen, L"(sAMAccountName=%s)", szName) != (buffLen - 1)) filter = (PWCHAR) LocalFree(filter); } } else if(kull_m_string_args_byName(argc, argv, L"sid", &szName, NULL)) { if(ConvertStringSidToSid(szName, &pSid)) { if(IsValidSid(pSid)) { sidLen = GetLengthSid(pSid); buffLen = wcslen(L"(objectSid=") + (sidLen * 3) + wcslen(L")") + 1; if(filter = (PWCHAR) LocalAlloc(LPTR, buffLen * sizeof(wchar_t))) { RtlCopyMemory(filter, L"(objectSid=", sizeof(L"(objectSid=")); for(i = 0; i < sidLen; i++) swprintf_s(filter + ARRAYSIZE(L"(objectSid=") - 1 + (i * 3), 3 + 1, L"\\%02x", ((PBYTE) pSid)[i]); filter[buffLen - 2] = L')'; } } else PRINT_ERROR(L"Invalid SID\n"); LocalFree(pSid); } else PRINT_ERROR_AUTO(L"ConvertStringSidToSid"); } else PRINT_ERROR(L"/sam or /sid to target the account is needed\n"); return filter; }
bool GetFileOwner(const wchar_t *Computer,const wchar_t *Name, string &strOwner) { bool Result=false; /* if(!Owner) { SIDCacheFlush(); return TRUE; } */ strOwner.Clear(); SECURITY_INFORMATION si=OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION;; DWORD LengthNeeded=0; NTPath strName(Name); PSECURITY_DESCRIPTOR sd=reinterpret_cast<PSECURITY_DESCRIPTOR>(sddata); if (GetFileSecurity(strName,si,sd,sizeof(sddata),&LengthNeeded) && LengthNeeded<=sizeof(sddata)) { PSID pOwner; BOOL OwnerDefaulted; if (GetSecurityDescriptorOwner(sd,&pOwner,&OwnerDefaulted)) { if (IsValidSid(pOwner)) { const wchar_t *Owner=GetNameFromSIDCache(pOwner); if (!Owner) { Owner=AddSIDToCache(Computer,pOwner); } if (Owner) { strOwner=Owner; Result=true; } } } } return Result; }
DWORD IDMCloneSid( PSID pSid, PSID *ppNewSid ) { DWORD dwError = 0; DWORD sidLen = 0; PSID pNewSid = NULL; if (!IsValidSid(pSid)) { dwError = ERROR_INVALID_SID; BAIL_ON_ERROR(dwError); } sidLen = GetLengthSid(pSid); dwError = IDMAllocateMemory( sidLen, (PVOID*) &pNewSid); BAIL_ON_ERROR(dwError); if (!CopySid(sidLen, pNewSid, pSid)) { dwError = GetLastError(); BAIL_ON_ERROR(dwError); } *ppNewSid = pNewSid; error: if (dwError) { IDM_SAFE_FREE_MEMORY(pNewSid); } return dwError; }
void AddDefaultUserdata(PluginPanelItem* Item,int level,int sortorder,int itemtype,PSID sid,const wchar_t* wide_name,const wchar_t* filename) { TCHAR* item_filename=(TCHAR*)malloc((_tcslen(filename)+1)*sizeof(TCHAR)); Item->FileName=item_filename; if(item_filename) { if(item_filename) _tcscpy(item_filename,filename); } PluginUserData *user_data; int user_data_size=sizeof(PluginUserData),sid_size=0,name_size=0; if(sid&&IsValidSid(sid)) sid_size=GetLengthSid(sid); name_size=(wcslen(wide_name)+1)*sizeof(wchar_t); user_data_size+=sid_size+name_size; user_data=(PluginUserData *)malloc(user_data_size); if(user_data) { user_data->size=user_data_size; user_data->level=level; user_data->sortorder=sortorder; user_data->itemtype=itemtype; if(sid_size) { CopySid(sid_size,(PSID)(user_data+1),sid); user_data->user_diff=sizeof(PluginUserData); } if(name_size) { wchar_t *ptr=(wchar_t *)((char *)(user_data+1)+sid_size); user_data->wide_name_diff=sizeof(PluginUserData)+sid_size; wcscpy(ptr,wide_name); } Item->UserData.FreeData=FreeUserData; Item->UserData.Data=user_data; } }
// // Function : sidToText // Role : Converts a binary SID to a nice one // Notes : http://win32.mvps.org/security/dumpacl/dumpacl.cpp // const char *sidToTextTok( PSID psid ) { // S-rev- + SIA + subauthlen*maxsubauth + terminator static char buf[15 + 12 + 12*SID_MAX_SUB_AUTHORITIES + 1]; char *p = &buf[0]; PSID_IDENTIFIER_AUTHORITY psia; DWORD numSubAuths, i; // Validate the binary SID. if ( ! IsValidSid( psid ) ) return FALSE; psia = GetSidIdentifierAuthority( psid ); p = buf; p += _snprintf_s( p, 15 + 12 + 12*SID_MAX_SUB_AUTHORITIES + 1, &buf[sizeof buf] - p, "S-%lu-", 0x0f & *( (byte *) psid ) ); if ( ( psia->Value[0] != 0 ) || ( psia->Value[1] != 0 ) ) p += _snprintf_s( p,15 + 12 + 12*SID_MAX_SUB_AUTHORITIES + 1, &buf[sizeof buf] - p, "0x%02hx%02hx%02hx%02hx%02hx%02hx", (USHORT) psia->Value[0], (USHORT) psia->Value[1], (USHORT) psia->Value[2], (USHORT) psia->Value[3], (USHORT) psia->Value[4], (USHORT) psia->Value[5] ); else p += _snprintf_s( p, 15 + 12 + 12*SID_MAX_SUB_AUTHORITIES + 1, &buf[sizeof buf] - p, "%lu", (ULONG) ( psia->Value[5] ) + (ULONG) ( psia->Value[4] << 8 ) + (ULONG) ( psia->Value[3] << 16 ) + (ULONG) ( psia->Value[2] << 24 ) ); // Add SID subauthorities to the string. numSubAuths = *GetSidSubAuthorityCount( psid ); for ( i = 0; i < numSubAuths; ++ i ) p += _snprintf_s( p, 15 + 12 + 12*SID_MAX_SUB_AUTHORITIES + 1,&buf[sizeof buf] - p, "-%lu", *GetSidSubAuthority( psid, i ) ); return buf; }
HRESULT COpcSecurity::GetTokenSids(HANDLE hToken, PSID* ppUserSid, PSID* ppGroupSid) { DWORD dwSize; HRESULT hr; PTOKEN_USER ptkUser = NULL; PTOKEN_PRIMARY_GROUP ptkGroup = NULL; if (ppUserSid) *ppUserSid = NULL; if (ppGroupSid) *ppGroupSid = NULL; if (ppUserSid) { // Get length required for TokenUser by specifying buffer length of 0 GetTokenInformation(hToken, TokenUser, NULL, 0, &dwSize); hr = GetLastError(); if (hr != ERROR_INSUFFICIENT_BUFFER) { // Expected ERROR_INSUFFICIENT_BUFFER OPCASSERT(FALSE); hr = HRESULT_FROM_WIN32(hr); goto failed; } ptkUser = (TOKEN_USER*) malloc(dwSize); if (ptkUser == NULL) { hr = E_OUTOFMEMORY; goto failed; } // Get Sid of process token. if (!GetTokenInformation(hToken, TokenUser, ptkUser, dwSize, &dwSize)) { // Couldn't get user info hr = HRESULT_FROM_WIN32(GetLastError()); OPCASSERT(FALSE); goto failed; } // Make a copy of the Sid for the return value dwSize = GetLengthSid(ptkUser->User.Sid); PSID pSid; pSid = (PSID) malloc(dwSize); if (pSid == NULL) { hr = E_OUTOFMEMORY; goto failed; } if (!CopySid(dwSize, pSid, ptkUser->User.Sid)) { hr = HRESULT_FROM_WIN32(GetLastError()); OPCASSERT(FALSE); goto failed; } OPCASSERT(IsValidSid(pSid)); *ppUserSid = pSid; free(ptkUser); } if (ppGroupSid) { // Get length required for TokenPrimaryGroup by specifying buffer length of 0 GetTokenInformation(hToken, TokenPrimaryGroup, NULL, 0, &dwSize); hr = GetLastError(); if (hr != ERROR_INSUFFICIENT_BUFFER) { // Expected ERROR_INSUFFICIENT_BUFFER OPCASSERT(FALSE); hr = HRESULT_FROM_WIN32(hr); goto failed; } ptkGroup = (TOKEN_PRIMARY_GROUP*) malloc(dwSize); if (ptkGroup == NULL) { hr = E_OUTOFMEMORY; goto failed; } // Get Sid of process token. if (!GetTokenInformation(hToken, TokenPrimaryGroup, ptkGroup, dwSize, &dwSize)) { // Couldn't get user info hr = HRESULT_FROM_WIN32(GetLastError()); OPCASSERT(FALSE); goto failed; } // Make a copy of the Sid for the return value dwSize = GetLengthSid(ptkGroup->PrimaryGroup); PSID pSid; pSid = (PSID) malloc(dwSize); if (pSid == NULL) { hr = E_OUTOFMEMORY; goto failed; } if (!CopySid(dwSize, pSid, ptkGroup->PrimaryGroup)) { hr = HRESULT_FROM_WIN32(GetLastError()); OPCASSERT(FALSE); goto failed; } OPCASSERT(IsValidSid(pSid)); *ppGroupSid = pSid; free(ptkGroup); } return S_OK; failed: if (ptkUser) free(ptkUser); if (ptkGroup) free (ptkGroup); return hr; }