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.

317 lines
6.6 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996
  5. //
  6. // File: share.cxx
  7. //
  8. // Contents: Active Directory Share manipulation
  9. //
  10. // History: 08-06-96 t-danal created for oledscmd from addfsh, delfsh
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "main.hxx"
  14. #include "macro.hxx"
  15. #include "sconv.hxx"
  16. #include "varconv.hxx"
  17. #define MAX_ADS_ENUM 100
  18. //
  19. // Dispatch Table Defs
  20. //
  21. #include "dispdef.hxx"
  22. DEFEXEC(ExecShareAdd);
  23. DEFEXEC(ExecShareDel);
  24. DEFDISPTABLE(DispTable) = {
  25. {"add", NULL, ExecShareAdd},
  26. {"del", NULL, ExecShareDel}
  27. };
  28. DEFDISPSIZE(nDispTable, DispTable);
  29. //
  30. // Static globals
  31. //
  32. static LPWSTR gpszDescription = NULL;
  33. static LPWSTR gpszPath = NULL;
  34. static LONG glMaxUserCount;
  35. //
  36. // Local functions
  37. //
  38. HRESULT
  39. SetFileShareProperties(
  40. IADs *
  41. );
  42. HRESULT
  43. AddFileShare(
  44. LPWSTR szParentContainer,
  45. LPWSTR szFileShareName
  46. );
  47. HRESULT
  48. DeleteFileShare(
  49. LPWSTR szParentContainer,
  50. LPWSTR szFileShareName
  51. );
  52. //
  53. // Local functions definitions
  54. //
  55. HRESULT
  56. SetFileShareProperties(IADs *pADs)
  57. {
  58. HRESULT hr = S_OK;
  59. IADsFileShare *pFileShare = NULL;
  60. BSTR bstrDescription = NULL;
  61. BSTR bstrPath = NULL;
  62. VARIANT var;
  63. hr = pADs->QueryInterface(IID_IADsFileShare,
  64. (void **)&pFileShare);
  65. BAIL_ON_FAILURE(hr);
  66. bstrDescription = SysAllocString(gpszDescription);
  67. VariantInit(&var);
  68. PackString2Variant(gpszDescription, &var);
  69. hr = pFileShare->Put(L"Description", var);
  70. VariantClear(&var);
  71. VariantInit(&var);
  72. PackString2Variant(gpszDescription, &var);
  73. hr = pFileShare->Put(L"Description", var);
  74. VariantClear(&var);
  75. BAIL_ON_FAILURE(hr);
  76. VariantInit(&var);
  77. PackDWORD2Variant(glMaxUserCount, &var);
  78. hr = pFileShare->Put(L"MaxUserCount", var);
  79. VariantClear(&var);
  80. BAIL_ON_FAILURE(hr);
  81. VariantInit(&var);
  82. PackString2Variant(gpszPath, &var);
  83. hr = pFileShare->Put(L"Path", var);
  84. VariantClear(&var);
  85. BAIL_ON_FAILURE(hr);
  86. hr = S_OK;
  87. error:
  88. if(pFileShare)
  89. pFileShare->Release();
  90. SysFreeString(bstrPath);
  91. SysFreeString(bstrDescription);
  92. return(hr);
  93. }
  94. HRESULT
  95. AddFileShare(
  96. LPWSTR szParentContainer,
  97. LPWSTR szFileShareName
  98. )
  99. {
  100. HRESULT hr;
  101. IADsContainer * pADsParent = NULL;
  102. IDispatch * pDispatch = NULL;
  103. IADs * pADs = NULL;
  104. hr = ADsGetObject(
  105. szParentContainer,
  106. IID_IADsContainer,
  107. (void **)&pADsParent
  108. );
  109. BAIL_ON_FAILURE(hr);
  110. hr = pADsParent->Create(L"fileshare",
  111. szFileShareName,
  112. &pDispatch);
  113. BAIL_ON_FAILURE(hr);
  114. hr = pDispatch->QueryInterface(
  115. IID_IADs,
  116. (void **)&pADs
  117. );
  118. BAIL_ON_FAILURE(hr);
  119. //
  120. // set mandatory properties
  121. //
  122. hr = SetFileShareProperties(pADs);
  123. BAIL_ON_FAILURE(hr);
  124. hr = pADs->SetInfo();
  125. BAIL_ON_FAILURE(hr);
  126. error:
  127. if (pADsParent) {
  128. pADsParent->Release();
  129. }
  130. if (pDispatch) {
  131. pDispatch->Release();
  132. }
  133. if (pADs) {
  134. pADs->Release();
  135. }
  136. return(hr);
  137. }
  138. HRESULT
  139. DeleteFileShare(
  140. LPWSTR szParentContainer,
  141. LPWSTR szFileShareName
  142. )
  143. {
  144. HRESULT hr;
  145. IADsContainer * pADsParent = NULL;
  146. IUnknown * pUnknown = NULL;
  147. IADs * pADs = NULL;
  148. hr = ADsGetObject(
  149. szParentContainer,
  150. IID_IADsContainer,
  151. (void **)&pADsParent
  152. );
  153. BAIL_ON_FAILURE(hr);
  154. hr = pADsParent->Delete(L"FileShare",
  155. szFileShareName);
  156. if(SUCCEEDED(hr)){
  157. printf("File Share successfully deleted\n");
  158. } else {
  159. printf("Failed to delete file share\n");
  160. }
  161. BAIL_ON_FAILURE(hr);
  162. error:
  163. if (pADsParent) {
  164. pADsParent->Release();
  165. }
  166. if (pUnknown) {
  167. pUnknown->Release();
  168. }
  169. if (pADs) {
  170. pADs->Release();
  171. }
  172. return(hr);
  173. }
  174. //
  175. // Exec function definitions
  176. //
  177. int
  178. ExecShare(char *szProgName, char *szAction, int argc, char * argv[])
  179. {
  180. if (!argc) {
  181. PrintUsage(szProgName, szAction, DispTable, nDispTable);
  182. return(1);
  183. }
  184. char *szPrevActions = szAction;
  185. szAction = argv[0];
  186. argc--;
  187. argv++;
  188. if (DoHelp(szProgName,
  189. szPrevActions, szAction, NULL,
  190. DispTable, nDispTable,
  191. NULL))
  192. return 0;
  193. return DispatchExec(DispTable, nDispTable,
  194. szProgName,
  195. szPrevActions, szAction,
  196. argc, argv);
  197. }
  198. int
  199. ExecShareAdd(char *szProgName, char *szAction, int argc, char * argv[])
  200. {
  201. HRESULT hr;
  202. LPWSTR szParentContainer = NULL;
  203. LPWSTR szFileShareName = NULL;
  204. if (argc != 5) {
  205. PrintUsage(szProgName, szAction,
  206. "<ParentContainer> <ShareName> <description> "
  207. "<maxusercount> <path>");
  208. return(1);
  209. }
  210. szParentContainer = AllocateUnicodeString(argv[0]);
  211. szFileShareName = AllocateUnicodeString(argv[1]);
  212. gpszDescription = AllocateUnicodeString(argv[2]);
  213. glMaxUserCount = atol(argv[3]);
  214. gpszPath = AllocateUnicodeString(argv[4]);
  215. hr = AddFileShare(
  216. szParentContainer,
  217. szFileShareName
  218. );
  219. FreeUnicodeString(szFileShareName);
  220. FreeUnicodeString(szParentContainer);
  221. FreeUnicodeString(gpszPath);
  222. FreeUnicodeString(gpszDescription);
  223. if (FAILED(hr)) {
  224. printf("AddFileShare failed with error code %x\n", hr);
  225. return(1);
  226. }
  227. printf("Successfully added fileshare \n");
  228. return(0);
  229. }
  230. int
  231. ExecShareDel(char *szProgName, char *szAction, int argc, char * argv[])
  232. {
  233. HRESULT hr;
  234. LPWSTR szParentContainer = NULL;
  235. LPWSTR szFileShareName = NULL;
  236. if (argc != 2) {
  237. PrintUsage(szProgName, szAction,
  238. "<ADs ParentContainer> <FileShareName>");
  239. return(1);
  240. }
  241. szParentContainer = AllocateUnicodeString(argv[0]);
  242. szFileShareName = AllocateUnicodeString(argv[1]);
  243. hr = DeleteFileShare(
  244. szParentContainer,
  245. szFileShareName
  246. );
  247. FreeUnicodeString(szFileShareName);
  248. FreeUnicodeString(szParentContainer);
  249. if (FAILED(hr)) {
  250. printf("DeleteFileShare failed with error code %x\n", hr);
  251. return(1);
  252. }
  253. return(0);
  254. }