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.

380 lines
10 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. Sharpath.H
  5. Abstract:
  6. display message box and share path
  7. Author:
  8. Bob Watson (a-robw)
  9. Revision History:
  10. 17 Feb 94 Written
  11. --*/
  12. //
  13. // Windows Include Files
  14. //
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <malloc.h>
  18. #include <tchar.h> // unicode macros
  19. #include <lmcons.h> // lanman API constants
  20. #include <lmerr.h> // lanman error returns
  21. #include <lmshare.h> // sharing API prototypes
  22. //
  23. // app include files
  24. //
  25. #include "otnboot.h"
  26. #include "otnbtdlg.h"
  27. #define NCDU_MSG_SHARE_DIR (WM_USER + 101)
  28. static PSPS_DATA pspData; // path & share info passed in
  29. PSECURITY_DESCRIPTOR
  30. GetShareSecurityDescriptor (
  31. VOID
  32. )
  33. /*++
  34. Routine Description:
  35. Allocates, and initializes a security descriptor for the
  36. share point created by the app. The security descriptor
  37. contains two ACE's
  38. Domain Admins: Full Control
  39. Everyone: Read only
  40. Arguments:
  41. None
  42. Return Value:
  43. Address of an initialized security descriptor of all went OK
  44. a Null pointer if an error occurred (which gives everyone Full Control)
  45. --*/
  46. {
  47. PSECURITY_DESCRIPTOR pSD = NULL;
  48. PSID psidAdmins = NULL;
  49. PSID psidWorld = NULL;
  50. PSID psidDomainAdmins = NULL;
  51. BOOL bValidSd = TRUE;
  52. PACL pACL = NULL;
  53. DWORD dwAclSize = 0;
  54. DWORD dwError = ERROR_SUCCESS;
  55. DWORD dwSidLength = 0;
  56. DWORD dwDomainNameLength = 0;
  57. SID_IDENTIFIER_AUTHORITY siaAdmins = SECURITY_NT_AUTHORITY;
  58. SID_IDENTIFIER_AUTHORITY siaWorld = SECURITY_WORLD_SID_AUTHORITY;
  59. LPTSTR szDomainName;
  60. SID_NAME_USE snu;
  61. // create an empty Security Descriptor
  62. pSD = GlobalAlloc (GPTR, SMALL_BUFFER_SIZE);
  63. if (pSD != NULL) {
  64. if (InitializeSecurityDescriptor (pSD, SECURITY_DESCRIPTOR_REVISION)) {
  65. // create the Admin SID
  66. if (AllocateAndInitializeSid (&siaAdmins,
  67. 2,
  68. SECURITY_BUILTIN_DOMAIN_RID,
  69. DOMAIN_ALIAS_RID_ADMINS,
  70. 0, 0, 0, 0, 0, 0, &psidAdmins)) {
  71. // create the World SID
  72. if (AllocateAndInitializeSid(&siaWorld,
  73. 1,
  74. SECURITY_WORLD_RID,
  75. 0, 0, 0, 0, 0, 0, 0, &psidWorld)) {
  76. psidDomainAdmins = GlobalAlloc (GPTR, SMALL_BUFFER_SIZE);
  77. if (psidDomainAdmins != NULL) {
  78. dwSidLength = SMALL_BUFFER_SIZE;
  79. }
  80. szDomainName = GlobalAlloc (GPTR, MAX_PATH_BYTES);
  81. if (szDomainName != NULL) {
  82. dwDomainNameLength = MAX_PATH;
  83. }
  84. LookupAccountName (
  85. pspData->szServer,
  86. GetStringResource (CSZ_DOMAIN_ADMINS),
  87. psidDomainAdmins,
  88. &dwSidLength,
  89. szDomainName,
  90. &dwDomainNameLength,
  91. &snu);
  92. // allocate and initialize the ACL;
  93. dwAclSize = sizeof(ACL) +
  94. (3 * sizeof(ACCESS_ALLOWED_ACE)) +
  95. GetLengthSid(psidAdmins) +
  96. GetLengthSid(psidWorld) +
  97. (psidDomainAdmins != NULL ? GetLengthSid(psidDomainAdmins) : 0) -
  98. sizeof(DWORD);
  99. pACL = GlobalAlloc (GPTR, dwAclSize);
  100. if(pACL == NULL) {
  101. // free local structures
  102. FreeSid (psidAdmins);
  103. FreeSid (psidWorld);
  104. FREE_IF_ALLOC (pSD);
  105. return NULL;
  106. }
  107. InitializeAcl (pACL, dwAclSize, ACL_REVISION);
  108. if (psidDomainAdmins != NULL) {
  109. // add the Domain Admin ACEs
  110. AddAccessAllowedAce (pACL, ACL_REVISION,
  111. GENERIC_ALL, psidDomainAdmins);
  112. }
  113. // add the Admin ACEs
  114. AddAccessAllowedAce (pACL, ACL_REVISION,
  115. GENERIC_ALL, psidAdmins);
  116. // add the World ACE
  117. AddAccessAllowedAce (pACL, ACL_REVISION,
  118. (GENERIC_READ | GENERIC_EXECUTE), psidWorld);
  119. // add a discretionary ACL to the Security Descriptor
  120. SetSecurityDescriptorDacl (
  121. pSD,
  122. TRUE,
  123. pACL,
  124. FALSE);
  125. // free local structures
  126. FreeSid (psidAdmins);
  127. FreeSid (psidWorld);
  128. // return the completed SD
  129. } else {
  130. // unable to allocate a World (everyone) SID
  131. // free Admin SID and set not valid flag
  132. FreeSid (psidAdmins);
  133. bValidSd = FALSE;
  134. }
  135. } else {
  136. // unable to allocate an Admin SID
  137. bValidSd = FALSE;
  138. }
  139. } else {
  140. // unable to init. Security Descriptor
  141. bValidSd = FALSE;
  142. }
  143. }
  144. if ((pSD == NULL) || !bValidSd ) {
  145. // something happened, so the SD is not valid. Free it and return
  146. // NULL
  147. FREE_IF_ALLOC (pSD);
  148. pSD = NULL;
  149. } else {
  150. // make sure all was created OK
  151. if (!IsValidSecurityDescriptor(pSD)) {
  152. // an invalid sercurity descriptor was created so
  153. // get reason, then trash it.
  154. dwError = GetLastError();
  155. FREE_IF_ALLOC (pSD);
  156. pSD = NULL;
  157. }
  158. }
  159. return pSD;
  160. }
  161. static
  162. BOOL
  163. SharePathDlg_WM_INITDIALOG (
  164. IN HWND hwndDlg,
  165. IN WPARAM wParam,
  166. IN LPARAM lParam
  167. )
  168. /*++
  169. Routine Description:
  170. Process the WM_INITDIALOG windows message. Initialized the
  171. values in the dialog box controls to reflect the current
  172. values of the Application data structure.
  173. Arguments:
  174. IN HWND hwndDlg
  175. handle to dialog box window
  176. IN WPARAM wParam
  177. Not Used
  178. IN LPARAM lParam
  179. address of SHARE_PATH_DLG_STRUCT that contains share information
  180. Return Value:
  181. FALSE
  182. --*/
  183. {
  184. PositionWindow (hwndDlg);
  185. if (lParam == 0) {
  186. pspData = NULL;
  187. EndDialog (hwndDlg, IDCANCEL);
  188. } else {
  189. pspData = (PSPS_DATA)lParam;
  190. SetDlgItemText (hwndDlg, NCDU_CTL_SHARING_PATH_NAME, pspData->szPath);
  191. SetDlgItemText (hwndDlg, NCDU_CTL_SHARING_PATH_ON,
  192. (pspData->szServer != NULL ? pspData->szServer :
  193. GetStringResource (CSZ_LOCAL_MACHINE)));
  194. SetDlgItemText (hwndDlg, NCDU_CTL_SHARING_PATH_AS,
  195. pspData->szShareName);
  196. SetCursor (LoadCursor(NULL, IDC_WAIT));
  197. PostMessage (hwndDlg, NCDU_MSG_SHARE_DIR, 0, 0);
  198. }
  199. // SetActiveWindow (hwndDlg);
  200. return TRUE;
  201. }
  202. static
  203. BOOL
  204. SharePathDlg_SHARE_DIR (
  205. IN HWND hwndDlg,
  206. IN WPARAM wParam,
  207. IN LPARAM lParam // LPTSTR to sharename
  208. )
  209. /*++
  210. Routine Description:
  211. Shares either the Distribution or the Destination dir depending on
  212. the wParam. Uses the share name entered in the display. If
  213. successful this message terminates the dialog box, otherwise
  214. an error message will be displayed.
  215. Arguments:
  216. IN HWND hwndDlg
  217. Handle to dialog box window
  218. IN WPARAM wParam
  219. Not Used
  220. IN LPARAM lParam
  221. Not Used
  222. Return Value:
  223. TRUE if shared
  224. FALSE if not (GetLastError for info)
  225. --*/
  226. {
  227. BOOL bDist;
  228. NET_API_STATUS naStatus;
  229. DWORD dwParmErr;
  230. SHARE_INFO_502 si502; // share info block
  231. LONG lCount;
  232. bDist = (BOOL)wParam;
  233. // initialize share data block
  234. si502.shi502_netname = pspData->szShareName;
  235. si502.shi502_type = STYPE_DISKTREE;
  236. si502.shi502_remark = (LPWSTR)pspData->szRemark;
  237. si502.shi502_permissions = PERM_FILE_READ;
  238. si502.shi502_max_uses = SHI_USES_UNLIMITED;
  239. si502.shi502_current_uses = 0;
  240. si502.shi502_path = pspData->szPath;
  241. si502.shi502_passwd = NULL;
  242. si502.shi502_reserved = 0L;
  243. si502.shi502_security_descriptor = GetShareSecurityDescriptor();
  244. naStatus = NetShareAdd (
  245. pspData->szServer, // machine
  246. 502, // level 502 request
  247. (LPBYTE)&si502, // data request buffer
  248. &dwParmErr); // parameter buffer
  249. if (naStatus != NERR_Success) {
  250. // restore cursor
  251. SetCursor (LoadCursor(NULL, IDC_ARROW));
  252. // display error
  253. MessageBox (
  254. hwndDlg,
  255. GetNetErrorMsg (naStatus),
  256. 0,
  257. MB_OK_TASK_EXCL);
  258. EndDialog (hwndDlg, IDCANCEL);
  259. } else {
  260. // successfully shared so wait til it registers or we get bored
  261. lCount = 200; // wait 20 seconds then give up and leave
  262. while (!LookupLocalShare (pspData->szPath, TRUE,
  263. NULL, NULL )) {
  264. Sleep (100); // wait until the new share registers
  265. if (--lCount == 0) break;
  266. }
  267. Sleep (1000); // wait for shared dir to become available
  268. SetCursor (LoadCursor(NULL, IDC_ARROW));
  269. EndDialog (hwndDlg, IDOK);
  270. }
  271. FREE_IF_ALLOC (si502.shi502_security_descriptor);
  272. return TRUE;
  273. }
  274. INT_PTR CALLBACK
  275. SharePathDlgProc (
  276. IN HWND hwndDlg,
  277. IN UINT message,
  278. IN WPARAM wParam,
  279. IN LPARAM lParam
  280. )
  281. /*++
  282. Routine Description:
  283. Main Dialog Box Window Procedure for the Initial configuration screen
  284. Processes the following windows messages by dispatching the
  285. appropriate routine.
  286. WM_INITDIALOG: dialog box initialization
  287. WM_COMMAND: user input
  288. All other windows messages are processed by the default dialog box
  289. procedure.
  290. Arguments:
  291. Standard WNDPROC arguments
  292. Return Value:
  293. FALSE if the message is not processed by this routine, otherwise the
  294. value returned by the dispatched routine.
  295. --*/
  296. {
  297. switch (message) {
  298. case WM_INITDIALOG: return (SharePathDlg_WM_INITDIALOG (hwndDlg, wParam, lParam));
  299. case NCDU_MSG_SHARE_DIR: return (SharePathDlg_SHARE_DIR (hwndDlg, wParam, lParam));
  300. default: return FALSE;
  301. }
  302. }