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
use std::sync::Arc;
use crate::core::client::{VimClient, Result};
/// Managed object used to control direct access to the host.
///
/// This should be used to control users and privileges on the host directly,
/// which are different from the users and privileges defined in vCenter.
///
/// See *AuthorizationManager* for more information on permissions.
#[derive(Clone)]
pub struct HostAccessManager {
client: Arc<dyn VimClient>,
mo_id: String,
}
impl HostAccessManager {
pub fn new(client: Arc<dyn VimClient>, mo_id: &str) -> Self {
Self {
client,
mo_id: mo_id.to_string(),
}
}
/// Update the access mode for a user or group.
///
/// If the host is in lockdown mode, this operation is allowed only on
/// users in the exceptions list - see *HostAccessManager.QueryLockdownExceptions*,
/// and trying to change the access mode of other users or groups
/// will fail with SecurityError.
///
/// ***Required privileges:*** Global.Settings
///
/// ## Parameters:
///
/// ### principal
/// The affected user or group.
///
/// ### is_group
/// True if principal refers to a group account,
/// false otherwise.
///
/// ### access_mode
/// AccessMode to be granted.
/// *accessOther* is meaningless and
/// will result in InvalidArgument exception.
///
/// ## Errors:
///
/// ***AuthMinimumAdminPermission***: if this change would render the
/// ESXi host inaccessible for local non-system users.
/// The API *HostAccessManager.ChangeLockdownMode* may be used
/// instead.
///
/// ***InvalidArgument***: if accessMode is not valid.
///
/// ***SecurityError***: if the host is in lockdown mode and 'principal'
/// is not in the exceptions list.
///
/// ***UserNotFound***: if the specified user is not found.
pub async fn change_access_mode(&self, principal: &str, is_group: bool, access_mode: crate::types::enums::HostAccessModeEnum) -> Result<()> {
let input = ChangeAccessModeRequestType {principal, is_group, access_mode, };
self.client.invoke_void("", "HostAccessManager", &self.mo_id, "ChangeAccessMode", Some(&input)).await
}
/// Changes the lockdown state of the ESXi host.
///
/// This operation will do nothing if the host is already in the desired
/// lockdown state.
///
/// When the host is in lockdown mode it can be managed only through vCenter
/// and through DCUI (Direct Console User Interface) if the DCUI service is
/// running.
/// This is achieved by removing all permissions on the host, except those
/// of the exception users defined with *HostAccessManager.UpdateLockdownExceptions*.
///
/// In addition, the permissions for users 'dcui' and 'vpxuser'
/// are always preserved.
///
/// When lockdown mode is disabled, the system will try to restore all
/// permissions that have been removed when lockdown mode was enabled.
/// It is possible that not all permissions may be restored and this is not
/// an error, e.g. if in the meantime some user or managed object was deleted.
///
/// It may be possible that after exiting lockdown mode the only permissions
/// on the host will be those of users 'dcui' and 'vpxuser'. This will render the
/// host unmanageable if it is not already managed by vCenter, or if the
/// connection to vCenter is lost. To prevent this, the users in the
/// "DCUI.Access" list will be assigned Admin roles.
///
/// While the host is in lockdown mode, some operations will fail with
/// SecurityError. This ensures that the conditions for lockdown mode cannot
/// be changed. For example it is allowed to change the access mode only for
/// users in the exceptions list.
///
/// When the host is in lockdown mode, changing the running state of service
/// DCUI through *HostServiceSystem* will also fail with
/// SecurityError accompanied with an appropriate localizeable message.
///
/// ***Required privileges:*** Host.Config.Settings
///
/// ## Parameters:
///
/// ### mode
/// The new desired lockdown mode.
///
/// If this is the same as the current lockdown mode state, the
/// operation will silently succeed and nothing will be changed.
///
/// If this is *lockdownDisabled*
/// then lockdown mode will be disabled and the system will
/// start service DCUI if it is not running.
///
/// If this is *lockdownNormal*
/// then lockdown mode will be enabled and the system will
/// start service DCUI if it is not running.
///
/// If this is *lockdownStrict*
/// then lockdown mode will be enabled and the system will
/// stop service DCUI if it is running.
///
/// ## Errors:
///
/// ***AuthMinimumAdminPermission***: if the user invoking the operation
/// is not in the exceptions list - see
/// *HostAccessManager.QueryLockdownExceptions*.
///
/// ***NoPermission***: if the current session does not have enough
/// permissions to perform the operation.
pub async fn change_lockdown_mode(&self, mode: crate::types::enums::HostLockdownModeEnum) -> Result<()> {
let input = ChangeLockdownModeRequestType {mode, };
self.client.invoke_void("", "HostAccessManager", &self.mo_id, "ChangeLockdownMode", Some(&input)).await
}
/// Get the list of users which are exceptions for lockdown mode.
///
/// See *HostAccessManager.UpdateLockdownExceptions*.
///
/// ***Required privileges:*** Global.Settings
///
/// ## Returns:
///
/// the list of users which will not lose their permissions when
/// the host enters lockdown mode.
pub async fn query_lockdown_exceptions(&self) -> Result<Option<Vec<String>>> {
let bytes_opt = self.client.invoke_optional("", "HostAccessManager", &self.mo_id, "QueryLockdownExceptions", None).await?;
match bytes_opt {
Some(ref b) => Ok(Some(crate::core::client::unmarshal_array(self.client.transport(), b)?)),
None => Ok(None),
}
}
/// Get the list of local system users.
///
/// These are special users like 'vpxuser' and 'dcui',
/// which may be used for authenticating different sub-components of the
/// vSphere system and may be essential for its correct functioning.
///
/// Usually these users may not be used by human operators to connect
/// directly to the host and the UI may choose to show them only in some
/// "advanced" UI view.
///
/// ***Required privileges:*** Global.Settings
///
/// ## Returns:
///
/// the list of local system users.
pub async fn query_system_users(&self) -> Result<Option<Vec<String>>> {
let bytes_opt = self.client.invoke_optional("", "HostAccessManager", &self.mo_id, "QuerySystemUsers", None).await?;
match bytes_opt {
Some(ref b) => Ok(Some(crate::core::client::unmarshal_array(self.client.transport(), b)?)),
None => Ok(None),
}
}
/// Retrieve access entries.
///
/// Returns a list of AccessEntry objects for each VIM user or group which
/// have explicitly assigned permissions on the host. This means that
/// *accessNone* will not be present in the result.
///
/// ***Required privileges:*** Global.Settings
///
/// ## Returns:
///
/// a list of AccessEntry objects.
pub async fn retrieve_host_access_control_entries(&self) -> Result<Option<Vec<crate::types::structs::HostAccessControlEntry>>> {
let bytes_opt = self.client.invoke_optional("", "HostAccessManager", &self.mo_id, "RetrieveHostAccessControlEntries", None).await?;
match bytes_opt {
Some(ref b) => Ok(Some(crate::core::client::unmarshal_array(self.client.transport(), b)?)),
None => Ok(None),
}
}
/// Update the list of users which are exceptions for lockdown mode.
///
/// Usually these are user accounts used by third party solutions and
/// external applications which need to continue to function in lockdown
/// mode. It is not advised to add user accounts used by human operators,
/// because this will compromise the purpose of lockdown mode.
///
/// Both local and domain users are supported. The format for domain accounts
/// is "DOMAIN\\login".
///
/// When this API is called when the host is in lockdown mode,
/// the behaviour is as follows:
/// - if a user is removed from the exceptions list,
/// then the permissions of that user are removed.
/// - if a user is added to the exceptions list,
/// then the permissions of that user are restored.
///
/// ***Required privileges:*** Global.Settings
///
/// ## Parameters:
///
/// ### users
/// the new list of lockdown mode exceptions.
///
/// ## Errors:
///
/// ***AuthMinimumAdminPermission***: if the user invoking the operation
/// is not present in the new list of exceptions.
///
/// ***UserNotFound***: if one of the specified users is not found.
pub async fn update_lockdown_exceptions(&self, users: Option<&[String]>) -> Result<()> {
let input = UpdateLockdownExceptionsRequestType {users, };
self.client.invoke_void("", "HostAccessManager", &self.mo_id, "UpdateLockdownExceptions", Some(&input)).await
}
/// Update the list of local system users.
///
/// The special users 'dcui' and 'vpxuser' need not be specified.
/// They are always reported in the list of system users.
///
/// ***Required privileges:*** Global.Settings
///
/// ## Parameters:
///
/// ### users
/// the new list of local system users.
///
/// ## Errors:
///
/// ***InvalidArgument***: If one of the specified user names is not valid,
/// or is in Active Directory domain format.
///
/// ***UserNotFound***: If one of the specified users is not found.
pub async fn update_system_users(&self, users: Option<&[String]>) -> Result<()> {
let input = UpdateSystemUsersRequestType {users, };
self.client.invoke_void("", "HostAccessManager", &self.mo_id, "UpdateSystemUsers", Some(&input)).await
}
/// Current lockdown state of the host.
///
/// ***Required privileges:*** System.View
pub async fn lockdown_mode(&self) -> Result<crate::types::enums::HostLockdownModeEnum> {
let pv_opt = self.client.fetch_property_raw("", "HostAccessManager", &self.mo_id, "lockdownMode").await?;
let pv = pv_opt.ok_or_else(|| crate::core::client::VimError::ParseError("property lockdownMode was empty".to_string()))?;
let result: crate::types::enums::HostLockdownModeEnum = crate::core::client::extract_property(pv)?;
Ok(result)
}
}
struct ChangeAccessModeRequestType<'a> {
principal: &'a str,
is_group: bool,
access_mode: crate::types::enums::HostAccessModeEnum,
}
impl<'a> miniserde::Serialize for ChangeAccessModeRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(ChangeAccessModeRequestTypeSer { data: self, seq: 0 }))
}
}
struct ChangeAccessModeRequestTypeSer<'b, 'a> {
data: &'b ChangeAccessModeRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for ChangeAccessModeRequestTypeSer<'b, 'a> {
fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
let seq = self.seq;
self.seq += 1;
match seq {
0 => return Some((std::borrow::Cow::Borrowed("_typeName"), &"ChangeAccessModeRequestType")),
1 => return Some((std::borrow::Cow::Borrowed("principal"), &self.data.principal as &dyn miniserde::Serialize)),
2 => return Some((std::borrow::Cow::Borrowed("isGroup"), &self.data.is_group as &dyn miniserde::Serialize)),
3 => return Some((std::borrow::Cow::Borrowed("accessMode"), &self.data.access_mode as &dyn miniserde::Serialize)),
_ => return None,
}
}
}
struct ChangeLockdownModeRequestType {
mode: crate::types::enums::HostLockdownModeEnum,
}
impl miniserde::Serialize for ChangeLockdownModeRequestType {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(ChangeLockdownModeRequestTypeSer { data: self, seq: 0 }))
}
}
struct ChangeLockdownModeRequestTypeSer<'b> {
data: &'b ChangeLockdownModeRequestType,
seq: usize,
}
impl<'b> miniserde::ser::Map for ChangeLockdownModeRequestTypeSer<'b> {
fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
let seq = self.seq;
self.seq += 1;
match seq {
0 => return Some((std::borrow::Cow::Borrowed("_typeName"), &"ChangeLockdownModeRequestType")),
1 => return Some((std::borrow::Cow::Borrowed("mode"), &self.data.mode as &dyn miniserde::Serialize)),
_ => return None,
}
}
}
struct UpdateLockdownExceptionsRequestType<'a> {
users: Option<&'a [String]>,
}
impl<'a> miniserde::Serialize for UpdateLockdownExceptionsRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(UpdateLockdownExceptionsRequestTypeSer { data: self, seq: 0 }))
}
}
struct UpdateLockdownExceptionsRequestTypeSer<'b, 'a> {
data: &'b UpdateLockdownExceptionsRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for UpdateLockdownExceptionsRequestTypeSer<'b, 'a> {
fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
loop {
let seq = self.seq;
self.seq += 1;
match seq {
0 => return Some((std::borrow::Cow::Borrowed("_typeName"), &"UpdateLockdownExceptionsRequestType")),
1 => {
let Some(ref val) = self.data.users else { continue; };
return Some((std::borrow::Cow::Borrowed("users"), val as &dyn miniserde::Serialize));
}
_ => return None,
}
}
}
}
struct UpdateSystemUsersRequestType<'a> {
users: Option<&'a [String]>,
}
impl<'a> miniserde::Serialize for UpdateSystemUsersRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(UpdateSystemUsersRequestTypeSer { data: self, seq: 0 }))
}
}
struct UpdateSystemUsersRequestTypeSer<'b, 'a> {
data: &'b UpdateSystemUsersRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for UpdateSystemUsersRequestTypeSer<'b, 'a> {
fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
loop {
let seq = self.seq;
self.seq += 1;
match seq {
0 => return Some((std::borrow::Cow::Borrowed("_typeName"), &"UpdateSystemUsersRequestType")),
1 => {
let Some(ref val) = self.data.users else { continue; };
return Some((std::borrow::Cow::Borrowed("users"), val as &dyn miniserde::Serialize));
}
_ => return None,
}
}
}
}