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.

323 lines
12 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. isremote.c
  5. Abstract:
  6. Contains the NetpIsRemote routine. This checks if a computer name
  7. designates the local machine
  8. Author:
  9. Richard L Firth (rfirth) 24th April 1991
  10. Revision History:
  11. 01-Nov-1991 JohnRo
  12. Fixed RAID 3414: allow explicit local server name. (NetpIsRemote was
  13. not canonizaling the computer name from NetWkstaGetInfo, so it was
  14. always saying that the local computer name was remote if it wasn't
  15. already canonicalized.)
  16. 07-Jun-1991 rfirth
  17. * Changed name of routine to conform to Nt naming conventions
  18. * Added LocalOrRemote parameter - returning just ISLOCAL or ISREMOTE
  19. is not sufficient - we can return error codes too
  20. * Added CanonicalizedName parameter - now passes back canonicalized
  21. name if requested. This is because, usually, subsequent routines call
  22. to NetRemoteComputerSupports which performs minimal checking on the
  23. name. Ergo, if we hand it a canonicalized name (which we have to do
  24. with this routine anyway) it can't complain. Can it?
  25. * NetpIsRemote no longer a NET_API_FUNCTION
  26. * Made semantics of CanonicalizedName orhogonal - if NULL or NUL passed
  27. in as ComputerName, caller still gets back the local computer name
  28. IF CanonicalizedName NOT NULL AND IF THE REDIRECTOR HAS BEEN STARTED
  29. --*/
  30. #include "nticanon.h"
  31. NET_API_STATUS
  32. NetpIsRemote(
  33. IN LPTSTR ComputerName OPTIONAL,
  34. OUT LPDWORD LocalOrRemote,
  35. OUT LPTSTR CanonicalizedName OPTIONAL,
  36. IN DWORD Flags
  37. )
  38. /*++
  39. Routine Description:
  40. Determines whether a computer name designates this machine or a renote
  41. one. Values of ComputerName which equate to the local machine are:
  42. NULL pointer to name
  43. pointer to NULL name
  44. pointer to non-NULL name which is lexically equivalent to this
  45. machine's name
  46. NB. This routine expects that the canonicalized version of ComputerName
  47. will fit into the buffer pointed at by CanonicalizedName. Since this
  48. is an INTERNAL function, this assumption is deemed valid
  49. Arguments:
  50. IN LPTSTR ComputerName OPTIONAL
  51. Pointer to computer name to check. May assume any of
  52. the above forms
  53. If a non-NULL string is passed in, then it may have
  54. preceeding back-slashes. This is kind of expected since
  55. this routine is called by remotable APIs and it is they
  56. who specify that the form a computer name is \\<name>
  57. OUT LPDWORD LocalOrRemote
  58. Points to the DWORD where the specifier for the
  59. symbolic location will be returned. MUST NOT BE NULL. On
  60. return will be one of:
  61. ISLOCAL The name defined by ComputerName specifies
  62. the local machine specifically or by default
  63. (ie. was NULL)
  64. ISREMOTE The name defined by ComputerName was non-NULL
  65. and was not the name of this machine
  66. OUT LPTSTR CanonicalizedName OPTIONAL
  67. Pointer to caller's buffer into which a copy of the
  68. canonicalized name will be placed if requested. This
  69. can then be used in subsequent calls, with the knowledge
  70. that no further checking of the computer name is required.
  71. Note that the format of this buffer will be \\<computername>
  72. on return. The contents of this buffer will not be
  73. modified unless this routine returns success
  74. IN DWORD Flags
  75. A bitmap. Flags are:
  76. NIRFLAG_MAPLOCAL if set, will map (ie canonicalize)
  77. the NULL local name to this
  78. computer's name proper. Used in
  79. conjunction with CanonicalizedName
  80. This stops extraneous calls to
  81. NetpNameCanonicalize with the
  82. inherited CanonicalizedName
  83. parameter. See below for elucidation
  84. Return Value:
  85. NET_API_STATUS
  86. Success = NERR_Success
  87. Failure = return code from:
  88. NetpNameCanonicalize
  89. NetWkstaGetInfo
  90. NetpNameCompare
  91. --*/
  92. {
  93. LPBYTE wksta_buffer_pointer;
  94. BOOL map_local_name = FALSE;
  95. LONG result;
  96. NET_API_STATUS rc;
  97. TCHAR name[MAX_PATH]; // canonicalized version of ComputerName
  98. LPTSTR wksta_name_uncanon; // our computer name (from NetWkstaGetInfo)
  99. TCHAR wksta_name_canon[MAX_PATH]; // our computer name (from canon)
  100. LPTSTR canonicalized_name; // as returned to caller
  101. //
  102. // Assert that we have a valid pointer in LocalOrRemote
  103. //
  104. //
  105. // Once again, shouldn't have to do this, since this routine is internal
  106. // and there is no interpretation about inputs. However, lets catch any
  107. // possible problems...
  108. //
  109. NetpAssert(ARGUMENT_PRESENT(LocalOrRemote));
  110. #ifdef CANONDBG
  111. DbgPrint("NetpIsRemote(%s, %x, %x, %x)\n",
  112. ComputerName,
  113. LocalOrRemote,
  114. CanonicalizedName,
  115. Flags
  116. );
  117. #endif
  118. //
  119. // NB. It is important to check this case first, before we call any Netp
  120. // routines since these could call back to this routine and we may get
  121. // stuck in an infinite loop
  122. //
  123. if (!ARGUMENT_PRESENT(ComputerName) || (*ComputerName == TCHAR_EOS)) {
  124. //
  125. // in this case its probably an internal call from one of our routines
  126. // and we want to return as quickly as possible. This will be borne out
  127. // by the NIRFLAG_MAPLOCAL flag being reset in the Flags parameter
  128. //
  129. //
  130. // A note about NIRFLAG_MAPLOCAL
  131. // This routine makes local calls to NetpNameValidate and
  132. // NetpNameCompare. If the NIRFLAG_MAPLOCAL flag is not reset then
  133. // these routines in turn will cause the local name to be returned
  134. // (because they always pass in non-NULL CanonicalizedName parameter)
  135. // which in most cases is inefficient, since the name won't be used
  136. // so we always say (in the Netp routines) that we don't want local
  137. // name canonicalization
  138. // Therefore, if (local) name canonicalization is implied by non-NULL
  139. // CanonicalizedName, verify this by checking Flags.NIRFLAG_MAPLOCAL
  140. // If it, too, is set then local name canonicalization is performed
  141. //
  142. if (!ARGUMENT_PRESENT(CanonicalizedName) || !(Flags & NIRFLAG_MAPLOCAL)) {
  143. *LocalOrRemote = ISLOCAL;
  144. #ifdef CANONDBG
  145. DbgPrint("NetpIsRemote(%s) - returning early\n", ComputerName);
  146. #endif
  147. return NERR_Success;
  148. } else {
  149. //
  150. // signify that the input name was NULL or NUL string but that the
  151. // caller wants a canonicalized name returned (from NetWkstaGetInfo)
  152. //
  153. map_local_name = TRUE;
  154. }
  155. } else {
  156. //
  157. // if the computername starts with \\ or // or any combination thereof,
  158. // skip the path separators - the canonicalization routines expect
  159. // computer names NOT to have these.
  160. //
  161. if (IS_PATH_SEPARATOR(ComputerName[0]) && IS_PATH_SEPARATOR(ComputerName[1])) {
  162. ComputerName += 2;
  163. }
  164. //
  165. // here's a use for canonicalization (!): ensure that we have been passed
  166. // a real and proper computer name and not some pale imitation
  167. //
  168. rc = NetpNameCanonicalize(
  169. NULL, // performed here, on our own premises
  170. ComputerName, // this is input
  171. name, // this is output
  172. sizeof(name), // how much buffer we have
  173. NAMETYPE_COMPUTER, // what we think it is
  174. INNCA_FLAGS_FULL_BUFLEN // say that o/p buffer must be large
  175. // enough for maximum-sized computer
  176. // name. Why? you ask, well its a fair
  177. // cop - the reason is that we can't
  178. // get into trouble on the one time that
  179. // we exercise the maximum requirement
  180. );
  181. if (rc) {
  182. return rc; // duff name (?)
  183. } else {
  184. canonicalized_name = name;
  185. }
  186. }
  187. //
  188. // get the name of this machine from the redirector. If we can't get the
  189. // name for whatever reason, return the error code.
  190. //
  191. if (rc = NetWkstaGetInfo(NULL, 100, &wksta_buffer_pointer)) {
  192. #ifdef CANONDBG
  193. DbgPrint("error: NetWkstaGetInfo returns %lu\n", rc);
  194. #endif
  195. return rc; // didn't work
  196. }
  197. wksta_name_uncanon =
  198. ((LPWKSTA_INFO_100) wksta_buffer_pointer)->wki100_computername;
  199. #ifdef CANONDBG
  200. DbgPrint("NetWkstaGetInfo returns level 100 computer name (uncanon)= %s\n",
  201. wksta_name_uncanon);
  202. #endif
  203. rc = NetpNameCanonicalize(
  204. NULL, // performed here, on our own premises
  205. wksta_name_uncanon, // this is input
  206. wksta_name_canon, // this is output
  207. sizeof(wksta_name_canon), // how much buffer we have
  208. NAMETYPE_COMPUTER, // what we think it is
  209. INNCA_FLAGS_FULL_BUFLEN // say that o/p buffer must be large
  210. // enough for maximum-sized computer
  211. // name. Why? you ask, well its a fair
  212. // cop - the reason is that we can't
  213. // get into trouble on the one time that
  214. // we exercise the maximum requirement
  215. );
  216. NetpAssert( rc == NERR_Success );
  217. //
  218. // compare our name and the name passed to us. NetpNameCompare returns
  219. // 0 if the names match else 1 or -1 (a la strcmp)
  220. //
  221. //
  222. // if the caller gave us a NULL computer name but wants a canonicalized
  223. // name output then get a pointer to the canonicalized name from
  224. // NetWkstaGetInfo
  225. //
  226. if (map_local_name) {
  227. canonicalized_name = wksta_name_canon;
  228. } else {
  229. //
  230. // otherwise, we have a non-NULL computername to compare with this
  231. // computer's name
  232. //
  233. result = NetpNameCompare(
  234. NULL, // performed here, on our own premises
  235. name, // canonicalized version of passed name
  236. wksta_name_canon, // name of our computer
  237. NAMETYPE_COMPUTER,
  238. INNC_FLAGS_NAMES_CANONICALIZED
  239. );
  240. }
  241. //
  242. // if the specified name equates to our computer name then its still local
  243. //
  244. *LocalOrRemote = (DWORD)((result == 0) ? ISLOCAL : ISREMOTE);
  245. //
  246. // if the caller specified that the canonicalized name be returned, then
  247. // give it to 'em. Note that the returned name is prefixed with \\ - it
  248. // is assumed the name is then used in a call to eg NetRemoteComputerSupports
  249. //
  250. if (ARGUMENT_PRESENT(CanonicalizedName)) {
  251. STRCPY(CanonicalizedName, TEXT("\\\\"));
  252. STRCAT(CanonicalizedName, canonicalized_name);
  253. }
  254. //
  255. // free the buffer created by NetWkstaGetInfo
  256. //
  257. NetApiBufferFree(wksta_buffer_pointer);
  258. return NERR_Success;
  259. }