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