summaryrefslogtreecommitdiffstats
path: root/src/pwgui/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/pwgui/main.cpp')
-rw-r--r--src/pwgui/main.cpp174
1 files changed, 99 insertions, 75 deletions
diff --git a/src/pwgui/main.cpp b/src/pwgui/main.cpp
index e673108..4dcfbfb 100644
--- a/src/pwgui/main.cpp
+++ b/src/pwgui/main.cpp
@@ -95,8 +95,9 @@ int main(int argc, char *argv[])
// Mimic cups behaviour wrt dropping privs (Only 0700 or 0500 == root)
helper_loadlpuser();
struct stat st;
- if (stat(backend, &st) != 0 || (st.st_mode & 0011) != 0)
+ if (stat(backend, &st) != 0 || (st.st_mode & 0011) != 0) {
helper_dropprivs();
+ }
WARNING("Over and out.\n");
execv(backend, argv);
exit(127);
@@ -118,7 +119,9 @@ int main(int argc, char *argv[])
int total = 0;
for (;;) {
bytes = read(STDIN_FILENO, buffer, BUFLEN);
- if (bytes == 0) break;
+ if (bytes == 0) {
+ break;
+ }
if (bytes < 0) {
helper_messageBox("PrinterGUI", "Konnte den Druckjob nicht auf STDIN empfangen.");
ERROR("Could not read print job from STDIN. Over and out.\n");
@@ -152,7 +155,9 @@ int main(int argc, char *argv[])
// Try right away with what we got
spoolres = run_backend(backend, device, argv[1], argv[2], argv[3], argv[4], argv[5], tmpfile, NULL);
- if (spoolres != CUPS_BACKEND_AUTH_REQUIRED) return helper_cupsError(spoolres); // Yay
+ if (spoolres != CUPS_BACKEND_AUTH_REQUIRED) {
+ return helper_cupsError(spoolres); // Yay
+ }
// Seems we need the dialog
int status;
@@ -193,7 +198,9 @@ int main(int argc, char *argv[])
}
creds[bytes] = '\0';
int len = strlen(creds);
- if (len < bytes) pass = creds + len + 1;
+ if (len < bytes) {
+ pass = creds + len + 1;
+ }
// Run backend with pimped user/pass
status = run_backend(backend, device, argv[1], creds, argv[3], argv[4], argv[5], tmpfile, pass);
helper_cupsError(status);
@@ -214,16 +221,22 @@ static int run_backend(char *backend, char *uri, char *jobid, char *user, char *
dup2(pipefd[1], 2); // send stderr to pipe
close(pipefd[1]); // no longer needed after dup
// Newer backend can read credentials from environment
- if (user != NULL) setenv("AUTH_USERNAME", user, 1);
- if (password != NULL) setenv("AUTH_PASSWORD", password, 1);
+ if (user != NULL) {
+ setenv("AUTH_USERNAME", user, 1);
+ }
+ if (password != NULL) {
+ setenv("AUTH_PASSWORD", password, 1);
+ }
// Compute proper string length
int len = strlen(uri) + 20 + strlen("?waitprinter=no&waitjob=no");
- if (user != NULL)
+ if (user != NULL) {
len += strlen(user) * 3;
+ }
- if(password != NULL)
+ if(password != NULL) {
len += strlen(password) * 3;
+ }
char *newstr = (char*)malloc(len);
@@ -237,30 +250,28 @@ static int run_backend(char *backend, char *uri, char *jobid, char *user, char *
*ptr++ = '@';
ptr += sprintf(ptr, "%s", uri + 8); // TODO: If uri already contains credentials, skip over them (check if there's a @ before the first /, starting at index 8)
// Since we've already forked and execv anyways there's no need to clean up anything...
- }
- else
+ } else {
strcpy(newstr, uri);
+ }
uri = newstr;
// Append wait options
if (strncmp(uri, "ipp:", 4) == 0
- || strncmp(uri, "ipp14:", 6) == 0
- || strncmp(uri, "ipps:", 5) == 0
- || strncmp(uri, "http:", 5) == 0
- || strncmp(uri, "https:", 6) == 0)
- {
- if (strchr(uri, '?') != NULL)
- {
+ || strncmp(uri, "ipp14:", 6) == 0
+ || strncmp(uri, "ipps:", 5) == 0
+ || strncmp(uri, "http:", 5) == 0
+ || strncmp(uri, "https:", 6) == 0) {
+ if (strchr(uri, '?') != NULL) {
// Question mark has been found, parse the options for waitprinter and
// waitjob.
- if(strstr(uri, "waitprinter=") == NULL)
+ if(strstr(uri, "waitprinter=") == NULL) {
strcat(uri, "&waitprinter=no");
- if(strstr(uri, "waitjob=") == NULL)
+ }
+ if(strstr(uri, "waitjob=") == NULL) {
strcat(uri, "&waitjob=no");
- }
- else
- {
+ }
+ } else {
// No question mark found. Fine hence just add the options
strcat(uri, "?waitprinter=no&waitjob=no");
}
@@ -279,7 +290,9 @@ static int run_backend(char *backend, char *uri, char *jobid, char *user, char *
args[7] = NULL;
// Priv dropping
struct stat st;
- if (stat(backend, &st) != 0 || (st.st_mode & 0011) != 0) helper_dropprivs();
+ if (stat(backend, &st) != 0 || (st.st_mode & 0011) != 0) {
+ helper_dropprivs();
+ }
// Exec
execv(backend, args);
exit(127);
@@ -329,7 +342,9 @@ static int run_backend(char *backend, char *uri, char *jobid, char *user, char *
return CUPS_BACKEND_FAILED;
}
status = WEXITSTATUS(status);
- if (status != CUPS_BACKEND_OK) ERROR("Backend returned %d\n", status);
+ if (status != CUPS_BACKEND_OK) {
+ ERROR("Backend returned %d\n", status);
+ }
return status;
}
@@ -383,7 +398,9 @@ static bool helper_getpiduid(char *user, char *title)
while ((ret = read(fh, ptr, ENVLEN - (ptr - gui_env) - 2)) > 0) {
bytes += ret;
ptr += ret;
- if (bytes + 3 >= ENVLEN) break;
+ if (bytes + 3 >= ENVLEN) {
+ break;
+ }
}
close(fh);
if (bytes >= 0) {
@@ -398,7 +415,9 @@ static bool helper_getpiduid(char *user, char *title)
static bool helper_loadlpuser()
{
struct passwd *pw = getpwnam("lp");
- if (pw == NULL) return false;
+ if (pw == NULL) {
+ return false;
+ }
ruid = pw->pw_uid;
rgid = pw->pw_gid;
return true;
@@ -406,7 +425,9 @@ static bool helper_loadlpuser()
static void helper_dropprivs()
{
- if (ruid == 0) return;
+ if (ruid == 0) {
+ return;
+ }
initgroups(ruser, rgid);
setgid(rgid);
setuid(ruid);
@@ -451,8 +472,11 @@ static char *helper_urlencode(char *source, char *enc)
unsigned char *s = (unsigned char*)source;
for (; *s != '\0'; s++) {
- if (table[*s] != '\0') len = sprintf(enc, "%c", table[*s]);
- else len = sprintf(enc, "%%%02X", *s);
+ if (table[*s] != '\0') {
+ len = sprintf(enc, "%c", table[*s]);
+ } else {
+ len = sprintf(enc, "%%%02X", *s);
+ }
enc += len;
}
*enc = '\0';
@@ -468,52 +492,52 @@ static char *helper_urlencode(char *source, char *enc)
static int helper_cupsError(const int code)
{
switch (code) {
- case CUPS_BACKEND_OK:
- helper_messageBox("CUPS", "Der Druckauftrag wurde erfolgreich an den print server übermittelt", false);
- return CUPS_BACKEND_OK;
-
- case CUPS_BACKEND_FAILED:
- // The print file was not successfully transmitted to the device or remote server. The scheduler will respond to this by canceling the job, retrying the job, or stopping the queue depending on the state of the error-policy attribute.
- helper_messageBox("CUPS Fehler", "Fehler beim lokalen Verarbeiten des Druckauftrags");
- return CUPS_BACKEND_HOLD;
-
- case CUPS_BACKEND_AUTH_REQUIRED:
- // The print file was not successfully transmitted because valid authentication information is required. The scheduler will respond to this by holding the job and adding the "cups-held-for-authentication" keyword to the "job-reasons" attribute.
- helper_messageBox("CUPS Fehler", "Authentifizierung am Druckserver/Drucker fehlgeschlagen");
- break;
-
- case CUPS_BACKEND_HOLD:
- // The print file was not successfully transmitted because it cannot be printed at this time. The scheduler will respond to this by holding the job.
- helper_messageBox("CUPS Fehler", "Der Drucker hat die Annahme des Druckauftrags verweigert (3)");
- break;
-
- case CUPS_BACKEND_STOP:
- // The print file was not successfully transmitted because it cannot be printed at this time. The scheduler will respond to this by stopping the queue.
- helper_messageBox("CUPS Fehler", "Der Drucker hat die Annahme des Druckauftrags verweigert (4)");
- return CUPS_BACKEND_HOLD;
-
- case CUPS_BACKEND_CANCEL:
- // The print file was not successfully transmitted because one or more attributes are not supported or the job was canceled at the printer. The scheduler will respond to this by canceling the job.
- helper_messageBox("CUPS Fehler", "Fehler 5 beim lokalen Verarbeiten des Druckauftrags");
- return CUPS_BACKEND_HOLD;
-
- case CUPS_BACKEND_RETRY:
- // The print file was not successfully transmitted because of a temporary issue. The scheduler will retry the job at a future time - other jobs may print before this one.
- helper_messageBox("CUPS Fehler", "Fehler 6 beim lokalen Verarbeiten des Druckauftrags");
- return CUPS_BACKEND_HOLD;
-
- case CUPS_BACKEND_RETRY_CURRENT:
- // The print file was not successfully transmitted because of a temporary issue. The scheduler will retry the job immediately without allowing intervening jobs.
- helper_messageBox("CUPS Fehler", "Fehler 7 beim lokalen Verarbeiten des Druckauftrags");
- return CUPS_BACKEND_HOLD;
-
- case CUSTOM_CUPS_NO_SUCH_PRINTER:
- helper_messageBox("CUPS Fehler", "Die Druckerwarteschlange existiert nicht auf dem print server. Fehlerhafte lokale printers.conf?");
- return CUPS_BACKEND_CANCEL;
- default:
- ERROR("Unknown cupsError code %d\n", code);
- helper_messageBox("CUPS Fehler", "Unbekannter Fehler beim Drucken", true);
- break;
+ case CUPS_BACKEND_OK:
+ helper_messageBox("CUPS", "Der Druckauftrag wurde erfolgreich an den print server übermittelt", false);
+ return CUPS_BACKEND_OK;
+
+ case CUPS_BACKEND_FAILED:
+ // The print file was not successfully transmitted to the device or remote server. The scheduler will respond to this by canceling the job, retrying the job, or stopping the queue depending on the state of the error-policy attribute.
+ helper_messageBox("CUPS Fehler", "Fehler beim lokalen Verarbeiten des Druckauftrags");
+ return CUPS_BACKEND_HOLD;
+
+ case CUPS_BACKEND_AUTH_REQUIRED:
+ // The print file was not successfully transmitted because valid authentication information is required. The scheduler will respond to this by holding the job and adding the "cups-held-for-authentication" keyword to the "job-reasons" attribute.
+ helper_messageBox("CUPS Fehler", "Authentifizierung am Druckserver/Drucker fehlgeschlagen");
+ break;
+
+ case CUPS_BACKEND_HOLD:
+ // The print file was not successfully transmitted because it cannot be printed at this time. The scheduler will respond to this by holding the job.
+ helper_messageBox("CUPS Fehler", "Der Drucker hat die Annahme des Druckauftrags verweigert (3)");
+ break;
+
+ case CUPS_BACKEND_STOP:
+ // The print file was not successfully transmitted because it cannot be printed at this time. The scheduler will respond to this by stopping the queue.
+ helper_messageBox("CUPS Fehler", "Der Drucker hat die Annahme des Druckauftrags verweigert (4)");
+ return CUPS_BACKEND_HOLD;
+
+ case CUPS_BACKEND_CANCEL:
+ // The print file was not successfully transmitted because one or more attributes are not supported or the job was canceled at the printer. The scheduler will respond to this by canceling the job.
+ helper_messageBox("CUPS Fehler", "Fehler 5 beim lokalen Verarbeiten des Druckauftrags");
+ return CUPS_BACKEND_HOLD;
+
+ case CUPS_BACKEND_RETRY:
+ // The print file was not successfully transmitted because of a temporary issue. The scheduler will retry the job at a future time - other jobs may print before this one.
+ helper_messageBox("CUPS Fehler", "Fehler 6 beim lokalen Verarbeiten des Druckauftrags");
+ return CUPS_BACKEND_HOLD;
+
+ case CUPS_BACKEND_RETRY_CURRENT:
+ // The print file was not successfully transmitted because of a temporary issue. The scheduler will retry the job immediately without allowing intervening jobs.
+ helper_messageBox("CUPS Fehler", "Fehler 7 beim lokalen Verarbeiten des Druckauftrags");
+ return CUPS_BACKEND_HOLD;
+
+ case CUSTOM_CUPS_NO_SUCH_PRINTER:
+ helper_messageBox("CUPS Fehler", "Die Druckerwarteschlange existiert nicht auf dem print server. Fehlerhafte lokale printers.conf?");
+ return CUPS_BACKEND_CANCEL;
+ default:
+ ERROR("Unknown cupsError code %d\n", code);
+ helper_messageBox("CUPS Fehler", "Unbekannter Fehler beim Drucken", true);
+ break;
}
return code;
}