2 * "lpadmin" command for CUPS.
4 * Copyright © 2020-2024 by OpenPrinting.
5 * Copyright © 2007-2021 by Apple Inc.
6 * Copyright © 1997-2006 by Easy Software Products.
8 * Licensed under Apache License v2.0. See the file "LICENSE" for more
13 * Include necessary headers...
16 #include <cups/cups-private.h>
17 #include <cups/ppd-private.h>
24 static int add_printer_to_class(http_t
*http
, char *printer
, char *pclass
);
25 static int default_printer(http_t
*http
, char *printer
);
26 static int delete_printer(http_t
*http
, char *printer
);
27 static int delete_printer_from_class(http_t
*http
, char *printer
,
29 static int delete_printer_option(http_t
*http
, char *printer
,
31 static int enable_printer(http_t
*http
, char *printer
);
32 static cups_ptype_t
get_printer_type(http_t
*http
, char *printer
, char *uri
,
34 static int set_printer_options(http_t
*http
, char *printer
,
35 int num_options
, cups_option_t
*options
,
36 char *file
, int enable
);
37 static void usage(void) _CUPS_NORETURN
;
38 static int validate_name(const char *name
);
42 * 'main()' - Parse options and configure the scheduler.
45 int /* O - Exit status */
46 main(int argc
, /* I - Number of command-line arguments */
47 char *argv
[]) /* I - Command-line arguments */
49 int i
; /* Looping var */
50 http_t
*http
; /* Connection to server */
51 char *printer
, /* Destination printer */
52 *pclass
, /* Printer class name */
53 *opt
, /* Option pointer */
54 *val
; /* Pointer to allow/deny value */
55 int enable
= 0; /* Enable/resume printer? */
56 int num_options
; /* Number of options */
57 cups_option_t
*options
; /* Options */
58 char *file
, /* New PPD file */
59 evefile
[1024] = ""; /* IPP Everywhere PPD */
60 const char *ppd_name
, /* ppd-name value */
61 *device_uri
; /* device-uri value */
72 for (i
= 1; i
< argc
; i
++)
74 if (!strcmp(argv
[i
], "--help"))
76 else if (argv
[i
][0] == '-')
78 for (opt
= argv
[i
] + 1; *opt
; opt
++)
82 case 'c' : /* Add printer to class */
85 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
89 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
97 _("lpadmin: Unable to add a printer to the class:\n"
98 " You must specify a printer name first."));
105 opt
+= strlen(opt
) - 1;
113 _cupsLangPuts(stderr
, _("lpadmin: Expected class name after \"-c\" option."));
120 if (!validate_name(pclass
))
122 _cupsLangPuts(stderr
,
123 _("lpadmin: Class name can only contain printable "
128 if (add_printer_to_class(http
, printer
, pclass
))
132 case 'd' : /* Set as default destination */
135 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
139 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
147 opt
+= strlen(opt
) - 1;
155 _cupsLangPuts(stderr
, _("lpadmin: Expected printer name after \"-d\" option."));
162 if (!validate_name(printer
))
164 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
168 if (default_printer(http
, printer
))
174 case 'h' : /* Connect to host */
183 cupsSetServer(opt
+ 1);
184 opt
+= strlen(opt
) - 1;
192 _cupsLangPuts(stderr
, _("lpadmin: Expected hostname after \"-h\" option."));
196 cupsSetServer(argv
[i
]);
200 case 'P' : /* Use the specified PPD file */
201 case 'i' : /* Use the specified PPD file */
205 opt
+= strlen(opt
) - 1;
213 _cupsLangPrintf(stderr
, _("lpadmin: Expected PPD after \"-%c\" option."), argv
[i
- 1][1]);
223 * Check to see that the specified file is, in fact, a PPD...
226 cups_file_t
*fp
= cupsFileOpen(file
, "r");
229 if (!cupsFileGets(fp
, line
, sizeof(line
)) || strncmp(line
, "*PPD-Adobe", 10))
231 _cupsLangPuts(stderr
, _("lpadmin: System V interface scripts are no longer supported for security reasons."));
240 case 'E' : /* Enable the printer/enable encryption */
243 cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED
);
246 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
252 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
256 _cupsLangPrintf(stderr
,
257 _("lpadmin: Unable to connect to server: %s"),
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"))
612 _cupsLangPuts(stderr
, _("lpadmin: Raw queues are no longer supported on macOS."));
614 _cupsLangPuts(stderr
, _("lpadmin: Raw queues are deprecated and will stop working in a future version of CUPS."));
615 #endif /* __APPLE__ */
617 if (device_uri
&& (!strncmp(device_uri
, "ipp://", 6) || !strncmp(device_uri
, "ipps://", 7)) && strstr(device_uri
, "/printers/"))
618 _cupsLangPuts(stderr
, _("lpadmin: Use the 'everywhere' model for shared printers."));
622 #endif /* __APPLE__ */
624 else if ((ppd_name
&& strcmp(ppd_name
, "everywhere") && strncmp(ppd_name
, "driverless:", 11)) || file
)
626 _cupsLangPuts(stderr
, _("lpadmin: Printer drivers are deprecated and will stop working in a future version of CUPS."));
629 if (num_options
|| file
)
633 _cupsLangPuts(stderr
,
634 _("lpadmin: Unable to set the printer options:\n"
635 " You must specify a printer name first."));
641 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
642 cupsEncryption(), 1, 30000, NULL
);
645 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"),
651 if (set_printer_options(http
, printer
, num_options
, options
, file
, enable
))
654 else if (enable
&& enable_printer(http
, printer
))
671 * 'add_printer_to_class()' - Add a printer to a class.
674 static int /* O - 0 on success, 1 on fail */
675 add_printer_to_class(http_t
*http
, /* I - Server connection */
676 char *printer
, /* I - Printer to add */
677 char *pclass
) /* I - Class to add to */
679 int i
; /* Looping var */
680 ipp_t
*request
, /* IPP Request */
681 *response
; /* IPP Response */
682 ipp_attribute_t
*attr
, /* Current attribute */
683 *members
; /* Members in class */
684 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
688 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
692 * attributes-natural-language
694 * requesting-user-name
697 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
699 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
700 "localhost", 0, "/classes/%s", pclass
);
701 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
702 "printer-uri", NULL
, uri
);
703 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
704 NULL
, cupsGetUser());
707 * Do the request and get back a response...
710 response
= cupsDoRequest(http
, request
, "/");
713 * Build a CUPS-Add-Modify-Class request, which requires the following
717 * attributes-natural-language
719 * requesting-user-name
723 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
725 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
726 "printer-uri", NULL
, uri
);
727 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
728 NULL
, cupsGetUser());
731 * See if the printer is already in the class...
734 if (response
!= NULL
&&
735 (members
= ippFindAttribute(response
, "member-names",
736 IPP_TAG_NAME
)) != NULL
)
737 for (i
= 0; i
< members
->num_values
; i
++)
738 if (_cups_strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
740 _cupsLangPrintf(stderr
,
741 _("lpadmin: Printer %s is already a member of class "
742 "%s."), printer
, pclass
);
749 * OK, the printer isn't part of the class, so add it...
752 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
753 "localhost", 0, "/printers/%s", printer
);
755 if (response
!= NULL
&&
756 (members
= ippFindAttribute(response
, "member-uris",
757 IPP_TAG_URI
)) != NULL
)
760 * Add the printer to the existing list...
763 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
764 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
765 for (i
= 0; i
< members
->num_values
; i
++)
766 attr
->values
[i
].string
.text
=
767 _cupsStrAlloc(members
->values
[i
].string
.text
);
769 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
772 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
776 * Then send the request...
781 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
782 if (cupsGetError() > IPP_STATUS_OK_CONFLICTING
)
784 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsGetErrorString());
794 * 'default_printer()' - Set the default printing destination.
797 static int /* O - 0 on success, 1 on fail */
798 default_printer(http_t
*http
, /* I - Server connection */
799 char *printer
) /* I - Printer name */
801 ipp_t
*request
; /* IPP Request */
802 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
806 * Build a CUPS-Set-Default request, which requires the following
810 * attributes-natural-language
812 * requesting-user-name
815 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
816 "localhost", 0, "/printers/%s", printer
);
818 request
= ippNewRequest(IPP_OP_CUPS_SET_DEFAULT
);
820 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
821 "printer-uri", NULL
, uri
);
822 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
823 NULL
, cupsGetUser());
826 * Do the request and get back a response...
829 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
831 if (cupsGetError() > IPP_STATUS_OK_CONFLICTING
)
833 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsGetErrorString());
843 * 'delete_printer()' - Delete a printer from the system...
846 static int /* O - 0 on success, 1 on fail */
847 delete_printer(http_t
*http
, /* I - Server connection */
848 char *printer
) /* I - Printer to delete */
850 ipp_t
*request
; /* IPP Request */
851 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
855 * Build a CUPS-Delete-Printer request, which requires the following
859 * attributes-natural-language
861 * requesting-user-name
864 request
= ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER
);
866 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
867 "localhost", 0, "/printers/%s", printer
);
868 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
869 "printer-uri", NULL
, uri
);
870 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
871 NULL
, cupsGetUser());
874 * Do the request and get back a response...
877 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
879 if (cupsGetError() > IPP_STATUS_OK_CONFLICTING
)
881 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsGetErrorString());
891 * 'delete_printer_from_class()' - Delete a printer from a class.
894 static int /* O - 0 on success, 1 on fail */
895 delete_printer_from_class(
896 http_t
*http
, /* I - Server connection */
897 char *printer
, /* I - Printer to remove */
898 char *pclass
) /* I - Class to remove from */
900 int i
, j
, k
; /* Looping vars */
901 ipp_t
*request
, /* IPP Request */
902 *response
; /* IPP Response */
903 ipp_attribute_t
*attr
, /* Current attribute */
904 *members
; /* Members in class */
905 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
909 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
913 * attributes-natural-language
915 * requesting-user-name
918 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
920 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
921 "localhost", 0, "/classes/%s", pclass
);
922 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
923 "printer-uri", NULL
, uri
);
924 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
925 NULL
, cupsGetUser());
928 * Do the request and get back a response...
931 if ((response
= cupsDoRequest(http
, request
, "/classes/")) == NULL
||
932 response
->request
.status
.status_code
== IPP_STATUS_ERROR_NOT_FOUND
)
934 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsGetErrorString());
942 * See if the printer is already in the class...
945 if ((members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) == NULL
)
947 _cupsLangPuts(stderr
, _("lpadmin: No member names were seen."));
954 for (i
= 0; i
< members
->num_values
; i
++)
955 if (!_cups_strcasecmp(printer
, members
->values
[i
].string
.text
))
958 if (i
>= members
->num_values
)
960 _cupsLangPrintf(stderr
,
961 _("lpadmin: Printer %s is not a member of class %s."),
969 if (members
->num_values
== 1)
972 * Build a CUPS-Delete-Class request, which requires the following
976 * attributes-natural-language
978 * requesting-user-name
981 request
= ippNewRequest(IPP_OP_CUPS_DELETE_CLASS
);
983 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
984 "printer-uri", NULL
, uri
);
985 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
986 "requesting-user-name", NULL
, cupsGetUser());
991 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
995 * attributes-natural-language
997 * requesting-user-name
1001 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_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
, cupsGetUser());
1009 * Delete the printer from the class...
1012 members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
);
1013 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1014 "member-uris", members
->num_values
- 1, NULL
, NULL
);
1016 for (j
= 0, k
= 0; j
< members
->num_values
; j
++)
1018 attr
->values
[k
++].string
.text
=
1019 _cupsStrAlloc(members
->values
[j
].string
.text
);
1023 * Then send the request...
1026 ippDelete(response
);
1028 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1030 if (cupsGetError() > IPP_STATUS_OK_CONFLICTING
)
1032 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsGetErrorString());
1042 * 'delete_printer_option()' - Delete a printer option.
1045 static int /* O - 0 on success, 1 on fail */
1046 delete_printer_option(http_t
*http
, /* I - Server connection */
1047 char *printer
, /* I - Printer */
1048 char *option
) /* I - Option to delete */
1050 ipp_t
*request
; /* IPP request */
1051 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1055 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1056 * requires the following attributes:
1058 * attributes-charset
1059 * attributes-natural-language
1061 * requesting-user-name
1062 * option with deleteAttr tag
1065 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1066 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1068 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1070 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1071 "printer-uri", NULL
, uri
);
1072 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1073 "requesting-user-name", NULL
, cupsGetUser());
1074 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_DELETEATTR
, option
, 0);
1077 * Do the request and get back a response...
1080 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1082 if (cupsGetError() > IPP_STATUS_OK_CONFLICTING
)
1084 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsGetErrorString());
1094 * 'enable_printer()' - Enable a printer...
1097 static int /* O - 0 on success, 1 on fail */
1098 enable_printer(http_t
*http
, /* I - Server connection */
1099 char *printer
) /* I - Printer to enable */
1101 ipp_t
*request
; /* IPP Request */
1102 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1106 * Send IPP_OP_ENABLE_PRINTER and IPP_OP_RESUME_PRINTER requests, which
1107 * require the following attributes:
1109 * attributes-charset
1110 * attributes-natural-language
1112 * requesting-user-name
1115 request
= ippNewRequest(IPP_OP_ENABLE_PRINTER
);
1117 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1118 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1119 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsGetUser());
1121 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1123 if (cupsGetError() > IPP_STATUS_OK_CONFLICTING
)
1125 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsGetErrorString());
1130 request
= ippNewRequest(IPP_OP_RESUME_PRINTER
);
1132 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1133 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsGetUser());
1135 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1137 if (cupsGetError() > IPP_STATUS_OK_CONFLICTING
)
1139 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsGetErrorString());
1149 * 'get_printer_type()' - Determine the printer type and URI.
1152 static cups_ptype_t
/* O - printer-type value */
1153 get_printer_type(http_t
*http
, /* I - Server connection */
1154 char *printer
, /* I - Printer name */
1155 char *uri
, /* I - URI buffer */
1156 size_t urisize
) /* I - Size of URI buffer */
1158 ipp_t
*request
, /* IPP request */
1159 *response
; /* IPP response */
1160 ipp_attribute_t
*attr
; /* printer-type attribute */
1161 cups_ptype_t type
; /* printer-type value */
1165 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1168 * attributes-charset
1169 * attributes-natural-language
1171 * requested-attributes
1172 * requesting-user-name
1175 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1177 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1178 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1179 "printer-uri", NULL
, uri
);
1180 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1181 "requested-attributes", NULL
, "printer-type");
1182 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1183 "requesting-user-name", NULL
, cupsGetUser());
1189 response
= cupsDoRequest(http
, request
, "/");
1190 if ((attr
= ippFindAttribute(response
, "printer-type",
1191 IPP_TAG_ENUM
)) != NULL
)
1193 type
= (cups_ptype_t
)attr
->values
[0].integer
;
1195 if (type
& CUPS_PRINTER_CLASS
)
1196 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/classes/%s", printer
);
1199 type
= CUPS_PRINTER_LOCAL
;
1201 ippDelete(response
);
1208 * 'set_printer_options()' - Set the printer options.
1211 static int /* O - 0 on success, 1 on fail */
1212 set_printer_options(
1213 http_t
*http
, /* I - Server connection */
1214 char *printer
, /* I - Printer */
1215 int num_options
, /* I - Number of options */
1216 cups_option_t
*options
, /* I - Options */
1217 char *file
, /* I - PPD file */
1218 int enable
) /* I - Enable printer? */
1220 ipp_t
*request
; /* IPP Request */
1221 const char *ppdfile
; /* PPD filename */
1222 int ppdchanged
= 0; /* PPD changed? */
1223 ppd_file_t
*ppd
; /* PPD file */
1224 ppd_choice_t
*choice
; /* Marked choice */
1225 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1226 line
[1024], /* Line from PPD file */
1227 keyword
[1024], /* Keyword from Default line */
1228 *keyptr
, /* Pointer into keyword... */
1229 tempfile
[1024]; /* Temporary filename */
1230 cups_file_t
*in
, /* PPD file */
1231 *out
; /* Temporary file */
1232 const char *ppdname
, /* ppd-name value */
1233 *protocol
, /* Old protocol option */
1234 *customval
, /* Custom option value */
1235 *boolval
; /* Boolean value */
1236 int wrote_ipp_supplies
= 0, /* Wrote cupsIPPSupplies keyword? */
1237 wrote_snmp_supplies
= 0,/* Wrote cupsSNMPSupplies keyword? */
1238 copied_options
= 0; /* Copied options? */
1242 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1243 * which requires the following attributes:
1245 * attributes-charset
1246 * attributes-natural-language
1248 * requesting-user-name
1252 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1253 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1255 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1257 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1258 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsGetUser());
1261 * Add the options...
1266 else if ((ppdname
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& strcmp(ppdname
, "everywhere") && strcmp(ppdname
, "raw") && num_options
> 1)
1268 if ((ppdfile
= cupsGetServerPPD(http
, ppdname
)) != NULL
)
1271 * Copy options array and remove ppd-name from it...
1274 cups_option_t
*temp
= NULL
, *optr
;
1275 int i
, num_temp
= 0;
1276 for (i
= num_options
, optr
= options
; i
> 0; i
--, optr
++)
1277 if (strcmp(optr
->name
, "ppd-name"))
1278 num_temp
= cupsAddOption(optr
->name
, optr
->value
, num_temp
, &temp
);
1282 num_options
= num_temp
;
1286 else if (request
->request
.op
.operation_id
== IPP_OP_CUPS_ADD_MODIFY_PRINTER
)
1287 ppdfile
= cupsGetPPD(printer
);
1291 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
1295 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state", IPP_PSTATE_IDLE
);
1296 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1299 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1301 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1303 if (!_cups_strcasecmp(protocol
, "bcp"))
1304 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1306 else if (!_cups_strcasecmp(protocol
, "tbcp"))
1307 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1314 * Set default options in the PPD file...
1317 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
1319 int linenum
; /* Line number of error */
1320 ppd_status_t status
= ppdLastError(&linenum
);
1323 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile
, ppdErrorString(status
), linenum
);
1327 ppdMarkDefaults(ppd
);
1328 cupsMarkOptions(ppd
, num_options
, options
);
1330 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1332 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1336 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1338 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s"), ppdfile
, strerror(errno
));
1344 while (cupsFileGets(in
, line
, sizeof(line
)))
1346 if (!strncmp(line
, "*cupsIPPSupplies:", 17) &&
1347 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1351 wrote_ipp_supplies
= 1;
1352 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1353 (!_cups_strcasecmp(boolval
, "true") ||
1354 !_cups_strcasecmp(boolval
, "yes") ||
1355 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1357 else if (!strncmp(line
, "*cupsSNMPSupplies:", 18) &&
1358 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1362 wrote_snmp_supplies
= 1;
1363 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1364 (!_cups_strcasecmp(boolval
, "true") ||
1365 !_cups_strcasecmp(boolval
, "yes") ||
1366 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1368 else if (strncmp(line
, "*Default", 8))
1369 cupsFilePrintf(out
, "%s\n", line
);
1373 * Get default option name...
1376 cupsCopyString(keyword
, line
+ 8, sizeof(keyword
));
1378 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1379 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1383 while (isspace(*keyptr
& 255))
1386 if (!strcmp(keyword
, "PageRegion") ||
1387 !strcmp(keyword
, "PageSize") ||
1388 !strcmp(keyword
, "PaperDimension") ||
1389 !strcmp(keyword
, "ImageableArea"))
1391 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1392 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1395 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1397 if (choice
&& strcmp(choice
->choice
, keyptr
))
1399 if (strcmp(choice
->choice
, "Custom"))
1401 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1404 else if ((customval
= cupsGetOption(keyword
, num_options
,
1407 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1411 cupsFilePrintf(out
, "%s\n", line
);
1414 cupsFilePrintf(out
, "%s\n", line
);
1418 if (!wrote_ipp_supplies
&&
1419 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1424 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1425 (!_cups_strcasecmp(boolval
, "true") ||
1426 !_cups_strcasecmp(boolval
, "yes") ||
1427 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1430 if (!wrote_snmp_supplies
&&
1431 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1436 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1437 (!_cups_strcasecmp(boolval
, "true") ||
1438 !_cups_strcasecmp(boolval
, "yes") ||
1439 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1450 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", ppdchanged
? tempfile
: file
));
1453 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1457 if (ppdfile
!= file
)
1464 * No PPD file - just set the options...
1467 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1471 cupsFreeOptions(num_options
, options
);
1474 * Check the response...
1477 if (cupsGetError() > IPP_STATUS_OK_CONFLICTING
)
1479 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsGetErrorString());
1494 if (ppdfile
!= file
)
1498 cupsFreeOptions(num_options
, options
);
1505 * 'usage()' - Show program usage and exit.
1511 _cupsLangPuts(stdout
, _("Usage: lpadmin [options] -d destination\n"
1512 " lpadmin [options] -p destination\n"
1513 " lpadmin [options] -p destination -c class\n"
1514 " lpadmin [options] -p destination -r class\n"
1515 " lpadmin [options] -x destination"));
1516 _cupsLangPuts(stdout
, _("Options:"));
1517 _cupsLangPuts(stdout
, _("-c class Add the named destination to a class"));
1518 _cupsLangPuts(stdout
, _("-d destination Set the named destination as the server default"));
1519 _cupsLangPuts(stdout
, _("-D description Specify the textual description of the printer"));
1520 _cupsLangPuts(stdout
, _("-E Encrypt the connection to the server"));
1521 _cupsLangPuts(stdout
, _("-E Enable and accept jobs on the printer (after -p)"));
1522 _cupsLangPuts(stdout
, _("-h server[:port] Connect to the named server and port"));
1523 _cupsLangPuts(stdout
, _("-i ppd-file Specify a PPD file for the printer"));
1524 _cupsLangPuts(stdout
, _("-L location Specify the textual location of the printer"));
1525 _cupsLangPuts(stdout
, _("-m model Specify a standard model/PPD file for the printer"));
1526 _cupsLangPuts(stdout
, _("-m everywhere Specify the printer is compatible with IPP Everywhere"));
1527 _cupsLangPuts(stdout
, _("-o name-default=value Specify the default value for the named option"));
1528 _cupsLangPuts(stdout
, _("-o Name=Value Specify the default value for the named PPD option "));
1529 _cupsLangPuts(stdout
, _("-o cupsIPPSupplies=false\n"
1530 " Disable supply level reporting via IPP"));
1531 _cupsLangPuts(stdout
, _("-o cupsSNMPSupplies=false\n"
1532 " Disable supply level reporting via SNMP"));
1533 _cupsLangPuts(stdout
, _("-o job-k-limit=N Specify the kilobyte limit for per-user quotas"));
1534 _cupsLangPuts(stdout
, _("-o job-page-limit=N Specify the page limit for per-user quotas"));
1535 _cupsLangPuts(stdout
, _("-o job-quota-period=N Specify the per-user quota period in seconds"));
1536 _cupsLangPuts(stdout
, _("-o printer-error-policy=name\n"
1537 " Specify the printer error policy"));
1538 _cupsLangPuts(stdout
, _("-o printer-is-shared=true\n"
1539 " Share the printer"));
1540 _cupsLangPuts(stdout
, _("-o printer-op-policy=name\n"
1541 " Specify the printer operation policy"));
1542 _cupsLangPuts(stdout
, _("-p destination Specify/add the named destination"));
1543 _cupsLangPuts(stdout
, _("-r class Remove the named destination from a class"));
1544 _cupsLangPuts(stdout
, _("-R name-default Remove the default value for the named option"));
1545 _cupsLangPuts(stdout
, _("-u allow:all Allow all users to print"));
1546 _cupsLangPuts(stdout
, _("-u allow:list Allow the list of users or groups (@name) to print"));
1547 _cupsLangPuts(stdout
, _("-u deny:list Prevent the list of users or groups (@name) to print"));
1548 _cupsLangPuts(stdout
, _("-U username Specify the username to use for authentication"));
1549 _cupsLangPuts(stdout
, _("-v device-uri Specify the device URI for the printer"));
1550 _cupsLangPuts(stdout
, _("-x destination Remove the named destination"));
1557 * 'validate_name()' - Make sure the printer name only contains valid chars.
1560 static int /* O - 0 if name is no good, 1 if name is good */
1561 validate_name(const char *name
) /* I - Name to check */
1563 const char *ptr
; /* Pointer into name */
1567 * Scan the whole name...
1570 for (ptr
= name
; *ptr
; ptr
++)
1573 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '\\' || *ptr
== '?' || *ptr
== '\'' || *ptr
== '\"' || *ptr
== '#')
1577 * All the characters are good; validate the length, too...
1580 return ((ptr
- name
) < 128);