Exemple #1
0
int
main(int ac, char *av[])
{
	papi_status_t status;
	papi_service_t svc = NULL;
	papi_attribute_t **list = NULL;
	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
	papi_job_t job = NULL;
	char prefetch[3];
	int prefetch_len = sizeof (prefetch);
	char *printer = NULL;
	char b = PAPI_TRUE;
	int copy = 0;
	int silent = 0;
	int dump = 0;
	int validate = 0;
	int modify = -1;
	int c;
	uid_t ruid;
	struct passwd *pw;

	(void) setlocale(LC_ALL, "");
	(void) textdomain("SUNW_OST_OSCMD");

	ruid = getuid();
	if ((pw = getpwuid(ruid)) != NULL)
		(void) initgroups(pw->pw_name, pw->pw_gid);
	(void) setuid(ruid);


	while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF)
		switch (c) {
		case 'H':	/* handling */
			if (strcasecmp(optarg, "hold") == 0)
				papiAttributeListAddString(&list,
				    PAPI_ATTR_EXCL,
				    "job-hold-until", "indefinite");
			else if (strcasecmp(optarg, "immediate") == 0)
				papiAttributeListAddString(&list,
				    PAPI_ATTR_EXCL,
				    "job-hold-until", "no-hold");
			else
				papiAttributeListAddString(&list,
				    PAPI_ATTR_EXCL,
				    "job-hold-until", optarg);
			break;
		case 'P': {	/* page list */
			char buf[BUFSIZ];

			snprintf(buf, sizeof (buf), "page-ranges=%s", optarg);
			papiAttributeListFromString(&list,
			    PAPI_ATTR_EXCL, buf);
			}
			break;
		case 'S':	/* charset */
			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
			    "lp-charset", optarg);
			break;
		case 'T':	/* type */
			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
			    "document-format",
			    lp_type_to_mime_type(optarg));
			break;
		case 'D':	/* dump */
			dump = 1;
			break;
		case 'c':	/* copy */
			copy = 1;
			break;
		case 'd':	/* destination */
			printer = optarg;
			break;
		case 'f':	/* form */
			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
			    "form", optarg);
			break;
		case 'i':	/* modify job */
			if ((get_printer_id(optarg, &printer, &modify) < 0) ||
			    (modify < 0)) {
				fprintf(stderr,
				    gettext("invalid request id: %s\n"),
				    optarg);
				exit(1);
			}
			break;
		case 'm':	/* mail when complete */
			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
			    "rfc-1179-mail", 1);
			break;
		case 'n':	/* copies */
			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
			    "copies", atoi(optarg));
			break;
		case 'o':	/* lp "options" */
			papiAttributeListFromString(&list,
			    PAPI_ATTR_REPLACE, optarg);
			break;
		case 'p':	/* Solaris - notification */
			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
			    "rfc-1179-mail", 1);
			break;
		case 'q': {	/* priority */
			int i = atoi(optarg);

			i = 100 - (i * 2.5);
			if ((i < 1) || (i > 100)) {
				fprintf(stderr, gettext("UX:lp: "));
				fprintf(stderr, gettext("ERROR: "));
				fprintf(stderr, gettext("Bad priority"
				    " value \"%s\"."), optarg);
				fprintf(stderr, gettext("\n      "));
				fprintf(stderr, gettext("TO FIX"));
				fprintf(stderr, gettext(": "));
				fprintf(stderr, gettext("Use an integer value"
				    " from 0 to 39."));
				fprintf(stderr, gettext("\n"));
				exit(1);
			}
			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
			    "job-priority", i);
			}
			break;
		case 'r':	/* "raw" mode */
			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
			    "document-format",
			    "application/octet-stream");
			papiAttributeListAddString(&list, PAPI_ATTR_APPEND,
			    "stty", "raw");
			break;
		case 's':	/* suppress message */
			silent = 1;
			break;
		case 't':	/* title */
			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
			    "job-name", optarg);
			break;
		case 'V':	/* validate */
			validate = 1;
			break;
		case 'w':
			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
			    "rfc-1179-mail", 1);
			break;
		case 'y':	/* lp "modes" */
			papiAttributeListAddString(&list, PAPI_ATTR_APPEND,
			    "lp-modes", optarg);
			break;
		case 'E':
			encryption = PAPI_ENCRYPT_REQUIRED;
			break;
		default:
			usage(av[0]);
		}

	/* convert "banner", "nobanner" to "job-sheet" */
	if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) {
		(void) papiAttributeListDelete(&list, "banner");
		if (b == PAPI_FALSE)
			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
			    "job-sheets", "none");
	}

	if ((printer == NULL) &&
	    ((printer = getenv("PRINTER")) == NULL) &&
	    ((printer = getenv("LPDEST")) == NULL))
		printer = DEFAULT_DEST;

	if (((optind + 1) == ac) && (strcmp(av[optind], "-") == 0))
		optind = ac;

	if (modify == -1) {
		char *document_format = "text/plain";

		if (optind != ac) {
			/* get the mime type of the file data */
#ifdef MAGIC_MIME
			magic_t ms = NULL;

			if ((ms = magic_open(MAGIC_MIME)) != NULL) {
				document_format = magic_file(ms, av[optind]);
				magic_close(ms);
			}
#else
			if (is_postscript(av[optind]) == 1)
				document_format = "application/postscript";
#endif
		} else {
			if (is_postscript_stream(0, prefetch, &prefetch_len)
			    == 1)
				document_format = "application/postscript";
		}

		papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1);
		papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
		    "document-format", document_format);
		papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
		    "job-sheets", "standard");
	}

	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
	    encryption, NULL);
	if (status != PAPI_OK) {
		fprintf(stderr, gettext(
		    "Failed to contact service for %s: %s\n"), printer,
		    verbose_papi_message(svc, status));
		exit(1);
	}

	if (dump != 0) {
		printf("requesting attributes:\n");
		papiAttributeListPrint(stdout, list, "\t");
		printf("\n");
	}

	if (modify != -1)
		status = papiJobModify(svc, printer, modify, list, &job);
	else if (optind == ac)	/* no file list, use stdin */
		status = jobSubmitSTDIN(svc, printer, prefetch, prefetch_len,
		    list, &job);
	else if (validate == 1)	/* validate the request can be processed */
		status = papiJobValidate(svc, printer, list,
		    NULL, &av[optind], &job);
	else if (copy == 0)	/* reference the files in the job, default */
		status = papiJobSubmitByReference(svc, printer, list,
		    NULL, &av[optind], &job);
	else			/* copy the files before return, -c */
		status = papiJobSubmit(svc, printer, list,
		    NULL, &av[optind], &job);

	papiAttributeListFree(list);

	if (status != PAPI_OK) {
		fprintf(stderr, gettext("%s: %s\n"), printer,
		    verbose_papi_message(svc, status));
		papiJobFree(job);
		papiServiceDestroy(svc);
		exit(1);
	}

	if (((silent == 0) || (dump != 0)) &&
	    ((list = papiJobGetAttributeList(job)) != NULL)) {
		int32_t id = -1;

		if (printer == NULL)
			papiAttributeListGetString(list, NULL,
			    "printer-name", &printer);

		papiAttributeListGetInteger(list, NULL,
		    "job-id-requested", &id);
		if (id == -1) {
			papiAttributeListGetInteger(list, NULL, "job-id", &id);
		}

		printf(gettext("request id is %s-%d "), printer, id);
		if (ac != optind)
			printf("(%d file(s))\n", ac - optind);
		else
			printf("(standard input)\n");

		if (dump != 0) {
			printf("job attributes:\n");
			papiAttributeListPrint(stdout, list, "\t");
			printf("\n");
		}
	}

	papiJobFree(job);
	papiServiceDestroy(svc);

	return (0);
}
Exemple #2
0
papi_status_t
lpsched_printer_configuration_to_attributes(service_t *svc, printer_t *p,
	char *dest)
{
	PRINTER *tmp;
	char buf[BUFSIZ+1];
	struct utsname sysname;
	char **allowed = NULL, **denied = NULL;

	if ((svc == NULL) || (p == NULL) || (dest == NULL))
		return (PAPI_BAD_ARGUMENT);

	/* get the configuration DB data */
	if ((tmp = getprinter(dest)) == NULL) {
		detailed_error(svc,
			gettext("unable to read configuration data"));
		return (PAPI_DEVICE_ERROR);
	}

	/* name */
	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
				"printer-name", tmp->name);
	if (tmp->name != NULL) {
		char uri[BUFSIZ];

		snprintf(uri, sizeof (uri), "lpsched://localhost/printers/%s",
				tmp->name);
		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
				"printer-uri-supported", uri);
	}

	/* banner */
	if ((tmp->banner & BAN_OPTIONAL) == BAN_OPTIONAL)
		papiAttributeListAddString(&p->attributes, PAPI_ATTR_APPEND,
				"job-sheets-supported", "optional");
	else if (tmp->banner & BAN_NEVER)
		papiAttributeListAddString(&p->attributes, PAPI_ATTR_APPEND,
				"job-sheets-supported", "none");
	else if (tmp->banner & BAN_ALWAYS)
		papiAttributeListAddString(&p->attributes, PAPI_ATTR_APPEND,
				"job-sheets-supported", "standard");

	/* input_types */
	if (tmp->input_types != NULL) {
		int i;

		for (i = 0; tmp->input_types[i] != NULL; i++)
			papiAttributeListAddLPString(&p->attributes,
				PAPI_ATTR_APPEND, "document-format-supported",
				lp_type_to_mime_type(tmp->input_types[i]));
	}

	/* description */
	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
			"printer-info", tmp->description);

	/* add lpsched specific attributes */
	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
				"device-uri", tmp->device);
	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-dial-info", tmp->dial_info);
	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-fault-recovery", tmp->fault_rec);
	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-interface-script", tmp->interface);
	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-data-rate", tmp->speed);
	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-stty", tmp->stty);
	papiAttributeListAddBoolean(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-login-term", tmp->login);
	papiAttributeListAddBoolean(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-daisy", tmp->daisy);
	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-charsets", tmp->char_sets);
#ifdef CAN_DO_MODULES
	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-modules", tmp->modules);
#endif /* CAN_DO_MODULES */
	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-options", tmp->options);
	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-printer-type", tmp->printer_types);
	if (tmp->fault_alert.shcmd != NULL) {
		papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-fault-alert-command",
				tmp->fault_alert.shcmd);
		papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-fault-alert-threshold",
				tmp->fault_alert.Q);
		papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-fault-alert-interval",
				tmp->fault_alert.W);
	}
	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-cpi-value", tmp->cpi.val);
	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-cpi-unit", tmp->cpi.sc);
	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-lpi-value", tmp->lpi.val);
	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-lpi-unit", tmp->lpi.sc);
	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-plen-value", tmp->plen.val);
	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-plen-unit", tmp->plen.sc);
	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-pwid-value", tmp->pwid.val);
	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
				"lpsched-pwid-unit", tmp->pwid.sc);

	/* allow/deny list */
	load_userprinter_access(dest, &allowed, &denied);
	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
				"requesting-user-name-allowed", allowed);
	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
				"requesting-user-name-denied", denied);

#ifdef LP_USE_PAPI_ATTR
	if (tmp->ppd != NULL) {
		int fd;
		struct stat sbuf;

		/* construct the two URIs for the printer's PPD file */
		if (uname(&sysname) < 0) {
			/* failed to get systen name */
			sysname.nodename[0] = 0;
		}
		snprintf(buf, sizeof (buf), "file://%s%s/ppd/%s.ppd",
			sysname.nodename, ETCDIR, tmp->name);
		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
			"ppd-file-uri", buf);

		snprintf(buf, sizeof (buf), "file://%s%s",
			sysname.nodename, tmp->ppd);
		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
			"lpsched-printer-configure-ppd-uri", buf);
		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
			"lpsched-ppd-source-path", tmp->ppd);

		snprintf(buf, sizeof (buf), "%s/ppd/%s.ppd", ETCDIR, tmp->name);

		/*
		 * We don't return error on any of the error conditions, we just
		 * silently return without adding the attribute.
		 */
		PPDFileToAttributesList(&p->attributes, buf);
	}
#endif

	freeprinter(tmp);

	return (PAPI_OK);
}