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