8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / lp / lib / papi / service.c
bloba15434fcfb33dba3a86fd5ca2bc2807603549e5b
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 #pragma ident "%Z%%M% %I% %E% SMI"
28 /*LINTLIBRARY*/
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <alloca.h>
35 #include <libintl.h>
36 #include <papi_impl.h>
38 #include <tsol/label.h>
40 papi_status_t
41 papiServiceCreate(papi_service_t *handle, char *service_name,
42 char *user_name, char *password,
43 int (*authCB)(papi_service_t svc, void *app_data),
44 papi_encryption_t encryption, void *app_data)
46 service_t *svc = NULL;
47 char *path = Lp_FIFO;
49 if (handle == NULL)
50 return (PAPI_BAD_ARGUMENT);
52 if ((*handle = svc = calloc(1, sizeof (*svc))) == NULL)
53 return (PAPI_TEMPORARY_ERROR);
55 svc->md = mconnect(path, 0, 0);
56 if (svc->md == NULL) {
57 detailed_error(svc,
58 gettext("can't connect to spooler for %s: %s"),
59 (service_name ? service_name : ""), strerror(errno));
60 return (PAPI_SERVICE_UNAVAILABLE);
63 svc->msgbuf_size = MSGMAX;
64 if ((svc->msgbuf = calloc(1, svc->msgbuf_size)) == NULL)
65 return (PAPI_TEMPORARY_ERROR);
67 if (service_name != NULL)
68 papiAttributeListAddString(&svc->attributes, PAPI_ATTR_EXCL,
69 "service-name", service_name);
71 (void) papiServiceSetUserName(svc, user_name);
72 (void) papiServiceSetPassword(svc, password);
73 (void) papiServiceSetAuthCB(svc, authCB);
74 (void) papiServiceSetAppData(svc, app_data);
75 (void) papiServiceSetEncryption(svc, encryption);
77 return (PAPI_OK);
80 void
81 papiServiceDestroy(papi_service_t handle)
83 service_t *svc = handle;
85 if (svc != NULL) {
86 if (svc->md != NULL)
87 mdisconnect(svc->md);
88 if (svc->msgbuf != NULL)
89 free(svc->msgbuf);
90 papiAttributeListFree(svc->attributes);
91 free(svc);
96 * interface for passing a peer's connection to gather sensitivity labeling
97 * from for Trusted Solaris.
99 papi_status_t
100 papiServiceSetPeer(papi_service_t handle, int peerfd)
102 papi_status_t result = PAPI_OK;
103 service_t *svc = handle;
105 if (svc == NULL)
106 return (PAPI_BAD_ARGUMENT);
108 if (is_system_labeled()) {
109 short status;
111 if ((snd_msg(svc, S_PASS_PEER_CONNECTION) < 0) ||
112 (ioctl(svc->md->writefd, I_SENDFD, peerfd) < 0) ||
113 (rcv_msg(svc, R_PASS_PEER_CONNECTION, &status) < 0))
114 status = MTRANSMITERR;
116 if (status != MOK) {
117 detailed_error(svc,
118 gettext("failed to send peer connection: %s"),
119 lpsched_status_string(status));
120 result = lpsched_status_to_papi_status(status);
124 return (result);
127 papi_status_t
128 papiServiceSetUserName(papi_service_t handle, char *user_name)
130 service_t *svc = handle;
132 if (svc == NULL)
133 return (PAPI_BAD_ARGUMENT);
135 return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
136 "user-name", user_name));
139 papi_status_t
140 papiServiceSetPassword(papi_service_t handle, char *password)
142 service_t *svc = handle;
144 if (svc == NULL)
145 return (PAPI_BAD_ARGUMENT);
147 return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
148 "password", password));
151 papi_status_t
152 papiServiceSetEncryption(papi_service_t handle,
153 papi_encryption_t encryption)
155 service_t *svc = handle;
157 if (svc == NULL)
158 return (PAPI_BAD_ARGUMENT);
160 return (papiAttributeListAddInteger(&svc->attributes, PAPI_ATTR_REPLACE,
161 "encryption", (int)encryption));
164 papi_status_t
165 papiServiceSetAuthCB(papi_service_t handle,
166 int (*authCB)(papi_service_t svc, void *app_data))
168 service_t *svc = handle;
170 if (svc == NULL)
171 return (PAPI_BAD_ARGUMENT);
173 svc->authCB = (int (*)(papi_service_t svc, void *app_data))authCB;
175 return (PAPI_OK);
178 papi_status_t
179 papiServiceSetAppData(papi_service_t handle, void *app_data)
181 service_t *svc = handle;
183 if (svc == NULL)
184 return (PAPI_BAD_ARGUMENT);
186 svc->app_data = (void *)app_data;
188 return (PAPI_OK);
191 char *
192 papiServiceGetServiceName(papi_service_t handle)
194 service_t *svc = handle;
195 char *result = NULL;
197 if (svc != NULL)
198 papiAttributeListGetString(svc->attributes, NULL,
199 "service-name", &result);
201 return (result);
204 char *
205 papiServiceGetUserName(papi_service_t handle)
207 service_t *svc = handle;
208 char *result = NULL;
210 if (svc != NULL)
211 papiAttributeListGetString(svc->attributes, NULL,
212 "user-name", &result);
214 return (result);
217 char *
218 papiServiceGetPassword(papi_service_t handle)
220 service_t *svc = handle;
221 char *result = NULL;
223 if (svc != NULL)
224 papiAttributeListGetString(svc->attributes, NULL,
225 "password", &result);
227 return (result);
230 papi_encryption_t
231 papiServiceGetEncryption(papi_service_t handle)
233 service_t *svc = handle;
234 papi_encryption_t result = PAPI_ENCRYPT_NEVER;
236 if (svc != NULL)
237 papiAttributeListGetInteger(svc->attributes, NULL,
238 "encryption", (int *)&result);
240 return (result);
243 void *
244 papiServiceGetAppData(papi_service_t handle)
246 service_t *svc = handle;
247 void *result = NULL;
249 if (svc != NULL)
250 result = svc->app_data;
252 return (result);
255 papi_attribute_t **
256 papiServiceGetAttributeList(papi_service_t handle)
258 service_t *svc = handle;
259 papi_attribute_t **result = NULL;
261 if (svc != NULL) {
262 lpsched_service_information(&svc->attributes);
263 result = svc->attributes;
266 return (result);
269 char *
270 papiServiceGetStatusMessage(papi_service_t handle)
272 service_t *svc = handle;
273 char *result = NULL;
275 if (svc != NULL)
276 papiAttributeListGetString(svc->attributes, NULL,
277 "detailed-status-message", &result);
279 return (result);
282 void
283 detailed_error(service_t *svc, char *fmt, ...)
285 if ((svc != NULL) && (fmt != NULL)) {
286 va_list ap;
287 size_t size;
288 char *message = alloca(BUFSIZ);
290 va_start(ap, fmt);
292 * fill in the message. If the buffer is too small, allocate
293 * one that is large enough and fill it in.
295 if ((size = vsnprintf(message, BUFSIZ, fmt, ap)) >= BUFSIZ)
296 if ((message = alloca(size)) != NULL)
297 vsnprintf(message, size, fmt, ap);
298 va_end(ap);
300 papiAttributeListAddString(&svc->attributes, PAPI_ATTR_APPEND,
301 "detailed-status-message", message);