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