Automatisk låsing av skjerm
[einar-bin] / addfollowmeprint.sh
1 #!/bin/bash
2 # This script installs the FollowMe print queue at NTNU on Linux and possibly Mac systems.
3 # The targeted and tested distros are: Debian, Ubuntu (and derivates), Fedora, CentOS, OpenSUSE and Mint
4 # Copyright © 2017-2019 einar.haraldseid@ntnu.no
5
6 # LibreOffice has some problems on Linux, see https://bugs.documentfoundation.org/show_bug.cgi?id=126604
7
8 # Documentation
9 function usage {
10 echo "Usage: ./$(basename "${0}") [OPTIONS]"
11 echo "Options:"
12 echo " -m, --model {ricoh|generic} Printer model to install (default: generic)"
13 echo " -d, --driver {pcl|postscript} Printer driver to use (default: postscript)"
14 echo " -f, --force Force running script as if on Linux systems"
15 echo " -p, --plaintext Store credentials as plaintext in /etc/cups/printers.conf (Linux only)"
16 echo " -h, --help Display this help text"
17 }
18
19 # Print errors to STDERR
20 function printerror() {
21 echo "${*}" 1>&2
22 }
23
24 # Set default options that may be overridden by passed options below
25 Model="generic"
26 Driver="postscript"
27
28 # Set other default options
29 PrintServer="followprint.win.ntnu.no"
30 PrintFile="ntnuprint-ricoh"
31 Workgroup="WIN-NTNU-NO"
32 QueueName="FollowMe"
33
34 while [[ $# -gt 0 ]]; do
35 Key="${1}"
36
37 case ${Key} in
38 -m|--model)
39 Model=$(echo "${2}" | tr "[:upper:]" "[:lower:]")
40 if [ "${Model}" != "ricoh" ] && [ "${Model}" != "generic" ]; then
41 printerror "Unknown model ${Model}, please choose one of ricoh or generic"
42 exit 1
43 fi
44 shift # Jump to next argument
45 ;;
46 -f|--force)
47 Force="YES"
48 shift # Jump to next argument
49 ;;
50 -d|--driver)
51 Driver=$(echo "${2}" | tr "[:upper:]" "[:lower:]")
52 if [ "${Driver}" != "postscript" ] && [ "${Driver}" != "pcl" ]; then
53 printerror "Unknown driver ${Driver}, plase choose one of postscript or pcl"
54 exit 1
55 fi
56 shift # Jump to next argument
57 ;;
58 -p|--plaintext)
59 Plaintext="YES"
60 shift # Jump to next argument
61 ;;
62 -h|--help)
63 usage
64 exit 0
65 ;;
66 *)
67 # Unknown argument
68 echo "Unknown argument: ${Key}"
69 exit 1
70 ;;
71 esac
72
73 shift # past argument or value
74 done
75
76 # Make sure we have sudo powers when we need it
77 if ! sudo -k; then
78 printerror "This script requires sudo to function"
79 exit 1
80 fi
81
82 # But don't run as super user, otherwise we can't access the keyring
83 if [ ${UID} -eq 0 ]; then
84 if [ "${Plaintext}" != "YES" ]; then
85 printerror "Please run this script as your normal user, we will prompt you for your sudo password when needed. Or you can use the --plaintext option"
86 exit 1
87 fi
88 else
89 echo "Please provide sudo password, as some parts of this script requires it"
90 if ! sudo true; then
91 printerror "Sorry, this script requires working sudo privileges to function"
92 exit 1
93 fi
94 fi
95
96 # Test for supported OS
97 Uname=$(uname | tr "[:upper:]" "[:lower:]")
98 if [ "${Uname}" != "darwin" ] && [ "${Uname}" != "linux" ] && [ "${Force}" != "YES" ]; then
99 printerror "I don't think I can run on ${Uname}, but if you insist, please use -f to force Linux detection. This may not work, though."
100 exit 1
101 fi
102
103 # Set Uname to linux if we are forcing running the script
104 if [ "${Force}" = "YES" ]; then
105 Uname="linux"
106 fi
107
108 # Set printer driver path based on driver (we will override these if we're specific distros
109 if [ "${Driver}" = "postscript" ]; then
110 DriverPath="drv:///sample.drv/generic.ppd"
111 else
112 DriverPath="drv:///sample.drv/generpcl.ppd"
113 fi
114
115 # Test for CUPS
116 if ! sudo bash -c "command -v lpadmin" > /dev/null 2>&1; then
117 printerror "You must have CUPS installed to add printers. Please install CUPS."
118 exit 1
119 fi
120
121 # Tests for Linux systems
122 if [ "${Uname}" = "linux" ]; then
123 # Test if smbclient is installed
124 if ! command -v smbclient >/dev/null 2>&1; then
125 printerror "You must have smbclient installed to print to NTNU followprint. Please install smbclient."
126 exit 1
127 fi
128
129 # Test if secret-tool is installed
130 if ! command -v secret-tool >/dev/null 2>&1; then
131 printerror "You must have secret-tool installed for this script to work. Please install libsecret-tools (Debian-systems) or secret-tool (Fedora/CentOS)."
132 exit 1
133 fi
134
135 # Try to determine if the necessary printer drivers are installed, and set correct driver paths
136
137 # This should match OpenSUSE, and probably SUSE Enterprise, and other derivates that use zypper as the primary package manager
138 if command -v zypper >/dev/null 2>&1; then
139 if [ "${Driver}" = "postscript" ]; then
140 if [ "${Model}" = "ricoh" ]; then
141 if ! sudo rpm -q OpenPrintingPPDs-postscript >/dev/null 2>&1; then
142 printerror "You don't seem to have the correct printer drivers installed, please run:"
143 printerror " sudo zypper install OpenPrintingPPDs-postscript"
144 printerror "first, or use the generic model instead."
145 exit 1
146 else
147 DriverPath="OpenPrintingPPDs/postscript/Ricoh-MP_C6003.Postscript-Ricoh.ppd.gz"
148 fi
149 else
150 DriverPath="Postscript.ppd.gz"
151 fi
152 else
153 if ! sudo rpm -q OpenPrintingPPDs-ghostscript >/dev/null 2>&1; then
154 printerror "You don't seem to have the correct printer drivers installed, please run:"
155 printerror " sudo zypper install OpenPrintingPPDs-ghostscript"
156 printerror "first, or use the generic model instead."
157 exit 1
158 else
159 if [ "${Model}" = "ricoh" ]; then
160 DriverPath="OpenPrintingPPDs/ghostscript/Ricoh-MP_C6003.pxlcolor-Ricoh.ppd.gz"
161 else
162 DriverPath="OpenPrintingPPDs/ghostscript/Generic-PCL_6_PCL_XL_Printer.pxlcolor.ppd.gz"
163 fi
164 fi
165 fi
166 else
167 # The rest of the systems can use drv:/// paths for the generic drivers, but we need to detect various distros for advice on installing Ricoh drivers
168 if [ "${Model}" = "ricoh" ]; then
169 # This should match Fedora and other modern rpm based systems that have dnf as the primary package manager
170 if command -v dnf >/dev/null 2>&1; then
171 if ! sudo rpm -q foomatic-db-ppds > /dev/null 2>&1; then
172 printerror "You don't seem to have the correct printer drivers installed, please run:"
173 printerror " sudo dnf install foomatic-db-ppds"
174 printerror "first, or use the generic model instead."
175 exit 1
176 else
177 if [ "${Driver}" = "postscript" ]; then
178 DriverPath="foomatic-db-ppds/Ricoh/PS/Ricoh-MP_C6003_PS.ppd.gz"
179 else
180 DriverPath="foomatic-db-ppds/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd.gz"
181 fi
182 fi
183 # This should match CentOS, RHEL and other RHEL based distros that have yum as the primary package manager
184 elif command -v yum >/dev/null 2>&1; then
185 if ! sudo rpm -q foomatic-filters > /dev/null 2>&1; then
186 printerror "You don't seem to have the foomatic-filters package installed, please run:"
187 printerror " sudo yum install foomatic-filters"
188 printerror "first, or use the generic model instead."
189 exit 1
190 fi
191 if ! sudo rpm -q foomatic-db-ppds > /dev/null 2>&1; then
192 printerror "You don't seem to have the correct printer drivers installed, please run:"
193 printerror " sudo yum install foomatic-db-ppds"
194 printerror "first, or use the generic model instead."
195 exit 1
196 else
197 if [ "${Driver}" = "postscript" ]; then
198 DriverPath="foomatic-db-ppds/Ricoh/PS/Ricoh-MP_C6003_PS.ppd.gz"
199 else
200 DriverPath="foomatic-db-ppds/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd.gz"
201 fi
202 fi
203 fi
204 # This should match Debian, Ubuntu and most if not all derivates that use dpkg as the primary package manager
205 if command -v dpkg >/dev/null 2>&1; then
206 if ! sudo dpkg -s openprinting-ppds > /dev/null 2>&1; then
207 printerror "You must have the correct printer drivers installed, please run:"
208 printerror " sudo apt-get install openprinting-ppds"
209 printerror "first, or use the generic model instead."
210 exit 1
211 else
212 if [ "${Driver}" = "postscript" ]; then
213 DriverPath="openprinting-ppds:0/ppd/openprinting/Ricoh/PS/Ricoh-MP_C6003_PS.ppd"
214 else
215 DriverPath="openprinting-ppds:0/ppd/openprinting/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd"
216 fi
217 fi
218 fi
219 fi
220 fi
221 fi
222
223 echo "This script will add a new printer called ${QueueName}, connecting to the
224 print server ${PrintServer} using your user name and password from NTNU."
225
226 # Get username and password
227 printf "NTNU User name: "
228 read -r Username
229 printf "NTNU Password: "
230 Settings=$(stty -g)
231 stty -echo
232 read -r Password
233 stty "${Settings}"
234
235 echo ""
236
237 # Some further tests for Linux systems
238 if [ "${Uname}" = "linux" ]; then
239 # Test for valid username and password
240 # Bonus: find out if the print share is actually available
241 PrintServerIP=$(getent ahostsv4 ${PrintServer} | head -n 1 | cut -d " " -f 1)
242 if ! smbclient -U "${Workgroup}/${Username}%${Password}" -L "//${PrintServer}" -I "${PrintServerIP}" > /dev/null 2>&1; then
243 printerror "User name or password incorrect, or no contact with the print server ${PrintServer}."
244 exit 1
245 fi
246 ShareFound=$(smbclient -U "${Workgroup}/${Username}%${Password}" -L "//${PrintServer}" -I "${PrintServerIP}" -g 2>/dev/null | grep ${PrintFile} | cut -d "|" -f 1)
247 if [ "${ShareFound}" != "Printer" ]; then
248 printerror "Could not find printer share called ${PrintFile} on the server"
249 printerror "This script must be broken or outdated. Please contact orakel@ntnu.no for further assistance."
250 exit 1
251 fi
252 fi
253
254 # Similar tests for OSX
255 if [ "${Uname}" = "darwin" ]; then
256 if ! smbutil view -A "//${Workgroup};${Username}:${Password}@${PrintServer}" > /dev/null 2>&1; then
257 printerror "User name or password incorrect, or no contact with the print server ${PrintServer}."
258 exit 1
259 fi
260 ShareFound=$(smbutil view "//${Workgroup};${Username}:${Password}@${PrintServer}" 2>/dev/null | grep ${PrintFile} | cut -d " " -f 1)
261 if [ "${ShareFound}" != "${PrintFile}" ]; then
262 printerror "Could not find printer share called ${PrintFile} on the server"
263 printerror "This script must be broken or outdated. Please contact orakel@ntnu.no for further assistance."
264 exit 1
265 fi
266 fi
267
268 # Finally we can add the printer, let's remove any existing printer share with the same name first
269 sudo lpadmin -x ${QueueName} > /dev/null 2>&1
270
271 # The Linux way
272 if [ "${Uname}" = "linux" ]; then
273 if [ "${Plaintext}" = "YES" ]; then
274 PrinterShare="smb://${Workgroup}/${Username}:${Password}@${PrintServer}/${PrintFile}"
275 AuthInfo="none"
276 echo -e "\nNOTE: Your credentials will be stored in plaintext in /etc/cups/printers.conf.\nThis is usually only necessary on headless systems or on systems that don't run a dbus-daemon and/or a keyring that can provide the org.freedesktop.secrets service.\nNeedless to say, this is not a good idea on multi-user systems.\n"
277 else
278 PrinterShare="smb://${Workgroup}/${PrintServer}/${PrintFile}"
279 AuthInfo="username,password"
280 echo -e "\nNOTE: Due to the way credentials are stored and accessed on Linux, some print operations will still halt for credentials, notably the \"Print test page\" function and printing from LibreOffice. In those cases, supply your normal NTNU username and password. If this becomes too tedious, you can try using the --plaintext option.\n"
281 fi
282 if ! sudo lpadmin -p ${QueueName} \
283 -D "FollowMe print queue at NTNU" \
284 -L "Many locations" \
285 -v "${PrinterShare}" \
286 -m "${DriverPath}" \
287 -o auth-info-required="$AuthInfo" \
288 -u allow:all -E; then
289 printerror "Could not connect to printer share. See above error for details."
290 exit 1
291 fi
292 if [ "${Plaintext}" != "YES" ]; then
293 # Add credentials to the keyring
294 if ! echo -n "${Password}" | secret-tool store \
295 --label "ipp://localhost:631/printers/$QueueName" \
296 uri "ipp://localhost:631/printers/${QueueName}" \
297 user "${Username}"; then
298 printerror "Could not store credentials to the keyring, see above error for details. A workaround can be to use the option --plaintext"
299 fi
300 fi
301 fi
302
303 # The OSX way
304 if [ "${Uname}" = "darwin" ]; then
305 if ! sudo lpadmin -p ${QueueName} \
306 -D "FollowMe print queue at NTNU" \
307 -L "Many locations" \
308 -v "smb://${PrintServer}/${PrintFile}" \
309 -m "${DriverPath}" \
310 -o printer-is-shared=false -o printer-op-policy=authenticated \
311 -u allow:all -E; then
312 printerror "Could not connect to printer share. See above error for details."
313 exit 1
314 fi
315
316 # Add credentials to the keychain if they are missing
317 # Shamelessly stolen^W^WBorrowed from https://github.com/Orakeltjenesten/scripts/blob/33abfb353524f449f0bbdee27adb2f1f0a9756a2/print/ntnuprint-mac.sh
318 security -v add-internet-password -U -a "${Workgroup}\\${Username}" -s "${PrintServer}" \
319 -w "${Password}" -D "Network Password" -r "smb " -l "${QueueName}" \
320 -T /System/Library/CoreServices/NetAuthAgent.app -T 'group://NetAuth' \
321 -T /System/Library/CoreServices/NetAuthAgent.app/Contents/MacOS/NetAuthSysAgent >/dev/null 2>&1
322
323 # Make sure the password has the correct ACL ref https://mostlikelee.com/blog-1/2017/9/16/scripting-the-macos-keychain-partition-ids
324 OS_Min_Vers=$(sw_vers | grep ProductVersion | awk '{print $2}' | cut -d "." -f2)
325 if [ "${OS_Min_Vers}" -ge 12 ]; then
326 echo -e "\nNOTE: You need to provide your local Mac password again here in order to give the printing system access to your credentials.\n"
327 security set-internet-password-partition-list -S "apple-tool:,apple:" -s "${PrintServer}" > /dev/null
328 fi
329
330 sudo cupsenable "${QueueName}"
331 sudo cupsaccept "${QueueName}"
332 sudo launchctl stop org.cups.cupsd
333 sudo launchctl start org.cups.cupsd
334 fi
335
336 # Set correct paper size and enable the duplexer option
337 if ! sudo lpadmin -p ${QueueName} -o PageSize=A4 -o Option1=True; then
338 printerror "Could not set default options on the print queue ${QueueName}. See above error for details."
339 exit 1
340 fi
341
342 # Set as default
343 if ! sudo lpadmin -d ${QueueName}; then
344 printerror "Could not set the print queue ${QueueName} as default printer. See above error for details."
345 exit 1
346 fi
347
348 echo "Printer successfully installed."