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.

420 lines
9.1 KiB

  1. /* Copyright (c) 1995, Microsoft Corporation, all rights reserved
  2. **
  3. ** rassrv.c
  4. ** RAS Server helpers
  5. ** Listed alphabetically
  6. **
  7. ** 03/05/96 Abolade Gbadegesin
  8. */
  9. #include <windows.h> // Win32 root
  10. #include <debug.h> // Trace/Assert library
  11. #include <nouiutil.h> // Our public header
  12. #include <raserror.h> // RAS error constants
  13. HANDLE g_hserver = NULL;
  14. DWORD
  15. RasServerConnect(
  16. IN HANDLE* phserver );
  17. DWORD
  18. GetRasConnection0Table(
  19. OUT RAS_CONNECTION_0 ** ppRc0Table,
  20. OUT DWORD * piRc0Count )
  21. /* This function queries the RAS server for a table of the inbound
  22. ** connections on the local machine.
  23. **
  24. ** (Abolade Gbadegesin Mar-05-1996)
  25. */
  26. {
  27. DWORD dwErr, dwTotal;
  28. dwErr = RasServerConnect(&g_hserver);
  29. if (dwErr != NO_ERROR) { return dwErr; }
  30. return g_pRasAdminConnectionEnum(
  31. g_hserver,
  32. 0,
  33. (BYTE**)ppRc0Table,
  34. (DWORD)-1,
  35. piRc0Count,
  36. &dwTotal,
  37. NULL
  38. );
  39. }
  40. DWORD
  41. GetRasdevFromRasPort0(
  42. IN RAS_PORT_0* pport,
  43. OUT RASDEV** ppdev,
  44. IN RASDEV* pDevTable OPTIONAL,
  45. IN DWORD iDevCount OPTIONAL )
  46. /* Given a RAS_PORT_0 structure, this function
  47. ** retrieves the RASDEV for the device referred to by the RAS_PORT_0.
  48. ** The second and third arguments are optional; they specify a
  49. ** table of RASDEV structures to be searched. This is useful if the
  50. ** caller has already enumerated the existing devices, so that this
  51. ** function does not need to re-enumerate them.
  52. **
  53. ** (Abolade Gbadegesin Nov-9-1995)
  54. */
  55. {
  56. DWORD i, dwErr;
  57. BOOL bFreeTable;
  58. TCHAR szPort[MAX_PORT_NAME + 1], *pszPort;
  59. //
  60. // validate the arguments
  61. //
  62. if (pport == NULL || ppdev == NULL) { return ERROR_INVALID_PARAMETER; }
  63. *ppdev = NULL;
  64. //
  65. // retrieve the device table if the caller didn't pass one in
  66. //
  67. bFreeTable = FALSE;
  68. if (pDevTable == NULL) {
  69. dwErr = GetRasdevTable(&pDevTable, &iDevCount);
  70. if (dwErr != NO_ERROR) {
  71. return dwErr;
  72. }
  73. bFreeTable = TRUE;
  74. }
  75. //
  76. // retrieve the portname for the RAS_PORT_0 passed in
  77. //
  78. #ifdef UNICODE
  79. pszPort = pport->wszPortName;
  80. #else
  81. StrCpyAFromW(
  82. szPort,
  83. pport->wszPortName,
  84. MAX_PORT_NAME + 1);
  85. pszPort = szPort;
  86. #endif
  87. //
  88. // find the device to which the HPORT corresponds
  89. //
  90. for (i = 0; i < iDevCount; i++) {
  91. if (lstrcmpi(pszPort, (pDevTable + i)->RD_PortName) == 0) { break; }
  92. }
  93. //
  94. // see how the search ended
  95. //
  96. if (i >= iDevCount) {
  97. dwErr = ERROR_NO_DATA;
  98. }
  99. else {
  100. dwErr = NO_ERROR;
  101. if (!bFreeTable) {
  102. *ppdev = pDevTable + i;
  103. }
  104. else {
  105. *ppdev = Malloc(sizeof(RASDEV));
  106. if (!*ppdev) { dwErr = ERROR_NOT_ENOUGH_MEMORY; }
  107. else {
  108. **ppdev = *(pDevTable + i);
  109. (*ppdev)->RD_DeviceName = StrDup(pDevTable[i].RD_DeviceName);
  110. if (!(*ppdev)->RD_DeviceName) {
  111. Free(*ppdev);
  112. *ppdev = NULL;
  113. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  114. }
  115. }
  116. }
  117. }
  118. if (bFreeTable) { FreeRasdevTable(pDevTable, iDevCount); }
  119. return dwErr;
  120. }
  121. DWORD
  122. GetRasPort0FromRasdev(
  123. IN RASDEV* pdev,
  124. OUT RAS_PORT_0** ppport,
  125. IN RAS_PORT_0* pPortTable OPTIONAL,
  126. IN DWORD iPortCount OPTIONAL )
  127. /* Given a RASDEV structure for an active device, this function retrieves
  128. ** the RAS_PORT_0 which corresponds to the device. The
  129. ** second and third arguments are optional; they specify a table of
  130. ** RAS_PORT_0 structures to be searched. This is useful if the caller has
  131. ** already enumerated the server's ports, so that this function does
  132. ** not need to re-enumerate them.
  133. **
  134. ** (Abolade Gbadegesin Feb-13-1996)
  135. */
  136. {
  137. BOOL bFreeTable;
  138. DWORD dwErr, i;
  139. WCHAR wszPort[MAX_PORT_NAME + 1], *pwszPort;
  140. //
  141. // validate arguments
  142. //
  143. if (pdev == NULL || ppport == NULL) { return ERROR_INVALID_PARAMETER; }
  144. *ppport = NULL;
  145. bFreeTable = FALSE;
  146. //
  147. // if the caller didn't pass in a table of RAS_PORT_0's, retrieve one
  148. //
  149. if (pPortTable == NULL) {
  150. dwErr = GetRasPort0Table(&pPortTable, &iPortCount);
  151. if (dwErr != NO_ERROR) { return dwErr; }
  152. bFreeTable = TRUE;
  153. }
  154. //
  155. // find the admin port which matches the RASDEV passed in
  156. //
  157. #ifdef UNICODE
  158. pwszPort = pdev->RD_PortName;
  159. #else
  160. StrCpyWFromA(wszPort, pdev->P_PortName, MAX_PORT_NAME);
  161. pwszPort = wszPort;
  162. #endif
  163. for (i = 0; i < iPortCount; i++) {
  164. if (lstrcmpiW(pwszPort, (pPortTable + i)->wszPortName) == 0) {
  165. break;
  166. }
  167. }
  168. //
  169. // see how the search ended
  170. //
  171. if (i >= iPortCount) {
  172. dwErr = ERROR_NO_DATA;
  173. }
  174. else {
  175. dwErr = NO_ERROR;
  176. if (!bFreeTable) {
  177. //
  178. // point to the place where we found the RAS_PORT_0
  179. //
  180. *ppport = pPortTable + i;
  181. }
  182. else {
  183. //
  184. // make a copy of the RAS_PORT_0 found
  185. //
  186. *ppport = Malloc(sizeof(RAS_PORT_0));
  187. if (!*ppport) { dwErr = ERROR_NOT_ENOUGH_MEMORY; }
  188. else { **ppport = *(pPortTable + i); }
  189. }
  190. }
  191. if (bFreeTable) { g_pRasAdminBufferFree(pPortTable); }
  192. return dwErr;
  193. }
  194. DWORD
  195. GetRasPort0Info(
  196. IN HANDLE hPort,
  197. OUT RAS_PORT_1 * pRasPort1 )
  198. /* This function queries the local RAS server for information
  199. ** about the specified port.
  200. **
  201. ** (Abolade Gbadegesin Mar-05-1996)
  202. */
  203. {
  204. DWORD dwErr;
  205. dwErr = RasServerConnect(&g_hserver);
  206. if (dwErr != NO_ERROR) { return dwErr; }
  207. return g_pRasAdminPortGetInfo(
  208. g_hserver,
  209. 1,
  210. hPort,
  211. (BYTE**)&pRasPort1
  212. );
  213. }
  214. DWORD
  215. GetRasPort0Table(
  216. OUT RAS_PORT_0 ** ppPortTable,
  217. OUT DWORD * piPortCount )
  218. /* This function queries the RAS server for a table of the dial-in ports
  219. ** on the local machine.
  220. **
  221. ** (Abolade Gbadegesin Mar-05-1996)
  222. */
  223. {
  224. DWORD dwErr;
  225. DWORD dwTotal;
  226. dwErr = RasServerConnect(&g_hserver);
  227. if (dwErr != NO_ERROR) { return dwErr; }
  228. dwErr = g_pRasAdminPortEnum(
  229. g_hserver,
  230. 0,
  231. INVALID_HANDLE_VALUE,
  232. (BYTE**)ppPortTable,
  233. (DWORD)-1,
  234. piPortCount,
  235. &dwTotal,
  236. NULL
  237. );
  238. return dwErr;
  239. }
  240. TCHAR *
  241. GetRasPort0UserString(
  242. IN RAS_PORT_0 * pport,
  243. IN TCHAR * pszUser OPTIONAL )
  244. /* This function formats the user and domain in the specified port
  245. ** as a standard DOMAINNAME\username string and returns the result,
  246. ** unless the argument 'pszUser' is non-NULL in which case
  247. ** the result is formatted into the given string.
  248. **
  249. ** (Abolade Gbadegesin Mar-06-1996)
  250. */
  251. {
  252. DWORD dwErr;
  253. PTSTR psz = NULL;
  254. RAS_CONNECTION_0 *prc0 = NULL;
  255. dwErr = RasServerConnect(&g_hserver);
  256. if (dwErr != NO_ERROR) { return NULL; }
  257. do {
  258. dwErr = g_pRasAdminConnectionGetInfo(
  259. g_hserver,
  260. 0,
  261. pport->hConnection,
  262. (BYTE**)&prc0
  263. );
  264. if (dwErr != NO_ERROR) { break; }
  265. if (pszUser) { psz = pszUser; }
  266. else {
  267. psz = Malloc(
  268. (lstrlenW(prc0->wszUserName) +
  269. lstrlenW(prc0->wszLogonDomain) + 2) * sizeof(TCHAR)
  270. );
  271. if (!psz) { break; }
  272. }
  273. wsprintf(psz, TEXT("%ls\\%ls"), prc0->wszLogonDomain, prc0->wszUserName);
  274. } while(FALSE);
  275. if (prc0) { g_pRasAdminBufferFree(prc0); }
  276. return psz;
  277. }
  278. DWORD
  279. RasPort0Hangup(
  280. IN HANDLE hPort )
  281. /* This function hangs up the specified dial-in port
  282. ** on the local RAS server.
  283. **
  284. ** (Abolade Gbadegesin Mar-05-1996)
  285. */
  286. {
  287. DWORD dwErr;
  288. dwErr = RasServerConnect(&g_hserver);
  289. if (dwErr != NO_ERROR) { return dwErr; }
  290. dwErr = g_pRasAdminPortDisconnect(g_hserver, hPort);
  291. return dwErr;
  292. }
  293. DWORD
  294. RasServerConnect(
  295. IN HANDLE* phserver )
  296. /* This function establishes a connection to the local MPR RAS server,
  297. ** if the connection has not already been established.
  298. */
  299. {
  300. DWORD dwErr;
  301. if (*phserver) { return NO_ERROR; }
  302. dwErr = g_pRasAdminServerConnect(NULL, phserver);
  303. return dwErr;
  304. }