2 * Administration CGI for CUPS.
4 * Copyright © 2007-2019 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include "cgi-private.h"
16 #include <cups/http-private.h>
17 #include <cups/ppd-private.h>
18 #include <cups/adminutil.h>
31 static int current_device
= 0; /* Current device shown */
38 static void choose_device_cb(const char *device_class
, const char *device_id
, const char *device_info
, const char *device_make_and_model
, const char *device_uri
, const char *device_location
, const char *title
);
39 static void do_am_class(http_t
*http
, int modify
);
40 static void do_am_printer(http_t
*http
, int modify
);
41 static void do_config_server(http_t
*http
);
42 static void do_delete_class(http_t
*http
);
43 static void do_delete_printer(http_t
*http
);
44 static void do_list_printers(http_t
*http
);
45 static void do_menu(http_t
*http
);
46 static void do_set_allowed_users(http_t
*http
);
47 static void do_set_default(http_t
*http
);
48 static void do_set_options(http_t
*http
, int is_class
);
49 static void do_set_sharing(http_t
*http
);
50 static char *get_option_value(ppd_file_t
*ppd
, const char *name
,
51 char *buffer
, size_t bufsize
);
52 static double get_points(double number
, const char *uval
);
53 static char *get_printer_ppd(const char *uri
, char *buffer
, size_t bufsize
);
57 * 'main()' - Main entry for CGI.
60 int /* O - Exit status */
63 http_t
*http
; /* Connection to the server */
64 const char *op
; /* Operation name */
68 * Connect to the HTTP server...
71 fputs("DEBUG: admin.cgi started...\n", stderr
);
73 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
77 perror("ERROR: Unable to connect to cupsd");
78 fprintf(stderr
, "DEBUG: cupsServer()=\"%s\"\n",
79 cupsServer() ? cupsServer() : "(null)");
80 fprintf(stderr
, "DEBUG: ippPort()=%d\n", ippPort());
81 fprintf(stderr
, "DEBUG: cupsEncryption()=%d\n", cupsEncryption());
85 fprintf(stderr
, "DEBUG: http=%p\n", http
);
88 * Set the web interface section...
91 cgiSetVariable("SECTION", "admin");
92 cgiSetVariable("REFRESH_PAGE", "");
95 * See if we have form data...
98 if (!cgiInitialize() || !cgiGetVariable("OP"))
101 * Nope, send the administration menu...
104 fputs("DEBUG: No form data, showing main menu...\n", stderr
);
108 else if ((op
= cgiGetVariable("OP")) != NULL
&& cgiIsPOST())
111 * Do the operation...
114 fprintf(stderr
, "DEBUG: op=\"%s\"...\n", op
);
118 const char *printer
= getenv("PRINTER_NAME"),
119 /* Printer or class name */
120 *server_port
= getenv("SERVER_PORT");
121 /* Port number string */
122 int port
= atoi(server_port
? server_port
: "0");
124 char uri
[1024]; /* URL */
127 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
128 getenv("HTTPS") ? "https" : "http", NULL
,
129 getenv("SERVER_NAME"), port
, "/%s/%s",
130 cgiGetVariable("IS_CLASS") ? "classes" : "printers",
133 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
134 getenv("HTTPS") ? "https" : "http", NULL
,
135 getenv("SERVER_NAME"), port
, "/admin");
137 printf("Location: %s\n\n", uri
);
139 else if (!strcmp(op
, "set-allowed-users"))
140 do_set_allowed_users(http
);
141 else if (!strcmp(op
, "set-as-default"))
142 do_set_default(http
);
143 else if (!strcmp(op
, "set-sharing"))
144 do_set_sharing(http
);
145 else if (!strcmp(op
, "find-new-printers") ||
146 !strcmp(op
, "list-available-printers"))
147 do_list_printers(http
);
148 else if (!strcmp(op
, "add-class"))
149 do_am_class(http
, 0);
150 else if (!strcmp(op
, "add-printer"))
151 do_am_printer(http
, 0);
152 else if (!strcmp(op
, "modify-class"))
153 do_am_class(http
, 1);
154 else if (!strcmp(op
, "modify-printer"))
155 do_am_printer(http
, 1);
156 else if (!strcmp(op
, "delete-class"))
157 do_delete_class(http
);
158 else if (!strcmp(op
, "delete-printer"))
159 do_delete_printer(http
);
160 else if (!strcmp(op
, "set-class-options"))
161 do_set_options(http
, 1);
162 else if (!strcmp(op
, "set-printer-options"))
163 do_set_options(http
, 0);
164 else if (!strcmp(op
, "config-server"))
165 do_config_server(http
);
169 * Bad operation code - display an error...
172 cgiStartHTML(cgiText(_("Administration")));
173 cgiCopyTemplateLang("error-op.tmpl");
177 else if (op
&& !strcmp(op
, "redirect"))
179 const char *url
; /* Redirection URL... */
180 char prefix
[1024]; /* URL prefix */
184 snprintf(prefix
, sizeof(prefix
), "https://%s:%s",
185 getenv("SERVER_NAME"), getenv("SERVER_PORT"));
187 snprintf(prefix
, sizeof(prefix
), "http://%s:%s",
188 getenv("SERVER_NAME"), getenv("SERVER_PORT"));
190 fprintf(stderr
, "DEBUG: redirecting with prefix %s!\n", prefix
);
192 if ((url
= cgiGetVariable("URL")) != NULL
)
194 char encoded
[1024], /* Encoded URL string */
195 *ptr
; /* Pointer into encoded string */
202 for (; *url
&& ptr
< (encoded
+ sizeof(encoded
) - 4); url
++)
204 if (strchr("%@&+ <>#=", *url
) || *url
< ' ' || *url
& 128)
207 * Percent-encode this character; safe because we have at least 4
208 * bytes left in the array...
211 sprintf(ptr
, "%%%02X", *url
& 255);
223 * URL was too long, just redirect to the admin page...
226 printf("Location: %s/admin\n\n", prefix
);
231 * URL is OK, redirect there...
234 printf("Location: %s%s\n\n", prefix
, encoded
);
238 printf("Location: %s/admin\n\n", prefix
);
243 * Form data but no operation code - display an error...
246 cgiStartHTML(cgiText(_("Administration")));
247 cgiCopyTemplateLang("error-op.tmpl");
252 * Close the HTTP server connection...
258 * Return with no errors...
266 * 'choose_device_cb()' - Add a device to the device selection page.
271 const char *device_class
, /* I - Class */
272 const char *device_id
, /* I - 1284 device ID */
273 const char *device_info
, /* I - Description */
274 const char *device_make_and_model
, /* I - Make and model */
275 const char *device_uri
, /* I - Device URI */
276 const char *device_location
, /* I - Location */
277 const char *title
) /* I - Page title */
280 * For modern browsers, start a multi-part page so we can show that something
281 * is happening. Non-modern browsers just get everything at the end...
284 if (current_device
== 0 && cgiSupportsMultipart())
288 cgiCopyTemplateLang("choose-device.tmpl");
295 * Add the device to the array...
298 cgiSetArray("device_class", current_device
, device_class
);
299 cgiSetArray("device_id", current_device
, device_id
);
300 cgiSetArray("device_info", current_device
, device_info
);
301 cgiSetArray("device_make_and_model", current_device
, device_make_and_model
);
302 cgiSetArray("device_uri", current_device
, device_uri
);
303 cgiSetArray("device_location", current_device
, device_location
);
310 * 'do_am_class()' - Add or modify a class.
314 do_am_class(http_t
*http
, /* I - HTTP connection */
315 int modify
) /* I - Modify the printer? */
317 int i
, j
; /* Looping vars */
318 int element
; /* Element number */
319 int num_printers
; /* Number of printers */
320 ipp_t
*request
, /* IPP request */
321 *response
; /* IPP response */
322 ipp_attribute_t
*attr
; /* member-uris attribute */
323 char uri
[HTTP_MAX_URI
]; /* Device or printer URI */
324 const char *name
, /* Pointer to class name */
325 *op
, /* Operation name */
326 *ptr
; /* Pointer to CGI variable */
327 const char *title
; /* Title of page */
328 static const char * const pattrs
[] = /* Requested printer attributes */
336 title
= cgiText(modify
? _("Modify Class") : _("Add Class"));
337 op
= cgiGetVariable("OP");
338 name
= cgiGetVariable("PRINTER_NAME");
340 if (cgiGetVariable("PRINTER_LOCATION") == NULL
)
343 * Build a CUPS_GET_PRINTERS request, which requires the
344 * following attributes:
347 * attributes-natural-language
350 request
= ippNewRequest(CUPS_GET_PRINTERS
);
352 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type",
354 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask",
355 CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
358 * Do the request and get back a response...
363 cgiSetVariable("OP", op
);
365 cgiSetVariable("PRINTER_NAME", name
);
367 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
370 * Create MEMBER_URIS and MEMBER_NAMES arrays...
373 for (element
= 0, attr
= response
->attrs
;
376 if (attr
->name
&& !strcmp(attr
->name
, "printer-uri-supported"))
378 if ((ptr
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
&&
379 (!name
|| _cups_strcasecmp(name
, ptr
+ 1)))
382 * Don't show the current class...
385 cgiSetArray("MEMBER_URIS", element
, attr
->values
[0].string
.text
);
390 for (element
= 0, attr
= response
->attrs
;
393 if (attr
->name
&& !strcmp(attr
->name
, "printer-name"))
395 if (!name
|| _cups_strcasecmp(name
, attr
->values
[0].string
.text
))
398 * Don't show the current class...
401 cgiSetArray("MEMBER_NAMES", element
, attr
->values
[0].string
.text
);
406 num_printers
= cgiGetSize("MEMBER_URIS");
416 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
417 * following attributes:
420 * attributes-natural-language
424 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
426 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
427 "localhost", 0, "/classes/%s", name
);
428 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
431 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
432 "requested-attributes",
433 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])),
437 * Do the request and get back a response...
440 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
442 if ((attr
= ippFindAttribute(response
, "member-names",
443 IPP_TAG_NAME
)) != NULL
)
446 * Mark any current members in the class...
449 for (j
= 0; j
< num_printers
; j
++)
450 cgiSetArray("MEMBER_SELECTED", j
, "");
452 for (i
= 0; i
< attr
->num_values
; i
++)
454 for (j
= 0; j
< num_printers
; j
++)
456 if (!_cups_strcasecmp(attr
->values
[i
].string
.text
,
457 cgiGetArray("MEMBER_NAMES", j
)))
459 cgiSetArray("MEMBER_SELECTED", j
, "SELECTED");
466 if ((attr
= ippFindAttribute(response
, "printer-info",
467 IPP_TAG_TEXT
)) != NULL
)
468 cgiSetVariable("PRINTER_INFO", attr
->values
[0].string
.text
);
470 if ((attr
= ippFindAttribute(response
, "printer-location",
471 IPP_TAG_TEXT
)) != NULL
)
472 cgiSetVariable("PRINTER_LOCATION", attr
->values
[0].string
.text
);
478 * Update the location and description of an existing printer...
482 cgiCopyTemplateLang("modify-class.tmpl");
487 * Get the name, location, and description for a new printer...
491 cgiCopyTemplateLang("add-class.tmpl");
502 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
503 cgiCopyTemplateLang("error.tmpl");
508 for (ptr
= name
; *ptr
; ptr
++)
509 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
512 if (*ptr
|| ptr
== name
|| strlen(name
) > 127)
514 cgiSetVariable("ERROR",
515 cgiText(_("The class name may only contain up to "
516 "127 printable characters and may not "
517 "contain spaces, slashes (/), or the "
518 "pound sign (#).")));
520 cgiCopyTemplateLang("error.tmpl");
526 * Build a CUPS_ADD_CLASS request, which requires the following
530 * attributes-natural-language
534 * printer-is-accepting-jobs
539 request
= ippNewRequest(CUPS_ADD_CLASS
);
541 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
542 "localhost", 0, "/classes/%s", name
);
543 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
546 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
547 NULL
, cgiGetVariable("PRINTER_LOCATION"));
549 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
550 NULL
, cgiGetVariable("PRINTER_INFO"));
552 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
554 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
557 if ((num_printers
= cgiGetSize("MEMBER_URIS")) > 0)
559 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris",
560 num_printers
, NULL
, NULL
);
561 for (i
= 0; i
< num_printers
; i
++)
562 ippSetString(request
, &attr
, i
, cgiGetArray("MEMBER_URIS", i
));
566 * Do the request and get back a response...
569 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
571 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
573 puts("Status: 401\n");
576 else if (cupsLastError() > IPP_OK_CONFLICT
)
579 cgiShowIPPError(modify
? _("Unable to modify class") :
580 _("Unable to add class"));
585 * Redirect successful updates back to the class page...
588 char refresh
[1024]; /* Refresh URL */
590 cgiFormEncode(uri
, name
, sizeof(uri
));
591 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=/classes/%s",
593 cgiSetVariable("refresh_page", refresh
);
598 cgiCopyTemplateLang("class-modified.tmpl");
600 cgiCopyTemplateLang("class-added.tmpl");
608 * 'do_am_printer()' - Add or modify a printer.
612 do_am_printer(http_t
*http
, /* I - HTTP connection */
613 int modify
) /* I - Modify the printer? */
615 int i
; /* Looping var */
616 ipp_attribute_t
*attr
; /* Current attribute */
617 ipp_t
*request
, /* IPP request */
618 *response
, /* IPP response */
619 *oldinfo
; /* Old printer information */
620 const cgi_file_t
*file
; /* Uploaded file, if any */
621 const char *var
; /* CGI variable */
622 char uri
[HTTP_MAX_URI
], /* Device or printer URI */
623 *uriptr
, /* Pointer into URI */
624 evefile
[1024] = ""; /* IPP Everywhere PPD file */
625 int maxrate
; /* Maximum baud rate */
626 char baudrate
[255]; /* Baud rate string */
627 const char *name
, /* Pointer to class name */
628 *ptr
; /* Pointer to CGI variable */
629 const char *title
; /* Title of page */
630 static int baudrates
[] = /* Baud rates */
645 ptr
= cgiGetVariable("DEVICE_URI");
646 fprintf(stderr
, "DEBUG: do_am_printer: DEVICE_URI=\"%s\"\n",
647 ptr
? ptr
: "(null)");
649 title
= cgiText(modify
? _("Modify Printer") : _("Add Printer"));
654 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
655 * following attributes:
658 * attributes-natural-language
662 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
664 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
665 "localhost", 0, "/printers/%s",
666 cgiGetVariable("PRINTER_NAME"));
667 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
671 * Do the request and get back a response...
674 oldinfo
= cupsDoRequest(http
, request
, "/");
683 fprintf(stderr
, "DEBUG: file->tempfile=%s\n", file
->tempfile
);
684 fprintf(stderr
, "DEBUG: file->name=%s\n", file
->name
);
685 fprintf(stderr
, "DEBUG: file->filename=%s\n", file
->filename
);
686 fprintf(stderr
, "DEBUG: file->mimetype=%s\n", file
->mimetype
);
689 if ((name
= cgiGetVariable("PRINTER_NAME")) != NULL
)
691 for (ptr
= name
; *ptr
; ptr
++)
692 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '\\' || *ptr
== '?' || *ptr
== '\'' || *ptr
== '\"' || *ptr
== '#')
695 if (*ptr
|| ptr
== name
|| strlen(name
) > 127)
697 cgiSetVariable("ERROR",
698 cgiText(_("The printer name may only contain up to 127 printable characters and may not contain spaces, slashes (/ \\), quotes (' \"), question mark (?), or the pound sign (#).")));
700 cgiCopyTemplateLang("error.tmpl");
706 if ((var
= cgiGetVariable("DEVICE_URI")) != NULL
)
708 if ((uriptr
= strrchr(var
, '|')) != NULL
)
711 * Extract make and make/model from device URI string...
714 char make
[1024], /* Make string */
715 *makeptr
; /* Pointer into make */
720 strlcpy(make
, uriptr
, sizeof(make
));
722 if ((makeptr
= strchr(make
, ' ')) != NULL
)
724 else if ((makeptr
= strchr(make
, '-')) != NULL
)
726 else if (!_cups_strncasecmp(make
, "laserjet", 8) ||
727 !_cups_strncasecmp(make
, "deskjet", 7) ||
728 !_cups_strncasecmp(make
, "designjet", 9))
729 strlcpy(make
, "HP", sizeof(make
));
730 else if (!_cups_strncasecmp(make
, "phaser", 6))
731 strlcpy(make
, "Xerox", sizeof(make
));
732 else if (!_cups_strncasecmp(make
, "stylus", 6))
733 strlcpy(make
, "Epson", sizeof(make
));
735 strlcpy(make
, "Generic", sizeof(make
));
737 if (!cgiGetVariable("CURRENT_MAKE"))
738 cgiSetVariable("CURRENT_MAKE", make
);
740 if (!cgiGetVariable("CURRENT_MAKE_AND_MODEL"))
741 cgiSetVariable("CURRENT_MAKE_AND_MODEL", uriptr
);
745 char template[128], /* Template name */
746 *tptr
; /* Pointer into template name */
748 cgiSetVariable("PRINTER_INFO", uriptr
);
750 for (tptr
= template;
751 tptr
< (template + sizeof(template) - 1) && *uriptr
;
753 if (isalnum(*uriptr
& 255) || *uriptr
== '_' || *uriptr
== '-' ||
756 else if ((*uriptr
== ' ' || *uriptr
== '/') && tptr
> template &&
759 else if (*uriptr
== '?' || *uriptr
== '(')
764 cgiSetVariable("TEMPLATE_NAME", template);
772 * Look for devices so the user can pick something...
775 if ((attr
= ippFindAttribute(oldinfo
, "device-uri", IPP_TAG_URI
)) != NULL
)
777 strlcpy(uri
, attr
->values
[0].string
.text
, sizeof(uri
));
778 if ((uriptr
= strchr(uri
, ':')) != NULL
&& strncmp(uriptr
, "://", 3) == 0)
781 cgiSetVariable("CURRENT_DEVICE_URI", attr
->values
[0].string
.text
);
782 cgiSetVariable("CURRENT_DEVICE_SCHEME", uri
);
786 * Scan for devices for up to 30 seconds...
789 fputs("DEBUG: Getting list of devices...\n", stderr
);
792 if (cupsGetDevices(http
, 5, CUPS_INCLUDE_ALL
, CUPS_EXCLUDE_NONE
,
793 (cups_device_cb_t
)choose_device_cb
,
794 (void *)title
) == IPP_OK
)
796 fputs("DEBUG: Got device list!\n", stderr
);
798 if (cgiSupportsMultipart())
801 cgiSetVariable("CUPS_GET_DEVICES_DONE", "1");
803 cgiCopyTemplateLang("choose-device.tmpl");
806 if (cgiSupportsMultipart())
812 "ERROR: CUPS-Get-Devices request failed with status %x: %s\n",
813 cupsLastError(), cupsLastErrorString());
814 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
816 puts("Status: 401\n");
822 cgiShowIPPError(modify
? _("Unable to modify printer") :
823 _("Unable to add printer"));
829 else if (!strchr(var
, '/') ||
830 (!strncmp(var
, "lpd://", 6) && !strchr(var
+ 6, '/')))
832 if ((attr
= ippFindAttribute(oldinfo
, "device-uri", IPP_TAG_URI
)) != NULL
)
835 * Set the current device URI for the form to the old one...
838 if (strncmp(attr
->values
[0].string
.text
, var
, strlen(var
)) == 0)
839 cgiSetVariable("CURRENT_DEVICE_URI", attr
->values
[0].string
.text
);
843 * User needs to set the full URI...
847 cgiCopyTemplateLang("choose-uri.tmpl");
850 else if (!strncmp(var
, "serial:", 7) && !cgiGetVariable("BAUDRATE"))
853 * Need baud rate, parity, etc.
856 if ((var
= strchr(var
, '?')) != NULL
&&
857 strncmp(var
, "?baud=", 6) == 0)
858 maxrate
= atoi(var
+ 6);
862 for (i
= 0; i
< 10; i
++)
863 if (baudrates
[i
] > maxrate
)
867 sprintf(baudrate
, "%d", baudrates
[i
]);
868 cgiSetArray("BAUDRATES", i
, baudrate
);
872 cgiCopyTemplateLang("choose-serial.tmpl");
875 else if (!name
|| !cgiGetVariable("PRINTER_LOCATION"))
882 * Update the location and description of an existing printer...
887 if ((attr
= ippFindAttribute(oldinfo
, "printer-info",
888 IPP_TAG_TEXT
)) != NULL
)
889 cgiSetVariable("PRINTER_INFO", attr
->values
[0].string
.text
);
891 if ((attr
= ippFindAttribute(oldinfo
, "printer-location",
892 IPP_TAG_TEXT
)) != NULL
)
893 cgiSetVariable("PRINTER_LOCATION", attr
->values
[0].string
.text
);
895 if ((attr
= ippFindAttribute(oldinfo
, "printer-is-shared",
896 IPP_TAG_BOOLEAN
)) != NULL
)
897 cgiSetVariable("PRINTER_IS_SHARED",
898 attr
->values
[0].boolean
? "1" : "0");
901 cgiCopyTemplateLang("modify-printer.tmpl");
906 * Get the name, location, and description for a new printer...
910 if (!strncmp(var
, "usb:", 4))
911 cgiSetVariable("printer_is_shared", "1");
913 #endif /* __APPLE__ */
914 cgiSetVariable("printer_is_shared", "0");
916 cgiCopyTemplateLang("add-printer.tmpl");
927 (!cgiGetVariable("PPD_NAME") || cgiGetVariable("SELECT_MAKE")))
929 int ipp_everywhere
= !strncmp(var
, "ipp://", 6) || !strncmp(var
, "ipps://", 7) || (!strncmp(var
, "dnssd://", 8) && (strstr(var
, "_ipp._tcp") || strstr(var
, "_ipps._tcp")));
931 if (modify
&& !cgiGetVariable("SELECT_MAKE"))
934 * Get the PPD file...
937 int fd
; /* PPD file */
938 char filename
[1024]; /* PPD filename */
939 ppd_file_t
*ppd
; /* PPD information */
940 char buffer
[1024]; /* Buffer */
941 ssize_t bytes
; /* Number of bytes */
942 http_status_t get_status
; /* Status of GET */
945 /* TODO: Use cupsGetFile() API... */
946 snprintf(uri
, sizeof(uri
), "/printers/%s.ppd", name
);
948 if (httpGet(http
, uri
))
951 while ((get_status
= httpUpdate(http
)) == HTTP_CONTINUE
);
953 if (get_status
!= HTTP_OK
)
957 fprintf(stderr
, "ERROR: Unable to get PPD file %s: %d - %s\n",
958 uri
, get_status
, httpStatus(get_status
));
960 else if ((fd
= cupsTempFd(filename
, sizeof(filename
))) >= 0)
962 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
963 write(fd
, buffer
, (size_t)bytes
);
967 if ((ppd
= ppdOpenFile(filename
)) != NULL
)
969 if (ppd
->manufacturer
)
970 cgiSetVariable("CURRENT_MAKE", ppd
->manufacturer
);
973 cgiSetVariable("CURRENT_MAKE_AND_MODEL", ppd
->nickname
);
980 int linenum
; /* Line number */
982 fprintf(stderr
, "ERROR: Unable to open PPD file %s: %s\n",
983 filename
, ppdErrorString(ppdLastError(&linenum
)));
991 "ERROR: Unable to create temporary file for PPD file: %s\n",
997 * Build a CUPS_GET_PPDS request, which requires the following
1000 * attributes-charset
1001 * attributes-natural-language
1005 request
= ippNewRequest(CUPS_GET_PPDS
);
1007 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1008 NULL
, "ipp://localhost/printers/");
1010 if ((var
= cgiGetVariable("PPD_MAKE")) == NULL
)
1011 var
= cgiGetVariable("CURRENT_MAKE");
1012 if (var
&& !cgiGetVariable("SELECT_MAKE"))
1014 const char *make_model
; /* Make and model */
1017 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
1018 "ppd-make", NULL
, var
);
1020 if ((make_model
= cgiGetVariable("CURRENT_MAKE_AND_MODEL")) != NULL
)
1021 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
1022 "ppd-make-and-model", NULL
, make_model
);
1025 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1026 "requested-attributes", NULL
, "ppd-make");
1029 * Do the request and get back a response...
1032 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1035 * Got the list of PPDs, see if the user has selected a make...
1038 if (cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0) == 0 && !modify
)
1041 * No PPD files with this make, try again with all makes...
1044 ippDelete(response
);
1046 request
= ippNewRequest(CUPS_GET_PPDS
);
1048 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1049 NULL
, "ipp://localhost/printers/");
1051 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1052 "requested-attributes", NULL
, "ppd-make");
1054 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1055 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
1057 cgiStartHTML(title
);
1058 cgiCopyTemplateLang("choose-make.tmpl");
1061 else if (!var
|| cgiGetVariable("SELECT_MAKE"))
1063 cgiStartHTML(title
);
1064 cgiCopyTemplateLang("choose-make.tmpl");
1070 * Let the user choose a model...
1073 cgiStartHTML(title
);
1074 if (!cgiGetVariable("PPD_MAKE"))
1075 cgiSetVariable("PPD_MAKE", cgiGetVariable("CURRENT_MAKE"));
1077 cgiSetVariable("SHOW_IPP_EVERYWHERE", "1");
1078 cgiCopyTemplateLang("choose-model.tmpl");
1082 ippDelete(response
);
1086 cgiStartHTML(title
);
1087 cgiShowIPPError(_("Unable to get list of printer drivers"));
1088 cgiCopyTemplateLang("error.tmpl");
1095 * Build a CUPS_ADD_PRINTER request, which requires the following
1098 * attributes-charset
1099 * attributes-natural-language
1105 * printer-is-accepting-jobs
1110 request
= ippNewRequest(CUPS_ADD_PRINTER
);
1112 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1113 "localhost", 0, "/printers/%s",
1114 cgiGetVariable("PRINTER_NAME"));
1115 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1120 var
= cgiGetVariable("PPD_NAME");
1121 if (!strcmp(var
, "everywhere"))
1122 get_printer_ppd(cgiGetVariable("DEVICE_URI"), evefile
, sizeof(evefile
));
1123 else if (strcmp(var
, "__no_change__"))
1124 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "ppd-name",
1128 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
1129 NULL
, cgiGetVariable("PRINTER_LOCATION"));
1131 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
1132 NULL
, cgiGetVariable("PRINTER_INFO"));
1134 strlcpy(uri
, cgiGetVariable("DEVICE_URI"), sizeof(uri
));
1137 * Strip make and model from URI...
1140 if ((uriptr
= strrchr(uri
, '|')) != NULL
)
1143 if (!strncmp(uri
, "serial:", 7))
1146 * Update serial port URI to include baud rate, etc.
1149 if ((uriptr
= strchr(uri
, '?')) == NULL
)
1150 uriptr
= uri
+ strlen(uri
);
1152 snprintf(uriptr
, sizeof(uri
) - (size_t)(uriptr
- uri
),
1153 "?baud=%s+bits=%s+parity=%s+flow=%s",
1154 cgiGetVariable("BAUDRATE"), cgiGetVariable("BITS"),
1155 cgiGetVariable("PARITY"), cgiGetVariable("FLOW"));
1158 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri",
1161 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1163 var
= cgiGetVariable("printer_is_shared");
1164 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-shared",
1165 var
&& (!strcmp(var
, "1") || !strcmp(var
, "on")));
1167 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1171 * Do the request and get back a response...
1175 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", file
->tempfile
));
1176 else if (evefile
[0])
1178 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", evefile
));
1182 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1184 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1186 puts("Status: 401\n");
1189 else if (cupsLastError() > IPP_OK_CONFLICT
)
1191 cgiStartHTML(title
);
1192 cgiShowIPPError(modify
? _("Unable to modify printer") :
1193 _("Unable to add printer"));
1198 * Redirect successful updates back to the printer page...
1201 char refresh
[1024]; /* Refresh URL */
1204 cgiFormEncode(uri
, name
, sizeof(uri
));
1206 snprintf(refresh
, sizeof(refresh
),
1207 "5;/admin/?OP=redirect&URL=/printers/%s", uri
);
1209 cgiSetVariable("refresh_page", refresh
);
1211 cgiStartHTML(title
);
1213 cgiCopyTemplateLang("printer-modified.tmpl");
1218 * Set the printer options...
1221 cgiSetVariable("OP", "set-printer-options");
1222 do_set_options(http
, 0);
1235 * 'do_config_server()' - Configure server settings.
1239 do_config_server(http_t
*http
) /* I - HTTP connection */
1241 if (cgiGetVariable("CHANGESETTINGS"))
1244 * Save basic setting changes...
1247 int num_settings
; /* Number of server settings */
1248 cups_option_t
*settings
; /* Server settings */
1249 int advanced
, /* Advanced settings shown? */
1250 changed
; /* Have settings changed? */
1251 const char *debug_logging
, /* DEBUG_LOGGING value */
1252 *preserve_jobs
= NULL
,
1253 /* PRESERVE_JOBS value */
1254 *remote_admin
, /* REMOTE_ADMIN value */
1255 *remote_any
, /* REMOTE_ANY value */
1256 *share_printers
,/* SHARE_PRINTERS value */
1258 /* USER_CANCEL_ANY value */
1259 *browse_web_if
= NULL
,
1260 /* BrowseWebIF value */
1261 *preserve_job_history
= NULL
,
1262 /* PreserveJobHistory value */
1263 *preserve_job_files
= NULL
,
1264 /* PreserveJobFiles value */
1265 *max_clients
= NULL
,
1266 /* MaxClients value */
1269 *max_log_size
= NULL
;
1270 /* MaxLogSize value */
1271 const char *current_browse_web_if
,
1272 /* BrowseWebIF value */
1273 *current_preserve_job_history
,
1274 /* PreserveJobHistory value */
1275 *current_preserve_job_files
,
1276 /* PreserveJobFiles value */
1277 *current_max_clients
,
1278 /* MaxClients value */
1281 *current_max_log_size
;
1282 /* MaxLogSize value */
1284 char default_auth_type
[255];
1285 /* DefaultAuthType value */
1286 const char *val
; /* Setting value */
1287 #endif /* HAVE_GSSAPI */
1291 * Get the checkbox values from the form...
1294 debug_logging
= cgiGetVariable("DEBUG_LOGGING") ? "1" : "0";
1295 remote_admin
= cgiGetVariable("REMOTE_ADMIN") ? "1" : "0";
1296 remote_any
= cgiGetVariable("REMOTE_ANY") ? "1" : "0";
1297 share_printers
= cgiGetVariable("SHARE_PRINTERS") ? "1" : "0";
1298 user_cancel_any
= cgiGetVariable("USER_CANCEL_ANY") ? "1" : "0";
1300 advanced
= cgiGetVariable("ADVANCEDSETTINGS") != NULL
;
1304 * Get advanced settings...
1307 browse_web_if
= cgiGetVariable("BROWSE_WEB_IF") ? "Yes" : "No";
1308 max_clients
= cgiGetVariable("MAX_CLIENTS");
1309 max_log_size
= cgiGetVariable("MAX_LOG_SIZE");
1310 preserve_jobs
= cgiGetVariable("PRESERVE_JOBS");
1314 max_jobs
= cgiGetVariable("MAX_JOBS");
1315 preserve_job_history
= cgiGetVariable("PRESERVE_JOB_HISTORY");
1316 preserve_job_files
= cgiGetVariable("PRESERVE_JOB_FILES");
1318 if (!max_jobs
|| atoi(max_jobs
) < 0)
1321 if (!preserve_job_history
)
1322 preserve_job_history
= "On";
1324 if (!preserve_job_files
)
1325 preserve_job_files
= "1d";
1330 preserve_job_history
= "No";
1331 preserve_job_files
= "No";
1334 if (!max_clients
|| atoi(max_clients
) <= 0)
1335 max_clients
= "100";
1337 if (!max_log_size
|| atoi(max_log_size
) <= 0.0)
1338 max_log_size
= "1m";
1342 * Get the current server settings...
1345 if (!cupsAdminGetServerSettings(http
, &num_settings
, &settings
))
1347 cgiStartHTML(cgiText(_("Change Settings")));
1348 cgiSetVariable("MESSAGE",
1349 cgiText(_("Unable to change server settings")));
1350 cgiSetVariable("ERROR", cupsLastErrorString());
1351 cgiCopyTemplateLang("error.tmpl");
1358 * Get authentication settings...
1361 if (cgiGetVariable("KERBEROS"))
1362 strlcpy(default_auth_type
, "Negotiate", sizeof(default_auth_type
));
1365 val
= cupsGetOption("DefaultAuthType", num_settings
, settings
);
1367 if (!val
|| !_cups_strcasecmp(val
, "Negotiate"))
1368 strlcpy(default_auth_type
, "Basic", sizeof(default_auth_type
));
1370 strlcpy(default_auth_type
, val
, sizeof(default_auth_type
));
1373 fprintf(stderr
, "DEBUG: DefaultAuthType %s\n", default_auth_type
);
1374 #endif /* HAVE_GSSAPI */
1376 if ((current_browse_web_if
= cupsGetOption("BrowseWebIF", num_settings
,
1378 current_browse_web_if
= "No";
1380 if ((current_preserve_job_history
= cupsGetOption("PreserveJobHistory",
1383 current_preserve_job_history
= "Yes";
1385 if ((current_preserve_job_files
= cupsGetOption("PreserveJobFiles",
1388 current_preserve_job_files
= "1d";
1390 if ((current_max_clients
= cupsGetOption("MaxClients", num_settings
,
1392 current_max_clients
= "100";
1394 if ((current_max_jobs
= cupsGetOption("MaxJobs", num_settings
,
1396 current_max_jobs
= "500";
1398 if ((current_max_log_size
= cupsGetOption("MaxLogSize", num_settings
,
1400 current_max_log_size
= "1m";
1403 * See if the settings have changed...
1406 changed
= strcmp(debug_logging
, cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
,
1407 num_settings
, settings
)) ||
1408 strcmp(remote_admin
, cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
,
1409 num_settings
, settings
)) ||
1410 strcmp(remote_any
, cupsGetOption(CUPS_SERVER_REMOTE_ANY
,
1411 num_settings
, settings
)) ||
1412 strcmp(share_printers
, cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
,
1413 num_settings
, settings
)) ||
1415 !cupsGetOption("DefaultAuthType", num_settings
, settings
) ||
1416 strcmp(default_auth_type
, cupsGetOption("DefaultAuthType",
1417 num_settings
, settings
)) ||
1418 #endif /* HAVE_GSSAPI */
1419 strcmp(user_cancel_any
, cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
,
1420 num_settings
, settings
));
1422 if (advanced
&& !changed
)
1423 changed
= _cups_strcasecmp(browse_web_if
, current_browse_web_if
) ||
1424 _cups_strcasecmp(preserve_job_history
, current_preserve_job_history
) ||
1425 _cups_strcasecmp(preserve_job_files
, current_preserve_job_files
) ||
1426 _cups_strcasecmp(max_clients
, current_max_clients
) ||
1427 _cups_strcasecmp(max_jobs
, current_max_jobs
) ||
1428 _cups_strcasecmp(max_log_size
, current_max_log_size
);
1433 * Settings *have* changed, so save the changes...
1436 cupsFreeOptions(num_settings
, settings
);
1439 num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1440 debug_logging
, num_settings
, &settings
);
1441 num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1442 remote_admin
, num_settings
, &settings
);
1443 num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1444 remote_any
, num_settings
, &settings
);
1445 num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1446 share_printers
, num_settings
, &settings
);
1447 num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1448 user_cancel_any
, num_settings
, &settings
);
1450 num_settings
= cupsAddOption("DefaultAuthType", default_auth_type
,
1451 num_settings
, &settings
);
1452 #endif /* HAVE_GSSAPI */
1457 * Add advanced settings...
1460 if (_cups_strcasecmp(browse_web_if
, current_browse_web_if
))
1461 num_settings
= cupsAddOption("BrowseWebIF", browse_web_if
,
1462 num_settings
, &settings
);
1463 if (_cups_strcasecmp(preserve_job_history
, current_preserve_job_history
))
1464 num_settings
= cupsAddOption("PreserveJobHistory",
1465 preserve_job_history
, num_settings
,
1467 if (_cups_strcasecmp(preserve_job_files
, current_preserve_job_files
))
1468 num_settings
= cupsAddOption("PreserveJobFiles", preserve_job_files
,
1469 num_settings
, &settings
);
1470 if (_cups_strcasecmp(max_clients
, current_max_clients
))
1471 num_settings
= cupsAddOption("MaxClients", max_clients
, num_settings
,
1473 if (_cups_strcasecmp(max_jobs
, current_max_jobs
))
1474 num_settings
= cupsAddOption("MaxJobs", max_jobs
, num_settings
,
1476 if (_cups_strcasecmp(max_log_size
, current_max_log_size
))
1477 num_settings
= cupsAddOption("MaxLogSize", max_log_size
, num_settings
,
1481 if (!cupsAdminSetServerSettings(http
, num_settings
, settings
))
1483 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1485 puts("Status: 401\n");
1489 cgiStartHTML(cgiText(_("Change Settings")));
1490 cgiSetVariable("MESSAGE",
1491 cgiText(_("Unable to change server settings")));
1492 cgiSetVariable("ERROR", cupsLastErrorString());
1493 cgiCopyTemplateLang("error.tmpl");
1498 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&"
1499 "URL=/admin/?ADVANCEDSETTINGS=YES");
1501 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
1502 cgiStartHTML(cgiText(_("Change Settings")));
1503 cgiCopyTemplateLang("restart.tmpl");
1512 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
1513 cgiStartHTML(cgiText(_("Change Settings")));
1514 cgiCopyTemplateLang("norestart.tmpl");
1517 cupsFreeOptions(num_settings
, settings
);
1521 else if (cgiGetVariable("SAVECHANGES") && cgiGetVariable("CUPSDCONF"))
1524 * Save hand-edited config file...
1527 http_status_t status
; /* PUT status */
1528 char tempfile
[1024]; /* Temporary new cupsd.conf */
1529 int tempfd
; /* Temporary file descriptor */
1530 cups_file_t
*temp
; /* Temporary file */
1531 const char *start
, /* Start of line */
1532 *end
; /* End of line */
1536 * Create a temporary file for the new cupsd.conf file...
1539 if ((tempfd
= cupsTempFd(tempfile
, sizeof(tempfile
))) < 0)
1541 cgiStartHTML(cgiText(_("Edit Configuration File")));
1542 cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file")));
1543 cgiSetVariable("ERROR", strerror(errno
));
1544 cgiCopyTemplateLang("error.tmpl");
1551 if ((temp
= cupsFileOpenFd(tempfd
, "w")) == NULL
)
1553 cgiStartHTML(cgiText(_("Edit Configuration File")));
1554 cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file")));
1555 cgiSetVariable("ERROR", strerror(errno
));
1556 cgiCopyTemplateLang("error.tmpl");
1566 * Copy the cupsd.conf text from the form variable...
1569 start
= cgiGetVariable("CUPSDCONF");
1572 if ((end
= strstr(start
, "\r\n")) == NULL
)
1573 if ((end
= strstr(start
, "\n")) == NULL
)
1574 end
= start
+ strlen(start
);
1576 cupsFileWrite(temp
, start
, (size_t)(end
- start
));
1577 cupsFilePutChar(temp
, '\n');
1581 else if (*end
== '\n')
1587 cupsFileClose(temp
);
1590 * Upload the configuration file to the server...
1593 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1595 if (status
== HTTP_UNAUTHORIZED
)
1597 puts("Status: 401\n");
1601 else if (status
!= HTTP_CREATED
)
1603 cgiSetVariable("MESSAGE",
1604 cgiText(_("Unable to upload cupsd.conf file")));
1605 cgiSetVariable("ERROR", httpStatus(status
));
1607 cgiStartHTML(cgiText(_("Edit Configuration File")));
1608 cgiCopyTemplateLang("error.tmpl");
1612 cgiSetVariable("refresh_page", "5;URL=/admin/");
1614 cgiStartHTML(cgiText(_("Edit Configuration File")));
1615 cgiCopyTemplateLang("restart.tmpl");
1624 struct stat info
; /* cupsd.conf information */
1625 cups_file_t
*cupsd
; /* cupsd.conf file */
1626 char *buffer
, /* Buffer for entire file */
1627 *bufptr
, /* Pointer into buffer */
1628 *bufend
; /* End of buffer */
1629 int ch
; /* Character from file */
1630 char filename
[1024]; /* Filename */
1631 const char *server_root
; /* Location of config files */
1635 * Locate the cupsd.conf file...
1638 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
1639 server_root
= CUPS_SERVERROOT
;
1641 snprintf(filename
, sizeof(filename
), "%s/cupsd.conf", server_root
);
1644 * Figure out the size...
1647 if (stat(filename
, &info
))
1649 cgiStartHTML(cgiText(_("Edit Configuration File")));
1650 cgiSetVariable("MESSAGE",
1651 cgiText(_("Unable to access cupsd.conf file")));
1652 cgiSetVariable("ERROR", strerror(errno
));
1653 cgiCopyTemplateLang("error.tmpl");
1660 if (info
.st_size
> (1024 * 1024))
1662 cgiStartHTML(cgiText(_("Edit Configuration File")));
1663 cgiSetVariable("MESSAGE",
1664 cgiText(_("Unable to access cupsd.conf file")));
1665 cgiSetVariable("ERROR",
1666 cgiText(_("Unable to edit cupsd.conf files larger than "
1668 cgiCopyTemplateLang("error.tmpl");
1671 fprintf(stderr
, "ERROR: \"%s\" too large (%ld) to edit!\n", filename
,
1672 (long)info
.st_size
);
1677 * Open the cupsd.conf file...
1680 if ((cupsd
= cupsFileOpen(filename
, "r")) == NULL
)
1683 * Unable to open - log an error...
1686 cgiStartHTML(cgiText(_("Edit Configuration File")));
1687 cgiSetVariable("MESSAGE",
1688 cgiText(_("Unable to access cupsd.conf file")));
1689 cgiSetVariable("ERROR", strerror(errno
));
1690 cgiCopyTemplateLang("error.tmpl");
1698 * Allocate memory and load the file into a string buffer...
1701 if ((buffer
= calloc(1, (size_t)info
.st_size
+ 1)) != NULL
)
1703 cupsFileRead(cupsd
, buffer
, (size_t)info
.st_size
);
1704 cgiSetVariable("CUPSDCONF", buffer
);
1708 cupsFileClose(cupsd
);
1711 * Then get the default cupsd.conf file and put that into a string as
1715 strlcat(filename
, ".default", sizeof(filename
));
1717 if (!stat(filename
, &info
) && info
.st_size
< (1024 * 1024) &&
1718 (cupsd
= cupsFileOpen(filename
, "r")) != NULL
)
1720 if ((buffer
= calloc(1, 2 * (size_t)info
.st_size
+ 1)) != NULL
)
1722 bufend
= buffer
+ 2 * info
.st_size
- 1;
1724 for (bufptr
= buffer
;
1725 bufptr
< bufend
&& (ch
= cupsFileGetChar(cupsd
)) != EOF
;)
1727 if (ch
== '\\' || ch
== '\"')
1730 *bufptr
++ = (char)ch
;
1732 else if (ch
== '\n')
1737 else if (ch
== '\t')
1743 *bufptr
++ = (char)ch
;
1748 cgiSetVariable("CUPSDCONF_DEFAULT", buffer
);
1752 cupsFileClose(cupsd
);
1756 * Show the current config file...
1759 cgiStartHTML(cgiText(_("Edit Configuration File")));
1761 cgiCopyTemplateLang("edit-config.tmpl");
1769 * 'do_delete_class()' - Delete a class.
1773 do_delete_class(http_t
*http
) /* I - HTTP connection */
1775 ipp_t
*request
; /* IPP request */
1776 char uri
[HTTP_MAX_URI
]; /* Job URI */
1777 const char *pclass
; /* Printer class name */
1781 * Get form variables...
1784 if (cgiGetVariable("CONFIRM") == NULL
)
1786 cgiStartHTML(cgiText(_("Delete Class")));
1787 cgiCopyTemplateLang("class-confirm.tmpl");
1792 if ((pclass
= cgiGetVariable("PRINTER_NAME")) != NULL
)
1793 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1794 "localhost", 0, "/classes/%s", pclass
);
1797 cgiStartHTML(cgiText(_("Delete Class")));
1798 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
1799 cgiCopyTemplateLang("error.tmpl");
1805 * Build a CUPS_DELETE_CLASS request, which requires the following
1808 * attributes-charset
1809 * attributes-natural-language
1813 request
= ippNewRequest(CUPS_DELETE_CLASS
);
1815 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1819 * Do the request and get back a response...
1822 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1825 * Show the results...
1828 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1830 puts("Status: 401\n");
1833 else if (cupsLastError() <= IPP_OK_CONFLICT
)
1836 * Redirect successful updates back to the classes page...
1839 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/classes");
1842 cgiStartHTML(cgiText(_("Delete Class")));
1844 if (cupsLastError() > IPP_OK_CONFLICT
)
1845 cgiShowIPPError(_("Unable to delete class"));
1847 cgiCopyTemplateLang("class-deleted.tmpl");
1854 * 'do_delete_printer()' - Delete a printer.
1858 do_delete_printer(http_t
*http
) /* I - HTTP connection */
1860 ipp_t
*request
; /* IPP request */
1861 char uri
[HTTP_MAX_URI
]; /* Job URI */
1862 const char *printer
; /* Printer printer name */
1866 * Get form variables...
1869 if (cgiGetVariable("CONFIRM") == NULL
)
1871 cgiStartHTML(cgiText(_("Delete Printer")));
1872 cgiCopyTemplateLang("printer-confirm.tmpl");
1877 if ((printer
= cgiGetVariable("PRINTER_NAME")) != NULL
)
1878 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1879 "localhost", 0, "/printers/%s", printer
);
1882 cgiStartHTML(cgiText(_("Delete Printer")));
1883 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
1884 cgiCopyTemplateLang("error.tmpl");
1890 * Build a CUPS_DELETE_PRINTER request, which requires the following
1893 * attributes-charset
1894 * attributes-natural-language
1898 request
= ippNewRequest(CUPS_DELETE_PRINTER
);
1900 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1904 * Do the request and get back a response...
1907 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1910 * Show the results...
1913 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1915 puts("Status: 401\n");
1918 else if (cupsLastError() <= IPP_OK_CONFLICT
)
1921 * Redirect successful updates back to the printers page...
1924 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/printers");
1927 cgiStartHTML(cgiText(_("Delete Printer")));
1929 if (cupsLastError() > IPP_OK_CONFLICT
)
1930 cgiShowIPPError(_("Unable to delete printer"));
1932 cgiCopyTemplateLang("printer-deleted.tmpl");
1939 * 'do_list_printers()' - List available printers.
1943 do_list_printers(http_t
*http
) /* I - HTTP connection */
1945 ipp_t
*request
, /* IPP request */
1946 *response
; /* IPP response */
1947 ipp_attribute_t
*attr
; /* IPP attribute */
1950 cgiStartHTML(cgiText(_("List Available Printers")));
1954 * Get the list of printers and their devices...
1957 request
= ippNewRequest(CUPS_GET_PRINTERS
);
1959 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1960 "requested-attributes", NULL
, "device-uri");
1962 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type",
1963 CUPS_PRINTER_LOCAL
);
1964 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask",
1965 CUPS_PRINTER_LOCAL
);
1967 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1970 * Got the printer list, now load the devices...
1973 int i
; /* Looping var */
1974 cups_array_t
*printer_devices
; /* Printer devices for local printers */
1975 char *printer_device
; /* Current printer device */
1979 * Allocate an array and copy the device strings...
1982 printer_devices
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
1984 for (attr
= ippFindAttribute(response
, "device-uri", IPP_TAG_URI
);
1986 attr
= ippFindNextAttribute(response
, "device-uri", IPP_TAG_URI
))
1988 cupsArrayAdd(printer_devices
, strdup(attr
->values
[0].string
.text
));
1992 * Free the printer list and get the device list...
1995 ippDelete(response
);
1997 request
= ippNewRequest(CUPS_GET_DEVICES
);
1999 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2002 * Got the device list, let's parse it...
2005 const char *device_uri
, /* device-uri attribute value */
2006 *device_make_and_model
, /* device-make-and-model value */
2007 *device_info
; /* device-info value */
2010 for (i
= 0, attr
= response
->attrs
; attr
; attr
= attr
->next
)
2013 * Skip leading attributes until we hit a device...
2016 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
2023 * Pull the needed attributes from this device...
2027 device_make_and_model
= NULL
;
2030 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
2032 if (!strcmp(attr
->name
, "device-info") &&
2033 attr
->value_tag
== IPP_TAG_TEXT
)
2034 device_info
= attr
->values
[0].string
.text
;
2036 if (!strcmp(attr
->name
, "device-make-and-model") &&
2037 attr
->value_tag
== IPP_TAG_TEXT
)
2038 device_make_and_model
= attr
->values
[0].string
.text
;
2040 if (!strcmp(attr
->name
, "device-uri") &&
2041 attr
->value_tag
== IPP_TAG_URI
)
2042 device_uri
= attr
->values
[0].string
.text
;
2048 * See if we have everything needed...
2051 if (device_info
&& device_make_and_model
&& device_uri
&&
2052 _cups_strcasecmp(device_make_and_model
, "unknown") &&
2053 strchr(device_uri
, ':'))
2056 * Yes, now see if there is already a printer for this
2060 if (!cupsArrayFind(printer_devices
, (void *)device_uri
))
2063 * Not found, so it must be a new printer...
2066 char option
[1024], /* Form variables for this device */
2067 *option_ptr
; /* Pointer into string */
2068 const char *ptr
; /* Pointer into device string */
2072 * Format the printer name variable for this device...
2074 * We use the device-info string first, then device-uri,
2075 * and finally device-make-and-model to come up with a
2079 if (_cups_strncasecmp(device_info
, "unknown", 7))
2081 else if ((ptr
= strstr(device_uri
, "://")) != NULL
)
2084 ptr
= device_make_and_model
;
2086 for (option_ptr
= option
;
2087 option_ptr
< (option
+ sizeof(option
) - 1) && *ptr
;
2089 if (isalnum(*ptr
& 255) || *ptr
== '_' || *ptr
== '-' ||
2091 *option_ptr
++ = *ptr
;
2092 else if ((*ptr
== ' ' || *ptr
== '/') && option_ptr
> option
&&
2093 option_ptr
[-1] != '_')
2094 *option_ptr
++ = '_';
2095 else if (*ptr
== '?' || *ptr
== '(')
2100 cgiSetArray("TEMPLATE_NAME", i
, option
);
2103 * Finally, set the form variables for this printer...
2106 cgiSetArray("device_info", i
, device_info
);
2107 cgiSetArray("device_make_and_model", i
, device_make_and_model
);
2108 cgiSetArray("device_uri", i
, device_uri
);
2117 ippDelete(response
);
2120 * Free the device list...
2123 for (printer_device
= (char *)cupsArrayFirst(printer_devices
);
2125 printer_device
= (char *)cupsArrayNext(printer_devices
))
2126 free(printer_device
);
2128 cupsArrayDelete(printer_devices
);
2133 * Finally, show the printer list...
2136 cgiCopyTemplateLang("list-available-printers.tmpl");
2143 * 'do_menu()' - Show the main menu.
2147 do_menu(http_t
*http
) /* I - HTTP connection */
2149 int num_settings
; /* Number of server settings */
2150 cups_option_t
*settings
; /* Server settings */
2151 const char *val
; /* Setting value */
2155 * Get the current server settings...
2158 if (!cupsAdminGetServerSettings(http
, &num_settings
, &settings
))
2160 cgiSetVariable("SETTINGS_MESSAGE",
2161 cgiText(_("Unable to open cupsd.conf file:")));
2162 cgiSetVariable("SETTINGS_ERROR", cupsLastErrorString());
2165 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
2166 settings
)) != NULL
&& atoi(val
))
2167 cgiSetVariable("DEBUG_LOGGING", "CHECKED");
2169 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
2170 settings
)) != NULL
&& atoi(val
))
2171 cgiSetVariable("REMOTE_ADMIN", "CHECKED");
2173 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
2174 settings
)) != NULL
&& atoi(val
))
2175 cgiSetVariable("REMOTE_ANY", "CHECKED");
2177 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
2178 settings
)) != NULL
&& atoi(val
))
2179 cgiSetVariable("SHARE_PRINTERS", "CHECKED");
2181 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
2182 settings
)) != NULL
&& atoi(val
))
2183 cgiSetVariable("USER_CANCEL_ANY", "CHECKED");
2186 cgiSetVariable("HAVE_GSSAPI", "1");
2188 if ((val
= cupsGetOption("DefaultAuthType", num_settings
,
2189 settings
)) != NULL
&& !_cups_strcasecmp(val
, "Negotiate"))
2190 cgiSetVariable("KERBEROS", "CHECKED");
2192 #endif /* HAVE_GSSAPI */
2193 cgiSetVariable("KERBEROS", "");
2195 if ((val
= cupsGetOption("BrowseWebIF", num_settings
,
2199 if (!_cups_strcasecmp(val
, "yes") || !_cups_strcasecmp(val
, "on") ||
2200 !_cups_strcasecmp(val
, "true"))
2201 cgiSetVariable("BROWSE_WEB_IF", "CHECKED");
2203 if ((val
= cupsGetOption("PreserveJobHistory", num_settings
,
2208 (!_cups_strcasecmp(val
, "0") || !_cups_strcasecmp(val
, "no") ||
2209 !_cups_strcasecmp(val
, "off") || !_cups_strcasecmp(val
, "false") ||
2210 !_cups_strcasecmp(val
, "disabled")))
2212 cgiSetVariable("PRESERVE_JOB_HISTORY", "0");
2213 cgiSetVariable("PRESERVE_JOB_FILES", "0");
2217 cgiSetVariable("PRESERVE_JOBS", "CHECKED");
2218 cgiSetVariable("PRESERVE_JOB_HISTORY", val
);
2220 if ((val
= cupsGetOption("PreserveJobFiles", num_settings
,
2224 cgiSetVariable("PRESERVE_JOB_FILES", val
);
2228 if ((val
= cupsGetOption("MaxClients", num_settings
, settings
)) == NULL
)
2231 cgiSetVariable("MAX_CLIENTS", val
);
2233 if ((val
= cupsGetOption("MaxJobs", num_settings
, settings
)) == NULL
)
2236 cgiSetVariable("MAX_JOBS", val
);
2238 if ((val
= cupsGetOption("MaxLogSize", num_settings
, settings
)) == NULL
)
2241 cgiSetVariable("MAX_LOG_SIZE", val
);
2243 cupsFreeOptions(num_settings
, settings
);
2246 * Finally, show the main menu template...
2249 cgiStartHTML(cgiText(_("Administration")));
2251 cgiCopyTemplateLang("admin.tmpl");
2258 * 'do_set_allowed_users()' - Set the allowed/denied users for a queue.
2262 do_set_allowed_users(http_t
*http
) /* I - HTTP connection */
2264 int i
; /* Looping var */
2265 ipp_t
*request
, /* IPP request */
2266 *response
; /* IPP response */
2267 char uri
[HTTP_MAX_URI
]; /* Printer URI */
2268 const char *printer
, /* Printer name (purge-jobs) */
2269 *is_class
, /* Is a class? */
2270 *users
, /* List of users or groups */
2271 *type
; /* Allow/deny type */
2272 int num_users
; /* Number of users */
2273 char *ptr
, /* Pointer into users string */
2274 *end
, /* Pointer to end of users string */
2275 quote
; /* Quote character */
2276 ipp_attribute_t
*attr
; /* Attribute */
2277 static const char * const attrs
[] = /* Requested attributes */
2279 "requesting-user-name-allowed",
2280 "requesting-user-name-denied"
2284 is_class
= cgiGetVariable("IS_CLASS");
2285 printer
= cgiGetVariable("PRINTER_NAME");
2289 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
2290 cgiStartHTML(cgiText(_("Set Allowed Users")));
2291 cgiCopyTemplateLang("error.tmpl");
2296 users
= cgiGetVariable("users");
2297 type
= cgiGetVariable("type");
2299 if (!users
|| !type
||
2300 (strcmp(type
, "requesting-user-name-allowed") &&
2301 strcmp(type
, "requesting-user-name-denied")))
2304 * Build a Get-Printer-Attributes request, which requires the following
2307 * attributes-charset
2308 * attributes-natural-language
2310 * requested-attributes
2313 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
2315 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2316 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2318 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2321 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2322 "requested-attributes",
2323 (int)(sizeof(attrs
) / sizeof(attrs
[0])), NULL
, attrs
);
2326 * Do the request and get back a response...
2329 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2331 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
2333 ippDelete(response
);
2336 cgiStartHTML(cgiText(_("Set Allowed Users")));
2338 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2340 puts("Status: 401\n");
2343 else if (cupsLastError() > IPP_OK_CONFLICT
)
2344 cgiShowIPPError(_("Unable to get printer attributes"));
2346 cgiCopyTemplateLang("users.tmpl");
2353 * Save the changes...
2356 for (num_users
= 0, ptr
= (char *)users
; *ptr
; num_users
++)
2359 * Skip whitespace and commas...
2362 while (*ptr
== ',' || isspace(*ptr
& 255))
2368 if (*ptr
== '\'' || *ptr
== '\"')
2371 * Scan quoted name...
2376 for (end
= ptr
; *end
; end
++)
2383 * Scan space or comma-delimited name...
2386 for (end
= ptr
; *end
; end
++)
2387 if (isspace(*end
& 255) || *end
== ',')
2392 * Advance to the next name...
2399 * Build a CUPS-Add-Printer/Class request, which requires the following
2402 * attributes-charset
2403 * attributes-natural-language
2405 * requesting-user-name-{allowed,denied}
2408 request
= ippNewRequest(is_class
? CUPS_ADD_CLASS
: CUPS_ADD_PRINTER
);
2410 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2411 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2413 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2417 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2418 "requesting-user-name-allowed", NULL
, "all");
2421 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2422 type
, num_users
, NULL
, NULL
);
2424 for (i
= 0, ptr
= (char *)users
; *ptr
; i
++)
2427 * Skip whitespace and commas...
2430 while (*ptr
== ',' || isspace(*ptr
& 255))
2436 if (*ptr
== '\'' || *ptr
== '\"')
2439 * Scan quoted name...
2444 for (end
= ptr
; *end
; end
++)
2451 * Scan space or comma-delimited name...
2454 for (end
= ptr
; *end
; end
++)
2455 if (isspace(*end
& 255) || *end
== ',')
2460 * Terminate the name...
2470 ippSetString(request
, &attr
, i
, ptr
);
2473 * Advance to the next name...
2481 * Do the request and get back a response...
2484 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
2486 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2488 puts("Status: 401\n");
2491 else if (cupsLastError() > IPP_OK_CONFLICT
)
2493 cgiStartHTML(cgiText(_("Set Allowed Users")));
2494 cgiShowIPPError(_("Unable to change printer"));
2499 * Redirect successful updates back to the printer page...
2502 char url
[1024], /* Printer/class URL */
2503 refresh
[1024]; /* Refresh URL */
2506 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
2507 cgiFormEncode(uri
, url
, sizeof(uri
));
2508 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s",
2510 cgiSetVariable("refresh_page", refresh
);
2512 cgiStartHTML(cgiText(_("Set Allowed Users")));
2514 cgiCopyTemplateLang(is_class
? "class-modified.tmpl" :
2515 "printer-modified.tmpl");
2524 * 'do_set_default()' - Set the server default printer/class.
2528 do_set_default(http_t
*http
) /* I - HTTP connection */
2530 const char *title
; /* Page title */
2531 ipp_t
*request
; /* IPP request */
2532 char uri
[HTTP_MAX_URI
]; /* Printer URI */
2533 const char *printer
, /* Printer name (purge-jobs) */
2534 *is_class
; /* Is a class? */
2537 is_class
= cgiGetVariable("IS_CLASS");
2538 printer
= cgiGetVariable("PRINTER_NAME");
2539 title
= cgiText(_("Set As Server Default"));
2543 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
2544 cgiStartHTML(title
);
2545 cgiCopyTemplateLang("error.tmpl");
2551 * Build a printer request, which requires the following
2554 * attributes-charset
2555 * attributes-natural-language
2559 request
= ippNewRequest(CUPS_SET_DEFAULT
);
2561 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2562 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2564 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2568 * Do the request and get back a response...
2571 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
2573 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2575 puts("Status: 401\n");
2578 else if (cupsLastError() > IPP_OK_CONFLICT
)
2580 cgiStartHTML(title
);
2581 cgiShowIPPError(_("Unable to set server default"));
2586 * Redirect successful updates back to the printer page...
2589 char url
[1024], /* Printer/class URL */
2590 refresh
[1024]; /* Refresh URL */
2593 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
2594 cgiFormEncode(uri
, url
, sizeof(uri
));
2595 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s", uri
);
2596 cgiSetVariable("refresh_page", refresh
);
2598 cgiStartHTML(title
);
2599 cgiCopyTemplateLang("printer-default.tmpl");
2607 * 'do_set_options()' - Configure the default options for a queue.
2611 do_set_options(http_t
*http
, /* I - HTTP connection */
2612 int is_class
) /* I - Set options for class? */
2614 int i
, j
, k
, m
; /* Looping vars */
2615 int have_options
; /* Have options? */
2616 ipp_t
*request
, /* IPP request */
2617 *response
; /* IPP response */
2618 ipp_attribute_t
*attr
; /* IPP attribute */
2619 char uri
[HTTP_MAX_URI
]; /* Job URI */
2620 const char *var
; /* Variable value */
2621 const char *printer
; /* Printer printer name */
2622 const char *filename
; /* PPD filename */
2623 char tempfile
[1024]; /* Temporary filename */
2624 cups_file_t
*in
, /* Input file */
2625 *out
; /* Output file */
2626 char line
[1024], /* Line from PPD file */
2627 value
[1024], /* Option value */
2628 keyword
[1024], /* Keyword from Default line */
2629 *keyptr
; /* Pointer into keyword... */
2630 ppd_file_t
*ppd
; /* PPD file */
2631 ppd_group_t
*group
; /* Option group */
2632 ppd_option_t
*option
; /* Option */
2633 ppd_coption_t
*coption
; /* Custom option */
2634 ppd_cparam_t
*cparam
; /* Custom parameter */
2635 ppd_attr_t
*ppdattr
; /* PPD attribute */
2636 const char *title
; /* Page title */
2639 title
= cgiText(is_class
? _("Set Class Options") : _("Set Printer Options"));
2641 fprintf(stderr
, "DEBUG: do_set_options(http=%p, is_class=%d)\n", http
,
2645 * Get the printer name...
2648 if ((printer
= cgiGetVariable("PRINTER_NAME")) != NULL
)
2649 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2650 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2654 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
2655 cgiStartHTML(title
);
2656 cgiCopyTemplateLang("error.tmpl");
2661 fprintf(stderr
, "DEBUG: printer=\"%s\", uri=\"%s\"...\n", printer
, uri
);
2664 * If the user clicks on the Auto-Configure button, send an AutoConfigure
2665 * command file to the printer...
2668 if (cgiGetVariable("AUTOCONFIGURE"))
2670 cgiPrintCommand(http
, printer
, "AutoConfigure", "Set Default Options");
2675 * Get the PPD file...
2681 filename
= cupsGetPPD2(http
, printer
);
2685 fprintf(stderr
, "DEBUG: Got PPD file: \"%s\"\n", filename
);
2687 if ((ppd
= ppdOpenFile(filename
)) == NULL
)
2689 cgiSetVariable("ERROR", ppdErrorString(ppdLastError(&i
)));
2690 cgiSetVariable("MESSAGE", cgiText(_("Unable to open PPD file")));
2691 cgiStartHTML(title
);
2692 cgiCopyTemplateLang("error.tmpl");
2699 fputs("DEBUG: No PPD file\n", stderr
);
2703 if (cgiGetVariable("job_sheets_start") != NULL
||
2704 cgiGetVariable("job_sheets_end") != NULL
)
2711 ppdMarkDefaults(ppd
);
2713 for (option
= ppdFirstOption(ppd
);
2715 option
= ppdNextOption(ppd
))
2717 if ((var
= cgiGetVariable(option
->keyword
)) != NULL
)
2720 ppdMarkOption(ppd
, option
->keyword
, var
);
2721 fprintf(stderr
, "DEBUG: Set %s to %s...\n", option
->keyword
, var
);
2724 fprintf(stderr
, "DEBUG: Didn't find %s...\n", option
->keyword
);
2728 if (!have_options
|| ppdConflicts(ppd
))
2731 * Show the options to the user...
2734 fputs("DEBUG: Showing options...\n", stderr
);
2737 * Show auto-configure button if supported...
2742 if (ppd
->num_filters
== 0 ||
2743 ((ppdattr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) != NULL
&&
2744 ppdattr
->value
&& strstr(ppdattr
->value
, "AutoConfigure")))
2745 cgiSetVariable("HAVE_AUTOCONFIGURE", "YES");
2748 for (i
= 0; i
< ppd
->num_filters
; i
++)
2749 if (!strncmp(ppd
->filters
[i
], "application/vnd.cups-postscript", 31))
2751 cgiSetVariable("HAVE_AUTOCONFIGURE", "YES");
2758 * Get the printer attributes...
2761 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
2763 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2764 "localhost", 0, "/printers/%s", printer
);
2765 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2768 response
= cupsDoRequest(http
, request
, "/");
2771 * List the groups used as "tabs"...
2778 for (group
= ppd
->groups
;
2779 i
< ppd
->num_groups
;
2782 cgiSetArray("GROUP_ID", i
, group
->name
);
2784 if (!strcmp(group
->name
, "InstallableOptions"))
2785 cgiSetArray("GROUP", i
, cgiText(_("Options Installed")));
2787 cgiSetArray("GROUP", i
, group
->text
);
2791 if (ippFindAttribute(response
, "job-sheets-supported", IPP_TAG_ZERO
))
2793 cgiSetArray("GROUP_ID", i
, "CUPS_BANNERS");
2794 cgiSetArray("GROUP", i
++, cgiText(_("Banners")));
2797 if (ippFindAttribute(response
, "printer-error-policy-supported",
2799 ippFindAttribute(response
, "printer-op-policy-supported",
2802 cgiSetArray("GROUP_ID", i
, "CUPS_POLICIES");
2803 cgiSetArray("GROUP", i
++, cgiText(_("Policies")));
2806 if ((attr
= ippFindAttribute(response
, "port-monitor-supported",
2807 IPP_TAG_NAME
)) != NULL
&& attr
->num_values
> 1)
2809 cgiSetArray("GROUP_ID", i
, "CUPS_PORT_MONITOR");
2810 cgiSetArray("GROUP", i
, cgiText(_("Port Monitor")));
2813 cgiStartHTML(cgiText(_("Set Printer Options")));
2814 cgiCopyTemplateLang("set-printer-options-header.tmpl");
2820 if (ppdConflicts(ppd
))
2822 for (i
= ppd
->num_groups
, k
= 0, group
= ppd
->groups
;
2825 for (j
= group
->num_options
, option
= group
->options
;
2828 if (option
->conflicted
)
2830 cgiSetArray("ckeyword", k
, option
->keyword
);
2831 cgiSetArray("ckeytext", k
, option
->text
);
2833 for (m
= 0; m
< option
->num_choices
; m
++)
2835 if (option
->choices
[m
].marked
)
2837 cgiSetArray("cchoice", k
, option
->choices
[m
].text
);
2845 cgiCopyTemplateLang("option-conflict.tmpl");
2848 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
2852 for (j
= group
->num_options
, option
= group
->options
;
2856 if (!strcmp(option
->keyword
, "PageRegion"))
2859 if (option
->num_choices
> 1)
2866 cgiSetVariable("GROUP_ID", group
->name
);
2868 if (!strcmp(group
->name
, "InstallableOptions"))
2869 cgiSetVariable("GROUP", cgiText(_("Options Installed")));
2871 cgiSetVariable("GROUP", group
->text
);
2873 cgiCopyTemplateLang("option-header.tmpl");
2875 for (j
= group
->num_options
, option
= group
->options
;
2879 if (!strcmp(option
->keyword
, "PageRegion") || option
->num_choices
< 2)
2882 cgiSetVariable("KEYWORD", option
->keyword
);
2883 cgiSetVariable("KEYTEXT", option
->text
);
2885 if (option
->conflicted
)
2886 cgiSetVariable("CONFLICTED", "1");
2888 cgiSetVariable("CONFLICTED", "0");
2890 cgiSetSize("CHOICES", 0);
2891 cgiSetSize("TEXT", 0);
2892 for (k
= 0, m
= 0; k
< option
->num_choices
; k
++)
2894 cgiSetArray("CHOICES", m
, option
->choices
[k
].choice
);
2895 cgiSetArray("TEXT", m
, option
->choices
[k
].text
);
2899 if (option
->choices
[k
].marked
)
2900 cgiSetVariable("DEFCHOICE", option
->choices
[k
].choice
);
2903 cgiSetSize("PARAMS", 0);
2904 cgiSetSize("PARAMTEXT", 0);
2905 cgiSetSize("PARAMVALUE", 0);
2906 cgiSetSize("INPUTTYPE", 0);
2908 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)))
2910 const char *units
= NULL
; /* Units value, if any */
2912 cgiSetVariable("ISCUSTOM", "1");
2914 for (cparam
= ppdFirstCustomParam(coption
), m
= 0;
2916 cparam
= ppdNextCustomParam(coption
), m
++)
2918 if (!_cups_strcasecmp(option
->keyword
, "PageSize") &&
2919 _cups_strcasecmp(cparam
->name
, "Width") &&
2920 _cups_strcasecmp(cparam
->name
, "Height"))
2926 cgiSetArray("PARAMS", m
, cparam
->name
);
2927 cgiSetArray("PARAMTEXT", m
, cparam
->text
);
2928 cgiSetArray("INPUTTYPE", m
, "text");
2930 switch (cparam
->type
)
2932 case PPD_CUSTOM_POINTS
:
2933 if (!_cups_strncasecmp(option
->defchoice
, "Custom.", 7))
2935 units
= option
->defchoice
+ strlen(option
->defchoice
) - 2;
2937 if (strcmp(units
, "mm") && strcmp(units
, "cm") &&
2938 strcmp(units
, "in") && strcmp(units
, "ft"))
2940 if (units
[1] == 'm')
2949 if (!strcmp(units
, "mm"))
2950 snprintf(value
, sizeof(value
), "%g",
2951 cparam
->current
.custom_points
/ 72.0 * 25.4);
2952 else if (!strcmp(units
, "cm"))
2953 snprintf(value
, sizeof(value
), "%g",
2954 cparam
->current
.custom_points
/ 72.0 * 2.54);
2955 else if (!strcmp(units
, "in"))
2956 snprintf(value
, sizeof(value
), "%g",
2957 cparam
->current
.custom_points
/ 72.0);
2958 else if (!strcmp(units
, "ft"))
2959 snprintf(value
, sizeof(value
), "%g",
2960 cparam
->current
.custom_points
/ 72.0 / 12.0);
2961 else if (!strcmp(units
, "m"))
2962 snprintf(value
, sizeof(value
), "%g",
2963 cparam
->current
.custom_points
/ 72.0 * 0.0254);
2965 snprintf(value
, sizeof(value
), "%g",
2966 cparam
->current
.custom_points
);
2967 cgiSetArray("PARAMVALUE", m
, value
);
2970 case PPD_CUSTOM_CURVE
:
2971 case PPD_CUSTOM_INVCURVE
:
2972 case PPD_CUSTOM_REAL
:
2973 snprintf(value
, sizeof(value
), "%g",
2974 cparam
->current
.custom_real
);
2975 cgiSetArray("PARAMVALUE", m
, value
);
2978 case PPD_CUSTOM_INT
:
2979 snprintf(value
, sizeof(value
), "%d",
2980 cparam
->current
.custom_int
);
2981 cgiSetArray("PARAMVALUE", m
, value
);
2984 case PPD_CUSTOM_PASSCODE
:
2985 case PPD_CUSTOM_PASSWORD
:
2986 if (cparam
->current
.custom_password
)
2987 cgiSetArray("PARAMVALUE", m
,
2988 cparam
->current
.custom_password
);
2990 cgiSetArray("PARAMVALUE", m
, "");
2991 cgiSetArray("INPUTTYPE", m
, "password");
2994 case PPD_CUSTOM_STRING
:
2995 if (cparam
->current
.custom_string
)
2996 cgiSetArray("PARAMVALUE", m
,
2997 cparam
->current
.custom_string
);
2999 cgiSetArray("PARAMVALUE", m
, "");
3006 cgiSetArray("PARAMS", m
, "Units");
3007 cgiSetArray("PARAMTEXT", m
, cgiText(_("Units")));
3008 cgiSetArray("PARAMVALUE", m
, units
);
3012 cgiSetVariable("ISCUSTOM", "0");
3016 case PPD_UI_BOOLEAN
:
3017 cgiCopyTemplateLang("option-boolean.tmpl");
3019 case PPD_UI_PICKONE
:
3020 cgiCopyTemplateLang("option-pickone.tmpl");
3022 case PPD_UI_PICKMANY
:
3023 cgiCopyTemplateLang("option-pickmany.tmpl");
3028 cgiCopyTemplateLang("option-trailer.tmpl");
3032 if ((attr
= ippFindAttribute(response
, "job-sheets-supported",
3033 IPP_TAG_ZERO
)) != NULL
)
3036 * Add the job sheets options...
3039 cgiSetVariable("GROUP_ID", "CUPS_BANNERS");
3040 cgiSetVariable("GROUP", cgiText(_("Banners")));
3041 cgiCopyTemplateLang("option-header.tmpl");
3043 cgiSetSize("CHOICES", attr
->num_values
);
3044 cgiSetSize("TEXT", attr
->num_values
);
3045 for (k
= 0; k
< attr
->num_values
; k
++)
3047 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3048 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3051 attr
= ippFindAttribute(response
, "job-sheets-default", IPP_TAG_ZERO
);
3053 cgiSetVariable("KEYWORD", "job_sheets_start");
3054 cgiSetVariable("KEYTEXT",
3055 /* TRANSLATORS: Banner/cover sheet before the print job. */
3056 cgiText(_("Starting Banner")));
3057 cgiSetVariable("DEFCHOICE", attr
!= NULL
?
3058 attr
->values
[0].string
.text
: "");
3060 cgiCopyTemplateLang("option-pickone.tmpl");
3062 cgiSetVariable("KEYWORD", "job_sheets_end");
3063 cgiSetVariable("KEYTEXT",
3064 /* TRANSLATORS: Banner/cover sheet after the print job. */
3065 cgiText(_("Ending Banner")));
3066 cgiSetVariable("DEFCHOICE", attr
!= NULL
&& attr
->num_values
> 1 ?
3067 attr
->values
[1].string
.text
: "");
3069 cgiCopyTemplateLang("option-pickone.tmpl");
3071 cgiCopyTemplateLang("option-trailer.tmpl");
3074 if (ippFindAttribute(response
, "printer-error-policy-supported",
3076 ippFindAttribute(response
, "printer-op-policy-supported",
3080 * Add the error and operation policy options...
3083 cgiSetVariable("GROUP_ID", "CUPS_POLICIES");
3084 cgiSetVariable("GROUP", cgiText(_("Policies")));
3085 cgiCopyTemplateLang("option-header.tmpl");
3091 attr
= ippFindAttribute(response
, "printer-error-policy-supported",
3096 cgiSetSize("CHOICES", attr
->num_values
);
3097 cgiSetSize("TEXT", attr
->num_values
);
3098 for (k
= 0; k
< attr
->num_values
; k
++)
3100 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3101 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3104 attr
= ippFindAttribute(response
, "printer-error-policy",
3107 cgiSetVariable("KEYWORD", "printer_error_policy");
3108 cgiSetVariable("KEYTEXT", cgiText(_("Error Policy")));
3109 cgiSetVariable("DEFCHOICE", attr
== NULL
?
3110 "" : attr
->values
[0].string
.text
);
3113 cgiCopyTemplateLang("option-pickone.tmpl");
3116 * Operation policy...
3119 attr
= ippFindAttribute(response
, "printer-op-policy-supported",
3124 cgiSetSize("CHOICES", attr
->num_values
);
3125 cgiSetSize("TEXT", attr
->num_values
);
3126 for (k
= 0; k
< attr
->num_values
; k
++)
3128 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3129 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3132 attr
= ippFindAttribute(response
, "printer-op-policy", IPP_TAG_ZERO
);
3134 cgiSetVariable("KEYWORD", "printer_op_policy");
3135 cgiSetVariable("KEYTEXT", cgiText(_("Operation Policy")));
3136 cgiSetVariable("DEFCHOICE", attr
== NULL
?
3137 "" : attr
->values
[0].string
.text
);
3139 cgiCopyTemplateLang("option-pickone.tmpl");
3142 cgiCopyTemplateLang("option-trailer.tmpl");
3146 * Binary protocol support...
3149 if ((attr
= ippFindAttribute(response
, "port-monitor-supported",
3150 IPP_TAG_NAME
)) != NULL
&& attr
->num_values
> 1)
3152 cgiSetVariable("GROUP_ID", "CUPS_PORT_MONITOR");
3153 cgiSetVariable("GROUP", cgiText(_("Port Monitor")));
3155 cgiSetSize("CHOICES", attr
->num_values
);
3156 cgiSetSize("TEXT", attr
->num_values
);
3158 for (i
= 0; i
< attr
->num_values
; i
++)
3160 cgiSetArray("CHOICES", i
, attr
->values
[i
].string
.text
);
3161 cgiSetArray("TEXT", i
, attr
->values
[i
].string
.text
);
3164 attr
= ippFindAttribute(response
, "port-monitor", IPP_TAG_NAME
);
3165 cgiSetVariable("KEYWORD", "port_monitor");
3166 cgiSetVariable("KEYTEXT", cgiText(_("Port Monitor")));
3167 cgiSetVariable("DEFCHOICE", attr
? attr
->values
[0].string
.text
: "none");
3169 cgiCopyTemplateLang("option-header.tmpl");
3170 cgiCopyTemplateLang("option-pickone.tmpl");
3171 cgiCopyTemplateLang("option-trailer.tmpl");
3174 cgiCopyTemplateLang("set-printer-options-trailer.tmpl");
3177 ippDelete(response
);
3182 * Set default options...
3185 fputs("DEBUG: Setting options...\n", stderr
);
3189 out
= cupsTempFile2(tempfile
, sizeof(tempfile
));
3190 in
= cupsFileOpen(filename
, "r");
3194 cgiSetVariable("ERROR", strerror(errno
));
3195 cgiStartHTML(cgiText(_("Set Printer Options")));
3196 cgiCopyTemplateLang("error.tmpl");
3212 while (cupsFileGets(in
, line
, sizeof(line
)))
3214 if (!strncmp(line
, "*cupsProtocol:", 14))
3216 else if (strncmp(line
, "*Default", 8))
3217 cupsFilePrintf(out
, "%s\n", line
);
3221 * Get default option name...
3224 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
3226 for (keyptr
= keyword
; *keyptr
; keyptr
++)
3227 if (*keyptr
== ':' || isspace(*keyptr
& 255))
3232 if (!strcmp(keyword
, "PageRegion") ||
3233 !strcmp(keyword
, "PaperDimension") ||
3234 !strcmp(keyword
, "ImageableArea"))
3235 var
= get_option_value(ppd
, "PageSize", value
, sizeof(value
));
3237 var
= get_option_value(ppd
, keyword
, value
, sizeof(value
));
3240 cupsFilePrintf(out
, "%s\n", line
);
3242 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, var
);
3252 * Make sure temporary filename is cleared when there is no PPD...
3259 * Build a CUPS_ADD_MODIFY_CLASS/PRINTER request, which requires the
3260 * following attributes:
3262 * attributes-charset
3263 * attributes-natural-language
3265 * job-sheets-default
3266 * printer-error-policy
3271 request
= ippNewRequest(is_class
? CUPS_ADD_MODIFY_CLASS
:
3272 CUPS_ADD_MODIFY_PRINTER
);
3274 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3277 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3278 "job-sheets-default", 2, NULL
, NULL
);
3279 ippSetString(request
, &attr
, 0, cgiGetVariable("job_sheets_start"));
3280 ippSetString(request
, &attr
, 1, cgiGetVariable("job_sheets_end"));
3282 if ((var
= cgiGetVariable("printer_error_policy")) != NULL
)
3283 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3284 "printer-error-policy", NULL
, var
);
3286 if ((var
= cgiGetVariable("printer_op_policy")) != NULL
)
3287 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3288 "printer-op-policy", NULL
, var
);
3290 if ((var
= cgiGetVariable("port_monitor")) != NULL
)
3291 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3292 "port-monitor", NULL
, var
);
3295 * Do the request and get back a response...
3299 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", tempfile
));
3301 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
3303 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
3305 puts("Status: 401\n");
3308 else if (cupsLastError() > IPP_OK_CONFLICT
)
3310 cgiStartHTML(title
);
3311 cgiShowIPPError(_("Unable to set options"));
3316 * Redirect successful updates back to the printer page...
3319 char refresh
[1024]; /* Refresh URL */
3322 cgiFormEncode(uri
, printer
, sizeof(uri
));
3323 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=/%s/%s",
3324 is_class
? "classes" : "printers", uri
);
3325 cgiSetVariable("refresh_page", refresh
);
3327 cgiStartHTML(title
);
3329 cgiCopyTemplateLang("printer-configured.tmpl");
3344 * 'do_set_sharing()' - Set printer-is-shared value.
3348 do_set_sharing(http_t
*http
) /* I - HTTP connection */
3350 ipp_t
*request
, /* IPP request */
3351 *response
; /* IPP response */
3352 char uri
[HTTP_MAX_URI
]; /* Printer URI */
3353 const char *printer
, /* Printer name */
3354 *is_class
, /* Is a class? */
3355 *shared
; /* Sharing value */
3358 is_class
= cgiGetVariable("IS_CLASS");
3359 printer
= cgiGetVariable("PRINTER_NAME");
3360 shared
= cgiGetVariable("SHARED");
3362 if (!printer
|| !shared
)
3364 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
3365 cgiStartHTML(cgiText(_("Set Publishing")));
3366 cgiCopyTemplateLang("error.tmpl");
3372 * Build a CUPS-Add-Printer/CUPS-Add-Class request, which requires the
3373 * following attributes:
3375 * attributes-charset
3376 * attributes-natural-language
3381 request
= ippNewRequest(is_class
? CUPS_ADD_CLASS
: CUPS_ADD_PRINTER
);
3383 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3384 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
3386 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3389 ippAddBoolean(request
, IPP_TAG_OPERATION
, "printer-is-shared", (char)atoi(shared
));
3392 * Do the request and get back a response...
3395 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
)
3397 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
3399 ippDelete(response
);
3402 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
3404 puts("Status: 401\n");
3407 else if (cupsLastError() > IPP_OK_CONFLICT
)
3409 cgiStartHTML(cgiText(_("Set Publishing")));
3410 cgiShowIPPError(_("Unable to change printer-is-shared attribute"));
3415 * Redirect successful updates back to the printer page...
3418 char url
[1024], /* Printer/class URL */
3419 refresh
[1024]; /* Refresh URL */
3422 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
3423 cgiFormEncode(uri
, url
, sizeof(uri
));
3424 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s", uri
);
3425 cgiSetVariable("refresh_page", refresh
);
3427 cgiStartHTML(cgiText(_("Set Publishing")));
3428 cgiCopyTemplateLang(is_class
? "class-modified.tmpl" :
3429 "printer-modified.tmpl");
3437 * 'get_option_value()' - Return the value of an option.
3439 * This function also handles generation of custom option values.
3442 static char * /* O - Value string or NULL on error */
3444 ppd_file_t
*ppd
, /* I - PPD file */
3445 const char *name
, /* I - Option name */
3446 char *buffer
, /* I - String buffer */
3447 size_t bufsize
) /* I - Size of buffer */
3449 char *bufptr
, /* Pointer into buffer */
3450 *bufend
; /* End of buffer */
3451 ppd_coption_t
*coption
; /* Custom option */
3452 ppd_cparam_t
*cparam
; /* Current custom parameter */
3453 char keyword
[256]; /* Parameter name */
3454 const char *val
, /* Parameter value */
3455 *uval
; /* Units value */
3456 long integer
; /* Integer value */
3457 double number
, /* Number value */
3458 number_points
; /* Number in points */
3462 * See if we have a custom option choice...
3465 if ((val
= cgiGetVariable(name
)) == NULL
)
3473 else if (_cups_strcasecmp(val
, "Custom") ||
3474 (coption
= ppdFindCustomOption(ppd
, name
)) == NULL
)
3477 * Not a custom choice...
3480 strlcpy(buffer
, val
, bufsize
);
3485 * OK, we have a custom option choice, format it...
3490 if (!strcmp(coption
->keyword
, "PageSize"))
3492 const char *lval
; /* Length string value */
3493 double width
, /* Width value */
3494 width_points
, /* Width in points */
3495 length
, /* Length value */
3496 length_points
; /* Length in points */
3499 val
= cgiGetVariable("PageSize.Width");
3500 lval
= cgiGetVariable("PageSize.Height");
3501 uval
= cgiGetVariable("PageSize.Units");
3503 if (!val
|| !lval
|| !uval
||
3504 (width
= strtod(val
, NULL
)) == 0.0 ||
3505 (length
= strtod(lval
, NULL
)) == 0.0 ||
3506 (strcmp(uval
, "pt") && strcmp(uval
, "in") && strcmp(uval
, "ft") &&
3507 strcmp(uval
, "cm") && strcmp(uval
, "mm") && strcmp(uval
, "m")))
3510 width_points
= get_points(width
, uval
);
3511 length_points
= get_points(length
, uval
);
3513 if (width_points
< ppd
->custom_min
[0] ||
3514 width_points
> ppd
->custom_max
[0] ||
3515 length_points
< ppd
->custom_min
[1] ||
3516 length_points
> ppd
->custom_max
[1])
3519 snprintf(buffer
, bufsize
, "Custom.%gx%g%s", width
, length
, uval
);
3521 else if (cupsArrayCount(coption
->params
) == 1)
3523 cparam
= ppdFirstCustomParam(coption
);
3524 snprintf(keyword
, sizeof(keyword
), "%s.%s", coption
->keyword
, cparam
->name
);
3526 if ((val
= cgiGetVariable(keyword
)) == NULL
)
3529 switch (cparam
->type
)
3531 case PPD_CUSTOM_CURVE
:
3532 case PPD_CUSTOM_INVCURVE
:
3533 case PPD_CUSTOM_REAL
:
3534 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3535 number
< cparam
->minimum
.custom_real
||
3536 number
> cparam
->maximum
.custom_real
)
3539 snprintf(buffer
, bufsize
, "Custom.%g", number
);
3542 case PPD_CUSTOM_INT
:
3543 if (!*val
|| (integer
= strtol(val
, NULL
, 10)) == LONG_MIN
||
3544 integer
== LONG_MAX
||
3545 integer
< cparam
->minimum
.custom_int
||
3546 integer
> cparam
->maximum
.custom_int
)
3549 snprintf(buffer
, bufsize
, "Custom.%ld", integer
);
3552 case PPD_CUSTOM_POINTS
:
3553 snprintf(keyword
, sizeof(keyword
), "%s.Units", coption
->keyword
);
3555 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3556 (uval
= cgiGetVariable(keyword
)) == NULL
||
3557 (strcmp(uval
, "pt") && strcmp(uval
, "in") && strcmp(uval
, "ft") &&
3558 strcmp(uval
, "cm") && strcmp(uval
, "mm") && strcmp(uval
, "m")))
3561 number_points
= get_points(number
, uval
);
3562 if (number_points
< cparam
->minimum
.custom_points
||
3563 number_points
> cparam
->maximum
.custom_points
)
3566 snprintf(buffer
, bufsize
, "Custom.%g%s", number
, uval
);
3569 case PPD_CUSTOM_PASSCODE
:
3570 for (uval
= val
; *uval
; uval
++)
3571 if (!isdigit(*uval
& 255))
3574 case PPD_CUSTOM_PASSWORD
:
3575 case PPD_CUSTOM_STRING
:
3576 integer
= (long)strlen(val
);
3577 if (integer
< cparam
->minimum
.custom_string
||
3578 integer
> cparam
->maximum
.custom_string
)
3581 snprintf(buffer
, bufsize
, "Custom.%s", val
);
3587 const char *prefix
= "{"; /* Prefix string */
3591 bufend
= buffer
+ bufsize
;
3593 for (cparam
= ppdFirstCustomParam(coption
);
3595 cparam
= ppdNextCustomParam(coption
))
3597 snprintf(keyword
, sizeof(keyword
), "%s.%s", coption
->keyword
,
3600 if ((val
= cgiGetVariable(keyword
)) == NULL
)
3603 snprintf(bufptr
, (size_t)(bufend
- bufptr
), "%s%s=", prefix
, cparam
->name
);
3604 bufptr
+= strlen(bufptr
);
3607 switch (cparam
->type
)
3609 case PPD_CUSTOM_CURVE
:
3610 case PPD_CUSTOM_INVCURVE
:
3611 case PPD_CUSTOM_REAL
:
3612 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3613 number
< cparam
->minimum
.custom_real
||
3614 number
> cparam
->maximum
.custom_real
)
3617 snprintf(bufptr
, (size_t)(bufend
- bufptr
), "%g", number
);
3620 case PPD_CUSTOM_INT
:
3621 if (!*val
|| (integer
= strtol(val
, NULL
, 10)) == LONG_MIN
||
3622 integer
== LONG_MAX
||
3623 integer
< cparam
->minimum
.custom_int
||
3624 integer
> cparam
->maximum
.custom_int
)
3627 snprintf(bufptr
, (size_t)(bufend
- bufptr
), "%ld", integer
);
3630 case PPD_CUSTOM_POINTS
:
3631 snprintf(keyword
, sizeof(keyword
), "%s.Units", coption
->keyword
);
3633 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3634 (uval
= cgiGetVariable(keyword
)) == NULL
||
3635 (strcmp(uval
, "pt") && strcmp(uval
, "in") &&
3636 strcmp(uval
, "ft") && strcmp(uval
, "cm") &&
3637 strcmp(uval
, "mm") && strcmp(uval
, "m")))
3640 number_points
= get_points(number
, uval
);
3641 if (number_points
< cparam
->minimum
.custom_points
||
3642 number_points
> cparam
->maximum
.custom_points
)
3645 snprintf(bufptr
, (size_t)(bufend
- bufptr
), "%g%s", number
, uval
);
3648 case PPD_CUSTOM_PASSCODE
:
3649 for (uval
= val
; *uval
; uval
++)
3650 if (!isdigit(*uval
& 255))
3653 case PPD_CUSTOM_PASSWORD
:
3654 case PPD_CUSTOM_STRING
:
3655 integer
= (long)strlen(val
);
3656 if (integer
< cparam
->minimum
.custom_string
||
3657 integer
> cparam
->maximum
.custom_string
)
3660 if ((bufptr
+ 2) > bufend
)
3666 while (*val
&& bufptr
< bufend
)
3668 if (*val
== '\\' || *val
== '\"')
3670 if ((bufptr
+ 1) >= bufend
)
3679 if (bufptr
>= bufend
)
3688 bufptr
+= strlen(bufptr
);
3691 if (bufptr
== buffer
|| (bufend
- bufptr
) < 2)
3694 memcpy(bufptr
, "}", 2);
3702 * 'get_points()' - Get a value in points.
3705 static double /* O - Number in points */
3706 get_points(double number
, /* I - Original number */
3707 const char *uval
) /* I - Units */
3709 if (!strcmp(uval
, "mm")) /* Millimeters */
3710 return (number
* 72.0 / 25.4);
3711 else if (!strcmp(uval
, "cm")) /* Centimeters */
3712 return (number
* 72.0 / 2.54);
3713 else if (!strcmp(uval
, "in")) /* Inches */
3714 return (number
* 72.0);
3715 else if (!strcmp(uval
, "ft")) /* Feet */
3716 return (number
* 72.0 * 12.0);
3717 else if (!strcmp(uval
, "m")) /* Meters */
3718 return (number
* 72.0 / 0.0254);
3725 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
3728 static char * /* O - Filename or NULL */
3729 get_printer_ppd(const char *uri
, /* I - Printer URI */
3730 char *buffer
, /* I - Filename buffer */
3731 size_t bufsize
) /* I - Size of filename buffer */
3733 http_t
*http
; /* Connection to printer */
3734 ipp_t
*request
, /* Get-Printer-Attributes request */
3735 *response
; /* Get-Printer-Attributes response */
3736 char resolved
[1024], /* Resolved URI */
3737 scheme
[32], /* URI scheme */
3738 userpass
[256], /* Username:password */
3739 host
[256], /* Hostname */
3740 resource
[256]; /* Resource path */
3741 int port
; /* Port number */
3742 static const char * const pattrs
[] = /* Printer attributes we need */
3745 "media-col-database"
3750 * Connect to the printer...
3753 if (strstr(uri
, "._tcp"))
3759 if (!_httpResolveURI(uri
, resolved
, sizeof(resolved
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
3761 fprintf(stderr
, "ERROR: Unable to resolve \"%s\".\n", uri
);
3768 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
3770 fprintf(stderr
, "ERROR: Bad printer URI \"%s\".\n", uri
);
3774 http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, !strcmp(scheme
, "ipps") ? HTTP_ENCRYPTION_ALWAYS
: HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
);
3777 fprintf(stderr
, "ERROR: Unable to connect to \"%s:%d\": %s\n", host
, port
, cupsLastErrorString());
3782 * Send a Get-Printer-Attributes request...
3785 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
3786 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
3787 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
3788 response
= cupsDoRequest(http
, request
, resource
);
3790 if (!_ppdCreateFromIPP(buffer
, bufsize
, response
))
3791 fprintf(stderr
, "ERROR: Unable to create PPD file: %s\n", strerror(errno
));
3793 ippDelete(response
);