Debian Codegraph @

brltty-mkuser

raw symbols SCIP
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 . "${programDirectory}/brltty-config.sh"
23
24 showProgramUsagePurpose() {
25 cat <<END_OF_PROGRAM_USAGE_PURPOSE
26 Simplify the job of Creating and making changes to BRLTTY's unprivileged user.
27 END_OF_PROGRAM_USAGE_PURPOSE
28 }
29
30 declare -A privilegeParameters
31 parseParameterString privilegeParameters "${BRLTTY_PRIVILEGE_PARAMETERS}" lx
32
33 readonly defaultUserName="${privilegeParameters["USER"]}"
34 readonly defaultUserInformation="Braille Device Daemon"
35 readonly defaultHomeDirectory="${BRLTTY_UPDATABLE_DIRECTORY}"
36 readonly defaultLoginShell="/sbin/nologin"
37
38 showProgramUsageNotes() {
39 cat <<END_OF_PROGRAM_USAGE_NOTES
40 When creating a new user, these options have the following defaults:
41 -d ${defaultHomeDirectory:-the home directory is not defined}
42 -g a new group with the same name as the user
43 -i ${defaultUserInformation}
44 -l ${defaultLoginShell}
45 These defaults don't apply when making changes to an existing user.
46 END_OF_PROGRAM_USAGE_NOTES
47 }
48
49 executeCommand() {
50 "${useSudo}" && set -- sudo -- "${@}"
51
52 if "${testMode}"
53 then
54 echo "${*}"
55 else
56 "${@}"
57 fi
58 }
59
60 groupList=()
61 addGroups() {
62 while [ "${#}" -gt 0 ]
63 do
64 local group="${1}"
65 shift 1
66 [ -n "${group}" ] || continue
67
68 [ "${group#*/}" = "${group}" ] || {
69 [ -e "${group}" ] || continue
70 group="$(stat -c "%G" "${group}")" || :
71 [ -n "${group}" ] || continue
72 }
73
74 local g
75 for g in "${groupList[@]}"
76 do
77 [ "${g}" = "${group}" ] && {
78 group=""
79 break
80 }
81 done
82
83 [ -n "${group}" ] && groupList[${#groupList[*]}]="${group}"
84 done
85
86 return 0
87 }
88
89 addProgramOption a flag noALSA "don't allow playing sound via the ALSA framework"
90 addProgramOption b flag noBrlapi "don't allow reading BrlAPI's authorization key file"
91 addProgramOption c flag noConsoles "don't allow access to the virtual consoles"
92 addProgramOption d string.path homeDirectory "set/change the home (login) directory"
93 addProgramOption g string.group primaryGroup "set/change the primary (login) group"
94 addProgramOption i string.text userInformation "set/change the user information (gecos) text"
95 addProgramOption k flag noKeyboards "don't allow keyboard monitoring"
96 addProgramOption l string.path loginShell "set/change the login shell"
97 addProgramOption p flag noPulse "don't allow playing sound via the Pulse Audio server"
98 addProgramOption s flag noSerial "don't allow access to serial devices"
99 addProgramOption u flag noUSB "don't allow access to USB devices"
100 addProgramOption E flag allowChanges "allow changes to an existing user"
101 addProgramOption G flag noGroups "don't set/change the supplementary group list"
102 addProgramOption N flag allowCreate "allow creating a new user"
103 addProgramOption S flag useSudo "use sudo to execute the commands as root"
104 addProgramOption T flag testMode "test mode - show the commands that would be executed"
105 addProgramOption U string.user userName "the user to be created/changed" "${defaultUserName}"
106 parseProgramArguments "${@}"
107
108 [ -n "${userName}" ] || userName="${defaultUserName}"
109 [ -n "${userName}" ] || syntaxError "unprivileged user not configured - use -U (user) to specify"
110
111 if id -u "${userName}" >/dev/null 2>&1
112 then
113 "${allowChanges}" || semanticError "user already exists: ${userName} - use -E (existing) to allow"
114 command=(usermod)
115 else
116 "${allowCreate}" || semanticError "user doesn't exist: ${userName} - use -N (new) to allow"
117 command=(useradd --system)
118 [ -n "${primaryGroup}" ] || command+=(--user-group)
119
120 [ -n "${userInformation}" ] || userInformation="${defaultUserInformation}"
121 [ -n "${homeDirectory}" ] || homeDirectory="${defaultHomeDirectory}"
122 [ -n "${loginShell}" ] || loginShell="${defaultLoginShell}"
123
124 if [ -z "${homeDirectory}" ]
125 then
126 createHomeDirectory=false
127 elif [ -e "${homeDirectory}" ]
128 then
129 [ -d "${homeDirectory}" ] || semanticError "not a directory: ${homeDirectory}"
130 createHomeDirectory=false
131 else
132 createHomeDirectory=true
133 fi
134
135 if "${createHomeDirectory}"
136 then
137 command+=(--create-home)
138 else
139 command+=(--no-create-home)
140 fi
141 fi
142
143 [ -n "${homeDirectory}" ] && command+=(--home "${homeDirectory}")
144 [ -n "${primaryGroup}" ] && command+=(--gid "${primaryGroup}")
145 [ -n "${userInformation}" ] && command+=(--comment "${userInformation}")
146 [ -n "${loginShell}" ] && command+=(--shell "${loginShell}")
147
148 "${noGroups}" || {
149 "${noALSA}" || addGroups audio
150 "${noBrlapi}" || addGroups "${BRLAPI_KEY_FILE}"
151 "${noConsoles}" || addGroups tty /dev/vcs1 /dev/tty1
152 "${noKeyboards}" || addGroups input
153 "${noPulse}" || addGroups pulse-access
154 "${noSerial}" || addGroups /dev/ttyS0
155 "${noUSB}" || addGroups /dev/bus/usb
156
157 groupsOperand="${groupList[*]}"
158 groupsOperand="${groupsOperand// /,}"
159 command+=(--groups "${groupsOperand}")
160 }
161
162 "${testMode}" || {
163 if "${useSudo}"
164 then
165 sudo -v
166 elif [ "$(id -u)" -ne 0 ]
167 then
168 semanticError "not executing as root"
169 fi
170 }
171
172 command+=(-- "${userName}")
173 executeCommand "${command[@]}"
174 exit 0