17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5004388ebScasper * Common Development and Distribution License (the "License"). 6004388ebScasper * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22004388ebScasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate * 257c478bd9Sstevel@tonic-gate * Common code and structures used by name-service-switch "user" backends. 267c478bd9Sstevel@tonic-gate * Much of this was taken directly from the files_common.c source. 277c478bd9Sstevel@tonic-gate */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate /* 327c478bd9Sstevel@tonic-gate * An implementation that used mmap() sensibly would be a wonderful thing, 337c478bd9Sstevel@tonic-gate * but this here is just yer standard fgets() thang. 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 36004388ebScasper #include "user_common.h" 377c478bd9Sstevel@tonic-gate #include <stdio.h> 387c478bd9Sstevel@tonic-gate #include <stdlib.h> 397c478bd9Sstevel@tonic-gate #include <ctype.h> 407c478bd9Sstevel@tonic-gate #include <fcntl.h> 417c478bd9Sstevel@tonic-gate #include <poll.h> 427c478bd9Sstevel@tonic-gate #include <unistd.h> 437c478bd9Sstevel@tonic-gate #include <sys/stat.h> 447c478bd9Sstevel@tonic-gate #include <string.h> 457c478bd9Sstevel@tonic-gate 46*cb5caa98Sdjl /*ARGSUSED*/ 477c478bd9Sstevel@tonic-gate nss_status_t 487c478bd9Sstevel@tonic-gate _nss_user_setent(be, dummy) 497c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 507c478bd9Sstevel@tonic-gate void *dummy; 517c478bd9Sstevel@tonic-gate { 527c478bd9Sstevel@tonic-gate if (be->f == 0) { 537c478bd9Sstevel@tonic-gate if (be->filename == 0) { 547c478bd9Sstevel@tonic-gate /* Backend isn't initialized properly? */ 557c478bd9Sstevel@tonic-gate return (NSS_UNAVAIL); 567c478bd9Sstevel@tonic-gate } 57004388ebScasper if ((be->f = fopen(be->filename, "rF")) == 0) { 587c478bd9Sstevel@tonic-gate return (NSS_UNAVAIL); 597c478bd9Sstevel@tonic-gate } 607c478bd9Sstevel@tonic-gate } else { 61004388ebScasper rewind(be->f); 627c478bd9Sstevel@tonic-gate } 637c478bd9Sstevel@tonic-gate return (NSS_SUCCESS); 647c478bd9Sstevel@tonic-gate } 657c478bd9Sstevel@tonic-gate 66*cb5caa98Sdjl /*ARGSUSED*/ 677c478bd9Sstevel@tonic-gate nss_status_t 687c478bd9Sstevel@tonic-gate _nss_user_endent(be, dummy) 697c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 707c478bd9Sstevel@tonic-gate void *dummy; 717c478bd9Sstevel@tonic-gate { 727c478bd9Sstevel@tonic-gate if (be->f != 0) { 73*cb5caa98Sdjl (void) fclose(be->f); 747c478bd9Sstevel@tonic-gate be->f = 0; 757c478bd9Sstevel@tonic-gate } 767c478bd9Sstevel@tonic-gate if (be->buf != 0) { 777c478bd9Sstevel@tonic-gate free(be->buf); 787c478bd9Sstevel@tonic-gate be->buf = 0; 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate return (NSS_SUCCESS); 817c478bd9Sstevel@tonic-gate } 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate /* 847c478bd9Sstevel@tonic-gate * This routine reads a line, including the processing of continuation 857c478bd9Sstevel@tonic-gate * characters. It always leaves (or inserts) \n\0 at the end of the line. 867c478bd9Sstevel@tonic-gate * It returns the length of the line read, excluding the \n\0. Who's idea 877c478bd9Sstevel@tonic-gate * was this? 887c478bd9Sstevel@tonic-gate * Returns -1 on EOF. 897c478bd9Sstevel@tonic-gate * 907c478bd9Sstevel@tonic-gate * Note that since each concurrent call to _nss_user_read_line has 917c478bd9Sstevel@tonic-gate * it's own FILE pointer, we can use getc_unlocked w/o difficulties, 927c478bd9Sstevel@tonic-gate * a substantial performance win. 937c478bd9Sstevel@tonic-gate */ 947c478bd9Sstevel@tonic-gate int 957c478bd9Sstevel@tonic-gate _nss_user_read_line(f, buffer, buflen) 96004388ebScasper FILE *f; 977c478bd9Sstevel@tonic-gate char *buffer; 987c478bd9Sstevel@tonic-gate int buflen; 997c478bd9Sstevel@tonic-gate { 1007c478bd9Sstevel@tonic-gate int linelen; /* 1st unused slot in buffer */ 1017c478bd9Sstevel@tonic-gate int c; 1027c478bd9Sstevel@tonic-gate 103*cb5caa98Sdjl /*CONSTCOND*/ 1047c478bd9Sstevel@tonic-gate while (1) { 1057c478bd9Sstevel@tonic-gate linelen = 0; 1067c478bd9Sstevel@tonic-gate while (linelen < buflen - 1) { /* "- 1" saves room for \n\0 */ 107004388ebScasper switch (c = getc_unlocked(f)) { 1087c478bd9Sstevel@tonic-gate case EOF: 1097c478bd9Sstevel@tonic-gate if (linelen == 0 || 1107c478bd9Sstevel@tonic-gate buffer[linelen - 1] == '\\') { 1117c478bd9Sstevel@tonic-gate return (-1); 1127c478bd9Sstevel@tonic-gate } else { 1137c478bd9Sstevel@tonic-gate buffer[linelen ] = '\n'; 1147c478bd9Sstevel@tonic-gate buffer[linelen + 1] = '\0'; 1157c478bd9Sstevel@tonic-gate return (linelen); 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate case '\n': 1187c478bd9Sstevel@tonic-gate if (linelen > 0 && 1197c478bd9Sstevel@tonic-gate buffer[linelen - 1] == '\\') { 1207c478bd9Sstevel@tonic-gate --linelen; /* remove the '\\' */ 1217c478bd9Sstevel@tonic-gate } else { 1227c478bd9Sstevel@tonic-gate buffer[linelen ] = '\n'; 1237c478bd9Sstevel@tonic-gate buffer[linelen + 1] = '\0'; 1247c478bd9Sstevel@tonic-gate return (linelen); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate break; 1277c478bd9Sstevel@tonic-gate default: 1287c478bd9Sstevel@tonic-gate buffer[linelen++] = c; 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate /* Buffer overflow -- eat rest of line and loop again */ 1327c478bd9Sstevel@tonic-gate /* ===> Should syslog() */ 1337c478bd9Sstevel@tonic-gate do { 134004388ebScasper c = getc_unlocked(f); 1357c478bd9Sstevel@tonic-gate if (c == EOF) { 1367c478bd9Sstevel@tonic-gate return (-1); 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate } while (c != '\n'); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* 1457c478bd9Sstevel@tonic-gate * Could implement this as an iterator function on top of _nss_user_do_all(), 1467c478bd9Sstevel@tonic-gate * but the shared code is small enough that it'd be pretty silly. 1477c478bd9Sstevel@tonic-gate */ 1487c478bd9Sstevel@tonic-gate nss_status_t 1497c478bd9Sstevel@tonic-gate _nss_user_XY_all(be, args, netdb, filter, check) 1507c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 1517c478bd9Sstevel@tonic-gate nss_XbyY_args_t *args; 1527c478bd9Sstevel@tonic-gate int netdb; /* whether it uses netdb */ 1537c478bd9Sstevel@tonic-gate /* format or not */ 1547c478bd9Sstevel@tonic-gate const char *filter; /* advisory, to speed up */ 1557c478bd9Sstevel@tonic-gate /* string search */ 1567c478bd9Sstevel@tonic-gate user_XY_check_func check; /* NULL means one-shot, for getXXent */ 1577c478bd9Sstevel@tonic-gate { 1587c478bd9Sstevel@tonic-gate nss_status_t res; 1597c478bd9Sstevel@tonic-gate int parsestat; 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate if (be->buf == 0 && 1627c478bd9Sstevel@tonic-gate (be->buf = malloc(be->minbuf)) == 0) { 1637c478bd9Sstevel@tonic-gate return (NSS_UNAVAIL); /* really panic, malloc failed */ 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate if (check != 0 || be->f == 0) { 1677c478bd9Sstevel@tonic-gate if ((res = _nss_user_setent(be, 0)) != NSS_SUCCESS) { 1687c478bd9Sstevel@tonic-gate return (res); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate res = NSS_NOTFOUND; 1737c478bd9Sstevel@tonic-gate 174*cb5caa98Sdjl /*CONSTCOND*/ 1757c478bd9Sstevel@tonic-gate while (1) { 1767c478bd9Sstevel@tonic-gate char *instr = be->buf; 1777c478bd9Sstevel@tonic-gate int linelen; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate if ((linelen = _nss_user_read_line(be->f, instr, 1807c478bd9Sstevel@tonic-gate be->minbuf)) < 0) { 1817c478bd9Sstevel@tonic-gate /* End of file */ 1827c478bd9Sstevel@tonic-gate args->returnval = 0; 1837c478bd9Sstevel@tonic-gate args->erange = 0; 1847c478bd9Sstevel@tonic-gate break; 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate if (filter != 0 && strstr(instr, filter) == 0) { 1877c478bd9Sstevel@tonic-gate /* 1887c478bd9Sstevel@tonic-gate * Optimization: if the entry doesn't contain the 1897c478bd9Sstevel@tonic-gate * filter string then it can't be the entry we want, 1907c478bd9Sstevel@tonic-gate * so don't bother looking more closely at it. 1917c478bd9Sstevel@tonic-gate */ 1927c478bd9Sstevel@tonic-gate continue; 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate if (netdb) { 1957c478bd9Sstevel@tonic-gate char *first; 1967c478bd9Sstevel@tonic-gate char *last; 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate if ((last = strchr(instr, '#')) == 0) { 1997c478bd9Sstevel@tonic-gate last = instr + linelen; 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate *last-- = '\0'; /* Nuke '\n' or #comment */ 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate /* 2047c478bd9Sstevel@tonic-gate * Skip leading whitespace. Normally there isn't 2057c478bd9Sstevel@tonic-gate * any, so it's not worth calling strspn(). 2067c478bd9Sstevel@tonic-gate */ 2077c478bd9Sstevel@tonic-gate for (first = instr; isspace(*first); first++) { 2087c478bd9Sstevel@tonic-gate ; 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate if (*first == '\0') { 2117c478bd9Sstevel@tonic-gate continue; 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate /* 2147c478bd9Sstevel@tonic-gate * Found something non-blank on the line. Skip back 2157c478bd9Sstevel@tonic-gate * over any trailing whitespace; since we know 2167c478bd9Sstevel@tonic-gate * there's non-whitespace earlier in the line, 2177c478bd9Sstevel@tonic-gate * checking for termination is easy. 2187c478bd9Sstevel@tonic-gate */ 2197c478bd9Sstevel@tonic-gate while (isspace(*last)) { 2207c478bd9Sstevel@tonic-gate --last; 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate linelen = last - first + 1; 2247c478bd9Sstevel@tonic-gate if (first != instr) { 2257c478bd9Sstevel@tonic-gate instr = first; 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate args->returnval = 0; 2307c478bd9Sstevel@tonic-gate parsestat = (*args->str2ent)(instr, linelen, args->buf.result, 2317c478bd9Sstevel@tonic-gate args->buf.buffer, args->buf.buflen); 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate if (parsestat == NSS_STR_PARSE_SUCCESS) { 2347c478bd9Sstevel@tonic-gate args->returnval = args->buf.result; 2357c478bd9Sstevel@tonic-gate if (check == 0 || (*check)(args)) { 2367c478bd9Sstevel@tonic-gate res = NSS_SUCCESS; 2377c478bd9Sstevel@tonic-gate break; 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate } else if (parsestat == NSS_STR_PARSE_ERANGE) { 2407c478bd9Sstevel@tonic-gate args->erange = 1; /* should we just skip this */ 2417c478bd9Sstevel@tonic-gate /* one long line ?? */ 2427c478bd9Sstevel@tonic-gate } /* else if (parsestat == NSS_STR_PARSE_PARSE) don't care ! */ 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate /* 2467c478bd9Sstevel@tonic-gate * stayopen is set to 0 by default in order to close the opened 2477c478bd9Sstevel@tonic-gate * file. Some applications may break if it is set to 1. 2487c478bd9Sstevel@tonic-gate */ 2497c478bd9Sstevel@tonic-gate if (check != 0 && !args->stayopen) { 2507c478bd9Sstevel@tonic-gate (void) _nss_user_endent(be, 0); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate return (res); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate 257*cb5caa98Sdjl /*ARGSUSED*/ 2587c478bd9Sstevel@tonic-gate nss_status_t 2597c478bd9Sstevel@tonic-gate _nss_user_destr(be, dummy) 2607c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 2617c478bd9Sstevel@tonic-gate void *dummy; 2627c478bd9Sstevel@tonic-gate { 2637c478bd9Sstevel@tonic-gate if (be != 0) { 2647c478bd9Sstevel@tonic-gate if (be->f != 0) { 265*cb5caa98Sdjl (void) _nss_user_endent(be, 0); 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate free((char *)be->filename); 2687c478bd9Sstevel@tonic-gate free(be); 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate return (NSS_SUCCESS); /* In case anyone is dumb enough to check */ 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate nss_backend_t * 2747c478bd9Sstevel@tonic-gate _nss_user_constr(ops, n_ops, filename, min_bufsize) 2757c478bd9Sstevel@tonic-gate user_backend_op_t ops[]; 2767c478bd9Sstevel@tonic-gate int n_ops; 2777c478bd9Sstevel@tonic-gate const char *filename; 2787c478bd9Sstevel@tonic-gate int min_bufsize; 2797c478bd9Sstevel@tonic-gate { 2807c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate if ((be = (user_backend_ptr_t)malloc(sizeof (*be))) == 0) { 2837c478bd9Sstevel@tonic-gate return (0); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate be->ops = ops; 2867c478bd9Sstevel@tonic-gate be->n_ops = n_ops; 2877c478bd9Sstevel@tonic-gate if ((be->filename = strdup(filename)) == NULL) { 2887c478bd9Sstevel@tonic-gate free(be); 2897c478bd9Sstevel@tonic-gate return (NULL); 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate be->minbuf = min_bufsize; 2927c478bd9Sstevel@tonic-gate be->f = 0; 2937c478bd9Sstevel@tonic-gate be->buf = 0; 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate return ((nss_backend_t *)be); 2967c478bd9Sstevel@tonic-gate } 297