4 * "lpadmin" command for CUPS.
6 * Copyright 2007-2015 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 * Include necessary headers...
20 #define _CUPS_NO_DEPRECATED
21 #define _PPD_DEPRECATED
22 #include <cups/cups-private.h>
23 #include <cups/ppd-private.h>
30 static int add_printer_to_class(http_t
*http
, char *printer
, char *pclass
);
31 static int default_printer(http_t
*http
, char *printer
);
32 static int delete_printer(http_t
*http
, char *printer
);
33 static int delete_printer_from_class(http_t
*http
, char *printer
,
35 static int delete_printer_option(http_t
*http
, char *printer
,
37 static int enable_printer(http_t
*http
, char *printer
);
38 static char *get_printer_ppd(const char *uri
, char *buffer
, size_t bufsize
);
39 static cups_ptype_t
get_printer_type(http_t
*http
, char *printer
, char *uri
,
41 static int set_printer_options(http_t
*http
, char *printer
,
42 int num_options
, cups_option_t
*options
,
44 static int validate_name(const char *name
);
48 * 'main()' - Parse options and configure the scheduler.
52 main(int argc
, /* I - Number of command-line arguments */
53 char *argv
[]) /* I - Command-line arguments */
55 int i
; /* Looping var */
56 http_t
*http
; /* Connection to server */
57 char *printer
, /* Destination printer */
58 *pclass
, /* Printer class name */
59 *val
; /* Pointer to allow/deny value */
60 int num_options
; /* Number of options */
61 cups_option_t
*options
; /* Options */
62 char *file
, /* New PPD file */
64 /* IPP Everywhere PPD */
65 const char *ppd_name
, /* ppd-name value */
66 *device_uri
; /* device-uri value */
77 for (i
= 1; i
< argc
; i
++)
78 if (argv
[i
][0] == '-')
81 case 'c' : /* Add printer to class */
84 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
88 _cupsLangPrintf(stderr
,
89 _("lpadmin: Unable to connect to server: %s"),
98 _("lpadmin: Unable to add a printer to the class:\n"
99 " You must specify a printer name "
105 pclass
= argv
[i
] + 2;
112 _cupsLangPuts(stderr
,
113 _("lpadmin: Expected class name after \"-c\" "
121 if (!validate_name(pclass
))
123 _cupsLangPuts(stderr
,
124 _("lpadmin: Class name can only contain printable "
129 if (add_printer_to_class(http
, printer
, pclass
))
133 case 'd' : /* Set as default destination */
136 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
140 _cupsLangPrintf(stderr
,
141 _("lpadmin: Unable to connect to server: %s"),
148 printer
= argv
[i
] + 2;
155 _cupsLangPuts(stderr
,
156 _("lpadmin: Expected printer name after \"-d\" "
164 if (!validate_name(printer
))
166 _cupsLangPuts(stderr
,
167 _("lpadmin: Printer name can only contain "
168 "printable characters."));
172 if (default_printer(http
, printer
))
178 case 'h' : /* Connect to host */
185 if (argv
[i
][2] != '\0')
186 cupsSetServer(argv
[i
] + 2);
193 _cupsLangPuts(stderr
,
194 _("lpadmin: Expected hostname after \"-h\" "
199 cupsSetServer(argv
[i
]);
203 case 'P' : /* Use the specified PPD file */
204 case 'i' : /* Use the specified PPD file */
213 _cupsLangPrintf(stderr
, _("lpadmin: Expected PPD after \"-%c\" option."), argv
[i
- 1][1]);
221 case 'E' : /* Enable the printer */
225 cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED
);
228 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
230 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
232 #endif /* HAVE_SSL */
238 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
242 _cupsLangPrintf(stderr
,
243 _("lpadmin: Unable to connect to server: %s"),
249 if (enable_printer(http
, printer
))
253 case 'm' : /* Use the specified standard script/PPD file */
255 num_options
= cupsAddOption("ppd-name", argv
[i
] + 2, num_options
,
263 _cupsLangPuts(stderr
,
264 _("lpadmin: Expected model after \"-m\" "
269 num_options
= cupsAddOption("ppd-name", argv
[i
], num_options
,
274 case 'o' : /* Set option */
276 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
283 _cupsLangPuts(stderr
,
284 _("lpadmin: Expected name=value after \"-o\" "
289 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
293 case 'p' : /* Add/modify a printer */
295 printer
= argv
[i
] + 2;
302 _cupsLangPuts(stderr
,
303 _("lpadmin: Expected printer after \"-p\" "
311 if (!validate_name(printer
))
313 _cupsLangPuts(stderr
,
314 _("lpadmin: Printer name can only contain "
315 "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 "
339 " You must specify a printer name "
345 pclass
= argv
[i
] + 2;
352 _cupsLangPuts(stderr
,
353 _("lpadmin: Expected class after \"-r\" "
361 if (!validate_name(pclass
))
363 _cupsLangPuts(stderr
,
364 _("lpadmin: Class name can only contain printable "
369 if (delete_printer_from_class(http
, printer
, pclass
))
373 case 'R' : /* Remove option */
376 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
380 _cupsLangPrintf(stderr
,
381 _("lpadmin: Unable to connect to server: %s"),
389 _cupsLangPuts(stderr
,
390 _("lpadmin: Unable to delete option:\n"
391 " You must specify a printer name "
404 _cupsLangPuts(stderr
,
405 _("lpadmin: Expected name after \"-R\" "
413 if (delete_printer_option(http
, printer
, val
))
417 case 'U' : /* Username */
418 if (argv
[i
][2] != '\0')
419 cupsSetUser(argv
[i
] + 2);
425 _cupsLangPrintf(stderr
,
426 _("%s: Error - expected username after "
427 "\"-U\" option."), argv
[0]);
431 cupsSetUser(argv
[i
]);
435 case 'u' : /* Allow/deny users */
444 _cupsLangPuts(stderr
,
445 _("lpadmin: Expected allow/deny:userlist after "
453 if (!_cups_strncasecmp(val
, "allow:", 6))
454 num_options
= cupsAddOption("requesting-user-name-allowed",
455 val
+ 6, num_options
, &options
);
456 else if (!_cups_strncasecmp(val
, "deny:", 5))
457 num_options
= cupsAddOption("requesting-user-name-denied",
458 val
+ 5, num_options
, &options
);
461 _cupsLangPrintf(stderr
,
462 _("lpadmin: Unknown allow/deny option \"%s\"."),
468 case 'v' : /* Set the device-uri attribute */
470 num_options
= cupsAddOption("device-uri", argv
[i
] + 2,
471 num_options
, &options
);
478 _cupsLangPuts(stderr
,
479 _("lpadmin: Expected device URI after \"-v\" "
484 num_options
= cupsAddOption("device-uri", argv
[i
],
485 num_options
, &options
);
489 case 'x' : /* Delete a printer */
492 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
496 _cupsLangPrintf(stderr
,
497 _("lpadmin: Unable to connect to server: %s"),
504 printer
= argv
[i
] + 2;
511 _cupsLangPuts(stderr
,
512 _("lpadmin: Expected printer or class after "
520 if (!validate_name(printer
))
522 _cupsLangPuts(stderr
,
523 _("lpadmin: Printer name can only contain "
524 "printable characters."));
528 if (delete_printer(http
, printer
))
534 case 'D' : /* Set the printer-info attribute */
536 num_options
= cupsAddOption("printer-info", argv
[i
] + 2,
537 num_options
, &options
);
544 _cupsLangPuts(stderr
,
545 _("lpadmin: Expected description after "
550 num_options
= cupsAddOption("printer-info", argv
[i
],
551 num_options
, &options
);
555 case 'I' : /* Set the supported file types (ignored) */
560 _cupsLangPuts(stderr
,
561 _("lpadmin: Expected file type(s) after \"-I\" "
566 _cupsLangPuts(stderr
,
567 _("lpadmin: Warning - content type list ignored."));
570 case 'L' : /* Set the printer-location attribute */
572 num_options
= cupsAddOption("printer-location", argv
[i
] + 2,
573 num_options
, &options
);
580 _cupsLangPuts(stderr
,
581 _("lpadmin: Expected location after \"-L\" "
586 num_options
= cupsAddOption("printer-location", argv
[i
],
587 num_options
, &options
);
592 _cupsLangPrintf(stderr
,
593 _("lpadmin: Unknown option \"%c\"."), argv
[i
][1]);
598 _cupsLangPrintf(stderr
, _("lpadmin: Unknown argument \"%s\"."),
604 * Set options as needed...
607 if ((ppd_name
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& !strcmp(ppd_name
, "everywhere") && (device_uri
= cupsGetOption("device-uri", num_options
, options
)) != NULL
)
609 if ((file
= get_printer_ppd(device_uri
, evefile
, sizeof(evefile
))) == NULL
)
612 num_options
= cupsRemoveOption("ppd-name", num_options
, &options
);
615 if (num_options
|| file
)
619 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
623 _cupsLangPrintf(stderr
,
624 _("lpadmin: Unable to connect to server: %s"),
632 _cupsLangPuts(stderr
,
633 _("lpadmin: Unable to set the printer options:\n"
634 " You must specify a printer name first."));
638 if (set_printer_options(http
, printer
, num_options
, options
, file
))
647 _cupsLangPuts(stdout
,
650 " lpadmin [-h server] -d destination\n"
651 " lpadmin [-h server] -x destination\n"
652 " lpadmin [-h server] -p printer [-c add-class] "
653 "[-i interface] [-m model]\n"
654 " [-r remove-class] [-v device] "
656 " [-P ppd-file] [-o name=value]\n"
657 " [-u allow:user,user] "
658 "[-u deny:user,user]"));
669 * 'add_printer_to_class()' - Add a printer to a class.
672 static int /* O - 0 on success, 1 on fail */
673 add_printer_to_class(http_t
*http
, /* I - Server connection */
674 char *printer
, /* I - Printer to add */
675 char *pclass
) /* I - Class to add to */
677 int i
; /* Looping var */
678 ipp_t
*request
, /* IPP Request */
679 *response
; /* IPP Response */
680 ipp_attribute_t
*attr
, /* Current attribute */
681 *members
; /* Members in class */
682 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
685 DEBUG_printf(("add_printer_to_class(%p, \"%s\", \"%s\")\n", http
,
689 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
693 * attributes-natural-language
695 * requesting-user-name
698 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
700 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
701 "localhost", 0, "/classes/%s", pclass
);
702 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
703 "printer-uri", NULL
, uri
);
704 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
708 * Do the request and get back a response...
711 response
= cupsDoRequest(http
, request
, "/");
714 * Build a CUPS-Add-Modify-Class request, which requires the following
718 * attributes-natural-language
720 * requesting-user-name
724 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
726 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
727 "printer-uri", NULL
, uri
);
728 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
732 * See if the printer is already in the class...
735 if (response
!= NULL
&&
736 (members
= ippFindAttribute(response
, "member-names",
737 IPP_TAG_NAME
)) != NULL
)
738 for (i
= 0; i
< members
->num_values
; i
++)
739 if (_cups_strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
741 _cupsLangPrintf(stderr
,
742 _("lpadmin: Printer %s is already a member of class "
743 "%s."), printer
, pclass
);
750 * OK, the printer isn't part of the class, so add it...
753 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
754 "localhost", 0, "/printers/%s", printer
);
756 if (response
!= NULL
&&
757 (members
= ippFindAttribute(response
, "member-uris",
758 IPP_TAG_URI
)) != NULL
)
761 * Add the printer to the existing list...
764 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
765 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
766 for (i
= 0; i
< members
->num_values
; i
++)
767 attr
->values
[i
].string
.text
=
768 _cupsStrAlloc(members
->values
[i
].string
.text
);
770 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
773 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
777 * Then send the request...
782 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
783 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
785 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
795 * 'default_printer()' - Set the default printing destination.
798 static int /* O - 0 on success, 1 on fail */
799 default_printer(http_t
*http
, /* I - Server connection */
800 char *printer
) /* I - Printer name */
802 ipp_t
*request
; /* IPP Request */
803 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
806 DEBUG_printf(("default_printer(%p, \"%s\")\n", http
, printer
));
809 * Build a CUPS-Set-Default request, which requires the following
813 * attributes-natural-language
815 * requesting-user-name
818 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
819 "localhost", 0, "/printers/%s", printer
);
821 request
= ippNewRequest(IPP_OP_CUPS_SET_DEFAULT
);
823 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
824 "printer-uri", NULL
, uri
);
825 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
829 * Do the request and get back a response...
832 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
834 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
836 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
846 * 'delete_printer()' - Delete a printer from the system...
849 static int /* O - 0 on success, 1 on fail */
850 delete_printer(http_t
*http
, /* I - Server connection */
851 char *printer
) /* I - Printer to delete */
853 ipp_t
*request
; /* IPP Request */
854 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
857 DEBUG_printf(("delete_printer(%p, \"%s\")\n", http
, printer
));
860 * Build a CUPS-Delete-Printer request, which requires the following
864 * attributes-natural-language
866 * requesting-user-name
869 request
= ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER
);
871 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
872 "localhost", 0, "/printers/%s", printer
);
873 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
874 "printer-uri", NULL
, uri
);
875 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
879 * Do the request and get back a response...
882 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
884 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
886 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
896 * 'delete_printer_from_class()' - Delete a printer from a class.
899 static int /* O - 0 on success, 1 on fail */
900 delete_printer_from_class(
901 http_t
*http
, /* I - Server connection */
902 char *printer
, /* I - Printer to remove */
903 char *pclass
) /* I - Class to remove from */
905 int i
, j
, k
; /* Looping vars */
906 ipp_t
*request
, /* IPP Request */
907 *response
; /* IPP Response */
908 ipp_attribute_t
*attr
, /* Current attribute */
909 *members
; /* Members in class */
910 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
913 DEBUG_printf(("delete_printer_from_class(%p, \"%s\", \"%s\")\n", http
,
917 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
921 * attributes-natural-language
923 * requesting-user-name
926 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
928 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
929 "localhost", 0, "/classes/%s", pclass
);
930 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
931 "printer-uri", NULL
, uri
);
932 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
936 * Do the request and get back a response...
939 if ((response
= cupsDoRequest(http
, request
, "/classes/")) == NULL
||
940 response
->request
.status
.status_code
== IPP_STATUS_ERROR_NOT_FOUND
)
942 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
950 * See if the printer is already in the class...
953 if ((members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) == NULL
)
955 _cupsLangPuts(stderr
, _("lpadmin: No member names were seen."));
962 for (i
= 0; i
< members
->num_values
; i
++)
963 if (!_cups_strcasecmp(printer
, members
->values
[i
].string
.text
))
966 if (i
>= members
->num_values
)
968 _cupsLangPrintf(stderr
,
969 _("lpadmin: Printer %s is not a member of class %s."),
977 if (members
->num_values
== 1)
980 * Build a CUPS-Delete-Class request, which requires the following
984 * attributes-natural-language
986 * requesting-user-name
989 request
= ippNewRequest(IPP_OP_CUPS_DELETE_CLASS
);
991 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
992 "printer-uri", NULL
, uri
);
993 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
994 "requesting-user-name", NULL
, cupsUser());
999 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
1002 * attributes-charset
1003 * attributes-natural-language
1005 * requesting-user-name
1009 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_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());
1017 * Delete the printer from the class...
1020 members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
);
1021 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1022 "member-uris", members
->num_values
- 1, NULL
, NULL
);
1024 for (j
= 0, k
= 0; j
< members
->num_values
; j
++)
1026 attr
->values
[k
++].string
.text
=
1027 _cupsStrAlloc(members
->values
[j
].string
.text
);
1031 * Then send the request...
1034 ippDelete(response
);
1036 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1038 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1040 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1050 * 'delete_printer_option()' - Delete a printer option.
1053 static int /* O - 0 on success, 1 on fail */
1054 delete_printer_option(http_t
*http
, /* I - Server connection */
1055 char *printer
, /* I - Printer */
1056 char *option
) /* I - Option to delete */
1058 ipp_t
*request
; /* IPP request */
1059 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1063 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1064 * requires the following attributes:
1066 * attributes-charset
1067 * attributes-natural-language
1069 * requesting-user-name
1070 * option with deleteAttr tag
1073 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1074 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1076 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1078 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1079 "printer-uri", NULL
, uri
);
1080 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1081 "requesting-user-name", NULL
, cupsUser());
1082 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_DELETEATTR
, option
, 0);
1085 * Do the request and get back a response...
1088 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1090 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1092 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1102 * 'enable_printer()' - Enable a printer...
1105 static int /* O - 0 on success, 1 on fail */
1106 enable_printer(http_t
*http
, /* I - Server connection */
1107 char *printer
) /* I - Printer to enable */
1109 ipp_t
*request
; /* IPP Request */
1110 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1113 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http
, printer
));
1116 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1117 * require the following attributes:
1119 * attributes-charset
1120 * attributes-natural-language
1122 * requesting-user-name
1124 * printer-is-accepting-jobs
1127 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1128 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1130 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1132 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1133 "printer-uri", NULL
, uri
);
1134 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1135 "requesting-user-name", NULL
, cupsUser());
1136 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1138 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1141 * Do the request and get back a response...
1144 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1146 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1148 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1158 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
1161 static char * /* O - Filename or NULL */
1162 get_printer_ppd(const char *uri
, /* I - Printer URI */
1163 char *buffer
, /* I - Filename buffer */
1164 size_t bufsize
) /* I - Size of filename buffer */
1166 http_t
*http
; /* Connection to printer */
1167 ipp_t
*request
, /* Get-Printer-Attributes request */
1168 *response
; /* Get-Printer-Attributes response */
1169 char resolved
[1024], /* Resolved URI */
1170 scheme
[32], /* URI scheme */
1171 userpass
[256], /* Username:password */
1172 host
[256], /* Hostname */
1173 resource
[256]; /* Resource path */
1174 int port
; /* Port number */
1178 * Connect to the printer...
1181 if (strstr(uri
, "._tcp"))
1187 if (!_httpResolveURI(uri
, resolved
, sizeof(resolved
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
1189 _cupsLangPrintf(stderr
, _("%s: Unable to resolve \"%s\"."), "lpadmin", uri
);
1196 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
1198 _cupsLangPrintf(stderr
, _("%s: Bad printer URI \"%s\"."), "lpadmin", uri
);
1202 http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, !strcmp(scheme
, "ipps") ? HTTP_ENCRYPTION_ALWAYS
: HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
);
1205 _cupsLangPrintf(stderr
, _("%s: Unable to connect to \"%s:%d\": %s"), "lpadmin", host
, port
, cupsLastErrorString());
1210 * Send a Get-Printer-Attributes request...
1213 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1214 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1215 response
= cupsDoRequest(http
, request
, resource
);
1217 if (!_ppdCreateFromIPP(buffer
, bufsize
, response
))
1218 _cupsLangPrintf(stderr
, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno
));
1220 ippDelete(response
);
1231 * 'get_printer_type()' - Determine the printer type and URI.
1234 static cups_ptype_t
/* O - printer-type value */
1235 get_printer_type(http_t
*http
, /* I - Server connection */
1236 char *printer
, /* I - Printer name */
1237 char *uri
, /* I - URI buffer */
1238 size_t urisize
) /* I - Size of URI buffer */
1240 ipp_t
*request
, /* IPP request */
1241 *response
; /* IPP response */
1242 ipp_attribute_t
*attr
; /* printer-type attribute */
1243 cups_ptype_t type
; /* printer-type value */
1247 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1250 * attributes-charset
1251 * attributes-natural-language
1253 * requested-attributes
1254 * requesting-user-name
1257 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1259 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1260 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1261 "printer-uri", NULL
, uri
);
1262 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1263 "requested-attributes", NULL
, "printer-type");
1264 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1265 "requesting-user-name", NULL
, cupsUser());
1271 response
= cupsDoRequest(http
, request
, "/");
1272 if ((attr
= ippFindAttribute(response
, "printer-type",
1273 IPP_TAG_ENUM
)) != NULL
)
1275 type
= (cups_ptype_t
)attr
->values
[0].integer
;
1277 if (type
& CUPS_PRINTER_CLASS
)
1278 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/classes/%s", printer
);
1281 type
= CUPS_PRINTER_LOCAL
;
1283 ippDelete(response
);
1290 * 'set_printer_options()' - Set the printer options.
1293 static int /* O - 0 on success, 1 on fail */
1294 set_printer_options(
1295 http_t
*http
, /* I - Server connection */
1296 char *printer
, /* I - Printer */
1297 int num_options
, /* I - Number of options */
1298 cups_option_t
*options
, /* I - Options */
1299 char *file
) /* I - PPD file/interface script */
1301 ipp_t
*request
; /* IPP Request */
1302 const char *ppdfile
; /* PPD filename */
1303 int ppdchanged
= 0; /* PPD changed? */
1304 ppd_file_t
*ppd
; /* PPD file */
1305 ppd_choice_t
*choice
; /* Marked choice */
1306 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1307 line
[1024], /* Line from PPD file */
1308 keyword
[1024], /* Keyword from Default line */
1309 *keyptr
, /* Pointer into keyword... */
1310 tempfile
[1024]; /* Temporary filename */
1311 cups_file_t
*in
, /* PPD file */
1312 *out
; /* Temporary file */
1313 const char *ppdname
, /* ppd-name value */
1314 *protocol
, /* Old protocol option */
1315 *customval
, /* Custom option value */
1316 *boolval
; /* Boolean value */
1317 int wrote_ipp_supplies
= 0, /* Wrote cupsIPPSupplies keyword? */
1318 wrote_snmp_supplies
= 0,/* Wrote cupsSNMPSupplies keyword? */
1319 copied_options
= 0; /* Copied options? */
1322 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1323 "options=%p, file=\"%s\")\n", http
, printer
, num_options
,
1327 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1328 * which requires the following attributes:
1330 * attributes-charset
1331 * attributes-natural-language
1333 * requesting-user-name
1337 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1338 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1340 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1342 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1343 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1346 * Add the options...
1351 else if ((ppdname
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& strcmp(ppdname
, "raw") && num_options
> 1)
1353 if ((ppdfile
= cupsGetServerPPD(http
, ppdname
)) != NULL
)
1356 * Copy options array and remove ppd-name from it...
1359 cups_option_t
*temp
= NULL
, *optr
;
1360 int i
, num_temp
= 0;
1361 for (i
= num_options
, optr
= options
; i
> 0; i
--, optr
++)
1362 if (strcmp(optr
->name
, "ppd-name"))
1363 num_temp
= cupsAddOption(optr
->name
, optr
->value
, num_temp
, &temp
);
1367 num_options
= num_temp
;
1371 else if (request
->request
.op
.operation_id
== IPP_OP_CUPS_ADD_MODIFY_PRINTER
)
1372 ppdfile
= cupsGetPPD(printer
);
1376 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
1377 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1379 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1381 if (!_cups_strcasecmp(protocol
, "bcp"))
1382 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1384 else if (!_cups_strcasecmp(protocol
, "tbcp"))
1385 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1392 * Set default options in the PPD file...
1395 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
1397 int linenum
; /* Line number of error */
1398 ppd_status_t status
= ppdLastError(&linenum
);
1401 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile
, ppdErrorString(status
), linenum
);
1404 ppdMarkDefaults(ppd
);
1405 cupsMarkOptions(ppd
, num_options
, options
);
1407 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1409 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1411 if (ppdfile
!= file
)
1414 cupsFreeOptions(num_options
, options
);
1418 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1420 _cupsLangPrintf(stderr
,
1421 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1422 ppdfile
, strerror(errno
));
1424 if (ppdfile
!= file
)
1427 cupsFreeOptions(num_options
, options
);
1433 while (cupsFileGets(in
, line
, sizeof(line
)))
1435 if (!strncmp(line
, "*cupsIPPSupplies:", 17) &&
1436 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1439 wrote_ipp_supplies
= 1;
1440 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1441 (!_cups_strcasecmp(boolval
, "true") ||
1442 !_cups_strcasecmp(boolval
, "yes") ||
1443 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1445 else if (!strncmp(line
, "*cupsSNMPSupplies:", 18) &&
1446 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1449 wrote_snmp_supplies
= 1;
1450 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1451 (!_cups_strcasecmp(boolval
, "true") ||
1452 !_cups_strcasecmp(boolval
, "yes") ||
1453 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1455 else if (strncmp(line
, "*Default", 8))
1456 cupsFilePrintf(out
, "%s\n", line
);
1460 * Get default option name...
1463 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
1465 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1466 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1470 while (isspace(*keyptr
& 255))
1473 if (!strcmp(keyword
, "PageRegion") ||
1474 !strcmp(keyword
, "PageSize") ||
1475 !strcmp(keyword
, "PaperDimension") ||
1476 !strcmp(keyword
, "ImageableArea"))
1478 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1479 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1482 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1484 if (choice
&& strcmp(choice
->choice
, keyptr
))
1486 if (strcmp(choice
->choice
, "Custom"))
1488 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1491 else if ((customval
= cupsGetOption(keyword
, num_options
,
1494 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1498 cupsFilePrintf(out
, "%s\n", line
);
1501 cupsFilePrintf(out
, "%s\n", line
);
1505 if (!wrote_ipp_supplies
&&
1506 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1509 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1510 (!_cups_strcasecmp(boolval
, "true") ||
1511 !_cups_strcasecmp(boolval
, "yes") ||
1512 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1515 if (!wrote_snmp_supplies
&&
1516 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1519 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1520 (!_cups_strcasecmp(boolval
, "true") ||
1521 !_cups_strcasecmp(boolval
, "yes") ||
1522 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1533 ippDelete(cupsDoFileRequest(http
, request
, "/admin/",
1534 ppdchanged
? tempfile
: file
));
1537 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1541 if (ppdfile
!= file
)
1548 * No PPD file - just set the options...
1551 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1555 cupsFreeOptions(num_options
, options
);
1558 * Check the response...
1561 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1563 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1573 * 'validate_name()' - Make sure the printer name only contains valid chars.
1576 static int /* O - 0 if name is no good, 1 if name is good */
1577 validate_name(const char *name
) /* I - Name to check */
1579 const char *ptr
; /* Pointer into name */
1583 * Scan the whole name...
1586 for (ptr
= name
; *ptr
; ptr
++)
1589 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' ||
1594 * All the characters are good; validate the length, too...
1597 return ((ptr
- name
) < 128);