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.

504 lines
18 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: uninstall.cpp
  4. //
  5. // Module: CMSTP.EXE
  6. //
  7. // Synopsis: This source file contains most of the code to uninstall CM profiles.
  8. //
  9. // Copyright (c) 1997-1999 Microsoft Corporation
  10. //
  11. // Author: quintinb Created 07/14/98
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include "cmmaster.h"
  15. const TCHAR* const c_pszCmPath = TEXT("%s\\SOFTWARE\\Microsoft\\Connection Manager");
  16. const TCHAR* const c_pszUserInfoPath = TEXT("%s\\SOFTWARE\\Microsoft\\Connection Manager\\UserInfo");
  17. const TCHAR* const c_pszProfileUserInfoPath = TEXT("%s\\SOFTWARE\\Microsoft\\Connection Manager\\UserInfo\\%s");
  18. //+----------------------------------------------------------------------------
  19. //
  20. // Function: PromptUserToUninstallProfile
  21. //
  22. // Synopsis: This function prompts the user to see if they wish to uninstall
  23. // the given profile.
  24. //
  25. // Arguments: HINSTANCE hInstance - Instance Handle to get resources with
  26. // LPCTSTR pszInfFile - full path to the profile inf file
  27. //
  28. // Returns: int - Return Value of the MessageBox prompt, IDNO signifies an
  29. // error or that the user didn't want to continue. IDYES
  30. // signifies that the install should continue.
  31. //
  32. // History: quintinb Created 6/28/99
  33. //
  34. //+----------------------------------------------------------------------------
  35. BOOL PromptUserToUninstallProfile(HINSTANCE hInstance, LPCTSTR pszInfFile)
  36. {
  37. BOOL bReturn = FALSE;
  38. //
  39. // On Legacy Platforms we need to prompt to see if the user wants to uninstall.
  40. // On NT5 this is taken care of by the connections folder. We also use no prompt
  41. // when uninstalling the old profile for a same name upgrade.
  42. //
  43. TCHAR szServiceName[MAX_PATH+1];
  44. TCHAR szMessage[MAX_PATH+1];
  45. TCHAR szTemp[MAX_PATH+1];
  46. TCHAR szTitle[MAX_PATH+1] = {TEXT("")};
  47. MYVERIFY(0 != LoadString(hInstance, IDS_CMSTP_TITLE, szTitle, MAX_PATH));
  48. MYDBGASSERT(TEXT('\0') != szTitle[0]);
  49. MYVERIFY(0 != GetPrivateProfileString(c_pszInfSectionStrings, c_pszCmEntryServiceName,
  50. TEXT(""), szServiceName, MAX_PATH, pszInfFile));
  51. if(TEXT('\0') != szServiceName[0])
  52. {
  53. MYVERIFY(0 != LoadString(hInstance, IDS_UNINSTALL_PROMPT, szTemp, MAX_PATH));
  54. MYDBGASSERT(TEXT('\0') != szTemp[0]);
  55. MYVERIFY(CELEMS(szMessage) > (UINT)wsprintf(szMessage, szTemp, szServiceName));
  56. bReturn = (IDYES == MessageBox(NULL, szMessage, szTitle, MB_YESNO | MB_DEFBUTTON2));
  57. }
  58. else
  59. {
  60. CMASSERTMSG(FALSE, TEXT("PromptUserToUninstallProfile: Failed to retrieve ServiceName from INF"));
  61. }
  62. return bReturn;
  63. }
  64. //+----------------------------------------------------------------------------
  65. //
  66. // Function: BuildUninstallDirKey
  67. //
  68. // Synopsis: Utility function to expand any environment strings in the passed
  69. // in Mappings Data value and then parse that path into the Install
  70. // dir value (basically remove the \<short service name>.cmp from
  71. // the full path to the cmp)
  72. //
  73. // Arguments: LPCTSTR pszMappingsData - Raw data from the mappings key, may
  74. // contain environment strings.
  75. // LPTSTR szInstallDir - Out buffer for the install dir
  76. //
  77. // Returns: Nothing
  78. //
  79. // History: quintinb Created Header 6/28/99
  80. //
  81. //+----------------------------------------------------------------------------
  82. void BuildUninstallDirKey(LPCTSTR pszMappingsData, LPTSTR szInstallDir)
  83. {
  84. TCHAR szCmp[MAX_PATH+1];
  85. ExpandEnvironmentStrings(pszMappingsData, szCmp, CELEMS(szCmp));
  86. CFileNameParts CmpParts(szCmp);
  87. wsprintf(szInstallDir, TEXT("%s%s"), CmpParts.m_Drive, CmpParts.m_Dir);
  88. if (TEXT('\\') == szInstallDir[lstrlen(szInstallDir) - 1])
  89. {
  90. szInstallDir[lstrlen(szInstallDir) - 1] = TEXT('\0');
  91. }
  92. }
  93. //+----------------------------------------------------------------------------
  94. //
  95. // Function: UninstallProfile
  96. //
  97. // Synopsis: This function uninstalls a CM profile.
  98. //
  99. // Arguments: HINSTANCE hInstance - Instance handle for resources
  100. // LPCTSTR szInfPath - full path of the INF to uninstall
  101. // BOOL bCleanUpCreds -- whether credential info in the registry
  102. // should be cleaned up or not
  103. //
  104. // Returns: HRESULT -- Standard COM Error Codes
  105. //
  106. // History: Created Header 7/14/98
  107. //
  108. //+----------------------------------------------------------------------------
  109. HRESULT UninstallProfile(HINSTANCE hInstance, LPCTSTR szInfFile, BOOL bCleanUpCreds)
  110. {
  111. TCHAR* pszPhonebook = NULL;
  112. TCHAR szSectionName[MAX_PATH+1];
  113. TCHAR szCmsFile[MAX_PATH+1];
  114. TCHAR szProfileDir[MAX_PATH+1];
  115. TCHAR szInstallDir[MAX_PATH+1];
  116. TCHAR szShortServiceName[MAX_PATH+1];
  117. TCHAR szPhonebookPath[MAX_PATH+1];
  118. TCHAR szTemp[MAX_PATH+1];
  119. TCHAR szLongServiceName[MAX_PATH+1];
  120. CPlatform plat;
  121. HANDLE hFile;
  122. BOOL bReturn = FALSE;
  123. BOOL bAllUserUninstall;
  124. HKEY hBaseKey;
  125. HKEY hKey;
  126. int nDesktopFolder;
  127. int iCmsVersion;
  128. HRESULT hr;
  129. const TCHAR* const c_pszRegGuidMappings = TEXT("SOFTWARE\\Microsoft\\Connection Manager\\Guid Mappings");
  130. ZeroMemory(szCmsFile, sizeof(szCmsFile));
  131. ZeroMemory(szLongServiceName, sizeof(szLongServiceName));
  132. ZeroMemory(szProfileDir, sizeof(szProfileDir));
  133. ZeroMemory(szPhonebookPath, sizeof(szPhonebookPath));
  134. //
  135. // Load the title in case IExpress needs to show error dialogs
  136. //
  137. TCHAR szTitle[MAX_PATH+1] = {TEXT("")};
  138. MYVERIFY(0 != LoadString(hInstance, IDS_CMSTP_TITLE, szTitle, MAX_PATH));
  139. MYDBGASSERT(TEXT('\0') != szTitle[0]);
  140. //
  141. // Get the Long Service Name
  142. //
  143. if (0 == GetPrivateProfileString(c_pszInfSectionStrings, c_pszCmEntryServiceName,
  144. TEXT(""), szLongServiceName, MAX_PATH, szInfFile))
  145. {
  146. CMASSERTMSG(FALSE, TEXT("UninstallProfile -- Unable to get Long Service Name. This situation will occur normally when cmstp.exe /u is called by hand on NT5."));
  147. return E_FAIL;
  148. }
  149. //
  150. // Determine if we are a private user profile or not
  151. //
  152. hr = HrIsCMProfilePrivate(szInfFile);
  153. if (FAILED(hr))
  154. {
  155. CMASSERTMSG(FALSE, TEXT("UninstallProfile: HrIsCMProfilePrivate failed"));
  156. goto exit;
  157. }
  158. else if (S_OK == hr)
  159. {
  160. //
  161. // Then we have a Private Profile, send Remove_Private as the uninstall command
  162. //
  163. lstrcpy(szSectionName, TEXT("Remove_Private"));
  164. //
  165. // All Registry access should be to the HKCU key
  166. //
  167. hBaseKey = HKEY_CURRENT_USER;
  168. //
  169. // Set these just in case we are on NT5 and will need them to remove the
  170. // Desktop and Start Menu shortcuts.
  171. //
  172. nDesktopFolder = CSIDL_DESKTOPDIRECTORY;
  173. //
  174. // We use this to determine if we need a phonebook path or if NULL will work
  175. //
  176. bAllUserUninstall = FALSE;
  177. }
  178. else
  179. {
  180. //
  181. // Then we have an All User profile, so send Remove as the uninstall command
  182. //
  183. lstrcpy(szSectionName, TEXT("Remove"));
  184. //
  185. // All Registry settings will be under HKLM
  186. //
  187. hBaseKey = HKEY_LOCAL_MACHINE;
  188. //
  189. // Set these just in case we are on NT5 and will need them to remove the
  190. // Desktop shortcut.
  191. //
  192. nDesktopFolder = CSIDL_COMMON_DESKTOPDIRECTORY;
  193. //
  194. // We use this to determine if we need a phonebook path or if NULL will work
  195. //
  196. bAllUserUninstall = TRUE;
  197. }
  198. //
  199. // Get the Short Service Name from the INF and use it to build the UninstallDir in the
  200. // registry.
  201. //
  202. MYVERIFY(0 != GetPrivateProfileString(c_pszInfSectionStrings, c_pszShortSvcName,
  203. TEXT(""), szShortServiceName, MAX_PATH, szInfFile));
  204. if (0 != szShortServiceName[0])
  205. {
  206. MYVERIFY(CELEMS(szTemp) > (UINT)wsprintf(szTemp,
  207. TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%s"),
  208. szShortServiceName));
  209. DWORD dwDisposition = 0;
  210. LONG lResult = RegCreateKeyEx(hBaseKey, szTemp, 0, NULL, REG_OPTION_NON_VOLATILE,
  211. KEY_READ | KEY_WRITE, NULL, &hKey, &dwDisposition);
  212. if (ERROR_SUCCESS == lResult)
  213. {
  214. const TCHAR* const c_pszUninstallDir = TEXT("UninstallDir");
  215. DWORD dwType = REG_SZ;
  216. DWORD dwSize;
  217. //
  218. // If this value doesn't exist then the inf will fail, so we need to create it from the
  219. // cmp path value. We used to write it on install, but since we were always expanding
  220. // it and rewriting it anyway (because of single user profiles), it is just easier
  221. // to ignore the existing value and create it from scratch.
  222. //
  223. HKEY hMappingsKey;
  224. lResult = RegOpenKeyEx(hBaseKey, c_pszRegCmMappings, 0, KEY_READ, &hMappingsKey);
  225. if (ERROR_SUCCESS == lResult)
  226. {
  227. dwSize = CELEMS(szTemp);
  228. lResult = RegQueryValueEx(hMappingsKey, szLongServiceName, NULL,
  229. NULL, (LPBYTE)szTemp, &dwSize);
  230. if (ERROR_SUCCESS == lResult)
  231. {
  232. //
  233. // Now build the UninstallDir key
  234. //
  235. BuildUninstallDirKey(szTemp, szInstallDir);
  236. }
  237. else
  238. {
  239. MYVERIFY(ERROR_SUCCESS == RegCloseKey(hKey));
  240. CMASSERTMSG(FALSE, TEXT("UninstallProfile: Unable to find the Profile Entry in Mappings!"));
  241. goto exit;
  242. }
  243. RegCloseKey(hMappingsKey);
  244. }
  245. else
  246. {
  247. MYVERIFY(ERROR_SUCCESS == RegCloseKey(hKey));
  248. CMASSERTMSG(FALSE, TEXT("UninstallProfile: Unable to open the Mappings key!"));
  249. goto exit;
  250. }
  251. //
  252. // We need to write the UninstallDir key to the registry now that we
  253. // have created it. We do this because the inf processing code in advpack
  254. // doesn't understand environment strings and single user strings contain
  255. // the %userprofile% variable, thus we were always rewriting it anyway.
  256. // Note we only write this on install of All User profiles and only because
  257. // we cannot update the bits on win98 OSR1 and on IEAK5 machines (with CMAK).
  258. // Otherwise we don't write this at setup time anymore.
  259. //
  260. dwSize = lstrlen(szInstallDir) + 1;
  261. if (ERROR_SUCCESS != RegSetValueEx(hKey, c_pszUninstallDir, NULL, dwType,
  262. (CONST BYTE *)szInstallDir, dwSize))
  263. {
  264. CMASSERTMSG(FALSE, TEXT("UninstallProfile: Unable to set the UninstallDir key!"));
  265. goto exit;
  266. }
  267. //
  268. // szInstallDir currently contains the directory above the profile directory, add
  269. // the shortservice name on the end
  270. //
  271. UINT uCount = (UINT)wsprintf(szProfileDir, TEXT("%s\\%s"), szInstallDir, szShortServiceName);
  272. MYDBGASSERT(uCount <= CELEMS(szProfileDir));
  273. MYVERIFY(ERROR_SUCCESS == RegCloseKey(hKey));
  274. }
  275. else
  276. {
  277. //
  278. // If this key doesn't exist then the inf will fail, so exit.
  279. //
  280. CMASSERTMSG(FALSE, TEXT("UninstallProfile: Unable to open profile uninstall key."));
  281. goto exit;
  282. }
  283. }
  284. else
  285. {
  286. //
  287. // We shouldn't have a problem getting the short service name from a profile.
  288. // something is definitely wrong here.
  289. //
  290. CMASSERTMSG(FALSE, TEXT("UninstallProfile: Unable to retrieve the ShortServiceName"));
  291. goto exit;
  292. }
  293. //
  294. // Create the path to the cms file
  295. //
  296. MYVERIFY(CELEMS(szCmsFile) > (UINT)wsprintf(szCmsFile, TEXT("%s\\%s.cms"), szProfileDir,
  297. szShortServiceName));
  298. //
  299. // Remove the phonebook entry
  300. //
  301. if (TEXT('\0') != szLongServiceName[0])
  302. {
  303. if (plat.IsAtLeastNT5())
  304. {
  305. //
  306. // On NT5 we want to delete the hidden phonebook entry for
  307. // double dial profiles.
  308. //
  309. if (GetHiddenPhoneBookPath(szInstallDir , &pszPhonebook))
  310. {
  311. MYVERIFY(FALSE != RemovePhonebookEntry(szLongServiceName, pszPhonebook, !(plat.IsAtLeastNT5())));
  312. CmFree(pszPhonebook);
  313. }
  314. if (bAllUserUninstall)
  315. {
  316. //
  317. // On NT5 legacy profiles could be installed anywhere and the
  318. // install dir may not reflect the actual pbk path. Thus if it
  319. // is an all user install we want to force the directory to
  320. // the Cm All Users dir so we get the correct phonebook.
  321. //
  322. MYVERIFY(FALSE != GetAllUsersCmDir(szInstallDir, hInstance));
  323. }
  324. }
  325. if (GetPhoneBookPath(szInstallDir, &pszPhonebook, bAllUserUninstall))
  326. {
  327. //
  328. // Note that usually on NT5 we are called by RasCustomDeleteEntryNotify (in cmdial32.dll)
  329. // throw RasDeleteEntry. Thus we don't really need to delete the entry. However, it is
  330. // possible that someone would call cmstp.exe /u directly and not through the RAS API, thus
  331. // we want to delete the connectoid in that case. Since we are called after RAS has already
  332. // deleted the entry it shouldn't be a problem.
  333. // Note that on NT5 we only remove the exact entry to fix NTRAID 349749
  334. // otherwise we could end up deleting similarly named connectoids
  335. // and lose the users only interface to CM.
  336. //
  337. MYVERIFY(FALSE != RemovePhonebookEntry(szLongServiceName, pszPhonebook, !(plat.IsAtLeastNT5())));
  338. }
  339. CmFree(pszPhonebook);
  340. }
  341. //
  342. // Launch the uninstall INF
  343. //
  344. iCmsVersion = GetPrivateProfileInt(c_pszCmSectionProfileFormat, c_pszVersion,
  345. 0, szCmsFile);
  346. if (1 >= iCmsVersion)
  347. {
  348. //
  349. // Then we have an old 1.0 profile and we should remove the showicon.exe
  350. // postsetup command.
  351. //
  352. RemoveShowIconFromRunPostSetupCommands(szInfFile);
  353. }
  354. bReturn = SUCCEEDED(LaunchInfSection(szInfFile, szSectionName, szTitle, FALSE)); // bQuiet = FALSE
  355. //
  356. // On NT5 we need to delete the desktop shortcut
  357. //
  358. if (plat.IsAtLeastNT5())
  359. {
  360. DeleteNT5ShortcutFromPathAndName(hInstance, szLongServiceName, nDesktopFolder);
  361. }
  362. //
  363. // Finally delete the profile directory. (Not deleted because the inf file resides there.
  364. // The dir can't be removed because the inf file is still in it and in use, unless this
  365. // is a legacy profile). Not that we could cause cmstp to cause an Access Violation
  366. // if we ask it to delete an empty string.
  367. //
  368. if ((TEXT('\0') != szProfileDir[0]) && SetFileAttributes(szProfileDir, FILE_ATTRIBUTE_NORMAL))
  369. {
  370. SHFILEOPSTRUCT fOpStruct;
  371. ZeroMemory(&fOpStruct, sizeof(fOpStruct));
  372. fOpStruct.wFunc = FO_DELETE;
  373. fOpStruct.pFrom = szProfileDir;
  374. fOpStruct.fFlags = FOF_SILENT | FOF_NOCONFIRMATION;
  375. MYVERIFY(ERROR_SUCCESS == SHFileOperation(&fOpStruct));
  376. }
  377. //
  378. // We need to try to delete the following regkeys:
  379. // HKCU\\Software\\Microsoft\\Connection Manager\\<UserInfo/SingleUserInfo>
  380. // HKCU\\Software\\Microsoft\\Connection Manager\\Mappings
  381. // HKLM\\Software\\Microsoft\\Connection Manager\\Mappings
  382. // HKCU\\Software\\Microsoft\\Connection Manager
  383. // HKLM\\Software\\Microsoft\\Connection Manager
  384. //
  385. //
  386. // Registry Cleanup. We want to delete the UserInfo keys if they are empty.
  387. // We then want to delete the mappings keys if they don't contain any more
  388. // values. We also want to delete the CM registry keys if they don't contain
  389. // any subkeys. Also kill the GUID Mappings key (this was beta only but still
  390. // should be deleted). The problem here is that win95 infs delete keys recursively,
  391. // even if they have subkeys. Thus we must use code to safely delete these keys.
  392. // Please note that this does mean we could be unnecessarily deleting the Components
  393. // Checked value in HKLM\\...\\Connection Manager but this can't be helped. I
  394. // would rather take the small startup perf hit than leave the users registry
  395. // dirty.
  396. //
  397. if (bAllUserUninstall)
  398. {
  399. wsprintf(szTemp, TEXT("%s%s"), c_pszRegCmUserInfo, szLongServiceName);
  400. }
  401. else
  402. {
  403. wsprintf(szTemp, TEXT("%s%s"), c_pszRegCmSingleUserInfo, szLongServiceName);
  404. }
  405. //
  406. // Delete the User Data, note that we have to do this programatically because
  407. // of 1.0 profiles that don't know to delete their User Data on uninstall (no commands
  408. // in the 1.0 inf to do so). Note that we don't want to cleanup user data if this
  409. // is a same name upgrade uninstall (uninstall the 1.0 profile before installing the
  410. // new profile).
  411. //
  412. if (bCleanUpCreds)
  413. {
  414. CmDeleteRegKeyWithoutSubKeys(HKEY_CURRENT_USER, szTemp, TRUE);
  415. CmDeleteRegKeyWithoutSubKeys(HKEY_CURRENT_USER, c_pszRegCmUserInfo, TRUE);
  416. CmDeleteRegKeyWithoutSubKeys(HKEY_CURRENT_USER, c_pszRegCmSingleUserInfo, TRUE);
  417. }
  418. CmDeleteRegKeyWithoutSubKeys(HKEY_LOCAL_MACHINE, c_pszRegCmMappings, FALSE);
  419. CmDeleteRegKeyWithoutSubKeys(HKEY_CURRENT_USER, c_pszRegCmMappings, FALSE);
  420. HrRegDeleteKeyTree(HKEY_LOCAL_MACHINE, c_pszRegGuidMappings);
  421. CmDeleteRegKeyWithoutSubKeys(HKEY_LOCAL_MACHINE, c_pszRegCmRoot, TRUE);
  422. CmDeleteRegKeyWithoutSubKeys(HKEY_CURRENT_USER, c_pszRegCmRoot, TRUE);
  423. //
  424. // Refresh the desktop so Desktop GUIDS disappear
  425. //
  426. RefreshDesktop();
  427. exit:
  428. return (bReturn ? S_OK : E_FAIL);
  429. }