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.

378 lines
11 KiB

  1. /*++ BUILD Version: 0000 // Increment this if a change has global effects
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. setup_netdde.c
  5. Abstract:
  6. This is used by syssetup to enable netdde. It's generated from various files under
  7. windows\netdde. Do not edit by hand.
  8. Revision History:
  9. --*/
  10. #ifndef H__shrtrust
  11. #define H__shrtrust
  12. /*
  13. NetDDE will fill in the following structure and pass it to NetDDE
  14. Agent whenever it wants to have an app started in the user's
  15. context. The reason for the sharename and modifyId is to that
  16. a user must explicitly permit NetDDE to start an app on behalf of
  17. other users.
  18. */
  19. #define NDDEAGT_CMD_REV 1
  20. #define NDDEAGT_CMD_MAGIC 0xDDE1DDE1
  21. /* commands */
  22. #define NDDEAGT_CMD_WINEXEC 0x1
  23. #define NDDEAGT_CMD_WININIT 0x2
  24. /* return status */
  25. #define NDDEAGT_START_NO 0x0
  26. #define NDDEAGT_INIT_NO 0x0
  27. #define NDDEAGT_INIT_OK 0x1
  28. typedef struct {
  29. DWORD dwMagic; // must be NDDEAGT_CMD_MAGIC
  30. DWORD dwRev; // must be 1
  31. DWORD dwCmd; // one of above NDDEAGT_CMD_*
  32. DWORD qwModifyId[2]; // modify Id of the share
  33. UINT fuCmdShow; // fuCmdShow to use with WinExec()
  34. char szData[1]; // sharename\0 cmdline\0
  35. } NDDEAGTCMD;
  36. typedef NDDEAGTCMD *PNDDEAGTCMD;
  37. #define DDE_SHARE_KEY_MAX 512
  38. #define TRUSTED_SHARES_KEY_MAX 512
  39. #define TRUSTED_SHARES_KEY_SIZE 15
  40. #define KEY_MODIFY_ID_SIZE 8
  41. #define DDE_SHARES_KEY_A "SOFTWARE\\Microsoft\\NetDDE\\DDE Shares"
  42. #define TRUSTED_SHARES_KEY_A "SOFTWARE\\Microsoft\\NetDDE\\DDE Trusted Shares"
  43. #define DEFAULT_TRUSTED_SHARES_KEY_A "DEFAULT\\"##TRUSTED_SHARES_KEY_A
  44. #define TRUSTED_SHARES_KEY_PREFIX_A "DDEDBi"
  45. #define TRUSTED_SHARES_KEY_DEFAULT_A "DDEDBi12345678"
  46. #define KEY_MODIFY_ID_A "SerialNumber"
  47. #define KEY_DB_INSTANCE_A "ShareDBInstance"
  48. #define KEY_CMDSHOW_A "CmdShow"
  49. #define KEY_START_APP_A "StartApp"
  50. #define KEY_INIT_ALLOWED_A "InitAllowed"
  51. #define DDE_SHARES_KEY_W L"SOFTWARE\\Microsoft\\NetDDE\\DDE Shares"
  52. #define TRUSTED_SHARES_KEY_W L"SOFTWARE\\Microsoft\\NetDDE\\DDE Trusted Shares"
  53. #define DEFAULT_TRUSTED_SHARES_KEY_W L"DEFAULT\\"##TRUSTED_SHARES_KEY_A
  54. #define TRUSTED_SHARES_KEY_PREFIX_W L"DDEDBi"
  55. #define TRUSTED_SHARES_KEY_DEFAULT_W L"DDEDBi12345678"
  56. #define KEY_MODIFY_ID_W L"SerialNumber"
  57. #define KEY_DB_INSTANCE_W L"ShareDBInstance"
  58. #define KEY_CMDSHOW_W L"CmdShow"
  59. #define KEY_START_APP_W L"StartApp"
  60. #define KEY_INIT_ALLOWED_W L"InitAllowed"
  61. #define DDE_SHARES_KEY TEXT(DDE_SHARES_KEY_A)
  62. #define TRUSTED_SHARES_KEY TEXT(TRUSTED_SHARES_KEY_A)
  63. #define DEFAULT_TRUSTED_SHARES_KEY TEXT(DEFAULT_TRUSTED_SHARES_KEY_A)
  64. #define TRUSTED_SHARES_KEY_PREFIX TEXT(TRUSTED_SHARES_KEY_PREFIX_A)
  65. #define TRUSTED_SHARES_KEY_DEFAULT TEXT(TRUSTED_SHARES_KEY_DEFAULT_A)
  66. #define KEY_MODIFY_ID TEXT(KEY_MODIFY_ID_A)
  67. #define KEY_DB_INSTANCE TEXT(KEY_DB_INSTANCE_A)
  68. #define KEY_CMDSHOW TEXT(KEY_CMDSHOW_A)
  69. #define KEY_START_APP TEXT(KEY_START_APP_A)
  70. #define KEY_INIT_ALLOWED TEXT(KEY_INIT_ALLOWED_A)
  71. #endif
  72. #if DBG
  73. #define KdPrint(_x_) DbgPrint _x_
  74. #else
  75. #define KdPrint(_x_)
  76. #endif
  77. ULONG DbgPrint(PCH Format, ...);
  78. BOOL GetDBSerialNumber(DWORD *lpdwId);
  79. BOOL GetDBInstance(char *lpszBuf);
  80. TCHAR szShareKey[] = DDE_SHARES_KEY;
  81. CHAR szSetup[] = "NetDDE Setup";
  82. #define SHARES_TO_INIT 3
  83. CHAR *szShareNames[SHARES_TO_INIT] = {
  84. "Chat$" ,
  85. "Hearts$" ,
  86. "CLPBK$"
  87. };
  88. BOOL
  89. CreateShareDBInstance()
  90. {
  91. HKEY hKey;
  92. LONG lRtn;
  93. BOOL bOK = TRUE;
  94. DWORD InstanceId;
  95. time_t time_tmp;
  96. /* Create the DDE Share database in the registry if it does not exist. */
  97. lRtn = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  98. szShareKey,
  99. 0,
  100. KEY_SET_VALUE,
  101. &hKey );
  102. if( lRtn == ERROR_SUCCESS ) {
  103. /*
  104. * create data base instance value
  105. */
  106. srand((int) time(&time_tmp));
  107. InstanceId = rand() * rand();
  108. lRtn = RegSetValueEx( hKey,
  109. KEY_DB_INSTANCE, 0,
  110. REG_DWORD,
  111. (LPBYTE)&InstanceId,
  112. sizeof( DWORD ) );
  113. if( lRtn == ERROR_SUCCESS ) {
  114. } else {
  115. KdPrint(("SETUPDLL: CreateShareDBInstnace: RegSetValueEx %x failed (%u)\n",InstanceId,lRtn));
  116. bOK = FALSE;
  117. }
  118. RegCloseKey( hKey );
  119. } else {
  120. /* Share DB key should have been created from default hives */
  121. KdPrint(("SETUPDLL: CreateShareDBInstnace: RegOpenKey %s failed (%u)\n",szShareKey,lRtn));
  122. bOK = FALSE;
  123. }
  124. return(bOK);
  125. }
  126. BOOL
  127. CreateDefaultTrust(
  128. HKEY hKeyUserRoot)
  129. {
  130. HKEY hKey;
  131. DWORD dwDisp;
  132. DWORD ret;
  133. BOOL bOK;
  134. char szTrustedShareKey[TRUSTED_SHARES_KEY_MAX];
  135. char szShareKey[DDE_SHARE_KEY_MAX];
  136. char szDBInstance[TRUSTED_SHARES_KEY_SIZE + 1];
  137. DWORD dwId[2];
  138. DWORD dwFlag;
  139. int nLoop;
  140. if (!GetDBInstance(szDBInstance)) {
  141. return(FALSE);
  142. }
  143. if (!GetDBSerialNumber(dwId)) {
  144. KdPrint(("SETUPDLL: CreateDefaultTrust: GetDBSerialNumber failed.\n"));
  145. return(FALSE);
  146. }
  147. for (nLoop = 0, bOK = TRUE;
  148. (nLoop < SHARES_TO_INIT) && bOK ;
  149. nLoop++) {
  150. /*
  151. * For each share to init...
  152. */
  153. KdPrint(("Shareing %s\n", szShareNames[nLoop]));
  154. /*
  155. * Build up szTrustedSharesKey IAW the DBInstance sring.
  156. */
  157. sprintf( szTrustedShareKey,
  158. "%s\\%s\\%s",
  159. TRUSTED_SHARES_KEY_A,
  160. szDBInstance,
  161. szShareNames[nLoop] );
  162. /*
  163. * Create the trusted share key (hKey)
  164. */
  165. ret = RegCreateKeyExA( hKeyUserRoot, szTrustedShareKey,
  166. 0,
  167. NULL,
  168. REG_OPTION_NON_VOLATILE,
  169. KEY_WRITE,
  170. NULL,
  171. &hKey,
  172. &dwDisp );
  173. if( ret != ERROR_SUCCESS ) {
  174. KdPrint(("SETUPDLL: CreateDefaultTrust: RegCreateKeyEx failed on HKEY_CURRENT_USER\\%s. (%u)\n",
  175. szTrustedShareKey, ret));
  176. return(FALSE);
  177. }
  178. /*
  179. * Get the serial number of the database. Note that the SN of each
  180. * trust share must == the SN of the database. Since the non-trusted
  181. * shares may not have the latest database SN, update all of them to
  182. * the current database SN as well. This allows apps like winchat to
  183. * work when they are called from outside the machine even though they
  184. * have never been run - which would fix the SNs of their trusts because
  185. * they automatically set up their trusts.
  186. */
  187. /*
  188. * Set the SN of the trusted share
  189. */
  190. ret = RegSetValueEx( hKey,
  191. KEY_MODIFY_ID,
  192. 0,
  193. REG_BINARY,
  194. (LPBYTE)&dwId,
  195. KEY_MODIFY_ID_SIZE );
  196. if (ret == ERROR_SUCCESS) {
  197. /*
  198. * set the StartApp flag to 1
  199. */
  200. dwFlag = 1;
  201. ret = RegSetValueEx( hKey,
  202. KEY_START_APP,
  203. 0,
  204. REG_DWORD,
  205. (LPBYTE)&dwFlag,
  206. sizeof( DWORD ) );
  207. if (ret == ERROR_SUCCESS) {
  208. /*
  209. * Set the InitAllowed flag to 1 too.
  210. */
  211. ret = RegSetValueEx( hKey,
  212. KEY_INIT_ALLOWED,
  213. 0,
  214. REG_DWORD,
  215. (LPBYTE)&dwFlag,
  216. sizeof( DWORD ) );
  217. }
  218. }
  219. RegCloseKey(hKey);
  220. if (ret != ERROR_SUCCESS) {
  221. KdPrint(("SETUPDLL: CreateDefaultTrust: RegSetValueEx failed (%u)\n",ret));
  222. return(FALSE);
  223. }
  224. /*
  225. * Build up szShareKey
  226. */
  227. sprintf( szShareKey,
  228. "%s\\%s",
  229. DDE_SHARES_KEY_A,
  230. szShareNames[nLoop] );
  231. /*
  232. * Now open up the base share
  233. */
  234. ret = RegOpenKeyExA(
  235. HKEY_LOCAL_MACHINE,
  236. szShareKey,
  237. REG_OPTION_NON_VOLATILE,
  238. KEY_WRITE,
  239. &hKey);
  240. if (ret != ERROR_SUCCESS) {
  241. KdPrint(("SETUPDLL: CreateDefaultTrust: RegOpenKeyEx failed on HKEY_LOCAL_MACHINE\\%s. (%u)\n",
  242. szShareKey, ret));
  243. if (ret != ERROR_ACCESS_DENIED) {
  244. return(FALSE);
  245. }
  246. }
  247. if (ret == ERROR_SUCCESS) {
  248. ret = RegSetValueEx( hKey,
  249. KEY_MODIFY_ID,
  250. 0,
  251. REG_BINARY,
  252. (LPBYTE)&dwId,
  253. KEY_MODIFY_ID_SIZE );
  254. RegCloseKey(hKey);
  255. if (ret != ERROR_SUCCESS) {
  256. KdPrint(("SETUPDLL: CreateDefaultTrust: RegSetValueEx on HKEY_LOCAL_MACHINE\\%s failed (%u)\n",
  257. KEY_MODIFY_ID, ret));
  258. return(FALSE);
  259. }
  260. }
  261. } // end for
  262. return(TRUE);
  263. }
  264. BOOL
  265. GetDBInstance(char *lpszBuf)
  266. {
  267. LONG lRtn;
  268. HKEY hKey;
  269. DWORD dwInstance;
  270. DWORD dwType = REG_DWORD;
  271. DWORD cbData = sizeof(DWORD);
  272. lRtn = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  273. szShareKey,
  274. 0,
  275. KEY_QUERY_VALUE,
  276. &hKey );
  277. if (lRtn != ERROR_SUCCESS) { /* unable to open DB key */
  278. KdPrint(("SETUPDLL: GetDBInstance: RegOpenKeyEx %s failed (%u)\n",szShareKey,lRtn));
  279. return(FALSE);
  280. }
  281. lRtn = RegQueryValueEx( hKey,
  282. KEY_DB_INSTANCE,
  283. NULL,
  284. &dwType,
  285. (LPBYTE)&dwInstance, &cbData );
  286. RegCloseKey(hKey);
  287. if (lRtn != ERROR_SUCCESS) { /* unable to open DB key */
  288. KdPrint(("SETUPDLL: GetDBInstance: RegQueryValueEx failed (%u)\n",lRtn));
  289. return(FALSE);
  290. }
  291. sprintf(lpszBuf, "%s%08X", TRUSTED_SHARES_KEY_PREFIX, dwInstance);
  292. return(TRUE);
  293. }
  294. BOOL
  295. GetDBSerialNumber(
  296. DWORD *lpdwId)
  297. {
  298. LONG lRtn;
  299. HKEY hKey;
  300. DWORD dwType = REG_BINARY;
  301. DWORD cbData = KEY_MODIFY_ID_SIZE;
  302. lRtn = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  303. szShareKey,
  304. 0,
  305. KEY_QUERY_VALUE,
  306. &hKey );
  307. if (lRtn != ERROR_SUCCESS) { /* unable to open DB key */
  308. return(FALSE);
  309. }
  310. lRtn = RegQueryValueEx( hKey,
  311. KEY_MODIFY_ID,
  312. NULL,
  313. &dwType,
  314. (LPBYTE)lpdwId, &cbData );
  315. RegCloseKey(hKey);
  316. if (lRtn != ERROR_SUCCESS) { /* unable to open DB key */
  317. return(FALSE);
  318. }
  319. return(TRUE);
  320. }