4 * "lpadmin" command for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Parse options and configure the scheduler.
18 * add_printer_to_class() - Add a printer to a class.
19 * default_printer() - Set the default printing destination.
20 * delete_printer() - Delete a printer from the system.
21 * delete_printer_from_class() - Delete a printer from a class.
22 * delete_printer_option() - Delete a printer option.
23 * enable_printer() - Enable a printer.
24 * get_printer_type() - Determine the printer type and URI.
25 * set_printer_options() - Set the printer options and/or file.
26 * validate_name() - Make sure the printer name only contains
31 * Include necessary headers...
34 #include <cups/cups-private.h>
41 static int add_printer_to_class(http_t
*http
, char *printer
, char *pclass
);
42 static int default_printer(http_t
*http
, char *printer
);
43 static int delete_printer(http_t
*http
, char *printer
);
44 static int delete_printer_from_class(http_t
*http
, char *printer
,
46 static int delete_printer_option(http_t
*http
, char *printer
,
48 static int enable_printer(http_t
*http
, char *printer
);
49 static cups_ptype_t
get_printer_type(http_t
*http
, char *printer
, char *uri
,
51 static int set_printer_options(http_t
*http
, char *printer
,
52 int num_options
, cups_option_t
*options
,
54 static int validate_name(const char *name
);
58 * 'main()' - Parse options and configure the scheduler.
62 main(int argc
, /* I - Number of command-line arguments */
63 char *argv
[]) /* I - Command-line arguments */
65 int i
; /* Looping var */
66 http_t
*http
; /* Connection to server */
67 char *printer
, /* Destination printer */
68 *pclass
, /* Printer class name */
69 *val
; /* Pointer to allow/deny value */
70 int num_options
; /* Number of options */
71 cups_option_t
*options
; /* Options */
72 char *file
; /* New PPD file/interface script */
83 for (i
= 1; i
< argc
; i
++)
84 if (argv
[i
][0] == '-')
87 case 'c' : /* Add printer to class */
90 http
= httpConnectEncrypt(cupsServer(), ippPort(),
95 _cupsLangPrintf(stderr
,
96 _("lpadmin: Unable to connect to server: %s"),
104 _cupsLangPuts(stderr
,
105 _("lpadmin: Unable to add a printer to the class:\n"
106 " You must specify a printer name "
112 pclass
= argv
[i
] + 2;
119 _cupsLangPuts(stderr
,
120 _("lpadmin: Expected class name after \"-c\" "
128 if (!validate_name(pclass
))
130 _cupsLangPuts(stderr
,
131 _("lpadmin: Class name can only contain printable "
136 if (add_printer_to_class(http
, printer
, pclass
))
140 case 'd' : /* Set as default destination */
143 http
= httpConnectEncrypt(cupsServer(), ippPort(),
148 _cupsLangPrintf(stderr
,
149 _("lpadmin: Unable to connect to server: %s"),
156 printer
= argv
[i
] + 2;
163 _cupsLangPuts(stderr
,
164 _("lpadmin: Expected printer name after \"-d\" "
172 if (!validate_name(printer
))
174 _cupsLangPuts(stderr
,
175 _("lpadmin: Printer name can only contain "
176 "printable characters."));
180 if (default_printer(http
, printer
))
186 case 'h' : /* Connect to host */
193 if (argv
[i
][2] != '\0')
194 cupsSetServer(argv
[i
] + 2);
201 _cupsLangPuts(stderr
,
202 _("lpadmin: Expected hostname after \"-h\" "
207 cupsSetServer(argv
[i
]);
211 case 'i' : /* Use the specified interface script */
220 _cupsLangPuts(stderr
,
221 _("lpadmin: Expected interface after \"-i\" "
230 case 'E' : /* Enable the printer */
234 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
237 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
239 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
241 #endif /* HAVE_SSL */
247 http
= httpConnectEncrypt(cupsServer(), ippPort(),
252 _cupsLangPrintf(stderr
,
253 _("lpadmin: Unable to connect to server: %s"),
259 if (enable_printer(http
, printer
))
263 case 'm' : /* Use the specified standard script/PPD file */
265 num_options
= cupsAddOption("ppd-name", argv
[i
] + 2, num_options
,
273 _cupsLangPuts(stderr
,
274 _("lpadmin: Expected model after \"-m\" "
279 num_options
= cupsAddOption("ppd-name", argv
[i
], num_options
,
284 case 'o' : /* Set option */
286 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
293 _cupsLangPuts(stderr
,
294 _("lpadmin: Expected name=value after \"-o\" "
299 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
303 case 'p' : /* Add/modify a printer */
305 printer
= argv
[i
] + 2;
312 _cupsLangPuts(stderr
,
313 _("lpadmin: Expected printer after \"-p\" "
321 if (!validate_name(printer
))
323 _cupsLangPuts(stderr
,
324 _("lpadmin: Printer name can only contain "
325 "printable characters."));
330 case 'r' : /* Remove printer from class */
333 http
= httpConnectEncrypt(cupsServer(), ippPort(),
338 _cupsLangPrintf(stderr
,
339 _("lpadmin: Unable to connect to server: %s"),
347 _cupsLangPuts(stderr
,
348 _("lpadmin: Unable to remove a printer from the "
350 " You must specify a printer name "
356 pclass
= argv
[i
] + 2;
363 _cupsLangPuts(stderr
,
364 _("lpadmin: Expected class after \"-r\" "
372 if (!validate_name(pclass
))
374 _cupsLangPuts(stderr
,
375 _("lpadmin: Class name can only contain printable "
380 if (delete_printer_from_class(http
, printer
, pclass
))
384 case 'R' : /* Remove option */
387 http
= httpConnectEncrypt(cupsServer(), ippPort(),
392 _cupsLangPrintf(stderr
,
393 _("lpadmin: Unable to connect to server: %s"),
401 _cupsLangPuts(stderr
,
402 _("lpadmin: Unable to delete option:\n"
403 " You must specify a printer name "
416 _cupsLangPuts(stderr
,
417 _("lpadmin: Expected name after \"-R\" "
425 if (delete_printer_option(http
, printer
, val
))
429 case 'U' : /* Username */
430 if (argv
[i
][2] != '\0')
431 cupsSetUser(argv
[i
] + 2);
437 _cupsLangPrintf(stderr
,
438 _("%s: Error - expected username after "
439 "\"-U\" option."), argv
[0]);
443 cupsSetUser(argv
[i
]);
447 case 'u' : /* Allow/deny users */
456 _cupsLangPuts(stderr
,
457 _("lpadmin: Expected allow/deny:userlist after "
465 if (!_cups_strncasecmp(val
, "allow:", 6))
466 num_options
= cupsAddOption("requesting-user-name-allowed",
467 val
+ 6, num_options
, &options
);
468 else if (!_cups_strncasecmp(val
, "deny:", 5))
469 num_options
= cupsAddOption("requesting-user-name-denied",
470 val
+ 5, num_options
, &options
);
473 _cupsLangPrintf(stderr
,
474 _("lpadmin: Unknown allow/deny option \"%s\"."),
480 case 'v' : /* Set the device-uri attribute */
482 num_options
= cupsAddOption("device-uri", argv
[i
] + 2,
483 num_options
, &options
);
490 _cupsLangPuts(stderr
,
491 _("lpadmin: Expected device URI after \"-v\" "
496 num_options
= cupsAddOption("device-uri", argv
[i
],
497 num_options
, &options
);
501 case 'x' : /* Delete a printer */
504 http
= httpConnectEncrypt(cupsServer(), ippPort(),
509 _cupsLangPrintf(stderr
,
510 _("lpadmin: Unable to connect to server: %s"),
517 printer
= argv
[i
] + 2;
524 _cupsLangPuts(stderr
,
525 _("lpadmin: Expected printer or class after "
533 if (!validate_name(printer
))
535 _cupsLangPuts(stderr
,
536 _("lpadmin: Printer name can only contain "
537 "printable characters."));
541 if (delete_printer(http
, printer
))
547 case 'D' : /* Set the printer-info attribute */
549 num_options
= cupsAddOption("printer-info", argv
[i
] + 2,
550 num_options
, &options
);
557 _cupsLangPuts(stderr
,
558 _("lpadmin: Expected description after "
563 num_options
= cupsAddOption("printer-info", argv
[i
],
564 num_options
, &options
);
568 case 'I' : /* Set the supported file types (ignored) */
573 _cupsLangPuts(stderr
,
574 _("lpadmin: Expected file type(s) after \"-I\" "
579 _cupsLangPuts(stderr
,
580 _("lpadmin: Warning - content type list ignored."));
583 case 'L' : /* Set the printer-location attribute */
585 num_options
= cupsAddOption("printer-location", argv
[i
] + 2,
586 num_options
, &options
);
593 _cupsLangPuts(stderr
,
594 _("lpadmin: Expected location after \"-L\" "
599 num_options
= cupsAddOption("printer-location", argv
[i
],
600 num_options
, &options
);
604 case 'P' : /* Use the specified PPD file */
613 _cupsLangPuts(stderr
,
614 _("lpadmin: Expected PPD after \"-P\" option."));
623 _cupsLangPrintf(stderr
,
624 _("lpadmin: Unknown option \"%c\"."), argv
[i
][1]);
629 _cupsLangPrintf(stderr
, _("lpadmin: Unknown argument \"%s\"."),
635 * Set options as needed...
638 if (num_options
|| file
)
642 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
646 _cupsLangPrintf(stderr
,
647 _("lpadmin: Unable to connect to server: %s"),
655 _cupsLangPuts(stderr
,
656 _("lpadmin: Unable to set the printer options:\n"
657 " You must specify a printer name first."));
661 if (set_printer_options(http
, printer
, num_options
, options
, file
))
667 _cupsLangPuts(stdout
,
670 " lpadmin [-h server] -d destination\n"
671 " lpadmin [-h server] -x destination\n"
672 " lpadmin [-h server] -p printer [-c add-class] "
673 "[-i interface] [-m model]\n"
674 " [-r remove-class] [-v device] "
676 " [-P ppd-file] [-o name=value]\n"
677 " [-u allow:user,user] "
678 "[-u deny:user,user]"));
689 * 'add_printer_to_class()' - Add a printer to a class.
692 static int /* O - 0 on success, 1 on fail */
693 add_printer_to_class(http_t
*http
, /* I - Server connection */
694 char *printer
, /* I - Printer to add */
695 char *pclass
) /* I - Class to add to */
697 int i
; /* Looping var */
698 ipp_t
*request
, /* IPP Request */
699 *response
; /* IPP Response */
700 ipp_attribute_t
*attr
, /* Current attribute */
701 *members
; /* Members in class */
702 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
705 DEBUG_printf(("add_printer_to_class(%p, \"%s\", \"%s\")\n", http
,
709 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
713 * attributes-natural-language
715 * requesting-user-name
718 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
720 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
721 "localhost", 0, "/classes/%s", pclass
);
722 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
723 "printer-uri", NULL
, uri
);
724 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
728 * Do the request and get back a response...
731 response
= cupsDoRequest(http
, request
, "/");
734 * Build a CUPS_ADD_MODIFY_CLASS request, which requires the following
738 * attributes-natural-language
740 * requesting-user-name
744 request
= ippNewRequest(CUPS_ADD_MODIFY_CLASS
);
746 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
747 "printer-uri", NULL
, uri
);
748 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
752 * See if the printer is already in the class...
755 if (response
!= NULL
&&
756 (members
= ippFindAttribute(response
, "member-names",
757 IPP_TAG_NAME
)) != NULL
)
758 for (i
= 0; i
< members
->num_values
; i
++)
759 if (_cups_strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
761 _cupsLangPrintf(stderr
,
762 _("lpadmin: Printer %s is already a member of class "
763 "%s."), printer
, pclass
);
770 * OK, the printer isn't part of the class, so add it...
773 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
774 "localhost", 0, "/printers/%s", printer
);
776 if (response
!= NULL
&&
777 (members
= ippFindAttribute(response
, "member-uris",
778 IPP_TAG_URI
)) != NULL
)
781 * Add the printer to the existing list...
784 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
785 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
786 for (i
= 0; i
< members
->num_values
; i
++)
787 attr
->values
[i
].string
.text
=
788 _cupsStrAlloc(members
->values
[i
].string
.text
);
790 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
793 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
797 * Then send the request...
802 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
803 if (cupsLastError() > IPP_OK_CONFLICT
)
805 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
815 * 'default_printer()' - Set the default printing destination.
818 static int /* O - 0 on success, 1 on fail */
819 default_printer(http_t
*http
, /* I - Server connection */
820 char *printer
) /* I - Printer name */
822 ipp_t
*request
; /* IPP Request */
823 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
826 DEBUG_printf(("default_printer(%p, \"%s\")\n", http
, printer
));
829 * Build a CUPS_SET_DEFAULT request, which requires the following
833 * attributes-natural-language
835 * requesting-user-name
838 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
839 "localhost", 0, "/printers/%s", printer
);
841 request
= ippNewRequest(CUPS_SET_DEFAULT
);
843 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
844 "printer-uri", NULL
, uri
);
845 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
849 * Do the request and get back a response...
852 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
854 if (cupsLastError() > IPP_OK_CONFLICT
)
856 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
866 * 'delete_printer()' - Delete a printer from the system...
869 static int /* O - 0 on success, 1 on fail */
870 delete_printer(http_t
*http
, /* I - Server connection */
871 char *printer
) /* I - Printer to delete */
873 ipp_t
*request
; /* IPP Request */
874 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
877 DEBUG_printf(("delete_printer(%p, \"%s\")\n", http
, printer
));
880 * Build a CUPS_DELETE_PRINTER request, which requires the following
884 * attributes-natural-language
886 * requesting-user-name
889 request
= ippNewRequest(CUPS_DELETE_PRINTER
);
891 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
892 "localhost", 0, "/printers/%s", printer
);
893 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
894 "printer-uri", NULL
, uri
);
895 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
899 * Do the request and get back a response...
902 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
904 if (cupsLastError() > IPP_OK_CONFLICT
)
906 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
916 * 'delete_printer_from_class()' - Delete a printer from a class.
919 static int /* O - 0 on success, 1 on fail */
920 delete_printer_from_class(
921 http_t
*http
, /* I - Server connection */
922 char *printer
, /* I - Printer to remove */
923 char *pclass
) /* I - Class to remove from */
925 int i
, j
, k
; /* Looping vars */
926 ipp_t
*request
, /* IPP Request */
927 *response
; /* IPP Response */
928 ipp_attribute_t
*attr
, /* Current attribute */
929 *members
; /* Members in class */
930 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
933 DEBUG_printf(("delete_printer_from_class(%p, \"%s\", \"%s\")\n", http
,
937 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
941 * attributes-natural-language
943 * requesting-user-name
946 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
948 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
949 "localhost", 0, "/classes/%s", pclass
);
950 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
951 "printer-uri", NULL
, uri
);
952 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
956 * Do the request and get back a response...
959 if ((response
= cupsDoRequest(http
, request
, "/classes/")) == NULL
||
960 response
->request
.status
.status_code
== IPP_NOT_FOUND
)
962 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
970 * See if the printer is already in the class...
973 if ((members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) == NULL
)
975 _cupsLangPuts(stderr
, _("lpadmin: No member names were seen."));
982 for (i
= 0; i
< members
->num_values
; i
++)
983 if (!_cups_strcasecmp(printer
, members
->values
[i
].string
.text
))
986 if (i
>= members
->num_values
)
988 _cupsLangPrintf(stderr
,
989 _("lpadmin: Printer %s is not a member of class %s."),
997 if (members
->num_values
== 1)
1000 * Build a CUPS_DELETE_CLASS request, which requires the following
1003 * attributes-charset
1004 * attributes-natural-language
1006 * requesting-user-name
1009 request
= ippNewRequest(CUPS_DELETE_CLASS
);
1011 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1012 "printer-uri", NULL
, uri
);
1013 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1014 "requesting-user-name", NULL
, cupsUser());
1019 * Build a CUPS_ADD_MODIFY_CLASS request, which requires the following
1022 * attributes-charset
1023 * attributes-natural-language
1025 * requesting-user-name
1029 request
= ippNewRequest(CUPS_ADD_MODIFY_CLASS
);
1031 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1032 "printer-uri", NULL
, uri
);
1033 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1034 "requesting-user-name", NULL
, cupsUser());
1037 * Delete the printer from the class...
1040 members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
);
1041 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1042 "member-uris", members
->num_values
- 1, NULL
, NULL
);
1044 for (j
= 0, k
= 0; j
< members
->num_values
; j
++)
1046 attr
->values
[k
++].string
.text
=
1047 _cupsStrAlloc(members
->values
[j
].string
.text
);
1051 * Then send the request...
1054 ippDelete(response
);
1056 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1058 if (cupsLastError() > IPP_OK_CONFLICT
)
1060 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1070 * 'delete_printer_option()' - Delete a printer option.
1073 static int /* O - 0 on success, 1 on fail */
1074 delete_printer_option(http_t
*http
, /* I - Server connection */
1075 char *printer
, /* I - Printer */
1076 char *option
) /* I - Option to delete */
1078 ipp_t
*request
; /* IPP request */
1079 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1083 * Build a CUPS_ADD_MODIFY_PRINTER or CUPS_ADD_MODIFY_CLASS request, which
1084 * requires the following attributes:
1086 * attributes-charset
1087 * attributes-natural-language
1089 * requesting-user-name
1090 * option with deleteAttr tag
1093 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1094 request
= ippNewRequest(CUPS_ADD_MODIFY_CLASS
);
1096 request
= ippNewRequest(CUPS_ADD_MODIFY_PRINTER
);
1098 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1099 "printer-uri", NULL
, uri
);
1100 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1101 "requesting-user-name", NULL
, cupsUser());
1102 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_DELETEATTR
, option
, 0);
1105 * Do the request and get back a response...
1108 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1110 if (cupsLastError() > IPP_OK_CONFLICT
)
1112 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1122 * 'enable_printer()' - Enable a printer...
1125 static int /* O - 0 on success, 1 on fail */
1126 enable_printer(http_t
*http
, /* I - Server connection */
1127 char *printer
) /* I - Printer to enable */
1129 ipp_t
*request
; /* IPP Request */
1130 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1133 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http
, printer
));
1136 * Build a CUPS_ADD_MODIFY_PRINTER or CUPS_ADD_MODIFY_CLASS request, which
1137 * require the following attributes:
1139 * attributes-charset
1140 * attributes-natural-language
1142 * requesting-user-name
1144 * printer-is-accepting-jobs
1147 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1148 request
= ippNewRequest(CUPS_ADD_MODIFY_CLASS
);
1150 request
= ippNewRequest(CUPS_ADD_MODIFY_PRINTER
);
1152 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1153 "printer-uri", NULL
, uri
);
1154 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1155 "requesting-user-name", NULL
, cupsUser());
1156 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1158 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1161 * Do the request and get back a response...
1164 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1166 if (cupsLastError() > IPP_OK_CONFLICT
)
1168 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1178 * 'get_printer_type()' - Determine the printer type and URI.
1181 static cups_ptype_t
/* O - printer-type value */
1182 get_printer_type(http_t
*http
, /* I - Server connection */
1183 char *printer
, /* I - Printer name */
1184 char *uri
, /* I - URI buffer */
1185 size_t urisize
) /* I - Size of URI buffer */
1187 ipp_t
*request
, /* IPP request */
1188 *response
; /* IPP response */
1189 ipp_attribute_t
*attr
; /* printer-type attribute */
1190 cups_ptype_t type
; /* printer-type value */
1194 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1197 * attributes-charset
1198 * attributes-natural-language
1200 * requested-attributes
1201 * requesting-user-name
1204 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, urisize
, "ipp", NULL
, "localhost",
1205 ippPort(), "/printers/%s", printer
);
1207 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1208 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1209 "printer-uri", NULL
, uri
);
1210 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1211 "requested-attributes", NULL
, "printer-type");
1212 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1213 "requesting-user-name", NULL
, cupsUser());
1219 response
= cupsDoRequest(http
, request
, "/");
1220 if ((attr
= ippFindAttribute(response
, "printer-type",
1221 IPP_TAG_ENUM
)) != NULL
)
1223 type
= (cups_ptype_t
)attr
->values
[0].integer
;
1225 if (type
& CUPS_PRINTER_CLASS
)
1226 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, urisize
, "ipp", NULL
,
1227 "localhost", ippPort(), "/classes/%s", printer
);
1230 type
= CUPS_PRINTER_LOCAL
;
1232 ippDelete(response
);
1239 * 'set_printer_options()' - Set the printer options.
1242 static int /* O - 0 on success, 1 on fail */
1243 set_printer_options(
1244 http_t
*http
, /* I - Server connection */
1245 char *printer
, /* I - Printer */
1246 int num_options
, /* I - Number of options */
1247 cups_option_t
*options
, /* I - Options */
1248 char *file
) /* I - PPD file/interface script */
1250 ipp_t
*request
; /* IPP Request */
1251 const char *ppdfile
; /* PPD filename */
1252 int ppdchanged
; /* PPD changed? */
1253 ppd_file_t
*ppd
; /* PPD file */
1254 ppd_choice_t
*choice
; /* Marked choice */
1255 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1256 line
[1024], /* Line from PPD file */
1257 keyword
[1024], /* Keyword from Default line */
1258 *keyptr
, /* Pointer into keyword... */
1259 tempfile
[1024]; /* Temporary filename */
1260 cups_file_t
*in
, /* PPD file */
1261 *out
; /* Temporary file */
1262 const char *protocol
, /* Old protocol option */
1263 *customval
, /* Custom option value */
1264 *boolval
; /* Boolean value */
1265 int wrote_ipp_supplies
= 0, /* Wrote cupsIPPSupplies keyword? */
1266 wrote_snmp_supplies
= 0;/* Wrote cupsSNMPSupplies keyword? */
1269 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1270 "options=%p, file=\"%s\")\n", http
, printer
, num_options
,
1274 * Build a CUPS_ADD_MODIFY_PRINTER or CUPS_ADD_MODIFY_CLASS request, which
1275 * requires the following attributes:
1277 * attributes-charset
1278 * attributes-natural-language
1280 * requesting-user-name
1284 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1285 request
= ippNewRequest(CUPS_ADD_MODIFY_CLASS
);
1287 request
= ippNewRequest(CUPS_ADD_MODIFY_PRINTER
);
1289 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1290 "printer-uri", NULL
, uri
);
1291 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1292 "requesting-user-name", NULL
, cupsUser());
1295 * Add the options...
1298 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1300 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1302 if (!_cups_strcasecmp(protocol
, "bcp"))
1303 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1305 else if (!_cups_strcasecmp(protocol
, "tbcp"))
1306 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1312 else if (request
->request
.op
.operation_id
== CUPS_ADD_MODIFY_PRINTER
)
1313 ppdfile
= cupsGetPPD(printer
);
1317 if (ppdfile
!= NULL
)
1320 * Set default options in the PPD file...
1323 ppd
= ppdOpenFile(ppdfile
);
1324 ppdMarkDefaults(ppd
);
1325 cupsMarkOptions(ppd
, num_options
, options
);
1327 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1329 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1331 if (ppdfile
!= file
)
1336 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1338 _cupsLangPrintf(stderr
,
1339 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1340 ppdfile
, strerror(errno
));
1342 if (ppdfile
!= file
)
1351 while (cupsFileGets(in
, line
, sizeof(line
)))
1353 if (!strncmp(line
, "*cupsIPPSupplies:", 17) &&
1354 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1357 wrote_ipp_supplies
= 1;
1358 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1359 (!_cups_strcasecmp(boolval
, "true") ||
1360 !_cups_strcasecmp(boolval
, "yes") ||
1361 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1363 else if (!strncmp(line
, "*cupsSNMPSupplies:", 18) &&
1364 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1367 wrote_snmp_supplies
= 1;
1368 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1369 (!_cups_strcasecmp(boolval
, "true") ||
1370 !_cups_strcasecmp(boolval
, "yes") ||
1371 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1373 else if (strncmp(line
, "*Default", 8))
1374 cupsFilePrintf(out
, "%s\n", line
);
1378 * Get default option name...
1381 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
1383 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1384 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1388 while (isspace(*keyptr
& 255))
1391 if (!strcmp(keyword
, "PageRegion") ||
1392 !strcmp(keyword
, "PageSize") ||
1393 !strcmp(keyword
, "PaperDimension") ||
1394 !strcmp(keyword
, "ImageableArea"))
1396 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1397 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1400 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1402 if (choice
&& strcmp(choice
->choice
, keyptr
))
1404 if (strcmp(choice
->choice
, "Custom"))
1406 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1409 else if ((customval
= cupsGetOption(keyword
, num_options
,
1412 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1416 cupsFilePrintf(out
, "%s\n", line
);
1419 cupsFilePrintf(out
, "%s\n", line
);
1423 if (!wrote_ipp_supplies
&&
1424 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1427 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1428 (!_cups_strcasecmp(boolval
, "true") ||
1429 !_cups_strcasecmp(boolval
, "yes") ||
1430 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1433 if (!wrote_snmp_supplies
&&
1434 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1437 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1438 (!_cups_strcasecmp(boolval
, "true") ||
1439 !_cups_strcasecmp(boolval
, "yes") ||
1440 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1451 ippDelete(cupsDoFileRequest(http
, request
, "/admin/",
1452 ppdchanged
? tempfile
: file
));
1455 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1459 if (ppdfile
!= file
)
1466 * No PPD file - just set the options...
1469 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1473 * Check the response...
1476 if (cupsLastError() > IPP_OK_CONFLICT
)
1478 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1488 * 'validate_name()' - Make sure the printer name only contains valid chars.
1491 static int /* O - 0 if name is no good, 1 if name is good */
1492 validate_name(const char *name
) /* I - Name to check */
1494 const char *ptr
; /* Pointer into name */
1498 * Scan the whole name...
1501 for (ptr
= name
; *ptr
; ptr
++)
1504 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' ||
1509 * All the characters are good; validate the length, too...
1512 return ((ptr
- name
) < 128);