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.

479 lines
12 KiB

  1. //
  2. // MySvrApi.cpp
  3. //
  4. // Thunk layer for SVRAPI.DLL (Win9x and NT)
  5. //
  6. //
  7. #include "stdafx.h"
  8. #include "mysvrapi.h"
  9. #include "TheApp.h"
  10. #include "cstrinout.h"
  11. #include <lm.h>
  12. //
  13. // Conversion classes.
  14. //
  15. class CShareInfo50to502
  16. {
  17. public:
  18. CShareInfo50to502(LPBYTE* ppBuff) {_ppBuffOut = ppBuff;}
  19. operator char*() {return _aBuffIn;}
  20. USHORT SizeOfBuffer() {return sizeof(_aBuffIn);}
  21. BOOL Convert();
  22. protected:
  23. CShareInfo50to502() {};
  24. ULONG SizeRequired(const share_info_50* psi50);
  25. void CopyData(const share_info_50* psi50, SHARE_INFO_502* psi502, WCHAR** ppsz, ULONG* pcch);
  26. private:
  27. void CopyStringAndAdvancePointer(LPCSTR pszSrc, LPWSTR* ppszDst, ULONG* pcch);
  28. DWORD ConvertPermissions(USHORT shi50_flags);
  29. private:
  30. BYTE** _ppBuffOut;
  31. char _aBuffIn[sizeof(share_info_50) + 2*MAX_PATH];
  32. };
  33. ULONG CShareInfo50to502::SizeRequired(const share_info_50* psi50)
  34. {
  35. ULONG cb = sizeof(SHARE_INFO_502);
  36. cb += sizeof(WCHAR) * MultiByteToWideChar(CP_ACP, 0, psi50->shi50_netname, -1, NULL, 0);
  37. cb += sizeof(WCHAR) * MultiByteToWideChar(CP_ACP, 0, (psi50->shi50_remark ? psi50->shi50_remark : ""), -1, NULL, 0);
  38. cb += sizeof(WCHAR) * MultiByteToWideChar(CP_ACP, 0, (psi50->shi50_path ? psi50->shi50_path : ""), -1, NULL, 0);
  39. cb += sizeof(WCHAR) * MultiByteToWideChar(CP_ACP, 0, psi50->shi50_rw_password, -1, NULL, 0);
  40. return cb;
  41. }
  42. void CShareInfo50to502::CopyStringAndAdvancePointer(LPCSTR pszSrc, LPWSTR* ppszDst, ULONG* pcch)
  43. {
  44. int cch = SHAnsiToUnicode(pszSrc, *ppszDst, *pcch);
  45. *ppszDst += cch;
  46. *pcch -= cch;
  47. }
  48. DWORD CShareInfo50to502::ConvertPermissions(USHORT shi50_flags)
  49. {
  50. DWORD dwRet;
  51. if (shi50_flags & SHI50F_FULL)
  52. {
  53. dwRet = ACCESS_ALL;
  54. }
  55. else if (shi50_flags & SHI50F_RDONLY)
  56. {
  57. dwRet = ACCESS_READ;
  58. }
  59. else
  60. {
  61. dwRet = 0;
  62. }
  63. return dwRet | SHI50F_PERSIST;
  64. }
  65. void CShareInfo50to502::CopyData(const share_info_50* psi50, SHARE_INFO_502* psi502, WCHAR** ppsz, ULONG* pcch)
  66. {
  67. psi502->shi502_type = psi50->shi50_type;
  68. psi502->shi502_permissions = ConvertPermissions(psi50->shi50_flags);
  69. psi502->shi502_max_uses = 0;
  70. psi502->shi502_current_uses = 0;
  71. psi502->shi502_reserved = 0;
  72. psi502->shi502_security_descriptor = NULL;
  73. psi502->shi502_netname = *ppsz;
  74. CopyStringAndAdvancePointer(psi50->shi50_netname, ppsz, pcch);
  75. psi502->shi502_remark = *ppsz;
  76. CopyStringAndAdvancePointer(psi50->shi50_remark, ppsz, pcch);
  77. psi502->shi502_path = *ppsz;
  78. CopyStringAndAdvancePointer(psi50->shi50_path, ppsz, pcch);
  79. psi502->shi502_passwd = *ppsz;
  80. CopyStringAndAdvancePointer(psi50->shi50_rw_password, ppsz, pcch);
  81. }
  82. BOOL CShareInfo50to502::Convert()
  83. {
  84. ULONG cb = SizeRequired((share_info_50*)_aBuffIn);
  85. *_ppBuffOut = (BYTE*)LocalAlloc(LPTR, cb);
  86. if (*_ppBuffOut)
  87. {
  88. WCHAR* psz = (WCHAR*)((BYTE*)*_ppBuffOut + sizeof(SHARE_INFO_502));
  89. ULONG cch = (cb - sizeof(SHARE_INFO_502)) / sizeof(WCHAR);
  90. CopyData((share_info_50*)_aBuffIn, (SHARE_INFO_502*)*_ppBuffOut, &psz, &cch);
  91. }
  92. return (*_ppBuffOut != NULL);
  93. }
  94. //
  95. //
  96. //
  97. class CMultiShareInfo50to502 : public CShareInfo50to502
  98. {
  99. public:
  100. CMultiShareInfo50to502(BYTE** ppBuff, const char* pData, ULONG nItems);
  101. BOOL Convert();
  102. private:
  103. ULONG MultiSizeRequired();
  104. void MultiCopyData(ULONG cb);
  105. private:
  106. SHARE_INFO_502** _ppBuffOut;
  107. const share_info_50* _pDataIn;
  108. ULONG _nItems;
  109. };
  110. CMultiShareInfo50to502::CMultiShareInfo50to502(BYTE** ppBuff, const char* pData, ULONG nItems)
  111. {
  112. _ppBuffOut = (SHARE_INFO_502**)ppBuff;
  113. _pDataIn = (share_info_50*)pData;
  114. _nItems = nItems;
  115. }
  116. ULONG CMultiShareInfo50to502::MultiSizeRequired()
  117. {
  118. ULONG cbRet = 0;
  119. for (ULONG i = 0; i < _nItems; i++)
  120. cbRet += SizeRequired(&_pDataIn[i]);
  121. return cbRet;
  122. }
  123. void CMultiShareInfo50to502::MultiCopyData(ULONG cb)
  124. {
  125. WCHAR* psz = (WCHAR*)((BYTE*)*_ppBuffOut + (sizeof(SHARE_INFO_502) * _nItems));
  126. ULONG cch = (cb - (sizeof(SHARE_INFO_502) * _nItems)) / sizeof(WCHAR);
  127. for (ULONG i = 0; i < _nItems; i++)
  128. CopyData(&_pDataIn[i], &(*_ppBuffOut)[i], &psz, &cch);
  129. }
  130. BOOL CMultiShareInfo50to502::Convert()
  131. {
  132. ULONG cb = MultiSizeRequired();
  133. *_ppBuffOut = (SHARE_INFO_502*)LocalAlloc(LPTR, cb);
  134. if (*_ppBuffOut)
  135. {
  136. MultiCopyData(cb);
  137. }
  138. return *_ppBuffOut != NULL;
  139. }
  140. //
  141. //
  142. //
  143. class CShareInfo502to50
  144. {
  145. public:
  146. CShareInfo502to50(BYTE* pBuff) {_pBuffIn = pBuff;}
  147. operator char*();
  148. WORD SizeOfBuffer() {return sizeof(_aBuff);}
  149. private:
  150. void CopyData();
  151. void CopyStringAndAdvancePointer(LPCWSTR pszSrc, LPSTR* ppszDst, ULONG* pcch);
  152. WORD ConvertPermissions(DWORD shi502_permissions);
  153. private:
  154. BYTE* _pBuffIn;
  155. BYTE _aBuff[sizeof(share_info_50) + 256];
  156. };
  157. WORD CShareInfo502to50::ConvertPermissions(DWORD shi502_permissions)
  158. {
  159. WORD wRet;
  160. if (shi502_permissions & (ACCESS_ALL ^ ACCESS_READ))
  161. {
  162. wRet = SHI50F_FULL;
  163. }
  164. else if (shi502_permissions & ACCESS_READ)
  165. {
  166. wRet = SHI50F_RDONLY;
  167. }
  168. else
  169. {
  170. wRet = 0;
  171. }
  172. return wRet | SHI50F_PERSIST; // Always persist share info.
  173. }
  174. void CShareInfo502to50::CopyStringAndAdvancePointer(LPCWSTR pszSrc, LPSTR* ppszDst, ULONG* pcch)
  175. {
  176. int cch = SHUnicodeToAnsi(pszSrc, *ppszDst, *pcch);
  177. *ppszDst += cch;
  178. *pcch -= cch;
  179. }
  180. void CShareInfo502to50::CopyData()
  181. {
  182. share_info_50* psi50 = (share_info_50*)_aBuff;
  183. SHARE_INFO_502* psi502 = (SHARE_INFO_502*)_pBuffIn;
  184. char* psz = (char*)(_aBuff + sizeof(share_info_50));
  185. ULONG cch = ARRAYSIZE(_aBuff) - sizeof(share_info_50);
  186. psi50->shi50_type = (BYTE)psi502->shi502_type;
  187. psi50->shi50_flags = ConvertPermissions(psi502->shi502_permissions);
  188. psi50->shi50_ro_password[0] = '\0';
  189. WideCharToMultiByte(CP_ACP, 0, psi502->shi502_netname, -1, psi50->shi50_netname,
  190. ARRAYSIZE(psi50->shi50_netname), NULL, NULL);
  191. WideCharToMultiByte(CP_ACP, 0, psi502->shi502_passwd, -1, psi50->shi50_rw_password,
  192. ARRAYSIZE(psi50->shi50_rw_password), NULL, NULL);
  193. if (psi502->shi502_remark)
  194. {
  195. psi50->shi50_remark = psz;
  196. CopyStringAndAdvancePointer(psi502->shi502_remark, &psz, &cch);
  197. }
  198. else
  199. {
  200. psi50->shi50_remark = NULL;
  201. }
  202. if (psi502->shi502_path)
  203. {
  204. psi50->shi50_path = psz;
  205. CopyStringAndAdvancePointer(psi502->shi502_path, &psz, &cch);
  206. }
  207. else
  208. {
  209. psi502->shi502_path = NULL;
  210. }
  211. }
  212. CShareInfo502to50::operator char*()
  213. {
  214. char* pRet;
  215. if (_pBuffIn)
  216. {
  217. CopyData();
  218. pRet = (char*)_aBuff;
  219. }
  220. else
  221. {
  222. pRet = NULL;
  223. }
  224. return pRet;
  225. }
  226. //
  227. //
  228. //
  229. NET_API_STATUS NetShareEnumWrap(LPCTSTR pszServer, DWORD level, LPBYTE * ppBuffer, DWORD dwPrefMaxLen, LPDWORD pdwEntriesRead, LPDWORD pdwTotalEntries, LPDWORD dsResumeHandle)
  230. {
  231. ASSERTMSG(502==level, "NetShareEnumWrap doesn't thunk the requestesd buffer level");
  232. NET_API_STATUS nasRet;
  233. if (!theApp.IsWindows9x())
  234. {
  235. nasRet = NetShareEnum_NT((LPWSTR)pszServer, level, ppBuffer, dwPrefMaxLen, pdwEntriesRead, pdwTotalEntries, dsResumeHandle);
  236. }
  237. else
  238. {
  239. CStrIn cstrServer(pszServer);
  240. USHORT cb = sizeof(share_info_50) + (2 * MAX_PATH);
  241. char* pData = (char*)LocalAlloc(LPTR, cb);
  242. if (pData)
  243. {
  244. *pdwEntriesRead = *pdwTotalEntries = 0;
  245. nasRet = NetShareEnum_W95(cstrServer, 50, pData, cb, (USHORT*)pdwEntriesRead, (USHORT*)pdwTotalEntries);
  246. if (*pdwEntriesRead < *pdwTotalEntries)
  247. {
  248. LocalFree(pData);
  249. cb = (USHORT)((sizeof(share_info_50) + (2 * MAX_PATH)) * (*pdwTotalEntries));
  250. pData = (char*)LocalAlloc(LPTR, cb);
  251. if (pData)
  252. {
  253. nasRet = NetShareEnum_W95(cstrServer, 50, pData, cb, (USHORT*)pdwEntriesRead, (USHORT*)pdwTotalEntries);
  254. }
  255. }
  256. if (NERR_Success == nasRet)
  257. {
  258. CMultiShareInfo50to502 cmnsio(ppBuffer, pData, *pdwEntriesRead);
  259. if (!cmnsio.Convert())
  260. nasRet = ERROR_NOT_ENOUGH_MEMORY;
  261. }
  262. LocalFree(pData);
  263. }
  264. else
  265. {
  266. nasRet = ERROR_NOT_ENOUGH_MEMORY;
  267. }
  268. }
  269. return nasRet;
  270. }
  271. NET_API_STATUS NetShareAddWrap(LPCTSTR pszServer, DWORD level, LPBYTE buffer)
  272. {
  273. ASSERTMSG(502==level, "NetShareAddWrap doesn't thunk the requested buffer level");
  274. NET_API_STATUS nasRet;
  275. if (!theApp.IsWindows9x())
  276. {
  277. nasRet = NetShareAdd_NT((LPTSTR)pszServer, level, buffer, NULL);
  278. }
  279. else
  280. {
  281. if (502 == level)
  282. {
  283. CStrIn cstrServer(pszServer);
  284. CShareInfo502to50 CSI(buffer);
  285. nasRet = NetShareAdd_W95(cstrServer, 50, CSI, sizeof(share_info_50));
  286. }
  287. else
  288. {
  289. nasRet = ERROR_INVALID_LEVEL;
  290. }
  291. }
  292. return nasRet;
  293. }
  294. NET_API_STATUS NetShareDelWrap(LPCTSTR pszServer, LPCTSTR pszNetName, DWORD reserved)
  295. {
  296. ASSERTMSG(0==reserved, "NetShareDelWrap called with non-zero reserved parameter");
  297. NET_API_STATUS nasRet;
  298. if (!theApp.IsWindows9x())
  299. {
  300. nasRet = NetShareDel_NT((LPTSTR)pszServer, (LPTSTR)pszNetName, 0);
  301. }
  302. else
  303. {
  304. CStrIn cstrServer(pszServer);
  305. CStrIn cstrNetName(pszNetName);
  306. nasRet = NetShareDel_W95(cstrServer, cstrNetName, 0);
  307. }
  308. return nasRet;
  309. }
  310. NET_API_STATUS NetShareGetInfoWrap(LPCTSTR pszServer, LPCTSTR pszNetName, DWORD level, LPBYTE * ppbuffer)
  311. {
  312. NET_API_STATUS nasRet;
  313. if (!theApp.IsWindows9x())
  314. {
  315. nasRet = NetShareGetInfo_NT((LPTSTR)pszServer, (LPTSTR)pszNetName, level, ppbuffer);
  316. }
  317. else
  318. {
  319. if (502==level)
  320. {
  321. CShareInfo50to502 CNSIOut(ppbuffer);
  322. CStrIn cstrServer(pszServer);
  323. CStrIn cstrNetName(pszNetName);
  324. USHORT n;
  325. nasRet = NetShareGetInfo_W95(cstrServer, cstrNetName, 50, CNSIOut, CNSIOut.SizeOfBuffer(), &n);
  326. if (NERR_Success == nasRet)
  327. {
  328. if (!CNSIOut.Convert())
  329. nasRet = ERROR_NOT_ENOUGH_MEMORY;
  330. }
  331. }
  332. else
  333. {
  334. *ppbuffer = NULL;
  335. nasRet = ERROR_INVALID_LEVEL;
  336. }
  337. }
  338. return nasRet;
  339. }
  340. NET_API_STATUS NetShareSetInfoWrap(LPCTSTR pszServer, LPCTSTR pszNetName, DWORD level, LPBYTE buffer)
  341. {
  342. NET_API_STATUS nasRet;
  343. if (!theApp.IsWindows9x())
  344. {
  345. nasRet = NetShareSetInfo_NT((LPTSTR)pszServer, (LPTSTR)pszNetName, level, buffer, NULL);
  346. }
  347. else
  348. {
  349. if (502==level)
  350. {
  351. CStrIn cstrServer(pszServer);
  352. CStrIn cstrNetName(pszNetName);
  353. CShareInfo502to50 CNSI(buffer);
  354. nasRet = NetShareSetInfo_W95(cstrServer, cstrNetName, 50, CNSI, sizeof(share_info_50), NULL);
  355. }
  356. else
  357. {
  358. nasRet = ERROR_INVALID_LEVEL;
  359. }
  360. }
  361. return nasRet;
  362. }
  363. NET_API_STATUS NetApiBufferFreeWrap(LPVOID p)
  364. {
  365. NET_API_STATUS nasRet;
  366. if (p)
  367. {
  368. if (!theApp.IsWindows9x())
  369. {
  370. nasRet = NetApiBufferFree_NT(p);
  371. }
  372. else
  373. {
  374. LocalFree(p);
  375. nasRet = NERR_Success;
  376. }
  377. }
  378. else
  379. {
  380. nasRet = NERR_Success;
  381. }
  382. return nasRet;
  383. }