2 * Administration CGI for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
18 #include "cgi-private.h"
19 #include <cups/http-private.h>
20 #include <cups/ppd-private.h>
21 #include <cups/adminutil.h>
34 static int current_device
= 0; /* Current device shown */
41 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
);
42 static void do_am_class(http_t
*http
, int modify
);
43 static void do_am_printer(http_t
*http
, int modify
);
44 static void do_config_server(http_t
*http
);
45 static void do_delete_class(http_t
*http
);
46 static void do_delete_printer(http_t
*http
);
47 static void do_export(http_t
*http
);
48 static void do_list_printers(http_t
*http
);
49 static void do_menu(http_t
*http
);
50 static void do_set_allowed_users(http_t
*http
);
51 static void do_set_default(http_t
*http
);
52 static void do_set_options(http_t
*http
, int is_class
);
53 static void do_set_sharing(http_t
*http
);
54 static char *get_option_value(ppd_file_t
*ppd
, const char *name
,
55 char *buffer
, size_t bufsize
);
56 static double get_points(double number
, const char *uval
);
57 static char *get_printer_ppd(const char *uri
, char *buffer
, size_t bufsize
);
61 * 'main()' - Main entry for CGI.
64 int /* O - Exit status */
67 http_t
*http
; /* Connection to the server */
68 const char *op
; /* Operation name */
72 * Connect to the HTTP server...
75 fputs("DEBUG: admin.cgi started...\n", stderr
);
77 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
81 perror("ERROR: Unable to connect to cupsd");
82 fprintf(stderr
, "DEBUG: cupsServer()=\"%s\"\n",
83 cupsServer() ? cupsServer() : "(null)");
84 fprintf(stderr
, "DEBUG: ippPort()=%d\n", ippPort());
85 fprintf(stderr
, "DEBUG: cupsEncryption()=%d\n", cupsEncryption());
89 fprintf(stderr
, "DEBUG: http=%p\n", http
);
92 * Set the web interface section...
95 cgiSetVariable("SECTION", "admin");
96 cgiSetVariable("REFRESH_PAGE", "");
99 * See if we have form data...
102 if (!cgiInitialize() || !cgiGetVariable("OP"))
105 * Nope, send the administration menu...
108 fputs("DEBUG: No form data, showing main menu...\n", stderr
);
112 else if ((op
= cgiGetVariable("OP")) != NULL
&& cgiIsPOST())
115 * Do the operation...
118 fprintf(stderr
, "DEBUG: op=\"%s\"...\n", op
);
122 const char *printer
= getenv("PRINTER_NAME"),
123 /* Printer or class name */
124 *server_port
= getenv("SERVER_PORT");
125 /* Port number string */
126 int port
= atoi(server_port
? server_port
: "0");
128 char uri
[1024]; /* URL */
131 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
132 getenv("HTTPS") ? "https" : "http", NULL
,
133 getenv("SERVER_NAME"), port
, "/%s/%s",
134 cgiGetVariable("IS_CLASS") ? "classes" : "printers",
137 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
138 getenv("HTTPS") ? "https" : "http", NULL
,
139 getenv("SERVER_NAME"), port
, "/admin");
141 printf("Location: %s\n\n", uri
);
143 else if (!strcmp(op
, "set-allowed-users"))
144 do_set_allowed_users(http
);
145 else if (!strcmp(op
, "set-as-default"))
146 do_set_default(http
);
147 else if (!strcmp(op
, "set-sharing"))
148 do_set_sharing(http
);
149 else if (!strcmp(op
, "find-new-printers") ||
150 !strcmp(op
, "list-available-printers"))
151 do_list_printers(http
);
152 else if (!strcmp(op
, "add-class"))
153 do_am_class(http
, 0);
154 else if (!strcmp(op
, "add-printer"))
155 do_am_printer(http
, 0);
156 else if (!strcmp(op
, "modify-class"))
157 do_am_class(http
, 1);
158 else if (!strcmp(op
, "modify-printer"))
159 do_am_printer(http
, 1);
160 else if (!strcmp(op
, "delete-class"))
161 do_delete_class(http
);
162 else if (!strcmp(op
, "delete-printer"))
163 do_delete_printer(http
);
164 else if (!strcmp(op
, "set-class-options"))
165 do_set_options(http
, 1);
166 else if (!strcmp(op
, "set-printer-options"))
167 do_set_options(http
, 0);
168 else if (!strcmp(op
, "config-server"))
169 do_config_server(http
);
170 else if (!strcmp(op
, "export-samba"))
175 * Bad operation code - display an error...
178 cgiStartHTML(cgiText(_("Administration")));
179 cgiCopyTemplateLang("error-op.tmpl");
183 else if (op
&& !strcmp(op
, "redirect"))
185 const char *url
; /* Redirection URL... */
186 char prefix
[1024]; /* URL prefix */
190 snprintf(prefix
, sizeof(prefix
), "https://%s:%s",
191 getenv("SERVER_NAME"), getenv("SERVER_PORT"));
193 snprintf(prefix
, sizeof(prefix
), "http://%s:%s",
194 getenv("SERVER_NAME"), getenv("SERVER_PORT"));
196 fprintf(stderr
, "DEBUG: redirecting with prefix %s!\n", prefix
);
198 if ((url
= cgiGetVariable("URL")) != NULL
)
200 char encoded
[1024], /* Encoded URL string */
201 *ptr
; /* Pointer into encoded string */
208 for (; *url
&& ptr
< (encoded
+ sizeof(encoded
) - 4); url
++)
210 if (strchr("%@&+ <>#=", *url
) || *url
< ' ' || *url
& 128)
213 * Percent-encode this character; safe because we have at least 4
214 * bytes left in the array...
217 sprintf(ptr
, "%%%02X", *url
& 255);
229 * URL was too long, just redirect to the admin page...
232 printf("Location: %s/admin\n\n", prefix
);
237 * URL is OK, redirect there...
240 printf("Location: %s%s\n\n", prefix
, encoded
);
244 printf("Location: %s/admin\n\n", prefix
);
249 * Form data but no operation code - display an error...
252 cgiStartHTML(cgiText(_("Administration")));
253 cgiCopyTemplateLang("error-op.tmpl");
258 * Close the HTTP server connection...
264 * Return with no errors...
272 * 'choose_device_cb()' - Add a device to the device selection page.
277 const char *device_class
, /* I - Class */
278 const char *device_id
, /* I - 1284 device ID */
279 const char *device_info
, /* I - Description */
280 const char *device_make_and_model
, /* I - Make and model */
281 const char *device_uri
, /* I - Device URI */
282 const char *device_location
, /* I - Location */
283 const char *title
) /* I - Page title */
286 * For modern browsers, start a multi-part page so we can show that something
287 * is happening. Non-modern browsers just get everything at the end...
290 if (current_device
== 0 && cgiSupportsMultipart())
294 cgiCopyTemplateLang("choose-device.tmpl");
301 * Add the device to the array...
304 cgiSetArray("device_class", current_device
, device_class
);
305 cgiSetArray("device_id", current_device
, device_id
);
306 cgiSetArray("device_info", current_device
, device_info
);
307 cgiSetArray("device_make_and_model", current_device
, device_make_and_model
);
308 cgiSetArray("device_uri", current_device
, device_uri
);
309 cgiSetArray("device_location", current_device
, device_location
);
316 * 'do_am_class()' - Add or modify a class.
320 do_am_class(http_t
*http
, /* I - HTTP connection */
321 int modify
) /* I - Modify the printer? */
323 int i
, j
; /* Looping vars */
324 int element
; /* Element number */
325 int num_printers
; /* Number of printers */
326 ipp_t
*request
, /* IPP request */
327 *response
; /* IPP response */
328 ipp_attribute_t
*attr
; /* member-uris attribute */
329 char uri
[HTTP_MAX_URI
]; /* Device or printer URI */
330 const char *name
, /* Pointer to class name */
331 *op
, /* Operation name */
332 *ptr
; /* Pointer to CGI variable */
333 const char *title
; /* Title of page */
334 static const char * const pattrs
[] = /* Requested printer attributes */
342 title
= cgiText(modify
? _("Modify Class") : _("Add Class"));
343 op
= cgiGetVariable("OP");
344 name
= cgiGetVariable("PRINTER_NAME");
346 if (cgiGetVariable("PRINTER_LOCATION") == NULL
)
349 * Build a CUPS_GET_PRINTERS request, which requires the
350 * following attributes:
353 * attributes-natural-language
356 request
= ippNewRequest(CUPS_GET_PRINTERS
);
358 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type",
360 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask",
361 CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
364 * Do the request and get back a response...
369 cgiSetVariable("OP", op
);
371 cgiSetVariable("PRINTER_NAME", name
);
373 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
376 * Create MEMBER_URIS and MEMBER_NAMES arrays...
379 for (element
= 0, attr
= response
->attrs
;
382 if (attr
->name
&& !strcmp(attr
->name
, "printer-uri-supported"))
384 if ((ptr
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
&&
385 (!name
|| _cups_strcasecmp(name
, ptr
+ 1)))
388 * Don't show the current class...
391 cgiSetArray("MEMBER_URIS", element
, attr
->values
[0].string
.text
);
396 for (element
= 0, attr
= response
->attrs
;
399 if (attr
->name
&& !strcmp(attr
->name
, "printer-name"))
401 if (!name
|| _cups_strcasecmp(name
, attr
->values
[0].string
.text
))
404 * Don't show the current class...
407 cgiSetArray("MEMBER_NAMES", element
, attr
->values
[0].string
.text
);
412 num_printers
= cgiGetSize("MEMBER_URIS");
422 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
423 * following attributes:
426 * attributes-natural-language
430 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
432 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
433 "localhost", 0, "/classes/%s", name
);
434 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
437 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
438 "requested-attributes",
439 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])),
443 * Do the request and get back a response...
446 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
448 if ((attr
= ippFindAttribute(response
, "member-names",
449 IPP_TAG_NAME
)) != NULL
)
452 * Mark any current members in the class...
455 for (j
= 0; j
< num_printers
; j
++)
456 cgiSetArray("MEMBER_SELECTED", j
, "");
458 for (i
= 0; i
< attr
->num_values
; i
++)
460 for (j
= 0; j
< num_printers
; j
++)
462 if (!_cups_strcasecmp(attr
->values
[i
].string
.text
,
463 cgiGetArray("MEMBER_NAMES", j
)))
465 cgiSetArray("MEMBER_SELECTED", j
, "SELECTED");
472 if ((attr
= ippFindAttribute(response
, "printer-info",
473 IPP_TAG_TEXT
)) != NULL
)
474 cgiSetVariable("PRINTER_INFO", attr
->values
[0].string
.text
);
476 if ((attr
= ippFindAttribute(response
, "printer-location",
477 IPP_TAG_TEXT
)) != NULL
)
478 cgiSetVariable("PRINTER_LOCATION", attr
->values
[0].string
.text
);
484 * Update the location and description of an existing printer...
488 cgiCopyTemplateLang("modify-class.tmpl");
493 * Get the name, location, and description for a new printer...
497 cgiCopyTemplateLang("add-class.tmpl");
508 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
509 cgiCopyTemplateLang("error.tmpl");
514 for (ptr
= name
; *ptr
; ptr
++)
515 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
518 if (*ptr
|| ptr
== name
|| strlen(name
) > 127)
520 cgiSetVariable("ERROR",
521 cgiText(_("The class name may only contain up to "
522 "127 printable characters and may not "
523 "contain spaces, slashes (/), or the "
524 "pound sign (#).")));
526 cgiCopyTemplateLang("error.tmpl");
532 * Build a CUPS_ADD_CLASS request, which requires the following
536 * attributes-natural-language
540 * printer-is-accepting-jobs
545 request
= ippNewRequest(CUPS_ADD_CLASS
);
547 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
548 "localhost", 0, "/classes/%s", name
);
549 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
552 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
553 NULL
, cgiGetVariable("PRINTER_LOCATION"));
555 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
556 NULL
, cgiGetVariable("PRINTER_INFO"));
558 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
560 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
563 if ((num_printers
= cgiGetSize("MEMBER_URIS")) > 0)
565 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris",
566 num_printers
, NULL
, NULL
);
567 for (i
= 0; i
< num_printers
; i
++)
568 attr
->values
[i
].string
.text
= _cupsStrAlloc(cgiGetArray("MEMBER_URIS", i
));
572 * Do the request and get back a response...
575 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
577 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
579 puts("Status: 401\n");
582 else if (cupsLastError() > IPP_OK_CONFLICT
)
585 cgiShowIPPError(modify
? _("Unable to modify class") :
586 _("Unable to add class"));
591 * Redirect successful updates back to the class page...
594 char refresh
[1024]; /* Refresh URL */
596 cgiFormEncode(uri
, name
, sizeof(uri
));
597 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=/classes/%s",
599 cgiSetVariable("refresh_page", refresh
);
604 cgiCopyTemplateLang("class-modified.tmpl");
606 cgiCopyTemplateLang("class-added.tmpl");
614 * 'do_am_printer()' - Add or modify a printer.
618 do_am_printer(http_t
*http
, /* I - HTTP connection */
619 int modify
) /* I - Modify the printer? */
621 int i
; /* Looping var */
622 ipp_attribute_t
*attr
; /* Current attribute */
623 ipp_t
*request
, /* IPP request */
624 *response
, /* IPP response */
625 *oldinfo
; /* Old printer information */
626 const cgi_file_t
*file
; /* Uploaded file, if any */
627 const char *var
; /* CGI variable */
628 char uri
[HTTP_MAX_URI
], /* Device or printer URI */
629 *uriptr
, /* Pointer into URI */
630 evefile
[1024] = ""; /* IPP Everywhere PPD file */
631 int maxrate
; /* Maximum baud rate */
632 char baudrate
[255]; /* Baud rate string */
633 const char *name
, /* Pointer to class name */
634 *ptr
; /* Pointer to CGI variable */
635 const char *title
; /* Title of page */
636 static int baudrates
[] = /* Baud rates */
651 ptr
= cgiGetVariable("DEVICE_URI");
652 fprintf(stderr
, "DEBUG: do_am_printer: DEVICE_URI=\"%s\"\n",
653 ptr
? ptr
: "(null)");
655 title
= cgiText(modify
? _("Modify Printer") : _("Add Printer"));
660 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
661 * following attributes:
664 * attributes-natural-language
668 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
670 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
671 "localhost", 0, "/printers/%s",
672 cgiGetVariable("PRINTER_NAME"));
673 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
677 * Do the request and get back a response...
680 oldinfo
= cupsDoRequest(http
, request
, "/");
689 fprintf(stderr
, "DEBUG: file->tempfile=%s\n", file
->tempfile
);
690 fprintf(stderr
, "DEBUG: file->name=%s\n", file
->name
);
691 fprintf(stderr
, "DEBUG: file->filename=%s\n", file
->filename
);
692 fprintf(stderr
, "DEBUG: file->mimetype=%s\n", file
->mimetype
);
695 if ((name
= cgiGetVariable("PRINTER_NAME")) != NULL
)
697 for (ptr
= name
; *ptr
; ptr
++)
698 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '\\' || *ptr
== '?' || *ptr
== '\'' || *ptr
== '\"' || *ptr
== '#')
701 if (*ptr
|| ptr
== name
|| strlen(name
) > 127)
703 cgiSetVariable("ERROR",
704 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 (#).")));
706 cgiCopyTemplateLang("error.tmpl");
712 if ((var
= cgiGetVariable("DEVICE_URI")) != NULL
)
714 if ((uriptr
= strrchr(var
, '|')) != NULL
)
717 * Extract make and make/model from device URI string...
720 char make
[1024], /* Make string */
721 *makeptr
; /* Pointer into make */
726 strlcpy(make
, uriptr
, sizeof(make
));
728 if ((makeptr
= strchr(make
, ' ')) != NULL
)
730 else if ((makeptr
= strchr(make
, '-')) != NULL
)
732 else if (!_cups_strncasecmp(make
, "laserjet", 8) ||
733 !_cups_strncasecmp(make
, "deskjet", 7) ||
734 !_cups_strncasecmp(make
, "designjet", 9))
735 strlcpy(make
, "HP", sizeof(make
));
736 else if (!_cups_strncasecmp(make
, "phaser", 6))
737 strlcpy(make
, "Xerox", sizeof(make
));
738 else if (!_cups_strncasecmp(make
, "stylus", 6))
739 strlcpy(make
, "Epson", sizeof(make
));
741 strlcpy(make
, "Generic", sizeof(make
));
743 if (!cgiGetVariable("CURRENT_MAKE"))
744 cgiSetVariable("CURRENT_MAKE", make
);
746 if (!cgiGetVariable("CURRENT_MAKE_AND_MODEL"))
747 cgiSetVariable("CURRENT_MAKE_AND_MODEL", uriptr
);
751 char template[128], /* Template name */
752 *tptr
; /* Pointer into template name */
754 cgiSetVariable("PRINTER_INFO", uriptr
);
756 for (tptr
= template;
757 tptr
< (template + sizeof(template) - 1) && *uriptr
;
759 if (isalnum(*uriptr
& 255) || *uriptr
== '_' || *uriptr
== '-' ||
762 else if ((*uriptr
== ' ' || *uriptr
== '/') && tptr
> template &&
765 else if (*uriptr
== '?' || *uriptr
== '(')
770 cgiSetVariable("TEMPLATE_NAME", template);
778 * Look for devices so the user can pick something...
781 if ((attr
= ippFindAttribute(oldinfo
, "device-uri", IPP_TAG_URI
)) != NULL
)
783 strlcpy(uri
, attr
->values
[0].string
.text
, sizeof(uri
));
784 if ((uriptr
= strchr(uri
, ':')) != NULL
&& strncmp(uriptr
, "://", 3) == 0)
787 cgiSetVariable("CURRENT_DEVICE_URI", attr
->values
[0].string
.text
);
788 cgiSetVariable("CURRENT_DEVICE_SCHEME", uri
);
792 * Scan for devices for up to 30 seconds...
795 fputs("DEBUG: Getting list of devices...\n", stderr
);
798 if (cupsGetDevices(http
, 5, CUPS_INCLUDE_ALL
, CUPS_EXCLUDE_NONE
,
799 (cups_device_cb_t
)choose_device_cb
,
800 (void *)title
) == IPP_OK
)
802 fputs("DEBUG: Got device list!\n", stderr
);
804 if (cgiSupportsMultipart())
807 cgiSetVariable("CUPS_GET_DEVICES_DONE", "1");
809 cgiCopyTemplateLang("choose-device.tmpl");
812 if (cgiSupportsMultipart())
818 "ERROR: CUPS-Get-Devices request failed with status %x: %s\n",
819 cupsLastError(), cupsLastErrorString());
820 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
822 puts("Status: 401\n");
828 cgiShowIPPError(modify
? _("Unable to modify printer") :
829 _("Unable to add printer"));
835 else if (!strchr(var
, '/') ||
836 (!strncmp(var
, "lpd://", 6) && !strchr(var
+ 6, '/')))
838 if ((attr
= ippFindAttribute(oldinfo
, "device-uri", IPP_TAG_URI
)) != NULL
)
841 * Set the current device URI for the form to the old one...
844 if (strncmp(attr
->values
[0].string
.text
, var
, strlen(var
)) == 0)
845 cgiSetVariable("CURRENT_DEVICE_URI", attr
->values
[0].string
.text
);
849 * User needs to set the full URI...
853 cgiCopyTemplateLang("choose-uri.tmpl");
856 else if (!strncmp(var
, "serial:", 7) && !cgiGetVariable("BAUDRATE"))
859 * Need baud rate, parity, etc.
862 if ((var
= strchr(var
, '?')) != NULL
&&
863 strncmp(var
, "?baud=", 6) == 0)
864 maxrate
= atoi(var
+ 6);
868 for (i
= 0; i
< 10; i
++)
869 if (baudrates
[i
] > maxrate
)
873 sprintf(baudrate
, "%d", baudrates
[i
]);
874 cgiSetArray("BAUDRATES", i
, baudrate
);
878 cgiCopyTemplateLang("choose-serial.tmpl");
881 else if (!name
|| !cgiGetVariable("PRINTER_LOCATION"))
888 * Update the location and description of an existing printer...
893 if ((attr
= ippFindAttribute(oldinfo
, "printer-info",
894 IPP_TAG_TEXT
)) != NULL
)
895 cgiSetVariable("PRINTER_INFO", attr
->values
[0].string
.text
);
897 if ((attr
= ippFindAttribute(oldinfo
, "printer-location",
898 IPP_TAG_TEXT
)) != NULL
)
899 cgiSetVariable("PRINTER_LOCATION", attr
->values
[0].string
.text
);
901 if ((attr
= ippFindAttribute(oldinfo
, "printer-is-shared",
902 IPP_TAG_BOOLEAN
)) != NULL
)
903 cgiSetVariable("PRINTER_IS_SHARED",
904 attr
->values
[0].boolean
? "1" : "0");
907 cgiCopyTemplateLang("modify-printer.tmpl");
912 * Get the name, location, and description for a new printer...
916 if (!strncmp(var
, "usb:", 4))
917 cgiSetVariable("printer_is_shared", "1");
919 #endif /* __APPLE__ */
920 cgiSetVariable("printer_is_shared", "0");
922 cgiCopyTemplateLang("add-printer.tmpl");
933 (!cgiGetVariable("PPD_NAME") || cgiGetVariable("SELECT_MAKE")))
935 if (modify
&& !cgiGetVariable("SELECT_MAKE"))
938 * Get the PPD file...
941 int fd
; /* PPD file */
942 char filename
[1024]; /* PPD filename */
943 ppd_file_t
*ppd
; /* PPD information */
944 char buffer
[1024]; /* Buffer */
945 ssize_t bytes
; /* Number of bytes */
946 http_status_t get_status
; /* Status of GET */
949 /* TODO: Use cupsGetFile() API... */
950 snprintf(uri
, sizeof(uri
), "/printers/%s.ppd", name
);
952 if (httpGet(http
, uri
))
955 while ((get_status
= httpUpdate(http
)) == HTTP_CONTINUE
);
957 if (get_status
!= HTTP_OK
)
961 fprintf(stderr
, "ERROR: Unable to get PPD file %s: %d - %s\n",
962 uri
, get_status
, httpStatus(get_status
));
964 else if ((fd
= cupsTempFd(filename
, sizeof(filename
))) >= 0)
966 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
967 write(fd
, buffer
, (size_t)bytes
);
971 if ((ppd
= ppdOpenFile(filename
)) != NULL
)
973 if (ppd
->manufacturer
)
974 cgiSetVariable("CURRENT_MAKE", ppd
->manufacturer
);
977 cgiSetVariable("CURRENT_MAKE_AND_MODEL", ppd
->nickname
);
984 int linenum
; /* Line number */
986 fprintf(stderr
, "ERROR: Unable to open PPD file %s: %s\n",
987 filename
, ppdErrorString(ppdLastError(&linenum
)));
995 "ERROR: Unable to create temporary file for PPD file: %s\n",
1001 * Build a CUPS_GET_PPDS request, which requires the following
1004 * attributes-charset
1005 * attributes-natural-language
1009 request
= ippNewRequest(CUPS_GET_PPDS
);
1011 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1012 NULL
, "ipp://localhost/printers/");
1014 if ((var
= cgiGetVariable("PPD_MAKE")) == NULL
)
1015 var
= cgiGetVariable("CURRENT_MAKE");
1016 if (var
&& !cgiGetVariable("SELECT_MAKE"))
1018 const char *make_model
; /* Make and model */
1021 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
1022 "ppd-make", NULL
, var
);
1024 if ((make_model
= cgiGetVariable("CURRENT_MAKE_AND_MODEL")) != NULL
)
1025 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
1026 "ppd-make-and-model", NULL
, make_model
);
1029 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1030 "requested-attributes", NULL
, "ppd-make");
1033 * Do the request and get back a response...
1036 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1039 * Got the list of PPDs, see if the user has selected a make...
1042 if (cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0) == 0 && !modify
)
1045 * No PPD files with this make, try again with all makes...
1048 ippDelete(response
);
1050 request
= ippNewRequest(CUPS_GET_PPDS
);
1052 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1053 NULL
, "ipp://localhost/printers/");
1055 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1056 "requested-attributes", NULL
, "ppd-make");
1058 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1059 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
1061 cgiStartHTML(title
);
1062 cgiCopyTemplateLang("choose-make.tmpl");
1065 else if (!var
|| cgiGetVariable("SELECT_MAKE"))
1067 cgiStartHTML(title
);
1068 cgiCopyTemplateLang("choose-make.tmpl");
1074 * Let the user choose a model...
1077 cgiStartHTML(title
);
1078 if (!cgiGetVariable("PPD_MAKE"))
1079 cgiSetVariable("PPD_MAKE", cgiGetVariable("CURRENT_MAKE"));
1081 cgiSetVariable("CURRENT_MAKE_AND_MODEL",
1082 cgiGetArray("PPD_MAKE_AND_MODEL", 0));
1083 cgiCopyTemplateLang("choose-model.tmpl");
1087 ippDelete(response
);
1091 cgiStartHTML(title
);
1092 cgiShowIPPError(_("Unable to get list of printer drivers"));
1093 cgiCopyTemplateLang("error.tmpl");
1100 * Build a CUPS_ADD_PRINTER request, which requires the following
1103 * attributes-charset
1104 * attributes-natural-language
1110 * printer-is-accepting-jobs
1115 request
= ippNewRequest(CUPS_ADD_PRINTER
);
1117 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1118 "localhost", 0, "/printers/%s",
1119 cgiGetVariable("PRINTER_NAME"));
1120 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1125 var
= cgiGetVariable("PPD_NAME");
1126 if (!strcmp(var
, "everywhere"))
1127 get_printer_ppd(cgiGetVariable("DEVICE_URI"), evefile
, sizeof(evefile
));
1128 else if (strcmp(var
, "__no_change__"))
1129 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "ppd-name",
1133 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
1134 NULL
, cgiGetVariable("PRINTER_LOCATION"));
1136 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
1137 NULL
, cgiGetVariable("PRINTER_INFO"));
1139 strlcpy(uri
, cgiGetVariable("DEVICE_URI"), sizeof(uri
));
1142 * Strip make and model from URI...
1145 if ((uriptr
= strrchr(uri
, '|')) != NULL
)
1148 if (!strncmp(uri
, "serial:", 7))
1151 * Update serial port URI to include baud rate, etc.
1154 if ((uriptr
= strchr(uri
, '?')) == NULL
)
1155 uriptr
= uri
+ strlen(uri
);
1157 snprintf(uriptr
, sizeof(uri
) - (size_t)(uriptr
- uri
),
1158 "?baud=%s+bits=%s+parity=%s+flow=%s",
1159 cgiGetVariable("BAUDRATE"), cgiGetVariable("BITS"),
1160 cgiGetVariable("PARITY"), cgiGetVariable("FLOW"));
1163 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri",
1166 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1168 var
= cgiGetVariable("printer_is_shared");
1169 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-shared",
1170 var
&& (!strcmp(var
, "1") || !strcmp(var
, "on")));
1172 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1176 * Do the request and get back a response...
1180 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", file
->tempfile
));
1181 else if (evefile
[0])
1183 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", evefile
));
1187 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1189 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1191 puts("Status: 401\n");
1194 else if (cupsLastError() > IPP_OK_CONFLICT
)
1196 cgiStartHTML(title
);
1197 cgiShowIPPError(modify
? _("Unable to modify printer") :
1198 _("Unable to add printer"));
1203 * Redirect successful updates back to the printer page...
1206 char refresh
[1024]; /* Refresh URL */
1209 cgiFormEncode(uri
, name
, sizeof(uri
));
1211 snprintf(refresh
, sizeof(refresh
),
1212 "5;/admin/?OP=redirect&URL=/printers/%s", uri
);
1214 cgiSetVariable("refresh_page", refresh
);
1216 cgiStartHTML(title
);
1218 cgiCopyTemplateLang("printer-modified.tmpl");
1223 * Set the printer options...
1226 cgiSetVariable("OP", "set-printer-options");
1227 do_set_options(http
, 0);
1240 * 'do_config_server()' - Configure server settings.
1244 do_config_server(http_t
*http
) /* I - HTTP connection */
1246 if (cgiGetVariable("CHANGESETTINGS"))
1249 * Save basic setting changes...
1252 int num_settings
; /* Number of server settings */
1253 cups_option_t
*settings
; /* Server settings */
1254 int advanced
, /* Advanced settings shown? */
1255 changed
; /* Have settings changed? */
1256 const char *debug_logging
, /* DEBUG_LOGGING value */
1257 *preserve_jobs
= NULL
,
1258 /* PRESERVE_JOBS value */
1259 *remote_admin
, /* REMOTE_ADMIN value */
1260 *remote_any
, /* REMOTE_ANY value */
1261 *share_printers
,/* SHARE_PRINTERS value */
1263 /* USER_CANCEL_ANY value */
1264 *browse_web_if
= NULL
,
1265 /* BrowseWebIF value */
1266 *preserve_job_history
= NULL
,
1267 /* PreserveJobHistory value */
1268 *preserve_job_files
= NULL
,
1269 /* PreserveJobFiles value */
1270 *max_clients
= NULL
,
1271 /* MaxClients value */
1274 *max_log_size
= NULL
;
1275 /* MaxLogSize value */
1276 const char *current_browse_web_if
,
1277 /* BrowseWebIF value */
1278 *current_preserve_job_history
,
1279 /* PreserveJobHistory value */
1280 *current_preserve_job_files
,
1281 /* PreserveJobFiles value */
1282 *current_max_clients
,
1283 /* MaxClients value */
1286 *current_max_log_size
;
1287 /* MaxLogSize value */
1289 char default_auth_type
[255];
1290 /* DefaultAuthType value */
1291 const char *val
; /* Setting value */
1292 #endif /* HAVE_GSSAPI */
1296 * Get the checkbox values from the form...
1299 debug_logging
= cgiGetVariable("DEBUG_LOGGING") ? "1" : "0";
1300 remote_admin
= cgiGetVariable("REMOTE_ADMIN") ? "1" : "0";
1301 remote_any
= cgiGetVariable("REMOTE_ANY") ? "1" : "0";
1302 share_printers
= cgiGetVariable("SHARE_PRINTERS") ? "1" : "0";
1303 user_cancel_any
= cgiGetVariable("USER_CANCEL_ANY") ? "1" : "0";
1305 advanced
= cgiGetVariable("ADVANCEDSETTINGS") != NULL
;
1309 * Get advanced settings...
1312 browse_web_if
= cgiGetVariable("BROWSE_WEB_IF") ? "Yes" : "No";
1313 max_clients
= cgiGetVariable("MAX_CLIENTS");
1314 max_log_size
= cgiGetVariable("MAX_LOG_SIZE");
1315 preserve_jobs
= cgiGetVariable("PRESERVE_JOBS");
1319 max_jobs
= cgiGetVariable("MAX_JOBS");
1320 preserve_job_history
= cgiGetVariable("PRESERVE_JOB_HISTORY");
1321 preserve_job_files
= cgiGetVariable("PRESERVE_JOB_FILES");
1323 if (!max_jobs
|| atoi(max_jobs
) < 0)
1326 if (!preserve_job_history
)
1327 preserve_job_history
= "On";
1329 if (!preserve_job_files
)
1330 preserve_job_files
= "1d";
1335 preserve_job_history
= "No";
1336 preserve_job_files
= "No";
1339 if (!max_clients
|| atoi(max_clients
) <= 0)
1340 max_clients
= "100";
1342 if (!max_log_size
|| atoi(max_log_size
) <= 0.0)
1343 max_log_size
= "1m";
1347 * Get the current server settings...
1350 if (!cupsAdminGetServerSettings(http
, &num_settings
, &settings
))
1352 cgiStartHTML(cgiText(_("Change Settings")));
1353 cgiSetVariable("MESSAGE",
1354 cgiText(_("Unable to change server settings")));
1355 cgiSetVariable("ERROR", cupsLastErrorString());
1356 cgiCopyTemplateLang("error.tmpl");
1363 * Get authentication settings...
1366 if (cgiGetVariable("KERBEROS"))
1367 strlcpy(default_auth_type
, "Negotiate", sizeof(default_auth_type
));
1370 val
= cupsGetOption("DefaultAuthType", num_settings
, settings
);
1372 if (!val
|| !_cups_strcasecmp(val
, "Negotiate"))
1373 strlcpy(default_auth_type
, "Basic", sizeof(default_auth_type
));
1375 strlcpy(default_auth_type
, val
, sizeof(default_auth_type
));
1378 fprintf(stderr
, "DEBUG: DefaultAuthType %s\n", default_auth_type
);
1379 #endif /* HAVE_GSSAPI */
1381 if ((current_browse_web_if
= cupsGetOption("BrowseWebIF", num_settings
,
1383 current_browse_web_if
= "No";
1385 if ((current_preserve_job_history
= cupsGetOption("PreserveJobHistory",
1388 current_preserve_job_history
= "Yes";
1390 if ((current_preserve_job_files
= cupsGetOption("PreserveJobFiles",
1393 current_preserve_job_files
= "1d";
1395 if ((current_max_clients
= cupsGetOption("MaxClients", num_settings
,
1397 current_max_clients
= "100";
1399 if ((current_max_jobs
= cupsGetOption("MaxJobs", num_settings
,
1401 current_max_jobs
= "500";
1403 if ((current_max_log_size
= cupsGetOption("MaxLogSize", num_settings
,
1405 current_max_log_size
= "1m";
1408 * See if the settings have changed...
1411 changed
= strcmp(debug_logging
, cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
,
1412 num_settings
, settings
)) ||
1413 strcmp(remote_admin
, cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
,
1414 num_settings
, settings
)) ||
1415 strcmp(remote_any
, cupsGetOption(CUPS_SERVER_REMOTE_ANY
,
1416 num_settings
, settings
)) ||
1417 strcmp(share_printers
, cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
,
1418 num_settings
, settings
)) ||
1420 !cupsGetOption("DefaultAuthType", num_settings
, settings
) ||
1421 strcmp(default_auth_type
, cupsGetOption("DefaultAuthType",
1422 num_settings
, settings
)) ||
1423 #endif /* HAVE_GSSAPI */
1424 strcmp(user_cancel_any
, cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
,
1425 num_settings
, settings
));
1427 if (advanced
&& !changed
)
1428 changed
= _cups_strcasecmp(browse_web_if
, current_browse_web_if
) ||
1429 _cups_strcasecmp(preserve_job_history
, current_preserve_job_history
) ||
1430 _cups_strcasecmp(preserve_job_files
, current_preserve_job_files
) ||
1431 _cups_strcasecmp(max_clients
, current_max_clients
) ||
1432 _cups_strcasecmp(max_jobs
, current_max_jobs
) ||
1433 _cups_strcasecmp(max_log_size
, current_max_log_size
);
1438 * Settings *have* changed, so save the changes...
1441 cupsFreeOptions(num_settings
, settings
);
1444 num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1445 debug_logging
, num_settings
, &settings
);
1446 num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1447 remote_admin
, num_settings
, &settings
);
1448 num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1449 remote_any
, num_settings
, &settings
);
1450 num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1451 share_printers
, num_settings
, &settings
);
1452 num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1453 user_cancel_any
, num_settings
, &settings
);
1455 num_settings
= cupsAddOption("DefaultAuthType", default_auth_type
,
1456 num_settings
, &settings
);
1457 #endif /* HAVE_GSSAPI */
1462 * Add advanced settings...
1465 if (_cups_strcasecmp(browse_web_if
, current_browse_web_if
))
1466 num_settings
= cupsAddOption("BrowseWebIF", browse_web_if
,
1467 num_settings
, &settings
);
1468 if (_cups_strcasecmp(preserve_job_history
, current_preserve_job_history
))
1469 num_settings
= cupsAddOption("PreserveJobHistory",
1470 preserve_job_history
, num_settings
,
1472 if (_cups_strcasecmp(preserve_job_files
, current_preserve_job_files
))
1473 num_settings
= cupsAddOption("PreserveJobFiles", preserve_job_files
,
1474 num_settings
, &settings
);
1475 if (_cups_strcasecmp(max_clients
, current_max_clients
))
1476 num_settings
= cupsAddOption("MaxClients", max_clients
, num_settings
,
1478 if (_cups_strcasecmp(max_jobs
, current_max_jobs
))
1479 num_settings
= cupsAddOption("MaxJobs", max_jobs
, num_settings
,
1481 if (_cups_strcasecmp(max_log_size
, current_max_log_size
))
1482 num_settings
= cupsAddOption("MaxLogSize", max_log_size
, num_settings
,
1486 if (!cupsAdminSetServerSettings(http
, num_settings
, settings
))
1488 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1490 puts("Status: 401\n");
1494 cgiStartHTML(cgiText(_("Change Settings")));
1495 cgiSetVariable("MESSAGE",
1496 cgiText(_("Unable to change server settings")));
1497 cgiSetVariable("ERROR", cupsLastErrorString());
1498 cgiCopyTemplateLang("error.tmpl");
1503 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&"
1504 "URL=/admin/?ADVANCEDSETTINGS=YES");
1506 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
1507 cgiStartHTML(cgiText(_("Change Settings")));
1508 cgiCopyTemplateLang("restart.tmpl");
1517 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
1518 cgiStartHTML(cgiText(_("Change Settings")));
1519 cgiCopyTemplateLang("norestart.tmpl");
1522 cupsFreeOptions(num_settings
, settings
);
1526 else if (cgiGetVariable("SAVECHANGES") && cgiGetVariable("CUPSDCONF"))
1529 * Save hand-edited config file...
1532 http_status_t status
; /* PUT status */
1533 char tempfile
[1024]; /* Temporary new cupsd.conf */
1534 int tempfd
; /* Temporary file descriptor */
1535 cups_file_t
*temp
; /* Temporary file */
1536 const char *start
, /* Start of line */
1537 *end
; /* End of line */
1541 * Create a temporary file for the new cupsd.conf file...
1544 if ((tempfd
= cupsTempFd(tempfile
, sizeof(tempfile
))) < 0)
1546 cgiStartHTML(cgiText(_("Edit Configuration File")));
1547 cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file")));
1548 cgiSetVariable("ERROR", strerror(errno
));
1549 cgiCopyTemplateLang("error.tmpl");
1556 if ((temp
= cupsFileOpenFd(tempfd
, "w")) == NULL
)
1558 cgiStartHTML(cgiText(_("Edit Configuration File")));
1559 cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file")));
1560 cgiSetVariable("ERROR", strerror(errno
));
1561 cgiCopyTemplateLang("error.tmpl");
1571 * Copy the cupsd.conf text from the form variable...
1574 start
= cgiGetVariable("CUPSDCONF");
1577 if ((end
= strstr(start
, "\r\n")) == NULL
)
1578 if ((end
= strstr(start
, "\n")) == NULL
)
1579 end
= start
+ strlen(start
);
1581 cupsFileWrite(temp
, start
, (size_t)(end
- start
));
1582 cupsFilePutChar(temp
, '\n');
1586 else if (*end
== '\n')
1592 cupsFileClose(temp
);
1595 * Upload the configuration file to the server...
1598 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1600 if (status
== HTTP_UNAUTHORIZED
)
1602 puts("Status: 401\n");
1606 else if (status
!= HTTP_CREATED
)
1608 cgiSetVariable("MESSAGE",
1609 cgiText(_("Unable to upload cupsd.conf file")));
1610 cgiSetVariable("ERROR", httpStatus(status
));
1612 cgiStartHTML(cgiText(_("Edit Configuration File")));
1613 cgiCopyTemplateLang("error.tmpl");
1617 cgiSetVariable("refresh_page", "5;URL=/admin/");
1619 cgiStartHTML(cgiText(_("Edit Configuration File")));
1620 cgiCopyTemplateLang("restart.tmpl");
1629 struct stat info
; /* cupsd.conf information */
1630 cups_file_t
*cupsd
; /* cupsd.conf file */
1631 char *buffer
, /* Buffer for entire file */
1632 *bufptr
, /* Pointer into buffer */
1633 *bufend
; /* End of buffer */
1634 int ch
; /* Character from file */
1635 char filename
[1024]; /* Filename */
1636 const char *server_root
; /* Location of config files */
1640 * Locate the cupsd.conf file...
1643 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
1644 server_root
= CUPS_SERVERROOT
;
1646 snprintf(filename
, sizeof(filename
), "%s/cupsd.conf", server_root
);
1649 * Figure out the size...
1652 if (stat(filename
, &info
))
1654 cgiStartHTML(cgiText(_("Edit Configuration File")));
1655 cgiSetVariable("MESSAGE",
1656 cgiText(_("Unable to access cupsd.conf file")));
1657 cgiSetVariable("ERROR", strerror(errno
));
1658 cgiCopyTemplateLang("error.tmpl");
1665 if (info
.st_size
> (1024 * 1024))
1667 cgiStartHTML(cgiText(_("Edit Configuration File")));
1668 cgiSetVariable("MESSAGE",
1669 cgiText(_("Unable to access cupsd.conf file")));
1670 cgiSetVariable("ERROR",
1671 cgiText(_("Unable to edit cupsd.conf files larger than "
1673 cgiCopyTemplateLang("error.tmpl");
1676 fprintf(stderr
, "ERROR: \"%s\" too large (%ld) to edit!\n", filename
,
1677 (long)info
.st_size
);
1682 * Open the cupsd.conf file...
1685 if ((cupsd
= cupsFileOpen(filename
, "r")) == NULL
)
1688 * Unable to open - log an error...
1691 cgiStartHTML(cgiText(_("Edit Configuration File")));
1692 cgiSetVariable("MESSAGE",
1693 cgiText(_("Unable to access cupsd.conf file")));
1694 cgiSetVariable("ERROR", strerror(errno
));
1695 cgiCopyTemplateLang("error.tmpl");
1703 * Allocate memory and load the file into a string buffer...
1706 if ((buffer
= calloc(1, (size_t)info
.st_size
+ 1)) != NULL
)
1708 cupsFileRead(cupsd
, buffer
, (size_t)info
.st_size
);
1709 cgiSetVariable("CUPSDCONF", buffer
);
1713 cupsFileClose(cupsd
);
1716 * Then get the default cupsd.conf file and put that into a string as
1720 strlcat(filename
, ".default", sizeof(filename
));
1722 if (!stat(filename
, &info
) && info
.st_size
< (1024 * 1024) &&
1723 (cupsd
= cupsFileOpen(filename
, "r")) != NULL
)
1725 if ((buffer
= calloc(1, 2 * (size_t)info
.st_size
+ 1)) != NULL
)
1727 bufend
= buffer
+ 2 * info
.st_size
- 1;
1729 for (bufptr
= buffer
;
1730 bufptr
< bufend
&& (ch
= cupsFileGetChar(cupsd
)) != EOF
;)
1732 if (ch
== '\\' || ch
== '\"')
1735 *bufptr
++ = (char)ch
;
1737 else if (ch
== '\n')
1742 else if (ch
== '\t')
1748 *bufptr
++ = (char)ch
;
1753 cgiSetVariable("CUPSDCONF_DEFAULT", buffer
);
1757 cupsFileClose(cupsd
);
1761 * Show the current config file...
1764 cgiStartHTML(cgiText(_("Edit Configuration File")));
1766 cgiCopyTemplateLang("edit-config.tmpl");
1774 * 'do_delete_class()' - Delete a class.
1778 do_delete_class(http_t
*http
) /* I - HTTP connection */
1780 ipp_t
*request
; /* IPP request */
1781 char uri
[HTTP_MAX_URI
]; /* Job URI */
1782 const char *pclass
; /* Printer class name */
1786 * Get form variables...
1789 if (cgiGetVariable("CONFIRM") == NULL
)
1791 cgiStartHTML(cgiText(_("Delete Class")));
1792 cgiCopyTemplateLang("class-confirm.tmpl");
1797 if ((pclass
= cgiGetVariable("PRINTER_NAME")) != NULL
)
1798 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1799 "localhost", 0, "/classes/%s", pclass
);
1802 cgiStartHTML(cgiText(_("Delete Class")));
1803 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
1804 cgiCopyTemplateLang("error.tmpl");
1810 * Build a CUPS_DELETE_CLASS request, which requires the following
1813 * attributes-charset
1814 * attributes-natural-language
1818 request
= ippNewRequest(CUPS_DELETE_CLASS
);
1820 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1824 * Do the request and get back a response...
1827 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1830 * Show the results...
1833 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1835 puts("Status: 401\n");
1838 else if (cupsLastError() <= IPP_OK_CONFLICT
)
1841 * Redirect successful updates back to the classes page...
1844 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/classes");
1847 cgiStartHTML(cgiText(_("Delete Class")));
1849 if (cupsLastError() > IPP_OK_CONFLICT
)
1850 cgiShowIPPError(_("Unable to delete class"));
1852 cgiCopyTemplateLang("class-deleted.tmpl");
1859 * 'do_delete_printer()' - Delete a printer.
1863 do_delete_printer(http_t
*http
) /* I - HTTP connection */
1865 ipp_t
*request
; /* IPP request */
1866 char uri
[HTTP_MAX_URI
]; /* Job URI */
1867 const char *printer
; /* Printer printer name */
1871 * Get form variables...
1874 if (cgiGetVariable("CONFIRM") == NULL
)
1876 cgiStartHTML(cgiText(_("Delete Printer")));
1877 cgiCopyTemplateLang("printer-confirm.tmpl");
1882 if ((printer
= cgiGetVariable("PRINTER_NAME")) != NULL
)
1883 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1884 "localhost", 0, "/printers/%s", printer
);
1887 cgiStartHTML(cgiText(_("Delete Printer")));
1888 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
1889 cgiCopyTemplateLang("error.tmpl");
1895 * Build a CUPS_DELETE_PRINTER request, which requires the following
1898 * attributes-charset
1899 * attributes-natural-language
1903 request
= ippNewRequest(CUPS_DELETE_PRINTER
);
1905 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1909 * Do the request and get back a response...
1912 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1915 * Show the results...
1918 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1920 puts("Status: 401\n");
1923 else if (cupsLastError() <= IPP_OK_CONFLICT
)
1926 * Redirect successful updates back to the printers page...
1929 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/printers");
1932 cgiStartHTML(cgiText(_("Delete Printer")));
1934 if (cupsLastError() > IPP_OK_CONFLICT
)
1935 cgiShowIPPError(_("Unable to delete printer"));
1937 cgiCopyTemplateLang("printer-deleted.tmpl");
1944 * 'do_export()' - Export printers to Samba.
1948 do_export(http_t
*http
) /* I - HTTP connection */
1950 int i
, j
; /* Looping vars */
1951 ipp_t
*request
, /* IPP request */
1952 *response
; /* IPP response */
1953 const char *username
, /* Samba username */
1954 *password
, /* Samba password */
1955 *export_all
; /* Export all printers? */
1956 int export_count
, /* Number of printers to export */
1957 printer_count
; /* Number of available printers */
1958 const char *name
, /* What name to pull */
1959 *dest
; /* Current destination */
1960 char ppd
[1024]; /* PPD file */
1967 username
= cgiGetVariable("USERNAME");
1968 password
= cgiGetVariable("PASSWORD");
1969 export_all
= cgiGetVariable("EXPORT_ALL");
1970 export_count
= cgiGetSize("EXPORT_NAME");
1973 * Get list of available printers...
1976 cgiSetSize("PRINTER_NAME", 0);
1977 cgiSetSize("PRINTER_EXPORT", 0);
1979 request
= ippNewRequest(CUPS_GET_PRINTERS
);
1981 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
1984 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
1985 "printer-type-mask", CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
1987 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1988 "requested-attributes", NULL
, "printer-name");
1990 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1992 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
1993 ippDelete(response
);
1997 printer_count
= cgiGetSize("PRINTER_NAME");
1999 for (i
= 0; i
< printer_count
; i
++)
2001 dest
= cgiGetArray("PRINTER_NAME", i
);
2003 for (j
= 0; j
< export_count
; j
++)
2004 if (!_cups_strcasecmp(dest
, cgiGetArray("EXPORT_NAME", j
)))
2007 cgiSetArray("PRINTER_EXPORT", i
, j
< export_count
? "Y" : "");
2013 * Export or get the printers to export...
2016 if (username
&& *username
&& password
&& *password
&&
2017 (export_all
|| export_count
> 0))
2023 fputs("DEBUG: Export printers...\n", stderr
);
2027 name
= "PRINTER_NAME";
2028 export_count
= cgiGetSize("PRINTER_NAME");
2031 name
= "EXPORT_NAME";
2033 for (i
= 0; i
< export_count
; i
++)
2035 dest
= cgiGetArray(name
, i
);
2037 if (!cupsAdminCreateWindowsPPD(http
, dest
, ppd
, sizeof(ppd
)))
2040 j
= cupsAdminExportSamba(dest
, ppd
, "localhost", username
, password
,
2049 if (i
< export_count
)
2050 cgiSetVariable("ERROR", cupsLastErrorString());
2053 cgiStartHTML(cgiText(_("Export Printers to Samba")));
2054 cgiCopyTemplateLang("samba-exported.tmpl");
2059 else if (username
&& !*username
)
2060 cgiSetVariable("ERROR",
2061 cgiText(_("A Samba username is required to export "
2062 "printer drivers")));
2063 else if (username
&& (!password
|| !*password
))
2064 cgiSetVariable("ERROR",
2065 cgiText(_("A Samba password is required to export "
2066 "printer drivers")));
2072 cgiStartHTML(cgiText(_("Export Printers to Samba")));
2073 cgiCopyTemplateLang("samba-export.tmpl");
2079 * 'do_list_printers()' - List available printers.
2083 do_list_printers(http_t
*http
) /* I - HTTP connection */
2085 ipp_t
*request
, /* IPP request */
2086 *response
; /* IPP response */
2087 ipp_attribute_t
*attr
; /* IPP attribute */
2090 cgiStartHTML(cgiText(_("List Available Printers")));
2094 * Get the list of printers and their devices...
2097 request
= ippNewRequest(CUPS_GET_PRINTERS
);
2099 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2100 "requested-attributes", NULL
, "device-uri");
2102 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type",
2103 CUPS_PRINTER_LOCAL
);
2104 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask",
2105 CUPS_PRINTER_LOCAL
);
2107 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2110 * Got the printer list, now load the devices...
2113 int i
; /* Looping var */
2114 cups_array_t
*printer_devices
; /* Printer devices for local printers */
2115 char *printer_device
; /* Current printer device */
2119 * Allocate an array and copy the device strings...
2122 printer_devices
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2124 for (attr
= ippFindAttribute(response
, "device-uri", IPP_TAG_URI
);
2126 attr
= ippFindNextAttribute(response
, "device-uri", IPP_TAG_URI
))
2128 cupsArrayAdd(printer_devices
, _cupsStrAlloc(attr
->values
[0].string
.text
));
2132 * Free the printer list and get the device list...
2135 ippDelete(response
);
2137 request
= ippNewRequest(CUPS_GET_DEVICES
);
2139 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2142 * Got the device list, let's parse it...
2145 const char *device_uri
, /* device-uri attribute value */
2146 *device_make_and_model
, /* device-make-and-model value */
2147 *device_info
; /* device-info value */
2150 for (i
= 0, attr
= response
->attrs
; attr
; attr
= attr
->next
)
2153 * Skip leading attributes until we hit a device...
2156 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
2163 * Pull the needed attributes from this device...
2167 device_make_and_model
= NULL
;
2170 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
2172 if (!strcmp(attr
->name
, "device-info") &&
2173 attr
->value_tag
== IPP_TAG_TEXT
)
2174 device_info
= attr
->values
[0].string
.text
;
2176 if (!strcmp(attr
->name
, "device-make-and-model") &&
2177 attr
->value_tag
== IPP_TAG_TEXT
)
2178 device_make_and_model
= attr
->values
[0].string
.text
;
2180 if (!strcmp(attr
->name
, "device-uri") &&
2181 attr
->value_tag
== IPP_TAG_URI
)
2182 device_uri
= attr
->values
[0].string
.text
;
2188 * See if we have everything needed...
2191 if (device_info
&& device_make_and_model
&& device_uri
&&
2192 _cups_strcasecmp(device_make_and_model
, "unknown") &&
2193 strchr(device_uri
, ':'))
2196 * Yes, now see if there is already a printer for this
2200 if (!cupsArrayFind(printer_devices
, (void *)device_uri
))
2203 * Not found, so it must be a new printer...
2206 char option
[1024], /* Form variables for this device */
2207 *option_ptr
; /* Pointer into string */
2208 const char *ptr
; /* Pointer into device string */
2212 * Format the printer name variable for this device...
2214 * We use the device-info string first, then device-uri,
2215 * and finally device-make-and-model to come up with a
2219 if (_cups_strncasecmp(device_info
, "unknown", 7))
2221 else if ((ptr
= strstr(device_uri
, "://")) != NULL
)
2224 ptr
= device_make_and_model
;
2226 for (option_ptr
= option
;
2227 option_ptr
< (option
+ sizeof(option
) - 1) && *ptr
;
2229 if (isalnum(*ptr
& 255) || *ptr
== '_' || *ptr
== '-' ||
2231 *option_ptr
++ = *ptr
;
2232 else if ((*ptr
== ' ' || *ptr
== '/') && option_ptr
> option
&&
2233 option_ptr
[-1] != '_')
2234 *option_ptr
++ = '_';
2235 else if (*ptr
== '?' || *ptr
== '(')
2240 cgiSetArray("TEMPLATE_NAME", i
, option
);
2243 * Finally, set the form variables for this printer...
2246 cgiSetArray("device_info", i
, device_info
);
2247 cgiSetArray("device_make_and_model", i
, device_make_and_model
);
2248 cgiSetArray("device_uri", i
, device_uri
);
2257 ippDelete(response
);
2260 * Free the device list...
2263 for (printer_device
= (char *)cupsArrayFirst(printer_devices
);
2265 printer_device
= (char *)cupsArrayNext(printer_devices
))
2266 _cupsStrFree(printer_device
);
2268 cupsArrayDelete(printer_devices
);
2273 * Finally, show the printer list...
2276 cgiCopyTemplateLang("list-available-printers.tmpl");
2283 * 'do_menu()' - Show the main menu.
2287 do_menu(http_t
*http
) /* I - HTTP connection */
2289 int num_settings
; /* Number of server settings */
2290 cups_option_t
*settings
; /* Server settings */
2291 const char *val
; /* Setting value */
2292 char filename
[1024]; /* Temporary filename */
2293 const char *datadir
; /* Location of data files */
2294 ipp_t
*request
, /* IPP request */
2295 *response
; /* IPP response */
2299 * Get the current server settings...
2302 if (!cupsAdminGetServerSettings(http
, &num_settings
, &settings
))
2304 cgiSetVariable("SETTINGS_MESSAGE",
2305 cgiText(_("Unable to open cupsd.conf file:")));
2306 cgiSetVariable("SETTINGS_ERROR", cupsLastErrorString());
2309 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
2310 settings
)) != NULL
&& atoi(val
))
2311 cgiSetVariable("DEBUG_LOGGING", "CHECKED");
2313 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
2314 settings
)) != NULL
&& atoi(val
))
2315 cgiSetVariable("REMOTE_ADMIN", "CHECKED");
2317 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
2318 settings
)) != NULL
&& atoi(val
))
2319 cgiSetVariable("REMOTE_ANY", "CHECKED");
2321 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
2322 settings
)) != NULL
&& atoi(val
))
2323 cgiSetVariable("SHARE_PRINTERS", "CHECKED");
2325 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
2326 settings
)) != NULL
&& atoi(val
))
2327 cgiSetVariable("USER_CANCEL_ANY", "CHECKED");
2330 cgiSetVariable("HAVE_GSSAPI", "1");
2332 if ((val
= cupsGetOption("DefaultAuthType", num_settings
,
2333 settings
)) != NULL
&& !_cups_strcasecmp(val
, "Negotiate"))
2334 cgiSetVariable("KERBEROS", "CHECKED");
2336 #endif /* HAVE_GSSAPI */
2337 cgiSetVariable("KERBEROS", "");
2339 if ((val
= cupsGetOption("BrowseWebIF", num_settings
,
2343 if (!_cups_strcasecmp(val
, "yes") || !_cups_strcasecmp(val
, "on") ||
2344 !_cups_strcasecmp(val
, "true"))
2345 cgiSetVariable("BROWSE_WEB_IF", "CHECKED");
2347 if ((val
= cupsGetOption("PreserveJobHistory", num_settings
,
2352 (!_cups_strcasecmp(val
, "0") || !_cups_strcasecmp(val
, "no") ||
2353 !_cups_strcasecmp(val
, "off") || !_cups_strcasecmp(val
, "false") ||
2354 !_cups_strcasecmp(val
, "disabled")))
2356 cgiSetVariable("PRESERVE_JOB_HISTORY", "0");
2357 cgiSetVariable("PRESERVE_JOB_FILES", "0");
2361 cgiSetVariable("PRESERVE_JOBS", "CHECKED");
2362 cgiSetVariable("PRESERVE_JOB_HISTORY", val
);
2364 if ((val
= cupsGetOption("PreserveJobFiles", num_settings
,
2368 cgiSetVariable("PRESERVE_JOB_FILES", val
);
2372 if ((val
= cupsGetOption("MaxClients", num_settings
, settings
)) == NULL
)
2375 cgiSetVariable("MAX_CLIENTS", val
);
2377 if ((val
= cupsGetOption("MaxJobs", num_settings
, settings
)) == NULL
)
2380 cgiSetVariable("MAX_JOBS", val
);
2382 if ((val
= cupsGetOption("MaxLogSize", num_settings
, settings
)) == NULL
)
2385 cgiSetVariable("MAX_LOG_SIZE", val
);
2387 cupsFreeOptions(num_settings
, settings
);
2390 * See if Samba and the Windows drivers are installed...
2393 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
2394 datadir
= CUPS_DATADIR
;
2396 snprintf(filename
, sizeof(filename
), "%s/drivers/pscript5.dll", datadir
);
2397 if (!access(filename
, R_OK
))
2400 * Found Windows 2000 driver file, see if we have smbclient and
2404 if (cupsFileFind("smbclient", getenv("PATH"), 1, filename
,
2405 sizeof(filename
)) &&
2406 cupsFileFind("rpcclient", getenv("PATH"), 1, filename
,
2408 cgiSetVariable("HAVE_SAMBA", "Y");
2411 if (!cupsFileFind("smbclient", getenv("PATH"), 1, filename
,
2413 fputs("ERROR: smbclient not found!\n", stderr
);
2415 if (!cupsFileFind("rpcclient", getenv("PATH"), 1, filename
,
2417 fputs("ERROR: rpcclient not found!\n", stderr
);
2427 request
= ippNewRequest(IPP_GET_SUBSCRIPTIONS
);
2429 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2430 NULL
, "ipp://localhost/");
2432 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2434 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
2435 ippDelete(response
);
2439 * Finally, show the main menu template...
2442 cgiStartHTML(cgiText(_("Administration")));
2444 cgiCopyTemplateLang("admin.tmpl");
2451 * 'do_set_allowed_users()' - Set the allowed/denied users for a queue.
2455 do_set_allowed_users(http_t
*http
) /* I - HTTP connection */
2457 int i
; /* Looping var */
2458 ipp_t
*request
, /* IPP request */
2459 *response
; /* IPP response */
2460 char uri
[HTTP_MAX_URI
]; /* Printer URI */
2461 const char *printer
, /* Printer name (purge-jobs) */
2462 *is_class
, /* Is a class? */
2463 *users
, /* List of users or groups */
2464 *type
; /* Allow/deny type */
2465 int num_users
; /* Number of users */
2466 char *ptr
, /* Pointer into users string */
2467 *end
, /* Pointer to end of users string */
2468 quote
; /* Quote character */
2469 ipp_attribute_t
*attr
; /* Attribute */
2470 static const char * const attrs
[] = /* Requested attributes */
2472 "requesting-user-name-allowed",
2473 "requesting-user-name-denied"
2477 is_class
= cgiGetVariable("IS_CLASS");
2478 printer
= cgiGetVariable("PRINTER_NAME");
2482 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
2483 cgiStartHTML(cgiText(_("Set Allowed Users")));
2484 cgiCopyTemplateLang("error.tmpl");
2489 users
= cgiGetVariable("users");
2490 type
= cgiGetVariable("type");
2492 if (!users
|| !type
||
2493 (strcmp(type
, "requesting-user-name-allowed") &&
2494 strcmp(type
, "requesting-user-name-denied")))
2497 * Build a Get-Printer-Attributes request, which requires the following
2500 * attributes-charset
2501 * attributes-natural-language
2503 * requested-attributes
2506 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
2508 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2509 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2511 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2514 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2515 "requested-attributes",
2516 (int)(sizeof(attrs
) / sizeof(attrs
[0])), NULL
, attrs
);
2519 * Do the request and get back a response...
2522 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2524 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
2526 ippDelete(response
);
2529 cgiStartHTML(cgiText(_("Set Allowed Users")));
2531 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2533 puts("Status: 401\n");
2536 else if (cupsLastError() > IPP_OK_CONFLICT
)
2537 cgiShowIPPError(_("Unable to get printer attributes"));
2539 cgiCopyTemplateLang("users.tmpl");
2546 * Save the changes...
2549 for (num_users
= 0, ptr
= (char *)users
; *ptr
; num_users
++)
2552 * Skip whitespace and commas...
2555 while (*ptr
== ',' || isspace(*ptr
& 255))
2561 if (*ptr
== '\'' || *ptr
== '\"')
2564 * Scan quoted name...
2569 for (end
= ptr
; *end
; end
++)
2576 * Scan space or comma-delimited name...
2579 for (end
= ptr
; *end
; end
++)
2580 if (isspace(*end
& 255) || *end
== ',')
2585 * Advance to the next name...
2592 * Build a CUPS-Add-Printer/Class request, which requires the following
2595 * attributes-charset
2596 * attributes-natural-language
2598 * requesting-user-name-{allowed,denied}
2601 request
= ippNewRequest(is_class
? CUPS_ADD_CLASS
: CUPS_ADD_PRINTER
);
2603 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2604 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2606 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2610 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2611 "requesting-user-name-allowed", NULL
, "all");
2614 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2615 type
, num_users
, NULL
, NULL
);
2617 for (i
= 0, ptr
= (char *)users
; *ptr
; i
++)
2620 * Skip whitespace and commas...
2623 while (*ptr
== ',' || isspace(*ptr
& 255))
2629 if (*ptr
== '\'' || *ptr
== '\"')
2632 * Scan quoted name...
2637 for (end
= ptr
; *end
; end
++)
2644 * Scan space or comma-delimited name...
2647 for (end
= ptr
; *end
; end
++)
2648 if (isspace(*end
& 255) || *end
== ',')
2653 * Terminate the name...
2663 attr
->values
[i
].string
.text
= _cupsStrAlloc(ptr
);
2666 * Advance to the next name...
2674 * Do the request and get back a response...
2677 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
2679 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2681 puts("Status: 401\n");
2684 else if (cupsLastError() > IPP_OK_CONFLICT
)
2686 cgiStartHTML(cgiText(_("Set Allowed Users")));
2687 cgiShowIPPError(_("Unable to change printer"));
2692 * Redirect successful updates back to the printer page...
2695 char url
[1024], /* Printer/class URL */
2696 refresh
[1024]; /* Refresh URL */
2699 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
2700 cgiFormEncode(uri
, url
, sizeof(uri
));
2701 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s",
2703 cgiSetVariable("refresh_page", refresh
);
2705 cgiStartHTML(cgiText(_("Set Allowed Users")));
2707 cgiCopyTemplateLang(is_class
? "class-modified.tmpl" :
2708 "printer-modified.tmpl");
2717 * 'do_set_default()' - Set the server default printer/class.
2721 do_set_default(http_t
*http
) /* I - HTTP connection */
2723 const char *title
; /* Page title */
2724 ipp_t
*request
; /* IPP request */
2725 char uri
[HTTP_MAX_URI
]; /* Printer URI */
2726 const char *printer
, /* Printer name (purge-jobs) */
2727 *is_class
; /* Is a class? */
2730 is_class
= cgiGetVariable("IS_CLASS");
2731 printer
= cgiGetVariable("PRINTER_NAME");
2732 title
= cgiText(_("Set As Server Default"));
2736 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
2737 cgiStartHTML(title
);
2738 cgiCopyTemplateLang("error.tmpl");
2744 * Build a printer request, which requires the following
2747 * attributes-charset
2748 * attributes-natural-language
2752 request
= ippNewRequest(CUPS_SET_DEFAULT
);
2754 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2755 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2757 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2761 * Do the request and get back a response...
2764 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
2766 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2768 puts("Status: 401\n");
2771 else if (cupsLastError() > IPP_OK_CONFLICT
)
2773 cgiStartHTML(title
);
2774 cgiShowIPPError(_("Unable to set server default"));
2779 * Redirect successful updates back to the printer page...
2782 char url
[1024], /* Printer/class URL */
2783 refresh
[1024]; /* Refresh URL */
2786 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
2787 cgiFormEncode(uri
, url
, sizeof(uri
));
2788 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s", uri
);
2789 cgiSetVariable("refresh_page", refresh
);
2791 cgiStartHTML(title
);
2792 cgiCopyTemplateLang("printer-default.tmpl");
2800 * 'do_set_options()' - Configure the default options for a queue.
2804 do_set_options(http_t
*http
, /* I - HTTP connection */
2805 int is_class
) /* I - Set options for class? */
2807 int i
, j
, k
, m
; /* Looping vars */
2808 int have_options
; /* Have options? */
2809 ipp_t
*request
, /* IPP request */
2810 *response
; /* IPP response */
2811 ipp_attribute_t
*attr
; /* IPP attribute */
2812 char uri
[HTTP_MAX_URI
]; /* Job URI */
2813 const char *var
; /* Variable value */
2814 const char *printer
; /* Printer printer name */
2815 const char *filename
; /* PPD filename */
2816 char tempfile
[1024]; /* Temporary filename */
2817 cups_file_t
*in
, /* Input file */
2818 *out
; /* Output file */
2819 char line
[1024], /* Line from PPD file */
2820 value
[1024], /* Option value */
2821 keyword
[1024], /* Keyword from Default line */
2822 *keyptr
; /* Pointer into keyword... */
2823 ppd_file_t
*ppd
; /* PPD file */
2824 ppd_group_t
*group
; /* Option group */
2825 ppd_option_t
*option
; /* Option */
2826 ppd_coption_t
*coption
; /* Custom option */
2827 ppd_cparam_t
*cparam
; /* Custom parameter */
2828 ppd_attr_t
*ppdattr
; /* PPD attribute */
2829 const char *title
; /* Page title */
2832 title
= cgiText(is_class
? _("Set Class Options") : _("Set Printer Options"));
2834 fprintf(stderr
, "DEBUG: do_set_options(http=%p, is_class=%d)\n", http
,
2838 * Get the printer name...
2841 if ((printer
= cgiGetVariable("PRINTER_NAME")) != NULL
)
2842 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2843 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2847 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
2848 cgiStartHTML(title
);
2849 cgiCopyTemplateLang("error.tmpl");
2854 fprintf(stderr
, "DEBUG: printer=\"%s\", uri=\"%s\"...\n", printer
, uri
);
2857 * If the user clicks on the Auto-Configure button, send an AutoConfigure
2858 * command file to the printer...
2861 if (cgiGetVariable("AUTOCONFIGURE"))
2863 cgiPrintCommand(http
, printer
, "AutoConfigure", "Set Default Options");
2868 * Get the PPD file...
2874 filename
= cupsGetPPD2(http
, printer
);
2878 fprintf(stderr
, "DEBUG: Got PPD file: \"%s\"\n", filename
);
2880 if ((ppd
= ppdOpenFile(filename
)) == NULL
)
2882 cgiSetVariable("ERROR", ppdErrorString(ppdLastError(&i
)));
2883 cgiSetVariable("MESSAGE", cgiText(_("Unable to open PPD file")));
2884 cgiStartHTML(title
);
2885 cgiCopyTemplateLang("error.tmpl");
2892 fputs("DEBUG: No PPD file\n", stderr
);
2896 if (cgiGetVariable("job_sheets_start") != NULL
||
2897 cgiGetVariable("job_sheets_end") != NULL
)
2904 ppdMarkDefaults(ppd
);
2906 for (option
= ppdFirstOption(ppd
);
2908 option
= ppdNextOption(ppd
))
2910 if ((var
= cgiGetVariable(option
->keyword
)) != NULL
)
2913 ppdMarkOption(ppd
, option
->keyword
, var
);
2914 fprintf(stderr
, "DEBUG: Set %s to %s...\n", option
->keyword
, var
);
2917 fprintf(stderr
, "DEBUG: Didn't find %s...\n", option
->keyword
);
2921 if (!have_options
|| ppdConflicts(ppd
))
2924 * Show the options to the user...
2927 fputs("DEBUG: Showing options...\n", stderr
);
2930 * Show auto-configure button if supported...
2935 if (ppd
->num_filters
== 0 ||
2936 ((ppdattr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) != NULL
&&
2937 ppdattr
->value
&& strstr(ppdattr
->value
, "AutoConfigure")))
2938 cgiSetVariable("HAVE_AUTOCONFIGURE", "YES");
2941 for (i
= 0; i
< ppd
->num_filters
; i
++)
2942 if (!strncmp(ppd
->filters
[i
], "application/vnd.cups-postscript", 31))
2944 cgiSetVariable("HAVE_AUTOCONFIGURE", "YES");
2951 * Get the printer attributes...
2954 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
2956 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2957 "localhost", 0, "/printers/%s", printer
);
2958 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2961 response
= cupsDoRequest(http
, request
, "/");
2964 * List the groups used as "tabs"...
2971 for (group
= ppd
->groups
;
2972 i
< ppd
->num_groups
;
2975 cgiSetArray("GROUP_ID", i
, group
->name
);
2977 if (!strcmp(group
->name
, "InstallableOptions"))
2978 cgiSetArray("GROUP", i
, cgiText(_("Options Installed")));
2980 cgiSetArray("GROUP", i
, group
->text
);
2984 if (ippFindAttribute(response
, "job-sheets-supported", IPP_TAG_ZERO
))
2986 cgiSetArray("GROUP_ID", i
, "CUPS_BANNERS");
2987 cgiSetArray("GROUP", i
++, cgiText(_("Banners")));
2990 if (ippFindAttribute(response
, "printer-error-policy-supported",
2992 ippFindAttribute(response
, "printer-op-policy-supported",
2995 cgiSetArray("GROUP_ID", i
, "CUPS_POLICIES");
2996 cgiSetArray("GROUP", i
++, cgiText(_("Policies")));
2999 if ((attr
= ippFindAttribute(response
, "port-monitor-supported",
3000 IPP_TAG_NAME
)) != NULL
&& attr
->num_values
> 1)
3002 cgiSetArray("GROUP_ID", i
, "CUPS_PORT_MONITOR");
3003 cgiSetArray("GROUP", i
, cgiText(_("Port Monitor")));
3006 cgiStartHTML(cgiText(_("Set Printer Options")));
3007 cgiCopyTemplateLang("set-printer-options-header.tmpl");
3013 if (ppdConflicts(ppd
))
3015 for (i
= ppd
->num_groups
, k
= 0, group
= ppd
->groups
;
3018 for (j
= group
->num_options
, option
= group
->options
;
3021 if (option
->conflicted
)
3023 cgiSetArray("ckeyword", k
, option
->keyword
);
3024 cgiSetArray("ckeytext", k
, option
->text
);
3026 for (m
= 0; m
< option
->num_choices
; m
++)
3028 if (option
->choices
[m
].marked
)
3030 cgiSetArray("cchoice", k
, option
->choices
[m
].text
);
3038 cgiCopyTemplateLang("option-conflict.tmpl");
3041 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
3045 for (j
= group
->num_options
, option
= group
->options
;
3049 if (!strcmp(option
->keyword
, "PageRegion"))
3052 if (option
->num_choices
> 1)
3059 cgiSetVariable("GROUP_ID", group
->name
);
3061 if (!strcmp(group
->name
, "InstallableOptions"))
3062 cgiSetVariable("GROUP", cgiText(_("Options Installed")));
3064 cgiSetVariable("GROUP", group
->text
);
3066 cgiCopyTemplateLang("option-header.tmpl");
3068 for (j
= group
->num_options
, option
= group
->options
;
3072 if (!strcmp(option
->keyword
, "PageRegion") || option
->num_choices
< 2)
3075 cgiSetVariable("KEYWORD", option
->keyword
);
3076 cgiSetVariable("KEYTEXT", option
->text
);
3078 if (option
->conflicted
)
3079 cgiSetVariable("CONFLICTED", "1");
3081 cgiSetVariable("CONFLICTED", "0");
3083 cgiSetSize("CHOICES", 0);
3084 cgiSetSize("TEXT", 0);
3085 for (k
= 0, m
= 0; k
< option
->num_choices
; k
++)
3087 cgiSetArray("CHOICES", m
, option
->choices
[k
].choice
);
3088 cgiSetArray("TEXT", m
, option
->choices
[k
].text
);
3092 if (option
->choices
[k
].marked
)
3093 cgiSetVariable("DEFCHOICE", option
->choices
[k
].choice
);
3096 cgiSetSize("PARAMS", 0);
3097 cgiSetSize("PARAMTEXT", 0);
3098 cgiSetSize("PARAMVALUE", 0);
3099 cgiSetSize("INPUTTYPE", 0);
3101 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)))
3103 const char *units
= NULL
; /* Units value, if any */
3105 cgiSetVariable("ISCUSTOM", "1");
3107 for (cparam
= ppdFirstCustomParam(coption
), m
= 0;
3109 cparam
= ppdNextCustomParam(coption
), m
++)
3111 if (!_cups_strcasecmp(option
->keyword
, "PageSize") &&
3112 _cups_strcasecmp(cparam
->name
, "Width") &&
3113 _cups_strcasecmp(cparam
->name
, "Height"))
3119 cgiSetArray("PARAMS", m
, cparam
->name
);
3120 cgiSetArray("PARAMTEXT", m
, cparam
->text
);
3121 cgiSetArray("INPUTTYPE", m
, "text");
3123 switch (cparam
->type
)
3125 case PPD_CUSTOM_POINTS
:
3126 if (!_cups_strncasecmp(option
->defchoice
, "Custom.", 7))
3128 units
= option
->defchoice
+ strlen(option
->defchoice
) - 2;
3130 if (strcmp(units
, "mm") && strcmp(units
, "cm") &&
3131 strcmp(units
, "in") && strcmp(units
, "ft"))
3133 if (units
[1] == 'm')
3142 if (!strcmp(units
, "mm"))
3143 snprintf(value
, sizeof(value
), "%g",
3144 cparam
->current
.custom_points
/ 72.0 * 25.4);
3145 else if (!strcmp(units
, "cm"))
3146 snprintf(value
, sizeof(value
), "%g",
3147 cparam
->current
.custom_points
/ 72.0 * 2.54);
3148 else if (!strcmp(units
, "in"))
3149 snprintf(value
, sizeof(value
), "%g",
3150 cparam
->current
.custom_points
/ 72.0);
3151 else if (!strcmp(units
, "ft"))
3152 snprintf(value
, sizeof(value
), "%g",
3153 cparam
->current
.custom_points
/ 72.0 / 12.0);
3154 else if (!strcmp(units
, "m"))
3155 snprintf(value
, sizeof(value
), "%g",
3156 cparam
->current
.custom_points
/ 72.0 * 0.0254);
3158 snprintf(value
, sizeof(value
), "%g",
3159 cparam
->current
.custom_points
);
3160 cgiSetArray("PARAMVALUE", m
, value
);
3163 case PPD_CUSTOM_CURVE
:
3164 case PPD_CUSTOM_INVCURVE
:
3165 case PPD_CUSTOM_REAL
:
3166 snprintf(value
, sizeof(value
), "%g",
3167 cparam
->current
.custom_real
);
3168 cgiSetArray("PARAMVALUE", m
, value
);
3171 case PPD_CUSTOM_INT
:
3172 snprintf(value
, sizeof(value
), "%d",
3173 cparam
->current
.custom_int
);
3174 cgiSetArray("PARAMVALUE", m
, value
);
3177 case PPD_CUSTOM_PASSCODE
:
3178 case PPD_CUSTOM_PASSWORD
:
3179 if (cparam
->current
.custom_password
)
3180 cgiSetArray("PARAMVALUE", m
,
3181 cparam
->current
.custom_password
);
3183 cgiSetArray("PARAMVALUE", m
, "");
3184 cgiSetArray("INPUTTYPE", m
, "password");
3187 case PPD_CUSTOM_STRING
:
3188 if (cparam
->current
.custom_string
)
3189 cgiSetArray("PARAMVALUE", m
,
3190 cparam
->current
.custom_string
);
3192 cgiSetArray("PARAMVALUE", m
, "");
3199 cgiSetArray("PARAMS", m
, "Units");
3200 cgiSetArray("PARAMTEXT", m
, cgiText(_("Units")));
3201 cgiSetArray("PARAMVALUE", m
, units
);
3205 cgiSetVariable("ISCUSTOM", "0");
3209 case PPD_UI_BOOLEAN
:
3210 cgiCopyTemplateLang("option-boolean.tmpl");
3212 case PPD_UI_PICKONE
:
3213 cgiCopyTemplateLang("option-pickone.tmpl");
3215 case PPD_UI_PICKMANY
:
3216 cgiCopyTemplateLang("option-pickmany.tmpl");
3221 cgiCopyTemplateLang("option-trailer.tmpl");
3225 if ((attr
= ippFindAttribute(response
, "job-sheets-supported",
3226 IPP_TAG_ZERO
)) != NULL
)
3229 * Add the job sheets options...
3232 cgiSetVariable("GROUP_ID", "CUPS_BANNERS");
3233 cgiSetVariable("GROUP", cgiText(_("Banners")));
3234 cgiCopyTemplateLang("option-header.tmpl");
3236 cgiSetSize("CHOICES", attr
->num_values
);
3237 cgiSetSize("TEXT", attr
->num_values
);
3238 for (k
= 0; k
< attr
->num_values
; k
++)
3240 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3241 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3244 attr
= ippFindAttribute(response
, "job-sheets-default", IPP_TAG_ZERO
);
3246 cgiSetVariable("KEYWORD", "job_sheets_start");
3247 cgiSetVariable("KEYTEXT",
3248 /* TRANSLATORS: Banner/cover sheet before the print job. */
3249 cgiText(_("Starting Banner")));
3250 cgiSetVariable("DEFCHOICE", attr
!= NULL
?
3251 attr
->values
[0].string
.text
: "");
3253 cgiCopyTemplateLang("option-pickone.tmpl");
3255 cgiSetVariable("KEYWORD", "job_sheets_end");
3256 cgiSetVariable("KEYTEXT",
3257 /* TRANSLATORS: Banner/cover sheet after the print job. */
3258 cgiText(_("Ending Banner")));
3259 cgiSetVariable("DEFCHOICE", attr
!= NULL
&& attr
->num_values
> 1 ?
3260 attr
->values
[1].string
.text
: "");
3262 cgiCopyTemplateLang("option-pickone.tmpl");
3264 cgiCopyTemplateLang("option-trailer.tmpl");
3267 if (ippFindAttribute(response
, "printer-error-policy-supported",
3269 ippFindAttribute(response
, "printer-op-policy-supported",
3273 * Add the error and operation policy options...
3276 cgiSetVariable("GROUP_ID", "CUPS_POLICIES");
3277 cgiSetVariable("GROUP", cgiText(_("Policies")));
3278 cgiCopyTemplateLang("option-header.tmpl");
3284 attr
= ippFindAttribute(response
, "printer-error-policy-supported",
3289 cgiSetSize("CHOICES", attr
->num_values
);
3290 cgiSetSize("TEXT", attr
->num_values
);
3291 for (k
= 0; k
< attr
->num_values
; k
++)
3293 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3294 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3297 attr
= ippFindAttribute(response
, "printer-error-policy",
3300 cgiSetVariable("KEYWORD", "printer_error_policy");
3301 cgiSetVariable("KEYTEXT", cgiText(_("Error Policy")));
3302 cgiSetVariable("DEFCHOICE", attr
== NULL
?
3303 "" : attr
->values
[0].string
.text
);
3306 cgiCopyTemplateLang("option-pickone.tmpl");
3309 * Operation policy...
3312 attr
= ippFindAttribute(response
, "printer-op-policy-supported",
3317 cgiSetSize("CHOICES", attr
->num_values
);
3318 cgiSetSize("TEXT", attr
->num_values
);
3319 for (k
= 0; k
< attr
->num_values
; k
++)
3321 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3322 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3325 attr
= ippFindAttribute(response
, "printer-op-policy", IPP_TAG_ZERO
);
3327 cgiSetVariable("KEYWORD", "printer_op_policy");
3328 cgiSetVariable("KEYTEXT", cgiText(_("Operation Policy")));
3329 cgiSetVariable("DEFCHOICE", attr
== NULL
?
3330 "" : attr
->values
[0].string
.text
);
3332 cgiCopyTemplateLang("option-pickone.tmpl");
3335 cgiCopyTemplateLang("option-trailer.tmpl");
3339 * Binary protocol support...
3342 if ((attr
= ippFindAttribute(response
, "port-monitor-supported",
3343 IPP_TAG_NAME
)) != NULL
&& attr
->num_values
> 1)
3345 cgiSetVariable("GROUP_ID", "CUPS_PORT_MONITOR");
3346 cgiSetVariable("GROUP", cgiText(_("Port Monitor")));
3348 cgiSetSize("CHOICES", attr
->num_values
);
3349 cgiSetSize("TEXT", attr
->num_values
);
3351 for (i
= 0; i
< attr
->num_values
; i
++)
3353 cgiSetArray("CHOICES", i
, attr
->values
[i
].string
.text
);
3354 cgiSetArray("TEXT", i
, attr
->values
[i
].string
.text
);
3357 attr
= ippFindAttribute(response
, "port-monitor", IPP_TAG_NAME
);
3358 cgiSetVariable("KEYWORD", "port_monitor");
3359 cgiSetVariable("KEYTEXT", cgiText(_("Port Monitor")));
3360 cgiSetVariable("DEFCHOICE", attr
? attr
->values
[0].string
.text
: "none");
3362 cgiCopyTemplateLang("option-header.tmpl");
3363 cgiCopyTemplateLang("option-pickone.tmpl");
3364 cgiCopyTemplateLang("option-trailer.tmpl");
3367 cgiCopyTemplateLang("set-printer-options-trailer.tmpl");
3370 ippDelete(response
);
3375 * Set default options...
3378 fputs("DEBUG: Setting options...\n", stderr
);
3382 out
= cupsTempFile2(tempfile
, sizeof(tempfile
));
3383 in
= cupsFileOpen(filename
, "r");
3387 cgiSetVariable("ERROR", strerror(errno
));
3388 cgiStartHTML(cgiText(_("Set Printer Options")));
3389 cgiCopyTemplateLang("error.tmpl");
3405 while (cupsFileGets(in
, line
, sizeof(line
)))
3407 if (!strncmp(line
, "*cupsProtocol:", 14))
3409 else if (strncmp(line
, "*Default", 8))
3410 cupsFilePrintf(out
, "%s\n", line
);
3414 * Get default option name...
3417 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
3419 for (keyptr
= keyword
; *keyptr
; keyptr
++)
3420 if (*keyptr
== ':' || isspace(*keyptr
& 255))
3425 if (!strcmp(keyword
, "PageRegion") ||
3426 !strcmp(keyword
, "PaperDimension") ||
3427 !strcmp(keyword
, "ImageableArea"))
3428 var
= get_option_value(ppd
, "PageSize", value
, sizeof(value
));
3430 var
= get_option_value(ppd
, keyword
, value
, sizeof(value
));
3433 cupsFilePrintf(out
, "%s\n", line
);
3435 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, var
);
3445 * Make sure temporary filename is cleared when there is no PPD...
3452 * Build a CUPS_ADD_MODIFY_CLASS/PRINTER request, which requires the
3453 * following attributes:
3455 * attributes-charset
3456 * attributes-natural-language
3458 * job-sheets-default
3459 * printer-error-policy
3464 request
= ippNewRequest(is_class
? CUPS_ADD_MODIFY_CLASS
:
3465 CUPS_ADD_MODIFY_PRINTER
);
3467 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3470 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3471 "job-sheets-default", 2, NULL
, NULL
);
3472 attr
->values
[0].string
.text
= _cupsStrAlloc(cgiGetVariable("job_sheets_start"));
3473 attr
->values
[1].string
.text
= _cupsStrAlloc(cgiGetVariable("job_sheets_end"));
3475 if ((var
= cgiGetVariable("printer_error_policy")) != NULL
)
3476 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3477 "printer-error-policy", NULL
, var
);
3479 if ((var
= cgiGetVariable("printer_op_policy")) != NULL
)
3480 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3481 "printer-op-policy", NULL
, var
);
3483 if ((var
= cgiGetVariable("port_monitor")) != NULL
)
3484 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3485 "port-monitor", NULL
, var
);
3488 * Do the request and get back a response...
3492 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", tempfile
));
3494 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
3496 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
3498 puts("Status: 401\n");
3501 else if (cupsLastError() > IPP_OK_CONFLICT
)
3503 cgiStartHTML(title
);
3504 cgiShowIPPError(_("Unable to set options"));
3509 * Redirect successful updates back to the printer page...
3512 char refresh
[1024]; /* Refresh URL */
3515 cgiFormEncode(uri
, printer
, sizeof(uri
));
3516 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=/%s/%s",
3517 is_class
? "classes" : "printers", uri
);
3518 cgiSetVariable("refresh_page", refresh
);
3520 cgiStartHTML(title
);
3522 cgiCopyTemplateLang("printer-configured.tmpl");
3537 * 'do_set_sharing()' - Set printer-is-shared value.
3541 do_set_sharing(http_t
*http
) /* I - HTTP connection */
3543 ipp_t
*request
, /* IPP request */
3544 *response
; /* IPP response */
3545 char uri
[HTTP_MAX_URI
]; /* Printer URI */
3546 const char *printer
, /* Printer name */
3547 *is_class
, /* Is a class? */
3548 *shared
; /* Sharing value */
3551 is_class
= cgiGetVariable("IS_CLASS");
3552 printer
= cgiGetVariable("PRINTER_NAME");
3553 shared
= cgiGetVariable("SHARED");
3555 if (!printer
|| !shared
)
3557 cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
3558 cgiStartHTML(cgiText(_("Set Publishing")));
3559 cgiCopyTemplateLang("error.tmpl");
3565 * Build a CUPS-Add-Printer/CUPS-Add-Class request, which requires the
3566 * following attributes:
3568 * attributes-charset
3569 * attributes-natural-language
3574 request
= ippNewRequest(is_class
? CUPS_ADD_CLASS
: CUPS_ADD_PRINTER
);
3576 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3577 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
3579 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3582 ippAddBoolean(request
, IPP_TAG_OPERATION
, "printer-is-shared", (char)atoi(shared
));
3585 * Do the request and get back a response...
3588 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
)
3590 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
3592 ippDelete(response
);
3595 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
3597 puts("Status: 401\n");
3600 else if (cupsLastError() > IPP_OK_CONFLICT
)
3602 cgiStartHTML(cgiText(_("Set Publishing")));
3603 cgiShowIPPError(_("Unable to change printer-is-shared attribute"));
3608 * Redirect successful updates back to the printer page...
3611 char url
[1024], /* Printer/class URL */
3612 refresh
[1024]; /* Refresh URL */
3615 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
3616 cgiFormEncode(uri
, url
, sizeof(uri
));
3617 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s", uri
);
3618 cgiSetVariable("refresh_page", refresh
);
3620 cgiStartHTML(cgiText(_("Set Publishing")));
3621 cgiCopyTemplateLang(is_class
? "class-modified.tmpl" :
3622 "printer-modified.tmpl");
3630 * 'get_option_value()' - Return the value of an option.
3632 * This function also handles generation of custom option values.
3635 static char * /* O - Value string or NULL on error */
3637 ppd_file_t
*ppd
, /* I - PPD file */
3638 const char *name
, /* I - Option name */
3639 char *buffer
, /* I - String buffer */
3640 size_t bufsize
) /* I - Size of buffer */
3642 char *bufptr
, /* Pointer into buffer */
3643 *bufend
; /* End of buffer */
3644 ppd_coption_t
*coption
; /* Custom option */
3645 ppd_cparam_t
*cparam
; /* Current custom parameter */
3646 char keyword
[256]; /* Parameter name */
3647 const char *val
, /* Parameter value */
3648 *uval
; /* Units value */
3649 long integer
; /* Integer value */
3650 double number
, /* Number value */
3651 number_points
; /* Number in points */
3655 * See if we have a custom option choice...
3658 if ((val
= cgiGetVariable(name
)) == NULL
)
3666 else if (_cups_strcasecmp(val
, "Custom") ||
3667 (coption
= ppdFindCustomOption(ppd
, name
)) == NULL
)
3670 * Not a custom choice...
3673 strlcpy(buffer
, val
, bufsize
);
3678 * OK, we have a custom option choice, format it...
3683 if (!strcmp(coption
->keyword
, "PageSize"))
3685 const char *lval
; /* Length string value */
3686 double width
, /* Width value */
3687 width_points
, /* Width in points */
3688 length
, /* Length value */
3689 length_points
; /* Length in points */
3692 val
= cgiGetVariable("PageSize.Width");
3693 lval
= cgiGetVariable("PageSize.Height");
3694 uval
= cgiGetVariable("PageSize.Units");
3696 if (!val
|| !lval
|| !uval
||
3697 (width
= strtod(val
, NULL
)) == 0.0 ||
3698 (length
= strtod(lval
, NULL
)) == 0.0 ||
3699 (strcmp(uval
, "pt") && strcmp(uval
, "in") && strcmp(uval
, "ft") &&
3700 strcmp(uval
, "cm") && strcmp(uval
, "mm") && strcmp(uval
, "m")))
3703 width_points
= get_points(width
, uval
);
3704 length_points
= get_points(length
, uval
);
3706 if (width_points
< ppd
->custom_min
[0] ||
3707 width_points
> ppd
->custom_max
[0] ||
3708 length_points
< ppd
->custom_min
[1] ||
3709 length_points
> ppd
->custom_max
[1])
3712 snprintf(buffer
, bufsize
, "Custom.%gx%g%s", width
, length
, uval
);
3714 else if (cupsArrayCount(coption
->params
) == 1)
3716 cparam
= ppdFirstCustomParam(coption
);
3717 snprintf(keyword
, sizeof(keyword
), "%s.%s", coption
->keyword
, cparam
->name
);
3719 if ((val
= cgiGetVariable(keyword
)) == NULL
)
3722 switch (cparam
->type
)
3724 case PPD_CUSTOM_CURVE
:
3725 case PPD_CUSTOM_INVCURVE
:
3726 case PPD_CUSTOM_REAL
:
3727 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3728 number
< cparam
->minimum
.custom_real
||
3729 number
> cparam
->maximum
.custom_real
)
3732 snprintf(buffer
, bufsize
, "Custom.%g", number
);
3735 case PPD_CUSTOM_INT
:
3736 if (!*val
|| (integer
= strtol(val
, NULL
, 10)) == LONG_MIN
||
3737 integer
== LONG_MAX
||
3738 integer
< cparam
->minimum
.custom_int
||
3739 integer
> cparam
->maximum
.custom_int
)
3742 snprintf(buffer
, bufsize
, "Custom.%ld", integer
);
3745 case PPD_CUSTOM_POINTS
:
3746 snprintf(keyword
, sizeof(keyword
), "%s.Units", coption
->keyword
);
3748 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3749 (uval
= cgiGetVariable(keyword
)) == NULL
||
3750 (strcmp(uval
, "pt") && strcmp(uval
, "in") && strcmp(uval
, "ft") &&
3751 strcmp(uval
, "cm") && strcmp(uval
, "mm") && strcmp(uval
, "m")))
3754 number_points
= get_points(number
, uval
);
3755 if (number_points
< cparam
->minimum
.custom_points
||
3756 number_points
> cparam
->maximum
.custom_points
)
3759 snprintf(buffer
, bufsize
, "Custom.%g%s", number
, uval
);
3762 case PPD_CUSTOM_PASSCODE
:
3763 for (uval
= val
; *uval
; uval
++)
3764 if (!isdigit(*uval
& 255))
3767 case PPD_CUSTOM_PASSWORD
:
3768 case PPD_CUSTOM_STRING
:
3769 integer
= (long)strlen(val
);
3770 if (integer
< cparam
->minimum
.custom_string
||
3771 integer
> cparam
->maximum
.custom_string
)
3774 snprintf(buffer
, bufsize
, "Custom.%s", val
);
3780 const char *prefix
= "{"; /* Prefix string */
3784 bufend
= buffer
+ bufsize
;
3786 for (cparam
= ppdFirstCustomParam(coption
);
3788 cparam
= ppdNextCustomParam(coption
))
3790 snprintf(keyword
, sizeof(keyword
), "%s.%s", coption
->keyword
,
3793 if ((val
= cgiGetVariable(keyword
)) == NULL
)
3796 snprintf(bufptr
, (size_t)(bufend
- bufptr
), "%s%s=", prefix
, cparam
->name
);
3797 bufptr
+= strlen(bufptr
);
3800 switch (cparam
->type
)
3802 case PPD_CUSTOM_CURVE
:
3803 case PPD_CUSTOM_INVCURVE
:
3804 case PPD_CUSTOM_REAL
:
3805 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3806 number
< cparam
->minimum
.custom_real
||
3807 number
> cparam
->maximum
.custom_real
)
3810 snprintf(bufptr
, (size_t)(bufend
- bufptr
), "%g", number
);
3813 case PPD_CUSTOM_INT
:
3814 if (!*val
|| (integer
= strtol(val
, NULL
, 10)) == LONG_MIN
||
3815 integer
== LONG_MAX
||
3816 integer
< cparam
->minimum
.custom_int
||
3817 integer
> cparam
->maximum
.custom_int
)
3820 snprintf(bufptr
, (size_t)(bufend
- bufptr
), "%ld", integer
);
3823 case PPD_CUSTOM_POINTS
:
3824 snprintf(keyword
, sizeof(keyword
), "%s.Units", coption
->keyword
);
3826 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3827 (uval
= cgiGetVariable(keyword
)) == NULL
||
3828 (strcmp(uval
, "pt") && strcmp(uval
, "in") &&
3829 strcmp(uval
, "ft") && strcmp(uval
, "cm") &&
3830 strcmp(uval
, "mm") && strcmp(uval
, "m")))
3833 number_points
= get_points(number
, uval
);
3834 if (number_points
< cparam
->minimum
.custom_points
||
3835 number_points
> cparam
->maximum
.custom_points
)
3838 snprintf(bufptr
, (size_t)(bufend
- bufptr
), "%g%s", number
, uval
);
3841 case PPD_CUSTOM_PASSCODE
:
3842 for (uval
= val
; *uval
; uval
++)
3843 if (!isdigit(*uval
& 255))
3846 case PPD_CUSTOM_PASSWORD
:
3847 case PPD_CUSTOM_STRING
:
3848 integer
= (long)strlen(val
);
3849 if (integer
< cparam
->minimum
.custom_string
||
3850 integer
> cparam
->maximum
.custom_string
)
3853 if ((bufptr
+ 2) > bufend
)
3859 while (*val
&& bufptr
< bufend
)
3861 if (*val
== '\\' || *val
== '\"')
3863 if ((bufptr
+ 1) >= bufend
)
3872 if (bufptr
>= bufend
)
3881 bufptr
+= strlen(bufptr
);
3884 if (bufptr
== buffer
|| (bufend
- bufptr
) < 2)
3887 memcpy(bufptr
, "}", 2);
3895 * 'get_points()' - Get a value in points.
3898 static double /* O - Number in points */
3899 get_points(double number
, /* I - Original number */
3900 const char *uval
) /* I - Units */
3902 if (!strcmp(uval
, "mm")) /* Millimeters */
3903 return (number
* 72.0 / 25.4);
3904 else if (!strcmp(uval
, "cm")) /* Centimeters */
3905 return (number
* 72.0 / 2.54);
3906 else if (!strcmp(uval
, "in")) /* Inches */
3907 return (number
* 72.0);
3908 else if (!strcmp(uval
, "ft")) /* Feet */
3909 return (number
* 72.0 * 12.0);
3910 else if (!strcmp(uval
, "m")) /* Meters */
3911 return (number
* 72.0 / 0.0254);
3918 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
3921 static char * /* O - Filename or NULL */
3922 get_printer_ppd(const char *uri
, /* I - Printer URI */
3923 char *buffer
, /* I - Filename buffer */
3924 size_t bufsize
) /* I - Size of filename buffer */
3926 http_t
*http
; /* Connection to printer */
3927 ipp_t
*request
, /* Get-Printer-Attributes request */
3928 *response
; /* Get-Printer-Attributes response */
3929 char resolved
[1024], /* Resolved URI */
3930 scheme
[32], /* URI scheme */
3931 userpass
[256], /* Username:password */
3932 host
[256], /* Hostname */
3933 resource
[256]; /* Resource path */
3934 int port
; /* Port number */
3938 * Connect to the printer...
3941 if (strstr(uri
, "._tcp"))
3947 if (!_httpResolveURI(uri
, resolved
, sizeof(resolved
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
3949 fprintf(stderr
, "ERROR: Unable to resolve \"%s\".\n", uri
);
3956 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
3958 fprintf(stderr
, "ERROR: Bad printer URI \"%s\".\n", uri
);
3962 http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, !strcmp(scheme
, "ipps") ? HTTP_ENCRYPTION_ALWAYS
: HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
);
3965 fprintf(stderr
, "ERROR: Unable to connect to \"%s:%d\": %s\n", host
, port
, cupsLastErrorString());
3970 * Send a Get-Printer-Attributes request...
3973 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
3974 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
3975 response
= cupsDoRequest(http
, request
, resource
);
3977 if (!_ppdCreateFromIPP(buffer
, bufsize
, response
))
3978 fprintf(stderr
, "ERROR: Unable to create PPD file: %s\n", strerror(errno
));
3980 ippDelete(response
);