From 22478a51a4a897be6e67fe1f2a5165f3a27f4613 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 21 Dec 2016 16:05:37 +0100 Subject: [maingui] Support SecuredPrint (Canon) --- src/maingui/printergui.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++--- src/maingui/printergui.h | 5 ++- src/maingui/printergui.ui | 48 ++++++++++++++++++++---- 3 files changed, 133 insertions(+), 13 deletions(-) diff --git a/src/maingui/printergui.cpp b/src/maingui/printergui.cpp index a7f993b..8758a6b 100644 --- a/src/maingui/printergui.cpp +++ b/src/maingui/printergui.cpp @@ -5,8 +5,8 @@ #include #include #include +#include #include -#include static QStringList knownColorOptions = QStringList() << "ColorModel" << "XRXColor"; static QStringList knownDuplexOptions = QStringList() << "Duplex"; @@ -20,6 +20,7 @@ static char* cleanCupsString(const char* in); PrinterGui::PrinterGui(char *argv[], QWidget *parent) : QDialog(parent), ui(new Ui::PrinterGui), + user(NULL), statusBar(NULL), bgTimeout(-1), jobId(0) @@ -29,10 +30,29 @@ PrinterGui::PrinterGui(char *argv[], QWidget *parent) : // Set username // Do not use getlogin[_r] as it doesn't fucking work! struct passwd *pw = getpwuid(getuid()); - if (pw != NULL && pw->pw_name != NULL && pw->pw_name[0] != '\0') { + if (pw != NULL && pw->pw_dir != NULL && pw->pw_dir[0] == '/') { + // Special case - .account file in user home, but only if it is owned by root + QFile file(QString(pw->pw_dir).append("/.account")); + if (file.exists()) { + QFileInfo fi(file); + // If owned by root, not writable by group or other, read user stored inside + if ((fi.permissions() & (QFile::WriteGroup | QFile::WriteOther)) == 0 && fi.owner() == "root" && file.open(QIODevice::ReadOnly)) { + char buffer[20]; + qint64 ret = file.readLine(buffer, sizeof(buffer)); + if (ret > 0) { + if (buffer[ret - 1] == '\n') { + buffer[ret - 1] = '\0'; + } + this->user = strdup(buffer); + } + } + } + } + if (this->user == NULL && pw != NULL && pw->pw_name != NULL && pw->pw_name[0] != '\0') { // Detect real name this->user = strdup(pw->pw_name); - } else { + } + if (this->user == NULL) { // Fallback to what command line says this->user = strdup(argv[1]); } @@ -51,6 +71,7 @@ PrinterGui::PrinterGui(char *argv[], QWidget *parent) : this->bgTimer = new QTimer(this); connect(bgTimer, SIGNAL(timeout()), this, SLOT(bgTimer_timeout())); this->bgTimer->start(1000); + ui->txtUserId->setText(this->user); } // ____________________________________________________________________________ @@ -82,7 +103,7 @@ void PrinterGui::initializeUI() /* Initialize Treeview */ ui->printerList->setColumnCount(3); - ui->printerList->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum); + //ui->printerList->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum); // Rename headers QStringList h; @@ -132,6 +153,49 @@ void PrinterGui::initializeUI() this->activateWindow(); } +void PrinterGui::handleTrustedPrint(ppd_file_t *ppd) +{ + bool supported = false; + bool enabled = false; + QString pwProperty, pwMask; + static QRegExpValidator sevenNum(QRegExp("^[0-9]{1,7}$")); + QRegExpValidator *pwValidator = NULL; + + + for (ppd_option_t *o = ppdFirstOption(ppd); o != NULL; o = ppdNextOption(ppd)) { + if (strcmp(o->keyword, "UseSecure") == 0) { + supported = true; + enabled = (strcmp(o->defchoice, "On") == 0); + } else if (strcmp(o->keyword, "SecuredPassword") == 0) { + pwProperty = o->keyword; + pwValidator = &sevenNum; + } else if (strcmp(o->keyword, "UserPassword") == 0) { + if (pwProperty.isEmpty()) { + pwProperty = o->keyword; + pwValidator = &sevenNum; + } + } + } + ui->txtUserPin->setProperty("key", pwProperty); + ui->txtUserPin->setValidator(pwValidator); + ui->chkSecurePrint->setChecked(enabled); + for (int i = 0; i < ui->gridSecurePrint->count(); ++i) { + QWidget *child = ui->gridSecurePrint->itemAt(i)->widget(); + if (child != NULL) { + child->setVisible(supported); + } + } +} + +void PrinterGui::on_chkSecurePrint_stateChanged(int state) +{ + ui->txtUserId->setEnabled(state == Qt::Checked); + ui->txtUserPin->setEnabled(state == Qt::Checked); + if (state == Qt::Checked) { + ui->txtUserPin->setFocus(); + } +} + static void enableOptionSelection(ppd_file_t *ppd, QStringList &nameList, QComboBox *combo, QLabel *label, bool forceDisabled) { // Check for each option if it matches an @@ -196,6 +260,7 @@ void PrinterGui::on_printerList_currentItemChanged(QTreeWidgetItem *current, QTr enableOptionSelection(ppd, knownDuplexOptions, ui->comboBoxSides, ui->label_duplex, false); // TODO: Make it so this selection overrides what the document specifies enableOptionSelection(ppd, knownPageSizeOptions, ui->cboPaperSize, ui->lblPaperSize, true); + handleTrustedPrint(ppd); ppdClose(ppd); } else { qDebug() << "ppd is null"<< dest->name << cupsLastErrorString(); @@ -267,6 +332,8 @@ void PrinterGui::on_buttonPrint_clicked() num_dests, dests); + // In dest we have the default options + // Color QString colorKey = ui->comboBoxColor->property("key").toString(); if (!colorKey.isEmpty()) { @@ -301,7 +368,21 @@ void PrinterGui::on_buttonPrint_clicked() dest->num_options = cupsRemoveOption("InputSlot", dest->num_options, &(dest->options)); - cupsSetUser(this->user); + // If we use secured print, allow overriding username + if (ui->txtUserId->isHidden() || ui->txtUserId->text().isEmpty()) { + cupsSetUser(this->user); + } else { + cupsSetUser(ui->txtUserId->text().toUtf8().constData()); + } + // Secured print -> set pin + if (!ui->txtUserPin->isHidden()) { + QString pinKey = ui->txtUserPin->property("key").toString(); + dest->num_options = cupsAddOption(pinKey.toUtf8().constData(), + ui->txtUserPin->text().toUtf8().constData(), + dest->num_options, + &(dest->options)); + } + char jobtitle[250]; const char *docName; docName = getenv("J"); @@ -342,6 +423,8 @@ void PrinterGui::bgTimer_timeout() { if (this->bgTimeout == -1) { // Could do something here every second.... + this->raise(); + this->activateWindow(); return; } if (++this->bgTimeout > 120) { diff --git a/src/maingui/printergui.h b/src/maingui/printergui.h index 88ec685..55a4ddb 100644 --- a/src/maingui/printergui.h +++ b/src/maingui/printergui.h @@ -5,7 +5,8 @@ #include #include #include -#include "cups/cups.h" +#include +#include namespace Ui { @@ -35,11 +36,13 @@ private slots: void on_printerList_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous); void on_buttonCancel_clicked(); void on_buttonPrint_clicked(); + void on_chkSecurePrint_stateChanged(int state); void bgTimer_timeout(); private: Ui::PrinterGui *ui; void initializeUI(); + void handleTrustedPrint(ppd_file_t *ppd); cups_dest_t *dests; int num_dests; char * user; diff --git a/src/maingui/printergui.ui b/src/maingui/printergui.ui index 4666390..047c5ac 100644 --- a/src/maingui/printergui.ui +++ b/src/maingui/printergui.ui @@ -7,7 +7,7 @@ 0 0 629 - 330 + 394 @@ -19,14 +19,14 @@ Druckauftrag - - - 0 - + 5 - + + 0 + + 5 @@ -46,6 +46,40 @@ + + + + + + + + + UserID + + + + + + + + + + PIN + + + + + + + Geschützter Druck + + + true + + + + + @@ -220,7 +254,7 @@ - + 0 -- cgit v1.2.3-55-g7522