Home | History | Annotate | Line # | Download | only in irs
      1      1.1  christos /*	$NetBSD: lcl_pw.c,v 1.1.1.2 2012/09/09 16:07:58 christos Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*
      4      1.1  christos  * Copyright (c) 1989, 1993, 1995
      5      1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6      1.1  christos  *
      7      1.1  christos  * Redistribution and use in source and binary forms, with or without
      8      1.1  christos  * modification, are permitted provided that the following conditions
      9      1.1  christos  * are met:
     10      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  christos  *    documentation and/or other materials provided with the distribution.
     15      1.1  christos  * 3. All advertising materials mentioning features or use of this software
     16      1.1  christos  *    must display the following acknowledgement:
     17      1.1  christos  *	This product includes software developed by the University of
     18      1.1  christos  *	California, Berkeley and its contributors.
     19      1.1  christos  * 4. Neither the name of the University nor the names of its contributors
     20      1.1  christos  *    may be used to endorse or promote products derived from this software
     21      1.1  christos  *    without specific prior written permission.
     22      1.1  christos  *
     23      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24      1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25      1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26      1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27      1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28      1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29      1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30      1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31      1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32      1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33      1.1  christos  * SUCH DAMAGE.
     34      1.1  christos  */
     35      1.1  christos 
     36      1.1  christos /*
     37      1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
     38      1.1  christos  * Portions Copyright (c) 1996,1999 by Internet Software Consortium.
     39      1.1  christos  *
     40      1.1  christos  * Permission to use, copy, modify, and distribute this software for any
     41      1.1  christos  * purpose with or without fee is hereby granted, provided that the above
     42      1.1  christos  * copyright notice and this permission notice appear in all copies.
     43      1.1  christos  *
     44      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     45      1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     46      1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     47      1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     48      1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     49      1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     50      1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     51      1.1  christos  */
     52      1.1  christos 
     53      1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     54  1.1.1.2  christos static const char rcsid[] = "Id: lcl_pw.c,v 1.3 2005/04/27 04:56:31 sra Exp ";
     55      1.1  christos #endif /* LIBC_SCCS and not lint */
     56      1.1  christos 
     57      1.1  christos /* Extern */
     58      1.1  christos 
     59      1.1  christos #include "port_before.h"
     60      1.1  christos 
     61      1.1  christos #ifndef WANT_IRS_PW
     62      1.1  christos static int __bind_irs_pw_unneeded;
     63      1.1  christos #else
     64      1.1  christos 
     65      1.1  christos #include <sys/param.h>
     66      1.1  christos #include <sys/types.h>
     67      1.1  christos #include <netinet/in.h>
     68      1.1  christos #include <arpa/nameser.h>
     69      1.1  christos #include <resolv.h>
     70      1.1  christos 
     71      1.1  christos #include <db.h>
     72      1.1  christos #include <errno.h>
     73      1.1  christos #include <fcntl.h>
     74      1.1  christos #include <limits.h>
     75      1.1  christos #include <pwd.h>
     76      1.1  christos #include <stdlib.h>
     77      1.1  christos #include <string.h>
     78      1.1  christos #include <syslog.h>
     79      1.1  christos #include <utmp.h>
     80      1.1  christos #include <unistd.h>
     81      1.1  christos 
     82      1.1  christos #include <isc/memcluster.h>
     83      1.1  christos #include <irs.h>
     84      1.1  christos 
     85      1.1  christos #include "port_after.h"
     86      1.1  christos 
     87      1.1  christos #include "irs_p.h"
     88      1.1  christos #include "lcl_p.h"
     89      1.1  christos 
     90      1.1  christos /*! \file
     91      1.1  christos  * \brief
     92      1.1  christos  * The lookup techniques and data extraction code here must be kept
     93      1.1  christos  * in sync with that in `pwd_mkdb'.
     94      1.1  christos  */
     95      1.1  christos 
     96      1.1  christos 
     97      1.1  christos /* Types */
     98      1.1  christos 
     99      1.1  christos struct  pvt {
    100      1.1  christos 	struct passwd	passwd;		/*%< password structure */
    101      1.1  christos 	DB 		*pw_db;		/*%< password database */
    102      1.1  christos 	int		pw_keynum;	/*%< key counter */
    103      1.1  christos 	int		warned;
    104      1.1  christos 	u_int		max;
    105      1.1  christos 	char *		line;
    106      1.1  christos };
    107      1.1  christos 
    108      1.1  christos /* Forward */
    109      1.1  christos 
    110      1.1  christos static void			pw_close(struct irs_pw *);
    111      1.1  christos static struct passwd *		pw_next(struct irs_pw *);
    112      1.1  christos static struct passwd *		pw_byname(struct irs_pw *, const char *);
    113      1.1  christos static struct passwd *		pw_byuid(struct irs_pw *, uid_t);
    114      1.1  christos static void			pw_rewind(struct irs_pw *);
    115      1.1  christos static void			pw_minimize(struct irs_pw *);
    116      1.1  christos 
    117      1.1  christos static int			initdb(struct pvt *);
    118      1.1  christos static int			hashpw(struct irs_pw *, DBT *);
    119      1.1  christos 
    120      1.1  christos /* Public */
    121      1.1  christos struct irs_pw *
    122      1.1  christos irs_lcl_pw(struct irs_acc *this) {
    123      1.1  christos 	struct irs_pw *pw;
    124      1.1  christos 	struct pvt *pvt;
    125      1.1  christos 
    126      1.1  christos 	UNUSED(this);
    127      1.1  christos 
    128      1.1  christos         if (!(pw = memget(sizeof *pw))) {
    129      1.1  christos 		errno = ENOMEM;
    130      1.1  christos 		return (NULL);
    131      1.1  christos 	}
    132      1.1  christos 	memset(pw, 0x5e, sizeof *pw);
    133      1.1  christos 	if (!(pvt = memget(sizeof *pvt))) {
    134      1.1  christos 		free(pw);
    135      1.1  christos 		errno = ENOMEM;
    136      1.1  christos 		return (NULL);
    137      1.1  christos 	}
    138      1.1  christos 	memset(pvt, 0, sizeof *pvt);
    139      1.1  christos 	pw->private = pvt;
    140      1.1  christos 	pw->close = pw_close;
    141      1.1  christos 	pw->next = pw_next;
    142      1.1  christos 	pw->byname = pw_byname;
    143      1.1  christos 	pw->byuid = pw_byuid;
    144      1.1  christos 	pw->rewind = pw_rewind;
    145      1.1  christos 	pw->minimize = pw_minimize;
    146      1.1  christos 	pw->res_get = NULL;
    147      1.1  christos 	pw->res_set = NULL;
    148      1.1  christos 	return (pw);
    149      1.1  christos }
    150      1.1  christos 
    151      1.1  christos /* Methods */
    152      1.1  christos 
    153      1.1  christos static void
    154      1.1  christos pw_close(struct irs_pw *this) {
    155      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    156      1.1  christos 
    157      1.1  christos 	if (pvt->pw_db) {
    158      1.1  christos 		(void)(pvt->pw_db->close)(pvt->pw_db);
    159      1.1  christos 		pvt->pw_db = NULL;
    160      1.1  christos 	}
    161      1.1  christos 	if (pvt->line)
    162      1.1  christos 		memput(pvt->line, pvt->max);
    163      1.1  christos 	memput(pvt, sizeof *pvt);
    164      1.1  christos 	memput(this, sizeof *this);
    165      1.1  christos }
    166      1.1  christos 
    167      1.1  christos static struct passwd *
    168      1.1  christos pw_next(struct irs_pw *this) {
    169      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    170      1.1  christos 
    171      1.1  christos 	DBT key;
    172      1.1  christos 	char bf[sizeof(pvt->pw_keynum) + 1];
    173      1.1  christos 
    174      1.1  christos 	if (!initdb(pvt))
    175      1.1  christos 		return (NULL);
    176      1.1  christos 
    177      1.1  christos 	++pvt->pw_keynum;
    178      1.1  christos 	bf[0] = _PW_KEYBYNUM;
    179      1.1  christos 	memcpy(bf + 1, (char *)&pvt->pw_keynum, sizeof(pvt->pw_keynum));
    180      1.1  christos 	key.data = (u_char *)bf;
    181      1.1  christos 	key.size = sizeof(pvt->pw_keynum) + 1;
    182      1.1  christos 	return (hashpw(this, &key) ? &pvt->passwd : NULL);
    183      1.1  christos }
    184      1.1  christos 
    185      1.1  christos static struct passwd *
    186      1.1  christos pw_byname(struct irs_pw *this, const char *name) {
    187      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    188      1.1  christos 	DBT key;
    189      1.1  christos 	int len, rval;
    190      1.1  christos 	char bf[UT_NAMESIZE + 1];
    191      1.1  christos 
    192      1.1  christos 	if (!initdb(pvt))
    193      1.1  christos 		return (NULL);
    194      1.1  christos 
    195      1.1  christos 	bf[0] = _PW_KEYBYNAME;
    196      1.1  christos 	len = strlen(name);
    197      1.1  christos 	memcpy(bf + 1, name, MIN(len, UT_NAMESIZE));
    198      1.1  christos 	key.data = (u_char *)bf;
    199      1.1  christos 	key.size = len + 1;
    200      1.1  christos 	rval = hashpw(this, &key);
    201      1.1  christos 
    202      1.1  christos 	return (rval ? &pvt->passwd : NULL);
    203      1.1  christos }
    204      1.1  christos 
    205      1.1  christos 
    206      1.1  christos static struct passwd *
    207      1.1  christos pw_byuid(struct irs_pw *this, uid_t uid) {
    208      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    209      1.1  christos 	DBT key;
    210      1.1  christos 	int keyuid, rval;
    211      1.1  christos 	char bf[sizeof(keyuid) + 1];
    212      1.1  christos 
    213      1.1  christos 	if (!initdb(pvt))
    214      1.1  christos 		return (NULL);
    215      1.1  christos 
    216      1.1  christos 	bf[0] = _PW_KEYBYUID;
    217      1.1  christos 	keyuid = uid;
    218      1.1  christos 	memcpy(bf + 1, &keyuid, sizeof(keyuid));
    219      1.1  christos 	key.data = (u_char *)bf;
    220      1.1  christos 	key.size = sizeof(keyuid) + 1;
    221      1.1  christos 	rval = hashpw(this, &key);
    222      1.1  christos 
    223      1.1  christos 	return (rval ? &pvt->passwd : NULL);
    224      1.1  christos }
    225      1.1  christos 
    226      1.1  christos static void
    227      1.1  christos pw_rewind(struct irs_pw *this) {
    228      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    229      1.1  christos 
    230      1.1  christos 	pvt->pw_keynum = 0;
    231      1.1  christos }
    232      1.1  christos 
    233      1.1  christos static void
    234      1.1  christos pw_minimize(struct irs_pw *this) {
    235      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    236      1.1  christos 
    237      1.1  christos 	if (pvt->pw_db != NULL) {
    238      1.1  christos 		(void) (*pvt->pw_db->close)(pvt->pw_db);
    239      1.1  christos 		pvt->pw_db = NULL;
    240      1.1  christos 	}
    241      1.1  christos }
    242      1.1  christos 
    243      1.1  christos /* Private. */
    244      1.1  christos 
    245      1.1  christos static int
    246      1.1  christos initdb(struct pvt *pvt) {
    247      1.1  christos 	const char *p;
    248      1.1  christos 
    249      1.1  christos 	if (pvt->pw_db) {
    250      1.1  christos 		if (lseek((*pvt->pw_db->fd)(pvt->pw_db), 0L, SEEK_CUR) >= 0L)
    251      1.1  christos 			return (1);
    252      1.1  christos 		else
    253      1.1  christos 			(void) (*pvt->pw_db->close)(pvt->pw_db);
    254      1.1  christos 	}
    255      1.1  christos 	pvt->pw_db = dbopen((p = _PATH_SMP_DB), O_RDONLY, 0, DB_HASH, NULL);
    256      1.1  christos 	if (!pvt->pw_db)
    257      1.1  christos 		pvt->pw_db = dbopen((p =_PATH_MP_DB), O_RDONLY,
    258      1.1  christos 				    0, DB_HASH, NULL);
    259      1.1  christos 	if (pvt->pw_db)
    260      1.1  christos 		return (1);
    261      1.1  christos 	if (!pvt->warned) {
    262      1.1  christos 		syslog(LOG_ERR, "%s: %m", p);
    263      1.1  christos 		pvt->warned++;
    264      1.1  christos 	}
    265      1.1  christos 	return (0);
    266      1.1  christos }
    267      1.1  christos 
    268      1.1  christos static int
    269      1.1  christos hashpw(struct irs_pw *this, DBT *key) {
    270      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    271      1.1  christos 	char *p, *t, *l;
    272      1.1  christos 	DBT data;
    273      1.1  christos 
    274      1.1  christos 	if ((pvt->pw_db->get)(pvt->pw_db, key, &data, 0))
    275      1.1  christos 		return (0);
    276      1.1  christos 	p = (char *)data.data;
    277      1.1  christos 	if (data.size > pvt->max) {
    278      1.1  christos 		size_t newlen = pvt->max + 1024;
    279      1.1  christos 		char *p = memget(newlen);
    280      1.1  christos 		if (p == NULL) {
    281      1.1  christos 			return (0);
    282      1.1  christos 		}
    283      1.1  christos 		if (pvt->line != NULL) {
    284      1.1  christos 			memcpy(p, pvt->line, pvt->max);
    285      1.1  christos 			memput(pvt->line, pvt->max);
    286      1.1  christos 		}
    287      1.1  christos 		pvt->max = newlen;
    288      1.1  christos 		pvt->line = p;
    289      1.1  christos 	}
    290      1.1  christos 
    291      1.1  christos 	/* THIS CODE MUST MATCH THAT IN pwd_mkdb. */
    292      1.1  christos 	t = pvt->line;
    293      1.1  christos 	l = pvt->line + pvt->max;
    294      1.1  christos #define EXPAND(e) if ((e = t) == NULL) return (0); else \
    295      1.1  christos 		  do if (t >= l) return (0); while ((*t++ = *p++) != '\0')
    296      1.1  christos #define SCALAR(v) if (t + sizeof v >= l) return (0); else \
    297      1.1  christos 		  (memmove(&(v), p, sizeof v), p += sizeof v)
    298      1.1  christos 	EXPAND(pvt->passwd.pw_name);
    299      1.1  christos 	EXPAND(pvt->passwd.pw_passwd);
    300      1.1  christos 	SCALAR(pvt->passwd.pw_uid);
    301      1.1  christos 	SCALAR(pvt->passwd.pw_gid);
    302      1.1  christos 	SCALAR(pvt->passwd.pw_change);
    303      1.1  christos 	EXPAND(pvt->passwd.pw_class);
    304      1.1  christos 	EXPAND(pvt->passwd.pw_gecos);
    305      1.1  christos 	EXPAND(pvt->passwd.pw_dir);
    306      1.1  christos 	EXPAND(pvt->passwd.pw_shell);
    307      1.1  christos 	SCALAR(pvt->passwd.pw_expire);
    308      1.1  christos 	return (1);
    309      1.1  christos }
    310      1.1  christos 
    311      1.1  christos #endif /* WANT_IRS_PW */
    312