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