windows_users 0.3.0

A crate for managing Windows local users and groups.
Documentation
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
use windows::{
    Win32::{
        Foundation::{CloseHandle, HANDLE},
        NetworkManagement::NetManagement::{
            NetApiBufferFree, NetUserAdd, NetUserChangePassword, NetUserDel, NetUserGetInfo,
            USER_INFO_4,
        },
        Security::{LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, LogonUserW},
        System::WindowsProgramming::GetUserNameW,
    },
    core::{PCWSTR, PWSTR},
};

use crate::{
    LogonHours, User, UserAccountFlags, UserManager, UserUpdate,
    error::WindowsUsersError,
    utils::{ToWideString, net_api_result, net_api_result_with_index},
};

pub mod update_ops;

impl UserManager {
    /// Adds a new user to the local machine.
    ///
    /// This function creates a local account from the provided [`User`] payload
    /// using `NetUserAdd` level 4.
    ///
    /// # Arguments
    ///
    /// * `user` - The user definition to create.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` when the account is created successfully.
    ///
    /// # Errors
    ///
    /// Returns a [`WindowsUsersError`] if:
    /// - The account creation API call fails (`NetUserAdd`)
    /// - A provided field is invalid
    /// - The account already exists
    /// - The caller does not have sufficient privileges
    ///
    /// # Security
    ///
    /// ⚠️ Requires **administrative privileges**.
    pub fn add_user(&self, user: &User) -> Result<(), WindowsUsersError> {
        let server_name_p = self.server;

        let mut info = user.to_user_info_4();

        let mut parm_err = 0;

        let status = unsafe {
            NetUserAdd(
                server_name_p,
                4,
                std::ptr::from_mut::<USER_INFO_4>(&mut info.user_info) as *const u8,
                Some(&raw mut parm_err),
            )
        };

        net_api_result_with_index(status, parm_err)?;

        Ok(())
    }

    /// Adds a user only if it does not already exist.
    ///
    /// This function checks whether the user already exists on the target machine.
    /// If the user is found, the function does nothing and returns `Ok(false)`.
    /// Otherwise, it creates the account using [`UserManager::add_user`] and returns `Ok(true)`.
    ///
    /// # Arguments
    ///
    /// * `user` - The user definition to create if absent.
    ///
    /// # Returns
    ///
    /// Returns:
    /// - `Ok(true)` if the user was created
    /// - `Ok(false)` if the user already existed and no action was taken
    ///
    /// # Errors
    ///
    /// Returns a [`WindowsUsersError`] if:
    /// - The existence check fails unexpectedly (rare, see notes below)
    /// - The account creation fails (`NetUserAdd`)
    /// - The caller does not have sufficient privileges
    ///
    /// # Security
    ///
    /// ⚠️ Requires **administrative privileges** when creating the user.
    pub fn add_user_if_not_exists(&self, user: &User) -> Result<bool, WindowsUsersError> {
        if self.user_exists(user.name()) {
            Ok(false)
        } else {
            self.add_user(user)?;
            Ok(true)
        }
    }

    /// Ensures that a user exists and matches the provided definition.
    ///
    /// This function implements a **create-or-update** behavior:
    ///
    /// - If the user does not exist, it is created using [`UserManager::add_user`]
    /// - If the user already exists, it is updated using [`UserManager::update_user`]
    ///
    /// # Arguments
    ///
    /// * `user` - The desired user state
    ///
    /// # Returns
    ///
    /// Returns:
    /// - `Ok(true)` if the user was created
    /// - `Ok(false)` if the user already existed and was updated
    ///
    /// # Errors
    ///
    /// Returns a [`WindowsUsersError`] if:
    /// - The update operation fails (`NetUserSetInfo`, group updates, etc.)
    /// - The creation operation fails (`NetUserAdd`)
    /// - The caller does not have sufficient privileges
    ///
    /// # Security
    ///
    /// ⚠️ Requires **administrative privileges** for both creation and update operations.
    pub fn add_user_or_update(&self, user: &User) -> Result<bool, WindowsUsersError> {
        if self.user_exists(user.name()) {
            self.update_user(user.name(), &UserUpdate::from(user.clone()))?;
            Ok(false)
        } else {
            self.add_user(user)?;
            Ok(true)
        }
    }

    /// Deletes a user from the local machine.
    ///
    /// This function removes an existing local account identified by `username`
    /// using `NetUserDel`.
    ///
    /// # Arguments
    ///
    /// * `username` - The account name to delete.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` when the account is deleted successfully.
    ///
    /// # Errors
    ///
    /// Returns a [`WindowsUsersError`] if:
    /// - The user does not exist
    /// - The deletion API call fails (`NetUserDel`)
    /// - The caller does not have sufficient privileges
    ///
    /// # Security
    ///
    /// ⚠️ Requires **administrative privileges**.
    pub fn delete_user(&self, username: &str) -> Result<(), WindowsUsersError> {
        let server_name = self.server;

        let username_w = username.to_wide();

        let status = unsafe { NetUserDel(server_name, PCWSTR(username_w.as_ptr())) };

        net_api_result(status)
    }

    /// Changes a user's password on the local machine.
    ///
    /// This function changes the password of `username` by validating
    /// `old_password` and replacing it with `new_password`.
    ///
    /// # Arguments
    ///
    /// * `username` - The target account name.
    /// * `old_password` - The current password used for validation.
    /// * `new_password` - The new password to set.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` when the password is changed successfully.
    ///
    /// # Errors
    ///
    /// Returns a [`WindowsUsersError`] if:
    /// - The username or old password is incorrect
    /// - The new password does not satisfy policy constraints
    /// - The API call fails (`NetUserChangePassword`)
    ///
    /// # Security
    ///
    /// Does not necessarily require administrative privileges when changing
    /// the caller's own password with valid credentials.
    pub fn change_user_password(
        &self,
        username: &str,
        old_password: &str,
        new_password: &str,
    ) -> Result<(), WindowsUsersError> {
        let server_name = self.server;

        let username_w = username.to_wide();
        let old_pw_w = old_password.to_wide();
        let new_pw_w = new_password.to_wide();

        let status = unsafe {
            NetUserChangePassword(
                server_name,
                PCWSTR(username_w.as_ptr()),
                PCWSTR(old_pw_w.as_ptr()),
                PCWSTR(new_pw_w.as_ptr()),
            )
        };

        net_api_result(status)
    }

    /// Updates an existing user on the local machine.
    ///
    /// This function updates account properties for `username` with a full
    /// `USER_INFO_4` payload generated from [`UserUpdate`].
    ///
    /// # Arguments
    ///
    /// * `username` - The account name to update.
    /// * `settings` - The update payload applied to the target account.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` when the update succeeds.
    ///
    /// # Errors
    ///
    /// Returns a [`WindowsUsersError`] if:
    /// - The user does not exist
    /// - One or more fields are invalid
    /// - The update API call fails (`NetUserSetInfo`)
    /// - The caller does not have sufficient privileges
    ///
    /// # Security
    ///
    /// ⚠️ Requires **administrative privileges**.
    pub fn update_user(
        &self,
        username: &str,
        settings: &UserUpdate,
    ) -> Result<(), WindowsUsersError> {
        if let Some(ref password) = settings.password {
            self.set_user_password(username, password)?;
        }

        if let Some(ref home_dir) = settings.home_dir {
            self.set_user_home_directory(username, home_dir)?;
        }

        if let Some(ref comment) = settings.comment {
            self.set_user_comment(username, comment)?;
        }

        if let Some(flags) = settings.flags {
            self.set_user_flags(username, flags)?;
        }

        if let Some(ref script_path) = settings.script_path {
            self.set_user_script_path(username, script_path)?;
        }

        if let Some(ref full_name) = settings.full_name {
            self.set_user_full_name(username, full_name)?;
        }

        if let Some(ref user_comment) = settings.user_comment {
            self.set_user_user_comment(username, user_comment)?;
        }

        if let Some(ref workstations) = settings.workstations {
            self.set_user_workstations(username, workstations)?;
        }

        if let Some(acct_expires) = settings.acct_expires {
            self.set_user_account_expiration(username, acct_expires)?;
        }

        if let Some(ref logon_hours) = settings.logon_hours {
            self.set_user_logon_hours(
                username,
                LogonHours::UNITS_PER_WEEK,
                logon_hours.clone().as_bytes(),
            )?;
        }

        if let Some(country_code) = settings.country_code {
            self.set_user_country_code(username, country_code)?;
        }

        if let Some(primary_group_id) = settings.primary_group_id {
            self.set_user_primary_group(username, primary_group_id)?;
        }

        if let Some(ref profile) = settings.profile {
            self.set_user_profile(username, profile)?;
        }

        if let Some(ref home_dir_drive) = settings.home_dir_drive {
            self.set_user_home_dir_drive(username, home_dir_drive)?;
        }

        if let Some(ref name) = settings.name {
            self.rename_user(username, name)?;
        }

        Ok(())
    }

    /// Gets a single user from the local machine.
    ///
    /// This function retrieves a local account by name using `NetUserGetInfo`
    /// level 4 and converts it into a [`User`] value.
    ///
    /// # Arguments
    ///
    /// * `username` - The account name to retrieve.
    ///
    /// # Returns
    ///
    /// Returns the fetched [`User`] on success.
    ///
    /// # Errors
    ///
    /// Returns a [`WindowsUsersError`] if:
    /// - The user does not exist
    /// - The API call fails (`NetUserGetInfo`)
    /// - Returned data cannot be converted into [`User`]
    pub fn get_user(&self, username: &str) -> Result<User, WindowsUsersError> {
        let server_name = self.server;

        let username_w = username.to_wide();

        let mut buffer = std::ptr::null_mut();

        let status =
            unsafe { NetUserGetInfo(server_name, PCWSTR(username_w.as_ptr()), 4, &raw mut buffer) };

        let _guard = scopeguard::guard(buffer, |buf| {
            if !buf.is_null() {
                unsafe { NetApiBufferFree(Some(buf.cast())) };
            }
        });

        net_api_result(status)?;

        let user = unsafe {
            let ui4 = &*(buffer as *const USER_INFO_4);
            User::try_from(ui4)?
        };

        Ok(user)
    }

    /// Checks if a user exists on the local machine.
    ///
    /// This function queries `NetUserGetInfo` level 0 and maps success to `true`.
    /// Any failure is interpreted as `false`.
    ///
    /// # Arguments
    ///
    /// * `username` - The account name to probe.
    ///
    /// # Returns
    ///
    /// Returns `true` if the user can be retrieved, otherwise `false`.
    ///
    /// # Errors
    ///
    /// This function does not return errors directly.
    pub fn user_exists(&self, username: &str) -> bool {
        let server_name = self.server;

        let username_w = username.to_wide();

        let mut buffer = std::ptr::null_mut();

        let status =
            unsafe { NetUserGetInfo(server_name, PCWSTR(username_w.as_ptr()), 0, &raw mut buffer) };

        let _guard = scopeguard::guard(buffer, |buf| {
            if !buf.is_null() {
                unsafe { NetApiBufferFree(Some(buf.cast())) };
            }
        });

        net_api_result(status).is_ok()
    }

    /// Retrieves the currently logged-in user as a full [`User`] object.
    ///
    /// This function resolves the identity of the current Windows session user
    /// and then fetches its full account information from the target machine
    /// using `NetUserGetInfo` level 4.
    ///
    /// The lookup is performed in two steps:
    ///
    /// - The current username is retrieved using `GetUserNameW` (Win32 API)
    /// - The username is resolved into a full account record using `NetUserGetInfo`
    ///
    /// # Returns
    ///
    /// Returns the full [`User`] object on success.
    ///
    /// # Errors
    ///
    /// Returns a [`WindowsUsersError`] if:
    /// - The current username cannot be retrieved (`GetUserNameW` fails)
    /// - The user does not exist in the system database
    /// - The NetAPI lookup fails (`NetUserGetInfo`)
    /// - The returned data cannot be converted into [`User`]
    pub fn current_user(&self) -> Result<User, WindowsUsersError> {
        let mut buffer = [0u16; 256];
        let mut size = buffer.len() as u32;

        unsafe { GetUserNameW(Some(PWSTR(buffer.as_mut_ptr())), &mut size)? };

        let username = String::from_utf16_lossy(&buffer[..size.saturating_sub(1) as usize]);

        let username_w = username.to_wide();

        let mut ptr = std::ptr::null_mut();

        let status =
            unsafe { NetUserGetInfo(self.server, PCWSTR(username_w.as_ptr()), 4, &raw mut ptr) };

        let _guard = scopeguard::guard(ptr, |p| {
            if !p.is_null() {
                unsafe { NetApiBufferFree(Some(p.cast())) };
            }
        });

        net_api_result(status)?;

        let user = unsafe {
            let info = &*(ptr as *const USER_INFO_4);
            User::try_from(info)?
        };

        Ok(user)
    }

    /// Enables or disables a user account.
    ///
    /// This function toggles the `ACCOUNTDISABLE` flag depending on the value
    /// of `enable`:
    ///
    /// - `true`  → removes the flag (account is enabled)
    /// - `false` → sets the flag (account is disabled)
    ///
    /// # Arguments
    ///
    /// * `username` - The account name to update.
    /// * `enable` - Whether the account should be enabled.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` on success.
    ///
    /// # Errors
    ///
    /// Returns a [`WindowsUsersError`] if:
    /// - The user does not exist
    /// - The update operation fails
    pub fn enable_user(&self, username: &str, enable: bool) -> Result<(), WindowsUsersError> {
        if enable {
            self.remove_user_flags(username, UserAccountFlags::ACCOUNTDISABLE)
        } else {
            self.add_user_flags(username, UserAccountFlags::ACCOUNTDISABLE)
        }
    }

    /// Attempts to authenticate a user using Windows LSA logon.
    ///
    /// This function performs a real authentication attempt using the Windows API
    /// `LogonUserW` with the `LOGON32_LOGON_NETWORK` logon type.
    ///
    /// It validates credentials against the Windows security subsystem (LSA),
    /// meaning it performs a real logon attempt rather than only checking password
    /// policy rules.
    ///
    /// When targeting a remote machine through [`UserManager::remote`], the
    /// server name is converted from UNC format (e.g. `"\\\\SERVER01"`) into a
    /// machine/domain name compatible with `LogonUserW`.
    ///
    /// # Arguments
    ///
    /// * `username` - The account name to authenticate.
    /// * `password` - The plaintext password to validate.
    ///
    /// # Returns
    ///
    /// Returns:
    /// - `Ok(())` if authentication succeeds
    /// - `Err(WindowsUsersError)` if authentication fails or a system error occurs
    ///
    /// # Notes
    ///
    /// This function does NOT return a boolean because Windows authentication
    /// failures carry meaningful semantic information (e.g. invalid credentials,
    /// account restrictions, lockout, etc.).
    ///
    /// All failures are returned as `Err`, including:
    /// - invalid credentials (`ERROR_LOGON_FAILURE`)
    /// - access denied (`ERROR_ACCESS_DENIED`)
    /// - account restrictions
    /// - policy violations
    /// - system or API errors
    pub fn validate_user_logon(
        &self,
        username: &str,
        password: &str,
    ) -> Result<(), WindowsUsersError> {
        let username_w = username.to_wide();
        let password_w = password.to_wide();

        // Transform "\\SERVER01" -> "SERVER01"
        let domain_wide = self._server_wide.as_ref().map(|wide| {
            let server = String::from_utf16_lossy(wide);
            server.trim_start_matches('\\').to_wide()
        });

        let domain = domain_wide
            .as_ref()
            .map(|v| PCWSTR(v.as_ptr()))
            .unwrap_or(PCWSTR::null());

        let mut token = HANDLE::default();

        let result = unsafe {
            LogonUserW(
                PCWSTR(username_w.as_ptr()),
                domain,
                PCWSTR(password_w.as_ptr()),
                LOGON32_LOGON_NETWORK,
                LOGON32_PROVIDER_DEFAULT,
                &mut token,
            )
        };

        match result {
            Ok(()) => {
                unsafe {
                    let _ = CloseHandle(token);
                }
                Ok(())
            }
            Err(err) => Err(WindowsUsersError::WindowsError(err)),
        }
    }
}