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 /*
232264Sjacobs  * Copyright 2006 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>
35*3125Sjacobs #include <sys/types.h>
36*3125Sjacobs #include <sys/stat.h>
37*3125Sjacobs #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 
80*3125Sjacobs static struct {
81*3125Sjacobs 	char *mime_type;
82*3125Sjacobs 	char *lp_type;
83*3125Sjacobs } type_map[] = {
84*3125Sjacobs 	{ "text/plain", "simple" },
85*3125Sjacobs 	{ "application/octet-stream", "raw" },
86*3125Sjacobs 	{ "application/octet-stream", "any" },
87*3125Sjacobs 	{ "application/postscript", "postscript" },
88*3125Sjacobs 	{ "application/postscript", "ps" },
89*3125Sjacobs 	{ "application/x-cif", "cif" },
90*3125Sjacobs 	{ "application/x-dvi", "dvi" },
91*3125Sjacobs 	{ "application/x-plot", "plot" },
92*3125Sjacobs 	{ "application/x-ditroff", "troff" },
93*3125Sjacobs 	{ "application/x-troff", "otroff" },
94*3125Sjacobs 	{ "application/x-pr", "pr" },
95*3125Sjacobs 	{ "application/x-fortran", "fortran" },
96*3125Sjacobs 	{ "application/x-raster", "raster" },
97*3125Sjacobs 	{ NULL, NULL}
98*3125Sjacobs };
99*3125Sjacobs 
100*3125Sjacobs char *
101*3125Sjacobs lp_type_to_mime_type(char *lp_type)
102*3125Sjacobs {
103*3125Sjacobs 	int i;
104*3125Sjacobs 
105*3125Sjacobs 	if (lp_type == NULL)
106*3125Sjacobs 		return ("application/octet-stream");
107*3125Sjacobs 
108*3125Sjacobs 	for (i = 0; type_map[i].lp_type != NULL; i++)
109*3125Sjacobs 		if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
110*3125Sjacobs 			return (type_map[i].mime_type);
111*3125Sjacobs 
112*3125Sjacobs 	return (lp_type);
113*3125Sjacobs }
114*3125Sjacobs 
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);
1962264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
1972264Sjacobs 					"job-originating-user-name", &user);
198*3125Sjacobs 	(void) papiAttributeListGetString(list, NULL,
199*3125Sjacobs 					"job-originating-host-name", &host);
2002264Sjacobs 
2012264Sjacobs 	/* if we are looking and it doesn't match, return early */
2022264Sjacobs 	if ((ac > 0) && (match_job(id, user, ac, av) < 0))
2032264Sjacobs 		return;
2042264Sjacobs 
2052264Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
2062264Sjacobs 					"copies", &copies);
2072264Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
2082264Sjacobs 					"number-of-intervening-jobs", &rank);
2092264Sjacobs 
2102264Sjacobs 	if (papiAttributeListGetInteger(list, NULL, "job-octets", &size)
2112264Sjacobs 			== PAPI_OK)
2122264Sjacobs 		suffix = "bytes";
2132264Sjacobs 	else
2142264Sjacobs 		(void) papiAttributeListGetInteger(list, NULL,
2152264Sjacobs 					"job-k-octets", &size);
2162264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
2172264Sjacobs 					"job-name", &name);
2182264Sjacobs 
2192264Sjacobs 	size *= copies;
2202264Sjacobs 
2212264Sjacobs 	if (fmt == 3) {
2222264Sjacobs 		fprintf(fp, gettext("%s\t%-8.8s %d\t%-32.32s%d %s\n"),
2232264Sjacobs 			rank_string(++rank), user, id, name, size, suffix);
2242264Sjacobs 	} else
2252264Sjacobs 		fprintf(fp, gettext(
2262264Sjacobs 			"\n%s: %s\t\t\t\t[job %d %s]\n\t%-32.32s\t%d %s\n"),
2272264Sjacobs 			user, rank_string(++rank), id, host, name, size,
2282264Sjacobs 			suffix);
2292264Sjacobs }
2302264Sjacobs 
2312264Sjacobs /*
2322264Sjacobs  * to support job cancelation
2332264Sjacobs  */
2342264Sjacobs static void
2352264Sjacobs cancel_job(papi_service_t svc, FILE *fp, char *printer, papi_job_t job,
2362264Sjacobs 		int ac, char *av[])
2372264Sjacobs {
2382264Sjacobs 	papi_status_t status;
2392264Sjacobs 	papi_attribute_t **list = papiJobGetAttributeList(job);
2402264Sjacobs 	int id = 0;
2412264Sjacobs 	char *user = "";
2422264Sjacobs 	char *mesg = gettext("cancelled");
2432264Sjacobs 
2442264Sjacobs 	papiAttributeListGetInteger(list, NULL,
2452264Sjacobs 					"job-id", &id);
2462264Sjacobs 	papiAttributeListGetString(list, NULL,
2472264Sjacobs 					"job-originating-user-name", &user);
2482264Sjacobs 
2492264Sjacobs 	/* if we are looking and it doesn't match, return early */
2502264Sjacobs 	if ((ac > 0) && (match_job(id, user, ac, av) < 0))
2512264Sjacobs 		return;
2522264Sjacobs 
2532264Sjacobs 	status = papiJobCancel(svc, printer, id);
2542264Sjacobs 	if (status != PAPI_OK)
2552264Sjacobs 		mesg = papiStatusString(status);
2562264Sjacobs 
2572264Sjacobs 	fprintf(fp, "%s-%d: %s\n", printer, id, mesg);
2582264Sjacobs }
2592264Sjacobs 
2602264Sjacobs int
2612264Sjacobs berkeley_queue_report(papi_service_t svc, FILE *fp, char *dest, int fmt,
2622264Sjacobs 		int ac, char *av[])
2632264Sjacobs {
2642264Sjacobs 	papi_status_t status;
2652264Sjacobs 	papi_printer_t p = NULL;
2662264Sjacobs 	papi_job_t *jobs = NULL;
2672264Sjacobs 	char *pattrs[] = { "printer-name", "printer-state",
2682264Sjacobs 			"printer-state-reasons", NULL };
269*3125Sjacobs 	char *jattrs[] = { "job-name", "job-octets", "job-k-octets", "job-id",
270*3125Sjacobs 			"job-originating-user-name",
271*3125Sjacobs 			"job-originating-host-name",
2722264Sjacobs 			"number-of-intervening-jobs", NULL };
2732264Sjacobs 	int num_jobs = 0;
2742264Sjacobs 
2752264Sjacobs 	status = papiPrinterQuery(svc, dest, pattrs, NULL, &p);
2762264Sjacobs 	if (status != PAPI_OK) {
2772264Sjacobs 		fprintf(fp, gettext(
2782264Sjacobs 			"Failed to query service for state of %s: %s\n"),
2792264Sjacobs 			dest, verbose_papi_message(svc, status));
2802264Sjacobs 		return (-1);
2812264Sjacobs 	}
2822264Sjacobs 
2832264Sjacobs 	status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
2842264Sjacobs 					0, &jobs);
2852264Sjacobs 	if (status != PAPI_OK) {
2862264Sjacobs 		fprintf(fp, gettext(
2872264Sjacobs 			"Failed to query service for jobs on %s: %s\n"),
2882264Sjacobs 			dest, verbose_papi_message(svc, status));
2892264Sjacobs 		return (-1);
2902264Sjacobs 	}
2912264Sjacobs 	if (jobs != NULL) {
2922264Sjacobs 		while (jobs[num_jobs] != NULL)
2932264Sjacobs 			num_jobs++;
2942264Sjacobs 	}
2952264Sjacobs 
2962264Sjacobs 	printer_state_line(fp, p, num_jobs, dest);
2972264Sjacobs 	if (num_jobs > 0) {
2982264Sjacobs 		int i;
2992264Sjacobs 
3002264Sjacobs 		if (fmt == 3)
3012264Sjacobs 			print_header(fp);
3022264Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
3032264Sjacobs 			print_job_line(fp, i, jobs[i], fmt, ac, av);
3042264Sjacobs 	}
3052264Sjacobs 
3062264Sjacobs 	papiPrinterFree(p);
3072264Sjacobs 	papiJobListFree(jobs);
3082264Sjacobs 
3092264Sjacobs 	return (num_jobs);
3102264Sjacobs }
3112264Sjacobs 
3122264Sjacobs int
3132264Sjacobs berkeley_cancel_request(papi_service_t svc, FILE *fp, char *dest,
3142264Sjacobs 		int ac, char *av[])
3152264Sjacobs {
3162264Sjacobs 	papi_status_t status;
3172264Sjacobs 	papi_job_t *jobs = NULL;
3182264Sjacobs 	char *jattrs[] = { "job-originating-user-name", "job-id", NULL };
3192264Sjacobs 
3202264Sjacobs 	status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
3212264Sjacobs 					0, &jobs);
3222264Sjacobs 
3232264Sjacobs 	if (status != PAPI_OK) {
3242264Sjacobs 		fprintf(fp, gettext("Failed to query service for %s: %s\n"),
3252264Sjacobs 			dest, verbose_papi_message(svc, status));
3262264Sjacobs 		return (-1);
3272264Sjacobs 	}
3282264Sjacobs 
3292264Sjacobs 	/* cancel the job(s) */
3302264Sjacobs 	if (jobs != NULL) {
3312264Sjacobs 		int i;
3322264Sjacobs 
3332264Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
3342264Sjacobs 			cancel_job(svc, fp, dest, jobs[i], ac, av);
3352264Sjacobs 	}
3362264Sjacobs 
3372264Sjacobs 	papiJobListFree(jobs);
3382264Sjacobs 
3392264Sjacobs 	return (0);
3402264Sjacobs }
3412264Sjacobs 
3422264Sjacobs int
3432264Sjacobs get_printer_id(char *name, char **printer, int *id)
3442264Sjacobs {
3452264Sjacobs 	int result = -1;
3462264Sjacobs 
3472264Sjacobs 	if (name != NULL) {
3482264Sjacobs 		char *p = strrchr(name, '-');
3492264Sjacobs 
3502264Sjacobs 		*printer = name;
3512264Sjacobs 		if (p != NULL) {
3522264Sjacobs 			char *s = NULL;
3532264Sjacobs 
3542264Sjacobs 			*id = strtol(p + 1, &s, 10);
3552264Sjacobs 			if (s[0] != '\0')
3562264Sjacobs 				*id = -1;
3572264Sjacobs 			else
3582264Sjacobs 				*p = '\0';
3592264Sjacobs 			result = 0;
3602264Sjacobs 		} else
3612264Sjacobs 			*id = -1;
3622264Sjacobs 	}
3632264Sjacobs 
3642264Sjacobs 	return (result);
3652264Sjacobs }
3662264Sjacobs 
3672264Sjacobs /*
3682264Sjacobs  * strsplit() splits a string into a NULL terminated array of substrings
3692264Sjacobs  * determined by a seperator.  The original string is modified, and newly
3702264Sjacobs  * allocated space is only returned for the array itself.  If more than
3712264Sjacobs  * 1024 substrings exist, they will be ignored.
3722264Sjacobs  */
3732264Sjacobs char **
3742264Sjacobs strsplit(char *string, const char *seperators)
3752264Sjacobs {
3762264Sjacobs 	char	*list[BUFSIZ],
3772264Sjacobs 		**result;
3782264Sjacobs 	int	length = 0;
3792264Sjacobs 
3802264Sjacobs 	if ((string == NULL) || (seperators == NULL))
3812264Sjacobs 		return (NULL);
3822264Sjacobs 
3832264Sjacobs 	(void) memset(list, 0, sizeof (list));
3842264Sjacobs 	for (list[length] = strtok(string, seperators);
3852264Sjacobs 		(list[length] != NULL) && (length < (BUFSIZ - 2));
3862264Sjacobs 		list[length] = strtok(NULL, seperators))
3872264Sjacobs 			length++;
3882264Sjacobs 
3892264Sjacobs 	if ((result = (char **)calloc(length+1, sizeof (char *))) != NULL)
3902264Sjacobs 		(void) memcpy(result, list, length * sizeof (char *));
3912264Sjacobs 
3922264Sjacobs 	return (result);
3932264Sjacobs }
3942264Sjacobs 
3952264Sjacobs papi_status_t
3962264Sjacobs jobSubmitSTDIN(papi_service_t svc, char *printer, papi_attribute_t **list,
3972264Sjacobs 		papi_job_t *job)
3982264Sjacobs {
3992264Sjacobs 	papi_status_t status;
4002264Sjacobs 	papi_stream_t stream = NULL;
4012264Sjacobs 	int rc;
4022264Sjacobs 	char buf[BUFSIZ];
4032264Sjacobs 
4042264Sjacobs 	status = papiJobStreamOpen(svc, printer, list, NULL, &stream);
4052264Sjacobs 	while ((status == PAPI_OK) && ((rc = read(0, buf, sizeof (buf))) > 0))
4062264Sjacobs 		status = papiJobStreamWrite(svc, stream, buf, rc);
4072264Sjacobs 
4082264Sjacobs 	if (status == PAPI_OK)
4092264Sjacobs 		status = papiJobStreamClose(svc, stream, job);
4102264Sjacobs 
4112264Sjacobs 	return (status);
4122264Sjacobs }
4132264Sjacobs 
414*3125Sjacobs /*
415*3125Sjacobs  * is_postscript() will detect if the file passed in contains postscript
416*3125Sjacobs  * data.  A one is returned if the file contains postscript, zero is returned
417*3125Sjacobs  * if the file is not postscript, and -1 is returned if an error occurs
418*3125Sjacobs  */
419*3125Sjacobs #define	PS_MAGIC	"%!"
420*3125Sjacobs #define	PC_PS_MAGIC	"^D%!"
421*3125Sjacobs int
422*3125Sjacobs is_postscript(const char *file)
423*3125Sjacobs {
424*3125Sjacobs 	char buf[3];
425*3125Sjacobs 	int fd;
426*3125Sjacobs 
427*3125Sjacobs 	if ((fd = open(file, O_RDONLY)) < 0)
428*3125Sjacobs 		return (-1);
429*3125Sjacobs 
430*3125Sjacobs 	if (read(fd, buf, sizeof (buf)) < 0) {
431*3125Sjacobs 		close(fd);
432*3125Sjacobs 		return (-1);
433*3125Sjacobs 	}
434*3125Sjacobs 	close(fd);
435*3125Sjacobs 
436*3125Sjacobs 	if ((strncmp(buf, PS_MAGIC, sizeof (PS_MAGIC) - 1) == 0) ||
437*3125Sjacobs 	    (strncmp(buf, PC_PS_MAGIC, sizeof (PC_PS_MAGIC) - 1) == 0))
438*3125Sjacobs 		return (1);
439*3125Sjacobs 	else
440*3125Sjacobs 		return (0);
441*3125Sjacobs }
442*3125Sjacobs 
4432264Sjacobs static char **
4442264Sjacobs all_list(papi_service_t svc)
4452264Sjacobs {
4462264Sjacobs 	papi_status_t status;
4472264Sjacobs 	papi_printer_t printer = NULL;
4482264Sjacobs 	char *list[] = { "member-names", NULL };
4492264Sjacobs 	char **result = NULL;
4502264Sjacobs 
4512264Sjacobs 	status = papiPrinterQuery(svc, "_all", list, NULL, &printer);
4522264Sjacobs 	if ((status == PAPI_OK) && (printer != NULL)) {
4532264Sjacobs 		papi_attribute_t **attributes =
4542264Sjacobs 					papiPrinterGetAttributeList(printer);
4552264Sjacobs 		if (attributes != NULL) {
4562264Sjacobs 			void *iter = NULL;
4572264Sjacobs 			char *value = NULL;
4582264Sjacobs 
4592264Sjacobs 			for (status = papiAttributeListGetString(attributes,
4602264Sjacobs 						&iter, "member-names", &value);
4612264Sjacobs 				status == PAPI_OK;
4622264Sjacobs 				status = papiAttributeListGetString(attributes,
4632264Sjacobs 						&iter, NULL, &value))
4642264Sjacobs 					list_append(&result, strdup(value));
4652264Sjacobs 		}
4662264Sjacobs 		papiPrinterFree(printer);
4672264Sjacobs 	}
4682264Sjacobs 
4692264Sjacobs 	return (result);
4702264Sjacobs }
4712264Sjacobs 
4722264Sjacobs static char **
4732264Sjacobs printers_list(papi_service_t svc)
4742264Sjacobs {
4752264Sjacobs 	papi_status_t status;
4762264Sjacobs 	papi_printer_t *printers = NULL;
4772264Sjacobs 	char *keys[] = { "printer-name", NULL };
4782264Sjacobs 	char **result = NULL;
4792264Sjacobs 
4802264Sjacobs 	status = papiPrintersList(svc, keys, NULL, &printers);
4812264Sjacobs 	if ((status == PAPI_OK) && (printers != NULL)) {
4822264Sjacobs 		int i;
4832264Sjacobs 
4842264Sjacobs 		for (i = 0; printers[i] != NULL; i++) {
4852264Sjacobs 			papi_attribute_t **attributes =
4862264Sjacobs 				papiPrinterGetAttributeList(printers[i]);
4872264Sjacobs 			char *name = NULL;
4882264Sjacobs 
4892264Sjacobs 			(void) papiAttributeListGetString(attributes, NULL,
4902264Sjacobs 						"printer-name", &name);
4912264Sjacobs 			if ((name != NULL) && (strcmp(name, "_default") != 0))
4922264Sjacobs 				list_append(&result, strdup(name));
4932264Sjacobs 		}
4942264Sjacobs 		papiPrinterListFree(printers);
4952264Sjacobs 	}
4962264Sjacobs 
4972264Sjacobs 	return (result);
4982264Sjacobs }
4992264Sjacobs 
5002264Sjacobs char **
5012264Sjacobs interest_list(papi_service_t svc)
5022264Sjacobs {
5032264Sjacobs 	static char been_here;
5042264Sjacobs 	static char **result;
5052264Sjacobs 
5062264Sjacobs 	if (been_here == 0) {	/* only do this once */
5072264Sjacobs 		been_here = 1;
5082264Sjacobs 
5092264Sjacobs 		if ((result = all_list(svc)) == NULL)
5102264Sjacobs 			result = printers_list(svc);
5112264Sjacobs 	}
5122264Sjacobs 
5132264Sjacobs 	return (result);
5142264Sjacobs }
5152264Sjacobs 
5162264Sjacobs char *
5172264Sjacobs localhostname()
5182264Sjacobs {
5192264Sjacobs 	static char *result;
5202264Sjacobs 
5212264Sjacobs 	if (result == NULL) {
5222264Sjacobs 		static char buf[256];
5232264Sjacobs 
5242264Sjacobs 		if (gethostname(buf, sizeof (buf)) == 0)
5252264Sjacobs 			result = buf;
5262264Sjacobs 	}
5272264Sjacobs 
5282264Sjacobs 	return (result);
5292264Sjacobs }
5302264Sjacobs 
5312264Sjacobs int
5322264Sjacobs cli_auth_callback(papi_service_t svc, void *app_data)
5332264Sjacobs {
5342264Sjacobs 	char prompt[BUFSIZ];
5352264Sjacobs 	char *user, *svc_name, *passphrase;
5362264Sjacobs 
5372264Sjacobs 	/* get the name of the service we are contacting */
5382264Sjacobs 	if ((svc_name = papiServiceGetServiceName(svc)) == NULL)
5392264Sjacobs 		return (-1);
5402264Sjacobs 
5412264Sjacobs 	/* find our who we are supposed to be */
5422264Sjacobs 	if ((user = papiServiceGetUserName(svc)) == NULL) {
5432264Sjacobs 		struct passwd *pw;
5442264Sjacobs 
5452264Sjacobs 		if ((pw = getpwuid(getuid())) != NULL)
5462264Sjacobs 			user = pw->pw_name;
5472264Sjacobs 		else
5482264Sjacobs 			user = "nobody";
5492264Sjacobs 	}
5502264Sjacobs 
5512264Sjacobs 	/* build the prompt string */
5522264Sjacobs 	snprintf(prompt, sizeof (prompt),
5532264Sjacobs 		gettext("passphrase for %s to access %s: "), user, svc_name);
5542264Sjacobs 
5552264Sjacobs 	/* ask for the passphrase */
5562264Sjacobs 	if ((passphrase = getpassphrase(prompt)) != NULL)
5572264Sjacobs 		papiServiceSetPassword(svc, passphrase);
5582264Sjacobs 
5592264Sjacobs 	return (0);
5602264Sjacobs }
561