X-Git-Url: https://git.slaskete.net/einar-bin/blobdiff_plain/ff740c3d5a8133e44f0c962bb0abfbfab6f2e36c..407e7e5c5d504db44d89cb6d5d3a92d693009a8c:/addfollowmeprint.sh?ds=sidebyside diff --git a/addfollowmeprint.sh b/addfollowmeprint.sh index 828cb32..f422c52 100755 --- a/addfollowmeprint.sh +++ b/addfollowmeprint.sh @@ -1,5 +1,5 @@ #!/bin/bash -# This script installs the FollowMe print queue at NTNU on Linux (and possibly Mac) systems. +# This script installs the FollowMe print queue at NTNU on Linux and possibly Mac systems. # The targeted and tested distros are: Debian, Ubuntu (and derivates), Fedora, CentOS, OpenSUSE and Mint # Copyright © 2017-2019 einar.haraldseid@ntnu.no @@ -8,7 +8,7 @@ function usage { echo "Usage: ./$(basename "${0}") [OPTIONS]" echo "Options:" echo " -m, --model {ricoh|generic} Printer model to install (default: generic)" - echo " -d, --driver {pcl,postscript} Printer driver to use (default: postscript)" + echo " -d, --driver {pcl|postscript} Printer driver to use (default: postscript)" echo " -f, --force Force running script as if on Linux systems" echo " -p, --plaintext Store credentials as plaintext in /etc/cups/printers.conf (Linux only)" echo " -h, --help Display this help text" @@ -19,13 +19,6 @@ function printerror() { echo "${*}" 1>&2 } -# Test for root -if [ ${UID} -ne 0 ] ; then - echo "This script must be run as root, relaunching with sudo:" - sudo bash "$0" "$@" - exit $? -fi - # Set default options that may be overridden by passed options below Model="generic" Driver="postscript" @@ -78,6 +71,26 @@ while [[ $# -gt 0 ]]; do shift # past argument or value done +# Make sure we have sudo powers when we need it +if ! sudo -k; then + printerror "This script requires sudo to function" + exit 1 +fi + +# But don't run as super user, otherwise we can't access the keyring +if [ ${UID} -eq 0 ]; then + if [ "${Plaintext}" != "YES" ]; then + 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" + exit 1 + fi +else + echo "Please provide sudo password, as some parts of this script requires it" + if ! sudo true; then + printerror "Sorry, this script requires working sudo privileges to function" + exit 1 + fi +fi + # Test for supported OS Uname=$(uname | tr "[:upper:]" "[:lower:]") if [ "${Uname}" != "darwin" ] && [ "${Uname}" != "linux" ] && [ "${Force}" != "YES" ]; then @@ -98,7 +111,7 @@ else fi # Test for CUPS -if ! command -v lpadmin > /dev/null 2>&1; then +if ! sudo bash -c "command -v lpadmin" > /dev/null 2>&1; then printerror "You must have CUPS installed to add printers. Please install CUPS." exit 1 fi @@ -123,10 +136,10 @@ if [ "${Uname}" = "linux" ]; then if command -v zypper >/dev/null 2>&1; then if [ "${Driver}" = "postscript" ]; then if [ "${Model}" = "ricoh" ]; then - if ! rpm -q OpenPrintingPPDs-postscript >/dev/null 2>&1; then + if ! sudo rpm -q OpenPrintingPPDs-postscript >/dev/null 2>&1; then printerror "You don't seem to have the correct printer drivers installed, please run:" printerror " sudo zypper install OpenPrintingPPDs-postscript" - printerror "first, or use the generic driver instead." + printerror "first, or use the generic model instead." exit 1 else DriverPath="OpenPrintingPPDs/postscript/Ricoh-MP_C6003.Postscript-Ricoh.ppd.gz" @@ -135,10 +148,10 @@ if [ "${Uname}" = "linux" ]; then DriverPath="Postscript.ppd.gz" fi else - if ! rpm -q OpenPrintingPPDs-ghostscript >/dev/null 2>&1; then + if ! sudo rpm -q OpenPrintingPPDs-ghostscript >/dev/null 2>&1; then printerror "You don't seem to have the correct printer drivers installed, please run:" printerror " sudo zypper install OpenPrintingPPDs-ghostscript" - printerror "first, or use the generic driver instead." + printerror "first, or use the generic model instead." exit 1 else if [ "${Model}" = "ricoh" ]; then @@ -153,10 +166,10 @@ if [ "${Uname}" = "linux" ]; then if [ "${Model}" = "ricoh" ]; then # This should match Fedora and other modern rpm based systems that have dnf as the primary package manager if command -v dnf >/dev/null 2>&1; then - if ! rpm -q foomatic-db-ppds 2>&1; then + if ! sudo rpm -q foomatic-db-ppds > /dev/null 2>&1; then printerror "You don't seem to have the correct printer drivers installed, please run:" printerror " sudo dnf install foomatic-db-ppds" - printerror "first, or use the generic driver instead." + printerror "first, or use the generic model instead." exit 1 else if [ "${Driver}" = "postscript" ]; then @@ -165,13 +178,18 @@ if [ "${Uname}" = "linux" ]; then DriverPath="foomatic-db-ppds/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd.gz" fi fi - fi # This should match CentOS, RHEL and other RHEL based distros that have yum as the primary package manager - if command -v yum >/dev/null 2>&1; then - if ! rpm -q foomatic-db-ppds >/dev/null 2>&1; then + elif command -v yum >/dev/null 2>&1; then + if ! sudo rpm -q foomatic-filters > /dev/null 2>&1; then + printerror "You don't seem to have the foomatic-filters package installed, please run:" + printerror " sudo yum install foomatic-filters" + printerror "first, or use the generic model instead." + exit 1 + fi + if ! sudo rpm -q foomatic-db-ppds > /dev/null 2>&1; then printerror "You don't seem to have the correct printer drivers installed, please run:" printerror " sudo yum install foomatic-db-ppds" - printerror "first, or use the generic driver instead." + printerror "first, or use the generic model instead." exit 1 else if [ "${Driver}" = "postscript" ]; then @@ -183,10 +201,10 @@ if [ "${Uname}" = "linux" ]; then fi # This should match Debian, Ubuntu and most if not all derivates that use dpkg as the primary package manager if command -v dpkg >/dev/null 2>&1; then - if ! dpkg -s openprinting-ppds > /dev/null 2>&1; then + if ! sudo dpkg -s openprinting-ppds > /dev/null 2>&1; then printerror "You must have the correct printer drivers installed, please run:" printerror " sudo apt-get install openprinting-ppds" - printerror "first, or use the generic driver instead." + printerror "first, or use the generic model instead." exit 1 else if [ "${Driver}" = "postscript" ]; then @@ -246,20 +264,21 @@ if [ "${Uname}" = "darwin" ]; then fi # Finally we can add the printer, let's remove any existing printer share with the same name first -lpadmin -x ${QueueName} > /dev/null 2&>1 +sudo lpadmin -x ${QueueName} > /dev/null 2>&1 # The Linux way if [ "${Uname}" = "linux" ]; then if [ "${Plaintext}" = "YES" ]; then - PrinterShare="smb://${Username}:${Password}@${Workgroup}/${PrintServer}/${PrintFile}" + PrinterShare="smb://${Workgroup}/${Username}:${Password}@${PrintServer}/${PrintFile}" AuthInfo="none" 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" else PrinterShare="smb://${Workgroup}/${PrintServer}/${PrintFile}" AuthInfo="username,password" fi - if ! lpadmin -p ${QueueName} \ + if ! sudo lpadmin -p ${QueueName} \ -D "FollowMe print queue at NTNU" \ + -L "Many locations" \ -v "${PrinterShare}" \ -m "${DriverPath}" \ -o auth-info-required="$AuthInfo" \ @@ -280,8 +299,9 @@ fi # The OSX way if [ "${Uname}" = "darwin" ]; then - if ! lpadmin -p ${QueueName} \ + if ! sudo lpadmin -p ${QueueName} \ -D "FollowMe print queue at NTNU" \ + -L "Many locations" \ -v "smb://${PrintServer}/${PrintFile}" \ -m "${DriverPath}" \ -o printer-is-shared=false -o printer-op-policy=authenticated \ @@ -290,8 +310,8 @@ if [ "${Uname}" = "darwin" ]; then exit 1 fi - cupsenable "${QueueName}" - cupsaccept "${QueueName}" + sudo cupsenable "${QueueName}" + sudo cupsaccept "${QueueName}" # Add credentials to the keychain if they are missing # Shamelessly stolen^W^WBorrowed from https://github.com/Orakeltjenesten/scripts/blob/33abfb353524f449f0bbdee27adb2f1f0a9756a2/print/ntnuprint-mac.sh @@ -305,15 +325,19 @@ if [ "${Uname}" = "darwin" ]; then fi # Set correct paper size and enable the duplexer option -if ! lpadmin -p ${QueueName} -o PageSize=A4 -o Option1=True; then +if ! sudo lpadmin -p ${QueueName} -o PageSize=A4 -o Option1=True; then printerror "Could not set default options on the print queue ${QueueName}. See above error for details." exit 1 fi # Set as default -if ! lpadmin -d ${QueueName}; then +if ! sudo lpadmin -d ${QueueName}; then printerror "Could not set the print queue ${QueueName} as default printer. See above error for details." exit 1 fi -echo "Printer successfully installed. You may need to restart cups for the changes to take effect." +echo "Printer successfully installed." + +if [ "${Uname}" = "linux" ] && [ "${Plaintext}" != "YES" ]; then + echo -e "\nPlease note: 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. In those cases, supply your normal NTNU username and password." +fi