/vmdns/server/vmdns/rpc.c

https://github.com/vmware/lightwave · C · 463 lines · 373 code · 62 blank · 28 comment · 31 complexity · 3b4f021e317bfef850258dfc1e17390f MD5 · raw file

  1. /*
  2. * Copyright © 2012-2015 VMware, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the “License”); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an “AS IS” BASIS, without
  10. * warranties or conditions of any kind, EITHER EXPRESS OR IMPLIED. See the
  11. * License for the specific language governing permissions and limitations
  12. * under the License.
  13. */
  14. /*
  15. * Module : service.c
  16. *
  17. * Abstract :
  18. *
  19. * VMware dns Service
  20. *
  21. * Service
  22. *
  23. * RPC common functions
  24. *
  25. */
  26. #include "includes.h"
  27. static
  28. PVOID
  29. VmDnsListenRpcServer(
  30. PVOID pInfo
  31. );
  32. static
  33. BOOLEAN
  34. VmDnsRpcCheckServerIsActive(
  35. VOID
  36. );
  37. static
  38. VOID
  39. VmDnsRpcIfCallbackFn(
  40. rpc_if_handle_t InterfaceUuid,
  41. PVOID Context,
  42. unsigned32* status
  43. );
  44. DWORD
  45. VmDnsRpcServerStartListen(
  46. VOID
  47. )
  48. {
  49. DWORD dwError = 0;
  50. int status = 0;
  51. status = dcethread_create(
  52. &gVmdnsGlobals.pRPCServerThread,
  53. NULL,
  54. VmDnsListenRpcServer,
  55. NULL);
  56. #ifndef _WIN32
  57. dwError = LwErrnoToWin32Error(status);
  58. #else
  59. dwError = status;
  60. #endif
  61. BAIL_ON_VMDNS_ERROR(dwError);
  62. while (!VmDnsRpcCheckServerIsActive())
  63. {
  64. // Wait for RPC Server to come up.
  65. }
  66. error:
  67. return dwError;
  68. }
  69. DWORD
  70. VmDnsRpcServerStopListen(
  71. VOID
  72. )
  73. {
  74. DWORD dwError = 0;
  75. DCETHREAD_TRY
  76. {
  77. rpc_mgmt_stop_server_listening(NULL, (unsigned32*)&dwError);
  78. }
  79. DCETHREAD_CATCH_ALL(THIS_CATCH)
  80. {
  81. if (!dwError)
  82. {
  83. dwError = dcethread_exc_getstatus(THIS_CATCH);
  84. }
  85. if(!dwError)
  86. {
  87. dwError = RPC_S_INTERNAL_ERROR;
  88. }
  89. }
  90. DCETHREAD_ENDTRY;
  91. BAIL_ON_VMDNS_ERROR(dwError);
  92. error:
  93. return dwError;
  94. }
  95. DWORD
  96. VmDnsRpcServerRegisterIf(
  97. rpc_if_handle_t pInterfaceSpec
  98. )
  99. {
  100. DWORD dwError = 0;
  101. DCETHREAD_TRY
  102. {
  103. rpc_server_register_if_ex(
  104. pInterfaceSpec,
  105. NULL,
  106. NULL,
  107. rpc_if_allow_secure_only,
  108. rpc_c_listen_max_calls_default,
  109. VmDnsRpcIfCallbackFn,
  110. &dwError
  111. );
  112. }
  113. DCETHREAD_CATCH_ALL(THIS_CATCH)
  114. {
  115. if ( dwError == rpc_s_ok )
  116. {
  117. dwError = dcethread_exc_getstatus(THIS_CATCH);
  118. if (!dwError)
  119. {
  120. dwError = RPC_S_INTERNAL_ERROR;
  121. }
  122. }
  123. }
  124. DCETHREAD_ENDTRY;
  125. return dwError;
  126. }
  127. DWORD
  128. VmDnsRpcServerUseProtSeq(
  129. PCSTR pszProtSeq
  130. )
  131. {
  132. DWORD dwError = 0;
  133. DCETHREAD_TRY
  134. {
  135. rpc_server_use_protseq(
  136. (unsigned char*) pszProtSeq,
  137. rpc_c_protseq_max_calls_default,
  138. (unsigned32*)&dwError);
  139. }
  140. DCETHREAD_CATCH_ALL(THIS_CATCH)
  141. {
  142. if ( dwError == rpc_s_ok )
  143. {
  144. dwError = dcethread_exc_getstatus(THIS_CATCH);
  145. if (!dwError)
  146. {
  147. dwError = RPC_S_INTERNAL_ERROR;
  148. }
  149. }
  150. }
  151. DCETHREAD_ENDTRY;
  152. BAIL_ON_VMDNS_ERROR(dwError);
  153. error:
  154. return dwError;
  155. }
  156. DWORD
  157. VmDnsRpcServerUseProtSeqEp(
  158. PCSTR pszProtSeq,
  159. PCSTR pszEndpoint
  160. )
  161. {
  162. DWORD dwError = 0;
  163. DCETHREAD_TRY
  164. {
  165. rpc_server_use_protseq_ep(
  166. (unsigned char*) pszProtSeq,
  167. rpc_c_protseq_max_calls_default,
  168. (unsigned char*) pszEndpoint,
  169. (unsigned32*)&dwError);
  170. }
  171. DCETHREAD_CATCH_ALL(THIS_CATCH)
  172. {
  173. if ( dwError == rpc_s_ok )
  174. {
  175. dwError = dcethread_exc_getstatus(THIS_CATCH);
  176. if (!dwError)
  177. {
  178. dwError = RPC_S_INTERNAL_ERROR;
  179. }
  180. }
  181. }
  182. DCETHREAD_ENDTRY;
  183. BAIL_ON_VMDNS_ERROR(dwError);
  184. error:
  185. return dwError;
  186. }
  187. DWORD
  188. VmDnsRpcServerInqBindings(
  189. rpc_binding_vector_p_t* ppServerBindings
  190. )
  191. {
  192. DWORD dwError = 0;
  193. DCETHREAD_TRY
  194. {
  195. rpc_server_inq_bindings(ppServerBindings, (unsigned32*)&dwError);
  196. }
  197. DCETHREAD_CATCH_ALL(THIS_CATCH)
  198. {
  199. if ( dwError == rpc_s_ok )
  200. {
  201. dwError = dcethread_exc_getstatus(THIS_CATCH);
  202. if (!dwError)
  203. {
  204. dwError = RPC_S_INTERNAL_ERROR;
  205. }
  206. }
  207. }
  208. DCETHREAD_ENDTRY;
  209. BAIL_ON_VMDNS_ERROR(dwError);
  210. error:
  211. return dwError;
  212. }
  213. DWORD
  214. VmDnsRpcEpRegister(
  215. rpc_binding_vector_p_t pServerBinding,
  216. rpc_if_handle_t pInterfaceSpec,
  217. PCSTR pszAnnotation
  218. )
  219. {
  220. DWORD dwError = 0;
  221. #if 1
  222. /* Do not register with dcerpc; all services use fixed endpoints */
  223. return dwError;
  224. #else
  225. DCETHREAD_TRY
  226. {
  227. rpc_ep_register(
  228. pInterfaceSpec,
  229. pServerBinding,
  230. NULL,
  231. (idl_char*)pszAnnotation,
  232. (unsigned32*)&dwError);
  233. }
  234. DCETHREAD_CATCH_ALL(THIS_CATCH)
  235. {
  236. if (dwError == rpc_s_ok )
  237. {
  238. dwError = dcethread_exc_getstatus(THIS_CATCH);
  239. if (!dwError)
  240. {
  241. dwError = RPC_S_INTERNAL_ERROR;
  242. }
  243. }
  244. }
  245. DCETHREAD_ENDTRY;
  246. return dwError;
  247. #endif
  248. }
  249. DWORD
  250. VmDnsRpcServerRegisterAuthInfo(
  251. VOID
  252. )
  253. {
  254. DWORD dwError = 0;
  255. DCETHREAD_TRY
  256. {
  257. rpc_server_register_auth_info (
  258. NULL, // Server principal name
  259. rpc_c_authn_gss_negotiate, // Authentication service
  260. NULL, // Use default key function
  261. NULL,
  262. &dwError);
  263. }
  264. DCETHREAD_CATCH_ALL(THIS_CATCH)
  265. {
  266. if (dwError == rpc_s_ok )
  267. {
  268. dwError = dcethread_exc_getstatus(THIS_CATCH);
  269. if (!dwError)
  270. {
  271. dwError = RPC_S_INTERNAL_ERROR;
  272. }
  273. }
  274. }
  275. DCETHREAD_ENDTRY;
  276. return dwError;
  277. }
  278. DWORD
  279. VmDnsRpcBindingInqAuthClient(
  280. rpc_binding_handle_t hClientBinding,
  281. rpc_authz_handle_t* pPrivs,
  282. PSTR* ppServerPrincName,
  283. DWORD* pAuthnLevel,
  284. DWORD* pAuthnSvc,
  285. DWORD* pAuthzSvc
  286. )
  287. {
  288. error_status_t rpcStatus = rpc_s_ok;
  289. rpc_binding_inq_auth_client(
  290. hClientBinding,
  291. pPrivs, // The data referenced by this parameter is read-only,
  292. // and therefore should not be modified/freed.
  293. (unsigned_char_p_t*)ppServerPrincName,
  294. pAuthnLevel,
  295. pAuthnSvc,
  296. pAuthzSvc,
  297. &rpcStatus);
  298. BAIL_ON_VMDNS_ERROR(rpcStatus);
  299. error:
  300. return rpcStatus;
  301. }
  302. DWORD
  303. VmDnsRpcBindingVectorFree(
  304. rpc_binding_vector_p_t* ppServerBindings
  305. )
  306. {
  307. error_status_t rpcStatus = rpc_s_ok;
  308. if ( (ppServerBindings != NULL) && ((*ppServerBindings) != NULL) )
  309. {
  310. rpc_binding_vector_free(ppServerBindings, &rpcStatus);
  311. }
  312. BAIL_ON_VMDNS_ERROR(rpcStatus);
  313. error:
  314. return rpcStatus;
  315. }
  316. static
  317. PVOID
  318. VmDnsListenRpcServer(
  319. PVOID pInfo
  320. )
  321. {
  322. DWORD dwError = 0;
  323. BOOLEAN bHeartbeat = TRUE;
  324. dwError = VmDnsCreateHeartbeatThread();
  325. if (dwError)
  326. {
  327. bHeartbeat = FALSE;
  328. }
  329. BAIL_ON_VMDNS_ERROR(dwError);
  330. DCETHREAD_TRY
  331. {
  332. rpc_server_listen(
  333. rpc_c_listen_max_calls_default, (unsigned32*)&dwError);
  334. }
  335. DCETHREAD_CATCH_ALL(THIS_CATCH)
  336. {
  337. if (!dwError)
  338. {
  339. dwError = dcethread_exc_getstatus(THIS_CATCH);
  340. }
  341. if(!dwError)
  342. {
  343. dwError = RPC_S_INTERNAL_ERROR;
  344. }
  345. }
  346. DCETHREAD_ENDTRY;
  347. BAIL_ON_VMDNS_ERROR(dwError);
  348. cleanup:
  349. if (bHeartbeat)
  350. {
  351. VmDnsKillHeartbeatThread();
  352. }
  353. #ifndef _WIN32
  354. raise(SIGTERM); // indicate that process must terminate
  355. #endif
  356. return NULL;
  357. error:
  358. goto cleanup;
  359. }
  360. static
  361. BOOLEAN
  362. VmDnsRpcCheckServerIsActive(
  363. VOID
  364. )
  365. {
  366. DWORD dwError = 0;
  367. BOOLEAN bIsActive = FALSE;
  368. DCETHREAD_TRY
  369. {
  370. bIsActive = rpc_mgmt_is_server_listening(NULL, (unsigned32*)&dwError);
  371. }
  372. DCETHREAD_CATCH_ALL(THIS_CATCH)
  373. {
  374. if (!dwError)
  375. {
  376. dwError = dcethread_exc_getstatus(THIS_CATCH);
  377. }
  378. if (!dwError)
  379. {
  380. dwError = RPC_S_INTERNAL_ERROR;
  381. }
  382. }
  383. DCETHREAD_ENDTRY;
  384. BAIL_ON_VMDNS_ERROR(dwError);
  385. cleanup:
  386. return bIsActive;
  387. error:
  388. bIsActive = FALSE;
  389. goto cleanup;
  390. }
  391. static
  392. VOID
  393. VmDnsRpcIfCallbackFn(
  394. rpc_if_handle_t InterfaceUuid,
  395. PVOID Context,
  396. unsigned32* status
  397. )
  398. {
  399. unsigned32 sts = 0;
  400. *status = sts;
  401. }