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