Leaked source code of windows server 2003
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.

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