diff options
Diffstat (limited to 'src/usr.bin/openssl/s_socket.c')
| -rw-r--r-- | src/usr.bin/openssl/s_socket.c | 351 |
1 files changed, 351 insertions, 0 deletions
diff --git a/src/usr.bin/openssl/s_socket.c b/src/usr.bin/openssl/s_socket.c new file mode 100644 index 0000000000..48af178a23 --- /dev/null +++ b/src/usr.bin/openssl/s_socket.c | |||
| @@ -0,0 +1,351 @@ | |||
| 1 | /* $OpenBSD: s_socket.c,v 1.1 2014/08/26 17:47:25 jsing Exp $ */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <sys/socket.h> | ||
| 60 | |||
| 61 | #include <netinet/in.h> | ||
| 62 | |||
| 63 | #include <errno.h> | ||
| 64 | #include <netdb.h> | ||
| 65 | #include <stdio.h> | ||
| 66 | #include <stdlib.h> | ||
| 67 | #include <string.h> | ||
| 68 | #include <unistd.h> | ||
| 69 | |||
| 70 | #include "apps.h" | ||
| 71 | |||
| 72 | #include <openssl/ssl.h> | ||
| 73 | |||
| 74 | #include "s_apps.h" | ||
| 75 | |||
| 76 | static int ssl_sock_init(void); | ||
| 77 | static int init_server(int *sock, int port, int type); | ||
| 78 | static int init_server_long(int *sock, int port, char *ip, int type); | ||
| 79 | static int do_accept(int acc_sock, int *sock, char **host); | ||
| 80 | |||
| 81 | #define SOCKET_PROTOCOL IPPROTO_TCP | ||
| 82 | |||
| 83 | static int | ||
| 84 | ssl_sock_init(void) | ||
| 85 | { | ||
| 86 | return (1); | ||
| 87 | } | ||
| 88 | |||
| 89 | int | ||
| 90 | init_client(int *sock, char *host, char *port, int type, int af) | ||
| 91 | { | ||
| 92 | struct addrinfo hints, *ai_top, *ai; | ||
| 93 | int i, s; | ||
| 94 | |||
| 95 | if (!ssl_sock_init()) | ||
| 96 | return (0); | ||
| 97 | |||
| 98 | memset(&hints, '\0', sizeof(hints)); | ||
| 99 | hints.ai_family = af; | ||
| 100 | hints.ai_socktype = type; | ||
| 101 | |||
| 102 | if ((i = getaddrinfo(host, port, &hints, &ai_top)) != 0) { | ||
| 103 | BIO_printf(bio_err, "getaddrinfo: %s\n", gai_strerror(i)); | ||
| 104 | return (0); | ||
| 105 | } | ||
| 106 | if (ai_top == NULL || ai_top->ai_addr == NULL) { | ||
| 107 | BIO_printf(bio_err, "getaddrinfo returned no addresses\n"); | ||
| 108 | if (ai_top != NULL) { | ||
| 109 | freeaddrinfo(ai_top); | ||
| 110 | } | ||
| 111 | return (0); | ||
| 112 | } | ||
| 113 | for (ai = ai_top; ai != NULL; ai = ai->ai_next) { | ||
| 114 | s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); | ||
| 115 | if (s == -1) { | ||
| 116 | continue; | ||
| 117 | } | ||
| 118 | if (type == SOCK_STREAM) { | ||
| 119 | i = 0; | ||
| 120 | i = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, | ||
| 121 | (char *) &i, sizeof(i)); | ||
| 122 | if (i < 0) { | ||
| 123 | perror("keepalive"); | ||
| 124 | close(s); | ||
| 125 | return (0); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | if ((i = connect(s, ai->ai_addr, ai->ai_addrlen)) == 0) { | ||
| 129 | *sock = s; | ||
| 130 | freeaddrinfo(ai_top); | ||
| 131 | return (1); | ||
| 132 | } | ||
| 133 | close(s); | ||
| 134 | } | ||
| 135 | |||
| 136 | perror("connect"); | ||
| 137 | close(s); | ||
| 138 | freeaddrinfo(ai_top); | ||
| 139 | return (0); | ||
| 140 | } | ||
| 141 | |||
| 142 | int | ||
| 143 | do_server(int port, int type, int *ret, | ||
| 144 | int (*cb) (char *hostname, int s, unsigned char *context), | ||
| 145 | unsigned char *context) | ||
| 146 | { | ||
| 147 | int sock; | ||
| 148 | char *name = NULL; | ||
| 149 | int accept_socket = 0; | ||
| 150 | int i; | ||
| 151 | |||
| 152 | if (!init_server(&accept_socket, port, type)) | ||
| 153 | return (0); | ||
| 154 | |||
| 155 | if (ret != NULL) { | ||
| 156 | *ret = accept_socket; | ||
| 157 | /* return(1); */ | ||
| 158 | } | ||
| 159 | for (;;) { | ||
| 160 | if (type == SOCK_STREAM) { | ||
| 161 | if (do_accept(accept_socket, &sock, &name) == 0) { | ||
| 162 | shutdown(accept_socket, SHUT_RD); | ||
| 163 | close(accept_socket); | ||
| 164 | return (0); | ||
| 165 | } | ||
| 166 | } else | ||
| 167 | sock = accept_socket; | ||
| 168 | i = (*cb) (name, sock, context); | ||
| 169 | free(name); | ||
| 170 | if (type == SOCK_STREAM) { | ||
| 171 | shutdown(sock, SHUT_RDWR); | ||
| 172 | close(sock); | ||
| 173 | } | ||
| 174 | if (i < 0) { | ||
| 175 | shutdown(accept_socket, SHUT_RDWR); | ||
| 176 | close(accept_socket); | ||
| 177 | return (i); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | static int | ||
| 183 | init_server_long(int *sock, int port, char *ip, int type) | ||
| 184 | { | ||
| 185 | int ret = 0; | ||
| 186 | struct sockaddr_in server; | ||
| 187 | int s = -1; | ||
| 188 | |||
| 189 | if (!ssl_sock_init()) | ||
| 190 | return (0); | ||
| 191 | |||
| 192 | memset((char *) &server, 0, sizeof(server)); | ||
| 193 | server.sin_family = AF_INET; | ||
| 194 | server.sin_port = htons((unsigned short) port); | ||
| 195 | if (ip == NULL) | ||
| 196 | server.sin_addr.s_addr = INADDR_ANY; | ||
| 197 | else | ||
| 198 | memcpy(&server.sin_addr.s_addr, ip, 4); | ||
| 199 | |||
| 200 | if (type == SOCK_STREAM) | ||
| 201 | s = socket(AF_INET, SOCK_STREAM, SOCKET_PROTOCOL); | ||
| 202 | else /* type == SOCK_DGRAM */ | ||
| 203 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | ||
| 204 | |||
| 205 | if (s == -1) | ||
| 206 | goto err; | ||
| 207 | #if defined SOL_SOCKET && defined SO_REUSEADDR | ||
| 208 | { | ||
| 209 | int j = 1; | ||
| 210 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, | ||
| 211 | (void *) &j, sizeof j); | ||
| 212 | } | ||
| 213 | #endif | ||
| 214 | if (bind(s, (struct sockaddr *) & server, sizeof(server)) == -1) { | ||
| 215 | perror("bind"); | ||
| 216 | goto err; | ||
| 217 | } | ||
| 218 | /* Make it 128 for linux */ | ||
| 219 | if (type == SOCK_STREAM && listen(s, 128) == -1) | ||
| 220 | goto err; | ||
| 221 | *sock = s; | ||
| 222 | ret = 1; | ||
| 223 | err: | ||
| 224 | if ((ret == 0) && (s != -1)) { | ||
| 225 | shutdown(s, SHUT_RD); | ||
| 226 | close(s); | ||
| 227 | } | ||
| 228 | return (ret); | ||
| 229 | } | ||
| 230 | |||
| 231 | static int | ||
| 232 | init_server(int *sock, int port, int type) | ||
| 233 | { | ||
| 234 | return (init_server_long(sock, port, NULL, type)); | ||
| 235 | } | ||
| 236 | |||
| 237 | static int | ||
| 238 | do_accept(int acc_sock, int *sock, char **host) | ||
| 239 | { | ||
| 240 | int ret; | ||
| 241 | struct hostent *h1, *h2; | ||
| 242 | static struct sockaddr_in from; | ||
| 243 | socklen_t len; | ||
| 244 | /* struct linger ling; */ | ||
| 245 | |||
| 246 | if (!ssl_sock_init()) | ||
| 247 | return (0); | ||
| 248 | |||
| 249 | redoit: | ||
| 250 | |||
| 251 | memset((char *) &from, 0, sizeof(from)); | ||
| 252 | len = sizeof(from); | ||
| 253 | ret = accept(acc_sock, (struct sockaddr *) & from, &len); | ||
| 254 | if (ret == -1) { | ||
| 255 | if (errno == EINTR) { | ||
| 256 | /* check_timeout(); */ | ||
| 257 | goto redoit; | ||
| 258 | } | ||
| 259 | fprintf(stderr, "errno=%d ", errno); | ||
| 260 | perror("accept"); | ||
| 261 | return (0); | ||
| 262 | } | ||
| 263 | /* | ||
| 264 | ling.l_onoff=1; | ||
| 265 | ling.l_linger=0; | ||
| 266 | i=setsockopt(ret,SOL_SOCKET,SO_LINGER,(char *)&ling,sizeof(ling)); | ||
| 267 | if (i < 0) { perror("linger"); return(0); } | ||
| 268 | i=0; | ||
| 269 | i=setsockopt(ret,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i)); | ||
| 270 | if (i < 0) { perror("keepalive"); return(0); } | ||
| 271 | */ | ||
| 272 | |||
| 273 | if (host == NULL) | ||
| 274 | goto end; | ||
| 275 | h1 = gethostbyaddr((char *) &from.sin_addr.s_addr, | ||
| 276 | sizeof(from.sin_addr.s_addr), AF_INET); | ||
| 277 | if (h1 == NULL) { | ||
| 278 | BIO_printf(bio_err, "bad gethostbyaddr\n"); | ||
| 279 | *host = NULL; | ||
| 280 | /* return(0); */ | ||
| 281 | } else { | ||
| 282 | if ((*host = strdup(h1->h_name)) == NULL) { | ||
| 283 | perror("strdup"); | ||
| 284 | close(ret); | ||
| 285 | return (0); | ||
| 286 | } | ||
| 287 | |||
| 288 | h2 = gethostbyname(*host); | ||
| 289 | if (h2 == NULL) { | ||
| 290 | BIO_printf(bio_err, "gethostbyname failure\n"); | ||
| 291 | close(ret); | ||
| 292 | return (0); | ||
| 293 | } | ||
| 294 | if (h2->h_addrtype != AF_INET) { | ||
| 295 | BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n"); | ||
| 296 | close(ret); | ||
| 297 | return (0); | ||
| 298 | } | ||
| 299 | } | ||
| 300 | |||
| 301 | end: | ||
| 302 | *sock = ret; | ||
| 303 | return (1); | ||
| 304 | } | ||
| 305 | |||
| 306 | int | ||
| 307 | extract_host_port(char *str, char **host_ptr, unsigned char *ip, | ||
| 308 | char **port_ptr) | ||
| 309 | { | ||
| 310 | char *h, *p; | ||
| 311 | |||
| 312 | h = str; | ||
| 313 | p = strrchr(str, '/'); /* IPv6 host/port */ | ||
| 314 | if (p == NULL) { | ||
| 315 | p = strrchr(str, ':'); | ||
| 316 | } | ||
| 317 | if (p == NULL) { | ||
| 318 | BIO_printf(bio_err, "no port defined\n"); | ||
| 319 | return (0); | ||
| 320 | } | ||
| 321 | *(p++) = '\0'; | ||
| 322 | |||
| 323 | if (host_ptr != NULL) | ||
| 324 | *host_ptr = h; | ||
| 325 | |||
| 326 | if (port_ptr != NULL && p != NULL && *p != '\0') | ||
| 327 | *port_ptr = p; | ||
| 328 | |||
| 329 | return (1); | ||
| 330 | } | ||
| 331 | |||
| 332 | int | ||
| 333 | extract_port(char *str, short *port_ptr) | ||
| 334 | { | ||
| 335 | int i; | ||
| 336 | const char *errstr; | ||
| 337 | struct servent *s; | ||
| 338 | |||
| 339 | i = strtonum(str, 1, 65535, &errstr); | ||
| 340 | if (!errstr) { | ||
| 341 | *port_ptr = (unsigned short) i; | ||
| 342 | } else { | ||
| 343 | s = getservbyname(str, "tcp"); | ||
| 344 | if (s == NULL) { | ||
| 345 | BIO_printf(bio_err, "getservbyname failure for %s\n", str); | ||
| 346 | return (0); | ||
| 347 | } | ||
| 348 | *port_ptr = ntohs((unsigned short) s->s_port); | ||
| 349 | } | ||
| 350 | return (1); | ||
| 351 | } | ||
