xref: /freebsd/usr.sbin/rpc.tlsservd/rpc.tlsservd.c (revision 8d098deda3786b223e44ad1bed923a6d66dfa341)
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";
107712aac13SRick Macklem static const char	*rpctls_ciphers = NULL;
1080637b12bSRick Macklem static int		rpctls_mintls = TLS1_3_VERSION;
109b9cbc85dSRick Macklem 
110b9cbc85dSRick Macklem static void		rpctlssd_terminate(int);
111b9cbc85dSRick Macklem static SSL_CTX		*rpctls_setup_ssl(const char *certdir);
112b9cbc85dSRick Macklem static SSL		*rpctls_server(SSL_CTX *ctx, int s,
113b9cbc85dSRick Macklem 			    uint32_t *flags, uint32_t *uidp,
114b9cbc85dSRick Macklem 			    int *ngrps, uint32_t *gidp, X509 **certp);
115b9cbc85dSRick Macklem static int		rpctls_cnname(X509 *cert, uint32_t *uidp,
116b9cbc85dSRick Macklem 			    int *ngrps, uint32_t *gidp);
117b9cbc85dSRick Macklem static char		*rpctls_getdnsname(char *dnsname);
118b9cbc85dSRick Macklem static void		rpctls_huphandler(int sig __unused);
119b9cbc85dSRick Macklem 
120b9cbc85dSRick Macklem extern void		rpctlssd_1(struct svc_req *rqstp, SVCXPRT *transp);
121b9cbc85dSRick Macklem 
122b9cbc85dSRick Macklem static struct option longopts[] = {
1230637b12bSRick Macklem 	{ "allowtls1_2",	no_argument,		NULL,	'2' },
124712aac13SRick Macklem 	{ "ciphers",		required_argument,	NULL,	'C' },
125b9cbc85dSRick Macklem 	{ "certdir",		required_argument,	NULL,	'D' },
126b9cbc85dSRick Macklem 	{ "debuglevel",		no_argument,		NULL,	'd' },
127b9cbc85dSRick Macklem 	{ "checkhost",		no_argument,		NULL,	'h' },
128b9cbc85dSRick Macklem 	{ "verifylocs",		required_argument,	NULL,	'l' },
129b9cbc85dSRick Macklem 	{ "mutualverf",		no_argument,		NULL,	'm' },
130b9cbc85dSRick Macklem 	{ "domain",		required_argument,	NULL,	'n' },
131b9cbc85dSRick Macklem 	{ "verifydir",		required_argument,	NULL,	'p' },
132b9cbc85dSRick Macklem 	{ "crl",		required_argument,	NULL,	'r' },
133b9cbc85dSRick Macklem 	{ "certuser",		no_argument,		NULL,	'u' },
134b9cbc85dSRick Macklem 	{ "verbose",		no_argument,		NULL,	'v' },
135b9cbc85dSRick Macklem 	{ "multiwild",		no_argument,		NULL,	'W' },
136b9cbc85dSRick Macklem 	{ "singlewild",		no_argument,		NULL,	'w' },
137b9cbc85dSRick Macklem 	{ NULL,			0,			NULL,	0  }
138b9cbc85dSRick Macklem };
139b9cbc85dSRick Macklem 
140b9cbc85dSRick Macklem int
141b9cbc85dSRick Macklem main(int argc, char **argv)
142b9cbc85dSRick Macklem {
143b9cbc85dSRick Macklem 	/*
144b9cbc85dSRick Macklem 	 * We provide an RPC service on a local-domain socket. The
145b9cbc85dSRick Macklem 	 * kernel rpctls code will upcall to this daemon to do the initial
146b9cbc85dSRick Macklem 	 * TLS handshake.
147b9cbc85dSRick Macklem 	 */
148b9cbc85dSRick Macklem 	struct sockaddr_un sun;
149350668ebSRick Macklem 	int ch, fd, oldmask;
150b9cbc85dSRick Macklem 	SVCXPRT *xprt;
151b9cbc85dSRick Macklem 	struct timeval tm;
152b9cbc85dSRick Macklem 	struct timezone tz;
153b9cbc85dSRick Macklem 	char hostname[MAXHOSTNAMELEN + 2];
154b9cbc85dSRick Macklem 	pid_t otherpid;
155b9cbc85dSRick Macklem 	bool tls_enable;
156b9cbc85dSRick Macklem 	size_t tls_enable_len;
157b9cbc85dSRick Macklem 
158b9cbc85dSRick Macklem 	/* Check that another rpctlssd isn't already running. */
159b9cbc85dSRick Macklem 	rpctls_pfh = pidfile_open(_PATH_RPCTLSSDPID, 0600, &otherpid);
160b9cbc85dSRick Macklem 	if (rpctls_pfh == NULL) {
161b9cbc85dSRick Macklem 		if (errno == EEXIST)
162b9cbc85dSRick Macklem 			errx(1, "rpctlssd already running, pid: %d.", otherpid);
163b9cbc85dSRick Macklem 		warn("cannot open or create pidfile");
164b9cbc85dSRick Macklem 	}
165b9cbc85dSRick Macklem 
166b9cbc85dSRick Macklem 	/* Check to see that the ktls is enabled. */
167b9cbc85dSRick Macklem 	tls_enable_len = sizeof(tls_enable);
168b9cbc85dSRick Macklem 	if (sysctlbyname("kern.ipc.tls.enable", &tls_enable, &tls_enable_len,
169b9cbc85dSRick Macklem 	    NULL, 0) != 0 || !tls_enable)
170b9cbc85dSRick Macklem 		errx(1, "Kernel TLS not enabled");
171b9cbc85dSRick Macklem 
172b9cbc85dSRick Macklem 	/* Get the time when this daemon is started. */
173b9cbc85dSRick Macklem 	gettimeofday(&tm, &tz);
174b9cbc85dSRick Macklem 	rpctls_ssl_sec = tm.tv_sec;
175b9cbc85dSRick Macklem 	rpctls_ssl_usec = tm.tv_usec;
176b9cbc85dSRick Macklem 
177b9cbc85dSRick Macklem 	/* Set the dns name for the server. */
178b9cbc85dSRick Macklem 	rpctls_dnsname = rpctls_getdnsname(hostname);
179b9cbc85dSRick Macklem 	if (rpctls_dnsname == NULL) {
180b9cbc85dSRick Macklem 		strcpy(hostname, "@default.domain");
181b9cbc85dSRick Macklem 		rpctls_dnsname = hostname;
182b9cbc85dSRick Macklem 	}
183b9cbc85dSRick Macklem 
184b9cbc85dSRick Macklem 	rpctls_verbose = false;
1850637b12bSRick Macklem 	while ((ch = getopt_long(argc, argv, "2C:D:dhl:n:mp:r:uvWw", longopts,
186b9cbc85dSRick Macklem 	    NULL)) != -1) {
187b9cbc85dSRick Macklem 		switch (ch) {
1880637b12bSRick Macklem 		case '2':
1890637b12bSRick Macklem 			rpctls_mintls = TLS1_2_VERSION;
1900637b12bSRick Macklem 			break;
191712aac13SRick Macklem 		case 'C':
192712aac13SRick Macklem 			rpctls_ciphers = optarg;
193712aac13SRick Macklem 			break;
194b9cbc85dSRick Macklem 		case 'D':
195b9cbc85dSRick Macklem 			rpctls_certdir = optarg;
196b9cbc85dSRick Macklem 			break;
197b9cbc85dSRick Macklem 		case 'd':
198b9cbc85dSRick Macklem 			rpctls_debug_level++;
199b9cbc85dSRick Macklem 			break;
200b9cbc85dSRick Macklem 		case 'h':
201b9cbc85dSRick Macklem 			rpctls_comparehost = true;
202b9cbc85dSRick Macklem 			break;
203b9cbc85dSRick Macklem 		case 'l':
204b9cbc85dSRick Macklem 			rpctls_verify_cafile = optarg;
205b9cbc85dSRick Macklem 			break;
206b9cbc85dSRick Macklem 		case 'm':
207b9cbc85dSRick Macklem 			rpctls_do_mutual = true;
208b9cbc85dSRick Macklem 			break;
209b9cbc85dSRick Macklem 		case 'n':
210b9cbc85dSRick Macklem 			hostname[0] = '@';
211b9cbc85dSRick Macklem 			strlcpy(&hostname[1], optarg, MAXHOSTNAMELEN + 1);
212b9cbc85dSRick Macklem 			rpctls_dnsname = hostname;
213b9cbc85dSRick Macklem 			break;
214b9cbc85dSRick Macklem 		case 'p':
215b9cbc85dSRick Macklem 			rpctls_verify_capath = optarg;
216b9cbc85dSRick Macklem 			break;
217b9cbc85dSRick Macklem 		case 'r':
218b9cbc85dSRick Macklem 			rpctls_crlfile = optarg;
219b9cbc85dSRick Macklem 			break;
220b9cbc85dSRick Macklem 		case 'u':
221b9cbc85dSRick Macklem 			rpctls_cnuser = true;
222b9cbc85dSRick Macklem 			break;
223b9cbc85dSRick Macklem 		case 'v':
224b9cbc85dSRick Macklem 			rpctls_verbose = true;
225b9cbc85dSRick Macklem 			break;
226b9cbc85dSRick Macklem 		case 'W':
227b9cbc85dSRick Macklem 			if (rpctls_wildcard != X509_CHECK_FLAG_NO_WILDCARDS)
228b9cbc85dSRick Macklem 				errx(1, "options -w and -W are mutually "
229b9cbc85dSRick Macklem 				    "exclusive");
230b9cbc85dSRick Macklem 			rpctls_wildcard = X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS;
231b9cbc85dSRick Macklem 			break;
232b9cbc85dSRick Macklem 		case 'w':
233b9cbc85dSRick Macklem 			if (rpctls_wildcard != X509_CHECK_FLAG_NO_WILDCARDS)
234b9cbc85dSRick Macklem 				errx(1, "options -w and -W are mutually "
235b9cbc85dSRick Macklem 				    "exclusive");
236b9cbc85dSRick Macklem 			rpctls_wildcard = 0;
237b9cbc85dSRick Macklem 			break;
238b9cbc85dSRick Macklem 		default:
239b9cbc85dSRick Macklem 			fprintf(stderr, "usage: %s "
240*8d098dedSRick Macklem 			    "[-C/--ciphers available_ciphers] "
241b9cbc85dSRick Macklem 			    "[-D/--certdir certdir] [-d/--debuglevel] "
242b9cbc85dSRick Macklem 			    "[-h/--checkhost] "
243b9cbc85dSRick Macklem 			    "[-l/--verifylocs CAfile] [-m/--mutualverf] "
244b9cbc85dSRick Macklem 			    "[-n/--domain domain_name] "
245b9cbc85dSRick Macklem 			    "[-p/--verifydir CApath] [-r/--crl CRLfile] "
246b9cbc85dSRick Macklem 			    "[-u/--certuser] [-v/--verbose] [-W/--multiwild] "
247b9cbc85dSRick Macklem 			    "[-w/--singlewild]\n", argv[0]);
248b9cbc85dSRick Macklem 			exit(1);
249b9cbc85dSRick Macklem 		}
250b9cbc85dSRick Macklem 	}
251b9cbc85dSRick Macklem 	if (rpctls_do_mutual && rpctls_verify_cafile == NULL &&
252b9cbc85dSRick Macklem 	    rpctls_verify_capath == NULL)
253b9cbc85dSRick Macklem 		errx(1, "-m requires the -l <CAfile> and/or "
254b9cbc85dSRick Macklem 		    "-p <CApath> options");
255b9cbc85dSRick Macklem 	if (rpctls_comparehost && (!rpctls_do_mutual ||
256b9cbc85dSRick Macklem 	    (rpctls_verify_cafile == NULL && rpctls_verify_capath == NULL)))
257b9cbc85dSRick Macklem 		errx(1, "-h requires the -m plus the "
258b9cbc85dSRick Macklem 		    "-l <CAfile> and/or -p <CApath> options");
259b9cbc85dSRick Macklem 	if (!rpctls_comparehost && rpctls_wildcard !=
260b9cbc85dSRick Macklem 	    X509_CHECK_FLAG_NO_WILDCARDS)
261b9cbc85dSRick Macklem 		errx(1, "The -w or -W options require the -h option");
262b9cbc85dSRick Macklem 	if (rpctls_cnuser && (!rpctls_do_mutual ||
263b9cbc85dSRick Macklem 	    (rpctls_verify_cafile == NULL && rpctls_verify_capath == NULL)))
264b9cbc85dSRick Macklem 		errx(1, "-u requires the -m plus the "
265b9cbc85dSRick Macklem 		    "-l <CAfile> and/or -p <CApath> options");
266b9cbc85dSRick Macklem 
267b9cbc85dSRick Macklem 	if (modfind("krpc") < 0) {
268b9cbc85dSRick Macklem 		/* Not present in kernel, try loading it */
269b9cbc85dSRick Macklem 		if (kldload("krpc") < 0 || modfind("krpc") < 0)
270b9cbc85dSRick Macklem 			errx(1, "Kernel RPC is not available");
271b9cbc85dSRick Macklem 	}
272b9cbc85dSRick Macklem 
273b9cbc85dSRick Macklem 	if (rpctls_debug_level == 0) {
274b9cbc85dSRick Macklem 		if (daemon(0, 0) != 0)
275b9cbc85dSRick Macklem 			err(1, "Can't daemonize");
276b9cbc85dSRick Macklem 		signal(SIGINT, SIG_IGN);
277b9cbc85dSRick Macklem 		signal(SIGQUIT, SIG_IGN);
278b9cbc85dSRick Macklem 		signal(SIGHUP, SIG_IGN);
279b9cbc85dSRick Macklem 	}
280b9cbc85dSRick Macklem 	signal(SIGTERM, rpctlssd_terminate);
281b9cbc85dSRick Macklem 	signal(SIGPIPE, SIG_IGN);
282b9cbc85dSRick Macklem 	signal(SIGHUP, rpctls_huphandler);
283b9cbc85dSRick Macklem 
284b9cbc85dSRick Macklem 	pidfile_write(rpctls_pfh);
285b9cbc85dSRick Macklem 
286b9cbc85dSRick Macklem 	memset(&sun, 0, sizeof sun);
287b9cbc85dSRick Macklem 	sun.sun_family = AF_LOCAL;
288b9cbc85dSRick Macklem 	unlink(_PATH_RPCTLSSDSOCK);
289b9cbc85dSRick Macklem 	strcpy(sun.sun_path, _PATH_RPCTLSSDSOCK);
290b9cbc85dSRick Macklem 	sun.sun_len = SUN_LEN(&sun);
291b9cbc85dSRick Macklem 	fd = socket(AF_LOCAL, SOCK_STREAM, 0);
292b9cbc85dSRick Macklem 	if (fd < 0) {
293b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0) {
294b9cbc85dSRick Macklem 			syslog(LOG_ERR, "Can't create local rpctlssd socket");
295b9cbc85dSRick Macklem 			exit(1);
296b9cbc85dSRick Macklem 		}
297b9cbc85dSRick Macklem 		err(1, "Can't create local rpctlssd socket");
298b9cbc85dSRick Macklem 	}
299b9cbc85dSRick Macklem 	oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
300b9cbc85dSRick Macklem 	if (bind(fd, (struct sockaddr *)&sun, sun.sun_len) < 0) {
301b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0) {
302b9cbc85dSRick Macklem 			syslog(LOG_ERR, "Can't bind local rpctlssd socket");
303b9cbc85dSRick Macklem 			exit(1);
304b9cbc85dSRick Macklem 		}
305b9cbc85dSRick Macklem 		err(1, "Can't bind local rpctlssd socket");
306b9cbc85dSRick Macklem 	}
307b9cbc85dSRick Macklem 	umask(oldmask);
308b9cbc85dSRick Macklem 	if (listen(fd, SOMAXCONN) < 0) {
309b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0) {
310b9cbc85dSRick Macklem 			syslog(LOG_ERR,
311b9cbc85dSRick Macklem 			    "Can't listen on local rpctlssd socket");
312b9cbc85dSRick Macklem 			exit(1);
313b9cbc85dSRick Macklem 		}
314b9cbc85dSRick Macklem 		err(1, "Can't listen on local rpctlssd socket");
315b9cbc85dSRick Macklem 	}
316b9cbc85dSRick Macklem 	xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
317b9cbc85dSRick Macklem 	if (!xprt) {
318b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0) {
319b9cbc85dSRick Macklem 			syslog(LOG_ERR,
320b9cbc85dSRick Macklem 			    "Can't create transport for local rpctlssd socket");
321b9cbc85dSRick Macklem 			exit(1);
322b9cbc85dSRick Macklem 		}
323b9cbc85dSRick Macklem 		err(1, "Can't create transport for local rpctlssd socket");
324b9cbc85dSRick Macklem 	}
325b9cbc85dSRick Macklem 	if (!svc_reg(xprt, RPCTLSSD, RPCTLSSDVERS, rpctlssd_1, NULL)) {
326b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0) {
327b9cbc85dSRick Macklem 			syslog(LOG_ERR,
328b9cbc85dSRick Macklem 			    "Can't register service for local rpctlssd socket");
329b9cbc85dSRick Macklem 			exit(1);
330b9cbc85dSRick Macklem 		}
331b9cbc85dSRick Macklem 		err(1, "Can't register service for local rpctlssd socket");
332b9cbc85dSRick Macklem 	}
333b9cbc85dSRick Macklem 
334b9cbc85dSRick Macklem 	rpctls_ctx = rpctls_setup_ssl(rpctls_certdir);
335b9cbc85dSRick Macklem 	if (rpctls_ctx == NULL) {
336b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0) {
337b9cbc85dSRick Macklem 			syslog(LOG_ERR, "Can't create SSL context");
338b9cbc85dSRick Macklem 			exit(1);
339b9cbc85dSRick Macklem 		}
340b9cbc85dSRick Macklem 		err(1, "Can't create SSL context");
341b9cbc85dSRick Macklem 	}
342b9cbc85dSRick Macklem 	rpctls_gothup = false;
343b9cbc85dSRick Macklem 	LIST_INIT(&rpctls_ssllist);
344b9cbc85dSRick Macklem 
345b9cbc85dSRick Macklem 	rpctls_syscall(RPCTLS_SYSC_SRVSETPATH, _PATH_RPCTLSSDSOCK);
346b9cbc85dSRick Macklem 
347b9cbc85dSRick Macklem 	rpctls_svc_run();
348b9cbc85dSRick Macklem 
349b9cbc85dSRick Macklem 	rpctls_syscall(RPCTLS_SYSC_SRVSHUTDOWN, "");
350b9cbc85dSRick Macklem 
351b9cbc85dSRick Macklem 	SSL_CTX_free(rpctls_ctx);
352b9cbc85dSRick Macklem 	EVP_cleanup();
353b9cbc85dSRick Macklem 	return (0);
354b9cbc85dSRick Macklem }
355b9cbc85dSRick Macklem 
356b9cbc85dSRick Macklem bool_t
357b9cbc85dSRick Macklem rpctlssd_null_1_svc(__unused void *argp, __unused void *result,
358b9cbc85dSRick Macklem     __unused struct svc_req *rqstp)
359b9cbc85dSRick Macklem {
360b9cbc85dSRick Macklem 
361b9cbc85dSRick Macklem 	rpctls_verbose_out("rpctlssd_null_svc: done\n");
362b9cbc85dSRick Macklem 	return (TRUE);
363b9cbc85dSRick Macklem }
364b9cbc85dSRick Macklem 
365b9cbc85dSRick Macklem bool_t
366b9cbc85dSRick Macklem rpctlssd_connect_1_svc(__unused void *argp,
367b9cbc85dSRick Macklem     struct rpctlssd_connect_res *result, __unused struct svc_req *rqstp)
368b9cbc85dSRick Macklem {
369b9cbc85dSRick Macklem 	int ngrps, s;
370b9cbc85dSRick Macklem 	SSL *ssl;
371b9cbc85dSRick Macklem 	uint32_t flags;
372b9cbc85dSRick Macklem 	struct ssl_entry *newslp;
373b9cbc85dSRick Macklem 	uint32_t uid;
374b9cbc85dSRick Macklem 	uint32_t *gidp;
375b9cbc85dSRick Macklem 	X509 *cert;
376b9cbc85dSRick Macklem 
377b9cbc85dSRick Macklem 	rpctls_verbose_out("rpctlsd_connect_svc: started\n");
378b9cbc85dSRick Macklem 	memset(result, 0, sizeof(*result));
379b9cbc85dSRick Macklem 	/* Get the socket fd from the kernel. */
380b9cbc85dSRick Macklem 	s = rpctls_syscall(RPCTLS_SYSC_SRVSOCKET, "");
381b9cbc85dSRick Macklem 	if (s < 0)
382b9cbc85dSRick Macklem 		return (FALSE);
383b9cbc85dSRick Macklem 
384b9cbc85dSRick Macklem 	/* Do the server side of a TLS handshake. */
385b9cbc85dSRick Macklem 	gidp = calloc(NGROUPS, sizeof(*gidp));
386b9cbc85dSRick Macklem 	ssl = rpctls_server(rpctls_ctx, s, &flags, &uid, &ngrps, gidp, &cert);
387b9cbc85dSRick Macklem 	if (ssl == NULL) {
388b9cbc85dSRick Macklem 		free(gidp);
389b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctlssd_connect_svc: ssl "
390b9cbc85dSRick Macklem 		    "accept failed\n");
391b9cbc85dSRick Macklem 		/*
392b9cbc85dSRick Macklem 		 * For RPC-over-TLS, this upcall is expected
393b9cbc85dSRick Macklem 		 * to close off the socket upon handshake failure.
394b9cbc85dSRick Macklem 		 */
395b9cbc85dSRick Macklem 		close(s);
396b9cbc85dSRick Macklem 		return (FALSE);
397b9cbc85dSRick Macklem 	} else {
398b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctlssd_connect_svc: "
399b9cbc85dSRick Macklem 		    "succeeded flags=0x%x\n", flags);
400b9cbc85dSRick Macklem 		result->flags = flags;
401b9cbc85dSRick Macklem 		result->sec = rpctls_ssl_sec;
402b9cbc85dSRick Macklem 		result->usec = rpctls_ssl_usec;
403b9cbc85dSRick Macklem 		result->ssl = ++rpctls_ssl_refno;
404b9cbc85dSRick Macklem 		/* Hard to believe this could ever wrap around.. */
405b9cbc85dSRick Macklem 		if (rpctls_ssl_refno == 0)
406b9cbc85dSRick Macklem 			result->ssl = ++rpctls_ssl_refno;
407b9cbc85dSRick Macklem 		if ((flags & RPCTLS_FLAGS_CERTUSER) != 0) {
408b9cbc85dSRick Macklem 			result->uid = uid;
409b9cbc85dSRick Macklem 			result->gid.gid_len = ngrps;
410b9cbc85dSRick Macklem 			result->gid.gid_val = gidp;
411b9cbc85dSRick Macklem 		} else {
412b9cbc85dSRick Macklem 			result->uid = 0;
413b9cbc85dSRick Macklem 			result->gid.gid_len = 0;
414b9cbc85dSRick Macklem 			result->gid.gid_val = gidp;
415b9cbc85dSRick Macklem 		}
416b9cbc85dSRick Macklem 	}
417b9cbc85dSRick Macklem 
418b9cbc85dSRick Macklem 	/* Maintain list of all current SSL *'s */
419b9cbc85dSRick Macklem 	newslp = malloc(sizeof(*newslp));
420b9cbc85dSRick Macklem 	newslp->ssl = ssl;
421b9cbc85dSRick Macklem 	newslp->s = s;
422b9cbc85dSRick Macklem 	newslp->shutoff = false;
423b9cbc85dSRick Macklem 	newslp->refno = rpctls_ssl_refno;
424b9cbc85dSRick Macklem 	newslp->cert = cert;
425b9cbc85dSRick Macklem 	LIST_INSERT_HEAD(&rpctls_ssllist, newslp, next);
426b9cbc85dSRick Macklem 	return (TRUE);
427b9cbc85dSRick Macklem }
428b9cbc85dSRick Macklem 
429b9cbc85dSRick Macklem bool_t
430b9cbc85dSRick Macklem rpctlssd_handlerecord_1_svc(struct rpctlssd_handlerecord_arg *argp,
431b9cbc85dSRick Macklem     struct rpctlssd_handlerecord_res *result, __unused struct svc_req *rqstp)
432b9cbc85dSRick Macklem {
433b9cbc85dSRick Macklem 	struct ssl_entry *slp;
434b9cbc85dSRick Macklem 	int ret;
435b9cbc85dSRick Macklem 	char junk;
436b9cbc85dSRick Macklem 
437b9cbc85dSRick Macklem 	slp = NULL;
438b9cbc85dSRick Macklem 	if (argp->sec == rpctls_ssl_sec && argp->usec ==
439b9cbc85dSRick Macklem 	    rpctls_ssl_usec) {
440b9cbc85dSRick Macklem 		LIST_FOREACH(slp, &rpctls_ssllist, next) {
441b9cbc85dSRick Macklem 			if (slp->refno == argp->ssl)
442b9cbc85dSRick Macklem 				break;
443b9cbc85dSRick Macklem 		}
444b9cbc85dSRick Macklem 	}
445b9cbc85dSRick Macklem 
446b9cbc85dSRick Macklem 	if (slp != NULL) {
447b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctlssd_handlerecord fd=%d\n",
448b9cbc85dSRick Macklem 		    slp->s);
449b9cbc85dSRick Macklem 		/*
450b9cbc85dSRick Macklem 		 * An SSL_read() of 0 bytes should fail, but it should
451b9cbc85dSRick Macklem 		 * handle the non-application data record before doing so.
452b9cbc85dSRick Macklem 		 */
453b9cbc85dSRick Macklem 		ret = SSL_read(slp->ssl, &junk, 0);
454b9cbc85dSRick Macklem 		if (ret <= 0) {
455b9cbc85dSRick Macklem 			/* Check to see if this was a close alert. */
456b9cbc85dSRick Macklem 			ret = SSL_get_shutdown(slp->ssl);
457b9cbc85dSRick Macklem 			if ((ret & (SSL_SENT_SHUTDOWN |
458b9cbc85dSRick Macklem 			    SSL_RECEIVED_SHUTDOWN)) == SSL_RECEIVED_SHUTDOWN)
459b9cbc85dSRick Macklem 				SSL_shutdown(slp->ssl);
460b9cbc85dSRick Macklem 		} else {
461b9cbc85dSRick Macklem 			if (rpctls_debug_level == 0)
462b9cbc85dSRick Macklem 				syslog(LOG_ERR, "SSL_read returned %d", ret);
463b9cbc85dSRick Macklem 			else
464b9cbc85dSRick Macklem 				fprintf(stderr, "SSL_read returned %d\n", ret);
465b9cbc85dSRick Macklem 		}
466b9cbc85dSRick Macklem 		result->reterr = RPCTLSERR_OK;
467b9cbc85dSRick Macklem 	} else
468b9cbc85dSRick Macklem 		result->reterr = RPCTLSERR_NOSSL;
469b9cbc85dSRick Macklem 	return (TRUE);
470b9cbc85dSRick Macklem }
471b9cbc85dSRick Macklem 
472b9cbc85dSRick Macklem bool_t
473b9cbc85dSRick Macklem rpctlssd_disconnect_1_svc(struct rpctlssd_disconnect_arg *argp,
474b9cbc85dSRick Macklem     struct rpctlssd_disconnect_res *result, __unused struct svc_req *rqstp)
475b9cbc85dSRick Macklem {
476b9cbc85dSRick Macklem 	struct ssl_entry *slp;
477b9cbc85dSRick Macklem 	int ret;
478b9cbc85dSRick Macklem 
479b9cbc85dSRick Macklem 	slp = NULL;
480b9cbc85dSRick Macklem 	if (argp->sec == rpctls_ssl_sec && argp->usec ==
481b9cbc85dSRick Macklem 	    rpctls_ssl_usec) {
482b9cbc85dSRick Macklem 		LIST_FOREACH(slp, &rpctls_ssllist, next) {
483b9cbc85dSRick Macklem 			if (slp->refno == argp->ssl)
484b9cbc85dSRick Macklem 				break;
485b9cbc85dSRick Macklem 		}
486b9cbc85dSRick Macklem 	}
487b9cbc85dSRick Macklem 
488b9cbc85dSRick Macklem 	if (slp != NULL) {
489b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctlssd_disconnect fd=%d closed\n",
490b9cbc85dSRick Macklem 		    slp->s);
491b9cbc85dSRick Macklem 		LIST_REMOVE(slp, next);
492b9cbc85dSRick Macklem 		if (!slp->shutoff) {
493b9cbc85dSRick Macklem 			ret = SSL_get_shutdown(slp->ssl);
494b9cbc85dSRick Macklem 			/*
495b9cbc85dSRick Macklem 			 * Do an SSL_shutdown() unless a close alert has
496b9cbc85dSRick Macklem 			 * already been sent.
497b9cbc85dSRick Macklem 			 */
498b9cbc85dSRick Macklem 			if ((ret & SSL_SENT_SHUTDOWN) == 0)
499b9cbc85dSRick Macklem 				SSL_shutdown(slp->ssl);
500b9cbc85dSRick Macklem 		}
501b9cbc85dSRick Macklem 		SSL_free(slp->ssl);
502b9cbc85dSRick Macklem 		if (slp->cert != NULL)
503b9cbc85dSRick Macklem 			X509_free(slp->cert);
504b9cbc85dSRick Macklem 		/*
505b9cbc85dSRick Macklem 		 * For RPC-over-TLS, this upcall is expected
506b9cbc85dSRick Macklem 		 * to close off the socket.
507b9cbc85dSRick Macklem 		 */
508b9cbc85dSRick Macklem 		if (!slp->shutoff)
509b9cbc85dSRick Macklem 			shutdown(slp->s, SHUT_WR);
510b9cbc85dSRick Macklem 		close(slp->s);
511b9cbc85dSRick Macklem 		free(slp);
512b9cbc85dSRick Macklem 		result->reterr = RPCTLSERR_OK;
513b9cbc85dSRick Macklem 	} else
514b9cbc85dSRick Macklem 		result->reterr = RPCTLSERR_NOCLOSE;
515b9cbc85dSRick Macklem 	return (TRUE);
516b9cbc85dSRick Macklem }
517b9cbc85dSRick Macklem 
518b9cbc85dSRick Macklem int
519b9cbc85dSRick Macklem rpctlssd_1_freeresult(__unused SVCXPRT *transp, xdrproc_t xdr_result,
520b9cbc85dSRick Macklem     caddr_t result)
521b9cbc85dSRick Macklem {
522b9cbc85dSRick Macklem 	rpctlssd_connect_res *res;
523b9cbc85dSRick Macklem 
524b9cbc85dSRick Macklem 	if (xdr_result == (xdrproc_t)xdr_rpctlssd_connect_res) {
525b9cbc85dSRick Macklem 		res = (rpctlssd_connect_res *)(void *)result;
526b9cbc85dSRick Macklem 		free(res->gid.gid_val);
527b9cbc85dSRick Macklem 	}
528b9cbc85dSRick Macklem 	return (TRUE);
529b9cbc85dSRick Macklem }
530b9cbc85dSRick Macklem 
531b9cbc85dSRick Macklem static void
532b9cbc85dSRick Macklem rpctlssd_terminate(int sig __unused)
533b9cbc85dSRick Macklem {
534b9cbc85dSRick Macklem 	struct ssl_entry *slp;
535b9cbc85dSRick Macklem 
536b9cbc85dSRick Macklem 	rpctls_syscall(RPCTLS_SYSC_SRVSHUTDOWN, "");
537b9cbc85dSRick Macklem 	pidfile_remove(rpctls_pfh);
538b9cbc85dSRick Macklem 
539b9cbc85dSRick Macklem 	LIST_FOREACH(slp, &rpctls_ssllist, next)
540b9cbc85dSRick Macklem 		shutdown(slp->s, SHUT_RD);
541b9cbc85dSRick Macklem 	exit(0);
542b9cbc85dSRick Macklem }
543b9cbc85dSRick Macklem 
544b9cbc85dSRick Macklem /* Allow the handshake to proceed. */
545b9cbc85dSRick Macklem static int
546b9cbc85dSRick Macklem rpctls_verify_callback(__unused int preverify_ok,
547b9cbc85dSRick Macklem     __unused X509_STORE_CTX *x509_ctx)
548b9cbc85dSRick Macklem {
549b9cbc85dSRick Macklem 
550b9cbc85dSRick Macklem 	return (1);
551b9cbc85dSRick Macklem }
552b9cbc85dSRick Macklem 
553b9cbc85dSRick Macklem static SSL_CTX *
554b9cbc85dSRick Macklem rpctls_setup_ssl(const char *certdir)
555b9cbc85dSRick Macklem {
556b9cbc85dSRick Macklem 	SSL_CTX *ctx;
557b9cbc85dSRick Macklem 	char path[PATH_MAX];
558b9cbc85dSRick Macklem 	size_t len, rlen;
559b9cbc85dSRick Macklem 	int ret;
560b9cbc85dSRick Macklem 
561b9cbc85dSRick Macklem 	SSL_library_init();
562b9cbc85dSRick Macklem 	SSL_load_error_strings();
563b9cbc85dSRick Macklem 	OpenSSL_add_all_algorithms();
564b9cbc85dSRick Macklem 
565b9cbc85dSRick Macklem 	ctx = SSL_CTX_new(TLS_server_method());
566b9cbc85dSRick Macklem 	if (ctx == NULL) {
567b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctls_setup_ssl: SSL_CTX_new failed\n");
568b9cbc85dSRick Macklem 		return (NULL);
569b9cbc85dSRick Macklem 	}
570b9cbc85dSRick Macklem 	SSL_CTX_set_ecdh_auto(ctx, 1);
571b9cbc85dSRick Macklem 
572712aac13SRick Macklem 	if (rpctls_ciphers != NULL) {
573b9cbc85dSRick Macklem 		/*
574*8d098dedSRick Macklem 		 * Set available ciphers, since KERN_TLS only supports a
575712aac13SRick Macklem 		 * few of them.  Normally, not doing this should be ok,
576712aac13SRick Macklem 		 * since the library defaults will work.
577b9cbc85dSRick Macklem 		 */
578*8d098dedSRick Macklem 		ret = SSL_CTX_set_ciphersuites(ctx, rpctls_ciphers);
579b9cbc85dSRick Macklem 		if (ret == 0) {
580b9cbc85dSRick Macklem 			rpctls_verbose_out("rpctls_setup_ssl: "
581*8d098dedSRick Macklem 			    "SSL_CTX_set_ciphersuites failed: %s\n",
582712aac13SRick Macklem 			    rpctls_ciphers);
583b9cbc85dSRick Macklem 			SSL_CTX_free(ctx);
584b9cbc85dSRick Macklem 			return (NULL);
585b9cbc85dSRick Macklem 		}
586712aac13SRick Macklem 	}
587b9cbc85dSRick Macklem 
5880637b12bSRick Macklem 	ret = SSL_CTX_set_min_proto_version(ctx, rpctls_mintls);
5890637b12bSRick Macklem 	if (ret == 0) {
5900637b12bSRick Macklem 		rpctls_verbose_out("rpctls_setup_ssl: "
5910637b12bSRick Macklem 		    "SSL_CTX_set_min_proto_version failed\n");
5920637b12bSRick Macklem 		SSL_CTX_free(ctx);
5930637b12bSRick Macklem 		return (NULL);
5940637b12bSRick Macklem 	}
5950637b12bSRick Macklem 	ret = SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
5960637b12bSRick Macklem 	if (ret == 0) {
5970637b12bSRick Macklem 		rpctls_verbose_out("rpctls_setup_ssl: "
5980637b12bSRick Macklem 		    "SSL_CTX_set_max_proto_version failed\n");
5990637b12bSRick Macklem 		SSL_CTX_free(ctx);
6000637b12bSRick Macklem 		return (NULL);
6010637b12bSRick Macklem 	}
6020637b12bSRick Macklem 
603b9cbc85dSRick Macklem 	/* Get the cert.pem and certkey.pem files from the directory certdir. */
604b9cbc85dSRick Macklem 	len = strlcpy(path, certdir, sizeof(path));
605b9cbc85dSRick Macklem 	rlen = sizeof(path) - len;
606b9cbc85dSRick Macklem 	if (strlcpy(&path[len], "cert.pem", rlen) != 8) {
607b9cbc85dSRick Macklem 		SSL_CTX_free(ctx);
608b9cbc85dSRick Macklem 		return (NULL);
609b9cbc85dSRick Macklem 	}
610b9cbc85dSRick Macklem 	ret = SSL_CTX_use_certificate_file(ctx, path, SSL_FILETYPE_PEM);
611b9cbc85dSRick Macklem 	if (ret != 1) {
612b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctls_setup_ssl: can't use certificate "
613b9cbc85dSRick Macklem 		    "file path=%s ret=%d\n", path, ret);
614b9cbc85dSRick Macklem 		SSL_CTX_free(ctx);
615b9cbc85dSRick Macklem 		return (NULL);
616b9cbc85dSRick Macklem 	}
617b9cbc85dSRick Macklem 	if (strlcpy(&path[len], "certkey.pem", rlen) != 11) {
618b9cbc85dSRick Macklem 		SSL_CTX_free(ctx);
619b9cbc85dSRick Macklem 		return (NULL);
620b9cbc85dSRick Macklem 	}
621b9cbc85dSRick Macklem 	ret = SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM);
622b9cbc85dSRick Macklem 	if (ret != 1) {
623b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctls_setup_ssl: Can't use private "
624b9cbc85dSRick Macklem 		    "key path=%s ret=%d\n", path, ret);
625b9cbc85dSRick Macklem 		SSL_CTX_free(ctx);
626b9cbc85dSRick Macklem 		return (NULL);
627b9cbc85dSRick Macklem 	}
628b9cbc85dSRick Macklem 
629b9cbc85dSRick Macklem 	/* Set Mutual authentication, as required. */
630b9cbc85dSRick Macklem 	if (rpctls_do_mutual) {
631b9cbc85dSRick Macklem 		if (rpctls_verify_cafile != NULL ||
632b9cbc85dSRick Macklem 		    rpctls_verify_capath != NULL) {
633b9cbc85dSRick Macklem 			if (rpctls_crlfile != NULL) {
634b9cbc85dSRick Macklem 				ret = rpctls_loadcrlfile(ctx);
635b9cbc85dSRick Macklem 				if (ret == 0) {
636b9cbc85dSRick Macklem 					rpctls_verbose_out("rpctls_setup_ssl:"
637b9cbc85dSRick Macklem 					    " Load CRLfile failed\n");
638b9cbc85dSRick Macklem 					SSL_CTX_free(ctx);
639b9cbc85dSRick Macklem 					return (NULL);
640b9cbc85dSRick Macklem 				}
641b9cbc85dSRick Macklem 			}
642b9cbc85dSRick Macklem #if OPENSSL_VERSION_NUMBER >= 0x30000000
643b9cbc85dSRick Macklem 			ret = 1;
644b9cbc85dSRick Macklem 			if (rpctls_verify_cafile != NULL)
645b9cbc85dSRick Macklem 				ret = SSL_CTX_load_verify_file(ctx,
646b9cbc85dSRick Macklem 				    rpctls_verify_cafile);
647b9cbc85dSRick Macklem 			if (ret != 0 && rpctls_verify_capath != NULL)
648b9cbc85dSRick Macklem 				ret = SSL_CTX_load_verify_dir(ctx,
649b9cbc85dSRick Macklem 				    rpctls_verify_capath);
650b9cbc85dSRick Macklem #else
651b9cbc85dSRick Macklem 			ret = SSL_CTX_load_verify_locations(ctx,
652b9cbc85dSRick Macklem 			    rpctls_verify_cafile, rpctls_verify_capath);
653b9cbc85dSRick Macklem #endif
654b9cbc85dSRick Macklem 			if (ret == 0) {
655b9cbc85dSRick Macklem 				rpctls_verbose_out("rpctls_setup_ssl: "
656b9cbc85dSRick Macklem 				    "Can't load verify locations\n");
657b9cbc85dSRick Macklem 				SSL_CTX_free(ctx);
658b9cbc85dSRick Macklem 				return (NULL);
659b9cbc85dSRick Macklem 			}
660b9cbc85dSRick Macklem 			if (rpctls_verify_cafile != NULL)
661b9cbc85dSRick Macklem 				SSL_CTX_set_client_CA_list(ctx,
662b9cbc85dSRick Macklem 				    SSL_load_client_CA_file(
663b9cbc85dSRick Macklem 			    rpctls_verify_cafile));
664b9cbc85dSRick Macklem 		}
665b9cbc85dSRick Macklem 		SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER,
666b9cbc85dSRick Macklem 		    rpctls_verify_callback);
667b9cbc85dSRick Macklem 	}
668c7bb0f47SJohn Baldwin #ifdef SSL_OP_ENABLE_KTLS
669c7bb0f47SJohn Baldwin 	SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS);
670c7bb0f47SJohn Baldwin #endif
671c7bb0f47SJohn Baldwin #ifdef SSL_MODE_NO_KTLS_TX
672b9cbc85dSRick Macklem 	SSL_CTX_clear_mode(ctx, SSL_MODE_NO_KTLS_TX | SSL_MODE_NO_KTLS_RX);
673c7bb0f47SJohn Baldwin #endif
674b9cbc85dSRick Macklem 	return (ctx);
675b9cbc85dSRick Macklem }
676b9cbc85dSRick Macklem 
677b9cbc85dSRick Macklem static SSL *
678b9cbc85dSRick Macklem rpctls_server(SSL_CTX *ctx, int s, uint32_t *flags, uint32_t *uidp,
679b9cbc85dSRick Macklem     int *ngrps, uint32_t *gidp, X509 **certp)
680b9cbc85dSRick Macklem {
681b9cbc85dSRick Macklem 	SSL *ssl;
682b9cbc85dSRick Macklem 	X509 *cert;
683b9cbc85dSRick Macklem 	struct sockaddr *sad;
684b9cbc85dSRick Macklem 	struct sockaddr_storage ad;
685b9cbc85dSRick Macklem 	char hostnam[NI_MAXHOST];
686b9cbc85dSRick Macklem 	int gethostret, ret;
687b9cbc85dSRick Macklem 	char *cp, *cp2;
688b9cbc85dSRick Macklem 	long verfret;
689b9cbc85dSRick Macklem 
690b9cbc85dSRick Macklem 	*flags = 0;
691b9cbc85dSRick Macklem 	*certp = NULL;
692b9cbc85dSRick Macklem 	sad = (struct sockaddr *)&ad;
693b9cbc85dSRick Macklem 	ssl = SSL_new(ctx);
694b9cbc85dSRick Macklem 	if (ssl == NULL) {
695b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctls_server: SSL_new failed\n");
696b9cbc85dSRick Macklem 		return (NULL);
697b9cbc85dSRick Macklem 	}
698b9cbc85dSRick Macklem 	if (SSL_set_fd(ssl, s) != 1) {
699b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctls_server: SSL_set_fd failed\n");
700b9cbc85dSRick Macklem 		SSL_free(ssl);
701b9cbc85dSRick Macklem 		return (NULL);
702b9cbc85dSRick Macklem 	}
703b9cbc85dSRick Macklem 	ret = SSL_accept(ssl);
704b9cbc85dSRick Macklem 	if (ret != 1) {
705b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctls_server: SSL_accept "
706b9cbc85dSRick Macklem 		    "failed ret=%d\n", ret);
707b9cbc85dSRick Macklem 		SSL_free(ssl);
708b9cbc85dSRick Macklem 		return (NULL);
709b9cbc85dSRick Macklem 	}
710b9cbc85dSRick Macklem 	*flags |= RPCTLS_FLAGS_HANDSHAKE;
711d71c9f14SRick Macklem 	if (rpctls_verbose) {
712d71c9f14SRick Macklem 		gethostret = rpctls_gethost(s, sad, hostnam, sizeof(hostnam));
713d71c9f14SRick Macklem 		if (gethostret == 0)
714d71c9f14SRick Macklem 			hostnam[0] = '\0';
715d71c9f14SRick Macklem 		rpctls_verbose_out("rpctls_server: SSL handshake ok for host %s"
716d71c9f14SRick Macklem 		    " <%s %s>\n", hostnam, SSL_get_version(ssl),
717d71c9f14SRick Macklem 		    SSL_get_cipher(ssl));
718d71c9f14SRick Macklem 	}
719b9cbc85dSRick Macklem 	if (rpctls_do_mutual) {
720b9cbc85dSRick Macklem 		cert = SSL_get_peer_certificate(ssl);
721b9cbc85dSRick Macklem 		if (cert != NULL) {
722d71c9f14SRick Macklem 			if (!rpctls_verbose) {
723b9cbc85dSRick Macklem 				gethostret = rpctls_gethost(s, sad, hostnam,
724b9cbc85dSRick Macklem 				    sizeof(hostnam));
725b9cbc85dSRick Macklem 				if (gethostret == 0)
726b9cbc85dSRick Macklem 					hostnam[0] = '\0';
727d71c9f14SRick Macklem 			}
728b9cbc85dSRick Macklem 			cp2 = X509_NAME_oneline(
729b9cbc85dSRick Macklem 			    X509_get_subject_name(cert), NULL, 0);
730b9cbc85dSRick Macklem 			*flags |= RPCTLS_FLAGS_GOTCERT;
731b9cbc85dSRick Macklem 			verfret = SSL_get_verify_result(ssl);
732b9cbc85dSRick Macklem 			if (verfret != X509_V_OK) {
733b9cbc85dSRick Macklem 				cp = X509_NAME_oneline(
734b9cbc85dSRick Macklem 				    X509_get_issuer_name(cert), NULL, 0);
735b9cbc85dSRick Macklem 				if (rpctls_debug_level == 0)
736b9cbc85dSRick Macklem 					syslog(LOG_INFO | LOG_DAEMON,
737b9cbc85dSRick Macklem 					    "rpctls_server: client IP %s "
738b9cbc85dSRick Macklem 					    "issuerName=%s subjectName=%s"
739b9cbc85dSRick Macklem 					    " verify failed %s\n", hostnam,
740b9cbc85dSRick Macklem 					    cp, cp2,
741b9cbc85dSRick Macklem 					    X509_verify_cert_error_string(
742b9cbc85dSRick Macklem 					    verfret));
743b9cbc85dSRick Macklem 				else
744b9cbc85dSRick Macklem 					fprintf(stderr,
745b9cbc85dSRick Macklem 					    "rpctls_server: client IP %s "
746b9cbc85dSRick Macklem 					    "issuerName=%s subjectName=%s"
747b9cbc85dSRick Macklem 					    " verify failed %s\n", hostnam,
748b9cbc85dSRick Macklem 					    cp, cp2,
749b9cbc85dSRick Macklem 					    X509_verify_cert_error_string(
750b9cbc85dSRick Macklem 					    verfret));
751b9cbc85dSRick Macklem 			}
752b9cbc85dSRick Macklem 			if (verfret ==
753b9cbc85dSRick Macklem 			    X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
754b9cbc85dSRick Macklem 			    verfret == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN)
755b9cbc85dSRick Macklem 				*flags |= RPCTLS_FLAGS_SELFSIGNED;
756b9cbc85dSRick Macklem 			else if (verfret == X509_V_OK) {
757b9cbc85dSRick Macklem 				if (rpctls_comparehost) {
758b9cbc85dSRick Macklem 					ret = 0;
759b9cbc85dSRick Macklem 					if (gethostret != 0)
760b9cbc85dSRick Macklem 						ret = rpctls_checkhost(sad,
761b9cbc85dSRick Macklem 						    cert, rpctls_wildcard);
762b9cbc85dSRick Macklem 					if (ret != 1) {
763b9cbc85dSRick Macklem 						*flags |=
764b9cbc85dSRick Macklem 						    RPCTLS_FLAGS_DISABLED;
765b9cbc85dSRick Macklem 						rpctls_verbose_out(
766b9cbc85dSRick Macklem 						    "rpctls_server: "
767b9cbc85dSRick Macklem 						    "checkhost "
768b9cbc85dSRick Macklem 						    "failed\n");
769b9cbc85dSRick Macklem 					}
770b9cbc85dSRick Macklem 				}
771b9cbc85dSRick Macklem 				if (rpctls_cnuser) {
772b9cbc85dSRick Macklem 					ret = rpctls_cnname(cert, uidp,
773b9cbc85dSRick Macklem 					    ngrps, gidp);
774b9cbc85dSRick Macklem 					if (ret != 0)
775b9cbc85dSRick Macklem 						*flags |= RPCTLS_FLAGS_CERTUSER;
776b9cbc85dSRick Macklem 				}
777b9cbc85dSRick Macklem 				*flags |= RPCTLS_FLAGS_VERIFIED;
778b9cbc85dSRick Macklem 				*certp = cert;
779b9cbc85dSRick Macklem 				cert = NULL;
780b9cbc85dSRick Macklem 			}
781b9cbc85dSRick Macklem 			if (cert != NULL)
782b9cbc85dSRick Macklem 				X509_free(cert);
783b9cbc85dSRick Macklem 		} else
784b9cbc85dSRick Macklem 			rpctls_verbose_out("rpctls_server: "
785b9cbc85dSRick Macklem 			    "No peer certificate\n");
786b9cbc85dSRick Macklem 	}
787b9cbc85dSRick Macklem 
788b9cbc85dSRick Macklem 	/* Check to see that ktls is working for the connection. */
789b9cbc85dSRick Macklem 	ret = BIO_get_ktls_send(SSL_get_wbio(ssl));
790b9cbc85dSRick Macklem 	rpctls_verbose_out("rpctls_server: BIO_get_ktls_send=%d\n", ret);
791b9cbc85dSRick Macklem 	if (ret != 0) {
792b9cbc85dSRick Macklem 		ret = BIO_get_ktls_recv(SSL_get_rbio(ssl));
793b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctls_server: BIO_get_ktls_recv=%d\n",
794b9cbc85dSRick Macklem 		    ret);
795b9cbc85dSRick Macklem 	}
796b9cbc85dSRick Macklem 	if (ret == 0) {
797b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0)
798b9cbc85dSRick Macklem 			syslog(LOG_ERR, "ktls not working");
799b9cbc85dSRick Macklem 		else
800b9cbc85dSRick Macklem 			fprintf(stderr, "ktls not working\n");
801b9cbc85dSRick Macklem 		/*
802b9cbc85dSRick Macklem 		 * The handshake has completed, so all that can be
803b9cbc85dSRick Macklem 		 * done is disable the connection.
804b9cbc85dSRick Macklem 		 */
805b9cbc85dSRick Macklem 		*flags |= RPCTLS_FLAGS_DISABLED;
806b9cbc85dSRick Macklem 	}
807b9cbc85dSRick Macklem 
808b9cbc85dSRick Macklem 	return (ssl);
809b9cbc85dSRick Macklem }
810b9cbc85dSRick Macklem 
811b9cbc85dSRick Macklem /*
812b9cbc85dSRick Macklem  * Acquire the dnsname for this server.
813b9cbc85dSRick Macklem  */
814b9cbc85dSRick Macklem static char *
815b9cbc85dSRick Macklem rpctls_getdnsname(char *hostname)
816b9cbc85dSRick Macklem {
817b9cbc85dSRick Macklem 	char *cp, *dnsname;
818b9cbc85dSRick Macklem 	struct addrinfo *aip, hints;
819b9cbc85dSRick Macklem 	int error;
820b9cbc85dSRick Macklem 
821b9cbc85dSRick Macklem 	dnsname = NULL;
822b9cbc85dSRick Macklem 	if (gethostname(hostname, MAXHOSTNAMELEN) == 0) {
823b9cbc85dSRick Macklem 		if ((cp = strchr(hostname, '.')) != NULL &&
824b9cbc85dSRick Macklem 		    *(cp + 1) != '\0') {
825b9cbc85dSRick Macklem 			*cp = '@';
826b9cbc85dSRick Macklem 			dnsname = cp;
827b9cbc85dSRick Macklem 		} else {
828b9cbc85dSRick Macklem 			memset((void *)&hints, 0, sizeof (hints));
829b9cbc85dSRick Macklem 			hints.ai_flags = AI_CANONNAME;
830b9cbc85dSRick Macklem 			error = getaddrinfo(hostname, NULL, &hints, &aip);
831b9cbc85dSRick Macklem 			if (error == 0) {
832b9cbc85dSRick Macklem 				if (aip->ai_canonname != NULL &&
833b9cbc85dSRick Macklem 				    (cp = strchr(aip->ai_canonname, '.')) !=
834b9cbc85dSRick Macklem 				    NULL && *(cp + 1) != '\0') {
835b9cbc85dSRick Macklem 					hostname[0] = '@';
836b9cbc85dSRick Macklem 					strlcpy(&hostname[1], cp + 1,
837b9cbc85dSRick Macklem 					    MAXHOSTNAMELEN + 1);
838b9cbc85dSRick Macklem 					dnsname = hostname;
839b9cbc85dSRick Macklem 				}
840b9cbc85dSRick Macklem 				freeaddrinfo(aip);
841b9cbc85dSRick Macklem 			}
842b9cbc85dSRick Macklem 		}
843b9cbc85dSRick Macklem 	}
844b9cbc85dSRick Macklem 	return (dnsname);
845b9cbc85dSRick Macklem }
846b9cbc85dSRick Macklem 
847b9cbc85dSRick Macklem /*
848b9cbc85dSRick Macklem  * Check for an otherName component of subjectAltName where the OID
849b9cbc85dSRick Macklem  * matches and the "domain" matches that of this server.
850b9cbc85dSRick Macklem  * If found, map "user" to a <uid, gidlist> for it.
851b9cbc85dSRick Macklem  */
852b9cbc85dSRick Macklem static int
853b9cbc85dSRick Macklem rpctls_cnname(X509 *cert, uint32_t *uidp, int *ngrps, uint32_t *gidp)
854b9cbc85dSRick Macklem {
855b9cbc85dSRick Macklem 	char *cp, usern[1024 + 1];
856b9cbc85dSRick Macklem 	struct passwd *pwd;
857b9cbc85dSRick Macklem 	gid_t gids[NGROUPS];
858b9cbc85dSRick Macklem 	int i, j;
859b9cbc85dSRick Macklem 	GENERAL_NAMES *genlist;
860b9cbc85dSRick Macklem 	GENERAL_NAME *genname;
861b9cbc85dSRick Macklem 	OTHERNAME *val;
862b9cbc85dSRick Macklem 	size_t slen;
863b9cbc85dSRick Macklem 
864b9cbc85dSRick Macklem 	/* First, find the otherName in the subjectAltName. */
865b9cbc85dSRick Macklem 	genlist = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
866b9cbc85dSRick Macklem 	if (genlist == NULL)
867b9cbc85dSRick Macklem 		return (0);
868b9cbc85dSRick Macklem 	cp = NULL;
869b9cbc85dSRick Macklem 	for (i = 0; i < sk_GENERAL_NAME_num(genlist); i++) {
870b9cbc85dSRick Macklem 		genname = sk_GENERAL_NAME_value(genlist, i);
871b9cbc85dSRick Macklem 		if (genname->type != GEN_OTHERNAME)
872b9cbc85dSRick Macklem 			continue;
873b9cbc85dSRick Macklem 		val = genname->d.otherName;
874b9cbc85dSRick Macklem 
875b9cbc85dSRick Macklem 		/* Check to see that it is the correct OID. */
876b9cbc85dSRick Macklem 		slen = i2t_ASN1_OBJECT(usern, sizeof(usern), val->type_id);
877b9cbc85dSRick Macklem 		if (slen != strlen(rpctls_cnuseroid) || memcmp(usern,
878b9cbc85dSRick Macklem 		    rpctls_cnuseroid, slen) != 0)
879b9cbc85dSRick Macklem 			continue;
880b9cbc85dSRick Macklem 
881b9cbc85dSRick Macklem 		/* Sanity check the otherName. */
882b9cbc85dSRick Macklem 		if (val->value->type != V_ASN1_UTF8STRING ||
883b9cbc85dSRick Macklem 		    val->value->value.utf8string->length < 3 ||
884b9cbc85dSRick Macklem 		    (size_t)val->value->value.utf8string->length > sizeof(usern)
885b9cbc85dSRick Macklem 		    - 1) {
886b9cbc85dSRick Macklem 			rpctls_verbose_out("rpctls_cnname: invalid cnuser "
887b9cbc85dSRick Macklem 			    "type=%d\n", val->value->type);
888b9cbc85dSRick Macklem 			continue;
889b9cbc85dSRick Macklem 		}
890b9cbc85dSRick Macklem 
891b9cbc85dSRick Macklem 		/* Look for a "user" in the otherName */
892b9cbc85dSRick Macklem 		memcpy(usern, val->value->value.utf8string->data,
893b9cbc85dSRick Macklem 		    val->value->value.utf8string->length);
894b9cbc85dSRick Macklem 		usern[val->value->value.utf8string->length] = '\0';
895b9cbc85dSRick Macklem 
896b9cbc85dSRick Macklem 		/* Now, look for the @dnsname suffix in the commonName. */
897b9cbc85dSRick Macklem 		cp = strcasestr(usern, rpctls_dnsname);
898b9cbc85dSRick Macklem 		if (cp == NULL)
899b9cbc85dSRick Macklem 			continue;
900b9cbc85dSRick Macklem 		if (*(cp + strlen(rpctls_dnsname)) != '\0') {
901b9cbc85dSRick Macklem 			cp = NULL;
902b9cbc85dSRick Macklem 			continue;
903b9cbc85dSRick Macklem 		}
904b9cbc85dSRick Macklem 		*cp = '\0';
905b9cbc85dSRick Macklem 		break;
906b9cbc85dSRick Macklem 	}
907b9cbc85dSRick Macklem 	if (cp == NULL)
908b9cbc85dSRick Macklem 		return (0);
909b9cbc85dSRick Macklem 
910b9cbc85dSRick Macklem 	/* See if the "user" is in the passwd database. */
911b9cbc85dSRick Macklem 	pwd = getpwnam(usern);
912b9cbc85dSRick Macklem 	if (pwd == NULL)
913b9cbc85dSRick Macklem 		return (0);
914b9cbc85dSRick Macklem 	*uidp = pwd->pw_uid;
915b9cbc85dSRick Macklem 	*ngrps = NGROUPS;
916b9cbc85dSRick Macklem 	if (getgrouplist(pwd->pw_name, pwd->pw_gid, gids, ngrps) < 0)
917b9cbc85dSRick Macklem 		return (0);
918b9cbc85dSRick Macklem 	rpctls_verbose_out("mapped user=%s ngrps=%d uid=%d\n", pwd->pw_name,
919b9cbc85dSRick Macklem 	    *ngrps, pwd->pw_uid);
920b9cbc85dSRick Macklem 	for (j = 0; j < *ngrps; j++)
921b9cbc85dSRick Macklem 		gidp[j] = gids[j];
922b9cbc85dSRick Macklem 	return (1);
923b9cbc85dSRick Macklem }
924b9cbc85dSRick Macklem 
925b9cbc85dSRick Macklem static void
926b9cbc85dSRick Macklem rpctls_huphandler(int sig __unused)
927b9cbc85dSRick Macklem {
928b9cbc85dSRick Macklem 
929b9cbc85dSRick Macklem 	rpctls_gothup = true;
930b9cbc85dSRick Macklem }
931