53039977b0ed5112d8fc7078eec50ea280dc5a2f
[reactos.git] / rosapps / sysutils / regexpl / ShellCommandOwner.cpp
1 /* $Id: ShellCommandOwner.cpp,v 1.1 2000/10/04 21:04:31 ea Exp $
2 *
3 * regexpl - Console Registry Explorer
4 *
5 * Copyright (c) 1999-2000 Nedko Arnaoudov <nedkohome@atia.com>
6 *
7 * License: GNU GPL
8 *
9 */
10
11 // ShellCommandOwner.cpp: implementation of the CShellCommandOwner class.
12 //
13 //////////////////////////////////////////////////////////////////////
14
15 #include "ph.h"
16 #include "ShellCommandOwner.h"
17 #include "RegistryExplorer.h"
18 #include "SecurityDescriptor.h"
19
20 #define OWNER_CMD _T("OWNER")
21 #define OWNER_CMD_LENGTH COMMAND_LENGTH(OWNER_CMD)
22 #define OWNER_CMD_SHORT_DESC OWNER_CMD _T(" command is used to view")/*"/change"*/_T(" key's owner.\n")
23
24 //////////////////////////////////////////////////////////////////////
25 // Construction/Destruction
26 //////////////////////////////////////////////////////////////////////
27
28 CShellCommandOwner::CShellCommandOwner(CRegistryTree& rTree):m_rTree(rTree)
29 {
30 }
31
32 CShellCommandOwner::~CShellCommandOwner()
33 {
34 }
35
36 BOOL CShellCommandOwner::Match(const TCHAR *pchCommand)
37 {
38 if (_tcsicmp(pchCommand,OWNER_CMD) == 0)
39 return TRUE;
40 if (_tcsnicmp(pchCommand,OWNER_CMD _T(".."),OWNER_CMD_LENGTH+2*sizeof(TCHAR)) == 0)
41 return TRUE;
42 if (_tcsnicmp(pchCommand,OWNER_CMD _T("/") ,OWNER_CMD_LENGTH+1*sizeof(TCHAR)) == 0)
43 return TRUE;
44 if (_tcsnicmp(pchCommand,OWNER_CMD _T("\\"),OWNER_CMD_LENGTH+1*sizeof(TCHAR)) == 0)
45 return TRUE;
46 return FALSE;
47 }
48
49 int CShellCommandOwner::Execute(CConsole &rConsole, CArgumentParser& rArguments)
50 {
51 const TCHAR *pchKey = NULL;
52 BOOL blnDo = TRUE;
53 BOOL blnBadParameter = FALSE;
54 BOOL blnHelp = FALSE;
55 const TCHAR *pchParameter;
56 DWORD dwError;
57
58 rArguments.ResetArgumentIteration();
59 const TCHAR *pchCommandItself = rArguments.GetNextArgument();
60
61 if ((_tcsnicmp(pchCommandItself,OWNER_CMD _T(".."),OWNER_CMD_LENGTH+2*sizeof(TCHAR)) == 0)||
62 (_tcsnicmp(pchCommandItself,OWNER_CMD _T("\\"),OWNER_CMD_LENGTH+1*sizeof(TCHAR)) == 0))
63 {
64 pchKey = pchCommandItself + OWNER_CMD_LENGTH;
65 }
66 else if (_tcsnicmp(pchCommandItself,OWNER_CMD _T("/"),OWNER_CMD_LENGTH+1*sizeof(TCHAR)) == 0)
67 {
68 pchParameter = pchCommandItself + OWNER_CMD_LENGTH;
69 goto CheckOwnerArgument;
70 }
71
72 while((pchParameter = rArguments.GetNextArgument()) != NULL)
73 {
74 CheckOwnerArgument:
75 blnBadParameter = FALSE;
76 if ((_tcsicmp(pchParameter,_T("/?")) == 0)
77 ||(_tcsicmp(pchParameter,_T("-?")) == 0))
78 {
79 blnHelp = TRUE;
80 blnDo = pchKey != NULL;
81 }
82 else if (!pchKey)
83 {
84 pchKey = pchParameter;
85 blnDo = TRUE;
86 }
87 else
88 {
89 blnBadParameter = TRUE;
90 }
91 if (blnBadParameter)
92 {
93 rConsole.Write(_T("Bad parameter: "));
94 rConsole.Write(pchParameter);
95 rConsole.Write(_T("\n"));
96 }
97 }
98
99 CRegistryTree *pTree = NULL;
100 CRegistryKey *pKey = NULL;
101
102 if (pchKey)
103 {
104 pTree = new CRegistryTree(m_rTree);
105 if ((_tcscmp(pTree->GetCurrentPath(),m_rTree.GetCurrentPath()) != 0)||(!pTree->ChangeCurrentKey(pchKey)))
106 {
107 rConsole.Write(_T("Cannot open key "));
108 rConsole.Write(pchKey);
109 rConsole.Write(_T("\n"));
110 //blnHelp = TRUE;
111 blnDo = FALSE;
112 }
113 else
114 {
115 pKey = pTree->GetCurrentKey();
116 }
117 }
118 else
119 {
120 pKey = m_rTree.GetCurrentKey();
121 }
122
123 if (blnHelp)
124 {
125 rConsole.Write(GetHelpString());
126 }
127
128 if (blnDo&&blnHelp) rConsole.Write(_T("\n"));
129
130 if (blnDo)
131 {
132 if (pKey == NULL)
133 { // root key
134 rConsole.Write(OWNER_CMD COMMAND_NA_ON_ROOT);
135 }
136 else
137 {
138 PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL;
139 TCHAR *pchName = NULL, *pchDomainName = NULL;
140 try
141 {
142 DWORD dwSecurityDescriptorLength;
143 rConsole.Write(_T("Key : "));
144 rConsole.Write(_T("\\"));
145 rConsole.Write(pTree?pTree->GetCurrentPath():m_rTree.GetCurrentPath());
146 rConsole.Write(_T("\n"));
147 dwError = pKey->GetSecurityDescriptorLength(&dwSecurityDescriptorLength);
148 if (dwError != ERROR_SUCCESS) throw dwError;
149
150 pSecurityDescriptor = (PSECURITY_DESCRIPTOR) new unsigned char [dwSecurityDescriptorLength];
151 DWORD dwSecurityDescriptorLength1 = dwSecurityDescriptorLength;
152 dwError = pKey->GetSecurityDescriptor((SECURITY_INFORMATION)OWNER_SECURITY_INFORMATION,pSecurityDescriptor,&dwSecurityDescriptorLength1);
153 if (dwError != ERROR_SUCCESS) throw dwError;
154 PSID psidOwner;
155 BOOL blnOwnerDefaulted;
156 if (!GetSecurityDescriptorOwner(pSecurityDescriptor,&psidOwner,&blnOwnerDefaulted))
157 throw GetLastError();
158 if (psidOwner == NULL)
159 {
160 rConsole.Write(_T("Key has no owner."));
161 }
162 else
163 {
164 if (!IsValidSid(psidOwner))
165 {
166 rConsole.Write(_T("Key has invalid owner SID."));
167 }
168 else
169 {
170 rConsole.Write(_T("Key Owner: \n"));
171 DWORD dwSIDStringSize = 0;
172 BOOL blnRet = GetTextualSid(psidOwner,NULL,&dwSIDStringSize);
173 ASSERT(!blnRet);
174 ASSERT(GetLastError() == ERROR_INSUFFICIENT_BUFFER);
175 TCHAR *pchSID = new TCHAR[dwSIDStringSize];
176 if(!GetTextualSid(psidOwner,pchSID,&dwSIDStringSize))
177 {
178 dwError = GetLastError();
179 ASSERT(dwError != ERROR_INSUFFICIENT_BUFFER);
180 rConsole.Write(_T("Error "));
181 TCHAR Buffer[256];
182 rConsole.Write(_itot(dwError,Buffer,10));
183 rConsole.Write(_T("\nGetting string representation of SID\n"));
184 }
185 else
186 {
187 rConsole.Write(_T("\tSID: "));
188 rConsole.Write(pchSID);
189 rConsole.Write(_T("\n"));
190 }
191 delete [] pchSID;
192 DWORD dwNameBufferLength, dwDomainNameBufferLength;
193 dwNameBufferLength = 1024;
194 dwDomainNameBufferLength = 1024;
195 pchName = new TCHAR [dwNameBufferLength];
196 pchDomainName = new TCHAR [dwDomainNameBufferLength];
197 DWORD dwNameLength = dwNameBufferLength, dwDomainNameLength = dwDomainNameBufferLength;
198 SID_NAME_USE Use;
199 if (!LookupAccountSid(NULL,psidOwner,pchName,&dwNameLength,pchDomainName,&dwDomainNameLength,&Use))
200 throw GetLastError();
201 else
202 {
203 rConsole.Write(_T("\tOwner Domain: "));
204 rConsole.Write(pchDomainName);
205 rConsole.Write(_T("\n"));
206 rConsole.Write(_T("\tOwner Name: "));
207 rConsole.Write(pchName);
208 rConsole.Write(_T("\n\tSID type: "));
209 rConsole.Write(GetSidTypeName(Use));
210 rConsole.Write(_T("\n"));
211 rConsole.Write(_T("\tOwner defaulted: "));
212 rConsole.Write(blnOwnerDefaulted?_T("Yes"):_T("No"));
213 rConsole.Write(_T("\n"));
214 }
215 delete [] pchName;
216 pchName = NULL;
217 delete [] pchDomainName;
218 pchDomainName = NULL;
219
220 }
221 }
222 delete [] pSecurityDescriptor;
223 }
224 catch (DWORD dwError)
225 {
226 rConsole.Write(_T("Error "));
227 TCHAR Buffer[256];
228 rConsole.Write(_itot(dwError,Buffer,10));
229 rConsole.Write(_T("\n"));
230 if (pchName) delete [] pchName;
231 if (pchDomainName) delete [] pchDomainName;
232 if (pSecurityDescriptor) delete [] pSecurityDescriptor;
233 }
234 } // else (pKey == NULL)
235 } // if (blnDo)
236
237 if (pTree)
238 delete pTree;
239
240 return 0;
241 }
242
243 const TCHAR * CShellCommandOwner::GetHelpString()
244 {
245 return OWNER_CMD_SHORT_DESC
246 _T("Syntax: ") OWNER_CMD _T(" [<KEY>] [/?]\n\n")
247 _T(" <KEY> - Optional relative path of desired key.\n")
248 _T(" /? - This help.\n\n")
249 _T("Without parameters, command displays information about owner of current key.\n");
250 }
251
252 const TCHAR * CShellCommandOwner::GetHelpShortDescriptionString()
253 {
254 return OWNER_CMD_SHORT_DESC;
255 }
256