@push.rocks/smartnftables 1.2.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/changelog.md +37 -0
  2. package/dist_rust/smartnftables_linux_amd64_musl +0 -0
  3. package/dist_rust/smartnftables_linux_amd64_musl.tsrust-build.json +14 -0
  4. package/dist_rust/smartnftables_linux_arm64_musl +0 -0
  5. package/dist_rust/smartnftables_linux_arm64_musl.tsrust-build.json +14 -0
  6. package/dist_ts/00_commitinfo_data.js +1 -1
  7. package/dist_ts/classes.managednftables.d.ts +21 -0
  8. package/dist_ts/classes.managednftables.js +182 -0
  9. package/dist_ts/index.d.ts +2 -0
  10. package/dist_ts/index.js +2 -1
  11. package/dist_ts/managed.types.d.ts +103 -0
  12. package/dist_ts/managed.types.js +2 -0
  13. package/dist_ts/plugins.d.ts +5 -2
  14. package/dist_ts/plugins.js +6 -3
  15. package/native-notices/crates/block-buffer-0.10.4/license-apache +201 -0
  16. package/native-notices/crates/block-buffer-0.10.4/license-mit +25 -0
  17. package/native-notices/crates/cfg-if-1.0.4/license-apache +201 -0
  18. package/native-notices/crates/cfg-if-1.0.4/license-mit +25 -0
  19. package/native-notices/crates/cpufeatures-0.2.17/license-apache +201 -0
  20. package/native-notices/crates/cpufeatures-0.2.17/license-mit +25 -0
  21. package/native-notices/crates/crypto-common-0.1.7/license-apache +201 -0
  22. package/native-notices/crates/crypto-common-0.1.7/license-mit +25 -0
  23. package/native-notices/crates/digest-0.10.7/license-apache +201 -0
  24. package/native-notices/crates/digest-0.10.7/license-mit +25 -0
  25. package/native-notices/crates/generic-array-0.14.7/license +21 -0
  26. package/native-notices/crates/itoa-1.0.18/license-apache +176 -0
  27. package/native-notices/crates/itoa-1.0.18/license-mit +23 -0
  28. package/native-notices/crates/libc-0.2.189/license-apache +176 -0
  29. package/native-notices/crates/libc-0.2.189/license-mit +25 -0
  30. package/native-notices/crates/memchr-2.8.3/copying +3 -0
  31. package/native-notices/crates/memchr-2.8.3/license-mit +21 -0
  32. package/native-notices/crates/proc-macro2-1.0.107/license-apache +176 -0
  33. package/native-notices/crates/proc-macro2-1.0.107/license-mit +23 -0
  34. package/native-notices/crates/quote-1.0.47/license-apache +176 -0
  35. package/native-notices/crates/quote-1.0.47/license-mit +23 -0
  36. package/native-notices/crates/serde-1.0.229/license-apache +176 -0
  37. package/native-notices/crates/serde-1.0.229/license-mit +23 -0
  38. package/native-notices/crates/serde_core-1.0.229/license-apache +176 -0
  39. package/native-notices/crates/serde_core-1.0.229/license-mit +23 -0
  40. package/native-notices/crates/serde_derive-1.0.229/license-apache +176 -0
  41. package/native-notices/crates/serde_derive-1.0.229/license-mit +23 -0
  42. package/native-notices/crates/serde_json-1.0.151/license-apache +176 -0
  43. package/native-notices/crates/serde_json-1.0.151/license-mit +23 -0
  44. package/native-notices/crates/sha2-0.10.9/license-apache +201 -0
  45. package/native-notices/crates/sha2-0.10.9/license-mit +27 -0
  46. package/native-notices/crates/syn-3.0.5/license-apache +176 -0
  47. package/native-notices/crates/syn-3.0.5/license-mit +23 -0
  48. package/native-notices/crates/typenum-1.20.1/license +1 -0
  49. package/native-notices/crates/typenum-1.20.1/license-apache +201 -0
  50. package/native-notices/crates/typenum-1.20.1/license-mit +21 -0
  51. package/native-notices/crates/unicode-ident-1.0.24/license-apache +176 -0
  52. package/native-notices/crates/unicode-ident-1.0.24/license-mit +23 -0
  53. package/native-notices/crates/unicode-ident-1.0.24/license-unicode +39 -0
  54. package/native-notices/crates/version_check-0.9.5/license-apache +201 -0
  55. package/native-notices/crates/version_check-0.9.5/license-mit +19 -0
  56. package/native-notices/crates/zmij-1.0.23/license-mit +23 -0
  57. package/native-notices/manifest.json +520 -0
  58. package/native-notices/readme.md +36 -0
  59. package/native-notices/rust-standard-library/addr2line-0.25.1-license-apache +201 -0
  60. package/native-notices/rust-standard-library/addr2line-0.25.1-license-mit +25 -0
  61. package/native-notices/rust-standard-library/cargo.lock +522 -0
  62. package/native-notices/rust-standard-library/compiler-builtins-license.txt +275 -0
  63. package/native-notices/rust-standard-library/dependency-notices.html +9072 -0
  64. package/native-notices/rust-standard-library/license-apache +176 -0
  65. package/native-notices/rust-standard-library/license-mit +25 -0
  66. package/native-notices/rust-standard-library/llvm-libunwind-license.txt +311 -0
  67. package/native-notices/rust-standard-library/musl-1.2.5-copyright.txt +193 -0
  68. package/native-notices/rust-standard-library/unicode-license.txt +39 -0
  69. package/package.json +23 -8
  70. package/readme.md +160 -2
  71. package/rust/.cargo/config.toml +2 -0
  72. package/rust/Cargo.lock +192 -0
  73. package/rust/Cargo.toml +13 -0
  74. package/rust/src/main.rs +159 -0
  75. package/rust/src/namespace.rs +51 -0
  76. package/rust/src/owner.rs +604 -0
  77. package/rust/src/owner_namespace_tests.rs +242 -0
  78. package/rust/src/owner_tests.rs +798 -0
  79. package/rust/src/policy.rs +427 -0
  80. package/rust/src/tests.rs +97 -0
  81. package/rust/src/wire.rs +437 -0
  82. package/scripts/check-native-notices.mjs +27 -0
  83. package/ts/00_commitinfo_data.ts +1 -1
  84. package/ts/classes.managednftables.ts +153 -0
  85. package/ts/index.ts +2 -0
  86. package/ts/managed.types.ts +91 -0
  87. package/ts/plugins.ts +6 -0
  88. /package/{license → license.md} +0 -0
@@ -0,0 +1,159 @@
1
+ mod namespace;
2
+ mod owner;
3
+ mod policy;
4
+ #[cfg(test)]
5
+ mod tests;
6
+ mod wire;
7
+
8
+ use serde::Deserialize;
9
+ use std::io::{BufRead, Read, Write};
10
+
11
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
12
+ pub enum Error {
13
+ Invalid,
14
+ Unsupported,
15
+ Permission,
16
+ ForeignOwner,
17
+ Conflict,
18
+ Absent,
19
+ Protocol,
20
+ Unavailable,
21
+ }
22
+ pub type Result<T> = std::result::Result<T, Error>;
23
+ impl Error {
24
+ pub fn code(self) -> &'static str {
25
+ match self {
26
+ Self::Invalid => "INVALID",
27
+ Self::Unsupported => "UNSUPPORTED_KERNEL",
28
+ Self::Permission => "PERMISSION",
29
+ Self::ForeignOwner => "FOREIGN_OWNER",
30
+ Self::Conflict => "CONFLICT",
31
+ Self::Absent => "ABSENT",
32
+ Self::Protocol => "PROTOCOL",
33
+ Self::Unavailable => "UNAVAILABLE",
34
+ }
35
+ }
36
+ pub fn kernel(code: i32) -> Self {
37
+ match -code {
38
+ libc::ENOENT => Self::Absent,
39
+ libc::EPERM | libc::EACCES => Self::Permission,
40
+ libc::EEXIST | libc::EBUSY | libc::ERESTART => Self::Conflict,
41
+ libc::EOPNOTSUPP => Self::Unsupported,
42
+ _ => Self::Unavailable,
43
+ }
44
+ }
45
+ }
46
+
47
+ #[derive(Deserialize)]
48
+ #[serde(deny_unknown_fields)]
49
+ struct Request {
50
+ id: String,
51
+ method: String,
52
+ params: Box<serde_json::value::RawValue>,
53
+ }
54
+ fn decode<T: serde::de::DeserializeOwned>(value: Box<serde_json::value::RawValue>) -> Result<T> {
55
+ serde_json::from_str(value.get()).map_err(|_| Error::Invalid)
56
+ }
57
+ fn encode(value: impl serde::Serialize) -> Result<serde_json::Value> {
58
+ serde_json::to_value(value).map_err(|_| Error::Protocol)
59
+ }
60
+ fn dispatch(owner: &mut Option<owner::Owner>, request: Request) -> Result<serde_json::Value> {
61
+ match request.method.as_str() {
62
+ "preparePolicy" => encode(decode::<policy::Policy>(request.params)?.prepare()?),
63
+ "openOwner" => {
64
+ if owner.is_some() {
65
+ return Err(Error::Conflict);
66
+ }
67
+ *owner = Some(owner::Owner::new(decode(request.params)?)?);
68
+ encode(owner.as_mut().unwrap().inspect())
69
+ }
70
+ "reconcilePolicy" => encode(
71
+ owner
72
+ .as_mut()
73
+ .ok_or(Error::Conflict)?
74
+ .reconcile(decode(request.params)?)?,
75
+ ),
76
+ "inspectPolicy" => {
77
+ if decode::<serde_json::Value>(request.params)? != serde_json::json!({}) {
78
+ return Err(Error::Invalid);
79
+ }
80
+ encode(owner.as_mut().ok_or(Error::Conflict)?.inspect())
81
+ }
82
+ "releasePolicy" => {
83
+ owner
84
+ .as_mut()
85
+ .ok_or(Error::Conflict)?
86
+ .release(decode(request.params)?)?;
87
+ Ok(serde_json::json!({"released":true}))
88
+ }
89
+ "closeOwner" => {
90
+ if decode::<serde_json::Value>(request.params)? != serde_json::json!({}) {
91
+ return Err(Error::Invalid);
92
+ }
93
+ owner.as_mut().ok_or(Error::Conflict)?.close()?;
94
+ Ok(serde_json::json!({"released":true}))
95
+ }
96
+ _ => Err(Error::Invalid),
97
+ }
98
+ }
99
+
100
+ fn main() {
101
+ let arguments = std::env::args().skip(1).collect::<Vec<_>>();
102
+ let descriptor = match arguments.as_slice() {
103
+ [management] if management == "--management" => None,
104
+ [management, option, fd]
105
+ if management == "--management" && option == "--network-namespace-fd" =>
106
+ {
107
+ match fd.parse::<i32>() {
108
+ Ok(value) => Some(value),
109
+ Err(_) => std::process::exit(2),
110
+ }
111
+ }
112
+ _ => std::process::exit(2),
113
+ };
114
+ if let Some(fd) = descriptor {
115
+ if let Err(error) = namespace::enter(fd) {
116
+ eprintln!("Network namespace startup failed: {error}");
117
+ std::process::exit(1);
118
+ }
119
+ }
120
+ // The single native owner must enter before readiness, context capture or sockets.
121
+ let mut output = std::io::stdout().lock();
122
+ writeln!(output, "{{\"event\":\"ready\",\"data\":{{}}}}").unwrap();
123
+ output.flush().unwrap();
124
+ let mut input = std::io::stdin().lock();
125
+ let mut owner = None;
126
+ loop {
127
+ let mut bytes = Vec::new();
128
+ let read = std::io::Read::by_ref(&mut input)
129
+ .take(262_145)
130
+ .read_until(b'\n', &mut bytes);
131
+ if matches!(read, Ok(0)) {
132
+ break;
133
+ }
134
+ if read.is_err() || bytes.len() > 262_144 || bytes.last() != Some(&b'\n') {
135
+ std::process::exit(2);
136
+ }
137
+ let request: Request = match serde_json::from_slice(&bytes) {
138
+ Ok(request) => request,
139
+ Err(_) => std::process::exit(2),
140
+ };
141
+ if request.id.is_empty() || request.id.len() > 128 {
142
+ std::process::exit(2);
143
+ }
144
+ let id = request.id.clone();
145
+ let response = match dispatch(&mut owner, request) {
146
+ Ok(result) => serde_json::json!({"id":id,"success":true,"result":result}),
147
+ Err(error) => {
148
+ serde_json::json!({"id":id,"success":false,"error":"Managed policy operation failed.","errorCode":error.code()})
149
+ }
150
+ };
151
+ if writeln!(output, "{response}")
152
+ .and_then(|_| output.flush())
153
+ .is_err()
154
+ {
155
+ break;
156
+ }
157
+ }
158
+ // EOF/process loss only drops socket ownership. PERSIST retains packet policy.
159
+ }
@@ -0,0 +1,51 @@
1
+ //! Startup-only entry. No thread, runtime, socket or ready event may precede it.
2
+ use std::io;
3
+
4
+ #[cfg(target_os = "linux")]
5
+ pub fn enter(fd: i32) -> io::Result<()> {
6
+ use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
7
+
8
+ // Only an explicitly inherited extra descriptor is accepted. Never consume IPC.
9
+ if fd < 3 {
10
+ return Err(io::Error::new(
11
+ io::ErrorKind::InvalidInput,
12
+ "network namespace descriptor must be at least 3",
13
+ ));
14
+ }
15
+ if unsafe { libc::fcntl(fd, libc::F_GETFD) } < 0 {
16
+ return Err(io::Error::new(
17
+ io::ErrorKind::InvalidInput,
18
+ "network namespace descriptor is not open",
19
+ ));
20
+ }
21
+ // Startup is single-threaded; this is the child's owned copy, not the parent's.
22
+ let descriptor = unsafe { OwnedFd::from_raw_fd(fd) };
23
+ let namespace_type = unsafe { libc::ioctl(descriptor.as_raw_fd(), libc::NS_GET_NSTYPE) };
24
+ if namespace_type != libc::CLONE_NEWNET {
25
+ return Err(io::Error::new(
26
+ io::ErrorKind::InvalidInput,
27
+ "descriptor is not a Linux network namespace",
28
+ ));
29
+ }
30
+ if unsafe { libc::setns(descriptor.as_raw_fd(), libc::CLONE_NEWNET) } != 0 {
31
+ return Err(io::Error::new(
32
+ io::ErrorKind::PermissionDenied,
33
+ format!(
34
+ "network namespace entry failed: {}",
35
+ io::Error::last_os_error()
36
+ ),
37
+ ));
38
+ }
39
+ // Close the inherited capability before creating threads. Namespace membership
40
+ // itself keeps the namespace alive until the complete process has exited.
41
+ drop(descriptor);
42
+ Ok(())
43
+ }
44
+
45
+ #[cfg(not(target_os = "linux"))]
46
+ pub fn enter(_fd: i32) -> io::Result<()> {
47
+ Err(io::Error::new(
48
+ io::ErrorKind::Unsupported,
49
+ "network namespace entry requires Linux",
50
+ ))
51
+ }