Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
6.3 KiB

  1. //
  2. // reg.c
  3. //
  4. // Common registry manipulation routines.
  5. //
  6. #ifdef UNICODE
  7. #define _UNICODE 1
  8. #endif
  9. #include <windows.h>
  10. #include <ole2.h>
  11. #include "acttest.h"
  12. #include "tchar.h"
  13. #ifndef CHICO
  14. #include <subauth.h>
  15. #include <ntlsa.h>
  16. #endif
  17. void DeleteSubTree( TCHAR * pszClsid, TCHAR * SubTree )
  18. {
  19. HKEY hClsidRoot;
  20. HKEY hClsid;
  21. long RegStatus;
  22. TCHAR szKeyName[256];
  23. DWORD KeyNameSize;
  24. FILETIME FileTime;
  25. int SubKey;
  26. RegStatus = RegOpenKeyEx( HKEY_CLASSES_ROOT,
  27. SubTree,
  28. 0,
  29. KEY_ALL_ACCESS,
  30. &hClsidRoot );
  31. if ( RegStatus != ERROR_SUCCESS )
  32. return;
  33. RegStatus = RegOpenKeyEx( hClsidRoot,
  34. pszClsid,
  35. 0,
  36. KEY_ALL_ACCESS,
  37. &hClsid );
  38. if ( RegStatus != ERROR_SUCCESS )
  39. return;
  40. for ( SubKey = 0; ; SubKey++ )
  41. {
  42. KeyNameSize = sizeof(szKeyName);
  43. RegStatus = RegEnumKeyEx(
  44. hClsid,
  45. SubKey,
  46. szKeyName,
  47. &KeyNameSize,
  48. 0,
  49. NULL,
  50. NULL,
  51. &FileTime );
  52. if ( RegStatus != ERROR_SUCCESS )
  53. break;
  54. RegStatus = RegDeleteKey( hClsid, szKeyName );
  55. }
  56. RegCloseKey( hClsid );
  57. RegDeleteKey( hClsidRoot, pszClsid );
  58. RegCloseKey( hClsidRoot );
  59. }
  60. void DeleteClsidKey( TCHAR * pwszClsid )
  61. {
  62. // Note that we also delete the corresponding AppID entries
  63. DeleteSubTree( pwszClsid, TEXT("CLSID"));
  64. DeleteSubTree( pwszClsid, TEXT("AppID"));
  65. }
  66. long SetAppIDSecurity( TCHAR * pszAppID )
  67. {
  68. HKEY hActKey;
  69. HKEY hAppIDKey;
  70. BYTE SecurityDescriptor[2000];
  71. LONG RegStatus;
  72. SECURITY_INFORMATION SI;
  73. DWORD dwSize = sizeof( SecurityDescriptor );
  74. DWORD Disposition;
  75. RegStatus = RegOpenKeyEx( HKEY_CLASSES_ROOT,
  76. TEXT("AppID"),
  77. 0,
  78. KEY_ALL_ACCESS,
  79. &hAppIDKey );
  80. if ( RegStatus != ERROR_SUCCESS )
  81. return RegStatus;
  82. RegStatus = RegCreateKeyEx(
  83. hAppIDKey,
  84. pszAppID,
  85. 0,
  86. TEXT("REG_SZ"),
  87. REG_OPTION_NON_VOLATILE,
  88. KEY_ALL_ACCESS,
  89. NULL,
  90. &hActKey,
  91. &Disposition );
  92. if ( RegStatus != ERROR_SUCCESS )
  93. return RegStatus;
  94. #ifndef CHICO
  95. RegStatus = RegGetKeySecurity( hActKey,
  96. OWNER_SECURITY_INFORMATION
  97. | GROUP_SECURITY_INFORMATION
  98. | DACL_SECURITY_INFORMATION,
  99. &SecurityDescriptor,
  100. &dwSize );
  101. if ( RegStatus != ERROR_SUCCESS )
  102. return RegStatus;
  103. #endif
  104. RegStatus = RegSetValueEx(
  105. hActKey,
  106. TEXT("LaunchPermission"),
  107. 0,
  108. REG_BINARY,
  109. SecurityDescriptor,
  110. dwSize );
  111. if ( RegStatus != ERROR_SUCCESS )
  112. return RegStatus;
  113. RegStatus = RegSetValueEx(
  114. hActKey,
  115. TEXT("AccessPermission"),
  116. 0,
  117. REG_BINARY,
  118. SecurityDescriptor,
  119. dwSize );
  120. if ( RegStatus != ERROR_SUCCESS )
  121. return RegStatus;
  122. RegCloseKey(hActKey);
  123. // make the key for the exe
  124. RegStatus = RegCreateKeyEx(
  125. hAppIDKey,
  126. TEXT("ActSrv.Exe"),
  127. 0,
  128. TEXT("REG_SZ"),
  129. REG_OPTION_NON_VOLATILE,
  130. KEY_ALL_ACCESS,
  131. NULL,
  132. &hActKey,
  133. &Disposition );
  134. if ( RegStatus != ERROR_SUCCESS )
  135. return RegStatus;
  136. RegStatus = RegSetValueEx(
  137. hActKey,
  138. TEXT("AppID"),
  139. 0,
  140. REG_SZ,
  141. (BYTE*) pszAppID,
  142. (_tcslen(pszAppID) + 1) * sizeof(TCHAR) );
  143. if ( RegStatus != ERROR_SUCCESS )
  144. return RegStatus;
  145. RegCloseKey(hActKey);
  146. RegCloseKey(hAppIDKey);
  147. return ERROR_SUCCESS;
  148. }
  149. int SetAccountRights(const TCHAR *szUser, TCHAR *szPrivilege)
  150. {
  151. #ifndef CHICO
  152. int err;
  153. LSA_HANDLE hPolicy;
  154. LSA_OBJECT_ATTRIBUTES objAtt;
  155. DWORD cbSid = 1;
  156. TCHAR szDomain[128];
  157. DWORD cbDomain = 128;
  158. PSID pSid = NULL;
  159. SID_NAME_USE snu;
  160. LSA_UNICODE_STRING privStr;
  161. // Get a policy handle
  162. memset(&objAtt, 0, sizeof(LSA_OBJECT_ATTRIBUTES));
  163. if (!NT_SUCCESS(LsaOpenPolicy(NULL,
  164. &objAtt,
  165. POLICY_CREATE_ACCOUNT | POLICY_LOOKUP_NAMES,
  166. &hPolicy)))
  167. {
  168. return GetLastError();
  169. }
  170. // Fetch the SID for the specified user
  171. LookupAccountName(NULL, szUser, pSid, &cbSid, szDomain, &cbDomain, &snu);
  172. if ((err = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)
  173. {
  174. return err;
  175. }
  176. pSid = (PSID*) malloc(cbSid);
  177. if (pSid == NULL)
  178. {
  179. return ERROR_OUTOFMEMORY;
  180. }
  181. if (!LookupAccountName(NULL, szUser, pSid, &cbSid,
  182. szDomain, &cbDomain, &snu))
  183. {
  184. return GetLastError();
  185. }
  186. // Set the specified privilege on this account
  187. privStr.Length = _tcslen(szPrivilege) * sizeof(WCHAR);
  188. privStr.MaximumLength = privStr.Length + sizeof(WCHAR);
  189. privStr.Buffer = szPrivilege;
  190. if (!NT_SUCCESS(LsaAddAccountRights(hPolicy, pSid, &privStr, 1)))
  191. {
  192. return GetLastError();
  193. }
  194. // We're done
  195. free( pSid );
  196. LsaClose(hPolicy);
  197. #endif
  198. return ERROR_SUCCESS;
  199. }
  200. int AddBatchPrivilege(const TCHAR *szUser)
  201. {
  202. #ifndef CHICO
  203. return !SetAccountRights( szUser, SE_BATCH_LOGON_NAME );
  204. #else
  205. return(TRUE);
  206. #endif
  207. }