1 1.1 christos /* $NetBSD: lcl_gr.c,v 1.1.1.2 2012/09/09 16:07:57 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (c) 1989, 1993, 1995 5 1.1 christos * The Regents of the University of California. All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 1. Redistributions of source code must retain the above copyright 11 1.1 christos * notice, this list of conditions and the following disclaimer. 12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos * notice, this list of conditions and the following disclaimer in the 14 1.1 christos * documentation and/or other materials provided with the distribution. 15 1.1 christos * 3. All advertising materials mentioning features or use of this software 16 1.1 christos * must display the following acknowledgement: 17 1.1 christos * This product includes software developed by the University of 18 1.1 christos * California, Berkeley and its contributors. 19 1.1 christos * 4. Neither the name of the University nor the names of its contributors 20 1.1 christos * may be used to endorse or promote products derived from this software 21 1.1 christos * without specific prior written permission. 22 1.1 christos * 23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 1.1 christos * SUCH DAMAGE. 34 1.1 christos */ 35 1.1 christos 36 1.1 christos /* 37 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 38 1.1 christos * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 39 1.1 christos * 40 1.1 christos * Permission to use, copy, modify, and distribute this software for any 41 1.1 christos * purpose with or without fee is hereby granted, provided that the above 42 1.1 christos * copyright notice and this permission notice appear in all copies. 43 1.1 christos * 44 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 45 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 46 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 47 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 48 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 49 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 50 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 51 1.1 christos */ 52 1.1 christos 53 1.1 christos #if defined(LIBC_SCCS) && !defined(lint) 54 1.1.1.2 christos static const char rcsid[] = "Id: lcl_gr.c,v 1.3 2005/04/27 04:56:30 sra Exp "; 55 1.1 christos /* from getgrent.c 8.2 (Berkeley) 3/21/94"; */ 56 1.1 christos /* from BSDI Id: getgrent.c,v 2.8 1996/05/28 18:15:14 bostic Exp $ */ 57 1.1 christos #endif /* LIBC_SCCS and not lint */ 58 1.1 christos 59 1.1 christos /* extern */ 60 1.1 christos 61 1.1 christos #include "port_before.h" 62 1.1 christos 63 1.1 christos #ifndef WANT_IRS_PW 64 1.1 christos static int __bind_irs_gr_unneeded; 65 1.1 christos #else 66 1.1 christos 67 1.1 christos #include <sys/param.h> 68 1.1 christos #include <sys/types.h> 69 1.1 christos #include <netinet/in.h> 70 1.1 christos #include <arpa/nameser.h> 71 1.1 christos #include <resolv.h> 72 1.1 christos 73 1.1 christos #include <errno.h> 74 1.1 christos #include <fcntl.h> 75 1.1 christos #include <grp.h> 76 1.1 christos #include <stdio.h> 77 1.1 christos #include <stdlib.h> 78 1.1 christos #include <string.h> 79 1.1 christos #include <unistd.h> 80 1.1 christos 81 1.1 christos #include <irs.h> 82 1.1 christos #include <isc/memcluster.h> 83 1.1 christos 84 1.1 christos #include "irs_p.h" 85 1.1 christos #include "lcl_p.h" 86 1.1 christos #include "irp_p.h" 87 1.1 christos 88 1.1 christos #include "port_after.h" 89 1.1 christos 90 1.1 christos 91 1.1 christos /* Types. */ 92 1.1 christos 93 1.1 christos struct pvt { 94 1.1 christos FILE * fp; 95 1.1 christos /*%< 96 1.1 christos * Need space to store the entries read from the group file. 97 1.1 christos * The members list also needs space per member, and the 98 1.1 christos * strings making up the user names must be allocated 99 1.1 christos * somewhere. Rather than doing lots of small allocations, 100 1.1 christos * we keep one buffer and resize it as needed. 101 1.1 christos */ 102 1.1 christos struct group group; 103 1.1 christos size_t nmemb; /*%< Malloc'd max index of gr_mem[]. */ 104 1.1 christos char * membuf; 105 1.1 christos size_t membufsize; 106 1.1 christos }; 107 1.1 christos 108 1.1 christos /* Forward. */ 109 1.1 christos 110 1.1 christos static void gr_close(struct irs_gr *); 111 1.1 christos static struct group * gr_next(struct irs_gr *); 112 1.1 christos static struct group * gr_byname(struct irs_gr *, const char *); 113 1.1 christos static struct group * gr_bygid(struct irs_gr *, gid_t); 114 1.1 christos static void gr_rewind(struct irs_gr *); 115 1.1 christos static void gr_minimize(struct irs_gr *); 116 1.1 christos 117 1.1 christos static int grstart(struct pvt *); 118 1.1 christos static char * grnext(struct pvt *); 119 1.1 christos static struct group * grscan(struct irs_gr *, int, gid_t, const char *); 120 1.1 christos 121 1.1 christos /* Portability. */ 122 1.1 christos 123 1.1 christos #ifndef SEEK_SET 124 1.1 christos # define SEEK_SET 0 125 1.1 christos #endif 126 1.1 christos 127 1.1 christos /* Public. */ 128 1.1 christos 129 1.1 christos struct irs_gr * 130 1.1 christos irs_lcl_gr(struct irs_acc *this) { 131 1.1 christos struct irs_gr *gr; 132 1.1 christos struct pvt *pvt; 133 1.1 christos 134 1.1 christos UNUSED(this); 135 1.1 christos 136 1.1 christos if (!(gr = memget(sizeof *gr))) { 137 1.1 christos errno = ENOMEM; 138 1.1 christos return (NULL); 139 1.1 christos } 140 1.1 christos memset(gr, 0x5e, sizeof *gr); 141 1.1 christos if (!(pvt = memget(sizeof *pvt))) { 142 1.1 christos memput(gr, sizeof *gr); 143 1.1 christos errno = ENOMEM; 144 1.1 christos return (NULL); 145 1.1 christos } 146 1.1 christos memset(pvt, 0, sizeof *pvt); 147 1.1 christos gr->private = pvt; 148 1.1 christos gr->close = gr_close; 149 1.1 christos gr->next = gr_next; 150 1.1 christos gr->byname = gr_byname; 151 1.1 christos gr->bygid = gr_bygid; 152 1.1 christos gr->rewind = gr_rewind; 153 1.1 christos gr->list = make_group_list; 154 1.1 christos gr->minimize = gr_minimize; 155 1.1 christos gr->res_get = NULL; 156 1.1 christos gr->res_set = NULL; 157 1.1 christos return (gr); 158 1.1 christos } 159 1.1 christos 160 1.1 christos /* Methods. */ 161 1.1 christos 162 1.1 christos static void 163 1.1 christos gr_close(struct irs_gr *this) { 164 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 165 1.1 christos 166 1.1 christos if (pvt->fp) 167 1.1 christos (void)fclose(pvt->fp); 168 1.1 christos if (pvt->group.gr_mem) 169 1.1 christos free(pvt->group.gr_mem); 170 1.1 christos if (pvt->membuf) 171 1.1 christos free(pvt->membuf); 172 1.1 christos memput(pvt, sizeof *pvt); 173 1.1 christos memput(this, sizeof *this); 174 1.1 christos } 175 1.1 christos 176 1.1 christos static struct group * 177 1.1 christos gr_next(struct irs_gr *this) { 178 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 179 1.1 christos 180 1.1 christos if (!pvt->fp && !grstart(pvt)) 181 1.1 christos return (NULL); 182 1.1 christos return (grscan(this, 0, 0, NULL)); 183 1.1 christos } 184 1.1 christos 185 1.1 christos static struct group * 186 1.1 christos gr_byname(struct irs_gr *this, const char *name) { 187 1.1 christos if (!grstart((struct pvt *)this->private)) 188 1.1 christos return (NULL); 189 1.1 christos return (grscan(this, 1, 0, name)); 190 1.1 christos } 191 1.1 christos 192 1.1 christos static struct group * 193 1.1 christos gr_bygid(struct irs_gr *this, gid_t gid) { 194 1.1 christos if (!grstart((struct pvt *)this->private)) 195 1.1 christos return (NULL); 196 1.1 christos return (grscan(this, 1, gid, NULL)); 197 1.1 christos } 198 1.1 christos 199 1.1 christos static void 200 1.1 christos gr_rewind(struct irs_gr *this) { 201 1.1 christos (void) grstart((struct pvt *)this->private); 202 1.1 christos } 203 1.1 christos 204 1.1 christos static void 205 1.1 christos gr_minimize(struct irs_gr *this) { 206 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 207 1.1 christos 208 1.1 christos if (pvt->fp != NULL) { 209 1.1 christos (void)fclose(pvt->fp); 210 1.1 christos pvt->fp = NULL; 211 1.1 christos } 212 1.1 christos } 213 1.1 christos 214 1.1 christos /* Private. */ 215 1.1 christos 216 1.1 christos static int 217 1.1 christos grstart(struct pvt *pvt) { 218 1.1 christos if (pvt->fp) { 219 1.1 christos if (fseek(pvt->fp, 0L, SEEK_SET) == 0) 220 1.1 christos return (1); 221 1.1 christos (void)fclose(pvt->fp); 222 1.1 christos } 223 1.1 christos if (!(pvt->fp = fopen(_PATH_GROUP, "r"))) 224 1.1 christos return (0); 225 1.1 christos if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) { 226 1.1 christos fclose(pvt->fp); 227 1.1 christos return (0); 228 1.1 christos } 229 1.1 christos return (1); 230 1.1 christos } 231 1.1 christos 232 1.1 christos #define INITIAL_NMEMB 30 /*%< about 120 bytes */ 233 1.1 christos #define INITIAL_BUFSIZ (INITIAL_NMEMB * 8) /*%< about 240 bytes */ 234 1.1 christos static char * 235 1.1 christos grnext(struct pvt *pvt) { 236 1.1 christos char *w, *e; 237 1.1 christos int ch; 238 1.1 christos 239 1.1 christos /* Make sure we have a buffer. */ 240 1.1 christos if (pvt->membuf == NULL) { 241 1.1 christos pvt->membuf = malloc(INITIAL_BUFSIZ); 242 1.1 christos if (pvt->membuf == NULL) { 243 1.1 christos enomem: 244 1.1 christos errno = ENOMEM; 245 1.1 christos return (NULL); 246 1.1 christos } 247 1.1 christos pvt->membufsize = INITIAL_BUFSIZ; 248 1.1 christos } 249 1.1 christos 250 1.1 christos /* Read until EOF or EOL. */ 251 1.1 christos w = pvt->membuf; 252 1.1 christos e = pvt->membuf + pvt->membufsize; 253 1.1 christos while ((ch = fgetc(pvt->fp)) != EOF && ch != '\n') { 254 1.1 christos /* Make sure we have room for this character and a \0. */ 255 1.1 christos if (w + 1 == e) { 256 1.1 christos size_t o = w - pvt->membuf; 257 1.1 christos size_t n = pvt->membufsize * 2; 258 1.1 christos char *t = realloc(pvt->membuf, n); 259 1.1 christos 260 1.1 christos if (t == NULL) 261 1.1 christos goto enomem; 262 1.1 christos pvt->membuf = t; 263 1.1 christos pvt->membufsize = n; 264 1.1 christos w = pvt->membuf + o; 265 1.1 christos e = pvt->membuf + pvt->membufsize; 266 1.1 christos } 267 1.1 christos /* Store it. */ 268 1.1 christos *w++ = (char)ch; 269 1.1 christos } 270 1.1 christos 271 1.1 christos /* Hitting EOF on the first character really does mean EOF. */ 272 1.1 christos if (w == pvt->membuf && ch == EOF) { 273 1.1 christos errno = ENOENT; 274 1.1 christos return (NULL); 275 1.1 christos } 276 1.1 christos 277 1.1 christos /* Last line of /etc/group need not end with \n; we don't care. */ 278 1.1 christos *w = '\0'; 279 1.1 christos return (pvt->membuf); 280 1.1 christos } 281 1.1 christos 282 1.1 christos static struct group * 283 1.1 christos grscan(struct irs_gr *this, int search, gid_t gid, const char *name) { 284 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 285 1.1 christos size_t n; 286 1.1 christos char *bp, **m, *p; 287 1.1 christos 288 1.1 christos /* Read lines until we find one that matches our search criteria. */ 289 1.1 christos for (;;) { 290 1.1 christos if ((bp = grnext(pvt)) == NULL) 291 1.1 christos return (NULL); 292 1.1 christos 293 1.1 christos /* Optimize the usual case of searching for a name. */ 294 1.1 christos pvt->group.gr_name = strsep(&bp, ":"); 295 1.1 christos if (search && name != NULL && 296 1.1 christos strcmp(pvt->group.gr_name, name) != 0) 297 1.1 christos continue; 298 1.1 christos if (bp == NULL || *bp == '\0') 299 1.1 christos goto corrupt; 300 1.1 christos 301 1.1 christos /* Skip past the password field. */ 302 1.1 christos pvt->group.gr_passwd = strsep(&bp, ":"); 303 1.1 christos if (bp == NULL || *bp == '\0') 304 1.1 christos goto corrupt; 305 1.1 christos 306 1.1 christos /* Checking for a gid. */ 307 1.1 christos if ((p = strsep(&bp, ":")) == NULL) 308 1.1 christos continue; 309 1.1 christos /* 310 1.1 christos * Unlike the tests above, the test below is supposed to be 311 1.1 christos * testing 'p' and not 'bp', in case you think it's a typo. 312 1.1 christos */ 313 1.1 christos if (p == NULL || *p == '\0') { 314 1.1 christos corrupt: 315 1.1 christos /* warning: corrupted %s file!", _PATH_GROUP */ 316 1.1 christos continue; 317 1.1 christos } 318 1.1 christos pvt->group.gr_gid = atoi(p); 319 1.1 christos if (search && name == NULL && (gid_t)pvt->group.gr_gid != gid) 320 1.1 christos continue; 321 1.1 christos 322 1.1 christos /* We want this record. */ 323 1.1 christos break; 324 1.1 christos } 325 1.1 christos 326 1.1 christos /* 327 1.1 christos * Count commas to find out how many members there might be. 328 1.1 christos * Note that commas separate, so if there is one comma there 329 1.1 christos * can be two members (group:*:id:user1,user2). Add another 330 1.1 christos * to account for the NULL terminator. As above, allocate 331 1.1 christos * largest of INITIAL_NMEMB, or 2*n. 332 1.1 christos */ 333 1.1 christos n = 1; 334 1.1 christos if (bp != NULL) 335 1.1 christos for (n = 2, p = bp; (p = strpbrk(p, ", ")) != NULL; ++n) 336 1.1 christos p += strspn(p, ", "); 337 1.1 christos if (n > pvt->nmemb || pvt->group.gr_mem == NULL) { 338 1.1 christos if ((n *= 2) < INITIAL_NMEMB) 339 1.1 christos n = INITIAL_NMEMB; 340 1.1 christos if ((m = realloc(pvt->group.gr_mem, n * sizeof *m)) == NULL) 341 1.1 christos return (NULL); 342 1.1 christos pvt->group.gr_mem = m; 343 1.1 christos pvt->nmemb = n; 344 1.1 christos } 345 1.1 christos 346 1.1 christos /* Set the name pointers. */ 347 1.1 christos for (m = pvt->group.gr_mem; (p = strsep(&bp, ", ")) != NULL;) 348 1.1 christos if (p[0] != '\0') 349 1.1 christos *m++ = p; 350 1.1 christos *m = NULL; 351 1.1 christos 352 1.1 christos return (&pvt->group); 353 1.1 christos } 354 1.1 christos 355 1.1 christos #endif /* WANT_IRS_GR */ 356 1.1 christos /*! \file */ 357