1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*0Sstevel@tonic-gate /* All Rights Reserved */ 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate /* 27*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 28*0Sstevel@tonic-gate * Use is subject to license terms. 29*0Sstevel@tonic-gate */ 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */ 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #include "stdio.h" 36*0Sstevel@tonic-gate #include "string.h" 37*0Sstevel@tonic-gate #include "errno.h" 38*0Sstevel@tonic-gate #include "sys/types.h" 39*0Sstevel@tonic-gate #include "stdlib.h" 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #include "lp.h" 42*0Sstevel@tonic-gate #include "printers.h" 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate extern struct { 45*0Sstevel@tonic-gate char *v; 46*0Sstevel@tonic-gate short len, 47*0Sstevel@tonic-gate okremote; 48*0Sstevel@tonic-gate } prtrheadings[]; 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate /** 51*0Sstevel@tonic-gate ** getprinter() - EXTRACT PRINTER STRUCTURE FROM DISK FILE 52*0Sstevel@tonic-gate **/ 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate PRINTER * 55*0Sstevel@tonic-gate getprinter(char *name) 56*0Sstevel@tonic-gate { 57*0Sstevel@tonic-gate static long lastdir = -1; 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate static PRINTER prbuf; 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate char buf[BUFSIZ]; 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate short daisy; 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate int fld; 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate int fd; 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate FALERT *pa; 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate register char * p; 72*0Sstevel@tonic-gate register char ** pp; 73*0Sstevel@tonic-gate register char *** ppp; 74*0Sstevel@tonic-gate register char * path; 75*0Sstevel@tonic-gate int isNameAll; 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate if (!name || !*name) { 80*0Sstevel@tonic-gate errno = EINVAL; 81*0Sstevel@tonic-gate return (0); 82*0Sstevel@tonic-gate } 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate /* 85*0Sstevel@tonic-gate * Getting ``all''? If so, jump into the directory 86*0Sstevel@tonic-gate * wherever we left off. 87*0Sstevel@tonic-gate */ 88*0Sstevel@tonic-gate isNameAll = STREQU(NAME_ALL, name); 89*0Sstevel@tonic-gate for (; ; ) { 90*0Sstevel@tonic-gate /* fix for bug 1117241 91*0Sstevel@tonic-gate * occasionally when a printer is removed, a printer directory 92*0Sstevel@tonic-gate * is left behind, but the CONFIGFILE is removed. In this 93*0Sstevel@tonic-gate * case this directory terminates the search for additional 94*0Sstevel@tonic-gate * printers as we have been returning 0 in this case. 95*0Sstevel@tonic-gate * Now, we loop back and try the next directory until 96*0Sstevel@tonic-gate * we have no more directories or we find a directory with 97*0Sstevel@tonic-gate * a CONFIGFILE 98*0Sstevel@tonic-gate */ 99*0Sstevel@tonic-gate if (isNameAll) { 100*0Sstevel@tonic-gate if (!(name = next_dir(Lp_A_Printers, &lastdir))) 101*0Sstevel@tonic-gate return (0); 102*0Sstevel@tonic-gate } else 103*0Sstevel@tonic-gate lastdir = -1; 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate /* 106*0Sstevel@tonic-gate * Get the printer configuration information. 107*0Sstevel@tonic-gate */ 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate path = getprinterfile(name, CONFIGFILE); 110*0Sstevel@tonic-gate if (!path) { 111*0Sstevel@tonic-gate if (isNameAll) 112*0Sstevel@tonic-gate Free(name); 113*0Sstevel@tonic-gate return (0); 114*0Sstevel@tonic-gate } 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate if ((fd = open_locked(path, "r", 0)) < 0) { 117*0Sstevel@tonic-gate Free(path); /* 118*0Sstevel@tonic-gate * go around to loop again for 119*0Sstevel@tonic-gate * NAME_ALL case 120*0Sstevel@tonic-gate */ 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate if (!isNameAll) /* fix for bug 1117241 */ 123*0Sstevel@tonic-gate return(0); 124*0Sstevel@tonic-gate else 125*0Sstevel@tonic-gate Free(name); 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate else 128*0Sstevel@tonic-gate break; 129*0Sstevel@tonic-gate } 130*0Sstevel@tonic-gate Free (path); 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate /* 133*0Sstevel@tonic-gate * Initialize the entire structure, to ensure no random 134*0Sstevel@tonic-gate * values get in it. However, make sure some values won't 135*0Sstevel@tonic-gate * be null or empty. Do the latter here as opposed to 136*0Sstevel@tonic-gate * after reading the file, because sometimes the file 137*0Sstevel@tonic-gate * contains an empty header to FORCE a null/empty value. 138*0Sstevel@tonic-gate */ 139*0Sstevel@tonic-gate (void)memset ((char *)&prbuf, 0, sizeof(prbuf)); 140*0Sstevel@tonic-gate prbuf.name = Strdup(name); 141*0Sstevel@tonic-gate if (isNameAll) 142*0Sstevel@tonic-gate Free(name); 143*0Sstevel@tonic-gate prbuf.printer_types = getlist(NAME_UNKNOWN, LP_WS, LP_SEP); 144*0Sstevel@tonic-gate prbuf.input_types = getlist(NAME_SIMPLE, LP_WS, LP_SEP); 145*0Sstevel@tonic-gate #if defined(CAN_DO_MODULES) 146*0Sstevel@tonic-gate prbuf.modules = getlist(NAME_DEFAULT, LP_WS, LP_SEP); 147*0Sstevel@tonic-gate #endif 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate /* 150*0Sstevel@tonic-gate * Read the file. 151*0Sstevel@tonic-gate */ 152*0Sstevel@tonic-gate errno = 0; 153*0Sstevel@tonic-gate while (fdgets(buf, BUFSIZ, fd) != NULL) { 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate buf[strlen(buf) - 1] = 0; 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate for (fld = 0; fld < PR_MAX; fld++) 158*0Sstevel@tonic-gate if ( 159*0Sstevel@tonic-gate prtrheadings[fld].v 160*0Sstevel@tonic-gate && prtrheadings[fld].len 161*0Sstevel@tonic-gate && STRNEQU( 162*0Sstevel@tonic-gate buf, 163*0Sstevel@tonic-gate prtrheadings[fld].v, 164*0Sstevel@tonic-gate prtrheadings[fld].len 165*0Sstevel@tonic-gate ) 166*0Sstevel@tonic-gate ) { 167*0Sstevel@tonic-gate p = buf + prtrheadings[fld].len; 168*0Sstevel@tonic-gate while (*p && *p == ' ') 169*0Sstevel@tonic-gate p++; 170*0Sstevel@tonic-gate break; 171*0Sstevel@tonic-gate } 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate /* 174*0Sstevel@tonic-gate * To allow future extensions to not impact applications 175*0Sstevel@tonic-gate * using old versions of this routine, ignore strange 176*0Sstevel@tonic-gate * fields. 177*0Sstevel@tonic-gate */ 178*0Sstevel@tonic-gate if (fld >= PR_MAX) 179*0Sstevel@tonic-gate continue; 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate switch (fld) { 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate case PR_BAN: 184*0Sstevel@tonic-gate if ((pp = getlist(p, LP_WS, ":"))) { 185*0Sstevel@tonic-gate if (pp[0] != NULL) { 186*0Sstevel@tonic-gate if (strcmp(pp[0], NAME_OPTIONAL) == 0) 187*0Sstevel@tonic-gate prbuf.banner = BAN_OPTIONAL; 188*0Sstevel@tonic-gate else if (strcmp(pp[0], NAME_OFF) == 0) 189*0Sstevel@tonic-gate prbuf.banner = BAN_NEVER; 190*0Sstevel@tonic-gate else if (strcmp(pp[0], NAME_ON) == 0) 191*0Sstevel@tonic-gate prbuf.banner = BAN_ALWAYS; 192*0Sstevel@tonic-gate else /* default to the LP default */ 193*0Sstevel@tonic-gate prbuf.banner = BAN_ALWAYS; 194*0Sstevel@tonic-gate } 195*0Sstevel@tonic-gate if (pp[1] && CS_STREQU(pp[1], NAME_ALWAYS)) 196*0Sstevel@tonic-gate prbuf.banner |= BAN_ALWAYS; 197*0Sstevel@tonic-gate freelist (pp); 198*0Sstevel@tonic-gate } 199*0Sstevel@tonic-gate break; 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate case PR_LOGIN: 202*0Sstevel@tonic-gate prbuf.login = LOG_IN; 203*0Sstevel@tonic-gate break; 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate case PR_CPI: 206*0Sstevel@tonic-gate prbuf.cpi = getcpi(p); 207*0Sstevel@tonic-gate break; 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate case PR_LPI: 210*0Sstevel@tonic-gate prbuf.lpi = getsdn(p); 211*0Sstevel@tonic-gate break; 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate case PR_LEN: 214*0Sstevel@tonic-gate prbuf.plen = getsdn(p); 215*0Sstevel@tonic-gate break; 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate case PR_WIDTH: 218*0Sstevel@tonic-gate prbuf.pwid = getsdn(p); 219*0Sstevel@tonic-gate break; 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate case PR_CS: 222*0Sstevel@tonic-gate ppp = &(prbuf.char_sets); 223*0Sstevel@tonic-gate goto CharStarStar; 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate case PR_ITYPES: 226*0Sstevel@tonic-gate ppp = &(prbuf.input_types); 227*0Sstevel@tonic-gate CharStarStar: if (*ppp) 228*0Sstevel@tonic-gate freelist (*ppp); 229*0Sstevel@tonic-gate *ppp = getlist(p, LP_WS, LP_SEP); 230*0Sstevel@tonic-gate break; 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate case PR_DEV: 233*0Sstevel@tonic-gate pp = &(prbuf.device); 234*0Sstevel@tonic-gate goto CharStar; 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate case PR_DIAL: 237*0Sstevel@tonic-gate pp = &(prbuf.dial_info); 238*0Sstevel@tonic-gate goto CharStar; 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate case PR_RECOV: 241*0Sstevel@tonic-gate pp = &(prbuf.fault_rec); 242*0Sstevel@tonic-gate goto CharStar; 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gate case PR_INTFC: 245*0Sstevel@tonic-gate pp = &(prbuf.interface); 246*0Sstevel@tonic-gate goto CharStar; 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate case PR_PTYPE: 249*0Sstevel@tonic-gate ppp = &(prbuf.printer_types); 250*0Sstevel@tonic-gate goto CharStarStar; 251*0Sstevel@tonic-gate 252*0Sstevel@tonic-gate case PR_REMOTE: 253*0Sstevel@tonic-gate pp = &(prbuf.remote); 254*0Sstevel@tonic-gate goto CharStar; 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate case PR_SPEED: 257*0Sstevel@tonic-gate pp = &(prbuf.speed); 258*0Sstevel@tonic-gate goto CharStar; 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gate case PR_STTY: 261*0Sstevel@tonic-gate pp = &(prbuf.stty); 262*0Sstevel@tonic-gate CharStar: if (*pp) 263*0Sstevel@tonic-gate Free (*pp); 264*0Sstevel@tonic-gate *pp = Strdup(p); 265*0Sstevel@tonic-gate break; 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate #if defined(CAN_DO_MODULES) 268*0Sstevel@tonic-gate case PR_MODULES: 269*0Sstevel@tonic-gate ppp = &(prbuf.modules); 270*0Sstevel@tonic-gate goto CharStarStar; 271*0Sstevel@tonic-gate #endif 272*0Sstevel@tonic-gate 273*0Sstevel@tonic-gate case PR_OPTIONS: 274*0Sstevel@tonic-gate ppp = &(prbuf.options); 275*0Sstevel@tonic-gate goto CharStarStar; 276*0Sstevel@tonic-gate break; 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate case PR_PPD: 279*0Sstevel@tonic-gate { 280*0Sstevel@tonic-gate pp = &(prbuf.ppd); 281*0Sstevel@tonic-gate goto CharStar; 282*0Sstevel@tonic-gate } 283*0Sstevel@tonic-gate } 284*0Sstevel@tonic-gate 285*0Sstevel@tonic-gate } 286*0Sstevel@tonic-gate if (errno != 0) { 287*0Sstevel@tonic-gate int save_errno = errno; 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate freeprinter (&prbuf); 290*0Sstevel@tonic-gate close(fd); 291*0Sstevel@tonic-gate errno = save_errno; 292*0Sstevel@tonic-gate return (0); 293*0Sstevel@tonic-gate } 294*0Sstevel@tonic-gate close(fd); 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate /* 297*0Sstevel@tonic-gate * Get the printer description (if it exists). 298*0Sstevel@tonic-gate */ 299*0Sstevel@tonic-gate if (!(path = getprinterfile(prbuf.name, COMMENTFILE))) 300*0Sstevel@tonic-gate return (0); 301*0Sstevel@tonic-gate if (!(prbuf.description = loadstring(path)) && errno != ENOENT) { 302*0Sstevel@tonic-gate Free (path); 303*0Sstevel@tonic-gate freeprinter (&prbuf); 304*0Sstevel@tonic-gate return (0); 305*0Sstevel@tonic-gate } 306*0Sstevel@tonic-gate Free (path); 307*0Sstevel@tonic-gate 308*0Sstevel@tonic-gate /* 309*0Sstevel@tonic-gate * Get the information for the alert. Don't fail if we can't 310*0Sstevel@tonic-gate * read it because of access permission UNLESS we're "root" 311*0Sstevel@tonic-gate * or "lp" 312*0Sstevel@tonic-gate */ 313*0Sstevel@tonic-gate if (!(pa = getalert(Lp_A_Printers, prbuf.name))) { 314*0Sstevel@tonic-gate if ( 315*0Sstevel@tonic-gate errno != ENOENT 316*0Sstevel@tonic-gate && ( 317*0Sstevel@tonic-gate errno != EACCES 318*0Sstevel@tonic-gate || !getpid() /* we be root */ 319*0Sstevel@tonic-gate || STREQU(getname(), LPUSER) /* we be lp */ 320*0Sstevel@tonic-gate ) 321*0Sstevel@tonic-gate ) { 322*0Sstevel@tonic-gate freeprinter (&prbuf); 323*0Sstevel@tonic-gate return (0); 324*0Sstevel@tonic-gate } 325*0Sstevel@tonic-gate } else 326*0Sstevel@tonic-gate prbuf.fault_alert = *pa; 327*0Sstevel@tonic-gate 328*0Sstevel@tonic-gate /* 329*0Sstevel@tonic-gate * Now go through the structure and see if we have 330*0Sstevel@tonic-gate * anything strange. 331*0Sstevel@tonic-gate */ 332*0Sstevel@tonic-gate if (!okprinter(prbuf.name, &prbuf, 0)) { 333*0Sstevel@tonic-gate freeprinter (&prbuf); 334*0Sstevel@tonic-gate errno = EBADF; 335*0Sstevel@tonic-gate return (0); 336*0Sstevel@tonic-gate } 337*0Sstevel@tonic-gate 338*0Sstevel@tonic-gate /* 339*0Sstevel@tonic-gate * Just in case somebody tried to pull a fast one 340*0Sstevel@tonic-gate * by giving a printer type header by itself.... 341*0Sstevel@tonic-gate */ 342*0Sstevel@tonic-gate if (!prbuf.printer_types) 343*0Sstevel@tonic-gate prbuf.printer_types = getlist(NAME_UNKNOWN, LP_WS, LP_SEP); 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate /* 346*0Sstevel@tonic-gate * If there are more than one printer type, then we can't 347*0Sstevel@tonic-gate * have any input types, except perhaps ``simple''. 348*0Sstevel@tonic-gate */ 349*0Sstevel@tonic-gate if ( 350*0Sstevel@tonic-gate lenlist(prbuf.printer_types) > 1 351*0Sstevel@tonic-gate && prbuf.input_types 352*0Sstevel@tonic-gate && ( 353*0Sstevel@tonic-gate lenlist(prbuf.input_types) > 1 354*0Sstevel@tonic-gate || !STREQU(NAME_SIMPLE, *prbuf.input_types) 355*0Sstevel@tonic-gate ) 356*0Sstevel@tonic-gate ) { 357*0Sstevel@tonic-gate freeprinter (&prbuf); 358*0Sstevel@tonic-gate badprinter = BAD_ITYPES; 359*0Sstevel@tonic-gate errno = EBADF; 360*0Sstevel@tonic-gate return (0); 361*0Sstevel@tonic-gate } 362*0Sstevel@tonic-gate 363*0Sstevel@tonic-gate /* 364*0Sstevel@tonic-gate * If there are more than one printer types, none can 365*0Sstevel@tonic-gate * be ``unknown''. 366*0Sstevel@tonic-gate */ 367*0Sstevel@tonic-gate if ( 368*0Sstevel@tonic-gate lenlist(prbuf.printer_types) > 1 369*0Sstevel@tonic-gate && searchlist(NAME_UNKNOWN, prbuf.printer_types) 370*0Sstevel@tonic-gate ) { 371*0Sstevel@tonic-gate freeprinter (&prbuf); 372*0Sstevel@tonic-gate badprinter = BAD_PTYPES; 373*0Sstevel@tonic-gate errno = EBADF; 374*0Sstevel@tonic-gate return (0); 375*0Sstevel@tonic-gate } 376*0Sstevel@tonic-gate 377*0Sstevel@tonic-gate /* 378*0Sstevel@tonic-gate * All the printer types had better agree on whether the 379*0Sstevel@tonic-gate * printer takes print wheels! 380*0Sstevel@tonic-gate */ 381*0Sstevel@tonic-gate prbuf.daisy = -1; 382*0Sstevel@tonic-gate for (pp = prbuf.printer_types; *pp; pp++) { 383*0Sstevel@tonic-gate tidbit (*pp, "daisy", &daisy); 384*0Sstevel@tonic-gate if (daisy == -1) 385*0Sstevel@tonic-gate daisy = 0; 386*0Sstevel@tonic-gate if (prbuf.daisy == -1) 387*0Sstevel@tonic-gate prbuf.daisy = daisy; 388*0Sstevel@tonic-gate else if (prbuf.daisy != daisy) { 389*0Sstevel@tonic-gate freeprinter (&prbuf); 390*0Sstevel@tonic-gate badprinter = BAD_DAISY; 391*0Sstevel@tonic-gate errno = EBADF; 392*0Sstevel@tonic-gate return (0); 393*0Sstevel@tonic-gate } 394*0Sstevel@tonic-gate } 395*0Sstevel@tonic-gate 396*0Sstevel@tonic-gate /* 397*0Sstevel@tonic-gate * Help out those who are still using the obsolete 398*0Sstevel@tonic-gate * "printer_type" member. 399*0Sstevel@tonic-gate */ 400*0Sstevel@tonic-gate prbuf.printer_type = Strdup(*prbuf.printer_types); 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate return (&prbuf); 403*0Sstevel@tonic-gate } 404