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 /* 2312091SSonam.Gupta@Sun.COM * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 242264Sjacobs * 252264Sjacobs */ 262264Sjacobs 272383Sjacobs /* $Id: lp.c 179 2006-07-17 18:24:07Z njacobs $ */ 282264Sjacobs 292264Sjacobs #include <stdio.h> 302264Sjacobs #include <stdlib.h> 312264Sjacobs #include <unistd.h> 322264Sjacobs #include <string.h> 332264Sjacobs #include <locale.h> 342264Sjacobs #include <libintl.h> 352264Sjacobs #include <papi.h> 362264Sjacobs #include "common.h" 37*12503SRalph.Turner@Sun.COM #include <pwd.h> 38*12503SRalph.Turner@Sun.COM #include <grp.h> 39*12503SRalph.Turner@Sun.COM #include <sys/types.h> 402264Sjacobs #ifdef HAVE_LIBMAGIC /* for mimetype auto-detection */ 412264Sjacobs #include <magic.h> 422264Sjacobs #endif /* HAVE_LIBMAGIC */ 432264Sjacobs 442264Sjacobs static void 452264Sjacobs usage(char *program) 462264Sjacobs { 472264Sjacobs char *name; 482264Sjacobs 492264Sjacobs if ((name = strrchr(program, '/')) == NULL) 502264Sjacobs name = program; 512264Sjacobs else 522264Sjacobs name++; 532264Sjacobs 542264Sjacobs fprintf(stdout, 559256SSonam.Gupta@Sun.COM gettext("Usage: %s [-c] [-m] [-p] [-s] [-w] [-d destination] " 569256SSonam.Gupta@Sun.COM "[-f form-name] [-H special-handling] [-n number] " 579256SSonam.Gupta@Sun.COM "[-o option] [-P page-list] [-q priority-level] " 589256SSonam.Gupta@Sun.COM "[-S character-set | print-wheel] [-t title] [-v] " 599256SSonam.Gupta@Sun.COM "[-T content-type [-r]] [-y mode-list] [file...]\n"), 609256SSonam.Gupta@Sun.COM name); 612264Sjacobs exit(1); 622264Sjacobs } 632264Sjacobs 642264Sjacobs int 652264Sjacobs main(int ac, char *av[]) 662264Sjacobs { 672264Sjacobs papi_status_t status; 682264Sjacobs papi_service_t svc = NULL; 692264Sjacobs papi_attribute_t **list = NULL; 702264Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 712264Sjacobs papi_job_t job = NULL; 727253Sjacobs char prefetch[3]; 737253Sjacobs int prefetch_len = sizeof (prefetch); 742264Sjacobs char *printer = NULL; 752264Sjacobs char b = PAPI_TRUE; 762264Sjacobs int copy = 0; 772264Sjacobs int silent = 0; 782264Sjacobs int dump = 0; 792264Sjacobs int validate = 0; 802264Sjacobs int modify = -1; 812264Sjacobs int c; 82*12503SRalph.Turner@Sun.COM uid_t ruid; 83*12503SRalph.Turner@Sun.COM struct passwd *pw; 842264Sjacobs 852264Sjacobs (void) setlocale(LC_ALL, ""); 862264Sjacobs (void) textdomain("SUNW_OST_OSCMD"); 872264Sjacobs 88*12503SRalph.Turner@Sun.COM ruid = getuid(); 89*12503SRalph.Turner@Sun.COM if ((pw = getpwuid(ruid)) != NULL) 90*12503SRalph.Turner@Sun.COM (void) initgroups(pw->pw_name, pw->pw_gid); 91*12503SRalph.Turner@Sun.COM (void) setuid(ruid); 92*12503SRalph.Turner@Sun.COM 93*12503SRalph.Turner@Sun.COM 942264Sjacobs while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF) 952264Sjacobs switch (c) { 962264Sjacobs case 'H': /* handling */ 972264Sjacobs if (strcasecmp(optarg, "hold") == 0) 982264Sjacobs papiAttributeListAddString(&list, 999256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, 1009256SSonam.Gupta@Sun.COM "job-hold-until", "indefinite"); 1016725Sjacobs else if (strcasecmp(optarg, "immediate") == 0) 1022264Sjacobs papiAttributeListAddString(&list, 1039256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, 1049256SSonam.Gupta@Sun.COM "job-hold-until", "no-hold"); 1052264Sjacobs else 1062264Sjacobs papiAttributeListAddString(&list, 1079256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, 1089256SSonam.Gupta@Sun.COM "job-hold-until", optarg); 1092264Sjacobs break; 1106725Sjacobs case 'P': { /* page list */ 1116725Sjacobs char buf[BUFSIZ]; 1126725Sjacobs 1136725Sjacobs snprintf(buf, sizeof (buf), "page-ranges=%s", optarg); 1146725Sjacobs papiAttributeListFromString(&list, 1159256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, buf); 1166725Sjacobs } 1172264Sjacobs break; 1182264Sjacobs case 'S': /* charset */ 1192264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1209256SSonam.Gupta@Sun.COM "lp-charset", optarg); 1212264Sjacobs break; 1222264Sjacobs case 'T': /* type */ 1232264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1249256SSonam.Gupta@Sun.COM "document-format", 1259256SSonam.Gupta@Sun.COM lp_type_to_mime_type(optarg)); 1262264Sjacobs break; 1272264Sjacobs case 'D': /* dump */ 1282264Sjacobs dump = 1; 1292264Sjacobs break; 1302264Sjacobs case 'c': /* copy */ 1312264Sjacobs copy = 1; 1322264Sjacobs break; 1332264Sjacobs case 'd': /* destination */ 1342264Sjacobs printer = optarg; 1352264Sjacobs break; 1362264Sjacobs case 'f': /* form */ 1372264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1389256SSonam.Gupta@Sun.COM "form", optarg); 1392264Sjacobs break; 1402264Sjacobs case 'i': /* modify job */ 1412264Sjacobs if ((get_printer_id(optarg, &printer, &modify) < 0) || 1422264Sjacobs (modify < 0)) { 1432264Sjacobs fprintf(stderr, 1449256SSonam.Gupta@Sun.COM gettext("invalid request id: %s\n"), 1459256SSonam.Gupta@Sun.COM optarg); 1462264Sjacobs exit(1); 1472264Sjacobs } 1482264Sjacobs break; 1492264Sjacobs case 'm': /* mail when complete */ 1502264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1519256SSonam.Gupta@Sun.COM "rfc-1179-mail", 1); 1522264Sjacobs break; 1532264Sjacobs case 'n': /* copies */ 1542264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 1559256SSonam.Gupta@Sun.COM "copies", atoi(optarg)); 1562264Sjacobs break; 1572264Sjacobs case 'o': /* lp "options" */ 1582264Sjacobs papiAttributeListFromString(&list, 1599256SSonam.Gupta@Sun.COM PAPI_ATTR_REPLACE, optarg); 1602264Sjacobs break; 1612264Sjacobs case 'p': /* Solaris - notification */ 1622264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1639256SSonam.Gupta@Sun.COM "rfc-1179-mail", 1); 1642264Sjacobs break; 1652264Sjacobs case 'q': { /* priority */ 1662264Sjacobs int i = atoi(optarg); 1672264Sjacobs 1686725Sjacobs i = 100 - (i * 2.5); 1692264Sjacobs if ((i < 1) || (i > 100)) { 1702264Sjacobs fprintf(stderr, gettext( 1712264Sjacobs "priority must be between 0 and 39.\n")); 1722264Sjacobs exit(1); 1732264Sjacobs } 1742264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 1759256SSonam.Gupta@Sun.COM "job-priority", i); 1762264Sjacobs } 1772264Sjacobs break; 1782264Sjacobs case 'r': /* "raw" mode */ 1792264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1809256SSonam.Gupta@Sun.COM "document-format", 1819256SSonam.Gupta@Sun.COM "application/octet-stream"); 1822264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 1839256SSonam.Gupta@Sun.COM "stty", "raw"); 1842264Sjacobs break; 1852264Sjacobs case 's': /* suppress message */ 1862264Sjacobs silent = 1; 1872264Sjacobs break; 1882264Sjacobs case 't': /* title */ 1892264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1909256SSonam.Gupta@Sun.COM "job-name", optarg); 1912264Sjacobs break; 1922264Sjacobs case 'V': /* validate */ 1932264Sjacobs validate = 1; 1942264Sjacobs break; 1952264Sjacobs case 'w': 1962264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1979256SSonam.Gupta@Sun.COM "rfc-1179-mail", 1); 1982264Sjacobs break; 1992264Sjacobs case 'y': /* lp "modes" */ 2002264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 2019256SSonam.Gupta@Sun.COM "lp-modes", optarg); 2022264Sjacobs break; 2032264Sjacobs case 'E': 2042264Sjacobs encryption = PAPI_ENCRYPT_REQUIRED; 2052264Sjacobs break; 2062264Sjacobs default: 2072264Sjacobs usage(av[0]); 2082264Sjacobs } 2092264Sjacobs 2102264Sjacobs /* convert "banner", "nobanner" to "job-sheet" */ 2112264Sjacobs if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) { 2122264Sjacobs (void) papiAttributeListDelete(&list, "banner"); 2132264Sjacobs if (b == PAPI_FALSE) 2142264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2159256SSonam.Gupta@Sun.COM "job-sheets", "none"); 2162264Sjacobs } 2172264Sjacobs 2182264Sjacobs if ((printer == NULL) && 2192264Sjacobs ((printer = getenv("PRINTER")) == NULL) && 2202264Sjacobs ((printer = getenv("LPDEST")) == NULL)) 2212264Sjacobs printer = DEFAULT_DEST; 2222264Sjacobs 2232660Sjacobs if (((optind + 1) == ac) && (strcmp(av[optind], "-") == 0)) 2242660Sjacobs optind = ac; 2252660Sjacobs 2262264Sjacobs if (modify == -1) { 2273125Sjacobs char *document_format = "text/plain"; 2282264Sjacobs 2292264Sjacobs if (optind != ac) { 2302264Sjacobs /* get the mime type of the file data */ 2313125Sjacobs #ifdef MAGIC_MIME 2322264Sjacobs magic_t ms = NULL; 2332264Sjacobs 2342264Sjacobs if ((ms = magic_open(MAGIC_MIME)) != NULL) { 2352264Sjacobs document_format = magic_file(ms, av[optind]); 2362264Sjacobs magic_close(ms); 2372264Sjacobs } 2383125Sjacobs #else 2393125Sjacobs if (is_postscript(av[optind]) == 1) 2403125Sjacobs document_format = "application/postscript"; 2413125Sjacobs #endif 2427253Sjacobs } else { 2437253Sjacobs if (is_postscript_stream(0, prefetch, &prefetch_len) 2449256SSonam.Gupta@Sun.COM == 1) 2457253Sjacobs document_format = "application/postscript"; 2462264Sjacobs } 2472264Sjacobs 2482264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1); 2492264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2509256SSonam.Gupta@Sun.COM "document-format", document_format); 2512264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2529256SSonam.Gupta@Sun.COM "job-sheets", "standard"); 2532264Sjacobs } 2542264Sjacobs 2552264Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback, 2569256SSonam.Gupta@Sun.COM encryption, NULL); 2572264Sjacobs if (status != PAPI_OK) { 2582264Sjacobs fprintf(stderr, gettext( 2599256SSonam.Gupta@Sun.COM "Failed to contact service for %s: %s\n"), printer, 2609256SSonam.Gupta@Sun.COM verbose_papi_message(svc, status)); 2612264Sjacobs exit(1); 2622264Sjacobs } 2632264Sjacobs 2643125Sjacobs if (dump != 0) { 2653125Sjacobs printf("requesting attributes:\n"); 2663125Sjacobs papiAttributeListPrint(stdout, list, "\t"); 2673125Sjacobs printf("\n"); 2683125Sjacobs } 2693125Sjacobs 2702264Sjacobs if (modify != -1) 2712264Sjacobs status = papiJobModify(svc, printer, modify, list, &job); 2722264Sjacobs else if (optind == ac) /* no file list, use stdin */ 2737253Sjacobs status = jobSubmitSTDIN(svc, printer, prefetch, prefetch_len, 2749256SSonam.Gupta@Sun.COM list, &job); 2752264Sjacobs else if (validate == 1) /* validate the request can be processed */ 2762264Sjacobs status = papiJobValidate(svc, printer, list, 2779256SSonam.Gupta@Sun.COM NULL, &av[optind], &job); 2782264Sjacobs else if (copy == 0) /* reference the files in the job, default */ 2792264Sjacobs status = papiJobSubmitByReference(svc, printer, list, 2809256SSonam.Gupta@Sun.COM NULL, &av[optind], &job); 2812264Sjacobs else /* copy the files before return, -c */ 2822264Sjacobs status = papiJobSubmit(svc, printer, list, 2839256SSonam.Gupta@Sun.COM NULL, &av[optind], &job); 2842264Sjacobs 2852264Sjacobs papiAttributeListFree(list); 2862264Sjacobs 2872264Sjacobs if (status != PAPI_OK) { 2882264Sjacobs fprintf(stderr, gettext("%s: %s\n"), printer, 2899256SSonam.Gupta@Sun.COM verbose_papi_message(svc, status)); 2902264Sjacobs papiJobFree(job); 2912264Sjacobs papiServiceDestroy(svc); 2922264Sjacobs exit(1); 2932264Sjacobs } 2942264Sjacobs 2952264Sjacobs if (((silent == 0) || (dump != 0)) && 2962264Sjacobs ((list = papiJobGetAttributeList(job)) != NULL)) { 29712091SSonam.Gupta@Sun.COM int32_t id = -1; 2982264Sjacobs 2999256SSonam.Gupta@Sun.COM if (printer == NULL) 3009256SSonam.Gupta@Sun.COM papiAttributeListGetString(list, NULL, 3019256SSonam.Gupta@Sun.COM "printer-name", &printer); 3029256SSonam.Gupta@Sun.COM 30312091SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL, 30412091SSonam.Gupta@Sun.COM "job-id-requested", &id); 30512091SSonam.Gupta@Sun.COM if (id == -1) { 30612091SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL, "job-id", &id); 30712091SSonam.Gupta@Sun.COM } 30812091SSonam.Gupta@Sun.COM 3092383Sjacobs printf(gettext("request id is %s-%d "), printer, id); 3102264Sjacobs if (ac != optind) 3112264Sjacobs printf("(%d file(s))\n", ac - optind); 3122264Sjacobs else 3132264Sjacobs printf("(standard input)\n"); 3142264Sjacobs 3152264Sjacobs if (dump != 0) { 3162264Sjacobs printf("job attributes:\n"); 3172264Sjacobs papiAttributeListPrint(stdout, list, "\t"); 3182264Sjacobs printf("\n"); 3192264Sjacobs } 3202264Sjacobs } 3212264Sjacobs 3222264Sjacobs papiJobFree(job); 3232264Sjacobs papiServiceDestroy(svc); 3242264Sjacobs 3252264Sjacobs return (0); 3262264Sjacobs } 327