2 * "lpadmin" command for CUPS.
4 * Copyright 2007-2015 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 *val
; /* Pointer to allow/deny value */
58 int num_options
; /* Number of options */
59 cups_option_t
*options
; /* Options */
60 char *file
, /* New PPD file */
62 /* IPP Everywhere PPD */
63 const char *ppd_name
, /* ppd-name value */
64 *device_uri
; /* device-uri value */
75 for (i
= 1; i
< argc
; i
++)
76 if (argv
[i
][0] == '-')
79 case 'c' : /* Add printer to class */
82 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
86 _cupsLangPrintf(stderr
,
87 _("lpadmin: Unable to connect to server: %s"),
96 _("lpadmin: Unable to add a printer to the class:\n"
97 " You must specify a printer name "
103 pclass
= argv
[i
] + 2;
110 _cupsLangPuts(stderr
,
111 _("lpadmin: Expected class name after \"-c\" "
119 if (!validate_name(pclass
))
121 _cupsLangPuts(stderr
,
122 _("lpadmin: Class name can only contain printable "
127 if (add_printer_to_class(http
, printer
, pclass
))
131 case 'd' : /* Set as default destination */
134 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
138 _cupsLangPrintf(stderr
,
139 _("lpadmin: Unable to connect to server: %s"),
146 printer
= argv
[i
] + 2;
153 _cupsLangPuts(stderr
,
154 _("lpadmin: Expected printer name after \"-d\" "
162 if (!validate_name(printer
))
164 _cupsLangPuts(stderr
,
165 _("lpadmin: Printer name can only contain "
166 "printable characters."));
170 if (default_printer(http
, printer
))
176 case 'h' : /* Connect to host */
183 if (argv
[i
][2] != '\0')
184 cupsSetServer(argv
[i
] + 2);
191 _cupsLangPuts(stderr
,
192 _("lpadmin: Expected hostname after \"-h\" "
197 cupsSetServer(argv
[i
]);
201 case 'P' : /* Use the specified PPD file */
202 case 'i' : /* Use the specified PPD file */
211 _cupsLangPrintf(stderr
, _("lpadmin: Expected PPD after \"-%c\" option."), argv
[i
- 1][1]);
219 case 'E' : /* Enable the printer */
223 cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED
);
226 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
228 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
230 #endif /* HAVE_SSL */
236 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
240 _cupsLangPrintf(stderr
,
241 _("lpadmin: Unable to connect to server: %s"),
247 if (enable_printer(http
, printer
))
251 case 'm' : /* Use the specified standard script/PPD file */
253 num_options
= cupsAddOption("ppd-name", argv
[i
] + 2, num_options
,
261 _cupsLangPuts(stderr
,
262 _("lpadmin: Expected model after \"-m\" "
267 num_options
= cupsAddOption("ppd-name", argv
[i
], num_options
,
272 case 'o' : /* Set option */
274 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
281 _cupsLangPuts(stderr
,
282 _("lpadmin: Expected name=value after \"-o\" "
287 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
291 case 'p' : /* Add/modify a printer */
293 printer
= argv
[i
] + 2;
300 _cupsLangPuts(stderr
,
301 _("lpadmin: Expected printer after \"-p\" "
309 if (!validate_name(printer
))
311 _cupsLangPuts(stderr
,
312 _("lpadmin: Printer name can only contain "
313 "printable characters."));
318 case 'r' : /* Remove printer from class */
321 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
325 _cupsLangPrintf(stderr
,
326 _("lpadmin: Unable to connect to server: %s"),
334 _cupsLangPuts(stderr
,
335 _("lpadmin: Unable to remove a printer from the "
337 " You must specify a printer name "
343 pclass
= argv
[i
] + 2;
350 _cupsLangPuts(stderr
,
351 _("lpadmin: Expected class after \"-r\" "
359 if (!validate_name(pclass
))
361 _cupsLangPuts(stderr
,
362 _("lpadmin: Class name can only contain printable "
367 if (delete_printer_from_class(http
, printer
, pclass
))
371 case 'R' : /* Remove option */
374 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
378 _cupsLangPrintf(stderr
,
379 _("lpadmin: Unable to connect to server: %s"),
387 _cupsLangPuts(stderr
,
388 _("lpadmin: Unable to delete option:\n"
389 " You must specify a printer name "
402 _cupsLangPuts(stderr
,
403 _("lpadmin: Expected name after \"-R\" "
411 if (delete_printer_option(http
, printer
, val
))
415 case 'U' : /* Username */
416 if (argv
[i
][2] != '\0')
417 cupsSetUser(argv
[i
] + 2);
423 _cupsLangPrintf(stderr
,
424 _("%s: Error - expected username after "
425 "\"-U\" option."), argv
[0]);
429 cupsSetUser(argv
[i
]);
433 case 'u' : /* Allow/deny users */
442 _cupsLangPuts(stderr
,
443 _("lpadmin: Expected allow/deny:userlist after "
451 if (!_cups_strncasecmp(val
, "allow:", 6))
452 num_options
= cupsAddOption("requesting-user-name-allowed",
453 val
+ 6, num_options
, &options
);
454 else if (!_cups_strncasecmp(val
, "deny:", 5))
455 num_options
= cupsAddOption("requesting-user-name-denied",
456 val
+ 5, num_options
, &options
);
459 _cupsLangPrintf(stderr
,
460 _("lpadmin: Unknown allow/deny option \"%s\"."),
466 case 'v' : /* Set the device-uri attribute */
468 num_options
= cupsAddOption("device-uri", argv
[i
] + 2,
469 num_options
, &options
);
476 _cupsLangPuts(stderr
,
477 _("lpadmin: Expected device URI after \"-v\" "
482 num_options
= cupsAddOption("device-uri", argv
[i
],
483 num_options
, &options
);
487 case 'x' : /* Delete a printer */
490 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
494 _cupsLangPrintf(stderr
,
495 _("lpadmin: Unable to connect to server: %s"),
502 printer
= argv
[i
] + 2;
509 _cupsLangPuts(stderr
,
510 _("lpadmin: Expected printer or class after "
518 if (!validate_name(printer
))
520 _cupsLangPuts(stderr
,
521 _("lpadmin: Printer name can only contain "
522 "printable characters."));
526 if (delete_printer(http
, printer
))
532 case 'D' : /* Set the printer-info attribute */
534 num_options
= cupsAddOption("printer-info", argv
[i
] + 2,
535 num_options
, &options
);
542 _cupsLangPuts(stderr
,
543 _("lpadmin: Expected description after "
548 num_options
= cupsAddOption("printer-info", argv
[i
],
549 num_options
, &options
);
553 case 'I' : /* Set the supported file types (ignored) */
558 _cupsLangPuts(stderr
,
559 _("lpadmin: Expected file type(s) after \"-I\" "
564 _cupsLangPuts(stderr
,
565 _("lpadmin: Warning - content type list ignored."));
568 case 'L' : /* Set the printer-location attribute */
570 num_options
= cupsAddOption("printer-location", argv
[i
] + 2,
571 num_options
, &options
);
578 _cupsLangPuts(stderr
,
579 _("lpadmin: Expected location after \"-L\" "
584 num_options
= cupsAddOption("printer-location", argv
[i
],
585 num_options
, &options
);
590 _cupsLangPrintf(stderr
,
591 _("lpadmin: Unknown option \"%c\"."), argv
[i
][1]);
596 _cupsLangPrintf(stderr
, _("lpadmin: Unknown argument \"%s\"."),
602 * Set options as needed...
605 if ((ppd_name
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& !strcmp(ppd_name
, "everywhere") && (device_uri
= cupsGetOption("device-uri", num_options
, options
)) != NULL
)
607 if ((file
= get_printer_ppd(device_uri
, evefile
, sizeof(evefile
))) == NULL
)
610 num_options
= cupsRemoveOption("ppd-name", num_options
, &options
);
613 if (num_options
|| file
)
617 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
621 _cupsLangPrintf(stderr
,
622 _("lpadmin: Unable to connect to server: %s"),
630 _cupsLangPuts(stderr
,
631 _("lpadmin: Unable to set the printer options:\n"
632 " You must specify a printer name first."));
636 if (set_printer_options(http
, printer
, num_options
, options
, file
))
645 _cupsLangPuts(stdout
,
648 " lpadmin [-h server] -d destination\n"
649 " lpadmin [-h server] -x destination\n"
650 " lpadmin [-h server] -p printer [-c add-class] "
651 "[-i interface] [-m model]\n"
652 " [-r remove-class] [-v device] "
654 " [-P ppd-file] [-o name=value]\n"
655 " [-u allow:user,user] "
656 "[-u deny:user,user]"));
667 * 'add_printer_to_class()' - Add a printer to a class.
670 static int /* O - 0 on success, 1 on fail */
671 add_printer_to_class(http_t
*http
, /* I - Server connection */
672 char *printer
, /* I - Printer to add */
673 char *pclass
) /* I - Class to add to */
675 int i
; /* Looping var */
676 ipp_t
*request
, /* IPP Request */
677 *response
; /* IPP Response */
678 ipp_attribute_t
*attr
, /* Current attribute */
679 *members
; /* Members in class */
680 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
683 DEBUG_printf(("add_printer_to_class(%p, \"%s\", \"%s\")\n", http
,
687 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
691 * attributes-natural-language
693 * requesting-user-name
696 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
698 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
699 "localhost", 0, "/classes/%s", pclass
);
700 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
701 "printer-uri", NULL
, uri
);
702 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
706 * Do the request and get back a response...
709 response
= cupsDoRequest(http
, request
, "/");
712 * Build a CUPS-Add-Modify-Class request, which requires the following
716 * attributes-natural-language
718 * requesting-user-name
722 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
724 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
725 "printer-uri", NULL
, uri
);
726 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
730 * See if the printer is already in the class...
733 if (response
!= NULL
&&
734 (members
= ippFindAttribute(response
, "member-names",
735 IPP_TAG_NAME
)) != NULL
)
736 for (i
= 0; i
< members
->num_values
; i
++)
737 if (_cups_strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
739 _cupsLangPrintf(stderr
,
740 _("lpadmin: Printer %s is already a member of class "
741 "%s."), printer
, pclass
);
748 * OK, the printer isn't part of the class, so add it...
751 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
752 "localhost", 0, "/printers/%s", printer
);
754 if (response
!= NULL
&&
755 (members
= ippFindAttribute(response
, "member-uris",
756 IPP_TAG_URI
)) != NULL
)
759 * Add the printer to the existing list...
762 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
763 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
764 for (i
= 0; i
< members
->num_values
; i
++)
765 attr
->values
[i
].string
.text
=
766 _cupsStrAlloc(members
->values
[i
].string
.text
);
768 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
771 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
775 * Then send the request...
780 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
781 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
783 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
793 * 'default_printer()' - Set the default printing destination.
796 static int /* O - 0 on success, 1 on fail */
797 default_printer(http_t
*http
, /* I - Server connection */
798 char *printer
) /* I - Printer name */
800 ipp_t
*request
; /* IPP Request */
801 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
804 DEBUG_printf(("default_printer(%p, \"%s\")\n", http
, printer
));
807 * Build a CUPS-Set-Default request, which requires the following
811 * attributes-natural-language
813 * requesting-user-name
816 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
817 "localhost", 0, "/printers/%s", printer
);
819 request
= ippNewRequest(IPP_OP_CUPS_SET_DEFAULT
);
821 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
822 "printer-uri", NULL
, uri
);
823 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
827 * Do the request and get back a response...
830 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
832 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
834 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
844 * 'delete_printer()' - Delete a printer from the system...
847 static int /* O - 0 on success, 1 on fail */
848 delete_printer(http_t
*http
, /* I - Server connection */
849 char *printer
) /* I - Printer to delete */
851 ipp_t
*request
; /* IPP Request */
852 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
855 DEBUG_printf(("delete_printer(%p, \"%s\")\n", http
, printer
));
858 * Build a CUPS-Delete-Printer request, which requires the following
862 * attributes-natural-language
864 * requesting-user-name
867 request
= ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER
);
869 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
870 "localhost", 0, "/printers/%s", printer
);
871 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
872 "printer-uri", NULL
, uri
);
873 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
877 * Do the request and get back a response...
880 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
882 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
884 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
894 * 'delete_printer_from_class()' - Delete a printer from a class.
897 static int /* O - 0 on success, 1 on fail */
898 delete_printer_from_class(
899 http_t
*http
, /* I - Server connection */
900 char *printer
, /* I - Printer to remove */
901 char *pclass
) /* I - Class to remove from */
903 int i
, j
, k
; /* Looping vars */
904 ipp_t
*request
, /* IPP Request */
905 *response
; /* IPP Response */
906 ipp_attribute_t
*attr
, /* Current attribute */
907 *members
; /* Members in class */
908 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
911 DEBUG_printf(("delete_printer_from_class(%p, \"%s\", \"%s\")\n", http
,
915 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
919 * attributes-natural-language
921 * requesting-user-name
924 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
926 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
927 "localhost", 0, "/classes/%s", pclass
);
928 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
929 "printer-uri", NULL
, uri
);
930 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
934 * Do the request and get back a response...
937 if ((response
= cupsDoRequest(http
, request
, "/classes/")) == NULL
||
938 response
->request
.status
.status_code
== IPP_STATUS_ERROR_NOT_FOUND
)
940 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
948 * See if the printer is already in the class...
951 if ((members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) == NULL
)
953 _cupsLangPuts(stderr
, _("lpadmin: No member names were seen."));
960 for (i
= 0; i
< members
->num_values
; i
++)
961 if (!_cups_strcasecmp(printer
, members
->values
[i
].string
.text
))
964 if (i
>= members
->num_values
)
966 _cupsLangPrintf(stderr
,
967 _("lpadmin: Printer %s is not a member of class %s."),
975 if (members
->num_values
== 1)
978 * Build a CUPS-Delete-Class request, which requires the following
982 * attributes-natural-language
984 * requesting-user-name
987 request
= ippNewRequest(IPP_OP_CUPS_DELETE_CLASS
);
989 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
990 "printer-uri", NULL
, uri
);
991 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
992 "requesting-user-name", NULL
, cupsUser());
997 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
1000 * attributes-charset
1001 * attributes-natural-language
1003 * requesting-user-name
1007 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1009 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1010 "printer-uri", NULL
, uri
);
1011 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1012 "requesting-user-name", NULL
, cupsUser());
1015 * Delete the printer from the class...
1018 members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
);
1019 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1020 "member-uris", members
->num_values
- 1, NULL
, NULL
);
1022 for (j
= 0, k
= 0; j
< members
->num_values
; j
++)
1024 attr
->values
[k
++].string
.text
=
1025 _cupsStrAlloc(members
->values
[j
].string
.text
);
1029 * Then send the request...
1032 ippDelete(response
);
1034 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1036 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1038 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1048 * 'delete_printer_option()' - Delete a printer option.
1051 static int /* O - 0 on success, 1 on fail */
1052 delete_printer_option(http_t
*http
, /* I - Server connection */
1053 char *printer
, /* I - Printer */
1054 char *option
) /* I - Option to delete */
1056 ipp_t
*request
; /* IPP request */
1057 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1061 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1062 * requires the following attributes:
1064 * attributes-charset
1065 * attributes-natural-language
1067 * requesting-user-name
1068 * option with deleteAttr tag
1071 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1072 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1074 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1076 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1077 "printer-uri", NULL
, uri
);
1078 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1079 "requesting-user-name", NULL
, cupsUser());
1080 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_DELETEATTR
, option
, 0);
1083 * Do the request and get back a response...
1086 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1088 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1090 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1100 * 'enable_printer()' - Enable a printer...
1103 static int /* O - 0 on success, 1 on fail */
1104 enable_printer(http_t
*http
, /* I - Server connection */
1105 char *printer
) /* I - Printer to enable */
1107 ipp_t
*request
; /* IPP Request */
1108 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1111 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http
, printer
));
1114 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1115 * require the following attributes:
1117 * attributes-charset
1118 * attributes-natural-language
1120 * requesting-user-name
1122 * printer-is-accepting-jobs
1125 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1126 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1128 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1130 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1131 "printer-uri", NULL
, uri
);
1132 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1133 "requesting-user-name", NULL
, cupsUser());
1134 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1136 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1139 * Do the request and get back a response...
1142 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1144 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1146 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1156 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
1159 static char * /* O - Filename or NULL */
1160 get_printer_ppd(const char *uri
, /* I - Printer URI */
1161 char *buffer
, /* I - Filename buffer */
1162 size_t bufsize
) /* I - Size of filename buffer */
1164 http_t
*http
; /* Connection to printer */
1165 ipp_t
*request
, /* Get-Printer-Attributes request */
1166 *response
; /* Get-Printer-Attributes response */
1167 char resolved
[1024], /* Resolved URI */
1168 scheme
[32], /* URI scheme */
1169 userpass
[256], /* Username:password */
1170 host
[256], /* Hostname */
1171 resource
[256]; /* Resource path */
1172 int port
; /* Port number */
1176 * Connect to the printer...
1179 if (strstr(uri
, "._tcp"))
1185 if (!_httpResolveURI(uri
, resolved
, sizeof(resolved
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
1187 _cupsLangPrintf(stderr
, _("%s: Unable to resolve \"%s\"."), "lpadmin", uri
);
1194 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
1196 _cupsLangPrintf(stderr
, _("%s: Bad printer URI \"%s\"."), "lpadmin", uri
);
1200 http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, !strcmp(scheme
, "ipps") ? HTTP_ENCRYPTION_ALWAYS
: HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
);
1203 _cupsLangPrintf(stderr
, _("%s: Unable to connect to \"%s:%d\": %s"), "lpadmin", host
, port
, cupsLastErrorString());
1208 * Send a Get-Printer-Attributes request...
1211 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1212 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1213 response
= cupsDoRequest(http
, request
, resource
);
1215 if (!_ppdCreateFromIPP(buffer
, bufsize
, response
))
1216 _cupsLangPrintf(stderr
, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno
));
1218 ippDelete(response
);
1229 * 'get_printer_type()' - Determine the printer type and URI.
1232 static cups_ptype_t
/* O - printer-type value */
1233 get_printer_type(http_t
*http
, /* I - Server connection */
1234 char *printer
, /* I - Printer name */
1235 char *uri
, /* I - URI buffer */
1236 size_t urisize
) /* I - Size of URI buffer */
1238 ipp_t
*request
, /* IPP request */
1239 *response
; /* IPP response */
1240 ipp_attribute_t
*attr
; /* printer-type attribute */
1241 cups_ptype_t type
; /* printer-type value */
1245 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1248 * attributes-charset
1249 * attributes-natural-language
1251 * requested-attributes
1252 * requesting-user-name
1255 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1257 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1258 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1259 "printer-uri", NULL
, uri
);
1260 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1261 "requested-attributes", NULL
, "printer-type");
1262 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1263 "requesting-user-name", NULL
, cupsUser());
1269 response
= cupsDoRequest(http
, request
, "/");
1270 if ((attr
= ippFindAttribute(response
, "printer-type",
1271 IPP_TAG_ENUM
)) != NULL
)
1273 type
= (cups_ptype_t
)attr
->values
[0].integer
;
1275 if (type
& CUPS_PRINTER_CLASS
)
1276 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/classes/%s", printer
);
1279 type
= CUPS_PRINTER_LOCAL
;
1281 ippDelete(response
);
1288 * 'set_printer_options()' - Set the printer options.
1291 static int /* O - 0 on success, 1 on fail */
1292 set_printer_options(
1293 http_t
*http
, /* I - Server connection */
1294 char *printer
, /* I - Printer */
1295 int num_options
, /* I - Number of options */
1296 cups_option_t
*options
, /* I - Options */
1297 char *file
) /* I - PPD file/interface script */
1299 ipp_t
*request
; /* IPP Request */
1300 const char *ppdfile
; /* PPD filename */
1301 int ppdchanged
= 0; /* PPD changed? */
1302 ppd_file_t
*ppd
; /* PPD file */
1303 ppd_choice_t
*choice
; /* Marked choice */
1304 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1305 line
[1024], /* Line from PPD file */
1306 keyword
[1024], /* Keyword from Default line */
1307 *keyptr
, /* Pointer into keyword... */
1308 tempfile
[1024]; /* Temporary filename */
1309 cups_file_t
*in
, /* PPD file */
1310 *out
; /* Temporary file */
1311 const char *ppdname
, /* ppd-name value */
1312 *protocol
, /* Old protocol option */
1313 *customval
, /* Custom option value */
1314 *boolval
; /* Boolean value */
1315 int wrote_ipp_supplies
= 0, /* Wrote cupsIPPSupplies keyword? */
1316 wrote_snmp_supplies
= 0,/* Wrote cupsSNMPSupplies keyword? */
1317 copied_options
= 0; /* Copied options? */
1320 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1321 "options=%p, file=\"%s\")\n", http
, printer
, num_options
,
1325 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1326 * which requires the following attributes:
1328 * attributes-charset
1329 * attributes-natural-language
1331 * requesting-user-name
1335 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1336 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1338 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1340 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1341 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1344 * Add the options...
1349 else if ((ppdname
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& strcmp(ppdname
, "raw") && num_options
> 1)
1351 if ((ppdfile
= cupsGetServerPPD(http
, ppdname
)) != NULL
)
1354 * Copy options array and remove ppd-name from it...
1357 cups_option_t
*temp
= NULL
, *optr
;
1358 int i
, num_temp
= 0;
1359 for (i
= num_options
, optr
= options
; i
> 0; i
--, optr
++)
1360 if (strcmp(optr
->name
, "ppd-name"))
1361 num_temp
= cupsAddOption(optr
->name
, optr
->value
, num_temp
, &temp
);
1365 num_options
= num_temp
;
1369 else if (request
->request
.op
.operation_id
== IPP_OP_CUPS_ADD_MODIFY_PRINTER
)
1370 ppdfile
= cupsGetPPD(printer
);
1374 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
1375 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1377 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1379 if (!_cups_strcasecmp(protocol
, "bcp"))
1380 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1382 else if (!_cups_strcasecmp(protocol
, "tbcp"))
1383 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1390 * Set default options in the PPD file...
1393 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
1395 int linenum
; /* Line number of error */
1396 ppd_status_t status
= ppdLastError(&linenum
);
1399 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile
, ppdErrorString(status
), linenum
);
1402 ppdMarkDefaults(ppd
);
1403 cupsMarkOptions(ppd
, num_options
, options
);
1405 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1407 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1409 if (ppdfile
!= file
)
1412 cupsFreeOptions(num_options
, options
);
1416 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1418 _cupsLangPrintf(stderr
,
1419 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1420 ppdfile
, strerror(errno
));
1422 if (ppdfile
!= file
)
1425 cupsFreeOptions(num_options
, options
);
1431 while (cupsFileGets(in
, line
, sizeof(line
)))
1433 if (!strncmp(line
, "*cupsIPPSupplies:", 17) &&
1434 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1437 wrote_ipp_supplies
= 1;
1438 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1439 (!_cups_strcasecmp(boolval
, "true") ||
1440 !_cups_strcasecmp(boolval
, "yes") ||
1441 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1443 else if (!strncmp(line
, "*cupsSNMPSupplies:", 18) &&
1444 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1447 wrote_snmp_supplies
= 1;
1448 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1449 (!_cups_strcasecmp(boolval
, "true") ||
1450 !_cups_strcasecmp(boolval
, "yes") ||
1451 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1453 else if (strncmp(line
, "*Default", 8))
1454 cupsFilePrintf(out
, "%s\n", line
);
1458 * Get default option name...
1461 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
1463 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1464 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1468 while (isspace(*keyptr
& 255))
1471 if (!strcmp(keyword
, "PageRegion") ||
1472 !strcmp(keyword
, "PageSize") ||
1473 !strcmp(keyword
, "PaperDimension") ||
1474 !strcmp(keyword
, "ImageableArea"))
1476 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1477 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1480 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1482 if (choice
&& strcmp(choice
->choice
, keyptr
))
1484 if (strcmp(choice
->choice
, "Custom"))
1486 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1489 else if ((customval
= cupsGetOption(keyword
, num_options
,
1492 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1496 cupsFilePrintf(out
, "%s\n", line
);
1499 cupsFilePrintf(out
, "%s\n", line
);
1503 if (!wrote_ipp_supplies
&&
1504 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1507 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1508 (!_cups_strcasecmp(boolval
, "true") ||
1509 !_cups_strcasecmp(boolval
, "yes") ||
1510 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1513 if (!wrote_snmp_supplies
&&
1514 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1517 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1518 (!_cups_strcasecmp(boolval
, "true") ||
1519 !_cups_strcasecmp(boolval
, "yes") ||
1520 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1531 ippDelete(cupsDoFileRequest(http
, request
, "/admin/",
1532 ppdchanged
? tempfile
: file
));
1535 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1539 if (ppdfile
!= file
)
1546 * No PPD file - just set the options...
1549 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1553 cupsFreeOptions(num_options
, options
);
1556 * Check the response...
1559 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1561 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1571 * 'validate_name()' - Make sure the printer name only contains valid chars.
1574 static int /* O - 0 if name is no good, 1 if name is good */
1575 validate_name(const char *name
) /* I - Name to check */
1577 const char *ptr
; /* Pointer into name */
1581 * Scan the whole name...
1584 for (ptr
= name
; *ptr
; ptr
++)
1587 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' ||
1592 * All the characters are good; validate the length, too...
1595 return ((ptr
- name
) < 128);