]>
Commit | Line | Data |
---|---|---|
c4adf588 | 1 | /* |
b5cb0608 | 2 | * "$Id: printers.c,v 1.21.2.1 2001/05/13 18:38:01 mike Exp $" |
c4adf588 | 3 | * |
4 | * Printer status CGI for the Common UNIX Printing System (CUPS). | |
5 | * | |
d2935a0f | 6 | * Copyright 1997-2001 by Easy Software Products. |
c4adf588 | 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-3111 USA | |
19 | * | |
20 | * Voice: (301) 373-9603 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
13c1a6d9 | 26 | * main() - Main entry for CGI. |
c4adf588 | 27 | */ |
28 | ||
29 | /* | |
30 | * Include necessary headers... | |
31 | */ | |
32 | ||
13c1a6d9 | 33 | #include "ipp-var.h" |
c4adf588 | 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 */ | |
13c1a6d9 | 47 | ipp_t *request, /* IPP request */ |
48 | *response; /* IPP response */ | |
96aa5c42 | 49 | ipp_attribute_t *attr; /* IPP attribute */ |
3d9e2586 | 50 | ipp_status_t status; /* Operation status... */ |
13c1a6d9 | 51 | char uri[HTTP_MAX_URI]; |
52 | /* Printer URI */ | |
f63a2256 | 53 | const char *which_jobs; /* Which jobs to show */ |
3d9e2586 | 54 | const char *op; /* Operation to perform, if any */ |
f63a2256 | 55 | |
13c1a6d9 | 56 | |
f63a2256 | 57 | /* |
58 | * Get any form variables... | |
59 | */ | |
c4adf588 | 60 | |
f63a2256 | 61 | cgiInitialize(); |
3d9e2586 | 62 | op = cgiGetVariable("OP"); |
c4adf588 | 63 | |
64 | /* | |
65 | * Get the request language... | |
66 | */ | |
67 | ||
da376774 | 68 | language = cupsLangDefault(); |
c4adf588 | 69 | |
70 | /* | |
71 | * Connect to the HTTP server... | |
72 | */ | |
73 | ||
b5cb0608 | 74 | http = httpConnectEncrypt("localhost", ippPort(), cupsEncryption()); |
c4adf588 | 75 | |
76 | /* | |
77 | * Tell the client to expect HTML... | |
78 | */ | |
79 | ||
80 | printf("Content-Type: text/html;charset=%s\n\n", cupsLangEncoding(language)); | |
81 | ||
82 | /* | |
83 | * See if we need to show a list of printers or the status of a | |
84 | * single printer... | |
85 | */ | |
86 | ||
3d9e2586 | 87 | ippSetServerVersion(); |
88 | ||
c4adf588 | 89 | printer = argv[0]; |
90 | if (strcmp(printer, "/") == 0 || strcmp(printer, "printers.cgi") == 0) | |
13c1a6d9 | 91 | { |
c4adf588 | 92 | printer = NULL; |
3d9e2586 | 93 | cgiSetVariable("TITLE", cupsLangString(language, CUPS_MSG_PRINTER)); |
13c1a6d9 | 94 | } |
c4adf588 | 95 | else |
13c1a6d9 | 96 | cgiSetVariable("TITLE", printer); |
c4adf588 | 97 | |
3d9e2586 | 98 | cgiCopyTemplateLang(stdout, TEMPLATES, "header.tmpl", getenv("LANG")); |
c4adf588 | 99 | |
3d9e2586 | 100 | if (op == NULL || strcasecmp(op, "print-test-page") != 0) |
101 | { | |
96aa5c42 | 102 | /* |
103 | * Get the default destination... | |
104 | */ | |
105 | ||
106 | request = ippNew(); | |
107 | request->request.op.operation_id = CUPS_GET_DEFAULT; | |
108 | request->request.op.request_id = 1; | |
109 | ||
110 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
111 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
112 | ||
113 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
114 | "attributes-natural-language", NULL, language->language); | |
115 | ||
116 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
117 | { | |
118 | if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL) | |
119 | cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text); | |
120 | ||
121 | if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL) | |
122 | { | |
123 | char method[HTTP_MAX_URI], | |
124 | username[HTTP_MAX_URI], | |
125 | hostname[HTTP_MAX_URI], | |
126 | resource[HTTP_MAX_URI], | |
127 | uri[HTTP_MAX_URI]; | |
128 | int port; /* URI data */ | |
aa66d75f | 129 | const char *server; /* Name of server */ |
96aa5c42 | 130 | |
131 | ||
132 | /* | |
133 | * Map localhost access to localhost... | |
134 | */ | |
135 | ||
aa66d75f | 136 | server = getenv("SERVER_NAME"); |
137 | ||
96aa5c42 | 138 | httpSeparate(attr->values[0].string.text, method, username, |
139 | hostname, &port, resource); | |
140 | ||
141 | if (strcasecmp(hostname, server) == 0 && | |
142 | (strcmp(getenv("REMOTE_HOST"), "127.0.0.1") == 0 || | |
143 | strcmp(getenv("REMOTE_HOST"), "localhost") == 0 || | |
144 | strcmp(getenv("REMOTE_HOST"), server) == 0)) | |
145 | strcpy(hostname, "localhost"); | |
146 | ||
147 | /* | |
148 | * Rewrite URI with HTTP address... | |
149 | */ | |
150 | ||
151 | snprintf(uri, sizeof(uri), "http://%s:%d%s", hostname, port, | |
152 | resource); | |
153 | ||
154 | cgiSetVariable("DEFAULT_URI", uri); | |
155 | } | |
156 | ||
157 | ippDelete(response); | |
158 | } | |
159 | ||
3d9e2586 | 160 | /* |
161 | * Get the printer info... | |
162 | */ | |
163 | ||
164 | request = ippNew(); | |
c4adf588 | 165 | |
3d9e2586 | 166 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
167 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
c4adf588 | 168 | |
3d9e2586 | 169 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, |
170 | "attributes-natural-language", NULL, language->language); | |
171 | ||
172 | if (printer == NULL) | |
173 | { | |
174 | /* | |
175 | * Build a CUPS_GET_PRINTERS request, which requires the following | |
176 | * attributes: | |
177 | * | |
178 | * attributes-charset | |
179 | * attributes-natural-language | |
180 | */ | |
181 | ||
182 | request->request.op.operation_id = CUPS_GET_PRINTERS; | |
183 | request->request.op.request_id = 1; | |
184 | } | |
185 | else | |
186 | { | |
187 | /* | |
188 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following | |
189 | * attributes: | |
190 | * | |
191 | * attributes-charset | |
192 | * attributes-natural-language | |
193 | * printer-uri | |
194 | */ | |
195 | ||
196 | request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES; | |
197 | request->request.op.request_id = 1; | |
198 | ||
199 | snprintf(uri, sizeof(uri), "ipp://%s/printers/%s", getenv("SERVER_NAME"), | |
200 | printer); | |
201 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
202 | uri); | |
203 | } | |
c4adf588 | 204 | |
c4adf588 | 205 | /* |
3d9e2586 | 206 | * Do the request and get back a response... |
c4adf588 | 207 | */ |
208 | ||
3d9e2586 | 209 | if ((response = cupsDoRequest(http, request, "/")) != NULL) |
210 | { | |
211 | ippSetCGIVars(response, NULL, NULL); | |
212 | ippDelete(response); | |
213 | } | |
214 | ||
c4adf588 | 215 | /* |
3d9e2586 | 216 | * Write the report... |
c4adf588 | 217 | */ |
218 | ||
3d9e2586 | 219 | cgiCopyTemplateLang(stdout, TEMPLATES, "printers.tmpl", getenv("LANG")); |
580af8cf | 220 | |
3d9e2586 | 221 | /* |
222 | * Get jobs for the specified printer if a printer has been chosen... | |
223 | */ | |
580af8cf | 224 | |
3d9e2586 | 225 | if (printer != NULL) |
226 | { | |
227 | /* | |
228 | * Build an IPP_GET_JOBS request, which requires the following | |
229 | * attributes: | |
230 | * | |
231 | * attributes-charset | |
232 | * attributes-natural-language | |
233 | * printer-uri | |
234 | */ | |
235 | ||
236 | request = ippNew(); | |
237 | ||
238 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
239 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
240 | ||
241 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
242 | "attributes-natural-language", NULL, language->language); | |
243 | ||
244 | request->request.op.operation_id = IPP_GET_JOBS; | |
245 | request->request.op.request_id = 1; | |
246 | ||
247 | snprintf(uri, sizeof(uri), "ipp://%s/printers/%s", getenv("SERVER_NAME"), | |
248 | printer); | |
249 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
250 | uri); | |
251 | ||
252 | if ((which_jobs = cgiGetVariable("which_jobs")) != NULL) | |
253 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs", | |
254 | NULL, which_jobs); | |
255 | ||
256 | /* | |
257 | * Do the request and get back a response... | |
258 | */ | |
259 | ||
260 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
261 | { | |
262 | ippSetCGIVars(response, NULL, NULL); | |
263 | ippDelete(response); | |
264 | ||
265 | cgiCopyTemplateLang(stdout, TEMPLATES, "jobs.tmpl", getenv("LANG")); | |
266 | } | |
267 | } | |
13c1a6d9 | 268 | } |
3d9e2586 | 269 | else |
270 | { | |
271 | /* | |
272 | * Print a test page... | |
273 | */ | |
c4adf588 | 274 | |
3d9e2586 | 275 | snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", printer); |
100cd8c8 | 276 | |
100cd8c8 | 277 | /* |
3d9e2586 | 278 | * Build an IPP_PRINT_JOB request, which requires the following |
100cd8c8 | 279 | * attributes: |
280 | * | |
281 | * attributes-charset | |
282 | * attributes-natural-language | |
283 | * printer-uri | |
3d9e2586 | 284 | * requesting-user-name |
285 | * document-format | |
100cd8c8 | 286 | */ |
287 | ||
288 | request = ippNew(); | |
289 | ||
3d9e2586 | 290 | request->request.op.operation_id = IPP_PRINT_JOB; |
291 | request->request.op.request_id = 1; | |
292 | ||
100cd8c8 | 293 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
294 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
295 | ||
296 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
297 | "attributes-natural-language", NULL, language->language); | |
298 | ||
3d9e2586 | 299 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", |
300 | NULL, uri); | |
100cd8c8 | 301 | |
3d9e2586 | 302 | if (getenv("REMOTE_USER") != NULL) |
303 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
304 | NULL, getenv("REMOTE_USER")); | |
305 | else | |
306 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
307 | NULL, "root"); | |
100cd8c8 | 308 | |
3d9e2586 | 309 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", |
310 | NULL, "Test Page"); | |
311 | ||
312 | ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format", | |
313 | NULL, "application/postscript"); | |
f63a2256 | 314 | |
100cd8c8 | 315 | /* |
316 | * Do the request and get back a response... | |
317 | */ | |
318 | ||
3d9e2586 | 319 | if ((response = cupsDoFileRequest(http, request, uri + 15, |
320 | CUPS_DATADIR "/data/testprint.ps")) != NULL) | |
100cd8c8 | 321 | { |
3d9e2586 | 322 | status = response->request.status.status_code; |
323 | ippSetCGIVars(response, NULL, NULL); | |
324 | ||
100cd8c8 | 325 | ippDelete(response); |
3d9e2586 | 326 | } |
327 | else | |
328 | status = IPP_GONE; | |
100cd8c8 | 329 | |
3d9e2586 | 330 | cgiSetVariable("PRINTER_NAME", printer); |
331 | ||
332 | if (status > IPP_OK_CONFLICT) | |
333 | { | |
334 | cgiSetVariable("ERROR", ippErrorString(status)); | |
335 | cgiCopyTemplateLang(stdout, TEMPLATES, "error.tmpl", getenv("LANG")); | |
100cd8c8 | 336 | } |
3d9e2586 | 337 | else |
338 | cgiCopyTemplateLang(stdout, TEMPLATES, "test-page.tmpl", getenv("LANG")); | |
100cd8c8 | 339 | } |
340 | ||
a3e17a89 | 341 | cgiCopyTemplateLang(stdout, TEMPLATES, "trailer.tmpl", getenv("LANG")); |
c4adf588 | 342 | |
13c1a6d9 | 343 | /* |
344 | * Close the HTTP server connection... | |
345 | */ | |
c4adf588 | 346 | |
13c1a6d9 | 347 | httpClose(http); |
348 | cupsLangFree(language); | |
580af8cf | 349 | |
13c1a6d9 | 350 | /* |
351 | * Return with no errors... | |
352 | */ | |
580af8cf | 353 | |
13c1a6d9 | 354 | return (0); |
c4adf588 | 355 | } |
356 | ||
357 | ||
358 | /* | |
b5cb0608 | 359 | * End of "$Id: printers.c,v 1.21.2.1 2001/05/13 18:38:01 mike Exp $". |
c4adf588 | 360 | */ |