From 937f6ba95528d35c570c101ffa5a2d6b90f4e3fa Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 23 May 2017 14:58:32 -0400 Subject: [PATCH] linux: Refactors Owner/Group to DevicePermissions --- params_linux.go | 30 +++++++++++++++++------------- syscalls_linux.go | 14 +++++++------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/params_linux.go b/params_linux.go index a0533f7..41203f6 100644 --- a/params_linux.go +++ b/params_linux.go @@ -2,6 +2,18 @@ package water +type DevicePermissions struct { + // ID of the user which will be granted ownership of the device. + // If set to a negative value, the owner value will not be changed. + // By default, Linux sets the owner to -1, which allows any user. + Owner uint + + // ID of the group which will be granted access to the device. + // If set to a negative value, the group value will not be changed. + // By default, Linux sets the group to -1, which allows any group. + Group uint +} + // PlatformSpecificParams defines parameters in Config that are specific to // Linux. A zero-value of such type is valid, yielding an interface // with OS defined name. @@ -15,20 +27,12 @@ type PlatformSpecificParams struct { // Enable or disable persistence mode for the interface device. Persist bool - // ID of the user which will be granted ownership of the device. - // If set to a negative value, the owner value will not be changed. - // By default, Linux sets the owner to -1, which allows any user. - Owner int - - // ID of the group which will be granted access to the device. - // If set to a negative value, the group value will not be changed. - // By default, Linux sets the group to -1, which allows any group. - Group int + // Owner and Group permissions for the device. + // A zero-value of this field, i.e. nil, indicates that no changes to owner + // or group will be made. + Permissions *DevicePermissions } func defaultPlatformSpecificParams() PlatformSpecificParams { - return PlatformSpecificParams{ - Owner: -1, - Group: -1, - } + return PlatformSpecificParams{} } diff --git a/syscalls_linux.go b/syscalls_linux.go index 4d97fd4..50a945c 100644 --- a/syscalls_linux.go +++ b/syscalls_linux.go @@ -81,16 +81,16 @@ func createInterface(fd uintptr, ifName string, flags uint16) (createdIFName str func setDeviceOptions(fd uintptr, config Config) (err error) { - // Set Device Owner - if config.Owner >= 0 { - if err = ioctl(fd, syscall.TUNSETOWNER, uintptr(config.Owner)); err != nil { + // Device Permissions + if config.Permissions != nil { + + // Set Owner + if err = ioctl(fd, syscall.TUNSETOWNER, uintptr(config.Permissions.Owner)); err != nil { return } - } - // Set Device Group - if config.Group >= 0 { - if err = ioctl(fd, syscall.TUNSETGROUP, uintptr(config.Group)); err != nil { + // Set Group + if err = ioctl(fd, syscall.TUNSETGROUP, uintptr(config.Permissions.Group)); err != nil { return } }