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