4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 /* $Id: service.c 171 2006-05-20 06:00:32Z njacobs $ */
30 #pragma ident "%Z%%M% %I% %E% SMI"
40 #include <papi_impl.h>
42 #include <config-site.h>
45 http_encryption_type(papi_encryption_t encryption
)
48 case PAPI_ENCRYPT_IF_REQUESTED
:
49 return (HTTP_ENCRYPT_IF_REQUESTED
);
50 case PAPI_ENCRYPT_REQUIRED
:
51 return (HTTP_ENCRYPT_REQUIRED
);
52 case PAPI_ENCRYPT_ALWAYS
:
53 return (HTTP_ENCRYPT_ALWAYS
);
54 case PAPI_ENCRYPT_NEVER
:
55 return (HTTP_ENCRYPT_NEVER
);
57 ; /* this should log an error */
60 return (HTTP_ENCRYPT_NEVER
); /* should never get here */
64 service_connect(service_t
*svc
, char *service_name
)
66 papi_status_t result
= PAPI_OK
;
70 return (PAPI_BAD_ARGUMENT
);
72 if (svc
->connection
!= NULL
) /* alread connected ? */
76 uri_from_string(service_name
, &svc
->uri
);
78 if ((service_name
!= NULL
) && (svc
->uri
== NULL
)) {
80 * a name was supplied and it's not in URI form, we will
81 * try to use a "default" IPP service under the assumption
82 * that this is most likely a short-form printer name from
83 * from a papiPrinter*() or papiJob*() call and not from a
84 * papiServiceCreate() call.
86 if ((service_name
= getenv("PAPI_SERVICE_URI")) == NULL
) {
89 if ((cups
= getenv("CUPS_SERVER")) != NULL
) {
92 snprintf(buf
, sizeof (buf
),
93 "ipp://%s/printers/", cups
);
94 service_name
= strdup(buf
);
97 if (service_name
== NULL
)
98 service_name
= DEFAULT_IPP_SERVICE_URI
;
100 uri_from_string(service_name
, &svc
->uri
);
103 if (svc
->uri
== NULL
)
104 return (PAPI_NOT_POSSIBLE
);
106 if (svc
->uri
->port
!= NULL
)
107 port
= strtol(svc
->uri
->port
, NULL
, 10);
109 svc
->connection
= httpConnectEncrypt(svc
->uri
->host
, port
,
110 http_encryption_type(svc
->encryption
));
111 if (svc
->connection
== NULL
) {
112 if (svc
->uri
!= NULL
) {
116 result
= PAPI_SERVICE_UNAVAILABLE
;
117 } else if (service_name
!= NULL
)
118 svc
->name
= strdup(service_name
);
124 papiServiceCreate(papi_service_t
*handle
, char *service_name
,
125 char *user_name
, char *password
,
126 int (*authCB
)(papi_service_t svc
, void *app_data
),
127 papi_encryption_t encryption
, void *app_data
)
129 papi_status_t result
= PAPI_NOT_POSSIBLE
;
130 service_t
*svc
= NULL
;
131 char *encoding
= getenv("HTTP_TRANSFER_ENCODING");
134 return (PAPI_BAD_ARGUMENT
);
136 if ((*handle
= svc
= calloc(1, sizeof (*svc
))) == NULL
)
137 return (PAPI_TEMPORARY_ERROR
);
139 if (user_name
!= NULL
)
140 svc
->user
= strdup(user_name
);
142 if (password
!= NULL
)
143 svc
->password
= strdup(password
);
145 svc
->encryption
= encryption
;
148 svc
->authCB
= authCB
;
150 if (app_data
!= NULL
)
151 svc
->app_data
= app_data
;
153 if ((encoding
!= NULL
) && (strcasecmp(encoding
, "content-length") == 0))
154 svc
->transfer_encoding
= TRANSFER_ENCODING_LENGTH
;
156 svc
->transfer_encoding
= TRANSFER_ENCODING_CHUNKED
;
158 if (service_name
!= NULL
) {
159 result
= service_connect(svc
, service_name
);
167 papiServiceDestroy(papi_service_t handle
)
169 if (handle
!= NULL
) {
170 service_t
*svc
= handle
;
172 if (svc
->attributes
!= NULL
)
173 papiAttributeListFree(svc
->attributes
);
174 if (svc
->name
!= NULL
)
176 if (svc
->user
!= NULL
)
178 if (svc
->password
!= NULL
)
180 if (svc
->uri
!= NULL
)
182 if (svc
->post
!= NULL
)
184 if (svc
->connection
!= NULL
)
185 httpClose(svc
->connection
);
192 papiServiceSetUserName(papi_service_t handle
, char *user_name
)
194 papi_status_t result
= PAPI_OK
;
196 if (handle
!= NULL
) {
197 service_t
*svc
= handle
;
199 if (svc
->user
!= NULL
)
202 if (user_name
!= NULL
)
203 svc
->user
= strdup(user_name
);
205 result
= PAPI_BAD_ARGUMENT
;
211 papiServiceSetPassword(papi_service_t handle
, char *password
)
213 papi_status_t result
= PAPI_OK
;
215 if (handle
!= NULL
) {
216 service_t
*svc
= handle
;
218 if (svc
->password
!= NULL
)
220 svc
->password
= NULL
;
221 if (password
!= NULL
)
222 svc
->password
= strdup(password
);
224 result
= PAPI_BAD_ARGUMENT
;
230 papiServiceSetEncryption(papi_service_t handle
,
231 papi_encryption_t encryption
)
233 papi_status_t result
= PAPI_OK
;
235 if (handle
!= NULL
) {
236 service_t
*svc
= handle
;
238 svc
->encryption
= encryption
;
239 httpEncryption(svc
->connection
,
240 (http_encryption_t
)svc
->encryption
);
242 result
= PAPI_BAD_ARGUMENT
;
248 papiServiceSetAuthCB(papi_service_t handle
,
249 int (*authCB
)(papi_service_t svc
, void *app_data
))
251 papi_status_t result
= PAPI_OK
;
253 if (handle
!= NULL
) {
254 service_t
*svc
= handle
;
256 svc
->authCB
= authCB
;
258 result
= PAPI_BAD_ARGUMENT
;
265 papiServiceSetAppData(papi_service_t handle
, void *app_data
)
267 papi_status_t result
= PAPI_OK
;
269 if (handle
!= NULL
) {
270 service_t
*svc
= handle
;
272 svc
->app_data
= (void *)app_data
;
274 result
= PAPI_BAD_ARGUMENT
;
280 papiServiceGetServiceName(papi_service_t handle
)
284 if (handle
!= NULL
) {
285 service_t
*svc
= handle
;
294 papiServiceGetUserName(papi_service_t handle
)
298 if (handle
!= NULL
) {
299 service_t
*svc
= handle
;
308 papiServiceGetPassword(papi_service_t handle
)
312 if (handle
!= NULL
) {
313 service_t
*svc
= handle
;
315 result
= svc
->password
;
322 papiServiceGetEncryption(papi_service_t handle
)
324 papi_encryption_t result
= PAPI_ENCRYPT_NEVER
;
326 if (handle
!= NULL
) {
327 service_t
*svc
= handle
;
329 result
= svc
->encryption
;
336 papiServiceGetAppData(papi_service_t handle
)
340 if (handle
!= NULL
) {
341 service_t
*svc
= handle
;
343 result
= svc
->app_data
;
350 papiServiceGetAttributeList(papi_service_t handle
)
352 papi_attribute_t
**result
= NULL
;
353 service_t
*svc
= handle
;
356 result
= svc
->attributes
;
362 papiServiceGetStatusMessage(papi_service_t handle
)
365 service_t
*svc
= handle
;
367 papiAttributeListGetString(svc
->attributes
, NULL
,
368 "detailed-status-message", &result
);
374 detailed_error(service_t
*svc
, char *fmt
, ...)
376 if ((svc
!= NULL
) && (fmt
!= NULL
)) {
379 char *message
= alloca(BUFSIZ
);
383 * fill in the message. If the buffer is too small, allocate
384 * one that is large enough and fill it in.
386 if ((size
= vsnprintf(message
, BUFSIZ
, fmt
, ap
)) >= BUFSIZ
)
387 if ((message
= alloca(size
)) != NULL
)
388 vsnprintf(message
, size
, fmt
, ap
);
391 papiAttributeListAddString(&svc
->attributes
, PAPI_ATTR_APPEND
,
392 "detailed-status-message", message
);