registry.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. // ERROR.C
  42. //
  43. // This file contains the functions needed to install the httpd server.
  44. // They are as follows.
  45. //
  46. // getreg.c
  47. //
  48. // This file has the function needed to get a particular value of a key from the registry...
  49. // 1/16/95 aruna
  50. //
  51. #include <windows.h>
  52. #include "nt/ntos.h"
  53. BOOL NS_WINAPI
  54. REG_CheckIfKeyExists( HKEY hKey, LPCTSTR key )
  55. {
  56. HKEY hQueryKey;
  57. if (RegOpenKeyEx(hKey, key, 0, KEY_ALL_ACCESS,
  58. &hQueryKey) != ERROR_SUCCESS) {
  59. return FALSE;
  60. }
  61. RegCloseKey(hQueryKey);
  62. return TRUE;
  63. }
  64. BOOL NS_WINAPI
  65. REG_GetRegistryParameter(
  66. HKEY hKey,
  67. LPCTSTR registryKey,
  68. LPTSTR QueryValueName,
  69. LPDWORD ValueType,
  70. LPBYTE ValueBuffer,
  71. LPDWORD ValueBufferSize
  72. )
  73. {
  74. HKEY hQueryKey;
  75. if (RegOpenKeyEx(hKey, registryKey, 0, KEY_ALL_ACCESS,
  76. &hQueryKey) != ERROR_SUCCESS) {
  77. return FALSE;
  78. }
  79. if (RegQueryValueEx(hQueryKey, QueryValueName, 0,
  80. ValueType, ValueBuffer, ValueBufferSize) != ERROR_SUCCESS) {
  81. RegCloseKey(hQueryKey);
  82. return FALSE;
  83. }
  84. RegCloseKey(hQueryKey);
  85. return TRUE;
  86. }
  87. BOOL NS_WINAPI
  88. REG_CreateKey( HKEY hKey, LPCTSTR registryKey )
  89. {
  90. HKEY hNewKey;
  91. if ( RegCreateKey (hKey, registryKey, &hNewKey) != ERROR_SUCCESS) {
  92. return FALSE;
  93. }
  94. RegCloseKey(hNewKey);
  95. return TRUE;
  96. }
  97. BOOL NS_WINAPI
  98. REG_DeleteKey( HKEY hKey, LPCTSTR registryKey )
  99. {
  100. HKEY hQueryKey;
  101. DWORD dwNumberOfSubKeys;
  102. char registrySubKey[256];
  103. DWORD i;
  104. /* if key does not exist, then consider it deleted */
  105. if ( !REG_CheckIfKeyExists( hKey, registryKey ) )
  106. return TRUE;
  107. if ( !REG_GetSubKeysInfo( hKey, registryKey, &dwNumberOfSubKeys, NULL ) )
  108. return FALSE;
  109. if ( dwNumberOfSubKeys ) {
  110. if (RegOpenKeyEx(hKey, registryKey, 0, KEY_ALL_ACCESS,
  111. &hQueryKey) != ERROR_SUCCESS) {
  112. return FALSE;
  113. }
  114. // loop through all sub keys and delete the subkeys (recursion)
  115. for ( i=0; i<dwNumberOfSubKeys; i++ ) {
  116. if ( RegEnumKey( hQueryKey, 0, registrySubKey, sizeof(registrySubKey) ) != ERROR_SUCCESS) {
  117. RegCloseKey(hQueryKey);
  118. return FALSE;
  119. }
  120. if ( !REG_DeleteKey( hQueryKey, registrySubKey ) ) {
  121. RegCloseKey(hQueryKey);
  122. return FALSE;
  123. }
  124. }
  125. RegCloseKey(hQueryKey);
  126. }
  127. if ( RegDeleteKey (hKey, registryKey) != ERROR_SUCCESS) {
  128. return FALSE;
  129. }
  130. return TRUE;
  131. }
  132. BOOL NS_WINAPI
  133. REG_GetSubKey( HKEY hKey, LPCTSTR registryKey, DWORD nSubKeyIndex, LPTSTR registrySubKeyBuffer, DWORD subKeyBufferSize )
  134. {
  135. HKEY hQueryKey;
  136. if (RegOpenKeyEx(hKey, registryKey, 0, KEY_ALL_ACCESS,
  137. &hQueryKey) != ERROR_SUCCESS) {
  138. return FALSE;
  139. }
  140. if ( RegEnumKey( hQueryKey, nSubKeyIndex, registrySubKeyBuffer, subKeyBufferSize ) != ERROR_SUCCESS) {
  141. RegCloseKey(hQueryKey);
  142. return FALSE;
  143. }
  144. RegCloseKey(hQueryKey);
  145. return TRUE;
  146. }
  147. BOOL NS_WINAPI
  148. REG_GetSubKeysInfo( HKEY hKey, LPCTSTR registryKey, LPDWORD lpdwNumberOfSubKeys, LPDWORD lpdwMaxSubKeyLength )
  149. {
  150. HKEY hQueryKey;
  151. char szClass[256]; // address of buffer for class string
  152. DWORD cchClass; // address of size of class string buffer
  153. DWORD cSubKeys; // address of buffer for number of subkeys
  154. DWORD cchMaxSubkey; // address of buffer for longest subkey name length
  155. DWORD cchMaxClass; // address of buffer for longest class string length
  156. DWORD cValues; // address of buffer for number of value entries
  157. DWORD cchMaxValueName; // address of buffer for longest value name length
  158. DWORD cbMaxValueData; // address of buffer for longest value data length
  159. DWORD cbSecurityDescriptor; // address of buffer for security descriptor length
  160. FILETIME ftLastWriteTime; // address of buffer for last write time
  161. if (RegOpenKeyEx(hKey, registryKey, 0, KEY_ALL_ACCESS,
  162. &hQueryKey) != ERROR_SUCCESS) {
  163. return FALSE;
  164. }
  165. if ( RegQueryInfoKey( hQueryKey, // handle of key to query
  166. (char*)&szClass, // address of buffer for class string
  167. &cchClass, // address of size of class string buffer
  168. NULL, // reserved
  169. &cSubKeys, // address of buffer for number of subkeys
  170. &cchMaxSubkey, // address of buffer for longest subkey name length
  171. &cchMaxClass, // address of buffer for longest class string length
  172. &cValues, // address of buffer for number of value entries
  173. &cchMaxValueName, // address of buffer for longest value name length
  174. &cbMaxValueData, // address of buffer for longest value data length
  175. &cbSecurityDescriptor, // address of buffer for security descriptor length
  176. &ftLastWriteTime // address of buffer for last write time
  177. ) != ERROR_SUCCESS) {
  178. RegCloseKey(hQueryKey);
  179. return FALSE;
  180. }
  181. // return desired information
  182. if ( lpdwNumberOfSubKeys )
  183. *lpdwNumberOfSubKeys = cSubKeys;
  184. if ( lpdwMaxSubKeyLength )
  185. *lpdwMaxSubKeyLength = cchMaxSubkey;
  186. RegCloseKey(hQueryKey);
  187. return TRUE;
  188. }
  189. BOOL NS_WINAPI
  190. REG_SetRegistryParameter(
  191. HKEY hKey,
  192. LPCTSTR registryKey,
  193. LPTSTR valueName,
  194. DWORD valueType,
  195. LPCTSTR ValueString,
  196. DWORD valueStringLength
  197. )
  198. {
  199. HKEY hQueryKey;
  200. if (RegOpenKeyEx(hKey, registryKey, 0, KEY_ALL_ACCESS,
  201. &hQueryKey) != ERROR_SUCCESS) {
  202. return FALSE;
  203. }
  204. if ( RegSetValueEx( hQueryKey, valueName, 0, valueType, (CONST BYTE *)ValueString,
  205. valueStringLength ) != ERROR_SUCCESS) {
  206. RegCloseKey(hQueryKey);
  207. return FALSE;
  208. }
  209. RegCloseKey(hQueryKey);
  210. return TRUE;
  211. }
  212. BOOL NS_WINAPI
  213. REG_DeleteValue( HKEY hKey, LPCTSTR registryKey, LPCSTR valueName )
  214. {
  215. HKEY hQueryKey;
  216. DWORD ValueBufferSize = 256;
  217. char ValueBuffer[256];
  218. DWORD i, ValueType;
  219. /* if key does not exist, then consider it deleted */
  220. if (RegOpenKeyEx(hKey, registryKey, 0, KEY_ALL_ACCESS,
  221. &hQueryKey) != ERROR_SUCCESS) {
  222. return FALSE;
  223. }
  224. /* if valuename does not exist, then consider it deleted */
  225. if (RegQueryValueEx(hQueryKey, valueName, 0,
  226. &ValueType, ValueBuffer, &ValueBufferSize) != ERROR_SUCCESS) {
  227. RegCloseKey(hQueryKey);
  228. return TRUE;
  229. }
  230. if (RegDeleteValue(hQueryKey, valueName) != ERROR_SUCCESS) {
  231. RegCloseKey(hQueryKey);
  232. return FALSE;
  233. }
  234. RegCloseKey(hQueryKey);
  235. return TRUE;
  236. }