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