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 5*004388ebScasper * Common Development and Distribution License (the "License"). 6*004388ebScasper * 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 /* 22*004388ebScasper * 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 36*004388ebScasper #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 467c478bd9Sstevel@tonic-gate nss_status_t 477c478bd9Sstevel@tonic-gate _nss_user_setent(be, dummy) 487c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 497c478bd9Sstevel@tonic-gate void *dummy; 507c478bd9Sstevel@tonic-gate { 517c478bd9Sstevel@tonic-gate if (be->f == 0) { 527c478bd9Sstevel@tonic-gate if (be->filename == 0) { 537c478bd9Sstevel@tonic-gate /* Backend isn't initialized properly? */ 547c478bd9Sstevel@tonic-gate return (NSS_UNAVAIL); 557c478bd9Sstevel@tonic-gate } 56*004388ebScasper if ((be->f = fopen(be->filename, "rF")) == 0) { 577c478bd9Sstevel@tonic-gate return (NSS_UNAVAIL); 587c478bd9Sstevel@tonic-gate } 597c478bd9Sstevel@tonic-gate } else { 60*004388ebScasper rewind(be->f); 617c478bd9Sstevel@tonic-gate } 627c478bd9Sstevel@tonic-gate return (NSS_SUCCESS); 637c478bd9Sstevel@tonic-gate } 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate nss_status_t 667c478bd9Sstevel@tonic-gate _nss_user_endent(be, dummy) 677c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 687c478bd9Sstevel@tonic-gate void *dummy; 697c478bd9Sstevel@tonic-gate { 707c478bd9Sstevel@tonic-gate if (be->f != 0) { 71*004388ebScasper fclose(be->f); 727c478bd9Sstevel@tonic-gate be->f = 0; 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate if (be->buf != 0) { 757c478bd9Sstevel@tonic-gate free(be->buf); 767c478bd9Sstevel@tonic-gate be->buf = 0; 777c478bd9Sstevel@tonic-gate } 787c478bd9Sstevel@tonic-gate return (NSS_SUCCESS); 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate /* 827c478bd9Sstevel@tonic-gate * This routine reads a line, including the processing of continuation 837c478bd9Sstevel@tonic-gate * characters. It always leaves (or inserts) \n\0 at the end of the line. 847c478bd9Sstevel@tonic-gate * It returns the length of the line read, excluding the \n\0. Who's idea 857c478bd9Sstevel@tonic-gate * was this? 867c478bd9Sstevel@tonic-gate * Returns -1 on EOF. 877c478bd9Sstevel@tonic-gate * 887c478bd9Sstevel@tonic-gate * Note that since each concurrent call to _nss_user_read_line has 897c478bd9Sstevel@tonic-gate * it's own FILE pointer, we can use getc_unlocked w/o difficulties, 907c478bd9Sstevel@tonic-gate * a substantial performance win. 917c478bd9Sstevel@tonic-gate */ 927c478bd9Sstevel@tonic-gate int 937c478bd9Sstevel@tonic-gate _nss_user_read_line(f, buffer, buflen) 94*004388ebScasper FILE *f; 957c478bd9Sstevel@tonic-gate char *buffer; 967c478bd9Sstevel@tonic-gate int buflen; 977c478bd9Sstevel@tonic-gate { 987c478bd9Sstevel@tonic-gate int linelen; /* 1st unused slot in buffer */ 997c478bd9Sstevel@tonic-gate int c; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate while (1) { 1027c478bd9Sstevel@tonic-gate linelen = 0; 1037c478bd9Sstevel@tonic-gate while (linelen < buflen - 1) { /* "- 1" saves room for \n\0 */ 104*004388ebScasper switch (c = getc_unlocked(f)) { 1057c478bd9Sstevel@tonic-gate case EOF: 1067c478bd9Sstevel@tonic-gate if (linelen == 0 || 1077c478bd9Sstevel@tonic-gate buffer[linelen - 1] == '\\') { 1087c478bd9Sstevel@tonic-gate return (-1); 1097c478bd9Sstevel@tonic-gate } else { 1107c478bd9Sstevel@tonic-gate buffer[linelen ] = '\n'; 1117c478bd9Sstevel@tonic-gate buffer[linelen + 1] = '\0'; 1127c478bd9Sstevel@tonic-gate return (linelen); 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate case '\n': 1157c478bd9Sstevel@tonic-gate if (linelen > 0 && 1167c478bd9Sstevel@tonic-gate buffer[linelen - 1] == '\\') { 1177c478bd9Sstevel@tonic-gate --linelen; /* remove the '\\' */ 1187c478bd9Sstevel@tonic-gate } else { 1197c478bd9Sstevel@tonic-gate buffer[linelen ] = '\n'; 1207c478bd9Sstevel@tonic-gate buffer[linelen + 1] = '\0'; 1217c478bd9Sstevel@tonic-gate return (linelen); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate break; 1247c478bd9Sstevel@tonic-gate default: 1257c478bd9Sstevel@tonic-gate buffer[linelen++] = c; 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate /* Buffer overflow -- eat rest of line and loop again */ 1297c478bd9Sstevel@tonic-gate /* ===> Should syslog() */ 1307c478bd9Sstevel@tonic-gate do { 131*004388ebScasper c = getc_unlocked(f); 1327c478bd9Sstevel@tonic-gate if (c == EOF) { 1337c478bd9Sstevel@tonic-gate return (-1); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate } while (c != '\n'); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate * Could implement this as an iterator function on top of _nss_user_do_all(), 1437c478bd9Sstevel@tonic-gate * but the shared code is small enough that it'd be pretty silly. 1447c478bd9Sstevel@tonic-gate */ 1457c478bd9Sstevel@tonic-gate nss_status_t 1467c478bd9Sstevel@tonic-gate _nss_user_XY_all(be, args, netdb, filter, check) 1477c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 1487c478bd9Sstevel@tonic-gate nss_XbyY_args_t *args; 1497c478bd9Sstevel@tonic-gate int netdb; /* whether it uses netdb */ 1507c478bd9Sstevel@tonic-gate /* format or not */ 1517c478bd9Sstevel@tonic-gate const char *filter; /* advisory, to speed up */ 1527c478bd9Sstevel@tonic-gate /* string search */ 1537c478bd9Sstevel@tonic-gate user_XY_check_func check; /* NULL means one-shot, for getXXent */ 1547c478bd9Sstevel@tonic-gate { 1557c478bd9Sstevel@tonic-gate nss_status_t res; 1567c478bd9Sstevel@tonic-gate int parsestat; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate if (be->buf == 0 && 1597c478bd9Sstevel@tonic-gate (be->buf = malloc(be->minbuf)) == 0) { 1607c478bd9Sstevel@tonic-gate return (NSS_UNAVAIL); /* really panic, malloc failed */ 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate if (check != 0 || be->f == 0) { 1647c478bd9Sstevel@tonic-gate if ((res = _nss_user_setent(be, 0)) != NSS_SUCCESS) { 1657c478bd9Sstevel@tonic-gate return (res); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate res = NSS_NOTFOUND; 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate while (1) { 1727c478bd9Sstevel@tonic-gate char *instr = be->buf; 1737c478bd9Sstevel@tonic-gate int linelen; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate if ((linelen = _nss_user_read_line(be->f, instr, 1767c478bd9Sstevel@tonic-gate be->minbuf)) < 0) { 1777c478bd9Sstevel@tonic-gate /* End of file */ 1787c478bd9Sstevel@tonic-gate args->returnval = 0; 1797c478bd9Sstevel@tonic-gate args->erange = 0; 1807c478bd9Sstevel@tonic-gate break; 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate if (filter != 0 && strstr(instr, filter) == 0) { 1837c478bd9Sstevel@tonic-gate /* 1847c478bd9Sstevel@tonic-gate * Optimization: if the entry doesn't contain the 1857c478bd9Sstevel@tonic-gate * filter string then it can't be the entry we want, 1867c478bd9Sstevel@tonic-gate * so don't bother looking more closely at it. 1877c478bd9Sstevel@tonic-gate */ 1887c478bd9Sstevel@tonic-gate continue; 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate if (netdb) { 1917c478bd9Sstevel@tonic-gate char *first; 1927c478bd9Sstevel@tonic-gate char *last; 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate if ((last = strchr(instr, '#')) == 0) { 1957c478bd9Sstevel@tonic-gate last = instr + linelen; 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate *last-- = '\0'; /* Nuke '\n' or #comment */ 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate /* 2007c478bd9Sstevel@tonic-gate * Skip leading whitespace. Normally there isn't 2017c478bd9Sstevel@tonic-gate * any, so it's not worth calling strspn(). 2027c478bd9Sstevel@tonic-gate */ 2037c478bd9Sstevel@tonic-gate for (first = instr; isspace(*first); first++) { 2047c478bd9Sstevel@tonic-gate ; 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate if (*first == '\0') { 2077c478bd9Sstevel@tonic-gate continue; 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate /* 2107c478bd9Sstevel@tonic-gate * Found something non-blank on the line. Skip back 2117c478bd9Sstevel@tonic-gate * over any trailing whitespace; since we know 2127c478bd9Sstevel@tonic-gate * there's non-whitespace earlier in the line, 2137c478bd9Sstevel@tonic-gate * checking for termination is easy. 2147c478bd9Sstevel@tonic-gate */ 2157c478bd9Sstevel@tonic-gate while (isspace(*last)) { 2167c478bd9Sstevel@tonic-gate --last; 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate linelen = last - first + 1; 2207c478bd9Sstevel@tonic-gate if (first != instr) { 2217c478bd9Sstevel@tonic-gate instr = first; 2227c478bd9Sstevel@tonic-gate } 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate args->returnval = 0; 2267c478bd9Sstevel@tonic-gate parsestat = (*args->str2ent)(instr, linelen, args->buf.result, 2277c478bd9Sstevel@tonic-gate args->buf.buffer, args->buf.buflen); 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate if (parsestat == NSS_STR_PARSE_SUCCESS) { 2307c478bd9Sstevel@tonic-gate args->returnval = args->buf.result; 2317c478bd9Sstevel@tonic-gate if (check == 0 || (*check)(args)) { 2327c478bd9Sstevel@tonic-gate res = NSS_SUCCESS; 2337c478bd9Sstevel@tonic-gate break; 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate } else if (parsestat == NSS_STR_PARSE_ERANGE) { 2367c478bd9Sstevel@tonic-gate args->erange = 1; /* should we just skip this */ 2377c478bd9Sstevel@tonic-gate /* one long line ?? */ 2387c478bd9Sstevel@tonic-gate } /* else if (parsestat == NSS_STR_PARSE_PARSE) don't care ! */ 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate /* 2427c478bd9Sstevel@tonic-gate * stayopen is set to 0 by default in order to close the opened 2437c478bd9Sstevel@tonic-gate * file. Some applications may break if it is set to 1. 2447c478bd9Sstevel@tonic-gate */ 2457c478bd9Sstevel@tonic-gate if (check != 0 && !args->stayopen) { 2467c478bd9Sstevel@tonic-gate (void) _nss_user_endent(be, 0); 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate return (res); 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate nss_status_t 2547c478bd9Sstevel@tonic-gate _nss_user_destr(be, dummy) 2557c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 2567c478bd9Sstevel@tonic-gate void *dummy; 2577c478bd9Sstevel@tonic-gate { 2587c478bd9Sstevel@tonic-gate if (be != 0) { 2597c478bd9Sstevel@tonic-gate if (be->f != 0) { 2607c478bd9Sstevel@tonic-gate _nss_user_endent(be, 0); 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate free((char *)be->filename); 2637c478bd9Sstevel@tonic-gate free(be); 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate return (NSS_SUCCESS); /* In case anyone is dumb enough to check */ 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate nss_backend_t * 2697c478bd9Sstevel@tonic-gate _nss_user_constr(ops, n_ops, filename, min_bufsize) 2707c478bd9Sstevel@tonic-gate user_backend_op_t ops[]; 2717c478bd9Sstevel@tonic-gate int n_ops; 2727c478bd9Sstevel@tonic-gate const char *filename; 2737c478bd9Sstevel@tonic-gate int min_bufsize; 2747c478bd9Sstevel@tonic-gate { 2757c478bd9Sstevel@tonic-gate user_backend_ptr_t be; 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate if ((be = (user_backend_ptr_t)malloc(sizeof (*be))) == 0) { 2787c478bd9Sstevel@tonic-gate return (0); 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate be->ops = ops; 2817c478bd9Sstevel@tonic-gate be->n_ops = n_ops; 2827c478bd9Sstevel@tonic-gate if ((be->filename = strdup(filename)) == NULL) { 2837c478bd9Sstevel@tonic-gate free(be); 2847c478bd9Sstevel@tonic-gate return (NULL); 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate be->minbuf = min_bufsize; 2877c478bd9Sstevel@tonic-gate be->f = 0; 2887c478bd9Sstevel@tonic-gate be->buf = 0; 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate return ((nss_backend_t *)be); 2917c478bd9Sstevel@tonic-gate } 292