Allow regular user to run a specific sysctl command? reddit / freebsd ---------------------------------------------------------------------- Niklas81 · 2021-03-19 20:13 ACPI is working so-so on Thinkpad P50. Only some of the Fn/function- keys are functioning satisfactory. I can control screen brightness, but only in 1% steps at a time. This I can hack by running `intel_backlight incr` on Super+F6. I can use the volume-keys by binding e.g. `XF86AudioLowerVolume` to running `mixer vol -10`. But my TWO volume mute buttons are problematic. I can access both as `XF86AudioMute`; one of them through `Fn+F1`, the other one sits above the numpad, and works on its own. However, only the former sets the led-indicator for the mute-button (ironically, the led sits on the other key). As root, I can simply run `sysctl dev.acpi_ibm.0.mute=1` to switch the led on. This also mutes the volume. If I mute using Fn+F1, volume stays off even if I change it with say `mixer vol 100`. So my question is, how can I run `sysctl dev.acpi_ibm.0.mute=1` as a regular user, without providing a password? I don't want to make all `sysctl`-commands available to regular users, just this one key. Or is there another way of setting this led indicator? Yeah, and if anyone knows how to make all the other Fn-keys available, I'd be most grateful. I'm missing mic mute (Fn+F4) and Fn+F7 through F12. I already have `kld_list="i915kms acpi_video acpi_call"` in `/etc/rc.conf` and `acpi_ibm_load="YES"` in `/boot/loader.conf`. Using FreeBSD 13 BETA 3. Thanks. Edit: Isn't this supposed to work by running `su`, then `visudo` and adding this line: `username ALL = NOPASSWD: /sbin/sysctl dev.acpi_ibm.0.mute=1` (replacing `username` with actual username). Nothing I put in `visudo` ever seems to have any effect! Edit 2: Boy am I embarrassed! I just realized you still need to use `sudo` when running the command you specify in `visudo` :) Anyway, I'd still like help on how to use the other function-keys, and this might help someone else, so I'll leave it! ---------------------------------------------------------------------- lumberjackninja · 2021-03-19 21:21 Write a shell script with the command you want, change owner to root, make executable and set the sticky bit. Then it'll run as root when invoked by your normal user. EDIT: you want the setuid bit, not sticky bit. So your script permissions should be 4755 (setuid, owner r+w+x, group r+x, others r+x) ---------------------------------------------------------------------- ctisred · 2021-03-20 03:08 Don't have a FreeBSD system handy (yes, i know...), but - if memory serves, and execve(2) confirms: The set-ID bits are not honored if the respective file system has the nosuid option enabled or if the new process file is an interpreter file. source: [https://www.freebsd.org/cgi/man.cgi?query=execve&sektion=2&ap ropos=0&manpath=FreeBSD+12.2- RELEASE](https://www.freebsd.org/cgi/man.cgi?query=execve&sektion=2&ap ropos=0&manpath=FreeBSD+12.2-RELEASE) IIRC linux is one of the few \*ix-like OS's that allows setuid scripts. ---------------------------------------------------------------------- lumberjackninja · 2021-03-20 03:19 Dang, good call. I just tested it on my system. I created a simple script: `#!/bin/sh` `whoami` When I change its owner to root and permissions to 4755, it still prints out the name of the user I use to invoke it. So, you're right. I had no idea it worked differently between Linux and FreeBSD. The solution I posted is the go-to on Linux machines for scenarios posted by the OP. Thanks for the education! ---------------------------------------------------------------------- ctisred · 2021-03-20 03:38 cheers :) Yeah, found this difference out the hard way back in the day on hp-ux. muh soul woz crushed. An adventurous person could probably write something in C that spawns a shell in a pipe and prints commands into it if so inclined. ---------------------------------------------------------------------- inco · 2021-03-20 16:10 Doas has an easy syntax to do this. ---------------------------------------------------------------------- Niklas81 · 2021-03-20 18:00 Well, I got most ACPI things for Thinkpad P50 working now. The only remianing problem is that pressing Fn+F1 and mute above the numpad both triggers `XF86AudioMute`, but pressing the former does in addition trigger an ACPI-event that sets the volume to 0 and lights/dims the led. The workaround is to simly only press mute with the key abouve the keypad. Then I put this in `visudo`: ``` niklas ALL = NOPASSWD: /sbin/sysctl dev.acpi_ibm.0.mute=1 niklas ALL = NOPASSWD: /sbin/sysctl dev.acpi_ibm.0.mute=0 niklas ALL = NOPASSWD: /usr/home/niklas/.bin/lcdbrightness ``` This is my script triggered by `XF86AudioMute`: ``` #!/bin/sh # Get current state before we take action: mixvol=$( mixer vol | cut -c 36-42 | xargs ) mutestate=$( sysctl dev.acpi_ibm.0.mute | cut -c22 ) # Get last saved volume (set before volume was last muted): lastvolume=$( cat ~/.config/.volume_mute_level ) # If currently muted: if [ $mutestate == 1 ]; then newstate=0 mixer vol $lastvolume # Restore volume to value before it was muted fi # If currently NOT muted: if [ $mutestate == 0 ]; then echo $mixvol > ~/.config/.volume_mute_level # Save current volume before muting mixer vol 0 newstate=1 fi sudo sysctl dev.acpi_ibm.0.mute=$newstate # Change led status ``` For `XF86AudioLowerVolume`: ``` #!/bin/sh mutestate=$( sysctl dev.acpi_ibm.0.mute | cut -c22 ) if [ $mutestate == 1 ]; then echo "0:0" > ~/.config/.volume_mute_level fi exec mixer vol -5 ``` For `XF86AudioRaiseVolume`: ``` #!/bin/sh mutestate=$( sysctl dev.acpi_ibm.0.mute | cut -c22 ) if [ $mutestate == 1 ]; then sudo sysctl dev.acpi_ibm.0.mute=0 fi exec mixer vol +5 ``` For `/usr/home/niklas/.bin/lcdbrightness`, which I bind to `0xE8` and `0xE9` and execute with command `sudo lcdbrightness incr`/`sudo lcdbrightness decr`: ``` #!/bin/sh lcdstate=$( sysctl hw.acpi.video.lcd0.brightness | cut -c 31-34 | xargs ) if [ "$1" == "incr" ]; then lcdstate=$(( $lcdstate+6 )) fi if [ "$1" == "decr" ]; then lcdstate=$(( $lcdstate-6 )) fi if [ "$lcdstate" -gt 100 ]; then lcdstate=100 fi if [ "$lcdstate" -lt 1 ]; then lcdstate=1 fi echo $lcdstate sudo sysctl hw.acpi.video.lcd0.brightness=$lcdstate ``` It adds 6 to the default 1, so totals to 7% increase/decrese per press, change to your liking. To access to additional Fn-keys, I've added the file `/usr/local/etc/devd/p50.conf`, `chmod +x` it, and place this in it: ``` notify 10 { match "system" "ACPI"; match "subsystem" "IBM"; action "/usr/local/bin/acpi_oem_exec.sh $notify ibm"; }; ``` And the matching file `/usr/local/bin/acpi_oem_exec.sh` has this for now: ``` #!/bin/sh if [ "$1" = "" -o "$2" = "" ] then echo "usage: $0 notify oem_name" exit 1 fi notify=`echo $1` echo $notify >> /usr/home/niklas/acpi-pressing.txt ``` So now I can use all Fn-keys! Of course you need to modify `/usr/local/bin/acpi_oem_exec.sh` so it acctually does something on the keypresses more than just logging them. Make sure you `sudo chown root:wheel` your `lcdbrightness` file, or you've got yourself a potential backdoor. Hope this helps someone else. ---------------------------------------------------------------------- backtickbot · 2021-03-20 18:00 [Fixed formatting.](https://np.reddit.com/r/backtickbot/comments/m9ceo m/httpsnpredditcomrfreebsdcommentsm8py0qallow/) Hello, Niklas81: code blocks using triple backticks (\`\`\`) don't work on all versions of Reddit! Some users see [this](https://stalas.alm.lt/backformat/grlzki6.png) / [this](https://stalas.alm.lt/backformat/grlzki6.html) instead. To fix this, **indent every line with 4 spaces** instead. [FAQ](https://www.reddit.com/r/backtickbot/wiki/index) ^(You can opt out by replying with backtickopt6 to this comment.) ---------------------------------------------------------------------- Niklas81 · 2021-03-20 18:17 Reddit should MAKE this work on all versions of Reddit. Bad bot.