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.

419 lines
11 KiB

  1. /*************************************************************************
  2. *
  3. * COMMON.C
  4. *
  5. * Miscellaneous routines for scripts, ported from DOS
  6. *
  7. * Copyright (c) 1995 Microsoft Corporation
  8. *
  9. * $Log: N:\NT\PRIVATE\NW4\NWSCRIPT\VCS\COMMON.C $
  10. *
  11. * Rev 1.3 10 Apr 1996 14:21:52 terryt
  12. * Hotfix for 21181hq
  13. *
  14. * Rev 1.3 12 Mar 1996 19:52:40 terryt
  15. * Relative NDS names and merge
  16. *
  17. * Rev 1.2 24 Jan 1996 17:14:54 terryt
  18. * Common read string routine
  19. *
  20. * Rev 1.1 22 Dec 1995 14:23:56 terryt
  21. * Add Microsoft headers
  22. *
  23. * Rev 1.0 15 Nov 1995 18:06:36 terryt
  24. * Initial revision.
  25. *
  26. * Rev 1.2 25 Aug 1995 16:22:18 terryt
  27. * Capture support
  28. *
  29. * Rev 1.1 26 Jul 1995 14:17:06 terryt
  30. * Clean up comments
  31. *
  32. * Rev 1.0 15 May 1995 19:10:18 terryt
  33. * Initial revision.
  34. *
  35. *************************************************************************/
  36. #include "common.h"
  37. /*
  38. Used by DisplayMapping() only.
  39. Return search number if the drive is a search drive.
  40. Return 0 if the drive is not a search drive.
  41. */
  42. int IsSearchDrive(int driveNum)
  43. {
  44. int searchNum = 1;
  45. char *path;
  46. path = NWGetPath();
  47. if (path == NULL) {
  48. return 0;
  49. }
  50. while (*path != 0)
  51. {
  52. if ((*path - 'A' + 1 == driveNum) &&
  53. (*(path+1) == ':'))
  54. {
  55. return searchNum;
  56. }
  57. if (path = strchr (path, ';'))
  58. {
  59. path++;
  60. searchNum++;
  61. }
  62. else
  63. return(0);
  64. }
  65. return(0);
  66. }
  67. /*
  68. Get path enviroment variable. This returns the pointer to the
  69. path in the parent enviroment segment.
  70. */
  71. char * NWGetPath(void)
  72. {
  73. //
  74. // On NT we can't change or get the parent's environment this way
  75. //
  76. return( getenv("PATH") );
  77. }
  78. /*
  79. Return TRUE if the memory block is large enough for adding new
  80. search path. FALSE otherwise.
  81. */
  82. int MemorySegmentLargeEnough (int nInsertByte)
  83. {
  84. return TRUE;
  85. }
  86. /*
  87. Display drive maps info.
  88. */
  89. void DisplayMapping(void)
  90. {
  91. unsigned int iRet = 0;
  92. int i;
  93. WORD status;
  94. char rootPath[MAX_PATH_LEN], relativePath[MAX_PATH_LEN];
  95. char *envPath, *tokenPath;
  96. char *path;
  97. DWORD LocalDrives;
  98. DWORD NonSearchDrives;
  99. char sLocalDrives[26*2+5];
  100. char * sptr;
  101. // Don't delete this line. This is for fixing bug 1176.
  102. DisplayMessage(IDR_NEWLINE);
  103. LocalDrives = 0;
  104. NonSearchDrives = 0;
  105. // Collect local drives and search drives
  106. for (i = 1; i <= 26; i++) {
  107. status = NTNetWareDriveStatus( (unsigned short)(i-1) );
  108. if ((status & NETWARE_LOCAL_DRIVE) && !(status & NETWARE_NETWORK_DRIVE))
  109. LocalDrives |= ( 1 << (i-1) );
  110. else if ((status & NETWARE_NETWORK_DRIVE) && (!IsSearchDrive(i)) )
  111. {
  112. if (status & NETWARE_NETWARE_DRIVE)
  113. NonSearchDrives |= ( 1 << (i-1) );
  114. else
  115. {
  116. //For NetWare compatibility
  117. LocalDrives |= ( 1 << (i-1) );
  118. }
  119. }
  120. }
  121. // Print out local drives
  122. if ( LocalDrives ) {
  123. sptr = &sLocalDrives[0];
  124. for (i = 1; i <= 26; i++)
  125. {
  126. if ( LocalDrives & ( 1 << (i - 1) ) ) {
  127. *sptr++ = 'A' + i - 1;
  128. *sptr++ = ',';
  129. }
  130. }
  131. sptr--;
  132. *sptr = '\0';
  133. DisplayMessage(IDR_ALL_LOCAL_DRIVES, sLocalDrives);
  134. }
  135. // Print out non search drives.
  136. for (i = 1; i <= 26; i++)
  137. {
  138. if ( NonSearchDrives & ( 1 << (i - 1) ) ) {
  139. if (iRet = GetDriveStatus ((unsigned short)i,
  140. NETWARE_FORMAT_SERVER_VOLUME,
  141. &status,
  142. NULL,
  143. rootPath,
  144. relativePath,
  145. NULL))
  146. {
  147. DisplayError (iRet, "GetDriveStatus");
  148. }
  149. else
  150. {
  151. DisplayMessage(IDR_NETWARE_DRIVE, 'A'+i-1, rootPath, relativePath);
  152. }
  153. }
  154. }
  155. // Print out dashed line as seperator between non search drives
  156. // and search drives.
  157. DisplayMessage(IDR_DASHED_LINE);
  158. // Get the PATH environment variable.
  159. path = NWGetPath();
  160. if (path == NULL) {
  161. return;
  162. }
  163. if ((envPath = malloc (strlen (path) + 1)) == NULL)
  164. {
  165. DisplayMessage(IDR_NOT_ENOUGH_MEMORY);
  166. return;
  167. }
  168. strcpy (envPath, path);
  169. tokenPath = strtok (envPath, PATH_SEPERATOR);
  170. // Print out search drvies.
  171. for (i = 1; tokenPath != NULL; i++)
  172. {
  173. if (tokenPath[1] == ':')
  174. {
  175. if (iRet = GetDriveStatus ((unsigned short)(toupper(tokenPath[0])-'A'+1),
  176. NETWARE_FORMAT_SERVER_VOLUME,
  177. &status,
  178. NULL,
  179. rootPath,
  180. relativePath,
  181. NULL))
  182. {
  183. DisplayError (iRet, "GetDriveStatus");
  184. }
  185. else
  186. {
  187. if (status & NETWARE_NETWARE_DRIVE)
  188. DisplayMessage(IDR_NETWARE_SEARCH, i, tokenPath, rootPath, relativePath);
  189. else
  190. DisplayMessage(IDR_LOCAL_SEARCH, i, tokenPath);
  191. }
  192. }
  193. else
  194. {
  195. // Path is specified without drive letter.
  196. DisplayMessage(IDR_LOCAL_SEARCH, i, tokenPath);
  197. }
  198. tokenPath = strtok (NULL, PATH_SEPERATOR);
  199. }
  200. free (envPath);
  201. }
  202. /*****************************************************************************
  203. * *
  204. * GetString *
  205. * *
  206. * *
  207. * entry: pointer to buffer *
  208. * length of buffer *
  209. * *
  210. * exit: length of string *
  211. * *
  212. *****************************************************************************/
  213. int
  214. GetString( char * pBuffer, int ByteCount )
  215. {
  216. char * pString = pBuffer;
  217. char ch;
  218. if ( ByteCount > 0 )
  219. ByteCount--;
  220. for( ;; ) {
  221. switch ( ch = (char) _getch() ) {
  222. case '\r' :
  223. *pString++ = '\0';
  224. putchar( '\n' );
  225. return( strlen( pBuffer ) );
  226. case '\b' :
  227. if ( pString != pBuffer ) {
  228. ByteCount++;
  229. pString--;
  230. printf( "\b \b" );
  231. }
  232. break;
  233. default :
  234. if ( ByteCount > 0 && ch >= 0x20 && ch < 0x80 ) {
  235. *pString++ = ch;
  236. ByteCount--;
  237. putchar( ch );
  238. }
  239. break;
  240. }
  241. }
  242. fflush(stdin);
  243. }
  244. /*
  245. Read user or server name from the keyboard input.
  246. Return TRUE if user typed in a username
  247. FALSE otherwise.
  248. */
  249. int ReadName (char * Name)
  250. {
  251. memset( Name, 0, MAX_NAME_LEN );
  252. if ( 0 == GetString( Name, MAX_NAME_LEN ) )
  253. return FALSE;
  254. _strupr(Name);
  255. return TRUE;
  256. }
  257. /*
  258. Try to log the user in.
  259. Return error code. 0 is success.
  260. */
  261. int Login( char *UserName,
  262. char *ServerName,
  263. char *Password,
  264. int bReadPassword)
  265. {
  266. unsigned int iRet = 0;
  267. // Try log the user in with no password first.
  268. iRet = NTLoginToFileServer( ServerName,
  269. UserName,
  270. Password);
  271. if (iRet == ERROR_INVALID_PASSWORD && bReadPassword)
  272. {
  273. // wrong password. ask for passowrd. and try login with
  274. // the input password.
  275. DisplayMessage(IDR_PASSWORD, UserName, ServerName);
  276. ReadPassword (Password);
  277. iRet = NTLoginToFileServer( ServerName,
  278. UserName,
  279. Password);
  280. }
  281. switch(iRet)
  282. {
  283. case NO_ERROR: // ok
  284. DisplayMessage(IDR_ATTACHED, ServerName);
  285. break;
  286. case ERROR_INVALID_PASSWORD: // wrong password.
  287. case ERROR_NO_SUCH_USER: // no such user.
  288. DisplayMessage(IDR_SERVER_USER, ServerName, UserName);
  289. DisplayMessage(IDR_ACCESS_DENIED);
  290. break;
  291. case ERROR_CONNECTION_COUNT_LIMIT: // concurrent connection restriction.
  292. DisplayMessage(IDR_SERVER_USER, ServerName, UserName);
  293. DisplayMessage(IDR_LOGIN_DENIED_NO_CONNECTION);
  294. break;
  295. case ERROR_LOGIN_TIME_RESTRICTION: // time restriction.
  296. DisplayMessage(IDR_SERVER_USER, ServerName, UserName);
  297. DisplayMessage(IDR_UNAUTHORIZED_LOGIN_TIME);
  298. break;
  299. case ERROR_LOGIN_WKSTA_RESTRICTION: // station restriction.
  300. DisplayMessage(IDR_SERVER_USER, ServerName, UserName);
  301. DisplayMessage(IDR_UNAUTHORIZED_LOGIN_STATION);
  302. break;
  303. case ERROR_ACCOUNT_DISABLED:
  304. DisplayMessage(IDR_SERVER_USER, ServerName, UserName);
  305. DisplayMessage(IDR_ACCOUNT_DISABLED);
  306. break;
  307. case ERROR_PASSWORD_EXPIRED: // password expired and no grace login left.
  308. DisplayMessage(IDR_SERVER_USER, ServerName, UserName);
  309. DisplayMessage(IDR_PASSWORD_EXPRIED_NO_GRACE);
  310. break;
  311. case ERROR_REMOTE_SESSION_LIMIT_EXCEEDED:
  312. // Server rejected access
  313. DisplayMessage(IDR_CONNECTION_REFUSED);
  314. break;
  315. case ERROR_EXTENDED_ERROR:
  316. NTPrintExtendedError();
  317. break;
  318. //
  319. // tommye - MS bug 8194 (MCS 240)
  320. // If we are already attached to this server under other credentials
  321. // we get back an ERROR_SESSION_CREDENTIAL_CONFLICT. This is okay,
  322. // we'll just print out that we're already attached. We have to
  323. // pass the error on up, though, so we don't add this server to the
  324. // attach list again.
  325. //
  326. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  327. DisplayMessage(IDR_ALREADY_ATTACHED, ServerName);
  328. break;
  329. default :
  330. DisplayError(iRet,"NtLoginToFileServer");
  331. break;
  332. }
  333. return(iRet);
  334. }
  335. int CAttachToFileServer(char *ServerName, unsigned int *pConn, int * pbAlreadyAttached)
  336. {
  337. unsigned int iRet = 0;
  338. if (pbAlreadyAttached != NULL)
  339. *pbAlreadyAttached = FALSE;
  340. // Validate the server name.
  341. iRet = AttachToFileServer(ServerName,pConn);
  342. switch (iRet)
  343. {
  344. case 0: // OK
  345. break;
  346. case 0x8800 : // Already atached.
  347. if (pbAlreadyAttached != NULL)
  348. *pbAlreadyAttached = TRUE;
  349. iRet = GetConnectionHandle (ServerName, pConn);
  350. break;
  351. default:
  352. DisplayMessage(IDR_NO_RESPONSE, ServerName);
  353. break;
  354. }
  355. return(iRet);
  356. }