2 * "lpadmin" command for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups-private.h>
15 #include <cups/ppd-private.h>
22 static int add_printer_to_class(http_t
*http
, char *printer
, char *pclass
);
23 static int default_printer(http_t
*http
, char *printer
);
24 static int delete_printer(http_t
*http
, char *printer
);
25 static int delete_printer_from_class(http_t
*http
, char *printer
,
27 static int delete_printer_option(http_t
*http
, char *printer
,
29 static int enable_printer(http_t
*http
, char *printer
);
30 static char *get_printer_ppd(const char *uri
, char *buffer
, size_t bufsize
, int *num_options
, cups_option_t
**options
);
31 static cups_ptype_t
get_printer_type(http_t
*http
, char *printer
, char *uri
,
33 static int set_printer_options(http_t
*http
, char *printer
,
34 int num_options
, cups_option_t
*options
,
36 static int validate_name(const char *name
);
40 * 'main()' - Parse options and configure the scheduler.
43 int /* O - Exit status */
44 main(int argc
, /* I - Number of command-line arguments */
45 char *argv
[]) /* I - Command-line arguments */
47 int i
; /* Looping var */
48 http_t
*http
; /* Connection to server */
49 char *printer
, /* Destination printer */
50 *pclass
, /* Printer class name */
51 *opt
, /* Option pointer */
52 *val
; /* Pointer to allow/deny value */
53 int num_options
; /* Number of options */
54 cups_option_t
*options
; /* Options */
55 char *file
, /* New PPD file */
56 evefile
[1024] = ""; /* IPP Everywhere PPD */
57 const char *ppd_name
, /* ppd-name value */
58 *device_uri
; /* device-uri value */
69 for (i
= 1; i
< argc
; i
++)
71 if (argv
[i
][0] == '-')
73 for (opt
= argv
[i
] + 1; *opt
; opt
++)
77 case 'c' : /* Add printer to class */
80 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
84 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
92 _("lpadmin: Unable to add a printer to the class:\n"
93 " You must specify a printer name first."));
100 opt
+= strlen(opt
) - 1;
108 _cupsLangPuts(stderr
, _("lpadmin: Expected class name after \"-c\" option."));
115 if (!validate_name(pclass
))
117 _cupsLangPuts(stderr
,
118 _("lpadmin: Class name can only contain printable "
123 if (add_printer_to_class(http
, printer
, pclass
))
127 case 'd' : /* Set as default destination */
130 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
134 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
142 opt
+= strlen(opt
) - 1;
150 _cupsLangPuts(stderr
, _("lpadmin: Expected printer name after \"-d\" option."));
157 if (!validate_name(printer
))
159 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
163 if (default_printer(http
, printer
))
169 case 'h' : /* Connect to host */
178 cupsSetServer(opt
+ 1);
179 opt
+= strlen(opt
) - 1;
187 _cupsLangPuts(stderr
, _("lpadmin: Expected hostname after \"-h\" option."));
191 cupsSetServer(argv
[i
]);
195 case 'P' : /* Use the specified PPD file */
196 case 'i' : /* Use the specified PPD file */
200 opt
+= strlen(opt
) - 1;
208 _cupsLangPrintf(stderr
, _("lpadmin: Expected PPD after \"-%c\" option."), argv
[i
- 1][1]);
218 * Check to see that the specified file is, in fact, a PPD...
221 cups_file_t
*fp
= cupsFileOpen(file
, "r");
224 if (!cupsFileGets(fp
, line
, sizeof(line
)) || strncmp(line
, "*PPD-Adobe", 10))
226 _cupsLangPuts(stderr
, _("lpadmin: System V interface scripts are no longer supported for security reasons."));
235 case 'E' : /* Enable the printer/enable encryption */
239 cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED
);
242 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
244 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
245 #endif /* HAVE_SSL */
251 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
255 _cupsLangPrintf(stderr
,
256 _("lpadmin: Unable to connect to server: %s"),
262 if (enable_printer(http
, printer
))
266 case 'm' : /* Use the specified standard script/PPD file */
269 num_options
= cupsAddOption("ppd-name", opt
+ 1, num_options
, &options
);
270 opt
+= strlen(opt
) - 1;
278 _cupsLangPuts(stderr
, _("lpadmin: Expected model after \"-m\" option."));
282 num_options
= cupsAddOption("ppd-name", argv
[i
], num_options
, &options
);
286 case 'o' : /* Set option */
289 num_options
= cupsParseOptions(opt
+ 1, num_options
, &options
);
290 opt
+= strlen(opt
) - 1;
298 _cupsLangPuts(stderr
, _("lpadmin: Expected name=value after \"-o\" option."));
302 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
306 case 'p' : /* Add/modify a printer */
310 opt
+= strlen(opt
) - 1;
318 _cupsLangPuts(stderr
, _("lpadmin: Expected printer after \"-p\" option."));
325 if (!validate_name(printer
))
327 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
332 case 'r' : /* Remove printer from class */
335 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
339 _cupsLangPrintf(stderr
,
340 _("lpadmin: Unable to connect to server: %s"),
348 _cupsLangPuts(stderr
,
349 _("lpadmin: Unable to remove a printer from the class:\n"
350 " You must specify a printer name first."));
357 opt
+= strlen(opt
) - 1;
365 _cupsLangPuts(stderr
, _("lpadmin: Expected class after \"-r\" option."));
372 if (!validate_name(pclass
))
374 _cupsLangPuts(stderr
, _("lpadmin: Class name can only contain printable characters."));
378 if (delete_printer_from_class(http
, printer
, pclass
))
382 case 'R' : /* Remove option */
385 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
389 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
396 _cupsLangPuts(stderr
,
397 _("lpadmin: Unable to delete option:\n"
398 " You must specify a printer name first."));
405 opt
+= strlen(opt
) - 1;
413 _cupsLangPuts(stderr
, _("lpadmin: Expected name after \"-R\" option."));
420 if (delete_printer_option(http
, printer
, val
))
424 case 'U' : /* Username */
427 cupsSetUser(opt
+ 1);
428 opt
+= strlen(opt
) - 1;
435 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-U\" option."), argv
[0]);
439 cupsSetUser(argv
[i
]);
443 case 'u' : /* Allow/deny users */
447 opt
+= strlen(opt
) - 1;
455 _cupsLangPuts(stderr
, _("lpadmin: Expected allow/deny:userlist after \"-u\" option."));
462 if (!_cups_strncasecmp(val
, "allow:", 6))
463 num_options
= cupsAddOption("requesting-user-name-allowed", val
+ 6, num_options
, &options
);
464 else if (!_cups_strncasecmp(val
, "deny:", 5))
465 num_options
= cupsAddOption("requesting-user-name-denied", val
+ 5, num_options
, &options
);
468 _cupsLangPrintf(stderr
, _("lpadmin: Unknown allow/deny option \"%s\"."), val
);
473 case 'v' : /* Set the device-uri attribute */
476 num_options
= cupsAddOption("device-uri", opt
+ 1, num_options
, &options
);
477 opt
+= strlen(opt
) - 1;
485 _cupsLangPuts(stderr
, _("lpadmin: Expected device URI after \"-v\" option."));
489 num_options
= cupsAddOption("device-uri", argv
[i
], num_options
, &options
);
493 case 'x' : /* Delete a printer */
496 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
500 _cupsLangPrintf(stderr
,
501 _("lpadmin: Unable to connect to server: %s"),
510 opt
+= strlen(opt
) - 1;
518 _cupsLangPuts(stderr
, _("lpadmin: Expected printer or class after \"-x\" option."));
525 if (!validate_name(printer
))
527 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
531 if (delete_printer(http
, printer
))
537 case 'D' : /* Set the printer-info attribute */
540 num_options
= cupsAddOption("printer-info", opt
+ 1, num_options
, &options
);
541 opt
+= strlen(opt
) - 1;
549 _cupsLangPuts(stderr
, _("lpadmin: Expected description after \"-D\" option."));
553 num_options
= cupsAddOption("printer-info", argv
[i
], num_options
, &options
);
557 case 'I' : /* Set the supported file types (ignored) */
562 _cupsLangPuts(stderr
, _("lpadmin: Expected file type(s) after \"-I\" option."));
566 _cupsLangPuts(stderr
, _("lpadmin: Warning - content type list ignored."));
569 case 'L' : /* Set the printer-location attribute */
572 num_options
= cupsAddOption("printer-location", opt
+ 1, num_options
, &options
);
573 opt
+= strlen(opt
) - 1;
581 _cupsLangPuts(stderr
, _("lpadmin: Expected location after \"-L\" option."));
585 num_options
= cupsAddOption("printer-location", argv
[i
], num_options
, &options
);
590 _cupsLangPrintf(stderr
, _("lpadmin: Unknown option \"%c\"."), *opt
);
597 _cupsLangPrintf(stderr
, _("lpadmin: Unknown argument \"%s\"."), argv
[i
]);
603 * Set options as needed...
606 ppd_name
= cupsGetOption("ppd-name", num_options
, options
);
607 device_uri
= cupsGetOption("device-uri", num_options
, options
);
609 if (ppd_name
&& !strcmp(ppd_name
, "raw"))
611 _cupsLangPuts(stderr
, _("lpadmin: Raw queues are deprecated and will stop working in a future version of CUPS."));
613 if (device_uri
&& (!strncmp(device_uri
, "ipp://", 6) || !strncmp(device_uri
, "ipps://", 7)) && strstr(device_uri
, "/printers/"))
614 _cupsLangPuts(stderr
, _("lpadmin: Use the 'everywhere' model for shared printers."));
616 else if (ppd_name
&& !strcmp(ppd_name
, "everywhere") && device_uri
)
618 if ((file
= get_printer_ppd(device_uri
, evefile
, sizeof(evefile
), &num_options
, &options
)) == NULL
)
621 num_options
= cupsRemoveOption("ppd-name", num_options
, &options
);
623 else if (ppd_name
|| file
)
625 _cupsLangPuts(stderr
, _("lpadmin: Printer drivers are deprecated and will stop working in a future version of CUPS."));
628 if (num_options
|| file
)
632 _cupsLangPuts(stderr
,
633 _("lpadmin: Unable to set the printer options:\n"
634 " You must specify a printer name first."));
640 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
641 cupsEncryption(), 1, 30000, NULL
);
644 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"),
650 if (set_printer_options(http
, printer
, num_options
, options
, file
))
659 _cupsLangPuts(stdout
,
662 " lpadmin [-h server] -d destination\n"
663 " lpadmin [-h server] -x destination\n"
664 " lpadmin [-h server] -p printer [-c add-class] "
665 "[-i interface] [-m model]\n"
666 " [-r remove-class] [-v device] "
668 " [-P ppd-file] [-o name=value]\n"
669 " [-u allow:user,user] "
670 "[-u deny:user,user]"));
681 * 'add_printer_to_class()' - Add a printer to a class.
684 static int /* O - 0 on success, 1 on fail */
685 add_printer_to_class(http_t
*http
, /* I - Server connection */
686 char *printer
, /* I - Printer to add */
687 char *pclass
) /* I - Class to add to */
689 int i
; /* Looping var */
690 ipp_t
*request
, /* IPP Request */
691 *response
; /* IPP Response */
692 ipp_attribute_t
*attr
, /* Current attribute */
693 *members
; /* Members in class */
694 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
697 DEBUG_printf(("add_printer_to_class(%p, \"%s\", \"%s\")\n", http
,
701 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
705 * attributes-natural-language
707 * requesting-user-name
710 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
712 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
713 "localhost", 0, "/classes/%s", pclass
);
714 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
715 "printer-uri", NULL
, uri
);
716 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
720 * Do the request and get back a response...
723 response
= cupsDoRequest(http
, request
, "/");
726 * Build a CUPS-Add-Modify-Class request, which requires the following
730 * attributes-natural-language
732 * requesting-user-name
736 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
738 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
739 "printer-uri", NULL
, uri
);
740 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
744 * See if the printer is already in the class...
747 if (response
!= NULL
&&
748 (members
= ippFindAttribute(response
, "member-names",
749 IPP_TAG_NAME
)) != NULL
)
750 for (i
= 0; i
< members
->num_values
; i
++)
751 if (_cups_strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
753 _cupsLangPrintf(stderr
,
754 _("lpadmin: Printer %s is already a member of class "
755 "%s."), printer
, pclass
);
762 * OK, the printer isn't part of the class, so add it...
765 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
766 "localhost", 0, "/printers/%s", printer
);
768 if (response
!= NULL
&&
769 (members
= ippFindAttribute(response
, "member-uris",
770 IPP_TAG_URI
)) != NULL
)
773 * Add the printer to the existing list...
776 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
777 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
778 for (i
= 0; i
< members
->num_values
; i
++)
779 attr
->values
[i
].string
.text
=
780 _cupsStrAlloc(members
->values
[i
].string
.text
);
782 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
785 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
789 * Then send the request...
794 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
795 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
797 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
807 * 'default_printer()' - Set the default printing destination.
810 static int /* O - 0 on success, 1 on fail */
811 default_printer(http_t
*http
, /* I - Server connection */
812 char *printer
) /* I - Printer name */
814 ipp_t
*request
; /* IPP Request */
815 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
818 DEBUG_printf(("default_printer(%p, \"%s\")\n", http
, printer
));
821 * Build a CUPS-Set-Default request, which requires the following
825 * attributes-natural-language
827 * requesting-user-name
830 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
831 "localhost", 0, "/printers/%s", printer
);
833 request
= ippNewRequest(IPP_OP_CUPS_SET_DEFAULT
);
835 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
836 "printer-uri", NULL
, uri
);
837 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
841 * Do the request and get back a response...
844 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
846 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
848 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
858 * 'delete_printer()' - Delete a printer from the system...
861 static int /* O - 0 on success, 1 on fail */
862 delete_printer(http_t
*http
, /* I - Server connection */
863 char *printer
) /* I - Printer to delete */
865 ipp_t
*request
; /* IPP Request */
866 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
869 DEBUG_printf(("delete_printer(%p, \"%s\")\n", http
, printer
));
872 * Build a CUPS-Delete-Printer request, which requires the following
876 * attributes-natural-language
878 * requesting-user-name
881 request
= ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER
);
883 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
884 "localhost", 0, "/printers/%s", printer
);
885 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
886 "printer-uri", NULL
, uri
);
887 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
891 * Do the request and get back a response...
894 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
896 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
898 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
908 * 'delete_printer_from_class()' - Delete a printer from a class.
911 static int /* O - 0 on success, 1 on fail */
912 delete_printer_from_class(
913 http_t
*http
, /* I - Server connection */
914 char *printer
, /* I - Printer to remove */
915 char *pclass
) /* I - Class to remove from */
917 int i
, j
, k
; /* Looping vars */
918 ipp_t
*request
, /* IPP Request */
919 *response
; /* IPP Response */
920 ipp_attribute_t
*attr
, /* Current attribute */
921 *members
; /* Members in class */
922 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
925 DEBUG_printf(("delete_printer_from_class(%p, \"%s\", \"%s\")\n", http
,
929 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
933 * attributes-natural-language
935 * requesting-user-name
938 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
940 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
941 "localhost", 0, "/classes/%s", pclass
);
942 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
943 "printer-uri", NULL
, uri
);
944 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
948 * Do the request and get back a response...
951 if ((response
= cupsDoRequest(http
, request
, "/classes/")) == NULL
||
952 response
->request
.status
.status_code
== IPP_STATUS_ERROR_NOT_FOUND
)
954 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
962 * See if the printer is already in the class...
965 if ((members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) == NULL
)
967 _cupsLangPuts(stderr
, _("lpadmin: No member names were seen."));
974 for (i
= 0; i
< members
->num_values
; i
++)
975 if (!_cups_strcasecmp(printer
, members
->values
[i
].string
.text
))
978 if (i
>= members
->num_values
)
980 _cupsLangPrintf(stderr
,
981 _("lpadmin: Printer %s is not a member of class %s."),
989 if (members
->num_values
== 1)
992 * Build a CUPS-Delete-Class request, which requires the following
996 * attributes-natural-language
998 * requesting-user-name
1001 request
= ippNewRequest(IPP_OP_CUPS_DELETE_CLASS
);
1003 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1004 "printer-uri", NULL
, uri
);
1005 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1006 "requesting-user-name", NULL
, cupsUser());
1011 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
1014 * attributes-charset
1015 * attributes-natural-language
1017 * requesting-user-name
1021 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1023 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1024 "printer-uri", NULL
, uri
);
1025 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1026 "requesting-user-name", NULL
, cupsUser());
1029 * Delete the printer from the class...
1032 members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
);
1033 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1034 "member-uris", members
->num_values
- 1, NULL
, NULL
);
1036 for (j
= 0, k
= 0; j
< members
->num_values
; j
++)
1038 attr
->values
[k
++].string
.text
=
1039 _cupsStrAlloc(members
->values
[j
].string
.text
);
1043 * Then send the request...
1046 ippDelete(response
);
1048 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1050 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1052 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1062 * 'delete_printer_option()' - Delete a printer option.
1065 static int /* O - 0 on success, 1 on fail */
1066 delete_printer_option(http_t
*http
, /* I - Server connection */
1067 char *printer
, /* I - Printer */
1068 char *option
) /* I - Option to delete */
1070 ipp_t
*request
; /* IPP request */
1071 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1075 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1076 * requires the following attributes:
1078 * attributes-charset
1079 * attributes-natural-language
1081 * requesting-user-name
1082 * option with deleteAttr tag
1085 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1086 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1088 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1090 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1091 "printer-uri", NULL
, uri
);
1092 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1093 "requesting-user-name", NULL
, cupsUser());
1094 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_DELETEATTR
, option
, 0);
1097 * Do the request and get back a response...
1100 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1102 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1104 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1114 * 'enable_printer()' - Enable a printer...
1117 static int /* O - 0 on success, 1 on fail */
1118 enable_printer(http_t
*http
, /* I - Server connection */
1119 char *printer
) /* I - Printer to enable */
1121 ipp_t
*request
; /* IPP Request */
1122 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1125 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http
, printer
));
1128 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1129 * require the following attributes:
1131 * attributes-charset
1132 * attributes-natural-language
1134 * requesting-user-name
1136 * printer-is-accepting-jobs
1139 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1140 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1142 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1144 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1145 "printer-uri", NULL
, uri
);
1146 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1147 "requesting-user-name", NULL
, cupsUser());
1148 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1150 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1153 * Do the request and get back a response...
1156 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1158 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1160 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1170 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
1173 static char * /* O - Filename or NULL */
1175 const char *uri
, /* I - Printer URI */
1176 char *buffer
, /* I - Filename buffer */
1177 size_t bufsize
, /* I - Size of filename buffer */
1178 int *num_options
, /* IO - Number of options */
1179 cups_option_t
**options
) /* IO - Options */
1181 http_t
*http
; /* Connection to printer */
1182 ipp_t
*request
, /* Get-Printer-Attributes request */
1183 *response
; /* Get-Printer-Attributes response */
1184 ipp_attribute_t
*attr
; /* Attribute from response */
1185 char resolved
[1024], /* Resolved URI */
1186 scheme
[32], /* URI scheme */
1187 userpass
[256], /* Username:password */
1188 host
[256], /* Hostname */
1189 resource
[256]; /* Resource path */
1190 int port
; /* Port number */
1191 static const char * const pattrs
[] = /* Attributes to use */
1195 "printer-description",
1196 "media-col-database"
1201 * Connect to the printer...
1204 if (strstr(uri
, "._tcp"))
1210 if (!_httpResolveURI(uri
, resolved
, sizeof(resolved
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
1212 _cupsLangPrintf(stderr
, _("%s: Unable to resolve \"%s\"."), "lpadmin", uri
);
1219 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
1221 _cupsLangPrintf(stderr
, _("%s: Bad printer URI \"%s\"."), "lpadmin", uri
);
1225 http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, !strcmp(scheme
, "ipps") ? HTTP_ENCRYPTION_ALWAYS
: HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
);
1228 _cupsLangPrintf(stderr
, _("%s: Unable to connect to \"%s:%d\": %s"), "lpadmin", host
, port
, cupsLastErrorString());
1233 * Send a Get-Printer-Attributes request...
1236 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1237 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1238 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]), NULL
, pattrs
);
1239 response
= cupsDoRequest(http
, request
, resource
);
1241 if (_ppdCreateFromIPP(buffer
, bufsize
, response
))
1243 if (!cupsGetOption("printer-geo-location", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-geo-location", IPP_TAG_URI
)) != NULL
)
1244 *num_options
= cupsAddOption("printer-geo-location", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1246 if (!cupsGetOption("printer-info", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-info", IPP_TAG_TEXT
)) != NULL
)
1247 *num_options
= cupsAddOption("printer-info", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1249 if (!cupsGetOption("printer-location", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-location", IPP_TAG_TEXT
)) != NULL
)
1250 *num_options
= cupsAddOption("printer-location", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1253 _cupsLangPrintf(stderr
, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno
));
1255 ippDelete(response
);
1266 * 'get_printer_type()' - Determine the printer type and URI.
1269 static cups_ptype_t
/* O - printer-type value */
1270 get_printer_type(http_t
*http
, /* I - Server connection */
1271 char *printer
, /* I - Printer name */
1272 char *uri
, /* I - URI buffer */
1273 size_t urisize
) /* I - Size of URI buffer */
1275 ipp_t
*request
, /* IPP request */
1276 *response
; /* IPP response */
1277 ipp_attribute_t
*attr
; /* printer-type attribute */
1278 cups_ptype_t type
; /* printer-type value */
1282 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1285 * attributes-charset
1286 * attributes-natural-language
1288 * requested-attributes
1289 * requesting-user-name
1292 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1294 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1295 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1296 "printer-uri", NULL
, uri
);
1297 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1298 "requested-attributes", NULL
, "printer-type");
1299 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1300 "requesting-user-name", NULL
, cupsUser());
1306 response
= cupsDoRequest(http
, request
, "/");
1307 if ((attr
= ippFindAttribute(response
, "printer-type",
1308 IPP_TAG_ENUM
)) != NULL
)
1310 type
= (cups_ptype_t
)attr
->values
[0].integer
;
1312 if (type
& CUPS_PRINTER_CLASS
)
1313 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/classes/%s", printer
);
1316 type
= CUPS_PRINTER_LOCAL
;
1318 ippDelete(response
);
1325 * 'set_printer_options()' - Set the printer options.
1328 static int /* O - 0 on success, 1 on fail */
1329 set_printer_options(
1330 http_t
*http
, /* I - Server connection */
1331 char *printer
, /* I - Printer */
1332 int num_options
, /* I - Number of options */
1333 cups_option_t
*options
, /* I - Options */
1334 char *file
) /* I - PPD file/interface script */
1336 ipp_t
*request
; /* IPP Request */
1337 const char *ppdfile
; /* PPD filename */
1338 int ppdchanged
= 0; /* PPD changed? */
1339 ppd_file_t
*ppd
; /* PPD file */
1340 ppd_choice_t
*choice
; /* Marked choice */
1341 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1342 line
[1024], /* Line from PPD file */
1343 keyword
[1024], /* Keyword from Default line */
1344 *keyptr
, /* Pointer into keyword... */
1345 tempfile
[1024]; /* Temporary filename */
1346 cups_file_t
*in
, /* PPD file */
1347 *out
; /* Temporary file */
1348 const char *ppdname
, /* ppd-name value */
1349 *protocol
, /* Old protocol option */
1350 *customval
, /* Custom option value */
1351 *boolval
; /* Boolean value */
1352 int wrote_ipp_supplies
= 0, /* Wrote cupsIPPSupplies keyword? */
1353 wrote_snmp_supplies
= 0,/* Wrote cupsSNMPSupplies keyword? */
1354 copied_options
= 0; /* Copied options? */
1357 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1358 "options=%p, file=\"%s\")\n", http
, printer
, num_options
,
1362 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1363 * which requires the following attributes:
1365 * attributes-charset
1366 * attributes-natural-language
1368 * requesting-user-name
1372 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1373 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1375 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1377 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1378 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1381 * Add the options...
1386 else if ((ppdname
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& strcmp(ppdname
, "raw") && num_options
> 1)
1388 if ((ppdfile
= cupsGetServerPPD(http
, ppdname
)) != NULL
)
1391 * Copy options array and remove ppd-name from it...
1394 cups_option_t
*temp
= NULL
, *optr
;
1395 int i
, num_temp
= 0;
1396 for (i
= num_options
, optr
= options
; i
> 0; i
--, optr
++)
1397 if (strcmp(optr
->name
, "ppd-name"))
1398 num_temp
= cupsAddOption(optr
->name
, optr
->value
, num_temp
, &temp
);
1402 num_options
= num_temp
;
1406 else if (request
->request
.op
.operation_id
== IPP_OP_CUPS_ADD_MODIFY_PRINTER
)
1407 ppdfile
= cupsGetPPD(printer
);
1411 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
1412 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1414 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1416 if (!_cups_strcasecmp(protocol
, "bcp"))
1417 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1419 else if (!_cups_strcasecmp(protocol
, "tbcp"))
1420 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1427 * Set default options in the PPD file...
1430 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
1432 int linenum
; /* Line number of error */
1433 ppd_status_t status
= ppdLastError(&linenum
);
1436 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile
, ppdErrorString(status
), linenum
);
1439 ppdMarkDefaults(ppd
);
1440 cupsMarkOptions(ppd
, num_options
, options
);
1442 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1444 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1446 if (ppdfile
!= file
)
1449 cupsFreeOptions(num_options
, options
);
1453 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1455 _cupsLangPrintf(stderr
,
1456 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1457 ppdfile
, strerror(errno
));
1459 if (ppdfile
!= file
)
1462 cupsFreeOptions(num_options
, options
);
1468 while (cupsFileGets(in
, line
, sizeof(line
)))
1470 if (!strncmp(line
, "*cupsIPPSupplies:", 17) &&
1471 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1474 wrote_ipp_supplies
= 1;
1475 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1476 (!_cups_strcasecmp(boolval
, "true") ||
1477 !_cups_strcasecmp(boolval
, "yes") ||
1478 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1480 else if (!strncmp(line
, "*cupsSNMPSupplies:", 18) &&
1481 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1484 wrote_snmp_supplies
= 1;
1485 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1486 (!_cups_strcasecmp(boolval
, "true") ||
1487 !_cups_strcasecmp(boolval
, "yes") ||
1488 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1490 else if (strncmp(line
, "*Default", 8))
1491 cupsFilePrintf(out
, "%s\n", line
);
1495 * Get default option name...
1498 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
1500 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1501 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1505 while (isspace(*keyptr
& 255))
1508 if (!strcmp(keyword
, "PageRegion") ||
1509 !strcmp(keyword
, "PageSize") ||
1510 !strcmp(keyword
, "PaperDimension") ||
1511 !strcmp(keyword
, "ImageableArea"))
1513 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1514 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1517 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1519 if (choice
&& strcmp(choice
->choice
, keyptr
))
1521 if (strcmp(choice
->choice
, "Custom"))
1523 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1526 else if ((customval
= cupsGetOption(keyword
, num_options
,
1529 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1533 cupsFilePrintf(out
, "%s\n", line
);
1536 cupsFilePrintf(out
, "%s\n", line
);
1540 if (!wrote_ipp_supplies
&&
1541 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1544 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1545 (!_cups_strcasecmp(boolval
, "true") ||
1546 !_cups_strcasecmp(boolval
, "yes") ||
1547 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1550 if (!wrote_snmp_supplies
&&
1551 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1554 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1555 (!_cups_strcasecmp(boolval
, "true") ||
1556 !_cups_strcasecmp(boolval
, "yes") ||
1557 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1568 ippDelete(cupsDoFileRequest(http
, request
, "/admin/",
1569 ppdchanged
? tempfile
: file
));
1572 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1576 if (ppdfile
!= file
)
1583 * No PPD file - just set the options...
1586 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1590 cupsFreeOptions(num_options
, options
);
1593 * Check the response...
1596 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1598 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1608 * 'validate_name()' - Make sure the printer name only contains valid chars.
1611 static int /* O - 0 if name is no good, 1 if name is good */
1612 validate_name(const char *name
) /* I - Name to check */
1614 const char *ptr
; /* Pointer into name */
1618 * Scan the whole name...
1621 for (ptr
= name
; *ptr
; ptr
++)
1624 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '\\' || *ptr
== '?' || *ptr
== '\'' || *ptr
== '\"' || *ptr
== '#')
1628 * All the characters are good; validate the length, too...
1631 return ((ptr
- name
) < 128);