17c478bd9Sstevel@tonic-gate /* 2*9525b14bSRao Shoaib * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC") 3*9525b14bSRao Shoaib * Copyright (C) 1998, 1999, 2001, 2003 Internet Software Consortium. 47c478bd9Sstevel@tonic-gate * 5*9525b14bSRao Shoaib * Permission to use, copy, modify, and/or distribute this software for any 67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 87c478bd9Sstevel@tonic-gate * 9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10*9525b14bSRao Shoaib * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11*9525b14bSRao Shoaib * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12*9525b14bSRao Shoaib * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13*9525b14bSRao Shoaib * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14*9525b14bSRao Shoaib * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15*9525b14bSRao Shoaib * PERFORMANCE OF THIS SOFTWARE. 167c478bd9Sstevel@tonic-gate */ 177c478bd9Sstevel@tonic-gate 187c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: getnetgrent_r.c,v 1.14 2008/11/14 02:36:51 marka Exp $"; 207c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate #include <port_before.h> 237c478bd9Sstevel@tonic-gate #if !defined(_REENTRANT) || !defined(DO_PTHREADS) 247c478bd9Sstevel@tonic-gate static int getnetgrent_r_not_required = 0; 257c478bd9Sstevel@tonic-gate #else 267c478bd9Sstevel@tonic-gate #include <errno.h> 277c478bd9Sstevel@tonic-gate #include <string.h> 287c478bd9Sstevel@tonic-gate #include <stdio.h> 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <netinet/in.h> 317c478bd9Sstevel@tonic-gate #include <netdb.h> 327c478bd9Sstevel@tonic-gate #include <stdlib.h> 337c478bd9Sstevel@tonic-gate #include <port_after.h> 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #ifdef NGR_R_RETURN 36*9525b14bSRao Shoaib #ifndef NGR_R_PRIVATE 37*9525b14bSRao Shoaib #define NGR_R_PRIVATE 0 38*9525b14bSRao Shoaib #endif 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate static NGR_R_RETURN 41*9525b14bSRao Shoaib copy_protoent(NGR_R_CONST char **, NGR_R_CONST char **, NGR_R_CONST char **, 42*9525b14bSRao Shoaib const char *, const char *, const char *, NGR_R_COPY_ARGS); 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate NGR_R_RETURN 457c478bd9Sstevel@tonic-gate innetgr_r(const char *netgroup, const char *host, const char *user, 467c478bd9Sstevel@tonic-gate const char *domain) { 477c478bd9Sstevel@tonic-gate char *ng, *ho, *us, *dom; 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate DE_CONST(netgroup, ng); 507c478bd9Sstevel@tonic-gate DE_CONST(host, ho); 517c478bd9Sstevel@tonic-gate DE_CONST(user, us); 527c478bd9Sstevel@tonic-gate DE_CONST(domain, dom); 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate return (innetgr(ng, ho, us, dom)); 557c478bd9Sstevel@tonic-gate } 567c478bd9Sstevel@tonic-gate 57*9525b14bSRao Shoaib /*% 587c478bd9Sstevel@tonic-gate * These assume a single context is in operation per thread. 597c478bd9Sstevel@tonic-gate * If this is not the case we will need to call irs directly 607c478bd9Sstevel@tonic-gate * rather than through the base functions. 617c478bd9Sstevel@tonic-gate */ 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate NGR_R_RETURN 64*9525b14bSRao Shoaib getnetgrent_r(NGR_R_CONST char **machinep, NGR_R_CONST char **userp, 65*9525b14bSRao Shoaib NGR_R_CONST char **domainp, NGR_R_ARGS) 66*9525b14bSRao Shoaib { 67*9525b14bSRao Shoaib NGR_R_CONST char *mp, *up, *dp; 687c478bd9Sstevel@tonic-gate int res = getnetgrent(&mp, &up, &dp); 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate if (res != 1) 717c478bd9Sstevel@tonic-gate return (res); 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate return (copy_protoent(machinep, userp, domainp, 747c478bd9Sstevel@tonic-gate mp, up, dp, NGR_R_COPY)); 757c478bd9Sstevel@tonic-gate } 767c478bd9Sstevel@tonic-gate 77*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 2 78*9525b14bSRao Shoaib struct private { 79*9525b14bSRao Shoaib char *buf; 80*9525b14bSRao Shoaib }; 81*9525b14bSRao Shoaib 82*9525b14bSRao Shoaib #endif 837c478bd9Sstevel@tonic-gate NGR_R_SET_RETURN 84*9525b14bSRao Shoaib #ifdef NGR_R_SET_ARGS 85*9525b14bSRao Shoaib setnetgrent_r(NGR_R_SET_CONST char *netgroup, NGR_R_SET_ARGS) 867c478bd9Sstevel@tonic-gate #else 87*9525b14bSRao Shoaib setnetgrent_r(NGR_R_SET_CONST char *netgroup) 887c478bd9Sstevel@tonic-gate #endif 897c478bd9Sstevel@tonic-gate { 90*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 2 91*9525b14bSRao Shoaib struct private *p; 92*9525b14bSRao Shoaib #endif 93*9525b14bSRao Shoaib char *tmp; 94*9525b14bSRao Shoaib #if defined(NGR_R_SET_ARGS) && NGR_R_PRIVATE == 0 95*9525b14bSRao Shoaib UNUSED(buf); 96*9525b14bSRao Shoaib UNUSED(buflen); 97*9525b14bSRao Shoaib #endif 98*9525b14bSRao Shoaib 99*9525b14bSRao Shoaib DE_CONST(netgroup, tmp); 100*9525b14bSRao Shoaib setnetgrent(tmp); 101*9525b14bSRao Shoaib 102*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 1 1037c478bd9Sstevel@tonic-gate *buf = NULL; 104*9525b14bSRao Shoaib #elif NGR_R_PRIVATE == 2 105*9525b14bSRao Shoaib *buf = p = malloc(sizeof(struct private)); 106*9525b14bSRao Shoaib if (p == NULL) 107*9525b14bSRao Shoaib #ifdef NGR_R_SET_RESULT 108*9525b14bSRao Shoaib return (NGR_R_BAD); 109*9525b14bSRao Shoaib #else 110*9525b14bSRao Shoaib return; 111*9525b14bSRao Shoaib #endif 112*9525b14bSRao Shoaib p->buf = NULL; 1137c478bd9Sstevel@tonic-gate #endif 1147c478bd9Sstevel@tonic-gate #ifdef NGR_R_SET_RESULT 1157c478bd9Sstevel@tonic-gate return (NGR_R_SET_RESULT); 1167c478bd9Sstevel@tonic-gate #endif 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate NGR_R_END_RETURN 120*9525b14bSRao Shoaib #ifdef NGR_R_END_ARGS 121*9525b14bSRao Shoaib endnetgrent_r(NGR_R_END_ARGS) 1227c478bd9Sstevel@tonic-gate #else 1237c478bd9Sstevel@tonic-gate endnetgrent_r(void) 1247c478bd9Sstevel@tonic-gate #endif 1257c478bd9Sstevel@tonic-gate { 126*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 2 127*9525b14bSRao Shoaib struct private *p = buf; 128*9525b14bSRao Shoaib #endif 129*9525b14bSRao Shoaib #if defined(NGR_R_SET_ARGS) && NGR_R_PRIVATE == 0 130*9525b14bSRao Shoaib UNUSED(buf); 131*9525b14bSRao Shoaib UNUSED(buflen); 132*9525b14bSRao Shoaib #endif 133*9525b14bSRao Shoaib 1347c478bd9Sstevel@tonic-gate endnetgrent(); 135*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 1 1367c478bd9Sstevel@tonic-gate if (*buf != NULL) 1377c478bd9Sstevel@tonic-gate free(*buf); 1387c478bd9Sstevel@tonic-gate *buf = NULL; 139*9525b14bSRao Shoaib #elif NGR_R_PRIVATE == 2 140*9525b14bSRao Shoaib if (p->buf != NULL) 141*9525b14bSRao Shoaib free(p->buf); 142*9525b14bSRao Shoaib free(p); 1437c478bd9Sstevel@tonic-gate #endif 1447c478bd9Sstevel@tonic-gate NGR_R_END_RESULT(NGR_R_OK); 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate /* Private */ 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate static int 150*9525b14bSRao Shoaib copy_protoent(NGR_R_CONST char **machinep, NGR_R_CONST char **userp, 151*9525b14bSRao Shoaib NGR_R_CONST char **domainp, const char *mp, const char *up, 152*9525b14bSRao Shoaib const char *dp, NGR_R_COPY_ARGS) 153*9525b14bSRao Shoaib { 154*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 2 155*9525b14bSRao Shoaib struct private *p = buf; 156*9525b14bSRao Shoaib #endif 1577c478bd9Sstevel@tonic-gate char *cp; 1587c478bd9Sstevel@tonic-gate int n; 1597c478bd9Sstevel@tonic-gate int len; 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate /* Find out the amount of space required to store the answer. */ 1627c478bd9Sstevel@tonic-gate len = 0; 1637c478bd9Sstevel@tonic-gate if (mp != NULL) len += strlen(mp) + 1; 1647c478bd9Sstevel@tonic-gate if (up != NULL) len += strlen(up) + 1; 1657c478bd9Sstevel@tonic-gate if (dp != NULL) len += strlen(dp) + 1; 1667c478bd9Sstevel@tonic-gate 167*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 1 168*9525b14bSRao Shoaib if (*buf != NULL) 1697c478bd9Sstevel@tonic-gate free(*buf); 1707c478bd9Sstevel@tonic-gate *buf = malloc(len); 1717c478bd9Sstevel@tonic-gate if (*buf == NULL) 1727c478bd9Sstevel@tonic-gate return(NGR_R_BAD); 1737c478bd9Sstevel@tonic-gate cp = *buf; 174*9525b14bSRao Shoaib #elif NGR_R_PRIVATE == 2 175*9525b14bSRao Shoaib if (p->buf) 176*9525b14bSRao Shoaib free(p->buf); 177*9525b14bSRao Shoaib p->buf = malloc(len); 178*9525b14bSRao Shoaib if (p->buf == NULL) 179*9525b14bSRao Shoaib return(NGR_R_BAD); 180*9525b14bSRao Shoaib cp = p->buf; 1817c478bd9Sstevel@tonic-gate #else 1827c478bd9Sstevel@tonic-gate if (len > (int)buflen) { 1837c478bd9Sstevel@tonic-gate errno = ERANGE; 1847c478bd9Sstevel@tonic-gate return (NGR_R_BAD); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate cp = buf; 1877c478bd9Sstevel@tonic-gate #endif 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate if (mp != NULL) { 1907c478bd9Sstevel@tonic-gate n = strlen(mp) + 1; 1917c478bd9Sstevel@tonic-gate strcpy(cp, mp); 1927c478bd9Sstevel@tonic-gate *machinep = cp; 1937c478bd9Sstevel@tonic-gate cp += n; 1947c478bd9Sstevel@tonic-gate } else 1957c478bd9Sstevel@tonic-gate *machinep = NULL; 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate if (up != NULL) { 1987c478bd9Sstevel@tonic-gate n = strlen(up) + 1; 1997c478bd9Sstevel@tonic-gate strcpy(cp, up); 2007c478bd9Sstevel@tonic-gate *userp = cp; 2017c478bd9Sstevel@tonic-gate cp += n; 2027c478bd9Sstevel@tonic-gate } else 2037c478bd9Sstevel@tonic-gate *userp = NULL; 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate if (dp != NULL) { 2067c478bd9Sstevel@tonic-gate n = strlen(dp) + 1; 2077c478bd9Sstevel@tonic-gate strcpy(cp, dp); 2087c478bd9Sstevel@tonic-gate *domainp = cp; 2097c478bd9Sstevel@tonic-gate cp += n; 2107c478bd9Sstevel@tonic-gate } else 2117c478bd9Sstevel@tonic-gate *domainp = NULL; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate return (NGR_R_OK); 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate #else /* NGR_R_RETURN */ 2167c478bd9Sstevel@tonic-gate static int getnetgrent_r_unknown_system = 0; 2177c478bd9Sstevel@tonic-gate #endif /* NGR_R_RETURN */ 2187c478bd9Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */ 219*9525b14bSRao Shoaib /*! \file */ 220