2 * "lpadmin" command for CUPS.
4 * Copyright © 2007-2019 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
18 #define _CUPS_NO_DEPRECATED
19 #define _PPD_DEPRECATED
20 #include <cups/cups-private.h>
21 #include <cups/ppd-private.h>
28 static int add_printer_to_class(http_t
*http
, char *printer
, char *pclass
);
29 static int default_printer(http_t
*http
, char *printer
);
30 static int delete_printer(http_t
*http
, char *printer
);
31 static int delete_printer_from_class(http_t
*http
, char *printer
,
33 static int delete_printer_option(http_t
*http
, char *printer
,
35 static int enable_printer(http_t
*http
, char *printer
);
36 static char *get_printer_ppd(const char *uri
, char *buffer
, size_t bufsize
, int *num_options
, cups_option_t
**options
);
37 static cups_ptype_t
get_printer_type(http_t
*http
, char *printer
, char *uri
,
39 static int set_printer_options(http_t
*http
, char *printer
,
40 int num_options
, cups_option_t
*options
,
41 char *file
, int enable
);
42 static int validate_name(const char *name
);
46 * 'main()' - Parse options and configure the scheduler.
49 int /* O - Exit status */
50 main(int argc
, /* I - Number of command-line arguments */
51 char *argv
[]) /* I - Command-line arguments */
53 int i
; /* Looping var */
54 http_t
*http
; /* Connection to server */
55 char *printer
, /* Destination printer */
56 *pclass
, /* Printer class name */
57 *opt
, /* Option pointer */
58 *val
; /* Pointer to allow/deny value */
59 int enable
= 0; /* Enable/resume printer? */
60 int num_options
; /* Number of options */
61 cups_option_t
*options
; /* Options */
62 char *file
, /* New PPD file */
63 evefile
[1024] = ""; /* IPP Everywhere PPD */
64 const char *ppd_name
, /* ppd-name value */
65 *device_uri
; /* device-uri value */
76 for (i
= 1; i
< argc
; i
++)
78 if (argv
[i
][0] == '-')
80 for (opt
= argv
[i
] + 1; *opt
; opt
++)
84 case 'c' : /* Add printer to class */
87 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
91 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
99 _("lpadmin: Unable to add a printer to the class:\n"
100 " You must specify a printer name first."));
107 opt
+= strlen(opt
) - 1;
115 _cupsLangPuts(stderr
, _("lpadmin: Expected class name after \"-c\" option."));
122 if (!validate_name(pclass
))
124 _cupsLangPuts(stderr
,
125 _("lpadmin: Class name can only contain printable "
130 if (add_printer_to_class(http
, printer
, pclass
))
134 case 'd' : /* Set as default destination */
137 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
141 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
149 opt
+= strlen(opt
) - 1;
157 _cupsLangPuts(stderr
, _("lpadmin: Expected printer name after \"-d\" option."));
164 if (!validate_name(printer
))
166 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
170 if (default_printer(http
, printer
))
176 case 'h' : /* Connect to host */
185 cupsSetServer(opt
+ 1);
186 opt
+= strlen(opt
) - 1;
194 _cupsLangPuts(stderr
, _("lpadmin: Expected hostname after \"-h\" option."));
198 cupsSetServer(argv
[i
]);
202 case 'P' : /* Use the specified PPD file */
203 case 'i' : /* Use the specified PPD file */
207 opt
+= strlen(opt
) - 1;
215 _cupsLangPrintf(stderr
, _("lpadmin: Expected PPD after \"-%c\" option."), argv
[i
- 1][1]);
225 * Check to see that the specified file is, in fact, a PPD...
228 cups_file_t
*fp
= cupsFileOpen(file
, "r");
231 if (!cupsFileGets(fp
, line
, sizeof(line
)) || strncmp(line
, "*PPD-Adobe", 10))
233 _cupsLangPuts(stderr
, _("lpadmin: System V interface scripts are no longer supported for security reasons."));
242 case 'E' : /* Enable the printer/enable encryption */
246 cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED
);
249 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
251 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
252 #endif /* HAVE_SSL */
258 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
262 _cupsLangPrintf(stderr
,
263 _("lpadmin: Unable to connect to server: %s"),
272 case 'm' : /* Use the specified standard script/PPD file */
275 num_options
= cupsAddOption("ppd-name", opt
+ 1, num_options
, &options
);
276 opt
+= strlen(opt
) - 1;
284 _cupsLangPuts(stderr
, _("lpadmin: Expected model after \"-m\" option."));
288 num_options
= cupsAddOption("ppd-name", argv
[i
], num_options
, &options
);
292 case 'o' : /* Set option */
295 num_options
= cupsParseOptions(opt
+ 1, num_options
, &options
);
296 opt
+= strlen(opt
) - 1;
304 _cupsLangPuts(stderr
, _("lpadmin: Expected name=value after \"-o\" option."));
308 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
312 case 'p' : /* Add/modify a printer */
316 opt
+= strlen(opt
) - 1;
324 _cupsLangPuts(stderr
, _("lpadmin: Expected printer after \"-p\" option."));
331 if (!validate_name(printer
))
333 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
338 case 'r' : /* Remove printer from class */
341 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
345 _cupsLangPrintf(stderr
,
346 _("lpadmin: Unable to connect to server: %s"),
354 _cupsLangPuts(stderr
,
355 _("lpadmin: Unable to remove a printer from the class:\n"
356 " You must specify a printer name first."));
363 opt
+= strlen(opt
) - 1;
371 _cupsLangPuts(stderr
, _("lpadmin: Expected class after \"-r\" option."));
378 if (!validate_name(pclass
))
380 _cupsLangPuts(stderr
, _("lpadmin: Class name can only contain printable characters."));
384 if (delete_printer_from_class(http
, printer
, pclass
))
388 case 'R' : /* Remove option */
391 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
395 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
402 _cupsLangPuts(stderr
,
403 _("lpadmin: Unable to delete option:\n"
404 " You must specify a printer name first."));
411 opt
+= strlen(opt
) - 1;
419 _cupsLangPuts(stderr
, _("lpadmin: Expected name after \"-R\" option."));
426 if (delete_printer_option(http
, printer
, val
))
430 case 'U' : /* Username */
433 cupsSetUser(opt
+ 1);
434 opt
+= strlen(opt
) - 1;
441 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-U\" option."), argv
[0]);
445 cupsSetUser(argv
[i
]);
449 case 'u' : /* Allow/deny users */
453 opt
+= strlen(opt
) - 1;
461 _cupsLangPuts(stderr
, _("lpadmin: Expected allow/deny:userlist after \"-u\" option."));
468 if (!_cups_strncasecmp(val
, "allow:", 6))
469 num_options
= cupsAddOption("requesting-user-name-allowed", val
+ 6, num_options
, &options
);
470 else if (!_cups_strncasecmp(val
, "deny:", 5))
471 num_options
= cupsAddOption("requesting-user-name-denied", val
+ 5, num_options
, &options
);
474 _cupsLangPrintf(stderr
, _("lpadmin: Unknown allow/deny option \"%s\"."), val
);
479 case 'v' : /* Set the device-uri attribute */
482 num_options
= cupsAddOption("device-uri", opt
+ 1, num_options
, &options
);
483 opt
+= strlen(opt
) - 1;
491 _cupsLangPuts(stderr
, _("lpadmin: Expected device URI after \"-v\" option."));
495 num_options
= cupsAddOption("device-uri", argv
[i
], num_options
, &options
);
499 case 'x' : /* Delete a printer */
502 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
506 _cupsLangPrintf(stderr
,
507 _("lpadmin: Unable to connect to server: %s"),
516 opt
+= strlen(opt
) - 1;
524 _cupsLangPuts(stderr
, _("lpadmin: Expected printer or class after \"-x\" option."));
531 if (!validate_name(printer
))
533 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
537 if (delete_printer(http
, printer
))
543 case 'D' : /* Set the printer-info attribute */
546 num_options
= cupsAddOption("printer-info", opt
+ 1, num_options
, &options
);
547 opt
+= strlen(opt
) - 1;
555 _cupsLangPuts(stderr
, _("lpadmin: Expected description after \"-D\" option."));
559 num_options
= cupsAddOption("printer-info", argv
[i
], num_options
, &options
);
563 case 'I' : /* Set the supported file types (ignored) */
568 _cupsLangPuts(stderr
, _("lpadmin: Expected file type(s) after \"-I\" option."));
572 _cupsLangPuts(stderr
, _("lpadmin: Warning - content type list ignored."));
575 case 'L' : /* Set the printer-location attribute */
578 num_options
= cupsAddOption("printer-location", opt
+ 1, num_options
, &options
);
579 opt
+= strlen(opt
) - 1;
587 _cupsLangPuts(stderr
, _("lpadmin: Expected location after \"-L\" option."));
591 num_options
= cupsAddOption("printer-location", argv
[i
], num_options
, &options
);
596 _cupsLangPrintf(stderr
, _("lpadmin: Unknown option \"%c\"."), *opt
);
603 _cupsLangPrintf(stderr
, _("lpadmin: Unknown argument \"%s\"."), argv
[i
]);
609 * Set options as needed...
612 ppd_name
= cupsGetOption("ppd-name", num_options
, options
);
613 device_uri
= cupsGetOption("device-uri", num_options
, options
);
615 if (ppd_name
&& !strcmp(ppd_name
, "raw"))
617 _cupsLangPuts(stderr
, _("lpadmin: Raw queues are deprecated and will stop working in a future version of CUPS."));
619 if (device_uri
&& (!strncmp(device_uri
, "ipp://", 6) || !strncmp(device_uri
, "ipps://", 7)) && strstr(device_uri
, "/printers/"))
620 _cupsLangPuts(stderr
, _("lpadmin: Use the 'everywhere' model for shared printers."));
622 else if (ppd_name
&& !strcmp(ppd_name
, "everywhere") && device_uri
)
624 if ((file
= get_printer_ppd(device_uri
, evefile
, sizeof(evefile
), &num_options
, &options
)) == NULL
)
627 num_options
= cupsRemoveOption("ppd-name", num_options
, &options
);
630 if (num_options
|| file
)
634 _cupsLangPuts(stderr
,
635 _("lpadmin: Unable to set the printer options:\n"
636 " You must specify a printer name first."));
642 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
643 cupsEncryption(), 1, 30000, NULL
);
646 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"),
652 if (set_printer_options(http
, printer
, num_options
, options
, file
, enable
))
655 else if (enable
&& enable_printer(http
, printer
))
663 _cupsLangPuts(stdout
,
666 " lpadmin [-h server] -d destination\n"
667 " lpadmin [-h server] -x destination\n"
668 " lpadmin [-h server] -p printer [-c add-class] "
669 "[-i interface] [-m model]\n"
670 " [-r remove-class] [-v device] "
672 " [-P ppd-file] [-o name=value]\n"
673 " [-u allow:user,user] "
674 "[-u deny:user,user]"));
685 * 'add_printer_to_class()' - Add a printer to a class.
688 static int /* O - 0 on success, 1 on fail */
689 add_printer_to_class(http_t
*http
, /* I - Server connection */
690 char *printer
, /* I - Printer to add */
691 char *pclass
) /* I - Class to add to */
693 int i
; /* Looping var */
694 ipp_t
*request
, /* IPP Request */
695 *response
; /* IPP Response */
696 ipp_attribute_t
*attr
, /* Current attribute */
697 *members
; /* Members in class */
698 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
701 DEBUG_printf(("add_printer_to_class(%p, \"%s\", \"%s\")\n", http
,
705 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
709 * attributes-natural-language
711 * requesting-user-name
714 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
716 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
717 "localhost", 0, "/classes/%s", pclass
);
718 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
719 "printer-uri", NULL
, uri
);
720 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
724 * Do the request and get back a response...
727 response
= cupsDoRequest(http
, request
, "/");
730 * Build a CUPS-Add-Modify-Class request, which requires the following
734 * attributes-natural-language
736 * requesting-user-name
740 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
742 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
743 "printer-uri", NULL
, uri
);
744 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
748 * See if the printer is already in the class...
751 if (response
!= NULL
&&
752 (members
= ippFindAttribute(response
, "member-names",
753 IPP_TAG_NAME
)) != NULL
)
754 for (i
= 0; i
< members
->num_values
; i
++)
755 if (_cups_strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
757 _cupsLangPrintf(stderr
,
758 _("lpadmin: Printer %s is already a member of class "
759 "%s."), printer
, pclass
);
766 * OK, the printer isn't part of the class, so add it...
769 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
770 "localhost", 0, "/printers/%s", printer
);
772 if (response
!= NULL
&&
773 (members
= ippFindAttribute(response
, "member-uris",
774 IPP_TAG_URI
)) != NULL
)
777 * Add the printer to the existing list...
780 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
781 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
782 for (i
= 0; i
< members
->num_values
; i
++)
783 attr
->values
[i
].string
.text
=
784 _cupsStrAlloc(members
->values
[i
].string
.text
);
786 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
789 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
793 * Then send the request...
798 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
799 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
801 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
811 * 'default_printer()' - Set the default printing destination.
814 static int /* O - 0 on success, 1 on fail */
815 default_printer(http_t
*http
, /* I - Server connection */
816 char *printer
) /* I - Printer name */
818 ipp_t
*request
; /* IPP Request */
819 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
822 DEBUG_printf(("default_printer(%p, \"%s\")\n", http
, printer
));
825 * Build a CUPS-Set-Default request, which requires the following
829 * attributes-natural-language
831 * requesting-user-name
834 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
835 "localhost", 0, "/printers/%s", printer
);
837 request
= ippNewRequest(IPP_OP_CUPS_SET_DEFAULT
);
839 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
840 "printer-uri", NULL
, uri
);
841 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
845 * Do the request and get back a response...
848 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
850 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
852 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
862 * 'delete_printer()' - Delete a printer from the system...
865 static int /* O - 0 on success, 1 on fail */
866 delete_printer(http_t
*http
, /* I - Server connection */
867 char *printer
) /* I - Printer to delete */
869 ipp_t
*request
; /* IPP Request */
870 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
873 DEBUG_printf(("delete_printer(%p, \"%s\")\n", http
, printer
));
876 * Build a CUPS-Delete-Printer request, which requires the following
880 * attributes-natural-language
882 * requesting-user-name
885 request
= ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER
);
887 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
888 "localhost", 0, "/printers/%s", printer
);
889 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
890 "printer-uri", NULL
, uri
);
891 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
895 * Do the request and get back a response...
898 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
900 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
902 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
912 * 'delete_printer_from_class()' - Delete a printer from a class.
915 static int /* O - 0 on success, 1 on fail */
916 delete_printer_from_class(
917 http_t
*http
, /* I - Server connection */
918 char *printer
, /* I - Printer to remove */
919 char *pclass
) /* I - Class to remove from */
921 int i
, j
, k
; /* Looping vars */
922 ipp_t
*request
, /* IPP Request */
923 *response
; /* IPP Response */
924 ipp_attribute_t
*attr
, /* Current attribute */
925 *members
; /* Members in class */
926 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
929 DEBUG_printf(("delete_printer_from_class(%p, \"%s\", \"%s\")\n", http
,
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 */
1129 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http
, printer
));
1132 * Send IPP_OP_ENABLE_PRINTER and IPP_OP_RESUME_PRINTER requests, which
1133 * require the following attributes:
1135 * attributes-charset
1136 * attributes-natural-language
1138 * requesting-user-name
1141 request
= ippNewRequest(IPP_OP_ENABLE_PRINTER
);
1143 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1144 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1145 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1147 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1149 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1151 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1156 request
= ippNewRequest(IPP_OP_RESUME_PRINTER
);
1158 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1159 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1161 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1163 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1165 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1175 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
1178 static char * /* O - Filename or NULL */
1180 const char *uri
, /* I - Printer URI */
1181 char *buffer
, /* I - Filename buffer */
1182 size_t bufsize
, /* I - Size of filename buffer */
1183 int *num_options
, /* IO - Number of options */
1184 cups_option_t
**options
) /* IO - Options */
1186 http_t
*http
; /* Connection to printer */
1187 ipp_t
*request
, /* Get-Printer-Attributes request */
1188 *response
; /* Get-Printer-Attributes response */
1189 ipp_attribute_t
*attr
; /* Attribute from response */
1190 char resolved
[1024], /* Resolved URI */
1191 scheme
[32], /* URI scheme */
1192 userpass
[256], /* Username:password */
1193 host
[256], /* Hostname */
1194 resource
[256]; /* Resource path */
1195 int port
; /* Port number */
1196 static const char * const pattrs
[] = /* Attributes to use */
1199 "media-col-database"
1204 * Connect to the printer...
1207 if (strstr(uri
, "._tcp"))
1213 if (!_httpResolveURI(uri
, resolved
, sizeof(resolved
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
1215 _cupsLangPrintf(stderr
, _("%s: Unable to resolve \"%s\"."), "lpadmin", uri
);
1222 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
1224 _cupsLangPrintf(stderr
, _("%s: Bad printer URI \"%s\"."), "lpadmin", uri
);
1228 http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, !strcmp(scheme
, "ipps") ? HTTP_ENCRYPTION_ALWAYS
: HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
);
1231 _cupsLangPrintf(stderr
, _("%s: Unable to connect to \"%s:%d\": %s"), "lpadmin", host
, port
, cupsLastErrorString());
1236 * Send a Get-Printer-Attributes request...
1239 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1240 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1241 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]), NULL
, pattrs
);
1242 response
= cupsDoRequest(http
, request
, resource
);
1244 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
1246 _cupsLangPrintf(stderr
, _("%s: Unable to query printer: %s"), "lpadmin", cupsLastErrorString());
1249 else if (_ppdCreateFromIPP(buffer
, bufsize
, response
))
1251 if (!cupsGetOption("printer-geo-location", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-geo-location", IPP_TAG_URI
)) != NULL
)
1252 *num_options
= cupsAddOption("printer-geo-location", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1254 if (!cupsGetOption("printer-info", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-info", IPP_TAG_TEXT
)) != NULL
)
1255 *num_options
= cupsAddOption("printer-info", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1257 if (!cupsGetOption("printer-location", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-location", IPP_TAG_TEXT
)) != NULL
)
1258 *num_options
= cupsAddOption("printer-location", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1261 _cupsLangPrintf(stderr
, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno
));
1263 ippDelete(response
);
1274 * 'get_printer_type()' - Determine the printer type and URI.
1277 static cups_ptype_t
/* O - printer-type value */
1278 get_printer_type(http_t
*http
, /* I - Server connection */
1279 char *printer
, /* I - Printer name */
1280 char *uri
, /* I - URI buffer */
1281 size_t urisize
) /* I - Size of URI buffer */
1283 ipp_t
*request
, /* IPP request */
1284 *response
; /* IPP response */
1285 ipp_attribute_t
*attr
; /* printer-type attribute */
1286 cups_ptype_t type
; /* printer-type value */
1290 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1293 * attributes-charset
1294 * attributes-natural-language
1296 * requested-attributes
1297 * requesting-user-name
1300 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1302 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1303 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1304 "printer-uri", NULL
, uri
);
1305 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1306 "requested-attributes", NULL
, "printer-type");
1307 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1308 "requesting-user-name", NULL
, cupsUser());
1314 response
= cupsDoRequest(http
, request
, "/");
1315 if ((attr
= ippFindAttribute(response
, "printer-type",
1316 IPP_TAG_ENUM
)) != NULL
)
1318 type
= (cups_ptype_t
)attr
->values
[0].integer
;
1320 if (type
& CUPS_PRINTER_CLASS
)
1321 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/classes/%s", printer
);
1324 type
= CUPS_PRINTER_LOCAL
;
1326 ippDelete(response
);
1333 * 'set_printer_options()' - Set the printer options.
1336 static int /* O - 0 on success, 1 on fail */
1337 set_printer_options(
1338 http_t
*http
, /* I - Server connection */
1339 char *printer
, /* I - Printer */
1340 int num_options
, /* I - Number of options */
1341 cups_option_t
*options
, /* I - Options */
1342 char *file
, /* I - PPD file */
1343 int enable
) /* I - Enable printer? */
1345 ipp_t
*request
; /* IPP Request */
1346 const char *ppdfile
; /* PPD filename */
1347 int ppdchanged
= 0; /* PPD changed? */
1348 ppd_file_t
*ppd
; /* PPD file */
1349 ppd_choice_t
*choice
; /* Marked choice */
1350 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1351 line
[1024], /* Line from PPD file */
1352 keyword
[1024], /* Keyword from Default line */
1353 *keyptr
, /* Pointer into keyword... */
1354 tempfile
[1024]; /* Temporary filename */
1355 cups_file_t
*in
, /* PPD file */
1356 *out
; /* Temporary file */
1357 const char *ppdname
, /* ppd-name value */
1358 *protocol
, /* Old protocol option */
1359 *customval
, /* Custom option value */
1360 *boolval
; /* Boolean value */
1361 int wrote_ipp_supplies
= 0, /* Wrote cupsIPPSupplies keyword? */
1362 wrote_snmp_supplies
= 0,/* Wrote cupsSNMPSupplies keyword? */
1363 copied_options
= 0; /* Copied options? */
1366 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1367 "options=%p, file=\"%s\")\n", http
, printer
, num_options
,
1371 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1372 * which requires the following attributes:
1374 * attributes-charset
1375 * attributes-natural-language
1377 * requesting-user-name
1381 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1382 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1384 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1386 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1387 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1390 * Add the options...
1395 else if ((ppdname
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& strcmp(ppdname
, "raw") && num_options
> 1)
1397 if ((ppdfile
= cupsGetServerPPD(http
, ppdname
)) != NULL
)
1400 * Copy options array and remove ppd-name from it...
1403 cups_option_t
*temp
= NULL
, *optr
;
1404 int i
, num_temp
= 0;
1405 for (i
= num_options
, optr
= options
; i
> 0; i
--, optr
++)
1406 if (strcmp(optr
->name
, "ppd-name"))
1407 num_temp
= cupsAddOption(optr
->name
, optr
->value
, num_temp
, &temp
);
1411 num_options
= num_temp
;
1415 else if (request
->request
.op
.operation_id
== IPP_OP_CUPS_ADD_MODIFY_PRINTER
)
1416 ppdfile
= cupsGetPPD(printer
);
1420 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
1424 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state", IPP_PSTATE_IDLE
);
1425 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1428 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1430 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1432 if (!_cups_strcasecmp(protocol
, "bcp"))
1433 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1435 else if (!_cups_strcasecmp(protocol
, "tbcp"))
1436 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1443 * Set default options in the PPD file...
1446 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
1448 int linenum
; /* Line number of error */
1449 ppd_status_t status
= ppdLastError(&linenum
);
1452 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile
, ppdErrorString(status
), linenum
);
1455 ppdMarkDefaults(ppd
);
1456 cupsMarkOptions(ppd
, num_options
, options
);
1458 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1460 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1462 if (ppdfile
!= file
)
1465 cupsFreeOptions(num_options
, options
);
1469 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1471 _cupsLangPrintf(stderr
,
1472 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1473 ppdfile
, strerror(errno
));
1475 if (ppdfile
!= file
)
1478 cupsFreeOptions(num_options
, options
);
1484 while (cupsFileGets(in
, line
, sizeof(line
)))
1486 if (!strncmp(line
, "*cupsIPPSupplies:", 17) &&
1487 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1490 wrote_ipp_supplies
= 1;
1491 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1492 (!_cups_strcasecmp(boolval
, "true") ||
1493 !_cups_strcasecmp(boolval
, "yes") ||
1494 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1496 else if (!strncmp(line
, "*cupsSNMPSupplies:", 18) &&
1497 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1500 wrote_snmp_supplies
= 1;
1501 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1502 (!_cups_strcasecmp(boolval
, "true") ||
1503 !_cups_strcasecmp(boolval
, "yes") ||
1504 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1506 else if (strncmp(line
, "*Default", 8))
1507 cupsFilePrintf(out
, "%s\n", line
);
1511 * Get default option name...
1514 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
1516 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1517 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1521 while (isspace(*keyptr
& 255))
1524 if (!strcmp(keyword
, "PageRegion") ||
1525 !strcmp(keyword
, "PageSize") ||
1526 !strcmp(keyword
, "PaperDimension") ||
1527 !strcmp(keyword
, "ImageableArea"))
1529 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1530 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1533 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1535 if (choice
&& strcmp(choice
->choice
, keyptr
))
1537 if (strcmp(choice
->choice
, "Custom"))
1539 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1542 else if ((customval
= cupsGetOption(keyword
, num_options
,
1545 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1549 cupsFilePrintf(out
, "%s\n", line
);
1552 cupsFilePrintf(out
, "%s\n", line
);
1556 if (!wrote_ipp_supplies
&&
1557 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1560 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1561 (!_cups_strcasecmp(boolval
, "true") ||
1562 !_cups_strcasecmp(boolval
, "yes") ||
1563 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1566 if (!wrote_snmp_supplies
&&
1567 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1570 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1571 (!_cups_strcasecmp(boolval
, "true") ||
1572 !_cups_strcasecmp(boolval
, "yes") ||
1573 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1584 ippDelete(cupsDoFileRequest(http
, request
, "/admin/",
1585 ppdchanged
? tempfile
: file
));
1588 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1592 if (ppdfile
!= file
)
1599 * No PPD file - just set the options...
1602 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1606 cupsFreeOptions(num_options
, options
);
1609 * Check the response...
1612 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1614 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1624 * 'validate_name()' - Make sure the printer name only contains valid chars.
1627 static int /* O - 0 if name is no good, 1 if name is good */
1628 validate_name(const char *name
) /* I - Name to check */
1630 const char *ptr
; /* Pointer into name */
1634 * Scan the whole name...
1637 for (ptr
= name
; *ptr
; ptr
++)
1640 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '\\' || *ptr
== '?' || *ptr
== '\'' || *ptr
== '\"' || *ptr
== '#')
1644 * All the characters are good; validate the length, too...
1647 return ((ptr
- name
) < 128);