kill tsol ("Trusted Solaris") aka TX ("Trusted Extensions")
[unleashed.git] / usr / src / cmd / lp / lib / papi / service.c
blob32d13c75536aee5ea4d019fea5cb941b30282f80
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /*LINTLIBRARY*/
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <alloca.h>
33 #include <libintl.h>
34 #include <papi_impl.h>
36 papi_status_t
37 papiServiceCreate(papi_service_t *handle, char *service_name,
38 char *user_name, char *password,
39 int (*authCB)(papi_service_t svc, void *app_data),
40 papi_encryption_t encryption, void *app_data)
42 service_t *svc = NULL;
43 char *path = Lp_FIFO;
45 if (handle == NULL)
46 return (PAPI_BAD_ARGUMENT);
48 if ((*handle = svc = calloc(1, sizeof (*svc))) == NULL)
49 return (PAPI_TEMPORARY_ERROR);
51 svc->md = mconnect(path, 0, 0);
52 if (svc->md == NULL) {
53 detailed_error(svc,
54 gettext("can't connect to spooler for %s: %s"),
55 (service_name ? service_name : ""), strerror(errno));
56 return (PAPI_SERVICE_UNAVAILABLE);
59 svc->msgbuf_size = MSGMAX;
60 if ((svc->msgbuf = calloc(1, svc->msgbuf_size)) == NULL)
61 return (PAPI_TEMPORARY_ERROR);
63 if (service_name != NULL)
64 papiAttributeListAddString(&svc->attributes, PAPI_ATTR_EXCL,
65 "service-name", service_name);
67 (void) papiServiceSetUserName(svc, user_name);
68 (void) papiServiceSetPassword(svc, password);
69 (void) papiServiceSetAuthCB(svc, authCB);
70 (void) papiServiceSetAppData(svc, app_data);
71 (void) papiServiceSetEncryption(svc, encryption);
73 return (PAPI_OK);
76 void
77 papiServiceDestroy(papi_service_t handle)
79 service_t *svc = handle;
81 if (svc != NULL) {
82 if (svc->md != NULL)
83 mdisconnect(svc->md);
84 free(svc->msgbuf);
85 papiAttributeListFree(svc->attributes);
86 free(svc);
90 papi_status_t
91 papiServiceSetUserName(papi_service_t handle, char *user_name)
93 service_t *svc = handle;
95 if (svc == NULL)
96 return (PAPI_BAD_ARGUMENT);
98 return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
99 "user-name", user_name));
102 papi_status_t
103 papiServiceSetPassword(papi_service_t handle, char *password)
105 service_t *svc = handle;
107 if (svc == NULL)
108 return (PAPI_BAD_ARGUMENT);
110 return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
111 "password", password));
114 papi_status_t
115 papiServiceSetEncryption(papi_service_t handle,
116 papi_encryption_t encryption)
118 service_t *svc = handle;
120 if (svc == NULL)
121 return (PAPI_BAD_ARGUMENT);
123 return (papiAttributeListAddInteger(&svc->attributes, PAPI_ATTR_REPLACE,
124 "encryption", (int)encryption));
127 papi_status_t
128 papiServiceSetAuthCB(papi_service_t handle,
129 int (*authCB)(papi_service_t svc, void *app_data))
131 service_t *svc = handle;
133 if (svc == NULL)
134 return (PAPI_BAD_ARGUMENT);
136 svc->authCB = (int (*)(papi_service_t svc, void *app_data))authCB;
138 return (PAPI_OK);
141 papi_status_t
142 papiServiceSetAppData(papi_service_t handle, void *app_data)
144 service_t *svc = handle;
146 if (svc == NULL)
147 return (PAPI_BAD_ARGUMENT);
149 svc->app_data = (void *)app_data;
151 return (PAPI_OK);
154 char *
155 papiServiceGetServiceName(papi_service_t handle)
157 service_t *svc = handle;
158 char *result = NULL;
160 if (svc != NULL)
161 papiAttributeListGetString(svc->attributes, NULL,
162 "service-name", &result);
164 return (result);
167 char *
168 papiServiceGetUserName(papi_service_t handle)
170 service_t *svc = handle;
171 char *result = NULL;
173 if (svc != NULL)
174 papiAttributeListGetString(svc->attributes, NULL,
175 "user-name", &result);
177 return (result);
180 char *
181 papiServiceGetPassword(papi_service_t handle)
183 service_t *svc = handle;
184 char *result = NULL;
186 if (svc != NULL)
187 papiAttributeListGetString(svc->attributes, NULL,
188 "password", &result);
190 return (result);
193 papi_encryption_t
194 papiServiceGetEncryption(papi_service_t handle)
196 service_t *svc = handle;
197 papi_encryption_t result = PAPI_ENCRYPT_NEVER;
199 if (svc != NULL)
200 papiAttributeListGetInteger(svc->attributes, NULL,
201 "encryption", (int *)&result);
203 return (result);
206 void *
207 papiServiceGetAppData(papi_service_t handle)
209 service_t *svc = handle;
210 void *result = NULL;
212 if (svc != NULL)
213 result = svc->app_data;
215 return (result);
218 papi_attribute_t **
219 papiServiceGetAttributeList(papi_service_t handle)
221 service_t *svc = handle;
222 papi_attribute_t **result = NULL;
224 if (svc != NULL) {
225 lpsched_service_information(&svc->attributes);
226 result = svc->attributes;
229 return (result);
232 char *
233 papiServiceGetStatusMessage(papi_service_t handle)
235 service_t *svc = handle;
236 char *result = NULL;
238 if (svc != NULL)
239 papiAttributeListGetString(svc->attributes, NULL,
240 "detailed-status-message", &result);
242 return (result);
245 void
246 detailed_error(service_t *svc, char *fmt, ...)
248 if ((svc != NULL) && (fmt != NULL)) {
249 va_list ap;
250 size_t size;
251 char *message = alloca(BUFSIZ);
253 va_start(ap, fmt);
255 * fill in the message. If the buffer is too small, allocate
256 * one that is large enough and fill it in.
258 if ((size = vsnprintf(message, BUFSIZ, fmt, ap)) >= BUFSIZ)
259 if ((message = alloca(size)) != NULL)
260 vsnprintf(message, size, fmt, ap);
261 va_end(ap);
263 papiAttributeListAddString(&svc->attributes, PAPI_ATTR_APPEND,
264 "detailed-status-message", message);