1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 1999-2002 Sun Microsystems, Inc. All rights reserved. 3*0Sstevel@tonic-gate * Use is subject to license terms. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate /* 7*0Sstevel@tonic-gate * Copyright (c) 1998-1999 by Internet Software Consortium. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 10*0Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 11*0Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 12*0Sstevel@tonic-gate * 13*0Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 14*0Sstevel@tonic-gate * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 15*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 16*0Sstevel@tonic-gate * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 17*0Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 18*0Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 19*0Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 20*0Sstevel@tonic-gate * SOFTWARE. 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate 23*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 26*0Sstevel@tonic-gate static const char rcsid[] = "$Id: getservent_r.c,v 8.5 2001/11/01 08:02:16 marka Exp $"; 27*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <port_before.h> 30*0Sstevel@tonic-gate #if !defined(_REENTRANT) || !defined(DO_PTHREADS) 31*0Sstevel@tonic-gate static int getservent_r_not_required = 0; 32*0Sstevel@tonic-gate #else 33*0Sstevel@tonic-gate #include <errno.h> 34*0Sstevel@tonic-gate #include <string.h> 35*0Sstevel@tonic-gate #include <stdio.h> 36*0Sstevel@tonic-gate #include <sys/types.h> 37*0Sstevel@tonic-gate #include <netinet/in.h> 38*0Sstevel@tonic-gate #include <netdb.h> 39*0Sstevel@tonic-gate #include <sys/param.h> 40*0Sstevel@tonic-gate #include <port_after.h> 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate #ifdef SERV_R_RETURN 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate static SERV_R_RETURN 45*0Sstevel@tonic-gate copy_servent(struct servent *, struct servent *, SERV_R_COPY_ARGS); 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate SERV_R_RETURN 48*0Sstevel@tonic-gate getservbyname_r(const char *name, const char *proto, 49*0Sstevel@tonic-gate struct servent *sptr, SERV_R_ARGS) { 50*0Sstevel@tonic-gate struct servent *se = getservbyname(name, proto); 51*0Sstevel@tonic-gate #ifdef SERV_R_SETANSWER 52*0Sstevel@tonic-gate int n = 0; 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0) 55*0Sstevel@tonic-gate *answerp = NULL; 56*0Sstevel@tonic-gate else 57*0Sstevel@tonic-gate *answerp = sptr; 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate return (n); 60*0Sstevel@tonic-gate #else 61*0Sstevel@tonic-gate if (se == NULL) 62*0Sstevel@tonic-gate return (SERV_R_BAD); 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate return (copy_servent(se, sptr, SERV_R_COPY)); 65*0Sstevel@tonic-gate #endif 66*0Sstevel@tonic-gate } 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate SERV_R_RETURN 69*0Sstevel@tonic-gate getservbyport_r(int port, const char *proto, 70*0Sstevel@tonic-gate struct servent *sptr, SERV_R_ARGS) { 71*0Sstevel@tonic-gate struct servent *se = getservbyport(port, proto); 72*0Sstevel@tonic-gate #ifdef SERV_R_SETANSWER 73*0Sstevel@tonic-gate int n = 0; 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0) 76*0Sstevel@tonic-gate *answerp = NULL; 77*0Sstevel@tonic-gate else 78*0Sstevel@tonic-gate *answerp = sptr; 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate return (n); 81*0Sstevel@tonic-gate #else 82*0Sstevel@tonic-gate if (se == NULL) 83*0Sstevel@tonic-gate return (SERV_R_BAD); 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate return (copy_servent(se, sptr, SERV_R_COPY)); 86*0Sstevel@tonic-gate #endif 87*0Sstevel@tonic-gate } 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate /* 90*0Sstevel@tonic-gate * These assume a single context is in operation per thread. 91*0Sstevel@tonic-gate * If this is not the case we will need to call irs directly 92*0Sstevel@tonic-gate * rather than through the base functions. 93*0Sstevel@tonic-gate */ 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate SERV_R_RETURN 96*0Sstevel@tonic-gate getservent_r(struct servent *sptr, SERV_R_ARGS) { 97*0Sstevel@tonic-gate struct servent *se = getservent(); 98*0Sstevel@tonic-gate #ifdef SERV_R_SETANSWER 99*0Sstevel@tonic-gate int n = 0; 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0) 102*0Sstevel@tonic-gate *answerp = NULL; 103*0Sstevel@tonic-gate else 104*0Sstevel@tonic-gate *answerp = sptr; 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate return (n); 107*0Sstevel@tonic-gate #else 108*0Sstevel@tonic-gate if (se == NULL) 109*0Sstevel@tonic-gate return (SERV_R_BAD); 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate return (copy_servent(se, sptr, SERV_R_COPY)); 112*0Sstevel@tonic-gate #endif 113*0Sstevel@tonic-gate } 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate SERV_R_SET_RETURN 116*0Sstevel@tonic-gate #ifdef SERV_R_ENT_ARGS 117*0Sstevel@tonic-gate setservent_r(int stay_open, SERV_R_ENT_ARGS) 118*0Sstevel@tonic-gate #else 119*0Sstevel@tonic-gate setservent_r(int stay_open) 120*0Sstevel@tonic-gate #endif 121*0Sstevel@tonic-gate { 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate setservent(stay_open); 124*0Sstevel@tonic-gate #ifdef SERV_R_SET_RESULT 125*0Sstevel@tonic-gate return (SERV_R_SET_RESULT); 126*0Sstevel@tonic-gate #endif 127*0Sstevel@tonic-gate } 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate SERV_R_END_RETURN 130*0Sstevel@tonic-gate #ifdef SERV_R_ENT_ARGS 131*0Sstevel@tonic-gate endservent_r(SERV_R_ENT_ARGS) 132*0Sstevel@tonic-gate #else 133*0Sstevel@tonic-gate endservent_r() 134*0Sstevel@tonic-gate #endif 135*0Sstevel@tonic-gate { 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate endservent(); 138*0Sstevel@tonic-gate SERV_R_END_RESULT(SERV_R_OK); 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate /* Private */ 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate #ifndef SERVENT_DATA 144*0Sstevel@tonic-gate static SERV_R_RETURN 145*0Sstevel@tonic-gate copy_servent(struct servent *se, struct servent *sptr, SERV_R_COPY_ARGS) { 146*0Sstevel@tonic-gate char *cp; 147*0Sstevel@tonic-gate int i, n; 148*0Sstevel@tonic-gate int numptr, len; 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate /* Find out the amount of space required to store the answer. */ 151*0Sstevel@tonic-gate numptr = 1; /* NULL ptr */ 152*0Sstevel@tonic-gate len = (char *)ALIGN(buf) - buf; 153*0Sstevel@tonic-gate for (i = 0; se->s_aliases[i]; i++, numptr++) { 154*0Sstevel@tonic-gate len += strlen(se->s_aliases[i]) + 1; 155*0Sstevel@tonic-gate } 156*0Sstevel@tonic-gate len += strlen(se->s_name) + 1; 157*0Sstevel@tonic-gate len += strlen(se->s_proto) + 1; 158*0Sstevel@tonic-gate len += numptr * sizeof(char*); 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate if (len > (int)buflen) { 161*0Sstevel@tonic-gate errno = ERANGE; 162*0Sstevel@tonic-gate return (SERV_R_BAD); 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate /* copy port value */ 166*0Sstevel@tonic-gate sptr->s_port = se->s_port; 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate cp = (char *)ALIGN(buf) + numptr * sizeof(char *); 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate /* copy official name */ 171*0Sstevel@tonic-gate n = strlen(se->s_name) + 1; 172*0Sstevel@tonic-gate strcpy(cp, se->s_name); 173*0Sstevel@tonic-gate sptr->s_name = cp; 174*0Sstevel@tonic-gate cp += n; 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate /* copy aliases */ 177*0Sstevel@tonic-gate sptr->s_aliases = (char **)ALIGN(buf); 178*0Sstevel@tonic-gate for (i = 0 ; se->s_aliases[i]; i++) { 179*0Sstevel@tonic-gate n = strlen(se->s_aliases[i]) + 1; 180*0Sstevel@tonic-gate strcpy(cp, se->s_aliases[i]); 181*0Sstevel@tonic-gate sptr->s_aliases[i] = cp; 182*0Sstevel@tonic-gate cp += n; 183*0Sstevel@tonic-gate } 184*0Sstevel@tonic-gate sptr->s_aliases[i] = NULL; 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate /* copy proto */ 187*0Sstevel@tonic-gate n = strlen(se->s_proto) + 1; 188*0Sstevel@tonic-gate strcpy(cp, se->s_proto); 189*0Sstevel@tonic-gate sptr->s_proto = cp; 190*0Sstevel@tonic-gate cp += n; 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate return (SERV_R_OK); 193*0Sstevel@tonic-gate } 194*0Sstevel@tonic-gate #else /* !SERVENT_DATA */ 195*0Sstevel@tonic-gate static int 196*0Sstevel@tonic-gate copy_servent(struct servent *se, struct servent *sptr, SERV_R_COPY_ARGS) { 197*0Sstevel@tonic-gate char *cp, *eob; 198*0Sstevel@tonic-gate int i, n; 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate /* copy port value */ 201*0Sstevel@tonic-gate sptr->s_port = se->s_port; 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate /* copy official name */ 204*0Sstevel@tonic-gate cp = ndptr->line; 205*0Sstevel@tonic-gate eob = ndptr->line + sizeof(ndptr->line); 206*0Sstevel@tonic-gate if ((n = strlen(se->s_name) + 1) < (eob - cp)) { 207*0Sstevel@tonic-gate strcpy(cp, se->s_name); 208*0Sstevel@tonic-gate sptr->s_name = cp; 209*0Sstevel@tonic-gate cp += n; 210*0Sstevel@tonic-gate } else { 211*0Sstevel@tonic-gate return (-1); 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate /* copy aliases */ 215*0Sstevel@tonic-gate i = 0; 216*0Sstevel@tonic-gate sptr->s_aliases = ndptr->serv_aliases; 217*0Sstevel@tonic-gate while (se->s_aliases[i] && i < (_MAXALIASES-1)) { 218*0Sstevel@tonic-gate if ((n = strlen(se->s_aliases[i]) + 1) < (eob - cp)) { 219*0Sstevel@tonic-gate strcpy(cp, se->s_aliases[i]); 220*0Sstevel@tonic-gate sptr->s_aliases[i] = cp; 221*0Sstevel@tonic-gate cp += n; 222*0Sstevel@tonic-gate } else { 223*0Sstevel@tonic-gate break; 224*0Sstevel@tonic-gate } 225*0Sstevel@tonic-gate i++; 226*0Sstevel@tonic-gate } 227*0Sstevel@tonic-gate sptr->s_aliases[i] = NULL; 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate /* copy proto */ 230*0Sstevel@tonic-gate if ((n = strlen(se->s_proto) + 1) < (eob - cp)) { 231*0Sstevel@tonic-gate strcpy(cp, se->s_proto); 232*0Sstevel@tonic-gate sptr->s_proto = cp; 233*0Sstevel@tonic-gate cp += n; 234*0Sstevel@tonic-gate } else { 235*0Sstevel@tonic-gate return (-1); 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate return (SERV_R_OK); 239*0Sstevel@tonic-gate } 240*0Sstevel@tonic-gate #endif /* !SERVENT_DATA */ 241*0Sstevel@tonic-gate #else /*SERV_R_RETURN */ 242*0Sstevel@tonic-gate static int getservent_r_unknown_system = 0; 243*0Sstevel@tonic-gate #endif /*SERV_R_RETURN */ 244*0Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */ 245