1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate * 26*7c478bd9Sstevel@tonic-gate * Common code and structures used by name-service-switch "user" backends. 27*7c478bd9Sstevel@tonic-gate * Much of this was taken directly from the files_common.c source. 28*7c478bd9Sstevel@tonic-gate */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate /* 33*7c478bd9Sstevel@tonic-gate * An implementation that used mmap() sensibly would be a wonderful thing, 34*7c478bd9Sstevel@tonic-gate * but this here is just yer standard fgets() thang. 35*7c478bd9Sstevel@tonic-gate */ 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #include <stdio.h> 38*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 39*7c478bd9Sstevel@tonic-gate #include <ctype.h> 40*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 41*7c478bd9Sstevel@tonic-gate #include <poll.h> 42*7c478bd9Sstevel@tonic-gate #include <unistd.h> 43*7c478bd9Sstevel@tonic-gate #include "user_common.h" 44*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 45*7c478bd9Sstevel@tonic-gate #include "../../../libnsl/include/nsl_stdio_prv.h" 46*7c478bd9Sstevel@tonic-gate #include <string.h> 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate nss_status_t 49*7c478bd9Sstevel@tonic-gate _nss_user_setent(be, dummy) 50*7c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 51*7c478bd9Sstevel@tonic-gate void *dummy; 52*7c478bd9Sstevel@tonic-gate { 53*7c478bd9Sstevel@tonic-gate if (be->f == 0) { 54*7c478bd9Sstevel@tonic-gate if (be->filename == 0) { 55*7c478bd9Sstevel@tonic-gate /* Backend isn't initialized properly? */ 56*7c478bd9Sstevel@tonic-gate return (NSS_UNAVAIL); 57*7c478bd9Sstevel@tonic-gate } 58*7c478bd9Sstevel@tonic-gate if ((be->f = __nsl_fopen(be->filename, "r")) == 0) { 59*7c478bd9Sstevel@tonic-gate return (NSS_UNAVAIL); 60*7c478bd9Sstevel@tonic-gate } 61*7c478bd9Sstevel@tonic-gate } else { 62*7c478bd9Sstevel@tonic-gate __nsl_rewind(be->f); 63*7c478bd9Sstevel@tonic-gate } 64*7c478bd9Sstevel@tonic-gate return (NSS_SUCCESS); 65*7c478bd9Sstevel@tonic-gate } 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate nss_status_t 68*7c478bd9Sstevel@tonic-gate _nss_user_endent(be, dummy) 69*7c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 70*7c478bd9Sstevel@tonic-gate void *dummy; 71*7c478bd9Sstevel@tonic-gate { 72*7c478bd9Sstevel@tonic-gate if (be->f != 0) { 73*7c478bd9Sstevel@tonic-gate __nsl_fclose(be->f); 74*7c478bd9Sstevel@tonic-gate be->f = 0; 75*7c478bd9Sstevel@tonic-gate } 76*7c478bd9Sstevel@tonic-gate if (be->buf != 0) { 77*7c478bd9Sstevel@tonic-gate free(be->buf); 78*7c478bd9Sstevel@tonic-gate be->buf = 0; 79*7c478bd9Sstevel@tonic-gate } 80*7c478bd9Sstevel@tonic-gate return (NSS_SUCCESS); 81*7c478bd9Sstevel@tonic-gate } 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate /* 84*7c478bd9Sstevel@tonic-gate * This routine reads a line, including the processing of continuation 85*7c478bd9Sstevel@tonic-gate * characters. It always leaves (or inserts) \n\0 at the end of the line. 86*7c478bd9Sstevel@tonic-gate * It returns the length of the line read, excluding the \n\0. Who's idea 87*7c478bd9Sstevel@tonic-gate * was this? 88*7c478bd9Sstevel@tonic-gate * Returns -1 on EOF. 89*7c478bd9Sstevel@tonic-gate * 90*7c478bd9Sstevel@tonic-gate * Note that since each concurrent call to _nss_user_read_line has 91*7c478bd9Sstevel@tonic-gate * it's own FILE pointer, we can use getc_unlocked w/o difficulties, 92*7c478bd9Sstevel@tonic-gate * a substantial performance win. 93*7c478bd9Sstevel@tonic-gate */ 94*7c478bd9Sstevel@tonic-gate int 95*7c478bd9Sstevel@tonic-gate _nss_user_read_line(f, buffer, buflen) 96*7c478bd9Sstevel@tonic-gate __NSL_FILE *f; 97*7c478bd9Sstevel@tonic-gate char *buffer; 98*7c478bd9Sstevel@tonic-gate int buflen; 99*7c478bd9Sstevel@tonic-gate { 100*7c478bd9Sstevel@tonic-gate int linelen; /* 1st unused slot in buffer */ 101*7c478bd9Sstevel@tonic-gate int c; 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate while (1) { 104*7c478bd9Sstevel@tonic-gate linelen = 0; 105*7c478bd9Sstevel@tonic-gate while (linelen < buflen - 1) { /* "- 1" saves room for \n\0 */ 106*7c478bd9Sstevel@tonic-gate switch (c = __nsl_getc_unlocked(f)) { 107*7c478bd9Sstevel@tonic-gate case EOF: 108*7c478bd9Sstevel@tonic-gate if (linelen == 0 || 109*7c478bd9Sstevel@tonic-gate buffer[linelen - 1] == '\\') { 110*7c478bd9Sstevel@tonic-gate return (-1); 111*7c478bd9Sstevel@tonic-gate } else { 112*7c478bd9Sstevel@tonic-gate buffer[linelen ] = '\n'; 113*7c478bd9Sstevel@tonic-gate buffer[linelen + 1] = '\0'; 114*7c478bd9Sstevel@tonic-gate return (linelen); 115*7c478bd9Sstevel@tonic-gate } 116*7c478bd9Sstevel@tonic-gate case '\n': 117*7c478bd9Sstevel@tonic-gate if (linelen > 0 && 118*7c478bd9Sstevel@tonic-gate buffer[linelen - 1] == '\\') { 119*7c478bd9Sstevel@tonic-gate --linelen; /* remove the '\\' */ 120*7c478bd9Sstevel@tonic-gate } else { 121*7c478bd9Sstevel@tonic-gate buffer[linelen ] = '\n'; 122*7c478bd9Sstevel@tonic-gate buffer[linelen + 1] = '\0'; 123*7c478bd9Sstevel@tonic-gate return (linelen); 124*7c478bd9Sstevel@tonic-gate } 125*7c478bd9Sstevel@tonic-gate break; 126*7c478bd9Sstevel@tonic-gate default: 127*7c478bd9Sstevel@tonic-gate buffer[linelen++] = c; 128*7c478bd9Sstevel@tonic-gate } 129*7c478bd9Sstevel@tonic-gate } 130*7c478bd9Sstevel@tonic-gate /* Buffer overflow -- eat rest of line and loop again */ 131*7c478bd9Sstevel@tonic-gate /* ===> Should syslog() */ 132*7c478bd9Sstevel@tonic-gate do { 133*7c478bd9Sstevel@tonic-gate c = __nsl_getc_unlocked(f); 134*7c478bd9Sstevel@tonic-gate if (c == EOF) { 135*7c478bd9Sstevel@tonic-gate return (-1); 136*7c478bd9Sstevel@tonic-gate } 137*7c478bd9Sstevel@tonic-gate } while (c != '\n'); 138*7c478bd9Sstevel@tonic-gate } 139*7c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 140*7c478bd9Sstevel@tonic-gate } 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate /* 144*7c478bd9Sstevel@tonic-gate * Could implement this as an iterator function on top of _nss_user_do_all(), 145*7c478bd9Sstevel@tonic-gate * but the shared code is small enough that it'd be pretty silly. 146*7c478bd9Sstevel@tonic-gate */ 147*7c478bd9Sstevel@tonic-gate nss_status_t 148*7c478bd9Sstevel@tonic-gate _nss_user_XY_all(be, args, netdb, filter, check) 149*7c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 150*7c478bd9Sstevel@tonic-gate nss_XbyY_args_t *args; 151*7c478bd9Sstevel@tonic-gate int netdb; /* whether it uses netdb */ 152*7c478bd9Sstevel@tonic-gate /* format or not */ 153*7c478bd9Sstevel@tonic-gate const char *filter; /* advisory, to speed up */ 154*7c478bd9Sstevel@tonic-gate /* string search */ 155*7c478bd9Sstevel@tonic-gate user_XY_check_func check; /* NULL means one-shot, for getXXent */ 156*7c478bd9Sstevel@tonic-gate { 157*7c478bd9Sstevel@tonic-gate nss_status_t res; 158*7c478bd9Sstevel@tonic-gate int parsestat; 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate if (be->buf == 0 && 161*7c478bd9Sstevel@tonic-gate (be->buf = malloc(be->minbuf)) == 0) { 162*7c478bd9Sstevel@tonic-gate return (NSS_UNAVAIL); /* really panic, malloc failed */ 163*7c478bd9Sstevel@tonic-gate } 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate if (check != 0 || be->f == 0) { 166*7c478bd9Sstevel@tonic-gate if ((res = _nss_user_setent(be, 0)) != NSS_SUCCESS) { 167*7c478bd9Sstevel@tonic-gate return (res); 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate } 170*7c478bd9Sstevel@tonic-gate 171*7c478bd9Sstevel@tonic-gate res = NSS_NOTFOUND; 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate while (1) { 174*7c478bd9Sstevel@tonic-gate char *instr = be->buf; 175*7c478bd9Sstevel@tonic-gate int linelen; 176*7c478bd9Sstevel@tonic-gate 177*7c478bd9Sstevel@tonic-gate if ((linelen = _nss_user_read_line(be->f, instr, 178*7c478bd9Sstevel@tonic-gate be->minbuf)) < 0) { 179*7c478bd9Sstevel@tonic-gate /* End of file */ 180*7c478bd9Sstevel@tonic-gate args->returnval = 0; 181*7c478bd9Sstevel@tonic-gate args->erange = 0; 182*7c478bd9Sstevel@tonic-gate break; 183*7c478bd9Sstevel@tonic-gate } 184*7c478bd9Sstevel@tonic-gate if (filter != 0 && strstr(instr, filter) == 0) { 185*7c478bd9Sstevel@tonic-gate /* 186*7c478bd9Sstevel@tonic-gate * Optimization: if the entry doesn't contain the 187*7c478bd9Sstevel@tonic-gate * filter string then it can't be the entry we want, 188*7c478bd9Sstevel@tonic-gate * so don't bother looking more closely at it. 189*7c478bd9Sstevel@tonic-gate */ 190*7c478bd9Sstevel@tonic-gate continue; 191*7c478bd9Sstevel@tonic-gate } 192*7c478bd9Sstevel@tonic-gate if (netdb) { 193*7c478bd9Sstevel@tonic-gate char *first; 194*7c478bd9Sstevel@tonic-gate char *last; 195*7c478bd9Sstevel@tonic-gate 196*7c478bd9Sstevel@tonic-gate if ((last = strchr(instr, '#')) == 0) { 197*7c478bd9Sstevel@tonic-gate last = instr + linelen; 198*7c478bd9Sstevel@tonic-gate } 199*7c478bd9Sstevel@tonic-gate *last-- = '\0'; /* Nuke '\n' or #comment */ 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate /* 202*7c478bd9Sstevel@tonic-gate * Skip leading whitespace. Normally there isn't 203*7c478bd9Sstevel@tonic-gate * any, so it's not worth calling strspn(). 204*7c478bd9Sstevel@tonic-gate */ 205*7c478bd9Sstevel@tonic-gate for (first = instr; isspace(*first); first++) { 206*7c478bd9Sstevel@tonic-gate ; 207*7c478bd9Sstevel@tonic-gate } 208*7c478bd9Sstevel@tonic-gate if (*first == '\0') { 209*7c478bd9Sstevel@tonic-gate continue; 210*7c478bd9Sstevel@tonic-gate } 211*7c478bd9Sstevel@tonic-gate /* 212*7c478bd9Sstevel@tonic-gate * Found something non-blank on the line. Skip back 213*7c478bd9Sstevel@tonic-gate * over any trailing whitespace; since we know 214*7c478bd9Sstevel@tonic-gate * there's non-whitespace earlier in the line, 215*7c478bd9Sstevel@tonic-gate * checking for termination is easy. 216*7c478bd9Sstevel@tonic-gate */ 217*7c478bd9Sstevel@tonic-gate while (isspace(*last)) { 218*7c478bd9Sstevel@tonic-gate --last; 219*7c478bd9Sstevel@tonic-gate } 220*7c478bd9Sstevel@tonic-gate 221*7c478bd9Sstevel@tonic-gate linelen = last - first + 1; 222*7c478bd9Sstevel@tonic-gate if (first != instr) { 223*7c478bd9Sstevel@tonic-gate instr = first; 224*7c478bd9Sstevel@tonic-gate } 225*7c478bd9Sstevel@tonic-gate } 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate args->returnval = 0; 228*7c478bd9Sstevel@tonic-gate parsestat = (*args->str2ent)(instr, linelen, args->buf.result, 229*7c478bd9Sstevel@tonic-gate args->buf.buffer, args->buf.buflen); 230*7c478bd9Sstevel@tonic-gate 231*7c478bd9Sstevel@tonic-gate if (parsestat == NSS_STR_PARSE_SUCCESS) { 232*7c478bd9Sstevel@tonic-gate args->returnval = args->buf.result; 233*7c478bd9Sstevel@tonic-gate if (check == 0 || (*check)(args)) { 234*7c478bd9Sstevel@tonic-gate res = NSS_SUCCESS; 235*7c478bd9Sstevel@tonic-gate break; 236*7c478bd9Sstevel@tonic-gate } 237*7c478bd9Sstevel@tonic-gate } else if (parsestat == NSS_STR_PARSE_ERANGE) { 238*7c478bd9Sstevel@tonic-gate args->erange = 1; /* should we just skip this */ 239*7c478bd9Sstevel@tonic-gate /* one long line ?? */ 240*7c478bd9Sstevel@tonic-gate } /* else if (parsestat == NSS_STR_PARSE_PARSE) don't care ! */ 241*7c478bd9Sstevel@tonic-gate } 242*7c478bd9Sstevel@tonic-gate 243*7c478bd9Sstevel@tonic-gate /* 244*7c478bd9Sstevel@tonic-gate * stayopen is set to 0 by default in order to close the opened 245*7c478bd9Sstevel@tonic-gate * file. Some applications may break if it is set to 1. 246*7c478bd9Sstevel@tonic-gate */ 247*7c478bd9Sstevel@tonic-gate if (check != 0 && !args->stayopen) { 248*7c478bd9Sstevel@tonic-gate (void) _nss_user_endent(be, 0); 249*7c478bd9Sstevel@tonic-gate } 250*7c478bd9Sstevel@tonic-gate 251*7c478bd9Sstevel@tonic-gate return (res); 252*7c478bd9Sstevel@tonic-gate } 253*7c478bd9Sstevel@tonic-gate 254*7c478bd9Sstevel@tonic-gate 255*7c478bd9Sstevel@tonic-gate nss_status_t 256*7c478bd9Sstevel@tonic-gate _nss_user_destr(be, dummy) 257*7c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 258*7c478bd9Sstevel@tonic-gate void *dummy; 259*7c478bd9Sstevel@tonic-gate { 260*7c478bd9Sstevel@tonic-gate if (be != 0) { 261*7c478bd9Sstevel@tonic-gate if (be->f != 0) { 262*7c478bd9Sstevel@tonic-gate _nss_user_endent(be, 0); 263*7c478bd9Sstevel@tonic-gate } 264*7c478bd9Sstevel@tonic-gate free((char *)be->filename); 265*7c478bd9Sstevel@tonic-gate free(be); 266*7c478bd9Sstevel@tonic-gate } 267*7c478bd9Sstevel@tonic-gate return (NSS_SUCCESS); /* In case anyone is dumb enough to check */ 268*7c478bd9Sstevel@tonic-gate } 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate nss_backend_t * 271*7c478bd9Sstevel@tonic-gate _nss_user_constr(ops, n_ops, filename, min_bufsize) 272*7c478bd9Sstevel@tonic-gate user_backend_op_t ops[]; 273*7c478bd9Sstevel@tonic-gate int n_ops; 274*7c478bd9Sstevel@tonic-gate const char *filename; 275*7c478bd9Sstevel@tonic-gate int min_bufsize; 276*7c478bd9Sstevel@tonic-gate { 277*7c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 278*7c478bd9Sstevel@tonic-gate 279*7c478bd9Sstevel@tonic-gate if ((be = (user_backend_ptr_t) malloc(sizeof (*be))) == 0) { 280*7c478bd9Sstevel@tonic-gate return (0); 281*7c478bd9Sstevel@tonic-gate } 282*7c478bd9Sstevel@tonic-gate be->ops = ops; 283*7c478bd9Sstevel@tonic-gate be->n_ops = n_ops; 284*7c478bd9Sstevel@tonic-gate if ((be->filename = strdup(filename)) == NULL) { 285*7c478bd9Sstevel@tonic-gate free(be); 286*7c478bd9Sstevel@tonic-gate return (NULL); 287*7c478bd9Sstevel@tonic-gate } 288*7c478bd9Sstevel@tonic-gate be->minbuf = min_bufsize; 289*7c478bd9Sstevel@tonic-gate be->f = 0; 290*7c478bd9Sstevel@tonic-gate be->buf = 0; 291*7c478bd9Sstevel@tonic-gate 292*7c478bd9Sstevel@tonic-gate return ((nss_backend_t *) be); 293*7c478bd9Sstevel@tonic-gate } 294