Skip to content

Commit

Permalink
Enforce root permission on AccessPoint constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
josephrhobbs committed Aug 13, 2024
1 parent 7f07f33 commit a1cbb34
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions proton_err/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub enum ProtonError {
/// Could not parse into CIDR range.
CouldNotParseAsCidr (String),

/// Root permissions required.
MustHaveRootPermissions,

/// CIDR range must contain network gateway.
CidrMustContainGateway {
/// Provided CIDR network range.
Expand All @@ -57,6 +60,7 @@ impl Display for ProtonError {
use ProtonError::*;
let error = match self {
MustBeEthernetInterface => "must be Ethernet interface",
MustHaveRootPermissions => "must execute with root permissions",
HotspotNotInitialized => "hotspot not initialized",
CouldNotFindWirelessInterface => "could not find wireless interface",
CouldNotGetDeviceInformation => "could not get wireless device information",
Expand Down
1 change: 1 addition & 0 deletions proton_wap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ name = "proton_wap"
path = "src/lib.rs"

[dependencies]
nix = { version = "0.29.0", features = ["user"] }

[dependencies.network-manager]
path = "../network-manager"
Expand Down
15 changes: 15 additions & 0 deletions proton_wap/src/ap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use network_manager::{
ConnectionState,
};

use nix::unistd::Uid;

use proton_cfg::HotspotConfig;

use proton_dev::{
Expand All @@ -21,6 +23,14 @@ use proton_err::{
};

/// A wireless access point.
///
/// **Note**: to construct and use this, you must run the associated
/// binary with root permissions. This is because some of the functionality
/// of the `AccessPoint` structure requires direct control over your
/// device's network interface.
///
/// This is enforced by `AccessPoint::new()`, as the constructor will return
/// a `ProtonError` if you attempt to execute it without root permission.
pub struct AccessPoint {
/// Device discovery manager.
device_manager: DeviceManager,
Expand All @@ -44,6 +54,11 @@ impl AccessPoint {
wlifname: &str,
config: HotspotConfig,
) -> ProtonResult<Self> {
// Check if the user is `root`
if !Uid::effective().is_root() {
return Err (ProtonError::MustHaveRootPermissions);
}

// Initialize NetworkManager API
let network_manager = NetworkManager::new();

Expand Down

0 comments on commit a1cbb34

Please sign in to comment.