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
562
563
//! Methods for creating and deleting states

use std::borrow::Borrow;
use std::fmt::{self, Debug, Formatter};
use std::io;

use tracing::debug;

use crate::ntapi;
use crate::security::{BoxedSecurityDescriptor, SecurityDescriptor};
use crate::state::{BorrowedState, OwnedState, RawState};
use crate::state_name::{DataScope, StateLifetime, StateName};
use crate::type_id::{TypeId, GUID};

/// The maximum size of a state in bytes
///
/// The maximum size of a state can be specified upon creation of the state and can be anything between `0` and
/// `4 KB`, which is the value of this constant. This value is also used as the default value when the maximum state
/// size is not specified.
pub const MAXIMUM_STATE_SIZE: usize = 0x1000;

/// A marker type for an unspecified lifetime when creating a state
///
/// The lifetime of a state must be specified upon its creation. When creating a state via a
/// [`StateCreation`], this is used as a type parameter to indicate that the lifetime has not been specified yet.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct UnspecifiedLifetime {
    _private: (),
}

impl UnspecifiedLifetime {
    const fn new() -> Self {
        Self { _private: () }
    }
}

impl Debug for UnspecifiedLifetime {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        // Hide the `_private` field
        f.debug_struct("UnspecifiedLifetime").finish()
    }
}

/// A marker type for an unspecified scope when creating a state
///
/// The scope of a state must be specified upon its creation. When creating a state via a [`StateCreation`],
/// this is used as a type parameter to indicate that the scope has not been specified yet.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct UnspecifiedScope {
    _private: (),
}

impl UnspecifiedScope {
    const fn new() -> Self {
        Self { _private: () }
    }
}

impl Debug for UnspecifiedScope {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        // Hide the `_private` field
        f.debug_struct("UnspecifiedScope").finish()
    }
}

/// A marker type for an unspecified security descriptor when creating a state
///
/// The security descriptor of a state can optionally be specified upon its creation. When creating a state via
/// a [`StateCreation`], this is used as a type parameter to indicate that no security descriptor has been specified.
/// In this case, a default security descriptor (see [`BoxedSecurityDescriptor::create_everyone_generic_all`]) will be
/// used.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct UnspecifiedSecurityDescriptor {
    _private: (),
}

impl UnspecifiedSecurityDescriptor {
    const fn new() -> Self {
        Self { _private: () }
    }
}

impl Debug for UnspecifiedSecurityDescriptor {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        // Hide the `_private` field
        f.debug_struct("UnspecifiedSecurityDescriptor").finish()
    }
}

/// The lifetime of a state when specified upon creation
///
/// This is different from a [`StateLifetime`] in two ways:
/// - It does not include an equivalent of the [`StateLifetime::WellKnown`] lifetime because states with that lifetime
///   are provisioned with the system and cannot be created.
/// - The [`CreatableStateLifetime::Permanent`] option comes with a `persist_data` flag because that flag only applies
///   to the [`StateLifetime::Permanent`](crate::state_name::StateLifetime::Permanent) (and
///   [`StateLifetime::WellKnown`](crate::state_name::StateLifetime::WellKnown)) lifetimes.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum CreatableStateLifetime {
    /// Lifetime of a *permanent* state, see [`StateLifetime::Permanent`]
    Permanent {
        /// Whether the state data (not the state itself) are persisted across system reboots
        persist_data: bool,
    },

    /// Lifetime of a *persistent* state (also known as *volatile*), see [`StateLifetime::Persistent`]
    Persistent,

    /// Lifetime of a *temporary* state, see [`StateLifetime::Temporary`]
    Temporary,
}

impl CreatableStateLifetime {
    /// Returns whether the `persist_data` flag should be set for this [`CreatableStateLifetime`]
    const fn persist_data(self) -> bool {
        matches!(self, Self::Permanent { persist_data: true })
    }
}

impl From<CreatableStateLifetime> for StateLifetime {
    fn from(lifetime: CreatableStateLifetime) -> Self {
        match lifetime {
            CreatableStateLifetime::Permanent { .. } => StateLifetime::Permanent,
            CreatableStateLifetime::Persistent => StateLifetime::Persistent,
            CreatableStateLifetime::Temporary => StateLifetime::Temporary,
        }
    }
}

/// A trait for types that can be fallibly converted into a security descriptor
///
/// Since [`SecurityDescriptor`] is an opaque type, this does not mean (fallibly) converting into an actual
/// [`SecurityDescriptor`] but fallibly converting into some type that can be borrowed as a [`SecurityDescriptor`].
///
/// This trait is implemented for
/// - all types that implement [`Borrow<SecurityDescriptor>`]
/// - the type [`UnspecifiedSecurityDescriptor`]
///
/// This allows the [`StateCreation::create_owned`] and [`StateCreation::create_static`] methods to be called after
/// - either setting a security descriptor explicitly through [`StateCreation::security_descriptor`]
/// - or leaving the security descriptor unspecified (in which case a default security descriptor will be used)
/// while avoiding initial creation of a default security descriptor if one is specified explicitly later.
///
/// This trait is sealed and cannot be implemented outside of `wnf`.
pub trait TryIntoSecurityDescriptor: private::Sealed {
    /// The target type of the fallible conversion
    type IntoSecurityDescriptor: Borrow<SecurityDescriptor>;

    /// Performs the fallible conversion
    ///
    /// # Errors
    /// Returns an error if the conversion fails
    fn try_into_security_descriptor(self) -> io::Result<Self::IntoSecurityDescriptor>;
}

impl<SD> TryIntoSecurityDescriptor for SD
where
    SD: Borrow<SecurityDescriptor>,
{
    type IntoSecurityDescriptor = Self;

    fn try_into_security_descriptor(self) -> io::Result<Self> {
        Ok(self)
    }
}

impl TryIntoSecurityDescriptor for UnspecifiedSecurityDescriptor {
    type IntoSecurityDescriptor = BoxedSecurityDescriptor;

    fn try_into_security_descriptor(self) -> io::Result<BoxedSecurityDescriptor> {
        BoxedSecurityDescriptor::create_everyone_generic_all()
    }
}

/// A builder type for creating states
///
/// You can use this type to create a state by applying the following steps:
/// 1. Create a new builder using [`StateCreation::new`]
/// 2. Configure options using the appropriate methods on [`StateCreation`]
/// 3. Call [`StateCreation::create_owned`] or [`StateCreation::create_static`] to create the state
///
/// The following options can be configured:
///
/// - [`lifetime`](StateCreation::lifetime): Mandatory
/// - [`scope`](StateCreation::scope): Mandatory
/// - [`maximum_state_size`](StateCreation::maximum_state_size): Optional, default: `0x1000`
/// - [`security_descriptor`](StateCreation::security_descriptor): Optional, default: see
///   [`BoxedSecurityDescriptor::create_everyone_generic_all`]
/// - [`type_id`](StateCreation::type_id): Optional, default: none
///
/// Note that the [`StateCreation::create_owned`] and [`StateCreation::create_static`] methods are only available once
/// the mandatory options have been configured.
///
/// # Example
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use wnf::{CreatableStateLifetime, DataScope, OwnedState, StateCreation};
///
/// let state: OwnedState<u32> = StateCreation::new()
///     .lifetime(CreatableStateLifetime::Temporary)
///     .scope(DataScope::Machine)
///     .create_owned()?;
/// # Ok(()) }
/// ```
///
/// If you want to create multiple states from a single builder, clone the builder first:
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use wnf::{CreatableStateLifetime, DataScope, OwnedState, StateCreation};
///
/// let template = StateCreation::new()
///     .lifetime(CreatableStateLifetime::Temporary)
///     .scope(DataScope::Machine);
///
/// let large_state: OwnedState<u32> = template.clone().maximum_state_size(0x800).create_owned()?;
///
/// let small_state: OwnedState<u32> = template.maximum_state_size(0x400).create_owned()?;
/// # Ok(()) }
/// ```
///
/// In order to quickly create a temporary machine-scoped state (e.g. for testing purposes), consider using the
/// [`OwnedState::create_temporary`] or [`BorrowedState::create_temporary`] methods.
///
/// Note that a newly created state is initialized with data of size zero. This means that unless the data type `T` is
/// zero-sized or a slice type, you need to update the state data with a value of type `T` before querying it for the
/// first time.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct StateCreation<L, S, SD> {
    // mandatory fields
    lifetime: L,
    scope: S,

    // optional fields
    maximum_state_size: Option<usize>,
    security_descriptor: SD,
    type_id: TypeId,
}

impl Default for StateCreation<UnspecifiedLifetime, UnspecifiedScope, UnspecifiedSecurityDescriptor> {
    fn default() -> Self {
        Self::new()
    }
}

impl StateCreation<UnspecifiedLifetime, UnspecifiedScope, UnspecifiedSecurityDescriptor> {
    /// Creates a new [`StateCreation`] builder with no configured options
    pub const fn new() -> Self {
        Self {
            lifetime: UnspecifiedLifetime::new(),
            scope: UnspecifiedScope::new(),

            maximum_state_size: None,
            security_descriptor: UnspecifiedSecurityDescriptor::new(),
            type_id: TypeId::none(),
        }
    }
}

impl<L, S, SD> StateCreation<L, S, SD> {
    /// Configures the lifetime of a [`StateCreation`] builder
    ///
    /// This is a mandatory option and must be configured before a state can be created.
    #[must_use]
    pub fn lifetime(self, lifetime: CreatableStateLifetime) -> StateCreation<CreatableStateLifetime, S, SD> {
        StateCreation {
            lifetime,

            scope: self.scope,
            security_descriptor: self.security_descriptor,
            maximum_state_size: self.maximum_state_size,
            type_id: self.type_id,
        }
    }

    /// Configures the scope of a [`StateCreation`] builder
    ///
    /// This is a mandatory option and must be configured before a state can be created.
    #[must_use]
    pub fn scope(self, scope: DataScope) -> StateCreation<L, DataScope, SD> {
        StateCreation {
            scope,

            lifetime: self.lifetime,
            maximum_state_size: self.maximum_state_size,
            security_descriptor: self.security_descriptor,
            type_id: self.type_id,
        }
    }

    /// Configures the maximum state size of a [`StateCreation`] builder
    ///
    /// If this is not configured, it defaults to `0x1000` (4 KB), which is the absolute maximum size of a state.
    #[must_use]
    pub fn maximum_state_size(self, maximum_state_size: usize) -> StateCreation<L, S, SD> {
        StateCreation {
            maximum_state_size: Some(maximum_state_size),
            ..self
        }
    }

    /// Configures the security descriptor of a [`StateCreation`] builder
    ///
    /// If this is not configured, it defaults to [`BoxedSecurityDescriptor::create_everyone_generic_all`].
    #[must_use]
    pub fn security_descriptor<NewSD>(self, security_descriptor: NewSD) -> StateCreation<L, S, NewSD>
    where
        NewSD: Borrow<SecurityDescriptor>,
    {
        StateCreation {
            security_descriptor,

            lifetime: self.lifetime,
            maximum_state_size: self.maximum_state_size,
            scope: self.scope,
            type_id: self.type_id,
        }
    }

    /// Configures the type id of a [`StateCreation`] builder
    ///
    /// If this is not configured, it defaults to no type id.
    #[must_use]
    pub fn type_id(self, type_id: impl Into<GUID>) -> StateCreation<L, S, SD> {
        StateCreation {
            type_id: type_id.into().into(),
            ..self
        }
    }
}

impl<SD> StateCreation<CreatableStateLifetime, DataScope, SD>
where
    SD: TryIntoSecurityDescriptor,
{
    /// Creates an [`OwnedState<T>`] from this [`StateCreation`]
    ///
    /// Note that the state will be deleted when the returned [`OwnedState<T>`] is dropped. You can avoid this by
    /// calling [`StateCreation::create_static`] instead, which returns a statically borrowed state.
    ///
    /// This method is only available once [`StateCreation::lifetime`] and [`StateCreation::scope`] have been called.
    ///
    /// # Errors
    /// Returns an error if creating the state fails
    pub fn create_owned<T>(self) -> io::Result<OwnedState<T>>
    where
        T: ?Sized,
    {
        self.create_raw().map(OwnedState::from_raw)
    }

    /// Creates a state from this [`StateCreation`], returning a [`BorrowedState<'static, T>`](BorrowedState)
    ///
    /// This is equivalent to creating an owned state and immediately leaking it:
    /// ```
    /// # use wnf::{BorrowedState, CreatableStateLifetime, DataScope, StateCreation};
    /// #
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let state: BorrowedState<'static, u32> = StateCreation::new()
    ///     .lifetime(CreatableStateLifetime::Temporary)
    ///     .scope(DataScope::Machine)
    ///     .create_owned()?
    ///     .leak();
    /// # Ok(()) }
    /// ```
    ///
    /// Note that since you only obtain a statically borrowed state, it will not be deleted automatically. If that is
    /// not the desired behavior, call [`StateCreation::create_owned`] instead, which returns an owned state.
    ///
    /// This method is only available once [`StateCreation::lifetime`] and [`StateCreation::scope`] have been called.
    ///
    /// # Errors
    /// Returns an error if creating the state fails
    pub fn create_static<T>(self) -> io::Result<BorrowedState<'static, T>>
    where
        T: ?Sized,
    {
        self.create_raw().map(BorrowedState::from_raw)
    }

    /// Creates a [`RawState<T>`] from this [`StateCreation`]
    fn create_raw<T>(self) -> io::Result<RawState<T>>
    where
        T: ?Sized,
    {
        RawState::create(
            self.lifetime.into(),
            self.scope,
            self.lifetime.persist_data(),
            self.type_id,
            self.maximum_state_size.unwrap_or(MAXIMUM_STATE_SIZE),
            self.security_descriptor.try_into_security_descriptor()?,
        )
    }
}

impl<T> OwnedState<T>
where
    T: ?Sized,
{
    /// Creates an [`OwnedState<T>`] with temporary lifetime and machine scope
    ///
    /// This is a convenience method for quickly creating a state, e.g. for testing purposes.
    /// For more precise control over the created state, use the [`StateCreation`] builder.
    ///
    /// Note that a newly created state is initialized with data of size zero. This means that unless the data type `T`
    /// is zero-sized or a slice type, you need to update the state data with a value of type `T` before querying it
    /// for the first time.
    ///
    /// # Errors
    /// Returns an error if creating the state fails
    pub fn create_temporary() -> io::Result<Self> {
        StateCreation::new()
            .lifetime(CreatableStateLifetime::Temporary)
            .scope(DataScope::Machine)
            .create_owned()
    }

    /// Deletes this state
    ///
    /// Note that an [`OwnedState<T>`] will be deleted automatically when it is dropped, so calling this method is
    /// usually not necessary. It is useful, however, if you want to handle errors.
    ///
    /// # Errors
    /// Returns an error if deleting the state fails
    pub fn delete(self) -> io::Result<()> {
        self.into_raw().delete()
    }
}

impl<T> BorrowedState<'static, T>
where
    T: ?Sized,
{
    /// Creates a [`BorrowedState<'static, T>`](BorrowedState::create_temporary) with temporary lifetime and machine
    /// scope
    ///
    /// This is a convenience method for quickly creating a state, e.g. for testing purposes.
    /// For more precise control over the created state, use the [`StateCreation`] builder.
    ///
    /// Note that a newly created state is initialized with data of size zero. This means that unless the data type `T`
    /// is zero-sized or a slice type, you need to update the state data with a value of type `T` before querying it
    /// for the first time.
    ///
    /// # Errors
    /// Returns an error if creating the state fails
    pub fn create_temporary() -> io::Result<Self> {
        StateCreation::new()
            .lifetime(CreatableStateLifetime::Temporary)
            .scope(DataScope::Machine)
            .create_static()
    }
}

impl<T> BorrowedState<'_, T>
where
    T: ?Sized,
{
    /// Deletes this state
    ///
    /// Returns an error if deleting the state fails
    pub fn delete(self) -> io::Result<()> {
        self.raw.delete()
    }
}

impl<T> RawState<T>
where
    T: ?Sized,
{
    /// Creates a state
    fn create(
        name_lifetime: StateLifetime,
        data_scope: DataScope,
        persist_data: bool,
        type_id: TypeId,
        maximum_state_size: usize,
        security_descriptor: impl Borrow<SecurityDescriptor>,
    ) -> io::Result<Self> {
        let mut opaque_value = 0;

        let name_lifetime = name_lifetime as u32;
        let data_scope = data_scope as u32;
        let persist_data: u8 = persist_data.into();
        let maximum_state_size = maximum_state_size as u32;

        // SAFETY:
        // - The pointer in the first argument is valid for writes of `u64` because it comes from a live mutable
        //   reference
        // - The pointer in the fifth argument is either a null pointer or points to a valid `GUID` by the guarantees of
        //   `TypeId::as_ptr`
        // - The pointer in the seventh argument points to a valid security descriptor because it comes from a live
        //   reference
        let result = unsafe {
            ntapi::NtCreateWnfStateName(
                &mut opaque_value,
                name_lifetime,
                data_scope,
                persist_data,
                type_id.as_ptr(),
                maximum_state_size,
                security_descriptor.borrow().as_ptr(),
            )
        };

        if result.is_ok() {
            let state_name = StateName::from_opaque_value(opaque_value);

            debug!(
                target: ntapi::TRACING_TARGET,
                ?result,
                input.name_lifetime = name_lifetime,
                input.data_scope = data_scope,
                input.persist_data = persist_data,
                input.type_id = %type_id,
                input.maximum_state_size = maximum_state_size,
                output.state_name = %state_name,
                "NtCreateWnfStateName",
            );

            Ok(Self::from_state_name_and_type_id(state_name, type_id))
        } else {
            debug!(
                target: ntapi::TRACING_TARGET,
                ?result,
                input.name_lifetime = name_lifetime,
                input.data_scope = data_scope,
                input.persist_data = persist_data,
                input.type_id = %type_id,
                input.maximum_state_size = maximum_state_size,
                "NtCreateWnfStateName",
            );

            Err(io::Error::from_raw_os_error(result.0))
        }
    }

    /// Deletes this state
    pub(crate) fn delete(self) -> io::Result<()> {
        // SAFETY:
        // The pointer points to a valid `u64` because it comes from a live reference
        let result = unsafe { ntapi::NtDeleteWnfStateName(&self.state_name.opaque_value()) };

        debug!(
            target: ntapi::TRACING_TARGET,
            ?result,
            input.state_name = %self.state_name,
            "NtDeleteWnfStateName",
        );

        result.ok()?;
        Ok(())
    }
}

/// Making [`TryIntoSecurityDescriptor`] a sealed trait
mod private {
    use super::*;

    pub trait Sealed {}

    impl<SD> Sealed for SD where SD: Borrow<SecurityDescriptor> {}
    impl Sealed for UnspecifiedSecurityDescriptor {}
}