1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * files/printers_getbyname.c -- "files" backend for 23 * nsswitch "printers" database. 24 * 25 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 26 * Use is subject to license terms. 27 */ 28 29 #pragma ident "%Z%%M% %I% %E% SMI" 30 31 static const char *printers = "/etc/printers.conf"; 32 33 #pragma weak _nss_files__printers_constr = _nss_files_printers_constr 34 35 #include "files_common.h" 36 #include <stdlib.h> 37 #include <strings.h> 38 #include <ctype.h> 39 40 static int 41 check_name(nss_XbyY_args_t *argp, const char *line, int linelen) 42 { 43 44 const char *limit, *linep; 45 const char *keyp = argp->key.name; 46 int klen = strlen(keyp); 47 48 linep = line; 49 limit = line + linelen; 50 51 /* 52 * find the name in the namelist a|b|c...: 53 */ 54 while (linep+klen < limit && *linep != '|' && *linep != ':') { 55 if ((strncmp(linep, keyp, klen) == 0) && 56 ((*(linep + klen) == '|') || (*(linep + klen) == ':'))) { 57 return (1); 58 } else { 59 while (linep < limit && *linep != '|' && *linep != ':') 60 linep++; 61 if (linep >= limit || *linep == ':') 62 return (0); 63 if (*linep == '|') 64 linep++; 65 } 66 } 67 return (0); 68 } 69 70 nss_status_t 71 _nss_files_XY_printer(be, args, filter, check) 72 files_backend_ptr_t be; 73 nss_XbyY_args_t *args; 74 const char *filter; /* advisory, to speed up */ 75 /* string search */ 76 files_XY_check_func check; /* NULL means one-shot, for getXXent */ 77 { 78 nss_status_t res; 79 int parsestat; 80 int (*func)(); 81 82 if (filter != NULL && *filter == '\0') 83 return (NSS_NOTFOUND); 84 if (be->buf == 0 && 85 (be->buf = malloc(be->minbuf)) == 0) { 86 return (NSS_UNAVAIL); /* really panic, malloc failed */ 87 } 88 89 if (check != 0 || be->f == 0) { 90 if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) { 91 return (res); 92 } 93 } 94 95 res = NSS_NOTFOUND; 96 97 /*CONSTCOND*/ 98 while (1) { 99 char *instr = be->buf; 100 int linelen; 101 102 if ((linelen = _nss_files_read_line(be->f, instr, 103 be->minbuf)) < 0) { 104 /* End of file */ 105 args->returnval = 0; 106 args->returnlen = 0; 107 break; 108 } 109 110 /* begin at the first non-blank character */ 111 while (isspace(*instr)) { 112 instr++; 113 linelen--; 114 } 115 116 /* comment line, skip it. */ 117 if (*instr == '#') 118 continue; 119 120 /* blank line, skip it */ 121 if ((*instr == '\n') || (*instr == '\0')) 122 continue; 123 124 if (filter != 0 && strstr(instr, filter) == 0) { 125 /* 126 * Optimization: if the entry doesn't contain the 127 * filter string then it can't be the entry we want, 128 * so don't bother looking more closely at it. 129 */ 130 continue; 131 } 132 133 args->returnval = 0; 134 args->returnlen = 0; 135 136 if (check != NULL && (*check)(args, instr, linelen) == 0) 137 continue; 138 139 func = args->str2ent; 140 parsestat = (*func)(instr, linelen, args->buf.result, 141 args->buf.buffer, args->buf.buflen); 142 143 if (parsestat == NSS_STR_PARSE_SUCCESS) { 144 args->returnval = (args->buf.result != NULL)? 145 args->buf.result : args->buf.buffer; 146 args->returnlen = linelen; 147 res = NSS_SUCCESS; 148 break; 149 } else if (parsestat == NSS_STR_PARSE_ERANGE) { 150 args->erange = 1; 151 break; 152 } else if (parsestat == NSS_STR_PARSE_PARSE) 153 continue; 154 } 155 156 /* 157 * stayopen is set to 0 by default in order to close the opened 158 * file. Some applications may break if it is set to 1. 159 */ 160 if (check != 0 && !args->stayopen) { 161 (void) _nss_files_endent(be, 0); 162 } 163 164 return (res); 165 } 166 167 static nss_status_t 168 getent(be, a) 169 files_backend_ptr_t be; 170 void *a; 171 { 172 nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 173 174 return (_nss_files_XY_printer(be, argp, (const char *)0, 175 (files_XY_check_func)0)); 176 } 177 178 static nss_status_t 179 getbyname(be, a) 180 files_backend_ptr_t be; 181 void *a; 182 { 183 nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 184 185 return (_nss_files_XY_printer(be, argp, argp->key.name, check_name)); 186 } 187 188 static files_backend_op_t printers_ops[] = { 189 _nss_files_destr, 190 _nss_files_endent, 191 _nss_files_setent, 192 getent, 193 getbyname 194 }; 195 196 /*ARGSUSED*/ 197 nss_backend_t * 198 _nss_files_printers_constr(dummy1, dummy2, dummy3) 199 const char *dummy1, *dummy2, *dummy3; 200 { 201 return (_nss_files_constr(printers_ops, 202 sizeof (printers_ops) / sizeof (printers_ops[0]), 203 printers, 204 NSS_LINELEN_PRINTERS, 205 NULL)); 206 } 207