12264Sjacobs /*
22264Sjacobs  * CDDL HEADER START
32264Sjacobs  *
42264Sjacobs  * The contents of this file are subject to the terms of the
52264Sjacobs  * Common Development and Distribution License (the "License").
62264Sjacobs  * You may not use this file except in compliance with the License.
72264Sjacobs  *
82264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs  * or http://www.opensolaris.org/os/licensing.
102264Sjacobs  * See the License for the specific language governing permissions
112264Sjacobs  * and limitations under the License.
122264Sjacobs  *
132264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs  *
192264Sjacobs  * CDDL HEADER END
202264Sjacobs  */
212264Sjacobs 
222264Sjacobs /*
23*6817Sjacobs  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
242264Sjacobs  * Use is subject to license terms.
252264Sjacobs  *
262264Sjacobs  */
272264Sjacobs 
282264Sjacobs /* $Id: common.c 162 2006-05-08 14:17:44Z njacobs $ */
292264Sjacobs 
302264Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
312264Sjacobs 
322264Sjacobs #include <stdio.h>
332264Sjacobs #include <stdlib.h>
342264Sjacobs #include <unistd.h>
353125Sjacobs #include <sys/types.h>
363125Sjacobs #include <sys/stat.h>
373125Sjacobs #include <fcntl.h>
382264Sjacobs #include <alloca.h>
392264Sjacobs #include <string.h>
402264Sjacobs #include <libintl.h>
412264Sjacobs #include <ctype.h>
422264Sjacobs #include <pwd.h>
432264Sjacobs #include <papi.h>
442264Sjacobs #include "common.h"
452264Sjacobs 
462264Sjacobs #ifndef HAVE_GETPASSPHRASE	/* some systems don't have getpassphrase() */
472264Sjacobs #define	getpassphrase getpass
482264Sjacobs #endif
492264Sjacobs 
502264Sjacobs /* give the most verbose error message to the caller */
512264Sjacobs char *
522264Sjacobs verbose_papi_message(papi_service_t svc, papi_status_t status)
532264Sjacobs {
542264Sjacobs 	char *mesg;
552264Sjacobs 
562264Sjacobs 	mesg = papiServiceGetStatusMessage(svc);
572264Sjacobs 
582264Sjacobs 	if (mesg == NULL)
592264Sjacobs 		mesg = papiStatusString(status);
602264Sjacobs 
612264Sjacobs 	return (mesg);
622264Sjacobs }
632264Sjacobs 
642264Sjacobs static int
652264Sjacobs match_job(int id, char *user, int ac, char *av[])
662264Sjacobs {
672264Sjacobs 	int i;
682264Sjacobs 
692264Sjacobs 	for (i = 0; i < ac; i++)
702264Sjacobs 		if (strcmp("-", av[i]) == 0)
712264Sjacobs 			return (0);	/* "current" user match */
722264Sjacobs 		else if ((isdigit(av[i][0]) != 0) && (id == atoi(av[i])))
732264Sjacobs 			return (0);	/* job-id match */
742264Sjacobs 		else if (strcmp(user, av[i]) == 0)
752264Sjacobs 			return (0);	/* user match */
762264Sjacobs 
772264Sjacobs 	return (-1);
782264Sjacobs }
792264Sjacobs 
803125Sjacobs static struct {
813125Sjacobs 	char *mime_type;
823125Sjacobs 	char *lp_type;
833125Sjacobs } type_map[] = {
843125Sjacobs 	{ "text/plain", "simple" },
853125Sjacobs 	{ "application/octet-stream", "raw" },
863125Sjacobs 	{ "application/octet-stream", "any" },
873125Sjacobs 	{ "application/postscript", "postscript" },
883125Sjacobs 	{ "application/postscript", "ps" },
893125Sjacobs 	{ "application/x-cif", "cif" },
903125Sjacobs 	{ "application/x-dvi", "dvi" },
913125Sjacobs 	{ "application/x-plot", "plot" },
923125Sjacobs 	{ "application/x-ditroff", "troff" },
933125Sjacobs 	{ "application/x-troff", "otroff" },
943125Sjacobs 	{ "application/x-pr", "pr" },
953125Sjacobs 	{ "application/x-fortran", "fortran" },
963125Sjacobs 	{ "application/x-raster", "raster" },
973125Sjacobs 	{ NULL, NULL}
983125Sjacobs };
993125Sjacobs 
1003125Sjacobs char *
1013125Sjacobs lp_type_to_mime_type(char *lp_type)
1023125Sjacobs {
1033125Sjacobs 	int i;
1043125Sjacobs 
1053125Sjacobs 	if (lp_type == NULL)
1063125Sjacobs 		return ("application/octet-stream");
1073125Sjacobs 
1083125Sjacobs 	for (i = 0; type_map[i].lp_type != NULL; i++)
1093125Sjacobs 		if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
1103125Sjacobs 			return (type_map[i].mime_type);
1113125Sjacobs 
1123125Sjacobs 	return (lp_type);
1133125Sjacobs }
1143125Sjacobs 
1152264Sjacobs /*
1162264Sjacobs  * to support job/printer status
1172264Sjacobs  */
1182264Sjacobs static char *
1192264Sjacobs state_string(int state)
1202264Sjacobs {
1212264Sjacobs 	switch (state) {
1222264Sjacobs 	case 3:
1232264Sjacobs 		return (gettext("idle"));
1242264Sjacobs 	case 4:
1252264Sjacobs 		return (gettext("processing"));
1262264Sjacobs 	case 5:
1272264Sjacobs 		return (gettext("stopped"));
1282264Sjacobs 	default:
1292264Sjacobs 		return (gettext("unknown"));
1302264Sjacobs 	}
1312264Sjacobs }
1322264Sjacobs 
1332264Sjacobs static char *_rank_suffixes[] = {
1342264Sjacobs 	"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
1352264Sjacobs };
1362264Sjacobs 
1372264Sjacobs static char *
1382264Sjacobs rank_string(const int rank)
1392264Sjacobs {
1402264Sjacobs 	static char buf[12];
1412264Sjacobs 
1422264Sjacobs 	if (rank < 0)
1432264Sjacobs 		snprintf(buf, sizeof (buf), gettext("invalid"));
1442264Sjacobs 	else if (rank == 0)
1452264Sjacobs 		snprintf(buf, sizeof (buf), gettext("active"));
1462264Sjacobs 	else if ((rank > 10) && (rank < 14))
1472264Sjacobs 		sprintf(buf, "%dth", rank);
1482264Sjacobs 	else
1492264Sjacobs 		sprintf(buf, "%d%s", rank, _rank_suffixes[rank % 10]);
1502264Sjacobs 
1512264Sjacobs 	return (buf);
1522264Sjacobs }
1532264Sjacobs 
1542264Sjacobs static void
1552264Sjacobs printer_state_line(FILE *fp, papi_printer_t p, int num_jobs, char *name)
1562264Sjacobs {
1572264Sjacobs 	papi_attribute_t **list = papiPrinterGetAttributeList(p);
1582264Sjacobs 	int state = 0;
1592264Sjacobs 	char *reason = "";
1602264Sjacobs 
1612264Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
1622264Sjacobs 				"printer-state", &state);
1632264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
1642264Sjacobs 				"printer-state-reasons", &reason);
1652264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
1662264Sjacobs 				"printer-name", &name);
1672264Sjacobs 
1682264Sjacobs 	if ((state != 0x03) || (num_jobs != 0)) {
1692264Sjacobs 		fprintf(fp, "%s: %s", name, state_string(state));
1702264Sjacobs 		if (state == 0x05) /* stopped */
1712264Sjacobs 			fprintf(fp, ": %s\n", reason);
1722264Sjacobs 		else
1732264Sjacobs 			fprintf(fp, "\n");
1742264Sjacobs 	} else
1752264Sjacobs 		fprintf(fp, "no entries\n");
1762264Sjacobs }
1772264Sjacobs 
1782264Sjacobs static void
1792264Sjacobs print_header(FILE *fp)
1802264Sjacobs {
1812264Sjacobs 	fprintf(fp, gettext("Rank\tOwner\t Job\tFile(s)\t\t\t\tTotal Size\n"));
1822264Sjacobs }
1832264Sjacobs 
1842264Sjacobs static void
1852264Sjacobs print_job_line(FILE *fp, int count, papi_job_t job, int fmt, int ac, char *av[])
1862264Sjacobs {
1872264Sjacobs 	papi_attribute_t **list = papiJobGetAttributeList(job);
1882264Sjacobs 	int copies = 1, id = 0, rank = count, size = 0;
1892264Sjacobs 	char *name = "print job";
1902264Sjacobs 	char *user = "nobody";
1912264Sjacobs 	char *host = "localhost";
1922264Sjacobs 	char *suffix = "k";
1932264Sjacobs 
1942264Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
1952264Sjacobs 					"job-id", &id);
196*6817Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
197*6817Sjacobs 					"job-id-requested", &id);
1982264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
1992264Sjacobs 					"job-originating-user-name", &user);
2003125Sjacobs 	(void) papiAttributeListGetString(list, NULL,
2013125Sjacobs 					"job-originating-host-name", &host);
2022264Sjacobs 
2032264Sjacobs 	/* if we are looking and it doesn't match, return early */
2042264Sjacobs 	if ((ac > 0) && (match_job(id, user, ac, av) < 0))
2052264Sjacobs 		return;
2062264Sjacobs 
2072264Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
2082264Sjacobs 					"copies", &copies);
2092264Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
2102264Sjacobs 					"number-of-intervening-jobs", &rank);
2112264Sjacobs 
2122264Sjacobs 	if (papiAttributeListGetInteger(list, NULL, "job-octets", &size)
2132264Sjacobs 			== PAPI_OK)
2142264Sjacobs 		suffix = "bytes";
2152264Sjacobs 	else
2162264Sjacobs 		(void) papiAttributeListGetInteger(list, NULL,
2172264Sjacobs 					"job-k-octets", &size);
2182264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
2192264Sjacobs 					"job-name", &name);
2202264Sjacobs 
2212264Sjacobs 	size *= copies;
2222264Sjacobs 
2232264Sjacobs 	if (fmt == 3) {
2242264Sjacobs 		fprintf(fp, gettext("%s\t%-8.8s %d\t%-32.32s%d %s\n"),
2252264Sjacobs 			rank_string(++rank), user, id, name, size, suffix);
2262264Sjacobs 	} else
2272264Sjacobs 		fprintf(fp, gettext(
2282264Sjacobs 			"\n%s: %s\t\t\t\t[job %d %s]\n\t%-32.32s\t%d %s\n"),
2292264Sjacobs 			user, rank_string(++rank), id, host, name, size,
2302264Sjacobs 			suffix);
2312264Sjacobs }
2322264Sjacobs 
2332264Sjacobs /*
2342264Sjacobs  * to support job cancelation
2352264Sjacobs  */
2362264Sjacobs static void
2372264Sjacobs cancel_job(papi_service_t svc, FILE *fp, char *printer, papi_job_t job,
2382264Sjacobs 		int ac, char *av[])
2392264Sjacobs {
2402264Sjacobs 	papi_status_t status;
2412264Sjacobs 	papi_attribute_t **list = papiJobGetAttributeList(job);
2422264Sjacobs 	int id = 0;
243*6817Sjacobs 	int rid = 0;
2442264Sjacobs 	char *user = "";
2452264Sjacobs 	char *mesg = gettext("cancelled");
2462264Sjacobs 
2472264Sjacobs 	papiAttributeListGetInteger(list, NULL,
2482264Sjacobs 					"job-id", &id);
249*6817Sjacobs 	papiAttributeListGetInteger(list, NULL,
250*6817Sjacobs 					"job-id-requested", &rid);
2512264Sjacobs 	papiAttributeListGetString(list, NULL,
2522264Sjacobs 					"job-originating-user-name", &user);
2532264Sjacobs 
2542264Sjacobs 	/* if we are looking and it doesn't match, return early */
255*6817Sjacobs 	if ((ac > 0) && (match_job(id, user, ac, av) < 0) &&
256*6817Sjacobs 	    (match_job(rid, user, ac, av) < 0))
2572264Sjacobs 		return;
2582264Sjacobs 
2592264Sjacobs 	status = papiJobCancel(svc, printer, id);
2602264Sjacobs 	if (status != PAPI_OK)
2612264Sjacobs 		mesg = papiStatusString(status);
2622264Sjacobs 
2632264Sjacobs 	fprintf(fp, "%s-%d: %s\n", printer, id, mesg);
2642264Sjacobs }
2652264Sjacobs 
2662264Sjacobs int
2672264Sjacobs berkeley_queue_report(papi_service_t svc, FILE *fp, char *dest, int fmt,
2682264Sjacobs 		int ac, char *av[])
2692264Sjacobs {
2702264Sjacobs 	papi_status_t status;
2712264Sjacobs 	papi_printer_t p = NULL;
2722264Sjacobs 	papi_job_t *jobs = NULL;
2732264Sjacobs 	char *pattrs[] = { "printer-name", "printer-state",
2742264Sjacobs 			"printer-state-reasons", NULL };
2753125Sjacobs 	char *jattrs[] = { "job-name", "job-octets", "job-k-octets", "job-id",
276*6817Sjacobs 			"job-originating-user-name", "job-id-requested",
2773125Sjacobs 			"job-originating-host-name",
2782264Sjacobs 			"number-of-intervening-jobs", NULL };
2792264Sjacobs 	int num_jobs = 0;
2802264Sjacobs 
2812264Sjacobs 	status = papiPrinterQuery(svc, dest, pattrs, NULL, &p);
2822264Sjacobs 	if (status != PAPI_OK) {
2832264Sjacobs 		fprintf(fp, gettext(
2842264Sjacobs 			"Failed to query service for state of %s: %s\n"),
2852264Sjacobs 			dest, verbose_papi_message(svc, status));
2862264Sjacobs 		return (-1);
2872264Sjacobs 	}
2882264Sjacobs 
2892264Sjacobs 	status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
2902264Sjacobs 					0, &jobs);
2912264Sjacobs 	if (status != PAPI_OK) {
2922264Sjacobs 		fprintf(fp, gettext(
2932264Sjacobs 			"Failed to query service for jobs on %s: %s\n"),
2942264Sjacobs 			dest, verbose_papi_message(svc, status));
2952264Sjacobs 		return (-1);
2962264Sjacobs 	}
2972264Sjacobs 	if (jobs != NULL) {
2982264Sjacobs 		while (jobs[num_jobs] != NULL)
2992264Sjacobs 			num_jobs++;
3002264Sjacobs 	}
3012264Sjacobs 
3022264Sjacobs 	printer_state_line(fp, p, num_jobs, dest);
3032264Sjacobs 	if (num_jobs > 0) {
3042264Sjacobs 		int i;
3052264Sjacobs 
3062264Sjacobs 		if (fmt == 3)
3072264Sjacobs 			print_header(fp);
3082264Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
3092264Sjacobs 			print_job_line(fp, i, jobs[i], fmt, ac, av);
3102264Sjacobs 	}
3112264Sjacobs 
3122264Sjacobs 	papiPrinterFree(p);
3132264Sjacobs 	papiJobListFree(jobs);
3142264Sjacobs 
3152264Sjacobs 	return (num_jobs);
3162264Sjacobs }
3172264Sjacobs 
3182264Sjacobs int
3192264Sjacobs berkeley_cancel_request(papi_service_t svc, FILE *fp, char *dest,
3202264Sjacobs 		int ac, char *av[])
3212264Sjacobs {
3222264Sjacobs 	papi_status_t status;
3232264Sjacobs 	papi_job_t *jobs = NULL;
324*6817Sjacobs 	char *jattrs[] = { "job-originating-user-name", "job-id",
325*6817Sjacobs 			"job-id-requested", NULL };
3262264Sjacobs 
3272264Sjacobs 	status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
3282264Sjacobs 					0, &jobs);
3292264Sjacobs 
3302264Sjacobs 	if (status != PAPI_OK) {
3312264Sjacobs 		fprintf(fp, gettext("Failed to query service for %s: %s\n"),
3322264Sjacobs 			dest, verbose_papi_message(svc, status));
3332264Sjacobs 		return (-1);
3342264Sjacobs 	}
3352264Sjacobs 
3362264Sjacobs 	/* cancel the job(s) */
3372264Sjacobs 	if (jobs != NULL) {
3382264Sjacobs 		int i;
3392264Sjacobs 
3402264Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
3412264Sjacobs 			cancel_job(svc, fp, dest, jobs[i], ac, av);
3422264Sjacobs 	}
3432264Sjacobs 
3442264Sjacobs 	papiJobListFree(jobs);
3452264Sjacobs 
3462264Sjacobs 	return (0);
3472264Sjacobs }
3482264Sjacobs 
3492264Sjacobs int
3502264Sjacobs get_printer_id(char *name, char **printer, int *id)
3512264Sjacobs {
3522264Sjacobs 	int result = -1;
3532264Sjacobs 
3542264Sjacobs 	if (name != NULL) {
3552264Sjacobs 		char *p = strrchr(name, '-');
3562264Sjacobs 
3572264Sjacobs 		*printer = name;
3582264Sjacobs 		if (p != NULL) {
3592264Sjacobs 			char *s = NULL;
3602264Sjacobs 
3612264Sjacobs 			*id = strtol(p + 1, &s, 10);
3622264Sjacobs 			if (s[0] != '\0')
3632264Sjacobs 				*id = -1;
3642264Sjacobs 			else
3652264Sjacobs 				*p = '\0';
3662264Sjacobs 			result = 0;
3672264Sjacobs 		} else
3682264Sjacobs 			*id = -1;
3692264Sjacobs 	}
3702264Sjacobs 
3712264Sjacobs 	return (result);
3722264Sjacobs }
3732264Sjacobs 
3742264Sjacobs /*
3752264Sjacobs  * strsplit() splits a string into a NULL terminated array of substrings
3762264Sjacobs  * determined by a seperator.  The original string is modified, and newly
3772264Sjacobs  * allocated space is only returned for the array itself.  If more than
3782264Sjacobs  * 1024 substrings exist, they will be ignored.
3792264Sjacobs  */
3802264Sjacobs char **
3812264Sjacobs strsplit(char *string, const char *seperators)
3822264Sjacobs {
3832264Sjacobs 	char	*list[BUFSIZ],
3842264Sjacobs 		**result;
3852264Sjacobs 	int	length = 0;
3862264Sjacobs 
3872264Sjacobs 	if ((string == NULL) || (seperators == NULL))
3882264Sjacobs 		return (NULL);
3892264Sjacobs 
3902264Sjacobs 	(void) memset(list, 0, sizeof (list));
3912264Sjacobs 	for (list[length] = strtok(string, seperators);
3922264Sjacobs 		(list[length] != NULL) && (length < (BUFSIZ - 2));
3932264Sjacobs 		list[length] = strtok(NULL, seperators))
3942264Sjacobs 			length++;
3952264Sjacobs 
3962264Sjacobs 	if ((result = (char **)calloc(length+1, sizeof (char *))) != NULL)
3972264Sjacobs 		(void) memcpy(result, list, length * sizeof (char *));
3982264Sjacobs 
3992264Sjacobs 	return (result);
4002264Sjacobs }
4012264Sjacobs 
4022264Sjacobs papi_status_t
4032264Sjacobs jobSubmitSTDIN(papi_service_t svc, char *printer, papi_attribute_t **list,
4042264Sjacobs 		papi_job_t *job)
4052264Sjacobs {
4062264Sjacobs 	papi_status_t status;
4072264Sjacobs 	papi_stream_t stream = NULL;
4082264Sjacobs 	int rc;
4092264Sjacobs 	char buf[BUFSIZ];
4102264Sjacobs 
4112264Sjacobs 	status = papiJobStreamOpen(svc, printer, list, NULL, &stream);
4122264Sjacobs 	while ((status == PAPI_OK) && ((rc = read(0, buf, sizeof (buf))) > 0))
4132264Sjacobs 		status = papiJobStreamWrite(svc, stream, buf, rc);
4142264Sjacobs 
4152264Sjacobs 	if (status == PAPI_OK)
4162264Sjacobs 		status = papiJobStreamClose(svc, stream, job);
4172264Sjacobs 
4182264Sjacobs 	return (status);
4192264Sjacobs }
4202264Sjacobs 
4213125Sjacobs /*
4223125Sjacobs  * is_postscript() will detect if the file passed in contains postscript
4233125Sjacobs  * data.  A one is returned if the file contains postscript, zero is returned
4243125Sjacobs  * if the file is not postscript, and -1 is returned if an error occurs
4253125Sjacobs  */
4263125Sjacobs #define	PS_MAGIC	"%!"
4273125Sjacobs #define	PC_PS_MAGIC	"^D%!"
4283125Sjacobs int
4293125Sjacobs is_postscript(const char *file)
4303125Sjacobs {
4313125Sjacobs 	char buf[3];
4323125Sjacobs 	int fd;
4333125Sjacobs 
4343125Sjacobs 	if ((fd = open(file, O_RDONLY)) < 0)
4353125Sjacobs 		return (-1);
4363125Sjacobs 
4373125Sjacobs 	if (read(fd, buf, sizeof (buf)) < 0) {
4383125Sjacobs 		close(fd);
4393125Sjacobs 		return (-1);
4403125Sjacobs 	}
4413125Sjacobs 	close(fd);
4423125Sjacobs 
4433125Sjacobs 	if ((strncmp(buf, PS_MAGIC, sizeof (PS_MAGIC) - 1) == 0) ||
4443125Sjacobs 	    (strncmp(buf, PC_PS_MAGIC, sizeof (PC_PS_MAGIC) - 1) == 0))
4453125Sjacobs 		return (1);
4463125Sjacobs 	else
4473125Sjacobs 		return (0);
4483125Sjacobs }
4493125Sjacobs 
4502264Sjacobs static char **
4512264Sjacobs all_list(papi_service_t svc)
4522264Sjacobs {
4532264Sjacobs 	papi_status_t status;
4542264Sjacobs 	papi_printer_t printer = NULL;
4552264Sjacobs 	char *list[] = { "member-names", NULL };
4562264Sjacobs 	char **result = NULL;
4572264Sjacobs 
4582264Sjacobs 	status = papiPrinterQuery(svc, "_all", list, NULL, &printer);
4592264Sjacobs 	if ((status == PAPI_OK) && (printer != NULL)) {
4602264Sjacobs 		papi_attribute_t **attributes =
4612264Sjacobs 					papiPrinterGetAttributeList(printer);
4622264Sjacobs 		if (attributes != NULL) {
4632264Sjacobs 			void *iter = NULL;
4642264Sjacobs 			char *value = NULL;
4652264Sjacobs 
4662264Sjacobs 			for (status = papiAttributeListGetString(attributes,
4672264Sjacobs 						&iter, "member-names", &value);
4682264Sjacobs 				status == PAPI_OK;
4692264Sjacobs 				status = papiAttributeListGetString(attributes,
4702264Sjacobs 						&iter, NULL, &value))
4712264Sjacobs 					list_append(&result, strdup(value));
4722264Sjacobs 		}
4732264Sjacobs 		papiPrinterFree(printer);
4742264Sjacobs 	}
4752264Sjacobs 
4762264Sjacobs 	return (result);
4772264Sjacobs }
4782264Sjacobs 
4792264Sjacobs static char **
4802264Sjacobs printers_list(papi_service_t svc)
4812264Sjacobs {
4822264Sjacobs 	papi_status_t status;
4832264Sjacobs 	papi_printer_t *printers = NULL;
4842264Sjacobs 	char *keys[] = { "printer-name", NULL };
4852264Sjacobs 	char **result = NULL;
4862264Sjacobs 
4872264Sjacobs 	status = papiPrintersList(svc, keys, NULL, &printers);
4882264Sjacobs 	if ((status == PAPI_OK) && (printers != NULL)) {
4892264Sjacobs 		int i;
4902264Sjacobs 
4912264Sjacobs 		for (i = 0; printers[i] != NULL; i++) {
4922264Sjacobs 			papi_attribute_t **attributes =
4932264Sjacobs 				papiPrinterGetAttributeList(printers[i]);
4942264Sjacobs 			char *name = NULL;
4952264Sjacobs 
4962264Sjacobs 			(void) papiAttributeListGetString(attributes, NULL,
4972264Sjacobs 						"printer-name", &name);
4982264Sjacobs 			if ((name != NULL) && (strcmp(name, "_default") != 0))
4992264Sjacobs 				list_append(&result, strdup(name));
5002264Sjacobs 		}
5012264Sjacobs 		papiPrinterListFree(printers);
5022264Sjacobs 	}
5032264Sjacobs 
5042264Sjacobs 	return (result);
5052264Sjacobs }
5062264Sjacobs 
5072264Sjacobs char **
5082264Sjacobs interest_list(papi_service_t svc)
5092264Sjacobs {
5102264Sjacobs 	static char been_here;
5112264Sjacobs 	static char **result;
5122264Sjacobs 
5132264Sjacobs 	if (been_here == 0) {	/* only do this once */
5142264Sjacobs 		been_here = 1;
5152264Sjacobs 
5162264Sjacobs 		if ((result = all_list(svc)) == NULL)
5172264Sjacobs 			result = printers_list(svc);
5182264Sjacobs 	}
5192264Sjacobs 
5202264Sjacobs 	return (result);
5212264Sjacobs }
5222264Sjacobs 
5232264Sjacobs char *
5242264Sjacobs localhostname()
5252264Sjacobs {
5262264Sjacobs 	static char *result;
5272264Sjacobs 
5282264Sjacobs 	if (result == NULL) {
5292264Sjacobs 		static char buf[256];
5302264Sjacobs 
5312264Sjacobs 		if (gethostname(buf, sizeof (buf)) == 0)
5322264Sjacobs 			result = buf;
5332264Sjacobs 	}
5342264Sjacobs 
5352264Sjacobs 	return (result);
5362264Sjacobs }
5372264Sjacobs 
5382264Sjacobs int
5392264Sjacobs cli_auth_callback(papi_service_t svc, void *app_data)
5402264Sjacobs {
5412264Sjacobs 	char prompt[BUFSIZ];
5422264Sjacobs 	char *user, *svc_name, *passphrase;
5432264Sjacobs 
5442264Sjacobs 	/* get the name of the service we are contacting */
5452264Sjacobs 	if ((svc_name = papiServiceGetServiceName(svc)) == NULL)
5462264Sjacobs 		return (-1);
5472264Sjacobs 
5482264Sjacobs 	/* find our who we are supposed to be */
5492264Sjacobs 	if ((user = papiServiceGetUserName(svc)) == NULL) {
5502264Sjacobs 		struct passwd *pw;
5512264Sjacobs 
5522264Sjacobs 		if ((pw = getpwuid(getuid())) != NULL)
5532264Sjacobs 			user = pw->pw_name;
5542264Sjacobs 		else
5552264Sjacobs 			user = "nobody";
5562264Sjacobs 	}
5572264Sjacobs 
5582264Sjacobs 	/* build the prompt string */
5592264Sjacobs 	snprintf(prompt, sizeof (prompt),
5602264Sjacobs 		gettext("passphrase for %s to access %s: "), user, svc_name);
5612264Sjacobs 
5622264Sjacobs 	/* ask for the passphrase */
5632264Sjacobs 	if ((passphrase = getpassphrase(prompt)) != NULL)
5642264Sjacobs 		papiServiceSetPassword(svc, passphrase);
5652264Sjacobs 
5662264Sjacobs 	return (0);
5672264Sjacobs }
568