openiwan 0.2.0

An independently implemented iWAN client and protocol library
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
use crate::{Error, Result};
use rand::RngCore;
use serde::{Deserialize, Serialize};
use std::fmt::Write as _;
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};

pub const STATE_VERSION: u32 = 1;

#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ManagedServer {
    pub name: String,
    pub host: String,
    pub port: u16,
    pub username: String,
    #[serde(rename = "encrypted_password")]
    pub(crate) encrypted_password: String,
}

impl std::fmt::Debug for ManagedServer {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("ManagedServer")
            .field("name", &self.name)
            .field("host", &self.host)
            .field("port", &self.port)
            .field("username", &self.username)
            .field("encrypted_password", &"[REDACTED]")
            .finish()
    }
}

impl ManagedServer {
    pub fn endpoint(&self) -> String {
        if self.host.contains(':') && !self.host.starts_with('[') {
            format!("[{}]:{}", self.host, self.port)
        } else {
            format!("{}:{}", self.host, self.port)
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ManagedState {
    pub version: u32,
    pub provider_id: String,
    pub domain: String,
    pub device_id: String,
    pub fetched_at_unix: u64,
    pub servers: Vec<ManagedServer>,
}

impl ManagedState {
    pub fn validate_for(&self, provider_id: &str, domain: &str) -> Result<()> {
        if self.version != STATE_VERSION {
            return Err(Error::ManagedProvider(format!(
                "unsupported managed state version {}; expected {STATE_VERSION}",
                self.version
            )));
        }
        if self.provider_id != provider_id || self.domain != domain {
            return Err(Error::ManagedProvider(
                "managed state does not match the selected provider".into(),
            ));
        }
        if self.device_id.is_empty() {
            return Err(Error::ManagedProvider(
                "managed state has an empty device id".into(),
            ));
        }
        if self.servers.is_empty() {
            return Err(Error::ManagedProvider(
                "managed state contains no lines".into(),
            ));
        }
        for server in &self.servers {
            if server.name.trim().is_empty()
                || server.host.trim().is_empty()
                || server.port == 0
                || server.username.trim().is_empty()
                || server.encrypted_password.is_empty()
            {
                return Err(Error::ManagedProvider(
                    "managed state contains an incomplete line".into(),
                ));
            }
        }
        Ok(())
    }
}

pub fn load_state(path: &Path) -> Result<ManagedState> {
    validate_state_file(path)?;
    let contents = fs::read_to_string(path).map_err(|error| {
        if error.kind() == std::io::ErrorKind::NotFound {
            Error::ManagedProvider(format!(
                "{} does not exist; run `openiwan managed fetch` first",
                path.display()
            ))
        } else {
            Error::Io(error)
        }
    })?;
    serde_json::from_str(&contents)
        .map_err(|error| Error::ManagedProvider(format!("{}: {error}", path.display())))
}

pub fn save_state(path: &Path, state: &ManagedState) -> Result<()> {
    let parent = path.parent().ok_or_else(|| {
        Error::ManagedProvider(format!("{} has no parent directory", path.display()))
    })?;
    fs::create_dir_all(parent)?;
    set_private_directory(parent)?;

    let mut random = [0_u8; 8];
    rand::rngs::OsRng.fill_bytes(&mut random);
    let suffix = hex_lower(&random);
    let temporary = parent.join(format!(".openiwan-state-{suffix}.tmp"));
    let serialized = serde_json::to_vec_pretty(state)
        .map_err(|error| Error::ManagedProvider(format!("serialize managed state: {error}")))?;
    let result = write_private_file(&temporary, &serialized)
        .and_then(|()| fs::rename(&temporary, path).map_err(Error::Io))
        .and_then(|()| sync_directory(parent));
    if result.is_err() {
        let _ = fs::remove_file(&temporary);
    }
    result
}

pub fn default_state_path(provider_id: &str, override_dir: Option<&Path>) -> Result<PathBuf> {
    let directory = if let Some(directory) = override_dir {
        directory.to_path_buf()
    } else {
        default_managed_directory()?
    };
    Ok(directory.join(format!("{provider_id}.json")))
}

#[cfg(unix)]
fn default_managed_directory() -> Result<PathBuf> {
    Ok(effective_home()?
        .join(".config")
        .join("openiwan")
        .join("managed"))
}

#[cfg(windows)]
fn default_managed_directory() -> Result<PathBuf> {
    Ok(
        windows_roaming_data_from(std::env::var_os("APPDATA"), std::env::var_os("USERPROFILE"))?
            .join("openiwan")
            .join("managed"),
    )
}

#[cfg(not(any(unix, windows)))]
fn default_managed_directory() -> Result<PathBuf> {
    Ok(effective_home()?
        .join(".config")
        .join("openiwan")
        .join("managed"))
}

#[cfg(windows)]
fn windows_roaming_data_from(
    app_data: Option<std::ffi::OsString>,
    user_profile: Option<std::ffi::OsString>,
) -> Result<PathBuf> {
    let path = app_data
        .map(PathBuf::from)
        .or_else(|| {
            user_profile.map(|profile| PathBuf::from(profile).join("AppData").join("Roaming"))
        })
        .ok_or_else(|| {
            Error::ManagedProvider(
                "cannot determine Windows AppData from APPDATA or USERPROFILE".into(),
            )
        })?;
    if !path.is_absolute() {
        return Err(Error::ManagedProvider(format!(
            "Windows AppData path is not absolute: {}",
            path.display()
        )));
    }
    Ok(path)
}

pub fn new_device_id() -> String {
    let mut bytes = [0_u8; 8];
    rand::rngs::OsRng.fill_bytes(&mut bytes);
    hex_lower(&bytes)
}

fn hex_lower(bytes: &[u8]) -> String {
    let mut output = String::with_capacity(bytes.len() * 2);
    for byte in bytes {
        let _ = write!(output, "{byte:02x}");
    }
    output
}

#[cfg(unix)]
fn write_private_file(path: &Path, contents: &[u8]) -> Result<()> {
    use std::os::unix::fs::OpenOptionsExt;

    let mut file = OpenOptions::new()
        .create_new(true)
        .write(true)
        .mode(0o600)
        .open(path)?;
    file.write_all(contents)?;
    file.sync_all()?;
    Ok(())
}

#[cfg(not(unix))]
fn write_private_file(path: &Path, contents: &[u8]) -> Result<()> {
    let mut file = OpenOptions::new().create_new(true).write(true).open(path)?;
    file.write_all(contents)?;
    file.sync_all()?;
    Ok(())
}

#[cfg(unix)]
fn set_private_directory(path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;

    fs::set_permissions(path, fs::Permissions::from_mode(0o700))?;
    Ok(())
}

#[cfg(not(unix))]
fn set_private_directory(_path: &Path) -> Result<()> {
    Ok(())
}

#[cfg(not(windows))]
fn effective_home() -> Result<PathBuf> {
    #[cfg(unix)]
    {
        let sudo_user = std::env::var("SUDO_USER").ok();
        let home = std::env::var_os("HOME").map(PathBuf::from);
        effective_home_from(sudo_user.as_deref(), home, passwd_home)
    }
    #[cfg(not(any(unix, windows)))]
    {
        std::env::var_os("HOME").map(PathBuf::from).ok_or_else(|| {
            Error::ManagedProvider("cannot determine the user home directory".into())
        })
    }
}

#[cfg(unix)]
fn effective_home_from<F>(
    sudo_user: Option<&str>,
    home: Option<PathBuf>,
    lookup: F,
) -> Result<PathBuf>
where
    F: FnOnce(&str) -> Option<PathBuf>,
{
    if let Some(user) = sudo_user.filter(|user| !user.is_empty() && *user != "root") {
        return lookup(user).ok_or_else(|| {
            Error::ManagedProvider(format!(
                "cannot determine home directory for SUDO_USER {user:?}"
            ))
        });
    }
    home.ok_or_else(|| Error::ManagedProvider("cannot determine the user home directory".into()))
}

#[cfg(unix)]
fn validate_state_file(path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;

    let metadata = fs::metadata(path).map_err(|error| {
        if error.kind() == std::io::ErrorKind::NotFound {
            Error::ManagedProvider(format!(
                "{} does not exist; run `openiwan managed fetch` first",
                path.display()
            ))
        } else {
            Error::Io(error)
        }
    })?;
    if metadata.permissions().mode() & 0o077 != 0 {
        return Err(Error::ManagedProvider(format!(
            "{} must not be accessible by group or other users",
            path.display()
        )));
    }
    Ok(())
}

#[cfg(not(unix))]
fn validate_state_file(path: &Path) -> Result<()> {
    if path.exists() {
        Ok(())
    } else {
        Err(Error::ManagedProvider(format!(
            "{} does not exist; run `openiwan managed fetch` first",
            path.display()
        )))
    }
}

#[cfg(unix)]
fn sync_directory(path: &Path) -> Result<()> {
    fs::File::open(path)?.sync_all()?;
    Ok(())
}

#[cfg(not(unix))]
fn sync_directory(_path: &Path) -> Result<()> {
    Ok(())
}

#[cfg(unix)]
fn passwd_home(user: &str) -> Option<PathBuf> {
    use std::ffi::{CStr, CString};

    let user = CString::new(user).ok()?;
    let mut record = std::mem::MaybeUninit::<libc::passwd>::uninit();
    let mut result = std::ptr::null_mut();
    let mut buffer = vec![0_u8; 16 * 1024];
    // SAFETY: all pointers reference writable storage for the duration of the
    // call; getpwnam_r initializes `record` only when `result` is non-null.
    let status = unsafe {
        libc::getpwnam_r(
            user.as_ptr(),
            record.as_mut_ptr(),
            buffer.as_mut_ptr().cast(),
            buffer.len(),
            &raw mut result,
        )
    };
    if status != 0 || result.is_null() {
        return None;
    }
    // SAFETY: getpwnam_r succeeded and returned a non-null pw_dir pointer owned
    // by `buffer`, which remains alive through conversion.
    let directory = unsafe { CStr::from_ptr((*record.as_ptr()).pw_dir) };
    Some(PathBuf::from(directory.to_string_lossy().into_owned()))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn state_round_trip_is_private() {
        let directory =
            std::env::temp_dir().join(format!("openiwan-store-test-{}", new_device_id()));
        let path = directory.join("test.json");
        let state = ManagedState {
            version: STATE_VERSION,
            provider_id: "test".into(),
            domain: "iwan.test".into(),
            device_id: "0102030405060708".into(),
            fetched_at_unix: 1,
            servers: vec![ManagedServer {
                name: "Line".into(),
                host: "192.0.2.1".into(),
                port: 6001,
                username: "line-user".into(),
                encrypted_password: "opaque".into(),
            }],
        };
        save_state(&path, &state).unwrap();
        assert_eq!(load_state(&path).unwrap(), state);
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            assert_eq!(
                fs::metadata(&path).unwrap().permissions().mode() & 0o777,
                0o600
            );
        }
        fs::remove_file(&path).unwrap();
        fs::remove_dir(&directory).unwrap();
    }

    #[cfg(unix)]
    #[test]
    fn sudo_home_resolution_never_falls_back_to_root_home() {
        let resolved = effective_home_from(Some("alice"), Some(PathBuf::from("/root")), |user| {
            (user == "alice").then(|| PathBuf::from("/home/alice"))
        })
        .unwrap();
        assert_eq!(resolved, PathBuf::from("/home/alice"));
        assert!(
            effective_home_from(Some("missing"), Some(PathBuf::from("/root")), |_| None,).is_err()
        );
    }

    #[cfg(windows)]
    #[test]
    fn windows_state_directory_uses_app_data_with_profile_fallback() {
        let app_data = PathBuf::from(r"C:\Users\alice\AppData\Roaming");
        assert_eq!(
            windows_roaming_data_from(Some(app_data.clone().into_os_string()), None).unwrap(),
            app_data
        );
        assert_eq!(
            windows_roaming_data_from(None, Some(std::ffi::OsString::from(r"C:\Users\alice")))
                .unwrap(),
            PathBuf::from(r"C:\Users\alice\AppData\Roaming")
        );
    }
}