1 1.1 christos /* $NetBSD: nis.c,v 1.1.1.2 2012/09/09 16:07:51 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 5 1.1 christos * Copyright (c) 1996-1999 by Internet Software Consortium. 6 1.1 christos * 7 1.1 christos * Permission to use, copy, modify, and distribute this software for any 8 1.1 christos * purpose with or without fee is hereby granted, provided that the above 9 1.1 christos * copyright notice and this permission notice appear in all copies. 10 1.1 christos * 11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 17 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 1.1 christos */ 19 1.1 christos 20 1.1 christos #if defined(LIBC_SCCS) && !defined(lint) 21 1.1.1.2 christos static const char rcsid[] = "Id: nis.c,v 1.3 2005/04/27 04:56:32 sra Exp "; 22 1.1 christos #endif 23 1.1 christos 24 1.1 christos /* Imports */ 25 1.1 christos 26 1.1 christos #include "port_before.h" 27 1.1 christos 28 1.1 christos #ifdef WANT_IRS_NIS 29 1.1 christos 30 1.1 christos #include <rpc/rpc.h> 31 1.1 christos #include <rpc/xdr.h> 32 1.1 christos #include <rpcsvc/yp_prot.h> 33 1.1 christos #include <rpcsvc/ypclnt.h> 34 1.1 christos 35 1.1 christos #include <stdlib.h> 36 1.1 christos #include <string.h> 37 1.1 christos #include <errno.h> 38 1.1 christos 39 1.1 christos #include <sys/types.h> 40 1.1 christos #include <netinet/in.h> 41 1.1 christos #ifdef T_NULL 42 1.1 christos #undef T_NULL /* Silence re-definition warning of T_NULL. */ 43 1.1 christos #endif 44 1.1 christos #include <arpa/nameser.h> 45 1.1 christos #include <resolv.h> 46 1.1 christos 47 1.1 christos #include <isc/memcluster.h> 48 1.1 christos #include <irs.h> 49 1.1 christos 50 1.1 christos #include "port_after.h" 51 1.1 christos 52 1.1 christos #include "irs_p.h" 53 1.1 christos #include "hesiod.h" 54 1.1 christos #include "nis_p.h" 55 1.1 christos 56 1.1 christos /* Forward */ 57 1.1 christos 58 1.1 christos static void nis_close(struct irs_acc *); 59 1.1 christos static struct __res_state * nis_res_get(struct irs_acc *); 60 1.1 christos static void nis_res_set(struct irs_acc *, struct __res_state *, 61 1.1 christos void (*)(void *)); 62 1.1 christos 63 1.1 christos /* Public */ 64 1.1 christos 65 1.1 christos struct irs_acc * 66 1.1 christos irs_nis_acc(const char *options) { 67 1.1 christos struct nis_p *nis; 68 1.1 christos struct irs_acc *acc; 69 1.1 christos char *domain; 70 1.1 christos 71 1.1 christos UNUSED(options); 72 1.1 christos 73 1.1 christos if (yp_get_default_domain(&domain) != 0) 74 1.1 christos return (NULL); 75 1.1 christos if (!(nis = memget(sizeof *nis))) { 76 1.1 christos errno = ENOMEM; 77 1.1 christos return (NULL); 78 1.1 christos } 79 1.1 christos memset(nis, 0, sizeof *nis); 80 1.1 christos if (!(acc = memget(sizeof *acc))) { 81 1.1 christos memput(nis, sizeof *nis); 82 1.1 christos errno = ENOMEM; 83 1.1 christos return (NULL); 84 1.1 christos } 85 1.1 christos memset(acc, 0x5e, sizeof *acc); 86 1.1 christos acc->private = nis; 87 1.1 christos nis->domain = strdup(domain); 88 1.1 christos #ifdef WANT_IRS_GR 89 1.1 christos acc->gr_map = irs_nis_gr; 90 1.1 christos #else 91 1.1 christos acc->gr_map = NULL; 92 1.1 christos #endif 93 1.1 christos #ifdef WANT_IRS_PW 94 1.1 christos acc->pw_map = irs_nis_pw; 95 1.1 christos #else 96 1.1 christos acc->pw_map = NULL; 97 1.1 christos #endif 98 1.1 christos acc->sv_map = irs_nis_sv; 99 1.1 christos acc->pr_map = irs_nis_pr; 100 1.1 christos acc->ho_map = irs_nis_ho; 101 1.1 christos acc->nw_map = irs_nis_nw; 102 1.1 christos acc->ng_map = irs_nis_ng; 103 1.1 christos acc->res_get = nis_res_get; 104 1.1 christos acc->res_set = nis_res_set; 105 1.1 christos acc->close = nis_close; 106 1.1 christos return (acc); 107 1.1 christos } 108 1.1 christos 109 1.1 christos /* Methods */ 110 1.1 christos 111 1.1 christos static struct __res_state * 112 1.1 christos nis_res_get(struct irs_acc *this) { 113 1.1 christos struct nis_p *nis = (struct nis_p *)this->private; 114 1.1 christos 115 1.1 christos if (nis->res == NULL) { 116 1.1 christos struct __res_state *res; 117 1.1 christos res = (struct __res_state *)malloc(sizeof *res); 118 1.1 christos if (res == NULL) 119 1.1 christos return (NULL); 120 1.1 christos memset(res, 0, sizeof *res); 121 1.1 christos nis_res_set(this, res, free); 122 1.1 christos } 123 1.1 christos 124 1.1 christos if ((nis->res->options & RES_INIT) == 0 && 125 1.1 christos res_ninit(nis->res) < 0) 126 1.1 christos return (NULL); 127 1.1 christos 128 1.1 christos return (nis->res); 129 1.1 christos } 130 1.1 christos 131 1.1 christos static void 132 1.1 christos nis_res_set(struct irs_acc *this, struct __res_state *res, 133 1.1 christos void (*free_res)(void *)) { 134 1.1 christos struct nis_p *nis = (struct nis_p *)this->private; 135 1.1 christos 136 1.1 christos if (nis->res && nis->free_res) { 137 1.1 christos res_nclose(nis->res); 138 1.1 christos (*nis->free_res)(nis->res); 139 1.1 christos } 140 1.1 christos 141 1.1 christos nis->res = res; 142 1.1 christos nis->free_res = free_res; 143 1.1 christos } 144 1.1 christos 145 1.1 christos static void 146 1.1 christos nis_close(struct irs_acc *this) { 147 1.1 christos struct nis_p *nis = (struct nis_p *)this->private; 148 1.1 christos 149 1.1 christos if (nis->res && nis->free_res) 150 1.1 christos (*nis->free_res)(nis->res); 151 1.1 christos free(nis->domain); 152 1.1 christos memput(nis, sizeof *nis); 153 1.1 christos memput(this, sizeof *this); 154 1.1 christos } 155 1.1 christos 156 1.1 christos #endif /*WANT_IRS_NIS*/ 157 1.1 christos 158 1.1 christos /*! \file */ 159