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 
78*11397SSonam.Gupta@Sun.COM /*
79*11397SSonam.Gupta@Sun.COM  * return 0 : argument passed is job-id && job-id matches
80*11397SSonam.Gupta@Sun.COM  *		or argument passed is user
81*11397SSonam.Gupta@Sun.COM  */
82*11397SSonam.Gupta@Sun.COM static int
83*11397SSonam.Gupta@Sun.COM match_job_rid(int id, int ac, char *av[])
84*11397SSonam.Gupta@Sun.COM {
85*11397SSonam.Gupta@Sun.COM 	int i;
86*11397SSonam.Gupta@Sun.COM 
87*11397SSonam.Gupta@Sun.COM 	for (i = 0; i < ac; i++)
88*11397SSonam.Gupta@Sun.COM 		if (isdigit(av[i][0]) != 0) {
89*11397SSonam.Gupta@Sun.COM 			if (id == atoi(av[i]))
90*11397SSonam.Gupta@Sun.COM 				/* job-id match */
91*11397SSonam.Gupta@Sun.COM 				return (0);
92*11397SSonam.Gupta@Sun.COM 		} else
93*11397SSonam.Gupta@Sun.COM 			/* argument passed is user */
94*11397SSonam.Gupta@Sun.COM 			return (0);
95*11397SSonam.Gupta@Sun.COM 	return (-1);
96*11397SSonam.Gupta@Sun.COM }
97*11397SSonam.Gupta@Sun.COM 
983125Sjacobs static struct {
993125Sjacobs 	char *mime_type;
1003125Sjacobs 	char *lp_type;
1013125Sjacobs } type_map[] = {
1023125Sjacobs 	{ "text/plain", "simple" },
1033125Sjacobs 	{ "application/octet-stream", "raw" },
1043125Sjacobs 	{ "application/octet-stream", "any" },
1053125Sjacobs 	{ "application/postscript", "postscript" },
1063125Sjacobs 	{ "application/postscript", "ps" },
1073125Sjacobs 	{ "application/x-cif", "cif" },
1083125Sjacobs 	{ "application/x-dvi", "dvi" },
1093125Sjacobs 	{ "application/x-plot", "plot" },
1103125Sjacobs 	{ "application/x-ditroff", "troff" },
1113125Sjacobs 	{ "application/x-troff", "otroff" },
1123125Sjacobs 	{ "application/x-pr", "pr" },
1133125Sjacobs 	{ "application/x-fortran", "fortran" },
1143125Sjacobs 	{ "application/x-raster", "raster" },
1153125Sjacobs 	{ NULL, NULL}
1163125Sjacobs };
1173125Sjacobs 
1183125Sjacobs char *
1193125Sjacobs lp_type_to_mime_type(char *lp_type)
1203125Sjacobs {
1213125Sjacobs 	int i;
1223125Sjacobs 
1233125Sjacobs 	if (lp_type == NULL)
1243125Sjacobs 		return ("application/octet-stream");
1253125Sjacobs 
1263125Sjacobs 	for (i = 0; type_map[i].lp_type != NULL; i++)
1273125Sjacobs 		if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
1283125Sjacobs 			return (type_map[i].mime_type);
1293125Sjacobs 
1303125Sjacobs 	return (lp_type);
1313125Sjacobs }
1323125Sjacobs 
1332264Sjacobs /*
1342264Sjacobs  * to support job/printer status
1352264Sjacobs  */
1362264Sjacobs static char *
1372264Sjacobs state_string(int state)
1382264Sjacobs {
1392264Sjacobs 	switch (state) {
1402264Sjacobs 	case 3:
1412264Sjacobs 		return (gettext("idle"));
1422264Sjacobs 	case 4:
1432264Sjacobs 		return (gettext("processing"));
1442264Sjacobs 	case 5:
1452264Sjacobs 		return (gettext("stopped"));
1462264Sjacobs 	default:
1472264Sjacobs 		return (gettext("unknown"));
1482264Sjacobs 	}
1492264Sjacobs }
1502264Sjacobs 
1512264Sjacobs static char *_rank_suffixes[] = {
1522264Sjacobs 	"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
1532264Sjacobs };
1542264Sjacobs 
1552264Sjacobs static char *
1562264Sjacobs rank_string(const int rank)
1572264Sjacobs {
1582264Sjacobs 	static char buf[12];
1592264Sjacobs 
1602264Sjacobs 	if (rank < 0)
1612264Sjacobs 		snprintf(buf, sizeof (buf), gettext("invalid"));
1622264Sjacobs 	else if (rank == 0)
1632264Sjacobs 		snprintf(buf, sizeof (buf), gettext("active"));
1642264Sjacobs 	else if ((rank > 10) && (rank < 14))
1652264Sjacobs 		sprintf(buf, "%dth", rank);
1662264Sjacobs 	else
1672264Sjacobs 		sprintf(buf, "%d%s", rank, _rank_suffixes[rank % 10]);
1682264Sjacobs 
1692264Sjacobs 	return (buf);
1702264Sjacobs }
1712264Sjacobs 
1722264Sjacobs static void
1732264Sjacobs printer_state_line(FILE *fp, papi_printer_t p, int num_jobs, char *name)
1742264Sjacobs {
1752264Sjacobs 	papi_attribute_t **list = papiPrinterGetAttributeList(p);
1762264Sjacobs 	int state = 0;
1772264Sjacobs 	char *reason = "";
1782264Sjacobs 
1792264Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
1808966SSonam.Gupta@Sun.COM 	    "printer-state", &state);
1812264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
1828966SSonam.Gupta@Sun.COM 	    "printer-state-reasons", &reason);
1832264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
1848966SSonam.Gupta@Sun.COM 	    "printer-name", &name);
1852264Sjacobs 
1862264Sjacobs 	if ((state != 0x03) || (num_jobs != 0)) {
1872264Sjacobs 		fprintf(fp, "%s: %s", name, state_string(state));
1882264Sjacobs 		if (state == 0x05) /* stopped */
1892264Sjacobs 			fprintf(fp, ": %s\n", reason);
1902264Sjacobs 		else
1912264Sjacobs 			fprintf(fp, "\n");
1922264Sjacobs 	} else
1932264Sjacobs 		fprintf(fp, "no entries\n");
1942264Sjacobs }
1952264Sjacobs 
1962264Sjacobs static void
1972264Sjacobs print_header(FILE *fp)
1982264Sjacobs {
1992264Sjacobs 	fprintf(fp, gettext("Rank\tOwner\t Job\tFile(s)\t\t\t\tTotal Size\n"));
2002264Sjacobs }
2012264Sjacobs 
2022264Sjacobs static void
2032264Sjacobs print_job_line(FILE *fp, int count, papi_job_t job, int fmt, int ac, char *av[])
2042264Sjacobs {
2052264Sjacobs 	papi_attribute_t **list = papiJobGetAttributeList(job);
2062264Sjacobs 	int copies = 1, id = 0, rank = count, size = 0;
2072264Sjacobs 	char *name = "print job";
2082264Sjacobs 	char *user = "nobody";
2092264Sjacobs 	char *host = "localhost";
2102264Sjacobs 	char *suffix = "k";
2112264Sjacobs 
2122264Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
2138966SSonam.Gupta@Sun.COM 	    "job-id", &id);
2146817Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
2158966SSonam.Gupta@Sun.COM 	    "job-id-requested", &id);
2162264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
2178966SSonam.Gupta@Sun.COM 	    "job-originating-user-name", &user);
2183125Sjacobs 	(void) papiAttributeListGetString(list, NULL,
2198966SSonam.Gupta@Sun.COM 	    "job-originating-host-name", &host);
2202264Sjacobs 
2212264Sjacobs 	/* if we are looking and it doesn't match, return early */
2222264Sjacobs 	if ((ac > 0) && (match_job(id, user, ac, av) < 0))
2232264Sjacobs 		return;
2242264Sjacobs 
2252264Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
2268966SSonam.Gupta@Sun.COM 	    "copies", &copies);
2272264Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
2288966SSonam.Gupta@Sun.COM 	    "number-of-intervening-jobs", &rank);
2292264Sjacobs 
2302264Sjacobs 	if (papiAttributeListGetInteger(list, NULL, "job-octets", &size)
2318966SSonam.Gupta@Sun.COM 	    == PAPI_OK)
2322264Sjacobs 		suffix = "bytes";
2332264Sjacobs 	else
2342264Sjacobs 		(void) papiAttributeListGetInteger(list, NULL,
2358966SSonam.Gupta@Sun.COM 		    "job-k-octets", &size);
2362264Sjacobs 	(void) papiAttributeListGetString(list, NULL,
2378966SSonam.Gupta@Sun.COM 	    "job-name", &name);
2382264Sjacobs 
2392264Sjacobs 	size *= copies;
2402264Sjacobs 
2412264Sjacobs 	if (fmt == 3) {
2422264Sjacobs 		fprintf(fp, gettext("%s\t%-8.8s %d\t%-32.32s%d %s\n"),
2438966SSonam.Gupta@Sun.COM 		    rank_string(++rank), user, id, name, size, suffix);
2442264Sjacobs 	} else
2452264Sjacobs 		fprintf(fp, gettext(
2468966SSonam.Gupta@Sun.COM 		    "\n%s: %s\t\t\t\t[job %d %s]\n\t%-32.32s\t%d %s\n"),
2478966SSonam.Gupta@Sun.COM 		    user, rank_string(++rank), id, host, name, size,
2488966SSonam.Gupta@Sun.COM 		    suffix);
2492264Sjacobs }
2502264Sjacobs 
2512264Sjacobs /*
2522264Sjacobs  * to support job cancelation
2532264Sjacobs  */
2542264Sjacobs static void
2552264Sjacobs cancel_job(papi_service_t svc, FILE *fp, char *printer, papi_job_t job,
2562264Sjacobs 		int ac, char *av[])
2572264Sjacobs {
2582264Sjacobs 	papi_status_t status;
2592264Sjacobs 	papi_attribute_t **list = papiJobGetAttributeList(job);
260*11397SSonam.Gupta@Sun.COM 	int id = -1;
26110472SSonam.Gupta@Sun.COM 	int rid = -1;
2622264Sjacobs 	char *user = "";
2632264Sjacobs 	char *mesg = gettext("cancelled");
264*11397SSonam.Gupta@Sun.COM 	int i = 0;
2652264Sjacobs 
2662264Sjacobs 	papiAttributeListGetInteger(list, NULL,
2678966SSonam.Gupta@Sun.COM 	    "job-id", &id);
2686817Sjacobs 	papiAttributeListGetInteger(list, NULL,
2698966SSonam.Gupta@Sun.COM 	    "job-id-requested", &rid);
2702264Sjacobs 	papiAttributeListGetString(list, NULL,
2718966SSonam.Gupta@Sun.COM 	    "job-originating-user-name", &user);
2722264Sjacobs 
2732264Sjacobs 	/* if we are looking and it doesn't match, return early */
2746817Sjacobs 	if ((ac > 0) && (match_job(id, user, ac, av) < 0) &&
2756817Sjacobs 	    (match_job(rid, user, ac, av) < 0))
2762264Sjacobs 		return;
2772264Sjacobs 
278*11397SSonam.Gupta@Sun.COM 	/*
279*11397SSonam.Gupta@Sun.COM 	 * A remote lpd job should be cancelled only based on
280*11397SSonam.Gupta@Sun.COM 	 * job-id-requested
281*11397SSonam.Gupta@Sun.COM 	 */
282*11397SSonam.Gupta@Sun.COM 	if (rid != -1) {
283*11397SSonam.Gupta@Sun.COM 		if (match_job_rid(rid, ac, av) == -1)
284*11397SSonam.Gupta@Sun.COM 			/* job-id mismatch */
285*11397SSonam.Gupta@Sun.COM 			return;
286*11397SSonam.Gupta@Sun.COM 	}
287*11397SSonam.Gupta@Sun.COM 
2882264Sjacobs 	status = papiJobCancel(svc, printer, id);
2892264Sjacobs 	if (status != PAPI_OK)
2902264Sjacobs 		mesg = papiStatusString(status);
2912264Sjacobs 
29210472SSonam.Gupta@Sun.COM 	if (rid != -1)
2939330SSonam.Gupta@Sun.COM 		fprintf(fp, "%s-%d: %s\n", printer, rid, mesg);
2949330SSonam.Gupta@Sun.COM 	else
2958966SSonam.Gupta@Sun.COM 		fprintf(fp, "%s-%d: %s\n", printer, id, mesg);
2962264Sjacobs }
2972264Sjacobs 
2982264Sjacobs int
2992264Sjacobs berkeley_queue_report(papi_service_t svc, FILE *fp, char *dest, int fmt,
3002264Sjacobs 		int ac, char *av[])
3012264Sjacobs {
3022264Sjacobs 	papi_status_t status;
3032264Sjacobs 	papi_printer_t p = NULL;
3042264Sjacobs 	papi_job_t *jobs = NULL;
3052264Sjacobs 	char *pattrs[] = { "printer-name", "printer-state",
3062264Sjacobs 			"printer-state-reasons", NULL };
3073125Sjacobs 	char *jattrs[] = { "job-name", "job-octets", "job-k-octets", "job-id",
3086817Sjacobs 			"job-originating-user-name", "job-id-requested",
3093125Sjacobs 			"job-originating-host-name",
3102264Sjacobs 			"number-of-intervening-jobs", NULL };
3112264Sjacobs 	int num_jobs = 0;
3122264Sjacobs 
3132264Sjacobs 	status = papiPrinterQuery(svc, dest, pattrs, NULL, &p);
3142264Sjacobs 	if (status != PAPI_OK) {
3152264Sjacobs 		fprintf(fp, gettext(
3168966SSonam.Gupta@Sun.COM 		    "Failed to query service for state of %s: %s\n"),
3178966SSonam.Gupta@Sun.COM 		    dest, verbose_papi_message(svc, status));
3182264Sjacobs 		return (-1);
3192264Sjacobs 	}
3202264Sjacobs 
3212264Sjacobs 	status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
3228966SSonam.Gupta@Sun.COM 	    0, &jobs);
3232264Sjacobs 	if (status != PAPI_OK) {
3242264Sjacobs 		fprintf(fp, gettext(
3258966SSonam.Gupta@Sun.COM 		    "Failed to query service for jobs on %s: %s\n"),
3268966SSonam.Gupta@Sun.COM 		    dest, verbose_papi_message(svc, status));
3272264Sjacobs 		return (-1);
3282264Sjacobs 	}
3292264Sjacobs 	if (jobs != NULL) {
3302264Sjacobs 		while (jobs[num_jobs] != NULL)
3312264Sjacobs 			num_jobs++;
3322264Sjacobs 	}
3332264Sjacobs 
3342264Sjacobs 	printer_state_line(fp, p, num_jobs, dest);
3352264Sjacobs 	if (num_jobs > 0) {
3362264Sjacobs 		int i;
3372264Sjacobs 
3382264Sjacobs 		if (fmt == 3)
3392264Sjacobs 			print_header(fp);
3402264Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
3412264Sjacobs 			print_job_line(fp, i, jobs[i], fmt, ac, av);
3422264Sjacobs 	}
3432264Sjacobs 
3442264Sjacobs 	papiPrinterFree(p);
3452264Sjacobs 	papiJobListFree(jobs);
3462264Sjacobs 
3472264Sjacobs 	return (num_jobs);
3482264Sjacobs }
3492264Sjacobs 
3502264Sjacobs int
3512264Sjacobs berkeley_cancel_request(papi_service_t svc, FILE *fp, char *dest,
3522264Sjacobs 		int ac, char *av[])
3532264Sjacobs {
3542264Sjacobs 	papi_status_t status;
3552264Sjacobs 	papi_job_t *jobs = NULL;
3566817Sjacobs 	char *jattrs[] = { "job-originating-user-name", "job-id",
3576817Sjacobs 			"job-id-requested", NULL };
3582264Sjacobs 
3592264Sjacobs 	status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
3608966SSonam.Gupta@Sun.COM 	    0, &jobs);
3612264Sjacobs 
3622264Sjacobs 	if (status != PAPI_OK) {
3632264Sjacobs 		fprintf(fp, gettext("Failed to query service for %s: %s\n"),
3648966SSonam.Gupta@Sun.COM 		    dest, verbose_papi_message(svc, status));
3652264Sjacobs 		return (-1);
3662264Sjacobs 	}
3672264Sjacobs 
3682264Sjacobs 	/* cancel the job(s) */
3692264Sjacobs 	if (jobs != NULL) {
3702264Sjacobs 		int i;
3712264Sjacobs 
3722264Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
3732264Sjacobs 			cancel_job(svc, fp, dest, jobs[i], ac, av);
3742264Sjacobs 	}
3752264Sjacobs 
3762264Sjacobs 	papiJobListFree(jobs);
3772264Sjacobs 
3782264Sjacobs 	return (0);
3792264Sjacobs }
3802264Sjacobs 
3812264Sjacobs int
3822264Sjacobs get_printer_id(char *name, char **printer, int *id)
3832264Sjacobs {
3842264Sjacobs 	int result = -1;
3852264Sjacobs 
3862264Sjacobs 	if (name != NULL) {
3872264Sjacobs 		char *p = strrchr(name, '-');
3882264Sjacobs 
3892264Sjacobs 		*printer = name;
3902264Sjacobs 		if (p != NULL) {
3912264Sjacobs 			char *s = NULL;
3922264Sjacobs 
3932264Sjacobs 			*id = strtol(p + 1, &s, 10);
3942264Sjacobs 			if (s[0] != '\0')
3952264Sjacobs 				*id = -1;
3962264Sjacobs 			else
3972264Sjacobs 				*p = '\0';
3982264Sjacobs 			result = 0;
3992264Sjacobs 		} else
4002264Sjacobs 			*id = -1;
4012264Sjacobs 	}
4022264Sjacobs 
4032264Sjacobs 	return (result);
4042264Sjacobs }
4052264Sjacobs 
4062264Sjacobs /*
4072264Sjacobs  * strsplit() splits a string into a NULL terminated array of substrings
4082264Sjacobs  * determined by a seperator.  The original string is modified, and newly
4092264Sjacobs  * allocated space is only returned for the array itself.  If more than
4102264Sjacobs  * 1024 substrings exist, they will be ignored.
4112264Sjacobs  */
4122264Sjacobs char **
4132264Sjacobs strsplit(char *string, const char *seperators)
4142264Sjacobs {
4152264Sjacobs 	char	*list[BUFSIZ],
4168966SSonam.Gupta@Sun.COM 	    **result;
4172264Sjacobs 	int	length = 0;
4182264Sjacobs 
4192264Sjacobs 	if ((string == NULL) || (seperators == NULL))
4202264Sjacobs 		return (NULL);
4212264Sjacobs 
4222264Sjacobs 	(void) memset(list, 0, sizeof (list));
4232264Sjacobs 	for (list[length] = strtok(string, seperators);
4248966SSonam.Gupta@Sun.COM 	    (list[length] != NULL) && (length < (BUFSIZ - 2));
4258966SSonam.Gupta@Sun.COM 	    list[length] = strtok(NULL, seperators))
4262264Sjacobs 			length++;
4272264Sjacobs 
4282264Sjacobs 	if ((result = (char **)calloc(length+1, sizeof (char *))) != NULL)
4292264Sjacobs 		(void) memcpy(result, list, length * sizeof (char *));
4302264Sjacobs 
4312264Sjacobs 	return (result);
4322264Sjacobs }
4332264Sjacobs 
4342264Sjacobs papi_status_t
4357253Sjacobs jobSubmitSTDIN(papi_service_t svc, char *printer, char *prefetch, int len,
4367253Sjacobs 		papi_attribute_t **list, papi_job_t *job)
4372264Sjacobs {
4382264Sjacobs 	papi_status_t status;
4392264Sjacobs 	papi_stream_t stream = NULL;
4402264Sjacobs 	int rc;
4412264Sjacobs 	char buf[BUFSIZ];
4422264Sjacobs 
4432264Sjacobs 	status = papiJobStreamOpen(svc, printer, list, NULL, &stream);
4447253Sjacobs 
4457253Sjacobs 	if (len > 0)
4467253Sjacobs 		status = papiJobStreamWrite(svc, stream, prefetch, len);
4477253Sjacobs 
4482264Sjacobs 	while ((status == PAPI_OK) && ((rc = read(0, buf, sizeof (buf))) > 0))
4492264Sjacobs 		status = papiJobStreamWrite(svc, stream, buf, rc);
4502264Sjacobs 
4512264Sjacobs 	if (status == PAPI_OK)
4522264Sjacobs 		status = papiJobStreamClose(svc, stream, job);
4532264Sjacobs 
4542264Sjacobs 	return (status);
4552264Sjacobs }
4562264Sjacobs 
4573125Sjacobs /*
4583125Sjacobs  * is_postscript() will detect if the file passed in contains postscript
4593125Sjacobs  * data.  A one is returned if the file contains postscript, zero is returned
4603125Sjacobs  * if the file is not postscript, and -1 is returned if an error occurs
4613125Sjacobs  */
4623125Sjacobs #define	PS_MAGIC	"%!"
4633125Sjacobs #define	PC_PS_MAGIC	"^D%!"
4643125Sjacobs int
4657253Sjacobs is_postscript_stream(int fd, char *buf, int *len)
4663125Sjacobs {
4677253Sjacobs 	if ((*len = read(fd, buf, *len)) < 0) {
4683125Sjacobs 		close(fd);
4693125Sjacobs 		return (-1);
4703125Sjacobs 	}
4713125Sjacobs 
4723125Sjacobs 	if ((strncmp(buf, PS_MAGIC, sizeof (PS_MAGIC) - 1) == 0) ||
4733125Sjacobs 	    (strncmp(buf, PC_PS_MAGIC, sizeof (PC_PS_MAGIC) - 1) == 0))
4743125Sjacobs 		return (1);
4753125Sjacobs 	else
4763125Sjacobs 		return (0);
4773125Sjacobs }
4783125Sjacobs 
4797253Sjacobs int
4807253Sjacobs is_postscript(const char *file)
4817253Sjacobs {
4827253Sjacobs 	int rc = -1;
4837253Sjacobs 	int fd;
4847253Sjacobs 
4857253Sjacobs 	if ((fd = open(file, O_RDONLY)) >= 0) {
4867253Sjacobs 		char buf[3];
4877253Sjacobs 		int len = sizeof (buf);
4887253Sjacobs 
4897253Sjacobs 		rc = is_postscript_stream(fd, buf, &len);
4907253Sjacobs 		close(fd);
4917253Sjacobs 	}
4927253Sjacobs 
4937253Sjacobs 	return (rc);
4947253Sjacobs }
4957253Sjacobs 
4962264Sjacobs static char **
4972264Sjacobs all_list(papi_service_t svc)
4982264Sjacobs {
4992264Sjacobs 	papi_status_t status;
5002264Sjacobs 	papi_printer_t printer = NULL;
5012264Sjacobs 	char *list[] = { "member-names", NULL };
5022264Sjacobs 	char **result = NULL;
5032264Sjacobs 
5042264Sjacobs 	status = papiPrinterQuery(svc, "_all", list, NULL, &printer);
5052264Sjacobs 	if ((status == PAPI_OK) && (printer != NULL)) {
5062264Sjacobs 		papi_attribute_t **attributes =
5078966SSonam.Gupta@Sun.COM 		    papiPrinterGetAttributeList(printer);
5082264Sjacobs 		if (attributes != NULL) {
5092264Sjacobs 			void *iter = NULL;
5102264Sjacobs 			char *value = NULL;
5112264Sjacobs 
5122264Sjacobs 			for (status = papiAttributeListGetString(attributes,
5138966SSonam.Gupta@Sun.COM 			    &iter, "member-names", &value);
5148966SSonam.Gupta@Sun.COM 			    status == PAPI_OK;
5158966SSonam.Gupta@Sun.COM 			    status = papiAttributeListGetString(attributes,
5168966SSonam.Gupta@Sun.COM 			    &iter, NULL, &value))
5172264Sjacobs 					list_append(&result, strdup(value));
5182264Sjacobs 		}
5192264Sjacobs 		papiPrinterFree(printer);
5202264Sjacobs 	}
5212264Sjacobs 
5222264Sjacobs 	return (result);
5232264Sjacobs }
5242264Sjacobs 
5252264Sjacobs static char **
5262264Sjacobs printers_list(papi_service_t svc)
5272264Sjacobs {
5282264Sjacobs 	papi_status_t status;
5292264Sjacobs 	papi_printer_t *printers = NULL;
5302264Sjacobs 	char *keys[] = { "printer-name", NULL };
5312264Sjacobs 	char **result = NULL;
5322264Sjacobs 
5332264Sjacobs 	status = papiPrintersList(svc, keys, NULL, &printers);
5342264Sjacobs 	if ((status == PAPI_OK) && (printers != NULL)) {
5352264Sjacobs 		int i;
5362264Sjacobs 
5372264Sjacobs 		for (i = 0; printers[i] != NULL; i++) {
5382264Sjacobs 			papi_attribute_t **attributes =
5398966SSonam.Gupta@Sun.COM 			    papiPrinterGetAttributeList(printers[i]);
5402264Sjacobs 			char *name = NULL;
5412264Sjacobs 
5422264Sjacobs 			(void) papiAttributeListGetString(attributes, NULL,
5438966SSonam.Gupta@Sun.COM 			    "printer-name", &name);
5442264Sjacobs 			if ((name != NULL) && (strcmp(name, "_default") != 0))
5452264Sjacobs 				list_append(&result, strdup(name));
5462264Sjacobs 		}
5472264Sjacobs 		papiPrinterListFree(printers);
5482264Sjacobs 	}
5492264Sjacobs 
5502264Sjacobs 	return (result);
5512264Sjacobs }
5522264Sjacobs 
5532264Sjacobs char **
5542264Sjacobs interest_list(papi_service_t svc)
5552264Sjacobs {
5562264Sjacobs 	static char been_here;
5572264Sjacobs 	static char **result;
5582264Sjacobs 
5592264Sjacobs 	if (been_here == 0) {	/* only do this once */
5602264Sjacobs 		been_here = 1;
5612264Sjacobs 
5622264Sjacobs 		if ((result = all_list(svc)) == NULL)
5632264Sjacobs 			result = printers_list(svc);
5642264Sjacobs 	}
5652264Sjacobs 
5662264Sjacobs 	return (result);
5672264Sjacobs }
5682264Sjacobs 
5692264Sjacobs char *
5702264Sjacobs localhostname()
5712264Sjacobs {
5722264Sjacobs 	static char *result;
5732264Sjacobs 
5742264Sjacobs 	if (result == NULL) {
5752264Sjacobs 		static char buf[256];
5762264Sjacobs 
5772264Sjacobs 		if (gethostname(buf, sizeof (buf)) == 0)
5782264Sjacobs 			result = buf;
5792264Sjacobs 	}
5802264Sjacobs 
5812264Sjacobs 	return (result);
5822264Sjacobs }
5832264Sjacobs 
5842264Sjacobs int
5852264Sjacobs cli_auth_callback(papi_service_t svc, void *app_data)
5862264Sjacobs {
5872264Sjacobs 	char prompt[BUFSIZ];
5882264Sjacobs 	char *user, *svc_name, *passphrase;
5892264Sjacobs 
5902264Sjacobs 	/* get the name of the service we are contacting */
5912264Sjacobs 	if ((svc_name = papiServiceGetServiceName(svc)) == NULL)
5922264Sjacobs 		return (-1);
5932264Sjacobs 
5942264Sjacobs 	/* find our who we are supposed to be */
5952264Sjacobs 	if ((user = papiServiceGetUserName(svc)) == NULL) {
5962264Sjacobs 		struct passwd *pw;
5972264Sjacobs 
5982264Sjacobs 		if ((pw = getpwuid(getuid())) != NULL)
5992264Sjacobs 			user = pw->pw_name;
6002264Sjacobs 		else
6012264Sjacobs 			user = "nobody";
6022264Sjacobs 	}
6032264Sjacobs 
6042264Sjacobs 	/* build the prompt string */
6052264Sjacobs 	snprintf(prompt, sizeof (prompt),
6068966SSonam.Gupta@Sun.COM 	    gettext("passphrase for %s to access %s: "), user, svc_name);
6072264Sjacobs 
6082264Sjacobs 	/* ask for the passphrase */
6092264Sjacobs 	if ((passphrase = getpassphrase(prompt)) != NULL)
6102264Sjacobs 		papiServiceSetPassword(svc, passphrase);
6112264Sjacobs 
6122264Sjacobs 	return (0);
6132264Sjacobs }
6149331SSonam.Gupta@Sun.COM 
6159331SSonam.Gupta@Sun.COM int32_t
6169331SSonam.Gupta@Sun.COM job_to_be_queried(papi_service_t svc, char *printer, int32_t id)
6179331SSonam.Gupta@Sun.COM {
6189331SSonam.Gupta@Sun.COM 	papi_job_t *jobs = NULL;
6199331SSonam.Gupta@Sun.COM 	papi_status_t status;
620*11397SSonam.Gupta@Sun.COM 	int ret = -1;
6219331SSonam.Gupta@Sun.COM 	char *jattrs[] = { "job-id",
6229331SSonam.Gupta@Sun.COM 			"job-id-requested", NULL };
6239331SSonam.Gupta@Sun.COM 
6249331SSonam.Gupta@Sun.COM 	status = papiPrinterListJobs(svc, printer, jattrs, PAPI_LIST_JOBS_ALL,
6259331SSonam.Gupta@Sun.COM 	    0, &jobs);
6269331SSonam.Gupta@Sun.COM 
6279331SSonam.Gupta@Sun.COM 	if (status != PAPI_OK) {
6289331SSonam.Gupta@Sun.COM 		fprintf(stderr, gettext("Failed to query service for %s: %s\n"),
6299331SSonam.Gupta@Sun.COM 		    printer, verbose_papi_message(svc, status));
6309331SSonam.Gupta@Sun.COM 		return (-1);
6319331SSonam.Gupta@Sun.COM 	}
6329331SSonam.Gupta@Sun.COM 
6339331SSonam.Gupta@Sun.COM 	if (jobs != NULL) {
6349331SSonam.Gupta@Sun.COM 		int i = 0;
6359331SSonam.Gupta@Sun.COM 
6369331SSonam.Gupta@Sun.COM 		for (i = 0; jobs[i] != NULL; i++) {
6379331SSonam.Gupta@Sun.COM 			int32_t rid = -1;
638*11397SSonam.Gupta@Sun.COM 			int32_t jid = -1;
6399331SSonam.Gupta@Sun.COM 			papi_attribute_t **list =
6409331SSonam.Gupta@Sun.COM 			    papiJobGetAttributeList(jobs[i]);
6419331SSonam.Gupta@Sun.COM 
6429331SSonam.Gupta@Sun.COM 			papiAttributeListGetInteger(list, NULL,
6439331SSonam.Gupta@Sun.COM 			    "job-id-requested", &rid);
644*11397SSonam.Gupta@Sun.COM 			papiAttributeListGetInteger(list, NULL,
645*11397SSonam.Gupta@Sun.COM 			    "job-id", &jid);
6469331SSonam.Gupta@Sun.COM 
647*11397SSonam.Gupta@Sun.COM 			/*
648*11397SSonam.Gupta@Sun.COM 			 * check if id matches with either rid or jid
649*11397SSonam.Gupta@Sun.COM 			 */
6509331SSonam.Gupta@Sun.COM 			if (rid == id) {
6519331SSonam.Gupta@Sun.COM 				/* get the actual id and return it */
6529331SSonam.Gupta@Sun.COM 				papiAttributeListGetInteger(list, NULL,
6539331SSonam.Gupta@Sun.COM 				    "job-id", &id);
6549331SSonam.Gupta@Sun.COM 				return (id);
655*11397SSonam.Gupta@Sun.COM 			} else if (id == jid) {
656*11397SSonam.Gupta@Sun.COM 				if (rid != -1) {
657*11397SSonam.Gupta@Sun.COM 					/*
658*11397SSonam.Gupta@Sun.COM 					 * It is a remote lpd job
659*11397SSonam.Gupta@Sun.COM 					 * can be cancelled only
660*11397SSonam.Gupta@Sun.COM 					 * using rid
661*11397SSonam.Gupta@Sun.COM 					 */
662*11397SSonam.Gupta@Sun.COM 					ret = -1;
663*11397SSonam.Gupta@Sun.COM 				} else {
664*11397SSonam.Gupta@Sun.COM 					/*
665*11397SSonam.Gupta@Sun.COM 					 * its local or
666*11397SSonam.Gupta@Sun.COM 					 * remote ipp job
667*11397SSonam.Gupta@Sun.COM 					 */
668*11397SSonam.Gupta@Sun.COM 					return (id);
669*11397SSonam.Gupta@Sun.COM 				}
6709331SSonam.Gupta@Sun.COM 			}
6719331SSonam.Gupta@Sun.COM 		}
672*11397SSonam.Gupta@Sun.COM 		return (ret);
6739331SSonam.Gupta@Sun.COM 	}
6749331SSonam.Gupta@Sun.COM 	return (id);
6759331SSonam.Gupta@Sun.COM }
676