docs - Modernize swapcache(8)
[dragonfly.git] / sbin / mount_portal / pt_tcp.c
blob48c6dcd3dc0daeb7c7584e18a2981f027cfca79e
1 /*
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * All rights reserved.
6 * This code is derived from software donated to Berkeley by
7 * Jan-Simon Pendry.
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. 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.
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.
33 * @(#)pt_tcp.c 8.5 (Berkeley) 4/28/95
35 * $FreeBSD: src/sbin/mount_portal/pt_tcp.c,v 1.9 1999/08/28 00:13:38 peter Exp $
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/syslog.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <netdb.h>
50 #include "portald.h"
53 * Key will be tcp/host/port[/"priv"]
54 * Create a TCP socket connected to the
55 * requested host and port.
56 * Some trailing suffix values have special meanings.
57 * An unrecognized suffix is an error.
59 int
60 portal_tcp(struct portal_cred *pcr, char *key, char **v, int kso __unused,
61 int *fdp)
63 char host[MAXHOSTNAMELEN];
64 char port[MAXHOSTNAMELEN];
65 char *p = key + (v[1] ? strlen(v[1]) : 0);
66 char *q;
67 struct hostent *hp;
68 struct servent *sp;
69 struct in_addr **ipp;
70 struct in_addr *ip[2];
71 struct in_addr ina;
72 u_short s_port;
73 int priv = 0;
74 struct sockaddr_in sain;
76 q = strchr(p, '/');
77 if (q == NULL || (size_t)(q - p) >= sizeof(host))
78 return (EINVAL);
79 *q = '\0';
80 strcpy(host, p);
81 p = q + 1;
83 q = strchr(p, '/');
84 if (q)
85 *q = '\0';
86 if (strlen(p) >= sizeof(port))
87 return (EINVAL);
88 strcpy(port, p);
89 if (q) {
90 p = q + 1;
91 if (strcmp(p, "priv") == 0) {
92 if (pcr->pcr_uid == 0)
93 priv = 1;
94 else
95 return (EPERM);
96 } else {
97 return (EINVAL);
101 hp = gethostbyname(host);
102 if (hp != NULL) {
103 ipp = (struct in_addr **) hp->h_addr_list;
104 } else {
105 ina.s_addr = inet_addr(host);
106 if (ina.s_addr == INADDR_NONE)
107 return (EINVAL);
108 ip[0] = &ina;
109 ip[1] = NULL;
110 ipp = ip;
112 #ifdef DEBUG
113 printf ("inet address for %s is %s\n", host, inet_ntoa(*ipp[0]));
114 #endif
116 sp = getservbyname(port, "tcp");
117 if (sp != NULL) {
118 s_port = (u_short)sp->s_port;
119 } else {
120 s_port = strtoul(port, &p, 0);
121 if (s_port == 0 || *p != '\0')
122 return (EINVAL);
123 s_port = htons(s_port);
125 #ifdef DEBUG
126 printf ("port number for %s is %d\n", port, (int)ntohs(s_port));
127 #endif
129 memset(&sain, 0, sizeof(sain));
130 sain.sin_len = sizeof(sain);
131 sain.sin_family = AF_INET;
132 sain.sin_port = s_port;
134 while (ipp[0]) {
135 int so;
137 if (priv)
138 so = rresvport(NULL);
139 else
140 so = socket(AF_INET, SOCK_STREAM, 0);
141 if (so < 0) {
142 syslog(LOG_ERR, "socket: %m");
143 return (errno);
146 sain.sin_addr = *ipp[0];
147 if (connect(so, (struct sockaddr *) &sain, sizeof(sain)) == 0) {
148 *fdp = so;
149 return (0);
151 close(so);
153 ipp++;
156 return (errno);