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>
382847Sjacobs #include <ctype.h>
390Sstevel@tonic-gate
402830Sdjl static int
check_name(nss_XbyY_args_t * argp,const char * line,int linelen)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
702847Sjacobs nss_status_t
_nss_files_XY_printer(be,args,filter,check)712847Sjacobs _nss_files_XY_printer(be, args, filter, check)
722847Sjacobs files_backend_ptr_t be;
732847Sjacobs nss_XbyY_args_t *args;
742847Sjacobs const char *filter; /* advisory, to speed up */
752847Sjacobs /* string search */
762847Sjacobs files_XY_check_func check; /* NULL means one-shot, for getXXent */
772847Sjacobs {
782847Sjacobs nss_status_t res;
792847Sjacobs int parsestat;
802847Sjacobs int (*func)();
812847Sjacobs
822847Sjacobs if (filter != NULL && *filter == '\0')
832847Sjacobs return (NSS_NOTFOUND);
842847Sjacobs if (be->buf == 0 &&
852847Sjacobs (be->buf = malloc(be->minbuf)) == 0) {
862847Sjacobs return (NSS_UNAVAIL); /* really panic, malloc failed */
872847Sjacobs }
882847Sjacobs
892847Sjacobs if (check != 0 || be->f == 0) {
902847Sjacobs if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) {
912847Sjacobs return (res);
922847Sjacobs }
932847Sjacobs }
942847Sjacobs
952847Sjacobs res = NSS_NOTFOUND;
962847Sjacobs
972847Sjacobs /*CONSTCOND*/
982847Sjacobs while (1) {
992847Sjacobs char *instr = be->buf;
1002847Sjacobs int linelen;
1012847Sjacobs
1022847Sjacobs if ((linelen = _nss_files_read_line(be->f, instr,
1032847Sjacobs be->minbuf)) < 0) {
1042847Sjacobs /* End of file */
1052847Sjacobs args->returnval = 0;
1062847Sjacobs args->returnlen = 0;
1072847Sjacobs break;
1082847Sjacobs }
1092847Sjacobs
1102847Sjacobs /* begin at the first non-blank character */
1112847Sjacobs while (isspace(*instr)) {
1122847Sjacobs instr++;
1132847Sjacobs linelen--;
1142847Sjacobs }
1152847Sjacobs
1162847Sjacobs /* comment line, skip it. */
1172847Sjacobs if (*instr == '#')
1182847Sjacobs continue;
1192847Sjacobs
1202847Sjacobs /* blank line, skip it */
1212847Sjacobs if ((*instr == '\n') || (*instr == '\0'))
1222847Sjacobs continue;
1232847Sjacobs
1242847Sjacobs if (filter != 0 && strstr(instr, filter) == 0) {
1252847Sjacobs /*
1262847Sjacobs * Optimization: if the entry doesn't contain the
1272847Sjacobs * filter string then it can't be the entry we want,
1282847Sjacobs * so don't bother looking more closely at it.
1292847Sjacobs */
1302847Sjacobs continue;
1312847Sjacobs }
1322847Sjacobs
1332847Sjacobs args->returnval = 0;
1342847Sjacobs args->returnlen = 0;
1352847Sjacobs
1362847Sjacobs if (check != NULL && (*check)(args, instr, linelen) == 0)
1372847Sjacobs continue;
1382847Sjacobs
1392847Sjacobs func = args->str2ent;
1402847Sjacobs parsestat = (*func)(instr, linelen, args->buf.result,
1412847Sjacobs args->buf.buffer, args->buf.buflen);
1422847Sjacobs
1432847Sjacobs if (parsestat == NSS_STR_PARSE_SUCCESS) {
1442847Sjacobs args->returnval = (args->buf.result != NULL)?
1452847Sjacobs args->buf.result : args->buf.buffer;
1462847Sjacobs args->returnlen = linelen;
1472847Sjacobs res = NSS_SUCCESS;
1482847Sjacobs break;
1492847Sjacobs } else if (parsestat == NSS_STR_PARSE_ERANGE) {
1502847Sjacobs args->erange = 1;
1512847Sjacobs break;
1522847Sjacobs } else if (parsestat == NSS_STR_PARSE_PARSE)
1532847Sjacobs continue;
1542847Sjacobs }
1552847Sjacobs
1562847Sjacobs /*
1572847Sjacobs * stayopen is set to 0 by default in order to close the opened
1582847Sjacobs * file. Some applications may break if it is set to 1.
1592847Sjacobs */
1602847Sjacobs if (check != 0 && !args->stayopen) {
1612847Sjacobs (void) _nss_files_endent(be, 0);
1622847Sjacobs }
1632847Sjacobs
1642847Sjacobs return (res);
1652847Sjacobs }
1662847Sjacobs
1672847Sjacobs static nss_status_t
getent(be,a)1682847Sjacobs getent(be, a)
1692847Sjacobs files_backend_ptr_t be;
1702847Sjacobs void *a;
1712847Sjacobs {
1722847Sjacobs nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
1732847Sjacobs
174*2849Sjacobs return (_nss_files_XY_printer(be, argp, (const char *)0,
175*2849Sjacobs (files_XY_check_func)0));
1762847Sjacobs }
1772847Sjacobs
1780Sstevel@tonic-gate static nss_status_t
getbyname(be,a)1790Sstevel@tonic-gate getbyname(be, a)
1800Sstevel@tonic-gate files_backend_ptr_t be;
1810Sstevel@tonic-gate void *a;
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
1840Sstevel@tonic-gate
1852847Sjacobs return (_nss_files_XY_printer(be, argp, argp->key.name, check_name));
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate static files_backend_op_t printers_ops[] = {
1890Sstevel@tonic-gate _nss_files_destr,
1900Sstevel@tonic-gate _nss_files_endent,
1910Sstevel@tonic-gate _nss_files_setent,
1922847Sjacobs getent,
1930Sstevel@tonic-gate getbyname
1940Sstevel@tonic-gate };
1950Sstevel@tonic-gate
1962830Sdjl /*ARGSUSED*/
1970Sstevel@tonic-gate nss_backend_t *
_nss_files_printers_constr(dummy1,dummy2,dummy3)1980Sstevel@tonic-gate _nss_files_printers_constr(dummy1, dummy2, dummy3)
1990Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3;
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate return (_nss_files_constr(printers_ops,
2022830Sdjl sizeof (printers_ops) / sizeof (printers_ops[0]),
2032830Sdjl printers,
2042830Sdjl NSS_LINELEN_PRINTERS,
2052830Sdjl NULL));
2060Sstevel@tonic-gate }
207