1 /* 2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (c) 1996, 1998 by Internet Software Consortium. 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 /* Imports */ 19 20 #include "port_before.h" 21 22 #include <errno.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <unistd.h> 27 #include <syslog.h> 28 29 #include <irs.h> 30 #include <irp.h> 31 #include <isc/memcluster.h> 32 #include <isc/irpmarshall.h> 33 34 #include "irs_p.h" 35 #include "irp_p.h" 36 37 #include "port_after.h" 38 39 /* Definitions */ 40 41 struct pvt { 42 struct irp_p *girpdata; 43 int warned; 44 }; 45 46 47 /* Forward */ 48 49 static void ng_rewind(struct irs_ng *, const char*); 50 static void ng_close(struct irs_ng *); 51 static int ng_next(struct irs_ng *, const char **, const char **, 52 const char **); 53 static int ng_test(struct irs_ng *, const char *, 54 const char *, const char *, 55 const char *); 56 static void ng_minimize(struct irs_ng *); 57 58 59 /* Public */ 60 61 /*% 62 * Intialize the irp netgroup module. 63 * 64 */ 65 66 struct irs_ng * 67 irs_irp_ng(struct irs_acc *this) { 68 struct irs_ng *ng; 69 struct pvt *pvt; 70 71 if (!(ng = memget(sizeof *ng))) { 72 errno = ENOMEM; 73 return (NULL); 74 } 75 memset(ng, 0x5e, sizeof *ng); 76 77 if (!(pvt = memget(sizeof *pvt))) { 78 memput(ng, sizeof *ng); 79 errno = ENOMEM; 80 return (NULL); 81 } 82 memset(pvt, 0, sizeof *pvt); 83 pvt->girpdata = this->private; 84 85 ng->private = pvt; 86 ng->close = ng_close; 87 ng->next = ng_next; 88 ng->test = ng_test; 89 ng->rewind = ng_rewind; 90 ng->minimize = ng_minimize; 91 return (ng); 92 } 93 94 /* Methods */ 95 96 97 98 /* 99 * void ng_close(struct irs_ng *this) 100 * 101 */ 102 103 static void 104 ng_close(struct irs_ng *this) { 105 struct pvt *pvt = (struct pvt *)this->private; 106 107 ng_minimize(this); 108 109 memput(pvt, sizeof *pvt); 110 memput(this, sizeof *this); 111 } 112 113 114 115 116 /* 117 * void ng_rewind(struct irs_ng *this, const char *group) 118 * 119 * 120 */ 121 122 static void 123 ng_rewind(struct irs_ng *this, const char *group) { 124 struct pvt *pvt = (struct pvt *)this->private; 125 char text[256]; 126 int code; 127 128 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { 129 return; 130 } 131 132 if (irs_irp_send_command(pvt->girpdata, 133 "setnetgrent %s", group) != 0) { 134 return; 135 } 136 137 code = irs_irp_read_response(pvt->girpdata, text, sizeof text); 138 if (code != IRPD_GETNETGR_SETOK) { 139 if (irp_log_errors) { 140 syslog(LOG_WARNING, "setnetgrent(%s) failed: %s", 141 group, text); 142 } 143 } 144 145 return; 146 } 147 148 /* 149 * Get the next netgroup item from the cache. 150 * 151 */ 152 153 static int 154 ng_next(struct irs_ng *this, const char **host, const char **user, 155 const char **domain) 156 { 157 struct pvt *pvt = (struct pvt *)this->private; 158 int code; 159 char *body = NULL; 160 size_t bodylen; 161 int rval = 0; 162 char text[256]; 163 164 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { 165 return (0); 166 } 167 168 if (irs_irp_send_command(pvt->girpdata, "getnetgrent") != 0) 169 return (0); 170 171 if (irs_irp_get_full_response(pvt->girpdata, &code, 172 text, sizeof text, 173 &body, &bodylen) != 0) { 174 return (0); 175 } 176 177 if (code == IRPD_GETNETGR_OK) { 178 if (irp_unmarshall_ng(host, user, domain, body) == 0) { 179 rval = 1; 180 } 181 } 182 183 if (body != NULL) { 184 memput(body, bodylen); 185 } 186 187 return (rval); 188 } 189 190 /* 191 * Search for a match in a netgroup. 192 * 193 */ 194 195 static int 196 ng_test(struct irs_ng *this, const char *name, 197 const char *host, const char *user, const char *domain) 198 { 199 struct pvt *pvt = (struct pvt *)this->private; 200 char *body = NULL; 201 size_t bodylen = 0; 202 int code; 203 char text[256]; 204 int rval = 0; 205 206 UNUSED(name); 207 208 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { 209 return (0); 210 } 211 212 if (irp_marshall_ng(host, user, domain, &body, &bodylen) != 0) { 213 return (0); 214 } 215 216 if (irs_irp_send_command(pvt->girpdata, "innetgr %s", body) == 0) { 217 code = irs_irp_read_response(pvt->girpdata, text, sizeof text); 218 if (code == IRPD_GETNETGR_MATCHES) { 219 rval = 1; 220 } 221 } 222 223 memput(body, bodylen); 224 225 return (rval); 226 } 227 228 229 230 231 /* 232 * void ng_minimize(struct irs_ng *this) 233 * 234 */ 235 236 static void 237 ng_minimize(struct irs_ng *this) { 238 struct pvt *pvt = (struct pvt *)this->private; 239 240 irs_irp_disconnect(pvt->girpdata); 241 } 242 243 244 245 246 /* Private */ 247 248 249 /*! \file */ 250