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.

383 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_W
  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. #include <strsafe.h>
  73. #if DBG
  74. #define KdPrint(_x_) DbgPrint _x_
  75. #else
  76. #define KdPrint(_x_)
  77. #endif
  78. ULONG DbgPrint(PCH Format, ...);
  79. BOOL GetDBSerialNumber(DWORD *lpdwId);
  80. BOOL GetDBInstance(char *lpszBuf);
  81. TCHAR szShareKey[] = DDE_SHARES_KEY;
  82. CHAR szSetup[] = "NetDDE Setup";
  83. #define SHARES_TO_INIT 3
  84. CHAR *szShareNames[SHARES_TO_INIT] = {
  85. "Chat$" ,
  86. "Hearts$" ,
  87. "CLPBK$"
  88. };
  89. BOOL
  90. CreateShareDBInstance()
  91. {
  92. HKEY hKey;
  93. LONG lRtn;
  94. BOOL bOK = TRUE;
  95. DWORD InstanceId;
  96. time_t time_tmp;
  97. /* Create the DDE Share database in the registry if it does not exist. */
  98. lRtn = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  99. szShareKey,
  100. 0,
  101. KEY_SET_VALUE,
  102. &hKey );
  103. if( lRtn == ERROR_SUCCESS ) {
  104. /*
  105. * create data base instance value
  106. */
  107. srand((int) time(&time_tmp));
  108. InstanceId = rand() * rand();
  109. lRtn = RegSetValueEx( hKey,
  110. KEY_DB_INSTANCE, 0,
  111. REG_DWORD,
  112. (LPBYTE)&InstanceId,
  113. sizeof( DWORD ) );
  114. if( lRtn == ERROR_SUCCESS ) {
  115. } else {
  116. KdPrint(("SETUPDLL: CreateShareDBInstnace: RegSetValueEx %x failed (%u)\n",InstanceId,lRtn));
  117. bOK = FALSE;
  118. }
  119. RegCloseKey( hKey );
  120. } else {
  121. /* Share DB key should have been created from default hives */
  122. KdPrint(("SETUPDLL: CreateShareDBInstnace: RegOpenKey %s failed (%u)\n",szShareKey,lRtn));
  123. bOK = FALSE;
  124. }
  125. return(bOK);
  126. }
  127. BOOL
  128. CreateDefaultTrust(
  129. HKEY hKeyUserRoot)
  130. {
  131. HKEY hKey;
  132. DWORD dwDisp;
  133. DWORD ret;
  134. BOOL bOK;
  135. char szTrustedShareKey[TRUSTED_SHARES_KEY_MAX];
  136. char szShareKey[DDE_SHARE_KEY_MAX];
  137. char szDBInstance[TRUSTED_SHARES_KEY_SIZE + 1];
  138. DWORD dwId[2];
  139. DWORD dwFlag;
  140. int nLoop;
  141. if (!GetDBInstance(szDBInstance)) {
  142. return(FALSE);
  143. }
  144. if (!GetDBSerialNumber(dwId)) {
  145. KdPrint(("SETUPDLL: CreateDefaultTrust: GetDBSerialNumber failed.\n"));
  146. return(FALSE);
  147. }
  148. for (nLoop = 0, bOK = TRUE;
  149. (nLoop < SHARES_TO_INIT) && bOK ;
  150. nLoop++) {
  151. /*
  152. * For each share to init...
  153. */
  154. KdPrint(("Shareing %s\n", szShareNames[nLoop]));
  155. /*
  156. * Build up szTrustedSharesKey IAW the DBInstance sring.
  157. */
  158. StringCchPrintfA( szTrustedShareKey,
  159. TRUSTED_SHARES_KEY_MAX,
  160. "%s\\%s\\%s",
  161. TRUSTED_SHARES_KEY_A,
  162. szDBInstance,
  163. szShareNames[nLoop] );
  164. /*
  165. * Create the trusted share key (hKey)
  166. */
  167. ret = RegCreateKeyExA( hKeyUserRoot, szTrustedShareKey,
  168. 0,
  169. NULL,
  170. REG_OPTION_NON_VOLATILE,
  171. KEY_WRITE,
  172. NULL,
  173. &hKey,
  174. &dwDisp );
  175. if( ret != ERROR_SUCCESS ) {
  176. KdPrint(("SETUPDLL: CreateDefaultTrust: RegCreateKeyEx failed on HKEY_CURRENT_USER\\%s. (%u)\n",
  177. szTrustedShareKey, ret));
  178. return(FALSE);
  179. }
  180. /*
  181. * Get the serial number of the database. Note that the SN of each
  182. * trust share must == the SN of the database. Since the non-trusted
  183. * shares may not have the latest database SN, update all of them to
  184. * the current database SN as well. This allows apps like winchat to
  185. * work when they are called from outside the machine even though they
  186. * have never been run - which would fix the SNs of their trusts because
  187. * they automatically set up their trusts.
  188. */
  189. /*
  190. * Set the SN of the trusted share
  191. */
  192. ret = RegSetValueEx( hKey,
  193. KEY_MODIFY_ID,
  194. 0,
  195. REG_BINARY,
  196. (LPBYTE)&dwId,
  197. KEY_MODIFY_ID_SIZE );
  198. if (ret == ERROR_SUCCESS) {
  199. /*
  200. * set the StartApp flag to 1
  201. */
  202. dwFlag = 1;
  203. ret = RegSetValueEx( hKey,
  204. KEY_START_APP,
  205. 0,
  206. REG_DWORD,
  207. (LPBYTE)&dwFlag,
  208. sizeof( DWORD ) );
  209. if (ret == ERROR_SUCCESS) {
  210. /*
  211. * Set the InitAllowed flag to 1 too.
  212. */
  213. ret = RegSetValueEx( hKey,
  214. KEY_INIT_ALLOWED,
  215. 0,
  216. REG_DWORD,
  217. (LPBYTE)&dwFlag,
  218. sizeof( DWORD ) );
  219. }
  220. }
  221. RegCloseKey(hKey);
  222. if (ret != ERROR_SUCCESS) {
  223. KdPrint(("SETUPDLL: CreateDefaultTrust: RegSetValueEx failed (%u)\n",ret));
  224. return(FALSE);
  225. }
  226. /*
  227. * Build up szShareKey
  228. */
  229. StringCchPrintfA( szShareKey,
  230. DDE_SHARE_KEY_MAX,
  231. "%s\\%s",
  232. DDE_SHARES_KEY_A,
  233. szShareNames[nLoop] );
  234. /*
  235. * Now open up the base share
  236. */
  237. ret = RegOpenKeyExA(
  238. HKEY_LOCAL_MACHINE,
  239. szShareKey,
  240. REG_OPTION_NON_VOLATILE,
  241. KEY_WRITE,
  242. &hKey);
  243. if (ret != ERROR_SUCCESS) {
  244. KdPrint(("SETUPDLL: CreateDefaultTrust: RegOpenKeyEx failed on HKEY_LOCAL_MACHINE\\%s. (%u)\n",
  245. szShareKey, ret));
  246. if (ret != ERROR_ACCESS_DENIED) {
  247. return(FALSE);
  248. }
  249. }
  250. if (ret == ERROR_SUCCESS) {
  251. ret = RegSetValueEx( hKey,
  252. KEY_MODIFY_ID,
  253. 0,
  254. REG_BINARY,
  255. (LPBYTE)&dwId,
  256. KEY_MODIFY_ID_SIZE );
  257. RegCloseKey(hKey);
  258. if (ret != ERROR_SUCCESS) {
  259. KdPrint(("SETUPDLL: CreateDefaultTrust: RegSetValueEx on HKEY_LOCAL_MACHINE\\%s failed (%u)\n",
  260. KEY_MODIFY_ID, ret));
  261. return(FALSE);
  262. }
  263. }
  264. } // end for
  265. return(TRUE);
  266. }
  267. BOOL
  268. GetDBInstance(char *lpszBuf)
  269. {
  270. LONG lRtn;
  271. HKEY hKey;
  272. DWORD dwInstance;
  273. DWORD dwType = REG_DWORD;
  274. DWORD cbData = sizeof(DWORD);
  275. lRtn = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  276. szShareKey,
  277. 0,
  278. KEY_QUERY_VALUE,
  279. &hKey );
  280. if (lRtn != ERROR_SUCCESS) { /* unable to open DB key */
  281. KdPrint(("SETUPDLL: GetDBInstance: RegOpenKeyEx %s failed (%u)\n",szShareKey,lRtn));
  282. return(FALSE);
  283. }
  284. lRtn = RegQueryValueEx( hKey,
  285. KEY_DB_INSTANCE,
  286. NULL,
  287. &dwType,
  288. (LPBYTE)&dwInstance, &cbData );
  289. RegCloseKey(hKey);
  290. if (lRtn != ERROR_SUCCESS) { /* unable to open DB key */
  291. KdPrint(("SETUPDLL: GetDBInstance: RegQueryValueEx failed (%u)\n",lRtn));
  292. return(FALSE);
  293. }
  294. StringCchPrintfA(lpszBuf, TRUSTED_SHARES_KEY_SIZE + 1, "%s%08X", TRUSTED_SHARES_KEY_PREFIX, dwInstance);
  295. return(TRUE);
  296. }
  297. BOOL
  298. GetDBSerialNumber(
  299. DWORD *lpdwId)
  300. {
  301. LONG lRtn;
  302. HKEY hKey;
  303. DWORD dwType = REG_BINARY;
  304. DWORD cbData = KEY_MODIFY_ID_SIZE;
  305. lRtn = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  306. szShareKey,
  307. 0,
  308. KEY_QUERY_VALUE,
  309. &hKey );
  310. if (lRtn != ERROR_SUCCESS) { /* unable to open DB key */
  311. return(FALSE);
  312. }
  313. lRtn = RegQueryValueEx( hKey,
  314. KEY_MODIFY_ID,
  315. NULL,
  316. &dwType,
  317. (LPBYTE)lpdwId, &cbData );
  318. RegCloseKey(hKey);
  319. if (lRtn != ERROR_SUCCESS) { /* unable to open DB key */
  320. return(FALSE);
  321. }
  322. return(TRUE);
  323. }