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
12 * Include necessary headers...
15 #include <cups/cups-private.h>
16 #include <cups/ppd-private.h>
23 static int add_printer_to_class(http_t
*http
, char *printer
, char *pclass
);
24 static int default_printer(http_t
*http
, char *printer
);
25 static int delete_printer(http_t
*http
, char *printer
);
26 static int delete_printer_from_class(http_t
*http
, char *printer
,
28 static int delete_printer_option(http_t
*http
, char *printer
,
30 static int enable_printer(http_t
*http
, char *printer
);
31 static char *get_printer_ppd(const char *uri
, char *buffer
, size_t bufsize
, int *num_options
, cups_option_t
**options
);
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_SSL */
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") && device_uri
)
630 if ((file
= get_printer_ppd(device_uri
, evefile
, sizeof(evefile
), &num_options
, &options
)) == NULL
)
633 num_options
= cupsRemoveOption("ppd-name", num_options
, &options
);
635 else if (ppd_name
|| file
)
637 _cupsLangPuts(stderr
, _("lpadmin: Printer drivers are deprecated and will stop working in a future version of CUPS."));
640 if (num_options
|| file
)
644 _cupsLangPuts(stderr
,
645 _("lpadmin: Unable to set the printer options:\n"
646 " You must specify a printer name first."));
652 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
653 cupsEncryption(), 1, 30000, NULL
);
656 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"),
662 if (set_printer_options(http
, printer
, num_options
, options
, file
, enable
))
665 else if (enable
&& enable_printer(http
, printer
))
673 _cupsLangPuts(stdout
,
676 " lpadmin [-h server] -d destination\n"
677 " lpadmin [-h server] -x destination\n"
678 " lpadmin [-h server] -p printer [-c add-class] "
679 "[-i interface] [-m model]\n"
680 " [-r remove-class] [-v device] "
682 " [-P ppd-file] [-o name=value]\n"
683 " [-u allow:user,user] "
684 "[-u deny:user,user]"));
695 * 'add_printer_to_class()' - Add a printer to a class.
698 static int /* O - 0 on success, 1 on fail */
699 add_printer_to_class(http_t
*http
, /* I - Server connection */
700 char *printer
, /* I - Printer to add */
701 char *pclass
) /* I - Class to add to */
703 int i
; /* Looping var */
704 ipp_t
*request
, /* IPP Request */
705 *response
; /* IPP Response */
706 ipp_attribute_t
*attr
, /* Current attribute */
707 *members
; /* Members in class */
708 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
712 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
716 * attributes-natural-language
718 * requesting-user-name
721 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
723 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
724 "localhost", 0, "/classes/%s", pclass
);
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",
731 * Do the request and get back a response...
734 response
= cupsDoRequest(http
, request
, "/");
737 * Build a CUPS-Add-Modify-Class request, which requires the following
741 * attributes-natural-language
743 * requesting-user-name
747 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
749 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
750 "printer-uri", NULL
, uri
);
751 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
755 * See if the printer is already in the class...
758 if (response
!= NULL
&&
759 (members
= ippFindAttribute(response
, "member-names",
760 IPP_TAG_NAME
)) != NULL
)
761 for (i
= 0; i
< members
->num_values
; i
++)
762 if (_cups_strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
764 _cupsLangPrintf(stderr
,
765 _("lpadmin: Printer %s is already a member of class "
766 "%s."), printer
, pclass
);
773 * OK, the printer isn't part of the class, so add it...
776 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
777 "localhost", 0, "/printers/%s", printer
);
779 if (response
!= NULL
&&
780 (members
= ippFindAttribute(response
, "member-uris",
781 IPP_TAG_URI
)) != NULL
)
784 * Add the printer to the existing list...
787 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
788 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
789 for (i
= 0; i
< members
->num_values
; i
++)
790 attr
->values
[i
].string
.text
=
791 _cupsStrAlloc(members
->values
[i
].string
.text
);
793 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
796 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
800 * Then send the request...
805 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
806 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
808 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
818 * 'default_printer()' - Set the default printing destination.
821 static int /* O - 0 on success, 1 on fail */
822 default_printer(http_t
*http
, /* I - Server connection */
823 char *printer
) /* I - Printer name */
825 ipp_t
*request
; /* IPP Request */
826 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
830 * Build a CUPS-Set-Default request, which requires the following
834 * attributes-natural-language
836 * requesting-user-name
839 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
840 "localhost", 0, "/printers/%s", printer
);
842 request
= ippNewRequest(IPP_OP_CUPS_SET_DEFAULT
);
844 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
845 "printer-uri", NULL
, uri
);
846 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
850 * Do the request and get back a response...
853 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
855 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
857 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
867 * 'delete_printer()' - Delete a printer from the system...
870 static int /* O - 0 on success, 1 on fail */
871 delete_printer(http_t
*http
, /* I - Server connection */
872 char *printer
) /* I - Printer to delete */
874 ipp_t
*request
; /* IPP Request */
875 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
879 * Build a CUPS-Delete-Printer request, which requires the following
883 * attributes-natural-language
885 * requesting-user-name
888 request
= ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER
);
890 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
891 "localhost", 0, "/printers/%s", printer
);
892 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
893 "printer-uri", NULL
, uri
);
894 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
898 * Do the request and get back a response...
901 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
903 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
905 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
915 * 'delete_printer_from_class()' - Delete a printer from a class.
918 static int /* O - 0 on success, 1 on fail */
919 delete_printer_from_class(
920 http_t
*http
, /* I - Server connection */
921 char *printer
, /* I - Printer to remove */
922 char *pclass
) /* I - Class to remove from */
924 int i
, j
, k
; /* Looping vars */
925 ipp_t
*request
, /* IPP Request */
926 *response
; /* IPP Response */
927 ipp_attribute_t
*attr
, /* Current attribute */
928 *members
; /* Members in class */
929 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
933 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
937 * attributes-natural-language
939 * requesting-user-name
942 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
944 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
945 "localhost", 0, "/classes/%s", pclass
);
946 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
947 "printer-uri", NULL
, uri
);
948 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
952 * Do the request and get back a response...
955 if ((response
= cupsDoRequest(http
, request
, "/classes/")) == NULL
||
956 response
->request
.status
.status_code
== IPP_STATUS_ERROR_NOT_FOUND
)
958 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
966 * See if the printer is already in the class...
969 if ((members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) == NULL
)
971 _cupsLangPuts(stderr
, _("lpadmin: No member names were seen."));
978 for (i
= 0; i
< members
->num_values
; i
++)
979 if (!_cups_strcasecmp(printer
, members
->values
[i
].string
.text
))
982 if (i
>= members
->num_values
)
984 _cupsLangPrintf(stderr
,
985 _("lpadmin: Printer %s is not a member of class %s."),
993 if (members
->num_values
== 1)
996 * Build a CUPS-Delete-Class request, which requires the following
1000 * attributes-natural-language
1002 * requesting-user-name
1005 request
= ippNewRequest(IPP_OP_CUPS_DELETE_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());
1015 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
1018 * attributes-charset
1019 * attributes-natural-language
1021 * requesting-user-name
1025 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1027 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1028 "printer-uri", NULL
, uri
);
1029 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1030 "requesting-user-name", NULL
, cupsUser());
1033 * Delete the printer from the class...
1036 members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
);
1037 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1038 "member-uris", members
->num_values
- 1, NULL
, NULL
);
1040 for (j
= 0, k
= 0; j
< members
->num_values
; j
++)
1042 attr
->values
[k
++].string
.text
=
1043 _cupsStrAlloc(members
->values
[j
].string
.text
);
1047 * Then send the request...
1050 ippDelete(response
);
1052 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1054 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1056 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1066 * 'delete_printer_option()' - Delete a printer option.
1069 static int /* O - 0 on success, 1 on fail */
1070 delete_printer_option(http_t
*http
, /* I - Server connection */
1071 char *printer
, /* I - Printer */
1072 char *option
) /* I - Option to delete */
1074 ipp_t
*request
; /* IPP request */
1075 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1079 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1080 * requires the following attributes:
1082 * attributes-charset
1083 * attributes-natural-language
1085 * requesting-user-name
1086 * option with deleteAttr tag
1089 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1090 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1092 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1094 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1095 "printer-uri", NULL
, uri
);
1096 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1097 "requesting-user-name", NULL
, cupsUser());
1098 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_DELETEATTR
, option
, 0);
1101 * Do the request and get back a response...
1104 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1106 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1108 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1118 * 'enable_printer()' - Enable a printer...
1121 static int /* O - 0 on success, 1 on fail */
1122 enable_printer(http_t
*http
, /* I - Server connection */
1123 char *printer
) /* I - Printer to enable */
1125 ipp_t
*request
; /* IPP Request */
1126 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1130 * Send IPP_OP_ENABLE_PRINTER and IPP_OP_RESUME_PRINTER requests, which
1131 * require the following attributes:
1133 * attributes-charset
1134 * attributes-natural-language
1136 * requesting-user-name
1139 request
= ippNewRequest(IPP_OP_ENABLE_PRINTER
);
1141 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1142 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1144 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1146 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1148 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1153 request
= ippNewRequest(IPP_OP_RESUME_PRINTER
);
1155 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1156 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1158 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1160 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1162 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1172 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
1175 static char * /* O - Filename or NULL */
1177 const char *uri
, /* I - Printer URI */
1178 char *buffer
, /* I - Filename buffer */
1179 size_t bufsize
, /* I - Size of filename buffer */
1180 int *num_options
, /* IO - Number of options */
1181 cups_option_t
**options
) /* IO - Options */
1183 http_t
*http
; /* Connection to printer */
1184 ipp_t
*request
, /* Get-Printer-Attributes request */
1185 *response
; /* Get-Printer-Attributes response */
1186 ipp_attribute_t
*attr
; /* Attribute from response */
1187 char resolved
[1024], /* Resolved URI */
1188 scheme
[32], /* URI scheme */
1189 userpass
[256], /* Username:password */
1190 host
[256], /* Hostname */
1191 resource
[256]; /* Resource path */
1192 int port
; /* Port number */
1193 static const char * const pattrs
[] = /* Attributes to use */
1197 "printer-description",
1198 "media-col-database"
1203 * Connect to the printer...
1206 if (strstr(uri
, "._tcp"))
1212 if (!_httpResolveURI(uri
, resolved
, sizeof(resolved
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
1214 _cupsLangPrintf(stderr
, _("%s: Unable to resolve \"%s\"."), "lpadmin", uri
);
1221 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
1223 _cupsLangPrintf(stderr
, _("%s: Bad printer URI \"%s\"."), "lpadmin", uri
);
1227 http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, !strcmp(scheme
, "ipps") ? HTTP_ENCRYPTION_ALWAYS
: HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
);
1230 _cupsLangPrintf(stderr
, _("%s: Unable to connect to \"%s:%d\": %s"), "lpadmin", host
, port
, cupsLastErrorString());
1235 * Send a Get-Printer-Attributes request...
1238 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1239 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1240 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]), NULL
, pattrs
);
1241 response
= cupsDoRequest(http
, request
, resource
);
1243 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
1245 _cupsLangPrintf(stderr
, _("%s: Unable to query printer: %s"), "lpadmin", cupsLastErrorString());
1248 else if (_ppdCreateFromIPP(buffer
, bufsize
, response
))
1250 if (!cupsGetOption("printer-geo-location", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-geo-location", IPP_TAG_URI
)) != NULL
)
1251 *num_options
= cupsAddOption("printer-geo-location", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1253 if (!cupsGetOption("printer-info", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-info", IPP_TAG_TEXT
)) != NULL
)
1254 *num_options
= cupsAddOption("printer-info", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1256 if (!cupsGetOption("printer-location", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-location", IPP_TAG_TEXT
)) != NULL
)
1257 *num_options
= cupsAddOption("printer-location", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1260 _cupsLangPrintf(stderr
, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno
));
1262 ippDelete(response
);
1273 * 'get_printer_type()' - Determine the printer type and URI.
1276 static cups_ptype_t
/* O - printer-type value */
1277 get_printer_type(http_t
*http
, /* I - Server connection */
1278 char *printer
, /* I - Printer name */
1279 char *uri
, /* I - URI buffer */
1280 size_t urisize
) /* I - Size of URI buffer */
1282 ipp_t
*request
, /* IPP request */
1283 *response
; /* IPP response */
1284 ipp_attribute_t
*attr
; /* printer-type attribute */
1285 cups_ptype_t type
; /* printer-type value */
1289 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1292 * attributes-charset
1293 * attributes-natural-language
1295 * requested-attributes
1296 * requesting-user-name
1299 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1301 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1302 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1303 "printer-uri", NULL
, uri
);
1304 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1305 "requested-attributes", NULL
, "printer-type");
1306 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1307 "requesting-user-name", NULL
, cupsUser());
1313 response
= cupsDoRequest(http
, request
, "/");
1314 if ((attr
= ippFindAttribute(response
, "printer-type",
1315 IPP_TAG_ENUM
)) != NULL
)
1317 type
= (cups_ptype_t
)attr
->values
[0].integer
;
1319 if (type
& CUPS_PRINTER_CLASS
)
1320 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/classes/%s", printer
);
1323 type
= CUPS_PRINTER_LOCAL
;
1325 ippDelete(response
);
1332 * 'set_printer_options()' - Set the printer options.
1335 static int /* O - 0 on success, 1 on fail */
1336 set_printer_options(
1337 http_t
*http
, /* I - Server connection */
1338 char *printer
, /* I - Printer */
1339 int num_options
, /* I - Number of options */
1340 cups_option_t
*options
, /* I - Options */
1341 char *file
, /* I - PPD file */
1342 int enable
) /* I - Enable printer? */
1344 ipp_t
*request
; /* IPP Request */
1345 const char *ppdfile
; /* PPD filename */
1346 int ppdchanged
= 0; /* PPD changed? */
1347 ppd_file_t
*ppd
; /* PPD file */
1348 ppd_choice_t
*choice
; /* Marked choice */
1349 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1350 line
[1024], /* Line from PPD file */
1351 keyword
[1024], /* Keyword from Default line */
1352 *keyptr
, /* Pointer into keyword... */
1353 tempfile
[1024]; /* Temporary filename */
1354 cups_file_t
*in
, /* PPD file */
1355 *out
; /* Temporary file */
1356 const char *ppdname
, /* ppd-name value */
1357 *protocol
, /* Old protocol option */
1358 *customval
, /* Custom option value */
1359 *boolval
; /* Boolean value */
1360 int wrote_ipp_supplies
= 0, /* Wrote cupsIPPSupplies keyword? */
1361 wrote_snmp_supplies
= 0,/* Wrote cupsSNMPSupplies keyword? */
1362 copied_options
= 0; /* Copied options? */
1366 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1367 * which requires the following attributes:
1369 * attributes-charset
1370 * attributes-natural-language
1372 * requesting-user-name
1376 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1377 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1379 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1381 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1382 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1385 * Add the options...
1390 else if ((ppdname
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& strcmp(ppdname
, "raw") && num_options
> 1)
1392 if ((ppdfile
= cupsGetServerPPD(http
, ppdname
)) != NULL
)
1395 * Copy options array and remove ppd-name from it...
1398 cups_option_t
*temp
= NULL
, *optr
;
1399 int i
, num_temp
= 0;
1400 for (i
= num_options
, optr
= options
; i
> 0; i
--, optr
++)
1401 if (strcmp(optr
->name
, "ppd-name"))
1402 num_temp
= cupsAddOption(optr
->name
, optr
->value
, num_temp
, &temp
);
1406 num_options
= num_temp
;
1410 else if (request
->request
.op
.operation_id
== IPP_OP_CUPS_ADD_MODIFY_PRINTER
)
1411 ppdfile
= cupsGetPPD(printer
);
1415 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
1419 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state", IPP_PSTATE_IDLE
);
1420 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1423 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1425 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1427 if (!_cups_strcasecmp(protocol
, "bcp"))
1428 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1430 else if (!_cups_strcasecmp(protocol
, "tbcp"))
1431 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1438 * Set default options in the PPD file...
1441 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
1443 int linenum
; /* Line number of error */
1444 ppd_status_t status
= ppdLastError(&linenum
);
1447 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile
, ppdErrorString(status
), linenum
);
1450 ppdMarkDefaults(ppd
);
1451 cupsMarkOptions(ppd
, num_options
, options
);
1453 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1455 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1457 if (ppdfile
!= file
)
1460 cupsFreeOptions(num_options
, options
);
1464 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1466 _cupsLangPrintf(stderr
,
1467 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1468 ppdfile
, strerror(errno
));
1470 if (ppdfile
!= file
)
1473 cupsFreeOptions(num_options
, options
);
1479 while (cupsFileGets(in
, line
, sizeof(line
)))
1481 if (!strncmp(line
, "*cupsIPPSupplies:", 17) &&
1482 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1485 wrote_ipp_supplies
= 1;
1486 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1487 (!_cups_strcasecmp(boolval
, "true") ||
1488 !_cups_strcasecmp(boolval
, "yes") ||
1489 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1491 else if (!strncmp(line
, "*cupsSNMPSupplies:", 18) &&
1492 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1495 wrote_snmp_supplies
= 1;
1496 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1497 (!_cups_strcasecmp(boolval
, "true") ||
1498 !_cups_strcasecmp(boolval
, "yes") ||
1499 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1501 else if (strncmp(line
, "*Default", 8))
1502 cupsFilePrintf(out
, "%s\n", line
);
1506 * Get default option name...
1509 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
1511 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1512 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1516 while (isspace(*keyptr
& 255))
1519 if (!strcmp(keyword
, "PageRegion") ||
1520 !strcmp(keyword
, "PageSize") ||
1521 !strcmp(keyword
, "PaperDimension") ||
1522 !strcmp(keyword
, "ImageableArea"))
1524 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1525 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1528 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1530 if (choice
&& strcmp(choice
->choice
, keyptr
))
1532 if (strcmp(choice
->choice
, "Custom"))
1534 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1537 else if ((customval
= cupsGetOption(keyword
, num_options
,
1540 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1544 cupsFilePrintf(out
, "%s\n", line
);
1547 cupsFilePrintf(out
, "%s\n", line
);
1551 if (!wrote_ipp_supplies
&&
1552 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1555 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1556 (!_cups_strcasecmp(boolval
, "true") ||
1557 !_cups_strcasecmp(boolval
, "yes") ||
1558 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1561 if (!wrote_snmp_supplies
&&
1562 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1565 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1566 (!_cups_strcasecmp(boolval
, "true") ||
1567 !_cups_strcasecmp(boolval
, "yes") ||
1568 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1579 ippDelete(cupsDoFileRequest(http
, request
, "/admin/",
1580 ppdchanged
? tempfile
: file
));
1583 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1587 if (ppdfile
!= file
)
1594 * No PPD file - just set the options...
1597 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1601 cupsFreeOptions(num_options
, options
);
1604 * Check the response...
1607 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1609 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1619 * 'usage()' - Show program usage and exit.
1625 _cupsLangPuts(stdout
, _("Usage: lpadmin [options] -d destination\n"
1626 " lpadmin [options] -p destination\n"
1627 " lpadmin [options] -p destination -c class\n"
1628 " lpadmin [options] -p destination -r class\n"
1629 " lpadmin [options] -x destination"));
1630 _cupsLangPuts(stdout
, _("Options:"));
1631 _cupsLangPuts(stdout
, _("-c class Add the named destination to a class"));
1632 _cupsLangPuts(stdout
, _("-d destination Set the named destination as the server default"));
1633 _cupsLangPuts(stdout
, _("-D description Specify the textual description of the printer"));
1634 _cupsLangPuts(stdout
, _("-E Encrypt the connection to the server"));
1635 _cupsLangPuts(stdout
, _("-E Enable and accept jobs on the printer (after -p)"));
1636 _cupsLangPuts(stdout
, _("-h server[:port] Connect to the named server and port"));
1637 _cupsLangPuts(stdout
, _("-i ppd-file Specify a PPD file for the printer"));
1638 _cupsLangPuts(stdout
, _("-L location Specify the textual location of the printer"));
1639 _cupsLangPuts(stdout
, _("-m model Specify a standard model/PPD file for the printer"));
1640 _cupsLangPuts(stdout
, _("-m everywhere Specify the printer is compatible with IPP Everywhere™"));
1641 _cupsLangPuts(stdout
, _("-o name-default=value Specify the default value for the named option"));
1642 _cupsLangPuts(stdout
, _("-o Name=Value Specify the default value for the named PPD option "));
1643 _cupsLangPuts(stdout
, _("-o cupsIPPSupplies=false\n"
1644 " Disable supply level reporting via IPP"));
1645 _cupsLangPuts(stdout
, _("-o cupsSNMPSupplies=false\n"
1646 " Disable supply level reporting via SNMP"));
1647 _cupsLangPuts(stdout
, _("-o job-k-limit=N Specify the kilobyte limit for per-user quotas"));
1648 _cupsLangPuts(stdout
, _("-o job-page-limit=N Specify the page limit for per-user quotas"));
1649 _cupsLangPuts(stdout
, _("-o job-quota-period=N Specify the per-user quota period in seconds"));
1650 _cupsLangPuts(stdout
, _("-o printer-error-policy=name\n"
1651 " Specify the printer error policy"));
1652 _cupsLangPuts(stdout
, _("-o printer-is-shared=true\n"
1653 " Share the printer"));
1654 _cupsLangPuts(stdout
, _("-o printer-op-policy=name\n"
1655 " Specify the printer operation policy"));
1656 _cupsLangPuts(stdout
, _("-p destination Specify/add the named destination"));
1657 _cupsLangPuts(stdout
, _("-r class Remove the named destination from a class"));
1658 _cupsLangPuts(stdout
, _("-R name-default Remove the default value for the named option"));
1659 _cupsLangPuts(stdout
, _("-u allow:all Allow all users to print"));
1660 _cupsLangPuts(stdout
, _("-u allow:list Allow the list of users or groups (@name) to print"));
1661 _cupsLangPuts(stdout
, _("-u deny:list Prevent the list of users or groups (@name) to print"));
1662 _cupsLangPuts(stdout
, _("-U username Specify the username to use for authentication"));
1663 _cupsLangPuts(stdout
, _("-v device-uri Specify the device URI for the printer"));
1664 _cupsLangPuts(stdout
, _("-x destination Remove the named destination"));
1671 * 'validate_name()' - Make sure the printer name only contains valid chars.
1674 static int /* O - 0 if name is no good, 1 if name is good */
1675 validate_name(const char *name
) /* I - Name to check */
1677 const char *ptr
; /* Pointer into name */
1681 * Scan the whole name...
1684 for (ptr
= name
; *ptr
; ptr
++)
1687 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '\\' || *ptr
== '?' || *ptr
== '\'' || *ptr
== '\"' || *ptr
== '#')
1691 * All the characters are good; validate the length, too...
1694 return ((ptr
- name
) < 128);