2 * "lpadmin" command for CUPS.
4 * Copyright © 2021 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 */
244 cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED
);
247 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
249 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
250 #endif /* HAVE_TLS */
256 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
260 _cupsLangPrintf(stderr
,
261 _("lpadmin: Unable to connect to server: %s"),
270 case 'm' : /* Use the specified standard script/PPD file */
273 num_options
= cupsAddOption("ppd-name", opt
+ 1, num_options
, &options
);
274 opt
+= strlen(opt
) - 1;
282 _cupsLangPuts(stderr
, _("lpadmin: Expected model after \"-m\" option."));
286 num_options
= cupsAddOption("ppd-name", argv
[i
], num_options
, &options
);
290 case 'o' : /* Set option */
293 num_options
= cupsParseOptions(opt
+ 1, num_options
, &options
);
294 opt
+= strlen(opt
) - 1;
302 _cupsLangPuts(stderr
, _("lpadmin: Expected name=value after \"-o\" option."));
306 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
310 case 'p' : /* Add/modify a printer */
314 opt
+= strlen(opt
) - 1;
322 _cupsLangPuts(stderr
, _("lpadmin: Expected printer after \"-p\" option."));
329 if (!validate_name(printer
))
331 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
336 case 'r' : /* Remove printer from class */
339 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
343 _cupsLangPrintf(stderr
,
344 _("lpadmin: Unable to connect to server: %s"),
352 _cupsLangPuts(stderr
,
353 _("lpadmin: Unable to remove a printer from the class:\n"
354 " You must specify a printer name first."));
361 opt
+= strlen(opt
) - 1;
369 _cupsLangPuts(stderr
, _("lpadmin: Expected class after \"-r\" option."));
376 if (!validate_name(pclass
))
378 _cupsLangPuts(stderr
, _("lpadmin: Class name can only contain printable characters."));
382 if (delete_printer_from_class(http
, printer
, pclass
))
386 case 'R' : /* Remove option */
389 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
393 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
400 _cupsLangPuts(stderr
,
401 _("lpadmin: Unable to delete option:\n"
402 " You must specify a printer name first."));
409 opt
+= strlen(opt
) - 1;
417 _cupsLangPuts(stderr
, _("lpadmin: Expected name after \"-R\" option."));
424 if (delete_printer_option(http
, printer
, val
))
428 case 'U' : /* Username */
431 cupsSetUser(opt
+ 1);
432 opt
+= strlen(opt
) - 1;
439 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-U\" option."), argv
[0]);
443 cupsSetUser(argv
[i
]);
447 case 'u' : /* Allow/deny users */
451 opt
+= strlen(opt
) - 1;
459 _cupsLangPuts(stderr
, _("lpadmin: Expected allow/deny:userlist after \"-u\" option."));
466 if (!_cups_strncasecmp(val
, "allow:", 6))
467 num_options
= cupsAddOption("requesting-user-name-allowed", val
+ 6, num_options
, &options
);
468 else if (!_cups_strncasecmp(val
, "deny:", 5))
469 num_options
= cupsAddOption("requesting-user-name-denied", val
+ 5, num_options
, &options
);
472 _cupsLangPrintf(stderr
, _("lpadmin: Unknown allow/deny option \"%s\"."), val
);
477 case 'v' : /* Set the device-uri attribute */
480 num_options
= cupsAddOption("device-uri", opt
+ 1, num_options
, &options
);
481 opt
+= strlen(opt
) - 1;
489 _cupsLangPuts(stderr
, _("lpadmin: Expected device URI after \"-v\" option."));
493 num_options
= cupsAddOption("device-uri", argv
[i
], num_options
, &options
);
497 case 'x' : /* Delete a printer */
500 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
504 _cupsLangPrintf(stderr
,
505 _("lpadmin: Unable to connect to server: %s"),
514 opt
+= strlen(opt
) - 1;
522 _cupsLangPuts(stderr
, _("lpadmin: Expected printer or class after \"-x\" option."));
529 if (!validate_name(printer
))
531 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
535 if (delete_printer(http
, printer
))
541 case 'D' : /* Set the printer-info attribute */
544 num_options
= cupsAddOption("printer-info", opt
+ 1, num_options
, &options
);
545 opt
+= strlen(opt
) - 1;
553 _cupsLangPuts(stderr
, _("lpadmin: Expected description after \"-D\" option."));
557 num_options
= cupsAddOption("printer-info", argv
[i
], num_options
, &options
);
561 case 'I' : /* Set the supported file types (ignored) */
566 _cupsLangPuts(stderr
, _("lpadmin: Expected file type(s) after \"-I\" option."));
570 _cupsLangPuts(stderr
, _("lpadmin: Warning - content type list ignored."));
573 case 'L' : /* Set the printer-location attribute */
576 num_options
= cupsAddOption("printer-location", opt
+ 1, num_options
, &options
);
577 opt
+= strlen(opt
) - 1;
585 _cupsLangPuts(stderr
, _("lpadmin: Expected location after \"-L\" option."));
589 num_options
= cupsAddOption("printer-location", argv
[i
], num_options
, &options
);
594 _cupsLangPrintf(stderr
, _("lpadmin: Unknown option \"%c\"."), *opt
);
601 _cupsLangPrintf(stderr
, _("lpadmin: Unknown argument \"%s\"."), argv
[i
]);
607 * Set options as needed...
610 ppd_name
= cupsGetOption("ppd-name", num_options
, options
);
611 device_uri
= cupsGetOption("device-uri", num_options
, options
);
613 if (ppd_name
&& !strcmp(ppd_name
, "raw"))
616 _cupsLangPuts(stderr
, _("lpadmin: Raw queues are no longer supported on macOS."));
618 _cupsLangPuts(stderr
, _("lpadmin: Raw queues are deprecated and will stop working in a future version of CUPS."));
619 #endif /* __APPLE__ */
621 if (device_uri
&& (!strncmp(device_uri
, "ipp://", 6) || !strncmp(device_uri
, "ipps://", 7)) && strstr(device_uri
, "/printers/"))
622 _cupsLangPuts(stderr
, _("lpadmin: Use the 'everywhere' model for shared printers."));
626 #endif /* __APPLE__ */
628 else if ((ppd_name
&& strcmp(ppd_name
, "everywhere")) || file
)
630 _cupsLangPuts(stderr
, _("lpadmin: Printer drivers are deprecated and will stop working in a future version of CUPS."));
633 if (num_options
|| file
)
637 _cupsLangPuts(stderr
,
638 _("lpadmin: Unable to set the printer options:\n"
639 " You must specify a printer name first."));
645 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
646 cupsEncryption(), 1, 30000, NULL
);
649 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"),
655 if (set_printer_options(http
, printer
, num_options
, options
, file
, enable
))
658 else if (enable
&& enable_printer(http
, printer
))
675 * 'add_printer_to_class()' - Add a printer to a class.
678 static int /* O - 0 on success, 1 on fail */
679 add_printer_to_class(http_t
*http
, /* I - Server connection */
680 char *printer
, /* I - Printer to add */
681 char *pclass
) /* I - Class to add to */
683 int i
; /* Looping var */
684 ipp_t
*request
, /* IPP Request */
685 *response
; /* IPP Response */
686 ipp_attribute_t
*attr
, /* Current attribute */
687 *members
; /* Members in class */
688 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
692 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
696 * attributes-natural-language
698 * requesting-user-name
701 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
703 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
704 "localhost", 0, "/classes/%s", pclass
);
705 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
706 "printer-uri", NULL
, uri
);
707 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
711 * Do the request and get back a response...
714 response
= cupsDoRequest(http
, request
, "/");
717 * Build a CUPS-Add-Modify-Class request, which requires the following
721 * attributes-natural-language
723 * requesting-user-name
727 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
729 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
730 "printer-uri", NULL
, uri
);
731 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
735 * See if the printer is already in the class...
738 if (response
!= NULL
&&
739 (members
= ippFindAttribute(response
, "member-names",
740 IPP_TAG_NAME
)) != NULL
)
741 for (i
= 0; i
< members
->num_values
; i
++)
742 if (_cups_strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
744 _cupsLangPrintf(stderr
,
745 _("lpadmin: Printer %s is already a member of class "
746 "%s."), printer
, pclass
);
753 * OK, the printer isn't part of the class, so add it...
756 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
757 "localhost", 0, "/printers/%s", printer
);
759 if (response
!= NULL
&&
760 (members
= ippFindAttribute(response
, "member-uris",
761 IPP_TAG_URI
)) != NULL
)
764 * Add the printer to the existing list...
767 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
768 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
769 for (i
= 0; i
< members
->num_values
; i
++)
770 attr
->values
[i
].string
.text
=
771 _cupsStrAlloc(members
->values
[i
].string
.text
);
773 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
776 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
780 * Then send the request...
785 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
786 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
788 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
798 * 'default_printer()' - Set the default printing destination.
801 static int /* O - 0 on success, 1 on fail */
802 default_printer(http_t
*http
, /* I - Server connection */
803 char *printer
) /* I - Printer name */
805 ipp_t
*request
; /* IPP Request */
806 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
810 * Build a CUPS-Set-Default request, which requires the following
814 * attributes-natural-language
816 * requesting-user-name
819 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
820 "localhost", 0, "/printers/%s", printer
);
822 request
= ippNewRequest(IPP_OP_CUPS_SET_DEFAULT
);
824 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
825 "printer-uri", NULL
, uri
);
826 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
830 * Do the request and get back a response...
833 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
835 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
837 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
847 * 'delete_printer()' - Delete a printer from the system...
850 static int /* O - 0 on success, 1 on fail */
851 delete_printer(http_t
*http
, /* I - Server connection */
852 char *printer
) /* I - Printer to delete */
854 ipp_t
*request
; /* IPP Request */
855 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
859 * Build a CUPS-Delete-Printer request, which requires the following
863 * attributes-natural-language
865 * requesting-user-name
868 request
= ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER
);
870 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
871 "localhost", 0, "/printers/%s", printer
);
872 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
873 "printer-uri", NULL
, uri
);
874 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
878 * Do the request and get back a response...
881 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
883 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
885 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
895 * 'delete_printer_from_class()' - Delete a printer from a class.
898 static int /* O - 0 on success, 1 on fail */
899 delete_printer_from_class(
900 http_t
*http
, /* I - Server connection */
901 char *printer
, /* I - Printer to remove */
902 char *pclass
) /* I - Class to remove from */
904 int i
, j
, k
; /* Looping vars */
905 ipp_t
*request
, /* IPP Request */
906 *response
; /* IPP Response */
907 ipp_attribute_t
*attr
, /* Current attribute */
908 *members
; /* Members in class */
909 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
913 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
917 * attributes-natural-language
919 * requesting-user-name
922 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
924 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
925 "localhost", 0, "/classes/%s", pclass
);
926 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
927 "printer-uri", NULL
, uri
);
928 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
932 * Do the request and get back a response...
935 if ((response
= cupsDoRequest(http
, request
, "/classes/")) == NULL
||
936 response
->request
.status
.status_code
== IPP_STATUS_ERROR_NOT_FOUND
)
938 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
946 * See if the printer is already in the class...
949 if ((members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) == NULL
)
951 _cupsLangPuts(stderr
, _("lpadmin: No member names were seen."));
958 for (i
= 0; i
< members
->num_values
; i
++)
959 if (!_cups_strcasecmp(printer
, members
->values
[i
].string
.text
))
962 if (i
>= members
->num_values
)
964 _cupsLangPrintf(stderr
,
965 _("lpadmin: Printer %s is not a member of class %s."),
973 if (members
->num_values
== 1)
976 * Build a CUPS-Delete-Class request, which requires the following
980 * attributes-natural-language
982 * requesting-user-name
985 request
= ippNewRequest(IPP_OP_CUPS_DELETE_CLASS
);
987 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
988 "printer-uri", NULL
, uri
);
989 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
990 "requesting-user-name", NULL
, cupsUser());
995 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
999 * attributes-natural-language
1001 * requesting-user-name
1005 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1007 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1008 "printer-uri", NULL
, uri
);
1009 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1010 "requesting-user-name", NULL
, cupsUser());
1013 * Delete the printer from the class...
1016 members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
);
1017 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1018 "member-uris", members
->num_values
- 1, NULL
, NULL
);
1020 for (j
= 0, k
= 0; j
< members
->num_values
; j
++)
1022 attr
->values
[k
++].string
.text
=
1023 _cupsStrAlloc(members
->values
[j
].string
.text
);
1027 * Then send the request...
1030 ippDelete(response
);
1032 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1034 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1036 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1046 * 'delete_printer_option()' - Delete a printer option.
1049 static int /* O - 0 on success, 1 on fail */
1050 delete_printer_option(http_t
*http
, /* I - Server connection */
1051 char *printer
, /* I - Printer */
1052 char *option
) /* I - Option to delete */
1054 ipp_t
*request
; /* IPP request */
1055 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1059 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1060 * requires the following attributes:
1062 * attributes-charset
1063 * attributes-natural-language
1065 * requesting-user-name
1066 * option with deleteAttr tag
1069 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1070 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1072 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1074 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1075 "printer-uri", NULL
, uri
);
1076 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1077 "requesting-user-name", NULL
, cupsUser());
1078 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_DELETEATTR
, option
, 0);
1081 * Do the request and get back a response...
1084 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1086 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1088 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1098 * 'enable_printer()' - Enable a printer...
1101 static int /* O - 0 on success, 1 on fail */
1102 enable_printer(http_t
*http
, /* I - Server connection */
1103 char *printer
) /* I - Printer to enable */
1105 ipp_t
*request
; /* IPP Request */
1106 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1110 * Send IPP_OP_ENABLE_PRINTER and IPP_OP_RESUME_PRINTER requests, which
1111 * require the following attributes:
1113 * attributes-charset
1114 * attributes-natural-language
1116 * requesting-user-name
1119 request
= ippNewRequest(IPP_OP_ENABLE_PRINTER
);
1121 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1122 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1123 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1125 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1127 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1129 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1134 request
= ippNewRequest(IPP_OP_RESUME_PRINTER
);
1136 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1137 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1139 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1141 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1143 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1153 * 'get_printer_type()' - Determine the printer type and URI.
1156 static cups_ptype_t
/* O - printer-type value */
1157 get_printer_type(http_t
*http
, /* I - Server connection */
1158 char *printer
, /* I - Printer name */
1159 char *uri
, /* I - URI buffer */
1160 size_t urisize
) /* I - Size of URI buffer */
1162 ipp_t
*request
, /* IPP request */
1163 *response
; /* IPP response */
1164 ipp_attribute_t
*attr
; /* printer-type attribute */
1165 cups_ptype_t type
; /* printer-type value */
1169 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1172 * attributes-charset
1173 * attributes-natural-language
1175 * requested-attributes
1176 * requesting-user-name
1179 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1181 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1182 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1183 "printer-uri", NULL
, uri
);
1184 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1185 "requested-attributes", NULL
, "printer-type");
1186 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1187 "requesting-user-name", NULL
, cupsUser());
1193 response
= cupsDoRequest(http
, request
, "/");
1194 if ((attr
= ippFindAttribute(response
, "printer-type",
1195 IPP_TAG_ENUM
)) != NULL
)
1197 type
= (cups_ptype_t
)attr
->values
[0].integer
;
1199 if (type
& CUPS_PRINTER_CLASS
)
1200 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/classes/%s", printer
);
1203 type
= CUPS_PRINTER_LOCAL
;
1205 ippDelete(response
);
1212 * 'set_printer_options()' - Set the printer options.
1215 static int /* O - 0 on success, 1 on fail */
1216 set_printer_options(
1217 http_t
*http
, /* I - Server connection */
1218 char *printer
, /* I - Printer */
1219 int num_options
, /* I - Number of options */
1220 cups_option_t
*options
, /* I - Options */
1221 char *file
, /* I - PPD file */
1222 int enable
) /* I - Enable printer? */
1224 ipp_t
*request
; /* IPP Request */
1225 const char *ppdfile
; /* PPD filename */
1226 int ppdchanged
= 0; /* PPD changed? */
1227 ppd_file_t
*ppd
; /* PPD file */
1228 ppd_choice_t
*choice
; /* Marked choice */
1229 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1230 line
[1024], /* Line from PPD file */
1231 keyword
[1024], /* Keyword from Default line */
1232 *keyptr
, /* Pointer into keyword... */
1233 tempfile
[1024]; /* Temporary filename */
1234 cups_file_t
*in
, /* PPD file */
1235 *out
; /* Temporary file */
1236 const char *ppdname
, /* ppd-name value */
1237 *protocol
, /* Old protocol option */
1238 *customval
, /* Custom option value */
1239 *boolval
; /* Boolean value */
1240 int wrote_ipp_supplies
= 0, /* Wrote cupsIPPSupplies keyword? */
1241 wrote_snmp_supplies
= 0,/* Wrote cupsSNMPSupplies keyword? */
1242 copied_options
= 0; /* Copied options? */
1246 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1247 * which requires the following attributes:
1249 * attributes-charset
1250 * attributes-natural-language
1252 * requesting-user-name
1256 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1257 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1259 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1261 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1262 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1265 * Add the options...
1270 else if ((ppdname
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& strcmp(ppdname
, "everywhere") && strcmp(ppdname
, "raw") && num_options
> 1)
1272 if ((ppdfile
= cupsGetServerPPD(http
, ppdname
)) != NULL
)
1275 * Copy options array and remove ppd-name from it...
1278 cups_option_t
*temp
= NULL
, *optr
;
1279 int i
, num_temp
= 0;
1280 for (i
= num_options
, optr
= options
; i
> 0; i
--, optr
++)
1281 if (strcmp(optr
->name
, "ppd-name"))
1282 num_temp
= cupsAddOption(optr
->name
, optr
->value
, num_temp
, &temp
);
1286 num_options
= num_temp
;
1290 else if (request
->request
.op
.operation_id
== IPP_OP_CUPS_ADD_MODIFY_PRINTER
)
1291 ppdfile
= cupsGetPPD(printer
);
1295 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
1299 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state", IPP_PSTATE_IDLE
);
1300 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1303 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1305 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1307 if (!_cups_strcasecmp(protocol
, "bcp"))
1308 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1310 else if (!_cups_strcasecmp(protocol
, "tbcp"))
1311 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1318 * Set default options in the PPD file...
1321 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
1323 int linenum
; /* Line number of error */
1324 ppd_status_t status
= ppdLastError(&linenum
);
1327 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile
, ppdErrorString(status
), linenum
);
1331 ppdMarkDefaults(ppd
);
1332 cupsMarkOptions(ppd
, num_options
, options
);
1334 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1336 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1338 if (ppdfile
!= file
)
1341 cupsFreeOptions(num_options
, options
);
1345 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1347 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s"), ppdfile
, strerror(errno
));
1349 if (ppdfile
!= file
)
1352 cupsFreeOptions(num_options
, options
);
1358 while (cupsFileGets(in
, line
, sizeof(line
)))
1360 if (!strncmp(line
, "*cupsIPPSupplies:", 17) &&
1361 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1365 wrote_ipp_supplies
= 1;
1366 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1367 (!_cups_strcasecmp(boolval
, "true") ||
1368 !_cups_strcasecmp(boolval
, "yes") ||
1369 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1371 else if (!strncmp(line
, "*cupsSNMPSupplies:", 18) &&
1372 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1376 wrote_snmp_supplies
= 1;
1377 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1378 (!_cups_strcasecmp(boolval
, "true") ||
1379 !_cups_strcasecmp(boolval
, "yes") ||
1380 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1382 else if (strncmp(line
, "*Default", 8))
1383 cupsFilePrintf(out
, "%s\n", line
);
1387 * Get default option name...
1390 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
1392 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1393 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1397 while (isspace(*keyptr
& 255))
1400 if (!strcmp(keyword
, "PageRegion") ||
1401 !strcmp(keyword
, "PageSize") ||
1402 !strcmp(keyword
, "PaperDimension") ||
1403 !strcmp(keyword
, "ImageableArea"))
1405 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1406 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1409 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1411 if (choice
&& strcmp(choice
->choice
, keyptr
))
1413 if (strcmp(choice
->choice
, "Custom"))
1415 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1418 else if ((customval
= cupsGetOption(keyword
, num_options
,
1421 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1425 cupsFilePrintf(out
, "%s\n", line
);
1428 cupsFilePrintf(out
, "%s\n", line
);
1432 if (!wrote_ipp_supplies
&&
1433 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1438 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1439 (!_cups_strcasecmp(boolval
, "true") ||
1440 !_cups_strcasecmp(boolval
, "yes") ||
1441 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1444 if (!wrote_snmp_supplies
&&
1445 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1450 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1451 (!_cups_strcasecmp(boolval
, "true") ||
1452 !_cups_strcasecmp(boolval
, "yes") ||
1453 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1464 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", ppdchanged
? tempfile
: file
));
1467 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1471 if (ppdfile
!= file
)
1478 * No PPD file - just set the options...
1481 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1485 cupsFreeOptions(num_options
, options
);
1488 * Check the response...
1491 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1493 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1503 * 'usage()' - Show program usage and exit.
1509 _cupsLangPuts(stdout
, _("Usage: lpadmin [options] -d destination\n"
1510 " lpadmin [options] -p destination\n"
1511 " lpadmin [options] -p destination -c class\n"
1512 " lpadmin [options] -p destination -r class\n"
1513 " lpadmin [options] -x destination"));
1514 _cupsLangPuts(stdout
, _("Options:"));
1515 _cupsLangPuts(stdout
, _("-c class Add the named destination to a class"));
1516 _cupsLangPuts(stdout
, _("-d destination Set the named destination as the server default"));
1517 _cupsLangPuts(stdout
, _("-D description Specify the textual description of the printer"));
1518 _cupsLangPuts(stdout
, _("-E Encrypt the connection to the server"));
1519 _cupsLangPuts(stdout
, _("-E Enable and accept jobs on the printer (after -p)"));
1520 _cupsLangPuts(stdout
, _("-h server[:port] Connect to the named server and port"));
1521 _cupsLangPuts(stdout
, _("-i ppd-file Specify a PPD file for the printer"));
1522 _cupsLangPuts(stdout
, _("-L location Specify the textual location of the printer"));
1523 _cupsLangPuts(stdout
, _("-m model Specify a standard model/PPD file for the printer"));
1524 _cupsLangPuts(stdout
, _("-m everywhere Specify the printer is compatible with IPP Everywhere"));
1525 _cupsLangPuts(stdout
, _("-o name-default=value Specify the default value for the named option"));
1526 _cupsLangPuts(stdout
, _("-o Name=Value Specify the default value for the named PPD option "));
1527 _cupsLangPuts(stdout
, _("-o cupsIPPSupplies=false\n"
1528 " Disable supply level reporting via IPP"));
1529 _cupsLangPuts(stdout
, _("-o cupsSNMPSupplies=false\n"
1530 " Disable supply level reporting via SNMP"));
1531 _cupsLangPuts(stdout
, _("-o job-k-limit=N Specify the kilobyte limit for per-user quotas"));
1532 _cupsLangPuts(stdout
, _("-o job-page-limit=N Specify the page limit for per-user quotas"));
1533 _cupsLangPuts(stdout
, _("-o job-quota-period=N Specify the per-user quota period in seconds"));
1534 _cupsLangPuts(stdout
, _("-o printer-error-policy=name\n"
1535 " Specify the printer error policy"));
1536 _cupsLangPuts(stdout
, _("-o printer-is-shared=true\n"
1537 " Share the printer"));
1538 _cupsLangPuts(stdout
, _("-o printer-op-policy=name\n"
1539 " Specify the printer operation policy"));
1540 _cupsLangPuts(stdout
, _("-p destination Specify/add the named destination"));
1541 _cupsLangPuts(stdout
, _("-r class Remove the named destination from a class"));
1542 _cupsLangPuts(stdout
, _("-R name-default Remove the default value for the named option"));
1543 _cupsLangPuts(stdout
, _("-u allow:all Allow all users to print"));
1544 _cupsLangPuts(stdout
, _("-u allow:list Allow the list of users or groups (@name) to print"));
1545 _cupsLangPuts(stdout
, _("-u deny:list Prevent the list of users or groups (@name) to print"));
1546 _cupsLangPuts(stdout
, _("-U username Specify the username to use for authentication"));
1547 _cupsLangPuts(stdout
, _("-v device-uri Specify the device URI for the printer"));
1548 _cupsLangPuts(stdout
, _("-x destination Remove the named destination"));
1555 * 'validate_name()' - Make sure the printer name only contains valid chars.
1558 static int /* O - 0 if name is no good, 1 if name is good */
1559 validate_name(const char *name
) /* I - Name to check */
1561 const char *ptr
; /* Pointer into name */
1565 * Scan the whole name...
1568 for (ptr
= name
; *ptr
; ptr
++)
1571 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '\\' || *ptr
== '?' || *ptr
== '\'' || *ptr
== '\"' || *ptr
== '#')
1575 * All the characters are good; validate the length, too...
1578 return ((ptr
- name
) < 128);