summaryrefslogtreecommitdiff
path: root/cups/ipp-vars.c
blob: 69efbd9aa0751ef28e61cc38a797a8255f90c100 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * IPP data file parsing functions.
 *
 * Copyright © 2007-2019 by Apple Inc.
 * Copyright © 1997-2007 by Easy Software Products.
 *
 * Licensed under Apache License v2.0.  See the file "LICENSE" for more
 * information.
 */

/*
 * Include necessary headers...
 */

#include "cups-private.h"
#include "ipp-private.h"
#include "string-private.h"
#include "debug-internal.h"


/*
 * '_ippVarsDeinit()' - Free all memory associated with the IPP variables.
 */

void
_ippVarsDeinit(_ipp_vars_t *v)		/* I - IPP variables */
{
  if (v->uri)
  {
    free(v->uri);
    v->uri = NULL;
  }

  cupsFreeOptions(v->num_vars, v->vars);
  v->num_vars = 0;
  v->vars     = NULL;
}


/*
 * '_ippVarsExpand()' - Expand variables in the source string.
 */

void
_ippVarsExpand(_ipp_vars_t *v,		/* I - IPP variables */
               char        *dst,	/* I - Destination buffer */
               const char  *src,	/* I - Source string */
               size_t      dstsize)	/* I - Destination buffer size */
{
  char		*dstptr,		/* Pointer into destination */
		*dstend,		/* End of destination */
		temp[256],		/* Temporary string */
		*tempptr;		/* Pointer into temporary string */
  const char	*value;			/* Value to substitute */


  dstptr = dst;
  dstend = dst + dstsize - 1;

  while (*src && dstptr < dstend)
  {
    if (*src == '$')
    {
     /*
      * Substitute a string/number...
      */

      if (!strncmp(src, "$$", 2))
      {
        value = "$";
	src   += 2;
      }
      else if (!strncmp(src, "$ENV[", 5))
      {
	strlcpy(temp, src + 5, sizeof(temp));

	for (tempptr = temp; *tempptr; tempptr ++)
	  if (*tempptr == ']')
	    break;

        if (*tempptr)
	  *tempptr++ = '\0';

	value = getenv(temp);
        src   += tempptr - temp + 5;
      }
      else
      {
        if (src[1] == '{')
	{
	  src += 2;
	  strlcpy(temp, src, sizeof(temp));
	  if ((tempptr = strchr(temp, '}')) != NULL)
	    *tempptr = '\0';
	  else
	    tempptr = temp + strlen(temp);
	}
	else
	{
	  strlcpy(temp, src + 1, sizeof(temp));

	  for (tempptr = temp; *tempptr; tempptr ++)
	    if (!isalnum(*tempptr & 255) && *tempptr != '-' && *tempptr != '_')
	      break;

	  if (*tempptr)
	    *tempptr = '\0';
        }

        value = _ippVarsGet(v, temp);

        src += tempptr - temp + 1;
      }

      if (value)
      {
        strlcpy(dstptr, value, (size_t)(dstend - dstptr + 1));
	dstptr += strlen(dstptr);
      }
    }
    else
      *dstptr++ = *src++;
  }

  *dstptr = '\0';
}


/*
 * '_ippVarsGet()' - Get a variable string.
 */

const char *				/* O - Value or @code NULL@ if not set */
_ippVarsGet(_ipp_vars_t *v,		/* I - IPP variables */
            const char  *name)		/* I - Variable name */
{
  if (!v)
    return (NULL);
  else if (!strcmp(name, "uri"))
    return (v->uri);
  else if (!strcmp(name, "uriuser") || !strcmp(name, "username"))
    return (v->username[0] ? v->username : NULL);
  else if (!strcmp(name, "scheme") || !strcmp(name, "method"))
    return (v->scheme);
  else if (!strcmp(name, "hostname"))
    return (v->host);
  else if (!strcmp(name, "port"))
    return (v->portstr);
  else if (!strcmp(name, "resource"))
    return (v->resource);
  else if (!strcmp(name, "user"))
    return (cupsUser());
  else
    return (cupsGetOption(name, v->num_vars, v->vars));
}


/*
 * '_ippVarsInit()' - Initialize .
 */

void
_ippVarsInit(_ipp_vars_t      *v,	/* I - IPP variables */
             _ipp_fattr_cb_t  attrcb,	/* I - Attribute (filter) callback */
             _ipp_ferror_cb_t errorcb,	/* I - Error callback */
             _ipp_ftoken_cb_t tokencb)	/* I - Token callback */
{
  memset(v, 0, sizeof(_ipp_vars_t));

  v->attrcb  = attrcb;
  v->errorcb = errorcb;
  v->tokencb = tokencb;
}


/*
 * '_ippVarsPasswordCB()' - Password callback using the IPP variables.
 */

const char *				/* O - Password string or @code NULL@ */
_ippVarsPasswordCB(
    const char *prompt,			/* I - Prompt string (not used) */
    http_t     *http,			/* I - HTTP connection (not used) */
    const char *method,			/* I - HTTP method (not used) */
    const char *resource,		/* I - Resource path (not used) */
    void       *user_data)		/* I - IPP variables */
{
  _ipp_vars_t	*v = (_ipp_vars_t *)user_data;
					/* I - IPP variables */


  (void)prompt;
  (void)http;
  (void)method;
  (void)resource;

  if (v->username[0] && v->password && v->password_tries < 3)
  {
    v->password_tries ++;

    cupsSetUser(v->username);

    return (v->password);
  }
  else
  {
    return (NULL);
  }
}


/*
 * '_ippVarsSet()' - Set an IPP variable.
 */

int					/* O - 1 on success, 0 on failure */
_ippVarsSet(_ipp_vars_t *v,		/* I - IPP variables */
            const char  *name,		/* I - Variable name */
            const char  *value)		/* I - Variable value */
{
  if (!strcmp(name, "uri"))
  {
    char	uri[1024];		/* New printer URI */
    char	resolved[1024];		/* Resolved mDNS URI */

    if (strstr(value, "._tcp"))
    {
     /*
      * Resolve URI...
      */

      if (!_httpResolveURI(value, resolved, sizeof(resolved), _HTTP_RESOLVE_DEFAULT, NULL, NULL))
        return (0);

      value = resolved;
    }

    if (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)
      return (0);

    if (v->username[0])
    {
      if ((v->password = strchr(v->username, ':')) != NULL)
	*(v->password)++ = '\0';
    }

    snprintf(v->portstr, sizeof(v->portstr), "%d", v->port);

    if (v->uri)
      free(v->uri);

    httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), v->scheme, NULL, v->host, v->port, v->resource);
    v->uri = strdup(uri);

    return (v->uri != NULL);
  }
  else
  {
    v->num_vars = cupsAddOption(name, value, v->num_vars, &v->vars);
    return (1);
  }
}