1 1.1 christos /* $NetBSD: gen_pw.c,v 1.1.1.2 2012/09/09 16:07:55 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(LINT) && !defined(CODECENTER) 21 1.1.1.2 christos static const char rcsid[] = "Id: gen_pw.c,v 1.3 2005/04/27 04:56:24 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 #ifndef WANT_IRS_PW 29 1.1 christos static int __bind_irs_pw_unneeded; 30 1.1 christos #else 31 1.1 christos 32 1.1 christos #include <sys/types.h> 33 1.1 christos #include <netinet/in.h> 34 1.1 christos #include <arpa/nameser.h> 35 1.1 christos #include <resolv.h> 36 1.1 christos 37 1.1 christos #include <errno.h> 38 1.1 christos #include <pwd.h> 39 1.1 christos #include <stdlib.h> 40 1.1 christos #include <string.h> 41 1.1 christos 42 1.1 christos #include <isc/memcluster.h> 43 1.1 christos #include <irs.h> 44 1.1 christos 45 1.1 christos #include "port_after.h" 46 1.1 christos 47 1.1 christos #include "irs_p.h" 48 1.1 christos #include "gen_p.h" 49 1.1 christos 50 1.1 christos /* Types */ 51 1.1 christos 52 1.1 christos struct pvt { 53 1.1 christos struct irs_rule * rules; 54 1.1 christos struct irs_rule * rule; 55 1.1 christos struct __res_state * res; 56 1.1 christos void (*free_res)(void *); 57 1.1 christos }; 58 1.1 christos 59 1.1 christos /* Forward */ 60 1.1 christos 61 1.1 christos static void pw_close(struct irs_pw *); 62 1.1 christos static struct passwd * pw_next(struct irs_pw *); 63 1.1 christos static struct passwd * pw_byname(struct irs_pw *, const char *); 64 1.1 christos static struct passwd * pw_byuid(struct irs_pw *, uid_t); 65 1.1 christos static void pw_rewind(struct irs_pw *); 66 1.1 christos static void pw_minimize(struct irs_pw *); 67 1.1 christos static struct __res_state * pw_res_get(struct irs_pw *); 68 1.1 christos static void pw_res_set(struct irs_pw *, 69 1.1 christos struct __res_state *, 70 1.1 christos void (*)(void *)); 71 1.1 christos 72 1.1 christos /* Public */ 73 1.1 christos 74 1.1 christos struct irs_pw * 75 1.1 christos irs_gen_pw(struct irs_acc *this) { 76 1.1 christos struct gen_p *accpvt = (struct gen_p *)this->private; 77 1.1 christos struct irs_pw *pw; 78 1.1 christos struct pvt *pvt; 79 1.1 christos 80 1.1 christos if (!(pw = memget(sizeof *pw))) { 81 1.1 christos errno = ENOMEM; 82 1.1 christos return (NULL); 83 1.1 christos } 84 1.1 christos memset(pw, 0x5e, sizeof *pw); 85 1.1 christos if (!(pvt = memget(sizeof *pvt))) { 86 1.1 christos memput(pw, sizeof *pvt); 87 1.1 christos errno = ENOMEM; 88 1.1 christos return (NULL); 89 1.1 christos } 90 1.1 christos memset(pvt, 0, sizeof *pvt); 91 1.1 christos pvt->rules = accpvt->map_rules[irs_pw]; 92 1.1 christos pvt->rule = pvt->rules; 93 1.1 christos pw->private = pvt; 94 1.1 christos pw->close = pw_close; 95 1.1 christos pw->next = pw_next; 96 1.1 christos pw->byname = pw_byname; 97 1.1 christos pw->byuid = pw_byuid; 98 1.1 christos pw->rewind = pw_rewind; 99 1.1 christos pw->minimize = pw_minimize; 100 1.1 christos pw->res_get = pw_res_get; 101 1.1 christos pw->res_set = pw_res_set; 102 1.1 christos return (pw); 103 1.1 christos } 104 1.1 christos 105 1.1 christos /* Methods */ 106 1.1 christos 107 1.1 christos static void 108 1.1 christos pw_close(struct irs_pw *this) { 109 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 110 1.1 christos 111 1.1 christos memput(pvt, sizeof *pvt); 112 1.1 christos memput(this, sizeof *this); 113 1.1 christos } 114 1.1 christos 115 1.1 christos static struct passwd * 116 1.1 christos pw_next(struct irs_pw *this) { 117 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 118 1.1 christos struct passwd *rval; 119 1.1 christos struct irs_pw *pw; 120 1.1 christos 121 1.1 christos while (pvt->rule) { 122 1.1 christos pw = pvt->rule->inst->pw; 123 1.1 christos rval = (*pw->next)(pw); 124 1.1 christos if (rval) 125 1.1 christos return (rval); 126 1.1 christos if (!(pvt->rule->flags & IRS_CONTINUE)) 127 1.1 christos break; 128 1.1 christos pvt->rule = pvt->rule->next; 129 1.1 christos if (pvt->rule) { 130 1.1 christos pw = pvt->rule->inst->pw; 131 1.1 christos (*pw->rewind)(pw); 132 1.1 christos } 133 1.1 christos } 134 1.1 christos return (NULL); 135 1.1 christos } 136 1.1 christos 137 1.1 christos static void 138 1.1 christos pw_rewind(struct irs_pw *this) { 139 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 140 1.1 christos struct irs_pw *pw; 141 1.1 christos 142 1.1 christos pvt->rule = pvt->rules; 143 1.1 christos if (pvt->rule) { 144 1.1 christos pw = pvt->rule->inst->pw; 145 1.1 christos (*pw->rewind)(pw); 146 1.1 christos } 147 1.1 christos } 148 1.1 christos 149 1.1 christos static struct passwd * 150 1.1 christos pw_byname(struct irs_pw *this, const char *name) { 151 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 152 1.1 christos struct irs_rule *rule; 153 1.1 christos struct passwd *rval; 154 1.1 christos struct irs_pw *pw; 155 1.1 christos 156 1.1 christos rval = NULL; 157 1.1 christos for (rule = pvt->rules; rule; rule = rule->next) { 158 1.1 christos pw = rule->inst->pw; 159 1.1 christos rval = (*pw->byname)(pw, name); 160 1.1 christos if (rval || !(rule->flags & IRS_CONTINUE)) 161 1.1 christos break; 162 1.1 christos } 163 1.1 christos return (rval); 164 1.1 christos } 165 1.1 christos 166 1.1 christos static struct passwd * 167 1.1 christos pw_byuid(struct irs_pw *this, uid_t uid) { 168 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 169 1.1 christos struct irs_rule *rule; 170 1.1 christos struct passwd *rval; 171 1.1 christos struct irs_pw *pw; 172 1.1 christos 173 1.1 christos rval = NULL; 174 1.1 christos for (rule = pvt->rules; rule; rule = rule->next) { 175 1.1 christos pw = rule->inst->pw; 176 1.1 christos rval = (*pw->byuid)(pw, uid); 177 1.1 christos if (rval || !(rule->flags & IRS_CONTINUE)) 178 1.1 christos break; 179 1.1 christos } 180 1.1 christos return (rval); 181 1.1 christos } 182 1.1 christos 183 1.1 christos static void 184 1.1 christos pw_minimize(struct irs_pw *this) { 185 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 186 1.1 christos struct irs_rule *rule; 187 1.1 christos 188 1.1 christos for (rule = pvt->rules; rule != NULL; rule = rule->next) { 189 1.1 christos struct irs_pw *pw = rule->inst->pw; 190 1.1 christos 191 1.1 christos (*pw->minimize)(pw); 192 1.1 christos } 193 1.1 christos } 194 1.1 christos 195 1.1 christos static struct __res_state * 196 1.1 christos pw_res_get(struct irs_pw *this) { 197 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 198 1.1 christos 199 1.1 christos if (!pvt->res) { 200 1.1 christos struct __res_state *res; 201 1.1 christos res = (struct __res_state *)malloc(sizeof *res); 202 1.1 christos if (!res) { 203 1.1 christos errno = ENOMEM; 204 1.1 christos return (NULL); 205 1.1 christos } 206 1.1 christos memset(res, 0, sizeof *res); 207 1.1 christos pw_res_set(this, res, free); 208 1.1 christos } 209 1.1 christos 210 1.1 christos return (pvt->res); 211 1.1 christos } 212 1.1 christos 213 1.1 christos static void 214 1.1 christos pw_res_set(struct irs_pw *this, struct __res_state *res, 215 1.1 christos void (*free_res)(void *)) { 216 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 217 1.1 christos struct irs_rule *rule; 218 1.1 christos 219 1.1 christos if (pvt->res && pvt->free_res) { 220 1.1 christos res_nclose(pvt->res); 221 1.1 christos (*pvt->free_res)(pvt->res); 222 1.1 christos } 223 1.1 christos 224 1.1 christos pvt->res = res; 225 1.1 christos pvt->free_res = free_res; 226 1.1 christos 227 1.1 christos for (rule = pvt->rules; rule != NULL; rule = rule->next) { 228 1.1 christos struct irs_pw *pw = rule->inst->pw; 229 1.1 christos 230 1.1 christos if (pw->res_set) 231 1.1 christos (*pw->res_set)(pw, pvt->res, NULL); 232 1.1 christos } 233 1.1 christos } 234 1.1 christos 235 1.1 christos #endif /* WANT_IRS_PW */ 236 1.1 christos /*! \file */ 237