]> git.proxmox.com Git - mirror_edk2.git/blame - RedfishPkg/PrivateLibrary/RedfishLib/RedfishMisc.c
RedfishPkg: Apply uncrustify changes
[mirror_edk2.git] / RedfishPkg / PrivateLibrary / RedfishLib / RedfishMisc.c
CommitLineData
4751a48a
AC
1/** @file\r
2 Internal Functions for RedfishLib.\r
3\r
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
5 (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>\r
6\r
7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
8\r
9**/\r
10\r
11#include "RedfishMisc.h"\r
12\r
39de741e 13EDKII_REDFISH_CREDENTIAL_PROTOCOL *mCredentialProtocol = NULL;\r
4751a48a
AC
14\r
15/**\r
16 This function returns the string of Redfish service version.\r
17\r
18 @param[in] RedfishService Redfish service instance.\r
19 @param[out] ServiceVersionStr Redfish service string.\r
20\r
21 @return EFI_STATUS\r
22\r
23**/\r
24EFI_STATUS\r
25RedfishGetServiceVersion (\r
39de741e
MK
26 IN REDFISH_SERVICE RedfishService,\r
27 OUT CHAR8 **ServiceVersionStr\r
4751a48a
AC
28 )\r
29{\r
39de741e
MK
30 redfishService *Redfish;\r
31 CHAR8 **KeysArray;\r
32 UINTN KeysNum;\r
4751a48a 33\r
39de741e 34 if ((RedfishService == NULL) || (ServiceVersionStr == NULL)) {\r
4751a48a
AC
35 return EFI_INVALID_PARAMETER;\r
36 }\r
39de741e 37\r
4751a48a
AC
38 Redfish = (redfishService *)RedfishService;\r
39 if (Redfish->versions == NULL) {\r
40 return EFI_INVALID_PARAMETER;\r
41 }\r
39de741e 42\r
4751a48a 43 KeysArray = JsonObjectGetKeys (Redfish->versions, &KeysNum);\r
39de741e 44 if ((KeysNum == 0) || (KeysArray == NULL)) {\r
4751a48a
AC
45 return EFI_NOT_FOUND;\r
46 }\r
39de741e 47\r
4751a48a
AC
48 *ServiceVersionStr = *KeysArray;\r
49 return EFI_SUCCESS;\r
50}\r
51\r
52/**\r
53 Creates a REDFISH_SERVICE which can be later used to access the Redfish resources.\r
54\r
55 This function will configure REST EX child according to parameters described in\r
56 Redfish network host interface in SMBIOS type 42 record. The service enumerator will also\r
57 handle the authentication flow automatically if HTTP basic auth or Redfish session\r
58 login is configured to use.\r
59\r
60 @param[in] RedfishConfigServiceInfo Redfish service information the EFI Redfish\r
61 feature driver communicates with.\r
62 @param[in] AuthMethod None, HTTP basic auth, or Redfish session login.\r
63 @param[in] UserId User Name used for authentication.\r
64 @param[in] Password Password used for authentication.\r
65\r
66 @return New created Redfish service, or NULL if error happens.\r
67\r
68**/\r
69REDFISH_SERVICE\r
70RedfishCreateLibredfishService (\r
39de741e
MK
71 IN REDFISH_CONFIG_SERVICE_INFORMATION *RedfishConfigServiceInfo,\r
72 IN EDKII_REDFISH_AUTH_METHOD AuthMethod,\r
73 IN CHAR8 *UserId,\r
74 IN CHAR8 *Password\r
4751a48a
AC
75 )\r
76{\r
39de741e
MK
77 UINTN Flags;\r
78 enumeratorAuthentication Auth;\r
79 redfishService *Redfish;\r
4751a48a 80\r
39de741e 81 Redfish = NULL;\r
4751a48a
AC
82\r
83 ZeroMem (&Auth, sizeof (Auth));\r
84 if (AuthMethod == AuthMethodHttpBasic) {\r
85 Auth.authType = REDFISH_AUTH_BASIC;\r
86 } else if (AuthMethod == AuthMethodRedfishSession) {\r
87 Auth.authType = REDFISH_AUTH_SESSION;\r
88 }\r
39de741e 89\r
4751a48a
AC
90 Auth.authCodes.userPass.username = UserId;\r
91 Auth.authCodes.userPass.password = Password;\r
92\r
93 Flags = REDFISH_FLAG_SERVICE_NO_VERSION_DOC;\r
94\r
95 if (AuthMethod != AuthMethodNone) {\r
39de741e 96 Redfish = createServiceEnumerator (RedfishConfigServiceInfo, NULL, &Auth, (unsigned int)Flags);\r
4751a48a 97 } else {\r
39de741e 98 Redfish = createServiceEnumerator (RedfishConfigServiceInfo, NULL, NULL, (unsigned int)Flags);\r
4751a48a
AC
99 }\r
100\r
101 //\r
102 // Zero the Password after use.\r
103 //\r
104 if (Password != NULL) {\r
39de741e 105 ZeroMem (Password, AsciiStrLen (Password));\r
4751a48a
AC
106 }\r
107\r
39de741e 108 return (REDFISH_SERVICE)Redfish;\r
4751a48a
AC
109}\r
110\r
111/**\r
112 Retrieve platform's Redfish authentication information.\r
113\r
114 This functions returns the Redfish authentication method together with the user\r
115 Id and password.\r
116 For AuthMethodNone, UserId and Password will point to NULL which means authentication\r
117 is not required to access the Redfish service.\r
118 For AuthMethodHttpBasic, the UserId and Password could be used for\r
119 HTTP header authentication as defined by RFC7235. For AuthMethodRedfishSession,\r
120 the UserId and Password could be used for Redfish session login as defined by\r
121 Redfish API specification (DSP0266).\r
122\r
123 Callers are responsible for freeing the returned string storage pointed by UserId\r
124 and Password.\r
125\r
126 @param[out] AuthMethod Type of Redfish authentication method.\r
127 @param[out] UserId The pointer to store the returned UserId string.\r
128 @param[out] Password The pointer to store the returned Password string.\r
129\r
130 @retval EFI_SUCCESS Get the authentication information successfully.\r
131 @retval EFI_INVALID_PARAMETER AuthMethod or UserId or Password is NULL.\r
132 @retval EFI_UNSUPPORTED Unsupported authentication method is found.\r
133**/\r
134EFI_STATUS\r
135RedfishGetAuthInfo (\r
39de741e
MK
136 OUT EDKII_REDFISH_AUTH_METHOD *AuthMethod,\r
137 OUT CHAR8 **UserId,\r
138 OUT CHAR8 **Password\r
4751a48a
AC
139 )\r
140{\r
39de741e 141 EFI_STATUS Status;\r
4751a48a 142\r
39de741e 143 if ((AuthMethod == NULL) || (UserId == NULL) || (Password == NULL)) {\r
4751a48a
AC
144 return EFI_INVALID_PARAMETER;\r
145 }\r
146\r
147 //\r
148 // Locate Redfish Credential Protocol.\r
149 //\r
150 if (mCredentialProtocol == NULL) {\r
151 Status = gBS->LocateProtocol (&gEdkIIRedfishCredentialProtocolGuid, NULL, (VOID **)&mCredentialProtocol);\r
152 if (EFI_ERROR (Status)) {\r
153 return EFI_UNSUPPORTED;\r
154 }\r
155 }\r
156\r
157 ASSERT (mCredentialProtocol != NULL);\r
158\r
159 Status = mCredentialProtocol->GetAuthInfo (mCredentialProtocol, AuthMethod, UserId, Password);\r
160 if (EFI_ERROR (Status)) {\r
161 DEBUG ((DEBUG_ERROR, "RedfishGetAuthInfo: failed to retrieve Redfish credential - %r\n", Status));\r
162 return Status;\r
163 }\r
164\r
165 return Status;\r
166}\r
39de741e 167\r
4751a48a
AC
168/**\r
169 This function returns the string of Redfish service version.\r
170\r
171 @param[in] ServiceVerisonStr The string of Redfish service version.\r
172 @param[in] Url The URL to build Redpath with ID.\r
173 Start with "/", for example "/Registries"\r
174 @param[in] Id ID string\r
175 @param[out] Redpath Pointer to retrive Redpath, caller has to free\r
176 the memory allocated for this string.\r
177 @return EFI_STATUS\r
178\r
179**/\r
180EFI_STATUS\r
181RedfishBuildRedpathUseId (\r
39de741e
MK
182 IN CHAR8 *ServiceVerisonStr,\r
183 IN CHAR8 *Url,\r
184 IN CHAR8 *Id,\r
185 OUT CHAR8 **Redpath\r
4751a48a
AC
186 )\r
187{\r
39de741e 188 UINTN RedpathSize;\r
4751a48a 189\r
39de741e 190 if ((Redpath == NULL) || (ServiceVerisonStr == NULL) || (Url == NULL) || (Id == NULL)) {\r
4751a48a
AC
191 return EFI_INVALID_PARAMETER;\r
192 }\r
193\r
194 RedpathSize = AsciiStrLen ("/") +\r
195 AsciiStrLen (ServiceVerisonStr) +\r
196 AsciiStrLen (Url) +\r
197 AsciiStrLen ("[Id=]") +\r
198 AsciiStrLen (Id) + 1;\r
39de741e 199 *Redpath = AllocatePool (RedpathSize);\r
4751a48a
AC
200 if (*Redpath == NULL) {\r
201 return EFI_OUT_OF_RESOURCES;\r
202 }\r
39de741e 203\r
4751a48a
AC
204 AsciiSPrint (*Redpath, RedpathSize, "/%a%a[Id=%a]", ServiceVerisonStr, Url, Id);\r
205 return EFI_SUCCESS;\r
206}\r