]>
Commit | Line | Data |
---|---|---|
b310a06a | 1 | /* |
17438bf4 | 2 | * "$Id: accept.c,v 1.16 2002/05/16 13:45:04 mike Exp $" |
b310a06a | 3 | * |
4 | * "accept", "disable", "enable", and "reject" commands for the Common | |
5 | * UNIX Printing System (CUPS). | |
6 | * | |
efb2f309 | 7 | * Copyright 1997-2002 by Easy Software Products. |
b310a06a | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
10 | * property of Easy Software Products and are protected by Federal | |
11 | * copyright law. Distribution and use rights are outlined in the file | |
12 | * "LICENSE.txt" which should have been included with this file. If this | |
13 | * file is missing or damaged please contact Easy Software Products | |
14 | * at: | |
15 | * | |
16 | * Attn: CUPS Licensing Information | |
17 | * Easy Software Products | |
8784b6a6 | 18 | * 44141 Airport View Drive, Suite 204 |
b310a06a | 19 | * Hollywood, Maryland 20636-3111 USA |
20 | * | |
21 | * Voice: (301) 373-9603 | |
22 | * EMail: cups-info@cups.org | |
23 | * WWW: http://www.cups.org | |
24 | * | |
25 | * Contents: | |
26 | * | |
27 | * main() - Parse options and accept/reject jobs or disable/enable printers. | |
28 | */ | |
29 | ||
30 | /* | |
31 | * Include necessary headers... | |
32 | */ | |
33 | ||
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include <ctype.h> | |
37 | ||
1c9e0181 | 38 | #include <config.h> |
b310a06a | 39 | #include <cups/cups.h> |
f9a8da2d | 40 | #include <cups/string.h> |
b310a06a | 41 | #include <cups/language.h> |
42 | ||
43 | ||
44 | /* | |
45 | * 'main()' - Parse options and accept/reject jobs or disable/enable printers. | |
46 | */ | |
47 | ||
48 | int /* O - Exit status */ | |
49 | main(int argc, /* I - Number of command-line arguments */ | |
50 | char *argv[]) /* I - Command-line arguments */ | |
51 | { | |
52 | http_t *http; /* HTTP connection to server */ | |
53 | int i; /* Looping var */ | |
54 | char *command, /* Command to do */ | |
55 | hostname[HTTP_MAX_URI], | |
56 | /* Name of host */ | |
57 | printer[HTTP_MAX_URI], | |
58 | /* Name of printer or class */ | |
59 | uri[1024], /* Printer URI */ | |
60 | *reason; /* Reason for reject/disable */ | |
b26652ce | 61 | const char *server; /* Server name */ |
b310a06a | 62 | ipp_t *request; /* IPP request */ |
63 | ipp_t *response; /* IPP response */ | |
64 | ipp_op_t op; /* Operation */ | |
65 | cups_lang_t *language; /* Language */ | |
2a2fbe49 | 66 | int cancel; /* Cancel jobs? */ |
1c9e0181 | 67 | http_encryption_t encryption; /* Encryption? */ |
b310a06a | 68 | |
69 | ||
70 | /* | |
71 | * See what operation we're supposed to do... | |
72 | */ | |
73 | ||
74 | if ((command = strrchr(argv[0], '/')) != NULL) | |
75 | command ++; | |
76 | else | |
77 | command = argv[0]; | |
78 | ||
2a2fbe49 | 79 | cancel = 0; |
80 | ||
b310a06a | 81 | if (strcmp(command, "accept") == 0) |
82 | op = CUPS_ACCEPT_JOBS; | |
83 | else if (strcmp(command, "reject") == 0) | |
84 | op = CUPS_REJECT_JOBS; | |
85 | else if (strcmp(command, "disable") == 0) | |
86 | op = IPP_PAUSE_PRINTER; | |
87 | else if (strcmp(command, "enable") == 0) | |
88 | op = IPP_RESUME_PRINTER; | |
89 | else | |
90 | { | |
91 | fprintf(stderr, "%s: Don't know what to do!\n", command); | |
92 | return (1); | |
93 | } | |
94 | ||
b26652ce | 95 | server = cupsServer(); |
1c9e0181 | 96 | http = NULL; |
97 | reason = NULL; | |
98 | encryption = cupsEncryption(); | |
b310a06a | 99 | |
100 | /* | |
101 | * Process command-line arguments... | |
102 | */ | |
103 | ||
104 | for (i = 1; i < argc; i ++) | |
105 | if (argv[i][0] == '-') | |
106 | switch (argv[i][1]) | |
107 | { | |
1c9e0181 | 108 | case 'E' : /* Encrypt */ |
109 | #ifdef HAVE_LIBSSL | |
110 | encryption = HTTP_ENCRYPT_REQUIRED; | |
111 | ||
112 | if (http) | |
113 | httpEncryption(http, encryption); | |
114 | #else | |
115 | fprintf(stderr, "%s: Sorry, no encryption support compiled in!\n", | |
116 | argv[0]); | |
117 | #endif /* HAVE_LIBSSL */ | |
118 | break; | |
119 | ||
2a2fbe49 | 120 | case 'c' : /* Cancel jobs */ |
121 | cancel = 1; | |
122 | break; | |
123 | ||
808dd474 | 124 | case 'h' : /* Connect to host */ |
125 | if (http != NULL) | |
126 | httpClose(http); | |
127 | ||
128 | if (argv[i][2] != '\0') | |
b26652ce | 129 | server = argv[i] + 2; |
808dd474 | 130 | else |
131 | { | |
132 | i ++; | |
db8c0a8c | 133 | if (i >= argc) |
134 | { | |
135 | fprintf(stderr, "%s: Expected server name after -h!\n", | |
136 | command); | |
137 | return (1); | |
138 | } | |
139 | ||
b26652ce | 140 | server = argv[i]; |
808dd474 | 141 | } |
142 | ||
b26652ce | 143 | http = httpConnectEncrypt(server, ippPort(), encryption); |
144 | ||
808dd474 | 145 | if (http == NULL) |
146 | { | |
147 | fputs(argv[0], stderr); | |
148 | perror(": Unable to connect to server"); | |
149 | return (1); | |
150 | } | |
151 | break; | |
152 | ||
b310a06a | 153 | case 'r' : /* Reason for cancellation */ |
154 | if (argv[i][2] != '\0') | |
155 | reason = argv[i] + 2; | |
156 | else | |
157 | { | |
158 | i ++; | |
159 | if (i >= argc) | |
160 | { | |
161 | fprintf(stderr, "%s: Expected reason text after -r!\n", command); | |
162 | return (1); | |
163 | } | |
164 | ||
165 | reason = argv[i]; | |
166 | } | |
167 | break; | |
168 | ||
169 | default : | |
170 | fprintf(stderr, "%s: Unknown option \'%c\'!\n", command, | |
171 | argv[i][1]); | |
172 | return (1); | |
173 | } | |
174 | else | |
175 | { | |
176 | /* | |
177 | * Accept/disable/enable/reject a destination... | |
178 | */ | |
179 | ||
970017a4 | 180 | if (sscanf(argv[i], "%1023[^@]@%1023s", printer, hostname) == 1) |
17438bf4 | 181 | strlcpy(hostname, server, sizeof(hostname)); |
b310a06a | 182 | |
183 | if (http != NULL && strcasecmp(http->hostname, hostname) != 0) | |
184 | { | |
185 | httpClose(http); | |
186 | http = NULL; | |
187 | } | |
188 | ||
189 | if (http == NULL) | |
a1793153 | 190 | http = httpConnectEncrypt(hostname, ippPort(), encryption); |
b310a06a | 191 | |
192 | if (http == NULL) | |
193 | { | |
96ecd6f0 | 194 | fputs(argv[0], stderr); |
195 | perror(": Unable to connect to server"); | |
b310a06a | 196 | return (1); |
197 | } | |
198 | ||
199 | /* | |
200 | * Build an IPP request, which requires the following | |
201 | * attributes: | |
202 | * | |
203 | * attributes-charset | |
204 | * attributes-natural-language | |
205 | * printer-uri | |
206 | * printer-state-message [optional] | |
207 | */ | |
208 | ||
209 | request = ippNew(); | |
210 | ||
211 | request->request.op.operation_id = op; | |
212 | request->request.op.request_id = 1; | |
213 | ||
214 | language = cupsLangDefault(); | |
215 | ||
216 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
217 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
218 | ||
219 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
220 | "attributes-natural-language", NULL, language->language); | |
221 | ||
970017a4 | 222 | snprintf(uri, sizeof(uri), "ipp://%s:%d/printers/%s", hostname, ippPort(), printer); |
b310a06a | 223 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, |
224 | "printer-uri", NULL, uri); | |
225 | ||
226 | if (reason != NULL) | |
227 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, | |
228 | "printer-state-message", NULL, reason); | |
229 | ||
230 | /* | |
231 | * Do the request and get back a response... | |
232 | */ | |
233 | ||
234 | if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) | |
96ecd6f0 | 235 | { |
236 | if (response->request.status.status_code > IPP_OK_CONFLICT) | |
237 | { | |
238 | fprintf(stderr, "%s: Operation failed: %s\n", command, | |
239 | ippErrorString(cupsLastError())); | |
240 | return (1); | |
241 | } | |
242 | ||
b310a06a | 243 | ippDelete(response); |
96ecd6f0 | 244 | } |
b310a06a | 245 | else |
246 | { | |
96ecd6f0 | 247 | fprintf(stderr, "%s: Operation failed: %s\n", command, |
248 | ippErrorString(cupsLastError())); | |
b310a06a | 249 | return (1); |
250 | } | |
2a2fbe49 | 251 | |
252 | /* | |
253 | * Cancel all jobs if requested... | |
254 | */ | |
255 | ||
256 | if (cancel) | |
257 | { | |
258 | /* | |
259 | * Build an IPP_PURGE_JOBS request, which requires the following | |
260 | * attributes: | |
261 | * | |
262 | * attributes-charset | |
263 | * attributes-natural-language | |
264 | * printer-uri | |
265 | */ | |
266 | ||
267 | request = ippNew(); | |
268 | ||
269 | request->request.op.operation_id = IPP_PURGE_JOBS; | |
270 | request->request.op.request_id = 1; | |
271 | ||
272 | language = cupsLangDefault(); | |
273 | ||
274 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
275 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
276 | ||
277 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
278 | "attributes-natural-language", NULL, language->language); | |
279 | ||
280 | snprintf(uri, sizeof(uri), "ipp://%s:%d/printers/%s", hostname, ippPort(), printer); | |
281 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
282 | "printer-uri", NULL, uri); | |
283 | ||
284 | if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) | |
96ecd6f0 | 285 | { |
286 | if (response->request.status.status_code > IPP_OK_CONFLICT) | |
287 | { | |
288 | fprintf(stderr, "%s: Operation failed: %s\n", command, | |
289 | ippErrorString(cupsLastError())); | |
290 | return (1); | |
291 | } | |
292 | ||
2a2fbe49 | 293 | ippDelete(response); |
96ecd6f0 | 294 | } |
295 | else | |
296 | { | |
297 | fprintf(stderr, "%s: Operation failed: %s\n", command, | |
298 | ippErrorString(cupsLastError())); | |
299 | return (1); | |
300 | } | |
2a2fbe49 | 301 | } |
b310a06a | 302 | } |
303 | ||
304 | if (http != NULL) | |
305 | httpClose(http); | |
306 | ||
307 | return (0); | |
308 | } | |
309 | ||
310 | ||
311 | /* | |
17438bf4 | 312 | * End of "$Id: accept.c,v 1.16 2002/05/16 13:45:04 mike Exp $". |
b310a06a | 313 | */ |