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.

623 lines
14 KiB

  1. /*****************************************************************************\
  2. * MODULE: config.cxx
  3. *
  4. * The module contains class to manage connection configurations
  5. *
  6. * Copyright (C) 2000 Microsoft Corporation
  7. *
  8. * History:
  9. * 05/12/00 Weihaic Created
  10. *
  11. \*****************************************************************************/
  12. #include "precomp.h"
  13. #include "priv.h"
  14. #ifdef WINNT32
  15. CPortConfigData::CPortConfigData():
  16. m_bValid (TRUE),
  17. m_pUserName (NULL),
  18. m_pPassword (NULL),
  19. m_dwAuthMethod (AUTH_UNKNOWN),
  20. m_bIgnoreSecurityDlg (FALSE)
  21. {
  22. }
  23. CPortConfigData::CPortConfigData(
  24. LPCTSTR pUserName,
  25. LPCTSTR pPassword):
  26. m_bValid (FALSE),
  27. m_pUserName (NULL),
  28. m_pPassword (NULL),
  29. m_dwAuthMethod (AUTH_UNKNOWN),
  30. m_bIgnoreSecurityDlg (FALSE)
  31. {
  32. if (AssignString (m_pUserName, pUserName) &&
  33. AssignString (m_pPassword, pPassword))
  34. m_bValid = TRUE;
  35. }
  36. CPortConfigData::~CPortConfigData()
  37. {
  38. LocalFree (m_pUserName);
  39. LocalFree (m_pPassword);
  40. }
  41. BOOL
  42. CPortConfigData::SetUserName (
  43. LPCTSTR pUserName)
  44. {
  45. BOOL bRet = FALSE;
  46. if (m_bValid) {
  47. bRet = AssignString(m_pUserName, pUserName);
  48. }
  49. return bRet;
  50. }
  51. BOOL
  52. CPortConfigData::SetPassword (
  53. LPCTSTR pPassword)
  54. {
  55. BOOL bRet = FALSE;
  56. if (m_bValid) {
  57. bRet = AssignString (m_pPassword, pPassword);
  58. }
  59. return bRet;
  60. }
  61. BOOL
  62. CPortConfigData::SetAuthMethod (
  63. DWORD dwAuthMethod)
  64. {
  65. BOOL bRet = FALSE;
  66. if (m_bValid) {
  67. m_dwAuthMethod = dwAuthMethod;
  68. bRet = TRUE;
  69. }
  70. return bRet;
  71. }
  72. extern HKEY
  73. GetClientUserHandle(
  74. IN REGSAM samDesired
  75. );
  76. CPortConfigDataMgr::CPortConfigDataMgr (
  77. LPCTSTR pszPortName):
  78. m_bValid (FALSE),
  79. m_pszPortName (NULL)
  80. {
  81. if (AssignString (m_pszPortName, pszPortName)) {
  82. m_bValid = TRUE;
  83. }
  84. }
  85. /*++
  86. Routine Description:
  87. Delete everyone's personal setting for the http printer
  88. Return Value:
  89. --*/
  90. BOOL
  91. CPortConfigDataMgr::DeleteAllSettings (
  92. VOID)
  93. {
  94. BOOL bRet = FALSE;
  95. WCHAR szKey[MAX_PATH];
  96. DWORD cchKey;
  97. DWORD i;
  98. FILETIME ftLastWriteTime;
  99. DWORD dwError;
  100. static LPWSTR szDotDefault = L".Default";
  101. HANDLE hToken;
  102. if (hToken = RevertToPrinterSelf()) {
  103. //
  104. // Go through all keys and fix them up.
  105. //
  106. for (i=0; TRUE; i++) {
  107. cchKey = COUNTOF(szKey);
  108. dwError = RegEnumKeyEx(HKEY_USERS,
  109. i,
  110. szKey,
  111. &cchKey,
  112. NULL,
  113. NULL,
  114. NULL,
  115. &ftLastWriteTime);
  116. if (dwError != ERROR_SUCCESS)
  117. break;
  118. if (!_wcsicmp(szKey, szDotDefault) || wcschr(szKey, L'_'))
  119. continue;
  120. DeletePerUserSettings (szKey);
  121. }
  122. DeletePerPortSettings ();
  123. bRet = ImpersonatePrinterClient(hToken);
  124. }
  125. return bRet;
  126. }
  127. BOOL
  128. CPortConfigDataMgr::GetPortSettings (
  129. HKEY hkPath,
  130. CPortConfigData* pConfigData) CONST
  131. {
  132. LONG lStat;
  133. HKEY hkPortNames;
  134. HKEY hkThisPortName;
  135. BOOL bRet = FALSE;
  136. DWORD dwType;
  137. DWORD dwSize;
  138. DWORD dwAuthMethod;
  139. PBYTE pbData;
  140. // Open registry key for Provider-Name.
  141. //
  142. bRet = FALSE;
  143. lStat = RegOpenKeyEx(hkPath, g_szRegPorts, 0, KEY_READ, &hkPortNames);
  144. if (lStat == ERROR_SUCCESS) {
  145. lStat = RegOpenKeyEx(hkPortNames, m_pszPortName, 0, KEY_READ, &hkThisPortName);
  146. if (lStat == ERROR_SUCCESS) {
  147. dwType = REG_DWORD;
  148. dwSize = sizeof (dwAuthMethod);
  149. lStat = RegQueryValueEx (hkThisPortName, g_szAuthMethod, 0, &dwType, (LPBYTE) &dwAuthMethod, &dwSize);
  150. if (lStat == ERROR_SUCCESS) {
  151. switch (dwAuthMethod)
  152. {
  153. case AUTH_ANONYMOUS:
  154. case AUTH_NT:
  155. case AUTH_OTHER:
  156. break;
  157. default:
  158. //
  159. // If the register is messed up because of upgrade,
  160. // we set it to anonymous by default.
  161. //
  162. dwAuthMethod = AUTH_ANONYMOUS;
  163. break;
  164. }
  165. pConfigData->SetAuthMethod (dwAuthMethod);
  166. dwType = REG_SZ;
  167. dwSize = 0;
  168. lStat = RegQueryValueEx (hkThisPortName, g_szUserName, 0, &dwType, NULL, &dwSize);
  169. if (lStat == ERROR_SUCCESS) {
  170. pbData = new BYTE [dwSize];
  171. lStat = RegQueryValueEx (hkThisPortName, g_szUserName, 0, &dwType, pbData, &dwSize);
  172. if (lStat == ERROR_SUCCESS) {
  173. pConfigData->SetUserName ( (LPCTSTR) pbData);
  174. }
  175. if (pbData) {
  176. delete [] pbData;
  177. }
  178. }
  179. dwType = REG_BINARY;
  180. dwSize = 0;
  181. lStat = RegQueryValueEx (hkThisPortName, g_szPassword, 0, &dwType, NULL, &dwSize);
  182. if (lStat == ERROR_SUCCESS) {
  183. pbData = new BYTE [dwSize];
  184. lStat = RegQueryValueEx (hkThisPortName, g_szPassword, 0, &dwType, pbData, &dwSize);
  185. if (lStat == ERROR_SUCCESS) {
  186. LPTSTR pPassword;
  187. DWORD dwPasswordSize;
  188. //
  189. // Decrypt the password
  190. //
  191. if (DecryptData (pbData, dwSize, (PBYTE *) &pPassword, &dwPasswordSize))
  192. pConfigData->SetPassword (pPassword);
  193. }
  194. if (pbData) {
  195. delete [] pbData;
  196. }
  197. }
  198. bRet = TRUE;
  199. }
  200. RegCloseKey (hkThisPortName);
  201. } else {
  202. SetLastError(lStat);
  203. }
  204. RegCloseKey(hkPortNames);
  205. } else {
  206. SetLastError(lStat);
  207. }
  208. return bRet;
  209. }
  210. BOOL
  211. CPortConfigDataMgr::SetPortSettings (
  212. HKEY hkPath,
  213. CPortConfigData &ConfigData)
  214. {
  215. LONG lStat;
  216. HKEY hkPortNames;
  217. HKEY hkThisPortName;
  218. BOOL bRet = FALSE;
  219. lStat = RegCreateKeyEx(hkPath,
  220. g_szRegPorts,
  221. 0,
  222. NULL,
  223. 0,
  224. KEY_WRITE,
  225. NULL,
  226. &hkPortNames,
  227. NULL);
  228. if (lStat == ERROR_SUCCESS) {
  229. lStat = RegCreateKeyEx(hkPortNames,
  230. m_pszPortName,
  231. 0,
  232. NULL,
  233. 0,
  234. KEY_WRITE,
  235. NULL,
  236. &hkThisPortName,
  237. NULL);
  238. if (lStat == ERROR_SUCCESS) {
  239. DWORD dwAuthMethod = ConfigData.GetAuthMethod ();
  240. lStat = RegSetValueEx(hkThisPortName,
  241. g_szAuthMethod,
  242. 0,
  243. REG_DWORD,
  244. (LPBYTE)&dwAuthMethod,
  245. sizeof (dwAuthMethod));
  246. bRet = (lStat == ERROR_SUCCESS ? TRUE : FALSE);
  247. if (lStat == ERROR_SUCCESS) {
  248. LPCTSTR pUserName = ConfigData.GetUserName ();
  249. LPCTSTR pPassword = ConfigData.GetPassword ();
  250. if (pUserName) {
  251. lStat = RegSetValueEx(hkThisPortName,
  252. g_szUserName,
  253. 0,
  254. REG_SZ,
  255. (LPBYTE) pUserName,
  256. sizeof (TCHAR) * (1 + lstrlen (pUserName)));
  257. } else
  258. lStat = RegDeleteValue (hkThisPortName, g_szUserName);
  259. if (pPassword) {
  260. LPBYTE pEncryptedPassword;
  261. DWORD dwEncryptedSize;
  262. if (EncryptData ((PBYTE) pPassword,
  263. sizeof (TCHAR) * (lstrlen (pPassword) + 1),
  264. &pEncryptedPassword,
  265. &dwEncryptedSize)) {
  266. lStat = RegSetValueEx(hkThisPortName,
  267. g_szPassword,
  268. 0,
  269. REG_BINARY,
  270. pEncryptedPassword,
  271. dwEncryptedSize);
  272. }
  273. else
  274. lStat = GetLastError ();
  275. } else
  276. lStat = RegDeleteValue (hkThisPortName, g_szPassword);
  277. bRet = TRUE;
  278. }
  279. RegCloseKey(hkThisPortName);
  280. }
  281. RegCloseKey(hkPortNames);
  282. }
  283. return bRet;
  284. }
  285. BOOL
  286. CPortConfigDataMgr::SetPerPortSettings (
  287. CPortConfigData &ConfigData)
  288. {
  289. LONG lStat;
  290. HKEY hkPath;
  291. BOOL bRet = FALSE;
  292. HANDLE hToken;
  293. if (hToken = RevertToPrinterSelf()) {
  294. lStat = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  295. g_szRegProvider,
  296. 0,
  297. NULL,
  298. 0,
  299. KEY_WRITE,
  300. NULL,
  301. &hkPath,
  302. NULL);
  303. if (lStat == ERROR_SUCCESS) {
  304. bRet = SetPortSettings (hkPath, ConfigData);
  305. RegCloseKey (hkPath);
  306. }
  307. if (!ImpersonatePrinterClient(hToken))
  308. bRet = FALSE;
  309. }
  310. return bRet;
  311. }
  312. BOOL
  313. CPortConfigDataMgr::SetPerUserSettings (
  314. CPortConfigData &ConfigData)
  315. {
  316. LONG lStat;
  317. HKEY hkPath;
  318. HKEY hkPortNames;
  319. HKEY hkCurUser;
  320. BOOL bRet = FALSE;
  321. hkCurUser = GetClientUserHandle (KEY_WRITE);
  322. if (hkCurUser) {
  323. lStat = RegCreateKeyEx(hkCurUser,
  324. g_szPerUserPath,
  325. 0,
  326. NULL,
  327. 0,
  328. KEY_WRITE,
  329. NULL,
  330. &hkPath,
  331. NULL);
  332. if (lStat == ERROR_SUCCESS) {
  333. bRet = SetPortSettings (hkPath, ConfigData);
  334. RegCloseKey (hkPath);
  335. }
  336. }
  337. RegCloseKey (hkCurUser);
  338. return bRet;
  339. }
  340. BOOL
  341. CPortConfigDataMgr::GetPerPortSettings (
  342. CPortConfigData* pConfigData) CONST
  343. {
  344. LONG lStat;
  345. HKEY hkPath;
  346. BOOL bRet = FALSE;
  347. HANDLE hToken;
  348. if (hToken = RevertToPrinterSelf()) {
  349. lStat = RegOpenKeyEx(HKEY_LOCAL_MACHINE, g_szRegProvider, 0, KEY_READ, &hkPath);
  350. if (lStat == ERROR_SUCCESS) {
  351. bRet = GetPortSettings (hkPath, pConfigData);
  352. RegCloseKey (hkPath);
  353. }
  354. if (!ImpersonatePrinterClient(hToken))
  355. bRet = FALSE;
  356. }
  357. return bRet;
  358. }
  359. BOOL
  360. CPortConfigDataMgr::GetPerUserSettings (
  361. CPortConfigData* pConfigData) CONST
  362. {
  363. LONG lStat;
  364. HKEY hkPath;
  365. HKEY hkCurUser;
  366. BOOL bRet = FALSE;
  367. hkCurUser = GetClientUserHandle (KEY_WRITE);
  368. if (hkCurUser) {
  369. lStat = RegOpenKeyEx(hkCurUser, g_szPerUserPath, 0, KEY_READ, &hkPath);
  370. if (lStat == ERROR_SUCCESS) {
  371. bRet = GetPortSettings (hkPath, pConfigData);
  372. RegCloseKey (hkPath);
  373. }
  374. }
  375. RegCloseKey (hkCurUser);
  376. return bRet;
  377. }
  378. BOOL
  379. CPortConfigDataMgr::GetCurrentSettings (
  380. CPortConfigData* pConfigData) CONST
  381. {
  382. BOOL bRet = FALSE;
  383. bRet = GetPerUserSettings (pConfigData);
  384. if (!bRet) {
  385. bRet = GetPerPortSettings (pConfigData);
  386. }
  387. return bRet;
  388. }
  389. BOOL
  390. CPortConfigDataMgr::DeleteSettings (
  391. HKEY hkPath)
  392. {
  393. LONG lStat;
  394. HKEY hkPortNames;
  395. BOOL bRet = FALSE;
  396. lStat = RegOpenKeyEx(hkPath,
  397. g_szRegPorts,
  398. 0,
  399. KEY_ALL_ACCESS,
  400. &hkPortNames);
  401. if (lStat == ERROR_SUCCESS) {
  402. lStat = RegDeleteKey(hkPortNames,
  403. m_pszPortName);
  404. bRet = (lStat == ERROR_SUCCESS ? TRUE : FALSE);
  405. } else {
  406. DBG_MSG(DBG_LEV_ERROR, (TEXT("RegOpenKeyEx (%s) failed: Error = %d"), g_szRegPorts, lStat));
  407. }
  408. return bRet;
  409. }
  410. BOOL
  411. CPortConfigDataMgr::DeletePerPortSettings (
  412. VOID)
  413. {
  414. LONG lStat;
  415. HKEY hkPath;
  416. HKEY hkPortNames;
  417. BOOL bRet = FALSE;
  418. lStat = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  419. g_szRegProvider,
  420. 0,
  421. KEY_ALL_ACCESS,
  422. &hkPath);
  423. if (lStat == ERROR_SUCCESS) {
  424. bRet = DeleteSettings (hkPath);
  425. RegCloseKey(hkPath);
  426. } else {
  427. DBG_MSG(DBG_LEV_ERROR, (TEXT("RegOpenKeyEx (%s) failed: Error = %d"), g_szRegProvider, lStat));
  428. }
  429. return bRet;
  430. }
  431. BOOL
  432. CPortConfigDataMgr::DeletePerUserSettings (
  433. LPTSTR pUser)
  434. {
  435. LONG lStat;
  436. HKEY hkPath;
  437. HKEY hkCurUser;
  438. BOOL bRet = FALSE;
  439. lStat = RegOpenKeyEx( HKEY_USERS, pUser, 0,KEY_ALL_ACCESS, &hkCurUser);
  440. if (lStat == ERROR_SUCCESS) {
  441. lStat = RegOpenKeyEx(hkCurUser, g_szPerUserPath, 0, KEY_ALL_ACCESS, &hkPath);
  442. if (lStat == ERROR_SUCCESS) {
  443. bRet = DeleteSettings (hkPath);
  444. RegCloseKey (hkPath);
  445. }
  446. RegCloseKey (hkCurUser);
  447. }
  448. return bRet;
  449. }
  450. #endif