| 1 | #!/bin/bash |
| 2 | ############################################################################### |
| 3 | # BRLTTY - A background process providing access to the console screen (when in |
| 4 | # text mode) for a blind person using a refreshable braille display. |
| 5 | # |
| 6 | # Copyright (C) 1995-2026 by The BRLTTY Developers. |
| 7 | # |
| 8 | # BRLTTY comes with ABSOLUTELY NO WARRANTY. |
| 9 | # |
| 10 | # This is free software, placed under the terms of the |
| 11 | # GNU Lesser General Public License, as published by the Free Software |
| 12 | # Foundation; either version 2.1 of the License, or (at your option) any |
| 13 | # later version. Please see the file LICENSE-LGPL for details. |
| 14 | # |
| 15 | # Web Page: http://brltty.app/ |
| 16 | # |
| 17 | # This software is maintained by Dave Mielke <[email protected]>. |
| 18 | ############################################################################### |
| 19 | |
| 20 | set -e |
| 21 | . "`dirname "${0}"`/brltty-prologue.sh" |
| 22 | |
| 23 | showProgramUsagePurpose() { |
| 24 | cat <<END_OF_PROGRAM_USAGE_PURPOSE |
| 25 | Simplify the job of assigning capabilities to the BRLTTY executable. |
| 26 | END_OF_PROGRAM_USAGE_PURPOSE |
| 27 | } |
| 28 | |
| 29 | executeCommand() { |
| 30 | "${useSudo}" && set -- sudo -- "${@}" |
| 31 | |
| 32 | if "${testMode}" |
| 33 | then |
| 34 | echo "${*}" |
| 35 | else |
| 36 | "${@}" |
| 37 | fi |
| 38 | } |
| 39 | |
| 40 | setOwner() { |
| 41 | local type="${1}" |
| 42 | local root="${2}" |
| 43 | local command="${3}" |
| 44 | |
| 45 | if "${root}" |
| 46 | then |
| 47 | local owner=0 |
| 48 | else |
| 49 | local owner="$(id -"${type}")" |
| 50 | fi |
| 51 | |
| 52 | executeCommand "${command}" "${owner}" -- "${executablePath}" |
| 53 | } |
| 54 | |
| 55 | setMode() { |
| 56 | local type="${1}" |
| 57 | local set="${2}" |
| 58 | |
| 59 | if "${set}" |
| 60 | then |
| 61 | local operator="+" |
| 62 | else |
| 63 | local operator="-" |
| 64 | fi |
| 65 | |
| 66 | executeCommand chmod "${type}${operator}s" "${executablePath}" |
| 67 | } |
| 68 | |
| 69 | capabilitiesList=() |
| 70 | addCapability() { |
| 71 | capabilitiesList[${#capabilitiesList[*]}]="${1}" |
| 72 | } |
| 73 | |
| 74 | addProgramOption c flag noCreation "don't allow creating missing state directories" |
| 75 | addProgramOption d flag noDevices "don't allow creating needed but missing device files" |
| 76 | addProgramOption g flag noGroups "don't allow switching to the writable group or joining the required groups" |
| 77 | addProgramOption i flag noInput "don't allow injecting input characters" |
| 78 | addProgramOption m flag noModules "don't allow installing kernel modules" |
| 79 | addProgramOption o flag noOwnership "don't allow claiming ownership of the state directories" |
| 80 | addProgramOption p flag noPermissions "don't allow adding group permissions to the state directories" |
| 81 | addProgramOption s flag noSpeaker "don't allow using the built-in PC speaker" |
| 82 | addProgramOption C flag noCapabilities "don't set the capabilities" |
| 83 | addProgramOption G flag rootGroup "set group root execution" |
| 84 | addProgramOption S flag useSudo "use sudo to execute the commands as root" |
| 85 | addProgramOption T flag testMode "test mode - show the commands that would be executed" |
| 86 | addProgramOption U flag rootUser "set user root execution" |
| 87 | addProgramParameter executable executablePath "the path to the executable" |
| 88 | parseProgramArguments "${@}" |
| 89 | |
| 90 | verifyExecutableFile "${executablePath}" |
| 91 | |
| 92 | "${testMode}" || { |
| 93 | if "${useSudo}" |
| 94 | then |
| 95 | sudo -v |
| 96 | elif [ "$(id -u)" -ne 0 ] |
| 97 | then |
| 98 | semanticError "not executing as root" |
| 99 | fi |
| 100 | } |
| 101 | |
| 102 | setOwner u "${rootUser}" chown |
| 103 | setOwner g "${rootGroup}" chgrp |
| 104 | |
| 105 | setMode u "${rootUser}" |
| 106 | setMode g "${rootGroup}" |
| 107 | |
| 108 | "${noCapabilities}" || { |
| 109 | "${noCreation}" || addCapability "cap_dac_override" |
| 110 | "${noDevices}" || addCapability "cap_mknod" |
| 111 | "${noGroups}" || addCapability "cap_setgid" |
| 112 | "${noInput}" || addCapability "cap_sys_admin" |
| 113 | "${noModules}" || addCapability "cap_sys_module" |
| 114 | "${noOwnership}" || addCapability "cap_chown" |
| 115 | "${noPermissions}" || addCapability "cap_fowner" |
| 116 | "${noSpeaker}" || addCapability "cap_sys_tty_config" |
| 117 | |
| 118 | [ "${#capabilitiesList[*]}" -eq 0 ] || { |
| 119 | capabilitiesOperand="${capabilitiesList[*]}" |
| 120 | capabilitiesOperand="${capabilitiesOperand// /,}" |
| 121 | capabilitiesOperand+="+p" |
| 122 | executeCommand setcap "${capabilitiesOperand}" "${executablePath}" |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | exit 0 |