Update UID drivers to align with latest UEFI spec 2.3.1.
[mirror_edk2.git] / SecurityPkg / UserIdentification / UserProfileManagerDxe / UserProfileAdd.c
1 /** @file
2 The functions to add a user profile.
3
4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "UserProfileManager.h"
16
17
18 /**
19 Get user name from the popup windows.
20
21 @param[in, out] UserNameLen On entry, point to UserName buffer lengh, in bytes.
22 On exit, point to input user name length, in bytes.
23 @param[out] UserName The buffer to hold the input user name.
24
25 @retval EFI_ABORTED It is given up by pressing 'ESC' key.
26 @retval EFI_NOT_READY Not a valid input at all.
27 @retval EFI_SUCCESS Get a user name successfully.
28
29 **/
30 EFI_STATUS
31 GetUserNameInput (
32 IN OUT UINTN *UserNameLen,
33 OUT CHAR16 *UserName
34 )
35 {
36 EFI_INPUT_KEY Key;
37 UINTN NameLen;
38 CHAR16 Name[USER_NAME_LENGTH];
39
40 NameLen = 0;
41 while (TRUE) {
42 Name[NameLen] = L'_';
43 Name[NameLen + 1] = L'\0';
44 CreatePopUp (
45 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
46 &Key,
47 L"Input User Name",
48 L"---------------------",
49 Name,
50 NULL
51 );
52 //
53 // Check key.
54 //
55 if (Key.ScanCode == SCAN_NULL) {
56 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
57 //
58 // Add the null terminator.
59 //
60 Name[NameLen] = 0;
61 NameLen++;
62 break;
63 } else if ((Key.UnicodeChar == CHAR_NULL) ||
64 (Key.UnicodeChar == CHAR_TAB) ||
65 (Key.UnicodeChar == CHAR_LINEFEED)
66 ) {
67 continue;
68 } else {
69 if (Key.UnicodeChar == CHAR_BACKSPACE) {
70 if (NameLen > 0) {
71 NameLen--;
72 }
73 } else {
74 Name[NameLen] = Key.UnicodeChar;
75 NameLen++;
76 if (NameLen + 1 == USER_NAME_LENGTH) {
77 //
78 // Add the null terminator.
79 //
80 Name[NameLen] = 0;
81 NameLen++;
82 break;
83 }
84 }
85 }
86 }
87
88 if (Key.ScanCode == SCAN_ESC) {
89 return EFI_ABORTED;
90 }
91 }
92
93 if (NameLen <= 1) {
94 return EFI_NOT_READY;
95 }
96
97 if (*UserNameLen < NameLen * sizeof (CHAR16)) {
98 return EFI_NOT_READY;
99 }
100
101 *UserNameLen = NameLen * sizeof (CHAR16);
102 CopyMem (UserName, Name, *UserNameLen);
103
104 return EFI_SUCCESS;
105 }
106
107 /**
108 Set a user's username.
109
110 @param[in] User Handle of a user profile .
111 @param[in] UserNameLen The lengh of UserName.
112 @param[in] UserName Point to the buffer of user name.
113
114 @retval EFI_NOT_READY The usernme in mAddUserName had been used.
115 @retval EFI_SUCCESS Change the user's username successfully with
116 username in mAddUserName.
117
118 **/
119 EFI_STATUS
120 SetUserName (
121 IN EFI_USER_PROFILE_HANDLE User,
122 IN UINTN UserNameLen,
123 IN CHAR16 *UserName
124 )
125 {
126 EFI_STATUS Status;
127 EFI_USER_INFO_HANDLE UserInfo;
128 EFI_USER_PROFILE_HANDLE TempUser;
129 EFI_USER_INFO *NewUserInfo;
130
131 NewUserInfo = AllocateZeroPool (sizeof (EFI_USER_INFO) + UserNameLen);
132 ASSERT (NewUserInfo != NULL);
133
134 NewUserInfo->InfoType = EFI_USER_INFO_NAME_RECORD;
135 NewUserInfo->InfoAttribs = EFI_USER_INFO_STORAGE_PLATFORM_NV |
136 EFI_USER_INFO_PUBLIC |
137 EFI_USER_INFO_EXCLUSIVE;
138 NewUserInfo->InfoSize = (UINT32) (sizeof (EFI_USER_INFO) + UserNameLen);
139 CopyMem ((UINT8 *) (NewUserInfo + 1), UserName, UserNameLen);
140 TempUser = NULL;
141 Status = mUserManager->Find (
142 mUserManager,
143 &TempUser,
144 NULL,
145 NewUserInfo,
146 NewUserInfo->InfoSize
147 );
148 if (!EFI_ERROR (Status)) {
149 //
150 // The user name had been used, return error.
151 //
152 FreePool (NewUserInfo);
153 return EFI_NOT_READY;
154 }
155
156 UserInfo = NULL;
157 mUserManager->SetInfo (
158 mUserManager,
159 User,
160 &UserInfo,
161 NewUserInfo,
162 NewUserInfo->InfoSize
163 );
164 FreePool (NewUserInfo);
165 return EFI_SUCCESS;
166 }
167
168
169 /**
170 Set create date of the specified user.
171
172 @param[in] User Handle of a user profile.
173
174 **/
175 VOID
176 SetCreateDate (
177 IN EFI_USER_PROFILE_HANDLE User
178 )
179 {
180 EFI_STATUS Status;
181 EFI_USER_INFO_HANDLE UserInfo;
182 EFI_USER_INFO_CREATE_DATE Date;
183 EFI_USER_INFO *NewUserInfo;
184
185 NewUserInfo = AllocateZeroPool (
186 sizeof (EFI_USER_INFO) +
187 sizeof (EFI_USER_INFO_CREATE_DATE)
188 );
189 ASSERT (NewUserInfo != NULL);
190
191 NewUserInfo->InfoType = EFI_USER_INFO_CREATE_DATE_RECORD;
192 NewUserInfo->InfoAttribs = EFI_USER_INFO_STORAGE_PLATFORM_NV |
193 EFI_USER_INFO_PUBLIC |
194 EFI_USER_INFO_EXCLUSIVE;
195 NewUserInfo->InfoSize = sizeof (EFI_USER_INFO) + sizeof (EFI_USER_INFO_CREATE_DATE);
196 Status = gRT->GetTime (&Date, NULL);
197 if (EFI_ERROR (Status)) {
198 FreePool (NewUserInfo);
199 return ;
200 }
201
202 CopyMem ((UINT8 *) (NewUserInfo + 1), &Date, sizeof (EFI_USER_INFO_CREATE_DATE));
203 UserInfo = NULL;
204 mUserManager->SetInfo (
205 mUserManager,
206 User,
207 &UserInfo,
208 NewUserInfo,
209 NewUserInfo->InfoSize
210 );
211 FreePool (NewUserInfo);
212 }
213
214
215 /**
216 Set the default identity policy of the specified user.
217
218 @param[in] User Handle of a user profile.
219
220 **/
221 VOID
222 SetIdentityPolicy (
223 IN EFI_USER_PROFILE_HANDLE User
224 )
225 {
226 EFI_USER_INFO_IDENTITY_POLICY *Policy;
227 EFI_USER_INFO_HANDLE UserInfo;
228 EFI_USER_INFO *NewUserInfo;
229
230 NewUserInfo = AllocateZeroPool (
231 sizeof (EFI_USER_INFO) +
232 sizeof (EFI_USER_INFO_IDENTITY_POLICY)
233 );
234 ASSERT (NewUserInfo != NULL);
235
236 Policy = (EFI_USER_INFO_IDENTITY_POLICY *) (NewUserInfo + 1);
237 Policy->Type = EFI_USER_INFO_IDENTITY_TRUE;
238 Policy->Length = sizeof (EFI_USER_INFO_IDENTITY_POLICY);
239
240 NewUserInfo->InfoType = EFI_USER_INFO_IDENTITY_POLICY_RECORD;
241 NewUserInfo->InfoAttribs = EFI_USER_INFO_STORAGE_PLATFORM_NV |
242 EFI_USER_INFO_PUBLIC |
243 EFI_USER_INFO_EXCLUSIVE;
244 NewUserInfo->InfoSize = sizeof (EFI_USER_INFO) + Policy->Length;
245 UserInfo = NULL;
246 mUserManager->SetInfo (
247 mUserManager,
248 User,
249 &UserInfo,
250 NewUserInfo,
251 NewUserInfo->InfoSize
252 );
253 FreePool (NewUserInfo);
254 }
255
256
257 /**
258 Set the default access policy of the specified user.
259
260 @param[in] User Handle of a user profile.
261
262 **/
263 VOID
264 SetAccessPolicy (
265 IN EFI_USER_PROFILE_HANDLE User
266 )
267 {
268 EFI_USER_INFO_ACCESS_CONTROL *Control;
269 EFI_USER_INFO_HANDLE UserInfo;
270 EFI_USER_INFO *NewUserInfo;
271
272 NewUserInfo = AllocateZeroPool (
273 sizeof (EFI_USER_INFO) +
274 sizeof (EFI_USER_INFO_ACCESS_CONTROL)
275 );
276 ASSERT (NewUserInfo != NULL);
277
278 Control = (EFI_USER_INFO_ACCESS_CONTROL *) (NewUserInfo + 1);
279 Control->Type = EFI_USER_INFO_ACCESS_ENROLL_SELF;
280 Control->Size = sizeof (EFI_USER_INFO_ACCESS_CONTROL);
281
282 NewUserInfo->InfoType = EFI_USER_INFO_ACCESS_POLICY_RECORD;
283 NewUserInfo->InfoAttribs = EFI_USER_INFO_STORAGE_PLATFORM_NV |
284 EFI_USER_INFO_PUBLIC |
285 EFI_USER_INFO_EXCLUSIVE;
286 NewUserInfo->InfoSize = sizeof (EFI_USER_INFO) + Control->Size;
287 UserInfo = NULL;
288 mUserManager->SetInfo (
289 mUserManager,
290 User,
291 &UserInfo,
292 NewUserInfo,
293 NewUserInfo->InfoSize
294 );
295 FreePool (NewUserInfo);
296 }
297
298
299 /**
300 Add a new user profile into the user profile database.
301
302 **/
303 VOID
304 CallAddUser (
305 VOID
306 )
307 {
308 EFI_STATUS Status;
309 EFI_INPUT_KEY Key;
310 EFI_USER_PROFILE_HANDLE User;
311 UINTN UserNameLen;
312 CHAR16 UserName[USER_NAME_LENGTH];
313 CHAR16 *QuestionStr;
314 CHAR16 *PromptStr;
315
316 QuestionStr = NULL;
317 PromptStr = NULL;
318
319 //
320 // Get user name to add.
321 //
322 UserNameLen = sizeof (UserName);
323 Status = GetUserNameInput (&UserNameLen, UserName);
324 if (EFI_ERROR (Status)) {
325 if (Status != EFI_ABORTED) {
326 QuestionStr = GetStringById (STRING_TOKEN (STR_GET_USERNAME_FAILED));
327 PromptStr = GetStringById (STRING_TOKEN (STR_STROKE_KEY_CONTINUE));
328 goto Done;
329 }
330 return ;
331 }
332
333 //
334 // Create a new user profile.
335 //
336 User = NULL;
337 Status = mUserManager->Create (mUserManager, &User);
338 if (EFI_ERROR (Status)) {
339 QuestionStr = GetStringById (STRING_TOKEN (STR_CREATE_PROFILE_FAILED));
340 PromptStr = GetStringById (STRING_TOKEN (STR_STROKE_KEY_CONTINUE));
341 } else {
342 //
343 // Add default user information.
344 //
345 Status = SetUserName (User, UserNameLen, UserName);
346 if (EFI_ERROR (Status)) {
347 QuestionStr = GetStringById (STRING_TOKEN (STR_USER_ALREADY_EXISTED));
348 PromptStr = GetStringById (STRING_TOKEN (STR_STROKE_KEY_CONTINUE));
349 goto Done;
350 }
351
352 SetCreateDate (User);
353 SetIdentityPolicy (User);
354 SetAccessPolicy (User);
355
356 QuestionStr = GetStringById (STRING_TOKEN (STR_CREATE_PROFILE_SUCCESS));
357 PromptStr = GetStringById (STRING_TOKEN (STR_STROKE_KEY_CONTINUE));
358 }
359
360 Done:
361 CreatePopUp (
362 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
363 &Key,
364 QuestionStr,
365 L"",
366 PromptStr,
367 NULL
368 );
369 FreePool (QuestionStr);
370 FreePool (PromptStr);
371 }
372