xref: /illumos-gate/usr/src/cmd/lp/lib/papi/service.c (revision b666b5be)
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 #include <stdlib.h>
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdarg.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <libintl.h>
317c478bd9Sstevel@tonic-gate #include <papi_impl.h>
327c478bd9Sstevel@tonic-gate 
3345916cd2Sjpk #include <tsol/label.h>
3445916cd2Sjpk 
357c478bd9Sstevel@tonic-gate 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)36355b4669Sjacobs papiServiceCreate(papi_service_t *handle, char *service_name,
37355b4669Sjacobs 		char *user_name, char *password,
38355b4669Sjacobs 		int (*authCB)(papi_service_t svc, void *app_data),
39355b4669Sjacobs 		papi_encryption_t encryption, void *app_data)
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate 	service_t *svc = NULL;
427c478bd9Sstevel@tonic-gate 	char *path = Lp_FIFO;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	if (handle == NULL)
457c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	if ((*handle = svc = calloc(1, sizeof (*svc))) == NULL)
487c478bd9Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	svc->md = mconnect(path, 0, 0);
517c478bd9Sstevel@tonic-gate 	if (svc->md == NULL) {
527c478bd9Sstevel@tonic-gate 		detailed_error(svc,
537c478bd9Sstevel@tonic-gate 			gettext("can't connect to spooler for %s: %s"),
547c478bd9Sstevel@tonic-gate 			(service_name ? service_name : ""), strerror(errno));
557c478bd9Sstevel@tonic-gate 		return (PAPI_SERVICE_UNAVAILABLE);
567c478bd9Sstevel@tonic-gate 	}
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	svc->msgbuf_size = MSGMAX;
597c478bd9Sstevel@tonic-gate 	if ((svc->msgbuf = calloc(1, svc->msgbuf_size)) == NULL)
607c478bd9Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	if (service_name != NULL)
637c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(&svc->attributes, PAPI_ATTR_EXCL,
647c478bd9Sstevel@tonic-gate 				"service-name", service_name);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	(void) papiServiceSetUserName(svc, user_name);
677c478bd9Sstevel@tonic-gate 	(void) papiServiceSetPassword(svc, password);
687c478bd9Sstevel@tonic-gate 	(void) papiServiceSetAuthCB(svc, authCB);
697c478bd9Sstevel@tonic-gate 	(void) papiServiceSetAppData(svc, app_data);
707c478bd9Sstevel@tonic-gate 	(void) papiServiceSetEncryption(svc, encryption);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate void
papiServiceDestroy(papi_service_t handle)767c478bd9Sstevel@tonic-gate papiServiceDestroy(papi_service_t handle)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (svc != NULL) {
817c478bd9Sstevel@tonic-gate 		if (svc->md != NULL)
827c478bd9Sstevel@tonic-gate 			mdisconnect(svc->md);
837c478bd9Sstevel@tonic-gate 		if (svc->msgbuf != NULL)
847c478bd9Sstevel@tonic-gate 			free(svc->msgbuf);
857c478bd9Sstevel@tonic-gate 		papiAttributeListFree(svc->attributes);
867c478bd9Sstevel@tonic-gate 		free(svc);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
9045916cd2Sjpk /*
9145916cd2Sjpk  * interface for passing a peer's connection to gather sensitivity labeling
9245916cd2Sjpk  * from for Trusted Solaris.
9345916cd2Sjpk  */
9445916cd2Sjpk papi_status_t
papiServiceSetPeer(papi_service_t handle,int peerfd)9545916cd2Sjpk papiServiceSetPeer(papi_service_t handle, int peerfd)
9645916cd2Sjpk {
9745916cd2Sjpk 	papi_status_t result = PAPI_OK;
9845916cd2Sjpk 	service_t *svc = handle;
9945916cd2Sjpk 
10045916cd2Sjpk 	if (svc == NULL)
10145916cd2Sjpk 		return (PAPI_BAD_ARGUMENT);
10245916cd2Sjpk 
103b9dac67bSrica 	if (is_system_labeled()) {
10445916cd2Sjpk 		short status;
10545916cd2Sjpk 
10645916cd2Sjpk 		if ((snd_msg(svc, S_PASS_PEER_CONNECTION) < 0) ||
10745916cd2Sjpk 		    (ioctl(svc->md->writefd, I_SENDFD, peerfd) < 0) ||
10845916cd2Sjpk 		    (rcv_msg(svc, R_PASS_PEER_CONNECTION, &status) < 0))
10945916cd2Sjpk 			status = MTRANSMITERR;
11045916cd2Sjpk 
11145916cd2Sjpk 		if (status != MOK) {
11245916cd2Sjpk 			detailed_error(svc,
11345916cd2Sjpk 				gettext("failed to send peer connection: %s"),
11445916cd2Sjpk 				lpsched_status_string(status));
11545916cd2Sjpk 			result = lpsched_status_to_papi_status(status);
11645916cd2Sjpk 		}
11745916cd2Sjpk 	}
11845916cd2Sjpk 
11945916cd2Sjpk 	return (result);
12045916cd2Sjpk }
12145916cd2Sjpk 
1227c478bd9Sstevel@tonic-gate papi_status_t
papiServiceSetUserName(papi_service_t handle,char * user_name)123355b4669Sjacobs papiServiceSetUserName(papi_service_t handle, char *user_name)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (svc == NULL)
1287c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
1317c478bd9Sstevel@tonic-gate 				"user-name", user_name));
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate papi_status_t
papiServiceSetPassword(papi_service_t handle,char * password)135355b4669Sjacobs papiServiceSetPassword(papi_service_t handle, char *password)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (svc == NULL)
1407c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
1437c478bd9Sstevel@tonic-gate 				"password", password));
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate papi_status_t
papiServiceSetEncryption(papi_service_t handle,papi_encryption_t encryption)1477c478bd9Sstevel@tonic-gate papiServiceSetEncryption(papi_service_t handle,
148355b4669Sjacobs 			papi_encryption_t encryption)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (svc == NULL)
1537c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	return (papiAttributeListAddInteger(&svc->attributes, PAPI_ATTR_REPLACE,
1567c478bd9Sstevel@tonic-gate 				"encryption", (int)encryption));
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate papi_status_t
papiServiceSetAuthCB(papi_service_t handle,int (* authCB)(papi_service_t svc,void * app_data))1607c478bd9Sstevel@tonic-gate papiServiceSetAuthCB(papi_service_t handle,
161355b4669Sjacobs 			int (*authCB)(papi_service_t svc, void *app_data))
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (svc == NULL)
1667c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
1677c478bd9Sstevel@tonic-gate 
168355b4669Sjacobs 	svc->authCB = (int (*)(papi_service_t svc, void *app_data))authCB;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate papi_status_t
papiServiceSetAppData(papi_service_t handle,void * app_data)174355b4669Sjacobs papiServiceSetAppData(papi_service_t handle, void *app_data)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (svc == NULL)
1797c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	svc->app_data = (void *)app_data;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate char *
papiServiceGetServiceName(papi_service_t handle)1877c478bd9Sstevel@tonic-gate papiServiceGetServiceName(papi_service_t handle)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
1907c478bd9Sstevel@tonic-gate 	char *result = NULL;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (svc != NULL)
1937c478bd9Sstevel@tonic-gate 		papiAttributeListGetString(svc->attributes, NULL,
1947c478bd9Sstevel@tonic-gate 					"service-name", &result);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	return (result);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate char *
papiServiceGetUserName(papi_service_t handle)2007c478bd9Sstevel@tonic-gate papiServiceGetUserName(papi_service_t handle)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
2037c478bd9Sstevel@tonic-gate 	char *result = NULL;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	if (svc != NULL)
2067c478bd9Sstevel@tonic-gate 		papiAttributeListGetString(svc->attributes, NULL,
2077c478bd9Sstevel@tonic-gate 					"user-name", &result);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	return (result);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate char *
papiServiceGetPassword(papi_service_t handle)2137c478bd9Sstevel@tonic-gate papiServiceGetPassword(papi_service_t handle)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
2167c478bd9Sstevel@tonic-gate 	char *result = NULL;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (svc != NULL)
2197c478bd9Sstevel@tonic-gate 		papiAttributeListGetString(svc->attributes, NULL,
2207c478bd9Sstevel@tonic-gate 					"password", &result);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	return (result);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate papi_encryption_t
papiServiceGetEncryption(papi_service_t handle)2267c478bd9Sstevel@tonic-gate papiServiceGetEncryption(papi_service_t handle)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
2297c478bd9Sstevel@tonic-gate 	papi_encryption_t result = PAPI_ENCRYPT_NEVER;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if (svc != NULL)
2327c478bd9Sstevel@tonic-gate 		papiAttributeListGetInteger(svc->attributes, NULL,
2337c478bd9Sstevel@tonic-gate 					"encryption", (int *)&result);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	return (result);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate void *
papiServiceGetAppData(papi_service_t handle)2397c478bd9Sstevel@tonic-gate papiServiceGetAppData(papi_service_t handle)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
2427c478bd9Sstevel@tonic-gate 	void *result = NULL;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (svc != NULL)
2457c478bd9Sstevel@tonic-gate 		result = svc->app_data;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	return (result);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
250355b4669Sjacobs papi_attribute_t **
papiServiceGetAttributeList(papi_service_t handle)251355b4669Sjacobs papiServiceGetAttributeList(papi_service_t handle)
252355b4669Sjacobs {
253355b4669Sjacobs 	service_t *svc = handle;
254355b4669Sjacobs 	papi_attribute_t **result = NULL;
255355b4669Sjacobs 
256355b4669Sjacobs 	if (svc != NULL) {
257355b4669Sjacobs 		lpsched_service_information(&svc->attributes);
258355b4669Sjacobs 		result = svc->attributes;
259355b4669Sjacobs 	}
260355b4669Sjacobs 
261355b4669Sjacobs 	return (result);
262355b4669Sjacobs }
263355b4669Sjacobs 
2647c478bd9Sstevel@tonic-gate char *
papiServiceGetStatusMessage(papi_service_t handle)2657c478bd9Sstevel@tonic-gate papiServiceGetStatusMessage(papi_service_t handle)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
2687c478bd9Sstevel@tonic-gate 	char *result = NULL;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (svc != NULL)
2717c478bd9Sstevel@tonic-gate 		papiAttributeListGetString(svc->attributes, NULL,
2727c478bd9Sstevel@tonic-gate 					"detailed-status-message", &result);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	return (result);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate void
detailed_error(service_t * svc,char * fmt,...)2787c478bd9Sstevel@tonic-gate detailed_error(service_t *svc, char *fmt, ...)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	if ((svc != NULL) && (fmt != NULL)) {
2817c478bd9Sstevel@tonic-gate 		va_list ap;
282*b666b5beSToomas Soome 		char *message;
283*b666b5beSToomas Soome 		int rv;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 		va_start(ap, fmt);
286*b666b5beSToomas Soome 		rv = vasprintf(&message, fmt, ap);
2877c478bd9Sstevel@tonic-gate 		va_end(ap);
2887c478bd9Sstevel@tonic-gate 
289*b666b5beSToomas Soome 		if (rv >= 0) {
290*b666b5beSToomas Soome 			papiAttributeListAddString(&svc->attributes,
291*b666b5beSToomas Soome 			    PAPI_ATTR_APPEND, "detailed-status-message",
292*b666b5beSToomas Soome 			    message);
293*b666b5beSToomas Soome 			free(message);
294*b666b5beSToomas Soome 		}
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate }
297