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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 /* $Id: service.c 172 2006-05-24 20:54:00Z njacobs $ */
30 #pragma ident "%Z%%M% %I% %E% SMI"
37 #include <sys/types.h>
42 #include <papi_impl.h>
43 #include <config-site.h>
46 interposed_auth_callback(papi_service_t handle
, void *app_data
)
49 service_t
*svc
= app_data
;
52 result
= svc
->authCB(svc
, svc
->app_data
);
58 default_service_uri(char *fallback
)
62 if (getuid() == geteuid())
63 result
= getenv("PAPI_SERVICE_URI");
68 if ((cups
= getenv("CUPS_SERVER")) != NULL
) {
71 snprintf(buf
, sizeof (buf
), "ipp://%s/printers/", cups
);
83 default_print_service()
85 static char *result
= NULL
;
88 char *service_uri
= default_service_uri(DEFAULT_SERVICE_URI
);
91 if (uri_from_string(service_uri
, &uri
) != -1)
92 result
= strdup(uri
->scheme
);
102 service_load(service_t
*svc
, char *name
)
104 papi_status_t result
;
105 char *scheme
= default_print_service();
107 if (svc
->so_handle
!= NULL
) /* already loaded */
110 if (name
== NULL
) /* no info, can't load yet */
113 /* Lookup the printer in the configuration DB */
114 svc
->attributes
= getprinterbyname((char *)name
, NULL
);
116 if (svc
->attributes
!= NULL
) {
119 /* Printer found (or was a URI), use the attribute data */
120 papiAttributeListGetString(svc
->attributes
, NULL
,
121 "printer-uri-supported", &tmp
);
123 svc
->name
= strdup(tmp
);
125 /* parse the URI and set the scheme(print service) */
126 if (uri_from_string(svc
->name
, &svc
->uri
) != -1)
127 scheme
= (svc
->uri
)->scheme
;
129 /* override the scheme if it was in the attributes */
130 papiAttributeListGetString(svc
->attributes
, NULL
,
131 "print-service-module", &scheme
);
133 } else /* not found, assume it is the actual print service name */
136 result
= psm_open(svc
, scheme
);
139 break; /* no error */
140 case PAPI_URI_SCHEME
:
141 result
= PAPI_NOT_FOUND
;
143 detailed_error(svc
, "Unable to load service for: %s", name
);
146 default: /* set the detailed message */
147 detailed_error(svc
, "Unable to load service (%s) for: %s",
155 service_send_peer(service_t
*svc
)
157 papi_status_t result
= PAPI_OK
;
159 if ((svc
->peer_fd
!= -1) && (svc
->so_handle
!= NULL
) &&
160 (svc
->svc_handle
!= NULL
)) {
161 papi_status_t (*f
)();
163 f
= (papi_status_t (*)())psm_sym(svc
, "papiServiceSetPeer");
166 result
= f(svc
->svc_handle
, svc
->peer_fd
);
173 service_connect(service_t
*svc
, char *name
)
175 papi_status_t result
= PAPI_NOT_POSSIBLE
;
177 /* if there is no print service module loaded, try and load one. */
178 if (svc
->so_handle
== NULL
)
179 result
= service_load(svc
, name
);
180 else if ((svc
->name
== NULL
) && (name
!= NULL
))
181 svc
->name
= strdup(name
);
184 * the print service module is loaded, but we don't have a service
187 if (svc
->so_handle
!= NULL
) {
188 papi_status_t (*f
)();
190 if (svc
->svc_handle
!= NULL
) /* already connected? */
193 f
= (papi_status_t (*)())psm_sym(svc
, "papiServiceCreate");
196 char *user
= svc
->user
;
197 char *password
= svc
->password
;
199 /* if no API user, try the URI user */
200 if ((user
== NULL
) && (svc
->uri
!= NULL
))
201 user
= (svc
->uri
)->user
;
202 /* if no API password, try the URI password */
203 if ((password
== NULL
) && (svc
->uri
!= NULL
))
204 password
= (svc
->uri
)->password
;
206 result
= f(&svc
->svc_handle
, svc
->name
, user
, password
,
207 (svc
->authCB
? interposed_auth_callback
209 svc
->encryption
, svc
);
210 (void) service_send_peer(svc
);
218 papiServiceCreate(papi_service_t
*handle
, char *service_name
, char *user_name
,
220 int (*authCB
)(papi_service_t svc
, void *app_data
),
221 papi_encryption_t encryption
, void *app_data
)
223 papi_status_t result
= PAPI_NOT_POSSIBLE
;
224 service_t
*svc
= NULL
;
228 return (PAPI_BAD_ARGUMENT
);
230 if ((*handle
= svc
= calloc(1, sizeof (*svc
))) == NULL
)
231 return (PAPI_TEMPORARY_ERROR
);
235 if (user_name
!= NULL
)
236 svc
->user
= strdup(user_name
);
238 if (password
!= NULL
)
239 svc
->password
= strdup(password
);
241 svc
->encryption
= encryption
;
244 svc
->authCB
= authCB
;
246 if (app_data
!= NULL
)
247 svc
->app_data
= app_data
;
249 /* If not specified, get a "default" service from the environment */
250 if (service_name
== NULL
)
251 service_name
= default_service_uri(NULL
);
253 if (service_name
!= NULL
) {
254 result
= service_load(svc
, service_name
);
255 /* if the psm loaded and the svc contains a URI, connect */
256 if ((result
== PAPI_OK
) && (svc
->uri
!= NULL
))
257 result
= service_connect(svc
, service_name
);
265 papiServiceDestroy(papi_service_t handle
)
267 if (handle
!= NULL
) {
268 service_t
*svc
= handle
;
270 if (svc
->so_handle
!= NULL
) {
271 if (svc
->svc_handle
!= NULL
) {
274 f
= (void (*)())psm_sym(svc
,
275 "papiServiceDestroy");
278 psm_close(svc
->so_handle
);
280 if (svc
->attributes
!= NULL
)
281 papiAttributeListFree(svc
->attributes
);
282 if (svc
->name
!= NULL
)
284 if (svc
->user
!= NULL
)
286 if (svc
->password
!= NULL
)
288 if (svc
->uri
!= NULL
)
296 papiServiceSetPeer(papi_service_t handle
, int fd
)
298 papi_status_t result
= PAPI_OK
;
300 if (handle
!= NULL
) {
301 service_t
*svc
= handle
;
304 result
= service_send_peer(svc
);
306 result
= PAPI_BAD_ARGUMENT
;
312 papiServiceSetUserName(papi_service_t handle
, char *user_name
)
314 papi_status_t result
= PAPI_OK
;
316 if (handle
!= NULL
) {
317 service_t
*svc
= handle
;
318 papi_status_t (*f
)();
320 if (svc
->user
!= NULL
)
322 if (user_name
!= NULL
)
323 svc
->user
= strdup(user_name
);
324 f
= (papi_status_t (*)())psm_sym(svc
, "papiServiceSetUserName");
326 result
= f(svc
->svc_handle
, user_name
);
328 result
= PAPI_BAD_ARGUMENT
;
334 papiServiceSetPassword(papi_service_t handle
, char *password
)
336 papi_status_t result
= PAPI_OK
;
338 if (handle
!= NULL
) {
339 service_t
*svc
= handle
;
340 papi_status_t (*f
)();
342 if (svc
->password
!= NULL
)
344 if (password
!= NULL
)
345 svc
->password
= strdup(password
);
346 f
= (papi_status_t (*)())psm_sym(svc
, "papiServiceSetPassword");
348 result
= f(svc
->svc_handle
, password
);
350 result
= PAPI_BAD_ARGUMENT
;
356 papiServiceSetEncryption(papi_service_t handle
, papi_encryption_t encryption
)
358 papi_status_t result
= PAPI_OK
;
360 if (handle
!= NULL
) {
361 service_t
*svc
= handle
;
362 papi_status_t (*f
)();
364 svc
->encryption
= encryption
;
365 f
= (papi_status_t (*)())psm_sym(svc
,
366 "papiServiceSetEncryption");
368 result
= f(svc
->svc_handle
, encryption
);
370 result
= PAPI_BAD_ARGUMENT
;
376 papiServiceSetAuthCB(papi_service_t handle
,
377 int (*authCB
)(papi_service_t svc
, void *app_data
))
379 papi_status_t result
= PAPI_OK
;
381 if (handle
!= NULL
) {
382 service_t
*svc
= handle
;
383 papi_status_t (*f
)();
385 svc
->authCB
= authCB
;
386 f
= (papi_status_t (*)())psm_sym(svc
, "papiServiceSetAuthCB");
388 result
= f(svc
->svc_handle
, interposed_auth_callback
);
390 result
= PAPI_BAD_ARGUMENT
;
397 papiServiceSetAppData(papi_service_t handle
, void *app_data
)
399 papi_status_t result
= PAPI_OK
;
401 if (handle
!= NULL
) {
402 service_t
*svc
= handle
;
403 papi_status_t (*f
)();
405 svc
->app_data
= (void *)app_data
;
407 result
= PAPI_BAD_ARGUMENT
;
413 papiServiceGetServiceName(papi_service_t handle
)
417 if (handle
!= NULL
) {
418 service_t
*svc
= handle
;
421 f
= (char *(*)())psm_sym(svc
, "papiServiceGetServiceName");
423 result
= f(svc
->svc_handle
);
432 papiServiceGetUserName(papi_service_t handle
)
436 if (handle
!= NULL
) {
437 service_t
*svc
= handle
;
440 f
= (char *(*)())psm_sym(svc
, "papiServiceGetUserName");
442 result
= f(svc
->svc_handle
);
451 papiServiceGetPassword(papi_service_t handle
)
455 if (handle
!= NULL
) {
456 service_t
*svc
= handle
;
459 f
= (char *(*)())psm_sym(svc
, "papiServiceGetPassword");
461 result
= f(svc
->svc_handle
);
463 result
= svc
->password
;
470 papiServiceGetEncryption(papi_service_t handle
)
472 papi_encryption_t result
= PAPI_ENCRYPT_NEVER
;
474 if (handle
!= NULL
) {
475 service_t
*svc
= handle
;
476 papi_encryption_t (*f
)();
478 f
= (papi_encryption_t (*)())psm_sym(svc
,
479 "papiServiceGetEncryption");
481 result
= f(svc
->svc_handle
);
482 if (result
== PAPI_ENCRYPT_NEVER
)
483 result
= svc
->encryption
;
490 papiServiceGetAppData(papi_service_t handle
)
493 service_t
*svc
= handle
;
496 result
= svc
->app_data
;
502 papiServiceGetAttributeList(papi_service_t handle
)
504 papi_attribute_t
**result
= NULL
;
505 service_t
*svc
= handle
;
507 if (handle
!= NULL
) {
508 papi_attribute_t
**(*f
)();
510 if (svc
->so_handle
== NULL
) {
511 char *uri
= default_service_uri(DEFAULT_SERVICE_URI
);
513 if (service_connect(svc
, uri
) != PAPI_OK
)
517 f
= (papi_attribute_t
**(*)())psm_sym(svc
,
518 "papiServiceGetAttributeList");
520 result
= f(svc
->svc_handle
);
522 result
= svc
->attributes
;
528 papiServiceGetStatusMessage(papi_service_t handle
)
531 service_t
*svc
= handle
;
533 if (handle
!= NULL
) {
536 f
= (char *(*)())psm_sym(svc
, "papiServiceGetStatusMessage");
538 result
= f(svc
->svc_handle
);
540 if (result
== NULL
) {
541 papiAttributeListGetString(svc
->attributes
, NULL
,
542 "detailed-status-message", &result
);
549 detailed_error(service_t
*svc
, char *fmt
, ...)
551 if ((svc
!= NULL
) && (fmt
!= NULL
)) {
554 char *message
= alloca(BUFSIZ
);
558 * fill in the message. If the buffer is too small, allocate
559 * one that is large enough and fill it in.
561 if ((size
= vsnprintf(message
, BUFSIZ
, fmt
, ap
)) >= BUFSIZ
)
562 if ((message
= alloca(size
)) != NULL
)
563 vsnprintf(message
, size
, fmt
, ap
);
566 papiAttributeListAddString(&svc
->attributes
, PAPI_ATTR_APPEND
,
567 "detailed-status-message", message
);
569 fprintf(stderr
, "detailed_error(%s)\n", message
);