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.

516 lines
9.2 KiB

  1. /********************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1987-1991 **/
  4. /********************************************************************/
  5. /*
  6. * Grammar.c - contains the functions to determine type of object
  7. * passed. Is used by the parser to check grammar.
  8. *
  9. * date who what
  10. * ??/??/??, ?????, initial code
  11. * 10/31/88, erichn, uses OS2.H instead of DOSCALLS
  12. * 05/02/89, erichn, NLS conversion
  13. * 06/08/89, erichn, canonicalization sweep
  14. * 06/23/89, erichn, replaced old NetI calls with new I_Net functions
  15. * 06/11/90, thomaspa, fixed IsValidAssign() to accept paths with
  16. * embedded spaces.
  17. * 02/20/91, danhi, change to use lm 16/32 mapping layer
  18. */
  19. #define INCL_NOCOMMON
  20. #include <os2.h>
  21. #include <lmcons.h>
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <process.h>
  25. #include <lmaccess.h>
  26. #include <lmserver.h>
  27. #include <lmshare.h>
  28. #include <icanon.h>
  29. #include "netcmds.h"
  30. #include "nettext.h"
  31. /* prototypes of worker functions */
  32. int is_other_resource(TCHAR *);
  33. int IsAccessSetting(TCHAR *x)
  34. {
  35. TCHAR FAR * pos;
  36. TCHAR buf[sizeof(ACCESS_LETTERS)];
  37. pos = _tcschr(x, COLON);
  38. if (pos == NULL)
  39. return 0;
  40. /* check if the first component is a user name. */
  41. *pos = NULLC;
  42. if (I_NetNameValidate(NULL, x, NAMETYPE_USER, LM2X_COMPATIBLE))
  43. {
  44. *pos = COLON;
  45. return 0;
  46. }
  47. *pos++ = COLON;
  48. /* if there is a letter that is not an access letter it can
  49. only be TEXT('y') or TEXT('n'), which must be alone. */
  50. _tcscpy(buf, pos);
  51. _tcsupr(buf);
  52. if ( _tcsspn(buf, TEXT(ACCESS_LETTERS)) != _tcslen(buf) )
  53. return ( !_tcsicmp(buf, TEXT("Y")) || !_tcsicmp(buf, TEXT("N")) );
  54. else
  55. return 1;
  56. }
  57. int IsPathname ( TCHAR * x )
  58. {
  59. ULONG type = 0;
  60. if (I_NetPathType(NULL, x, &type, 0L))
  61. return 0;
  62. return (type == ITYPE_PATH_ABSD ||
  63. type == ITYPE_PATH_ABSND ||
  64. type == ITYPE_PATH_RELD ||
  65. type == ITYPE_PATH_RELND );
  66. }
  67. int IsPathnameOrUNC ( TCHAR * x )
  68. {
  69. ULONG type = 0;
  70. if (I_NetPathType(NULL, x, &type, 0L))
  71. return 0;
  72. return (type == ITYPE_PATH_ABSD ||
  73. type == ITYPE_PATH_ABSND ||
  74. type == ITYPE_PATH_RELD ||
  75. type == ITYPE_PATH_RELND ||
  76. type == ITYPE_UNC);
  77. }
  78. /* Access type resource only, does not include lpt, com etc... */
  79. int IsResource ( TCHAR * x )
  80. {
  81. ULONG type = 0;
  82. if (I_NetPathType(NULL, x, &type, 0L))
  83. return 0;
  84. return (type == ITYPE_PATH_ABSD ||
  85. type == ITYPE_PATH_ABSND ||
  86. type == ITYPE_DEVICE_DISK ||
  87. type == ITYPE_PATH_SYS_PIPE ||
  88. type == ITYPE_PATH_SYS_COMM ||
  89. type == ITYPE_PATH_SYS_PRINT ||
  90. is_other_resource(x) );
  91. }
  92. int is_other_resource(TCHAR * x)
  93. {
  94. return (!_tcsicmp(x, TEXT("\\PIPE")) ||
  95. !_tcsicmp(x, TEXT("\\PRINT")) ||
  96. !_tcsicmp(x, TEXT("\\COMM")));
  97. }
  98. int IsNetname(TCHAR * x)
  99. {
  100. return (!I_NetNameValidate(NULL, x, NAMETYPE_SHARE, 0));
  101. }
  102. int IsComputerName(TCHAR *x)
  103. {
  104. ULONG type = 0;
  105. if (I_NetPathType(NULL, x, &type, 0L))
  106. return 0;
  107. return ( type == ITYPE_UNC_COMPNAME );
  108. }
  109. int IsDomainName(TCHAR *x)
  110. {
  111. return (!I_NetNameValidate(NULL, x, NAMETYPE_DOMAIN, 0L) || !I_NetNameValidate(NULL, x, NAMETYPE_COMPUTER, 0L));
  112. }
  113. int IsComputerNameShare(TCHAR *x)
  114. {
  115. ULONG type;
  116. TCHAR FAR * ptr;
  117. if (I_NetPathType(NULL, x, &type, 0L))
  118. return 0;
  119. if (!(type & ITYPE_UNC))
  120. return 0;
  121. if (type == ITYPE_UNC_COMPNAME)
  122. return 0;
  123. /* Find the slash separating the computername and the
  124. * sharename. We know this is a UNC name, thus we can safely
  125. * skip 2 bytes for the leading backslashes.
  126. */
  127. ptr = _tcspbrk(x+2, TEXT("\\/") );
  128. if ( ptr == NULL )
  129. return 0;
  130. ptr +=1; /* point past slash TCHAR */
  131. /*
  132. * Make sure there are no more slashes
  133. */
  134. if( _tcspbrk(ptr, TEXT("\\/")) != NULL)
  135. return 0;
  136. return 1;
  137. }
  138. int IsDeviceName(TCHAR *x)
  139. {
  140. ULONG type = 0;
  141. TCHAR FAR * pos;
  142. if (I_NetPathType(NULL, x, &type, 0L))
  143. return 0;
  144. if (type & ITYPE_DEVICE)
  145. {
  146. if (type == ITYPE_DEVICE_DISK)
  147. return 1;
  148. if (pos = _tcschr(x, COLON))
  149. *pos = NULLC;
  150. return 1;
  151. }
  152. return 0;
  153. }
  154. /*
  155. * IsMsgid -- determines if passed string is a valid message id.
  156. * msd id's are of the form yyyxxxx where
  157. * yyy is any string beginning with a non-numeric character OR null
  158. * xxxx is a string containing only numeric characters.
  159. */
  160. int
  161. IsMsgid(
  162. LPTSTR x
  163. )
  164. {
  165. if (IsNumber(x))
  166. return TRUE;
  167. x+= NET_KEYWORD_SIZE;
  168. if (IsNumber(x))
  169. return TRUE;
  170. return FALSE;
  171. }
  172. int
  173. IsNumber(
  174. LPTSTR x
  175. )
  176. {
  177. return (*x && (_tcslen(x) == _tcsspn(x, TEXT("0123456789"))));
  178. }
  179. int
  180. IsShareAssignment(
  181. LPTSTR x
  182. )
  183. {
  184. TCHAR * pos;
  185. int result;
  186. /* WARNING: x ALWAYS should be a TCHAR * */
  187. pos = _tcschr (x, '=');
  188. if (pos == NULL)
  189. {
  190. return 0;
  191. }
  192. *pos = NULLC;
  193. result = (int) ( IsNetname(x) && IsValidAssign(pos+1) );
  194. *pos = '=';
  195. return result;
  196. }
  197. int
  198. IsValidAssign(
  199. LPTSTR name
  200. )
  201. {
  202. TCHAR name_out[MAX_PATH];
  203. ULONG types[64];
  204. DWORD count;
  205. DWORD i;
  206. ULONG type = 0;
  207. /*
  208. * First check if it is a path. Since a path may contain spaces, we
  209. * return successfully immediately.
  210. */
  211. I_NetPathType(NULL, name, &type, 0L);
  212. if ( type == ITYPE_PATH_ABSD || type == ITYPE_DEVICE_DISK )
  213. {
  214. return 1;
  215. }
  216. /*
  217. * Not an absolute path, so go about our normal business.
  218. */
  219. if (I_NetListCanonicalize(NULL, /* server name, NULL means local */
  220. name, /* list to canonicalize */
  221. txt_LIST_DELIMITER_STR_UI,
  222. name_out,
  223. DIMENSION(name_out),
  224. &count,
  225. types,
  226. DIMENSION(types),
  227. (NAMETYPE_PATH | OUTLIST_TYPE_API) ))
  228. {
  229. return 0;
  230. }
  231. if (count == 0)
  232. {
  233. return 0;
  234. }
  235. for (i = 0; i < count; i++)
  236. {
  237. if (types[i] != ITYPE_DEVICE_LPT &&
  238. types[i] != ITYPE_DEVICE_COM &&
  239. types[i] != ITYPE_DEVICE_NUL)
  240. {
  241. return 0;
  242. }
  243. }
  244. return 1;
  245. }
  246. int
  247. IsAnyShareAssign(
  248. LPTSTR x
  249. )
  250. {
  251. TCHAR * pos;
  252. int result;
  253. /* WARNNING: x ALWAYS should be a TCHAR * */
  254. pos = _tcschr (x, '=');
  255. if (pos == NULL)
  256. return 0;
  257. *pos = NULLC;
  258. result = (int) ( IsNetname(x) && IsAnyValidAssign(pos+1) );
  259. *pos = '=';
  260. return result;
  261. }
  262. int
  263. IsAnyValidAssign(
  264. LPTSTR name
  265. )
  266. {
  267. TCHAR name_out[MAX_PATH];
  268. ULONG types[64];
  269. DWORD count;
  270. if (I_NetListCanonicalize(NULL, /* server name, NULL means local */
  271. name, /* list to canonicalize */
  272. txt_LIST_DELIMITER_STR_UI,
  273. name_out,
  274. DIMENSION(name_out),
  275. &count,
  276. types,
  277. DIMENSION(types),
  278. (NAMETYPE_PATH | OUTLIST_TYPE_API) ))
  279. return 0;
  280. if (count == 0)
  281. return 0;
  282. return 1;
  283. }
  284. #ifdef OS2
  285. int IsAdminShare(TCHAR * x)
  286. {
  287. if ((_tcsicmp(x, TEXT("IPC$"))) && (_tcsicmp(x, ADMIN_DOLLAR)))
  288. return 0;
  289. else
  290. return 1;
  291. }
  292. #endif /* OS2 */
  293. #ifdef OS2
  294. /*
  295. * what we are looking for here is PRINT=xxxx
  296. */
  297. int IsPrintDest(TCHAR *x)
  298. {
  299. TCHAR FAR * ptr;
  300. if (!_tcsnicmp(x, TEXT("PRINT="), 6) && _tcslen(x) > 6)
  301. {
  302. x += 6;
  303. if (!IsDeviceName(x))
  304. return 0;
  305. if (ptr = _tcschr(x,COLON))
  306. *ptr = NULLC;
  307. return 1;
  308. }
  309. return 0;
  310. }
  311. #endif /* OS2 */
  312. /*
  313. * returns true is the arg is a valid username
  314. */
  315. int IsUsername(TCHAR * x)
  316. {
  317. return !(I_NetNameValidate(NULL, x, NAMETYPE_USER, LM2X_COMPATIBLE));
  318. }
  319. /*
  320. * returns true is the arg is a valid username or a qualified username,
  321. * of form domain\user or a potential UPN
  322. */
  323. int IsQualifiedUsername(TCHAR * x)
  324. {
  325. TCHAR *ptr, name[UNLEN + 1 + DNLEN + 1] ;
  326. if (_tcschr(x, '@'))
  327. return 1;
  328. // check for overflow
  329. if (_tcslen(x) >= DIMENSION(name))
  330. return 0 ;
  331. // make copy
  332. _tcscpy(name, x) ;
  333. // do we have a domain\username format?
  334. if (ptr = _tcschr(name, '\\'))
  335. {
  336. *ptr = NULLC ;
  337. ++ptr ; // this is DCS safe since we found single byte char
  338. // if its a domain, check the username part
  339. if (IsDomainName(name))
  340. return IsUsername(ptr) ;
  341. // its not valid
  342. return(0) ;
  343. }
  344. // else just straight username
  345. return IsUsername(x) ;
  346. }
  347. int IsGroupname(TCHAR * x)
  348. {
  349. return !(I_NetNameValidate(NULL, x, NAMETYPE_GROUP, 0L));
  350. }
  351. int IsMsgname(TCHAR * x)
  352. {
  353. if (!_tcscmp(x, TEXT("*")))
  354. return 1;
  355. return !(I_NetNameValidate(NULL, x, NAMETYPE_COMPUTER, LM2X_COMPATIBLE));
  356. }
  357. int IsPassword(TCHAR * x)
  358. {
  359. if (!_tcscmp(x, TEXT("*")))
  360. return 1;
  361. return !(I_NetNameValidate(NULL, x, NAMETYPE_PASSWORD, 0L));
  362. }
  363. int IsWildCard(TCHAR * x)
  364. {
  365. if (x == NULL)
  366. return 0 ;
  367. return ( (!_tcscmp(x, TEXT("*"))) || (!_tcscmp(x, TEXT("?"))) ) ;
  368. }
  369. int IsQuestionMark(TCHAR * x)
  370. {
  371. if (x == NULL)
  372. return 0 ;
  373. return (!_tcscmp(x, TEXT("?"))) ;
  374. }
  375. #ifdef OS2
  376. int IsSharePassword(TCHAR * x)
  377. {
  378. if (!_tcscmp(x, TEXT("*")))
  379. return 1;
  380. if (_tcslen(x) > SHPWLEN)
  381. return 0;
  382. return !(I_NetNameValidate(NULL, x, NAMETYPE_PASSWORD, LM2X_COMPATIBLE));
  383. }
  384. #endif /* OS2 */
  385. int IsNtAliasname(TCHAR *name)
  386. {
  387. return !(I_NetNameValidate(NULL, name, NAMETYPE_GROUP, 0L));
  388. }
  389. #ifdef OS2
  390. #ifdef IBM_ONLY
  391. int IsAliasname(TCHAR * x)
  392. {
  393. if ( _tcslen(x) > 8 )
  394. return 0;
  395. return !(I_NetNameValidate(NULL, x, NAMETYPE_SHARE, LM2X_COMPATIBLE));
  396. }
  397. #endif /* IBM_ONLY */
  398. #endif /* OS2 */