]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/usr.sbin/mount_portalfs/pt_tcp.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / usr.sbin / mount_portalfs / pt_tcp.c
1 /*
2  * Copyright (c) 1992, 1993, 1994
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  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)pt_tcp.c    8.5 (Berkeley) 4/28/95
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <errno.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/syslog.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <netdb.h>
50
51 #include "portald.h"
52
53 /*
54  * Key will be tcp/host/port[/"priv"]
55  * Create a TCP socket connected to the
56  * requested host and port.
57  * Some trailing suffix values have special meanings.
58  * An unrecognized suffix is an error.
59  */
60 int portal_tcp(pcr, key, v, kso, fdp)
61         struct portal_cred *pcr;
62         char *key;
63         char **v;
64         int kso;
65         int *fdp;
66 {
67         char host[MAXHOSTNAMELEN];
68         char port[MAXHOSTNAMELEN];
69         char *p = key + (v[1] ? strlen(v[1]) : 0);
70         char *q;
71         struct hostent *hp;
72         struct servent *sp;
73         struct in_addr **ipp;
74         struct in_addr *ip[2];
75         struct in_addr ina;
76         u_short s_port;
77         int priv = 0;
78         struct sockaddr_in sain;
79
80         q = strchr(p, '/');
81         if (q == 0 || q - p >= sizeof(host))
82                 return (EINVAL);
83         *q = '\0';
84         strcpy(host, p);
85         p = q + 1;
86
87         q = strchr(p, '/');
88         if (q)
89                 *q = '\0';
90         if (strlen(p) >= sizeof(port))
91                 return (EINVAL);
92         strcpy(port, p);
93         if (q) {
94                 p = q + 1;
95                 if (strcmp(p, "priv") == 0) {
96                         if (pcr->pcr_uid == 0)
97                                 priv = 1;
98                         else
99                                 return (EPERM);
100                 } else {
101                         return (EINVAL);
102                 }
103         }
104
105         hp = gethostbyname(host);
106         if (hp != 0) {
107                 ipp = (struct in_addr **) hp->h_addr_list;
108         } else {
109                 ina.s_addr = inet_addr(host);
110                 if (ina.s_addr == INADDR_NONE)
111                         return (EINVAL);
112                 ip[0] = &ina;
113                 ip[1] = 0;
114                 ipp = ip;
115         }
116 #ifdef DEBUG
117         printf ("inet address for %s is %s\n", host, inet_ntoa(*ipp[0]));
118 #endif
119
120         sp = getservbyname(port, "tcp");
121         if (sp != NULL) {
122                 s_port = (u_short)sp->s_port;
123         } else {
124                 s_port = strtoul(port, &p, 0);
125                 if (s_port == 0 || *p != '\0')
126                         return (EINVAL);
127                 s_port = htons(s_port);
128         }
129 #ifdef DEBUG
130         printf ("port number for %s is %d\n", port, (int)ntohs(s_port));
131 #endif
132
133         memset(&sain, 0, sizeof(sain));
134         sain.sin_len = sizeof(sain);
135         sain.sin_family = AF_INET;
136         sain.sin_port = s_port;
137
138         while (ipp[0]) {
139                 int so;
140
141                 if (priv)
142                         so = rresvport((int *) 0);
143                 else
144                         so = socket(AF_INET, SOCK_STREAM, 0);
145                 if (so < 0) {
146                         syslog(LOG_ERR, "socket: %m");
147                         return (errno);
148                 }
149
150                 sain.sin_addr = *ipp[0];
151                 if (connect(so, (struct sockaddr *) &sain, sizeof(sain)) == 0) {
152                         *fdp = so;
153                         return (0);
154                 }
155                 (void) close(so);
156
157                 ipp++;
158         }
159
160         return (errno);
161 }