]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2 | * "$Id: printers.c 4921 2006-01-12 21:26:26Z mike $" | |
3 | * | |
4 | * Printer status CGI for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2006 by Easy Software Products. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * main() - Main entry for CGI. | |
27 | */ | |
28 | ||
29 | /* | |
30 | * Include necessary headers... | |
31 | */ | |
32 | ||
33 | #include "cgi-private.h" | |
34 | ||
35 | ||
36 | /* | |
37 | * 'main()' - Main entry for CGI. | |
38 | */ | |
39 | ||
40 | int /* O - Exit status */ | |
41 | main(int argc, /* I - Number of command-line arguments */ | |
42 | char *argv[]) /* I - Command-line arguments */ | |
43 | { | |
44 | cups_lang_t *language; /* Language information */ | |
45 | char *printer; /* Printer name */ | |
46 | http_t *http; /* Connection to the server */ | |
47 | ipp_t *request, /* IPP request */ | |
48 | *response; /* IPP response */ | |
49 | ipp_attribute_t *attr; /* IPP attribute */ | |
50 | ipp_status_t status; /* Operation status... */ | |
51 | char uri[HTTP_MAX_URI]; /* Printer URI */ | |
52 | const char *which_jobs; /* Which jobs to show */ | |
53 | const char *op; /* Operation to perform, if any */ | |
54 | char refresh[1024]; /* Refresh URL */ | |
55 | static const char *def_attrs[] = /* Attributes for default printer */ | |
56 | { | |
57 | "printer-name", | |
58 | "printer-uri-supported" | |
59 | }; | |
60 | ||
61 | ||
62 | /* | |
63 | * Get any form variables... | |
64 | */ | |
65 | ||
66 | cgiInitialize(); | |
67 | op = cgiGetVariable("OP"); | |
68 | ||
69 | /* | |
70 | * Get the request language... | |
71 | */ | |
72 | ||
73 | language = cupsLangDefault(); | |
74 | ||
75 | /* | |
76 | * Set the web interface section... | |
77 | */ | |
78 | ||
79 | cgiSetVariable("SECTION", "printers"); | |
80 | ||
81 | /* | |
82 | * Connect to the HTTP server... | |
83 | */ | |
84 | ||
85 | http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); | |
86 | ||
87 | /* | |
88 | * Tell the client to expect UTF-8 encoded HTML... | |
89 | */ | |
90 | ||
91 | puts("Content-Type: text/html;charset=utf-8\n"); | |
92 | ||
93 | /* | |
94 | * See if we need to show a list of printers or the status of a | |
95 | * single printer... | |
96 | */ | |
97 | ||
98 | cgiSetServerVersion(); | |
99 | ||
100 | printer = argv[0]; | |
101 | if (strcmp(printer, "/") == 0 || strstr(printer, "printers.cgi") != NULL) | |
102 | { | |
103 | printer = NULL; | |
104 | cgiSetVariable("TITLE", _cupsLangString(language, _("Printer"))); | |
105 | } | |
106 | else | |
107 | cgiSetVariable("TITLE", printer); | |
108 | ||
109 | if (op == NULL || strcasecmp(op, "print-test-page") != 0) | |
110 | { | |
111 | /* | |
112 | * Get the default destination... | |
113 | */ | |
114 | ||
115 | request = ippNew(); | |
116 | request->request.op.operation_id = CUPS_GET_DEFAULT; | |
117 | request->request.op.request_id = 1; | |
118 | ||
119 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
120 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
121 | ||
122 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
123 | "attributes-natural-language", NULL, language->language); | |
124 | ||
125 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
126 | "requested-attributes", | |
127 | sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs); | |
128 | ||
129 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
130 | { | |
131 | if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL) | |
132 | cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text); | |
133 | ||
134 | if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL) | |
135 | { | |
136 | char url[HTTP_MAX_URI]; /* New URL */ | |
137 | ||
138 | ||
139 | cgiSetVariable("DEFAULT_URI", | |
140 | cgiRewriteURL(attr->values[0].string.text, | |
141 | url, sizeof(url), NULL)); | |
142 | } | |
143 | ||
144 | ippDelete(response); | |
145 | } | |
146 | ||
147 | /* | |
148 | * Get the printer info... | |
149 | */ | |
150 | ||
151 | request = ippNew(); | |
152 | ||
153 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
154 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
155 | ||
156 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
157 | "attributes-natural-language", NULL, language->language); | |
158 | ||
159 | if (printer == NULL) | |
160 | { | |
161 | /* | |
162 | * Build a CUPS_GET_PRINTERS request, which requires the following | |
163 | * attributes: | |
164 | * | |
165 | * attributes-charset | |
166 | * attributes-natural-language | |
167 | */ | |
168 | ||
169 | request->request.op.operation_id = CUPS_GET_PRINTERS; | |
170 | request->request.op.request_id = 1; | |
171 | ||
172 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, | |
173 | "printer-type", 0); | |
174 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, | |
175 | "printer-type-mask", CUPS_PRINTER_CLASS); | |
176 | ||
177 | if (getenv("REMOTE_USER") != NULL) | |
178 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
179 | NULL, getenv("REMOTE_USER")); | |
180 | } | |
181 | else | |
182 | { | |
183 | /* | |
184 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following | |
185 | * attributes: | |
186 | * | |
187 | * attributes-charset | |
188 | * attributes-natural-language | |
189 | * printer-uri | |
190 | */ | |
191 | ||
192 | request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES; | |
193 | request->request.op.request_id = 1; | |
194 | ||
195 | httpAssembleURIf(uri, sizeof(uri), "ipp", NULL, "localhost", 0, | |
196 | "/printers/%s", printer); | |
197 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
198 | uri); | |
199 | } | |
200 | ||
201 | cgiGetAttributes(request, "printers.tmpl"); | |
202 | ||
203 | /* | |
204 | * Do the request and get back a response... | |
205 | */ | |
206 | ||
207 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
208 | { | |
209 | /* | |
210 | * Got the result; set the CGI variables and check the status of a | |
211 | * single-queue request... | |
212 | */ | |
213 | ||
214 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); | |
215 | ||
216 | if (printer && (attr = ippFindAttribute(response, "printer-state", | |
217 | IPP_TAG_ENUM)) != NULL && | |
218 | attr->values[0].integer == IPP_PRINTER_PROCESSING) | |
219 | { | |
220 | /* | |
221 | * Printer is processing - automatically refresh the page until we | |
222 | * are done printing... | |
223 | */ | |
224 | ||
225 | cgiFormEncode(uri, printer, sizeof(uri)); | |
226 | snprintf(refresh, sizeof(refresh), "10;/printers/%s", uri); | |
227 | cgiSetVariable("refresh_page", refresh); | |
228 | } | |
229 | ||
230 | /* | |
231 | * Delete the response... | |
232 | */ | |
233 | ||
234 | ippDelete(response); | |
235 | } | |
236 | else if (printer) | |
237 | fprintf(stderr, "ERROR: Get-Printer-Attributes request failed - %s (%x)\n", | |
238 | ippErrorString(cupsLastError()), cupsLastError()); | |
239 | else | |
240 | fprintf(stderr, "ERROR: CUPS-Get-Printers request failed - %s (%x)\n", | |
241 | ippErrorString(cupsLastError()), cupsLastError()); | |
242 | ||
243 | /* | |
244 | * Show the standard header... | |
245 | */ | |
246 | ||
247 | cgiCopyTemplateLang("header.tmpl"); | |
248 | ||
249 | /* | |
250 | * Write the report... | |
251 | */ | |
252 | ||
253 | cgiCopyTemplateLang("printers.tmpl"); | |
254 | ||
255 | /* | |
256 | * Get jobs for the specified printer if a printer has been chosen... | |
257 | */ | |
258 | ||
259 | if (printer != NULL) | |
260 | { | |
261 | /* | |
262 | * Build an IPP_GET_JOBS request, which requires the following | |
263 | * attributes: | |
264 | * | |
265 | * attributes-charset | |
266 | * attributes-natural-language | |
267 | * printer-uri | |
268 | */ | |
269 | ||
270 | request = ippNew(); | |
271 | ||
272 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
273 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
274 | ||
275 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
276 | "attributes-natural-language", NULL, language->language); | |
277 | ||
278 | request->request.op.operation_id = IPP_GET_JOBS; | |
279 | request->request.op.request_id = 1; | |
280 | ||
281 | httpAssembleURIf(uri, sizeof(uri), "ipp", NULL, "localhost", 0, | |
282 | "/printers/%s", printer); | |
283 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
284 | uri); | |
285 | ||
286 | if ((which_jobs = cgiGetVariable("which_jobs")) != NULL) | |
287 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs", | |
288 | NULL, which_jobs); | |
289 | ||
290 | if (getenv("REMOTE_USER") != NULL) | |
291 | { | |
292 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
293 | NULL, getenv("REMOTE_USER")); | |
294 | ||
295 | if (strcmp(getenv("REMOTE_USER"), "root")) | |
296 | ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); | |
297 | } | |
298 | else | |
299 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
300 | NULL, "unknown"); | |
301 | ||
302 | cgiGetAttributes(request, "jobs.tmpl"); | |
303 | ||
304 | /* | |
305 | * Do the request and get back a response... | |
306 | */ | |
307 | ||
308 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
309 | { | |
310 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); | |
311 | ippDelete(response); | |
312 | ||
313 | cgiCopyTemplateLang("jobs.tmpl"); | |
314 | } | |
315 | else | |
316 | fprintf(stderr, "ERROR: Get-Jobs request failed - %s (%x)\n", | |
317 | ippErrorString(cupsLastError()), cupsLastError()); | |
318 | } | |
319 | } | |
320 | else | |
321 | { | |
322 | /* | |
323 | * Print a test page... | |
324 | */ | |
325 | ||
326 | char filename[1024]; /* Test page filename */ | |
327 | const char *datadir; /* CUPS_DATADIR env var */ | |
328 | ||
329 | ||
330 | cgiFormEncode(uri, printer, sizeof(uri)); | |
331 | snprintf(refresh, sizeof(refresh), "2;/printers/%s", uri); | |
332 | cgiSetVariable("refresh_page", refresh); | |
333 | ||
334 | if ((datadir = getenv("CUPS_DATADIR")) == NULL) | |
335 | datadir = CUPS_DATADIR; | |
336 | ||
337 | snprintf(filename, sizeof(filename), "%s/data/testprint.ps", datadir); | |
338 | httpAssembleURIf(uri, sizeof(uri), "ipp", NULL, "localhost", 0, | |
339 | "/printers/%s", printer); | |
340 | ||
341 | /* | |
342 | * Build an IPP_PRINT_JOB request, which requires the following | |
343 | * attributes: | |
344 | * | |
345 | * attributes-charset | |
346 | * attributes-natural-language | |
347 | * printer-uri | |
348 | * requesting-user-name | |
349 | * document-format | |
350 | */ | |
351 | ||
352 | request = ippNew(); | |
353 | ||
354 | request->request.op.operation_id = IPP_PRINT_JOB; | |
355 | request->request.op.request_id = 1; | |
356 | ||
357 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
358 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
359 | ||
360 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
361 | "attributes-natural-language", NULL, language->language); | |
362 | ||
363 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
364 | NULL, uri); | |
365 | ||
366 | if (getenv("REMOTE_USER") != NULL) | |
367 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
368 | NULL, getenv("REMOTE_USER")); | |
369 | else | |
370 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
371 | NULL, "root"); | |
372 | ||
373 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", | |
374 | NULL, "Test Page"); | |
375 | ||
376 | ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format", | |
377 | NULL, "application/postscript"); | |
378 | ||
379 | /* | |
380 | * Do the request and get back a response... | |
381 | */ | |
382 | ||
383 | if ((response = cupsDoFileRequest(http, request, uri + 15, | |
384 | filename)) != NULL) | |
385 | { | |
386 | status = response->request.status.status_code; | |
387 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); | |
388 | ||
389 | ippDelete(response); | |
390 | } | |
391 | else | |
392 | status = cupsLastError(); | |
393 | ||
394 | cgiSetVariable("PRINTER_NAME", printer); | |
395 | ||
396 | /* | |
397 | * Show the standard header... | |
398 | */ | |
399 | ||
400 | cgiCopyTemplateLang("header.tmpl"); | |
401 | ||
402 | /* | |
403 | * Show the result... | |
404 | */ | |
405 | ||
406 | if (status > IPP_OK_CONFLICT) | |
407 | { | |
408 | cgiSetVariable("ERROR", ippErrorString(status)); | |
409 | cgiCopyTemplateLang("error.tmpl"); | |
410 | } | |
411 | else | |
412 | cgiCopyTemplateLang("test-page.tmpl"); | |
413 | } | |
414 | ||
415 | cgiCopyTemplateLang("trailer.tmpl"); | |
416 | ||
417 | /* | |
418 | * Close the HTTP server connection... | |
419 | */ | |
420 | ||
421 | httpClose(http); | |
422 | cupsLangFree(language); | |
423 | ||
424 | /* | |
425 | * Return with no errors... | |
426 | */ | |
427 | ||
428 | return (0); | |
429 | } | |
430 | ||
431 | ||
432 | /* | |
433 | * End of "$Id: printers.c 4921 2006-01-12 21:26:26Z mike $". | |
434 | */ |