2 * "lpadmin" command for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #define _CUPS_NO_DEPRECATED
15 #define _PPD_DEPRECATED
16 #include <cups/cups-private.h>
17 #include <cups/ppd-private.h>
24 static int add_printer_to_class(http_t
*http
, char *printer
, char *pclass
);
25 static int default_printer(http_t
*http
, char *printer
);
26 static int delete_printer(http_t
*http
, char *printer
);
27 static int delete_printer_from_class(http_t
*http
, char *printer
,
29 static int delete_printer_option(http_t
*http
, char *printer
,
31 static int enable_printer(http_t
*http
, char *printer
);
32 static char *get_printer_ppd(const char *uri
, char *buffer
, size_t bufsize
);
33 static cups_ptype_t
get_printer_type(http_t
*http
, char *printer
, char *uri
,
35 static int set_printer_options(http_t
*http
, char *printer
,
36 int num_options
, cups_option_t
*options
,
38 static int validate_name(const char *name
);
42 * 'main()' - Parse options and configure the scheduler.
46 main(int argc
, /* I - Number of command-line arguments */
47 char *argv
[]) /* I - Command-line arguments */
49 int i
; /* Looping var */
50 http_t
*http
; /* Connection to server */
51 char *printer
, /* Destination printer */
52 *pclass
, /* Printer class name */
53 *opt
, /* Option pointer */
54 *val
; /* Pointer to allow/deny value */
55 int num_options
; /* Number of options */
56 cups_option_t
*options
; /* Options */
57 char *file
, /* New PPD file */
59 /* IPP Everywhere PPD */
60 const char *ppd_name
, /* ppd-name value */
61 *device_uri
; /* device-uri value */
72 for (i
= 1; i
< argc
; i
++)
74 if (argv
[i
][0] == '-')
76 for (opt
= argv
[i
] + 1; *opt
; opt
++)
80 case 'c' : /* Add printer to class */
83 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
87 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
95 _("lpadmin: Unable to add a printer to the class:\n"
96 " You must specify a printer name first."));
103 opt
+= strlen(opt
) - 1;
111 _cupsLangPuts(stderr
, _("lpadmin: Expected class name after \"-c\" option."));
118 if (!validate_name(pclass
))
120 _cupsLangPuts(stderr
,
121 _("lpadmin: Class name can only contain printable "
126 if (add_printer_to_class(http
, printer
, pclass
))
130 case 'd' : /* Set as default destination */
133 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
137 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
145 opt
+= strlen(opt
) - 1;
153 _cupsLangPuts(stderr
, _("lpadmin: Expected printer name after \"-d\" option."));
160 if (!validate_name(printer
))
162 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
166 if (default_printer(http
, printer
))
172 case 'h' : /* Connect to host */
181 cupsSetServer(opt
+ 1);
182 opt
+= strlen(opt
) - 1;
190 _cupsLangPuts(stderr
, _("lpadmin: Expected hostname after \"-h\" option."));
194 cupsSetServer(argv
[i
]);
198 case 'P' : /* Use the specified PPD file */
199 case 'i' : /* Use the specified PPD file */
203 opt
+= strlen(opt
) - 1;
211 _cupsLangPrintf(stderr
, _("lpadmin: Expected PPD after \"-%c\" option."), argv
[i
- 1][1]);
221 * Check to see that the specified file is, in fact, a PPD...
224 cups_file_t
*fp
= cupsFileOpen(file
, "r");
227 if (!cupsFileGets(fp
, line
, sizeof(line
)) || strncmp(line
, "*PPD-Adobe", 10))
229 _cupsLangPuts(stderr
, _("lpadmin: System V interface scripts are no longer supported for security reasons."));
238 case 'E' : /* Enable the printer/enable encryption */
242 cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED
);
245 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
247 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
248 #endif /* HAVE_SSL */
254 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
258 _cupsLangPrintf(stderr
,
259 _("lpadmin: Unable to connect to server: %s"),
265 if (enable_printer(http
, printer
))
269 case 'm' : /* Use the specified standard script/PPD file */
272 num_options
= cupsAddOption("ppd-name", opt
+ 1, num_options
, &options
);
273 opt
+= strlen(opt
) - 1;
281 _cupsLangPuts(stderr
, _("lpadmin: Expected model after \"-m\" option."));
285 num_options
= cupsAddOption("ppd-name", argv
[i
], num_options
, &options
);
289 case 'o' : /* Set option */
292 num_options
= cupsParseOptions(opt
+ 1, num_options
, &options
);
293 opt
+= strlen(opt
) - 1;
301 _cupsLangPuts(stderr
, _("lpadmin: Expected name=value after \"-o\" option."));
305 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
309 case 'p' : /* Add/modify a printer */
313 opt
+= strlen(opt
) - 1;
321 _cupsLangPuts(stderr
, _("lpadmin: Expected printer after \"-p\" option."));
328 if (!validate_name(printer
))
330 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
335 case 'r' : /* Remove printer from class */
338 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
342 _cupsLangPrintf(stderr
,
343 _("lpadmin: Unable to connect to server: %s"),
351 _cupsLangPuts(stderr
,
352 _("lpadmin: Unable to remove a printer from the class:\n"
353 " You must specify a printer name first."));
360 opt
+= strlen(opt
) - 1;
368 _cupsLangPuts(stderr
, _("lpadmin: Expected class after \"-r\" option."));
375 if (!validate_name(pclass
))
377 _cupsLangPuts(stderr
, _("lpadmin: Class name can only contain printable characters."));
381 if (delete_printer_from_class(http
, printer
, pclass
))
385 case 'R' : /* Remove option */
388 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
392 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"), strerror(errno
));
399 _cupsLangPuts(stderr
,
400 _("lpadmin: Unable to delete option:\n"
401 " You must specify a printer name first."));
408 opt
+= strlen(opt
) - 1;
416 _cupsLangPuts(stderr
, _("lpadmin: Expected name after \"-R\" option."));
423 if (delete_printer_option(http
, printer
, val
))
427 case 'U' : /* Username */
430 cupsSetUser(opt
+ 1);
431 opt
+= strlen(opt
) - 1;
438 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-U\" option."), argv
[0]);
442 cupsSetUser(argv
[i
]);
446 case 'u' : /* Allow/deny users */
450 opt
+= strlen(opt
) - 1;
458 _cupsLangPuts(stderr
, _("lpadmin: Expected allow/deny:userlist after \"-u\" option."));
465 if (!_cups_strncasecmp(val
, "allow:", 6))
466 num_options
= cupsAddOption("requesting-user-name-allowed", val
+ 6, num_options
, &options
);
467 else if (!_cups_strncasecmp(val
, "deny:", 5))
468 num_options
= cupsAddOption("requesting-user-name-denied", val
+ 5, num_options
, &options
);
471 _cupsLangPrintf(stderr
, _("lpadmin: Unknown allow/deny option \"%s\"."), val
);
476 case 'v' : /* Set the device-uri attribute */
479 num_options
= cupsAddOption("device-uri", opt
+ 1, num_options
, &options
);
480 opt
+= strlen(opt
) - 1;
488 _cupsLangPuts(stderr
, _("lpadmin: Expected device URI after \"-v\" option."));
492 num_options
= cupsAddOption("device-uri", argv
[i
], num_options
, &options
);
496 case 'x' : /* Delete a printer */
499 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
503 _cupsLangPrintf(stderr
,
504 _("lpadmin: Unable to connect to server: %s"),
513 opt
+= strlen(opt
) - 1;
521 _cupsLangPuts(stderr
, _("lpadmin: Expected printer or class after \"-x\" option."));
528 if (!validate_name(printer
))
530 _cupsLangPuts(stderr
, _("lpadmin: Printer name can only contain printable characters."));
534 if (delete_printer(http
, printer
))
540 case 'D' : /* Set the printer-info attribute */
543 num_options
= cupsAddOption("printer-info", opt
+ 1, num_options
, &options
);
544 opt
+= strlen(opt
) - 1;
552 _cupsLangPuts(stderr
, _("lpadmin: Expected description after \"-D\" option."));
556 num_options
= cupsAddOption("printer-info", argv
[i
], num_options
, &options
);
560 case 'I' : /* Set the supported file types (ignored) */
565 _cupsLangPuts(stderr
, _("lpadmin: Expected file type(s) after \"-I\" option."));
569 _cupsLangPuts(stderr
, _("lpadmin: Warning - content type list ignored."));
572 case 'L' : /* Set the printer-location attribute */
575 num_options
= cupsAddOption("printer-location", opt
+ 1, num_options
, &options
);
576 opt
+= strlen(opt
) - 1;
584 _cupsLangPuts(stderr
, _("lpadmin: Expected location after \"-L\" option."));
588 num_options
= cupsAddOption("printer-location", argv
[i
], num_options
, &options
);
593 _cupsLangPrintf(stderr
, _("lpadmin: Unknown option \"%c\"."), *opt
);
600 _cupsLangPrintf(stderr
, _("lpadmin: Unknown argument \"%s\"."), argv
[i
]);
606 * Set options as needed...
609 if ((ppd_name
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& !strcmp(ppd_name
, "everywhere") && (device_uri
= cupsGetOption("device-uri", num_options
, options
)) != NULL
)
611 if ((file
= get_printer_ppd(device_uri
, evefile
, sizeof(evefile
))) == NULL
)
614 num_options
= cupsRemoveOption("ppd-name", num_options
, &options
);
617 if (num_options
|| file
)
621 _cupsLangPuts(stderr
,
622 _("lpadmin: Unable to set the printer options:\n"
623 " You must specify a printer name first."));
629 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
630 cupsEncryption(), 1, 30000, NULL
);
633 _cupsLangPrintf(stderr
, _("lpadmin: Unable to connect to server: %s"),
639 if (set_printer_options(http
, printer
, num_options
, options
, file
))
648 _cupsLangPuts(stdout
,
651 " lpadmin [-h server] -d destination\n"
652 " lpadmin [-h server] -x destination\n"
653 " lpadmin [-h server] -p printer [-c add-class] "
654 "[-i interface] [-m model]\n"
655 " [-r remove-class] [-v device] "
657 " [-P ppd-file] [-o name=value]\n"
658 " [-u allow:user,user] "
659 "[-u deny:user,user]"));
670 * 'add_printer_to_class()' - Add a printer to a class.
673 static int /* O - 0 on success, 1 on fail */
674 add_printer_to_class(http_t
*http
, /* I - Server connection */
675 char *printer
, /* I - Printer to add */
676 char *pclass
) /* I - Class to add to */
678 int i
; /* Looping var */
679 ipp_t
*request
, /* IPP Request */
680 *response
; /* IPP Response */
681 ipp_attribute_t
*attr
, /* Current attribute */
682 *members
; /* Members in class */
683 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
686 DEBUG_printf(("add_printer_to_class(%p, \"%s\", \"%s\")\n", http
,
690 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
694 * attributes-natural-language
696 * requesting-user-name
699 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
701 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
702 "localhost", 0, "/classes/%s", pclass
);
703 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
704 "printer-uri", NULL
, uri
);
705 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
709 * Do the request and get back a response...
712 response
= cupsDoRequest(http
, request
, "/");
715 * Build a CUPS-Add-Modify-Class request, which requires the following
719 * attributes-natural-language
721 * requesting-user-name
725 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
727 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
728 "printer-uri", NULL
, uri
);
729 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
733 * See if the printer is already in the class...
736 if (response
!= NULL
&&
737 (members
= ippFindAttribute(response
, "member-names",
738 IPP_TAG_NAME
)) != NULL
)
739 for (i
= 0; i
< members
->num_values
; i
++)
740 if (_cups_strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
742 _cupsLangPrintf(stderr
,
743 _("lpadmin: Printer %s is already a member of class "
744 "%s."), printer
, pclass
);
751 * OK, the printer isn't part of the class, so add it...
754 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
755 "localhost", 0, "/printers/%s", printer
);
757 if (response
!= NULL
&&
758 (members
= ippFindAttribute(response
, "member-uris",
759 IPP_TAG_URI
)) != NULL
)
762 * Add the printer to the existing list...
765 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
766 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
767 for (i
= 0; i
< members
->num_values
; i
++)
768 attr
->values
[i
].string
.text
=
769 _cupsStrAlloc(members
->values
[i
].string
.text
);
771 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
774 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
778 * Then send the request...
783 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
784 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
786 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
796 * 'default_printer()' - Set the default printing destination.
799 static int /* O - 0 on success, 1 on fail */
800 default_printer(http_t
*http
, /* I - Server connection */
801 char *printer
) /* I - Printer name */
803 ipp_t
*request
; /* IPP Request */
804 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
807 DEBUG_printf(("default_printer(%p, \"%s\")\n", http
, printer
));
810 * Build a CUPS-Set-Default request, which requires the following
814 * attributes-natural-language
816 * requesting-user-name
819 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
820 "localhost", 0, "/printers/%s", printer
);
822 request
= ippNewRequest(IPP_OP_CUPS_SET_DEFAULT
);
824 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
825 "printer-uri", NULL
, uri
);
826 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
830 * Do the request and get back a response...
833 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
835 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
837 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
847 * 'delete_printer()' - Delete a printer from the system...
850 static int /* O - 0 on success, 1 on fail */
851 delete_printer(http_t
*http
, /* I - Server connection */
852 char *printer
) /* I - Printer to delete */
854 ipp_t
*request
; /* IPP Request */
855 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
858 DEBUG_printf(("delete_printer(%p, \"%s\")\n", http
, printer
));
861 * Build a CUPS-Delete-Printer request, which requires the following
865 * attributes-natural-language
867 * requesting-user-name
870 request
= ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER
);
872 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
873 "localhost", 0, "/printers/%s", printer
);
874 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
875 "printer-uri", NULL
, uri
);
876 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
880 * Do the request and get back a response...
883 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
885 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
887 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
897 * 'delete_printer_from_class()' - Delete a printer from a class.
900 static int /* O - 0 on success, 1 on fail */
901 delete_printer_from_class(
902 http_t
*http
, /* I - Server connection */
903 char *printer
, /* I - Printer to remove */
904 char *pclass
) /* I - Class to remove from */
906 int i
, j
, k
; /* Looping vars */
907 ipp_t
*request
, /* IPP Request */
908 *response
; /* IPP Response */
909 ipp_attribute_t
*attr
, /* Current attribute */
910 *members
; /* Members in class */
911 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
914 DEBUG_printf(("delete_printer_from_class(%p, \"%s\", \"%s\")\n", http
,
918 * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
922 * attributes-natural-language
924 * requesting-user-name
927 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
929 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
930 "localhost", 0, "/classes/%s", pclass
);
931 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
932 "printer-uri", NULL
, uri
);
933 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
937 * Do the request and get back a response...
940 if ((response
= cupsDoRequest(http
, request
, "/classes/")) == NULL
||
941 response
->request
.status
.status_code
== IPP_STATUS_ERROR_NOT_FOUND
)
943 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
951 * See if the printer is already in the class...
954 if ((members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) == NULL
)
956 _cupsLangPuts(stderr
, _("lpadmin: No member names were seen."));
963 for (i
= 0; i
< members
->num_values
; i
++)
964 if (!_cups_strcasecmp(printer
, members
->values
[i
].string
.text
))
967 if (i
>= members
->num_values
)
969 _cupsLangPrintf(stderr
,
970 _("lpadmin: Printer %s is not a member of class %s."),
978 if (members
->num_values
== 1)
981 * Build a CUPS-Delete-Class request, which requires the following
985 * attributes-natural-language
987 * requesting-user-name
990 request
= ippNewRequest(IPP_OP_CUPS_DELETE_CLASS
);
992 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
993 "printer-uri", NULL
, uri
);
994 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
995 "requesting-user-name", NULL
, cupsUser());
1000 * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
1003 * attributes-charset
1004 * attributes-natural-language
1006 * requesting-user-name
1010 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1012 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1013 "printer-uri", NULL
, uri
);
1014 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1015 "requesting-user-name", NULL
, cupsUser());
1018 * Delete the printer from the class...
1021 members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
);
1022 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1023 "member-uris", members
->num_values
- 1, NULL
, NULL
);
1025 for (j
= 0, k
= 0; j
< members
->num_values
; j
++)
1027 attr
->values
[k
++].string
.text
=
1028 _cupsStrAlloc(members
->values
[j
].string
.text
);
1032 * Then send the request...
1035 ippDelete(response
);
1037 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1039 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1041 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1051 * 'delete_printer_option()' - Delete a printer option.
1054 static int /* O - 0 on success, 1 on fail */
1055 delete_printer_option(http_t
*http
, /* I - Server connection */
1056 char *printer
, /* I - Printer */
1057 char *option
) /* I - Option to delete */
1059 ipp_t
*request
; /* IPP request */
1060 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1064 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1065 * requires the following attributes:
1067 * attributes-charset
1068 * attributes-natural-language
1070 * requesting-user-name
1071 * option with deleteAttr tag
1074 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1075 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1077 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1079 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1080 "printer-uri", NULL
, uri
);
1081 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1082 "requesting-user-name", NULL
, cupsUser());
1083 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_DELETEATTR
, option
, 0);
1086 * Do the request and get back a response...
1089 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1091 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1093 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1103 * 'enable_printer()' - Enable a printer...
1106 static int /* O - 0 on success, 1 on fail */
1107 enable_printer(http_t
*http
, /* I - Server connection */
1108 char *printer
) /* I - Printer to enable */
1110 ipp_t
*request
; /* IPP Request */
1111 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1114 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http
, printer
));
1117 * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
1118 * require the following attributes:
1120 * attributes-charset
1121 * attributes-natural-language
1123 * requesting-user-name
1125 * printer-is-accepting-jobs
1128 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1129 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1131 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1133 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1134 "printer-uri", NULL
, uri
);
1135 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1136 "requesting-user-name", NULL
, cupsUser());
1137 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1139 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1142 * Do the request and get back a response...
1145 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1147 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1149 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1159 * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
1162 static char * /* O - Filename or NULL */
1163 get_printer_ppd(const char *uri
, /* I - Printer URI */
1164 char *buffer
, /* I - Filename buffer */
1165 size_t bufsize
) /* I - Size of filename buffer */
1167 http_t
*http
; /* Connection to printer */
1168 ipp_t
*request
, /* Get-Printer-Attributes request */
1169 *response
; /* Get-Printer-Attributes response */
1170 char resolved
[1024], /* Resolved URI */
1171 scheme
[32], /* URI scheme */
1172 userpass
[256], /* Username:password */
1173 host
[256], /* Hostname */
1174 resource
[256]; /* Resource path */
1175 int port
; /* Port number */
1179 * Connect to the printer...
1182 if (strstr(uri
, "._tcp"))
1188 if (!_httpResolveURI(uri
, resolved
, sizeof(resolved
), _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
))
1190 _cupsLangPrintf(stderr
, _("%s: Unable to resolve \"%s\"."), "lpadmin", uri
);
1197 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
1199 _cupsLangPrintf(stderr
, _("%s: Bad printer URI \"%s\"."), "lpadmin", uri
);
1203 http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, !strcmp(scheme
, "ipps") ? HTTP_ENCRYPTION_ALWAYS
: HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
);
1206 _cupsLangPrintf(stderr
, _("%s: Unable to connect to \"%s:%d\": %s"), "lpadmin", host
, port
, cupsLastErrorString());
1211 * Send a Get-Printer-Attributes request...
1214 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1215 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1216 response
= cupsDoRequest(http
, request
, resource
);
1218 if (!_ppdCreateFromIPP(buffer
, bufsize
, response
))
1219 _cupsLangPrintf(stderr
, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno
));
1221 ippDelete(response
);
1232 * 'get_printer_type()' - Determine the printer type and URI.
1235 static cups_ptype_t
/* O - printer-type value */
1236 get_printer_type(http_t
*http
, /* I - Server connection */
1237 char *printer
, /* I - Printer name */
1238 char *uri
, /* I - URI buffer */
1239 size_t urisize
) /* I - Size of URI buffer */
1241 ipp_t
*request
, /* IPP request */
1242 *response
; /* IPP response */
1243 ipp_attribute_t
*attr
; /* printer-type attribute */
1244 cups_ptype_t type
; /* printer-type value */
1248 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1251 * attributes-charset
1252 * attributes-natural-language
1254 * requested-attributes
1255 * requesting-user-name
1258 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/printers/%s", printer
);
1260 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
1261 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1262 "printer-uri", NULL
, uri
);
1263 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1264 "requested-attributes", NULL
, "printer-type");
1265 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1266 "requesting-user-name", NULL
, cupsUser());
1272 response
= cupsDoRequest(http
, request
, "/");
1273 if ((attr
= ippFindAttribute(response
, "printer-type",
1274 IPP_TAG_ENUM
)) != NULL
)
1276 type
= (cups_ptype_t
)attr
->values
[0].integer
;
1278 if (type
& CUPS_PRINTER_CLASS
)
1279 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, (int)urisize
, "ipp", NULL
, "localhost", ippPort(), "/classes/%s", printer
);
1282 type
= CUPS_PRINTER_LOCAL
;
1284 ippDelete(response
);
1291 * 'set_printer_options()' - Set the printer options.
1294 static int /* O - 0 on success, 1 on fail */
1295 set_printer_options(
1296 http_t
*http
, /* I - Server connection */
1297 char *printer
, /* I - Printer */
1298 int num_options
, /* I - Number of options */
1299 cups_option_t
*options
, /* I - Options */
1300 char *file
) /* I - PPD file/interface script */
1302 ipp_t
*request
; /* IPP Request */
1303 const char *ppdfile
; /* PPD filename */
1304 int ppdchanged
= 0; /* PPD changed? */
1305 ppd_file_t
*ppd
; /* PPD file */
1306 ppd_choice_t
*choice
; /* Marked choice */
1307 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1308 line
[1024], /* Line from PPD file */
1309 keyword
[1024], /* Keyword from Default line */
1310 *keyptr
, /* Pointer into keyword... */
1311 tempfile
[1024]; /* Temporary filename */
1312 cups_file_t
*in
, /* PPD file */
1313 *out
; /* Temporary file */
1314 const char *ppdname
, /* ppd-name value */
1315 *protocol
, /* Old protocol option */
1316 *customval
, /* Custom option value */
1317 *boolval
; /* Boolean value */
1318 int wrote_ipp_supplies
= 0, /* Wrote cupsIPPSupplies keyword? */
1319 wrote_snmp_supplies
= 0,/* Wrote cupsSNMPSupplies keyword? */
1320 copied_options
= 0; /* Copied options? */
1323 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1324 "options=%p, file=\"%s\")\n", http
, printer
, num_options
,
1328 * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
1329 * which requires the following attributes:
1331 * attributes-charset
1332 * attributes-natural-language
1334 * requesting-user-name
1338 if (get_printer_type(http
, printer
, uri
, sizeof(uri
)) & CUPS_PRINTER_CLASS
)
1339 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS
);
1341 request
= ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER
);
1343 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
1344 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
1347 * Add the options...
1352 else if ((ppdname
= cupsGetOption("ppd-name", num_options
, options
)) != NULL
&& strcmp(ppdname
, "raw") && num_options
> 1)
1354 if ((ppdfile
= cupsGetServerPPD(http
, ppdname
)) != NULL
)
1357 * Copy options array and remove ppd-name from it...
1360 cups_option_t
*temp
= NULL
, *optr
;
1361 int i
, num_temp
= 0;
1362 for (i
= num_options
, optr
= options
; i
> 0; i
--, optr
++)
1363 if (strcmp(optr
->name
, "ppd-name"))
1364 num_temp
= cupsAddOption(optr
->name
, optr
->value
, num_temp
, &temp
);
1368 num_options
= num_temp
;
1372 else if (request
->request
.op
.operation_id
== IPP_OP_CUPS_ADD_MODIFY_PRINTER
)
1373 ppdfile
= cupsGetPPD(printer
);
1377 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
1378 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1380 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1382 if (!_cups_strcasecmp(protocol
, "bcp"))
1383 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1385 else if (!_cups_strcasecmp(protocol
, "tbcp"))
1386 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1393 * Set default options in the PPD file...
1396 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
1398 int linenum
; /* Line number of error */
1399 ppd_status_t status
= ppdLastError(&linenum
);
1402 _cupsLangPrintf(stderr
, _("lpadmin: Unable to open PPD \"%s\": %s on line %d."), ppdfile
, ppdErrorString(status
), linenum
);
1405 ppdMarkDefaults(ppd
);
1406 cupsMarkOptions(ppd
, num_options
, options
);
1408 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1410 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1412 if (ppdfile
!= file
)
1415 cupsFreeOptions(num_options
, options
);
1419 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1421 _cupsLangPrintf(stderr
,
1422 _("lpadmin: Unable to open PPD file \"%s\" - %s"),
1423 ppdfile
, strerror(errno
));
1425 if (ppdfile
!= file
)
1428 cupsFreeOptions(num_options
, options
);
1434 while (cupsFileGets(in
, line
, sizeof(line
)))
1436 if (!strncmp(line
, "*cupsIPPSupplies:", 17) &&
1437 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1440 wrote_ipp_supplies
= 1;
1441 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1442 (!_cups_strcasecmp(boolval
, "true") ||
1443 !_cups_strcasecmp(boolval
, "yes") ||
1444 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1446 else if (!strncmp(line
, "*cupsSNMPSupplies:", 18) &&
1447 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1450 wrote_snmp_supplies
= 1;
1451 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1452 (!_cups_strcasecmp(boolval
, "true") ||
1453 !_cups_strcasecmp(boolval
, "yes") ||
1454 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1456 else if (strncmp(line
, "*Default", 8))
1457 cupsFilePrintf(out
, "%s\n", line
);
1461 * Get default option name...
1464 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
1466 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1467 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1471 while (isspace(*keyptr
& 255))
1474 if (!strcmp(keyword
, "PageRegion") ||
1475 !strcmp(keyword
, "PageSize") ||
1476 !strcmp(keyword
, "PaperDimension") ||
1477 !strcmp(keyword
, "ImageableArea"))
1479 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1480 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1483 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1485 if (choice
&& strcmp(choice
->choice
, keyptr
))
1487 if (strcmp(choice
->choice
, "Custom"))
1489 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1492 else if ((customval
= cupsGetOption(keyword
, num_options
,
1495 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1499 cupsFilePrintf(out
, "%s\n", line
);
1502 cupsFilePrintf(out
, "%s\n", line
);
1506 if (!wrote_ipp_supplies
&&
1507 (boolval
= cupsGetOption("cupsIPPSupplies", num_options
,
1510 cupsFilePrintf(out
, "*cupsIPPSupplies: %s\n",
1511 (!_cups_strcasecmp(boolval
, "true") ||
1512 !_cups_strcasecmp(boolval
, "yes") ||
1513 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1516 if (!wrote_snmp_supplies
&&
1517 (boolval
= cupsGetOption("cupsSNMPSupplies", num_options
,
1520 cupsFilePrintf(out
, "*cupsSNMPSupplies: %s\n",
1521 (!_cups_strcasecmp(boolval
, "true") ||
1522 !_cups_strcasecmp(boolval
, "yes") ||
1523 !_cups_strcasecmp(boolval
, "on")) ? "True" : "False");
1534 ippDelete(cupsDoFileRequest(http
, request
, "/admin/",
1535 ppdchanged
? tempfile
: file
));
1538 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1542 if (ppdfile
!= file
)
1549 * No PPD file - just set the options...
1552 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1556 cupsFreeOptions(num_options
, options
);
1559 * Check the response...
1562 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1564 _cupsLangPrintf(stderr
, _("%s: %s"), "lpadmin", cupsLastErrorString());
1574 * 'validate_name()' - Make sure the printer name only contains valid chars.
1577 static int /* O - 0 if name is no good, 1 if name is good */
1578 validate_name(const char *name
) /* I - Name to check */
1580 const char *ptr
; /* Pointer into name */
1584 * Scan the whole name...
1587 for (ptr
= name
; *ptr
; ptr
++)
1590 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '\\' || *ptr
== '?' || *ptr
== '\'' || *ptr
== '\"' || *ptr
== '#')
1594 * All the characters are good; validate the length, too...
1597 return ((ptr
- name
) < 128);