summaryrefslogtreecommitdiff
path: root/SecurityPkg/UserIdentification/UserProfileManagerDxe/UserProfileAdd.c
blob: 11233a1509547dcf06fa0ba75fe0ea9c02098720 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/** @file
  The functions to add a user profile.
    
Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials 
are licensed and made available under the terms and conditions of the BSD License 
which accompanies this distribution.  The full text of the license may be found at 
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "UserProfileManager.h"


/**
  Get user name from the popup windows.
  
  @param[in, out]  UserNameLen  On entry, point to UserName buffer lengh, in bytes.
                                On exit, point to input user name length, in bytes.
  @param[out]      UserName     The buffer to hold the input user name.
 
  @retval EFI_ABORTED           It is given up by pressing 'ESC' key.
  @retval EFI_NOT_READY         Not a valid input at all.
  @retval EFI_SUCCESS           Get a user name successfully.

**/
EFI_STATUS
GetUserNameInput (
  IN OUT  UINTN         *UserNameLen,
     OUT  CHAR16        *UserName
  )
{
  EFI_INPUT_KEY Key;
  UINTN         NameLen;
  CHAR16        Name[USER_NAME_LENGTH];

  NameLen = 0;
  while (TRUE) {
    Name[NameLen]     = L'_';
    Name[NameLen + 1] = L'\0';
    CreatePopUp (
      EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
      &Key,
      L"Input User Name",
      L"---------------------",
      Name,
      NULL
      );
    //
    // Check key.
    //
    if (Key.ScanCode == SCAN_NULL) {
      if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
        //
        // Add the null terminator.
        //
        Name[NameLen] = 0;
        NameLen++;
        break;
      } else if ((Key.UnicodeChar == CHAR_NULL) ||
                 (Key.UnicodeChar == CHAR_TAB) ||
                 (Key.UnicodeChar == CHAR_LINEFEED)
                 ) {
        continue;
      } else {
        if (Key.UnicodeChar == CHAR_BACKSPACE) {
          if (NameLen > 0) {
            NameLen--;
          }
        } else {
          Name[NameLen] = Key.UnicodeChar;
          NameLen++;
          if (NameLen + 1 == USER_NAME_LENGTH) {
            //
            // Add the null terminator.
            //
            Name[NameLen] = 0;
            NameLen++;
            break;
          }
        }
      }
    }

    if (Key.ScanCode == SCAN_ESC) {
      return EFI_ABORTED;
    }
  }

  if (NameLen <= 1) {
    return EFI_NOT_READY;
  }

  if (*UserNameLen < NameLen * sizeof (CHAR16)) {
    return EFI_NOT_READY;
  }

  *UserNameLen = NameLen * sizeof (CHAR16);
  CopyMem (UserName, Name, *UserNameLen);
  
  return EFI_SUCCESS;
}

/**
  Set a user's username.

  @param[in]   User          Handle of a user profile .
  @param[in]   UserNameLen   The lengh of UserName.
  @param[in]   UserName      Point to the buffer of user name.

  @retval EFI_NOT_READY      The usernme in mAddUserName had been used.
  @retval EFI_SUCCESS        Change the user's username successfully with 
                             username in mAddUserName.

**/
EFI_STATUS
SetUserName (
  IN  EFI_USER_PROFILE_HANDLE    User,
  IN  UINTN                      UserNameLen,
  IN  CHAR16                     *UserName
  )
{
  EFI_STATUS              Status;
  EFI_USER_INFO_HANDLE    UserInfo;
  EFI_USER_PROFILE_HANDLE TempUser;
  EFI_USER_INFO           *NewUserInfo;
   
  NewUserInfo = AllocateZeroPool (sizeof (EFI_USER_INFO) + UserNameLen);
  ASSERT (NewUserInfo != NULL);

  NewUserInfo->InfoType    = EFI_USER_INFO_NAME_RECORD;
  NewUserInfo->InfoAttribs = EFI_USER_INFO_STORAGE_PLATFORM_NV | 
                             EFI_USER_INFO_PUBLIC | 
                             EFI_USER_INFO_EXCLUSIVE;
  NewUserInfo->InfoSize    = (UINT32) (sizeof (EFI_USER_INFO) + UserNameLen);
  CopyMem ((UINT8 *) (NewUserInfo + 1), UserName, UserNameLen);
  TempUser  = NULL;
  Status    = mUserManager->Find (
                              mUserManager,
                              &TempUser,
                              NULL,
                              NewUserInfo,
                              NewUserInfo->InfoSize
                              );
  if (!EFI_ERROR (Status)) {
    //
    // The user name had been used, return error.
    //
    FreePool (NewUserInfo);
    return EFI_NOT_READY;
  }

  UserInfo = NULL;
  mUserManager->SetInfo (
                  mUserManager,
                  User,
                  &UserInfo,
                  NewUserInfo,
                  NewUserInfo->InfoSize
                  );
  FreePool (NewUserInfo);
  return EFI_SUCCESS;
}


/**
  Set create date of the specified user.

  @param[in]  User               Handle of a user profile.

**/
VOID
SetCreateDate (
  IN        EFI_USER_PROFILE_HANDLE   User
  )
{
  EFI_STATUS                Status;
  EFI_USER_INFO_HANDLE      UserInfo;
  EFI_USER_INFO_CREATE_DATE Date;
  EFI_USER_INFO             *NewUserInfo;
  
  NewUserInfo = AllocateZeroPool (
                  sizeof (EFI_USER_INFO) +
                  sizeof (EFI_USER_INFO_CREATE_DATE)
                  );
  ASSERT (NewUserInfo != NULL);

  NewUserInfo->InfoType    = EFI_USER_INFO_CREATE_DATE_RECORD;
  NewUserInfo->InfoAttribs = EFI_USER_INFO_STORAGE_PLATFORM_NV | 
                             EFI_USER_INFO_PUBLIC | 
                             EFI_USER_INFO_EXCLUSIVE;
  NewUserInfo->InfoSize    = sizeof (EFI_USER_INFO) + sizeof (EFI_USER_INFO_CREATE_DATE);
  Status                   = gRT->GetTime (&Date, NULL);
  if (EFI_ERROR (Status)) {
    FreePool (NewUserInfo);
    return ;
  }

  CopyMem ((UINT8 *) (NewUserInfo + 1), &Date, sizeof (EFI_USER_INFO_CREATE_DATE));
  UserInfo = NULL;
  mUserManager->SetInfo (
                  mUserManager,
                  User,
                  &UserInfo,
                  NewUserInfo,
                  NewUserInfo->InfoSize
                  );
  FreePool (NewUserInfo);
}


/**
  Set the default identity policy of the specified user.

  @param[in]  User               Handle of a user profile. 

**/
VOID
SetIdentityPolicy (
  IN        EFI_USER_PROFILE_HANDLE   User
  )
{
  EFI_USER_INFO_IDENTITY_POLICY *Policy;
  EFI_USER_INFO_HANDLE          UserInfo;
  EFI_USER_INFO                 *NewUserInfo;
  
  NewUserInfo = AllocateZeroPool (
                  sizeof (EFI_USER_INFO) + 
                  sizeof (EFI_USER_INFO_IDENTITY_POLICY)
                  );
  ASSERT (NewUserInfo != NULL);
  
  Policy                   = (EFI_USER_INFO_IDENTITY_POLICY *) (NewUserInfo + 1);
  Policy->Type             = EFI_USER_INFO_IDENTITY_TRUE;
  Policy->Length           = sizeof (EFI_USER_INFO_IDENTITY_POLICY);

  NewUserInfo->InfoType    = EFI_USER_INFO_IDENTITY_POLICY_RECORD;
  NewUserInfo->InfoAttribs = EFI_USER_INFO_STORAGE_PLATFORM_NV | 
                             EFI_USER_INFO_PUBLIC | 
                             EFI_USER_INFO_EXCLUSIVE;
  NewUserInfo->InfoSize    = sizeof (EFI_USER_INFO) + Policy->Length;
  UserInfo                 = NULL;
  mUserManager->SetInfo (
                  mUserManager,
                  User,
                  &UserInfo,
                  NewUserInfo,
                  NewUserInfo->InfoSize
                  );
  FreePool (NewUserInfo);
}


/**
  Set the default access policy of the specified user.

  @param[in]  User               Handle of a user profile. 

**/
VOID
SetAccessPolicy (
  IN        EFI_USER_PROFILE_HANDLE   User
  )
{
  EFI_USER_INFO_ACCESS_CONTROL  *Control;
  EFI_USER_INFO_HANDLE          UserInfo;
  EFI_USER_INFO                 *NewUserInfo;
  
  NewUserInfo = AllocateZeroPool (
                  sizeof (EFI_USER_INFO) + 
                  sizeof (EFI_USER_INFO_ACCESS_CONTROL)
                  );
  ASSERT (NewUserInfo != NULL);
  
  Control                  = (EFI_USER_INFO_ACCESS_CONTROL *) (NewUserInfo + 1);
  Control->Type            = EFI_USER_INFO_ACCESS_ENROLL_SELF;
  Control->Size            = sizeof (EFI_USER_INFO_ACCESS_CONTROL);

  NewUserInfo->InfoType    = EFI_USER_INFO_ACCESS_POLICY_RECORD;
  NewUserInfo->InfoAttribs = EFI_USER_INFO_STORAGE_PLATFORM_NV | 
                             EFI_USER_INFO_PUBLIC | 
                             EFI_USER_INFO_EXCLUSIVE;
  NewUserInfo->InfoSize    = sizeof (EFI_USER_INFO) + Control->Size;
  UserInfo                 = NULL;
  mUserManager->SetInfo (
                  mUserManager,
                  User,
                  &UserInfo,
                  NewUserInfo,
                  NewUserInfo->InfoSize
                  );
  FreePool (NewUserInfo);
}


/**
  Add a new user profile into the user profile database.

**/
VOID
CallAddUser (
  VOID
  )
{
  EFI_STATUS              Status;
  EFI_INPUT_KEY           Key;
  EFI_USER_PROFILE_HANDLE User;
  UINTN                   UserNameLen;
  CHAR16                  UserName[USER_NAME_LENGTH];
  CHAR16                  *QuestionStr;
  CHAR16                  *PromptStr;

  QuestionStr = NULL;
  PromptStr   = NULL;
  
  //
  // Get user name to add.
  //
  UserNameLen = sizeof (UserName);
  Status = GetUserNameInput (&UserNameLen, UserName);
  if (EFI_ERROR (Status)) {
    if (Status != EFI_ABORTED) {
      QuestionStr = GetStringById (STRING_TOKEN (STR_GET_USERNAME_FAILED));
      PromptStr   = GetStringById (STRING_TOKEN (STR_STROKE_KEY_CONTINUE)); 
      goto Done;
    }
    return ;
  }

  //
  // Create a new user profile.
  //
  User    = NULL;
  Status  = mUserManager->Create (mUserManager, &User);
  if (EFI_ERROR (Status)) {
    QuestionStr = GetStringById (STRING_TOKEN (STR_CREATE_PROFILE_FAILED));
    PromptStr   = GetStringById (STRING_TOKEN (STR_STROKE_KEY_CONTINUE)); 
  } else {
    //
    // Add default user information.
    //
    Status = SetUserName (User, UserNameLen, UserName);
    if (EFI_ERROR (Status)) {
      QuestionStr = GetStringById (STRING_TOKEN (STR_USER_ALREADY_EXISTED));
      PromptStr   = GetStringById (STRING_TOKEN (STR_STROKE_KEY_CONTINUE)); 
      goto Done;
    }

    SetCreateDate (User);
    SetIdentityPolicy (User);
    SetAccessPolicy (User);

    QuestionStr = GetStringById (STRING_TOKEN (STR_CREATE_PROFILE_SUCCESS));
    PromptStr   = GetStringById (STRING_TOKEN (STR_STROKE_KEY_CONTINUE)); 
  }

Done:
  CreatePopUp (
    EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
    &Key,
    QuestionStr,
    L"",
    PromptStr,
    NULL
    );
  FreePool (QuestionStr);
  FreePool (PromptStr);
}