summaryrefslogtreecommitdiffstats
path: root/SecurityPkg/UserIdentification/UserProfileManagerDxe/UserProfileDelete.c
blob: 8be302e1ccb4fff3052e680e65c295a9a0869068 (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
/** @file
  The functions to delete 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 the username from the specified user.

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

  @retval EFI_STRING_ID          The String Id of the user's username.

**/
EFI_STRING_ID 
GetUserName (
  IN  EFI_USER_PROFILE_HANDLE                   User
  )
{
  EFI_STATUS            Status;
  EFI_USER_INFO_HANDLE  UserInfo;
  EFI_USER_INFO         *Info;
  UINTN                 InfoSize;
  UINTN                 MemSize;
  UINTN                 NameLen;
  CHAR16                UserName[USER_NAME_LENGTH];
  EFI_STRING_ID         UserId;
  
  //
  // Allocate user information memory.
  //
  MemSize = sizeof (EFI_USER_INFO) + 63;
  Info    = AllocateZeroPool (MemSize);
  ASSERT (Info != NULL);
  
  //
  // Get user name information.
  //
  UserInfo = NULL;
  while (TRUE) {
    InfoSize = MemSize;
    //
    // Get next user information.
    //
    Status = mUserManager->GetNextInfo (
                             mUserManager,
                             User,
                             &UserInfo
                             );
    if (EFI_ERROR (Status)) {
      break;
    }

    Status = mUserManager->GetInfo (
                             mUserManager,
                             User,
                             UserInfo,
                             Info,
                             &InfoSize
                             );
    if (Status == EFI_BUFFER_TOO_SMALL) {
      MemSize = InfoSize;
      FreePool (Info);
      Info = AllocateZeroPool (MemSize);
      ASSERT (Info != NULL);

      Status = mUserManager->GetInfo (
                               mUserManager,
                               User,
                               UserInfo,
                               Info,
                               &InfoSize
                               );
    }
    //
    // Check user information.
    //
    if (Status == EFI_SUCCESS) {
      if (Info->InfoType == EFI_USER_INFO_NAME_RECORD) {
        NameLen = Info->InfoSize - sizeof (EFI_USER_INFO);
        if (NameLen > USER_NAME_LENGTH * sizeof (CHAR16)) {
          NameLen = USER_NAME_LENGTH * sizeof (CHAR16);
        }
        ASSERT (NameLen >= sizeof (CHAR16));
        CopyMem (UserName, (UINT8 *) (Info + 1), NameLen);
        UserName[NameLen / sizeof (CHAR16) - 1] = 0;
        UserId = HiiSetString (
                   mCallbackInfo->HiiHandle,
                   0,
                   UserName,
                   NULL
                   );
        if (UserId != 0) {
          FreePool (Info);
          return UserId;
        }
      }
    }
  }

  FreePool (Info);
  return 0;
}


/**
  Add a username item in form.

  @param[in]  User          Points to the user profile whose username is added. 
  @param[in]  Index         The index of the user in the user name list
  @param[in]  OpCodeHandle  Points to container for dynamic created opcodes.

**/
VOID
AddUserToForm (
  IN  EFI_USER_PROFILE_HANDLE                   User,
  IN  UINT16                                    Index,
  IN  VOID                                      *OpCodeHandle
  )
{
  EFI_STRING_ID NameId;

  //
  // Get user name
  //
  NameId = GetUserName (User);
  if (NameId == 0) {
    return ;
  }
  
  //
  // Create user name option.
  //
  switch (Index & KEY_FIRST_FORM_MASK) {
  case KEY_MODIFY_USER:
    HiiCreateGotoOpCode (
      OpCodeHandle,                   // Container for dynamic created opcodes
      FORMID_USER_INFO,               // Target Form ID
      NameId,                         // Prompt text
      STRING_TOKEN (STR_NULL_STRING), // Help text
      EFI_IFR_FLAG_CALLBACK,          // Question flag
      Index                           // Question ID
      );
    break;

  case KEY_DEL_USER:
    HiiCreateActionOpCode (
      OpCodeHandle,                   // Container for dynamic created opcodes
      Index,                          // Question ID
      NameId,                         // Prompt text
      STRING_TOKEN (STR_NULL_STRING), // Help text
      EFI_IFR_FLAG_CALLBACK,          // Question flag
      0                               // Action String ID
      );
    break;

  default:
    break;
  }
}


/**
  Delete the user specified by UserIndex in user profile database.

  @param[in]  UserIndex       The index of user in the user name list 
                              to be deleted.

**/
VOID
DeleteUser (
  IN UINT8                                      UserIndex
  )
{
  EFI_STATUS              Status;
  EFI_USER_PROFILE_HANDLE User;
  EFI_INPUT_KEY           Key;
  EFI_USER_INFO_HANDLE    UserInfo;
  EFI_USER_INFO           *Info;
  UINTN                   InfoSize;

  //
  // Find specified user profile and delete it.
  //
  User    = NULL;
  Status  = mUserManager->GetNext (mUserManager, &User);
  if (EFI_ERROR (Status)) {
    goto Done;
  }
  
  while (UserIndex > 1) {
    Status = mUserManager->GetNext (mUserManager, &User);
    if (EFI_ERROR (Status)) {
      goto Done;
    }
    UserIndex--;
  }

  if (UserIndex == 1) {
    //
    // Get the identification policy.
    //
    Status = FindInfoByType (User, EFI_USER_INFO_IDENTITY_POLICY_RECORD, &UserInfo);
    if (EFI_ERROR (Status)) {
      goto Done;
    }

    InfoSize = 0;
    Info = NULL;
    Status   = mUserManager->GetInfo (mUserManager, User, UserInfo, Info, &InfoSize);
    if (Status == EFI_BUFFER_TOO_SMALL) {
      Info = AllocateZeroPool (InfoSize);
      if (Info == NULL) {
        goto Done;
      }
      Status = mUserManager->GetInfo (mUserManager, User, UserInfo, Info, &InfoSize);
    }

    //
    // Delete the user on the credential providers by its identification policy.
    //
    ASSERT (Info != NULL);
    DeleteCredentialFromProviders ((UINT8 *)(Info + 1), Info->InfoSize - sizeof (EFI_USER_INFO), User);
    FreePool (Info);
    
    Status = mUserManager->Delete (mUserManager, User);
    if (EFI_ERROR (Status)) {
      goto Done;
    }
    CreatePopUp (
      EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
      &Key,
      L"Delete User Succeed!",
      L"",
      L"Please Press Any Key to Continue ...",
      NULL
      );
    return ;  
  }

Done:
  CreatePopUp (
    EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
    &Key,
    L"Delete User Failed!",
    L"",
    L"Please Press Any Key to Continue ...",
    NULL
    );
}


/**
  Display user select form, cab select a user to delete.

**/
VOID
SelectUserToDelete (
  VOID
  )
{
  EFI_STATUS              Status;
  UINT8                   Index;
  EFI_USER_PROFILE_HANDLE User;
  EFI_USER_PROFILE_HANDLE CurrentUser;
  VOID                    *StartOpCodeHandle;
  VOID                    *EndOpCodeHandle;
  EFI_IFR_GUID_LABEL      *StartLabel;
  EFI_IFR_GUID_LABEL      *EndLabel;

  //
  // Initialize the container for dynamic opcodes.
  //
  StartOpCodeHandle = HiiAllocateOpCodeHandle ();
  ASSERT (StartOpCodeHandle != NULL);

  EndOpCodeHandle = HiiAllocateOpCodeHandle ();
  ASSERT (EndOpCodeHandle != NULL);

  //
  // Create Hii Extend Label OpCode.
  //
  StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (
                                        StartOpCodeHandle,
                                        &gEfiIfrTianoGuid,
                                        NULL,
                                        sizeof (EFI_IFR_GUID_LABEL)
                                        );
  StartLabel->ExtendOpCode  = EFI_IFR_EXTEND_OP_LABEL;
  StartLabel->Number        = LABEL_USER_DEL_FUNC;

  EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (
                                      EndOpCodeHandle,
                                      &gEfiIfrTianoGuid,
                                      NULL,
                                      sizeof (EFI_IFR_GUID_LABEL)
                                      );
  EndLabel->ExtendOpCode  = EFI_IFR_EXTEND_OP_LABEL;
  EndLabel->Number        = LABEL_END;

  //
  // Add each user can be deleted.
  //
  User  = NULL;
  Index = 1;
  mUserManager->Current (mUserManager, &CurrentUser);
  while (TRUE) {
    Status = mUserManager->GetNext (mUserManager, &User);
    if (EFI_ERROR (Status)) {
      break;
    }

    if (User != CurrentUser) {
      AddUserToForm (
        User,
        (UINT16)(KEY_DEL_USER | KEY_SELECT_USER | Index),
        StartOpCodeHandle
        );
    }
    Index++;
  }

  HiiUpdateForm (
    mCallbackInfo->HiiHandle, // HII handle
    &gUserProfileManagerGuid, // Formset GUID
    FORMID_DEL_USER,          // Form ID
    StartOpCodeHandle,        // Label for where to insert opcodes
    EndOpCodeHandle           // Replace data
    );

  HiiFreeOpCodeHandle (StartOpCodeHandle);
  HiiFreeOpCodeHandle (EndOpCodeHandle);
}