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 /*
23355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: service.c 163 2006-05-09 15:07:45Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs #include <stdlib.h>
31355b4669Sjacobs #include <stdio.h>
32355b4669Sjacobs #include <string.h>
33355b4669Sjacobs #include <stdarg.h>
34355b4669Sjacobs #include <uri.h>
35355b4669Sjacobs #include <papi_impl.h>
36355b4669Sjacobs 
37355b4669Sjacobs papi_status_t
service_fill_in(service_t * svc,char * name)38355b4669Sjacobs service_fill_in(service_t *svc, char *name)
39355b4669Sjacobs {
40355b4669Sjacobs 	papi_status_t status = PAPI_OK;
41355b4669Sjacobs 	uri_t *uri = NULL;
42355b4669Sjacobs 
43355b4669Sjacobs 	if (svc == NULL)
44355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
45355b4669Sjacobs 
46355b4669Sjacobs 	if (name == NULL)
47355b4669Sjacobs 		return (PAPI_OK);
48355b4669Sjacobs 
49355b4669Sjacobs 	/*
50355b4669Sjacobs 	 * valid URIs are in the form:
51355b4669Sjacobs 	 *	lpd://server[:port]/.../queue[#extensions]
52355b4669Sjacobs 	 *	rfc-1179://server[:port]/.../queue[#extensions]
53355b4669Sjacobs 	 * any authentication information supplied the URI is ignored.
54355b4669Sjacobs 	 */
55355b4669Sjacobs 	if (uri_from_string((char *)name, &uri) != -1) {
56355b4669Sjacobs 		if ((strcasecmp(uri->scheme, "lpd") == 0) ||
57355b4669Sjacobs 		    (strcasecmp(uri->scheme, "rfc-1179") == 0)) {
58355b4669Sjacobs 			if (svc->uri != NULL)
59355b4669Sjacobs 				uri_free(svc->uri);
60355b4669Sjacobs 			svc->uri = uri;
61355b4669Sjacobs 		} else {
62355b4669Sjacobs 			uri_free(uri);
63355b4669Sjacobs 			status = PAPI_URI_SCHEME;
64355b4669Sjacobs 		}
65355b4669Sjacobs 	}
66355b4669Sjacobs 
67355b4669Sjacobs 	return (status);
68355b4669Sjacobs }
69355b4669Sjacobs 
70355b4669Sjacobs papi_status_t
papiServiceCreate(papi_service_t * handle,char * service_name,char * user_name,char * password,int (* authCB)(papi_service_t svc,void * app_data),papi_encryption_t encryption,void * app_data)71355b4669Sjacobs papiServiceCreate(papi_service_t *handle, char *service_name,
72355b4669Sjacobs 		char *user_name, char *password,
73355b4669Sjacobs 		int (*authCB)(papi_service_t svc, void *app_data),
74355b4669Sjacobs 		papi_encryption_t encryption, void *app_data)
75355b4669Sjacobs {
76355b4669Sjacobs 	papi_status_t status;
77355b4669Sjacobs 	service_t *svc = NULL;
78355b4669Sjacobs 
79355b4669Sjacobs 	if (handle == NULL)
80355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
81355b4669Sjacobs 
82355b4669Sjacobs 	if ((*handle = svc = (service_t *)calloc(1, sizeof (*svc))) == NULL)
83355b4669Sjacobs 		return (PAPI_TEMPORARY_ERROR);
84355b4669Sjacobs 
85355b4669Sjacobs 	if (service_name != NULL)
86355b4669Sjacobs 		papiAttributeListAddString(&svc->attributes, PAPI_ATTR_EXCL,
87355b4669Sjacobs 		"service-name", service_name);
88355b4669Sjacobs 
89355b4669Sjacobs 	(void) papiServiceSetUserName(svc, user_name);
90355b4669Sjacobs 	(void) papiServiceSetPassword(svc, password);
91355b4669Sjacobs 	(void) papiServiceSetAuthCB(svc, authCB);
92355b4669Sjacobs 	(void) papiServiceSetAppData(svc, app_data);
93355b4669Sjacobs 	(void) papiServiceSetEncryption(svc, encryption);
94355b4669Sjacobs 
95355b4669Sjacobs 	status = service_fill_in(svc, service_name);
96355b4669Sjacobs 
97355b4669Sjacobs 	return (status);
98355b4669Sjacobs }
99355b4669Sjacobs 
100355b4669Sjacobs void
papiServiceDestroy(papi_service_t handle)101355b4669Sjacobs papiServiceDestroy(papi_service_t handle)
102355b4669Sjacobs {
103355b4669Sjacobs 	if (handle != NULL) {
104355b4669Sjacobs 		service_t *svc = handle;
105355b4669Sjacobs 
106355b4669Sjacobs #ifdef DEADBEEF
107355b4669Sjacobs 		if (svc->cache != NULL)
108355b4669Sjacobs 			cache_free(svc->cache);
109355b4669Sjacobs #endif
110355b4669Sjacobs 		if (svc->uri != NULL)
111355b4669Sjacobs 			uri_free(svc->uri);
112355b4669Sjacobs 		if (svc->attributes != NULL)
113355b4669Sjacobs 			papiAttributeListFree(svc->attributes);
114355b4669Sjacobs 		free(svc);
115355b4669Sjacobs 	}
116355b4669Sjacobs }
117355b4669Sjacobs 
118355b4669Sjacobs papi_status_t
papiServiceSetUserName(papi_service_t handle,char * user_name)119355b4669Sjacobs papiServiceSetUserName(papi_service_t handle, char *user_name)
120355b4669Sjacobs {
121355b4669Sjacobs 	service_t *svc = handle;
122355b4669Sjacobs 
123355b4669Sjacobs 	if (svc == NULL)
124355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
125355b4669Sjacobs 
126355b4669Sjacobs 	return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
127355b4669Sjacobs 			"user-name", user_name));
128355b4669Sjacobs }
129355b4669Sjacobs 
130355b4669Sjacobs papi_status_t
papiServiceSetPassword(papi_service_t handle,char * password)131355b4669Sjacobs papiServiceSetPassword(papi_service_t handle, char *password)
132355b4669Sjacobs {
133355b4669Sjacobs 	service_t *svc = handle;
134355b4669Sjacobs 
135355b4669Sjacobs 	if (svc == NULL)
136355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
137355b4669Sjacobs 
138355b4669Sjacobs 	return (papiAttributeListAddString(&svc->attributes,
139355b4669Sjacobs 		PAPI_ATTR_REPLACE, "password", password));
140355b4669Sjacobs }
141355b4669Sjacobs 
142355b4669Sjacobs papi_status_t
papiServiceSetEncryption(papi_service_t handle,papi_encryption_t encryption)143355b4669Sjacobs papiServiceSetEncryption(papi_service_t handle,
144355b4669Sjacobs 			papi_encryption_t encryption)
145355b4669Sjacobs {
146355b4669Sjacobs 	service_t *svc = handle;
147355b4669Sjacobs 
148355b4669Sjacobs 	if (svc == NULL)
149355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
150355b4669Sjacobs 
151355b4669Sjacobs 	return (papiAttributeListAddInteger(&svc->attributes, PAPI_ATTR_REPLACE,
152355b4669Sjacobs 			"encryption", (int)encryption));
153355b4669Sjacobs }
154355b4669Sjacobs 
155355b4669Sjacobs papi_status_t
papiServiceSetAuthCB(papi_service_t handle,int (* authCB)(papi_service_t svc,void * app_data))156355b4669Sjacobs papiServiceSetAuthCB(papi_service_t handle,
157355b4669Sjacobs 			int (*authCB)(papi_service_t svc, void *app_data))
158355b4669Sjacobs {
159355b4669Sjacobs 	service_t *svc = handle;
160355b4669Sjacobs 
161355b4669Sjacobs 	if (svc == NULL)
162355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
163355b4669Sjacobs 
164355b4669Sjacobs 	svc->authCB = (int (*)(papi_service_t svc, void *))authCB;
165355b4669Sjacobs 
166355b4669Sjacobs 	return (PAPI_OK);
167355b4669Sjacobs }
168355b4669Sjacobs 
169355b4669Sjacobs papi_status_t
papiServiceSetAppData(papi_service_t handle,void * app_data)170355b4669Sjacobs papiServiceSetAppData(papi_service_t handle, void *app_data)
171355b4669Sjacobs {
172355b4669Sjacobs 	service_t *svc = handle;
173355b4669Sjacobs 
174355b4669Sjacobs 	if (svc == NULL)
175355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
176355b4669Sjacobs 
177355b4669Sjacobs 	svc->app_data = (void *)app_data;
178355b4669Sjacobs 
179355b4669Sjacobs 	return (PAPI_OK);
180355b4669Sjacobs }
181355b4669Sjacobs 
182355b4669Sjacobs char *
papiServiceGetServiceName(papi_service_t handle)183355b4669Sjacobs papiServiceGetServiceName(papi_service_t handle)
184355b4669Sjacobs {
185355b4669Sjacobs 	service_t *svc = handle;
186355b4669Sjacobs 	char *result = NULL;
187355b4669Sjacobs 
188355b4669Sjacobs 	if (svc != NULL)
189355b4669Sjacobs 		papiAttributeListGetString(svc->attributes, NULL,
190355b4669Sjacobs 				"service-name", &result);
191355b4669Sjacobs 
192355b4669Sjacobs 	return (result);
193355b4669Sjacobs }
194355b4669Sjacobs 
195355b4669Sjacobs char *
papiServiceGetUserName(papi_service_t handle)196355b4669Sjacobs papiServiceGetUserName(papi_service_t handle)
197355b4669Sjacobs {
198355b4669Sjacobs 	service_t *svc = handle;
199355b4669Sjacobs 	char *result = NULL;
200355b4669Sjacobs 
201355b4669Sjacobs 	if (svc != NULL)
202355b4669Sjacobs 		papiAttributeListGetString(svc->attributes, NULL,
203355b4669Sjacobs 				"user-name", &result);
204355b4669Sjacobs 
205355b4669Sjacobs 	return (result);
206355b4669Sjacobs 
207355b4669Sjacobs }
208355b4669Sjacobs 
209355b4669Sjacobs char *
papiServiceGetPassword(papi_service_t handle)210355b4669Sjacobs papiServiceGetPassword(papi_service_t handle)
211355b4669Sjacobs {
212355b4669Sjacobs 	service_t *svc = handle;
213355b4669Sjacobs 	char *result = NULL;
214355b4669Sjacobs 
215355b4669Sjacobs 	if (svc != NULL)
216355b4669Sjacobs 		papiAttributeListGetString(svc->attributes, NULL,
217355b4669Sjacobs 				"password", &result);
218355b4669Sjacobs 
219355b4669Sjacobs 	return (result);
220355b4669Sjacobs }
221355b4669Sjacobs 
222355b4669Sjacobs papi_encryption_t
papiServiceGetEncryption(papi_service_t handle)223355b4669Sjacobs papiServiceGetEncryption(papi_service_t handle)
224355b4669Sjacobs {
225355b4669Sjacobs 	service_t *svc = handle;
226355b4669Sjacobs 	papi_encryption_t result = PAPI_ENCRYPT_NEVER;
227355b4669Sjacobs 
228355b4669Sjacobs 	if (svc != NULL)
229355b4669Sjacobs 		papiAttributeListGetInteger(svc->attributes, NULL,
230355b4669Sjacobs 				"encryption", (int *)&result);
231355b4669Sjacobs 
232355b4669Sjacobs 	return (result);
233355b4669Sjacobs }
234355b4669Sjacobs 
235355b4669Sjacobs void *
papiServiceGetAppData(papi_service_t handle)236355b4669Sjacobs papiServiceGetAppData(papi_service_t handle)
237355b4669Sjacobs {
238355b4669Sjacobs 	service_t *svc = handle;
239355b4669Sjacobs 	void *result = NULL;
240355b4669Sjacobs 
241355b4669Sjacobs 	if (svc != NULL) {
242355b4669Sjacobs 		result = svc->app_data;
243355b4669Sjacobs 	}
244355b4669Sjacobs 
245355b4669Sjacobs 	return (result);
246355b4669Sjacobs 
247355b4669Sjacobs }
248355b4669Sjacobs 
249355b4669Sjacobs papi_attribute_t **
papiServiceGetAttributeList(papi_service_t handle)250355b4669Sjacobs papiServiceGetAttributeList(papi_service_t handle)
251355b4669Sjacobs {
252355b4669Sjacobs 	service_t *svc = handle;
253355b4669Sjacobs 	papi_attribute_t **result = NULL;
254355b4669Sjacobs 
255355b4669Sjacobs 	if (svc != NULL)
256355b4669Sjacobs 		result = svc->attributes;
257355b4669Sjacobs 
258355b4669Sjacobs 	return (result);
259355b4669Sjacobs }
260355b4669Sjacobs 
261355b4669Sjacobs char *
papiServiceGetStatusMessage(papi_service_t handle)262355b4669Sjacobs papiServiceGetStatusMessage(papi_service_t handle)
263355b4669Sjacobs {
264355b4669Sjacobs 	service_t *svc = handle;
265355b4669Sjacobs 	char *result = NULL;
266355b4669Sjacobs 
267355b4669Sjacobs 	if (svc != NULL) {
268355b4669Sjacobs 		papiAttributeListGetString(svc->attributes, NULL,
269355b4669Sjacobs 				"detailed-status-message", &result);
270355b4669Sjacobs 	}
271355b4669Sjacobs 
272355b4669Sjacobs 	return (result);
273355b4669Sjacobs }
274355b4669Sjacobs 
275355b4669Sjacobs void
detailed_error(service_t * svc,char * fmt,...)276355b4669Sjacobs detailed_error(service_t *svc, char *fmt, ...)
277355b4669Sjacobs {
278355b4669Sjacobs 	if ((svc != NULL) && (fmt != NULL)) {
279355b4669Sjacobs 		va_list ap;
280*b666b5beSToomas Soome 		char *message;
281*b666b5beSToomas Soome 		int rv;
282355b4669Sjacobs 
283355b4669Sjacobs 		va_start(ap, fmt);
284*b666b5beSToomas Soome 		rv = vasprintf(&message, fmt, ap);
285355b4669Sjacobs 		va_end(ap);
286355b4669Sjacobs 
287*b666b5beSToomas Soome 		if (rv >= 0) {
288*b666b5beSToomas Soome 			papiAttributeListAddString(&svc->attributes,
289*b666b5beSToomas Soome 			    PAPI_ATTR_APPEND, "detailed-status-message",
290*b666b5beSToomas Soome 			    message);
291*b666b5beSToomas Soome 			free(message);
292*b666b5beSToomas Soome 		}
293355b4669Sjacobs 	}
294355b4669Sjacobs }
295