xref: /titanic_52/usr/src/cmd/lp/lib/papi/service.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2245916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <alloca.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #include <papi_impl.h>
377c478bd9Sstevel@tonic-gate 
3845916cd2Sjpk #include <tsol/label.h>
3945916cd2Sjpk 
407c478bd9Sstevel@tonic-gate papi_status_t
41*355b4669Sjacobs papiServiceCreate(papi_service_t *handle, char *service_name,
42*355b4669Sjacobs 		char *user_name, char *password,
43*355b4669Sjacobs 		int (*authCB)(papi_service_t svc, void *app_data),
44*355b4669Sjacobs 		papi_encryption_t encryption, void *app_data)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	service_t *svc = NULL;
477c478bd9Sstevel@tonic-gate 	char *path = Lp_FIFO;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	if (handle == NULL)
507c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	if ((*handle = svc = calloc(1, sizeof (*svc))) == NULL)
537c478bd9Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	svc->md = mconnect(path, 0, 0);
567c478bd9Sstevel@tonic-gate 	if (svc->md == NULL) {
577c478bd9Sstevel@tonic-gate 		detailed_error(svc,
587c478bd9Sstevel@tonic-gate 			gettext("can't connect to spooler for %s: %s"),
597c478bd9Sstevel@tonic-gate 			(service_name ? service_name : ""), strerror(errno));
607c478bd9Sstevel@tonic-gate 		return (PAPI_SERVICE_UNAVAILABLE);
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	svc->msgbuf_size = MSGMAX;
647c478bd9Sstevel@tonic-gate 	if ((svc->msgbuf = calloc(1, svc->msgbuf_size)) == NULL)
657c478bd9Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	if (service_name != NULL)
687c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(&svc->attributes, PAPI_ATTR_EXCL,
697c478bd9Sstevel@tonic-gate 				"service-name", service_name);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	(void) papiServiceSetUserName(svc, user_name);
727c478bd9Sstevel@tonic-gate 	(void) papiServiceSetPassword(svc, password);
737c478bd9Sstevel@tonic-gate 	(void) papiServiceSetAuthCB(svc, authCB);
747c478bd9Sstevel@tonic-gate 	(void) papiServiceSetAppData(svc, app_data);
757c478bd9Sstevel@tonic-gate 	(void) papiServiceSetEncryption(svc, encryption);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate void
817c478bd9Sstevel@tonic-gate papiServiceDestroy(papi_service_t handle)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (svc != NULL) {
867c478bd9Sstevel@tonic-gate 		if (svc->md != NULL)
877c478bd9Sstevel@tonic-gate 			mdisconnect(svc->md);
887c478bd9Sstevel@tonic-gate 		if (svc->msgbuf != NULL)
897c478bd9Sstevel@tonic-gate 			free(svc->msgbuf);
907c478bd9Sstevel@tonic-gate 		papiAttributeListFree(svc->attributes);
917c478bd9Sstevel@tonic-gate 		free(svc);
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
9545916cd2Sjpk /*
9645916cd2Sjpk  * interface for passing a peer's connection to gather sensitivity labeling
9745916cd2Sjpk  * from for Trusted Solaris.
9845916cd2Sjpk  */
9945916cd2Sjpk papi_status_t
10045916cd2Sjpk papiServiceSetPeer(papi_service_t handle, int peerfd)
10145916cd2Sjpk {
10245916cd2Sjpk 	papi_status_t result = PAPI_OK;
10345916cd2Sjpk 	service_t *svc = handle;
10445916cd2Sjpk 
10545916cd2Sjpk 	if (svc == NULL)
10645916cd2Sjpk 		return (PAPI_BAD_ARGUMENT);
10745916cd2Sjpk 
108b9dac67bSrica 	if (is_system_labeled()) {
10945916cd2Sjpk 		short status;
11045916cd2Sjpk 
11145916cd2Sjpk 		if ((snd_msg(svc, S_PASS_PEER_CONNECTION) < 0) ||
11245916cd2Sjpk 		    (ioctl(svc->md->writefd, I_SENDFD, peerfd) < 0) ||
11345916cd2Sjpk 		    (rcv_msg(svc, R_PASS_PEER_CONNECTION, &status) < 0))
11445916cd2Sjpk 			status = MTRANSMITERR;
11545916cd2Sjpk 
11645916cd2Sjpk 		if (status != MOK) {
11745916cd2Sjpk 			detailed_error(svc,
11845916cd2Sjpk 				gettext("failed to send peer connection: %s"),
11945916cd2Sjpk 				lpsched_status_string(status));
12045916cd2Sjpk 			result = lpsched_status_to_papi_status(status);
12145916cd2Sjpk 		}
12245916cd2Sjpk 	}
12345916cd2Sjpk 
12445916cd2Sjpk 	return (result);
12545916cd2Sjpk }
12645916cd2Sjpk 
1277c478bd9Sstevel@tonic-gate papi_status_t
128*355b4669Sjacobs papiServiceSetUserName(papi_service_t handle, char *user_name)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (svc == NULL)
1337c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
1367c478bd9Sstevel@tonic-gate 				"user-name", user_name));
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate papi_status_t
140*355b4669Sjacobs papiServiceSetPassword(papi_service_t handle, char *password)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (svc == NULL)
1457c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
1487c478bd9Sstevel@tonic-gate 				"password", password));
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate papi_status_t
1527c478bd9Sstevel@tonic-gate papiServiceSetEncryption(papi_service_t handle,
153*355b4669Sjacobs 			papi_encryption_t encryption)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (svc == NULL)
1587c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	return (papiAttributeListAddInteger(&svc->attributes, PAPI_ATTR_REPLACE,
1617c478bd9Sstevel@tonic-gate 				"encryption", (int)encryption));
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate papi_status_t
1657c478bd9Sstevel@tonic-gate papiServiceSetAuthCB(papi_service_t handle,
166*355b4669Sjacobs 			int (*authCB)(papi_service_t svc, void *app_data))
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (svc == NULL)
1717c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
1727c478bd9Sstevel@tonic-gate 
173*355b4669Sjacobs 	svc->authCB = (int (*)(papi_service_t svc, void *app_data))authCB;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate papi_status_t
179*355b4669Sjacobs papiServiceSetAppData(papi_service_t handle, void *app_data)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (svc == NULL)
1847c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	svc->app_data = (void *)app_data;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate char *
1927c478bd9Sstevel@tonic-gate papiServiceGetServiceName(papi_service_t handle)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
1957c478bd9Sstevel@tonic-gate 	char *result = NULL;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (svc != NULL)
1987c478bd9Sstevel@tonic-gate 		papiAttributeListGetString(svc->attributes, NULL,
1997c478bd9Sstevel@tonic-gate 					"service-name", &result);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	return (result);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate char *
2057c478bd9Sstevel@tonic-gate papiServiceGetUserName(papi_service_t handle)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
2087c478bd9Sstevel@tonic-gate 	char *result = NULL;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (svc != NULL)
2117c478bd9Sstevel@tonic-gate 		papiAttributeListGetString(svc->attributes, NULL,
2127c478bd9Sstevel@tonic-gate 					"user-name", &result);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	return (result);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate char *
2187c478bd9Sstevel@tonic-gate papiServiceGetPassword(papi_service_t handle)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
2217c478bd9Sstevel@tonic-gate 	char *result = NULL;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if (svc != NULL)
2247c478bd9Sstevel@tonic-gate 		papiAttributeListGetString(svc->attributes, NULL,
2257c478bd9Sstevel@tonic-gate 					"password", &result);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	return (result);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate papi_encryption_t
2317c478bd9Sstevel@tonic-gate papiServiceGetEncryption(papi_service_t handle)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
2347c478bd9Sstevel@tonic-gate 	papi_encryption_t result = PAPI_ENCRYPT_NEVER;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if (svc != NULL)
2377c478bd9Sstevel@tonic-gate 		papiAttributeListGetInteger(svc->attributes, NULL,
2387c478bd9Sstevel@tonic-gate 					"encryption", (int *)&result);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	return (result);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate void *
2447c478bd9Sstevel@tonic-gate papiServiceGetAppData(papi_service_t handle)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
2477c478bd9Sstevel@tonic-gate 	void *result = NULL;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if (svc != NULL)
2507c478bd9Sstevel@tonic-gate 		result = svc->app_data;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	return (result);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
255*355b4669Sjacobs papi_attribute_t **
256*355b4669Sjacobs papiServiceGetAttributeList(papi_service_t handle)
257*355b4669Sjacobs {
258*355b4669Sjacobs 	service_t *svc = handle;
259*355b4669Sjacobs 	papi_attribute_t **result = NULL;
260*355b4669Sjacobs 
261*355b4669Sjacobs 	if (svc != NULL) {
262*355b4669Sjacobs 		lpsched_service_information(&svc->attributes);
263*355b4669Sjacobs 		result = svc->attributes;
264*355b4669Sjacobs 	}
265*355b4669Sjacobs 
266*355b4669Sjacobs 	return (result);
267*355b4669Sjacobs }
268*355b4669Sjacobs 
2697c478bd9Sstevel@tonic-gate char *
2707c478bd9Sstevel@tonic-gate papiServiceGetStatusMessage(papi_service_t handle)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
2737c478bd9Sstevel@tonic-gate 	char *result = NULL;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (svc != NULL)
2767c478bd9Sstevel@tonic-gate 		papiAttributeListGetString(svc->attributes, NULL,
2777c478bd9Sstevel@tonic-gate 					"detailed-status-message", &result);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	return (result);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate void
2837c478bd9Sstevel@tonic-gate detailed_error(service_t *svc, char *fmt, ...)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 	if ((svc != NULL) && (fmt != NULL)) {
2867c478bd9Sstevel@tonic-gate 		va_list ap;
2877c478bd9Sstevel@tonic-gate 		size_t size;
2887c478bd9Sstevel@tonic-gate 		char *message = alloca(BUFSIZ);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		va_start(ap, fmt);
2917c478bd9Sstevel@tonic-gate 		/*
2927c478bd9Sstevel@tonic-gate 		 * fill in the message.  If the buffer is too small, allocate
2937c478bd9Sstevel@tonic-gate 		 * one that is large enough and fill it in.
2947c478bd9Sstevel@tonic-gate 		 */
2957c478bd9Sstevel@tonic-gate 		if ((size = vsnprintf(message, BUFSIZ, fmt, ap)) >= BUFSIZ)
2967c478bd9Sstevel@tonic-gate 			if ((message = alloca(size)) != NULL)
2977c478bd9Sstevel@tonic-gate 				vsnprintf(message, size, fmt, ap);
2987c478bd9Sstevel@tonic-gate 		va_end(ap);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(&svc->attributes, PAPI_ATTR_APPEND,
3017c478bd9Sstevel@tonic-gate 					"detailed-status-message", message);
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate }
304