/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2014 Nexenta Systems, Inc. All rights reserved.
*/
/*
* Common code and structures used by name-service-switch "files" backends.
*/
/*
* An implementation that used mmap() sensibly would be a wonderful thing,
* but this here is just yer standard fgets() thang.
*/
#include "files_common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
/*ARGSUSED*/
void *dummy;
{
if (be->f == 0) {
/* Backend isn't initialized properly? */
return (NSS_UNAVAIL);
}
return (NSS_UNAVAIL);
}
} else {
}
return (NSS_SUCCESS);
}
/*ARGSUSED*/
void *dummy;
{
if (be->f != 0) {
be->f = 0;
}
}
return (NSS_SUCCESS);
}
/*
* This routine reads a line, including the processing of continuation
* characters. It always leaves (or inserts) \n\0 at the end of the line.
* It returns the length of the line read, excluding the \n\0. Who's idea
* was this?
* Returns -1 on EOF.
*
* Note that since each concurrent call to _nss_files_read_line has
* it's own FILE pointer, we can use getc_unlocked w/o difficulties,
* a substantial performance win.
*/
int
FILE *f;
char *buffer;
int buflen;
{
int c;
/*CONSTCOND*/
while (1) {
linelen = 0;
switch (c = getc_unlocked(f)) {
case EOF:
if (linelen == 0 ||
return (-1);
} else {
return (linelen);
}
case '\n':
if (linelen > 0 &&
--linelen; /* remove the '\\' */
} else {
return (linelen);
}
break;
default:
}
}
/* Buffer overflow -- eat rest of line and loop again */
/* ===> Should syslog() */
do {
c = getc_unlocked(f);
if (c == EOF) {
return (-1);
}
} while (c != '\n');
}
/*NOTREACHED*/
}
/*
* used only for getgroupbymem() now.
*/
void *args;
const char *filter;
{
long grlen;
char *buffer;
int buflen;
return (NSS_UNAVAIL);
}
return (res);
}
res = NSS_NOTFOUND;
do {
int linelen;
buflen)) < 0) {
/* End of file */
break;
}
/*
* Optimization: if the entry doesn't contain the
* filter string then it can't be the entry we want,
* so don't bother looking more closely at it.
*/
continue;
}
} while (res == NSS_NOTFOUND);
(void) _nss_files_endent(be, 0);
return (res);
}
/*
* Could implement this as an iterator function on top of _nss_files_do_all(),
* but the shared code is small enough that it'd be pretty silly.
*/
int netdb; /* whether it uses netdb */
/* format or not */
const char *filter; /* advisory, to speed up */
/* string search */
{
char *r;
int parsestat;
int (*func)();
return (NSS_NOTFOUND);
} else if (
}
}
return (NSS_UNAVAIL);
}
return (res);
}
}
res = NSS_NOTFOUND;
/*CONSTCOND*/
while (1) {
int linelen;
/* End of file */
break;
}
/*
* Optimization: if the entry doesn't contain the
* filter string then it can't be the entry we want,
* so don't bother looking more closely at it.
*/
continue;
}
if (netdb) {
char *first;
char *last;
}
/*
* Skip leading whitespace. Normally there isn't
* any, so it's not worth calling strspn().
*/
;
}
if (*first == '\0') {
continue;
}
/*
* Found something non-blank on the line. Skip back
* over any trailing whitespace; since we know
* there's non-whitespace earlier in the line,
* checking for termination is easy.
*/
--last;
}
}
}
continue;
/*
* Special case for passwd and group wherein we
* because files backend does not support
* ephemeral ids.
*/
}
if (parsestat == NSS_STR_PARSE_SUCCESS) {
}
if (parsestat == NSS_STR_PARSE_SUCCESS) {
res = NSS_SUCCESS;
break;
} else if (parsestat == NSS_STR_PARSE_ERANGE) {
break;
} /* else if (parsestat == NSS_STR_PARSE_PARSE) don't care ! */
}
/*
* stayopen is set to 0 by default in order to close the opened
* file. Some applications may break if it is set to 1.
*/
(void) _nss_files_endent(be, 0);
}
return (res);
}
/*
* File hashing support. Critical for sites with large (e.g. 1000+ lines)
* getgr*() routines, but any files backend can use this stuff.
*/
static void
{
}
#ifdef PIC
/*
* It turns out the hashing stuff really needs to be disabled for processes
* other than the nscd; the consumption of swap space and memory is otherwise
* unacceptable when the nscd is killed w/ a large passwd file (4M) active.
* See 4031930 for details.
* So we just use this psuedo function to enable the hashing feature. Since
* this function name is private, we just create a function w/ the name
* __nss_use_files_hash in the nscd itself and everyone else uses the old
* interface.
* We also disable hashing for .a executables to avoid problems with large
* files....
*/
extern void __nss_use_files_hash(void);
#endif /* pic */
/*ARGSUSED*/
{
/* LINTED E_FUNC_VAR_UNUSED */
/* LINTED E_FUNC_VAR_UNUSED */
/* LINTED E_FUNC_VAR_UNUSED */
/* LINTED E_FUNC_VAR_UNUSED */
/* LINTED E_FUNC_VAR_UNUSED */
/* LINTED E_FUNC_VAR_UNUSED */
#ifndef PIC
}
#else
if (__nss_use_files_hash == 0)
/*
* This can happen only in two cases: Either the file is
* completely missing and we were not able to read it yet
* (fh_table is NULL), or there is some brief period when the
* settle down, but eventually give up.
*/
goto unavail;
poll(0, 0, 100);
}
continue;
continue;
1);
1, check);
if (stat != NSS_STR_PARSE_SUCCESS) {
if (stat == NSS_STR_PARSE_ERANGE)
continue;
}
}
return (NSS_SUCCESS);
} else {
}
}
return (NSS_NOTFOUND);
}
goto unavail;
goto unavail;
goto unavail;
goto retry;
}
/*
* If the file changed since we read it, or if it's less than
* 1-2 seconds old, don't trust it; its modification may still
* be in progress. The latter is a heuristic hack to minimize
* directly (as opposed to editing and renaming a temp file).
*
* Note: the cast to u_int is there in case (1) someone rdated
* or (2) this is a diskless client whose time is badly out of sync
* with its server. The 1-2 second age hack doesn't cover these
* cases -- oh well.
*/
poll(0, 0, 1000);
goto retry;
}
line = 1;
if (*cp == '\n')
line++;
if (line % f == 0) {
f = 1;
line++;
}
}
goto unavail;
line = 0;
while (*cp != '\n')
cp++;
continue;
}
*cp++ = '\0';
if (netdb) {
first++;
continue;
--last;
*++last = '\0';
}
}
}
/*
* Populate the hash tables in reverse order so that the hash chains
* end up in forward order. This ensures that hashed lookups find
* things in the same order that a linear search of the file would.
* This is essential in cases where there could be multiple matches.
* For example: until 2.7, root and smtp both had uid 0; but we
* certainly wouldn't want getpwuid(0) to return smtp.
*/
}
}
goto retry;
return (NSS_UNAVAIL);
}
#endif /* PIC */
void *a;
{
}
void *a;
{
}
/*ARGSUSED*/
void *dummy;
{
if (be != 0) {
if (be->f != 0) {
(void) _nss_files_endent(be, 0);
}
}
}
return (NSS_SUCCESS); /* In case anyone is dumb enough to check */
}
int n_ops;
const char *filename;
int min_bufsize;
{
return (0);
}
be->f = 0;
}
return ((nss_backend_t *)be);
}
int
int linelen)
{
keyp++;
linep++;
}
}
/*
* This routine is used to parse lines of the form:
* name number aliases
* It returns 1 if the key in argp matches any one of the
* names in the line, otherwise 0
* Used by rpc, networks, protocols
*/
int
int linelen)
{
/* compare name */
keyp++;
linep++;
}
return (1);
/* skip remainder of the name, if any */
linep++;
/* skip the delimiting spaces */
linep++;
/* compare with the aliases */
/*
* 1st pass: skip number
* Other passes: skip remainder of the alias name, if any
*/
linep++;
/* skip the delimiting spaces */
linep++;
/* compare with the alias name */
keyp++;
linep++;
}
return (1);
}
return (0);
}