asahi-wifisync-0.2.1/Cargo.lock0000644000000165370000000000100117410ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "adler32" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" [[package]] name = "ahash" version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" dependencies = [ "getrandom", "once_cell", "version_check", ] [[package]] name = "apple-nvram" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86c1056f6f4803156772f3369ae54d01cdfa36e901a1a271c620c3d445ae7eab" dependencies = [ "adler32", "crc32fast", "nix", ] [[package]] name = "asahi-wifisync" version = "0.2.1" dependencies = [ "apple-nvram", "clap", "rust-ini", ] [[package]] name = "atty" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ "hermit-abi", "libc", "winapi", ] [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" version = "3.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" dependencies = [ "atty", "bitflags", "clap_lex", "indexmap", "once_cell", "strsim", "termcolor", "textwrap", ] [[package]] name = "clap_lex" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" dependencies = [ "os_str_bytes", ] [[package]] name = "crc32fast" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" dependencies = [ "cfg-if", ] [[package]] name = "dlv-list" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" [[package]] name = "getrandom" version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ "cfg-if", "libc", "wasi", ] [[package]] name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" dependencies = [ "ahash", ] [[package]] name = "hermit-abi" version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" dependencies = [ "libc", ] [[package]] name = "indexmap" version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", "hashbrown", ] [[package]] name = "libc" version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "memoffset" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" dependencies = [ "autocfg", ] [[package]] name = "nix" version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfdda3d196821d6af13126e40375cdf7da646a96114af134d5f417a9a1dc8e1a" dependencies = [ "bitflags", "cfg-if", "libc", "memoffset", "pin-utils", "static_assertions", ] [[package]] name = "once_cell" version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] name = "ordered-multimap" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a" dependencies = [ "dlv-list", "hashbrown", ] [[package]] name = "os_str_bytes" version = "6.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d5d9eb14b174ee9aa2ef96dc2b94637a2d4b6e7cb873c7e171f0c20c6cf3eac" [[package]] name = "pin-utils" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "rust-ini" version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df" dependencies = [ "cfg-if", "ordered-multimap", ] [[package]] name = "static_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "strsim" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "termcolor" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" dependencies = [ "winapi-util", ] [[package]] name = "textwrap" version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" [[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" dependencies = [ "winapi", ] [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" asahi-wifisync-0.2.1/Cargo.toml0000644000000016110000000000100117470ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "asahi-wifisync" version = "0.2.1" description = "A tool to sync Wifi passwords with macos on ARM Macs" homepage = "https://github.com/WhatAmISupposedToPutHere/asahi-nvram" license = "MIT" repository = "https://github.com/WhatAmISupposedToPutHere/asahi-nvram" [dependencies.apple-nvram] version = "0.3" [dependencies.clap] version = "3" features = ["cargo"] [dependencies.rust-ini] version = "0.18.0" asahi-wifisync-0.2.1/Cargo.toml.orig000064400000000000000000000010521046102023000154270ustar 00000000000000[package] name = "asahi-wifisync" license = "MIT" version = "0.2.1" edition = "2021" description = "A tool to sync Wifi passwords with macos on ARM Macs" homepage = "https://github.com/WhatAmISupposedToPutHere/asahi-nvram" repository = "https://github.com/WhatAmISupposedToPutHere/asahi-nvram" [dependencies] rust-ini = "0.18.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies.apple-nvram] path = "../apple-nvram" version = "0.3" [dependencies.clap] version = "3" features = ["cargo"] asahi-wifisync-0.2.1/LICENSE000064400000000000000000000020741046102023000135520ustar 00000000000000MIT License Copyright (c) 2022 The Asahi Linux Contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.asahi-wifisync-0.2.1/src/main.rs000064400000000000000000000105301046102023000146220ustar 00000000000000/* SPDX-License-Identifier: MIT */ use std::{ env, fmt::Debug, fs::OpenOptions, io::{self, Read}, path::Path, }; use apple_nvram::{nvram_parse, VarType, Variable}; use ini::Ini; #[derive(Debug)] #[allow(dead_code)] enum Error { Parse, SectionTooBig, ApplyError(std::io::Error), VariableNotFound, FileIO, IWDConfigDirNotFound, } impl From for Error { fn from(e: apple_nvram::Error) -> Self { match e { apple_nvram::Error::ParseError => Error::Parse, apple_nvram::Error::SectionTooBig => Error::SectionTooBig, apple_nvram::Error::ApplyError(e) => Error::ApplyError(e), } } } impl From for Error { fn from(_e: io::Error) -> Self { Error::FileIO } } type Result = std::result::Result; fn main() { real_main().unwrap(); } fn real_main() -> Result<()> { let matches = clap::command!() .arg(clap::arg!(-d --device [DEVICE] "Path to the nvram device.")) .subcommand(clap::Command::new("list").about("Parse shared wlan keys from nvram")) .subcommand( clap::Command::new("sync") .about("Sync wlan information from nvram") .arg(clap::arg!(-c --config [CONFIG] "IWD config path.")) .arg(clap::Arg::new("variable").multiple_values(true)), ) .get_matches(); let default_name = "/dev/mtd0ro".to_owned(); let default_config = "/var/lib/iwd".to_owned(); let wlan_var = "preferred-networks"; let mut file = OpenOptions::new() .read(true) .open(matches.get_one::("device").unwrap_or(&default_name)) .unwrap(); let mut data = Vec::new(); file.read_to_end(&mut data).unwrap(); let mut nv = nvram_parse(&data)?; let active = nv.active_part_mut(); let wlan_devs = active .get_variable(wlan_var.as_bytes(), VarType::System) .ok_or(Error::VariableNotFound)?; match matches.subcommand() { Some(("list", _args)) => { print_wlankeys(wlan_devs).expect("Failed to parse wlan device info"); } Some(("sync", args)) => { sync_wlankeys( wlan_devs, args.get_one::("config").unwrap_or(&default_config), ) .expect("Failed to sync wlan device info"); } _ => { print_wlankeys(wlan_devs).expect("Failed to parse wlan device info"); } } Ok(()) } struct Network { ssid: String, psk: Option>, } const CHUNK_LEN: usize = 0xc0; fn parse_wlan_info(var: &dyn Variable) -> Vec { let mut nets = Vec::new(); let data = var.value(); for chunk in data.chunks(CHUNK_LEN) { let ssid_len = u32::from_le_bytes(chunk[0xc..0x10].try_into().unwrap()) as usize; let ssid = String::from_utf8_lossy(&chunk[0x10..0x10 + ssid_len]).to_string(); let secure = u32::from_le_bytes(chunk[0x8..0xc].try_into().unwrap()) != 0; let psk = if secure { Some(chunk[0xa0..0xc0].to_owned()) } else { None }; nets.push(Network { ssid, psk }); } nets } fn format_psk(psk: &[u8]) -> String { psk.iter() .map(|x| format!("{x:02x}")) .collect::>() .join("") } fn print_wlankeys(var: &dyn Variable) -> Result<()> { let info = parse_wlan_info(var); for network in info { let psk_str = if let Some(psk) = network.psk { format!("PSK {}", format_psk(&psk)) } else { "Open".to_owned() }; println!("SSID {}, {}", network.ssid, psk_str); } Ok(()) } fn sync_wlankeys(var: &dyn Variable, config: &String) -> Result<()> { let config_path = Path::new(config); if !config_path.is_dir() { return Err(Error::IWDConfigDirNotFound); } let nets = parse_wlan_info(var); for net in nets { let suffix = if net.psk.is_some() { ".psk" } else { ".open" }; let net_path = config_path.join(format!("{}{}", net.ssid, suffix)); if net_path.exists() { continue; } let mut info = Ini::new(); if let Some(psk) = net.psk { info.with_section(Some("Security")) .set("PreSharedKey", format_psk(&psk)); } info.write_to_file(net_path)?; } Ok(()) }