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.

365 lines
8.6 KiB

  1. /*************************************************************************
  2. *
  3. * DRIVE.C
  4. *
  5. * NT drive routines
  6. *
  7. * Copyright (c) 1995 Microsoft Corporation
  8. *
  9. * $Log: N:\NT\PRIVATE\NW4\NWSCRIPT\VCS\DRIVE.C $
  10. *
  11. * Rev 1.2 10 Apr 1996 14:22:12 terryt
  12. * Hotfix for 21181hq
  13. *
  14. * Rev 1.2 12 Mar 1996 19:53:22 terryt
  15. * Relative NDS names and merge
  16. *
  17. * Rev 1.1 22 Dec 1995 14:24:24 terryt
  18. * Add Microsoft headers
  19. *
  20. * Rev 1.0 15 Nov 1995 18:06:52 terryt
  21. * Initial revision.
  22. *
  23. * Rev 1.2 25 Aug 1995 16:22:38 terryt
  24. * Capture support
  25. *
  26. * Rev 1.1 23 May 1995 19:36:46 terryt
  27. * Spruce up source
  28. *
  29. * Rev 1.0 15 May 1995 19:10:30 terryt
  30. * Initial revision.
  31. *
  32. *************************************************************************/
  33. #include <stdio.h>
  34. #include <direct.h>
  35. #include <time.h>
  36. #include <stdlib.h>
  37. #include <nt.h>
  38. #include <ntrtl.h>
  39. #include <nturtl.h>
  40. #include <windows.h>
  41. #include <nwapi32.h>
  42. #include <nwapi.h>
  43. #include <npapi.h>
  44. #include <regapi.h>
  45. #include "nwscript.h"
  46. #include "ntnw.h"
  47. #include "inc/nwlibs.h"
  48. #include <mpr.h>
  49. extern unsigned char NW_PROVIDERA[];
  50. // now all SKUs have TerminalServer flag. If App Server is enabled, SingleUserTS flag is cleared
  51. #define IsTerminalServer() (BOOLEAN)(!(USER_SHARED_DATA->SuiteMask & (1 << SingleUserTS)))
  52. /********************************************************************
  53. GetFirstDrive
  54. Routine Description:
  55. Return the first non-local drive
  56. Arguments:
  57. pFirstDrive = pointer to drive
  58. 1-26
  59. Return Value:
  60. 0 = success
  61. F = failure
  62. ********************************************************************/
  63. unsigned int
  64. GetFirstDrive( unsigned short *pFirstDrive )
  65. {
  66. int i;
  67. char DriveName[10];
  68. unsigned int drivetype;
  69. HKEY hKey;
  70. char InitDrive[3] = "";
  71. DWORD dwTemp;
  72. if (IsTerminalServer()) {
  73. // Check if there is a override specified in the registry for the
  74. // initial NetWare drive (to prevent collisions with client drive mappings)
  75. if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
  76. REG_CONTROL_TSERVER,
  77. 0,
  78. KEY_READ,
  79. &hKey) == ERROR_SUCCESS) {
  80. dwTemp = sizeof(InitDrive);
  81. if (RegQueryValueExA(hKey,
  82. REG_CITRIX_INITIALNETWAREDRIVE_A,
  83. NULL,
  84. NULL,
  85. InitDrive,
  86. &dwTemp) != ERROR_SUCCESS) {
  87. }
  88. RegCloseKey(hKey);
  89. }
  90. // Original code defaulted to C:
  91. if (!isalpha(InitDrive[0])) {
  92. InitDrive[0] = 'C';
  93. }
  94. strcpy( DriveName, "A:\\" );
  95. dwTemp = toupper(InitDrive[0]) - 'A';
  96. }
  97. else {
  98. strcpy( DriveName, "A:\\" );
  99. dwTemp=2;
  100. }
  101. for ( i = dwTemp; i < 26; i++ ) {
  102. DriveName[0] = 'A' + i;
  103. drivetype = GetDriveTypeA( DriveName );
  104. if ( ( ( drivetype == DRIVE_REMOTE ) &&
  105. ( NTIsNetWareDrive( i ) ) ) ||
  106. ( drivetype == DRIVE_NO_ROOT_DIR ) ) {
  107. *pFirstDrive = i + 1;
  108. return 0x0000;
  109. }
  110. }
  111. return 0x000F;
  112. }
  113. /********************************************************************
  114. IsDriveRemote
  115. Routine Description:
  116. Is the given drive remote?
  117. Arguments:
  118. DriveNumber 1-26
  119. pRemote 0x1000 = remote, 0x0000 = local
  120. Return Value:
  121. 0 = success
  122. F = invalid drive
  123. ********************************************************************/
  124. unsigned int
  125. IsDriveRemote(
  126. unsigned char DriveNumber,
  127. unsigned int *pRemote
  128. )
  129. {
  130. char DriveName[10];
  131. unsigned int drivetype;
  132. strcpy( DriveName, "A:\\" );
  133. DriveName[0] = 'A' + DriveNumber;
  134. drivetype = GetDriveTypeA( DriveName );
  135. if ( drivetype == DRIVE_REMOTE ) {
  136. *pRemote = 0x1000;
  137. return 0;
  138. }
  139. else if ( drivetype == DRIVE_NO_ROOT_DIR ) {
  140. return 0xF;
  141. }
  142. else {
  143. *pRemote = 0;
  144. return 0;
  145. }
  146. }
  147. /********************************************************************
  148. NTNetWareDriveStatus
  149. Routine Description:
  150. Return the type of drive
  151. Arguments:
  152. DriveNumber - Number of drive 0-25
  153. Return Value:
  154. Combination of:
  155. NETWARE_NETWORK_DRIVE
  156. NETWARE_NETWARE_DRIVE
  157. NETWARE_LOCAL_FREE_DRIVE
  158. NETWARE_LOCAL_DRIVE
  159. *******************************************************************/
  160. unsigned short
  161. NTNetWareDriveStatus( unsigned short DriveNumber )
  162. {
  163. char DriveName[10];
  164. unsigned int drivetype;
  165. unsigned int Status = 0;
  166. strcpy( DriveName, "A:\\" );
  167. DriveName[0] = 'A' + DriveNumber;
  168. drivetype = GetDriveTypeA( DriveName );
  169. if ( drivetype == DRIVE_REMOTE ) {
  170. Status |= NETWARE_NETWORK_DRIVE;
  171. if ( NTIsNetWareDrive( (unsigned int)DriveNumber ) )
  172. Status |= NETWARE_NETWARE_DRIVE;
  173. }
  174. else if ( drivetype == DRIVE_NO_ROOT_DIR ) {
  175. Status = NETWARE_LOCAL_FREE_DRIVE;
  176. }
  177. else {
  178. Status = NETWARE_LOCAL_DRIVE;
  179. }
  180. return (USHORT)Status;
  181. }
  182. /********************************************************************
  183. NTGetNWDrivePath
  184. Routine Description:
  185. Return the server name and path of the specified drive
  186. Arguments:
  187. DriveNumber - Number of drive 0-25
  188. ServerName - Name of file server
  189. Path - Volume:\Path
  190. Return Value:
  191. 0 = success
  192. else NT error
  193. *******************************************************************/
  194. unsigned int NTGetNWDrivePath(
  195. unsigned short DriveNumber,
  196. unsigned char * ServerName,
  197. unsigned char * Path )
  198. {
  199. static char localname[] = "A:";
  200. unsigned int Result;
  201. char * p;
  202. char * volume;
  203. char remotename[1024];
  204. int length = 1024;
  205. if ( ServerName != NULL )
  206. *ServerName = 0;
  207. if ( Path != NULL )
  208. *Path = 0;
  209. localname[0] = 'A' + DriveNumber;
  210. Result = WNetGetConnectionA ( localname, remotename, &length );
  211. if ( Result != NO_ERROR ) {
  212. Result = GetLastError();
  213. if ( Result == ERROR_EXTENDED_ERROR )
  214. NTPrintExtendedError();
  215. return Result;
  216. }
  217. p = strchr (remotename + 2, '\\');
  218. if ( !p )
  219. return 0xffffffff;
  220. *p++ = '\0';
  221. volume = p;
  222. if ( ServerName != NULL ) {
  223. strcpy( ServerName, remotename + 2 );
  224. _strupr( ServerName );
  225. }
  226. if ( Path != NULL ) {
  227. p = strchr (volume, '\\');
  228. if ( !p ) {
  229. strcpy( Path, volume );
  230. strcat( Path, ":" );
  231. }
  232. else {
  233. *p = ':';
  234. strcpy( Path, volume );
  235. }
  236. _strupr( Path );
  237. }
  238. return NO_ERROR;
  239. }
  240. /********************************************************************
  241. NTIsNetWareDrive
  242. Routine Description:
  243. Returns TRUE if the drive is a netware mapped drive
  244. Arguments:
  245. DriveNumber - Number of drive 0-25
  246. Return Value:
  247. TRUE - drive is NetWare
  248. FALSE - drive is not NetWare
  249. *******************************************************************/
  250. unsigned int
  251. NTIsNetWareDrive( unsigned int DriveNumber )
  252. {
  253. LPBYTE Buffer ;
  254. DWORD dwErr ;
  255. HANDLE EnumHandle ;
  256. char DriveName[10];
  257. DWORD BufferSize = 4096;
  258. LPWNET_CONNECTIONINFOA pConnectionInfo;
  259. strcpy( DriveName, "A:" );
  260. DriveName[0] = 'A' + DriveNumber;
  261. //
  262. // allocate memory and open the enumeration
  263. //
  264. if (!(Buffer = LocalAlloc( LPTR, BufferSize ))) {
  265. DisplayMessage(IDR_NOT_ENOUGH_MEMORY);
  266. return FALSE;
  267. }
  268. dwErr = WNetGetConnection2A( DriveName, Buffer, &BufferSize );
  269. if (dwErr != WN_SUCCESS) {
  270. dwErr = GetLastError();
  271. if ( dwErr == ERROR_EXTENDED_ERROR )
  272. NTPrintExtendedError();
  273. (void) LocalFree((HLOCAL) Buffer) ;
  274. return FALSE;
  275. }
  276. pConnectionInfo = (LPWNET_CONNECTIONINFOA) Buffer;
  277. if ( !_strcmpi ( pConnectionInfo->lpProvider, NW_PROVIDERA ) ) {
  278. (void) LocalFree((HLOCAL) Buffer) ;
  279. return TRUE;
  280. }
  281. else {
  282. (void) LocalFree((HLOCAL) Buffer) ;
  283. return FALSE;
  284. }
  285. return FALSE;
  286. }