1/* 2 * Copyright (c) 1992, 1993 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 * Modified by Duncan Barclay. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)pt_tcp.c 8.3 (Berkeley) 3/27/94 36 * 37 * pt_tcp.c,v 1.1.1.1 1994/05/26 06:34:34 rgrimes Exp 38 */ 39 40#include <sys/cdefs.h> 41__FBSDID("$FreeBSD$"); 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 tcplisten/host/port 60 * 61 * Create a TCP socket bound to the requested host and port. 62 * If the host is "ANY" the receving address will be set to INADDR_ANY. 63 * If the port is 0 the caller must find out the returned port number 64 * using a call to getsockname. 65 * 66 * XXX! The owner of the socket will be root rather then the user. This 67 * may cause remote auth (identd) to return unexpected results. 68 * 69 */ 70int portal_tcplisten(struct portal_cred *pcr, char *key, char **v, 71 int kso __unused, 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 = NULL; 80 struct in_addr *ip[2]; 81 struct in_addr ina; 82 u_short s_port; 83 int any = 0; 84 struct sockaddr_in sain; 85 86 q = strchr(p, '/'); 87 if (q == 0 || q - p >= (int)sizeof(host)) 88 return (EINVAL); 89 *q = '\0'; 90 snprintf(host, sizeof(host), "%s", 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 snprintf(port, sizeof(port), "%s", p); 99 100 if (strcmp(host, "ANY") == 0) { 101 any = 1; 102 } else { 103 hp = gethostbyname(host); 104 if (hp != 0) { 105 ipp = (struct in_addr **) hp->h_addr_list; 106 } else { 107 ina.s_addr = inet_addr(host); 108 if (ina.s_addr == INADDR_NONE) 109 return (EINVAL); 110 ip[0] = &ina; 111 ip[1] = 0; 112 ipp = ip; 113 } 114 } 115#ifdef DEBUG 116 if (any) 117 printf("INADDR_ANY to be used for hostname\n"); 118 else 119 printf("inet address for %s is %s\n", host, inet_ntoa(*ipp[0])); 120#endif 121 122 sp = getservbyname(port, "tcp"); 123 if (sp != NULL) { 124 s_port = (u_short) sp->s_port; 125 } else { 126 s_port = strtoul(port, &p, 0); 127 if (*p != '\0') 128 return (EINVAL); 129 s_port = htons(s_port); 130 } 131 if ((ntohs(s_port) != 0) && 132 (ntohs(s_port) <= IPPORT_RESERVED) && 133 (pcr->pcr_uid != 0)) 134 return (EPERM); 135#ifdef DEBUG 136 printf("port number for %s is %d\n", port, 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 if (any) { 145 int so; 146 int sock; 147 148 so = socket(AF_INET, SOCK_STREAM, 0); 149 if (so < 0) { 150 syslog(LOG_ERR, "socket: %m"); 151 return (errno); 152 } 153 154 sain.sin_addr.s_addr = INADDR_ANY; 155 if (bind(so, (struct sockaddr *) &sain, sizeof(sain)) == 0) { 156 listen(so, 1); 157 if ((sock = accept(so, (struct sockaddr *)0, (int *)0)) == -1) { 158 syslog(LOG_ERR, "accept: %m"); 159 (void) close(so); 160 return (errno); 161 } 162 *fdp = sock; 163 (void) close(so); 164 return (0); 165 } 166 syslog(LOG_ERR, "bind: %m"); 167 (void) close(so); 168 return (errno); 169 } 170 171 while (ipp[0]) { 172 int so; 173 int sock; 174 175 so = socket(AF_INET, SOCK_STREAM, 0); 176 if (so < 0) { 177 syslog(LOG_ERR, "socket: %m"); 178 return (errno); 179 } 180 181 sain.sin_addr = *ipp[0]; 182 if (bind(so, (struct sockaddr *) &sain, sizeof(sain)) == 0) { 183 listen(so, 1); 184 if ((sock = accept(so, (struct sockaddr *)0, (int *)0)) == -1) { 185 syslog(LOG_ERR, "accept: %m"); 186 (void) close(so); 187 return (errno); 188 } 189 *fdp = sock; 190 (void) close(so); 191 return (0); 192 } 193 (void) close(so); 194 195 ipp++; 196 } 197 198 syslog(LOG_ERR, "bind: %m"); 199 return (errno); 200 201} 202