Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sbin / mount_portal / 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  * 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  *      @(#)pt_tcp.c    8.5 (Berkeley) 4/28/95
38  */
39
40 #ifndef lint
41 static const char rcsid[] =
42   "$FreeBSD: src/sbin/mount_portal/pt_tcp.c,v 1.9 1999/08/28 00:13:38 peter Exp $";
43 #endif /* not lint */
44
45 #include <errno.h>
46 #include <stdlib.h>
47 #include <strings.h>
48 #include <unistd.h>
49 #include <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/syslog.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <netdb.h>
56
57 #include "portald.h"
58
59 /*
60  * Key will be tcp/host/port[/"priv"]
61  * Create a TCP socket connected to the
62  * requested host and port.
63  * Some trailing suffix values have special meanings.
64  * An unrecognized suffix is an error.
65  */
66 int portal_tcp(pcr, key, v, kso, fdp)
67         struct portal_cred *pcr;
68         char *key;
69         char **v;
70         int kso;
71         int *fdp;
72 {
73         char host[MAXHOSTNAMELEN];
74         char port[MAXHOSTNAMELEN];
75         char *p = key + (v[1] ? strlen(v[1]) : 0);
76         char *q;
77         struct hostent *hp;
78         struct servent *sp;
79         struct in_addr **ipp;
80         struct in_addr *ip[2];
81         struct in_addr ina;
82         u_short s_port;
83         int priv = 0;
84         struct sockaddr_in sain;
85
86         q = strchr(p, '/');
87         if (q == 0 || q - p >= sizeof(host))
88                 return (EINVAL);
89         *q = '\0';
90         strcpy(host, p);
91         p = q + 1;
92
93         q = strchr(p, '/');
94         if (q)
95                 *q = '\0';
96         if (strlen(p) >= sizeof(port))
97                 return (EINVAL);
98         strcpy(port, p);
99         if (q) {
100                 p = q + 1;
101                 if (strcmp(p, "priv") == 0) {
102                         if (pcr->pcr_uid == 0)
103                                 priv = 1;
104                         else
105                                 return (EPERM);
106                 } else {
107                         return (EINVAL);
108                 }
109         }
110
111         hp = gethostbyname(host);
112         if (hp != 0) {
113                 ipp = (struct in_addr **) hp->h_addr_list;
114         } else {
115                 ina.s_addr = inet_addr(host);
116                 if (ina.s_addr == INADDR_NONE)
117                         return (EINVAL);
118                 ip[0] = &ina;
119                 ip[1] = 0;
120                 ipp = ip;
121         }
122 #ifdef DEBUG
123         printf ("inet address for %s is %s\n", host, inet_ntoa(*ipp[0]));
124 #endif
125
126         sp = getservbyname(port, "tcp");
127         if (sp != NULL) {
128                 s_port = (u_short)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 #ifdef DEBUG
136         printf ("port number for %s is %d\n", port, (int)ntohs(s_port));
137 #endif
138
139         memset(&sain, 0, sizeof(sain));
140         sain.sin_len = sizeof(sain);
141         sain.sin_family = AF_INET;
142         sain.sin_port = s_port;
143
144         while (ipp[0]) {
145                 int so;
146
147                 if (priv)
148                         so = rresvport((int *) 0);
149                 else
150                         so = socket(AF_INET, SOCK_STREAM, 0);
151                 if (so < 0) {
152                         syslog(LOG_ERR, "socket: %m");
153                         return (errno);
154                 }
155
156                 sain.sin_addr = *ipp[0];
157                 if (connect(so, (struct sockaddr *) &sain, sizeof(sain)) == 0) {
158                         *fdp = so;
159                         return (0);
160                 }
161                 (void) close(so);
162
163                 ipp++;
164         }
165
166         return (errno);
167 }