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.

247 lines
6.8 KiB

  1. //================================================================================
  2. // Copyright (C) 1997 Microsoft Corporation
  3. // Author: Koti, modified by RameshV
  4. // Description: Validates a service against the DS
  5. //================================================================================
  6. #include <hdrmacro.h>
  7. #include <store.h>
  8. #include <dhcpmsg.h>
  9. #include <wchar.h>
  10. #include <dhcpbas.h>
  11. #include <dhcpapi.h>
  12. #include <st_srvr.h>
  13. #include <rpcapi2.h>
  14. #undef NET_API_FUNCTION
  15. #include <iphlpapi.h>
  16. #include <dnsapi.h>
  17. #include <mmreg\regutil.h>
  18. //
  19. // Retrieve a list of IP addresses for the interfaces
  20. //
  21. DWORD
  22. GetIpAddressList(
  23. IN OUT PDWORD *Addresses,
  24. IN OUT DWORD *nAddresses
  25. )
  26. {
  27. DWORD Size, Error, i;
  28. PMIB_IPADDRTABLE IpAddrTable;
  29. AssertRet((( Addresses != NULL ) && ( nAddresses != NULL)),
  30. ERROR_INVALID_PARAMETER );
  31. Size = 0;
  32. // Get the required size
  33. Error = GetIpAddrTable( NULL, &Size, FALSE );
  34. if( ERROR_INSUFFICIENT_BUFFER != Error ) {
  35. return Error;
  36. }
  37. IpAddrTable = MemAlloc( Size );
  38. if ( NULL == IpAddrTable ) {
  39. return ERROR_NOT_ENOUGH_MEMORY;
  40. }
  41. Error = GetIpAddrTable( IpAddrTable, &Size, FALSE );
  42. if (( NO_ERROR != Error ) ||
  43. ( 0 == IpAddrTable->dwNumEntries )) {
  44. *Addresses = NULL;
  45. *nAddresses = 0;
  46. MemFree(IpAddrTable);
  47. return Error;
  48. }
  49. *Addresses = MemAlloc( IpAddrTable->dwNumEntries * sizeof( DWORD ));
  50. if ( NULL == *Addresses ) {
  51. *nAddresses = 0;
  52. MemFree( IpAddrTable );
  53. return ERROR_NOT_ENOUGH_MEMORY;
  54. }
  55. *nAddresses = IpAddrTable->dwNumEntries ;
  56. for( i = 0; i < *nAddresses; i ++ ) {
  57. (*Addresses)[i] = IpAddrTable->table[i].dwAddr;
  58. }
  59. MemFree( IpAddrTable );
  60. return ERROR_SUCCESS;
  61. } // GetIpAddressList()
  62. LPWSTR GetHostName(
  63. VOID
  64. )
  65. {
  66. LPWSTR HostName;
  67. DWORD Length;
  68. DWORD Error;
  69. // Get the required length
  70. Length = 0;
  71. GetComputerNameExW( ComputerNameDnsFullyQualified,
  72. NULL, &Length );
  73. HostName = MemAlloc( Length * sizeof( WCHAR ));
  74. if ( NULL == HostName ) {
  75. return NULL;
  76. }
  77. if ( !GetComputerNameExW( ComputerNameDnsFullyQualified,
  78. HostName, &Length )) {
  79. MemFree( HostName );
  80. return NULL;
  81. }
  82. return HostName;
  83. } // GetHostName()
  84. //BeginExport(function)
  85. //DOC This function is declared in dhcpds.c..
  86. //DOC DhcpDsValidateService checks the given service in the DS to see if it exists
  87. //DOC If the machine is a standalone, it sets IsStandAlone and returns ERROR_SUCCESS
  88. //DOC If the entry for the given Address is found, it sets Found to TRUE and returns
  89. //DOC ERROR_SUCCESS. If the DhcpRoot node is found, but entry is not Found, it sets
  90. //DOC Found to FALSE and returns ERROR_SUCCESS; If the DS could not be reached, it
  91. //DOC returns ERROR_FILE_NOT_FOUND or probably other DS errors.
  92. DWORD
  93. DhcpDsValidateService( // check to validate for dhcp
  94. IN LPWSTR Domain,
  95. IN DWORD *Addresses, OPTIONAL
  96. IN ULONG nAddresses,
  97. IN LPWSTR UserName,
  98. IN LPWSTR Password,
  99. IN DWORD AuthFlags,
  100. OUT LPBOOL Found,
  101. OUT LPBOOL IsStandAlone // not used
  102. ) //EndExport(function)
  103. {
  104. DWORD Result, Error,i;
  105. STORE_HANDLE hStoreCC, hDhcpC, hDhcpRoot;
  106. DWORD *Addr;
  107. BOOL TableAddr;
  108. WCHAR PrintableIp[ 20 ]; // 000.000.000.000
  109. LPWSTR HostName;
  110. if( NULL == Found || NULL == IsStandAlone ) {
  111. return ERROR_INVALID_PARAMETER;
  112. }
  113. Error = ERROR_FILE_NOT_FOUND;
  114. *IsStandAlone = FALSE;
  115. *Found = FALSE;
  116. Result = StoreInitHandle // get the config container handle
  117. (
  118. /* hStore */ &hStoreCC, // config container
  119. /* Reserved */ DDS_RESERVED_DWORD,
  120. /* ThisDomain */ Domain,
  121. /* UserName */ UserName,
  122. /* Password */ Password,
  123. /* AuthFlags */ AuthFlags
  124. );
  125. if( ERROR_SUCCESS != Result ) return Result; // DS error
  126. Result = DhcpDsGetRoot // get dhcp root object
  127. (
  128. /* Flags */ 0, // no flags
  129. /* hStoreCC */ &hStoreCC,
  130. /* hDhcpRoot */ &hDhcpRoot
  131. );
  132. if( ERROR_SUCCESS != Result ) {
  133. //
  134. // If the failure is because the dhcp root object
  135. // can't be seen, then treat that as positive failure
  136. // to authorize.
  137. //
  138. if( ERROR_DDS_NO_DHCP_ROOT == Result ) {
  139. Result = GetLastError();
  140. }
  141. StoreCleanupHandle(&hStoreCC, 0);
  142. return Result;
  143. }
  144. Result = DhcpDsGetDhcpC
  145. (
  146. /* Flags */ 0, // no flags
  147. /* hStoreCC */ &hStoreCC,
  148. /* hDhcpC */ &hDhcpC
  149. );
  150. if( ERROR_SUCCESS != Result ) {
  151. StoreCleanupHandle(&hStoreCC, 0);
  152. StoreCleanupHandle(&hDhcpRoot, 0);
  153. return Result;
  154. }
  155. do {
  156. // if addresses are not specified, get it from the ipaddr table
  157. if( NULL != Addresses && 0 != nAddresses ) {
  158. Addr = Addresses;
  159. TableAddr = FALSE;
  160. }
  161. else {
  162. Error = GetIpAddressList( &Addr, &nAddresses );
  163. if ( ERROR_SUCCESS != Error) {
  164. break;
  165. }
  166. TableAddr = TRUE;
  167. } // else
  168. //
  169. // Check to see if any of the ip addresses or hostname are authorized
  170. // A seperate call to check for hostname is not necessary because,
  171. // the hostname is also added to the filter.
  172. //
  173. HostName = GetHostName();
  174. if ( NULL == HostName ) {
  175. Error = GetLastError();
  176. break;
  177. }
  178. for ( i = 0; i < nAddresses; i++ ) {
  179. // skip loopback IP 127.0.0.1
  180. if ( INADDR_LOOPBACK == htonl( Addr [ i ] )) {
  181. continue;
  182. }
  183. ConvertAddressToLPWSTR( htonl( Addr[ i ] ), PrintableIp );
  184. Result = DhcpDsLookupServer( &hDhcpC, &hDhcpRoot,
  185. DDS_RESERVED_DWORD,
  186. PrintableIp, HostName );
  187. if ( Result ) {
  188. *Found = TRUE;
  189. Error = ERROR_SUCCESS;
  190. break;
  191. }
  192. } // for i
  193. } while ( FALSE );
  194. StoreCleanupHandle(&hStoreCC, 0); // free ds handle
  195. StoreCleanupHandle(&hDhcpRoot, 0); // free ds handle
  196. StoreCleanupHandle(&hDhcpC, 0); // free ds handle
  197. if( TableAddr && NULL != Addr ) {
  198. MemFree( Addr );
  199. }
  200. return Error;
  201. } // DhcpDsValidateService()
  202. //================================================================================
  203. // end of file
  204. //================================================================================