xref: /titanic_52/usr/src/lib/print/libpapi-ipp/common/ipp-support.c (revision e059026e2da8f12ef0ebea9d686d67f32660cfc0)
1355b4669Sjacobs /*
2355b4669Sjacobs  * CDDL HEADER START
3355b4669Sjacobs  *
4355b4669Sjacobs  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
7355b4669Sjacobs  *
8355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs  * See the License for the specific language governing permissions
11355b4669Sjacobs  * and limitations under the License.
12355b4669Sjacobs  *
13355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs  *
19355b4669Sjacobs  * CDDL HEADER END
20355b4669Sjacobs  */
21355b4669Sjacobs 
22355b4669Sjacobs /*
2395c2d302SJonathan Cowper-Andrewes  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: ipp-support.c 148 2006-04-25 16:54:17Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs 
31355b4669Sjacobs #include <papi_impl.h>
32355b4669Sjacobs #include <stdlib.h>
33355b4669Sjacobs #include <pwd.h>
34355b4669Sjacobs #include <locale.h>
35355b4669Sjacobs #include <errno.h>
36355b4669Sjacobs #include <fcntl.h>
37355b4669Sjacobs #include <sys/stat.h>
38355b4669Sjacobs #include <md5.h>
39355b4669Sjacobs 
40355b4669Sjacobs #include <config-site.h>
41355b4669Sjacobs 
420a44ef6dSjacobs #include <ipp.h>
430a44ef6dSjacobs 
44223f6c28Sjacobs static void ipp_add_printer_uri(service_t *svc, char *name,
45223f6c28Sjacobs 		papi_attribute_t ***op);
46223f6c28Sjacobs 
47355b4669Sjacobs papi_status_t
48355b4669Sjacobs http_to_papi_status(http_status_t status)
49355b4669Sjacobs {
50355b4669Sjacobs 	switch (status) {
51355b4669Sjacobs 	case HTTP_OK:
52355b4669Sjacobs 		return (PAPI_OK);
53355b4669Sjacobs 	case HTTP_BAD_REQUEST:
54355b4669Sjacobs 		return (PAPI_BAD_REQUEST);
55355b4669Sjacobs 	case HTTP_UNAUTHORIZED:
56355b4669Sjacobs 	case HTTP_FORBIDDEN:
57355b4669Sjacobs 		return (PAPI_NOT_AUTHORIZED);
58355b4669Sjacobs 	case HTTP_NOT_FOUND:
59355b4669Sjacobs 		return (PAPI_NOT_FOUND);
60355b4669Sjacobs 	case HTTP_GONE:
61355b4669Sjacobs 		return (PAPI_GONE);
62355b4669Sjacobs 	case HTTP_SERVICE_UNAVAILABLE:
63355b4669Sjacobs 		return (PAPI_SERVICE_UNAVAILABLE);
64355b4669Sjacobs 	default:
65355b4669Sjacobs 		return ((papi_status_t)status);
66355b4669Sjacobs 	}
67355b4669Sjacobs }
68355b4669Sjacobs 
69355b4669Sjacobs papi_status_t
70355b4669Sjacobs ipp_to_papi_status(uint16_t status)
71355b4669Sjacobs {
72355b4669Sjacobs 	switch (status) {
73355b4669Sjacobs 	case IPP_OK:
74355b4669Sjacobs 		return (PAPI_OK);
75355b4669Sjacobs 	case IPP_OK_IGNORED_ATTRIBUTES:
76355b4669Sjacobs 		return (PAPI_OK);
77355b4669Sjacobs 	case IPP_OK_CONFLICTING_ATTRIBUTES:
78355b4669Sjacobs 		return (PAPI_OK);
79355b4669Sjacobs 	case IPP_OK_IGNORED_SUBSCRIPTIONS:
80355b4669Sjacobs 		return (PAPI_OK_IGNORED_SUBSCRIPTIONS);
81355b4669Sjacobs 	case IPP_OK_IGNORED_NOTIFICATIONS:
82355b4669Sjacobs 		return (PAPI_OK_IGNORED_NOTIFICATIONS);
83355b4669Sjacobs 	case IPP_CERR_BAD_REQUEST:
84355b4669Sjacobs 		return (PAPI_BAD_REQUEST);
85355b4669Sjacobs 	case IPP_CERR_FORBIDDEN:
86355b4669Sjacobs 		return (PAPI_FORBIDDEN);
87355b4669Sjacobs 	case IPP_CERR_NOT_AUTHENTICATED:
88355b4669Sjacobs 		return (PAPI_NOT_AUTHENTICATED);
89355b4669Sjacobs 	case IPP_CERR_NOT_AUTHORIZED:
90355b4669Sjacobs 		return (PAPI_NOT_AUTHORIZED);
91355b4669Sjacobs 	case IPP_CERR_NOT_POSSIBLE:
92355b4669Sjacobs 		return (PAPI_NOT_POSSIBLE);
93355b4669Sjacobs 	case IPP_CERR_TIMEOUT:
94355b4669Sjacobs 		return (PAPI_TIMEOUT);
95355b4669Sjacobs 	case IPP_CERR_NOT_FOUND:
96355b4669Sjacobs 		return (PAPI_NOT_FOUND);
97355b4669Sjacobs 	case IPP_CERR_GONE:
98355b4669Sjacobs 		return (PAPI_GONE);
99355b4669Sjacobs 	case IPP_CERR_REQUEST_ENTITY:
100355b4669Sjacobs 		return (PAPI_REQUEST_ENTITY);
101355b4669Sjacobs 	case IPP_CERR_REQUEST_VALUE:
102355b4669Sjacobs 		return (PAPI_REQUEST_VALUE);
103355b4669Sjacobs 	case IPP_CERR_DOCUMENT_FORMAT:
104355b4669Sjacobs 		return (PAPI_DOCUMENT_FORMAT);
105355b4669Sjacobs 	case IPP_CERR_ATTRIBUTES:
106355b4669Sjacobs 		return (PAPI_ATTRIBUTES);
107355b4669Sjacobs 	case IPP_CERR_URI_SCHEME:
108355b4669Sjacobs 		return (PAPI_URI_SCHEME);
109355b4669Sjacobs 	case IPP_CERR_CHARSET:
110355b4669Sjacobs 		return (PAPI_CHARSET);
111355b4669Sjacobs 	case IPP_CERR_CONFLICT:
112355b4669Sjacobs 		return (PAPI_CONFLICT);
113355b4669Sjacobs 	case IPP_CERR_COMPRESSION_NOT_SUPPORTED:
114355b4669Sjacobs 		return (PAPI_COMPRESSION_NOT_SUPPORTED);
115355b4669Sjacobs 	case IPP_CERR_COMPRESSION_ERROR:
116355b4669Sjacobs 		return (PAPI_COMPRESSION_ERROR);
117355b4669Sjacobs 	case IPP_CERR_DOCUMENT_FORMAT_ERROR:
118355b4669Sjacobs 		return (PAPI_DOCUMENT_FORMAT_ERROR);
119355b4669Sjacobs 	case IPP_CERR_DOCUMENT_ACCESS_ERROR:
120355b4669Sjacobs 		return (PAPI_DOCUMENT_ACCESS_ERROR);
121355b4669Sjacobs 	case IPP_CERR_ATTRIBUTES_NOT_SETTABLE:
122355b4669Sjacobs 		return (PAPI_ATTRIBUTES_NOT_SETTABLE);
123355b4669Sjacobs 	case IPP_CERR_IGNORED_ALL_SUBSCRIPTIONS:
124355b4669Sjacobs 		return (PAPI_IGNORED_ALL_SUBSCRIPTIONS);
125355b4669Sjacobs 	case IPP_CERR_TOO_MANY_SUBSCRIPTIONS:
126355b4669Sjacobs 		return (PAPI_TOO_MANY_SUBSCRIPTIONS);
127355b4669Sjacobs 	case IPP_CERR_IGNORED_ALL_NOTIFICATIONS:
128355b4669Sjacobs 		return (PAPI_IGNORED_ALL_NOTIFICATIONS);
129355b4669Sjacobs 	case IPP_CERR_PRINT_SUPPORT_FILE_NOT_FOUND:
130355b4669Sjacobs 		return (PAPI_PRINT_SUPPORT_FILE_NOT_FOUND);
131355b4669Sjacobs 	case IPP_SERR_INTERNAL:
132355b4669Sjacobs 		return (PAPI_INTERNAL_ERROR);
133355b4669Sjacobs 	case IPP_SERR_OPERATION_NOT_SUPPORTED:
134355b4669Sjacobs 		return (PAPI_OPERATION_NOT_SUPPORTED);
135355b4669Sjacobs 	case IPP_SERR_SERVICE_UNAVAILABLE:
136355b4669Sjacobs 		return (PAPI_SERVICE_UNAVAILABLE);
137355b4669Sjacobs 	case IPP_SERR_VERSION_NOT_SUPPORTED:
138355b4669Sjacobs 		return (PAPI_VERSION_NOT_SUPPORTED);
139355b4669Sjacobs 	case IPP_SERR_DEVICE_ERROR:
140355b4669Sjacobs 		return (PAPI_DEVICE_ERROR);
141355b4669Sjacobs 	case IPP_SERR_TEMPORARY_ERROR:
142355b4669Sjacobs 		return (PAPI_TEMPORARY_ERROR);
143355b4669Sjacobs 	case IPP_SERR_NOT_ACCEPTING:
144355b4669Sjacobs 		return (PAPI_NOT_ACCEPTING);
145355b4669Sjacobs 	case IPP_SERR_BUSY:
146355b4669Sjacobs 	case IPP_SERR_CANCELLED:
147355b4669Sjacobs 	default:
148355b4669Sjacobs 		return (PAPI_TEMPORARY_ERROR);
149355b4669Sjacobs 	}
150355b4669Sjacobs }
151355b4669Sjacobs 
152355b4669Sjacobs void
153355b4669Sjacobs ipp_initialize_request(service_t *svc, papi_attribute_t ***request,
154355b4669Sjacobs 		uint16_t operation)
155355b4669Sjacobs {
156355b4669Sjacobs 	papiAttributeListAddInteger(request, PAPI_ATTR_EXCL,
157355b4669Sjacobs 	    "version-major", 1);
158355b4669Sjacobs 	papiAttributeListAddInteger(request, PAPI_ATTR_EXCL,
159355b4669Sjacobs 	    "version-minor", 1);
160355b4669Sjacobs 	papiAttributeListAddInteger(request, PAPI_ATTR_EXCL,
161355b4669Sjacobs 	    "request-id", (short)lrand48());
162355b4669Sjacobs 	papiAttributeListAddInteger(request, PAPI_ATTR_EXCL,
163355b4669Sjacobs 	    "operation-id", operation);
164355b4669Sjacobs }
165355b4669Sjacobs 
166355b4669Sjacobs void
167355b4669Sjacobs ipp_initialize_operational_attributes(service_t *svc, papi_attribute_t ***op,
168223f6c28Sjacobs 		char *printer, int job_id)
169355b4669Sjacobs {
170355b4669Sjacobs 	char *charset = "utf-8"; /* default to UTF-8 encoding */
171355b4669Sjacobs 	char *language = setlocale(LC_ALL, "");
172355b4669Sjacobs 	char *user = "nobody";
173355b4669Sjacobs 	struct passwd *pw = NULL;
174355b4669Sjacobs 
175355b4669Sjacobs 	/*
176355b4669Sjacobs 	 * All IPP requests must contain the following:
177355b4669Sjacobs 	 * 	attributes-charset		(UTF-8)
178355b4669Sjacobs 	 *	attributes-natural-language	(our current locale)
179223f6c28Sjacobs 	 *	(object identifier)		printer-uri/job-id or job-uri
180223f6c28Sjacobs 	 *	requesting-user-name		(process user or none)
181355b4669Sjacobs 	 */
182355b4669Sjacobs 	papiAttributeListAddString(op, PAPI_ATTR_EXCL,
183355b4669Sjacobs 	    "attributes-charset", charset);
184355b4669Sjacobs 
185355b4669Sjacobs 	papiAttributeListAddString(op, PAPI_ATTR_EXCL,
186355b4669Sjacobs 	    "attributes-natural-language", language);
187355b4669Sjacobs 
188223f6c28Sjacobs 	if (printer != NULL)
189223f6c28Sjacobs 		ipp_add_printer_uri(svc, printer, op);
190223f6c28Sjacobs 
191223f6c28Sjacobs 	if ((printer != NULL) && (job_id >= 0))
192223f6c28Sjacobs 		papiAttributeListAddInteger(op, PAPI_ATTR_EXCL,
193223f6c28Sjacobs 		    "job-id", job_id);
194223f6c28Sjacobs 
195355b4669Sjacobs 	if ((pw = getpwuid(getuid())) != NULL)
196355b4669Sjacobs 		user = pw->pw_name;
197355b4669Sjacobs 	/*
198355b4669Sjacobs 	 * if our euid is 0 "super user", we will allow the system supplied
199355b4669Sjacobs 	 * user name to be overridden, if the requestor wants to.
200355b4669Sjacobs 	 */
201355b4669Sjacobs 	if (geteuid() == 0) {
202355b4669Sjacobs 		if (svc->user != NULL)
203355b4669Sjacobs 			user = svc->user;
204355b4669Sjacobs 	}
205355b4669Sjacobs 	papiAttributeListAddString(op, PAPI_ATTR_REPLACE,
206355b4669Sjacobs 	    "requesting-user-name", user);
207355b4669Sjacobs }
208355b4669Sjacobs 
209355b4669Sjacobs #ifndef OPID_CUPS_GET_DEFAULT	   /* for servers that will enumerate */
210355b4669Sjacobs #define	OPID_CUPS_GET_DEFAULT	   0x4001
211355b4669Sjacobs #endif  /* OPID_CUPS_GET_DEFAULT */
212355b4669Sjacobs 
213355b4669Sjacobs static papi_status_t
214355b4669Sjacobs _default_destination(service_t *svc, char **uri)
215355b4669Sjacobs {
216355b4669Sjacobs 	papi_status_t result = PAPI_INTERNAL_ERROR;
217355b4669Sjacobs 	printer_t *p = NULL;
218355b4669Sjacobs 	papi_attribute_t **request = NULL, **op = NULL, **response = NULL;
219355b4669Sjacobs 	char *tmp = NULL;
220355b4669Sjacobs 
221355b4669Sjacobs 	if ((svc == NULL) || (uri == NULL))
222355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
223355b4669Sjacobs 
224355b4669Sjacobs 	/* we must be connected to find the default destination */
225355b4669Sjacobs 	if (svc->connection == NULL)
226355b4669Sjacobs 		return (PAPI_NOT_POSSIBLE);
227355b4669Sjacobs 
228355b4669Sjacobs 	if ((p = calloc(1, sizeof (*p))) == NULL)
229355b4669Sjacobs 		return (PAPI_TEMPORARY_ERROR);
230355b4669Sjacobs 
231355b4669Sjacobs 	ipp_initialize_request(svc, &request, OPID_CUPS_GET_DEFAULT);
232223f6c28Sjacobs 	ipp_initialize_operational_attributes(svc, &op, NULL, -1);
233355b4669Sjacobs 	papiAttributeListAddString(&op, PAPI_ATTR_APPEND,
234355b4669Sjacobs 	    "requested-attributes", "printer-uri-supported");
235355b4669Sjacobs 	papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE,
236355b4669Sjacobs 	    "operational-attributes-group", op);
237355b4669Sjacobs 	papiAttributeListFree(op);
238355b4669Sjacobs 	result = ipp_send_request(svc, request, &response);
239355b4669Sjacobs 	papiAttributeListFree(request);
240355b4669Sjacobs 
241355b4669Sjacobs 	op = NULL;
242355b4669Sjacobs 	papiAttributeListGetCollection(response, NULL,
243355b4669Sjacobs 	    "printer-attributes-group", &op);
244355b4669Sjacobs 
245355b4669Sjacobs 	if (uri != NULL) {
246355b4669Sjacobs 		char *tmp = NULL;
247355b4669Sjacobs 
248355b4669Sjacobs 		papiAttributeListGetString(op, NULL, "printer-uri", &tmp);
249355b4669Sjacobs 		papiAttributeListGetString(op, NULL,
250355b4669Sjacobs 		    "printer-uri-supported", &tmp);
251355b4669Sjacobs 		if (tmp != NULL)
252355b4669Sjacobs 			*uri = strdup(tmp);
253355b4669Sjacobs 	}
254355b4669Sjacobs 
255355b4669Sjacobs 	papiAttributeListFree(response);
256355b4669Sjacobs 
257355b4669Sjacobs 	return (result);
258355b4669Sjacobs }
259355b4669Sjacobs 
260223f6c28Sjacobs static void
261355b4669Sjacobs ipp_add_printer_uri(service_t *svc, char *name, papi_attribute_t ***op)
262355b4669Sjacobs {
263355b4669Sjacobs 	char *uri = name;
264355b4669Sjacobs 	char buf[BUFSIZ];
265355b4669Sjacobs 	uri_t *tmp = NULL;
266355b4669Sjacobs 
267355b4669Sjacobs 	if (strstr(name, "://") == NULL) { /* not in URI form */
268355b4669Sjacobs 		if (strcmp(name, DEFAULT_DEST) != 0) {
269355b4669Sjacobs 			/* not the "default" */
270355b4669Sjacobs 			snprintf(buf, sizeof (buf), "%s/%s", svc->name, name);
271355b4669Sjacobs 			uri = buf;
272355b4669Sjacobs 		} else
273355b4669Sjacobs 			_default_destination(svc, &uri);
274355b4669Sjacobs 	}
275355b4669Sjacobs 
276355b4669Sjacobs 	papiAttributeListAddString(op, PAPI_ATTR_EXCL, "printer-uri", uri);
277355b4669Sjacobs 
278355b4669Sjacobs 	/* save the printer-uri's path to be used by http POST request */
279355b4669Sjacobs 	if ((uri_from_string(uri, &tmp) == 0) && (tmp != NULL)) {
280355b4669Sjacobs 		if (svc->post != NULL)
281355b4669Sjacobs 			free(svc->post);
282355b4669Sjacobs 		svc->post = strdup(tmp->path);
283355b4669Sjacobs 		uri_free(tmp);
284355b4669Sjacobs 	}
285355b4669Sjacobs }
286355b4669Sjacobs 
287355b4669Sjacobs 
288355b4669Sjacobs /*
289355b4669Sjacobs  * don't actually write anything, just add to the total size and return the
290355b4669Sjacobs  * size of what would be written, so we can figure out how big the request
291355b4669Sjacobs  * is going to be.
292355b4669Sjacobs  */
293355b4669Sjacobs static ssize_t
294355b4669Sjacobs size_calculate(void *fd, void *buffer, size_t length)
295355b4669Sjacobs {
296355b4669Sjacobs 	ssize_t *size = (ssize_t *)fd;
297355b4669Sjacobs 
298355b4669Sjacobs 	*size += length;
299355b4669Sjacobs 	return (length);
300355b4669Sjacobs }
301355b4669Sjacobs 
302355b4669Sjacobs 
303355b4669Sjacobs static ssize_t
304355b4669Sjacobs build_chunk(void *fd, void *buffer, size_t length)
305355b4669Sjacobs {
306355b4669Sjacobs 	char **s1 = fd;
307355b4669Sjacobs 
308355b4669Sjacobs 	memcpy(*s1, buffer, length);
309355b4669Sjacobs 	*s1 = *s1 + length;
310355b4669Sjacobs 
311355b4669Sjacobs 	return (length);
312355b4669Sjacobs }
313355b4669Sjacobs 
314355b4669Sjacobs ssize_t
315355b4669Sjacobs ipp_request_write(void *fd, void *buffer, size_t length)
316355b4669Sjacobs {
317355b4669Sjacobs 	service_t *svc = (service_t *)fd;
318355b4669Sjacobs 
319355b4669Sjacobs #ifdef DEBUG
320355b4669Sjacobs 	printf("ipp_request_write(0x%8.8x, 0x%8.8x, %d)\n", fd, buffer, length);
321355b4669Sjacobs 	httpDumpData(stdout, "ipp_request_write:", buffer, length);
322355b4669Sjacobs #endif
323355b4669Sjacobs 	return (httpWrite(svc->connection, buffer, length));
324355b4669Sjacobs }
325355b4669Sjacobs 
326355b4669Sjacobs ssize_t
327355b4669Sjacobs ipp_request_read(void *fd, void *buffer, size_t length)
328355b4669Sjacobs {
329355b4669Sjacobs 	service_t *svc = (service_t *)fd;
330355b4669Sjacobs 	ssize_t rc, i = length;
331355b4669Sjacobs 	char *p = buffer;
332355b4669Sjacobs 
333355b4669Sjacobs 	while ((rc = httpRead(svc->connection, p, i)) != i) {
334355b4669Sjacobs 		if (rc == 0)
335355b4669Sjacobs 			return (rc);
336355b4669Sjacobs 		if (rc < 0)
337355b4669Sjacobs 			return (rc);
338355b4669Sjacobs 		i -= rc;
339355b4669Sjacobs 		p += rc;
340355b4669Sjacobs 	}
341355b4669Sjacobs #ifdef DEBUG
342355b4669Sjacobs 	printf("ipp_request_read(0x%8.8x, 0x%8.8x, %d) = %d\n",
343355b4669Sjacobs 	    fd, buffer, length, rc);
344355b4669Sjacobs 	httpDumpData(stdout, "ipp_request_read:", buffer, length);
345355b4669Sjacobs #endif
346355b4669Sjacobs 
347355b4669Sjacobs 	return (length);
348355b4669Sjacobs }
349355b4669Sjacobs 
350355b4669Sjacobs papi_status_t
351355b4669Sjacobs ipp_send_initial_request_block(service_t *svc, papi_attribute_t **request,
352355b4669Sjacobs 		ssize_t file_size)
353355b4669Sjacobs {
354355b4669Sjacobs 	papi_status_t result = PAPI_OK;
355355b4669Sjacobs 	ssize_t chunk_size = 0;
356355b4669Sjacobs 	char length[32];
357355b4669Sjacobs 	void *chunk, *ptr;
358355b4669Sjacobs 	http_status_t status;
359355b4669Sjacobs 
360355b4669Sjacobs 	/* calculate the request size */
361355b4669Sjacobs 	(void) ipp_write_message(&size_calculate, &chunk_size, request);
362355b4669Sjacobs 
363355b4669Sjacobs 	/* Fill in the HTTP Header information */
364355b4669Sjacobs 	httpClearFields(svc->connection);
365355b4669Sjacobs 	if (svc->transfer_encoding == TRANSFER_ENCODING_CHUNKED)
366355b4669Sjacobs 		httpSetField(svc->connection, HTTP_FIELD_TRANSFER_ENCODING,
367355b4669Sjacobs 		    "chunked");
368355b4669Sjacobs 	else {
369355b4669Sjacobs 		sprintf(length, "%lu", (unsigned long)(file_size + chunk_size));
370355b4669Sjacobs 		httpSetField(svc->connection, HTTP_FIELD_CONTENT_LENGTH,
371355b4669Sjacobs 		    length);
372355b4669Sjacobs 	}
373355b4669Sjacobs 	httpSetField(svc->connection, HTTP_FIELD_CONTENT_TYPE,
374355b4669Sjacobs 	    "application/ipp");
375355b4669Sjacobs 	httpSetField(svc->connection, HTTP_FIELD_AUTHORIZATION,
376355b4669Sjacobs 	    svc->connection->authstring);
377355b4669Sjacobs 
378355b4669Sjacobs 	/* flush any state information about this connection */
379355b4669Sjacobs 	httpFlush(svc->connection);
380355b4669Sjacobs 
381355b4669Sjacobs 	/* if we have don't have a POST path, use the service uri path */
382*e059026eSKeerthi Kondaka 	if ((svc->post == NULL) && (svc->uri->path))
383355b4669Sjacobs 		svc->post = strdup(svc->uri->path);
384355b4669Sjacobs 	/* send the HTTP POST message for the IPP request */
385355b4669Sjacobs 	/* if the POST fails, return the error */
386355b4669Sjacobs 	status = httpPost(svc->connection, svc->post);
387355b4669Sjacobs 	if (status != 0)
388355b4669Sjacobs 		return (http_to_papi_status(status));
389355b4669Sjacobs 
390355b4669Sjacobs 	if (httpCheck(svc->connection) != 0) {
391355b4669Sjacobs 		status = httpUpdate(svc->connection);
392355b4669Sjacobs 		if (status != HTTP_OK)
393355b4669Sjacobs 			return (http_to_papi_status(status));
394355b4669Sjacobs 	}
395355b4669Sjacobs 
396355b4669Sjacobs 	/* build the request chunk */
397355b4669Sjacobs 	chunk = ptr = calloc(1, chunk_size);
398355b4669Sjacobs 	result = ipp_write_message(&build_chunk, &ptr, request);
399355b4669Sjacobs #ifdef DEBUG
400355b4669Sjacobs 	printf("request: %d (0x%x) bytes\n", chunk_size, chunk_size);
401355b4669Sjacobs 	httpDumpData(stdout, "request:", chunk, chunk_size);
402355b4669Sjacobs #endif
403355b4669Sjacobs 
404355b4669Sjacobs 	/* send the actual IPP request */
405355b4669Sjacobs 	if (ipp_request_write(svc, chunk, chunk_size) != chunk_size)
406355b4669Sjacobs 		result = PAPI_TEMPORARY_ERROR;
407355b4669Sjacobs 	free(chunk);
408355b4669Sjacobs 
409355b4669Sjacobs 	if (httpCheck(svc->connection) != 0) {
410355b4669Sjacobs 		status = httpUpdate(svc->connection);
411355b4669Sjacobs 		if (status != HTTP_OK)
412355b4669Sjacobs 			return (http_to_papi_status(status));
413355b4669Sjacobs 	}
414355b4669Sjacobs 
415355b4669Sjacobs 	return (result);
416355b4669Sjacobs }
417355b4669Sjacobs 
418355b4669Sjacobs static int
419355b4669Sjacobs setAuthString(service_t *svc)
420355b4669Sjacobs {
421355b4669Sjacobs 	http_t *http;
422355b4669Sjacobs 	char *user, *passphrase;
423355b4669Sjacobs 	char encoded[BUFSIZ];
424355b4669Sjacobs 
425355b4669Sjacobs 	if ((svc == NULL) || (svc->connection == NULL) || (svc->name == NULL))
426355b4669Sjacobs 		return (-1);
427355b4669Sjacobs 
428355b4669Sjacobs 	http = svc->connection;
429355b4669Sjacobs 
430355b4669Sjacobs 	if (svc->user == NULL) {
431355b4669Sjacobs 		struct passwd *p;
432355b4669Sjacobs 
433355b4669Sjacobs 		if ((p = getpwuid(getuid())) != NULL) {
434355b4669Sjacobs 			user = p->pw_name;
435355b4669Sjacobs 		} else if ((user = getenv("LOGNAME")) == NULL)
436355b4669Sjacobs 			user = getenv("USER");
437355b4669Sjacobs 		if (user == NULL)
438355b4669Sjacobs 			user = "nobody";
439355b4669Sjacobs 	} else
440355b4669Sjacobs 		user = svc->user;
441355b4669Sjacobs 
442355b4669Sjacobs 	/* if the passphrase is not set, use the Authentication Callback */
443355b4669Sjacobs 	if (((svc->password == NULL) || (svc->password[0] == '\0')) &&
444355b4669Sjacobs 	    (svc->authCB != NULL))
445355b4669Sjacobs 		(svc->authCB)(svc, svc->app_data);
446355b4669Sjacobs 	passphrase = svc->password;
447355b4669Sjacobs 
448355b4669Sjacobs 	/* if there is still no passphrase, we have to fail */
449355b4669Sjacobs 	if ((passphrase == NULL) || (passphrase[0] == '\0'))
450355b4669Sjacobs 		return (-1);
451355b4669Sjacobs 
452355b4669Sjacobs 	if (strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE],
453355b4669Sjacobs 	    "Basic", 5) == 0) {
454355b4669Sjacobs 		char plain[BUFSIZ];
455355b4669Sjacobs 
456355b4669Sjacobs 		snprintf(plain, sizeof (plain), "%s:%s", user, passphrase);
457355b4669Sjacobs 		httpEncode64(encoded, plain);
458355b4669Sjacobs 		snprintf(http->authstring, sizeof (http->authstring),
459355b4669Sjacobs 		    "Basic %s", encoded);
460355b4669Sjacobs 	} else if (strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE],
461355b4669Sjacobs 	    "Digest", 6) == 0) {
462355b4669Sjacobs 		char realm[HTTP_MAX_VALUE];
463355b4669Sjacobs 		char nonce[HTTP_MAX_VALUE];
464355b4669Sjacobs 		char line [BUFSIZ];
465355b4669Sjacobs 		char urp[128];
466355b4669Sjacobs 		char mr[128];
467355b4669Sjacobs 		char *uri = svc->post;
468355b4669Sjacobs 
469355b4669Sjacobs 		httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE,
470355b4669Sjacobs 		    "realm", realm);
471355b4669Sjacobs 		httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE,
472355b4669Sjacobs 		    "nonce", nonce);
473355b4669Sjacobs 
474355b4669Sjacobs 		snprintf(line, sizeof (line), "%s:%s:%s", user, realm,
475355b4669Sjacobs 		    passphrase);
476355b4669Sjacobs 		md5_calc(urp, line, strlen(line));
477355b4669Sjacobs 
478355b4669Sjacobs 		snprintf(line, sizeof (line), "POST:%s", uri);
479355b4669Sjacobs 		md5_calc(mr, line, strlen(line));
480355b4669Sjacobs 
481355b4669Sjacobs 		snprintf(line, sizeof (line), "%s:%s:%s", urp, mr, nonce);
482355b4669Sjacobs 		md5_calc(encoded, line, strlen(line));
483355b4669Sjacobs 
484355b4669Sjacobs 		snprintf(http->authstring, sizeof (http->authstring),
485355b4669Sjacobs 		    "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", "
486355b4669Sjacobs 		    "uri=\"%s\", response=\"%s\"", user, realm, nonce, uri,
487355b4669Sjacobs 		    encoded);
488355b4669Sjacobs 	}
489355b4669Sjacobs 
490355b4669Sjacobs 	return (0);
491355b4669Sjacobs }
492355b4669Sjacobs 
493355b4669Sjacobs papi_status_t
494355b4669Sjacobs ipp_status_info(service_t *svc, papi_attribute_t **response)
495355b4669Sjacobs {
496355b4669Sjacobs 	papi_attribute_t **operational = NULL;
497355b4669Sjacobs 	int32_t status = 0;
498355b4669Sjacobs 
499355b4669Sjacobs 	papiAttributeListGetCollection(response, NULL,
500355b4669Sjacobs 	    "operational-attributes-group", &operational);
501355b4669Sjacobs 	if (operational != NULL) {
502355b4669Sjacobs 		char *message = NULL;
503355b4669Sjacobs 
504355b4669Sjacobs 		papiAttributeListGetString(response, NULL,
505355b4669Sjacobs 		    "status-message", &message);
506355b4669Sjacobs 		papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
507355b4669Sjacobs 		    "detailed-status-message", message);
508355b4669Sjacobs 	}
509355b4669Sjacobs 	papiAttributeListGetInteger(response, NULL, "status-code", &status);
510355b4669Sjacobs 
511355b4669Sjacobs 	return (ipp_to_papi_status(status));
512355b4669Sjacobs }
513355b4669Sjacobs 
514355b4669Sjacobs papi_status_t
515355b4669Sjacobs ipp_send_request_with_file(service_t *svc, papi_attribute_t **request,
516355b4669Sjacobs 		papi_attribute_t ***response, char *file)
517355b4669Sjacobs {
518355b4669Sjacobs 	papi_status_t result = PAPI_OK;
519355b4669Sjacobs 	ssize_t size = 0;
52095c2d302SJonathan Cowper-Andrewes 	struct stat statbuf;
521355b4669Sjacobs 	int fd;
522355b4669Sjacobs 
523355b4669Sjacobs #ifdef DEBUG
524355b4669Sjacobs 	fprintf(stderr, "\nIPP-REQUEST: (%s)", (file ? file : ""));
525355b4669Sjacobs 	papiAttributeListPrint(stderr, request, "    ");
526355b4669Sjacobs 	putc('\n', stderr);
527355b4669Sjacobs 	fflush(stderr);
528355b4669Sjacobs #endif
529355b4669Sjacobs 
530355b4669Sjacobs 	/*
531355b4669Sjacobs 	 * if we are sending a file, open it and include it's size in the
532355b4669Sjacobs 	 * message size.
533355b4669Sjacobs 	 */
534355b4669Sjacobs 	if (file != NULL) {
535355b4669Sjacobs 		if ((fd = open(file, O_RDONLY)) < 0) {
536355b4669Sjacobs 			detailed_error(svc, "%s: %s", file, strerror(errno));
537355b4669Sjacobs 			return (PAPI_DOCUMENT_ACCESS_ERROR);
53895c2d302SJonathan Cowper-Andrewes 		} else if (strcmp("standard input", file) != 0) {
539a5669307SJonathan Cowper-Andrewes 			if (stat(file, &statbuf) < 0) {
540a5669307SJonathan Cowper-Andrewes 				detailed_error(svc,
541a5669307SJonathan Cowper-Andrewes 				    gettext("Cannot access file: %s: %s"),
542a5669307SJonathan Cowper-Andrewes 				    file, strerror(errno));
543a5669307SJonathan Cowper-Andrewes 				return (PAPI_DOCUMENT_ACCESS_ERROR);
544a5669307SJonathan Cowper-Andrewes 			}
54595c2d302SJonathan Cowper-Andrewes 			if (statbuf.st_size == 0) {
54695c2d302SJonathan Cowper-Andrewes 				detailed_error(svc,
54795c2d302SJonathan Cowper-Andrewes 				    "Zero byte (empty) file: %s", file);
54895c2d302SJonathan Cowper-Andrewes 				return (PAPI_BAD_ARGUMENT);
54995c2d302SJonathan Cowper-Andrewes 			}
550355b4669Sjacobs 		} else if (svc->transfer_encoding !=
551355b4669Sjacobs 		    TRANSFER_ENCODING_CHUNKED) {
552355b4669Sjacobs 			struct stat st;
553355b4669Sjacobs 
554355b4669Sjacobs 			if (fstat(fd, &st) >= 0)
555355b4669Sjacobs 				size = st.st_size;
556355b4669Sjacobs 		}
557355b4669Sjacobs 	}
558355b4669Sjacobs 
559355b4669Sjacobs 	*response = NULL;
560355b4669Sjacobs 	while (*response == NULL) {
561355b4669Sjacobs 		http_status_t status = HTTP_CONTINUE;
562355b4669Sjacobs 
563355b4669Sjacobs 		result = ipp_send_initial_request_block(svc, request, size);
564355b4669Sjacobs 
565355b4669Sjacobs 		if (result == PAPI_OK) {
566355b4669Sjacobs 			if (file != NULL) {
567355b4669Sjacobs 				/* send the file contents if we have it */
568355b4669Sjacobs 				int rc;
569355b4669Sjacobs 				char buf[BUFSIZ];
570355b4669Sjacobs 
571355b4669Sjacobs 				lseek(fd, 0L, SEEK_SET);
572355b4669Sjacobs 				while ((rc = read(fd, buf, sizeof (buf))) > 0) {
573355b4669Sjacobs 					if (ipp_request_write(svc, buf, rc)
574355b4669Sjacobs 					    < rc) {
575355b4669Sjacobs 						break;
576355b4669Sjacobs 					}
577355b4669Sjacobs 				}
578355b4669Sjacobs 			}
579355b4669Sjacobs 
580355b4669Sjacobs 			(void) ipp_request_write(svc, "", 0);
581355b4669Sjacobs 		}
582355b4669Sjacobs 
583355b4669Sjacobs 		/* update our connection info */
584355b4669Sjacobs 		while (status == HTTP_CONTINUE)
585355b4669Sjacobs 			status = httpUpdate(svc->connection);
586355b4669Sjacobs 
587355b4669Sjacobs 		if (status == HTTP_UNAUTHORIZED) {
588355b4669Sjacobs 			httpFlush(svc->connection);
589355b4669Sjacobs 			if ((svc->connection->authstring[0] == '\0') &&
590355b4669Sjacobs 			    (setAuthString(svc) == 0)) {
591355b4669Sjacobs 				httpReconnect(svc->connection);
592355b4669Sjacobs 				continue;
593355b4669Sjacobs 			}
594355b4669Sjacobs 		} else if (status == HTTP_UPGRADE_REQUIRED) {
595355b4669Sjacobs 			/*
596355b4669Sjacobs 			 * If the transport was built with TLS support, we can
597355b4669Sjacobs 			 * try to use it.
598355b4669Sjacobs 			 */
599355b4669Sjacobs 			httpFlush(svc->connection);
600355b4669Sjacobs 			httpReconnect(svc->connection);
601355b4669Sjacobs 			httpEncryption(svc->connection, HTTP_ENCRYPT_REQUIRED);
602355b4669Sjacobs 			continue;
603355b4669Sjacobs 		}
604355b4669Sjacobs 
605355b4669Sjacobs 		if (status != HTTP_OK)
606355b4669Sjacobs 			return (http_to_papi_status(status));
607355b4669Sjacobs 
608355b4669Sjacobs 		/* read the IPP response */
609355b4669Sjacobs 		result = ipp_read_message(&ipp_request_read, svc, response,
610355b4669Sjacobs 		    IPP_TYPE_RESPONSE);
611355b4669Sjacobs 
612355b4669Sjacobs 		if (result == PAPI_OK)
613355b4669Sjacobs 			result = ipp_status_info(svc, *response);
614355b4669Sjacobs #ifdef DEBUG
615355b4669Sjacobs 		fprintf(stderr, "\nIPP-RESPONSE: (%s) (%s)", (file ? file : ""),
616355b4669Sjacobs 		    papiStatusString(result));
617355b4669Sjacobs 		papiAttributeListPrint(stderr, *response, "    ");
618355b4669Sjacobs 		putc('\n', stderr);
619355b4669Sjacobs 		fflush(stderr);
620355b4669Sjacobs #endif
621355b4669Sjacobs 	}
622355b4669Sjacobs 
623355b4669Sjacobs 	return (result);
624355b4669Sjacobs }
625355b4669Sjacobs 
626355b4669Sjacobs papi_status_t
627355b4669Sjacobs ipp_send_request(service_t *svc, papi_attribute_t **request,
628355b4669Sjacobs 		papi_attribute_t ***response)
629355b4669Sjacobs {
630355b4669Sjacobs 	return (ipp_send_request_with_file(svc, request, response, NULL));
631355b4669Sjacobs }
632