netget-tool 0.3.4

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
//! SMB protocol actions implementation

use crate::llm::actions::{
    protocol_trait::{ActionResult, Protocol, Server},
    ActionDefinition, Parameter,
};
use crate::protocol::log_template::LogTemplate;
use crate::protocol::EventType;
use crate::state::app_state::AppState;
use anyhow::{Context, Result};
use serde_json::json;
use std::sync::LazyLock;

/// SMB protocol action handler
pub struct SmbProtocol;

impl Default for SmbProtocol {
    fn default() -> Self {
        Self
    }
}

impl SmbProtocol {
    pub fn new() -> Self {
        Self
    }
}

// Implement Protocol trait (common functionality)
impl Protocol for SmbProtocol {
    /// The three read deadlines, and nothing else. Each defaults to the constant the server
    /// uses, so the form, `get_protocol_docs` and the model all show the number in force.
    fn get_startup_parameters(&self) -> Vec<crate::llm::actions::ParameterDefinition> {
        vec![
            crate::llm::actions::ParameterDefinition {
                name: "first_byte_timeout_secs".to_string(),
                type_hint: "integer".to_string(),
                description: "Seconds a connected peer may send nothing before any session is \
                              admitted; the server then closes it without a reply. SMB2 is \
                              client-speaks-first and every real client sends NEGOTIATE as it \
                              connects."
                    .to_string(),
                required: false,
                example: json!(30),
                default: Some(json!(super::FIRST_MESSAGE_READ_TIMEOUT.as_secs())),
            },
            crate::llm::actions::ParameterDefinition {
                name: "idle_timeout_secs".to_string(),
                type_hint: "integer".to_string(),
                description: "Seconds a peer holding an admitted session may send nothing \
                              before the server closes it. Default 900, Windows' \
                              `autodisconnect`: a mounted share with no I/O is idle for long \
                              stretches."
                    .to_string(),
                required: false,
                example: json!(900),
                default: Some(json!(super::IDLE_BETWEEN_MESSAGES_TIMEOUT.as_secs())),
            },
            crate::llm::actions::ParameterDefinition {
                name: "body_timeout_secs".to_string(),
                type_hint: "integer".to_string(),
                description: "Seconds a peer may stall part-way through a message whose \
                              length its Direct TCP header has already announced."
                    .to_string(),
                required: false,
                example: json!(30),
                default: Some(json!(super::BODY_READ_TIMEOUT.as_secs())),
            },
        ]
    }
    fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
        vec![disconnect_client_action()]
    }
    /// SMB raises exactly one event, and it must be declared here as well as emitted.
    ///
    /// This was missing, so the trait default applied and returned an empty vec: SMB_OPERATION_EVENT
    /// was built and dispatched at runtime, but invisible to anything that walks the registry.
    /// `tests/event_action_declarations_test.rs` audits every registered protocol's events and
    /// therefore audited none of SMB's, and `tests/mock_event_ids_test.rs` reported all nineteen
    /// of this protocol's own mock rules as naming an unknown event.
    fn get_event_types(&self) -> Vec<crate::protocol::EventType> {
        vec![SMB_OPERATION_EVENT.clone()]
    }

    fn get_sync_actions(&self) -> Vec<ActionDefinition> {
        // Every action listed here has an executor branch in src/server/smb/mod.rs and is
        // attached to SMB_OPERATION_EVENT below, so the model can both see it and have it
        // take effect.
        //
        // `smb_delete_file` and `smb_delete_directory` used to be listed and were removed:
        // SMB2 has no DELETE command. A client deletes by opening the file and issuing
        // SET_INFO with FileDispositionInformation (MS-SMB2 2.2.39 / 2.2.21), and this
        // server does not implement SET_INFO at all - the command falls through to the
        // "Unknown SMB2 command" arm. Advertising a delete action the server can never be
        // asked to perform only gave the model a response that did nothing.
        vec![
            smb_auth_success_action(),
            smb_auth_deny_action(),
            smb_list_directory_action(),
            smb_read_file_action(),
            smb_write_file_action(),
            smb_get_file_info_action(),
            smb_create_file_action(),
            smb_create_directory_action(),
        ]
    }
    fn protocol_name(&self) -> &'static str {
        "SMB"
    }
    fn stack_name(&self) -> &'static str {
        "ETH>IP>TCP>SMB"
    }
    fn keywords(&self) -> Vec<&'static str> {
        vec!["smb", "cifs"]
    }
    fn metadata(&self) -> crate::protocol::metadata::ProtocolMetadataV2 {
        use crate::protocol::metadata::{DevelopmentState, ProtocolMetadataV2};

        ProtocolMetadataV2::builder()
            // Stable on the six conditions in the root CLAUDE.md, each re-derived against
            // source on 30 September 2026; "Maturity: the six conditions" at the foot of
            // src/server/smb/CLAUDE.md says what each rests on and what the rating does not
            // cover.
            .state(DevelopmentState::Stable)
            .well_known_port(445)
            // The Direct TCP frame length is the one peer-chosen size this server allocates
            // for; a frame over MAX_MESSAGE_BYTES is refused after its 64-byte header, and a
            // WRITE over the negotiated MaxWriteSize inside a legal frame is refused before the
            // model. Both tested in tests/server/smb/inbound_limit_test.rs; every other bound
            // in tests/server/smb/bounds_test.rs.
            .max_inbound_bytes(crate::server::smb::MAX_MESSAGE_BYTES)
            .implementation(
                "Hand-written SMB2 (dialects 0x0202 and 0x0210) over Direct TCP (MS-SMB2 2.1), \
                 with compound requests. SESSION_SETUP walks SPNEGO/NTLMSSP so real clients \
                 finish the login; no password is verified and no session is signed. Every \
                 request is parsed by a pure function in wire.rs.",
            )
            .llm_control(
                "Authentication (allow/deny, on the user name the NTLMSSP AUTHENTICATE carries), \
                 directory listings, file metadata, file content on read, file-vs-directory on \
                 create, deletes (a create carrying delete_on_close), and write authorisation. \
                 File payloads carry an explicit `encoding` field (utf8/base64/hex) in both \
                 directions, so binary content survives a read and a written binary payload is \
                 shown to the model losslessly.",
            )
            .e2e_testing(
                "Condition 1: two independent real clients against a mocked model \
                 (tests/server/smb/real_client_test.rs), neither linked by the server, both \
                 failing rather than skipping when absent, counted verb by verb from the \
                 recorded bytes. Samba's smbclient 4.24 logs in anonymously over \
                 SPNEGO/NTLMSSP and runs ls, get of a 70 000-byte binary file, put of a \
                 100 000-byte one in two WRITEs, mkdir, rm (a delete-on-close open the model is \
                 told about), echo, tdis and logoff; smbprotocol 1.17 logs in as a named guest \
                 over bare NTLMSSP and runs listdir, read, stat (a related compound of CREATE, \
                 five QUERY_INFOs and CLOSE), a two-WRITE upload, FLUSH, mkdir, echo, \
                 TREE_DISCONNECT and LOGOFF. Every answered verb is driven by both clients \
                 except FLUSH (smbclient has no command for it). The model's write events must \
                 reassemble to exactly the uploaded bytes. Condition 2: both sessions and a raw \
                 session of every command the server answers (header_layout_test.rs) read \
                 clean in Wireshark's nbss/smb2 dissectors. Condition 3: fuzz targets \
                 smb2_request and ntlmssp_token, 300s each clean against a corpus with a \
                 nested-DER depth bomb and a 14 000-request compound chain. Condition 4: every \
                 declared bound tested at and past the bound and verified by removal \
                 (bounds_test.rs, inbound_limit_test.rs). failure_modes_test.rs asserts all \
                 eighteen refusal paths on the wire and in the log. Not verified against \
                 Windows Explorer, mount_smbfs or mount.cifs.",
            )
            .notes(
                "Stable covers the surface implemented, a small subset of MS-SMB2: SMB \
                 2.0.2/2.1 only; no SMB 3.x, signing, encryption, oplocks, leases, durable \
                 handles, DFS or named pipes (IPC$ connects, every open on it is refused). \
                 Sessions are guest or null: NTLMSSP is walked, never verified. No SET_INFO (so \
                 no rename, truncate or set-times; delete only through FILE_DELETE_ON_CLOSE), \
                 no LOCK, no CHANGE_NOTIFY, IOCTL refused, CANCEL unanswered. The volume size \
                 is a fixed report, not a measurement. Per connection: 16 sessions, 64 trees, \
                 1024 open handles, 32 acted-on requests per compound. Adjacent operations \
                 share no state beyond the per-connection session, tree and handle tables: the \
                 model is the filesystem.",
            )
            .build()
    }
    fn description(&self) -> &'static str {
        "SMB/CIFS file server"
    }
    fn example_prompt(&self) -> &'static str {
        "Start an SMB/CIFS file server on port 8445"
    }
    fn group_name(&self) -> &'static str {
        "Web & File"
    }

    fn get_startup_examples(&self) -> crate::llm::actions::StartupExamples {
        use crate::llm::actions::StartupExamples;

        // Deterministic: accept every session on this share, no LLM call.
        let script = r#"import json, sys
data = json.load(sys.stdin)
event = data["event"]
if data["event_type_id"] == "smb_operation":
    actions = [{"type": "smb_auth_success",
                "username": event.get("username", "guest")}]
else:
    actions = []
print(json.dumps({"actions": actions}))"#;

        StartupExamples::new(
            // LLM mode
            json!({
                "type": "open_server",
                "port": 445,
                "base_stack": "smb",
                "instruction": "SMB file server. Accept all guest connections. Provide /documents directory with sample files. Return file content on reads."
            }),
            // Script mode
            json!({
                "type": "open_server",
                "port": 445,
                "base_stack": "smb",
                "event_handlers": [{
                    "event_pattern": "smb_operation",
                    "handler": {
                        "type": "script",
                        "language": "python",
                        "code": script
                    }
                }]
            }),
            // Static mode
            json!({
                "type": "open_server",
                "port": 445,
                "base_stack": "smb",
                "event_handlers": [{
                    "event_pattern": "smb_operation",
                    "handler": {
                        "type": "static",
                        "actions": [{
                            "type": "smb_auth_success",
                            "username": "guest"
                        }]
                    }
                }]
            }),
        )
    }
}

// Implement Server trait (server-specific functionality)
impl Server for SmbProtocol {
    fn spawn(
        &self,
        ctx: crate::protocol::SpawnContext,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = anyhow::Result<std::net::SocketAddr>> + Send>,
    > {
        Box::pin(async move {
            use crate::server::smb::{Deadlines, SmbServer};
            // Propagate, never unwrap: these values come from the model or an MCP caller. A
            // zero deadline would close every connection before it could speak, so it is
            // refused rather than honoured.
            let secs = |name: &str, default: std::time::Duration| -> Result<std::time::Duration> {
                let value = match ctx.startup_params.as_ref() {
                    Some(p) => p.get_optional_u64(name)?,
                    None => None,
                };
                match value {
                    None => Ok(default),
                    Some(0) => anyhow::bail!("{name} must be at least 1 second, got 0"),
                    Some(n) => Ok(std::time::Duration::from_secs(n)),
                }
            };
            let defaults = Deadlines::default();
            let deadlines = Deadlines {
                first_message: secs("first_byte_timeout_secs", defaults.first_message)?,
                idle: secs("idle_timeout_secs", defaults.idle)?,
                body: secs("body_timeout_secs", defaults.body)?,
            };
            SmbServer::spawn_with_llm_actions(
                ctx.legacy_listen_addr(),
                ctx.llm_client,
                ctx.state,
                ctx.status_tx,
                ctx.server_id,
                deadlines,
            )
            .await
        })
    }
    fn execute_action(&self, action: serde_json::Value) -> Result<ActionResult> {
        let action_type = action
            .get("type")
            .and_then(|v| v.as_str())
            .context("Missing 'type' field in action")?;

        // `close_connection` is the one action this executor resolves itself, and it is
        // deliberately NOT advertised in `get_sync_actions()` / the event's action list —
        // adding it there would change the model's tool list. It exists for the peer handle:
        // the dashboard's "[ disconnect this peer ]" injects a bare
        // `{"type": "close_connection"}` whatever the protocol calls its own close verb, and
        // `server::peer_support` turns `ActionResult::CloseConnection` into the half-close.
        // Without this arm the button would come back "executed" having done nothing, because
        // the fall-through below answers every name with a `Custom` result.
        if action_type == "close_connection" {
            return Ok(ActionResult::CloseConnection);
        }

        // Return Custom result with the action data for SMB server to handle
        Ok(ActionResult::Custom {
            name: action_type.to_string(),
            data: action,
        })
    }
}

// Event type for SMB operations
pub static SMB_OPERATION_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "smb_operation",
        "An SMB2 client asked for a filesystem operation. You are the filesystem: nothing is \
         read from or written to disk, so invent a consistent virtual tree and keep it in memory \
         across operations. Answer with the action matching 'operation': session_setup -> \
         smb_auth_success or smb_auth_deny, create -> smb_create_file or smb_create_directory \
         (which one decides whether the client is told the handle is a directory; a create \
         with delete_on_close true is the client deleting that path, and answering either \
         action approves the delete), read -> \
         smb_read_file, write -> smb_write_file (the write is REFUSED with STATUS_ACCESS_DENIED \
         unless you return it), query_info -> smb_get_file_info, query_directory -> \
         smb_list_directory.",
        json!({
            "type": "smb_read_file",
            "path": "/documents/file.txt",
            "content": "Sample file content",
            "encoding": "utf8"
        }),
    )
    // Every action here has an executor branch in src/server/smb/mod.rs. `call_llm` builds the
    // model's tool list from this list, not from get_sync_actions(), so anything missing here is
    // invisible to the model and anything here without an executor branch is a no-op it can emit.
    // Keep the two in sync.
    .with_actions(vec![
        smb_auth_success_action(),
        smb_auth_deny_action(),
        smb_create_file_action(),
        smb_create_directory_action(),
        smb_read_file_action(),
        smb_write_file_action(),
        smb_get_file_info_action(),
        smb_list_directory_action(),
    ])
    .with_parameters(vec![
        Parameter {
            name: "operation".to_string(),
            type_hint: "string".to_string(),
            description: "Which request this is: \"session_setup\" (authentication), \"create\" \
                          (open), \"read\", \"write\", \"query_info\" (stat) or \
                          \"query_directory\" (list)"
                .to_string(),
            required: true,
        },
        Parameter {
            name: "path".to_string(),
            type_hint: "string".to_string(),
            description: "The file or directory path being accessed".to_string(),
            required: false,
        },
        Parameter {
            name: "data".to_string(),
            type_hint: "string".to_string(),
            description: "write only: the bytes the client wrote, rendered according to the \
                          'encoding' field of this event. Absent for every other operation."
                .to_string(),
            required: false,
        },
        Parameter {
            name: "encoding".to_string(),
            type_hint: "string".to_string(),
            description: "write only: how to read 'data'. \"utf8\" means 'data' is the written \
                          bytes as literal text; \"base64\" means 'data' is the written bytes \
                          base64-encoded, used whenever they are not all printable ASCII. To \
                          hand the same bytes back on a later read, pass this 'data' and this \
                          'encoding' straight into smb_read_file's 'content' and 'encoding'."
                .to_string(),
            required: false,
        },
        Parameter {
            name: "delete_on_close".to_string(),
            type_hint: "boolean".to_string(),
            description: "create only, and present only when true: the client opened the path \
                          in order to delete it (SMB2 has no DELETE command; `rm` opens with \
                          delete-on-close and closes). smb_create_file or smb_create_directory \
                          approves the delete, so forget the path; answering neither refuses it \
                          with STATUS_ACCESS_DENIED."
                .to_string(),
            required: false,
        },
    ])
    .with_log_template(
        LogTemplate::new()
            .with_info("SMB {operation} {path}")
            .with_debug("SMB {operation}: {path}")
            .with_trace("SMB: {json_pretty(.)}"),
    )
});

// ============================================================================
// Payload encoding
//
// SMB carries file *contents*, which are routinely not text. Both directions
// therefore carry an explicit `encoding` field next to the payload string, and
// there is deliberately no sniffing: "SGVsbG8=" is simultaneously valid text and
// valid base64, and only the sender knows which it means. This is the same shape
// as `send_tcp_data`'s `encoding` field (d70bb5b5); the defect fixed here was
// that `smb_read_file.content` was documented as "base64 encoded for binary"
// while the executor did `.as_bytes()`, so a model that followed the
// documentation put literal base64 ASCII into the file.
// ============================================================================

/// Turn an outbound payload string into the exact bytes the server writes into an
/// SMB2 response, honouring the action's optional `encoding` field.
///
/// - absent or `"utf8"`: the string's UTF-8 bytes, verbatim (default, backwards compatible)
/// - `"base64"`: standard base64, so `"SGVsbG8="` yields the 5 bytes `Hello`
/// - `"hex"`: two hex digits per byte, so `"48656c6c6f"` yields the same 5 bytes
pub fn decode_smb_payload(payload: &str, encoding: Option<&str>) -> Result<Vec<u8>> {
    use base64::Engine as _;

    match encoding.unwrap_or("utf8") {
        "utf8" | "text" => Ok(payload.as_bytes().to_vec()),
        "base64" => {
            // Models frequently wrap long base64 across lines; tolerate whitespace.
            let cleaned: String = payload
                .chars()
                .filter(|c| !c.is_ascii_whitespace())
                .collect();
            base64::engine::general_purpose::STANDARD
                .decode(&cleaned)
                .map_err(|e| {
                    anyhow::anyhow!(
                        "Invalid base64 in payload ({payload:?}): {e}. To send this string as \
                         literal text instead, omit 'encoding' or set it to \"utf8\"."
                    )
                })
        }
        "hex" => {
            let cleaned: String = payload
                .chars()
                .filter(|c| !c.is_ascii_whitespace() && *c != ':')
                .collect();
            let cleaned = cleaned.strip_prefix("0x").unwrap_or(&cleaned);
            if cleaned.len() % 2 != 0 {
                return Err(anyhow::anyhow!(
                    "Invalid hex in payload: expected an even number of hex digits, got {} \
                     ({payload:?}). Each byte is two hex digits, e.g. \"48656c6c6f\" = \"Hello\".",
                    cleaned.len()
                ));
            }
            hex::decode(cleaned).map_err(|e| {
                anyhow::anyhow!(
                    "Invalid hex in payload ({payload:?}): {e}. Use only 0-9/a-f, two digits per \
                     byte. To send this string as literal text instead, omit 'encoding' or set \
                     it to \"utf8\"."
                )
            })
        }
        other => Err(anyhow::anyhow!(
            "Invalid 'encoding' value {other:?}. Valid values are \"utf8\" (default, the \
             string's characters as-is), \"base64\" and \"hex\"."
        )),
    }
}

/// Render bytes received from the client for the model, together with the `encoding`
/// name that says how to read them back.
///
/// Printable ASCII is passed through as text so ordinary text files stay readable in
/// prompts and logs; anything else is base64-encoded rather than lossily converted.
/// The pair is symmetric with [`decode_smb_payload`]: feeding the returned string and
/// encoding back through it reproduces the original bytes exactly.
pub fn encode_smb_payload(bytes: &[u8]) -> (String, &'static str) {
    use base64::Engine as _;

    if bytes
        .iter()
        .all(|&b| b.is_ascii_graphic() || b.is_ascii_whitespace())
    {
        (String::from_utf8_lossy(bytes).to_string(), "utf8")
    } else {
        (
            base64::engine::general_purpose::STANDARD.encode(bytes),
            "base64",
        )
    }
}

/// Shared `encoding` parameter for every action carrying an outbound payload string.
fn encoding_parameter(payload_field: &str) -> Parameter {
    Parameter {
        name: "encoding".to_string(),
        type_hint: "string".to_string(),
        description: format!(
            "How to turn '{payload_field}' into the bytes the client receives. \"utf8\" (the \
             default when omitted) uses the characters of '{payload_field}' unchanged - use it \
             for text files. \"base64\" decodes '{payload_field}' as standard base64 and \
             \"hex\" as hex digits - use one of those for binary files, e.g. \
             {{\"{payload_field}\": \"SGVsbG8=\", \"encoding\": \"base64\"}} delivers the 5 \
             bytes 'Hello', whereas the same value without \"encoding\" delivers the 8 \
             characters S-G-V-s-b-G-8-=. There is no auto-detection. No other values are \
             accepted"
        ),
        required: false,
    }
}

// Action definitions

fn disconnect_client_action() -> ActionDefinition {
    ActionDefinition {
        name: "disconnect_client".to_string(),
        description: "Disconnect an SMB client".to_string(),
        parameters: vec![Parameter {
            name: "client".to_string(),
            type_hint: "string".to_string(),
            description: "Client address to disconnect".to_string(),
            required: true,
        }],
        example: json!({
            "type": "disconnect_client",
            "client": "192.168.1.100:54321"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("SMB disconnect {client}")
                .with_debug("SMB disconnect_client: {client}"),
        ),
    }
}

fn smb_list_directory_action() -> ActionDefinition {
    ActionDefinition {
        name: "smb_list_directory".to_string(),
        description: "List files in a directory".to_string(),
        parameters: vec![
            Parameter {
                name: "path".to_string(),
                type_hint: "string".to_string(),
                description: "Directory path to list".to_string(),
                required: true,
            },
            Parameter {
                name: "files".to_string(),
                type_hint: "array".to_string(),
                description: "Array of file objects with name, size, is_directory, modified_time"
                    .to_string(),
                required: true,
            },
        ],
        example: json!({
            "type": "smb_list_directory",
            "path": "/documents",
            "files": [
                {
                    "name": "report.pdf",
                    "size": 524288,
                    "is_directory": false,
                    "modified_time": "2025-01-15T10:30:00Z"
                }
            ]
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> SMB DIR {path} ({files_len} files)")
                .with_debug("SMB smb_list_directory: path={path}, {files_len} files"),
        ),
    }
}

fn smb_read_file_action() -> ActionDefinition {
    ActionDefinition {
        name: "smb_read_file".to_string(),
        description: "Answer a 'read' operation with the file's contents. The bytes in 'content' \
                      (interpreted according to 'encoding') become the body of the SMB2 READ \
                      response."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "path".to_string(),
                type_hint: "string".to_string(),
                description: "File path to read".to_string(),
                required: true,
            },
            Parameter {
                name: "content".to_string(),
                type_hint: "string".to_string(),
                description: "File content. Interpreted according to 'encoding': by default the \
                              characters of this string are delivered as-is (UTF-8)."
                    .to_string(),
                required: true,
            },
            encoding_parameter("content"),
        ],
        example: json!({
            "type": "smb_read_file",
            "path": "/documents/file.txt",
            "content": "Hello, World!",
            "encoding": "utf8"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> SMB READ {path}")
                .with_debug("SMB smb_read_file: path={path}"),
        ),
    }
}

fn smb_write_file_action() -> ActionDefinition {
    ActionDefinition {
        name: "smb_write_file".to_string(),
        description: "Accept a 'write' operation. The client has already sent the bytes - they \
                      are in the event's 'data' field - so this action does not carry them back; \
                      it authorises the write and the server answers STATUS_SUCCESS. If you do \
                      NOT return this action for a 'write' operation the write is refused with \
                      STATUS_ACCESS_DENIED, so silence is a denial, not an approval."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "path".to_string(),
                type_hint: "string".to_string(),
                description: "File path being written (echo the event's 'path')".to_string(),
                required: true,
            },
            Parameter {
                name: "bytes_written".to_string(),
                type_hint: "number".to_string(),
                description: "How many bytes to report as written. Omit to report all the bytes \
                              the client sent, which is what a normal filesystem does. A smaller \
                              number tells the client the write was partial."
                    .to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "smb_write_file",
            "path": "/documents/file.txt"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> SMB WRITE OK {path}")
                .with_debug("SMB smb_write_file: path={path}"),
        ),
    }
}

fn smb_get_file_info_action() -> ActionDefinition {
    ActionDefinition {
        name: "smb_get_file_info".to_string(),
        description: "Get file metadata".to_string(),
        parameters: vec![
            Parameter {
                name: "path".to_string(),
                type_hint: "string".to_string(),
                description: "File path".to_string(),
                required: true,
            },
            Parameter {
                name: "size".to_string(),
                type_hint: "number".to_string(),
                description: "File size in bytes".to_string(),
                required: true,
            },
            Parameter {
                name: "is_directory".to_string(),
                type_hint: "boolean".to_string(),
                description: "Whether path is a directory".to_string(),
                required: true,
            },
            Parameter {
                name: "modified_time".to_string(),
                type_hint: "string".to_string(),
                description: "Last modified time (ISO 8601)".to_string(),
                required: true,
            },
        ],
        example: json!({
            "type": "smb_get_file_info",
            "path": "/documents/file.txt",
            "size": 1024,
            "is_directory": false,
            "modified_time": "2025-01-15T10:30:00Z"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> SMB INFO {path}")
                .with_debug("SMB smb_get_file_info: path={path}, size={size}"),
        ),
    }
}

fn smb_create_file_action() -> ActionDefinition {
    ActionDefinition {
        name: "smb_create_file".to_string(),
        description: "Answer a 'create' operation: the path is (or becomes) a regular file. The \
                      handle the client receives is marked FILE_ATTRIBUTE_NORMAL, so the client \
                      will follow up with read/write rather than query_directory. Returning \
                      neither create action is NOT a default: it refuses the open with \
                      STATUS_ACCESS_DENIED, because silence must not become consent for an \
                      admission decision."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "path".to_string(),
                type_hint: "string".to_string(),
                description: "File path being opened or created".to_string(),
                required: true,
            },
            Parameter {
                name: "size".to_string(),
                type_hint: "number".to_string(),
                description: "The file's size in bytes, reported to the client as the open \
                              handle's end of file. Give it whenever the file has content: \
                              some clients read exactly this many bytes and never ask again, \
                              so an open without it reads as an empty file. Omit only for a \
                              file that is empty or being created."
                    .to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "smb_create_file",
            "path": "/documents/report.txt",
            "size": 1024
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> SMB CREATE {path}")
                .with_debug("SMB smb_create_file: path={path}"),
        ),
    }
}

// `smb_delete_file` and `smb_delete_directory` used to be defined here. SMB2 has no DELETE
// command - deletion is SET_INFO/FileDispositionInformation on an open handle - and this
// server does not implement SET_INFO, so neither action could ever have been requested or
// routed. They were removed rather than left advertised.

fn smb_create_directory_action() -> ActionDefinition {
    ActionDefinition {
        name: "smb_create_directory".to_string(),
        description: "Answer a 'create' operation: the path is (or becomes) a directory. The \
                      handle the client receives carries FILE_ATTRIBUTE_DIRECTORY, which is what \
                      makes the client issue query_directory against it instead of read."
            .to_string(),
        parameters: vec![Parameter {
            name: "path".to_string(),
            type_hint: "string".to_string(),
            description: "Directory path being opened or created".to_string(),
            required: true,
        }],
        example: json!({
            "type": "smb_create_directory",
            "path": "/documents/newdir"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> SMB MKDIR {path}")
                .with_debug("SMB smb_create_directory: path={path}"),
        ),
    }
}

fn smb_auth_success_action() -> ActionDefinition {
    ActionDefinition {
        name: "smb_auth_success".to_string(),
        description: "Allow SMB authentication for the user (respond to session_setup event)"
            .to_string(),
        parameters: vec![
            Parameter {
                name: "username".to_string(),
                type_hint: "string".to_string(),
                description: "Username that was authenticated".to_string(),
                required: true,
            },
            Parameter {
                name: "message".to_string(),
                type_hint: "string".to_string(),
                description: "Optional message explaining why auth was allowed".to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "smb_auth_success",
            "username": "alice",
            "message": "User alice authenticated successfully"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> SMB AUTH OK {username}")
                .with_debug("SMB smb_auth_success: user={username}"),
        ),
    }
}

fn smb_auth_deny_action() -> ActionDefinition {
    ActionDefinition {
        name: "smb_auth_deny".to_string(),
        description: "Deny SMB authentication for the user (respond to session_setup event)"
            .to_string(),
        parameters: vec![
            Parameter {
                name: "username".to_string(),
                type_hint: "string".to_string(),
                description: "Username that was denied".to_string(),
                required: true,
            },
            Parameter {
                name: "reason".to_string(),
                type_hint: "string".to_string(),
                description: "Reason for denying authentication".to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "smb_auth_deny",
            "username": "hacker",
            "reason": "User not authorized"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> SMB AUTH DENIED {username}")
                .with_debug("SMB smb_auth_deny: user={username}, reason={reason}"),
        ),
    }
}