pt_tcp.c revision 1.17 1 /* $NetBSD: pt_tcp.c,v 1.17 2003/08/07 10:04:31 agc 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * from: Id: pt_tcp.c,v 1.1 1992/05/25 21:43:09 jsp Exp
35 * @(#)pt_tcp.c 8.5 (Berkeley) 4/28/95
36 */
37
38 #include <sys/cdefs.h>
39 #ifndef lint
40 __RCSID("$NetBSD: pt_tcp.c,v 1.17 2003/08/07 10:04:31 agc Exp $");
41 #endif /* not lint */
42
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <string.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
66 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 int priv = 0;
78 #ifdef INET6
79 struct addrinfo hints, *res, *lres;
80 int so = -1;
81 const char *cause = "unknown";
82 #else /* ! INET6 */
83 struct hostent *hp;
84 struct servent *sp;
85 struct in_addr **ipp;
86 struct in_addr *ip[2];
87 struct in_addr ina;
88 int s_port;
89 struct sockaddr_in sain;
90 #endif
91
92 q = strchr(p, '/');
93 if (q == 0 || q - p >= sizeof(host))
94 return (EINVAL);
95 *q = '\0';
96 if (strlcpy(host, p, sizeof(host)) >= sizeof(host))
97 return (EINVAL);
98 p = q + 1;
99
100 q = strchr(p, '/');
101 if (q)
102 *q = '\0';
103 if (strlcpy(port, p, sizeof(port)) >= sizeof(port))
104 return (EINVAL);
105 if (q) {
106 p = q + 1;
107 if (strcmp(p, "priv") == 0) {
108 if (pcr->pcr_uid == 0)
109 priv = 1;
110 else
111 return (EPERM);
112 } else {
113 return (EINVAL);
114 }
115 }
116
117 #ifdef INET6
118 memset(&hints, 0, sizeof(hints));
119 hints.ai_family = PF_UNSPEC;
120 hints.ai_socktype = SOCK_STREAM;
121 hints.ai_protocol = 0;
122 if (getaddrinfo(host, port, &hints, &res) != 0)
123 return(EINVAL);
124
125 for (lres = res; lres; lres = lres->ai_next) {
126 if (priv)
127 so = rresvport((int *) 0);
128 else
129 so = socket(lres->ai_family, lres->ai_socktype,
130 lres->ai_protocol);
131 if (so < 0) {
132 cause = "socket";
133 continue;
134 }
135
136 if (connect(so, lres->ai_addr, lres->ai_addrlen) != 0) {
137 cause = "connect";
138 (void)close(so);
139 so = -1;
140 continue;
141 }
142
143 *fdp = so;
144 errno = 0;
145 break;
146 }
147
148 if (so < 0)
149 syslog(LOG_WARNING, "%s: %m", cause);
150
151 freeaddrinfo(res);
152 #else /* ! INET6 */
153 if (inet_aton(host, &ina) == 0) {
154 hp = gethostbyname(host);
155 if (hp == 0)
156 return (EINVAL);
157 ipp = (struct in_addr **) hp->h_addr_list;
158 } else {
159 ip[0] = &ina;
160 ip[1] = 0;
161 ipp = ip;
162 }
163
164 sp = getservbyname(port, "tcp");
165 if (sp != 0)
166 s_port = sp->s_port;
167 else {
168 s_port = strtoul(port, &p, 0);
169 if (s_port == 0 || *p != '\0')
170 return (EINVAL);
171 s_port = htons(s_port);
172 }
173
174 memset(&sain, 0, sizeof(sain));
175 sain.sin_len = sizeof(sain);
176 sain.sin_family = AF_INET;
177 sain.sin_port = s_port;
178
179 while (ipp[0]) {
180 int so;
181
182 if (priv)
183 so = rresvport((int *) 0);
184 else
185 so = socket(AF_INET, SOCK_STREAM, 0);
186 if (so < 0) {
187 syslog(LOG_WARNING, "socket: %m");
188 return (errno);
189 }
190
191 sain.sin_addr = *ipp[0];
192 if (connect(so, (struct sockaddr *) &sain,
193 sizeof(sain)) == 0) {
194 *fdp = so;
195 return (0);
196 }
197 (void) close(so);
198
199 ipp++;
200 }
201 #endif /* INET6 */
202
203 return (errno);
204 }
205