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.

487 lines
14 KiB

  1. /*++
  2. Copyright (c) 1991-1993 Microsoft Corporation
  3. Module Name:
  4. Use.c
  5. Abstract:
  6. This file contains the RpcXlate code to handle the NetUse APIs that can't
  7. be handled by simple calls to RxRemoteApi.
  8. Author:
  9. John Rogers (JohnRo) 17-Jun-1991
  10. Environment:
  11. Portable to any flat, 32-bit environment. (Uses Win32 typedefs.)
  12. Requires ANSI C extensions: slash-slash comments, long external names.
  13. Revision History:
  14. 17-Jun-1991 JohnRo
  15. Created.
  16. 18-Jun-1991 JohnRo
  17. Changed RxNetUse routines to use LPBYTE rather than LPVOID parameters,
  18. for consistency with NetUse routines.
  19. 20-Jun-1991 JohnRo
  20. RitaW told me about a MIPS build error (incorrect cast).
  21. 29-Jul-1991 JohnRo
  22. Level 2 is NT only, so return ERROR_NOT_SUPPORTED for it. Also use
  23. LM20_ equates for lengths.
  24. 15-Oct-1991 JohnRo
  25. Be paranoid about possible infinite loop.
  26. 21-Nov-1991 JohnRo
  27. Removed NT dependencies to reduce recompiles.
  28. 07-Feb-1992 JohnRo
  29. Use NetApiBufferAllocate() instead of private version.
  30. 02-Sep-1992 JohnRo
  31. RAID 5150: NetUseAdd to downlevel fails.
  32. Use PREFIX_ equates.
  33. Quiet normal debug output.
  34. Avoid compiler warnings.
  35. 27-Jan-1993 JohnRo
  36. RAID 8926: NetConnectionEnum to downlevel: memory leak on error.
  37. Also set buffer pointer to NULL if success but no entries returned.
  38. Use NetpKdPrint() where possible.
  39. --*/
  40. // These must be included first:
  41. #include <windef.h> // IN, DWORD, etc.
  42. #include <lmcons.h> // LM20_ equates, NET_API_STATUS, etc.
  43. // These may be included in any order:
  44. #include <apinums.h> // API_ equates.
  45. #include <lmapibuf.h> // NetApiBufferAllocate().
  46. #include <lmerr.h> // ERROR_ and NERR_ equates.
  47. #include <lmuse.h> // USE_INFO_0, etc.
  48. #include <netdebug.h> // DBGSTATIC, NetpKdPrint(), FORMAT_ equates.
  49. #include <netlib.h> // NetpSetParmError().
  50. #include <prefix.h> // PREFIX_ equates.
  51. #include <rap.h> // LPDESC.
  52. #include <remdef.h> // REM16_, REM32_, REMSmb_ equates.
  53. #include <rx.h> // RxRemoteApi().
  54. #include <rxp.h> // RxpFatalErrorCode().
  55. #include <rxpdebug.h> // IF_DEBUG().
  56. #include <rxuse.h> // My prototypes.
  57. #define MAX_USE_INFO_0_STRING_LEN \
  58. (LM20_DEVLEN+1 + MAX_PATH+1)
  59. #define MAX_USE_INFO_1_STRING_LEN \
  60. (MAX_USE_INFO_0_STRING_LEN + LM20_PWLEN+1)
  61. #define MAX_USE_INFO_0_STRING_SIZE \
  62. ( MAX_USE_INFO_0_STRING_LEN * sizeof(TCHAR) )
  63. #define MAX_USE_INFO_1_STRING_SIZE \
  64. ( MAX_USE_INFO_1_STRING_LEN * sizeof(TCHAR) )
  65. #define ENUM_ARRAY_OVERHEAD_SIZE 0
  66. DBGSTATIC NET_API_STATUS
  67. RxpGetUseDataDescs(
  68. IN DWORD Level,
  69. OUT LPDESC * DataDesc16,
  70. OUT LPDESC * DataDesc32,
  71. OUT LPDESC * DataDescSmb,
  72. OUT LPDWORD ApiBufferSize32 OPTIONAL
  73. )
  74. {
  75. switch (Level) {
  76. case 0 :
  77. *DataDesc16 = REM16_use_info_0;
  78. *DataDesc32 = REM32_use_info_0;
  79. *DataDescSmb = REMSmb_use_info_0;
  80. NetpSetOptionalArg(
  81. ApiBufferSize32,
  82. sizeof(USE_INFO_0) + MAX_USE_INFO_0_STRING_SIZE);
  83. return (NERR_Success);
  84. case 1 :
  85. *DataDesc16 = REM16_use_info_1;
  86. *DataDesc32 = REM32_use_info_1;
  87. *DataDescSmb = REMSmb_use_info_1;
  88. NetpSetOptionalArg(
  89. ApiBufferSize32,
  90. sizeof(USE_INFO_1) + MAX_USE_INFO_1_STRING_SIZE);
  91. return (NERR_Success);
  92. // Level 2 is NT-only (contains user name), so it doesn't get handled
  93. // by us.
  94. case 2 :
  95. return (ERROR_NOT_SUPPORTED);
  96. default :
  97. return (ERROR_INVALID_LEVEL);
  98. }
  99. /* NOTREACHED */
  100. } // RxpGetUseDataDescs
  101. NET_API_STATUS
  102. RxNetUseAdd(
  103. IN LPTSTR UncServerName,
  104. IN DWORD Level,
  105. IN LPBYTE UseInfoStruct,
  106. OUT LPDWORD ParmError OPTIONAL // (This name needed by NetpSetParmError.)
  107. )
  108. /*++
  109. Routine Description:
  110. RxNetUseAdd performs the same function as NetUseAdd, except that the
  111. server name is known to refer to a downlevel server.
  112. Arguments:
  113. (Same as NetUseAdd, except UncServerName must not be null, and must not
  114. refer to the local computer.)
  115. Return Value:
  116. (Same as NetUseAdd.)
  117. --*/
  118. {
  119. LPDESC DataDesc16, DataDesc32, DataDescSmb;
  120. DWORD MaxEntrySize;
  121. NET_API_STATUS Status;
  122. // Life is easier if we set this for failure, and change it if this API
  123. // call actually succeeds.
  124. NetpSetParmError( PARM_ERROR_UNKNOWN );
  125. if ( UseInfoStruct == NULL )
  126. return ERROR_INVALID_PARAMETER;
  127. Status = RxpGetUseDataDescs(
  128. Level,
  129. & DataDesc16,
  130. & DataDesc32,
  131. & DataDescSmb,
  132. & MaxEntrySize); // API buffer size 32
  133. if (Status != NERR_Success) {
  134. return (Status);
  135. }
  136. NetpAssert(UncServerName != NULL);
  137. Status = RxRemoteApi(
  138. API_WUseAdd, // API number
  139. UncServerName, // Required, with \\name.
  140. REMSmb_NetUseAdd_P, // parm desc string
  141. DataDesc16,
  142. DataDesc32,
  143. DataDescSmb,
  144. NULL, // no aux desc 16
  145. NULL, // no aux desc 32
  146. NULL, // no aux desc Smb
  147. FALSE, // not a null session API.
  148. // rest of API's arguments, in 32-bit LM 2.x form:
  149. Level, // sLevel
  150. UseInfoStruct, // pbBuffer
  151. MaxEntrySize ); // cbBuffer
  152. if (Status == NERR_Success) {
  153. NetpSetParmError( PARM_ERROR_NONE );
  154. }
  155. IF_DEBUG( USE ) {
  156. NetpKdPrint(( PREFIX_NETAPI "RxNetUseAdd: after RxRemoteApi, Status="
  157. FORMAT_API_STATUS ".\n", Status ));
  158. }
  159. return (Status);
  160. } // RxNetUseAdd
  161. NET_API_STATUS
  162. RxNetUseDel(
  163. IN LPTSTR UncServerName,
  164. IN LPTSTR UseName,
  165. IN DWORD ForceCond
  166. )
  167. {
  168. NetpAssert(UncServerName != NULL);
  169. return (RxRemoteApi(
  170. API_WUseDel, // API number
  171. UncServerName,
  172. REMSmb_NetUseDel_P, // parm desc
  173. NULL, // no data desc 16
  174. NULL, // no data desc 32
  175. NULL, // no data desc SMB
  176. NULL, // no aux desc 16
  177. NULL, // no aux desc 32
  178. NULL, // no aux desc SMB
  179. FALSE, // not a null session API
  180. // rest of API's arguments, in 32-bit LM 2.x format:
  181. UseName,
  182. ForceCond) );
  183. }
  184. NET_API_STATUS
  185. RxNetUseEnum (
  186. IN LPTSTR UncServerName,
  187. IN DWORD Level,
  188. OUT LPBYTE *BufPtr,
  189. IN DWORD PreferedMaximumSize,
  190. OUT LPDWORD EntriesRead,
  191. OUT LPDWORD TotalEntries,
  192. IN OUT LPDWORD ResumeHandle OPTIONAL
  193. )
  194. /*++
  195. Routine Description:
  196. RxNetUseEnum performs the same function as NetUseEnum, except that the
  197. server name is known to refer to a downlevel server.
  198. Arguments:
  199. (Same as NetUseEnum, except UncServerName must not be null, and must not
  200. refer to the local computer.)
  201. Return Value:
  202. (Same as NetUseEnum.)
  203. --*/
  204. {
  205. LPDESC DataDesc16;
  206. LPDESC DataDesc32;
  207. LPDESC DataDescSmb;
  208. DWORD EntriesToAllocate;
  209. LPVOID InfoArray = NULL;
  210. DWORD InfoArraySize;
  211. DWORD MaxEntrySize;
  212. NET_API_STATUS Status;
  213. UNREFERENCED_PARAMETER(ResumeHandle);
  214. // Make sure caller didn't get confused.
  215. NetpAssert(UncServerName != NULL);
  216. if (BufPtr == NULL) {
  217. return (ERROR_INVALID_PARAMETER);
  218. }
  219. // Check for bad pointer before we do anything else.
  220. *BufPtr = NULL;
  221. Status = RxpGetUseDataDescs(
  222. Level,
  223. & DataDesc16,
  224. & DataDesc32,
  225. & DataDescSmb,
  226. & MaxEntrySize); // API buffer size 32
  227. if (Status != NERR_Success) {
  228. return (Status);
  229. }
  230. //
  231. // Because downlevel servers don't support resume handles, and we don't
  232. // have a way to say "close this resume handle" even if we wanted to
  233. // emulate them here, we have to do everthing in one shot. So, the first
  234. // time around, we'll try using the caller's prefered maximum, but we
  235. // will enlarge that until we can get everything in one buffer.
  236. //
  237. // First time: try caller's prefered maximum.
  238. NetpAdjustPreferedMaximum (
  239. PreferedMaximumSize, // caller's request
  240. MaxEntrySize, // byte count per array element
  241. ENUM_ARRAY_OVERHEAD_SIZE, // num bytes overhead to show array end
  242. NULL, // we'll compute byte counts ourselves.
  243. & EntriesToAllocate); // num of entries we can get.
  244. //
  245. // Loop until we have enough memory or we die for some other reason.
  246. //
  247. do {
  248. //
  249. // Figure out how much memory we need, within the protocol limit.
  250. //
  251. InfoArraySize = (EntriesToAllocate * MaxEntrySize)
  252. + ENUM_ARRAY_OVERHEAD_SIZE;
  253. if (InfoArraySize > MAX_TRANSACT_RET_DATA_SIZE) {
  254. InfoArraySize = MAX_TRANSACT_RET_DATA_SIZE;
  255. }
  256. //
  257. // Alloc memory for the array.
  258. //
  259. Status = NetApiBufferAllocate( InfoArraySize, & InfoArray );
  260. if (Status != NERR_Success) {
  261. return (Status);
  262. }
  263. //
  264. // Remote the API, and see if we've got enough space in the array.
  265. //
  266. Status = RxRemoteApi(
  267. API_WUseEnum, // api number
  268. UncServerName, // \\servername
  269. REMSmb_NetUseEnum_P, // parm desc (SMB version)
  270. DataDesc16,
  271. DataDesc32,
  272. DataDescSmb,
  273. NULL, // no aux desc 16
  274. NULL, // no aux desc 32
  275. NULL, // no aux desc SMB
  276. 0, // flags: normal
  277. // rest of API's arguments in 32-bit LM 2.x format:
  278. Level, // sLevel: info level
  279. InfoArray, // pbBuffer: info lvl array
  280. InfoArraySize, // cbBuffer: info lvl array len
  281. EntriesRead, // pcEntriesRead
  282. TotalEntries); // pcTotalAvail
  283. //
  284. // If the server returned ERROR_MORE_DATA, free the buffer and try
  285. // again. (Actually, if we already tried 64K, then forget it.)
  286. //
  287. NetpAssert( InfoArraySize <= MAX_TRANSACT_RET_DATA_SIZE );
  288. if (Status != ERROR_MORE_DATA) {
  289. break;
  290. } else if (InfoArraySize == MAX_TRANSACT_RET_DATA_SIZE) {
  291. NetpKdPrint(( PREFIX_NETAPI
  292. "RxNetUseEnum: "
  293. "**WARNING** protocol limit reached (64KB).\n" ));
  294. break;
  295. }
  296. (void) NetApiBufferFree( InfoArray );
  297. InfoArray = NULL;
  298. NetpAssert( EntriesToAllocate < *TotalEntries );
  299. EntriesToAllocate = *TotalEntries;
  300. } while (Status == ERROR_MORE_DATA);
  301. if ( (Status == NO_ERROR) && ((*EntriesRead) > 0) ) {
  302. *BufPtr = InfoArray;
  303. } else {
  304. (VOID) NetApiBufferFree( InfoArray );
  305. NetpAssert( *BufPtr == NULL );
  306. }
  307. return (Status);
  308. } // RxNetUseEnum
  309. NET_API_STATUS
  310. RxNetUseGetInfo (
  311. IN LPTSTR UncServerName,
  312. IN LPTSTR UseName,
  313. IN DWORD Level,
  314. OUT LPBYTE *BufPtr
  315. )
  316. /*++
  317. Routine Description:
  318. RxNetUseGetInfo performs the same function as NetUseGetInfo, except that the
  319. server name is known to refer to a downlevel server.
  320. Arguments:
  321. (Same as NetUseGetInfo, except UncServerName must not be null, and must not
  322. refer to the local computer.)
  323. Return Value:
  324. (Same as NetUseGetInfo.)
  325. --*/
  326. {
  327. LPBYTE ApiBuffer32; // Buffer to be returned to caller.
  328. DWORD ApiBufferSize32;
  329. LPDESC DataDesc16, DataDesc32, DataDescSmb;
  330. NET_API_STATUS Status;
  331. DWORD TotalAvail;
  332. IF_DEBUG( USE ) {
  333. NetpKdPrint(( PREFIX_NETAPI
  334. "RxNetUseGetInfo: starting, server=" FORMAT_LPTSTR
  335. ", usename=" FORMAT_LPTSTR
  336. ", lvl=" FORMAT_DWORD ".\n",
  337. UncServerName, UseName, Level ));
  338. }
  339. NetpAssert(UncServerName != NULL);
  340. NetpAssert(UseName != NULL);
  341. // Pick which descriptors to use based on the info level.
  342. Status = RxpGetUseDataDescs(
  343. Level,
  344. & DataDesc16,
  345. & DataDesc32,
  346. & DataDescSmb,
  347. & ApiBufferSize32);
  348. if (Status != NERR_Success) {
  349. return (Status);
  350. }
  351. // Allocate memory for 32-bit version of info, which we'll return to
  352. // caller. (Caller must free it with NetApiBufferFree.)
  353. Status = NetApiBufferAllocate(
  354. ApiBufferSize32,
  355. (LPVOID *) & ApiBuffer32);
  356. if (Status != NERR_Success) {
  357. return (Status);
  358. }
  359. IF_DEBUG( USE ) {
  360. NetpKdPrint(( PREFIX_NETAPI
  361. "RxNetUseGetInfo: allocated buffer at " FORMAT_LPVOID
  362. "\n", (LPVOID) ApiBuffer32 ));
  363. }
  364. *BufPtr = ApiBuffer32;
  365. Status = RxRemoteApi(
  366. API_WUseGetInfo, // API number
  367. UncServerName, // Required, with \\name.
  368. REMSmb_NetUseGetInfo_P, // parm desc
  369. DataDesc16,
  370. DataDesc32,
  371. DataDescSmb,
  372. NULL, // no aux data desc 16
  373. NULL, // no aux data desc 32
  374. NULL, // no aux data desc SMB
  375. FALSE, // not a null session API
  376. // rest of API's arguments, in 32-bit LM 2.x format:
  377. UseName,
  378. Level,
  379. ApiBuffer32,
  380. ApiBufferSize32,
  381. & TotalAvail);
  382. return (Status);
  383. } // RxNetUseGetInfo