xref: /illumos-gate/usr/src/lib/print/libpapi-lpd/common/service.c (revision ed093b41a93e8563e6e1e5dae0768dda2a7bcc27)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  */
27 
28 /* $Id: service.c 163 2006-05-09 15:07:45Z njacobs $ */
29 
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <uri.h>
35 #include <papi_impl.h>
36 
37 papi_status_t
38 service_fill_in(service_t *svc, char *name)
39 {
40 	papi_status_t status = PAPI_OK;
41 	uri_t *uri = NULL;
42 
43 	if (svc == NULL)
44 		return (PAPI_BAD_ARGUMENT);
45 
46 	if (name == NULL)
47 		return (PAPI_OK);
48 
49 	/*
50 	 * valid URIs are in the form:
51 	 *	lpd://server[:port]/.../queue[#extensions]
52 	 *	rfc-1179://server[:port]/.../queue[#extensions]
53 	 * any authentication information supplied the URI is ignored.
54 	 */
55 	if (uri_from_string((char *)name, &uri) != -1) {
56 		if ((strcasecmp(uri->scheme, "lpd") == 0) ||
57 		    (strcasecmp(uri->scheme, "rfc-1179") == 0)) {
58 			if (svc->uri != NULL)
59 				uri_free(svc->uri);
60 			svc->uri = uri;
61 		} else {
62 			uri_free(uri);
63 			status = PAPI_URI_SCHEME;
64 		}
65 	}
66 
67 	return (status);
68 }
69 
70 papi_status_t
71 papiServiceCreate(papi_service_t *handle, char *service_name,
72 		char *user_name, char *password,
73 		int (*authCB)(papi_service_t svc, void *app_data),
74 		papi_encryption_t encryption, void *app_data)
75 {
76 	papi_status_t status;
77 	service_t *svc = NULL;
78 
79 	if (handle == NULL)
80 		return (PAPI_BAD_ARGUMENT);
81 
82 	if ((*handle = svc = (service_t *)calloc(1, sizeof (*svc))) == NULL)
83 		return (PAPI_TEMPORARY_ERROR);
84 
85 	if (service_name != NULL)
86 		papiAttributeListAddString(&svc->attributes, PAPI_ATTR_EXCL,
87 		"service-name", service_name);
88 
89 	(void) papiServiceSetUserName(svc, user_name);
90 	(void) papiServiceSetPassword(svc, password);
91 	(void) papiServiceSetAuthCB(svc, authCB);
92 	(void) papiServiceSetAppData(svc, app_data);
93 	(void) papiServiceSetEncryption(svc, encryption);
94 
95 	status = service_fill_in(svc, service_name);
96 
97 	return (status);
98 }
99 
100 void
101 papiServiceDestroy(papi_service_t handle)
102 {
103 	if (handle != NULL) {
104 		service_t *svc = handle;
105 
106 #ifdef DEADBEEF
107 		if (svc->cache != NULL)
108 			cache_free(svc->cache);
109 #endif
110 		if (svc->uri != NULL)
111 			uri_free(svc->uri);
112 		if (svc->attributes != NULL)
113 			papiAttributeListFree(svc->attributes);
114 		free(svc);
115 	}
116 }
117 
118 papi_status_t
119 papiServiceSetUserName(papi_service_t handle, char *user_name)
120 {
121 	service_t *svc = handle;
122 
123 	if (svc == NULL)
124 		return (PAPI_BAD_ARGUMENT);
125 
126 	return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
127 			"user-name", user_name));
128 }
129 
130 papi_status_t
131 papiServiceSetPassword(papi_service_t handle, char *password)
132 {
133 	service_t *svc = handle;
134 
135 	if (svc == NULL)
136 		return (PAPI_BAD_ARGUMENT);
137 
138 	return (papiAttributeListAddString(&svc->attributes,
139 		PAPI_ATTR_REPLACE, "password", password));
140 }
141 
142 papi_status_t
143 papiServiceSetEncryption(papi_service_t handle,
144 			papi_encryption_t encryption)
145 {
146 	service_t *svc = handle;
147 
148 	if (svc == NULL)
149 		return (PAPI_BAD_ARGUMENT);
150 
151 	return (papiAttributeListAddInteger(&svc->attributes, PAPI_ATTR_REPLACE,
152 			"encryption", (int)encryption));
153 }
154 
155 papi_status_t
156 papiServiceSetAuthCB(papi_service_t handle,
157 			int (*authCB)(papi_service_t svc, void *app_data))
158 {
159 	service_t *svc = handle;
160 
161 	if (svc == NULL)
162 		return (PAPI_BAD_ARGUMENT);
163 
164 	svc->authCB = (int (*)(papi_service_t svc, void *))authCB;
165 
166 	return (PAPI_OK);
167 }
168 
169 papi_status_t
170 papiServiceSetAppData(papi_service_t handle, void *app_data)
171 {
172 	service_t *svc = handle;
173 
174 	if (svc == NULL)
175 		return (PAPI_BAD_ARGUMENT);
176 
177 	svc->app_data = (void *)app_data;
178 
179 	return (PAPI_OK);
180 }
181 
182 char *
183 papiServiceGetServiceName(papi_service_t handle)
184 {
185 	service_t *svc = handle;
186 	char *result = NULL;
187 
188 	if (svc != NULL)
189 		papiAttributeListGetString(svc->attributes, NULL,
190 				"service-name", &result);
191 
192 	return (result);
193 }
194 
195 char *
196 papiServiceGetUserName(papi_service_t handle)
197 {
198 	service_t *svc = handle;
199 	char *result = NULL;
200 
201 	if (svc != NULL)
202 		papiAttributeListGetString(svc->attributes, NULL,
203 				"user-name", &result);
204 
205 	return (result);
206 
207 }
208 
209 char *
210 papiServiceGetPassword(papi_service_t handle)
211 {
212 	service_t *svc = handle;
213 	char *result = NULL;
214 
215 	if (svc != NULL)
216 		papiAttributeListGetString(svc->attributes, NULL,
217 				"password", &result);
218 
219 	return (result);
220 }
221 
222 papi_encryption_t
223 papiServiceGetEncryption(papi_service_t handle)
224 {
225 	service_t *svc = handle;
226 	papi_encryption_t result = PAPI_ENCRYPT_NEVER;
227 
228 	if (svc != NULL)
229 		papiAttributeListGetInteger(svc->attributes, NULL,
230 				"encryption", (int *)&result);
231 
232 	return (result);
233 }
234 
235 void *
236 papiServiceGetAppData(papi_service_t handle)
237 {
238 	service_t *svc = handle;
239 	void *result = NULL;
240 
241 	if (svc != NULL) {
242 		result = svc->app_data;
243 	}
244 
245 	return (result);
246 
247 }
248 
249 papi_attribute_t **
250 papiServiceGetAttributeList(papi_service_t handle)
251 {
252 	service_t *svc = handle;
253 	papi_attribute_t **result = NULL;
254 
255 	if (svc != NULL)
256 		result = svc->attributes;
257 
258 	return (result);
259 }
260 
261 char *
262 papiServiceGetStatusMessage(papi_service_t handle)
263 {
264 	service_t *svc = handle;
265 	char *result = NULL;
266 
267 	if (svc != NULL) {
268 		papiAttributeListGetString(svc->attributes, NULL,
269 				"detailed-status-message", &result);
270 	}
271 
272 	return (result);
273 }
274 
275 void
276 detailed_error(service_t *svc, char *fmt, ...)
277 {
278 	if ((svc != NULL) && (fmt != NULL)) {
279 		va_list ap;
280 		char *message;
281 		int rv;
282 
283 		va_start(ap, fmt);
284 		rv = vasprintf(&message, fmt, ap);
285 		va_end(ap);
286 
287 		if (rv >= 0) {
288 			papiAttributeListAddString(&svc->attributes,
289 			    PAPI_ATTR_APPEND, "detailed-status-message",
290 			    message);
291 			free(message);
292 		}
293 	}
294 }
295