1b9cbc85dSRick Macklem /*- 2b9cbc85dSRick Macklem * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3b9cbc85dSRick Macklem * 4b9cbc85dSRick Macklem * Copyright (c) 2008 Isilon Inc http://www.isilon.com/ 5b9cbc85dSRick Macklem * Authors: Doug Rabson <dfr@rabson.org> 6b9cbc85dSRick Macklem * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org> 7b9cbc85dSRick Macklem * 8b9cbc85dSRick Macklem * Redistribution and use in source and binary forms, with or without 9b9cbc85dSRick Macklem * modification, are permitted provided that the following conditions 10b9cbc85dSRick Macklem * are met: 11b9cbc85dSRick Macklem * 1. Redistributions of source code must retain the above copyright 12b9cbc85dSRick Macklem * notice, this list of conditions and the following disclaimer. 13b9cbc85dSRick Macklem * 2. Redistributions in binary form must reproduce the above copyright 14b9cbc85dSRick Macklem * notice, this list of conditions and the following disclaimer in the 15b9cbc85dSRick Macklem * documentation and/or other materials provided with the distribution. 16b9cbc85dSRick Macklem * 17b9cbc85dSRick Macklem * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18b9cbc85dSRick Macklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19b9cbc85dSRick Macklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20b9cbc85dSRick Macklem * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21b9cbc85dSRick Macklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22b9cbc85dSRick Macklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23b9cbc85dSRick Macklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24b9cbc85dSRick Macklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25b9cbc85dSRick Macklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26b9cbc85dSRick Macklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27b9cbc85dSRick Macklem * SUCH DAMAGE. 28b9cbc85dSRick Macklem */ 29b9cbc85dSRick Macklem 30b9cbc85dSRick Macklem /* 31b9cbc85dSRick Macklem * Extensively modified from /usr/src/usr.sbin/gssd.c r344402 for 32b9cbc85dSRick Macklem * the server side of kernel RPC-over-TLS by Rick Macklem. 33b9cbc85dSRick Macklem */ 34b9cbc85dSRick Macklem 35b9cbc85dSRick Macklem #include <sys/cdefs.h> 36b9cbc85dSRick Macklem __FBSDID("$FreeBSD$"); 37b9cbc85dSRick Macklem 38b9cbc85dSRick Macklem #include <sys/param.h> 39b9cbc85dSRick Macklem #include <sys/types.h> 40b9cbc85dSRick Macklem #include <sys/linker.h> 41b9cbc85dSRick Macklem #include <sys/module.h> 42b9cbc85dSRick Macklem #include <sys/queue.h> 43b9cbc85dSRick Macklem #include <sys/stat.h> 44b9cbc85dSRick Macklem #include <sys/sysctl.h> 45b9cbc85dSRick Macklem #include <sys/syslog.h> 46b9cbc85dSRick Macklem #include <sys/time.h> 47b9cbc85dSRick Macklem #include <err.h> 48b9cbc85dSRick Macklem #include <getopt.h> 49b9cbc85dSRick Macklem #include <libutil.h> 50b9cbc85dSRick Macklem #include <netdb.h> 51b9cbc85dSRick Macklem #include <pwd.h> 52b9cbc85dSRick Macklem #include <signal.h> 53b9cbc85dSRick Macklem #include <stdarg.h> 54b9cbc85dSRick Macklem #include <stdio.h> 55b9cbc85dSRick Macklem #include <stdlib.h> 56b9cbc85dSRick Macklem #include <stdbool.h> 57b9cbc85dSRick Macklem #include <string.h> 58b9cbc85dSRick Macklem #include <unistd.h> 59b9cbc85dSRick Macklem 60b9cbc85dSRick Macklem #include <rpc/rpc.h> 61b9cbc85dSRick Macklem #include <rpc/rpc_com.h> 62b9cbc85dSRick Macklem #include <rpc/rpcsec_tls.h> 63b9cbc85dSRick Macklem 64b9cbc85dSRick Macklem #include <openssl/opensslconf.h> 65b9cbc85dSRick Macklem #include <openssl/bio.h> 66b9cbc85dSRick Macklem #include <openssl/ssl.h> 67b9cbc85dSRick Macklem #include <openssl/err.h> 68b9cbc85dSRick Macklem #include <openssl/x509v3.h> 69b9cbc85dSRick Macklem 70b9cbc85dSRick Macklem #include "rpctlssd.h" 71b9cbc85dSRick Macklem #include "rpc.tlscommon.h" 72b9cbc85dSRick Macklem 73b9cbc85dSRick Macklem #ifndef _PATH_RPCTLSSDSOCK 74b9cbc85dSRick Macklem #define _PATH_RPCTLSSDSOCK "/var/run/rpc.tlsservd.sock" 75b9cbc85dSRick Macklem #endif 76b9cbc85dSRick Macklem #ifndef _PATH_CERTANDKEY 77b9cbc85dSRick Macklem #define _PATH_CERTANDKEY "/etc/rpc.tlsservd/" 78b9cbc85dSRick Macklem #endif 79b9cbc85dSRick Macklem #ifndef _PATH_RPCTLSSDPID 80b9cbc85dSRick Macklem #define _PATH_RPCTLSSDPID "/var/run/rpc.tlsservd.pid" 81b9cbc85dSRick Macklem #endif 82b9cbc85dSRick Macklem #ifndef _PREFERRED_CIPHERS 83b9cbc85dSRick Macklem #define _PREFERRED_CIPHERS "AES128-GCM-SHA256" 84b9cbc85dSRick Macklem #endif 85b9cbc85dSRick Macklem 86b9cbc85dSRick Macklem /* Global variables also used by rpc.tlscommon.c. */ 87b9cbc85dSRick Macklem int rpctls_debug_level; 88b9cbc85dSRick Macklem bool rpctls_verbose; 89b9cbc85dSRick Macklem SSL_CTX *rpctls_ctx = NULL; 90b9cbc85dSRick Macklem const char *rpctls_verify_cafile = NULL; 91b9cbc85dSRick Macklem const char *rpctls_verify_capath = NULL; 92b9cbc85dSRick Macklem char *rpctls_crlfile = NULL; 93b9cbc85dSRick Macklem bool rpctls_gothup = false; 94b9cbc85dSRick Macklem struct ssl_list rpctls_ssllist; 95b9cbc85dSRick Macklem 96b9cbc85dSRick Macklem static struct pidfh *rpctls_pfh = NULL; 97b9cbc85dSRick Macklem static bool rpctls_do_mutual = false; 98b9cbc85dSRick Macklem static const char *rpctls_certdir = _PATH_CERTANDKEY; 99b9cbc85dSRick Macklem static bool rpctls_comparehost = false; 100b9cbc85dSRick Macklem static unsigned int rpctls_wildcard = X509_CHECK_FLAG_NO_WILDCARDS; 101b9cbc85dSRick Macklem static uint64_t rpctls_ssl_refno = 0; 102b9cbc85dSRick Macklem static uint64_t rpctls_ssl_sec = 0; 103b9cbc85dSRick Macklem static uint64_t rpctls_ssl_usec = 0; 104b9cbc85dSRick Macklem static bool rpctls_cnuser = false; 105b9cbc85dSRick Macklem static char *rpctls_dnsname; 106b9cbc85dSRick Macklem static const char *rpctls_cnuseroid = "1.3.6.1.4.1.2238.1.1.1"; 107b9cbc85dSRick Macklem 108b9cbc85dSRick Macklem static void rpctlssd_terminate(int); 109b9cbc85dSRick Macklem static SSL_CTX *rpctls_setup_ssl(const char *certdir); 110b9cbc85dSRick Macklem static SSL *rpctls_server(SSL_CTX *ctx, int s, 111b9cbc85dSRick Macklem uint32_t *flags, uint32_t *uidp, 112b9cbc85dSRick Macklem int *ngrps, uint32_t *gidp, X509 **certp); 113b9cbc85dSRick Macklem static int rpctls_cnname(X509 *cert, uint32_t *uidp, 114b9cbc85dSRick Macklem int *ngrps, uint32_t *gidp); 115b9cbc85dSRick Macklem static char *rpctls_getdnsname(char *dnsname); 116b9cbc85dSRick Macklem static void rpctls_huphandler(int sig __unused); 117b9cbc85dSRick Macklem 118b9cbc85dSRick Macklem extern void rpctlssd_1(struct svc_req *rqstp, SVCXPRT *transp); 119b9cbc85dSRick Macklem 120b9cbc85dSRick Macklem static struct option longopts[] = { 121b9cbc85dSRick Macklem { "certdir", required_argument, NULL, 'D' }, 122b9cbc85dSRick Macklem { "debuglevel", no_argument, NULL, 'd' }, 123b9cbc85dSRick Macklem { "checkhost", no_argument, NULL, 'h' }, 124b9cbc85dSRick Macklem { "verifylocs", required_argument, NULL, 'l' }, 125b9cbc85dSRick Macklem { "mutualverf", no_argument, NULL, 'm' }, 126b9cbc85dSRick Macklem { "domain", required_argument, NULL, 'n' }, 127b9cbc85dSRick Macklem { "verifydir", required_argument, NULL, 'p' }, 128b9cbc85dSRick Macklem { "crl", required_argument, NULL, 'r' }, 129b9cbc85dSRick Macklem { "certuser", no_argument, NULL, 'u' }, 130b9cbc85dSRick Macklem { "verbose", no_argument, NULL, 'v' }, 131b9cbc85dSRick Macklem { "multiwild", no_argument, NULL, 'W' }, 132b9cbc85dSRick Macklem { "singlewild", no_argument, NULL, 'w' }, 133b9cbc85dSRick Macklem { NULL, 0, NULL, 0 } 134b9cbc85dSRick Macklem }; 135b9cbc85dSRick Macklem 136b9cbc85dSRick Macklem int 137b9cbc85dSRick Macklem main(int argc, char **argv) 138b9cbc85dSRick Macklem { 139b9cbc85dSRick Macklem /* 140b9cbc85dSRick Macklem * We provide an RPC service on a local-domain socket. The 141b9cbc85dSRick Macklem * kernel rpctls code will upcall to this daemon to do the initial 142b9cbc85dSRick Macklem * TLS handshake. 143b9cbc85dSRick Macklem */ 144b9cbc85dSRick Macklem struct sockaddr_un sun; 145b9cbc85dSRick Macklem int ch, debug, fd, oldmask; 146b9cbc85dSRick Macklem SVCXPRT *xprt; 147b9cbc85dSRick Macklem struct timeval tm; 148b9cbc85dSRick Macklem struct timezone tz; 149b9cbc85dSRick Macklem char hostname[MAXHOSTNAMELEN + 2]; 150b9cbc85dSRick Macklem pid_t otherpid; 151b9cbc85dSRick Macklem bool tls_enable; 152b9cbc85dSRick Macklem size_t tls_enable_len; 153b9cbc85dSRick Macklem 154b9cbc85dSRick Macklem /* Check that another rpctlssd isn't already running. */ 155b9cbc85dSRick Macklem rpctls_pfh = pidfile_open(_PATH_RPCTLSSDPID, 0600, &otherpid); 156b9cbc85dSRick Macklem if (rpctls_pfh == NULL) { 157b9cbc85dSRick Macklem if (errno == EEXIST) 158b9cbc85dSRick Macklem errx(1, "rpctlssd already running, pid: %d.", otherpid); 159b9cbc85dSRick Macklem warn("cannot open or create pidfile"); 160b9cbc85dSRick Macklem } 161b9cbc85dSRick Macklem 162b9cbc85dSRick Macklem /* Check to see that the ktls is enabled. */ 163b9cbc85dSRick Macklem tls_enable_len = sizeof(tls_enable); 164b9cbc85dSRick Macklem if (sysctlbyname("kern.ipc.tls.enable", &tls_enable, &tls_enable_len, 165b9cbc85dSRick Macklem NULL, 0) != 0 || !tls_enable) 166b9cbc85dSRick Macklem errx(1, "Kernel TLS not enabled"); 167b9cbc85dSRick Macklem 168b9cbc85dSRick Macklem /* Get the time when this daemon is started. */ 169b9cbc85dSRick Macklem gettimeofday(&tm, &tz); 170b9cbc85dSRick Macklem rpctls_ssl_sec = tm.tv_sec; 171b9cbc85dSRick Macklem rpctls_ssl_usec = tm.tv_usec; 172b9cbc85dSRick Macklem 173b9cbc85dSRick Macklem /* Set the dns name for the server. */ 174b9cbc85dSRick Macklem rpctls_dnsname = rpctls_getdnsname(hostname); 175b9cbc85dSRick Macklem if (rpctls_dnsname == NULL) { 176b9cbc85dSRick Macklem strcpy(hostname, "@default.domain"); 177b9cbc85dSRick Macklem rpctls_dnsname = hostname; 178b9cbc85dSRick Macklem } 179b9cbc85dSRick Macklem 180b9cbc85dSRick Macklem debug = 0; 181b9cbc85dSRick Macklem rpctls_verbose = false; 182b9cbc85dSRick Macklem while ((ch = getopt_long(argc, argv, "D:dhl:n:mp:r:uvWw", longopts, 183b9cbc85dSRick Macklem NULL)) != -1) { 184b9cbc85dSRick Macklem switch (ch) { 185b9cbc85dSRick Macklem case 'D': 186b9cbc85dSRick Macklem rpctls_certdir = optarg; 187b9cbc85dSRick Macklem break; 188b9cbc85dSRick Macklem case 'd': 189b9cbc85dSRick Macklem rpctls_debug_level++; 190b9cbc85dSRick Macklem break; 191b9cbc85dSRick Macklem case 'h': 192b9cbc85dSRick Macklem rpctls_comparehost = true; 193b9cbc85dSRick Macklem break; 194b9cbc85dSRick Macklem case 'l': 195b9cbc85dSRick Macklem rpctls_verify_cafile = optarg; 196b9cbc85dSRick Macklem break; 197b9cbc85dSRick Macklem case 'm': 198b9cbc85dSRick Macklem rpctls_do_mutual = true; 199b9cbc85dSRick Macklem break; 200b9cbc85dSRick Macklem case 'n': 201b9cbc85dSRick Macklem hostname[0] = '@'; 202b9cbc85dSRick Macklem strlcpy(&hostname[1], optarg, MAXHOSTNAMELEN + 1); 203b9cbc85dSRick Macklem rpctls_dnsname = hostname; 204b9cbc85dSRick Macklem break; 205b9cbc85dSRick Macklem case 'p': 206b9cbc85dSRick Macklem rpctls_verify_capath = optarg; 207b9cbc85dSRick Macklem break; 208b9cbc85dSRick Macklem case 'r': 209b9cbc85dSRick Macklem rpctls_crlfile = optarg; 210b9cbc85dSRick Macklem break; 211b9cbc85dSRick Macklem case 'u': 212b9cbc85dSRick Macklem rpctls_cnuser = true; 213b9cbc85dSRick Macklem break; 214b9cbc85dSRick Macklem case 'v': 215b9cbc85dSRick Macklem rpctls_verbose = true; 216b9cbc85dSRick Macklem break; 217b9cbc85dSRick Macklem case 'W': 218b9cbc85dSRick Macklem if (rpctls_wildcard != X509_CHECK_FLAG_NO_WILDCARDS) 219b9cbc85dSRick Macklem errx(1, "options -w and -W are mutually " 220b9cbc85dSRick Macklem "exclusive"); 221b9cbc85dSRick Macklem rpctls_wildcard = X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS; 222b9cbc85dSRick Macklem break; 223b9cbc85dSRick Macklem case 'w': 224b9cbc85dSRick Macklem if (rpctls_wildcard != X509_CHECK_FLAG_NO_WILDCARDS) 225b9cbc85dSRick Macklem errx(1, "options -w and -W are mutually " 226b9cbc85dSRick Macklem "exclusive"); 227b9cbc85dSRick Macklem rpctls_wildcard = 0; 228b9cbc85dSRick Macklem break; 229b9cbc85dSRick Macklem default: 230b9cbc85dSRick Macklem fprintf(stderr, "usage: %s " 231b9cbc85dSRick Macklem "[-D/--certdir certdir] [-d/--debuglevel] " 232b9cbc85dSRick Macklem "[-h/--checkhost] " 233b9cbc85dSRick Macklem "[-l/--verifylocs CAfile] [-m/--mutualverf] " 234b9cbc85dSRick Macklem "[-n/--domain domain_name] " 235b9cbc85dSRick Macklem "[-p/--verifydir CApath] [-r/--crl CRLfile] " 236b9cbc85dSRick Macklem "[-u/--certuser] [-v/--verbose] [-W/--multiwild] " 237b9cbc85dSRick Macklem "[-w/--singlewild]\n", argv[0]); 238b9cbc85dSRick Macklem exit(1); 239b9cbc85dSRick Macklem } 240b9cbc85dSRick Macklem } 241b9cbc85dSRick Macklem if (rpctls_do_mutual && rpctls_verify_cafile == NULL && 242b9cbc85dSRick Macklem rpctls_verify_capath == NULL) 243b9cbc85dSRick Macklem errx(1, "-m requires the -l <CAfile> and/or " 244b9cbc85dSRick Macklem "-p <CApath> options"); 245b9cbc85dSRick Macklem if (rpctls_comparehost && (!rpctls_do_mutual || 246b9cbc85dSRick Macklem (rpctls_verify_cafile == NULL && rpctls_verify_capath == NULL))) 247b9cbc85dSRick Macklem errx(1, "-h requires the -m plus the " 248b9cbc85dSRick Macklem "-l <CAfile> and/or -p <CApath> options"); 249b9cbc85dSRick Macklem if (!rpctls_comparehost && rpctls_wildcard != 250b9cbc85dSRick Macklem X509_CHECK_FLAG_NO_WILDCARDS) 251b9cbc85dSRick Macklem errx(1, "The -w or -W options require the -h option"); 252b9cbc85dSRick Macklem if (rpctls_cnuser && (!rpctls_do_mutual || 253b9cbc85dSRick Macklem (rpctls_verify_cafile == NULL && rpctls_verify_capath == NULL))) 254b9cbc85dSRick Macklem errx(1, "-u requires the -m plus the " 255b9cbc85dSRick Macklem "-l <CAfile> and/or -p <CApath> options"); 256b9cbc85dSRick Macklem 257b9cbc85dSRick Macklem if (modfind("krpc") < 0) { 258b9cbc85dSRick Macklem /* Not present in kernel, try loading it */ 259b9cbc85dSRick Macklem if (kldload("krpc") < 0 || modfind("krpc") < 0) 260b9cbc85dSRick Macklem errx(1, "Kernel RPC is not available"); 261b9cbc85dSRick Macklem } 262b9cbc85dSRick Macklem 263b9cbc85dSRick Macklem if (rpctls_debug_level == 0) { 264b9cbc85dSRick Macklem if (daemon(0, 0) != 0) 265b9cbc85dSRick Macklem err(1, "Can't daemonize"); 266b9cbc85dSRick Macklem signal(SIGINT, SIG_IGN); 267b9cbc85dSRick Macklem signal(SIGQUIT, SIG_IGN); 268b9cbc85dSRick Macklem signal(SIGHUP, SIG_IGN); 269b9cbc85dSRick Macklem } 270b9cbc85dSRick Macklem signal(SIGTERM, rpctlssd_terminate); 271b9cbc85dSRick Macklem signal(SIGPIPE, SIG_IGN); 272b9cbc85dSRick Macklem signal(SIGHUP, rpctls_huphandler); 273b9cbc85dSRick Macklem 274b9cbc85dSRick Macklem pidfile_write(rpctls_pfh); 275b9cbc85dSRick Macklem 276b9cbc85dSRick Macklem memset(&sun, 0, sizeof sun); 277b9cbc85dSRick Macklem sun.sun_family = AF_LOCAL; 278b9cbc85dSRick Macklem unlink(_PATH_RPCTLSSDSOCK); 279b9cbc85dSRick Macklem strcpy(sun.sun_path, _PATH_RPCTLSSDSOCK); 280b9cbc85dSRick Macklem sun.sun_len = SUN_LEN(&sun); 281b9cbc85dSRick Macklem fd = socket(AF_LOCAL, SOCK_STREAM, 0); 282b9cbc85dSRick Macklem if (fd < 0) { 283b9cbc85dSRick Macklem if (rpctls_debug_level == 0) { 284b9cbc85dSRick Macklem syslog(LOG_ERR, "Can't create local rpctlssd socket"); 285b9cbc85dSRick Macklem exit(1); 286b9cbc85dSRick Macklem } 287b9cbc85dSRick Macklem err(1, "Can't create local rpctlssd socket"); 288b9cbc85dSRick Macklem } 289b9cbc85dSRick Macklem oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO); 290b9cbc85dSRick Macklem if (bind(fd, (struct sockaddr *)&sun, sun.sun_len) < 0) { 291b9cbc85dSRick Macklem if (rpctls_debug_level == 0) { 292b9cbc85dSRick Macklem syslog(LOG_ERR, "Can't bind local rpctlssd socket"); 293b9cbc85dSRick Macklem exit(1); 294b9cbc85dSRick Macklem } 295b9cbc85dSRick Macklem err(1, "Can't bind local rpctlssd socket"); 296b9cbc85dSRick Macklem } 297b9cbc85dSRick Macklem umask(oldmask); 298b9cbc85dSRick Macklem if (listen(fd, SOMAXCONN) < 0) { 299b9cbc85dSRick Macklem if (rpctls_debug_level == 0) { 300b9cbc85dSRick Macklem syslog(LOG_ERR, 301b9cbc85dSRick Macklem "Can't listen on local rpctlssd socket"); 302b9cbc85dSRick Macklem exit(1); 303b9cbc85dSRick Macklem } 304b9cbc85dSRick Macklem err(1, "Can't listen on local rpctlssd socket"); 305b9cbc85dSRick Macklem } 306b9cbc85dSRick Macklem xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE); 307b9cbc85dSRick Macklem if (!xprt) { 308b9cbc85dSRick Macklem if (rpctls_debug_level == 0) { 309b9cbc85dSRick Macklem syslog(LOG_ERR, 310b9cbc85dSRick Macklem "Can't create transport for local rpctlssd socket"); 311b9cbc85dSRick Macklem exit(1); 312b9cbc85dSRick Macklem } 313b9cbc85dSRick Macklem err(1, "Can't create transport for local rpctlssd socket"); 314b9cbc85dSRick Macklem } 315b9cbc85dSRick Macklem if (!svc_reg(xprt, RPCTLSSD, RPCTLSSDVERS, rpctlssd_1, NULL)) { 316b9cbc85dSRick Macklem if (rpctls_debug_level == 0) { 317b9cbc85dSRick Macklem syslog(LOG_ERR, 318b9cbc85dSRick Macklem "Can't register service for local rpctlssd socket"); 319b9cbc85dSRick Macklem exit(1); 320b9cbc85dSRick Macklem } 321b9cbc85dSRick Macklem err(1, "Can't register service for local rpctlssd socket"); 322b9cbc85dSRick Macklem } 323b9cbc85dSRick Macklem 324b9cbc85dSRick Macklem rpctls_ctx = rpctls_setup_ssl(rpctls_certdir); 325b9cbc85dSRick Macklem if (rpctls_ctx == NULL) { 326b9cbc85dSRick Macklem if (rpctls_debug_level == 0) { 327b9cbc85dSRick Macklem syslog(LOG_ERR, "Can't create SSL context"); 328b9cbc85dSRick Macklem exit(1); 329b9cbc85dSRick Macklem } 330b9cbc85dSRick Macklem err(1, "Can't create SSL context"); 331b9cbc85dSRick Macklem } 332b9cbc85dSRick Macklem rpctls_gothup = false; 333b9cbc85dSRick Macklem LIST_INIT(&rpctls_ssllist); 334b9cbc85dSRick Macklem 335b9cbc85dSRick Macklem rpctls_syscall(RPCTLS_SYSC_SRVSETPATH, _PATH_RPCTLSSDSOCK); 336b9cbc85dSRick Macklem 337b9cbc85dSRick Macklem rpctls_svc_run(); 338b9cbc85dSRick Macklem 339b9cbc85dSRick Macklem rpctls_syscall(RPCTLS_SYSC_SRVSHUTDOWN, ""); 340b9cbc85dSRick Macklem 341b9cbc85dSRick Macklem SSL_CTX_free(rpctls_ctx); 342b9cbc85dSRick Macklem EVP_cleanup(); 343b9cbc85dSRick Macklem return (0); 344b9cbc85dSRick Macklem } 345b9cbc85dSRick Macklem 346b9cbc85dSRick Macklem bool_t 347b9cbc85dSRick Macklem rpctlssd_null_1_svc(__unused void *argp, __unused void *result, 348b9cbc85dSRick Macklem __unused struct svc_req *rqstp) 349b9cbc85dSRick Macklem { 350b9cbc85dSRick Macklem 351b9cbc85dSRick Macklem rpctls_verbose_out("rpctlssd_null_svc: done\n"); 352b9cbc85dSRick Macklem return (TRUE); 353b9cbc85dSRick Macklem } 354b9cbc85dSRick Macklem 355b9cbc85dSRick Macklem bool_t 356b9cbc85dSRick Macklem rpctlssd_connect_1_svc(__unused void *argp, 357b9cbc85dSRick Macklem struct rpctlssd_connect_res *result, __unused struct svc_req *rqstp) 358b9cbc85dSRick Macklem { 359b9cbc85dSRick Macklem int ngrps, s; 360b9cbc85dSRick Macklem SSL *ssl; 361b9cbc85dSRick Macklem uint32_t flags; 362b9cbc85dSRick Macklem struct ssl_entry *newslp; 363b9cbc85dSRick Macklem uint32_t uid; 364b9cbc85dSRick Macklem uint32_t *gidp; 365b9cbc85dSRick Macklem X509 *cert; 366b9cbc85dSRick Macklem 367b9cbc85dSRick Macklem rpctls_verbose_out("rpctlsd_connect_svc: started\n"); 368b9cbc85dSRick Macklem memset(result, 0, sizeof(*result)); 369b9cbc85dSRick Macklem /* Get the socket fd from the kernel. */ 370b9cbc85dSRick Macklem s = rpctls_syscall(RPCTLS_SYSC_SRVSOCKET, ""); 371b9cbc85dSRick Macklem if (s < 0) 372b9cbc85dSRick Macklem return (FALSE); 373b9cbc85dSRick Macklem 374b9cbc85dSRick Macklem /* Do the server side of a TLS handshake. */ 375b9cbc85dSRick Macklem gidp = calloc(NGROUPS, sizeof(*gidp)); 376b9cbc85dSRick Macklem ssl = rpctls_server(rpctls_ctx, s, &flags, &uid, &ngrps, gidp, &cert); 377b9cbc85dSRick Macklem if (ssl == NULL) { 378b9cbc85dSRick Macklem free(gidp); 379b9cbc85dSRick Macklem rpctls_verbose_out("rpctlssd_connect_svc: ssl " 380b9cbc85dSRick Macklem "accept failed\n"); 381b9cbc85dSRick Macklem /* 382b9cbc85dSRick Macklem * For RPC-over-TLS, this upcall is expected 383b9cbc85dSRick Macklem * to close off the socket upon handshake failure. 384b9cbc85dSRick Macklem */ 385b9cbc85dSRick Macklem close(s); 386b9cbc85dSRick Macklem return (FALSE); 387b9cbc85dSRick Macklem } else { 388b9cbc85dSRick Macklem rpctls_verbose_out("rpctlssd_connect_svc: " 389b9cbc85dSRick Macklem "succeeded flags=0x%x\n", flags); 390b9cbc85dSRick Macklem result->flags = flags; 391b9cbc85dSRick Macklem result->sec = rpctls_ssl_sec; 392b9cbc85dSRick Macklem result->usec = rpctls_ssl_usec; 393b9cbc85dSRick Macklem result->ssl = ++rpctls_ssl_refno; 394b9cbc85dSRick Macklem /* Hard to believe this could ever wrap around.. */ 395b9cbc85dSRick Macklem if (rpctls_ssl_refno == 0) 396b9cbc85dSRick Macklem result->ssl = ++rpctls_ssl_refno; 397b9cbc85dSRick Macklem if ((flags & RPCTLS_FLAGS_CERTUSER) != 0) { 398b9cbc85dSRick Macklem result->uid = uid; 399b9cbc85dSRick Macklem result->gid.gid_len = ngrps; 400b9cbc85dSRick Macklem result->gid.gid_val = gidp; 401b9cbc85dSRick Macklem } else { 402b9cbc85dSRick Macklem result->uid = 0; 403b9cbc85dSRick Macklem result->gid.gid_len = 0; 404b9cbc85dSRick Macklem result->gid.gid_val = gidp; 405b9cbc85dSRick Macklem } 406b9cbc85dSRick Macklem } 407b9cbc85dSRick Macklem 408b9cbc85dSRick Macklem /* Maintain list of all current SSL *'s */ 409b9cbc85dSRick Macklem newslp = malloc(sizeof(*newslp)); 410b9cbc85dSRick Macklem newslp->ssl = ssl; 411b9cbc85dSRick Macklem newslp->s = s; 412b9cbc85dSRick Macklem newslp->shutoff = false; 413b9cbc85dSRick Macklem newslp->refno = rpctls_ssl_refno; 414b9cbc85dSRick Macklem newslp->cert = cert; 415b9cbc85dSRick Macklem LIST_INSERT_HEAD(&rpctls_ssllist, newslp, next); 416b9cbc85dSRick Macklem return (TRUE); 417b9cbc85dSRick Macklem } 418b9cbc85dSRick Macklem 419b9cbc85dSRick Macklem bool_t 420b9cbc85dSRick Macklem rpctlssd_handlerecord_1_svc(struct rpctlssd_handlerecord_arg *argp, 421b9cbc85dSRick Macklem struct rpctlssd_handlerecord_res *result, __unused struct svc_req *rqstp) 422b9cbc85dSRick Macklem { 423b9cbc85dSRick Macklem struct ssl_entry *slp; 424b9cbc85dSRick Macklem int ret; 425b9cbc85dSRick Macklem char junk; 426b9cbc85dSRick Macklem 427b9cbc85dSRick Macklem slp = NULL; 428b9cbc85dSRick Macklem if (argp->sec == rpctls_ssl_sec && argp->usec == 429b9cbc85dSRick Macklem rpctls_ssl_usec) { 430b9cbc85dSRick Macklem LIST_FOREACH(slp, &rpctls_ssllist, next) { 431b9cbc85dSRick Macklem if (slp->refno == argp->ssl) 432b9cbc85dSRick Macklem break; 433b9cbc85dSRick Macklem } 434b9cbc85dSRick Macklem } 435b9cbc85dSRick Macklem 436b9cbc85dSRick Macklem if (slp != NULL) { 437b9cbc85dSRick Macklem rpctls_verbose_out("rpctlssd_handlerecord fd=%d\n", 438b9cbc85dSRick Macklem slp->s); 439b9cbc85dSRick Macklem /* 440b9cbc85dSRick Macklem * An SSL_read() of 0 bytes should fail, but it should 441b9cbc85dSRick Macklem * handle the non-application data record before doing so. 442b9cbc85dSRick Macklem */ 443b9cbc85dSRick Macklem ret = SSL_read(slp->ssl, &junk, 0); 444b9cbc85dSRick Macklem if (ret <= 0) { 445b9cbc85dSRick Macklem /* Check to see if this was a close alert. */ 446b9cbc85dSRick Macklem ret = SSL_get_shutdown(slp->ssl); 447b9cbc85dSRick Macklem if ((ret & (SSL_SENT_SHUTDOWN | 448b9cbc85dSRick Macklem SSL_RECEIVED_SHUTDOWN)) == SSL_RECEIVED_SHUTDOWN) 449b9cbc85dSRick Macklem SSL_shutdown(slp->ssl); 450b9cbc85dSRick Macklem } else { 451b9cbc85dSRick Macklem if (rpctls_debug_level == 0) 452b9cbc85dSRick Macklem syslog(LOG_ERR, "SSL_read returned %d", ret); 453b9cbc85dSRick Macklem else 454b9cbc85dSRick Macklem fprintf(stderr, "SSL_read returned %d\n", ret); 455b9cbc85dSRick Macklem } 456b9cbc85dSRick Macklem result->reterr = RPCTLSERR_OK; 457b9cbc85dSRick Macklem } else 458b9cbc85dSRick Macklem result->reterr = RPCTLSERR_NOSSL; 459b9cbc85dSRick Macklem return (TRUE); 460b9cbc85dSRick Macklem } 461b9cbc85dSRick Macklem 462b9cbc85dSRick Macklem bool_t 463b9cbc85dSRick Macklem rpctlssd_disconnect_1_svc(struct rpctlssd_disconnect_arg *argp, 464b9cbc85dSRick Macklem struct rpctlssd_disconnect_res *result, __unused struct svc_req *rqstp) 465b9cbc85dSRick Macklem { 466b9cbc85dSRick Macklem struct ssl_entry *slp; 467b9cbc85dSRick Macklem int ret; 468b9cbc85dSRick Macklem 469b9cbc85dSRick Macklem slp = NULL; 470b9cbc85dSRick Macklem if (argp->sec == rpctls_ssl_sec && argp->usec == 471b9cbc85dSRick Macklem rpctls_ssl_usec) { 472b9cbc85dSRick Macklem LIST_FOREACH(slp, &rpctls_ssllist, next) { 473b9cbc85dSRick Macklem if (slp->refno == argp->ssl) 474b9cbc85dSRick Macklem break; 475b9cbc85dSRick Macklem } 476b9cbc85dSRick Macklem } 477b9cbc85dSRick Macklem 478b9cbc85dSRick Macklem if (slp != NULL) { 479b9cbc85dSRick Macklem rpctls_verbose_out("rpctlssd_disconnect fd=%d closed\n", 480b9cbc85dSRick Macklem slp->s); 481b9cbc85dSRick Macklem LIST_REMOVE(slp, next); 482b9cbc85dSRick Macklem if (!slp->shutoff) { 483b9cbc85dSRick Macklem ret = SSL_get_shutdown(slp->ssl); 484b9cbc85dSRick Macklem /* 485b9cbc85dSRick Macklem * Do an SSL_shutdown() unless a close alert has 486b9cbc85dSRick Macklem * already been sent. 487b9cbc85dSRick Macklem */ 488b9cbc85dSRick Macklem if ((ret & SSL_SENT_SHUTDOWN) == 0) 489b9cbc85dSRick Macklem SSL_shutdown(slp->ssl); 490b9cbc85dSRick Macklem } 491b9cbc85dSRick Macklem SSL_free(slp->ssl); 492b9cbc85dSRick Macklem if (slp->cert != NULL) 493b9cbc85dSRick Macklem X509_free(slp->cert); 494b9cbc85dSRick Macklem /* 495b9cbc85dSRick Macklem * For RPC-over-TLS, this upcall is expected 496b9cbc85dSRick Macklem * to close off the socket. 497b9cbc85dSRick Macklem */ 498b9cbc85dSRick Macklem if (!slp->shutoff) 499b9cbc85dSRick Macklem shutdown(slp->s, SHUT_WR); 500b9cbc85dSRick Macklem close(slp->s); 501b9cbc85dSRick Macklem free(slp); 502b9cbc85dSRick Macklem result->reterr = RPCTLSERR_OK; 503b9cbc85dSRick Macklem } else 504b9cbc85dSRick Macklem result->reterr = RPCTLSERR_NOCLOSE; 505b9cbc85dSRick Macklem return (TRUE); 506b9cbc85dSRick Macklem } 507b9cbc85dSRick Macklem 508b9cbc85dSRick Macklem int 509b9cbc85dSRick Macklem rpctlssd_1_freeresult(__unused SVCXPRT *transp, xdrproc_t xdr_result, 510b9cbc85dSRick Macklem caddr_t result) 511b9cbc85dSRick Macklem { 512b9cbc85dSRick Macklem rpctlssd_connect_res *res; 513b9cbc85dSRick Macklem 514b9cbc85dSRick Macklem if (xdr_result == (xdrproc_t)xdr_rpctlssd_connect_res) { 515b9cbc85dSRick Macklem res = (rpctlssd_connect_res *)(void *)result; 516b9cbc85dSRick Macklem free(res->gid.gid_val); 517b9cbc85dSRick Macklem } 518b9cbc85dSRick Macklem return (TRUE); 519b9cbc85dSRick Macklem } 520b9cbc85dSRick Macklem 521b9cbc85dSRick Macklem static void 522b9cbc85dSRick Macklem rpctlssd_terminate(int sig __unused) 523b9cbc85dSRick Macklem { 524b9cbc85dSRick Macklem struct ssl_entry *slp; 525b9cbc85dSRick Macklem 526b9cbc85dSRick Macklem rpctls_syscall(RPCTLS_SYSC_SRVSHUTDOWN, ""); 527b9cbc85dSRick Macklem pidfile_remove(rpctls_pfh); 528b9cbc85dSRick Macklem 529b9cbc85dSRick Macklem LIST_FOREACH(slp, &rpctls_ssllist, next) 530b9cbc85dSRick Macklem shutdown(slp->s, SHUT_RD); 531b9cbc85dSRick Macklem exit(0); 532b9cbc85dSRick Macklem } 533b9cbc85dSRick Macklem 534b9cbc85dSRick Macklem /* Allow the handshake to proceed. */ 535b9cbc85dSRick Macklem static int 536b9cbc85dSRick Macklem rpctls_verify_callback(__unused int preverify_ok, 537b9cbc85dSRick Macklem __unused X509_STORE_CTX *x509_ctx) 538b9cbc85dSRick Macklem { 539b9cbc85dSRick Macklem 540b9cbc85dSRick Macklem return (1); 541b9cbc85dSRick Macklem } 542b9cbc85dSRick Macklem 543b9cbc85dSRick Macklem static SSL_CTX * 544b9cbc85dSRick Macklem rpctls_setup_ssl(const char *certdir) 545b9cbc85dSRick Macklem { 546b9cbc85dSRick Macklem SSL_CTX *ctx; 547b9cbc85dSRick Macklem char path[PATH_MAX]; 548b9cbc85dSRick Macklem size_t len, rlen; 549b9cbc85dSRick Macklem int ret; 550b9cbc85dSRick Macklem 551b9cbc85dSRick Macklem SSL_library_init(); 552b9cbc85dSRick Macklem SSL_load_error_strings(); 553b9cbc85dSRick Macklem OpenSSL_add_all_algorithms(); 554b9cbc85dSRick Macklem 555b9cbc85dSRick Macklem ctx = SSL_CTX_new(TLS_server_method()); 556b9cbc85dSRick Macklem if (ctx == NULL) { 557b9cbc85dSRick Macklem rpctls_verbose_out("rpctls_setup_ssl: SSL_CTX_new failed\n"); 558b9cbc85dSRick Macklem return (NULL); 559b9cbc85dSRick Macklem } 560b9cbc85dSRick Macklem SSL_CTX_set_ecdh_auto(ctx, 1); 561b9cbc85dSRick Macklem 562b9cbc85dSRick Macklem /* 563b9cbc85dSRick Macklem * Set preferred ciphers, since KERN_TLS only supports a 564b9cbc85dSRick Macklem * few of them. 565b9cbc85dSRick Macklem */ 566b9cbc85dSRick Macklem ret = SSL_CTX_set_cipher_list(ctx, _PREFERRED_CIPHERS); 567b9cbc85dSRick Macklem if (ret == 0) { 568b9cbc85dSRick Macklem rpctls_verbose_out("rpctls_setup_ssl: " 569b9cbc85dSRick Macklem "SSL_CTX_set_cipher_list failed to set any ciphers\n"); 570b9cbc85dSRick Macklem SSL_CTX_free(ctx); 571b9cbc85dSRick Macklem return (NULL); 572b9cbc85dSRick Macklem } 573b9cbc85dSRick Macklem 574b9cbc85dSRick Macklem /* Get the cert.pem and certkey.pem files from the directory certdir. */ 575b9cbc85dSRick Macklem len = strlcpy(path, certdir, sizeof(path)); 576b9cbc85dSRick Macklem rlen = sizeof(path) - len; 577b9cbc85dSRick Macklem if (strlcpy(&path[len], "cert.pem", rlen) != 8) { 578b9cbc85dSRick Macklem SSL_CTX_free(ctx); 579b9cbc85dSRick Macklem return (NULL); 580b9cbc85dSRick Macklem } 581b9cbc85dSRick Macklem ret = SSL_CTX_use_certificate_file(ctx, path, SSL_FILETYPE_PEM); 582b9cbc85dSRick Macklem if (ret != 1) { 583b9cbc85dSRick Macklem rpctls_verbose_out("rpctls_setup_ssl: can't use certificate " 584b9cbc85dSRick Macklem "file path=%s ret=%d\n", path, ret); 585b9cbc85dSRick Macklem SSL_CTX_free(ctx); 586b9cbc85dSRick Macklem return (NULL); 587b9cbc85dSRick Macklem } 588b9cbc85dSRick Macklem if (strlcpy(&path[len], "certkey.pem", rlen) != 11) { 589b9cbc85dSRick Macklem SSL_CTX_free(ctx); 590b9cbc85dSRick Macklem return (NULL); 591b9cbc85dSRick Macklem } 592b9cbc85dSRick Macklem ret = SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM); 593b9cbc85dSRick Macklem if (ret != 1) { 594b9cbc85dSRick Macklem rpctls_verbose_out("rpctls_setup_ssl: Can't use private " 595b9cbc85dSRick Macklem "key path=%s ret=%d\n", path, ret); 596b9cbc85dSRick Macklem SSL_CTX_free(ctx); 597b9cbc85dSRick Macklem return (NULL); 598b9cbc85dSRick Macklem } 599b9cbc85dSRick Macklem 600b9cbc85dSRick Macklem /* Set Mutual authentication, as required. */ 601b9cbc85dSRick Macklem if (rpctls_do_mutual) { 602b9cbc85dSRick Macklem if (rpctls_verify_cafile != NULL || 603b9cbc85dSRick Macklem rpctls_verify_capath != NULL) { 604b9cbc85dSRick Macklem if (rpctls_crlfile != NULL) { 605b9cbc85dSRick Macklem ret = rpctls_loadcrlfile(ctx); 606b9cbc85dSRick Macklem if (ret == 0) { 607b9cbc85dSRick Macklem rpctls_verbose_out("rpctls_setup_ssl:" 608b9cbc85dSRick Macklem " Load CRLfile failed\n"); 609b9cbc85dSRick Macklem SSL_CTX_free(ctx); 610b9cbc85dSRick Macklem return (NULL); 611b9cbc85dSRick Macklem } 612b9cbc85dSRick Macklem } 613b9cbc85dSRick Macklem #if OPENSSL_VERSION_NUMBER >= 0x30000000 614b9cbc85dSRick Macklem ret = 1; 615b9cbc85dSRick Macklem if (rpctls_verify_cafile != NULL) 616b9cbc85dSRick Macklem ret = SSL_CTX_load_verify_file(ctx, 617b9cbc85dSRick Macklem rpctls_verify_cafile); 618b9cbc85dSRick Macklem if (ret != 0 && rpctls_verify_capath != NULL) 619b9cbc85dSRick Macklem ret = SSL_CTX_load_verify_dir(ctx, 620b9cbc85dSRick Macklem rpctls_verify_capath); 621b9cbc85dSRick Macklem #else 622b9cbc85dSRick Macklem ret = SSL_CTX_load_verify_locations(ctx, 623b9cbc85dSRick Macklem rpctls_verify_cafile, rpctls_verify_capath); 624b9cbc85dSRick Macklem #endif 625b9cbc85dSRick Macklem if (ret == 0) { 626b9cbc85dSRick Macklem rpctls_verbose_out("rpctls_setup_ssl: " 627b9cbc85dSRick Macklem "Can't load verify locations\n"); 628b9cbc85dSRick Macklem SSL_CTX_free(ctx); 629b9cbc85dSRick Macklem return (NULL); 630b9cbc85dSRick Macklem } 631b9cbc85dSRick Macklem if (rpctls_verify_cafile != NULL) 632b9cbc85dSRick Macklem SSL_CTX_set_client_CA_list(ctx, 633b9cbc85dSRick Macklem SSL_load_client_CA_file( 634b9cbc85dSRick Macklem rpctls_verify_cafile)); 635b9cbc85dSRick Macklem } 636b9cbc85dSRick Macklem SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 637b9cbc85dSRick Macklem rpctls_verify_callback); 638b9cbc85dSRick Macklem } 639*c7bb0f47SJohn Baldwin #ifdef SSL_OP_ENABLE_KTLS 640*c7bb0f47SJohn Baldwin SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS); 641*c7bb0f47SJohn Baldwin #endif 642*c7bb0f47SJohn Baldwin #ifdef SSL_MODE_NO_KTLS_TX 643b9cbc85dSRick Macklem SSL_CTX_clear_mode(ctx, SSL_MODE_NO_KTLS_TX | SSL_MODE_NO_KTLS_RX); 644*c7bb0f47SJohn Baldwin #endif 645b9cbc85dSRick Macklem return (ctx); 646b9cbc85dSRick Macklem } 647b9cbc85dSRick Macklem 648b9cbc85dSRick Macklem static SSL * 649b9cbc85dSRick Macklem rpctls_server(SSL_CTX *ctx, int s, uint32_t *flags, uint32_t *uidp, 650b9cbc85dSRick Macklem int *ngrps, uint32_t *gidp, X509 **certp) 651b9cbc85dSRick Macklem { 652b9cbc85dSRick Macklem SSL *ssl; 653b9cbc85dSRick Macklem X509 *cert; 654b9cbc85dSRick Macklem struct sockaddr *sad; 655b9cbc85dSRick Macklem struct sockaddr_storage ad; 656b9cbc85dSRick Macklem char hostnam[NI_MAXHOST]; 657b9cbc85dSRick Macklem int gethostret, ret; 658b9cbc85dSRick Macklem char *cp, *cp2; 659b9cbc85dSRick Macklem long verfret; 660b9cbc85dSRick Macklem 661b9cbc85dSRick Macklem *flags = 0; 662b9cbc85dSRick Macklem *certp = NULL; 663b9cbc85dSRick Macklem sad = (struct sockaddr *)&ad; 664b9cbc85dSRick Macklem ssl = SSL_new(ctx); 665b9cbc85dSRick Macklem if (ssl == NULL) { 666b9cbc85dSRick Macklem rpctls_verbose_out("rpctls_server: SSL_new failed\n"); 667b9cbc85dSRick Macklem return (NULL); 668b9cbc85dSRick Macklem } 669b9cbc85dSRick Macklem if (SSL_set_fd(ssl, s) != 1) { 670b9cbc85dSRick Macklem rpctls_verbose_out("rpctls_server: SSL_set_fd failed\n"); 671b9cbc85dSRick Macklem SSL_free(ssl); 672b9cbc85dSRick Macklem return (NULL); 673b9cbc85dSRick Macklem } 674b9cbc85dSRick Macklem ret = SSL_accept(ssl); 675b9cbc85dSRick Macklem if (ret != 1) { 676b9cbc85dSRick Macklem rpctls_verbose_out("rpctls_server: SSL_accept " 677b9cbc85dSRick Macklem "failed ret=%d\n", ret); 678b9cbc85dSRick Macklem SSL_free(ssl); 679b9cbc85dSRick Macklem return (NULL); 680b9cbc85dSRick Macklem } 681b9cbc85dSRick Macklem *flags |= RPCTLS_FLAGS_HANDSHAKE; 682b9cbc85dSRick Macklem if (rpctls_do_mutual) { 683b9cbc85dSRick Macklem cert = SSL_get_peer_certificate(ssl); 684b9cbc85dSRick Macklem if (cert != NULL) { 685b9cbc85dSRick Macklem gethostret = rpctls_gethost(s, sad, hostnam, 686b9cbc85dSRick Macklem sizeof(hostnam)); 687b9cbc85dSRick Macklem if (gethostret == 0) 688b9cbc85dSRick Macklem hostnam[0] = '\0'; 689b9cbc85dSRick Macklem cp2 = X509_NAME_oneline( 690b9cbc85dSRick Macklem X509_get_subject_name(cert), NULL, 0); 691b9cbc85dSRick Macklem *flags |= RPCTLS_FLAGS_GOTCERT; 692b9cbc85dSRick Macklem verfret = SSL_get_verify_result(ssl); 693b9cbc85dSRick Macklem if (verfret != X509_V_OK) { 694b9cbc85dSRick Macklem cp = X509_NAME_oneline( 695b9cbc85dSRick Macklem X509_get_issuer_name(cert), NULL, 0); 696b9cbc85dSRick Macklem if (rpctls_debug_level == 0) 697b9cbc85dSRick Macklem syslog(LOG_INFO | LOG_DAEMON, 698b9cbc85dSRick Macklem "rpctls_server: client IP %s " 699b9cbc85dSRick Macklem "issuerName=%s subjectName=%s" 700b9cbc85dSRick Macklem " verify failed %s\n", hostnam, 701b9cbc85dSRick Macklem cp, cp2, 702b9cbc85dSRick Macklem X509_verify_cert_error_string( 703b9cbc85dSRick Macklem verfret)); 704b9cbc85dSRick Macklem else 705b9cbc85dSRick Macklem fprintf(stderr, 706b9cbc85dSRick Macklem "rpctls_server: client IP %s " 707b9cbc85dSRick Macklem "issuerName=%s subjectName=%s" 708b9cbc85dSRick Macklem " verify failed %s\n", hostnam, 709b9cbc85dSRick Macklem cp, cp2, 710b9cbc85dSRick Macklem X509_verify_cert_error_string( 711b9cbc85dSRick Macklem verfret)); 712b9cbc85dSRick Macklem } 713b9cbc85dSRick Macklem if (verfret == 714b9cbc85dSRick Macklem X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT || 715b9cbc85dSRick Macklem verfret == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) 716b9cbc85dSRick Macklem *flags |= RPCTLS_FLAGS_SELFSIGNED; 717b9cbc85dSRick Macklem else if (verfret == X509_V_OK) { 718b9cbc85dSRick Macklem if (rpctls_comparehost) { 719b9cbc85dSRick Macklem ret = 0; 720b9cbc85dSRick Macklem if (gethostret != 0) 721b9cbc85dSRick Macklem ret = rpctls_checkhost(sad, 722b9cbc85dSRick Macklem cert, rpctls_wildcard); 723b9cbc85dSRick Macklem if (ret != 1) { 724b9cbc85dSRick Macklem *flags |= 725b9cbc85dSRick Macklem RPCTLS_FLAGS_DISABLED; 726b9cbc85dSRick Macklem rpctls_verbose_out( 727b9cbc85dSRick Macklem "rpctls_server: " 728b9cbc85dSRick Macklem "checkhost " 729b9cbc85dSRick Macklem "failed\n"); 730b9cbc85dSRick Macklem } 731b9cbc85dSRick Macklem } 732b9cbc85dSRick Macklem if (rpctls_cnuser) { 733b9cbc85dSRick Macklem ret = rpctls_cnname(cert, uidp, 734b9cbc85dSRick Macklem ngrps, gidp); 735b9cbc85dSRick Macklem if (ret != 0) 736b9cbc85dSRick Macklem *flags |= RPCTLS_FLAGS_CERTUSER; 737b9cbc85dSRick Macklem } 738b9cbc85dSRick Macklem *flags |= RPCTLS_FLAGS_VERIFIED; 739b9cbc85dSRick Macklem *certp = cert; 740b9cbc85dSRick Macklem cert = NULL; 741b9cbc85dSRick Macklem } 742b9cbc85dSRick Macklem if (cert != NULL) 743b9cbc85dSRick Macklem X509_free(cert); 744b9cbc85dSRick Macklem } else 745b9cbc85dSRick Macklem rpctls_verbose_out("rpctls_server: " 746b9cbc85dSRick Macklem "No peer certificate\n"); 747b9cbc85dSRick Macklem } 748b9cbc85dSRick Macklem 749b9cbc85dSRick Macklem /* Check to see that ktls is working for the connection. */ 750b9cbc85dSRick Macklem ret = BIO_get_ktls_send(SSL_get_wbio(ssl)); 751b9cbc85dSRick Macklem rpctls_verbose_out("rpctls_server: BIO_get_ktls_send=%d\n", ret); 752b9cbc85dSRick Macklem if (ret != 0) { 753b9cbc85dSRick Macklem ret = BIO_get_ktls_recv(SSL_get_rbio(ssl)); 754b9cbc85dSRick Macklem rpctls_verbose_out("rpctls_server: BIO_get_ktls_recv=%d\n", 755b9cbc85dSRick Macklem ret); 756b9cbc85dSRick Macklem } 757b9cbc85dSRick Macklem if (ret == 0) { 758b9cbc85dSRick Macklem if (rpctls_debug_level == 0) 759b9cbc85dSRick Macklem syslog(LOG_ERR, "ktls not working"); 760b9cbc85dSRick Macklem else 761b9cbc85dSRick Macklem fprintf(stderr, "ktls not working\n"); 762b9cbc85dSRick Macklem /* 763b9cbc85dSRick Macklem * The handshake has completed, so all that can be 764b9cbc85dSRick Macklem * done is disable the connection. 765b9cbc85dSRick Macklem */ 766b9cbc85dSRick Macklem *flags |= RPCTLS_FLAGS_DISABLED; 767b9cbc85dSRick Macklem } 768b9cbc85dSRick Macklem 769b9cbc85dSRick Macklem return (ssl); 770b9cbc85dSRick Macklem } 771b9cbc85dSRick Macklem 772b9cbc85dSRick Macklem /* 773b9cbc85dSRick Macklem * Acquire the dnsname for this server. 774b9cbc85dSRick Macklem */ 775b9cbc85dSRick Macklem static char * 776b9cbc85dSRick Macklem rpctls_getdnsname(char *hostname) 777b9cbc85dSRick Macklem { 778b9cbc85dSRick Macklem char *cp, *dnsname; 779b9cbc85dSRick Macklem struct addrinfo *aip, hints; 780b9cbc85dSRick Macklem int error; 781b9cbc85dSRick Macklem 782b9cbc85dSRick Macklem dnsname = NULL; 783b9cbc85dSRick Macklem if (gethostname(hostname, MAXHOSTNAMELEN) == 0) { 784b9cbc85dSRick Macklem if ((cp = strchr(hostname, '.')) != NULL && 785b9cbc85dSRick Macklem *(cp + 1) != '\0') { 786b9cbc85dSRick Macklem *cp = '@'; 787b9cbc85dSRick Macklem dnsname = cp; 788b9cbc85dSRick Macklem } else { 789b9cbc85dSRick Macklem memset((void *)&hints, 0, sizeof (hints)); 790b9cbc85dSRick Macklem hints.ai_flags = AI_CANONNAME; 791b9cbc85dSRick Macklem error = getaddrinfo(hostname, NULL, &hints, &aip); 792b9cbc85dSRick Macklem if (error == 0) { 793b9cbc85dSRick Macklem if (aip->ai_canonname != NULL && 794b9cbc85dSRick Macklem (cp = strchr(aip->ai_canonname, '.')) != 795b9cbc85dSRick Macklem NULL && *(cp + 1) != '\0') { 796b9cbc85dSRick Macklem hostname[0] = '@'; 797b9cbc85dSRick Macklem strlcpy(&hostname[1], cp + 1, 798b9cbc85dSRick Macklem MAXHOSTNAMELEN + 1); 799b9cbc85dSRick Macklem dnsname = hostname; 800b9cbc85dSRick Macklem } 801b9cbc85dSRick Macklem freeaddrinfo(aip); 802b9cbc85dSRick Macklem } 803b9cbc85dSRick Macklem } 804b9cbc85dSRick Macklem } 805b9cbc85dSRick Macklem return (dnsname); 806b9cbc85dSRick Macklem } 807b9cbc85dSRick Macklem 808b9cbc85dSRick Macklem /* 809b9cbc85dSRick Macklem * Check for an otherName component of subjectAltName where the OID 810b9cbc85dSRick Macklem * matches and the "domain" matches that of this server. 811b9cbc85dSRick Macklem * If found, map "user" to a <uid, gidlist> for it. 812b9cbc85dSRick Macklem */ 813b9cbc85dSRick Macklem static int 814b9cbc85dSRick Macklem rpctls_cnname(X509 *cert, uint32_t *uidp, int *ngrps, uint32_t *gidp) 815b9cbc85dSRick Macklem { 816b9cbc85dSRick Macklem char *cp, usern[1024 + 1]; 817b9cbc85dSRick Macklem struct passwd *pwd; 818b9cbc85dSRick Macklem gid_t gids[NGROUPS]; 819b9cbc85dSRick Macklem int i, j; 820b9cbc85dSRick Macklem GENERAL_NAMES *genlist; 821b9cbc85dSRick Macklem GENERAL_NAME *genname; 822b9cbc85dSRick Macklem OTHERNAME *val; 823b9cbc85dSRick Macklem size_t slen; 824b9cbc85dSRick Macklem 825b9cbc85dSRick Macklem /* First, find the otherName in the subjectAltName. */ 826b9cbc85dSRick Macklem genlist = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); 827b9cbc85dSRick Macklem if (genlist == NULL) 828b9cbc85dSRick Macklem return (0); 829b9cbc85dSRick Macklem cp = NULL; 830b9cbc85dSRick Macklem for (i = 0; i < sk_GENERAL_NAME_num(genlist); i++) { 831b9cbc85dSRick Macklem genname = sk_GENERAL_NAME_value(genlist, i); 832b9cbc85dSRick Macklem if (genname->type != GEN_OTHERNAME) 833b9cbc85dSRick Macklem continue; 834b9cbc85dSRick Macklem val = genname->d.otherName; 835b9cbc85dSRick Macklem 836b9cbc85dSRick Macklem /* Check to see that it is the correct OID. */ 837b9cbc85dSRick Macklem slen = i2t_ASN1_OBJECT(usern, sizeof(usern), val->type_id); 838b9cbc85dSRick Macklem if (slen != strlen(rpctls_cnuseroid) || memcmp(usern, 839b9cbc85dSRick Macklem rpctls_cnuseroid, slen) != 0) 840b9cbc85dSRick Macklem continue; 841b9cbc85dSRick Macklem 842b9cbc85dSRick Macklem /* Sanity check the otherName. */ 843b9cbc85dSRick Macklem if (val->value->type != V_ASN1_UTF8STRING || 844b9cbc85dSRick Macklem val->value->value.utf8string->length < 3 || 845b9cbc85dSRick Macklem (size_t)val->value->value.utf8string->length > sizeof(usern) 846b9cbc85dSRick Macklem - 1) { 847b9cbc85dSRick Macklem rpctls_verbose_out("rpctls_cnname: invalid cnuser " 848b9cbc85dSRick Macklem "type=%d\n", val->value->type); 849b9cbc85dSRick Macklem continue; 850b9cbc85dSRick Macklem } 851b9cbc85dSRick Macklem 852b9cbc85dSRick Macklem /* Look for a "user" in the otherName */ 853b9cbc85dSRick Macklem memcpy(usern, val->value->value.utf8string->data, 854b9cbc85dSRick Macklem val->value->value.utf8string->length); 855b9cbc85dSRick Macklem usern[val->value->value.utf8string->length] = '\0'; 856b9cbc85dSRick Macklem 857b9cbc85dSRick Macklem /* Now, look for the @dnsname suffix in the commonName. */ 858b9cbc85dSRick Macklem cp = strcasestr(usern, rpctls_dnsname); 859b9cbc85dSRick Macklem if (cp == NULL) 860b9cbc85dSRick Macklem continue; 861b9cbc85dSRick Macklem if (*(cp + strlen(rpctls_dnsname)) != '\0') { 862b9cbc85dSRick Macklem cp = NULL; 863b9cbc85dSRick Macklem continue; 864b9cbc85dSRick Macklem } 865b9cbc85dSRick Macklem *cp = '\0'; 866b9cbc85dSRick Macklem break; 867b9cbc85dSRick Macklem } 868b9cbc85dSRick Macklem if (cp == NULL) 869b9cbc85dSRick Macklem return (0); 870b9cbc85dSRick Macklem 871b9cbc85dSRick Macklem /* See if the "user" is in the passwd database. */ 872b9cbc85dSRick Macklem pwd = getpwnam(usern); 873b9cbc85dSRick Macklem if (pwd == NULL) 874b9cbc85dSRick Macklem return (0); 875b9cbc85dSRick Macklem *uidp = pwd->pw_uid; 876b9cbc85dSRick Macklem *ngrps = NGROUPS; 877b9cbc85dSRick Macklem if (getgrouplist(pwd->pw_name, pwd->pw_gid, gids, ngrps) < 0) 878b9cbc85dSRick Macklem return (0); 879b9cbc85dSRick Macklem rpctls_verbose_out("mapped user=%s ngrps=%d uid=%d\n", pwd->pw_name, 880b9cbc85dSRick Macklem *ngrps, pwd->pw_uid); 881b9cbc85dSRick Macklem for (j = 0; j < *ngrps; j++) 882b9cbc85dSRick Macklem gidp[j] = gids[j]; 883b9cbc85dSRick Macklem return (1); 884b9cbc85dSRick Macklem } 885b9cbc85dSRick Macklem 886b9cbc85dSRick Macklem static void 887b9cbc85dSRick Macklem rpctls_huphandler(int sig __unused) 888b9cbc85dSRick Macklem { 889b9cbc85dSRick Macklem 890b9cbc85dSRick Macklem rpctls_gothup = true; 891b9cbc85dSRick Macklem } 892