]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/ipp-var.c
Add support for HTTPS environment variable in CGIs (to keep encryption
[thirdparty/cups.git] / cgi-bin / ipp-var.c
1 /*
2 * "$Id: ipp-var.c,v 1.23 2001/03/10 15:19:35 mike Exp $"
3 *
4 * IPP variable routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2001 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-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * ippGetTemplateDir() - Get the templates directory...
27 * ippSetServerVersion() - Set the server name and CUPS version...
28 * ippSetCGIVars() - Set CGI variables from an IPP response.
29 */
30
31 /*
32 * Include necessary headers...
33 */
34
35 #include "ipp-var.h"
36
37
38 /*
39 * 'ippGetTemplateDir()' - Get the templates directory...
40 */
41
42 char * /* O - Template directory */
43 ippGetTemplateDir(void)
44 {
45 const char *datadir; /* CUPS_DATADIR env var */
46 static char templates[1024] = ""; /* Template directory */
47
48
49 if (!templates[0])
50 {
51 /*
52 * Build the template directory pathname...
53 */
54
55 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
56 datadir = CUPS_DATADIR;
57
58 snprintf(templates, sizeof(templates), "%s/templates", datadir);
59 }
60
61 return (templates);
62 }
63
64
65 /*
66 * 'ippSetServerVersion()' - Set the server name and CUPS version...
67 */
68
69 void
70 ippSetServerVersion(void)
71 {
72 cgiSetVariable("SERVER_NAME", getenv("SERVER_NAME"));
73 cgiSetVariable("REMOTE_USER", getenv("REMOTE_USER"));
74 cgiSetVariable("CUPS_VERSION", CUPS_SVERSION);
75 }
76
77
78 /*
79 * 'ippSetCGIVars()' - Set CGI variables from an IPP response.
80 */
81
82 void
83 ippSetCGIVars(ipp_t *response, /* I - Response data to be copied... */
84 const char *filter_name, /* I - Filter name */
85 const char *filter_value) /* I - Filter value */
86 {
87 int element; /* Element in CGI array */
88 ipp_attribute_t *attr, /* Attribute in response... */
89 *filter; /* Filtering attribute */
90 int i; /* Looping var */
91 char name[1024], /* Name of attribute */
92 value[16384], /* Value(s) */
93 *valptr; /* Pointer into value */
94 char method[HTTP_MAX_URI],
95 username[HTTP_MAX_URI],
96 hostname[HTTP_MAX_URI],
97 resource[HTTP_MAX_URI],
98 uri[HTTP_MAX_URI];
99 int port; /* URI data */
100 int ishttps; /* Using encryption? */
101 const char *server; /* Name of server */
102 struct tm *date; /* Date information */
103
104
105 ippSetServerVersion();
106
107 server = getenv("SERVER_NAME");
108 ishttps = getenv("HTTPS") != NULL;
109
110 for (attr = response->attrs;
111 attr && attr->group_tag == IPP_TAG_OPERATION;
112 attr = attr->next);
113
114 for (element = 0; attr != NULL; attr = attr->next, element ++)
115 {
116 /*
117 * Copy attributes to a separator...
118 */
119
120 if (filter_name)
121 {
122 for (filter = attr;
123 filter != NULL && filter->group_tag != IPP_TAG_ZERO;
124 filter = filter->next)
125 if (filter->name && strcmp(filter->name, filter_name) == 0 &&
126 (filter->value_tag == IPP_TAG_STRING ||
127 (filter->value_tag >= IPP_TAG_TEXTLANG &&
128 filter->value_tag <= IPP_TAG_MIMETYPE)) &&
129 filter->values[0].string.text != NULL &&
130 strcasecmp(filter->values[0].string.text, filter_value) == 0)
131 break;
132
133 if (!filter)
134 return;
135
136 if (filter->group_tag == IPP_TAG_ZERO)
137 {
138 attr = filter;
139 element --;
140 continue;
141 }
142 }
143
144 for (; attr != NULL && attr->group_tag != IPP_TAG_ZERO; attr = attr->next)
145 {
146 /*
147 * Copy the attribute name, substituting "_" for "-"...
148 */
149
150 if (attr->name == NULL)
151 continue;
152
153 for (i = 0; attr->name[i]; i ++)
154 if (attr->name[i] == '-')
155 name[i] = '_';
156 else
157 name[i] = attr->name[i];
158
159 name[i] = '\0';
160
161 /*
162 * Add "job_printer_name" variable if we have a "job_printer_uri"
163 * attribute...
164 */
165
166 if (strcmp(name, "job_printer_uri") == 0)
167 {
168 if ((valptr = strrchr(attr->values[0].string.text, '/')) == NULL)
169 valptr = "unknown";
170 else
171 valptr ++;
172
173 cgiSetArray("job_printer_name", element, valptr);
174 }
175
176 /*
177 * Copy values...
178 */
179
180 value[0] = '\0'; /* Initially an empty string */
181 value[sizeof(value) - 1] = '\0'; /* In case string gets full */
182 valptr = value; /* Start at the beginning */
183
184 for (i = 0; i < attr->num_values; i ++)
185 {
186 if (i)
187 strncat(valptr, ",", sizeof(value) - (valptr - value) - 1);
188
189 valptr += strlen(valptr);
190
191 switch (attr->value_tag)
192 {
193 case IPP_TAG_INTEGER :
194 case IPP_TAG_ENUM :
195 if (strncmp(name, "time_at_", 8) == 0)
196 {
197 date = localtime((time_t *)&(attr->values[i].integer));
198 strftime(valptr, sizeof(value) - (valptr - value),
199 CUPS_STRFTIME_FORMAT, date);
200 }
201 else
202 snprintf(valptr, sizeof(value) - (valptr - value),
203 "%d", attr->values[i].integer);
204 break;
205
206 case IPP_TAG_BOOLEAN :
207 snprintf(valptr, sizeof(value) - (valptr - value),
208 "%d", attr->values[i].boolean);
209 break;
210
211 case IPP_TAG_NOVALUE :
212 strncat(valptr, "novalue", sizeof(value) - (valptr - value) - 1);
213 break;
214
215 case IPP_TAG_RANGE :
216 snprintf(valptr, sizeof(value) - (valptr - value),
217 "%d-%d", attr->values[i].range.lower,
218 attr->values[i].range.upper);
219 break;
220
221 case IPP_TAG_RESOLUTION :
222 snprintf(valptr, sizeof(value) - (valptr - value),
223 "%dx%d%s", attr->values[i].resolution.xres,
224 attr->values[i].resolution.yres,
225 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
226 "dpi" : "dpc");
227 break;
228
229 case IPP_TAG_URI :
230 if (strchr(attr->values[i].string.text, ':') != NULL)
231 {
232 httpSeparate(attr->values[i].string.text, method, username,
233 hostname, &port, resource);
234
235 if (strcmp(method, "ipp") == 0 ||
236 strcmp(method, "http") == 0)
237 {
238 /*
239 * Map localhost access to localhost and local port...
240 */
241
242 if (strcasecmp(hostname, server) == 0 &&
243 (strcmp(getenv("REMOTE_HOST"), "127.0.0.1") == 0 ||
244 strcmp(getenv("REMOTE_HOST"), "localhost") == 0 ||
245 strcmp(getenv("REMOTE_HOST"), server) == 0))
246 {
247 strcpy(hostname, "localhost");
248 port = atoi(getenv("SERVER_PORT"));
249 }
250
251 /*
252 * Rewrite URI with HTTP address...
253 */
254
255 if (username[0])
256 snprintf(uri, sizeof(uri), "%s://%s@%s:%d%s",
257 ishttps ? "https" : "http",
258 username, hostname, port, resource);
259 else
260 snprintf(uri, sizeof(uri), "%s://%s:%d%s",
261 ishttps ? "https" : "http",
262 hostname, port, resource);
263
264 strncat(valptr, uri, sizeof(value) - (valptr - value) - 1);
265 break;
266 }
267 }
268
269 case IPP_TAG_STRING :
270 case IPP_TAG_TEXT :
271 case IPP_TAG_NAME :
272 case IPP_TAG_KEYWORD :
273 case IPP_TAG_CHARSET :
274 case IPP_TAG_LANGUAGE :
275 strncat(valptr, attr->values[i].string.text,
276 sizeof(value) - (valptr - value) - 1);
277 break;
278
279 default :
280 break; /* anti-compiler-warning-code */
281 }
282 }
283
284 /*
285 * Add the element...
286 */
287
288 cgiSetArray(name, element, value);
289 }
290
291 if (attr == NULL)
292 break;
293 }
294 }
295
296
297 /*
298 * End of "$Id: ipp-var.c,v 1.23 2001/03/10 15:19:35 mike Exp $".
299 */