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
use nu_engine::CallExt;
use nu_protocol::{
    ast::Call,
    engine::{Command, EngineState, Stack},
    Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError,
    Signature, Span, Spanned, SyntaxShape, Type, Value,
};
use winreg::{enums::*, RegKey};

#[derive(Clone)]
pub struct RegistryQuery;

struct RegistryQueryArgs {
    hkcr: bool,
    hkcu: bool,
    hklm: bool,
    hku: bool,
    hkpd: bool,
    hkpt: bool,
    hkpnls: bool,
    hkcc: bool,
    hkdd: bool,
    hkculs: bool,
    key: String,
}

impl Command for RegistryQuery {
    fn name(&self) -> &str {
        "registry query"
    }

    fn signature(&self) -> Signature {
        Signature::build("registry query")
            .input_output_types(vec![(Type::Nothing, Type::Any)])
            .switch("hkcr", "query the hkey_classes_root hive", None)
            .switch("hkcu", "query the hkey_current_user hive", None)
            .switch("hklm", "query the hkey_local_machine hive", None)
            .switch("hku", "query the hkey_users hive", None)
            .switch("hkpd", "query the hkey_performance_data hive", None)
            .switch("hkpt", "query the hkey_performance_text hive", None)
            .switch("hkpnls", "query the hkey_performance_nls_text hive", None)
            .switch("hkcc", "query the hkey_current_config hive", None)
            .switch("hkdd", "query the hkey_dyn_data hive", None)
            .switch(
                "hkculs",
                "query the hkey_current_user_local_settings hive",
                None,
            )
            .required("key", SyntaxShape::String, "registry key to query")
            .optional(
                "value",
                SyntaxShape::String,
                "optionally supply a registry value to query",
            )
            .category(Category::System)
    }

    fn usage(&self) -> &str {
        "Query the Windows registry."
    }

    fn extra_usage(&self) -> &str {
        "Currently supported only on Windows systems."
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        registry_query(engine_state, stack, call)
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Query the HKEY_CURRENT_USER hive",
                example: "registry query --hkcu environment",
                result: None,
            },
            Example {
                description: "Query the HKEY_LOCAL_MACHINE hive",
                example: r"registry query --hklm 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'",
                result: None,
            },
        ]
    }
}

fn registry_query(
    engine_state: &EngineState,
    stack: &mut Stack,
    call: &Call,
) -> Result<PipelineData, ShellError> {
    let call_span = call.head;

    let registry_key: Spanned<String> = call.req(engine_state, stack, 0)?;
    let registry_key_span = &registry_key.clone().span;
    let registry_value: Option<Spanned<String>> = call.opt(engine_state, stack, 1)?;

    let reg_params = RegistryQueryArgs {
        hkcr: call.has_flag("hkcr"),
        hkcu: call.has_flag("hkcu"),
        hklm: call.has_flag("hklm"),
        hku: call.has_flag("hku"),
        hkpd: call.has_flag("hkpd"),
        hkpt: call.has_flag("hkpt"),
        hkpnls: call.has_flag("hkpnls"),
        hkcc: call.has_flag("hkcc"),
        hkdd: call.has_flag("hkdd"),
        hkculs: call.has_flag("hkculs"),
        key: registry_key.item,
    };

    let reg_key = get_reg_key(reg_params, call_span)?;

    if registry_value.is_none() {
        let mut reg_values = vec![];
        for (name, val) in reg_key.enum_values().flatten() {
            let (nu_value, reg_type) = reg_value_to_nu_value(val, call_span);
            reg_values.push(Value::Record {
                cols: vec!["name".to_string(), "value".to_string(), "type".to_string()],
                vals: vec![
                    Value::string(name, call_span),
                    nu_value,
                    Value::string(format!("{:?}", reg_type), call_span),
                ],
                span: *registry_key_span,
            })
        }
        Ok(reg_values.into_pipeline_data(engine_state.ctrlc.clone()))
    } else {
        match registry_value {
            Some(value) => {
                let reg_value = reg_key.get_raw_value(value.item.as_str());
                match reg_value {
                    Ok(val) => {
                        let (nu_value, reg_type) = reg_value_to_nu_value(val, call_span);
                        Ok(Value::Record {
                            cols: vec!["name".to_string(), "value".to_string(), "type".to_string()],
                            vals: vec![
                                Value::string(value.item, call_span),
                                nu_value,
                                Value::string(format!("{:?}", reg_type), call_span),
                            ],
                            span: value.span,
                        }
                        .into_pipeline_data())
                    }
                    Err(_) => Err(ShellError::GenericError(
                        "Unable to find registry key/value".to_string(),
                        format!("Registry value: {} was not found", value.item),
                        Some(value.span),
                        None,
                        Vec::new(),
                    )),
                }
            }
            None => Ok(Value::nothing(call_span).into_pipeline_data()),
        }
    }
}

fn get_reg_key(reg_params: RegistryQueryArgs, call_span: Span) -> Result<RegKey, ShellError> {
    let mut key_count = 0;
    let registry_key = if reg_params.hkcr {
        key_count += 1;
        RegKey::predef(HKEY_CLASSES_ROOT).open_subkey(reg_params.key)?
    } else if reg_params.hkcu {
        key_count += 1;
        RegKey::predef(HKEY_CURRENT_USER).open_subkey(reg_params.key)?
    } else if reg_params.hklm {
        key_count += 1;
        RegKey::predef(HKEY_LOCAL_MACHINE).open_subkey(reg_params.key)?
    } else if reg_params.hku {
        key_count += 1;
        RegKey::predef(HKEY_USERS).open_subkey(reg_params.key)?
    } else if reg_params.hkpd {
        key_count += 1;
        RegKey::predef(HKEY_PERFORMANCE_DATA).open_subkey(reg_params.key)?
    } else if reg_params.hkpt {
        key_count += 1;
        RegKey::predef(HKEY_PERFORMANCE_TEXT).open_subkey(reg_params.key)?
    } else if reg_params.hkpnls {
        key_count += 1;
        RegKey::predef(HKEY_PERFORMANCE_NLSTEXT).open_subkey(reg_params.key)?
    } else if reg_params.hkcc {
        key_count += 1;
        RegKey::predef(HKEY_CURRENT_CONFIG).open_subkey(reg_params.key)?
    } else if reg_params.hkdd {
        key_count += 1;
        RegKey::predef(HKEY_DYN_DATA).open_subkey(reg_params.key)?
    } else if reg_params.hkculs {
        key_count += 1;
        RegKey::predef(HKEY_CURRENT_USER_LOCAL_SETTINGS).open_subkey(reg_params.key)?
    } else {
        RegKey::predef(HKEY_CURRENT_USER).open_subkey(reg_params.key)?
    };

    if key_count > 1 {
        return Err(ShellError::GenericError(
            "Only one registry key can be specified".into(),
            "Only one registry key can be specified".into(),
            Some(call_span),
            None,
            Vec::new(),
        ));
    }
    Ok(registry_key)
}

fn clean_string(string: &str) -> String {
    string
        .trim_start_matches('"')
        .trim_end_matches('"')
        .replace("\\\\", "\\")
}
fn reg_value_to_nu_value(
    reg_value: winreg::RegValue,
    call_span: Span,
) -> (nu_protocol::Value, winreg::enums::RegType) {
    match reg_value.vtype {
        REG_NONE => (Value::nothing(call_span), reg_value.vtype),
        REG_SZ => (
            Value::string(clean_string(&reg_value.to_string()), call_span),
            reg_value.vtype,
        ),
        REG_EXPAND_SZ => (
            Value::string(clean_string(&reg_value.to_string()), call_span),
            reg_value.vtype,
        ),
        REG_BINARY => (Value::binary(reg_value.bytes, call_span), reg_value.vtype),
        REG_DWORD => (
            Value::int(
                unsafe { *(reg_value.bytes.as_ptr() as *const u32) } as i64,
                call_span,
            ),
            reg_value.vtype,
        ),
        REG_DWORD_BIG_ENDIAN => (
            Value::int(
                unsafe { *(reg_value.bytes.as_ptr() as *const u32) } as i64,
                call_span,
            ),
            reg_value.vtype,
        ),
        REG_LINK => (
            Value::string(clean_string(&reg_value.to_string()), call_span),
            reg_value.vtype,
        ),
        REG_MULTI_SZ => (
            Value::string(clean_string(&reg_value.to_string()), call_span),
            reg_value.vtype,
        ),
        REG_RESOURCE_LIST => (
            Value::string(clean_string(&reg_value.to_string()), call_span),
            reg_value.vtype,
        ),
        REG_FULL_RESOURCE_DESCRIPTOR => (
            Value::string(clean_string(&reg_value.to_string()), call_span),
            reg_value.vtype,
        ),
        REG_RESOURCE_REQUIREMENTS_LIST => (
            Value::string(clean_string(&reg_value.to_string()), call_span),
            reg_value.vtype,
        ),
        REG_QWORD => (
            Value::int(
                unsafe { *(reg_value.bytes.as_ptr() as *const u32) } as i64,
                call_span,
            ),
            reg_value.vtype,
        ),
    }
}