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
6 # LibreOffice has some problems on Linux, see https://bugs.documentfoundation.org/show_bug.cgi?id=126604
10 echo "Usage: ./$(basename "${0}") [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"
19 # Print errors to STDERR
20 function printerror
() {
24 # Set default options that may be overridden by passed options below
28 # Set other default options
29 PrintServer
="followprint.win.ntnu.no"
30 PrintFile
="ntnuprint-ricoh"
31 Workgroup
="WIN-NTNU-NO"
34 while [[ $# -gt 0 ]]; do
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"
44 shift # Jump to next argument
48 shift # Jump to next argument
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"
56 shift # Jump to next argument
60 shift # Jump to next argument
68 echo "Unknown argument: ${Key}"
73 shift # past argument or value
76 # Make sure we have sudo powers when we need it
78 printerror
"This script requires sudo to function"
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"
89 echo "Please provide sudo password, as some parts of this script requires it"
91 printerror
"Sorry, this script requires working sudo privileges to function"
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."
103 # Set Uname to linux if we are forcing running the script
104 if [ "${Force}" = "YES" ]; then
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"
112 DriverPath
="drv:///sample.drv/generpcl.ppd"
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."
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."
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)."
135 # Try to determine if the necessary printer drivers are installed, and set correct driver paths
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."
147 DriverPath
="OpenPrintingPPDs/postscript/Ricoh-MP_C6003.Postscript-Ricoh.ppd.gz"
150 DriverPath
="Postscript.ppd.gz"
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."
159 if [ "${Model}" = "ricoh" ]; then
160 DriverPath
="OpenPrintingPPDs/ghostscript/Ricoh-MP_C6003.pxlcolor-Ricoh.ppd.gz"
162 DriverPath
="OpenPrintingPPDs/ghostscript/Generic-PCL_6_PCL_XL_Printer.pxlcolor.ppd.gz"
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."
177 if [ "${Driver}" = "postscript" ]; then
178 DriverPath
="foomatic-db-ppds/Ricoh/PS/Ricoh-MP_C6003_PS.ppd.gz"
180 DriverPath
="foomatic-db-ppds/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd.gz"
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."
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."
197 if [ "${Driver}" = "postscript" ]; then
198 DriverPath
="foomatic-db-ppds/Ricoh/PS/Ricoh-MP_C6003_PS.ppd.gz"
200 DriverPath
="foomatic-db-ppds/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd.gz"
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."
212 if [ "${Driver}" = "postscript" ]; then
213 DriverPath
="openprinting-ppds:0/ppd/openprinting/Ricoh/PS/Ricoh-MP_C6003_PS.ppd"
215 DriverPath
="openprinting-ppds:0/ppd/openprinting/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd"
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."
226 # Get username and password
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}."
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.
"
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}.
"
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."
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
272 if [ "${Uname}" = "linux" ]; then
273 if [ "${Plaintext}" = "YES" ]; then
274 PrinterShare
="smb://${Workgroup}/${Username}:${Password}@${PrintServer}/${PrintFile}"
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"
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"
282 if ! sudo lpadmin
-p ${QueueName} \
283 -D "FollowMe print queue at NTNU" \
284 -L "Many locations" \
285 -v "${PrinterShare}" \
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."
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"
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}" \
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."
316 sudo cupsenable
"${QueueName}"
317 sudo cupsaccept
"${QueueName}"
319 # Add credentials to the keychain if they are missing
320 # Shamelessly stolen^W^WBorrowed from https://github.com/Orakeltjenesten/scripts/blob/33abfb353524f449f0bbdee27adb2f1f0a9756a2/print/ntnuprint-mac.sh
321 security
-v add-internet-password
-U -a "${Workgroup}\\${Username}" -s "${PrintServer}" \
322 -w "${Password}" -D "Network Password" -r "smb " -l "${QueueName}" \
323 -T /System
/Library
/CoreServices
/NetAuthAgent.app
-T 'group://NetAuth' \
324 -T /System
/Library
/CoreServices
/NetAuthAgent.app
/Contents
/MacOS
/NetAuthSysAgent
>/dev
/null
2>&1
326 # Make sure the password has the correct ACL ref https://mostlikelee.com/blog-1/2017/9/16/scripting-the-macos-keychain-partition-ids
327 OS_Min_Vers
=$
(sw_vers |
grep ProductVersion |
awk '{print $2}' | cut
-d "." -f2)
328 if [ "${OS_Min_Vers}" -ge 12 ]; then
329 security set-generic-password-partition-list
-S "apple-tool:,apple:" -s "${PrintServer}" -k "${Password}"
333 # Set correct paper size and enable the duplexer option
334 if ! sudo lpadmin
-p ${QueueName} -o PageSize
=A4
-o Option1
=True
; then
335 printerror
"Could not set default options on the print queue ${QueueName}. See above error for details."
340 if ! sudo lpadmin
-d ${QueueName}; then
341 printerror
"Could not set the print queue ${QueueName} as default printer. See above error for details."
345 echo "Printer successfully installed."