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.

496 lines
9.3 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. int
  155. IsNumber(
  156. LPTSTR x
  157. )
  158. {
  159. return (*x && (_tcslen(x) == _tcsspn(x, TEXT("0123456789"))));
  160. }
  161. int
  162. IsShareAssignment(
  163. LPTSTR x
  164. )
  165. {
  166. TCHAR * pos;
  167. int result;
  168. /* WARNING: x ALWAYS should be a TCHAR * */
  169. pos = _tcschr (x, '=');
  170. if (pos == NULL)
  171. {
  172. return 0;
  173. }
  174. *pos = NULLC;
  175. result = (int) ( IsNetname(x) && IsValidAssign(pos+1) );
  176. *pos = '=';
  177. return result;
  178. }
  179. int
  180. IsValidAssign(
  181. LPTSTR name
  182. )
  183. {
  184. TCHAR name_out[MAX_PATH];
  185. ULONG types[64];
  186. DWORD count;
  187. DWORD i;
  188. ULONG type = 0;
  189. /*
  190. * First check if it is a path. Since a path may contain spaces, we
  191. * return successfully immediately.
  192. */
  193. I_NetPathType(NULL, name, &type, 0L);
  194. if ( type == ITYPE_PATH_ABSD || type == ITYPE_DEVICE_DISK )
  195. {
  196. return 1;
  197. }
  198. /*
  199. * Not an absolute path, so go about our normal business.
  200. */
  201. if (I_NetListCanonicalize(NULL, /* server name, NULL means local */
  202. name, /* list to canonicalize */
  203. txt_LIST_DELIMITER_STR_UI,
  204. name_out,
  205. DIMENSION(name_out),
  206. &count,
  207. types,
  208. DIMENSION(types),
  209. (NAMETYPE_PATH | OUTLIST_TYPE_API) ))
  210. {
  211. return 0;
  212. }
  213. if (count == 0)
  214. {
  215. return 0;
  216. }
  217. for (i = 0; i < count; i++)
  218. {
  219. if (types[i] != ITYPE_DEVICE_LPT &&
  220. types[i] != ITYPE_DEVICE_COM &&
  221. types[i] != ITYPE_DEVICE_NUL)
  222. {
  223. return 0;
  224. }
  225. }
  226. return 1;
  227. }
  228. int
  229. IsAnyShareAssign(
  230. LPTSTR x
  231. )
  232. {
  233. TCHAR * pos;
  234. int result;
  235. /* WARNNING: x ALWAYS should be a TCHAR * */
  236. pos = _tcschr (x, '=');
  237. if (pos == NULL)
  238. return 0;
  239. *pos = NULLC;
  240. result = (int) ( IsNetname(x) && IsAnyValidAssign(pos+1) );
  241. *pos = '=';
  242. return result;
  243. }
  244. int
  245. IsAnyValidAssign(
  246. LPTSTR name
  247. )
  248. {
  249. TCHAR name_out[MAX_PATH];
  250. ULONG types[64];
  251. DWORD count;
  252. if (I_NetListCanonicalize(NULL, /* server name, NULL means local */
  253. name, /* list to canonicalize */
  254. txt_LIST_DELIMITER_STR_UI,
  255. name_out,
  256. DIMENSION(name_out),
  257. &count,
  258. types,
  259. DIMENSION(types),
  260. (NAMETYPE_PATH | OUTLIST_TYPE_API) ))
  261. return 0;
  262. if (count == 0)
  263. return 0;
  264. return 1;
  265. }
  266. #ifdef OS2
  267. int IsAdminShare(TCHAR * x)
  268. {
  269. if ((_tcsicmp(x, TEXT("IPC$"))) && (_tcsicmp(x, ADMIN_DOLLAR)))
  270. return 0;
  271. else
  272. return 1;
  273. }
  274. #endif /* OS2 */
  275. #ifdef OS2
  276. /*
  277. * what we are looking for here is PRINT=xxxx
  278. */
  279. int IsPrintDest(TCHAR *x)
  280. {
  281. TCHAR FAR * ptr;
  282. if (!_tcsnicmp(x, TEXT("PRINT="), 6) && _tcslen(x) > 6)
  283. {
  284. x += 6;
  285. if (!IsDeviceName(x))
  286. return 0;
  287. if (ptr = _tcschr(x,COLON))
  288. *ptr = NULLC;
  289. return 1;
  290. }
  291. return 0;
  292. }
  293. #endif /* OS2 */
  294. /*
  295. * returns true is the arg is a valid username
  296. */
  297. int IsUsername(TCHAR * x)
  298. {
  299. return !(I_NetNameValidate(NULL, x, NAMETYPE_USER, LM2X_COMPATIBLE));
  300. }
  301. /*
  302. * returns true is the arg is a valid username or a qualified username,
  303. * of form domain\user or a potential UPN
  304. */
  305. int IsQualifiedUsername(TCHAR * x)
  306. {
  307. TCHAR *ptr, name[UNLEN + 1 + DNLEN + 1] ;
  308. if (_tcschr(x, '@'))
  309. return 1;
  310. // check for overflow
  311. if (_tcslen(x) >= DIMENSION(name))
  312. return 0 ;
  313. // make copy
  314. _tcscpy(name, x) ;
  315. // do we have a domain\username format?
  316. if (ptr = _tcschr(name, '\\'))
  317. {
  318. *ptr = NULLC ;
  319. ++ptr ; // this is DCS safe since we found single byte char
  320. // if its a domain, check the username part
  321. if (IsDomainName(name))
  322. return IsUsername(ptr) ;
  323. // its not valid
  324. return(0) ;
  325. }
  326. // else just straight username
  327. return IsUsername(x) ;
  328. }
  329. int IsGroupname(TCHAR * x)
  330. {
  331. return !(I_NetNameValidate(NULL, x, NAMETYPE_GROUP, 0L));
  332. }
  333. int IsMsgname(TCHAR * x)
  334. {
  335. if (!_tcscmp(x, TEXT("*")))
  336. return 1;
  337. return !(I_NetNameValidate(NULL, x, NAMETYPE_COMPUTER, LM2X_COMPATIBLE));
  338. }
  339. int IsPassword(TCHAR * x)
  340. {
  341. if (!_tcscmp(x, TEXT("*")))
  342. return 1;
  343. return !(I_NetNameValidate(NULL, x, NAMETYPE_PASSWORD, 0L));
  344. }
  345. int IsWildCard(TCHAR * x)
  346. {
  347. if (x == NULL)
  348. return 0 ;
  349. return ( (!_tcscmp(x, TEXT("*"))) || (!_tcscmp(x, TEXT("?"))) ) ;
  350. }
  351. int IsQuestionMark(TCHAR * x)
  352. {
  353. if (x == NULL)
  354. return 0 ;
  355. return (!_tcscmp(x, TEXT("?"))) ;
  356. }
  357. #ifdef OS2
  358. int IsSharePassword(TCHAR * x)
  359. {
  360. if (!_tcscmp(x, TEXT("*")))
  361. return 1;
  362. if (_tcslen(x) > SHPWLEN)
  363. return 0;
  364. return !(I_NetNameValidate(NULL, x, NAMETYPE_PASSWORD, LM2X_COMPATIBLE));
  365. }
  366. #endif /* OS2 */
  367. int IsNtAliasname(TCHAR *name)
  368. {
  369. return !(I_NetNameValidate(NULL, name, NAMETYPE_GROUP, 0L));
  370. }
  371. #ifdef OS2
  372. #ifdef IBM_ONLY
  373. int IsAliasname(TCHAR * x)
  374. {
  375. if ( _tcslen(x) > 8 )
  376. return 0;
  377. return !(I_NetNameValidate(NULL, x, NAMETYPE_SHARE, LM2X_COMPATIBLE));
  378. }
  379. #endif /* IBM_ONLY */
  380. #endif /* OS2 */