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