2 * "$Id: lpadmin.c 9793 2011-05-20 03:49:49Z mike $"
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)) &
1094 (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
1095 request = ippNewRequest(CUPS_ADD_MODIFY_CLASS);
1097 request = ippNewRequest(CUPS_ADD_MODIFY_PRINTER);
1099 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1100 "printer-uri", NULL, uri);
1101 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1102 "requesting-user-name", NULL, cupsUser());
1103 ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_DELETEATTR, option, 0);
1106 * Do the request and get back a response...
1109 ippDelete(cupsDoRequest(http, request, "/admin/"));
1111 if (cupsLastError() > IPP_OK_CONFLICT)
1113 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1123 * 'enable_printer()' - Enable a printer...
1126 static int /* O - 0 on success, 1 on fail */
1127 enable_printer(http_t *http, /* I - Server connection */
1128 char *printer) /* I - Printer to enable */
1130 ipp_t *request; /* IPP Request */
1131 char uri[HTTP_MAX_URI]; /* URI for printer/class */
1134 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http, printer));
1137 * Build a CUPS_ADD_MODIFY_PRINTER or CUPS_ADD_MODIFY_CLASS request, which
1138 * require the following attributes:
1140 * attributes-charset
1141 * attributes-natural-language
1143 * requesting-user-name
1145 * printer-is-accepting-jobs
1148 if (get_printer_type(http, printer, uri, sizeof(uri)) &
1149 (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
1150 request = ippNewRequest(CUPS_ADD_MODIFY_CLASS);
1152 request = ippNewRequest(CUPS_ADD_MODIFY_PRINTER);
1154 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1155 "printer-uri", NULL, uri);
1156 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1157 "requesting-user-name", NULL, cupsUser());
1158 ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
1160 ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-accepting-jobs", 1);
1163 * Do the request and get back a response...
1166 ippDelete(cupsDoRequest(http, request, "/admin/"));
1168 if (cupsLastError() > IPP_OK_CONFLICT)
1170 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1180 * 'get_printer_type()' - Determine the printer type and URI.
1183 static cups_ptype_t /* O - printer-type value */
1184 get_printer_type(http_t *http, /* I - Server connection */
1185 char *printer, /* I - Printer name */
1186 char *uri, /* I - URI buffer */
1187 size_t urisize) /* I - Size of URI buffer */
1189 ipp_t *request, /* IPP request */
1190 *response; /* IPP response */
1191 ipp_attribute_t *attr; /* printer-type attribute */
1192 cups_ptype_t type; /* printer-type value */
1196 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1199 * attributes-charset
1200 * attributes-natural-language
1202 * requested-attributes
1203 * requesting-user-name
1206 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, urisize, "ipp", NULL, "localhost",
1207 ippPort(), "/printers/%s", printer);
1209 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
1210 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1211 "printer-uri", NULL, uri);
1212 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1213 "requested-attributes", NULL, "printer-type");
1214 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1215 "requesting-user-name", NULL, cupsUser());
1221 response = cupsDoRequest(http, request, "/");
1222 if ((attr = ippFindAttribute(response, "printer-type",
1223 IPP_TAG_ENUM)) != NULL)
1225 type = (cups_ptype_t)attr->values[0].integer;
1227 if (type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
1228 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, urisize, "ipp", NULL,
1229 "localhost", ippPort(), "/classes/%s", printer);
1232 type = CUPS_PRINTER_LOCAL;
1234 ippDelete(response);
1241 * 'set_printer_options()' - Set the printer options.
1244 static int /* O - 0 on success, 1 on fail */
1245 set_printer_options(
1246 http_t *http, /* I - Server connection */
1247 char *printer, /* I - Printer */
1248 int num_options, /* I - Number of options */
1249 cups_option_t *options, /* I - Options */
1250 char *file) /* I - PPD file/interface script */
1252 ipp_t *request; /* IPP Request */
1253 const char *ppdfile; /* PPD filename */
1254 int ppdchanged; /* PPD changed? */
1255 ppd_file_t *ppd; /* PPD file */
1256 ppd_choice_t *choice; /* Marked choice */
1257 char uri[HTTP_MAX_URI], /* URI for printer/class */
1258 line[1024], /* Line from PPD file */
1259 keyword[1024], /* Keyword from Default line */
1260 *keyptr, /* Pointer into keyword... */
1261 tempfile[1024]; /* Temporary filename */
1262 cups_file_t *in, /* PPD file */
1263 *out; /* Temporary file */
1264 const char *protocol, /* Old protocol option */
1265 *customval, /* Custom option value */
1266 *boolval; /* Boolean value */
1267 int wrote_ipp_supplies = 0, /* Wrote cupsIPPSupplies keyword? */
1268 wrote_snmp_supplies = 0;/* Wrote cupsSNMPSupplies keyword? */
1271 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1272 "options=%p, file=\"%s\")\n", http, printer, num_options,
1276 * Build a CUPS_ADD_MODIFY_PRINTER or CUPS_ADD_MODIFY_CLASS request, which
1277 * requires the following attributes:
1279 * attributes-charset
1280 * attributes-natural-language
1282 * requesting-user-name
1286 if (get_printer_type(http, printer, uri, sizeof(uri)) &
1287 (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
1288 request = ippNewRequest(CUPS_ADD_MODIFY_CLASS);
1290 request = ippNewRequest(CUPS_ADD_MODIFY_PRINTER);
1292 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1293 "printer-uri", NULL, uri);
1294 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1295 "requesting-user-name", NULL, cupsUser());
1298 * Add the options...
1301 cupsEncodeOptions2(request, num_options, options, IPP_TAG_PRINTER);
1303 if ((protocol = cupsGetOption("protocol", num_options, options)) != NULL)
1305 if (!_cups_strcasecmp(protocol, "bcp"))
1306 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
1308 else if (!_cups_strcasecmp(protocol, "tbcp"))
1309 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
1315 else if (request->request.op.operation_id == CUPS_ADD_MODIFY_PRINTER)
1316 ppdfile = cupsGetPPD(printer);
1320 if (ppdfile != NULL)
1323 * Set default options in the PPD file...
1326 ppd = ppdOpenFile(ppdfile);
1327 ppdMarkDefaults(ppd);
1328 cupsMarkOptions(ppd, num_options, options);
1330 if ((out = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
1332 _cupsLangPrintError(NULL, _("lpadmin: Unable to create temporary file"));
1334 if (ppdfile != file)
1339 if ((in = cupsFileOpen(ppdfile, "r")) == NULL)
1341 _cupsLangPrintf(stderr,
1342 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1343 ppdfile, strerror(errno));
1345 if (ppdfile != file)
1354 while (cupsFileGets(in, line, sizeof(line)))
1356 if (!strncmp(line, "*cupsIPPSupplies:", 17) &&
1357 (boolval = cupsGetOption("cupsIPPSupplies", num_options,
1360 wrote_ipp_supplies = 1;
1361 cupsFilePrintf(out, "*cupsIPPSupplies: %s\n",
1362 (!_cups_strcasecmp(boolval, "true") ||
1363 !_cups_strcasecmp(boolval, "yes") ||
1364 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1366 else if (!strncmp(line, "*cupsSNMPSupplies:", 18) &&
1367 (boolval = cupsGetOption("cupsSNMPSupplies", num_options,
1370 wrote_snmp_supplies = 1;
1371 cupsFilePrintf(out, "*cupsSNMPSupplies: %s\n",
1372 (!_cups_strcasecmp(boolval, "true") ||
1373 !_cups_strcasecmp(boolval, "yes") ||
1374 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1376 else if (strncmp(line, "*Default", 8))
1377 cupsFilePrintf(out, "%s\n", line);
1381 * Get default option name...
1384 strlcpy(keyword, line + 8, sizeof(keyword));
1386 for (keyptr = keyword; *keyptr; keyptr ++)
1387 if (*keyptr == ':' || isspace(*keyptr & 255))
1391 while (isspace(*keyptr & 255))
1394 if (!strcmp(keyword, "PageRegion") ||
1395 !strcmp(keyword, "PageSize") ||
1396 !strcmp(keyword, "PaperDimension") ||
1397 !strcmp(keyword, "ImageableArea"))
1399 if ((choice = ppdFindMarkedChoice(ppd, "PageSize")) == NULL)
1400 choice = ppdFindMarkedChoice(ppd, "PageRegion");
1403 choice = ppdFindMarkedChoice(ppd, keyword);
1405 if (choice && strcmp(choice->choice, keyptr))
1407 if (strcmp(choice->choice, "Custom"))
1409 cupsFilePrintf(out, "*Default%s: %s\n", keyword, choice->choice);
1412 else if ((customval = cupsGetOption(keyword, num_options,
1415 cupsFilePrintf(out, "*Default%s: %s\n", keyword, customval);
1419 cupsFilePrintf(out, "%s\n", line);
1422 cupsFilePrintf(out, "%s\n", line);
1426 if (!wrote_ipp_supplies &&
1427 (boolval = cupsGetOption("cupsIPPSupplies", num_options,
1430 cupsFilePrintf(out, "*cupsIPPSupplies: %s\n",
1431 (!_cups_strcasecmp(boolval, "true") ||
1432 !_cups_strcasecmp(boolval, "yes") ||
1433 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1436 if (!wrote_snmp_supplies &&
1437 (boolval = cupsGetOption("cupsSNMPSupplies", num_options,
1440 cupsFilePrintf(out, "*cupsSNMPSupplies: %s\n",
1441 (!_cups_strcasecmp(boolval, "true") ||
1442 !_cups_strcasecmp(boolval, "yes") ||
1443 !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
1454 ippDelete(cupsDoFileRequest(http, request, "/admin/",
1455 ppdchanged ? tempfile : file));
1458 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1462 if (ppdfile != file)
1469 * No PPD file - just set the options...
1472 ippDelete(cupsDoRequest(http, request, "/admin/"));
1476 * Check the response...
1479 if (cupsLastError() > IPP_OK_CONFLICT)
1481 _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
1491 * 'validate_name()' - Make sure the printer name only contains valid chars.
1494 static int /* O - 0 if name is no good, 1 if name is good */
1495 validate_name(const char *name) /* I - Name to check */
1497 const char *ptr; /* Pointer into name */
1501 * Scan the whole name...
1504 for (ptr = name; *ptr; ptr ++)
1507 else if ((*ptr >= 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' ||
1512 * All the characters are good; validate the length, too...
1515 return ((ptr - name) < 128);
1520 * End of "$Id: lpadmin.c 9793 2011-05-20 03:49:49Z mike $".