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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
*/
/* $Id: ipp-support.c 148 2006-04-25 16:54:17Z njacobs $ */
#include <papi_impl.h>
#include <stdlib.h>
#include <pwd.h>
#include <locale.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <md5.h>
#include <config-site.h>
#include <ipp.h>
static void ipp_add_printer_uri(service_t *svc, char *name,
papi_attribute_t ***op);
papi_status_t
http_to_papi_status(http_status_t status)
{
switch (status) {
case HTTP_OK:
return (PAPI_OK);
case HTTP_BAD_REQUEST:
return (PAPI_BAD_REQUEST);
case HTTP_UNAUTHORIZED:
case HTTP_FORBIDDEN:
return (PAPI_NOT_AUTHORIZED);
case HTTP_NOT_FOUND:
return (PAPI_NOT_FOUND);
case HTTP_GONE:
return (PAPI_GONE);
case HTTP_SERVICE_UNAVAILABLE:
return (PAPI_SERVICE_UNAVAILABLE);
default:
return ((papi_status_t)status);
}
}
papi_status_t
ipp_to_papi_status(uint16_t status)
{
switch (status) {
case IPP_OK:
return (PAPI_OK);
case IPP_OK_IGNORED_ATTRIBUTES:
return (PAPI_OK);
case IPP_OK_CONFLICTING_ATTRIBUTES:
return (PAPI_OK);
case IPP_OK_IGNORED_SUBSCRIPTIONS:
return (PAPI_OK_IGNORED_SUBSCRIPTIONS);
case IPP_OK_IGNORED_NOTIFICATIONS:
return (PAPI_OK_IGNORED_NOTIFICATIONS);
case IPP_CERR_BAD_REQUEST:
return (PAPI_BAD_REQUEST);
case IPP_CERR_FORBIDDEN:
return (PAPI_FORBIDDEN);
case IPP_CERR_NOT_AUTHENTICATED:
return (PAPI_NOT_AUTHENTICATED);
case IPP_CERR_NOT_AUTHORIZED:
return (PAPI_NOT_AUTHORIZED);
case IPP_CERR_NOT_POSSIBLE:
return (PAPI_NOT_POSSIBLE);
case IPP_CERR_TIMEOUT:
return (PAPI_TIMEOUT);
case IPP_CERR_NOT_FOUND:
return (PAPI_NOT_FOUND);
case IPP_CERR_GONE:
return (PAPI_GONE);
case IPP_CERR_REQUEST_ENTITY:
return (PAPI_REQUEST_ENTITY);
case IPP_CERR_REQUEST_VALUE:
return (PAPI_REQUEST_VALUE);
case IPP_CERR_DOCUMENT_FORMAT:
return (PAPI_DOCUMENT_FORMAT);
case IPP_CERR_ATTRIBUTES:
return (PAPI_ATTRIBUTES);
case IPP_CERR_URI_SCHEME:
return (PAPI_URI_SCHEME);
case IPP_CERR_CHARSET:
return (PAPI_CHARSET);
case IPP_CERR_CONFLICT:
return (PAPI_CONFLICT);
case IPP_CERR_COMPRESSION_NOT_SUPPORTED:
return (PAPI_COMPRESSION_NOT_SUPPORTED);
case IPP_CERR_COMPRESSION_ERROR:
return (PAPI_COMPRESSION_ERROR);
case IPP_CERR_DOCUMENT_FORMAT_ERROR:
return (PAPI_DOCUMENT_FORMAT_ERROR);
case IPP_CERR_DOCUMENT_ACCESS_ERROR:
return (PAPI_DOCUMENT_ACCESS_ERROR);
case IPP_CERR_ATTRIBUTES_NOT_SETTABLE:
return (PAPI_ATTRIBUTES_NOT_SETTABLE);
case IPP_CERR_IGNORED_ALL_SUBSCRIPTIONS:
return (PAPI_IGNORED_ALL_SUBSCRIPTIONS);
case IPP_CERR_TOO_MANY_SUBSCRIPTIONS:
return (PAPI_TOO_MANY_SUBSCRIPTIONS);
case IPP_CERR_IGNORED_ALL_NOTIFICATIONS:
return (PAPI_IGNORED_ALL_NOTIFICATIONS);
case IPP_CERR_PRINT_SUPPORT_FILE_NOT_FOUND:
return (PAPI_PRINT_SUPPORT_FILE_NOT_FOUND);
case IPP_SERR_INTERNAL:
return (PAPI_INTERNAL_ERROR);
case IPP_SERR_OPERATION_NOT_SUPPORTED:
return (PAPI_OPERATION_NOT_SUPPORTED);
case IPP_SERR_SERVICE_UNAVAILABLE:
return (PAPI_SERVICE_UNAVAILABLE);
case IPP_SERR_VERSION_NOT_SUPPORTED:
return (PAPI_VERSION_NOT_SUPPORTED);
case IPP_SERR_DEVICE_ERROR:
return (PAPI_DEVICE_ERROR);
case IPP_SERR_TEMPORARY_ERROR:
return (PAPI_TEMPORARY_ERROR);
case IPP_SERR_NOT_ACCEPTING:
return (PAPI_NOT_ACCEPTING);
case IPP_SERR_BUSY:
case IPP_SERR_CANCELLED:
default:
return (PAPI_TEMPORARY_ERROR);
}
}
void
ipp_initialize_request(service_t *svc, papi_attribute_t ***request,
uint16_t operation)
{
papiAttributeListAddInteger(request, PAPI_ATTR_EXCL,
"version-major", 1);
papiAttributeListAddInteger(request, PAPI_ATTR_EXCL,
"version-minor", 1);
papiAttributeListAddInteger(request, PAPI_ATTR_EXCL,
"request-id", (short)lrand48());
papiAttributeListAddInteger(request, PAPI_ATTR_EXCL,
"operation-id", operation);
}
void
ipp_initialize_operational_attributes(service_t *svc, papi_attribute_t ***op,
char *printer, int job_id)
{
char *charset = "utf-8"; /* default to UTF-8 encoding */
char *language = setlocale(LC_ALL, "");
char *user = "nobody";
struct passwd *pw = NULL;
/*
* All IPP requests must contain the following:
* attributes-charset (UTF-8)
* attributes-natural-language (our current locale)
* (object identifier) printer-uri/job-id or job-uri
* requesting-user-name (process user or none)
*/
papiAttributeListAddString(op, PAPI_ATTR_EXCL,
"attributes-charset", charset);
papiAttributeListAddString(op, PAPI_ATTR_EXCL,
"attributes-natural-language", language);
if (printer != NULL)
ipp_add_printer_uri(svc, printer, op);
if ((printer != NULL) && (job_id >= 0))
papiAttributeListAddInteger(op, PAPI_ATTR_EXCL,
"job-id", job_id);
if ((pw = getpwuid(getuid())) != NULL)
user = pw->pw_name;
/*
* if our euid is 0 "super user", we will allow the system supplied
* user name to be overridden, if the requestor wants to.
*/
if (geteuid() == 0) {
if (svc->user != NULL)
user = svc->user;
}
papiAttributeListAddString(op, PAPI_ATTR_REPLACE,
"requesting-user-name", user);
}
#ifndef OPID_CUPS_GET_DEFAULT /* for servers that will enumerate */
#define OPID_CUPS_GET_DEFAULT 0x4001
#endif /* OPID_CUPS_GET_DEFAULT */
static papi_status_t
_default_destination(service_t *svc, char **uri)
{
papi_status_t result = PAPI_INTERNAL_ERROR;
printer_t *p = NULL;
papi_attribute_t **request = NULL, **op = NULL, **response = NULL;
char *tmp = NULL;
if ((svc == NULL) || (uri == NULL))
return (PAPI_BAD_ARGUMENT);
/* we must be connected to find the default destination */
if (svc->connection == NULL)
return (PAPI_NOT_POSSIBLE);
if ((p = calloc(1, sizeof (*p))) == NULL)
return (PAPI_TEMPORARY_ERROR);
ipp_initialize_request(svc, &request, OPID_CUPS_GET_DEFAULT);
ipp_initialize_operational_attributes(svc, &op, NULL, -1);
papiAttributeListAddString(&op, PAPI_ATTR_APPEND,
"requested-attributes", "printer-uri-supported");
papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE,
"operational-attributes-group", op);
papiAttributeListFree(op);
result = ipp_send_request(svc, request, &response);
papiAttributeListFree(request);
op = NULL;
papiAttributeListGetCollection(response, NULL,
"printer-attributes-group", &op);
if (uri != NULL) {
char *tmp = NULL;
papiAttributeListGetString(op, NULL, "printer-uri", &tmp);
papiAttributeListGetString(op, NULL,
"printer-uri-supported", &tmp);
if (tmp != NULL)
*uri = strdup(tmp);
}
papiAttributeListFree(response);
return (result);
}
static void
ipp_add_printer_uri(service_t *svc, char *name, papi_attribute_t ***op)
{
char *uri = name;
char buf[BUFSIZ];
uri_t *tmp = NULL;
if (strstr(name, "://") == NULL) { /* not in URI form */
if (strcmp(name, DEFAULT_DEST) != 0) {
/* not the "default" */
snprintf(buf, sizeof (buf), "%s/%s", svc->name, name);
uri = buf;
} else
_default_destination(svc, &uri);
}
papiAttributeListAddString(op, PAPI_ATTR_EXCL, "printer-uri", uri);
/* save the printer-uri's path to be used by http POST request */
if ((uri_from_string(uri, &tmp) == 0) && (tmp != NULL)) {
if (svc->post != NULL)
free(svc->post);
svc->post = strdup(tmp->path);
uri_free(tmp);
}
}
/*
* don't actually write anything, just add to the total size and return the
* size of what would be written, so we can figure out how big the request
* is going to be.
*/
static ssize_t
size_calculate(void *fd, void *buffer, size_t length)
{
ssize_t *size = (ssize_t *)fd;
*size += length;
return (length);
}
static ssize_t
build_chunk(void *fd, void *buffer, size_t length)
{
char **s1 = fd;
memcpy(*s1, buffer, length);
*s1 = *s1 + length;
return (length);
}
ssize_t
ipp_request_write(void *fd, void *buffer, size_t length)
{
service_t *svc = (service_t *)fd;
#ifdef DEBUG
printf("ipp_request_write(0x%8.8x, 0x%8.8x, %d)\n", fd, buffer, length);
httpDumpData(stdout, "ipp_request_write:", buffer, length);
#endif
return (httpWrite(svc->connection, buffer, length));
}
ssize_t
ipp_request_read(void *fd, void *buffer, size_t length)
{
service_t *svc = (service_t *)fd;
ssize_t rc, i = length;
char *p = buffer;
while ((rc = httpRead(svc->connection, p, i)) != i) {
if (rc == 0)
return (rc);
if (rc < 0)
return (rc);
i -= rc;
p += rc;
}
#ifdef DEBUG
printf("ipp_request_read(0x%8.8x, 0x%8.8x, %d) = %d\n",
fd, buffer, length, rc);
httpDumpData(stdout, "ipp_request_read:", buffer, length);
#endif
return (length);
}
papi_status_t
ipp_send_initial_request_block(service_t *svc, papi_attribute_t **request,
ssize_t file_size)
{
papi_status_t result = PAPI_OK;
ssize_t chunk_size = 0;
char length[32];
void *chunk, *ptr;
http_status_t status;
/* calculate the request size */
(void) ipp_write_message(&size_calculate, &chunk_size, request);
/* Fill in the HTTP Header information */
httpClearFields(svc->connection);
if (svc->transfer_encoding == TRANSFER_ENCODING_CHUNKED)
httpSetField(svc->connection, HTTP_FIELD_TRANSFER_ENCODING,
"chunked");
else {
sprintf(length, "%lu", (unsigned long)(file_size + chunk_size));
httpSetField(svc->connection, HTTP_FIELD_CONTENT_LENGTH,
length);
}
httpSetField(svc->connection, HTTP_FIELD_CONTENT_TYPE,
"application/ipp");
httpSetField(svc->connection, HTTP_FIELD_AUTHORIZATION,
svc->connection->authstring);
/* flush any state information about this connection */
httpFlush(svc->connection);
/* if we have don't have a POST path, use the service uri path */
if ((svc->post == NULL) && (svc->uri->path))
svc->post = strdup(svc->uri->path);
/* send the HTTP POST message for the IPP request */
/* if the POST fails, return the error */
status = httpPost(svc->connection, svc->post);
if (status != 0)
return (http_to_papi_status(status));
if (httpCheck(svc->connection) != 0) {
status = httpUpdate(svc->connection);
if (status != HTTP_OK)
return (http_to_papi_status(status));
}
/* build the request chunk */
chunk = ptr = calloc(1, chunk_size);
result = ipp_write_message(&build_chunk, &ptr, request);
#ifdef DEBUG
printf("request: %d (0x%x) bytes\n", chunk_size, chunk_size);
httpDumpData(stdout, "request:", chunk, chunk_size);
#endif
/* send the actual IPP request */
if (ipp_request_write(svc, chunk, chunk_size) != chunk_size)
result = PAPI_TEMPORARY_ERROR;
free(chunk);
if (httpCheck(svc->connection) != 0) {
status = httpUpdate(svc->connection);
if (status != HTTP_OK)
return (http_to_papi_status(status));
}
return (result);
}
static int
setAuthString(service_t *svc)
{
http_t *http;
char *user, *passphrase;
char encoded[BUFSIZ];
if ((svc == NULL) || (svc->connection == NULL) || (svc->name == NULL))
return (-1);
http = svc->connection;
if (svc->user == NULL) {
struct passwd *p;
if ((p = getpwuid(getuid())) != NULL) {
user = p->pw_name;
} else if ((user = getenv("LOGNAME")) == NULL)
user = getenv("USER");
if (user == NULL)
user = "nobody";
} else
user = svc->user;
/* if the passphrase is not set, use the Authentication Callback */
if (((svc->password == NULL) || (svc->password[0] == '\0')) &&
(svc->authCB != NULL))
(svc->authCB)(svc, svc->app_data);
passphrase = svc->password;
/* if there is still no passphrase, we have to fail */
if ((passphrase == NULL) || (passphrase[0] == '\0'))
return (-1);
if (strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE],
"Basic", 5) == 0) {
char plain[BUFSIZ];
snprintf(plain, sizeof (plain), "%s:%s", user, passphrase);
httpEncode64(encoded, plain);
snprintf(http->authstring, sizeof (http->authstring),
"Basic %s", encoded);
} else if (strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE],
"Digest", 6) == 0) {
char realm[HTTP_MAX_VALUE];
char nonce[HTTP_MAX_VALUE];
char line [BUFSIZ];
char urp[128];
char mr[128];
char *uri = svc->post;
httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE,
"realm", realm);
httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE,
"nonce", nonce);
snprintf(line, sizeof (line), "%s:%s:%s", user, realm,
passphrase);
md5_calc(urp, line, strlen(line));
snprintf(line, sizeof (line), "POST:%s", uri);
md5_calc(mr, line, strlen(line));
snprintf(line, sizeof (line), "%s:%s:%s", urp, mr, nonce);
md5_calc(encoded, line, strlen(line));
snprintf(http->authstring, sizeof (http->authstring),
"Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", "
"uri=\"%s\", response=\"%s\"", user, realm, nonce, uri,
encoded);
}
return (0);
}
papi_status_t
ipp_status_info(service_t *svc, papi_attribute_t **response)
{
papi_attribute_t **operational = NULL;
int32_t status = 0;
papiAttributeListGetCollection(response, NULL,
"operational-attributes-group", &operational);
if (operational != NULL) {
char *message = NULL;
papiAttributeListGetString(response, NULL,
"status-message", &message);
papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
"detailed-status-message", message);
}
papiAttributeListGetInteger(response, NULL, "status-code", &status);
return (ipp_to_papi_status(status));
}
papi_status_t
ipp_send_request_with_file(service_t *svc, papi_attribute_t **request,
papi_attribute_t ***response, char *file)
{
papi_status_t result = PAPI_OK;
ssize_t size = 0;
struct stat statbuf;
int fd;
#ifdef DEBUG
fprintf(stderr, "\nIPP-REQUEST: (%s)", (file ? file : ""));
papiAttributeListPrint(stderr, request, " ");
putc('\n', stderr);
fflush(stderr);
#endif
/*
* if we are sending a file, open it and include it's size in the
* message size.
*/
if (file != NULL) {
if ((fd = open(file, O_RDONLY)) < 0) {
detailed_error(svc, "%s: %s", file, strerror(errno));
return (PAPI_DOCUMENT_ACCESS_ERROR);
} else if (strcmp("standard input", file) != 0) {
if (stat(file, &statbuf) < 0) {
detailed_error(svc,
gettext("Cannot access file: %s: %s"),
file, strerror(errno));
return (PAPI_DOCUMENT_ACCESS_ERROR);
}
if (statbuf.st_size == 0) {
detailed_error(svc,
"Zero byte (empty) file: %s", file);
return (PAPI_BAD_ARGUMENT);
}
} else if (svc->transfer_encoding !=
TRANSFER_ENCODING_CHUNKED) {
struct stat st;
if (fstat(fd, &st) >= 0)
size = st.st_size;
}
}
*response = NULL;
while (*response == NULL) {
http_status_t status = HTTP_CONTINUE;
result = ipp_send_initial_request_block(svc, request, size);
if (result == PAPI_OK) {
if (file != NULL) {
/* send the file contents if we have it */
int rc;
char buf[BUFSIZ];
lseek(fd, 0L, SEEK_SET);
while ((rc = read(fd, buf, sizeof (buf))) > 0) {
if (ipp_request_write(svc, buf, rc)
< rc) {
break;
}
}
}
(void) ipp_request_write(svc, "", 0);
}
/* update our connection info */
while (status == HTTP_CONTINUE)
status = httpUpdate(svc->connection);
if (status == HTTP_UNAUTHORIZED) {
httpFlush(svc->connection);
if ((svc->connection->authstring[0] == '\0') &&
(setAuthString(svc) == 0)) {
httpReconnect(svc->connection);
continue;
}
} else if (status == HTTP_UPGRADE_REQUIRED) {
/*
* If the transport was built with TLS support, we can
* try to use it.
*/
httpFlush(svc->connection);
httpReconnect(svc->connection);
httpEncryption(svc->connection, HTTP_ENCRYPT_REQUIRED);
continue;
}
if (status != HTTP_OK)
return (http_to_papi_status(status));
/* read the IPP response */
result = ipp_read_message(&ipp_request_read, svc, response,
IPP_TYPE_RESPONSE);
if (result == PAPI_OK)
result = ipp_status_info(svc, *response);
#ifdef DEBUG
fprintf(stderr, "\nIPP-RESPONSE: (%s) (%s)", (file ? file : ""),
papiStatusString(result));
papiAttributeListPrint(stderr, *response, " ");
putc('\n', stderr);
fflush(stderr);
#endif
}
return (result);
}
papi_status_t
ipp_send_request(service_t *svc, papi_attribute_t **request,
papi_attribute_t ***response)
{
return (ipp_send_request_with_file(svc, request, response, NULL));
}
|