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