int CShellCommandOwner::Execute(CConsole &rConsole, CArgumentParser& rArguments) { const TCHAR *pchKey = NULL; BOOL blnDo = TRUE; BOOL blnBadParameter = FALSE; BOOL blnHelp = FALSE; const TCHAR *pchParameter; DWORD dwError; rArguments.ResetArgumentIteration(); const TCHAR *pchCommandItself = rArguments.GetNextArgument(); if ((_tcsnicmp(pchCommandItself,OWNER_CMD _T(".."),OWNER_CMD_LENGTH+2*sizeof(TCHAR)) == 0)|| (_tcsnicmp(pchCommandItself,OWNER_CMD _T("\\"),OWNER_CMD_LENGTH+1*sizeof(TCHAR)) == 0)) { pchKey = pchCommandItself + OWNER_CMD_LENGTH; } else if (_tcsnicmp(pchCommandItself,OWNER_CMD _T("/"),OWNER_CMD_LENGTH+1*sizeof(TCHAR)) == 0) { pchParameter = pchCommandItself + OWNER_CMD_LENGTH; goto CheckOwnerArgument; } while((pchParameter = rArguments.GetNextArgument()) != NULL) { CheckOwnerArgument: blnBadParameter = FALSE; if ((_tcsicmp(pchParameter,_T("/?")) == 0) ||(_tcsicmp(pchParameter,_T("-?")) == 0)) { blnHelp = TRUE; blnDo = pchKey != NULL; } else if (!pchKey) { pchKey = pchParameter; blnDo = TRUE; } else { blnBadParameter = TRUE; } if (blnBadParameter) { rConsole.Write(_T("Bad parameter: ")); rConsole.Write(pchParameter); rConsole.Write(_T("\n")); } } CRegistryKey Key; if (!m_rTree.GetKey(pchKey?pchKey:_T("."),KEY_QUERY_VALUE|READ_CONTROL,Key)) { rConsole.Write(m_rTree.GetLastErrorDescription()); blnDo = FALSE; } if (blnHelp) { rConsole.Write(GetHelpString()); } if (blnDo&&blnHelp) rConsole.Write(_T("\n")); if (!blnDo) return 0; if (Key.IsRoot()) { // root key rConsole.Write(OWNER_CMD COMMAND_NA_ON_ROOT); return 0; } PISECURITY_DESCRIPTOR pSecurityDescriptor = NULL; TCHAR *pchName = NULL, *pchDomainName = NULL; try { DWORD dwSecurityDescriptorLength; rConsole.Write(_T("Key : ")); rConsole.Write(_T("\\")); rConsole.Write(Key.GetKeyName()); rConsole.Write(_T("\n")); dwError = Key.GetSecurityDescriptorLength(&dwSecurityDescriptorLength); if (dwError != ERROR_SUCCESS) throw dwError; pSecurityDescriptor = (PISECURITY_DESCRIPTOR) new unsigned char [dwSecurityDescriptorLength]; DWORD dwSecurityDescriptorLength1 = dwSecurityDescriptorLength; dwError = Key.GetSecurityDescriptor((SECURITY_INFORMATION)OWNER_SECURITY_INFORMATION,pSecurityDescriptor,&dwSecurityDescriptorLength1); if (dwError != ERROR_SUCCESS) throw dwError; PSID psidOwner; BOOL blnOwnerDefaulted; if (!GetSecurityDescriptorOwner(pSecurityDescriptor,&psidOwner,&blnOwnerDefaulted)) throw GetLastError(); if (psidOwner == NULL) { rConsole.Write(_T("Key has no owner.")); } else { if (!IsValidSid(psidOwner)) { rConsole.Write(_T("Key has invalid owner SID.")); } else { rConsole.Write(_T("Key Owner: \n")); DWORD dwSIDStringSize = 0; BOOL blnRet = GetTextualSid(psidOwner,NULL,&dwSIDStringSize); ASSERT(!blnRet); ASSERT(GetLastError() == ERROR_INSUFFICIENT_BUFFER); TCHAR *pchSID = new TCHAR[dwSIDStringSize]; if(!GetTextualSid(psidOwner,pchSID,&dwSIDStringSize)) { dwError = GetLastError(); ASSERT(dwError != ERROR_INSUFFICIENT_BUFFER); rConsole.Write(_T("Error ")); TCHAR Buffer[256]; rConsole.Write(_itoa(dwError,Buffer,10)); rConsole.Write(_T("\nGetting string representation of SID\n")); } else { rConsole.Write(_T("\tSID: ")); rConsole.Write(pchSID); rConsole.Write(_T("\n")); } delete [] pchSID; DWORD dwNameBufferLength, dwDomainNameBufferLength; dwNameBufferLength = 1024; dwDomainNameBufferLength = 1024; pchName = new TCHAR [dwNameBufferLength]; pchDomainName = new TCHAR [dwDomainNameBufferLength]; DWORD dwNameLength = dwNameBufferLength, dwDomainNameLength = dwDomainNameBufferLength; SID_NAME_USE Use; if (!LookupAccountSid(NULL,psidOwner,pchName,&dwNameLength,pchDomainName,&dwDomainNameLength,&Use)) throw GetLastError(); else { rConsole.Write(_T("\tOwner Domain: ")); rConsole.Write(pchDomainName); rConsole.Write(_T("\n")); rConsole.Write(_T("\tOwner Name: ")); rConsole.Write(pchName); rConsole.Write(_T("\n\tSID type: ")); rConsole.Write(GetSidTypeName(Use)); rConsole.Write(_T("\n")); rConsole.Write(_T("\tOwner defaulted: ")); rConsole.Write(blnOwnerDefaulted?_T("Yes"):_T("No")); rConsole.Write(_T("\n")); } delete [] pchName; pchName = NULL; delete [] pchDomainName; pchDomainName = NULL; } } delete [] pSecurityDescriptor; } catch (DWORD dwError) { rConsole.Write(_T("Error ")); TCHAR Buffer[256]; rConsole.Write(_itoa(dwError,Buffer,10)); rConsole.Write(_T("\n")); if (pchName) delete [] pchName; if (pchDomainName) delete [] pchDomainName; if (pSecurityDescriptor) delete [] pSecurityDescriptor; } return 0; }
int CShellCommandDeleteValue::Execute(CConsole &rConsole, CArgumentParser& rArguments) { rArguments.ResetArgumentIteration(); TCHAR *pszCommandItself = rArguments.GetNextArgument(); TCHAR *pszParameter; TCHAR *pszValueFull = NULL; BOOL blnHelp = FALSE; if ((_tcsnicmp(pszCommandItself,DV_CMD _T(".."),DV_CMD_LENGTH+2*sizeof(TCHAR)) == 0)|| (_tcsnicmp(pszCommandItself,DV_CMD _T("\\"),DV_CMD_LENGTH+1*sizeof(TCHAR)) == 0)) { pszValueFull = pszCommandItself + DV_CMD_LENGTH; } else if (_tcsnicmp(pszCommandItself,DV_CMD _T("/"),DV_CMD_LENGTH+1*sizeof(TCHAR)) == 0) { pszParameter = pszCommandItself + DV_CMD_LENGTH; goto CheckValueArgument; } while((pszParameter = rArguments.GetNextArgument()) != NULL) { CheckValueArgument: if ((_tcsicmp(pszParameter,_T("/?")) == 0) ||(_tcsicmp(pszParameter,_T("-?")) == 0)) { blnHelp = TRUE; break; } else if (!pszValueFull) { pszValueFull = pszParameter; } else { rConsole.Write(_T("Bad parameter: ")); rConsole.Write(pszParameter); rConsole.Write(_T("\n")); } } CRegistryKey Key; TCHAR *pszValueNamePattern; const TCHAR *pszEmpty = _T(""); const TCHAR *pszPath; if (blnHelp) { rConsole.Write(GetHelpString()); return 0; } if (pszValueFull) { if (_tcscmp(pszValueFull,_T("\\")) == 0) goto CommandNAonRoot; TCHAR *pchSep = _tcsrchr(pszValueFull,_T('\\')); pszValueNamePattern = pchSep?(pchSep+1):(pszValueFull); pszPath = pchSep?pszValueFull:_T("."); if (pchSep) *pchSep = 0; } else { pszValueNamePattern = (TCHAR*)pszEmpty; pszPath = _T("."); } { size_t s = _tcslen(pszValueNamePattern); if (s && (pszValueNamePattern[0] == _T('\"'))&&(pszValueNamePattern[s-1] == _T('\"'))) { pszValueNamePattern[s-1] = 0; pszValueNamePattern++; } } if (!m_rTree.GetKey(pszPath,KEY_QUERY_VALUE|KEY_SET_VALUE,Key)) { rConsole.Write(m_rTree.GetLastErrorDescription()); return 0; } if (!Key.IsRoot()) { // not root key ??? TCHAR Buffer[254]; DWORD dwMaxValueNameLength; LONG nError = Key.GetMaxValueNameLength(dwMaxValueNameLength); if (nError != ERROR_SUCCESS) { _stprintf(Buffer,_T("Cannot query info about %s key. Error is %u\n"),Key.GetKeyName(),(unsigned int)nError); rConsole.Write(Buffer); return 0; } TCHAR *pszValueName = new (std::nothrow) TCHAR[dwMaxValueNameLength]; if (!pszValueName) { rConsole.Write("Out of memory."); return 0; } Key.InitValueEnumeration(pszValueName,dwMaxValueNameLength,NULL,0,NULL); while ((nError = Key.GetNextValue()) == ERROR_SUCCESS) { if (PatternMatch(pszValueNamePattern,pszValueName)) { nError = Key.DeleteValue(pszValueName); if (nError != ERROR_SUCCESS) { _stprintf(Buffer,_T("Cannot delete value. Error is %u\n"),(unsigned int)nError); rConsole.Write(Buffer); } else { InvalidateCompletion(); } Key.InitValueEnumeration(pszValueName,dwMaxValueNameLength,NULL,0,NULL); // reset iteration } } } // if (pKey) else { CommandNAonRoot: rConsole.Write(DV_CMD COMMAND_NA_ON_ROOT); } return 0; }
int CShellCommandValue::Execute(CConsole &rConsole, CArgumentParser& rArguments) { rArguments.ResetArgumentIteration(); TCHAR *pchCommandItself = rArguments.GetNextArgument(); TCHAR *pchParameter; TCHAR *pchValueFull = NULL; BOOL blnUnicodeDump = FALSE; BOOL blnBadParameter = FALSE; BOOL blnHelp = FALSE; LONG nError; DWORD dwValueSize; DWORD dwType = REG_NONE; BYTE *pDataBuffer = NULL; TCHAR *pchFilename = NULL; if ((_tcsnicmp(pchCommandItself,VALUE_CMD _T(".."),VALUE_CMD_LENGTH+2*sizeof(TCHAR)) == 0)|| (_tcsnicmp(pchCommandItself,VALUE_CMD _T("\\"),VALUE_CMD_LENGTH+1*sizeof(TCHAR)) == 0)) { pchValueFull = pchCommandItself + VALUE_CMD_LENGTH; } else if (_tcsnicmp(pchCommandItself,VALUE_CMD _T("/"),VALUE_CMD_LENGTH+1*sizeof(TCHAR)) == 0) { pchParameter = pchCommandItself + VALUE_CMD_LENGTH; goto CheckValueArgument; } while((pchParameter = rArguments.GetNextArgument()) != NULL) { CheckValueArgument: blnBadParameter = FALSE; if ((_tcsicmp(pchParameter,_T("/?")) == 0) ||(_tcsicmp(pchParameter,_T("-?")) == 0)) { blnHelp = TRUE; break; } else if (_tcsicmp(pchParameter,_T("/u")) == 0) { blnUnicodeDump = TRUE; } else if ((*pchParameter == _T('/'))&&(*(pchParameter+1) == _T('f'))) { pchFilename = pchParameter+2; } else if (!pchValueFull) { pchValueFull = pchParameter; } else { blnBadParameter = TRUE; } if (blnBadParameter) { rConsole.Write(_T("Bad parameter: ")); rConsole.Write(pchParameter); rConsole.Write(_T("\n")); } } CRegistryKey Key; TCHAR *pchValueName; const TCHAR *pszEmpty = _T(""); const TCHAR *pszPath; if (blnHelp) { rConsole.Write(GetHelpString()); if (pDataBuffer) delete pDataBuffer; return 0; } if (pchValueFull) { if (_tcscmp(pchValueFull,_T("\\")) == 0) goto ValueCommandNAonRoot; TCHAR *pchSep = _tcsrchr(pchValueFull,_T('\\')); pchValueName = pchSep?(pchSep+1):(pchValueFull); pszPath = pchSep?pchValueFull:_T("."); //if (_tcsrchr(pchValueName,_T('.'))) //{ // pchValueName = _T(""); // pchPath = pchValueFull; //} //else if (pchSep) *pchSep = 0; } else { pchValueName = (TCHAR*)pszEmpty; pszPath = _T("."); } if (!m_rTree.GetKey(pszPath,KEY_READ,Key)) { rConsole.Write(m_rTree.GetLastErrorDescription()); goto SkipValueCommand; } if (Key.IsRoot()) goto ValueCommandNAonRoot; { rConsole.Write(_T("Value name : \"")); rConsole.Write(_T("\\")); rConsole.Write(Key.GetKeyName()); size_t l = _tcslen(pchValueName); if (l&& (*pchValueName == _T('\"'))&& (pchValueName[l-1] == _T('\"'))) { pchValueName[l-1] = 0; pchValueName++; } rConsole.Write(pchValueName); rConsole.Write(_T("\"\n")); nError = Key.GetValue(pchValueName,NULL,NULL,&dwValueSize); if (nError == ERROR_SUCCESS) { pDataBuffer = new BYTE [dwValueSize]; Key.GetValue(pchValueName,&dwType,pDataBuffer,&dwValueSize); rConsole.Write(_T("Value type : ")); rConsole.Write(CRegistryKey::GetValueTypeName(dwType)); rConsole.Write(_T("\nValue data : ")); switch(dwType) { case REG_DWORD_LITTLE_ENDIAN: { TCHAR Buffer[3]; rConsole.Write(_T("0x")); for (unsigned int i = 0 ; i < dwValueSize ; i++) { _stprintf(Buffer,_T("%02X"),*(pDataBuffer+((dwValueSize-1)-i))); rConsole.Write(Buffer); } } rConsole.Write(_T("\n")); break; case REG_DWORD_BIG_ENDIAN: { TCHAR Buffer[3]; rConsole.Write(_T("0x")); for (unsigned int i = 0 ; i < dwValueSize ; i++) { _stprintf(Buffer,_T("%02X"),*(pDataBuffer+i)); rConsole.Write(Buffer); } } rConsole.Write(_T("\n")); break; case REG_LINK: break; case REG_MULTI_SZ: { TCHAR *pchCurrentString = (TCHAR *)pDataBuffer; rConsole.Write(_T("\n")); while(*pchCurrentString) { rConsole.Write(_T("\"")); rConsole.Write(pchCurrentString); rConsole.Write(_T("\"\n")); pchCurrentString += _tcslen(pchCurrentString)+1; } } break; case REG_RESOURCE_LIST: break; case REG_SZ: case REG_EXPAND_SZ: rConsole.Write(_T("\"")); rConsole.Write((TCHAR *)pDataBuffer); rConsole.Write(_T("\"\n")); break; case REG_BINARY: default: { TCHAR Buffer[256]; DWORD i, j; for (i = 0 ; i < dwValueSize ; i++) { if (i%16 == 0) { // ok this is begining of line rConsole.Write(_T("\n")); // print offset _stprintf(Buffer,_T("0x%08X "),(unsigned int)i); rConsole.Write(Buffer); } else if (i%8 == 0) { // this is the additional space between 7th and 8th byte in current line rConsole.Write(_T(" ")); } // print current byte unsigned int n = *(pDataBuffer+i); _stprintf(Buffer,_T("%02X "),n); rConsole.Write(Buffer); if (i && (i%16 == 15)) { // if this is the last byte in line // Dump text representation for (j = i-15; j <= i; j += blnUnicodeDump?2:1)\ { if ((j%8 == 0)&&(j%16 != 0)) { // this is the additional space between 7th and 8th byte in current line rConsole.Write(_T(" ")); } ASSERT(i-j < 16); // write current char representation if (blnUnicodeDump) { ASSERT(j%2 == 0); wchar_t ch = *(TCHAR *)(pDataBuffer+j); _stprintf(Buffer, #ifdef _UNICODE _T("%c"), #else // g++ may print warnings here (warning: __wchar_t format, different type arg (arg 3)) // %C in format string is a Microsoft extension. _T("%C"), #endif iswprint(ch)?ch:L'.'); } else { unsigned char ch = *(pDataBuffer+j); _stprintf(Buffer, #ifdef _UNICODE // g++ may print warnings here (warning: __wchar_t format, different type arg (arg 3)) // %C in format string is a Microsoft extension. _T("%C"), #else _T("%c"), #endif isprint(ch)?ch:'.'); } rConsole.Write(Buffer); } // for } // if } // for // print text representation of last line if it is not full (it have less than 16 bytes) // k is pseudo offset for (DWORD k = i; k%16 != 0; k++) { if (k%8 == 0) { // this is the additional space between 7th and 8th byte in current line rConsole.Write(_T(" ")); } _tcscpy(Buffer,_T(" ")); // the replacement of two digit of current byte + spacing rConsole.Write(Buffer); if (k && (k%16 == 15)) { // if this is the last byte in line ASSERT((k-15)%16 == 0); // k-15 must point at begin of last line for (j = k-15; j < i; j += blnUnicodeDump?2:1) { if (blnUnicodeDump&&(j+1 >= i)) { // ok, buffer size is odd number, so we don't display last byte. ASSERT(j+1 == i); break; } if ((j%8 == 0)&&(j%16 != 0)) { // this is the additional space between 7th and 8th byte in current line rConsole.Write(_T(" ")); } // write current char representation if (blnUnicodeDump) { ASSERT(j%2 == 0); wchar_t ch = *(TCHAR *)(pDataBuffer+j); _stprintf(Buffer, #ifdef _UNICODE _T("%c"), #else // g++ may print warnings here (warning: __wchar_t format, different type arg (arg 3)) // %C in format string is a Microsoft extension. _T("%C"), #endif iswprint(ch)?ch:L'.'); } else { unsigned char ch = *(pDataBuffer+j); _stprintf(Buffer, #ifdef _UNICODE // g++ may print warnings here (warning: __wchar_t format, different type arg (arg 3)) // %C in format string is a Microsoft extension. _T("%C"), #else _T("%c"), #endif isprint(ch)?ch:'.'); } rConsole.Write(Buffer); } // for } // if } // for } // default: rConsole.Write(_T("\n")); } // switch rConsole.Write(_T("\n")); if (pchFilename) { rConsole.Write(_T("Exporting value data to ")); rConsole.Write(pchFilename); rConsole.Write(_T(" ...\n")); HANDLE hFile = CreateFile(pchFilename,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL); if (hFile == INVALID_HANDLE_VALUE) { rConsole.Write(_T("Cannot create new file ")); rConsole.Write(pchFilename); rConsole.Write(_T("\n")); goto SkipValueCommand; } DWORD dwBytesWritten; if (!WriteFile(hFile,pDataBuffer,dwValueSize,&dwBytesWritten,NULL)) { rConsole.Write(_T("Error writting file.\n")); VERIFY(CloseHandle(hFile)); goto SkipValueCommand; } ASSERT(dwBytesWritten == dwValueSize); VERIFY(CloseHandle(hFile)); } } else { rConsole.Write(_T("Error ")); TCHAR Buffer[256]; rConsole.Write(_itoa(nError,Buffer,10)); rConsole.Write(_T("\n")); if (nError == ERROR_FILE_NOT_FOUND) { rConsole.Write(_T("(System cannot find the value specified)\n")); } } } SkipValueCommand: if (pDataBuffer) delete[] pDataBuffer; return 0; ValueCommandNAonRoot: rConsole.Write(VALUE_CMD COMMAND_NA_ON_ROOT); return 0; }