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 /*
238966SSonam.Gupta@Sun.COM  * Copyright 2009 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 #include <stdio.h>
312264Sjacobs #include <stdlib.h>
322264Sjacobs #include <unistd.h>
333125Sjacobs #include <sys/types.h>
343125Sjacobs #include <sys/stat.h>
353125Sjacobs #include <fcntl.h>
362264Sjacobs #include <alloca.h>
372264Sjacobs #include <string.h>
382264Sjacobs #include <libintl.h>
392264Sjacobs #include <ctype.h>
402264Sjacobs #include <pwd.h>
412264Sjacobs #include <papi.h>
422264Sjacobs #include "common.h"
432264Sjacobs 
442264Sjacobs #ifndef HAVE_GETPASSPHRASE	/* some systems don't have getpassphrase() */
452264Sjacobs #define	getpassphrase getpass
462264Sjacobs #endif
472264Sjacobs 
482264Sjacobs /* give the most verbose error message to the caller */
492264Sjacobs char *
502264Sjacobs verbose_papi_message(papi_service_t svc, papi_status_t status)
512264Sjacobs {
522264Sjacobs 	char *mesg;
532264Sjacobs 
542264Sjacobs 	mesg = papiServiceGetStatusMessage(svc);
552264Sjacobs 
562264Sjacobs 	if (mesg == NULL)
572264Sjacobs 		mesg = papiStatusString(status);
582264Sjacobs 
592264Sjacobs 	return (mesg);
602264Sjacobs }
612264Sjacobs 
622264Sjacobs static int
632264Sjacobs match_job(int id, char *user, int ac, char *av[])
642264Sjacobs {
652264Sjacobs 	int i;
662264Sjacobs 
672264Sjacobs 	for (i = 0; i < ac; i++)
682264Sjacobs 		if (strcmp("-", av[i]) == 0)
692264Sjacobs 			return (0);	/* "current" user match */
702264Sjacobs 		else if ((isdigit(av[i][0]) != 0) && (id == atoi(av[i])))
712264Sjacobs 			return (0);	/* job-id match */
722264Sjacobs 		else if (strcmp(user, av[i]) == 0)
732264Sjacobs 			return (0);	/* user match */
742264Sjacobs 
752264Sjacobs 	return (-1);
762264Sjacobs }
772264Sjacobs 
783125Sjacobs static struct {
793125Sjacobs 	char *mime_type;
803125Sjacobs 	char *lp_type;
813125Sjacobs } type_map[] = {
823125Sjacobs 	{ "text/plain", "simple" },
833125Sjacobs 	{ "application/octet-stream", "raw" },
843125Sjacobs 	{ "application/octet-stream", "any" },
853125Sjacobs 	{ "application/postscript", "postscript" },
863125Sjacobs 	{ "application/postscript", "ps" },
873125Sjacobs 	{ "application/x-cif", "cif" },
883125Sjacobs 	{ "application/x-dvi", "dvi" },
893125Sjacobs 	{ "application/x-plot", "plot" },
903125Sjacobs 	{ "application/x-ditroff", "troff" },
913125Sjacobs 	{ "application/x-troff", "otroff" },
923125Sjacobs 	{ "application/x-pr", "pr" },
933125Sjacobs 	{ "application/x-fortran", "fortran" },
943125Sjacobs 	{ "application/x-raster", "raster" },
953125Sjacobs 	{ NULL, NULL}
963125Sjacobs };
973125Sjacobs 
983125Sjacobs char *
993125Sjacobs lp_type_to_mime_type(char *lp_type)
1003125Sjacobs {
1013125Sjacobs 	int i;
1023125Sjacobs 
1033125Sjacobs 	if (lp_type == NULL)
1043125Sjacobs 		return ("application/octet-stream");
1053125Sjacobs 
1063125Sjacobs 	for (i = 0; type_map[i].lp_type != NULL; i++)
1073125Sjacobs 		if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
1083125Sjacobs 			return (type_map[i].mime_type);
1093125Sjacobs 
1103125Sjacobs 	return (lp_type);
1113125Sjacobs }
1123125Sjacobs 
1132264Sjacobs /*
1142264Sjacobs  * to support job/printer status
1152264Sjacobs  */
1162264Sjacobs static char *
1172264Sjacobs state_string(int state)
1182264Sjacobs {
1192264Sjacobs 	switch (state) {
1202264Sjacobs 	case 3:
1212264Sjacobs 		return (gettext("idle"));
1222264Sjacobs 	case 4:
1232264Sjacobs 		return (gettext("processing"));
1242264Sjacobs 	case 5:
1252264Sjacobs 		return (gettext("stopped"));
1262264Sjacobs 	default:
1272264Sjacobs 		return (gettext("unknown"));
1282264Sjacobs 	}
1292264Sjacobs }
1302264Sjacobs 
1312264Sjacobs static char *_rank_suffixes[] = {
1322264Sjacobs 	"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
1332264Sjacobs };
1342264Sjacobs 
1352264Sjacobs static char *
1362264Sjacobs rank_string(const int rank)
1372264Sjacobs {
1382264Sjacobs 	static char buf[12];
1392264Sjacobs 
1402264Sjacobs 	if (rank < 0)
1412264Sjacobs 		snprintf(buf, sizeof (buf), gettext("invalid"));
1422264Sjacobs 	else if (rank == 0)
1432264Sjacobs 		snprintf(buf, sizeof (buf), gettext("active"));
1442264Sjacobs 	else if ((rank > 10) && (rank < 14))
1452264Sjacobs 		sprintf(buf, "%dth", rank);
1462264Sjacobs 	else
1472264Sjacobs 		sprintf(buf, "%d%s", rank, _rank_suffixes[rank % 10]);
1482264Sjacobs 
1492264Sjacobs 	return (buf);
1502264Sjacobs }
1512264Sjacobs 
1522264Sjacobs static void
1532264Sjacobs printer_state_line(FILE *fp, papi_printer_t p, int num_jobs, char *name)
1542264Sjacobs {
1552264Sjacobs 	papi_attribute_t **list = papiPrinterGetAttributeList(p);
1562264Sjacobs 	int state = 0;
1572264Sjacobs 	char *reason = "";
1582264Sjacobs 
1592264Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
1608966SSonam.Gupta@Sun.COM 	    "printer-state", &state);
1612264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
1628966SSonam.Gupta@Sun.COM 	    "printer-state-reasons", &reason);
1632264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
1648966SSonam.Gupta@Sun.COM 	    "printer-name", &name);
1652264Sjacobs 
1662264Sjacobs 	if ((state != 0x03) || (num_jobs != 0)) {
1672264Sjacobs 		fprintf(fp, "%s: %s", name, state_string(state));
1682264Sjacobs 		if (state == 0x05) /* stopped */
1692264Sjacobs 			fprintf(fp, ": %s\n", reason);
1702264Sjacobs 		else
1712264Sjacobs 			fprintf(fp, "\n");
1722264Sjacobs 	} else
1732264Sjacobs 		fprintf(fp, "no entries\n");
1742264Sjacobs }
1752264Sjacobs 
1762264Sjacobs static void
1772264Sjacobs print_header(FILE *fp)
1782264Sjacobs {
1792264Sjacobs 	fprintf(fp, gettext("Rank\tOwner\t Job\tFile(s)\t\t\t\tTotal Size\n"));
1802264Sjacobs }
1812264Sjacobs 
1822264Sjacobs static void
1832264Sjacobs print_job_line(FILE *fp, int count, papi_job_t job, int fmt, int ac, char *av[])
1842264Sjacobs {
1852264Sjacobs 	papi_attribute_t **list = papiJobGetAttributeList(job);
1862264Sjacobs 	int copies = 1, id = 0, rank = count, size = 0;
1872264Sjacobs 	char *name = "print job";
1882264Sjacobs 	char *user = "nobody";
1892264Sjacobs 	char *host = "localhost";
1902264Sjacobs 	char *suffix = "k";
1912264Sjacobs 
1922264Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
1938966SSonam.Gupta@Sun.COM 	    "job-id", &id);
1946817Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
1958966SSonam.Gupta@Sun.COM 	    "job-id-requested", &id);
1962264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
1978966SSonam.Gupta@Sun.COM 	    "job-originating-user-name", &user);
1983125Sjacobs 	(void) papiAttributeListGetString(list, NULL,
1998966SSonam.Gupta@Sun.COM 	    "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,
2068966SSonam.Gupta@Sun.COM 	    "copies", &copies);
2072264Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
2088966SSonam.Gupta@Sun.COM 	    "number-of-intervening-jobs", &rank);
2092264Sjacobs 
2102264Sjacobs 	if (papiAttributeListGetInteger(list, NULL, "job-octets", &size)
2118966SSonam.Gupta@Sun.COM 	    == PAPI_OK)
2122264Sjacobs 		suffix = "bytes";
2132264Sjacobs 	else
2142264Sjacobs 		(void) papiAttributeListGetInteger(list, NULL,
2158966SSonam.Gupta@Sun.COM 		    "job-k-octets", &size);
2162264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
2178966SSonam.Gupta@Sun.COM 	    "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"),
2238966SSonam.Gupta@Sun.COM 		    rank_string(++rank), user, id, name, size, suffix);
2242264Sjacobs 	} else
2252264Sjacobs 		fprintf(fp, gettext(
2268966SSonam.Gupta@Sun.COM 		    "\n%s: %s\t\t\t\t[job %d %s]\n\t%-32.32s\t%d %s\n"),
2278966SSonam.Gupta@Sun.COM 		    user, rank_string(++rank), id, host, name, size,
2288966SSonam.Gupta@Sun.COM 		    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;
2416817Sjacobs 	int rid = 0;
2422264Sjacobs 	char *user = "";
2432264Sjacobs 	char *mesg = gettext("cancelled");
2442264Sjacobs 
2452264Sjacobs 	papiAttributeListGetInteger(list, NULL,
2468966SSonam.Gupta@Sun.COM 	    "job-id", &id);
2476817Sjacobs 	papiAttributeListGetInteger(list, NULL,
2488966SSonam.Gupta@Sun.COM 	    "job-id-requested", &rid);
2492264Sjacobs 	papiAttributeListGetString(list, NULL,
2508966SSonam.Gupta@Sun.COM 	    "job-originating-user-name", &user);
2512264Sjacobs 
2522264Sjacobs 	/* if we are looking and it doesn't match, return early */
2536817Sjacobs 	if ((ac > 0) && (match_job(id, user, ac, av) < 0) &&
2546817Sjacobs 	    (match_job(rid, user, ac, av) < 0))
2552264Sjacobs 		return;
2562264Sjacobs 
2572264Sjacobs 	status = papiJobCancel(svc, printer, id);
2582264Sjacobs 	if (status != PAPI_OK)
2592264Sjacobs 		mesg = papiStatusString(status);
2602264Sjacobs 
261*9330SSonam.Gupta@Sun.COM 	if (rid != 0)
262*9330SSonam.Gupta@Sun.COM 		fprintf(fp, "%s-%d: %s\n", printer, rid, mesg);
263*9330SSonam.Gupta@Sun.COM 	else
2648966SSonam.Gupta@Sun.COM 		fprintf(fp, "%s-%d: %s\n", printer, id, mesg);
2652264Sjacobs }
2662264Sjacobs 
2672264Sjacobs int
2682264Sjacobs berkeley_queue_report(papi_service_t svc, FILE *fp, char *dest, int fmt,
2692264Sjacobs 		int ac, char *av[])
2702264Sjacobs {
2712264Sjacobs 	papi_status_t status;
2722264Sjacobs 	papi_printer_t p = NULL;
2732264Sjacobs 	papi_job_t *jobs = NULL;
2742264Sjacobs 	char *pattrs[] = { "printer-name", "printer-state",
2752264Sjacobs 			"printer-state-reasons", NULL };
2763125Sjacobs 	char *jattrs[] = { "job-name", "job-octets", "job-k-octets", "job-id",
2776817Sjacobs 			"job-originating-user-name", "job-id-requested",
2783125Sjacobs 			"job-originating-host-name",
2792264Sjacobs 			"number-of-intervening-jobs", NULL };
2802264Sjacobs 	int num_jobs = 0;
2812264Sjacobs 
2822264Sjacobs 	status = papiPrinterQuery(svc, dest, pattrs, NULL, &p);
2832264Sjacobs 	if (status != PAPI_OK) {
2842264Sjacobs 		fprintf(fp, gettext(
2858966SSonam.Gupta@Sun.COM 		    "Failed to query service for state of %s: %s\n"),
2868966SSonam.Gupta@Sun.COM 		    dest, verbose_papi_message(svc, status));
2872264Sjacobs 		return (-1);
2882264Sjacobs 	}
2892264Sjacobs 
2902264Sjacobs 	status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
2918966SSonam.Gupta@Sun.COM 	    0, &jobs);
2922264Sjacobs 	if (status != PAPI_OK) {
2932264Sjacobs 		fprintf(fp, gettext(
2948966SSonam.Gupta@Sun.COM 		    "Failed to query service for jobs on %s: %s\n"),
2958966SSonam.Gupta@Sun.COM 		    dest, verbose_papi_message(svc, status));
2962264Sjacobs 		return (-1);
2972264Sjacobs 	}
2982264Sjacobs 	if (jobs != NULL) {
2992264Sjacobs 		while (jobs[num_jobs] != NULL)
3002264Sjacobs 			num_jobs++;
3012264Sjacobs 	}
3022264Sjacobs 
3032264Sjacobs 	printer_state_line(fp, p, num_jobs, dest);
3042264Sjacobs 	if (num_jobs > 0) {
3052264Sjacobs 		int i;
3062264Sjacobs 
3072264Sjacobs 		if (fmt == 3)
3082264Sjacobs 			print_header(fp);
3092264Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
3102264Sjacobs 			print_job_line(fp, i, jobs[i], fmt, ac, av);
3112264Sjacobs 	}
3122264Sjacobs 
3132264Sjacobs 	papiPrinterFree(p);
3142264Sjacobs 	papiJobListFree(jobs);
3152264Sjacobs 
3162264Sjacobs 	return (num_jobs);
3172264Sjacobs }
3182264Sjacobs 
3192264Sjacobs int
3202264Sjacobs berkeley_cancel_request(papi_service_t svc, FILE *fp, char *dest,
3212264Sjacobs 		int ac, char *av[])
3222264Sjacobs {
3232264Sjacobs 	papi_status_t status;
3242264Sjacobs 	papi_job_t *jobs = NULL;
3256817Sjacobs 	char *jattrs[] = { "job-originating-user-name", "job-id",
3266817Sjacobs 			"job-id-requested", NULL };
3272264Sjacobs 
3282264Sjacobs 	status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
3298966SSonam.Gupta@Sun.COM 	    0, &jobs);
3302264Sjacobs 
3312264Sjacobs 	if (status != PAPI_OK) {
3322264Sjacobs 		fprintf(fp, gettext("Failed to query service for %s: %s\n"),
3338966SSonam.Gupta@Sun.COM 		    dest, verbose_papi_message(svc, status));
3342264Sjacobs 		return (-1);
3352264Sjacobs 	}
3362264Sjacobs 
3372264Sjacobs 	/* cancel the job(s) */
3382264Sjacobs 	if (jobs != NULL) {
3392264Sjacobs 		int i;
3402264Sjacobs 
3412264Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
3422264Sjacobs 			cancel_job(svc, fp, dest, jobs[i], ac, av);
3432264Sjacobs 	}
3442264Sjacobs 
3452264Sjacobs 	papiJobListFree(jobs);
3462264Sjacobs 
3472264Sjacobs 	return (0);
3482264Sjacobs }
3492264Sjacobs 
3502264Sjacobs int
3512264Sjacobs get_printer_id(char *name, char **printer, int *id)
3522264Sjacobs {
3532264Sjacobs 	int result = -1;
3542264Sjacobs 
3552264Sjacobs 	if (name != NULL) {
3562264Sjacobs 		char *p = strrchr(name, '-');
3572264Sjacobs 
3582264Sjacobs 		*printer = name;
3592264Sjacobs 		if (p != NULL) {
3602264Sjacobs 			char *s = NULL;
3612264Sjacobs 
3622264Sjacobs 			*id = strtol(p + 1, &s, 10);
3632264Sjacobs 			if (s[0] != '\0')
3642264Sjacobs 				*id = -1;
3652264Sjacobs 			else
3662264Sjacobs 				*p = '\0';
3672264Sjacobs 			result = 0;
3682264Sjacobs 		} else
3692264Sjacobs 			*id = -1;
3702264Sjacobs 	}
3712264Sjacobs 
3722264Sjacobs 	return (result);
3732264Sjacobs }
3742264Sjacobs 
3752264Sjacobs /*
3762264Sjacobs  * strsplit() splits a string into a NULL terminated array of substrings
3772264Sjacobs  * determined by a seperator.  The original string is modified, and newly
3782264Sjacobs  * allocated space is only returned for the array itself.  If more than
3792264Sjacobs  * 1024 substrings exist, they will be ignored.
3802264Sjacobs  */
3812264Sjacobs char **
3822264Sjacobs strsplit(char *string, const char *seperators)
3832264Sjacobs {
3842264Sjacobs 	char	*list[BUFSIZ],
3858966SSonam.Gupta@Sun.COM 	    **result;
3862264Sjacobs 	int	length = 0;
3872264Sjacobs 
3882264Sjacobs 	if ((string == NULL) || (seperators == NULL))
3892264Sjacobs 		return (NULL);
3902264Sjacobs 
3912264Sjacobs 	(void) memset(list, 0, sizeof (list));
3922264Sjacobs 	for (list[length] = strtok(string, seperators);
3938966SSonam.Gupta@Sun.COM 	    (list[length] != NULL) && (length < (BUFSIZ - 2));
3948966SSonam.Gupta@Sun.COM 	    list[length] = strtok(NULL, seperators))
3952264Sjacobs 			length++;
3962264Sjacobs 
3972264Sjacobs 	if ((result = (char **)calloc(length+1, sizeof (char *))) != NULL)
3982264Sjacobs 		(void) memcpy(result, list, length * sizeof (char *));
3992264Sjacobs 
4002264Sjacobs 	return (result);
4012264Sjacobs }
4022264Sjacobs 
4032264Sjacobs papi_status_t
4047253Sjacobs jobSubmitSTDIN(papi_service_t svc, char *printer, char *prefetch, int len,
4057253Sjacobs 		papi_attribute_t **list, papi_job_t *job)
4062264Sjacobs {
4072264Sjacobs 	papi_status_t status;
4082264Sjacobs 	papi_stream_t stream = NULL;
4092264Sjacobs 	int rc;
4102264Sjacobs 	char buf[BUFSIZ];
4112264Sjacobs 
4122264Sjacobs 	status = papiJobStreamOpen(svc, printer, list, NULL, &stream);
4137253Sjacobs 
4147253Sjacobs 	if (len > 0)
4157253Sjacobs 		status = papiJobStreamWrite(svc, stream, prefetch, len);
4167253Sjacobs 
4172264Sjacobs 	while ((status == PAPI_OK) && ((rc = read(0, buf, sizeof (buf))) > 0))
4182264Sjacobs 		status = papiJobStreamWrite(svc, stream, buf, rc);
4192264Sjacobs 
4202264Sjacobs 	if (status == PAPI_OK)
4212264Sjacobs 		status = papiJobStreamClose(svc, stream, job);
4222264Sjacobs 
4232264Sjacobs 	return (status);
4242264Sjacobs }
4252264Sjacobs 
4263125Sjacobs /*
4273125Sjacobs  * is_postscript() will detect if the file passed in contains postscript
4283125Sjacobs  * data.  A one is returned if the file contains postscript, zero is returned
4293125Sjacobs  * if the file is not postscript, and -1 is returned if an error occurs
4303125Sjacobs  */
4313125Sjacobs #define	PS_MAGIC	"%!"
4323125Sjacobs #define	PC_PS_MAGIC	"^D%!"
4333125Sjacobs int
4347253Sjacobs is_postscript_stream(int fd, char *buf, int *len)
4353125Sjacobs {
4367253Sjacobs 	if ((*len = read(fd, buf, *len)) < 0) {
4373125Sjacobs 		close(fd);
4383125Sjacobs 		return (-1);
4393125Sjacobs 	}
4403125Sjacobs 
4413125Sjacobs 	if ((strncmp(buf, PS_MAGIC, sizeof (PS_MAGIC) - 1) == 0) ||
4423125Sjacobs 	    (strncmp(buf, PC_PS_MAGIC, sizeof (PC_PS_MAGIC) - 1) == 0))
4433125Sjacobs 		return (1);
4443125Sjacobs 	else
4453125Sjacobs 		return (0);
4463125Sjacobs }
4473125Sjacobs 
4487253Sjacobs int
4497253Sjacobs is_postscript(const char *file)
4507253Sjacobs {
4517253Sjacobs 	int rc = -1;
4527253Sjacobs 	int fd;
4537253Sjacobs 
4547253Sjacobs 	if ((fd = open(file, O_RDONLY)) >= 0) {
4557253Sjacobs 		char buf[3];
4567253Sjacobs 		int len = sizeof (buf);
4577253Sjacobs 
4587253Sjacobs 		rc = is_postscript_stream(fd, buf, &len);
4597253Sjacobs 		close(fd);
4607253Sjacobs 	}
4617253Sjacobs 
4627253Sjacobs 	return (rc);
4637253Sjacobs }
4647253Sjacobs 
4652264Sjacobs static char **
4662264Sjacobs all_list(papi_service_t svc)
4672264Sjacobs {
4682264Sjacobs 	papi_status_t status;
4692264Sjacobs 	papi_printer_t printer = NULL;
4702264Sjacobs 	char *list[] = { "member-names", NULL };
4712264Sjacobs 	char **result = NULL;
4722264Sjacobs 
4732264Sjacobs 	status = papiPrinterQuery(svc, "_all", list, NULL, &printer);
4742264Sjacobs 	if ((status == PAPI_OK) && (printer != NULL)) {
4752264Sjacobs 		papi_attribute_t **attributes =
4768966SSonam.Gupta@Sun.COM 		    papiPrinterGetAttributeList(printer);
4772264Sjacobs 		if (attributes != NULL) {
4782264Sjacobs 			void *iter = NULL;
4792264Sjacobs 			char *value = NULL;
4802264Sjacobs 
4812264Sjacobs 			for (status = papiAttributeListGetString(attributes,
4828966SSonam.Gupta@Sun.COM 			    &iter, "member-names", &value);
4838966SSonam.Gupta@Sun.COM 			    status == PAPI_OK;
4848966SSonam.Gupta@Sun.COM 			    status = papiAttributeListGetString(attributes,
4858966SSonam.Gupta@Sun.COM 			    &iter, NULL, &value))
4862264Sjacobs 					list_append(&result, strdup(value));
4872264Sjacobs 		}
4882264Sjacobs 		papiPrinterFree(printer);
4892264Sjacobs 	}
4902264Sjacobs 
4912264Sjacobs 	return (result);
4922264Sjacobs }
4932264Sjacobs 
4942264Sjacobs static char **
4952264Sjacobs printers_list(papi_service_t svc)
4962264Sjacobs {
4972264Sjacobs 	papi_status_t status;
4982264Sjacobs 	papi_printer_t *printers = NULL;
4992264Sjacobs 	char *keys[] = { "printer-name", NULL };
5002264Sjacobs 	char **result = NULL;
5012264Sjacobs 
5022264Sjacobs 	status = papiPrintersList(svc, keys, NULL, &printers);
5032264Sjacobs 	if ((status == PAPI_OK) && (printers != NULL)) {
5042264Sjacobs 		int i;
5052264Sjacobs 
5062264Sjacobs 		for (i = 0; printers[i] != NULL; i++) {
5072264Sjacobs 			papi_attribute_t **attributes =
5088966SSonam.Gupta@Sun.COM 			    papiPrinterGetAttributeList(printers[i]);
5092264Sjacobs 			char *name = NULL;
5102264Sjacobs 
5112264Sjacobs 			(void) papiAttributeListGetString(attributes, NULL,
5128966SSonam.Gupta@Sun.COM 			    "printer-name", &name);
5132264Sjacobs 			if ((name != NULL) && (strcmp(name, "_default") != 0))
5142264Sjacobs 				list_append(&result, strdup(name));
5152264Sjacobs 		}
5162264Sjacobs 		papiPrinterListFree(printers);
5172264Sjacobs 	}
5182264Sjacobs 
5192264Sjacobs 	return (result);
5202264Sjacobs }
5212264Sjacobs 
5222264Sjacobs char **
5232264Sjacobs interest_list(papi_service_t svc)
5242264Sjacobs {
5252264Sjacobs 	static char been_here;
5262264Sjacobs 	static char **result;
5272264Sjacobs 
5282264Sjacobs 	if (been_here == 0) {	/* only do this once */
5292264Sjacobs 		been_here = 1;
5302264Sjacobs 
5312264Sjacobs 		if ((result = all_list(svc)) == NULL)
5322264Sjacobs 			result = printers_list(svc);
5332264Sjacobs 	}
5342264Sjacobs 
5352264Sjacobs 	return (result);
5362264Sjacobs }
5372264Sjacobs 
5382264Sjacobs char *
5392264Sjacobs localhostname()
5402264Sjacobs {
5412264Sjacobs 	static char *result;
5422264Sjacobs 
5432264Sjacobs 	if (result == NULL) {
5442264Sjacobs 		static char buf[256];
5452264Sjacobs 
5462264Sjacobs 		if (gethostname(buf, sizeof (buf)) == 0)
5472264Sjacobs 			result = buf;
5482264Sjacobs 	}
5492264Sjacobs 
5502264Sjacobs 	return (result);
5512264Sjacobs }
5522264Sjacobs 
5532264Sjacobs int
5542264Sjacobs cli_auth_callback(papi_service_t svc, void *app_data)
5552264Sjacobs {
5562264Sjacobs 	char prompt[BUFSIZ];
5572264Sjacobs 	char *user, *svc_name, *passphrase;
5582264Sjacobs 
5592264Sjacobs 	/* get the name of the service we are contacting */
5602264Sjacobs 	if ((svc_name = papiServiceGetServiceName(svc)) == NULL)
5612264Sjacobs 		return (-1);
5622264Sjacobs 
5632264Sjacobs 	/* find our who we are supposed to be */
5642264Sjacobs 	if ((user = papiServiceGetUserName(svc)) == NULL) {
5652264Sjacobs 		struct passwd *pw;
5662264Sjacobs 
5672264Sjacobs 		if ((pw = getpwuid(getuid())) != NULL)
5682264Sjacobs 			user = pw->pw_name;
5692264Sjacobs 		else
5702264Sjacobs 			user = "nobody";
5712264Sjacobs 	}
5722264Sjacobs 
5732264Sjacobs 	/* build the prompt string */
5742264Sjacobs 	snprintf(prompt, sizeof (prompt),
5758966SSonam.Gupta@Sun.COM 	    gettext("passphrase for %s to access %s: "), user, svc_name);
5762264Sjacobs 
5772264Sjacobs 	/* ask for the passphrase */
5782264Sjacobs 	if ((passphrase = getpassphrase(prompt)) != NULL)
5792264Sjacobs 		papiServiceSetPassword(svc, passphrase);
5802264Sjacobs 
5812264Sjacobs 	return (0);
5822264Sjacobs }
583