]> git.ipfire.org Git - thirdparty/cups.git/blame - cgi-bin/printers.c
Sandboxed applications were not able to get the default printer (Issue #5676)
[thirdparty/cups.git] / cgi-bin / printers.c
CommitLineData
ef416fc2 1/*
7e86f2f6 2 * Printer status CGI for CUPS.
ef416fc2 3 *
9e6d7a0f 4 * Copyright 2007-2016 by Apple Inc.
7e86f2f6 5 * Copyright 1997-2006 by Easy Software Products.
ef416fc2 6 *
e3101897 7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
ef416fc2 8 */
9
10/*
11 * Include necessary headers...
12 */
13
14#include "cgi-private.h"
b423cd4c 15#include <errno.h>
ef416fc2 16
17
fa73b229 18/*
19 * Local functions...
20 */
21
58dc1933
MS
22static void do_printer_op(http_t *http, const char *printer, ipp_op_t op,
23 const char *title);
24static void show_all_printers(http_t *http, const char *username);
25static void show_printer(http_t *http, const char *printer);
fa73b229 26
27
ef416fc2 28/*
29 * 'main()' - Main entry for CGI.
30 */
31
32int /* O - Exit status */
7e86f2f6 33main(void)
ef416fc2 34{
fa73b229 35 const char *printer; /* Printer name */
36 const char *user; /* Username */
ef416fc2 37 http_t *http; /* Connection to the server */
38 ipp_t *request, /* IPP request */
39 *response; /* IPP response */
40 ipp_attribute_t *attr; /* IPP attribute */
ef416fc2 41 const char *op; /* Operation to perform, if any */
ef416fc2 42 static const char *def_attrs[] = /* Attributes for default printer */
43 {
44 "printer-name",
45 "printer-uri-supported"
46 };
47
48
49 /*
50 * Get any form variables...
51 */
52
53 cgiInitialize();
ef416fc2 54
fa73b229 55 op = cgiGetVariable("OP");
ef416fc2 56
57 /*
58 * Set the web interface section...
59 */
60
61 cgiSetVariable("SECTION", "printers");
ef55b745 62 cgiSetVariable("REFRESH_PAGE", "");
ef416fc2 63
64 /*
fa73b229 65 * See if we are displaying a printer or all printers...
ef416fc2 66 */
67
b423cd4c 68 if ((printer = getenv("PATH_INFO")) != NULL)
4744bd90 69 {
b423cd4c 70 printer ++;
ef416fc2 71
4744bd90 72 if (!*printer)
73 printer = NULL;
58dc1933
MS
74
75 if (printer)
76 cgiSetVariable("PRINTER_NAME", printer);
4744bd90 77 }
78
ef416fc2 79 /*
fa73b229 80 * See who is logged in...
ef416fc2 81 */
82
f301802f 83 user = getenv("REMOTE_USER");
ef416fc2 84
85 /*
fa73b229 86 * Connect to the HTTP server...
ef416fc2 87 */
88
fa73b229 89 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
ef416fc2 90
fa73b229 91 /*
92 * Get the default printer...
93 */
ef416fc2 94
2e4ff8af 95 if (!op || !cgiIsPOST())
ef416fc2 96 {
97 /*
98 * Get the default destination...
99 */
100
fa73b229 101 request = ippNewRequest(CUPS_GET_DEFAULT);
ef416fc2 102
103 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
104 "requested-attributes",
105 sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
106
107 if ((response = cupsDoRequest(http, request, "/")) != NULL)
108 {
109 if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
110 cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
111
112 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
113 {
114 char url[HTTP_MAX_URI]; /* New URL */
115
116
117 cgiSetVariable("DEFAULT_URI",
118 cgiRewriteURL(attr->values[0].string.text,
119 url, sizeof(url), NULL));
120 }
121
122 ippDelete(response);
123 }
124
125 /*
fa73b229 126 * See if we need to show a list of printers or the status of a
127 * single printer...
ef416fc2 128 */
129
fa73b229 130 if (!printer)
131 show_all_printers(http, user);
132 else
133 show_printer(http, printer);
134 }
58dc1933
MS
135 else if (printer)
136 {
0268488e
MS
137 if (!*op)
138 {
139 const char *server_port = getenv("SERVER_PORT");
140 /* Port number string */
141 int port = atoi(server_port ? server_port : "0");
142 /* Port number */
143 char uri[1024]; /* URL */
144
145 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri),
146 getenv("HTTPS") ? "https" : "http", NULL,
147 getenv("SERVER_NAME"), port, "/printers/%s", printer);
148
149 printf("Location: %s\n\n", uri);
150 }
151 else if (!strcmp(op, "start-printer"))
58dc1933
MS
152 do_printer_op(http, printer, IPP_RESUME_PRINTER,
153 cgiText(_("Resume Printer")));
154 else if (!strcmp(op, "stop-printer"))
155 do_printer_op(http, printer, IPP_PAUSE_PRINTER,
156 cgiText(_("Pause Printer")));
157 else if (!strcmp(op, "accept-jobs"))
158 do_printer_op(http, printer, CUPS_ACCEPT_JOBS, cgiText(_("Accept Jobs")));
159 else if (!strcmp(op, "reject-jobs"))
160 do_printer_op(http, printer, CUPS_REJECT_JOBS, cgiText(_("Reject Jobs")));
96be8b6c
MS
161 else if (!strcmp(op, "cancel-jobs"))
162 do_printer_op(http, printer, IPP_OP_CANCEL_JOBS, cgiText(_("Cancel Jobs")));
88f9aafc 163 else if (!_cups_strcasecmp(op, "print-self-test-page"))
58dc1933
MS
164 cgiPrintCommand(http, printer, "PrintSelfTestPage",
165 cgiText(_("Print Self-Test Page")));
88f9aafc 166 else if (!_cups_strcasecmp(op, "clean-print-heads"))
58dc1933
MS
167 cgiPrintCommand(http, printer, "Clean all",
168 cgiText(_("Clean Print Heads")));
88f9aafc 169 else if (!_cups_strcasecmp(op, "print-test-page"))
58dc1933 170 cgiPrintTestPage(http, printer);
88f9aafc 171 else if (!_cups_strcasecmp(op, "move-jobs"))
58dc1933
MS
172 cgiMoveJobs(http, printer, 0);
173 else
174 {
175 /*
176 * Unknown/bad operation...
177 */
178
179 cgiStartHTML(printer);
180 cgiCopyTemplateLang("error-op.tmpl");
181 cgiEndHTML();
182 }
183 }
fa73b229 184 else
185 {
186 /*
187 * Unknown/bad operation...
188 */
ef416fc2 189
58dc1933 190 cgiStartHTML(cgiText(_("Printers")));
fa73b229 191 cgiCopyTemplateLang("error-op.tmpl");
192 cgiEndHTML();
193 }
ef416fc2 194
fa73b229 195 /*
196 * Close the HTTP server connection...
197 */
198
199 httpClose(http);
200
201 /*
202 * Return with no errors...
203 */
ef416fc2 204
fa73b229 205 return (0);
206}
ef416fc2 207
ef416fc2 208
b423cd4c 209/*
58dc1933 210 * 'do_printer_op()' - Do a printer operation.
b423cd4c 211 */
212
58dc1933
MS
213static void
214do_printer_op(http_t *http, /* I - HTTP connection */
215 const char *printer, /* I - Printer name */
216 ipp_op_t op, /* I - Operation to perform */
217 const char *title) /* I - Title of page */
b423cd4c 218{
58dc1933 219 ipp_t *request; /* IPP request */
b423cd4c 220 char uri[HTTP_MAX_URI], /* Printer URI */
58dc1933 221 resource[HTTP_MAX_URI]; /* Path for request */
b423cd4c 222
b423cd4c 223
224 /*
58dc1933 225 * Build a printer request, which requires the following
b423cd4c 226 * attributes:
227 *
228 * attributes-charset
229 * attributes-natural-language
230 * printer-uri
b423cd4c 231 */
232
58dc1933 233 request = ippNewRequest(op);
b423cd4c 234
58dc1933
MS
235 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
236 "localhost", 0, "/printers/%s", printer);
b423cd4c 237 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
238 NULL, uri);
239
b423cd4c 240 /*
241 * Do the request and get back a response...
242 */
243
58dc1933
MS
244 snprintf(resource, sizeof(resource), "/printers/%s", printer);
245 ippDelete(cupsDoRequest(http, request, resource));
b423cd4c 246
58dc1933
MS
247 if (cupsLastError() == IPP_NOT_AUTHORIZED)
248 {
249 puts("Status: 401\n");
250 exit(0);
b423cd4c 251 }
58dc1933
MS
252 else if (cupsLastError() > IPP_OK_CONFLICT)
253 {
254 cgiStartHTML(title);
f3c17241 255 cgiShowIPPError(_("Unable to do maintenance command"));
58dc1933
MS
256 }
257 else
b423cd4c 258 {
259 /*
58dc1933 260 * Redirect successful updates back to the printer page...
b423cd4c 261 */
262
58dc1933
MS
263 char url[1024], /* Printer/class URL */
264 refresh[1024]; /* Refresh URL */
b423cd4c 265
b423cd4c 266
58dc1933
MS
267 cgiRewriteURL(uri, url, sizeof(url), NULL);
268 cgiFormEncode(uri, url, sizeof(uri));
52f6f666 269 snprintf(refresh, sizeof(refresh), "5;URL=%s", uri);
58dc1933 270 cgiSetVariable("refresh_page", refresh);
b423cd4c 271
58dc1933
MS
272 cgiStartHTML(title);
273
274 if (op == IPP_PAUSE_PRINTER)
275 cgiCopyTemplateLang("printer-stop.tmpl");
276 else if (op == IPP_RESUME_PRINTER)
277 cgiCopyTemplateLang("printer-start.tmpl");
278 else if (op == CUPS_ACCEPT_JOBS)
279 cgiCopyTemplateLang("printer-accept.tmpl");
280 else if (op == CUPS_REJECT_JOBS)
281 cgiCopyTemplateLang("printer-reject.tmpl");
96be8b6c
MS
282 else if (op == IPP_OP_CANCEL_JOBS)
283 cgiCopyTemplateLang("printer-cancel-jobs.tmpl");
b423cd4c 284 }
285
286 cgiEndHTML();
287}
288
289
fa73b229 290/*
291 * 'show_all_printers()' - Show all printers...
292 */
ef416fc2 293
58dc1933 294static void
fa73b229 295show_all_printers(http_t *http, /* I - Connection to server */
296 const char *user) /* I - Username */
297{
298 int i; /* Looping var */
299 ipp_t *request, /* IPP request */
300 *response; /* IPP response */
301 cups_array_t *printers; /* Array of printer objects */
e6013cfa 302 ipp_attribute_t *printer; /* Printer object */
9e6d7a0f 303 int first, /* First printer to show */
fa73b229 304 count; /* Number of printers */
305 const char *var; /* Form variable */
306 void *search; /* Search data */
2e4ff8af 307 char val[1024]; /* Form variable */
ef416fc2 308
ef416fc2 309
bd7854cb 310 fprintf(stderr, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n",
f301802f 311 http, user ? user : "(null)");
bd7854cb 312
fa73b229 313 /*
314 * Show the standard header...
315 */
ef416fc2 316
fa73b229 317 cgiStartHTML(cgiText(_("Printers")));
ef416fc2 318
fa73b229 319 /*
320 * Build a CUPS_GET_PRINTERS request, which requires the following
321 * attributes:
322 *
323 * attributes-charset
324 * attributes-natural-language
325 * printer-type
326 * printer-type-mask
327 * requesting-user-name
328 */
ef416fc2 329
fa73b229 330 request = ippNewRequest(CUPS_GET_PRINTERS);
ef416fc2 331
fa73b229 332 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
333 "printer-type", 0);
334 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
335 "printer-type-mask", CUPS_PRINTER_CLASS);
ef416fc2 336
f301802f 337 if (user)
338 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
339 "requesting-user-name", NULL, user);
ef416fc2 340
fa73b229 341 cgiGetAttributes(request, "printers.tmpl");
ef416fc2 342
fa73b229 343 /*
344 * Do the request and get back a response...
345 */
346
347 if ((response = cupsDoRequest(http, request, "/")) != NULL)
348 {
ef416fc2 349 /*
fa73b229 350 * Get a list of matching job objects.
ef416fc2 351 */
352
2e4ff8af
MS
353 if ((var = cgiGetVariable("QUERY")) != NULL &&
354 !cgiGetVariable("CLEAR"))
fa73b229 355 search = cgiCompileSearch(var);
356 else
357 search = NULL;
ef416fc2 358
fa73b229 359 printers = cgiGetIPPObjects(response, search);
360 count = cupsArrayCount(printers);
ef416fc2 361
fa73b229 362 if (search)
363 cgiFreeSearch(search);
ef416fc2 364
365 /*
fa73b229 366 * Figure out which printers to display...
ef416fc2 367 */
368
fa73b229 369 if ((var = cgiGetVariable("FIRST")) != NULL)
370 first = atoi(var);
371 else
372 first = 0;
ef416fc2 373
fa73b229 374 if (first >= count)
375 first = count - CUPS_PAGE_MAX;
ef416fc2 376
fa73b229 377 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
ef416fc2 378
fa73b229 379 if (first < 0)
380 first = 0;
ef416fc2 381
2e4ff8af
MS
382 sprintf(val, "%d", count);
383 cgiSetVariable("TOTAL", val);
ef416fc2 384
9e6d7a0f
MS
385 for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, first);
386 i < CUPS_PAGE_MAX && printer;
387 i ++, printer = (ipp_attribute_t *)cupsArrayNext(printers))
388 cgiSetIPPObjectVars(printer, NULL, i);
ef416fc2 389
fa73b229 390 /*
391 * Save navigation URLs...
392 */
ef416fc2 393
2e4ff8af 394 cgiSetVariable("THISURL", "/printers/");
fa73b229 395
396 if (first > 0)
397 {
2e4ff8af
MS
398 sprintf(val, "%d", first - CUPS_PAGE_MAX);
399 cgiSetVariable("PREV", val);
ef416fc2 400 }
fa73b229 401
402 if ((first + CUPS_PAGE_MAX) < count)
403 {
2e4ff8af
MS
404 sprintf(val, "%d", first + CUPS_PAGE_MAX);
405 cgiSetVariable("NEXT", val);
fa73b229 406 }
407
9e6d7a0f
MS
408 if (count > CUPS_PAGE_MAX)
409 {
410 snprintf(val, sizeof(val), "%d", CUPS_PAGE_MAX * (count / CUPS_PAGE_MAX));
411 cgiSetVariable("LAST", val);
412 }
413
ef416fc2 414 /*
fa73b229 415 * Then show everything...
ef416fc2 416 */
417
fa73b229 418 cgiCopyTemplateLang("search.tmpl");
ef416fc2 419
fa73b229 420 cgiCopyTemplateLang("printers-header.tmpl");
ef416fc2 421
b19ccc9e 422 if (count > CUPS_PAGE_MAX)
fa73b229 423 cgiCopyTemplateLang("pager.tmpl");
ef416fc2 424
fa73b229 425 cgiCopyTemplateLang("printers.tmpl");
ef416fc2 426
b19ccc9e 427 if (count > CUPS_PAGE_MAX)
fa73b229 428 cgiCopyTemplateLang("pager.tmpl");
ef416fc2 429
430 /*
fa73b229 431 * Delete the response...
ef416fc2 432 */
433
bd7854cb 434 cupsArrayDelete(printers);
fa73b229 435 ippDelete(response);
436 }
437 else
438 {
439 /*
440 * Show the error...
441 */
ef416fc2 442
f3c17241 443 cgiShowIPPError(_("Unable to get printer list"));
fa73b229 444 }
ef416fc2 445
fa73b229 446 cgiEndHTML();
447}
ef416fc2 448
ef416fc2 449
fa73b229 450/*
451 * 'show_printer()' - Show a single printer.
452 */
ef416fc2 453
58dc1933 454static void
fa73b229 455show_printer(http_t *http, /* I - Connection to server */
456 const char *printer) /* I - Name of printer */
457{
458 ipp_t *request, /* IPP request */
459 *response; /* IPP response */
460 ipp_attribute_t *attr; /* IPP attribute */
461 char uri[HTTP_MAX_URI]; /* Printer URI */
462 char refresh[1024]; /* Refresh URL */
ef416fc2 463
ef416fc2 464
bd7854cb 465 fprintf(stderr, "DEBUG: show_printer(http=%p, printer=\"%s\")\n",
f301802f 466 http, printer ? printer : "(null)");
bd7854cb 467
fa73b229 468 /*
469 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
470 * attributes:
471 *
472 * attributes-charset
473 * attributes-natural-language
474 * printer-uri
475 */
476
477 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
478
a4d04587 479 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
480 "localhost", 0, "/printers/%s", printer);
fa73b229 481 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
482 uri);
483
b19ccc9e 484 cgiGetAttributes(request, "printer.tmpl");
fa73b229 485
486 /*
487 * Do the request and get back a response...
488 */
ef416fc2 489
fa73b229 490 if ((response = cupsDoRequest(http, request, "/")) != NULL)
491 {
ef416fc2 492 /*
fa73b229 493 * Got the result; set the CGI variables and check the status of a
494 * single-queue request...
ef416fc2 495 */
496
fa73b229 497 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
498
499 if (printer && (attr = ippFindAttribute(response, "printer-state",
500 IPP_TAG_ENUM)) != NULL &&
501 attr->values[0].integer == IPP_PRINTER_PROCESSING)
ef416fc2 502 {
fa73b229 503 /*
504 * Printer is processing - automatically refresh the page until we
505 * are done printing...
506 */
ef416fc2 507
fa73b229 508 cgiFormEncode(uri, printer, sizeof(uri));
f301802f 509 snprintf(refresh, sizeof(refresh), "10;URL=/printers/%s", uri);
fa73b229 510 cgiSetVariable("refresh_page", refresh);
ef416fc2 511 }
ef416fc2 512
fa73b229 513 /*
514 * Delete the response...
515 */
516
517 ippDelete(response);
ef416fc2 518
519 /*
520 * Show the standard header...
521 */
522
fa73b229 523 cgiStartHTML(printer);
ef416fc2 524
525 /*
fa73b229 526 * Show the printer status...
ef416fc2 527 */
528
b19ccc9e 529 cgiCopyTemplateLang("printer.tmpl");
ef416fc2 530
fa73b229 531 /*
532 * Show jobs for the specified printer...
533 */
ef416fc2 534
fa73b229 535 cgiCopyTemplateLang("printer-jobs-header.tmpl");
536 cgiShowJobs(http, printer);
537 }
538 else
539 {
540 /*
541 * Show the IPP error...
542 */
ef416fc2 543
fa73b229 544 cgiStartHTML(printer);
f3c17241 545 cgiShowIPPError(_("Unable to get printer status"));
fa73b229 546 }
ef416fc2 547
fa73b229 548 cgiEndHTML();
ef416fc2 549}