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.

706 lines
15 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: shrinfo.cxx
  7. //
  8. // Contents: Lanman SHARE_INFO_502 encapsulation
  9. //
  10. // History: 21-Feb-95 BruceFo Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "headers.hxx"
  14. #pragma hdrstop
  15. #include "shrinfo.hxx"
  16. #include "util.hxx"
  17. CShareInfo::CShareInfo(
  18. VOID
  19. )
  20. :
  21. m_bOwn(TRUE),
  22. m_flags(0),
  23. m_pInfo(NULL)
  24. {
  25. INIT_SIG(CShareInfo);
  26. }
  27. CShareInfo::CShareInfo(
  28. IN SHARE_INFO_502* pInfo
  29. )
  30. :
  31. m_bOwn(FALSE),
  32. m_flags(0),
  33. m_pInfo(pInfo)
  34. {
  35. INIT_SIG(CShareInfo);
  36. }
  37. HRESULT
  38. CShareInfo::InitInstance(
  39. VOID
  40. )
  41. {
  42. CHECK_SIG(CShareInfo);
  43. if (m_bOwn)
  44. {
  45. appAssert(m_pInfo == NULL);
  46. m_pInfo = new SHARE_INFO_502;
  47. if (NULL == m_pInfo)
  48. {
  49. return E_OUTOFMEMORY;
  50. }
  51. m_pInfo->shi502_netname = NULL;
  52. m_pInfo->shi502_type = STYPE_DISKTREE;
  53. m_pInfo->shi502_remark = NULL;
  54. m_pInfo->shi502_permissions = ACCESS_ALL;
  55. m_pInfo->shi502_max_uses = SHI_USES_UNLIMITED;
  56. m_pInfo->shi502_path = NULL;
  57. m_pInfo->shi502_passwd = NULL;
  58. m_pInfo->shi502_reserved = 0;
  59. m_pInfo->shi502_security_descriptor = NULL;
  60. }
  61. return S_OK;
  62. }
  63. CShareInfo::~CShareInfo()
  64. {
  65. CHECK_SIG(CShareInfo);
  66. if (m_bOwn)
  67. {
  68. if (NULL != m_pInfo) // must check; InitInstance might have failed
  69. {
  70. if ( m_pInfo->shi502_netname )
  71. delete[] m_pInfo->shi502_netname;
  72. if ( m_pInfo->shi502_remark )
  73. delete[] m_pInfo->shi502_remark;
  74. if ( m_pInfo->shi502_path )
  75. delete[] m_pInfo->shi502_path;
  76. if ( m_pInfo->shi502_passwd )
  77. delete[] m_pInfo->shi502_passwd;
  78. if ( m_pInfo->shi502_security_descriptor )
  79. ::LocalFree( (BYTE*)m_pInfo->shi502_security_descriptor );
  80. delete m_pInfo;
  81. }
  82. }
  83. }
  84. NET_API_STATUS
  85. CShareInfo::Commit(
  86. IN PWSTR pszMachine
  87. )
  88. {
  89. CHECK_SIG(CShareInfo);
  90. appAssert(NULL != m_pInfo);
  91. if (m_flags == 0)
  92. {
  93. // nothing changed
  94. appDebugOut((DEB_ITRACE, "CShareInfo::Commit: nothing (%ws)\n", m_pInfo->shi502_netname));
  95. return NERR_Success;
  96. }
  97. // #if DBG == 1
  98. // Dump(L"Commit");
  99. // #endif // DBG == 1
  100. NET_API_STATUS ret;
  101. // Note: we store a path, even for admin$. However, the NetShare* APIs
  102. // don't like seeing a path for admin$, so we temporarily strip it here
  103. // if necessary, before calling any APIs.
  104. LPWSTR pszPathTmp = m_pInfo->shi502_path;
  105. if (0 == _wcsicmp(g_szAdminShare, m_pInfo->shi502_netname))
  106. {
  107. m_pInfo->shi502_path = NULL;
  108. }
  109. if (SHARE_FLAG_ADDED == m_flags)
  110. {
  111. appDebugOut((DEB_TRACE, "CShareInfo::Commit: add (%ws)\n", m_pInfo->shi502_netname));
  112. ret = NetShareAdd(pszMachine, 502, (LPBYTE)m_pInfo, NULL);
  113. }
  114. else if (SHARE_FLAG_REMOVE == m_flags)
  115. {
  116. appDebugOut((DEB_TRACE, "CShareInfo::Commit: remove (%ws)\n", m_pInfo->shi502_netname));
  117. ret = NetShareDel(pszMachine, m_pInfo->shi502_netname, 0);
  118. }
  119. else if (SHARE_FLAG_MODIFY == m_flags)
  120. {
  121. appDebugOut((DEB_TRACE, "CShareInfo::Commit: modify (%ws)\n", m_pInfo->shi502_netname));
  122. DWORD parm_err;
  123. ret = NetShareSetInfo(pszMachine, m_pInfo->shi502_netname, 502, (LPBYTE)m_pInfo, &parm_err);
  124. }
  125. // Restore the original, in case of admin$
  126. m_pInfo->shi502_path = pszPathTmp;
  127. // Must refresh the cache of shares after all commits
  128. if (ret != NERR_Success)
  129. {
  130. appDebugOut((DEB_TRACE, "CShareInfo::Commit: err = %d\n", ret));
  131. }
  132. return ret;
  133. }
  134. SHARE_INFO_502*
  135. CShareInfo::GetShareInfo(
  136. VOID
  137. )
  138. {
  139. CHECK_SIG(CShareInfo);
  140. appAssert(NULL != m_pInfo);
  141. return m_pInfo;
  142. }
  143. PWSTR
  144. CShareInfo::GetNetname(
  145. VOID
  146. )
  147. {
  148. CHECK_SIG(CShareInfo);
  149. appAssert(NULL != m_pInfo);
  150. return m_pInfo->shi502_netname;
  151. }
  152. DWORD
  153. CShareInfo::GetType(
  154. VOID
  155. )
  156. {
  157. CHECK_SIG(CShareInfo);
  158. appAssert(NULL != m_pInfo);
  159. return m_pInfo->shi502_type;
  160. }
  161. PWSTR
  162. CShareInfo::GetRemark(
  163. VOID
  164. )
  165. {
  166. CHECK_SIG(CShareInfo);
  167. appAssert(NULL != m_pInfo);
  168. return m_pInfo->shi502_remark;
  169. }
  170. DWORD
  171. CShareInfo::GetMaxUses(
  172. VOID
  173. )
  174. {
  175. CHECK_SIG(CShareInfo);
  176. appAssert(NULL != m_pInfo);
  177. return m_pInfo->shi502_max_uses;
  178. }
  179. PWSTR
  180. CShareInfo::GetPassword(
  181. VOID
  182. )
  183. {
  184. CHECK_SIG(CShareInfo);
  185. appAssert(NULL != m_pInfo);
  186. return m_pInfo->shi502_passwd;
  187. }
  188. PWSTR
  189. CShareInfo::GetPath(
  190. VOID
  191. )
  192. {
  193. CHECK_SIG(CShareInfo);
  194. appAssert(NULL != m_pInfo);
  195. return m_pInfo->shi502_path;
  196. }
  197. PSECURITY_DESCRIPTOR
  198. CShareInfo::GetSecurityDescriptor(
  199. VOID
  200. )
  201. {
  202. CHECK_SIG(CShareInfo);
  203. appAssert(NULL != m_pInfo);
  204. return m_pInfo->shi502_security_descriptor;
  205. }
  206. HRESULT
  207. CShareInfo::SetNetname(
  208. IN PWSTR pszNetname
  209. )
  210. {
  211. CHECK_SIG(CShareInfo);
  212. appAssert(m_flags != SHARE_FLAG_REMOVE);
  213. if (!TakeOwn())
  214. {
  215. return E_OUTOFMEMORY;
  216. }
  217. appDebugOut((DEB_ITRACE,
  218. "CShareInfo::SetNetname() = '%ws'\n",
  219. pszNetname));
  220. if ( m_pInfo->shi502_netname )
  221. delete[] m_pInfo->shi502_netname;
  222. m_pInfo->shi502_netname = NewDup(pszNetname);
  223. if (m_flags != SHARE_FLAG_ADDED)
  224. {
  225. m_flags = SHARE_FLAG_MODIFY;
  226. }
  227. return S_OK;
  228. }
  229. HRESULT
  230. CShareInfo::SetType(
  231. IN DWORD dwType
  232. )
  233. {
  234. CHECK_SIG(CShareInfo);
  235. appAssert(m_flags != SHARE_FLAG_REMOVE);
  236. if (dwType != m_pInfo->shi502_type)
  237. {
  238. // only take ownership and set the data if it's changed!
  239. if (!TakeOwn())
  240. {
  241. return E_OUTOFMEMORY;
  242. }
  243. appDebugOut((DEB_ITRACE,
  244. "CShareInfo::SetType(%ws) = %d\n",
  245. m_pInfo->shi502_netname,
  246. dwType));
  247. m_pInfo->shi502_type = dwType;
  248. if (m_flags != SHARE_FLAG_ADDED)
  249. {
  250. m_flags = SHARE_FLAG_MODIFY;
  251. }
  252. }
  253. return S_OK;
  254. }
  255. HRESULT
  256. CShareInfo::SetRemark(
  257. IN PWSTR pszRemark
  258. )
  259. {
  260. CHECK_SIG(CShareInfo);
  261. appAssert(m_flags != SHARE_FLAG_REMOVE);
  262. if (!TakeOwn())
  263. {
  264. return E_OUTOFMEMORY;
  265. }
  266. appDebugOut((DEB_ITRACE,
  267. "CShareInfo::SetRemark(%ws) = '%ws'\n",
  268. m_pInfo->shi502_netname,
  269. pszRemark));
  270. if ( m_pInfo->shi502_remark )
  271. delete[] m_pInfo->shi502_remark;
  272. m_pInfo->shi502_remark = NewDup(pszRemark);
  273. if (m_flags != SHARE_FLAG_ADDED)
  274. {
  275. m_flags = SHARE_FLAG_MODIFY;
  276. }
  277. return S_OK;
  278. }
  279. HRESULT
  280. CShareInfo::SetMaxUses(
  281. IN DWORD dwMaxUses
  282. )
  283. {
  284. CHECK_SIG(CShareInfo);
  285. appAssert(m_flags != SHARE_FLAG_REMOVE);
  286. if (dwMaxUses != m_pInfo->shi502_max_uses)
  287. {
  288. // only take ownership and set the data if it's changed!
  289. if (!TakeOwn())
  290. {
  291. return E_OUTOFMEMORY;
  292. }
  293. appDebugOut((DEB_ITRACE,
  294. "CShareInfo::SetMaxUses(%ws) = %d\n",
  295. m_pInfo->shi502_netname,
  296. dwMaxUses));
  297. m_pInfo->shi502_max_uses = dwMaxUses;
  298. if (m_flags != SHARE_FLAG_ADDED)
  299. {
  300. m_flags = SHARE_FLAG_MODIFY;
  301. }
  302. }
  303. return S_OK;
  304. }
  305. HRESULT
  306. CShareInfo::SetPassword(
  307. IN PWSTR pszPassword
  308. )
  309. {
  310. CHECK_SIG(CShareInfo);
  311. appAssert(m_flags != SHARE_FLAG_REMOVE);
  312. if (!TakeOwn())
  313. {
  314. return E_OUTOFMEMORY;
  315. }
  316. appDebugOut((DEB_ITRACE,
  317. "CShareInfo::SetPassword(%ws) = '%ws'\n",
  318. m_pInfo->shi502_netname,
  319. pszPassword));
  320. if ( m_pInfo->shi502_passwd )
  321. delete[] m_pInfo->shi502_passwd;
  322. m_pInfo->shi502_passwd = NewDup(pszPassword);
  323. if (m_flags != SHARE_FLAG_ADDED)
  324. {
  325. m_flags = SHARE_FLAG_MODIFY;
  326. }
  327. return S_OK;
  328. }
  329. HRESULT
  330. CShareInfo::SetPath(
  331. IN PWSTR pszPath
  332. )
  333. {
  334. CHECK_SIG(CShareInfo);
  335. appAssert(m_flags != SHARE_FLAG_REMOVE);
  336. if (!TakeOwn())
  337. {
  338. return E_OUTOFMEMORY;
  339. }
  340. appDebugOut((DEB_ITRACE,
  341. "CShareInfo::SetPath(%ws) = '%ws'\n",
  342. m_pInfo->shi502_netname,
  343. pszPath));
  344. if ( m_pInfo->shi502_path )
  345. delete[] m_pInfo->shi502_path;
  346. if (pszPath[0] == TEXT('\0'))
  347. {
  348. m_pInfo->shi502_path = NULL; // so IPC$ and ADMIN$ work
  349. }
  350. else
  351. {
  352. m_pInfo->shi502_path = NewDup(pszPath);
  353. }
  354. if (m_flags != SHARE_FLAG_ADDED)
  355. {
  356. m_flags = SHARE_FLAG_MODIFY;
  357. }
  358. return S_OK;
  359. }
  360. HRESULT
  361. CShareInfo::SetSecurityDescriptor(
  362. IN PSECURITY_DESCRIPTOR pSecDesc
  363. )
  364. {
  365. CHECK_SIG(CShareInfo);
  366. appAssert(m_flags != SHARE_FLAG_REMOVE);
  367. if (!TakeOwn())
  368. {
  369. return E_OUTOFMEMORY;
  370. }
  371. appDebugOut((DEB_ITRACE,
  372. "CShareInfo::SetSecurityDescriptor(%ws) = ...\n",
  373. m_pInfo->shi502_netname));
  374. if ( m_pInfo->shi502_security_descriptor )
  375. ::LocalFree( (BYTE*)m_pInfo->shi502_security_descriptor );
  376. m_pInfo->shi502_security_descriptor = CopySecurityDescriptor(pSecDesc);
  377. if (m_flags != SHARE_FLAG_ADDED)
  378. {
  379. m_flags = SHARE_FLAG_MODIFY;
  380. }
  381. return S_OK;
  382. }
  383. HRESULT
  384. CShareInfo::TransferSecurityDescriptor(
  385. IN PSECURITY_DESCRIPTOR pSecDesc
  386. )
  387. {
  388. CHECK_SIG(CShareInfo);
  389. appAssert(m_flags != SHARE_FLAG_REMOVE);
  390. if (!TakeOwn())
  391. {
  392. return E_OUTOFMEMORY;
  393. }
  394. appDebugOut((DEB_ITRACE,
  395. "CShareInfo::TransferSecurityDescriptor(%ws) = ...\n",
  396. m_pInfo->shi502_netname));
  397. if ( m_pInfo->shi502_security_descriptor )
  398. ::LocalFree( (BYTE*)m_pInfo->shi502_security_descriptor );
  399. m_pInfo->shi502_security_descriptor = pSecDesc;
  400. if (m_flags != SHARE_FLAG_ADDED)
  401. {
  402. m_flags = SHARE_FLAG_MODIFY;
  403. }
  404. return S_OK;
  405. }
  406. ULONG
  407. CShareInfo::GetFlag(
  408. VOID
  409. )
  410. {
  411. CHECK_SIG(CShareInfo);
  412. return m_flags;
  413. }
  414. VOID
  415. CShareInfo::SetDirtyFlag(
  416. ULONG flag
  417. )
  418. {
  419. CHECK_SIG(CShareInfo);
  420. m_flags = flag;
  421. }
  422. HRESULT
  423. CShareInfo::Copy(
  424. IN SHARE_INFO_502* pInfo
  425. )
  426. {
  427. CHECK_SIG(CShareInfo);
  428. // get a valid SHARE_INFO_502 structure...
  429. if (m_bOwn)
  430. {
  431. // delete what's already there
  432. appAssert(NULL != m_pInfo);
  433. if ( m_pInfo->shi502_netname )
  434. delete[] m_pInfo->shi502_netname;
  435. if ( m_pInfo->shi502_remark )
  436. delete[] m_pInfo->shi502_remark;
  437. if ( m_pInfo->shi502_path )
  438. delete[] m_pInfo->shi502_path;
  439. if ( m_pInfo->shi502_passwd )
  440. delete[] m_pInfo->shi502_passwd;
  441. if ( m_pInfo->shi502_security_descriptor )
  442. ::LocalFree( (BYTE*)m_pInfo->shi502_security_descriptor );
  443. }
  444. else
  445. {
  446. m_pInfo = new SHARE_INFO_502;
  447. if (NULL == m_pInfo)
  448. {
  449. return E_OUTOFMEMORY;
  450. }
  451. }
  452. appAssert(NULL != m_pInfo);
  453. m_bOwn = TRUE;
  454. m_pInfo->shi502_netname = NULL;
  455. m_pInfo->shi502_type = pInfo->shi502_type;
  456. m_pInfo->shi502_remark = NULL;
  457. m_pInfo->shi502_permissions = pInfo->shi502_permissions;
  458. m_pInfo->shi502_max_uses = pInfo->shi502_max_uses;
  459. m_pInfo->shi502_path = NULL;
  460. m_pInfo->shi502_passwd = NULL;
  461. m_pInfo->shi502_reserved = pInfo->shi502_reserved;
  462. m_pInfo->shi502_security_descriptor = NULL;
  463. if (NULL != pInfo->shi502_netname)
  464. {
  465. m_pInfo->shi502_netname = NewDup(pInfo->shi502_netname);
  466. }
  467. if (NULL != pInfo->shi502_remark)
  468. {
  469. m_pInfo->shi502_remark = NewDup(pInfo->shi502_remark);
  470. }
  471. if (NULL != pInfo->shi502_path)
  472. {
  473. m_pInfo->shi502_path = NewDup(pInfo->shi502_path);
  474. }
  475. if (NULL != pInfo->shi502_passwd)
  476. {
  477. m_pInfo->shi502_passwd = NewDup(pInfo->shi502_passwd);
  478. }
  479. if (NULL != pInfo->shi502_security_descriptor)
  480. {
  481. m_pInfo->shi502_security_descriptor = CopySecurityDescriptor(pInfo->shi502_security_descriptor);
  482. }
  483. return S_OK;
  484. }
  485. BOOL
  486. CShareInfo::TakeOwn(
  487. VOID
  488. )
  489. {
  490. CHECK_SIG(CShareInfo);
  491. if (m_bOwn)
  492. {
  493. return TRUE; // already own the memory
  494. }
  495. SHARE_INFO_502* pInfo = new SHARE_INFO_502;
  496. if (NULL == pInfo)
  497. {
  498. return FALSE;
  499. }
  500. pInfo->shi502_type = m_pInfo->shi502_type;
  501. pInfo->shi502_permissions = m_pInfo->shi502_permissions;
  502. pInfo->shi502_max_uses = m_pInfo->shi502_max_uses;
  503. pInfo->shi502_reserved = 0;
  504. pInfo->shi502_netname = NULL;
  505. if (NULL != m_pInfo->shi502_netname)
  506. {
  507. pInfo->shi502_netname = NewDup(m_pInfo->shi502_netname);
  508. }
  509. pInfo->shi502_remark = NULL;
  510. if (NULL != m_pInfo->shi502_remark)
  511. {
  512. pInfo->shi502_remark = NewDup(m_pInfo->shi502_remark);
  513. }
  514. pInfo->shi502_path = NULL;
  515. if (NULL != m_pInfo->shi502_path)
  516. {
  517. pInfo->shi502_path = NewDup(m_pInfo->shi502_path);
  518. }
  519. pInfo->shi502_passwd = NULL;
  520. if (NULL != m_pInfo->shi502_passwd)
  521. {
  522. pInfo->shi502_passwd = NewDup(m_pInfo->shi502_passwd);
  523. }
  524. pInfo->shi502_security_descriptor = NULL;
  525. if (NULL != m_pInfo->shi502_security_descriptor)
  526. {
  527. pInfo->shi502_security_descriptor = CopySecurityDescriptor(m_pInfo->shi502_security_descriptor);
  528. }
  529. m_pInfo = pInfo;
  530. m_bOwn = TRUE;
  531. #if DBG == 1
  532. Dump(L"After TakeOwn");
  533. #endif // DBG == 1
  534. return TRUE;
  535. }
  536. #if DBG == 1
  537. VOID
  538. CShareInfo::Dump(
  539. IN PWSTR pszCaption
  540. )
  541. {
  542. CHECK_SIG(CShareInfo);
  543. appDebugOut((DEB_TRACE,
  544. "CShareInfo::Dump, %ws\n",
  545. pszCaption));
  546. appDebugOut((DEB_TRACE | DEB_NOCOMPNAME,
  547. "\t This: 0x%08lx\n"
  548. "\t Info: 0x%08lx\n"
  549. "\tOwn memory?: %ws\n"
  550. "\t Flags: %ws\n"
  551. "\t Share name: %ws\n"
  552. "\t Type: %d\n"
  553. "\t Comment: %ws\n"
  554. "\tPermissions: %d\n"
  555. "\t Max uses: %d\n"
  556. "\t Path: %ws\n"
  557. "\t Password: %ws\n"
  558. "\t Reserved: %d\n"
  559. "\t Security? %ws\n"
  560. ,
  561. this,
  562. m_pInfo,
  563. m_bOwn ? L"yes" : L"no",
  564. (m_flags == 0)
  565. ? L"none"
  566. : (m_flags == SHARE_FLAG_ADDED)
  567. ? L"added"
  568. : (m_flags == SHARE_FLAG_REMOVE)
  569. ? L"remove"
  570. : (m_flags == SHARE_FLAG_MODIFY)
  571. ? L"modify"
  572. : L"UNKNOWN!",
  573. (NULL == m_pInfo->shi502_netname) ? L"none" : m_pInfo->shi502_netname,
  574. m_pInfo->shi502_type,
  575. (NULL == m_pInfo->shi502_remark) ? L"none" : m_pInfo->shi502_remark,
  576. m_pInfo->shi502_permissions,
  577. m_pInfo->shi502_max_uses,
  578. (NULL == m_pInfo->shi502_path) ? L"none" : m_pInfo->shi502_path,
  579. (NULL == m_pInfo->shi502_passwd) ? L"none" : m_pInfo->shi502_passwd,
  580. m_pInfo->shi502_reserved,
  581. (NULL == m_pInfo->shi502_security_descriptor) ? L"No" : L"Yes"
  582. ));
  583. }
  584. #endif // DBG == 1