cam - Fix bus registration race
[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. 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  * $FreeBSD: src/sbin/mount_portal/pt_tcp.c,v 1.9 1999/08/28 00:13:38 peter Exp $
36  */
37
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>
49
50 #include "portald.h"
51
52 /*
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.
58  */
59 int
60 portal_tcp(struct portal_cred *pcr, char *key, char **v, int kso __unused,
61     int *fdp)
62 {
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;
75
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;
82
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);
98                 }
99         }
100
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;
111         }
112 #ifdef DEBUG
113         printf ("inet address for %s is %s\n", host, inet_ntoa(*ipp[0]));
114 #endif
115
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);
124         }
125 #ifdef DEBUG
126         printf ("port number for %s is %d\n", port, (int)ntohs(s_port));
127 #endif
128
129         memset(&sain, 0, sizeof(sain));
130         sain.sin_len = sizeof(sain);
131         sain.sin_family = AF_INET;
132         sain.sin_port = s_port;
133
134         while (ipp[0]) {
135                 int so;
136
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);
144                 }
145
146                 sain.sin_addr = *ipp[0];
147                 if (connect(so, (struct sockaddr *) &sain, sizeof(sain)) == 0) {
148                         *fdp = so;
149                         return (0);
150                 }
151                 close(so);
152
153                 ipp++;
154         }
155
156         return (errno);
157 }