[BACK]Return to getusershell.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / gen

File: [cvs.NetBSD.org] / src / lib / libc / gen / getusershell.c (download)

Revision 1.29, Tue Mar 13 21:13:36 2012 UTC (12 years ago) by christos
Branch: MAIN
CVS Tags: yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20200421, phil-wifi-20200411, phil-wifi-20200406, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-3-RELEASE, netbsd-9-2-RELEASE, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-10-base, netbsd-10-0-RC6, netbsd-10-0-RC5, netbsd-10-0-RC4, netbsd-10-0-RC3, netbsd-10-0-RC2, netbsd-10-0-RC1, netbsd-10, matt-nb8-mediatek-base, matt-nb8-mediatek, localcount-20160914, is-mlppp-base, is-mlppp, cjep_sun2x-base1, cjep_sun2x-base, cjep_sun2x, cjep_staticlib_x-base1, cjep_staticlib_x-base, cjep_staticlib_x, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, agc-symver-base, agc-symver, HEAD
Changes since 1.28: +4 -3 lines

PR/45989: Martin Husemann: lint invocation does include -w only on i386

- turn lint -w for all the platforms after fixing the lint warnings.
- add _DIAGASSERTS() for casts that would assign values to types that
  would not fit.
- change types, add casts
- change into ansii prototypes
- turn on _DIAGNOSTIC for libc (during current, to be eliminated for release
  builds)

approved by core@

/*	$NetBSD: getusershell.c,v 1.29 2012/03/13 21:13:36 christos Exp $	*/

/*-
 * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Luke Mewburn.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright (c) 1985, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)getusershell.c	8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: getusershell.c,v 1.29 2012/03/13 21:13:36 christos Exp $");
#endif
#endif /* LIBC_SCCS and not lint */

#include "namespace.h"
#include "reentrant.h"

#include <sys/param.h>
#include <sys/file.h>

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <nsswitch.h>
#include <paths.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef HESIOD
#include <hesiod.h>
#endif
#ifdef YP
#include <rpc/rpc.h>
#include <rpcsvc/ypclnt.h>
#include <rpcsvc/yp_prot.h>
#endif

#ifdef __weak_alias
__weak_alias(endusershell,_endusershell)
__weak_alias(getusershell,_getusershell)
__weak_alias(setusershell,_setusershell)
#endif

/*
 * Local shells should NOT be added here.
 * They should be added in /etc/shells.
 */
static const char *const okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL };

#ifdef _REENTRANT
static mutex_t __shellmutex = MUTEX_INITIALIZER;
#endif

static char		  curshell[MAXPATHLEN + 2];

static const char *const *curokshell = okshells;
static int		  shellsfound = 0;

		/*
		 *	files methods
		 */

	/* state shared between files methods */
struct files_state {
	FILE	*fp;
};

static struct files_state _files_state;


static int
_files_start(struct files_state *state)
{

	_DIAGASSERT(state != NULL);

	if (state->fp == NULL) {
		state->fp = fopen(_PATH_SHELLS, "re");
		if (state->fp == NULL)
			return NS_UNAVAIL;
	} else {
		rewind(state->fp);
	}
	return NS_SUCCESS;
}

static int
_files_end(struct files_state *state)
{

	_DIAGASSERT(state != NULL);

	if (state->fp) {
		(void) fclose(state->fp);
		state->fp = NULL;
	}
	return NS_SUCCESS;
}

/*ARGSUSED*/
static int
_files_setusershell(void *nsrv, void *nscb, va_list ap)
{

	return _files_start(&_files_state);
}

/*ARGSUSED*/
static int
_files_endusershell(void *nsrv, void *nscb, va_list ap)
{

	return _files_end(&_files_state);
}

/*ARGSUSED*/
static int
_files_getusershell(void *nsrv, void *nscb, va_list ap)
{
	char	**retval = va_arg(ap, char **);

	char	*sp, *cp;
	int	 rv;

	_DIAGASSERT(retval != NULL);

	*retval = NULL;
	if (_files_state.fp == NULL) {	/* only start if file not open yet */
		rv = _files_start(&_files_state);
		if (rv != NS_SUCCESS)
			return rv;
	}

	while (fgets(curshell, (int)sizeof(curshell) - 1, _files_state.fp)
	    != NULL) {
		sp = cp = curshell;
		while (*cp != '#' && *cp != '/' && *cp != '\0')
			cp++;
		if (*cp == '#' || *cp == '\0')
			continue;
		sp = cp;
		while (!isspace((unsigned char) *cp) && *cp != '#'
		    && *cp != '\0')
			cp++;
		*cp++ = '\0';
		*retval = sp;
		return NS_SUCCESS;
	}

	return NS_NOTFOUND;
}


#ifdef HESIOD
		/*
		 *	dns methods
		 */

	/* state shared between dns methods */
struct dns_state {
	void	*context;		/* Hesiod context */
	int	 num;			/* shell index, -1 if no more */
};

static struct dns_state		_dns_state;

static int
_dns_start(struct dns_state *state)
{

	_DIAGASSERT(state != NULL);

	state->num = 0;
	if (state->context == NULL) {			/* setup Hesiod */
		if (hesiod_init(&state->context) == -1)
			return NS_UNAVAIL;
	}

	return NS_SUCCESS;
}

static int
_dns_end(struct dns_state *state)
{

	_DIAGASSERT(state != NULL);

	state->num = 0;
	if (state->context) {
		hesiod_end(state->context);
		state->context = NULL;
	}
	return NS_SUCCESS;
}

/*ARGSUSED*/
static int
_dns_setusershell(void *nsrv, void *nscb, va_list ap)
{

	return _dns_start(&_dns_state);
}

/*ARGSUSED*/
static int
_dns_endusershell(void *nsrv, void *nscb, va_list ap)
{

	return _dns_end(&_dns_state);
}

/*ARGSUSED*/
static int
_dns_getusershell(void *nsrv, void *nscb, va_list ap)
{
	char	**retval = va_arg(ap, char **);

	char	  shellname[] = "shells-NNNNNNNNNN";
	char	**hp, *ep;
	int	  rv;

	_DIAGASSERT(retval != NULL);

	*retval = NULL;

	if (_dns_state.num == -1)			/* exhausted search */
		return NS_NOTFOUND;

	if (_dns_state.context == NULL) {
			/* only start if Hesiod not setup */
		rv = _dns_start(&_dns_state);
		if (rv != NS_SUCCESS)
			return rv;
	}

	hp = NULL;
	rv = NS_NOTFOUND;

							/* find shells-NNN */
	snprintf(shellname, sizeof(shellname), "shells-%d", _dns_state.num);
	_dns_state.num++;

	hp = hesiod_resolve(_dns_state.context, shellname, "shells");
	if (hp == NULL) {
		if (errno == ENOENT)
			rv = NS_NOTFOUND;
		else
			rv = NS_UNAVAIL;
	} else {
		if ((ep = strchr(hp[0], '\n')) != NULL)
			*ep = '\0';			/* clear trailing \n */
						/* only use first result */
		strlcpy(curshell, hp[0], sizeof(curshell));
		*retval = curshell;
		rv = NS_SUCCESS;
	}

	if (hp)
		hesiod_free_list(_dns_state.context, hp);
	if (rv != NS_SUCCESS)
		_dns_state.num = -1;		/* any failure halts search */
	return rv;
}

#endif /* HESIOD */


#ifdef YP
		/*
		 *	nis methods
		 */
	/* state shared between nis methods */
struct nis_state {
	char		*domain;	/* NIS domain */
	int		 done;		/* non-zero if search exhausted */
	char		*current;	/* current first/next match */
	int		 currentlen;	/* length of _nis_current */
};

static struct nis_state		_nis_state;

static int
_nis_start(struct nis_state *state)
{

	_DIAGASSERT(state != NULL);

	state->done = 0;
	if (state->current) {
		free(state->current);
		state->current = NULL;
	}
	if (state->domain == NULL) {			/* setup NIS */
		switch (yp_get_default_domain(&state->domain)) {
		case 0:
			break;
		case YPERR_RESRC:
			return NS_TRYAGAIN;
		default:
			return NS_UNAVAIL;
		}
	}
	return NS_SUCCESS;
}

static int
_nis_end(struct nis_state *state)
{

	_DIAGASSERT(state != NULL);

	if (state->domain)
		state->domain = NULL;
	state->done = 0;
	if (state->current)
		free(state->current);
	state->current = NULL;
	return NS_SUCCESS;
}

/*ARGSUSED*/
static int
_nis_setusershell(void *nsrv, void *nscb, va_list ap)
{

	return _nis_start(&_nis_state);
}

/*ARGSUSED*/
static int
_nis_endusershell(void *nsrv, void *nscb, va_list ap)
{

	return _nis_end(&_nis_state);
}

/*ARGSUSED*/
static int
_nis_getusershell(void *nsrv, void *nscb, va_list ap)
{
	char	**retval = va_arg(ap, char **);

	char	*key, *data;
	int	keylen, datalen, rv, nisr;

	_DIAGASSERT(retval != NULL);

	*retval = NULL;

	if (_nis_state.done)				/* exhausted search */
		return NS_NOTFOUND;
	if (_nis_state.domain == NULL) {
					/* only start if NIS not setup */
		rv = _nis_start(&_nis_state);
		if (rv != NS_SUCCESS)
			return rv;
	}

	key = NULL;
	data = NULL;
	rv = NS_NOTFOUND;

	if (_nis_state.current) {			/* already searching */
		nisr = yp_next(_nis_state.domain, "shells",
		    _nis_state.current, _nis_state.currentlen,
		    &key, &keylen, &data, &datalen);
		free(_nis_state.current);
		_nis_state.current = NULL;
		switch (nisr) {
		case 0:
			_nis_state.current = key;
			_nis_state.currentlen = keylen;
			key = NULL;
			break;
		case YPERR_NOMORE:
			rv = NS_NOTFOUND;
			goto nisent_out;
		default:
			rv = NS_UNAVAIL;
			goto nisent_out;
		}
	} else {					/* new search */
		if (yp_first(_nis_state.domain, "shells",
		    &_nis_state.current, &_nis_state.currentlen,
		    &data, &datalen)) {
			rv = NS_UNAVAIL;
			goto nisent_out;
		}
	}

	data[datalen] = '\0';				/* clear trailing \n */
	strlcpy(curshell, data, sizeof(curshell));
	*retval = curshell;
	rv = NS_SUCCESS;

 nisent_out:
	if (key)
		free(key);
	if (data)
		free(data);
	if (rv != NS_SUCCESS)			/* any failure halts search */
		_nis_state.done = 1;
	return rv;
}

#endif /* YP */


		/*
		 *	public functions
		 */

void
endusershell(void)
{
	static const ns_dtab dtab[] = {
		NS_FILES_CB(_files_endusershell, NULL)
		NS_DNS_CB(_dns_endusershell, NULL)
		NS_NIS_CB(_nis_endusershell, NULL)
		NS_NULL_CB
	};

	mutex_lock(&__shellmutex);

	curokshell = okshells;		/* reset okshells fallback state */
	shellsfound = 0;

					/* force all endusershell() methods */
	(void) nsdispatch(NULL, dtab, NSDB_SHELLS, "endusershell",
	    __nsdefaultfiles_forceall);
	mutex_unlock(&__shellmutex);
}

__aconst char *
getusershell(void)
{
	int		 rv;
	__aconst char	*retval;

	static const ns_dtab dtab[] = {
		NS_FILES_CB(_files_getusershell, NULL)
		NS_DNS_CB(_dns_getusershell, NULL)
		NS_NIS_CB(_nis_getusershell, NULL)
		NS_NULL_CB
	};

	mutex_lock(&__shellmutex);

	retval = NULL;
	do {
		rv = nsdispatch(NULL, dtab, NSDB_SHELLS, "getusershell",
		    __nsdefaultsrc, &retval);
				/* loop until failure or non-blank result */
	} while (rv == NS_SUCCESS && retval[0] == '\0');

	if (rv == NS_SUCCESS) {
		shellsfound++;
	} else if (shellsfound == 0) {	/* no shells; fall back to okshells */
		if (curokshell != NULL) {
			retval = __UNCONST(*curokshell);
			curokshell++;
			rv = NS_SUCCESS;
		}
	}

	mutex_unlock(&__shellmutex);
	return (rv == NS_SUCCESS) ? retval : NULL;
}

void
setusershell(void)
{
	static const ns_dtab dtab[] = {
		NS_FILES_CB(_files_setusershell, NULL)
		NS_DNS_CB(_dns_setusershell, NULL)
		NS_NIS_CB(_nis_setusershell, NULL)
		NS_NULL_CB
	};

	mutex_lock(&__shellmutex);

	curokshell = okshells;		/* reset okshells fallback state */
	shellsfound = 0;

					/* force all setusershell() methods */
	(void) nsdispatch(NULL, dtab, NSDB_SHELLS, "setusershell",
	    __nsdefaultfiles_forceall);
	mutex_unlock(&__shellmutex);
}