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.

355 lines
8.5 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // File: tlsbkupc.cpp
  6. //
  7. // Contents:
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #include "pch.cpp"
  13. #include "locks.h"
  14. #include "tlsbkup.h"
  15. #define LSERVER_REGISTRY_BASE _TEXT("SYSTEM\\CurrentControlSet\\Services\\")
  16. #define LSERVER_PARAMETERS _TEXT("Parameters")
  17. #define LSERVER_PARAMETERS_DBPATH _TEXT("DBPath") // database file
  18. #define LSERVER_PARAMETERS_DBFILE _TEXT("DBFile") // database file
  19. #define SZSERVICENAME _TEXT("TermServLicensing")
  20. #define LSERVER_DEFAULT_DBPATH _TEXT("%SYSTEMROOT%\\SYSTEM32\\LSERVER\\")
  21. #define LSERVER_DEFAULT_EDB _TEXT("TLSLic.edb")
  22. #define TLSBACKUP_EXPORT_DIR _TEXT("Export")
  23. TCHAR g_szDatabaseDir[MAX_PATH+1];
  24. TCHAR g_szDatabaseFile[MAX_PATH+1];
  25. TCHAR g_szExportedDb[MAX_PATH+1];
  26. TCHAR g_szDatabaseFname[MAX_PATH+1];
  27. CCriticalSection g_ImportExportLock;
  28. DWORD GetDatabasePaths()
  29. {
  30. DWORD Status;
  31. HKEY hKey = NULL;
  32. DWORD dwBuffer;
  33. TCHAR szDbPath[MAX_PATH+1];;
  34. //-------------------------------------------------------------------
  35. //
  36. // Open HKLM\system\currentcontrolset\sevices\termservlicensing\parameters
  37. //
  38. //-------------------------------------------------------------------
  39. Status = RegCreateKeyEx(
  40. HKEY_LOCAL_MACHINE,
  41. LSERVER_REGISTRY_BASE SZSERVICENAME _TEXT("\\") LSERVER_PARAMETERS,
  42. 0,
  43. NULL,
  44. REG_OPTION_NON_VOLATILE,
  45. KEY_ALL_ACCESS,
  46. NULL,
  47. &hKey,
  48. NULL
  49. );
  50. if(Status != ERROR_SUCCESS)
  51. {
  52. return Status;
  53. }
  54. //-------------------------------------------------------------------
  55. //
  56. // Get database file location and file name
  57. //
  58. //-------------------------------------------------------------------
  59. dwBuffer = sizeof(szDbPath) / sizeof(szDbPath[0]);
  60. Status = RegQueryValueEx(
  61. hKey,
  62. LSERVER_PARAMETERS_DBPATH,
  63. NULL,
  64. NULL,
  65. (LPBYTE)szDbPath,
  66. &dwBuffer
  67. );
  68. if(Status != ERROR_SUCCESS)
  69. {
  70. //
  71. // use default value,
  72. //
  73. _tcscpy(
  74. szDbPath,
  75. LSERVER_DEFAULT_DBPATH
  76. );
  77. }
  78. //
  79. // Get database file name
  80. //
  81. dwBuffer = sizeof(g_szDatabaseFname) / sizeof(g_szDatabaseFname[0]);
  82. Status = RegQueryValueEx(
  83. hKey,
  84. LSERVER_PARAMETERS_DBFILE,
  85. NULL,
  86. NULL,
  87. (LPBYTE)g_szDatabaseFname,
  88. &dwBuffer
  89. );
  90. if(Status != ERROR_SUCCESS)
  91. {
  92. //
  93. // Use default value.
  94. //
  95. _tcscpy(
  96. g_szDatabaseFname,
  97. LSERVER_DEFAULT_EDB
  98. );
  99. }
  100. RegCloseKey(hKey);
  101. //
  102. // Always expand DB Path.
  103. //
  104. Status = ExpandEnvironmentStrings(
  105. szDbPath,
  106. g_szDatabaseDir,
  107. sizeof(g_szDatabaseDir) / sizeof(g_szDatabaseDir[0])
  108. );
  109. if(Status == 0)
  110. {
  111. // can't expand environment variable, error out.
  112. return GetLastError();
  113. }
  114. Status = 0;
  115. if(g_szDatabaseDir[_tcslen(g_szDatabaseDir) - 1] != _TEXT('\\'))
  116. {
  117. // JetBlue needs this.
  118. _tcscat(g_szDatabaseDir, _TEXT("\\"));
  119. }
  120. //
  121. // Full path to database file
  122. //
  123. _tcscpy(g_szDatabaseFile, g_szDatabaseDir);
  124. _tcscat(g_szDatabaseFile, g_szDatabaseFname);
  125. _tcscpy(g_szExportedDb,g_szDatabaseDir);
  126. _tcscat(g_szExportedDb,TLSBACKUP_EXPORT_DIR);
  127. return Status;
  128. }
  129. HRESULT WINAPI
  130. I_ExportTlsDatabaseC()
  131. {
  132. DWORD dwRet = 0;
  133. LPTSTR pszStringBinding;
  134. TCHAR pComputer[MAX_COMPUTERNAME_LENGTH + 1];
  135. DWORD dwSize = sizeof(pComputer) / sizeof(TCHAR);
  136. RPC_STATUS Status = RPC_S_OK;
  137. if (!GetComputerName(pComputer,&dwSize))
  138. {
  139. return GetLastError();
  140. }
  141. Status = RpcStringBindingCompose(NULL,TEXT("ncalrpc"),pComputer,NULL,NULL,&pszStringBinding);
  142. if (Status)
  143. {
  144. return Status;
  145. }
  146. Status = RpcBindingFromStringBinding(pszStringBinding,
  147. &TermServLicensingBackup_IfHandle);
  148. if (Status)
  149. {
  150. RpcStringFree(&pszStringBinding);
  151. goto TryCopyFile;
  152. }
  153. RpcTryExcept {
  154. dwRet = ExportTlsDatabase();
  155. }
  156. RpcExcept (I_RpcExceptionFilter(RpcExceptionCode())) {
  157. Status = RpcExceptionCode();
  158. }
  159. RpcEndExcept;
  160. RpcStringFree(&pszStringBinding);
  161. RpcBindingFree(&TermServLicensingBackup_IfHandle);
  162. #if DBG
  163. {
  164. char szStatusCode[256];
  165. sprintf( szStatusCode, "I_ExportTlsDatabaseC() returns %d\n", Status );
  166. OutputDebugStringA( szStatusCode );
  167. }
  168. #endif
  169. //
  170. // Only actually touch file when server is not available
  171. //
  172. if ( RPC_S_OK == Status)
  173. {
  174. return dwRet;
  175. }
  176. TryCopyFile:
  177. Status = GetDatabasePaths();
  178. if (Status != 0)
  179. {
  180. return Status;
  181. }
  182. CreateDirectoryEx(g_szDatabaseDir,
  183. g_szExportedDb,
  184. NULL); // Ignore errors, they'll show up in CopyFile
  185. _tcscat(g_szExportedDb, _TEXT("\\"));
  186. _tcscat(g_szExportedDb,g_szDatabaseFname);
  187. // Copy database file
  188. if (!CopyFile(g_szDatabaseFile,g_szExportedDb,FALSE))
  189. {
  190. return GetLastError();
  191. }
  192. return 0; // Success
  193. }
  194. HRESULT WINAPI
  195. ExportTlsDatabaseC()
  196. {
  197. // avoid compiler error C2712
  198. // no need for multi-process save.
  199. CCriticalSectionLocker lock( g_ImportExportLock );
  200. return I_ExportTlsDatabaseC();
  201. }
  202. HRESULT WINAPI
  203. I_ImportTlsDatabaseC()
  204. {
  205. DWORD dwRet = 0;
  206. LPTSTR pszStringBinding;
  207. TCHAR pComputer[MAX_COMPUTERNAME_LENGTH + 1];
  208. DWORD dwSize = sizeof(pComputer) / sizeof(TCHAR);
  209. RPC_STATUS Status = RPC_S_OK;
  210. HANDLE hFile;
  211. SYSTEMTIME systime;
  212. FILETIME ft;
  213. if (!GetComputerName(pComputer,&dwSize))
  214. {
  215. return GetLastError();
  216. }
  217. Status = RpcStringBindingCompose(NULL,TEXT("ncalrpc"),pComputer,NULL,NULL,&pszStringBinding);
  218. if (Status)
  219. {
  220. return Status;
  221. }
  222. Status = RpcBindingFromStringBinding(pszStringBinding,
  223. &TermServLicensingBackup_IfHandle);
  224. if (Status)
  225. {
  226. RpcStringFree(&pszStringBinding);
  227. goto TouchFile;
  228. }
  229. RpcTryExcept {
  230. dwRet = ImportTlsDatabase();
  231. }
  232. RpcExcept (I_RpcExceptionFilter(RpcExceptionCode())) {
  233. Status = RpcExceptionCode();
  234. }
  235. RpcEndExcept;
  236. RpcStringFree(&pszStringBinding);
  237. RpcBindingFree(&TermServLicensingBackup_IfHandle);
  238. #if DBG
  239. {
  240. char szStatusCode[256];
  241. sprintf( szStatusCode, "I_ImportTlsDatabaseC() returns %d\n", Status );
  242. OutputDebugStringA( szStatusCode );
  243. }
  244. #endif
  245. //
  246. // Only actually touch file when server is not available
  247. //
  248. if ( RPC_S_OK == Status )
  249. {
  250. return(dwRet);
  251. }
  252. TouchFile:
  253. Status = GetDatabasePaths();
  254. if (Status != 0)
  255. {
  256. return Status;
  257. }
  258. _tcscat(g_szExportedDb, _TEXT("\\"));
  259. _tcscat(g_szExportedDb,g_szDatabaseFname);
  260. GetSystemTime(&systime);
  261. if (!SystemTimeToFileTime(&systime,&ft))
  262. {
  263. return GetLastError();
  264. }
  265. hFile = CreateFile(g_szExportedDb,
  266. GENERIC_WRITE,
  267. 0,
  268. NULL,
  269. OPEN_EXISTING,
  270. FILE_ATTRIBUTE_NORMAL,
  271. NULL);
  272. if (hFile == INVALID_HANDLE_VALUE)
  273. {
  274. return GetLastError();
  275. }
  276. if (!SetFileTime(hFile,
  277. NULL, // Creation time
  278. NULL, // Last access time
  279. &ft)) // Last write time
  280. {
  281. CloseHandle(hFile);
  282. return GetLastError();
  283. }
  284. CloseHandle(hFile);
  285. return 0; // Success
  286. }
  287. HRESULT WINAPI
  288. ImportTlsDatabaseC()
  289. {
  290. // avoid compiler error C2712
  291. // no need for multi-process save.
  292. CCriticalSectionLocker lock( g_ImportExportLock );
  293. return I_ImportTlsDatabaseC();
  294. }