]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/ipp-vars.c
Setting the timeout should also timeout the TLS negotiation
[thirdparty/cups.git] / cups / ipp-vars.c
1 /*
2 * IPP data file parsing functions.
3 *
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
8 * information.
9 */
10
11 /*
12 * Include necessary headers...
13 */
14
15 #include <cups/cups.h>
16 #include "ipp-private.h"
17 #include "string-private.h"
18
19
20 /*
21 * '_ippVarsDeinit()' - Free all memory associated with the IPP variables.
22 */
23
24 void
25 _ippVarsDeinit(_ipp_vars_t *v) /* I - IPP variables */
26 {
27 if (v->uri)
28 {
29 free(v->uri);
30 v->uri = NULL;
31 }
32
33 cupsFreeOptions(v->num_vars, v->vars);
34 v->num_vars = 0;
35 v->vars = NULL;
36 }
37
38
39 /*
40 * '_ippVarsExpand()' - Expand variables in the source string.
41 */
42
43 void
44 _ippVarsExpand(_ipp_vars_t *v, /* I - IPP variables */
45 char *dst, /* I - Destination buffer */
46 const char *src, /* I - Source string */
47 size_t dstsize) /* I - Destination buffer size */
48 {
49 char *dstptr, /* Pointer into destination */
50 *dstend, /* End of destination */
51 temp[256], /* Temporary string */
52 *tempptr; /* Pointer into temporary string */
53 const char *value; /* Value to substitute */
54
55
56 dstptr = dst;
57 dstend = dst + dstsize - 1;
58
59 while (*src && dstptr < dstend)
60 {
61 if (*src == '$')
62 {
63 /*
64 * Substitute a string/number...
65 */
66
67 if (!strncmp(src, "$$", 2))
68 {
69 value = "$";
70 src += 2;
71 }
72 else if (!strncmp(src, "$ENV[", 5))
73 {
74 strlcpy(temp, src + 5, sizeof(temp));
75
76 for (tempptr = temp; *tempptr; tempptr ++)
77 if (*tempptr == ']')
78 break;
79
80 if (*tempptr)
81 *tempptr++ = '\0';
82
83 value = getenv(temp);
84 src += tempptr - temp + 5;
85 }
86 else
87 {
88 if (src[1] == '{')
89 {
90 src += 2;
91 strlcpy(temp, src, sizeof(temp));
92 if ((tempptr = strchr(temp, '}')) != NULL)
93 *tempptr = '\0';
94 else
95 tempptr = temp + strlen(temp);
96 }
97 else
98 {
99 strlcpy(temp, src + 1, sizeof(temp));
100
101 for (tempptr = temp; *tempptr; tempptr ++)
102 if (!isalnum(*tempptr & 255) && *tempptr != '-' && *tempptr != '_')
103 break;
104
105 if (*tempptr)
106 *tempptr = '\0';
107 }
108
109 value = _ippVarsGet(v, temp);
110
111 src += tempptr - temp + 1;
112 }
113
114 if (value)
115 {
116 strlcpy(dstptr, value, (size_t)(dstend - dstptr + 1));
117 dstptr += strlen(dstptr);
118 }
119 }
120 else
121 *dstptr++ = *src++;
122 }
123
124 *dstptr = '\0';
125 }
126
127
128 /*
129 * '_ippVarsGet()' - Get a variable string.
130 */
131
132 const char * /* O - Value or @code NULL@ if not set */
133 _ippVarsGet(_ipp_vars_t *v, /* I - IPP variables */
134 const char *name) /* I - Variable name */
135 {
136 if (!strcmp(name, "uri"))
137 return (v->uri);
138 else if (!strcmp(name, "uriuser") || !strcmp(name, "username"))
139 return (v->username[0] ? v->username : NULL);
140 else if (!strcmp(name, "scheme") || !strcmp(name, "method"))
141 return (v->scheme);
142 else if (!strcmp(name, "hostname"))
143 return (v->host);
144 else if (!strcmp(name, "port"))
145 return (v->portstr);
146 else if (!strcmp(name, "resource"))
147 return (v->resource);
148 else if (!strcmp(name, "user"))
149 return (cupsUser());
150 else
151 return (cupsGetOption(name, v->num_vars, v->vars));
152 }
153
154
155 /*
156 * '_ippVarsInit()' - Initialize .
157 */
158
159 void
160 _ippVarsInit(_ipp_vars_t *v, /* I - IPP variables */
161 _ipp_fattr_cb_t attrcb, /* I - Attribute (filter) callback */
162 _ipp_ferror_cb_t errorcb, /* I - Error callback */
163 _ipp_ftoken_cb_t tokencb) /* I - Token callback */
164 {
165 memset(v, 0, sizeof(_ipp_vars_t));
166
167 v->attrcb = attrcb;
168 v->errorcb = errorcb;
169 v->tokencb = tokencb;
170 }
171
172
173 /*
174 * '_ippVarsPasswordCB()' - Password callback using the IPP variables.
175 */
176
177 const char * /* O - Password string or @code NULL@ */
178 _ippVarsPasswordCB(
179 const char *prompt, /* I - Prompt string (not used) */
180 http_t *http, /* I - HTTP connection (not used) */
181 const char *method, /* I - HTTP method (not used) */
182 const char *resource, /* I - Resource path (not used) */
183 void *user_data) /* I - IPP variables */
184 {
185 _ipp_vars_t *v = (_ipp_vars_t *)user_data;
186 /* I - IPP variables */
187
188
189 (void)prompt;
190 (void)http;
191 (void)method;
192 (void)resource;
193
194 if (v->username[0] && v->password && v->password_tries < 3)
195 {
196 v->password_tries ++;
197
198 cupsSetUser(v->username);
199
200 return (v->password);
201 }
202 else
203 {
204 return (NULL);
205 }
206 }
207
208
209 /*
210 * '_ippVarsSet()' - Set an IPP variable.
211 */
212
213 int /* O - 1 on success, 0 on failure */
214 _ippVarsSet(_ipp_vars_t *v, /* I - IPP variables */
215 const char *name, /* I - Variable name */
216 const char *value) /* I - Variable value */
217 {
218 if (!strcmp(name, "uri"))
219 {
220 char uri[1024]; /* New printer URI */
221 http_uri_status_t uri_status; /* URI status */
222
223 if ((uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, value, v->scheme, sizeof(v->scheme), v->username, sizeof(v->username), v->host, sizeof(v->host), &(v->port), v->resource, sizeof(v->resource))) < HTTP_URI_STATUS_OK)
224 return (0);
225
226 if (v->username[0])
227 {
228 if ((v->password = strchr(v->username, ':')) != NULL)
229 *(v->password)++ = '\0';
230 }
231
232 snprintf(v->portstr, sizeof(v->portstr), "%d", v->port);
233
234 if (v->uri)
235 free(v->uri);
236
237 httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), v->scheme, NULL, v->host, v->port, v->resource);
238 v->uri = strdup(uri);
239
240 return (v->uri != NULL);
241 }
242 else
243 {
244 v->num_vars = cupsAddOption(name, value, v->num_vars, &v->vars);
245 return (1);
246 }
247 }