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 * 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 _cupsLangPuts(stderr
,
607 _("lpadmin: Unable to set the printer options:\n"
608 " You must specify a printer name first."));
614 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
615 cupsEncryption(), 1, 30000, NULL
);
618 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"),
624 if (set_printer_options(http
, printer
, num_options
, options
, file
))
633 _cupsLangPuts(stdout
,
636 " lpadmin [-h server] -d destination\n"
637 " lpadmin [-h server] -x destination\n"
638 " lpadmin [-h server] -p printer [-c add-class] "
639 "[-i interface] [-m model]\n"
640 " [-r remove-class] [-v device] "
642 " [-P ppd-file] [-o name=value]\n"
643 " [-u allow:user,user] "
644 "[-u deny:user,user]"));
655 * 'add_printer_to_class()' - Add a printer to a class.
658 static int /* O - 0 on success, 1 on fail */
659 add_printer_to_class(http_t
*http
, /* I - Server connection */
660 char *printer
, /* I - Printer to add */
661 char *pclass
) /* I - Class to add to */
663 int i
; /* Looping var */
664 ipp_t
*request
, /* IPP Request */
665 *response
; /* IPP Response */
666 ipp_attribute_t
*attr
, /* Current attribute */
667 *members
; /* Members in class */
668 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
671 DEBUG_printf(("add_printer_to_class(%p, \"%s\", \"%s\")\n", http
,
675 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
679 * attributes-natural-language
681 * requesting-user-name
684 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
686 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
687 "localhost", 0, "/classes/%s", pclass
);
688 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
689 "printer-uri", NULL
, uri
);
690 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
694 * Do the request and get back a response...
697 response
= cupsDoRequest(http
, request
, "/");
700 * Build a CUPS-Add-Modify-Class request, which requires the following
704 * attributes-natural-language
706 * requesting-user-name
710 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
712 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
713 "printer-uri", NULL
, uri
);
714 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
718 * See if the printer is already in the class...
721 if (response
!= NULL
&&
722 (members
= ippFindAttribute(response
, "member-names",
723 IPP_TAG_NAME
)) != NULL
)
724 for (i
= 0; i
< members
->num_values
; i
++)
725 if (_cups_strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
727 _cupsLangPrintf(stderr
,
728 _("lpadmin: Printer %s is already a member of class "
729 "%s."), printer
, pclass
);
736 * OK, the printer isn't part of the class, so add it...
739 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
740 "localhost", 0, "/printers/%s", printer
);
742 if (response
!= NULL
&&
743 (members
= ippFindAttribute(response
, "member-uris",
744 IPP_TAG_URI
)) != NULL
)
747 * Add the printer to the existing list...
750 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
751 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
752 for (i
= 0; i
< members
->num_values
; i
++)
753 attr
->values
[i
].string
.text
=
754 _cupsStrAlloc(members
->values
[i
].string
.text
);
756 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
759 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
763 * Then send the request...
768 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
769 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
771 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
781 * 'default_printer()' - Set the default printing destination.
784 static int /* O - 0 on success, 1 on fail */
785 default_printer(http_t
*http
, /* I - Server connection */
786 char *printer
) /* I - Printer name */
788 ipp_t
*request
; /* IPP Request */
789 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
792 DEBUG_printf(("default_printer(%p, \"%s\")\n", http
, printer
));
795 * Build a CUPS-Set-Default request, which requires the following
799 * attributes-natural-language
801 * requesting-user-name
804 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
805 "localhost", 0, "/printers/%s", printer
);
807 request
= ippNewRequest(IPP_OP_CUPS_SET_DEFAULT
);
809 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
810 "printer-uri", NULL
, uri
);
811 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
815 * Do the request and get back a response...
818 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
820 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
822 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
832 * 'delete_printer()' - Delete a printer from the system...
835 static int /* O - 0 on success, 1 on fail */
836 delete_printer(http_t
*http
, /* I - Server connection */
837 char *printer
) /* I - Printer to delete */
839 ipp_t
*request
; /* IPP Request */
840 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
843 DEBUG_printf(("delete_printer(%p, \"%s\")\n", http
, printer
));
846 * Build a CUPS-Delete-Printer request, which requires the following
850 * attributes-natural-language
852 * requesting-user-name
855 request
= ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER
);
857 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
858 "localhost", 0, "/printers/%s", printer
);
859 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
860 "printer-uri", NULL
, uri
);
861 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
865 * Do the request and get back a response...
868 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
870 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
872 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
882 * 'delete_printer_from_class()' - Delete a printer from a class.
885 static int /* O - 0 on success, 1 on fail */
886 delete_printer_from_class(
887 http_t
*http
, /* I - Server connection */
888 char *printer
, /* I - Printer to remove */
889 char *pclass
) /* I - Class to remove from */
891 int i
, j
, k
; /* Looping vars */
892 ipp_t
*request
, /* IPP Request */
893 *response
; /* IPP Response */
894 ipp_attribute_t
*attr
, /* Current attribute */
895 *members
; /* Members in class */
896 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
899 DEBUG_printf(("delete_printer_from_class(%p, \"%s\", \"%s\")\n", http
,
903 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
907 * attributes-natural-language
909 * requesting-user-name
912 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
914 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
915 "localhost", 0, "/classes/%s", pclass
);
916 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
917 "printer-uri", NULL
, uri
);
918 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
922 * Do the request and get back a response...
925 if ((response
= cupsDoRequest(http
, request
, "/classes/")) == NULL
||
926 response
->request
.status
.status_code
== IPP_STATUS_ERROR_NOT_FOUND
)
928 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
936 * See if the printer is already in the class...
939 if ((members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) == NULL
)
941 _cupsLangPuts(stderr
, _("lpadmin: No member names were seen."));
948 for (i
= 0; i
< members
->num_values
; i
++)
949 if (!_cups_strcasecmp(printer
, members
->values
[i
].string
.text
))
952 if (i
>= members
->num_values
)
954 _cupsLangPrintf(stderr
,
955 _("lpadmin: Printer %s is not a member of class %s."),
963 if (members
->num_values
== 1)
966 * Build a CUPS-Delete-Class request, which requires the following
970 * attributes-natural-language
972 * requesting-user-name
975 request
= ippNewRequest(IPP_OP_CUPS_DELETE_CLASS
);
977 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
978 "printer-uri", NULL
, uri
);
979 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
980 "requesting-user-name", NULL
, cupsUser());
985 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
989 * attributes-natural-language
991 * requesting-user-name
995 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
997 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
998 "printer-uri", NULL
, uri
);
999 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1000 "requesting-user-name", NULL
, cupsUser());
1003 * Delete the printer from the class...
1006 members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
);
1007 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1008 "member-uris", members
->num_values
- 1, NULL
, NULL
);
1010 for (j
= 0, k
= 0; j
< members
->num_values
; j
++)
1012 attr
->values
[k
++].string
.text
=
1013 _cupsStrAlloc(members
->values
[j
].string
.text
);
1017 * Then send the request...
1020 ippDelete(response
);
1022 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1024 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1026 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1036 * 'delete_printer_option()' - Delete a printer option.
1039 static int /* O - 0 on success, 1 on fail */
1040 delete_printer_option(http_t
*http
, /* I - Server connection */
1041 char *printer
, /* I - Printer */
1042 char *option
) /* I - Option to delete */
1044 ipp_t
*request
; /* IPP request */
1045 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1049 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1050 * requires the following attributes:
1052 * attributes-charset
1053 * attributes-natural-language
1055 * requesting-user-name
1056 * option with deleteAttr tag
1059 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1060 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1062 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1064 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1065 "printer-uri", NULL
, uri
);
1066 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1067 "requesting-user-name", NULL
, cupsUser());
1068 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_DELETEATTR
, option
, 0);
1071 * Do the request and get back a response...
1074 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1076 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1078 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1088 * 'enable_printer()' - Enable a printer...
1091 static int /* O - 0 on success, 1 on fail */
1092 enable_printer(http_t
*http
, /* I - Server connection */
1093 char *printer
) /* I - Printer to enable */
1095 ipp_t
*request
; /* IPP Request */
1096 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1099 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http
, printer
));
1102 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1103 * require the following attributes:
1105 * attributes-charset
1106 * attributes-natural-language
1108 * requesting-user-name
1110 * printer-is-accepting-jobs
1113 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1114 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1116 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1118 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1119 "printer-uri", NULL
, uri
);
1120 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1121 "requesting-user-name", NULL
, cupsUser());
1122 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1124 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1127 * Do the request and get back a response...
1130 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1132 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1134 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1144 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
1147 static char * /* O - Filename or NULL */
1148 get_printer_ppd(const char *uri
, /* I - Printer URI */
1149 char *buffer
, /* I - Filename buffer */
1150 size_t bufsize
) /* I - Size of filename buffer */
1152 http_t
*http
; /* Connection to printer */
1153 ipp_t
*request
, /* Get-Printer-Attributes request */
1154 *response
; /* Get-Printer-Attributes response */
1155 char resolved
[1024], /* Resolved URI */
1156 scheme
[32], /* URI scheme */
1157 userpass
[256], /* Username:password */
1158 host
[256], /* Hostname */
1159 resource
[256]; /* Resource path */
1160 int port
; /* Port number */
1164 * Connect to the printer...
1167 if (strstr(uri
, "._tcp"))
1173 if (!_httpResolveURI(uri
, resolved
, sizeof(resolved
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
1175 _cupsLangPrintf(stderr
, _("%s: Unable to resolve \"%s\"."), "lpadmin", uri
);
1182 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
1184 _cupsLangPrintf(stderr
, _("%s: Bad printer URI \"%s\"."), "lpadmin", uri
);
1188 http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, !strcmp(scheme
, "ipps") ? HTTP_ENCRYPTION_ALWAYS
: HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
);
1191 _cupsLangPrintf(stderr
, _("%s: Unable to connect to \"%s:%d\": %s"), "lpadmin", host
, port
, cupsLastErrorString());
1196 * Send a Get-Printer-Attributes request...
1199 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1200 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1201 response
= cupsDoRequest(http
, request
, resource
);
1203 if (!_ppdCreateFromIPP(buffer
, bufsize
, response
))
1204 _cupsLangPrintf(stderr
, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno
));
1206 ippDelete(response
);
1217 * 'get_printer_type()' - Determine the printer type and URI.
1220 static cups_ptype_t
/* O - printer-type value */
1221 get_printer_type(http_t
*http
, /* I - Server connection */
1222 char *printer
, /* I - Printer name */
1223 char *uri
, /* I - URI buffer */
1224 size_t urisize
) /* I - Size of URI buffer */
1226 ipp_t
*request
, /* IPP request */
1227 *response
; /* IPP response */
1228 ipp_attribute_t
*attr
; /* printer-type attribute */
1229 cups_ptype_t type
; /* printer-type value */
1233 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1236 * attributes-charset
1237 * attributes-natural-language
1239 * requested-attributes
1240 * requesting-user-name
1243 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1245 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1246 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1247 "printer-uri", NULL
, uri
);
1248 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1249 "requested-attributes", NULL
, "printer-type");
1250 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1251 "requesting-user-name", NULL
, cupsUser());
1257 response
= cupsDoRequest(http
, request
, "/");
1258 if ((attr
= ippFindAttribute(response
, "printer-type",
1259 IPP_TAG_ENUM
)) != NULL
)
1261 type
= (cups_ptype_t
)attr
->values
[0].integer
;
1263 if (type
& CUPS_PRINTER_CLASS
)
1264 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/classes/%s", printer
);
1267 type
= CUPS_PRINTER_LOCAL
;
1269 ippDelete(response
);
1276 * 'set_printer_options()' - Set the printer options.
1279 static int /* O - 0 on success, 1 on fail */
1280 set_printer_options(
1281 http_t
*http
, /* I - Server connection */
1282 char *printer
, /* I - Printer */
1283 int num_options
, /* I - Number of options */
1284 cups_option_t
*options
, /* I - Options */
1285 char *file
) /* I - PPD file/interface script */
1287 ipp_t
*request
; /* IPP Request */
1288 const char *ppdfile
; /* PPD filename */
1289 int ppdchanged
= 0; /* PPD changed? */
1290 ppd_file_t
*ppd
; /* PPD file */
1291 ppd_choice_t
*choice
; /* Marked choice */
1292 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1293 line
[1024], /* Line from PPD file */
1294 keyword
[1024], /* Keyword from Default line */
1295 *keyptr
, /* Pointer into keyword... */
1296 tempfile
[1024]; /* Temporary filename */
1297 cups_file_t
*in
, /* PPD file */
1298 *out
; /* Temporary file */
1299 const char *ppdname
, /* ppd-name value */
1300 *protocol
, /* Old protocol option */
1301 *customval
, /* Custom option value */
1302 *boolval
; /* Boolean value */
1303 int wrote_ipp_supplies
= 0, /* Wrote cupsIPPSupplies keyword? */
1304 wrote_snmp_supplies
= 0,/* Wrote cupsSNMPSupplies keyword? */
1305 copied_options
= 0; /* Copied options? */
1308 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1309 "options=%p, file=\"%s\")\n", http
, printer
, num_options
,
1313 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1314 * which requires the following attributes:
1316 * attributes-charset
1317 * attributes-natural-language
1319 * requesting-user-name
1323 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1324 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1326 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1328 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1329 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1332 * Add the options...
1337 else if ((ppdname
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& strcmp(ppdname
, "raw") && num_options
> 1)
1339 if ((ppdfile
= cupsGetServerPPD(http
, ppdname
)) != NULL
)
1342 * Copy options array and remove ppd-name from it...
1345 cups_option_t
*temp
= NULL
, *optr
;
1346 int i
, num_temp
= 0;
1347 for (i
= num_options
, optr
= options
; i
> 0; i
--, optr
++)
1348 if (strcmp(optr
->name
, "ppd-name"))
1349 num_temp
= cupsAddOption(optr
->name
, optr
->value
, num_temp
, &temp
);
1353 num_options
= num_temp
;
1357 else if (request
->request
.op
.operation_id
== IPP_OP_CUPS_ADD_MODIFY_PRINTER
)
1358 ppdfile
= cupsGetPPD(printer
);
1362 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
1363 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1365 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1367 if (!_cups_strcasecmp(protocol
, "bcp"))
1368 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1370 else if (!_cups_strcasecmp(protocol
, "tbcp"))
1371 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1378 * Set default options in the PPD file...
1381 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
1383 int linenum
; /* Line number of error */
1384 ppd_status_t status
= ppdLastError(&linenum
);
1387 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile
, ppdErrorString(status
), linenum
);
1390 ppdMarkDefaults(ppd
);
1391 cupsMarkOptions(ppd
, num_options
, options
);
1393 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1395 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1397 if (ppdfile
!= file
)
1400 cupsFreeOptions(num_options
, options
);
1404 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1406 _cupsLangPrintf(stderr
,
1407 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1408 ppdfile
, strerror(errno
));
1410 if (ppdfile
!= file
)
1413 cupsFreeOptions(num_options
, options
);
1419 while (cupsFileGets(in
, line
, sizeof(line
)))
1421 if (!strncmp(line
, "*cupsIPPSupplies:", 17) &&
1422 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1425 wrote_ipp_supplies
= 1;
1426 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1427 (!_cups_strcasecmp(boolval
, "true") ||
1428 !_cups_strcasecmp(boolval
, "yes") ||
1429 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1431 else if (!strncmp(line
, "*cupsSNMPSupplies:", 18) &&
1432 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1435 wrote_snmp_supplies
= 1;
1436 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1437 (!_cups_strcasecmp(boolval
, "true") ||
1438 !_cups_strcasecmp(boolval
, "yes") ||
1439 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1441 else if (strncmp(line
, "*Default", 8))
1442 cupsFilePrintf(out
, "%s\n", line
);
1446 * Get default option name...
1449 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
1451 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1452 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1456 while (isspace(*keyptr
& 255))
1459 if (!strcmp(keyword
, "PageRegion") ||
1460 !strcmp(keyword
, "PageSize") ||
1461 !strcmp(keyword
, "PaperDimension") ||
1462 !strcmp(keyword
, "ImageableArea"))
1464 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1465 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1468 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1470 if (choice
&& strcmp(choice
->choice
, keyptr
))
1472 if (strcmp(choice
->choice
, "Custom"))
1474 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1477 else if ((customval
= cupsGetOption(keyword
, num_options
,
1480 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1484 cupsFilePrintf(out
, "%s\n", line
);
1487 cupsFilePrintf(out
, "%s\n", line
);
1491 if (!wrote_ipp_supplies
&&
1492 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1495 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1496 (!_cups_strcasecmp(boolval
, "true") ||
1497 !_cups_strcasecmp(boolval
, "yes") ||
1498 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1501 if (!wrote_snmp_supplies
&&
1502 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1505 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1506 (!_cups_strcasecmp(boolval
, "true") ||
1507 !_cups_strcasecmp(boolval
, "yes") ||
1508 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1519 ippDelete(cupsDoFileRequest(http
, request
, "/admin/",
1520 ppdchanged
? tempfile
: file
));
1523 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1527 if (ppdfile
!= file
)
1534 * No PPD file - just set the options...
1537 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1541 cupsFreeOptions(num_options
, options
);
1544 * Check the response...
1547 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1549 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1559 * 'validate_name()' - Make sure the printer name only contains valid chars.
1562 static int /* O - 0 if name is no good, 1 if name is good */
1563 validate_name(const char *name
) /* I - Name to check */
1565 const char *ptr
; /* Pointer into name */
1569 * Scan the whole name...
1572 for (ptr
= name
; *ptr
; ptr
++)
1575 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' ||
1580 * All the characters are good; validate the length, too...
1583 return ((ptr
- name
) < 128);