Home | History | Annotate | Line # | Download | only in irs
dns_pw.c revision 1.1.1.1.4.2
      1  1.1.1.1.4.2  riz /*	$NetBSD: dns_pw.c,v 1.1.1.1.4.2 2011/01/06 21:42:16 riz Exp $	*/
      2  1.1.1.1.4.2  riz 
      3  1.1.1.1.4.2  riz /*
      4  1.1.1.1.4.2  riz  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5  1.1.1.1.4.2  riz  * Copyright (c) 1996,1999 by Internet Software Consortium.
      6  1.1.1.1.4.2  riz  *
      7  1.1.1.1.4.2  riz  * Permission to use, copy, modify, and distribute this software for any
      8  1.1.1.1.4.2  riz  * purpose with or without fee is hereby granted, provided that the above
      9  1.1.1.1.4.2  riz  * copyright notice and this permission notice appear in all copies.
     10  1.1.1.1.4.2  riz  *
     11  1.1.1.1.4.2  riz  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12  1.1.1.1.4.2  riz  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  1.1.1.1.4.2  riz  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14  1.1.1.1.4.2  riz  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  1.1.1.1.4.2  riz  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  1.1.1.1.4.2  riz  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  1.1.1.1.4.2  riz  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  1.1.1.1.4.2  riz  */
     19  1.1.1.1.4.2  riz 
     20  1.1.1.1.4.2  riz #if defined(LIBC_SCCS) && !defined(lint)
     21  1.1.1.1.4.2  riz static const char rcsid[] = "Id: dns_pw.c,v 1.3 2005/04/27 04:56:22 sra Exp";
     22  1.1.1.1.4.2  riz #endif
     23  1.1.1.1.4.2  riz 
     24  1.1.1.1.4.2  riz #include "port_before.h"
     25  1.1.1.1.4.2  riz 
     26  1.1.1.1.4.2  riz #ifndef WANT_IRS_PW
     27  1.1.1.1.4.2  riz static int __bind_irs_pw_unneeded;
     28  1.1.1.1.4.2  riz #else
     29  1.1.1.1.4.2  riz 
     30  1.1.1.1.4.2  riz #include <stdio.h>
     31  1.1.1.1.4.2  riz #include <stdlib.h>
     32  1.1.1.1.4.2  riz #include <errno.h>
     33  1.1.1.1.4.2  riz #include <string.h>
     34  1.1.1.1.4.2  riz 
     35  1.1.1.1.4.2  riz #include <sys/types.h>
     36  1.1.1.1.4.2  riz #include <netinet/in.h>
     37  1.1.1.1.4.2  riz #include <arpa/nameser.h>
     38  1.1.1.1.4.2  riz #include <resolv.h>
     39  1.1.1.1.4.2  riz 
     40  1.1.1.1.4.2  riz #include <isc/memcluster.h>
     41  1.1.1.1.4.2  riz 
     42  1.1.1.1.4.2  riz #include <irs.h>
     43  1.1.1.1.4.2  riz 
     44  1.1.1.1.4.2  riz #include "port_after.h"
     45  1.1.1.1.4.2  riz 
     46  1.1.1.1.4.2  riz #include "irs_p.h"
     47  1.1.1.1.4.2  riz #include "hesiod.h"
     48  1.1.1.1.4.2  riz #include "dns_p.h"
     49  1.1.1.1.4.2  riz 
     50  1.1.1.1.4.2  riz /* Types. */
     51  1.1.1.1.4.2  riz 
     52  1.1.1.1.4.2  riz struct pvt {
     53  1.1.1.1.4.2  riz 	struct dns_p *	dns;
     54  1.1.1.1.4.2  riz 	struct passwd	passwd;
     55  1.1.1.1.4.2  riz 	char *		pwbuf;
     56  1.1.1.1.4.2  riz };
     57  1.1.1.1.4.2  riz 
     58  1.1.1.1.4.2  riz /* Forward. */
     59  1.1.1.1.4.2  riz 
     60  1.1.1.1.4.2  riz static void 			pw_close(struct irs_pw *);
     61  1.1.1.1.4.2  riz static struct passwd *		pw_byname(struct irs_pw *, const char *);
     62  1.1.1.1.4.2  riz static struct passwd *		pw_byuid(struct irs_pw *, uid_t);
     63  1.1.1.1.4.2  riz static struct passwd *		pw_next(struct irs_pw *);
     64  1.1.1.1.4.2  riz static void 			pw_rewind(struct irs_pw *);
     65  1.1.1.1.4.2  riz static void			pw_minimize(struct irs_pw *);
     66  1.1.1.1.4.2  riz static struct __res_state *	pw_res_get(struct irs_pw *);
     67  1.1.1.1.4.2  riz static void			pw_res_set(struct irs_pw *,
     68  1.1.1.1.4.2  riz 					   struct __res_state *,
     69  1.1.1.1.4.2  riz 					   void (*)(void *));
     70  1.1.1.1.4.2  riz 
     71  1.1.1.1.4.2  riz static struct passwd *		getpwcommon(struct irs_pw *, const char *,
     72  1.1.1.1.4.2  riz 					    const char *);
     73  1.1.1.1.4.2  riz 
     74  1.1.1.1.4.2  riz /* Public. */
     75  1.1.1.1.4.2  riz 
     76  1.1.1.1.4.2  riz struct irs_pw *
     77  1.1.1.1.4.2  riz irs_dns_pw(struct irs_acc *this) {
     78  1.1.1.1.4.2  riz 	struct dns_p *dns = (struct dns_p *)this->private;
     79  1.1.1.1.4.2  riz 	struct irs_pw *pw;
     80  1.1.1.1.4.2  riz 	struct pvt *pvt;
     81  1.1.1.1.4.2  riz 
     82  1.1.1.1.4.2  riz 	if (!dns || !dns->hes_ctx) {
     83  1.1.1.1.4.2  riz 		errno = ENODEV;
     84  1.1.1.1.4.2  riz 		return (NULL);
     85  1.1.1.1.4.2  riz 	}
     86  1.1.1.1.4.2  riz 	if (!(pvt = memget(sizeof *pvt))) {
     87  1.1.1.1.4.2  riz 		errno = ENOMEM;
     88  1.1.1.1.4.2  riz 		return (NULL);
     89  1.1.1.1.4.2  riz 	}
     90  1.1.1.1.4.2  riz 	memset(pvt, 0, sizeof *pvt);
     91  1.1.1.1.4.2  riz 	pvt->dns = dns;
     92  1.1.1.1.4.2  riz 	if (!(pw = memget(sizeof *pw))) {
     93  1.1.1.1.4.2  riz 		memput(pvt, sizeof *pvt);
     94  1.1.1.1.4.2  riz 		errno = ENOMEM;
     95  1.1.1.1.4.2  riz 		return (NULL);
     96  1.1.1.1.4.2  riz 	}
     97  1.1.1.1.4.2  riz 	memset(pw, 0x5e, sizeof *pw);
     98  1.1.1.1.4.2  riz 	pw->private = pvt;
     99  1.1.1.1.4.2  riz 	pw->close = pw_close;
    100  1.1.1.1.4.2  riz 	pw->byname = pw_byname;
    101  1.1.1.1.4.2  riz 	pw->byuid = pw_byuid;
    102  1.1.1.1.4.2  riz 	pw->next = pw_next;
    103  1.1.1.1.4.2  riz 	pw->rewind = pw_rewind;
    104  1.1.1.1.4.2  riz 	pw->minimize = pw_minimize;
    105  1.1.1.1.4.2  riz 	pw->res_get = pw_res_get;
    106  1.1.1.1.4.2  riz 	pw->res_set = pw_res_set;
    107  1.1.1.1.4.2  riz 	return (pw);
    108  1.1.1.1.4.2  riz }
    109  1.1.1.1.4.2  riz 
    110  1.1.1.1.4.2  riz /* Methods. */
    111  1.1.1.1.4.2  riz 
    112  1.1.1.1.4.2  riz static void
    113  1.1.1.1.4.2  riz pw_close(struct irs_pw *this) {
    114  1.1.1.1.4.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    115  1.1.1.1.4.2  riz 
    116  1.1.1.1.4.2  riz 	if (pvt->pwbuf)
    117  1.1.1.1.4.2  riz 		free(pvt->pwbuf);
    118  1.1.1.1.4.2  riz 
    119  1.1.1.1.4.2  riz 	memput(pvt, sizeof *pvt);
    120  1.1.1.1.4.2  riz 	memput(this, sizeof *this);
    121  1.1.1.1.4.2  riz }
    122  1.1.1.1.4.2  riz 
    123  1.1.1.1.4.2  riz static struct passwd *
    124  1.1.1.1.4.2  riz pw_byname(struct irs_pw *this, const char *nam) {
    125  1.1.1.1.4.2  riz 	return (getpwcommon(this, nam, "passwd"));
    126  1.1.1.1.4.2  riz }
    127  1.1.1.1.4.2  riz 
    128  1.1.1.1.4.2  riz static struct passwd *
    129  1.1.1.1.4.2  riz pw_byuid(struct irs_pw *this, uid_t uid) {
    130  1.1.1.1.4.2  riz 	char uidstr[16];
    131  1.1.1.1.4.2  riz 
    132  1.1.1.1.4.2  riz 	sprintf(uidstr, "%lu", (u_long)uid);
    133  1.1.1.1.4.2  riz 	return (getpwcommon(this, uidstr, "uid"));
    134  1.1.1.1.4.2  riz }
    135  1.1.1.1.4.2  riz 
    136  1.1.1.1.4.2  riz static struct passwd *
    137  1.1.1.1.4.2  riz pw_next(struct irs_pw *this) {
    138  1.1.1.1.4.2  riz 	UNUSED(this);
    139  1.1.1.1.4.2  riz 	errno = ENODEV;
    140  1.1.1.1.4.2  riz 	return (NULL);
    141  1.1.1.1.4.2  riz }
    142  1.1.1.1.4.2  riz 
    143  1.1.1.1.4.2  riz static void
    144  1.1.1.1.4.2  riz pw_rewind(struct irs_pw *this) {
    145  1.1.1.1.4.2  riz 	UNUSED(this);
    146  1.1.1.1.4.2  riz 	/* NOOP */
    147  1.1.1.1.4.2  riz }
    148  1.1.1.1.4.2  riz 
    149  1.1.1.1.4.2  riz static void
    150  1.1.1.1.4.2  riz pw_minimize(struct irs_pw *this) {
    151  1.1.1.1.4.2  riz 	UNUSED(this);
    152  1.1.1.1.4.2  riz 	/* NOOP */
    153  1.1.1.1.4.2  riz }
    154  1.1.1.1.4.2  riz 
    155  1.1.1.1.4.2  riz static struct __res_state *
    156  1.1.1.1.4.2  riz pw_res_get(struct irs_pw *this) {
    157  1.1.1.1.4.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    158  1.1.1.1.4.2  riz 	struct dns_p *dns = pvt->dns;
    159  1.1.1.1.4.2  riz 
    160  1.1.1.1.4.2  riz 	return (__hesiod_res_get(dns->hes_ctx));
    161  1.1.1.1.4.2  riz }
    162  1.1.1.1.4.2  riz 
    163  1.1.1.1.4.2  riz static void
    164  1.1.1.1.4.2  riz pw_res_set(struct irs_pw *this, struct __res_state * res,
    165  1.1.1.1.4.2  riz 	   void (*free_res)(void *)) {
    166  1.1.1.1.4.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    167  1.1.1.1.4.2  riz 	struct dns_p *dns = pvt->dns;
    168  1.1.1.1.4.2  riz 
    169  1.1.1.1.4.2  riz 	__hesiod_res_set(dns->hes_ctx, res, free_res);
    170  1.1.1.1.4.2  riz }
    171  1.1.1.1.4.2  riz 
    172  1.1.1.1.4.2  riz /* Private. */
    173  1.1.1.1.4.2  riz 
    174  1.1.1.1.4.2  riz static struct passwd *
    175  1.1.1.1.4.2  riz getpwcommon(struct irs_pw *this, const char *arg, const char *type) {
    176  1.1.1.1.4.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    177  1.1.1.1.4.2  riz 	char **hes_list, *cp;
    178  1.1.1.1.4.2  riz 
    179  1.1.1.1.4.2  riz 	if (!(hes_list = hesiod_resolve(pvt->dns->hes_ctx, arg, type)))
    180  1.1.1.1.4.2  riz 		return (NULL);
    181  1.1.1.1.4.2  riz 	if (!*hes_list) {
    182  1.1.1.1.4.2  riz 		hesiod_free_list(pvt->dns->hes_ctx, hes_list);
    183  1.1.1.1.4.2  riz 		errno = ENOENT;
    184  1.1.1.1.4.2  riz 		return (NULL);
    185  1.1.1.1.4.2  riz 	}
    186  1.1.1.1.4.2  riz 
    187  1.1.1.1.4.2  riz 	memset(&pvt->passwd, 0, sizeof pvt->passwd);
    188  1.1.1.1.4.2  riz 	if (pvt->pwbuf)
    189  1.1.1.1.4.2  riz 		free(pvt->pwbuf);
    190  1.1.1.1.4.2  riz 	pvt->pwbuf = strdup(*hes_list);
    191  1.1.1.1.4.2  riz 	hesiod_free_list(pvt->dns->hes_ctx, hes_list);
    192  1.1.1.1.4.2  riz 
    193  1.1.1.1.4.2  riz 	cp = pvt->pwbuf;
    194  1.1.1.1.4.2  riz 	pvt->passwd.pw_name = cp;
    195  1.1.1.1.4.2  riz 	if (!(cp = strchr(cp, ':')))
    196  1.1.1.1.4.2  riz 		goto cleanup;
    197  1.1.1.1.4.2  riz 	*cp++ = '\0';
    198  1.1.1.1.4.2  riz 
    199  1.1.1.1.4.2  riz 	pvt->passwd.pw_passwd = cp;
    200  1.1.1.1.4.2  riz 	if (!(cp = strchr(cp, ':')))
    201  1.1.1.1.4.2  riz 		goto cleanup;
    202  1.1.1.1.4.2  riz 	*cp++ = '\0';
    203  1.1.1.1.4.2  riz 
    204  1.1.1.1.4.2  riz 	pvt->passwd.pw_uid = atoi(cp);
    205  1.1.1.1.4.2  riz 	if (!(cp = strchr(cp, ':')))
    206  1.1.1.1.4.2  riz 		goto cleanup;
    207  1.1.1.1.4.2  riz 	*cp++ = '\0';
    208  1.1.1.1.4.2  riz 
    209  1.1.1.1.4.2  riz 	pvt->passwd.pw_gid = atoi(cp);
    210  1.1.1.1.4.2  riz 	if (!(cp = strchr(cp, ':')))
    211  1.1.1.1.4.2  riz 		goto cleanup;
    212  1.1.1.1.4.2  riz 	*cp++ = '\0';
    213  1.1.1.1.4.2  riz 
    214  1.1.1.1.4.2  riz 	pvt->passwd.pw_gecos = cp;
    215  1.1.1.1.4.2  riz 	if (!(cp = strchr(cp, ':')))
    216  1.1.1.1.4.2  riz 		goto cleanup;
    217  1.1.1.1.4.2  riz 	*cp++ = '\0';
    218  1.1.1.1.4.2  riz 
    219  1.1.1.1.4.2  riz 	pvt->passwd.pw_dir = cp;
    220  1.1.1.1.4.2  riz 	if (!(cp = strchr(cp, ':')))
    221  1.1.1.1.4.2  riz 		goto cleanup;
    222  1.1.1.1.4.2  riz 	*cp++ = '\0';
    223  1.1.1.1.4.2  riz 
    224  1.1.1.1.4.2  riz 	pvt->passwd.pw_shell = cp;
    225  1.1.1.1.4.2  riz 	return (&pvt->passwd);
    226  1.1.1.1.4.2  riz 
    227  1.1.1.1.4.2  riz  cleanup:
    228  1.1.1.1.4.2  riz 	free(pvt->pwbuf);
    229  1.1.1.1.4.2  riz 	pvt->pwbuf = NULL;
    230  1.1.1.1.4.2  riz 	return (NULL);
    231  1.1.1.1.4.2  riz }
    232  1.1.1.1.4.2  riz 
    233  1.1.1.1.4.2  riz #endif /* WANT_IRS_PW */
    234  1.1.1.1.4.2  riz /*! \file */
    235