Home | History | Annotate | Line # | Download | only in mount_portal
pt_tcp.c revision 1.12
      1 /*	$NetBSD: pt_tcp.c,v 1.12 1997/09/21 02:35:44 enami Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software donated to Berkeley by
      8  * Jan-Simon Pendry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	from: Id: pt_tcp.c,v 1.1 1992/05/25 21:43:09 jsp Exp
     39  *	@(#)pt_tcp.c	8.5 (Berkeley) 4/28/95
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 #ifndef lint
     44 __RCSID("$NetBSD: pt_tcp.c,v 1.12 1997/09/21 02:35:44 enami Exp $");
     45 #endif /* not lint */
     46 
     47 #include <stdio.h>
     48 #include <unistd.h>
     49 #include <stdlib.h>
     50 #include <errno.h>
     51 #include <strings.h>
     52 #include <sys/types.h>
     53 #include <sys/param.h>
     54 #include <sys/syslog.h>
     55 #include <sys/socket.h>
     56 #include <netinet/in.h>
     57 #include <arpa/inet.h>
     58 #include <netdb.h>
     59 
     60 #include "portald.h"
     61 
     62 /*
     63  * Key will be tcp/host/port[/"priv"]
     64  * Create a TCP socket connected to the
     65  * requested host and port.
     66  * Some trailing suffix values have special meanings.
     67  * An unrecognised suffix is an error.
     68  */
     69 int
     70 portal_tcp(pcr, key, v, kso, fdp)
     71 	struct portal_cred *pcr;
     72 	char *key;
     73 	char **v;
     74 	int kso;
     75 	int *fdp;
     76 {
     77 	char host[MAXHOSTNAMELEN];
     78 	char port[MAXHOSTNAMELEN];
     79 	char *p = key + (v[1] ? strlen(v[1]) : 0);
     80 	char *q;
     81 	struct hostent *hp;
     82 	struct servent *sp;
     83 	struct in_addr **ipp;
     84 	struct in_addr *ip[2];
     85 	struct in_addr ina;
     86 	int s_port;
     87 	int priv = 0;
     88 	struct sockaddr_in sain;
     89 
     90 	q = strchr(p, '/');
     91 	if (q == 0 || q - p >= sizeof(host))
     92 		return (EINVAL);
     93 	*q = '\0';
     94 	strcpy(host, p);
     95 	p = q + 1;
     96 
     97 	q = strchr(p, '/');
     98 	if (q)
     99 		*q = '\0';
    100 	if (strlen(p) >= sizeof(port))
    101 		return (EINVAL);
    102 	strcpy(port, p);
    103 	if (q) {
    104 		p = q + 1;
    105 		if (strcmp(p, "priv") == 0) {
    106 			if (pcr->pcr_uid == 0)
    107 				priv = 1;
    108 			else
    109 				return (EPERM);
    110 		} else {
    111 			return (EINVAL);
    112 		}
    113 	}
    114 
    115 	if (inet_aton(host, &ina) == 0) {
    116 		hp = gethostbyname(host);
    117 		if (hp == 0)
    118 			return (EINVAL);
    119 		ipp = (struct in_addr **) hp->h_addr_list;
    120 	} else {
    121 		ip[0] = &ina;
    122 		ip[1] = 0;
    123 		ipp = ip;
    124 	}
    125 
    126 	sp = getservbyname(port, "tcp");
    127 	if (sp != 0)
    128 		s_port = sp->s_port;
    129 	else {
    130 		s_port = strtoul(port, &p, 0);
    131 		if (s_port == 0 || *p != '\0')
    132 			return (EINVAL);
    133 		s_port = htons(s_port);
    134 	}
    135 
    136 	memset(&sain, 0, sizeof(sain));
    137 	sain.sin_len = sizeof(sain);
    138 	sain.sin_family = AF_INET;
    139 	sain.sin_port = s_port;
    140 
    141 	while (ipp[0]) {
    142 		int so;
    143 
    144 		if (priv)
    145 			so = rresvport((int *) 0);
    146 		else
    147 			so = socket(AF_INET, SOCK_STREAM, 0);
    148 		if (so < 0) {
    149 			syslog(LOG_ERR, "socket: %m");
    150 			return (errno);
    151 		}
    152 
    153 		sain.sin_addr = *ipp[0];
    154 		if (connect(so, (struct sockaddr *) &sain,
    155 		    sizeof(sain)) == 0) {
    156 			*fdp = so;
    157 			return (0);
    158 		}
    159 		(void) close(so);
    160 
    161 		ipp++;
    162 	}
    163 
    164 	return (errno);
    165 }
    166