/lwraft/server/rest-head/utils.c

https://github.com/vmware/lightwave · C · 186 lines · 147 code · 23 blank · 16 comment · 17 complexity · a8709e3a91aa5c2545b7eb5be3616368 MD5 · raw file

  1. /*
  2. * Copyright ©2018 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. #include "includes.h"
  15. static
  16. DWORD
  17. VmDirRESTTestIfHostIPMatch(
  18. PSTR pszOrigin,
  19. BOOLEAN *pbMatch
  20. );
  21. DWORD
  22. VmDirRESTSetCORSHeaders(
  23. PVDIR_REST_OPERATION pRestOp,
  24. PREST_RESPONSE* ppResponse
  25. )
  26. {
  27. DWORD dwError = 0;
  28. if (!pRestOp || !ppResponse)
  29. {
  30. dwError = VMDIR_ERROR_INVALID_PARAMETER;
  31. BAIL_ON_VMDIR_ERROR(dwError);
  32. }
  33. //determine if Origin header is present & set CORS headers accordingly
  34. if (pRestOp->bisValidOrigin)
  35. {
  36. dwError = VmRESTSetHttpHeader (ppResponse,
  37. "Access-Control-Allow-Origin", pRestOp->pszOrigin);
  38. BAIL_ON_VMDIR_ERROR(dwError);
  39. dwError = VmRESTSetHttpHeader (ppResponse,
  40. "Access-Control-Allow-Headers",
  41. "Origin, X-Requested-With, Content-Type, Authorization");
  42. BAIL_ON_VMDIR_ERROR(dwError);
  43. dwError = VmRESTSetHttpHeader (ppResponse,
  44. "Access-Control-Allow-Methods",
  45. "GET, OPTIONS, PUT, DELETE, PATCH");
  46. BAIL_ON_VMDIR_ERROR(dwError);
  47. }
  48. cleanup:
  49. return dwError;
  50. error:
  51. VMDIR_LOG_ERROR(
  52. VMDIR_LOG_MASK_ALL,
  53. "%s failed, error (%d)",
  54. __FUNCTION__,
  55. dwError);
  56. goto cleanup;
  57. }
  58. DWORD
  59. VmDirRESTIsValidOrigin(
  60. PSTR pszOrigin,
  61. BOOLEAN *pbIsValidOrigin
  62. )
  63. {
  64. DWORD dwError = 0;
  65. BOOLEAN bIsValidOrigin = FALSE;
  66. PSTR pszDomainName = NULL;
  67. if (IsNullOrEmptyString(pszOrigin) || !pbIsValidOrigin)
  68. {
  69. dwError = VMDIR_ERROR_INVALID_PARAMETER;
  70. BAIL_ON_VMDIR_ERROR(dwError);
  71. }
  72. if (VmDirStringStartsWith (pszOrigin, HTTP_PROTOCOL_PREFIX, FALSE))
  73. {
  74. // get the part of origin after "https://"
  75. PSTR pszOriginValue = pszOrigin + strlen(HTTP_PROTOCOL_PREFIX);
  76. if (VmDirIsIPAddrFormat(pszOriginValue))
  77. {
  78. dwError = VmDirRESTTestIfHostIPMatch(pszOrigin, &bIsValidOrigin);
  79. BAIL_ON_VMDIR_ERROR(dwError);
  80. }
  81. else
  82. {
  83. dwError = VmDirRESTGetDomainName(&pszDomainName);
  84. BAIL_ON_VMDIR_ERROR(dwError);
  85. if (VmDirStringEndsWith(
  86. pszOriginValue,
  87. pszDomainName,
  88. FALSE /* case insensitive */
  89. ))
  90. {
  91. bIsValidOrigin = TRUE;
  92. }
  93. }
  94. }
  95. *pbIsValidOrigin = bIsValidOrigin;
  96. cleanup:
  97. VMDIR_SAFE_FREE_MEMORY(pszDomainName);
  98. return dwError;
  99. error:
  100. VMDIR_LOG_ERROR(
  101. VMDIR_LOG_MASK_ALL,
  102. "%s failed, error (%d)",
  103. __FUNCTION__,
  104. dwError);
  105. goto cleanup;
  106. }
  107. static
  108. DWORD
  109. VmDirRESTTestIfHostIPMatch(
  110. PSTR pszOrigin,
  111. BOOLEAN *pbMatch
  112. )
  113. {
  114. DWORD dwError = 0;
  115. BOOLEAN bMatch = FALSE;
  116. char pszAddr[INET_ADDRSTRLEN];
  117. struct ifaddrs *addrs = NULL, *pCur = NULL;
  118. struct sockaddr_in *pAddr = NULL;
  119. if (IsNullOrEmptyString(pszOrigin) || !pbMatch)
  120. {
  121. dwError = VMDIR_ERROR_INVALID_PARAMETER;
  122. BAIL_ON_VMDIR_ERROR(dwError);
  123. }
  124. // Compare with current IP addrss
  125. if (getifaddrs(&addrs) < 0)
  126. {
  127. dwError = VMDIR_ERROR_REST_IP_UNKNOWN;
  128. BAIL_ON_VMDIR_ERROR(dwError);
  129. }
  130. pCur = addrs;
  131. while (pCur)
  132. {
  133. if (pCur->ifa_addr != NULL)
  134. {
  135. pAddr = (struct sockaddr_in *)pCur->ifa_addr;
  136. if (!inet_ntop(AF_INET, &(pAddr->sin_addr), pszAddr, INET_ADDRSTRLEN))
  137. {
  138. dwError = VMDIR_ERROR_REST_IP_UNKNOWN;
  139. BAIL_ON_VMDIR_ERROR(dwError);
  140. }
  141. if (!strncasecmp(pszOrigin + strlen(HTTP_PROTOCOL_PREFIX), pszAddr, strlen(pszAddr)))
  142. {
  143. bMatch = TRUE;
  144. break;
  145. }
  146. }
  147. pCur = pCur->ifa_next;
  148. }
  149. *pbMatch = bMatch;
  150. cleanup:
  151. if (addrs)
  152. {
  153. freeifaddrs(addrs);
  154. }
  155. return dwError;
  156. error:
  157. VMDIR_LOG_ERROR(
  158. VMDIR_LOG_MASK_ALL,
  159. "%s failed, error (%d)",
  160. __FUNCTION__,
  161. dwError);
  162. goto cleanup;
  163. }