2 * "lpadmin" command for CUPS.
4 * Copyright 2007-2016 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 * file is 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
);
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
,
42 static int validate_name(const char *name
);
46 * 'main()' - Parse options and configure the scheduler.
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 num_options
; /* Number of options */
60 cups_option_t
*options
; /* Options */
61 char *file
, /* New PPD file */
63 /* 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]);
223 case 'E' : /* Enable the printer/enable encryption */
227 cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED
);
230 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
232 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
233 #endif /* HAVE_SSL */
239 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
243 _cupsLangPrintf(stderr
,
244 _("lpadmin: Unable to connect to server: %s"),
250 if (enable_printer(http
, printer
))
254 case 'm' : /* Use the specified standard script/PPD file */
257 num_options
= cupsAddOption("ppd-name", opt
+ 1, num_options
, &options
);
258 opt
+= strlen(opt
) - 1;
266 _cupsLangPuts(stderr
, _("lpadmin: Expected model after \"-m\" option."));
270 num_options
= cupsAddOption("ppd-name", argv
[i
], num_options
, &options
);
274 case 'o' : /* Set option */
277 num_options
= cupsParseOptions(opt
+ 1, num_options
, &options
);
278 opt
+= strlen(opt
) - 1;
286 _cupsLangPuts(stderr
, _("lpadmin: Expected name=value after \"-o\" option."));
290 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
294 case 'p' : /* Add/modify a printer */
298 opt
+= strlen(opt
) - 1;
306 _cupsLangPuts(stderr
, _("lpadmin: Expected printer after \"-p\" option."));
313 if (!validate_name(printer
))
315 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
320 case 'r' : /* Remove printer from class */
323 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
327 _cupsLangPrintf(stderr
,
328 _("lpadmin: Unable to connect to server: %s"),
336 _cupsLangPuts(stderr
,
337 _("lpadmin: Unable to remove a printer from the class:\n"
338 " You must specify a printer name first."));
345 opt
+= strlen(opt
) - 1;
353 _cupsLangPuts(stderr
, _("lpadmin: Expected class after \"-r\" option."));
360 if (!validate_name(pclass
))
362 _cupsLangPuts(stderr
, _("lpadmin: Class name can only contain printable characters."));
366 if (delete_printer_from_class(http
, printer
, pclass
))
370 case 'R' : /* Remove option */
373 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
377 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
384 _cupsLangPuts(stderr
,
385 _("lpadmin: Unable to delete option:\n"
386 " You must specify a printer name first."));
393 opt
+= strlen(opt
) - 1;
401 _cupsLangPuts(stderr
, _("lpadmin: Expected name after \"-R\" option."));
408 if (delete_printer_option(http
, printer
, val
))
412 case 'U' : /* Username */
415 cupsSetUser(opt
+ 1);
416 opt
+= strlen(opt
) - 1;
423 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-U\" option."), argv
[0]);
427 cupsSetUser(argv
[i
]);
431 case 'u' : /* Allow/deny users */
435 opt
+= strlen(opt
) - 1;
443 _cupsLangPuts(stderr
, _("lpadmin: Expected allow/deny:userlist after \"-u\" option."));
450 if (!_cups_strncasecmp(val
, "allow:", 6))
451 num_options
= cupsAddOption("requesting-user-name-allowed", val
+ 6, num_options
, &options
);
452 else if (!_cups_strncasecmp(val
, "deny:", 5))
453 num_options
= cupsAddOption("requesting-user-name-denied", val
+ 5, num_options
, &options
);
456 _cupsLangPrintf(stderr
, _("lpadmin: Unknown allow/deny option \"%s\"."), val
);
461 case 'v' : /* Set the device-uri attribute */
464 num_options
= cupsAddOption("device-uri", opt
+ 1, num_options
, &options
);
465 opt
+= strlen(opt
) - 1;
473 _cupsLangPuts(stderr
, _("lpadmin: Expected device URI after \"-v\" option."));
477 num_options
= cupsAddOption("device-uri", argv
[i
], num_options
, &options
);
481 case 'x' : /* Delete a printer */
484 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
488 _cupsLangPrintf(stderr
,
489 _("lpadmin: Unable to connect to server: %s"),
498 opt
+= strlen(opt
) - 1;
506 _cupsLangPuts(stderr
, _("lpadmin: Expected printer or class after \"-x\" option."));
513 if (!validate_name(printer
))
515 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
519 if (delete_printer(http
, printer
))
525 case 'D' : /* Set the printer-info attribute */
528 num_options
= cupsAddOption("printer-info", opt
+ 1, num_options
, &options
);
529 opt
+= strlen(opt
) - 1;
537 _cupsLangPuts(stderr
, _("lpadmin: Expected description after \"-D\" option."));
541 num_options
= cupsAddOption("printer-info", argv
[i
], num_options
, &options
);
545 case 'I' : /* Set the supported file types (ignored) */
550 _cupsLangPuts(stderr
, _("lpadmin: Expected file type(s) after \"-I\" option."));
554 _cupsLangPuts(stderr
, _("lpadmin: Warning - content type list ignored."));
557 case 'L' : /* Set the printer-location attribute */
560 num_options
= cupsAddOption("printer-location", opt
+ 1, num_options
, &options
);
561 opt
+= strlen(opt
) - 1;
569 _cupsLangPuts(stderr
, _("lpadmin: Expected location after \"-L\" option."));
573 num_options
= cupsAddOption("printer-location", argv
[i
], num_options
, &options
);
578 _cupsLangPrintf(stderr
, _("lpadmin: Unknown option \"%c\"."), *opt
);
585 _cupsLangPrintf(stderr
, _("lpadmin: Unknown argument \"%s\"."), argv
[i
]);
591 * Set options as needed...
594 if ((ppd_name
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& !strcmp(ppd_name
, "everywhere") && (device_uri
= cupsGetOption("device-uri", num_options
, options
)) != NULL
)
596 if ((file
= get_printer_ppd(device_uri
, evefile
, sizeof(evefile
))) == NULL
)
599 num_options
= cupsRemoveOption("ppd-name", num_options
, &options
);
602 if (num_options
|| file
)
606 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
610 _cupsLangPrintf(stderr
,
611 _("lpadmin: Unable to connect to server: %s"),
619 _cupsLangPuts(stderr
,
620 _("lpadmin: Unable to set the printer options:\n"
621 " You must specify a printer name first."));
625 if (set_printer_options(http
, printer
, num_options
, options
, file
))
634 _cupsLangPuts(stdout
,
637 " lpadmin [-h server] -d destination\n"
638 " lpadmin [-h server] -x destination\n"
639 " lpadmin [-h server] -p printer [-c add-class] "
640 "[-i interface] [-m model]\n"
641 " [-r remove-class] [-v device] "
643 " [-P ppd-file] [-o name=value]\n"
644 " [-u allow:user,user] "
645 "[-u deny:user,user]"));
656 * 'add_printer_to_class()' - Add a printer to a class.
659 static int /* O - 0 on success, 1 on fail */
660 add_printer_to_class(http_t
*http
, /* I - Server connection */
661 char *printer
, /* I - Printer to add */
662 char *pclass
) /* I - Class to add to */
664 int i
; /* Looping var */
665 ipp_t
*request
, /* IPP Request */
666 *response
; /* IPP Response */
667 ipp_attribute_t
*attr
, /* Current attribute */
668 *members
; /* Members in class */
669 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
672 DEBUG_printf(("add_printer_to_class(%p, \"%s\", \"%s\")\n", http
,
676 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
680 * attributes-natural-language
682 * requesting-user-name
685 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
687 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
688 "localhost", 0, "/classes/%s", pclass
);
689 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
690 "printer-uri", NULL
, uri
);
691 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
695 * Do the request and get back a response...
698 response
= cupsDoRequest(http
, request
, "/");
701 * Build a CUPS-Add-Modify-Class request, which requires the following
705 * attributes-natural-language
707 * requesting-user-name
711 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
713 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
714 "printer-uri", NULL
, uri
);
715 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
719 * See if the printer is already in the class...
722 if (response
!= NULL
&&
723 (members
= ippFindAttribute(response
, "member-names",
724 IPP_TAG_NAME
)) != NULL
)
725 for (i
= 0; i
< members
->num_values
; i
++)
726 if (_cups_strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
728 _cupsLangPrintf(stderr
,
729 _("lpadmin: Printer %s is already a member of class "
730 "%s."), printer
, pclass
);
737 * OK, the printer isn't part of the class, so add it...
740 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
741 "localhost", 0, "/printers/%s", printer
);
743 if (response
!= NULL
&&
744 (members
= ippFindAttribute(response
, "member-uris",
745 IPP_TAG_URI
)) != NULL
)
748 * Add the printer to the existing list...
751 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
752 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
753 for (i
= 0; i
< members
->num_values
; i
++)
754 attr
->values
[i
].string
.text
=
755 _cupsStrAlloc(members
->values
[i
].string
.text
);
757 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
760 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
764 * Then send the request...
769 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
770 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
772 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
782 * 'default_printer()' - Set the default printing destination.
785 static int /* O - 0 on success, 1 on fail */
786 default_printer(http_t
*http
, /* I - Server connection */
787 char *printer
) /* I - Printer name */
789 ipp_t
*request
; /* IPP Request */
790 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
793 DEBUG_printf(("default_printer(%p, \"%s\")\n", http
, printer
));
796 * Build a CUPS-Set-Default request, which requires the following
800 * attributes-natural-language
802 * requesting-user-name
805 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
806 "localhost", 0, "/printers/%s", printer
);
808 request
= ippNewRequest(IPP_OP_CUPS_SET_DEFAULT
);
810 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
811 "printer-uri", NULL
, uri
);
812 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
816 * Do the request and get back a response...
819 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
821 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
823 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
833 * 'delete_printer()' - Delete a printer from the system...
836 static int /* O - 0 on success, 1 on fail */
837 delete_printer(http_t
*http
, /* I - Server connection */
838 char *printer
) /* I - Printer to delete */
840 ipp_t
*request
; /* IPP Request */
841 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
844 DEBUG_printf(("delete_printer(%p, \"%s\")\n", http
, printer
));
847 * Build a CUPS-Delete-Printer request, which requires the following
851 * attributes-natural-language
853 * requesting-user-name
856 request
= ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER
);
858 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
859 "localhost", 0, "/printers/%s", printer
);
860 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
861 "printer-uri", NULL
, uri
);
862 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
866 * Do the request and get back a response...
869 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
871 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
873 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
883 * 'delete_printer_from_class()' - Delete a printer from a class.
886 static int /* O - 0 on success, 1 on fail */
887 delete_printer_from_class(
888 http_t
*http
, /* I - Server connection */
889 char *printer
, /* I - Printer to remove */
890 char *pclass
) /* I - Class to remove from */
892 int i
, j
, k
; /* Looping vars */
893 ipp_t
*request
, /* IPP Request */
894 *response
; /* IPP Response */
895 ipp_attribute_t
*attr
, /* Current attribute */
896 *members
; /* Members in class */
897 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
900 DEBUG_printf(("delete_printer_from_class(%p, \"%s\", \"%s\")\n", http
,
904 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
908 * attributes-natural-language
910 * requesting-user-name
913 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
915 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
916 "localhost", 0, "/classes/%s", pclass
);
917 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
918 "printer-uri", NULL
, uri
);
919 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
923 * Do the request and get back a response...
926 if ((response
= cupsDoRequest(http
, request
, "/classes/")) == NULL
||
927 response
->request
.status
.status_code
== IPP_STATUS_ERROR_NOT_FOUND
)
929 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
937 * See if the printer is already in the class...
940 if ((members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) == NULL
)
942 _cupsLangPuts(stderr
, _("lpadmin: No member names were seen."));
949 for (i
= 0; i
< members
->num_values
; i
++)
950 if (!_cups_strcasecmp(printer
, members
->values
[i
].string
.text
))
953 if (i
>= members
->num_values
)
955 _cupsLangPrintf(stderr
,
956 _("lpadmin: Printer %s is not a member of class %s."),
964 if (members
->num_values
== 1)
967 * Build a CUPS-Delete-Class request, which requires the following
971 * attributes-natural-language
973 * requesting-user-name
976 request
= ippNewRequest(IPP_OP_CUPS_DELETE_CLASS
);
978 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
979 "printer-uri", NULL
, uri
);
980 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
981 "requesting-user-name", NULL
, cupsUser());
986 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
990 * attributes-natural-language
992 * requesting-user-name
996 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
998 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
999 "printer-uri", NULL
, uri
);
1000 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1001 "requesting-user-name", NULL
, cupsUser());
1004 * Delete the printer from the class...
1007 members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
);
1008 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1009 "member-uris", members
->num_values
- 1, NULL
, NULL
);
1011 for (j
= 0, k
= 0; j
< members
->num_values
; j
++)
1013 attr
->values
[k
++].string
.text
=
1014 _cupsStrAlloc(members
->values
[j
].string
.text
);
1018 * Then send the request...
1021 ippDelete(response
);
1023 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1025 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1027 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1037 * 'delete_printer_option()' - Delete a printer option.
1040 static int /* O - 0 on success, 1 on fail */
1041 delete_printer_option(http_t
*http
, /* I - Server connection */
1042 char *printer
, /* I - Printer */
1043 char *option
) /* I - Option to delete */
1045 ipp_t
*request
; /* IPP request */
1046 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1050 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1051 * requires the following attributes:
1053 * attributes-charset
1054 * attributes-natural-language
1056 * requesting-user-name
1057 * option with deleteAttr tag
1060 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1061 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1063 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1065 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1066 "printer-uri", NULL
, uri
);
1067 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1068 "requesting-user-name", NULL
, cupsUser());
1069 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_DELETEATTR
, option
, 0);
1072 * Do the request and get back a response...
1075 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1077 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1079 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1089 * 'enable_printer()' - Enable a printer...
1092 static int /* O - 0 on success, 1 on fail */
1093 enable_printer(http_t
*http
, /* I - Server connection */
1094 char *printer
) /* I - Printer to enable */
1096 ipp_t
*request
; /* IPP Request */
1097 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1100 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http
, printer
));
1103 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1104 * require the following attributes:
1106 * attributes-charset
1107 * attributes-natural-language
1109 * requesting-user-name
1111 * printer-is-accepting-jobs
1114 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1115 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1117 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1119 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1120 "printer-uri", NULL
, uri
);
1121 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1122 "requesting-user-name", NULL
, cupsUser());
1123 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1125 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1128 * Do the request and get back a response...
1131 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1133 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1135 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1145 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
1148 static char * /* O - Filename or NULL */
1149 get_printer_ppd(const char *uri
, /* I - Printer URI */
1150 char *buffer
, /* I - Filename buffer */
1151 size_t bufsize
) /* I - Size of filename buffer */
1153 http_t
*http
; /* Connection to printer */
1154 ipp_t
*request
, /* Get-Printer-Attributes request */
1155 *response
; /* Get-Printer-Attributes response */
1156 char resolved
[1024], /* Resolved URI */
1157 scheme
[32], /* URI scheme */
1158 userpass
[256], /* Username:password */
1159 host
[256], /* Hostname */
1160 resource
[256]; /* Resource path */
1161 int port
; /* Port number */
1165 * Connect to the printer...
1168 if (strstr(uri
, "._tcp"))
1174 if (!_httpResolveURI(uri
, resolved
, sizeof(resolved
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
1176 _cupsLangPrintf(stderr
, _("%s: Unable to resolve \"%s\"."), "lpadmin", uri
);
1183 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
1185 _cupsLangPrintf(stderr
, _("%s: Bad printer URI \"%s\"."), "lpadmin", uri
);
1189 http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, !strcmp(scheme
, "ipps") ? HTTP_ENCRYPTION_ALWAYS
: HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
);
1192 _cupsLangPrintf(stderr
, _("%s: Unable to connect to \"%s:%d\": %s"), "lpadmin", host
, port
, cupsLastErrorString());
1197 * Send a Get-Printer-Attributes request...
1200 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1201 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1202 response
= cupsDoRequest(http
, request
, resource
);
1204 if (!_ppdCreateFromIPP(buffer
, bufsize
, response
))
1205 _cupsLangPrintf(stderr
, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno
));
1207 ippDelete(response
);
1218 * 'get_printer_type()' - Determine the printer type and URI.
1221 static cups_ptype_t
/* O - printer-type value */
1222 get_printer_type(http_t
*http
, /* I - Server connection */
1223 char *printer
, /* I - Printer name */
1224 char *uri
, /* I - URI buffer */
1225 size_t urisize
) /* I - Size of URI buffer */
1227 ipp_t
*request
, /* IPP request */
1228 *response
; /* IPP response */
1229 ipp_attribute_t
*attr
; /* printer-type attribute */
1230 cups_ptype_t type
; /* printer-type value */
1234 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1237 * attributes-charset
1238 * attributes-natural-language
1240 * requested-attributes
1241 * requesting-user-name
1244 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1246 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1247 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1248 "printer-uri", NULL
, uri
);
1249 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1250 "requested-attributes", NULL
, "printer-type");
1251 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1252 "requesting-user-name", NULL
, cupsUser());
1258 response
= cupsDoRequest(http
, request
, "/");
1259 if ((attr
= ippFindAttribute(response
, "printer-type",
1260 IPP_TAG_ENUM
)) != NULL
)
1262 type
= (cups_ptype_t
)attr
->values
[0].integer
;
1264 if (type
& CUPS_PRINTER_CLASS
)
1265 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/classes/%s", printer
);
1268 type
= CUPS_PRINTER_LOCAL
;
1270 ippDelete(response
);
1277 * 'set_printer_options()' - Set the printer options.
1280 static int /* O - 0 on success, 1 on fail */
1281 set_printer_options(
1282 http_t
*http
, /* I - Server connection */
1283 char *printer
, /* I - Printer */
1284 int num_options
, /* I - Number of options */
1285 cups_option_t
*options
, /* I - Options */
1286 char *file
) /* I - PPD file/interface script */
1288 ipp_t
*request
; /* IPP Request */
1289 const char *ppdfile
; /* PPD filename */
1290 int ppdchanged
= 0; /* PPD changed? */
1291 ppd_file_t
*ppd
; /* PPD file */
1292 ppd_choice_t
*choice
; /* Marked choice */
1293 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1294 line
[1024], /* Line from PPD file */
1295 keyword
[1024], /* Keyword from Default line */
1296 *keyptr
, /* Pointer into keyword... */
1297 tempfile
[1024]; /* Temporary filename */
1298 cups_file_t
*in
, /* PPD file */
1299 *out
; /* Temporary file */
1300 const char *ppdname
, /* ppd-name value */
1301 *protocol
, /* Old protocol option */
1302 *customval
, /* Custom option value */
1303 *boolval
; /* Boolean value */
1304 int wrote_ipp_supplies
= 0, /* Wrote cupsIPPSupplies keyword? */
1305 wrote_snmp_supplies
= 0,/* Wrote cupsSNMPSupplies keyword? */
1306 copied_options
= 0; /* Copied options? */
1309 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1310 "options=%p, file=\"%s\")\n", http
, printer
, num_options
,
1314 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1315 * which requires the following attributes:
1317 * attributes-charset
1318 * attributes-natural-language
1320 * requesting-user-name
1324 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1325 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1327 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1329 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1330 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1333 * Add the options...
1338 else if ((ppdname
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& strcmp(ppdname
, "raw") && num_options
> 1)
1340 if ((ppdfile
= cupsGetServerPPD(http
, ppdname
)) != NULL
)
1343 * Copy options array and remove ppd-name from it...
1346 cups_option_t
*temp
= NULL
, *optr
;
1347 int i
, num_temp
= 0;
1348 for (i
= num_options
, optr
= options
; i
> 0; i
--, optr
++)
1349 if (strcmp(optr
->name
, "ppd-name"))
1350 num_temp
= cupsAddOption(optr
->name
, optr
->value
, num_temp
, &temp
);
1354 num_options
= num_temp
;
1358 else if (request
->request
.op
.operation_id
== IPP_OP_CUPS_ADD_MODIFY_PRINTER
)
1359 ppdfile
= cupsGetPPD(printer
);
1363 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
1364 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1366 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1368 if (!_cups_strcasecmp(protocol
, "bcp"))
1369 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1371 else if (!_cups_strcasecmp(protocol
, "tbcp"))
1372 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1379 * Set default options in the PPD file...
1382 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
1384 int linenum
; /* Line number of error */
1385 ppd_status_t status
= ppdLastError(&linenum
);
1388 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile
, ppdErrorString(status
), linenum
);
1391 ppdMarkDefaults(ppd
);
1392 cupsMarkOptions(ppd
, num_options
, options
);
1394 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1396 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1398 if (ppdfile
!= file
)
1401 cupsFreeOptions(num_options
, options
);
1405 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1407 _cupsLangPrintf(stderr
,
1408 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1409 ppdfile
, strerror(errno
));
1411 if (ppdfile
!= file
)
1414 cupsFreeOptions(num_options
, options
);
1420 while (cupsFileGets(in
, line
, sizeof(line
)))
1422 if (!strncmp(line
, "*cupsIPPSupplies:", 17) &&
1423 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1426 wrote_ipp_supplies
= 1;
1427 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1428 (!_cups_strcasecmp(boolval
, "true") ||
1429 !_cups_strcasecmp(boolval
, "yes") ||
1430 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1432 else if (!strncmp(line
, "*cupsSNMPSupplies:", 18) &&
1433 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1436 wrote_snmp_supplies
= 1;
1437 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1438 (!_cups_strcasecmp(boolval
, "true") ||
1439 !_cups_strcasecmp(boolval
, "yes") ||
1440 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1442 else if (strncmp(line
, "*Default", 8))
1443 cupsFilePrintf(out
, "%s\n", line
);
1447 * Get default option name...
1450 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
1452 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1453 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1457 while (isspace(*keyptr
& 255))
1460 if (!strcmp(keyword
, "PageRegion") ||
1461 !strcmp(keyword
, "PageSize") ||
1462 !strcmp(keyword
, "PaperDimension") ||
1463 !strcmp(keyword
, "ImageableArea"))
1465 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1466 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1469 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1471 if (choice
&& strcmp(choice
->choice
, keyptr
))
1473 if (strcmp(choice
->choice
, "Custom"))
1475 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1478 else if ((customval
= cupsGetOption(keyword
, num_options
,
1481 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1485 cupsFilePrintf(out
, "%s\n", line
);
1488 cupsFilePrintf(out
, "%s\n", line
);
1492 if (!wrote_ipp_supplies
&&
1493 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1496 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1497 (!_cups_strcasecmp(boolval
, "true") ||
1498 !_cups_strcasecmp(boolval
, "yes") ||
1499 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1502 if (!wrote_snmp_supplies
&&
1503 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1506 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1507 (!_cups_strcasecmp(boolval
, "true") ||
1508 !_cups_strcasecmp(boolval
, "yes") ||
1509 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1520 ippDelete(cupsDoFileRequest(http
, request
, "/admin/",
1521 ppdchanged
? tempfile
: file
));
1524 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1528 if (ppdfile
!= file
)
1535 * No PPD file - just set the options...
1538 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1542 cupsFreeOptions(num_options
, options
);
1545 * Check the response...
1548 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1550 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1560 * 'validate_name()' - Make sure the printer name only contains valid chars.
1563 static int /* O - 0 if name is no good, 1 if name is good */
1564 validate_name(const char *name
) /* I - Name to check */
1566 const char *ptr
; /* Pointer into name */
1570 * Scan the whole name...
1573 for (ptr
= name
; *ptr
; ptr
++)
1576 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' ||
1581 * All the characters are good; validate the length, too...
1584 return ((ptr
- name
) < 128);