10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52571Sjacobs * Common Development and Distribution License (the "License"). 62571Sjacobs * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 220Sstevel@tonic-gate * files/printers_getbyname.c -- "files" backend for 230Sstevel@tonic-gate * nsswitch "printers" database. 240Sstevel@tonic-gate * 252571Sjacobs * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 260Sstevel@tonic-gate * Use is subject to license terms. 270Sstevel@tonic-gate */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 300Sstevel@tonic-gate 310Sstevel@tonic-gate static const char *printers = "/etc/printers.conf"; 320Sstevel@tonic-gate 330Sstevel@tonic-gate #pragma weak _nss_files__printers_constr = _nss_files_printers_constr 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include "files_common.h" 360Sstevel@tonic-gate #include <stdlib.h> 370Sstevel@tonic-gate #include <strings.h> 38*2847Sjacobs #include <ctype.h> 390Sstevel@tonic-gate 402830Sdjl static int 412830Sdjl check_name(nss_XbyY_args_t *argp, const char *line, int linelen) 422830Sdjl { 430Sstevel@tonic-gate 442830Sdjl const char *limit, *linep; 452830Sdjl const char *keyp = argp->key.name; 462830Sdjl int klen = strlen(keyp); 472830Sdjl 482830Sdjl linep = line; 492830Sdjl limit = line + linelen; 500Sstevel@tonic-gate 512830Sdjl /* 522830Sdjl * find the name in the namelist a|b|c...: 532830Sdjl */ 542830Sdjl while (linep+klen < limit && *linep != '|' && *linep != ':') { 552830Sdjl if ((strncmp(linep, keyp, klen) == 0) && 562830Sdjl ((*(linep + klen) == '|') || (*(linep + klen) == ':'))) { 572830Sdjl return (1); 582830Sdjl } else { 592830Sdjl while (linep < limit && *linep != '|' && *linep != ':') 602830Sdjl linep++; 612830Sdjl if (linep >= limit || *linep == ':') 622830Sdjl return (0); 632830Sdjl if (*linep == '|') 642830Sdjl linep++; 652830Sdjl } 662830Sdjl } 672830Sdjl return (0); 680Sstevel@tonic-gate } 690Sstevel@tonic-gate 70*2847Sjacobs nss_status_t 71*2847Sjacobs _nss_files_XY_printer(be, args, filter, check) 72*2847Sjacobs files_backend_ptr_t be; 73*2847Sjacobs nss_XbyY_args_t *args; 74*2847Sjacobs const char *filter; /* advisory, to speed up */ 75*2847Sjacobs /* string search */ 76*2847Sjacobs files_XY_check_func check; /* NULL means one-shot, for getXXent */ 77*2847Sjacobs { 78*2847Sjacobs nss_status_t res; 79*2847Sjacobs int parsestat; 80*2847Sjacobs int (*func)(); 81*2847Sjacobs 82*2847Sjacobs if (filter != NULL && *filter == '\0') 83*2847Sjacobs return (NSS_NOTFOUND); 84*2847Sjacobs if (be->buf == 0 && 85*2847Sjacobs (be->buf = malloc(be->minbuf)) == 0) { 86*2847Sjacobs return (NSS_UNAVAIL); /* really panic, malloc failed */ 87*2847Sjacobs } 88*2847Sjacobs 89*2847Sjacobs if (check != 0 || be->f == 0) { 90*2847Sjacobs if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) { 91*2847Sjacobs return (res); 92*2847Sjacobs } 93*2847Sjacobs } 94*2847Sjacobs 95*2847Sjacobs res = NSS_NOTFOUND; 96*2847Sjacobs 97*2847Sjacobs /*CONSTCOND*/ 98*2847Sjacobs while (1) { 99*2847Sjacobs char *instr = be->buf; 100*2847Sjacobs int linelen; 101*2847Sjacobs 102*2847Sjacobs if ((linelen = _nss_files_read_line(be->f, instr, 103*2847Sjacobs be->minbuf)) < 0) { 104*2847Sjacobs /* End of file */ 105*2847Sjacobs args->returnval = 0; 106*2847Sjacobs args->returnlen = 0; 107*2847Sjacobs break; 108*2847Sjacobs } 109*2847Sjacobs 110*2847Sjacobs /* begin at the first non-blank character */ 111*2847Sjacobs while (isspace(*instr)) { 112*2847Sjacobs instr++; 113*2847Sjacobs linelen--; 114*2847Sjacobs } 115*2847Sjacobs 116*2847Sjacobs /* comment line, skip it. */ 117*2847Sjacobs if (*instr == '#') 118*2847Sjacobs continue; 119*2847Sjacobs 120*2847Sjacobs /* blank line, skip it */ 121*2847Sjacobs if ((*instr == '\n') || (*instr == '\0')) 122*2847Sjacobs continue; 123*2847Sjacobs 124*2847Sjacobs if (filter != 0 && strstr(instr, filter) == 0) { 125*2847Sjacobs /* 126*2847Sjacobs * Optimization: if the entry doesn't contain the 127*2847Sjacobs * filter string then it can't be the entry we want, 128*2847Sjacobs * so don't bother looking more closely at it. 129*2847Sjacobs */ 130*2847Sjacobs continue; 131*2847Sjacobs } 132*2847Sjacobs 133*2847Sjacobs args->returnval = 0; 134*2847Sjacobs args->returnlen = 0; 135*2847Sjacobs 136*2847Sjacobs if (check != NULL && (*check)(args, instr, linelen) == 0) 137*2847Sjacobs continue; 138*2847Sjacobs 139*2847Sjacobs func = args->str2ent; 140*2847Sjacobs parsestat = (*func)(instr, linelen, args->buf.result, 141*2847Sjacobs args->buf.buffer, args->buf.buflen); 142*2847Sjacobs 143*2847Sjacobs if (parsestat == NSS_STR_PARSE_SUCCESS) { 144*2847Sjacobs args->returnval = (args->buf.result != NULL)? 145*2847Sjacobs args->buf.result : args->buf.buffer; 146*2847Sjacobs args->returnlen = linelen; 147*2847Sjacobs res = NSS_SUCCESS; 148*2847Sjacobs break; 149*2847Sjacobs } else if (parsestat == NSS_STR_PARSE_ERANGE) { 150*2847Sjacobs args->erange = 1; 151*2847Sjacobs break; 152*2847Sjacobs } else if (parsestat == NSS_STR_PARSE_PARSE) 153*2847Sjacobs continue; 154*2847Sjacobs } 155*2847Sjacobs 156*2847Sjacobs /* 157*2847Sjacobs * stayopen is set to 0 by default in order to close the opened 158*2847Sjacobs * file. Some applications may break if it is set to 1. 159*2847Sjacobs */ 160*2847Sjacobs if (check != 0 && !args->stayopen) { 161*2847Sjacobs (void) _nss_files_endent(be, 0); 162*2847Sjacobs } 163*2847Sjacobs 164*2847Sjacobs return (res); 165*2847Sjacobs } 166*2847Sjacobs 167*2847Sjacobs static nss_status_t 168*2847Sjacobs getent(be, a) 169*2847Sjacobs files_backend_ptr_t be; 170*2847Sjacobs void *a; 171*2847Sjacobs { 172*2847Sjacobs nss_status_t status; 173*2847Sjacobs nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 174*2847Sjacobs 175*2847Sjacobs return (_nss_files_XY_printer(be, argp, (files_XY_check_func)0, 176*2847Sjacobs (const char *)0)); 177*2847Sjacobs } 178*2847Sjacobs 1790Sstevel@tonic-gate static nss_status_t 1800Sstevel@tonic-gate getbyname(be, a) 1810Sstevel@tonic-gate files_backend_ptr_t be; 1820Sstevel@tonic-gate void *a; 1830Sstevel@tonic-gate { 1840Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 1850Sstevel@tonic-gate 186*2847Sjacobs return (_nss_files_XY_printer(be, argp, argp->key.name, check_name)); 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate static files_backend_op_t printers_ops[] = { 1900Sstevel@tonic-gate _nss_files_destr, 1910Sstevel@tonic-gate _nss_files_endent, 1920Sstevel@tonic-gate _nss_files_setent, 193*2847Sjacobs getent, 1940Sstevel@tonic-gate getbyname 1950Sstevel@tonic-gate }; 1960Sstevel@tonic-gate 1972830Sdjl /*ARGSUSED*/ 1980Sstevel@tonic-gate nss_backend_t * 1990Sstevel@tonic-gate _nss_files_printers_constr(dummy1, dummy2, dummy3) 2000Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3; 2010Sstevel@tonic-gate { 2020Sstevel@tonic-gate return (_nss_files_constr(printers_ops, 2032830Sdjl sizeof (printers_ops) / sizeof (printers_ops[0]), 2042830Sdjl printers, 2052830Sdjl NSS_LINELEN_PRINTERS, 2062830Sdjl NULL)); 2070Sstevel@tonic-gate } 208