2 * "lpadmin" command for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups-private.h>
15 #include <cups/ppd-private.h>
22 static int add_printer_to_class(http_t
*http
, char *printer
, char *pclass
);
23 static int default_printer(http_t
*http
, char *printer
);
24 static int delete_printer(http_t
*http
, char *printer
);
25 static int delete_printer_from_class(http_t
*http
, char *printer
,
27 static int delete_printer_option(http_t
*http
, char *printer
,
29 static int enable_printer(http_t
*http
, char *printer
);
30 static char *get_printer_ppd(const char *uri
, char *buffer
, size_t bufsize
, int *num_options
, cups_option_t
**options
);
31 static cups_ptype_t
get_printer_type(http_t
*http
, char *printer
, char *uri
,
33 static int set_printer_options(http_t
*http
, char *printer
,
34 int num_options
, cups_option_t
*options
,
35 char *file
, int enable
);
36 static int validate_name(const char *name
);
40 * 'main()' - Parse options and configure the scheduler.
43 int /* O - Exit status */
44 main(int argc
, /* I - Number of command-line arguments */
45 char *argv
[]) /* I - Command-line arguments */
47 int i
; /* Looping var */
48 http_t
*http
; /* Connection to server */
49 char *printer
, /* Destination printer */
50 *pclass
, /* Printer class name */
51 *opt
, /* Option pointer */
52 *val
; /* Pointer to allow/deny value */
53 int enable
= 0; /* Enable/resume printer? */
54 int num_options
; /* Number of options */
55 cups_option_t
*options
; /* Options */
56 char *file
, /* New PPD file */
57 evefile
[1024] = ""; /* IPP Everywhere PPD */
58 const char *ppd_name
, /* ppd-name value */
59 *device_uri
; /* device-uri value */
70 for (i
= 1; i
< argc
; i
++)
72 if (argv
[i
][0] == '-')
74 for (opt
= argv
[i
] + 1; *opt
; opt
++)
78 case 'c' : /* Add printer to class */
81 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
85 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
93 _("lpadmin: Unable to add a printer to the class:\n"
94 " You must specify a printer name first."));
101 opt
+= strlen(opt
) - 1;
109 _cupsLangPuts(stderr
, _("lpadmin: Expected class name after \"-c\" option."));
116 if (!validate_name(pclass
))
118 _cupsLangPuts(stderr
,
119 _("lpadmin: Class name can only contain printable "
124 if (add_printer_to_class(http
, printer
, pclass
))
128 case 'd' : /* Set as default destination */
131 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
135 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
143 opt
+= strlen(opt
) - 1;
151 _cupsLangPuts(stderr
, _("lpadmin: Expected printer name after \"-d\" option."));
158 if (!validate_name(printer
))
160 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
164 if (default_printer(http
, printer
))
170 case 'h' : /* Connect to host */
179 cupsSetServer(opt
+ 1);
180 opt
+= strlen(opt
) - 1;
188 _cupsLangPuts(stderr
, _("lpadmin: Expected hostname after \"-h\" option."));
192 cupsSetServer(argv
[i
]);
196 case 'P' : /* Use the specified PPD file */
197 case 'i' : /* Use the specified PPD file */
201 opt
+= strlen(opt
) - 1;
209 _cupsLangPrintf(stderr
, _("lpadmin: Expected PPD after \"-%c\" option."), argv
[i
- 1][1]);
219 * Check to see that the specified file is, in fact, a PPD...
222 cups_file_t
*fp
= cupsFileOpen(file
, "r");
225 if (!cupsFileGets(fp
, line
, sizeof(line
)) || strncmp(line
, "*PPD-Adobe", 10))
227 _cupsLangPuts(stderr
, _("lpadmin: System V interface scripts are no longer supported for security reasons."));
236 case 'E' : /* Enable the printer/enable encryption */
240 cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED
);
243 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
245 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
246 #endif /* HAVE_SSL */
252 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
256 _cupsLangPrintf(stderr
,
257 _("lpadmin: Unable to connect to server: %s"),
266 case 'm' : /* Use the specified standard script/PPD file */
269 num_options
= cupsAddOption("ppd-name", opt
+ 1, num_options
, &options
);
270 opt
+= strlen(opt
) - 1;
278 _cupsLangPuts(stderr
, _("lpadmin: Expected model after \"-m\" option."));
282 num_options
= cupsAddOption("ppd-name", argv
[i
], num_options
, &options
);
286 case 'o' : /* Set option */
289 num_options
= cupsParseOptions(opt
+ 1, num_options
, &options
);
290 opt
+= strlen(opt
) - 1;
298 _cupsLangPuts(stderr
, _("lpadmin: Expected name=value after \"-o\" option."));
302 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
306 case 'p' : /* Add/modify a printer */
310 opt
+= strlen(opt
) - 1;
318 _cupsLangPuts(stderr
, _("lpadmin: Expected printer after \"-p\" option."));
325 if (!validate_name(printer
))
327 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
332 case 'r' : /* Remove printer from class */
335 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
339 _cupsLangPrintf(stderr
,
340 _("lpadmin: Unable to connect to server: %s"),
348 _cupsLangPuts(stderr
,
349 _("lpadmin: Unable to remove a printer from the class:\n"
350 " You must specify a printer name first."));
357 opt
+= strlen(opt
) - 1;
365 _cupsLangPuts(stderr
, _("lpadmin: Expected class after \"-r\" option."));
372 if (!validate_name(pclass
))
374 _cupsLangPuts(stderr
, _("lpadmin: Class name can only contain printable characters."));
378 if (delete_printer_from_class(http
, printer
, pclass
))
382 case 'R' : /* Remove option */
385 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
389 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
396 _cupsLangPuts(stderr
,
397 _("lpadmin: Unable to delete option:\n"
398 " You must specify a printer name first."));
405 opt
+= strlen(opt
) - 1;
413 _cupsLangPuts(stderr
, _("lpadmin: Expected name after \"-R\" option."));
420 if (delete_printer_option(http
, printer
, val
))
424 case 'U' : /* Username */
427 cupsSetUser(opt
+ 1);
428 opt
+= strlen(opt
) - 1;
435 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-U\" option."), argv
[0]);
439 cupsSetUser(argv
[i
]);
443 case 'u' : /* Allow/deny users */
447 opt
+= strlen(opt
) - 1;
455 _cupsLangPuts(stderr
, _("lpadmin: Expected allow/deny:userlist after \"-u\" option."));
462 if (!_cups_strncasecmp(val
, "allow:", 6))
463 num_options
= cupsAddOption("requesting-user-name-allowed", val
+ 6, num_options
, &options
);
464 else if (!_cups_strncasecmp(val
, "deny:", 5))
465 num_options
= cupsAddOption("requesting-user-name-denied", val
+ 5, num_options
, &options
);
468 _cupsLangPrintf(stderr
, _("lpadmin: Unknown allow/deny option \"%s\"."), val
);
473 case 'v' : /* Set the device-uri attribute */
476 num_options
= cupsAddOption("device-uri", opt
+ 1, num_options
, &options
);
477 opt
+= strlen(opt
) - 1;
485 _cupsLangPuts(stderr
, _("lpadmin: Expected device URI after \"-v\" option."));
489 num_options
= cupsAddOption("device-uri", argv
[i
], num_options
, &options
);
493 case 'x' : /* Delete a printer */
496 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
500 _cupsLangPrintf(stderr
,
501 _("lpadmin: Unable to connect to server: %s"),
510 opt
+= strlen(opt
) - 1;
518 _cupsLangPuts(stderr
, _("lpadmin: Expected printer or class after \"-x\" option."));
525 if (!validate_name(printer
))
527 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
531 if (delete_printer(http
, printer
))
537 case 'D' : /* Set the printer-info attribute */
540 num_options
= cupsAddOption("printer-info", opt
+ 1, num_options
, &options
);
541 opt
+= strlen(opt
) - 1;
549 _cupsLangPuts(stderr
, _("lpadmin: Expected description after \"-D\" option."));
553 num_options
= cupsAddOption("printer-info", argv
[i
], num_options
, &options
);
557 case 'I' : /* Set the supported file types (ignored) */
562 _cupsLangPuts(stderr
, _("lpadmin: Expected file type(s) after \"-I\" option."));
566 _cupsLangPuts(stderr
, _("lpadmin: Warning - content type list ignored."));
569 case 'L' : /* Set the printer-location attribute */
572 num_options
= cupsAddOption("printer-location", opt
+ 1, num_options
, &options
);
573 opt
+= strlen(opt
) - 1;
581 _cupsLangPuts(stderr
, _("lpadmin: Expected location after \"-L\" option."));
585 num_options
= cupsAddOption("printer-location", argv
[i
], num_options
, &options
);
590 _cupsLangPrintf(stderr
, _("lpadmin: Unknown option \"%c\"."), *opt
);
597 _cupsLangPrintf(stderr
, _("lpadmin: Unknown argument \"%s\"."), argv
[i
]);
603 * Set options as needed...
606 ppd_name
= cupsGetOption("ppd-name", num_options
, options
);
607 device_uri
= cupsGetOption("device-uri", num_options
, options
);
609 if (ppd_name
&& !strcmp(ppd_name
, "raw"))
612 _cupsLangPuts(stderr
, _("lpadmin: Raw queues are no longer supported on macOS."));
614 _cupsLangPuts(stderr
, _("lpadmin: Raw queues are deprecated and will stop working in a future version of CUPS."));
615 #endif /* __APPLE__ */
617 if (device_uri
&& (!strncmp(device_uri
, "ipp://", 6) || !strncmp(device_uri
, "ipps://", 7)) && strstr(device_uri
, "/printers/"))
618 _cupsLangPuts(stderr
, _("lpadmin: Use the 'everywhere' model for shared printers."));
622 #endif /* __APPLE__ */
624 else if (ppd_name
&& !strcmp(ppd_name
, "everywhere") && device_uri
)
626 if ((file
= get_printer_ppd(device_uri
, evefile
, sizeof(evefile
), &num_options
, &options
)) == NULL
)
629 num_options
= cupsRemoveOption("ppd-name", num_options
, &options
);
631 else if (ppd_name
|| file
)
633 _cupsLangPuts(stderr
, _("lpadmin: Printer drivers are deprecated and will stop working in a future version of CUPS."));
636 if (num_options
|| file
)
640 _cupsLangPuts(stderr
,
641 _("lpadmin: Unable to set the printer options:\n"
642 " You must specify a printer name first."));
648 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
649 cupsEncryption(), 1, 30000, NULL
);
652 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"),
658 if (set_printer_options(http
, printer
, num_options
, options
, file
, enable
))
661 else if (enable
&& enable_printer(http
, printer
))
669 _cupsLangPuts(stdout
,
672 " lpadmin [-h server] -d destination\n"
673 " lpadmin [-h server] -x destination\n"
674 " lpadmin [-h server] -p printer [-c add-class] "
675 "[-i interface] [-m model]\n"
676 " [-r remove-class] [-v device] "
678 " [-P ppd-file] [-o name=value]\n"
679 " [-u allow:user,user] "
680 "[-u deny:user,user]"));
691 * 'add_printer_to_class()' - Add a printer to a class.
694 static int /* O - 0 on success, 1 on fail */
695 add_printer_to_class(http_t
*http
, /* I - Server connection */
696 char *printer
, /* I - Printer to add */
697 char *pclass
) /* I - Class to add to */
699 int i
; /* Looping var */
700 ipp_t
*request
, /* IPP Request */
701 *response
; /* IPP Response */
702 ipp_attribute_t
*attr
, /* Current attribute */
703 *members
; /* Members in class */
704 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
707 DEBUG_printf(("add_printer_to_class(%p, \"%s\", \"%s\")\n", http
,
711 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
715 * attributes-natural-language
717 * requesting-user-name
720 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
722 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
723 "localhost", 0, "/classes/%s", pclass
);
724 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
725 "printer-uri", NULL
, uri
);
726 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
730 * Do the request and get back a response...
733 response
= cupsDoRequest(http
, request
, "/");
736 * Build a CUPS-Add-Modify-Class request, which requires the following
740 * attributes-natural-language
742 * requesting-user-name
746 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
748 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
749 "printer-uri", NULL
, uri
);
750 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
754 * See if the printer is already in the class...
757 if (response
!= NULL
&&
758 (members
= ippFindAttribute(response
, "member-names",
759 IPP_TAG_NAME
)) != NULL
)
760 for (i
= 0; i
< members
->num_values
; i
++)
761 if (_cups_strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
763 _cupsLangPrintf(stderr
,
764 _("lpadmin: Printer %s is already a member of class "
765 "%s."), printer
, pclass
);
772 * OK, the printer isn't part of the class, so add it...
775 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
776 "localhost", 0, "/printers/%s", printer
);
778 if (response
!= NULL
&&
779 (members
= ippFindAttribute(response
, "member-uris",
780 IPP_TAG_URI
)) != NULL
)
783 * Add the printer to the existing list...
786 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
787 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
788 for (i
= 0; i
< members
->num_values
; i
++)
789 attr
->values
[i
].string
.text
=
790 _cupsStrAlloc(members
->values
[i
].string
.text
);
792 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
795 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
799 * Then send the request...
804 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
805 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
807 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
817 * 'default_printer()' - Set the default printing destination.
820 static int /* O - 0 on success, 1 on fail */
821 default_printer(http_t
*http
, /* I - Server connection */
822 char *printer
) /* I - Printer name */
824 ipp_t
*request
; /* IPP Request */
825 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
828 DEBUG_printf(("default_printer(%p, \"%s\")\n", http
, printer
));
831 * Build a CUPS-Set-Default request, which requires the following
835 * attributes-natural-language
837 * requesting-user-name
840 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
841 "localhost", 0, "/printers/%s", printer
);
843 request
= ippNewRequest(IPP_OP_CUPS_SET_DEFAULT
);
845 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
846 "printer-uri", NULL
, uri
);
847 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
851 * Do the request and get back a response...
854 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
856 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
858 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
868 * 'delete_printer()' - Delete a printer from the system...
871 static int /* O - 0 on success, 1 on fail */
872 delete_printer(http_t
*http
, /* I - Server connection */
873 char *printer
) /* I - Printer to delete */
875 ipp_t
*request
; /* IPP Request */
876 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
879 DEBUG_printf(("delete_printer(%p, \"%s\")\n", http
, printer
));
882 * Build a CUPS-Delete-Printer request, which requires the following
886 * attributes-natural-language
888 * requesting-user-name
891 request
= ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER
);
893 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
894 "localhost", 0, "/printers/%s", printer
);
895 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
896 "printer-uri", NULL
, uri
);
897 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
901 * Do the request and get back a response...
904 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
906 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
908 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
918 * 'delete_printer_from_class()' - Delete a printer from a class.
921 static int /* O - 0 on success, 1 on fail */
922 delete_printer_from_class(
923 http_t
*http
, /* I - Server connection */
924 char *printer
, /* I - Printer to remove */
925 char *pclass
) /* I - Class to remove from */
927 int i
, j
, k
; /* Looping vars */
928 ipp_t
*request
, /* IPP Request */
929 *response
; /* IPP Response */
930 ipp_attribute_t
*attr
, /* Current attribute */
931 *members
; /* Members in class */
932 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
935 DEBUG_printf(("delete_printer_from_class(%p, \"%s\", \"%s\")\n", http
,
939 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
943 * attributes-natural-language
945 * requesting-user-name
948 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
950 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
951 "localhost", 0, "/classes/%s", pclass
);
952 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
953 "printer-uri", NULL
, uri
);
954 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
958 * Do the request and get back a response...
961 if ((response
= cupsDoRequest(http
, request
, "/classes/")) == NULL
||
962 response
->request
.status
.status_code
== IPP_STATUS_ERROR_NOT_FOUND
)
964 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
972 * See if the printer is already in the class...
975 if ((members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) == NULL
)
977 _cupsLangPuts(stderr
, _("lpadmin: No member names were seen."));
984 for (i
= 0; i
< members
->num_values
; i
++)
985 if (!_cups_strcasecmp(printer
, members
->values
[i
].string
.text
))
988 if (i
>= members
->num_values
)
990 _cupsLangPrintf(stderr
,
991 _("lpadmin: Printer %s is not a member of class %s."),
999 if (members
->num_values
== 1)
1002 * Build a CUPS-Delete-Class request, which requires the following
1005 * attributes-charset
1006 * attributes-natural-language
1008 * requesting-user-name
1011 request
= ippNewRequest(IPP_OP_CUPS_DELETE_CLASS
);
1013 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1014 "printer-uri", NULL
, uri
);
1015 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1016 "requesting-user-name", NULL
, cupsUser());
1021 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
1024 * attributes-charset
1025 * attributes-natural-language
1027 * requesting-user-name
1031 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1033 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1034 "printer-uri", NULL
, uri
);
1035 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1036 "requesting-user-name", NULL
, cupsUser());
1039 * Delete the printer from the class...
1042 members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
);
1043 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1044 "member-uris", members
->num_values
- 1, NULL
, NULL
);
1046 for (j
= 0, k
= 0; j
< members
->num_values
; j
++)
1048 attr
->values
[k
++].string
.text
=
1049 _cupsStrAlloc(members
->values
[j
].string
.text
);
1053 * Then send the request...
1056 ippDelete(response
);
1058 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1060 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1062 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1072 * 'delete_printer_option()' - Delete a printer option.
1075 static int /* O - 0 on success, 1 on fail */
1076 delete_printer_option(http_t
*http
, /* I - Server connection */
1077 char *printer
, /* I - Printer */
1078 char *option
) /* I - Option to delete */
1080 ipp_t
*request
; /* IPP request */
1081 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1085 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1086 * requires the following attributes:
1088 * attributes-charset
1089 * attributes-natural-language
1091 * requesting-user-name
1092 * option with deleteAttr tag
1095 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1096 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1098 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1100 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1101 "printer-uri", NULL
, uri
);
1102 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1103 "requesting-user-name", NULL
, cupsUser());
1104 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_DELETEATTR
, option
, 0);
1107 * Do the request and get back a response...
1110 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1112 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1114 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1124 * 'enable_printer()' - Enable a printer...
1127 static int /* O - 0 on success, 1 on fail */
1128 enable_printer(http_t
*http
, /* I - Server connection */
1129 char *printer
) /* I - Printer to enable */
1131 ipp_t
*request
; /* IPP Request */
1132 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1135 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http
, printer
));
1138 * Send IPP_OP_ENABLE_PRINTER and IPP_OP_RESUME_PRINTER requests, which
1139 * require the following attributes:
1141 * attributes-charset
1142 * attributes-natural-language
1144 * requesting-user-name
1147 request
= ippNewRequest(IPP_OP_ENABLE_PRINTER
);
1149 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1150 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1152 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1154 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1156 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1161 request
= ippNewRequest(IPP_OP_RESUME_PRINTER
);
1163 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1164 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1166 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1168 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1170 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1180 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
1183 static char * /* O - Filename or NULL */
1185 const char *uri
, /* I - Printer URI */
1186 char *buffer
, /* I - Filename buffer */
1187 size_t bufsize
, /* I - Size of filename buffer */
1188 int *num_options
, /* IO - Number of options */
1189 cups_option_t
**options
) /* IO - Options */
1191 http_t
*http
; /* Connection to printer */
1192 ipp_t
*request
, /* Get-Printer-Attributes request */
1193 *response
; /* Get-Printer-Attributes response */
1194 ipp_attribute_t
*attr
; /* Attribute from response */
1195 char resolved
[1024], /* Resolved URI */
1196 scheme
[32], /* URI scheme */
1197 userpass
[256], /* Username:password */
1198 host
[256], /* Hostname */
1199 resource
[256]; /* Resource path */
1200 int port
; /* Port number */
1201 static const char * const pattrs
[] = /* Attributes to use */
1205 "printer-description",
1206 "media-col-database"
1211 * Connect to the printer...
1214 if (strstr(uri
, "._tcp"))
1220 if (!_httpResolveURI(uri
, resolved
, sizeof(resolved
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
1222 _cupsLangPrintf(stderr
, _("%s: Unable to resolve \"%s\"."), "lpadmin", uri
);
1229 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
1231 _cupsLangPrintf(stderr
, _("%s: Bad printer URI \"%s\"."), "lpadmin", uri
);
1235 http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, !strcmp(scheme
, "ipps") ? HTTP_ENCRYPTION_ALWAYS
: HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
);
1238 _cupsLangPrintf(stderr
, _("%s: Unable to connect to \"%s:%d\": %s"), "lpadmin", host
, port
, cupsLastErrorString());
1243 * Send a Get-Printer-Attributes request...
1246 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1247 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1248 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]), NULL
, pattrs
);
1249 response
= cupsDoRequest(http
, request
, resource
);
1251 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
1253 _cupsLangPrintf(stderr
, _("%s: Unable to query printer: %s"), "lpadmin", cupsLastErrorString());
1256 else if (_ppdCreateFromIPP(buffer
, bufsize
, response
))
1258 if (!cupsGetOption("printer-geo-location", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-geo-location", IPP_TAG_URI
)) != NULL
)
1259 *num_options
= cupsAddOption("printer-geo-location", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1261 if (!cupsGetOption("printer-info", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-info", IPP_TAG_TEXT
)) != NULL
)
1262 *num_options
= cupsAddOption("printer-info", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1264 if (!cupsGetOption("printer-location", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-location", IPP_TAG_TEXT
)) != NULL
)
1265 *num_options
= cupsAddOption("printer-location", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1268 _cupsLangPrintf(stderr
, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno
));
1270 ippDelete(response
);
1281 * 'get_printer_type()' - Determine the printer type and URI.
1284 static cups_ptype_t
/* O - printer-type value */
1285 get_printer_type(http_t
*http
, /* I - Server connection */
1286 char *printer
, /* I - Printer name */
1287 char *uri
, /* I - URI buffer */
1288 size_t urisize
) /* I - Size of URI buffer */
1290 ipp_t
*request
, /* IPP request */
1291 *response
; /* IPP response */
1292 ipp_attribute_t
*attr
; /* printer-type attribute */
1293 cups_ptype_t type
; /* printer-type value */
1297 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1300 * attributes-charset
1301 * attributes-natural-language
1303 * requested-attributes
1304 * requesting-user-name
1307 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1309 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1310 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1311 "printer-uri", NULL
, uri
);
1312 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1313 "requested-attributes", NULL
, "printer-type");
1314 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1315 "requesting-user-name", NULL
, cupsUser());
1321 response
= cupsDoRequest(http
, request
, "/");
1322 if ((attr
= ippFindAttribute(response
, "printer-type",
1323 IPP_TAG_ENUM
)) != NULL
)
1325 type
= (cups_ptype_t
)attr
->values
[0].integer
;
1327 if (type
& CUPS_PRINTER_CLASS
)
1328 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/classes/%s", printer
);
1331 type
= CUPS_PRINTER_LOCAL
;
1333 ippDelete(response
);
1340 * 'set_printer_options()' - Set the printer options.
1343 static int /* O - 0 on success, 1 on fail */
1344 set_printer_options(
1345 http_t
*http
, /* I - Server connection */
1346 char *printer
, /* I - Printer */
1347 int num_options
, /* I - Number of options */
1348 cups_option_t
*options
, /* I - Options */
1349 char *file
, /* I - PPD file */
1350 int enable
) /* I - Enable printer? */
1352 ipp_t
*request
; /* IPP Request */
1353 const char *ppdfile
; /* PPD filename */
1354 int ppdchanged
= 0; /* PPD changed? */
1355 ppd_file_t
*ppd
; /* PPD file */
1356 ppd_choice_t
*choice
; /* Marked choice */
1357 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1358 line
[1024], /* Line from PPD file */
1359 keyword
[1024], /* Keyword from Default line */
1360 *keyptr
, /* Pointer into keyword... */
1361 tempfile
[1024]; /* Temporary filename */
1362 cups_file_t
*in
, /* PPD file */
1363 *out
; /* Temporary file */
1364 const char *ppdname
, /* ppd-name value */
1365 *protocol
, /* Old protocol option */
1366 *customval
, /* Custom option value */
1367 *boolval
; /* Boolean value */
1368 int wrote_ipp_supplies
= 0, /* Wrote cupsIPPSupplies keyword? */
1369 wrote_snmp_supplies
= 0,/* Wrote cupsSNMPSupplies keyword? */
1370 copied_options
= 0; /* Copied options? */
1373 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1374 "options=%p, file=\"%s\")\n", http
, printer
, num_options
,
1378 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1379 * which requires the following attributes:
1381 * attributes-charset
1382 * attributes-natural-language
1384 * requesting-user-name
1388 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1389 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1391 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1393 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1394 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1398 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state", IPP_PSTATE_IDLE
);
1399 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1403 * Add the options...
1408 else if ((ppdname
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& strcmp(ppdname
, "raw") && num_options
> 1)
1410 if ((ppdfile
= cupsGetServerPPD(http
, ppdname
)) != NULL
)
1413 * Copy options array and remove ppd-name from it...
1416 cups_option_t
*temp
= NULL
, *optr
;
1417 int i
, num_temp
= 0;
1418 for (i
= num_options
, optr
= options
; i
> 0; i
--, optr
++)
1419 if (strcmp(optr
->name
, "ppd-name"))
1420 num_temp
= cupsAddOption(optr
->name
, optr
->value
, num_temp
, &temp
);
1424 num_options
= num_temp
;
1428 else if (request
->request
.op
.operation_id
== IPP_OP_CUPS_ADD_MODIFY_PRINTER
)
1429 ppdfile
= cupsGetPPD(printer
);
1433 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
1434 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1436 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1438 if (!_cups_strcasecmp(protocol
, "bcp"))
1439 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1441 else if (!_cups_strcasecmp(protocol
, "tbcp"))
1442 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1449 * Set default options in the PPD file...
1452 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
1454 int linenum
; /* Line number of error */
1455 ppd_status_t status
= ppdLastError(&linenum
);
1458 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile
, ppdErrorString(status
), linenum
);
1461 ppdMarkDefaults(ppd
);
1462 cupsMarkOptions(ppd
, num_options
, options
);
1464 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1466 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1468 if (ppdfile
!= file
)
1471 cupsFreeOptions(num_options
, options
);
1475 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1477 _cupsLangPrintf(stderr
,
1478 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1479 ppdfile
, strerror(errno
));
1481 if (ppdfile
!= file
)
1484 cupsFreeOptions(num_options
, options
);
1490 while (cupsFileGets(in
, line
, sizeof(line
)))
1492 if (!strncmp(line
, "*cupsIPPSupplies:", 17) &&
1493 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1496 wrote_ipp_supplies
= 1;
1497 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1498 (!_cups_strcasecmp(boolval
, "true") ||
1499 !_cups_strcasecmp(boolval
, "yes") ||
1500 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1502 else if (!strncmp(line
, "*cupsSNMPSupplies:", 18) &&
1503 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1506 wrote_snmp_supplies
= 1;
1507 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1508 (!_cups_strcasecmp(boolval
, "true") ||
1509 !_cups_strcasecmp(boolval
, "yes") ||
1510 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1512 else if (strncmp(line
, "*Default", 8))
1513 cupsFilePrintf(out
, "%s\n", line
);
1517 * Get default option name...
1520 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
1522 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1523 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1527 while (isspace(*keyptr
& 255))
1530 if (!strcmp(keyword
, "PageRegion") ||
1531 !strcmp(keyword
, "PageSize") ||
1532 !strcmp(keyword
, "PaperDimension") ||
1533 !strcmp(keyword
, "ImageableArea"))
1535 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1536 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1539 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1541 if (choice
&& strcmp(choice
->choice
, keyptr
))
1543 if (strcmp(choice
->choice
, "Custom"))
1545 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1548 else if ((customval
= cupsGetOption(keyword
, num_options
,
1551 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1555 cupsFilePrintf(out
, "%s\n", line
);
1558 cupsFilePrintf(out
, "%s\n", line
);
1562 if (!wrote_ipp_supplies
&&
1563 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1566 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1567 (!_cups_strcasecmp(boolval
, "true") ||
1568 !_cups_strcasecmp(boolval
, "yes") ||
1569 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1572 if (!wrote_snmp_supplies
&&
1573 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1576 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1577 (!_cups_strcasecmp(boolval
, "true") ||
1578 !_cups_strcasecmp(boolval
, "yes") ||
1579 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1590 ippDelete(cupsDoFileRequest(http
, request
, "/admin/",
1591 ppdchanged
? tempfile
: file
));
1594 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1598 if (ppdfile
!= file
)
1605 * No PPD file - just set the options...
1608 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1612 cupsFreeOptions(num_options
, options
);
1615 * Check the response...
1618 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1620 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1630 * 'validate_name()' - Make sure the printer name only contains valid chars.
1633 static int /* O - 0 if name is no good, 1 if name is good */
1634 validate_name(const char *name
) /* I - Name to check */
1636 const char *ptr
; /* Pointer into name */
1640 * Scan the whole name...
1643 for (ptr
= name
; *ptr
; ptr
++)
1646 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '\\' || *ptr
== '?' || *ptr
== '\'' || *ptr
== '\"' || *ptr
== '#')
1650 * All the characters are good; validate the length, too...
1653 return ((ptr
- name
) < 128);