1  | 
     | 
     | 
    /*	$OpenBSD: getservent.c,v 1.15 2015/09/14 07:38:38 guenther Exp $ */  | 
    
    
    2  | 
     | 
     | 
    /*  | 
    
    
    3  | 
     | 
     | 
     * Copyright (c) 1983, 1993  | 
    
    
    4  | 
     | 
     | 
     *	The Regents of the University of California.  All rights reserved.  | 
    
    
    5  | 
     | 
     | 
     *  | 
    
    
    6  | 
     | 
     | 
     * Redistribution and use in source and binary forms, with or without  | 
    
    
    7  | 
     | 
     | 
     * modification, are permitted provided that the following conditions  | 
    
    
    8  | 
     | 
     | 
     * are met:  | 
    
    
    9  | 
     | 
     | 
     * 1. Redistributions of source code must retain the above copyright  | 
    
    
    10  | 
     | 
     | 
     *    notice, this list of conditions and the following disclaimer.  | 
    
    
    11  | 
     | 
     | 
     * 2. Redistributions in binary form must reproduce the above copyright  | 
    
    
    12  | 
     | 
     | 
     *    notice, this list of conditions and the following disclaimer in the  | 
    
    
    13  | 
     | 
     | 
     *    documentation and/or other materials provided with the distribution.  | 
    
    
    14  | 
     | 
     | 
     * 3. Neither the name of the University nor the names of its contributors  | 
    
    
    15  | 
     | 
     | 
     *    may be used to endorse or promote products derived from this software  | 
    
    
    16  | 
     | 
     | 
     *    without specific prior written permission.  | 
    
    
    17  | 
     | 
     | 
     *  | 
    
    
    18  | 
     | 
     | 
     * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND  | 
    
    
    19  | 
     | 
     | 
     * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  | 
    
    
    20  | 
     | 
     | 
     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  | 
    
    
    21  | 
     | 
     | 
     * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE  | 
    
    
    22  | 
     | 
     | 
     * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  | 
    
    
    23  | 
     | 
     | 
     * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  | 
    
    
    24  | 
     | 
     | 
     * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  | 
    
    
    25  | 
     | 
     | 
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  | 
    
    
    26  | 
     | 
     | 
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  | 
    
    
    27  | 
     | 
     | 
     * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  | 
    
    
    28  | 
     | 
     | 
     * SUCH DAMAGE.  | 
    
    
    29  | 
     | 
     | 
     */  | 
    
    
    30  | 
     | 
     | 
     | 
    
    
    31  | 
     | 
     | 
    #include <sys/types.h>  | 
    
    
    32  | 
     | 
     | 
    #include <sys/socket.h>  | 
    
    
    33  | 
     | 
     | 
     | 
    
    
    34  | 
     | 
     | 
    #include <errno.h>  | 
    
    
    35  | 
     | 
     | 
    #include <limits.h>  | 
    
    
    36  | 
     | 
     | 
    #include <netdb.h>  | 
    
    
    37  | 
     | 
     | 
    #include <stdio.h>  | 
    
    
    38  | 
     | 
     | 
    #include <string.h>  | 
    
    
    39  | 
     | 
     | 
    #include <stdlib.h>  | 
    
    
    40  | 
     | 
     | 
     | 
    
    
    41  | 
     | 
     | 
    void  | 
    
    
    42  | 
     | 
     | 
    setservent_r(int f, struct servent_data *sd)  | 
    
    
    43  | 
     | 
     | 
    { | 
    
    
    44  | 
     | 
     | 
    	if (sd->fp == NULL)  | 
    
    
    45  | 
     | 
     | 
    		sd->fp = fopen(_PATH_SERVICES, "re" );  | 
    
    
    46  | 
     | 
     | 
    	else  | 
    
    
    47  | 
     | 
     | 
    		rewind(sd->fp);  | 
    
    
    48  | 
     | 
     | 
    	sd->stayopen |= f;  | 
    
    
    49  | 
     | 
     | 
    }  | 
    
    
    50  | 
     | 
     | 
    DEF_WEAK(setservent_r);  | 
    
    
    51  | 
     | 
     | 
     | 
    
    
    52  | 
     | 
     | 
    void  | 
    
    
    53  | 
     | 
     | 
    endservent_r(struct servent_data *sd)  | 
    
    
    54  | 
     | 
     | 
    { | 
    
    
    55  | 
     | 
     | 
    	if (sd->fp) { | 
    
    
    56  | 
     | 
     | 
    		fclose(sd->fp);  | 
    
    
    57  | 
     | 
     | 
    		sd->fp = NULL;  | 
    
    
    58  | 
     | 
     | 
    	}  | 
    
    
    59  | 
     | 
     | 
    	free(sd->aliases);  | 
    
    
    60  | 
     | 
     | 
    	sd->aliases = NULL;  | 
    
    
    61  | 
     | 
     | 
    	sd->maxaliases = 0;  | 
    
    
    62  | 
     | 
     | 
    	free(sd->line);  | 
    
    
    63  | 
     | 
     | 
    	sd->line = NULL;  | 
    
    
    64  | 
     | 
     | 
    	sd->stayopen = 0;  | 
    
    
    65  | 
     | 
     | 
    }  | 
    
    
    66  | 
     | 
     | 
    DEF_WEAK(endservent_r);  | 
    
    
    67  | 
     | 
     | 
     | 
    
    
    68  | 
     | 
     | 
    int  | 
    
    
    69  | 
     | 
     | 
    getservent_r(struct servent *se, struct servent_data *sd)  | 
    
    
    70  | 
     | 
     | 
    { | 
    
    
    71  | 
     | 
     | 
    	char *p, *cp, **q, *endp;  | 
    
    
    72  | 
     | 
     | 
    	size_t len;  | 
    
    
    73  | 
     | 
     | 
    	long l;  | 
    
    
    74  | 
     | 
     | 
    	int serrno;  | 
    
    
    75  | 
     | 
     | 
     | 
    
    
    76  | 
     | 
     | 
    	if (sd->fp == NULL && (sd->fp = fopen(_PATH_SERVICES, "re" )) == NULL)  | 
    
    
    77  | 
     | 
     | 
    		return (-1);  | 
    
    
    78  | 
     | 
     | 
    again:  | 
    
    
    79  | 
     | 
     | 
    	if ((p = fgetln(sd->fp, &len)) == NULL)  | 
    
    
    80  | 
     | 
     | 
    		return (-1);  | 
    
    
    81  | 
     | 
     | 
    	if (len == 0 || *p == '#' || *p == '\n')  | 
    
    
    82  | 
     | 
     | 
    		goto again;  | 
    
    
    83  | 
     | 
     | 
    	if (p[len-1] == '\n')  | 
    
    
    84  | 
     | 
     | 
    		len--;  | 
    
    
    85  | 
     | 
     | 
    	if ((cp = memchr(p, '#', len)) != NULL)  | 
    
    
    86  | 
     | 
     | 
    		len = cp - p;  | 
    
    
    87  | 
     | 
     | 
    	cp = realloc(sd->line, len + 1);  | 
    
    
    88  | 
     | 
     | 
    	if (cp == NULL)  | 
    
    
    89  | 
     | 
     | 
    		return (-1);  | 
    
    
    90  | 
     | 
     | 
    	sd->line = se->s_name = memcpy(cp, p, len);  | 
    
    
    91  | 
     | 
     | 
    	cp[len] = '\0';  | 
    
    
    92  | 
     | 
     | 
    	p = strpbrk(cp, " \t");  | 
    
    
    93  | 
     | 
     | 
    	if (p == NULL)  | 
    
    
    94  | 
     | 
     | 
    		goto again;  | 
    
    
    95  | 
     | 
     | 
    	*p++ = '\0';  | 
    
    
    96  | 
     | 
     | 
    	while (*p == ' ' || *p == '\t')  | 
    
    
    97  | 
     | 
     | 
    		p++;  | 
    
    
    98  | 
     | 
     | 
    	cp = strpbrk(p, ",/");  | 
    
    
    99  | 
     | 
     | 
    	if (cp == NULL)  | 
    
    
    100  | 
     | 
     | 
    		goto again;  | 
    
    
    101  | 
     | 
     | 
    	*cp++ = '\0';  | 
    
    
    102  | 
     | 
     | 
    	l = strtol(p, &endp, 10);  | 
    
    
    103  | 
     | 
     | 
    	if (endp == p || *endp != '\0' || l < 0 || l > USHRT_MAX)  | 
    
    
    104  | 
     | 
     | 
    		goto again;  | 
    
    
    105  | 
     | 
     | 
    	se->s_port = htons((in_port_t)l);  | 
    
    
    106  | 
     | 
     | 
    	se->s_proto = cp;  | 
    
    
    107  | 
     | 
     | 
    	if (sd->aliases == NULL) { | 
    
    
    108  | 
     | 
     | 
    		sd->maxaliases = 10;  | 
    
    
    109  | 
     | 
     | 
    		sd->aliases = calloc(sd->maxaliases, sizeof(char *));  | 
    
    
    110  | 
     | 
     | 
    		if (sd->aliases == NULL) { | 
    
    
    111  | 
     | 
     | 
    			serrno = errno;  | 
    
    
    112  | 
     | 
     | 
    			endservent_r(sd);  | 
    
    
    113  | 
     | 
     | 
    			errno = serrno;  | 
    
    
    114  | 
     | 
     | 
    			return (-1);  | 
    
    
    115  | 
     | 
     | 
    		}  | 
    
    
    116  | 
     | 
     | 
    	}  | 
    
    
    117  | 
     | 
     | 
    	q = se->s_aliases = sd->aliases;  | 
    
    
    118  | 
     | 
     | 
    	cp = strpbrk(cp, " \t");  | 
    
    
    119  | 
     | 
     | 
    	if (cp != NULL)  | 
    
    
    120  | 
     | 
     | 
    		*cp++ = '\0';  | 
    
    
    121  | 
     | 
     | 
    	while (cp && *cp) { | 
    
    
    122  | 
     | 
     | 
    		if (*cp == ' ' || *cp == '\t') { | 
    
    
    123  | 
     | 
     | 
    			cp++;  | 
    
    
    124  | 
     | 
     | 
    			continue;  | 
    
    
    125  | 
     | 
     | 
    		}  | 
    
    
    126  | 
     | 
     | 
    		if (q == &se->s_aliases[sd->maxaliases - 1]) { | 
    
    
    127  | 
     | 
     | 
    			p = reallocarray(se->s_aliases, sd->maxaliases,  | 
    
    
    128  | 
     | 
     | 
    			    2 * sizeof(char *));  | 
    
    
    129  | 
     | 
     | 
    			if (p == NULL) { | 
    
    
    130  | 
     | 
     | 
    				serrno = errno;  | 
    
    
    131  | 
     | 
     | 
    				endservent_r(sd);  | 
    
    
    132  | 
     | 
     | 
    				errno = serrno;  | 
    
    
    133  | 
     | 
     | 
    				return (-1);  | 
    
    
    134  | 
     | 
     | 
    			}  | 
    
    
    135  | 
     | 
     | 
    			sd->maxaliases *= 2;  | 
    
    
    136  | 
     | 
     | 
    			q = (char **)p + (q - se->s_aliases);  | 
    
    
    137  | 
     | 
     | 
    			se->s_aliases = sd->aliases = (char **)p;  | 
    
    
    138  | 
     | 
     | 
    		}  | 
    
    
    139  | 
     | 
     | 
    		*q++ = cp;  | 
    
    
    140  | 
     | 
     | 
    		cp = strpbrk(cp, " \t");  | 
    
    
    141  | 
     | 
     | 
    		if (cp != NULL)  | 
    
    
    142  | 
     | 
     | 
    			*cp++ = '\0';  | 
    
    
    143  | 
     | 
     | 
    	}  | 
    
    
    144  | 
     | 
     | 
    	*q = NULL;  | 
    
    
    145  | 
     | 
     | 
    	return (0);  | 
    
    
    146  | 
     | 
     | 
    }  | 
    
    
    147  | 
     | 
     | 
    DEF_WEAK(getservent_r);  | 
    
    
    148  | 
     | 
     | 
     | 
    
    
    149  | 
     | 
     | 
    struct servent_data _servent_data;	/* shared with getservby{name,port}.c */ | 
    
    
    150  | 
     | 
     | 
     | 
    
    
    151  | 
     | 
     | 
    void  | 
    
    
    152  | 
     | 
     | 
    setservent(int f)  | 
    
    
    153  | 
     | 
     | 
    { | 
    
    
    154  | 
     | 
     | 
    	setservent_r(f, &_servent_data);  | 
    
    
    155  | 
     | 
     | 
    }  | 
    
    
    156  | 
     | 
     | 
     | 
    
    
    157  | 
     | 
     | 
    void  | 
    
    
    158  | 
     | 
     | 
    endservent(void)  | 
    
    
    159  | 
     | 
     | 
    { | 
    
    
    160  | 
     | 
     | 
    	endservent_r(&_servent_data);  | 
    
    
    161  | 
     | 
     | 
    }  | 
    
    
    162  | 
     | 
     | 
     | 
    
    
    163  | 
     | 
     | 
    struct servent *  | 
    
    
    164  | 
     | 
     | 
    getservent(void)  | 
    
    
    165  | 
     | 
     | 
    { | 
    
    
    166  | 
     | 
     | 
    	static struct servent serv;  | 
    
    
    167  | 
     | 
     | 
     | 
    
    
    168  | 
     | 
     | 
    	if (getservent_r(&serv, &_servent_data) != 0)  | 
    
    
    169  | 
     | 
     | 
    		return (NULL);  | 
    
    
    170  | 
     | 
     | 
    	return (&serv);  | 
    
    
    171  | 
     | 
     | 
    }  |