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 #define _CUPS_NO_DEPRECATED
15 #define _PPD_DEPRECATED
16 #include <cups/cups-private.h>
17 #include <cups/ppd-private.h>
24 static int add_printer_to_class(http_t
*http
, char *printer
, char *pclass
);
25 static int default_printer(http_t
*http
, char *printer
);
26 static int delete_printer(http_t
*http
, char *printer
);
27 static int delete_printer_from_class(http_t
*http
, char *printer
,
29 static int delete_printer_option(http_t
*http
, char *printer
,
31 static int enable_printer(http_t
*http
, char *printer
);
32 static char *get_printer_ppd(const char *uri
, char *buffer
, size_t bufsize
, int *num_options
, cups_option_t
**options
);
33 static cups_ptype_t
get_printer_type(http_t
*http
, char *printer
, char *uri
,
35 static int set_printer_options(http_t
*http
, char *printer
,
36 int num_options
, cups_option_t
*options
,
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 num_options
; /* Number of options */
56 cups_option_t
*options
; /* Options */
57 char *file
, /* New PPD file */
58 evefile
[1024] = ""; /* IPP Everywhere PPD */
59 const char *ppd_name
, /* ppd-name value */
60 *device_uri
; /* device-uri value */
71 for (i
= 1; i
< argc
; i
++)
73 if (argv
[i
][0] == '-')
75 for (opt
= argv
[i
] + 1; *opt
; opt
++)
79 case 'c' : /* Add printer to class */
82 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
86 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
94 _("lpadmin: Unable to add a printer to the class:\n"
95 " You must specify a printer name first."));
102 opt
+= strlen(opt
) - 1;
110 _cupsLangPuts(stderr
, _("lpadmin: Expected class name after \"-c\" option."));
117 if (!validate_name(pclass
))
119 _cupsLangPuts(stderr
,
120 _("lpadmin: Class name can only contain printable "
125 if (add_printer_to_class(http
, printer
, pclass
))
129 case 'd' : /* Set as default destination */
132 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
136 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
144 opt
+= strlen(opt
) - 1;
152 _cupsLangPuts(stderr
, _("lpadmin: Expected printer name after \"-d\" option."));
159 if (!validate_name(printer
))
161 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
165 if (default_printer(http
, printer
))
171 case 'h' : /* Connect to host */
180 cupsSetServer(opt
+ 1);
181 opt
+= strlen(opt
) - 1;
189 _cupsLangPuts(stderr
, _("lpadmin: Expected hostname after \"-h\" option."));
193 cupsSetServer(argv
[i
]);
197 case 'P' : /* Use the specified PPD file */
198 case 'i' : /* Use the specified PPD file */
202 opt
+= strlen(opt
) - 1;
210 _cupsLangPrintf(stderr
, _("lpadmin: Expected PPD after \"-%c\" option."), argv
[i
- 1][1]);
220 * Check to see that the specified file is, in fact, a PPD...
223 cups_file_t
*fp
= cupsFileOpen(file
, "r");
226 if (!cupsFileGets(fp
, line
, sizeof(line
)) || strncmp(line
, "*PPD-Adobe", 10))
228 _cupsLangPuts(stderr
, _("lpadmin: System V interface scripts are no longer supported for security reasons."));
237 case 'E' : /* Enable the printer/enable encryption */
241 cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED
);
244 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
246 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
247 #endif /* HAVE_SSL */
253 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
257 _cupsLangPrintf(stderr
,
258 _("lpadmin: Unable to connect to server: %s"),
264 if (enable_printer(http
, printer
))
268 case 'm' : /* Use the specified standard script/PPD file */
271 num_options
= cupsAddOption("ppd-name", opt
+ 1, num_options
, &options
);
272 opt
+= strlen(opt
) - 1;
280 _cupsLangPuts(stderr
, _("lpadmin: Expected model after \"-m\" option."));
284 num_options
= cupsAddOption("ppd-name", argv
[i
], num_options
, &options
);
288 case 'o' : /* Set option */
291 num_options
= cupsParseOptions(opt
+ 1, num_options
, &options
);
292 opt
+= strlen(opt
) - 1;
300 _cupsLangPuts(stderr
, _("lpadmin: Expected name=value after \"-o\" option."));
304 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
308 case 'p' : /* Add/modify a printer */
312 opt
+= strlen(opt
) - 1;
320 _cupsLangPuts(stderr
, _("lpadmin: Expected printer after \"-p\" option."));
327 if (!validate_name(printer
))
329 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
334 case 'r' : /* Remove printer from class */
337 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
341 _cupsLangPrintf(stderr
,
342 _("lpadmin: Unable to connect to server: %s"),
350 _cupsLangPuts(stderr
,
351 _("lpadmin: Unable to remove a printer from the class:\n"
352 " You must specify a printer name first."));
359 opt
+= strlen(opt
) - 1;
367 _cupsLangPuts(stderr
, _("lpadmin: Expected class after \"-r\" option."));
374 if (!validate_name(pclass
))
376 _cupsLangPuts(stderr
, _("lpadmin: Class name can only contain printable characters."));
380 if (delete_printer_from_class(http
, printer
, pclass
))
384 case 'R' : /* Remove option */
387 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
391 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
398 _cupsLangPuts(stderr
,
399 _("lpadmin: Unable to delete option:\n"
400 " You must specify a printer name first."));
407 opt
+= strlen(opt
) - 1;
415 _cupsLangPuts(stderr
, _("lpadmin: Expected name after \"-R\" option."));
422 if (delete_printer_option(http
, printer
, val
))
426 case 'U' : /* Username */
429 cupsSetUser(opt
+ 1);
430 opt
+= strlen(opt
) - 1;
437 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-U\" option."), argv
[0]);
441 cupsSetUser(argv
[i
]);
445 case 'u' : /* Allow/deny users */
449 opt
+= strlen(opt
) - 1;
457 _cupsLangPuts(stderr
, _("lpadmin: Expected allow/deny:userlist after \"-u\" option."));
464 if (!_cups_strncasecmp(val
, "allow:", 6))
465 num_options
= cupsAddOption("requesting-user-name-allowed", val
+ 6, num_options
, &options
);
466 else if (!_cups_strncasecmp(val
, "deny:", 5))
467 num_options
= cupsAddOption("requesting-user-name-denied", val
+ 5, num_options
, &options
);
470 _cupsLangPrintf(stderr
, _("lpadmin: Unknown allow/deny option \"%s\"."), val
);
475 case 'v' : /* Set the device-uri attribute */
478 num_options
= cupsAddOption("device-uri", opt
+ 1, num_options
, &options
);
479 opt
+= strlen(opt
) - 1;
487 _cupsLangPuts(stderr
, _("lpadmin: Expected device URI after \"-v\" option."));
491 num_options
= cupsAddOption("device-uri", argv
[i
], num_options
, &options
);
495 case 'x' : /* Delete a printer */
498 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
502 _cupsLangPrintf(stderr
,
503 _("lpadmin: Unable to connect to server: %s"),
512 opt
+= strlen(opt
) - 1;
520 _cupsLangPuts(stderr
, _("lpadmin: Expected printer or class after \"-x\" option."));
527 if (!validate_name(printer
))
529 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
533 if (delete_printer(http
, printer
))
539 case 'D' : /* Set the printer-info attribute */
542 num_options
= cupsAddOption("printer-info", opt
+ 1, num_options
, &options
);
543 opt
+= strlen(opt
) - 1;
551 _cupsLangPuts(stderr
, _("lpadmin: Expected description after \"-D\" option."));
555 num_options
= cupsAddOption("printer-info", argv
[i
], num_options
, &options
);
559 case 'I' : /* Set the supported file types (ignored) */
564 _cupsLangPuts(stderr
, _("lpadmin: Expected file type(s) after \"-I\" option."));
568 _cupsLangPuts(stderr
, _("lpadmin: Warning - content type list ignored."));
571 case 'L' : /* Set the printer-location attribute */
574 num_options
= cupsAddOption("printer-location", opt
+ 1, num_options
, &options
);
575 opt
+= strlen(opt
) - 1;
583 _cupsLangPuts(stderr
, _("lpadmin: Expected location after \"-L\" option."));
587 num_options
= cupsAddOption("printer-location", argv
[i
], num_options
, &options
);
592 _cupsLangPrintf(stderr
, _("lpadmin: Unknown option \"%c\"."), *opt
);
599 _cupsLangPrintf(stderr
, _("lpadmin: Unknown argument \"%s\"."), argv
[i
]);
605 * Set options as needed...
608 ppd_name
= cupsGetOption("ppd-name", num_options
, options
);
609 device_uri
= cupsGetOption("device-uri", num_options
, options
);
611 if (ppd_name
&& !strcmp(ppd_name
, "raw"))
613 _cupsLangPuts(stderr
, _("lpadmin: Raw queues are deprecated and will stop working in a future version of CUPS."));
615 if (device_uri
&& (!strncmp(device_uri
, "ipp://", 6) || !strncmp(device_uri
, "ipps://", 7)) && strstr(device_uri
, "/printers/"))
616 _cupsLangPuts(stderr
, _("lpadmin: Use the 'everywhere' model for shared printers."));
618 else if (ppd_name
&& !strcmp(ppd_name
, "everywhere") && device_uri
)
620 if ((file
= get_printer_ppd(device_uri
, evefile
, sizeof(evefile
), &num_options
, &options
)) == NULL
)
623 num_options
= cupsRemoveOption("ppd-name", num_options
, &options
);
625 else if (ppd_name
|| file
)
627 _cupsLangPuts(stderr
, _("lpadmin: Printer drivers are deprecated and will stop working in a future version of CUPS."));
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
))
661 _cupsLangPuts(stdout
,
664 " lpadmin [-h server] -d destination\n"
665 " lpadmin [-h server] -x destination\n"
666 " lpadmin [-h server] -p printer [-c add-class] "
667 "[-i interface] [-m model]\n"
668 " [-r remove-class] [-v device] "
670 " [-P ppd-file] [-o name=value]\n"
671 " [-u allow:user,user] "
672 "[-u deny:user,user]"));
683 * 'add_printer_to_class()' - Add a printer to a class.
686 static int /* O - 0 on success, 1 on fail */
687 add_printer_to_class(http_t
*http
, /* I - Server connection */
688 char *printer
, /* I - Printer to add */
689 char *pclass
) /* I - Class to add to */
691 int i
; /* Looping var */
692 ipp_t
*request
, /* IPP Request */
693 *response
; /* IPP Response */
694 ipp_attribute_t
*attr
, /* Current attribute */
695 *members
; /* Members in class */
696 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
699 DEBUG_printf(("add_printer_to_class(%p, \"%s\", \"%s\")\n", http
,
703 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
707 * attributes-natural-language
709 * requesting-user-name
712 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
714 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
715 "localhost", 0, "/classes/%s", pclass
);
716 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
717 "printer-uri", NULL
, uri
);
718 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
722 * Do the request and get back a response...
725 response
= cupsDoRequest(http
, request
, "/");
728 * Build a CUPS-Add-Modify-Class request, which requires the following
732 * attributes-natural-language
734 * requesting-user-name
738 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
740 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
741 "printer-uri", NULL
, uri
);
742 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
746 * See if the printer is already in the class...
749 if (response
!= NULL
&&
750 (members
= ippFindAttribute(response
, "member-names",
751 IPP_TAG_NAME
)) != NULL
)
752 for (i
= 0; i
< members
->num_values
; i
++)
753 if (_cups_strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
755 _cupsLangPrintf(stderr
,
756 _("lpadmin: Printer %s is already a member of class "
757 "%s."), printer
, pclass
);
764 * OK, the printer isn't part of the class, so add it...
767 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
768 "localhost", 0, "/printers/%s", printer
);
770 if (response
!= NULL
&&
771 (members
= ippFindAttribute(response
, "member-uris",
772 IPP_TAG_URI
)) != NULL
)
775 * Add the printer to the existing list...
778 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
779 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
780 for (i
= 0; i
< members
->num_values
; i
++)
781 attr
->values
[i
].string
.text
=
782 _cupsStrAlloc(members
->values
[i
].string
.text
);
784 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
787 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
791 * Then send the request...
796 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
797 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
799 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
809 * 'default_printer()' - Set the default printing destination.
812 static int /* O - 0 on success, 1 on fail */
813 default_printer(http_t
*http
, /* I - Server connection */
814 char *printer
) /* I - Printer name */
816 ipp_t
*request
; /* IPP Request */
817 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
820 DEBUG_printf(("default_printer(%p, \"%s\")\n", http
, printer
));
823 * Build a CUPS-Set-Default request, which requires the following
827 * attributes-natural-language
829 * requesting-user-name
832 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
833 "localhost", 0, "/printers/%s", printer
);
835 request
= ippNewRequest(IPP_OP_CUPS_SET_DEFAULT
);
837 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
838 "printer-uri", NULL
, uri
);
839 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
843 * Do the request and get back a response...
846 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
848 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
850 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
860 * 'delete_printer()' - Delete a printer from the system...
863 static int /* O - 0 on success, 1 on fail */
864 delete_printer(http_t
*http
, /* I - Server connection */
865 char *printer
) /* I - Printer to delete */
867 ipp_t
*request
; /* IPP Request */
868 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
871 DEBUG_printf(("delete_printer(%p, \"%s\")\n", http
, printer
));
874 * Build a CUPS-Delete-Printer request, which requires the following
878 * attributes-natural-language
880 * requesting-user-name
883 request
= ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER
);
885 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
886 "localhost", 0, "/printers/%s", printer
);
887 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
888 "printer-uri", NULL
, uri
);
889 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
893 * Do the request and get back a response...
896 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
898 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
900 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
910 * 'delete_printer_from_class()' - Delete a printer from a class.
913 static int /* O - 0 on success, 1 on fail */
914 delete_printer_from_class(
915 http_t
*http
, /* I - Server connection */
916 char *printer
, /* I - Printer to remove */
917 char *pclass
) /* I - Class to remove from */
919 int i
, j
, k
; /* Looping vars */
920 ipp_t
*request
, /* IPP Request */
921 *response
; /* IPP Response */
922 ipp_attribute_t
*attr
, /* Current attribute */
923 *members
; /* Members in class */
924 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
927 DEBUG_printf(("delete_printer_from_class(%p, \"%s\", \"%s\")\n", http
,
931 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
935 * attributes-natural-language
937 * requesting-user-name
940 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
942 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
943 "localhost", 0, "/classes/%s", pclass
);
944 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
945 "printer-uri", NULL
, uri
);
946 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
950 * Do the request and get back a response...
953 if ((response
= cupsDoRequest(http
, request
, "/classes/")) == NULL
||
954 response
->request
.status
.status_code
== IPP_STATUS_ERROR_NOT_FOUND
)
956 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
964 * See if the printer is already in the class...
967 if ((members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) == NULL
)
969 _cupsLangPuts(stderr
, _("lpadmin: No member names were seen."));
976 for (i
= 0; i
< members
->num_values
; i
++)
977 if (!_cups_strcasecmp(printer
, members
->values
[i
].string
.text
))
980 if (i
>= members
->num_values
)
982 _cupsLangPrintf(stderr
,
983 _("lpadmin: Printer %s is not a member of class %s."),
991 if (members
->num_values
== 1)
994 * Build a CUPS-Delete-Class request, which requires the following
998 * attributes-natural-language
1000 * requesting-user-name
1003 request
= ippNewRequest(IPP_OP_CUPS_DELETE_CLASS
);
1005 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1006 "printer-uri", NULL
, uri
);
1007 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1008 "requesting-user-name", NULL
, cupsUser());
1013 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
1016 * attributes-charset
1017 * attributes-natural-language
1019 * requesting-user-name
1023 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1025 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1026 "printer-uri", NULL
, uri
);
1027 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1028 "requesting-user-name", NULL
, cupsUser());
1031 * Delete the printer from the class...
1034 members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
);
1035 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1036 "member-uris", members
->num_values
- 1, NULL
, NULL
);
1038 for (j
= 0, k
= 0; j
< members
->num_values
; j
++)
1040 attr
->values
[k
++].string
.text
=
1041 _cupsStrAlloc(members
->values
[j
].string
.text
);
1045 * Then send the request...
1048 ippDelete(response
);
1050 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1052 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1054 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1064 * 'delete_printer_option()' - Delete a printer option.
1067 static int /* O - 0 on success, 1 on fail */
1068 delete_printer_option(http_t
*http
, /* I - Server connection */
1069 char *printer
, /* I - Printer */
1070 char *option
) /* I - Option to delete */
1072 ipp_t
*request
; /* IPP request */
1073 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1077 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1078 * requires the following attributes:
1080 * attributes-charset
1081 * attributes-natural-language
1083 * requesting-user-name
1084 * option with deleteAttr tag
1087 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1088 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1090 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1092 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1093 "printer-uri", NULL
, uri
);
1094 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1095 "requesting-user-name", NULL
, cupsUser());
1096 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_DELETEATTR
, option
, 0);
1099 * Do the request and get back a response...
1102 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1104 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1106 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1116 * 'enable_printer()' - Enable a printer...
1119 static int /* O - 0 on success, 1 on fail */
1120 enable_printer(http_t
*http
, /* I - Server connection */
1121 char *printer
) /* I - Printer to enable */
1123 ipp_t
*request
; /* IPP Request */
1124 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1127 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http
, printer
));
1130 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1131 * require the following attributes:
1133 * attributes-charset
1134 * attributes-natural-language
1136 * requesting-user-name
1138 * printer-is-accepting-jobs
1141 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1142 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1144 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1146 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1147 "printer-uri", NULL
, uri
);
1148 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1149 "requesting-user-name", NULL
, cupsUser());
1150 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1152 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1155 * Do the request and get back a response...
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 (_ppdCreateFromIPP(buffer
, bufsize
, response
))
1245 if (!cupsGetOption("printer-geo-location", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-geo-location", IPP_TAG_URI
)) != NULL
)
1246 *num_options
= cupsAddOption("printer-geo-location", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1248 if (!cupsGetOption("printer-info", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-info", IPP_TAG_TEXT
)) != NULL
)
1249 *num_options
= cupsAddOption("printer-info", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1251 if (!cupsGetOption("printer-location", *num_options
, *options
) && (attr
= ippFindAttribute(response
, "printer-location", IPP_TAG_TEXT
)) != NULL
)
1252 *num_options
= cupsAddOption("printer-location", ippGetString(attr
, 0, NULL
), *num_options
, options
);
1255 _cupsLangPrintf(stderr
, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno
));
1257 ippDelete(response
);
1268 * 'get_printer_type()' - Determine the printer type and URI.
1271 static cups_ptype_t
/* O - printer-type value */
1272 get_printer_type(http_t
*http
, /* I - Server connection */
1273 char *printer
, /* I - Printer name */
1274 char *uri
, /* I - URI buffer */
1275 size_t urisize
) /* I - Size of URI buffer */
1277 ipp_t
*request
, /* IPP request */
1278 *response
; /* IPP response */
1279 ipp_attribute_t
*attr
; /* printer-type attribute */
1280 cups_ptype_t type
; /* printer-type value */
1284 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1287 * attributes-charset
1288 * attributes-natural-language
1290 * requested-attributes
1291 * requesting-user-name
1294 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1296 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1297 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1298 "printer-uri", NULL
, uri
);
1299 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1300 "requested-attributes", NULL
, "printer-type");
1301 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1302 "requesting-user-name", NULL
, cupsUser());
1308 response
= cupsDoRequest(http
, request
, "/");
1309 if ((attr
= ippFindAttribute(response
, "printer-type",
1310 IPP_TAG_ENUM
)) != NULL
)
1312 type
= (cups_ptype_t
)attr
->values
[0].integer
;
1314 if (type
& CUPS_PRINTER_CLASS
)
1315 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/classes/%s", printer
);
1318 type
= CUPS_PRINTER_LOCAL
;
1320 ippDelete(response
);
1327 * 'set_printer_options()' - Set the printer options.
1330 static int /* O - 0 on success, 1 on fail */
1331 set_printer_options(
1332 http_t
*http
, /* I - Server connection */
1333 char *printer
, /* I - Printer */
1334 int num_options
, /* I - Number of options */
1335 cups_option_t
*options
, /* I - Options */
1336 char *file
) /* I - PPD file/interface script */
1338 ipp_t
*request
; /* IPP Request */
1339 const char *ppdfile
; /* PPD filename */
1340 int ppdchanged
= 0; /* PPD changed? */
1341 ppd_file_t
*ppd
; /* PPD file */
1342 ppd_choice_t
*choice
; /* Marked choice */
1343 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1344 line
[1024], /* Line from PPD file */
1345 keyword
[1024], /* Keyword from Default line */
1346 *keyptr
, /* Pointer into keyword... */
1347 tempfile
[1024]; /* Temporary filename */
1348 cups_file_t
*in
, /* PPD file */
1349 *out
; /* Temporary file */
1350 const char *ppdname
, /* ppd-name value */
1351 *protocol
, /* Old protocol option */
1352 *customval
, /* Custom option value */
1353 *boolval
; /* Boolean value */
1354 int wrote_ipp_supplies
= 0, /* Wrote cupsIPPSupplies keyword? */
1355 wrote_snmp_supplies
= 0,/* Wrote cupsSNMPSupplies keyword? */
1356 copied_options
= 0; /* Copied options? */
1359 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1360 "options=%p, file=\"%s\")\n", http
, printer
, num_options
,
1364 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1365 * which requires the following attributes:
1367 * attributes-charset
1368 * attributes-natural-language
1370 * requesting-user-name
1374 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1375 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1377 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1379 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1380 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1383 * Add the options...
1388 else if ((ppdname
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& strcmp(ppdname
, "raw") && num_options
> 1)
1390 if ((ppdfile
= cupsGetServerPPD(http
, ppdname
)) != NULL
)
1393 * Copy options array and remove ppd-name from it...
1396 cups_option_t
*temp
= NULL
, *optr
;
1397 int i
, num_temp
= 0;
1398 for (i
= num_options
, optr
= options
; i
> 0; i
--, optr
++)
1399 if (strcmp(optr
->name
, "ppd-name"))
1400 num_temp
= cupsAddOption(optr
->name
, optr
->value
, num_temp
, &temp
);
1404 num_options
= num_temp
;
1408 else if (request
->request
.op
.operation_id
== IPP_OP_CUPS_ADD_MODIFY_PRINTER
)
1409 ppdfile
= cupsGetPPD(printer
);
1413 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
1414 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1416 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1418 if (!_cups_strcasecmp(protocol
, "bcp"))
1419 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1421 else if (!_cups_strcasecmp(protocol
, "tbcp"))
1422 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1429 * Set default options in the PPD file...
1432 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
1434 int linenum
; /* Line number of error */
1435 ppd_status_t status
= ppdLastError(&linenum
);
1438 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile
, ppdErrorString(status
), linenum
);
1441 ppdMarkDefaults(ppd
);
1442 cupsMarkOptions(ppd
, num_options
, options
);
1444 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1446 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1448 if (ppdfile
!= file
)
1451 cupsFreeOptions(num_options
, options
);
1455 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1457 _cupsLangPrintf(stderr
,
1458 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1459 ppdfile
, strerror(errno
));
1461 if (ppdfile
!= file
)
1464 cupsFreeOptions(num_options
, options
);
1470 while (cupsFileGets(in
, line
, sizeof(line
)))
1472 if (!strncmp(line
, "*cupsIPPSupplies:", 17) &&
1473 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1476 wrote_ipp_supplies
= 1;
1477 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1478 (!_cups_strcasecmp(boolval
, "true") ||
1479 !_cups_strcasecmp(boolval
, "yes") ||
1480 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1482 else if (!strncmp(line
, "*cupsSNMPSupplies:", 18) &&
1483 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1486 wrote_snmp_supplies
= 1;
1487 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1488 (!_cups_strcasecmp(boolval
, "true") ||
1489 !_cups_strcasecmp(boolval
, "yes") ||
1490 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1492 else if (strncmp(line
, "*Default", 8))
1493 cupsFilePrintf(out
, "%s\n", line
);
1497 * Get default option name...
1500 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
1502 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1503 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1507 while (isspace(*keyptr
& 255))
1510 if (!strcmp(keyword
, "PageRegion") ||
1511 !strcmp(keyword
, "PageSize") ||
1512 !strcmp(keyword
, "PaperDimension") ||
1513 !strcmp(keyword
, "ImageableArea"))
1515 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1516 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1519 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1521 if (choice
&& strcmp(choice
->choice
, keyptr
))
1523 if (strcmp(choice
->choice
, "Custom"))
1525 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1528 else if ((customval
= cupsGetOption(keyword
, num_options
,
1531 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1535 cupsFilePrintf(out
, "%s\n", line
);
1538 cupsFilePrintf(out
, "%s\n", line
);
1542 if (!wrote_ipp_supplies
&&
1543 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1546 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1547 (!_cups_strcasecmp(boolval
, "true") ||
1548 !_cups_strcasecmp(boolval
, "yes") ||
1549 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1552 if (!wrote_snmp_supplies
&&
1553 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1556 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1557 (!_cups_strcasecmp(boolval
, "true") ||
1558 !_cups_strcasecmp(boolval
, "yes") ||
1559 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1570 ippDelete(cupsDoFileRequest(http
, request
, "/admin/",
1571 ppdchanged
? tempfile
: file
));
1574 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1578 if (ppdfile
!= file
)
1585 * No PPD file - just set the options...
1588 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1592 cupsFreeOptions(num_options
, options
);
1595 * Check the response...
1598 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1600 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1610 * 'validate_name()' - Make sure the printer name only contains valid chars.
1613 static int /* O - 0 if name is no good, 1 if name is good */
1614 validate_name(const char *name
) /* I - Name to check */
1616 const char *ptr
; /* Pointer into name */
1620 * Scan the whole name...
1623 for (ptr
= name
; *ptr
; ptr
++)
1626 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '\\' || *ptr
== '?' || *ptr
== '\'' || *ptr
== '\"' || *ptr
== '#')
1630 * All the characters are good; validate the length, too...
1633 return ((ptr
- name
) < 128);