]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
503b54c9 MS |
2 | * "cupsaccept", "cupsdisable", "cupsenable", and "cupsreject" commands for |
3 | * CUPS. | |
ef416fc2 | 4 | * |
8e52928c MS |
5 | * Copyright © 2007-2018 by Apple Inc. |
6 | * Copyright © 1997-2006 by Easy Software Products. | |
ef416fc2 | 7 | * |
8e52928c MS |
8 | * Licensed under Apache License v2.0. See the file "LICENSE" for more |
9 | * information. | |
ef416fc2 | 10 | */ |
11 | ||
12 | /* | |
13 | * Include necessary headers... | |
14 | */ | |
15 | ||
71e16022 | 16 | #include <cups/cups-private.h> |
ef416fc2 | 17 | |
18 | ||
8e52928c MS |
19 | /* |
20 | * Local functions... | |
21 | */ | |
22 | ||
23 | static void usage(const char *command) _CUPS_NORETURN; | |
24 | ||
25 | ||
ef416fc2 | 26 | /* |
27 | * 'main()' - Parse options and accept/reject jobs or disable/enable printers. | |
28 | */ | |
29 | ||
30 | int /* O - Exit status */ | |
31 | main(int argc, /* I - Number of command-line arguments */ | |
32 | char *argv[]) /* I - Command-line arguments */ | |
33 | { | |
ef416fc2 | 34 | int i; /* Looping var */ |
35 | char *command, /* Command to do */ | |
bdbfacc7 | 36 | *opt, /* Option pointer */ |
ef416fc2 | 37 | uri[1024], /* Printer URI */ |
38 | *reason; /* Reason for reject/disable */ | |
39 | ipp_t *request; /* IPP request */ | |
ef416fc2 | 40 | ipp_op_t op; /* Operation */ |
ef416fc2 | 41 | int cancel; /* Cancel jobs? */ |
42 | ||
43 | ||
07725fee | 44 | _cupsSetLocale(argv); |
d09495fa | 45 | |
ef416fc2 | 46 | /* |
47 | * See what operation we're supposed to do... | |
48 | */ | |
49 | ||
50 | if ((command = strrchr(argv[0], '/')) != NULL) | |
51 | command ++; | |
52 | else | |
53 | command = argv[0]; | |
54 | ||
fa73b229 | 55 | cancel = 0; |
ef416fc2 | 56 | |
8e52928c | 57 | if (!strcmp(command, "cupsaccept")) |
ef416fc2 | 58 | op = CUPS_ACCEPT_JOBS; |
8e52928c | 59 | else if (!strcmp(command, "cupsreject")) |
ef416fc2 | 60 | op = CUPS_REJECT_JOBS; |
8e52928c | 61 | else if (!strcmp(command, "cupsdisable")) |
ef416fc2 | 62 | op = IPP_PAUSE_PRINTER; |
8e52928c | 63 | else if (!strcmp(command, "cupsenable")) |
ef416fc2 | 64 | op = IPP_RESUME_PRINTER; |
65 | else | |
66 | { | |
0837b7e8 | 67 | _cupsLangPrintf(stderr, _("%s: Don't know what to do."), command); |
ef416fc2 | 68 | return (1); |
69 | } | |
70 | ||
ef416fc2 | 71 | reason = NULL; |
72 | ||
73 | /* | |
74 | * Process command-line arguments... | |
75 | */ | |
76 | ||
77 | for (i = 1; i < argc; i ++) | |
bdbfacc7 | 78 | { |
8e52928c MS |
79 | if (!strcmp(argv[i], "--help")) |
80 | usage(command); | |
81 | else if (!strcmp(argv[i], "--hold")) | |
bdbfacc7 MS |
82 | op = IPP_HOLD_NEW_JOBS; |
83 | else if (!strcmp(argv[i], "--release")) | |
84 | op = IPP_RELEASE_HELD_NEW_JOBS; | |
85 | else if (argv[i][0] == '-') | |
61cf44e2 | 86 | { |
bdbfacc7 | 87 | for (opt = argv[i] + 1; *opt; opt ++) |
ef416fc2 | 88 | { |
bdbfacc7 MS |
89 | switch (*opt) |
90 | { | |
91 | case 'E' : /* Encrypt */ | |
87de8b54 | 92 | #ifdef HAVE_SSL |
bdbfacc7 | 93 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); |
87de8b54 | 94 | #else |
bdbfacc7 | 95 | _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."), command); |
87de8b54 | 96 | #endif /* HAVE_SSL */ |
bdbfacc7 MS |
97 | break; |
98 | ||
99 | case 'U' : /* Username */ | |
100 | if (opt[1] != '\0') | |
fa73b229 | 101 | { |
bdbfacc7 MS |
102 | cupsSetUser(opt + 1); |
103 | opt += strlen(opt) - 1; | |
fa73b229 | 104 | } |
bdbfacc7 MS |
105 | else |
106 | { | |
107 | i ++; | |
108 | if (i >= argc) | |
109 | { | |
110 | _cupsLangPrintf(stderr, _("%s: Error - expected username after \"-U\" option."), command); | |
8e52928c | 111 | usage(command); |
bdbfacc7 MS |
112 | } |
113 | ||
114 | cupsSetUser(argv[i]); | |
115 | } | |
116 | break; | |
57b7b66b | 117 | |
bdbfacc7 MS |
118 | case 'c' : /* Cancel jobs */ |
119 | cancel = 1; | |
120 | break; | |
121 | ||
122 | case 'h' : /* Connect to host */ | |
123 | if (opt[1] != '\0') | |
124 | { | |
125 | cupsSetServer(opt + 1); | |
126 | opt += strlen(opt) - 1; | |
127 | } | |
128 | else | |
ef416fc2 | 129 | { |
bdbfacc7 MS |
130 | i ++; |
131 | if (i >= argc) | |
132 | { | |
133 | _cupsLangPrintf(stderr, _("%s: Error - expected hostname after \"-h\" option."), command); | |
8e52928c | 134 | usage(command); |
bdbfacc7 MS |
135 | } |
136 | ||
137 | cupsSetServer(argv[i]); | |
ef416fc2 | 138 | } |
bdbfacc7 | 139 | break; |
ef416fc2 | 140 | |
bdbfacc7 MS |
141 | case 'r' : /* Reason for cancellation */ |
142 | if (opt[1] != '\0') | |
143 | { | |
144 | reason = opt + 1; | |
145 | opt += strlen(opt) - 1; | |
146 | } | |
147 | else | |
ef416fc2 | 148 | { |
bdbfacc7 MS |
149 | i ++; |
150 | if (i >= argc) | |
151 | { | |
152 | _cupsLangPrintf(stderr, _("%s: Error - expected reason text after \"-r\" option."), command); | |
8e52928c | 153 | usage(command); |
bdbfacc7 MS |
154 | } |
155 | ||
156 | reason = argv[i]; | |
ef416fc2 | 157 | } |
bdbfacc7 | 158 | break; |
ef416fc2 | 159 | |
bdbfacc7 MS |
160 | default : |
161 | _cupsLangPrintf(stderr, _("%s: Error - unknown option \"%c\"."), command, *opt); | |
8e52928c | 162 | usage(command); |
bdbfacc7 | 163 | } |
ef416fc2 | 164 | } |
61cf44e2 | 165 | } |
ef416fc2 | 166 | else |
167 | { | |
168 | /* | |
169 | * Accept/disable/enable/reject a destination... | |
170 | */ | |
171 | ||
fa73b229 | 172 | request = ippNewRequest(op); |
ef416fc2 | 173 | |
a4d04587 | 174 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
175 | "localhost", 0, "/printers/%s", argv[i]); | |
ef416fc2 | 176 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, |
177 | "printer-uri", NULL, uri); | |
178 | ||
fa73b229 | 179 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, |
180 | "requesting-user-name", NULL, cupsUser()); | |
181 | ||
ef416fc2 | 182 | if (reason != NULL) |
183 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, | |
184 | "printer-state-message", NULL, reason); | |
185 | ||
186 | /* | |
187 | * Do the request and get back a response... | |
188 | */ | |
189 | ||
61cf44e2 MS |
190 | ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/admin/")); |
191 | ||
192 | if (cupsLastError() > IPP_OK_CONFLICT) | |
ef416fc2 | 193 | { |
61cf44e2 | 194 | _cupsLangPrintf(stderr, |
0837b7e8 | 195 | _("%s: Operation failed: %s"), |
61cf44e2 | 196 | command, ippErrorString(cupsLastError())); |
ef416fc2 | 197 | return (1); |
198 | } | |
199 | ||
200 | /* | |
201 | * Cancel all jobs if requested... | |
202 | */ | |
203 | ||
204 | if (cancel) | |
205 | { | |
206 | /* | |
207 | * Build an IPP_PURGE_JOBS request, which requires the following | |
208 | * attributes: | |
209 | * | |
210 | * attributes-charset | |
211 | * attributes-natural-language | |
212 | * printer-uri | |
213 | */ | |
214 | ||
fa73b229 | 215 | request = ippNewRequest(IPP_PURGE_JOBS); |
ef416fc2 | 216 | |
217 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
218 | "printer-uri", NULL, uri); | |
219 | ||
61cf44e2 | 220 | ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/admin/")); |
ef416fc2 | 221 | |
61cf44e2 | 222 | if (cupsLastError() > IPP_OK_CONFLICT) |
ef416fc2 | 223 | { |
0837b7e8 | 224 | _cupsLangPrintf(stderr, "%s: %s", command, cupsLastErrorString()); |
ef416fc2 | 225 | return (1); |
226 | } | |
227 | } | |
228 | } | |
bdbfacc7 | 229 | } |
ef416fc2 | 230 | |
ef416fc2 | 231 | return (0); |
232 | } | |
8e52928c MS |
233 | |
234 | ||
235 | /* | |
236 | * 'usage()' - Show program usage and exit. | |
237 | */ | |
238 | ||
239 | static void | |
240 | usage(const char *command) /* I - Command name */ | |
241 | { | |
242 | _cupsLangPrintf(stdout, _("Usage: %s [options] destination(s)"), command); | |
243 | _cupsLangPuts(stdout, _("Options:")); | |
244 | _cupsLangPuts(stdout, _("-E Encrypt the connection to the server")); | |
245 | _cupsLangPuts(stdout, _("-h server[:port] Connect to the named server and port")); | |
246 | _cupsLangPuts(stdout, _("-r reason Specify a reason message that others can see")); | |
247 | _cupsLangPuts(stdout, _("-U username Specify the username to use for authentication")); | |
248 | if (!strcmp(command, "cupsdisable")) | |
249 | _cupsLangPuts(stdout, _("--hold Hold new jobs")); | |
250 | if (!strcmp(command, "cupsenable")) | |
251 | _cupsLangPuts(stdout, _("--release Release previously held jobs")); | |
252 | ||
253 | exit(1); | |
254 | } |