xref: /freebsd/usr.sbin/rpc.tlsservd/rpc.tlsservd.c (revision d5566d755694b0efeef9b0e0ff839ee5b3ef43bd)
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 
dummy_thread(void * v __unused)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
main(int argc,char ** argv)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 	ncpu = (u_int)sysconf(_SC_NPROCESSORS_ONLN);
171 	rpctls_maxthreads = ncpu > 1 ? ncpu / 2 : 1;
172 
173 	while ((ch = getopt_long(argc, argv, "2C:D:dhl:N:n:mp:r:uvWw", longopts,
174 	    NULL)) != -1) {
175 		switch (ch) {
176 		case '2':
177 			rpctls_mintls = TLS1_2_VERSION;
178 			break;
179 		case 'C':
180 			rpctls_ciphers = optarg;
181 			break;
182 		case 'D':
183 			rpctls_certdir = optarg;
184 			break;
185 		case 'd':
186 			rpctls_debug_level++;
187 			break;
188 		case 'h':
189 			rpctls_comparehost = true;
190 			break;
191 		case 'l':
192 			rpctls_verify_cafile = optarg;
193 			break;
194 		case 'm':
195 			rpctls_do_mutual = true;
196 			break;
197 		case 'N':
198 			rpctls_maxthreads = atoi(optarg);
199 			if (rpctls_maxthreads < 1 || rpctls_maxthreads > ncpu)
200 				errx(1, "maximum threads must be between 1 and "
201 				    "number of CPUs (%d)", ncpu);
202 			break;
203 		case 'n':
204 			hostname[0] = '@';
205 			strlcpy(&hostname[1], optarg, MAXHOSTNAMELEN + 1);
206 			rpctls_dnsname = hostname;
207 			break;
208 		case 'p':
209 			rpctls_verify_capath = optarg;
210 			break;
211 		case 'r':
212 			rpctls_crlfile = optarg;
213 			break;
214 		case 'u':
215 			rpctls_cnuser = true;
216 			break;
217 		case 'v':
218 			rpctls_verbose = true;
219 			break;
220 		case 'W':
221 			if (rpctls_wildcard != X509_CHECK_FLAG_NO_WILDCARDS)
222 				errx(1, "options -w and -W are mutually "
223 				    "exclusive");
224 			rpctls_wildcard = X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS;
225 			break;
226 		case 'w':
227 			if (rpctls_wildcard != X509_CHECK_FLAG_NO_WILDCARDS)
228 				errx(1, "options -w and -W are mutually "
229 				    "exclusive");
230 			rpctls_wildcard = 0;
231 			break;
232 		default:
233 			fprintf(stderr, "usage: %s "
234 			    "[-2/--allowtls1_2] "
235 			    "[-C/--ciphers available_ciphers] "
236 			    "[-D/--certdir certdir] [-d/--debuglevel] "
237 			    "[-h/--checkhost] "
238 			    "[-l/--verifylocs CAfile] [-m/--mutualverf] "
239 			    "[-n/--domain domain_name] "
240 			    "[-p/--verifydir CApath] [-r/--crl CRLfile] "
241 			    "[-u/--certuser] [-v/--verbose] [-W/--multiwild] "
242 			    "[-w/--singlewild]\n", argv[0]);
243 			exit(1);
244 		}
245 	}
246 	if (rpctls_do_mutual && rpctls_verify_cafile == NULL &&
247 	    rpctls_verify_capath == NULL)
248 		errx(1, "-m requires the -l <CAfile> and/or "
249 		    "-p <CApath> options");
250 	if (rpctls_comparehost && (!rpctls_do_mutual ||
251 	    (rpctls_verify_cafile == NULL && rpctls_verify_capath == NULL)))
252 		errx(1, "-h requires the -m plus the "
253 		    "-l <CAfile> and/or -p <CApath> options");
254 	if (!rpctls_comparehost && rpctls_wildcard !=
255 	    X509_CHECK_FLAG_NO_WILDCARDS)
256 		errx(1, "The -w or -W options require the -h option");
257 	if (rpctls_cnuser && (!rpctls_do_mutual ||
258 	    (rpctls_verify_cafile == NULL && rpctls_verify_capath == NULL)))
259 		errx(1, "-u requires the -m plus the "
260 		    "-l <CAfile> and/or -p <CApath> options");
261 
262 	if (modfind("krpc") < 0) {
263 		/* Not present in kernel, try loading it */
264 		if (kldload("krpc") < 0 || modfind("krpc") < 0)
265 			errx(1, "Kernel RPC is not available");
266 	}
267 	signal(SIGPIPE, SIG_IGN);
268 	signal(SIGHUP, rpctls_huphandler);
269 	signal(SIGTERM, rpctls_cleanup_term);
270 
271 	if (rpctls_debug_level == 0 && daemon(0, 0) != 0)
272 		err(1, "Can't daemonize");
273 	pidfile_write(rpctls_pfh);
274 
275 	/*
276 	 * XXX: Push libc internal state into threaded mode before creating
277 	 * the threaded svc_nl xprt.
278 	 */
279 	(void)pthread_create(&tid, NULL, dummy_thread, NULL);
280 	(void)pthread_join(tid, NULL);
281 	if ((xprt = svc_nl_create("tlsserv")) == NULL) {
282 		if (rpctls_debug_level == 0) {
283 			syslog(LOG_ERR,
284 			    "Can't create transport for local rpctlssd socket");
285 			exit(1);
286 		}
287 		err(1, "Can't create transport for local rpctlssd socket");
288 	}
289 	if (!SVC_CONTROL(xprt, SVCNL_GET_XIDKEY, &xidkey))
290 		err(1, "Failed to obtain pthread key for xid from svc_nl");
291 	if (!svc_reg(xprt, RPCTLSSD, RPCTLSSDVERS, rpctlssd_2, NULL)) {
292 		if (rpctls_debug_level == 0) {
293 			syslog(LOG_ERR,
294 			    "Can't register service for local rpctlssd socket");
295 			exit(1);
296 		}
297 		err(1, "Can't register service for local rpctlssd socket");
298 	}
299 
300 	rpctls_ctx = rpctls_setup_ssl(rpctls_certdir);
301 	if (rpctls_ctx == NULL) {
302 		if (rpctls_debug_level == 0) {
303 			syslog(LOG_ERR, "Can't create SSL context");
304 			exit(1);
305 		}
306 		err(1, "Can't create SSL context");
307 	}
308 	rpctls_gothup = false;
309 	pthread_rwlock_init(&rpctls_rwlock, NULL);
310 	pthread_mutex_init(&rpctls_mtx, NULL);
311 	pthread_cond_init(&rpctls_cv, NULL);
312 	LIST_INIT(&rpctls_ssllist);
313 
314 	rpctls_svc_run();
315 
316 	SSL_CTX_free(rpctls_ctx);
317 	return (0);
318 }
319 
320 bool_t
rpctlssd_null_2_svc(__unused void * argp,__unused void * result,__unused struct svc_req * rqstp)321 rpctlssd_null_2_svc(__unused void *argp, __unused void *result,
322     __unused struct svc_req *rqstp)
323 {
324 
325 	rpctls_verbose_out("rpctlssd_null_svc: done\n");
326 	return (TRUE);
327 }
328 
329 /*
330  * To parallelize SSL handshakes we will launch a thread per handshake.  Thread
331  * creation/destruction shall be order(s) of magnitude cheaper than a crypto
332  * handshake, so we are not keeping a pool of workers here.
333  *
334  * Marrying rpc(3) and pthread(3):
335  *
336  * Normally the rpcgen(1) generated rpctlssd_V() calls rpctlssd_connect_V_svc(),
337  * and the latter processes the RPC all the way to the end and returns a TRUE
338  * value and populates the result.  The generated code immediately calls
339  * svc_sendreply() transmitting the result back.
340  *
341  * We will make a private copy of arguments and return FALSE.  Then it is our
342  * obligation to call svc_sendreply() once we do the work in the thread.
343  */
344 
345 static void * rpctlssd_connect_thread(void *);
346 struct rpctlssd_connect_thread_ctx {
347 	struct rpctlssd_connect_arg arg;
348 	uint32_t xid;
349 };
350 
351 bool_t
rpctlssd_connect_2_svc(struct rpctlssd_connect_arg * argp,struct rpctlssd_connect_res * result __unused,struct svc_req * rqstp)352 rpctlssd_connect_2_svc(struct rpctlssd_connect_arg *argp,
353     struct rpctlssd_connect_res *result __unused, struct svc_req *rqstp)
354 {
355 	struct rpctlssd_connect_thread_ctx *ctx;
356 	pthread_t tid;
357 
358 	assert(rqstp->rq_xprt == xprt);
359 
360 	ctx = malloc(sizeof(*ctx));
361 	memcpy(&ctx->arg, argp, sizeof(ctx->arg));
362 	ctx->xid = *(uint32_t *)pthread_getspecific(xidkey);
363 
364 	pthread_mutex_lock(&rpctls_mtx);
365 	while (rpctls_nthreads >= rpctls_maxthreads)
366 		pthread_cond_wait(&rpctls_cv, &rpctls_mtx);
367 	rpctls_nthreads++;
368 	pthread_mutex_unlock(&rpctls_mtx);
369 
370 	rpctls_verbose_out("rpctlsd_connect_svc: xid %u thread %u\n",
371 	    ctx->xid, rpctls_nthreads);
372 
373 	if (pthread_create(&tid, NULL, rpctlssd_connect_thread, ctx) != 0)
374 		warn("failed to start handshake thread");
375 
376 	/* Intentionally, so that RPC generated code doesn't try to reply. */
377 	return (FALSE);
378 }
379 
380 static void *
rpctlssd_connect_thread(void * v)381 rpctlssd_connect_thread(void *v)
382 {
383 	struct rpctlssd_connect_thread_ctx *ctx = v;
384 	struct rpctlssd_connect_res result;
385 	uint64_t socookie;
386 	int ngrps, s;
387 	SSL *ssl;
388 	uint32_t flags;
389 	struct ssl_entry *newslp;
390 	uint32_t xid, uid;
391 	uint32_t *gidp;
392 	X509 *cert;
393 
394 	socookie = ctx->arg.socookie;
395 	xid = ctx->xid;
396 	free(ctx);
397 	ctx = NULL;
398 	pthread_detach(pthread_self());
399 
400 	if (pthread_setspecific(xidkey, &xid) != 0) {
401 		rpctls_verbose_out("rpctlssd_connect_svc: pthread_setspecific "
402 		    "failed\n");
403 		goto out;
404 	}
405 
406 	/* Get the socket fd from the kernel. */
407 	s = rpctls_syscall(socookie);
408 	if (s < 0) {
409 		rpctls_verbose_out("rpctlssd_connect_svc: rpctls_syscall "
410 		    "accept failed\n");
411 		goto out;
412 	}
413 
414 	/* Do the server side of a TLS handshake. */
415 	gidp = calloc(NGROUPS, sizeof(*gidp));
416 	ssl = rpctls_server(rpctls_ctx, s, &flags, &uid, &ngrps, gidp, &cert);
417 	if (ssl == NULL) {
418 		free(gidp);
419 		rpctls_verbose_out("rpctlssd_connect_svc: ssl "
420 		    "accept failed\n");
421 		/*
422 		 * For RPC-over-TLS, this upcall is expected
423 		 * to close off the socket upon handshake failure.
424 		 */
425 		close(s);
426 		goto out;
427 	} else {
428 		rpctls_verbose_out("rpctlssd_connect_svc: "
429 		    "succeeded flags=0x%x\n", flags);
430 		if ((flags & RPCTLS_FLAGS_CERTUSER) != 0)
431 			result = (struct rpctlssd_connect_res){
432 				.flags = flags,
433 				.uid = uid,
434 				.gid.gid_len = ngrps,
435 				.gid.gid_val = gidp,
436 			};
437 		else
438 			result = (struct rpctlssd_connect_res){
439 				.flags = flags,
440 				.uid = 0,
441 				.gid.gid_len = 0,
442 				.gid.gid_val = gidp,
443 			};
444 	}
445 
446 	/* Maintain list of all current SSL *'s */
447 	newslp = malloc(sizeof(*newslp));
448 	newslp->ssl = ssl;
449 	newslp->s = s;
450 	newslp->shutoff = false;
451 	newslp->cookie = socookie;
452 	newslp->cert = cert;
453 	pthread_rwlock_wrlock(&rpctls_rwlock);
454 	LIST_INSERT_HEAD(&rpctls_ssllist, newslp, next);
455 	pthread_rwlock_unlock(&rpctls_rwlock);
456 
457 	if (!svc_sendreply(xprt, (xdrproc_t)xdr_rpctlssd_connect_res, &result))
458 		svcerr_systemerr(xprt);
459 
460 	free(result.gid.gid_val);
461 	rpctls_verbose_out("rpctlsd_connect_svc: xid %u: thread finished\n",
462 	    xid);
463 
464 out:
465 	pthread_mutex_lock(&rpctls_mtx);
466 	if (rpctls_nthreads-- >= rpctls_maxthreads) {
467 		pthread_mutex_unlock(&rpctls_mtx);
468 		pthread_cond_signal(&rpctls_cv);
469 	} else
470 		pthread_mutex_unlock(&rpctls_mtx);
471 	return (NULL);
472 }
473 
474 bool_t
rpctlssd_handlerecord_2_svc(struct rpctlssd_handlerecord_arg * argp,struct rpctlssd_handlerecord_res * result,__unused struct svc_req * rqstp)475 rpctlssd_handlerecord_2_svc(struct rpctlssd_handlerecord_arg *argp,
476     struct rpctlssd_handlerecord_res *result, __unused struct svc_req *rqstp)
477 {
478 	struct ssl_entry *slp;
479 	int ret;
480 	char junk;
481 
482 	pthread_rwlock_rdlock(&rpctls_rwlock);
483 	LIST_FOREACH(slp, &rpctls_ssllist, next)
484 		if (slp->cookie == argp->socookie)
485 			break;
486 	pthread_rwlock_unlock(&rpctls_rwlock);
487 
488 	if (slp != NULL) {
489 		rpctls_verbose_out("rpctlssd_handlerecord fd=%d\n",
490 		    slp->s);
491 		/*
492 		 * An SSL_read() of 0 bytes should fail, but it should
493 		 * handle the non-application data record before doing so.
494 		 */
495 		ret = SSL_read(slp->ssl, &junk, 0);
496 		if (ret <= 0) {
497 			/* Check to see if this was a close alert. */
498 			ret = SSL_get_shutdown(slp->ssl);
499 			if ((ret & (SSL_SENT_SHUTDOWN |
500 			    SSL_RECEIVED_SHUTDOWN)) == SSL_RECEIVED_SHUTDOWN)
501 				SSL_shutdown(slp->ssl);
502 		} else {
503 			if (rpctls_debug_level == 0)
504 				syslog(LOG_ERR, "SSL_read returned %d", ret);
505 			else
506 				fprintf(stderr, "SSL_read returned %d\n", ret);
507 		}
508 		result->reterr = RPCTLSERR_OK;
509 	} else
510 		result->reterr = RPCTLSERR_NOSSL;
511 	return (TRUE);
512 }
513 
514 bool_t
rpctlssd_disconnect_2_svc(struct rpctlssd_disconnect_arg * argp,struct rpctlssd_disconnect_res * result,__unused struct svc_req * rqstp)515 rpctlssd_disconnect_2_svc(struct rpctlssd_disconnect_arg *argp,
516     struct rpctlssd_disconnect_res *result, __unused struct svc_req *rqstp)
517 {
518 	struct ssl_entry *slp;
519 	int ret;
520 
521 	pthread_rwlock_wrlock(&rpctls_rwlock);
522 	LIST_FOREACH(slp, &rpctls_ssllist, next)
523 		if (slp->cookie == argp->socookie) {
524 			LIST_REMOVE(slp, next);
525 			break;
526 		}
527 	pthread_rwlock_unlock(&rpctls_rwlock);
528 
529 	if (slp != NULL) {
530 		rpctls_verbose_out("rpctlssd_disconnect fd=%d closed\n",
531 		    slp->s);
532 		if (!slp->shutoff) {
533 			ret = SSL_get_shutdown(slp->ssl);
534 			/*
535 			 * Do an SSL_shutdown() unless a close alert has
536 			 * already been sent.
537 			 */
538 			if ((ret & SSL_SENT_SHUTDOWN) == 0)
539 				SSL_shutdown(slp->ssl);
540 		}
541 		SSL_free(slp->ssl);
542 		if (slp->cert != NULL)
543 			X509_free(slp->cert);
544 		/*
545 		 * For RPC-over-TLS, this upcall is expected
546 		 * to close off the socket.
547 		 */
548 		if (!slp->shutoff)
549 			shutdown(slp->s, SHUT_WR);
550 		close(slp->s);
551 		free(slp);
552 		result->reterr = RPCTLSERR_OK;
553 	} else
554 		result->reterr = RPCTLSERR_NOCLOSE;
555 	return (TRUE);
556 }
557 
558 int
rpctlssd_2_freeresult(__unused SVCXPRT * transp,xdrproc_t xdr_result __unused,caddr_t result __unused)559 rpctlssd_2_freeresult(__unused SVCXPRT *transp, xdrproc_t xdr_result __unused,
560     caddr_t result __unused)
561 {
562 	return (TRUE);
563 }
564 
565 /*
566  * cleanup_term() called via SIGTERM (or SIGCHLD if a child dies).
567  */
568 static void
rpctls_cleanup_term(int sig __unused)569 rpctls_cleanup_term(int sig __unused)
570 {
571 	struct ssl_entry *slp;
572 
573 	LIST_FOREACH(slp, &rpctls_ssllist, next)
574 		shutdown(slp->s, SHUT_RD);
575 	SSL_CTX_free(rpctls_ctx);
576 	EVP_cleanup();
577 	pidfile_remove(rpctls_pfh);
578 
579 	exit(0);
580 }
581 
582 /* Allow the handshake to proceed. */
583 static int
rpctls_verify_callback(__unused int preverify_ok,__unused X509_STORE_CTX * x509_ctx)584 rpctls_verify_callback(__unused int preverify_ok,
585     __unused X509_STORE_CTX *x509_ctx)
586 {
587 
588 	return (1);
589 }
590 
591 static SSL_CTX *
rpctls_setup_ssl(const char * certdir)592 rpctls_setup_ssl(const char *certdir)
593 {
594 	SSL_CTX *ctx;
595 	char path[PATH_MAX];
596 	size_t len, rlen;
597 	int ret;
598 
599 	ctx = SSL_CTX_new(TLS_server_method());
600 	if (ctx == NULL) {
601 		rpctls_verbose_out("rpctls_setup_ssl: SSL_CTX_new failed\n");
602 		return (NULL);
603 	}
604 
605 	if (rpctls_ciphers != NULL) {
606 		/*
607 		 * Set available ciphers, since KERN_TLS only supports a
608 		 * few of them.  Normally, not doing this should be ok,
609 		 * since the library defaults will work.
610 		 */
611 		ret = SSL_CTX_set_ciphersuites(ctx, rpctls_ciphers);
612 		if (ret == 0) {
613 			rpctls_verbose_out("rpctls_setup_ssl: "
614 			    "SSL_CTX_set_ciphersuites failed: %s\n",
615 			    rpctls_ciphers);
616 			SSL_CTX_free(ctx);
617 			return (NULL);
618 		}
619 	}
620 
621 	ret = SSL_CTX_set_min_proto_version(ctx, rpctls_mintls);
622 	if (ret == 0) {
623 		rpctls_verbose_out("rpctls_setup_ssl: "
624 		    "SSL_CTX_set_min_proto_version failed\n");
625 		SSL_CTX_free(ctx);
626 		return (NULL);
627 	}
628 	ret = SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
629 	if (ret == 0) {
630 		rpctls_verbose_out("rpctls_setup_ssl: "
631 		    "SSL_CTX_set_max_proto_version failed\n");
632 		SSL_CTX_free(ctx);
633 		return (NULL);
634 	}
635 
636 	/* Get the cert.pem and certkey.pem files from the directory certdir. */
637 	len = strlcpy(path, certdir, sizeof(path));
638 	rlen = sizeof(path) - len;
639 	if (strlcpy(&path[len], "cert.pem", rlen) != 8) {
640 		SSL_CTX_free(ctx);
641 		return (NULL);
642 	}
643 	ret = SSL_CTX_use_certificate_file(ctx, path, SSL_FILETYPE_PEM);
644 	if (ret != 1) {
645 		rpctls_verbose_out("rpctls_setup_ssl: can't use certificate "
646 		    "file path=%s ret=%d\n", path, ret);
647 		SSL_CTX_free(ctx);
648 		return (NULL);
649 	}
650 	if (strlcpy(&path[len], "certkey.pem", rlen) != 11) {
651 		SSL_CTX_free(ctx);
652 		return (NULL);
653 	}
654 	ret = SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM);
655 	if (ret != 1) {
656 		rpctls_verbose_out("rpctls_setup_ssl: Can't use private "
657 		    "key path=%s ret=%d\n", path, ret);
658 		SSL_CTX_free(ctx);
659 		return (NULL);
660 	}
661 
662 	/* Set Mutual authentication, as required. */
663 	if (rpctls_do_mutual) {
664 		if (rpctls_verify_cafile != NULL ||
665 		    rpctls_verify_capath != NULL) {
666 			if (rpctls_crlfile != NULL) {
667 				ret = rpctls_loadcrlfile(ctx);
668 				if (ret == 0) {
669 					rpctls_verbose_out("rpctls_setup_ssl:"
670 					    " Load CRLfile failed\n");
671 					SSL_CTX_free(ctx);
672 					return (NULL);
673 				}
674 			}
675 #if OPENSSL_VERSION_NUMBER >= 0x30000000
676 			ret = 1;
677 			if (rpctls_verify_cafile != NULL)
678 				ret = SSL_CTX_load_verify_file(ctx,
679 				    rpctls_verify_cafile);
680 			if (ret != 0 && rpctls_verify_capath != NULL)
681 				ret = SSL_CTX_load_verify_dir(ctx,
682 				    rpctls_verify_capath);
683 #else
684 			ret = SSL_CTX_load_verify_locations(ctx,
685 			    rpctls_verify_cafile, rpctls_verify_capath);
686 #endif
687 			if (ret == 0) {
688 				rpctls_verbose_out("rpctls_setup_ssl: "
689 				    "Can't load verify locations\n");
690 				SSL_CTX_free(ctx);
691 				return (NULL);
692 			}
693 			if (rpctls_verify_cafile != NULL)
694 				SSL_CTX_set_client_CA_list(ctx,
695 				    SSL_load_client_CA_file(
696 			    rpctls_verify_cafile));
697 		}
698 		SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER,
699 		    rpctls_verify_callback);
700 	}
701 #ifdef SSL_OP_ENABLE_KTLS
702 	SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS);
703 #endif
704 #ifdef SSL_MODE_NO_KTLS_TX
705 	SSL_CTX_clear_mode(ctx, SSL_MODE_NO_KTLS_TX | SSL_MODE_NO_KTLS_RX);
706 #endif
707 	return (ctx);
708 }
709 
710 static SSL *
rpctls_server(SSL_CTX * ctx,int s,uint32_t * flags,uint32_t * uidp,int * ngrps,uint32_t * gidp,X509 ** certp)711 rpctls_server(SSL_CTX *ctx, int s, uint32_t *flags, uint32_t *uidp,
712     int *ngrps, uint32_t *gidp, X509 **certp)
713 {
714 	SSL *ssl;
715 	X509 *cert;
716 	struct sockaddr *sad;
717 	struct sockaddr_storage ad;
718 	char hostnam[NI_MAXHOST];
719 	int gethostret, ret;
720 	char *cp, *cp2;
721 	long verfret;
722 
723 	*flags = 0;
724 	*certp = NULL;
725 	sad = (struct sockaddr *)&ad;
726 	ssl = SSL_new(ctx);
727 	if (ssl == NULL) {
728 		rpctls_verbose_out("rpctls_server: SSL_new failed\n");
729 		return (NULL);
730 	}
731 	if (SSL_set_fd(ssl, s) != 1) {
732 		rpctls_verbose_out("rpctls_server: SSL_set_fd failed\n");
733 		SSL_free(ssl);
734 		return (NULL);
735 	}
736 	ret = SSL_accept(ssl);
737 	if (ret != 1) {
738 		rpctls_verbose_out("rpctls_server: SSL_accept "
739 		    "failed ret=%d\n", ret);
740 		SSL_free(ssl);
741 		return (NULL);
742 	}
743 	*flags |= RPCTLS_FLAGS_HANDSHAKE;
744 	if (rpctls_verbose) {
745 		gethostret = rpctls_gethost(s, sad, hostnam, sizeof(hostnam));
746 		if (gethostret == 0)
747 			hostnam[0] = '\0';
748 		rpctls_verbose_out("rpctls_server: SSL handshake ok for host %s"
749 		    " <%s %s>\n", hostnam, SSL_get_version(ssl),
750 		    SSL_get_cipher(ssl));
751 	}
752 	if (rpctls_do_mutual) {
753 #if OPENSSL_VERSION_NUMBER >= 0x30000000
754 		cert = SSL_get1_peer_certificate(ssl);
755 #else
756 		cert = SSL_get_peer_certificate(ssl);
757 #endif
758 		if (cert != NULL) {
759 			if (!rpctls_verbose) {
760 				gethostret = rpctls_gethost(s, sad, hostnam,
761 				    sizeof(hostnam));
762 				if (gethostret == 0)
763 					hostnam[0] = '\0';
764 			}
765 			cp2 = X509_NAME_oneline(
766 			    X509_get_subject_name(cert), NULL, 0);
767 			*flags |= RPCTLS_FLAGS_GOTCERT;
768 			verfret = SSL_get_verify_result(ssl);
769 			if (verfret != X509_V_OK) {
770 				cp = X509_NAME_oneline(
771 				    X509_get_issuer_name(cert), NULL, 0);
772 				if (rpctls_debug_level == 0)
773 					syslog(LOG_INFO | LOG_DAEMON,
774 					    "rpctls_server: client IP %s "
775 					    "issuerName=%s subjectName=%s"
776 					    " verify failed %s\n", hostnam,
777 					    cp, cp2,
778 					    X509_verify_cert_error_string(
779 					    verfret));
780 				else
781 					fprintf(stderr,
782 					    "rpctls_server: client IP %s "
783 					    "issuerName=%s subjectName=%s"
784 					    " verify failed %s\n", hostnam,
785 					    cp, cp2,
786 					    X509_verify_cert_error_string(
787 					    verfret));
788 			}
789 			if (verfret ==
790 			    X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
791 			    verfret == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN)
792 				*flags |= RPCTLS_FLAGS_SELFSIGNED;
793 			else if (verfret == X509_V_OK) {
794 				if (rpctls_comparehost) {
795 					ret = 0;
796 					if (gethostret != 0)
797 						ret = rpctls_checkhost(sad,
798 						    cert, rpctls_wildcard);
799 					if (ret != 1) {
800 						*flags |=
801 						    RPCTLS_FLAGS_DISABLED;
802 						rpctls_verbose_out(
803 						    "rpctls_server: "
804 						    "checkhost "
805 						    "failed\n");
806 					}
807 				}
808 				if (rpctls_cnuser) {
809 					ret = rpctls_cnname(cert, uidp,
810 					    ngrps, gidp);
811 					if (ret != 0)
812 						*flags |= RPCTLS_FLAGS_CERTUSER;
813 				}
814 				*flags |= RPCTLS_FLAGS_VERIFIED;
815 				*certp = cert;
816 				cert = NULL;
817 			}
818 			if (cert != NULL)
819 				X509_free(cert);
820 		} else
821 			rpctls_verbose_out("rpctls_server: "
822 			    "No peer certificate\n");
823 	}
824 
825 	/* Check to see that ktls is working for the connection. */
826 	ret = BIO_get_ktls_send(SSL_get_wbio(ssl));
827 	rpctls_verbose_out("rpctls_server: BIO_get_ktls_send=%d\n", ret);
828 	if (ret != 0) {
829 		ret = BIO_get_ktls_recv(SSL_get_rbio(ssl));
830 		rpctls_verbose_out("rpctls_server: BIO_get_ktls_recv=%d\n",
831 		    ret);
832 	}
833 	if (ret == 0) {
834 		if (rpctls_debug_level == 0)
835 			syslog(LOG_ERR, "ktls not working");
836 		else
837 			fprintf(stderr, "ktls not working\n");
838 		/*
839 		 * The handshake has completed, so all that can be
840 		 * done is disable the connection.
841 		 */
842 		*flags |= RPCTLS_FLAGS_DISABLED;
843 	}
844 
845 	return (ssl);
846 }
847 
848 /*
849  * Acquire the dnsname for this server.
850  */
851 static char *
rpctls_getdnsname(char * hostname)852 rpctls_getdnsname(char *hostname)
853 {
854 	char *cp, *dnsname;
855 	struct addrinfo *aip, hints;
856 	int error;
857 
858 	dnsname = NULL;
859 	if (gethostname(hostname, MAXHOSTNAMELEN) == 0) {
860 		if ((cp = strchr(hostname, '.')) != NULL &&
861 		    *(cp + 1) != '\0') {
862 			*cp = '@';
863 			dnsname = cp;
864 		} else {
865 			memset((void *)&hints, 0, sizeof (hints));
866 			hints.ai_flags = AI_CANONNAME;
867 			error = getaddrinfo(hostname, NULL, &hints, &aip);
868 			if (error == 0) {
869 				if (aip->ai_canonname != NULL &&
870 				    (cp = strchr(aip->ai_canonname, '.')) !=
871 				    NULL && *(cp + 1) != '\0') {
872 					hostname[0] = '@';
873 					strlcpy(&hostname[1], cp + 1,
874 					    MAXHOSTNAMELEN + 1);
875 					dnsname = hostname;
876 				}
877 				freeaddrinfo(aip);
878 			}
879 		}
880 	}
881 	return (dnsname);
882 }
883 
884 /*
885  * Check for an otherName component of subjectAltName where the OID
886  * matches and the "domain" matches that of this server.
887  * If found, map "user" to a <uid, gidlist> for it.
888  */
889 static int
rpctls_cnname(X509 * cert,uint32_t * uidp,int * ngrps,uint32_t * gidp)890 rpctls_cnname(X509 *cert, uint32_t *uidp, int *ngrps, uint32_t *gidp)
891 {
892 	char *cp, usern[1024 + 1];
893 	struct passwd *pwd;
894 	gid_t gids[NGROUPS];
895 	int i, j;
896 	GENERAL_NAMES *genlist;
897 	GENERAL_NAME *genname;
898 	OTHERNAME *val;
899 	size_t slen;
900 
901 	/* First, find the otherName in the subjectAltName. */
902 	genlist = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
903 	if (genlist == NULL)
904 		return (0);
905 	cp = NULL;
906 	for (i = 0; i < sk_GENERAL_NAME_num(genlist); i++) {
907 		genname = sk_GENERAL_NAME_value(genlist, i);
908 		if (genname->type != GEN_OTHERNAME)
909 			continue;
910 		val = genname->d.otherName;
911 
912 		/* Check to see that it is the correct OID. */
913 		slen = i2t_ASN1_OBJECT(usern, sizeof(usern), val->type_id);
914 		if (slen != strlen(rpctls_cnuseroid) || memcmp(usern,
915 		    rpctls_cnuseroid, slen) != 0)
916 			continue;
917 
918 		/* Sanity check the otherName. */
919 		if (val->value->type != V_ASN1_UTF8STRING ||
920 		    val->value->value.utf8string->length < 3 ||
921 		    (size_t)val->value->value.utf8string->length > sizeof(usern)
922 		    - 1) {
923 			rpctls_verbose_out("rpctls_cnname: invalid cnuser "
924 			    "type=%d\n", val->value->type);
925 			continue;
926 		}
927 
928 		/* Look for a "user" in the otherName */
929 		memcpy(usern, val->value->value.utf8string->data,
930 		    val->value->value.utf8string->length);
931 		usern[val->value->value.utf8string->length] = '\0';
932 
933 		/* Now, look for the @dnsname suffix in the commonName. */
934 		cp = strcasestr(usern, rpctls_dnsname);
935 		if (cp == NULL)
936 			continue;
937 		if (*(cp + strlen(rpctls_dnsname)) != '\0') {
938 			cp = NULL;
939 			continue;
940 		}
941 		*cp = '\0';
942 		break;
943 	}
944 	if (cp == NULL)
945 		return (0);
946 
947 	/* See if the "user" is in the passwd database. */
948 	pwd = getpwnam(usern);
949 	if (pwd == NULL)
950 		return (0);
951 	*uidp = pwd->pw_uid;
952 	*ngrps = NGROUPS;
953 	if (getgrouplist(pwd->pw_name, pwd->pw_gid, gids, ngrps) < 0)
954 		return (0);
955 	rpctls_verbose_out("mapped user=%s ngrps=%d uid=%d\n", pwd->pw_name,
956 	    *ngrps, pwd->pw_uid);
957 	for (j = 0; j < *ngrps; j++)
958 		gidp[j] = gids[j];
959 	return (1);
960 }
961 
962 static void
rpctls_huphandler(int sig __unused)963 rpctls_huphandler(int sig __unused)
964 {
965 
966 	rpctls_gothup = true;
967 }
968