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.

460 lines
16 KiB

  1. //================================================================================
  2. // Copyright (C) 1997 Microsoft Corporation
  3. // Author: RameshV
  4. // Description: Actual stubs that are the equivalent the rpcapi1.c and rpcapi2.c
  5. // in the server\server directory.. (or more accurately, the implementations are
  6. // the same as for the functions defined in server\client\dhcpsapi.def)
  7. // NOTE: THE FOLLOWING FUNCTIONS ARE NOT RPC, BUT THEY BEHAVE JUST THE SAME AS
  8. // THE DHCP RPC CALLS, EXCEPT THEY ACCESS THE DS DIRECTLY.
  9. //================================================================================
  10. #include <hdrmacro.h>
  11. #include <store.h>
  12. #include <dhcpmsg.h>
  13. #include <wchar.h>
  14. #include <dhcpbas.h>
  15. #include <mm\opt.h>
  16. #include <mm\optl.h>
  17. #include <mm\optdefl.h>
  18. #include <mm\optclass.h>
  19. #include <mm\classdefl.h>
  20. #include <mm\bitmask.h>
  21. #include <mm\reserve.h>
  22. #include <mm\range.h>
  23. #include <mm\subnet.h>
  24. #include <mm\sscope.h>
  25. #include <mm\oclassdl.h>
  26. #include <mm\server.h>
  27. #include <mm\address.h>
  28. #include <mm\server2.h>
  29. #include <mm\memfree.h>
  30. #include <mmreg\regutil.h>
  31. #include <mmreg\regread.h>
  32. #include <mmreg\regsave.h>
  33. #include <dhcpapi.h>
  34. #include <delete.h>
  35. #include <st_srvr.h>
  36. #include <rpcapi2.h>
  37. //================================================================================
  38. // global variables..
  39. //================================================================================
  40. BOOL
  41. StubInitialized = FALSE;
  42. STORE_HANDLE hDhcpC, hDhcpRoot;
  43. CRITICAL_SECTION DhcpDsDllCriticalSection;
  44. //================================================================================
  45. // THE FOLLOWING FUNCTIONS HAVE BEEN COPIED OVER FROM RPCAPI1.C (IN THE
  46. // DHCP\SERVER\SERVER DIRECTORY).
  47. //================================================================================
  48. #undef DhcpPrint
  49. #define DhcpPrint(X)
  50. #define DhcpAssert(X)
  51. //================================================================================
  52. // helper routines
  53. //================================================================================
  54. VOID
  55. MemFreeFunc( // free memory
  56. IN OUT LPVOID Mem
  57. )
  58. {
  59. MemFree(Mem);
  60. }
  61. //
  62. // ErrorNotInitialized used to be ZERO.. but why would we NOT return an error?
  63. // so changed it to return errors..
  64. //
  65. #define ErrorNotInitialized Err
  66. #define STUB_NOT_INITIALIZED(Err) ( !StubInitialized && ((Err) = StubInitialize()))
  67. //DOC StubInitialize initializes all the modules involved in the dhcp ds dll.
  68. //DOC It also sets a global variable StubInitialized to TRUE to indicate that
  69. //DOC initialization went fine. This should be called as part of DllInit so that
  70. //DOC everything can be done at this point..
  71. DWORD
  72. StubInitialize( // initialize all global vars
  73. VOID
  74. )
  75. {
  76. DWORD Err,Err2;
  77. STORE_HANDLE ConfigC;
  78. if( StubInitialized ) return ERROR_SUCCESS; // already initialized
  79. Err = Err2 = ERROR_SUCCESS;
  80. EnterCriticalSection( &DhcpDsDllCriticalSection );
  81. do {
  82. if( StubInitialized ) break;
  83. Err = StoreInitHandle(
  84. /* hStore */ &ConfigC,
  85. /* Reserved */ DDS_RESERVED_DWORD,
  86. /* ThisDomain */ NULL, // current domain
  87. /* UserName */ NULL, // current user
  88. /* Password */ NULL, // current credentials
  89. /* AuthFlags */ ADS_SECURE_AUTHENTICATION | ADS_USE_SIGNING | ADS_USE_SEALING
  90. );
  91. if( ERROR_SUCCESS != Err ) {
  92. Err = ERROR_DDS_NO_DS_AVAILABLE; // could not get config hdl
  93. break;
  94. }
  95. Err = DhcpDsGetDhcpC(
  96. DDS_RESERVED_DWORD, &ConfigC, &hDhcpC
  97. );
  98. if( ERROR_SUCCESS == Err ) {
  99. Err2 = DhcpDsGetRoot( // now try to get root handle
  100. DDS_FLAGS_CREATE, &ConfigC, &hDhcpRoot
  101. );
  102. }
  103. StoreCleanupHandle(&ConfigC, DDS_RESERVED_DWORD);
  104. } while (0);
  105. if( ERROR_SUCCESS != Err2 ) { // could not get dhcp root hdl
  106. DhcpAssert(ERROR_SUCCESS == Err);
  107. StoreCleanupHandle(&hDhcpC, DDS_RESERVED_DWORD);
  108. Err = Err2;
  109. }
  110. StubInitialized = (ERROR_SUCCESS == Err );
  111. LeaveCriticalSection( &DhcpDsDllCriticalSection );
  112. return Err;
  113. }
  114. //DOC StubCleanup de-initializes all the modules involved in the dhcp ds dll.
  115. //DOC its effect is to undo everything done by StubInitialize
  116. VOID
  117. StubCleanup( // undo StubInitialize
  118. VOID
  119. )
  120. {
  121. if( ! StubInitialized ) return; // never initialized anyways
  122. EnterCriticalSection(&DhcpDsDllCriticalSection);
  123. if( StubInitialized ) {
  124. StoreCleanupHandle(&hDhcpC, DDS_RESERVED_DWORD);
  125. StoreCleanupHandle(&hDhcpRoot, DDS_RESERVED_DWORD);
  126. StubInitialized = FALSE;
  127. }
  128. LeaveCriticalSection(&DhcpDsDllCriticalSection);
  129. }
  130. //DOC DhcpDsLock is not yet implemented
  131. DWORD
  132. DhcpDsLock( // lock the ds
  133. IN OUT LPSTORE_HANDLE hDhcpRoot // dhcp root object to lock via
  134. )
  135. {
  136. EnterCriticalSection(&DhcpDsDllCriticalSection);
  137. return ERROR_SUCCESS;
  138. }
  139. //DOC DhcpDsUnlock not yet implemented
  140. VOID
  141. DhcpDsUnlock(
  142. IN OUT LPSTORE_HANDLE hDhcpRoot // dhcp root object..
  143. )
  144. {
  145. LeaveCriticalSection(&DhcpDsDllCriticalSection);
  146. }
  147. //DOC GetServerNameFromAddr gets the server name given ip address
  148. DWORD
  149. GetServerNameFromAddr( // get server name from ip addr
  150. IN DWORD IpAddress, // look for server w/ this addr
  151. OUT LPWSTR *ServerName // fill this with matching name
  152. )
  153. {
  154. DWORD Err, Err2;
  155. ARRAY Servers;
  156. ARRAY_LOCATION Loc;
  157. PEATTRIB ThisAttrib;
  158. LPWSTR ThisStr, AllocStr;
  159. MemArrayInit(&Servers);
  160. Err = DhcpDsGetLists // get list of servers
  161. (
  162. /* Reserved */ DDS_RESERVED_DWORD,
  163. /* hStore */ &hDhcpRoot,
  164. /* RecursionDepth */ 0xFFFFFFFF,
  165. /* Servers */ &Servers, // array of PEATTRIB 's
  166. /* Subnets */ NULL,
  167. /* IpAddress */ NULL,
  168. /* Mask */ NULL,
  169. /* Ranges */ NULL,
  170. /* Sites */ NULL,
  171. /* Reservations */ NULL,
  172. /* SuperScopes */ NULL,
  173. /* OptionDescription */ NULL,
  174. /* OptionsLocation */ NULL,
  175. /* Options */ NULL,
  176. /* Classes */ NULL
  177. );
  178. if( ERROR_SUCCESS != Err ) return Err;
  179. ThisStr = NULL;
  180. for( // find name for ip-address
  181. Err = MemArrayInitLoc(&Servers,&Loc)
  182. ; ERROR_FILE_NOT_FOUND != Err ;
  183. Err = MemArrayNextLoc(&Servers, &Loc)
  184. ) {
  185. //= require ERROR_SUCCESS == Err
  186. Err = MemArrayGetElement(&Servers, &Loc, &ThisAttrib);
  187. //= require ERROR_SUCCESS == Err && NULL != ThisAttrib
  188. if( !IS_STRING1_PRESENT(ThisAttrib) || // no name for this server
  189. !IS_ADDRESS1_PRESENT(ThisAttrib) ) { // no address for this server
  190. continue; //= ds inconsistent
  191. }
  192. ThisStr = ThisAttrib->String1;
  193. break;
  194. }
  195. AllocStr = NULL;
  196. if( NULL == ThisStr ) { // didnt find server name
  197. Err = ERROR_FILE_NOT_FOUND;
  198. } else { // found the server name
  199. AllocStr = MemAlloc(sizeof(WCHAR)*(1+wcslen(ThisStr)));
  200. if( NULL == AllocStr ) { // couldnt alloc mem?
  201. Err = ERROR_NOT_ENOUGH_MEMORY;
  202. } else { // now just copy the str over
  203. wcscpy(AllocStr, ThisStr);
  204. Err = ERROR_SUCCESS;
  205. }
  206. }
  207. MemArrayFree(&Servers, MemFreeFunc);
  208. *ServerName = AllocStr;
  209. return Err;
  210. }
  211. //================================================================================
  212. // the following functions are NOT based on RPC, but actually direct calls to
  213. // the DS. But, they have the same interface as the RPC stubs in dhcpsapi.dll.
  214. //================================================================================
  215. BOOLEAN
  216. DllMain(
  217. IN HINSTANCE DllHandle,
  218. IN ULONG Reason,
  219. IN PCONTEXT Context OPTIONAL
  220. )
  221. /*++
  222. Routine Description:
  223. This routine is the standard DLL initialization
  224. routine and all it does is intiialize a critical section
  225. for actual initialization to be done at startup elsewhere.
  226. Arguments:
  227. DllHandle -- handle to current module
  228. Reason -- reason for DLL_PROCESS_ATTACH.. DLL_PROCESS_DETACH
  229. Return Value:
  230. TRUE -- success, FALSE -- failure
  231. --*/
  232. {
  233. if( DLL_PROCESS_ATTACH == Reason ) {
  234. //
  235. // First disable further calls to DllInit
  236. //
  237. if( !DisableThreadLibraryCalls( DllHandle ) ) return FALSE;
  238. //
  239. // Now try to create critical section
  240. //
  241. try {
  242. InitializeCriticalSection(&DhcpDsDllCriticalSection);
  243. } except ( EXCEPTION_EXECUTE_HANDLER ) {
  244. // shouldnt happen but you never know.
  245. return FALSE;
  246. }
  247. } else if( DLL_PROCESS_DETACH == Reason ) {
  248. //
  249. // Cleanup the initialization critical section
  250. //
  251. DeleteCriticalSection(&DhcpDsDllCriticalSection);
  252. }
  253. //
  254. // InitializeCriticalSection does not fail, just throws exception..
  255. // so we always return success.
  256. //
  257. return TRUE;
  258. }
  259. //================================================================================
  260. // DS only NON-rpc stubs
  261. //================================================================================
  262. //BeginExport(function)
  263. //DOC DhcpEnumServersDS lists the servers found in the DS along with the
  264. //DOC addresses and other information. The whole server is allocated as a blob,
  265. //DOC and should be freed in one shot. No parameters are currently used, other
  266. //DOC than Servers which will be an OUT parameter only.
  267. DWORD
  268. DhcpEnumServersDS(
  269. IN DWORD Flags,
  270. IN LPVOID IdInfo,
  271. OUT LPDHCP_SERVER_INFO_ARRAY *Servers,
  272. IN LPVOID CallbackFn,
  273. IN LPVOID CallbackData
  274. ) //EndExport(function)
  275. {
  276. DWORD Err, Err2, Size,i;
  277. LPDHCPDS_SERVERS DhcpDsServers;
  278. AssertRet(Servers, ERROR_INVALID_PARAMETER);
  279. AssertRet(!Flags, ERROR_INVALID_PARAMETER);
  280. *Servers = NULL;
  281. if( STUB_NOT_INITIALIZED(Err) ) return ERROR_DDS_NO_DS_AVAILABLE;
  282. Err = DhcpDsLock(&hDhcpRoot); // take a lock on the DS
  283. if( ERROR_SUCCESS != Err ) return ERROR_DDS_NO_DS_AVAILABLE;
  284. DhcpDsServers = NULL;
  285. Err = DhcpDsEnumServers // get the list of servers
  286. (
  287. /* hDhcpC */ &hDhcpC,
  288. /* hDhcpRoot */ &hDhcpRoot,
  289. /* Reserved */ DDS_RESERVED_DWORD,
  290. /* ServersInfo */ &DhcpDsServers
  291. );
  292. DhcpDsUnlock(&hDhcpRoot);
  293. if( ERROR_SUCCESS != Err ) return Err; // return err..
  294. *Servers = DhcpDsServers;
  295. return ERROR_SUCCESS;
  296. }
  297. //BeginExport(function)
  298. //DOC DhcpAddServerDS adds a particular server to the DS. If the server exists,
  299. //DOC then, this returns error. If the server does not exist, then this function
  300. //DOC adds the server in DS, and also uploads the configuration from the server
  301. //DOC to the ds.
  302. DWORD
  303. DhcpAddServerDS(
  304. IN DWORD Flags,
  305. IN LPVOID IdInfo,
  306. IN LPDHCP_SERVER_INFO NewServer,
  307. IN LPVOID CallbackFn,
  308. IN LPVOID CallbackData
  309. ) //EndExport(function)
  310. {
  311. DWORD Err, Err2;
  312. WCHAR TmpBuf[sizeof(L"000.000.000.000")];
  313. AssertRet(NewServer, ERROR_INVALID_PARAMETER);
  314. AssertRet(!Flags, ERROR_INVALID_PARAMETER);
  315. if( STUB_NOT_INITIALIZED(Err) ) return ERROR_DDS_NO_DS_AVAILABLE;
  316. Err = DhcpDsLock(&hDhcpRoot); // take a lock on the DS
  317. if( ERROR_SUCCESS != Err ) return ERROR_DDS_NO_DS_AVAILABLE;
  318. Err = DhcpDsAddServer // add the new server
  319. (
  320. /* hDhcpC */ &hDhcpC,
  321. /* hDhcpRoot */ &hDhcpRoot,
  322. /* Reserved */ DDS_RESERVED_DWORD,
  323. /* ServerName */ NewServer->ServerName,
  324. /* ReservedPtr */ DDS_RESERVED_PTR,
  325. /* IpAddress */ NewServer->ServerAddress,
  326. /* State */ Flags
  327. );
  328. DhcpDsUnlock(&hDhcpRoot);
  329. return Err;
  330. }
  331. //BeginExport(function)
  332. //DOC DhcpDeleteServerDS deletes the servers from off the DS and recursively
  333. //DOC deletes the server object..(i.e everything belonging to the server is deleted).
  334. //DOC If the server does not exist, it returns an error.
  335. DWORD
  336. DhcpDeleteServerDS(
  337. IN DWORD Flags,
  338. IN LPVOID IdInfo,
  339. IN LPDHCP_SERVER_INFO NewServer,
  340. IN LPVOID CallbackFn,
  341. IN LPVOID CallbackData
  342. ) //EndExport(function)
  343. {
  344. DWORD Err, Err2;
  345. AssertRet(NewServer, ERROR_INVALID_PARAMETER);
  346. AssertRet(!Flags, ERROR_INVALID_PARAMETER);
  347. if( STUB_NOT_INITIALIZED(Err) ) return ERROR_DDS_NO_DS_AVAILABLE;
  348. Err = DhcpDsLock(&hDhcpRoot); // take a lock on the DS
  349. if( ERROR_SUCCESS != Err ) return ERROR_DDS_NO_DS_AVAILABLE;
  350. Err = DhcpDsDelServer // del this server
  351. (
  352. /* hDhcpC */ &hDhcpC,
  353. /* hDhcpRoot */ &hDhcpRoot,
  354. /* Reserved */ DDS_RESERVED_DWORD,
  355. /* ServerName */ NewServer->ServerName,
  356. /* ReservedPtr */ DDS_RESERVED_PTR,
  357. /* IpAddress */ NewServer->ServerAddress
  358. );
  359. DhcpDsUnlock(&hDhcpRoot);
  360. return Err;
  361. }
  362. //BeginExport(function)
  363. //DOC DhcpDsInitDS initializes everything in this module.
  364. DWORD
  365. DhcpDsInitDS(
  366. DWORD Flags,
  367. LPVOID IdInfo
  368. ) //EndExport(function)
  369. {
  370. return StubInitialize();
  371. }
  372. //BeginExport(function)
  373. //DOC DhcpDsCleanupDS uninitiailzes everything in this module.
  374. VOID
  375. DhcpDsCleanupDS(
  376. VOID
  377. ) //EndExport(function)
  378. {
  379. StubCleanup();
  380. }
  381. //BeginExport(header)
  382. //DOC This function is defined in validate.c
  383. //DOC Only the stub is here.
  384. DWORD
  385. DhcpDsValidateService( // check to validate for dhcp
  386. IN LPWSTR Domain,
  387. IN DWORD *Addresses OPTIONAL,
  388. IN ULONG nAddresses,
  389. IN LPWSTR UserName,
  390. IN LPWSTR Password,
  391. IN DWORD AuthFlags,
  392. OUT LPBOOL Found,
  393. OUT LPBOOL IsStandAlone
  394. );
  395. //EndExport(header)
  396. //================================================================================
  397. // end of file
  398. //================================================================================