16863 implement C23 stdbit.h
[illumos-gate.git] / usr / src / cmd / fs.d / nfs / mountd / nfsauth.c
blobfe49f911b25a747de353ac315513e7ced455ff0e
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
34 #include <string.h>
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 #include <sys/file.h>
38 #include <sys/time.h>
39 #include <sys/errno.h>
40 #include <rpcsvc/mount.h>
41 #include <sys/pathconf.h>
42 #include <sys/systeminfo.h>
43 #include <sys/utsname.h>
44 #include <arpa/inet.h>
45 #include <signal.h>
46 #include <syslog.h>
47 #include <locale.h>
48 #include <unistd.h>
49 #include <thread.h>
50 #include <netdir.h>
51 #include <nfs/auth.h>
52 #include <sharefs/share.h>
53 #include <alloca.h>
54 #include "../lib/sharetab.h"
55 #include "mountd.h"
57 static void
58 nfsauth_access(auth_req *argp, auth_res *result)
60 struct netbuf nbuf;
61 struct share *sh;
63 struct cln cln;
65 result->auth_perm = NFSAUTH_DENIED;
67 nbuf.len = argp->req_client.n_len;
68 nbuf.buf = argp->req_client.n_bytes;
70 if (nbuf.len == 0 || nbuf.buf == NULL)
71 return;
74 * Find the export
76 sh = findentry(argp->req_path);
77 if (sh == NULL) {
78 syslog(LOG_ERR, "%s not exported", argp->req_path);
79 return;
82 cln_init_lazy(&cln, argp->req_netid, &nbuf);
84 result->auth_perm = check_client(sh, &cln, argp->req_flavor,
85 argp->req_clnt_uid, argp->req_clnt_gid, argp->req_clnt_gids.len,
86 argp->req_clnt_gids.val, &result->auth_srv_uid,
87 &result->auth_srv_gid, &result->auth_srv_gids.len,
88 &result->auth_srv_gids.val);
90 sharefree(sh);
92 if (result->auth_perm == NFSAUTH_DENIED) {
93 char *host = cln_gethost(&cln);
94 if (host != NULL)
95 syslog(LOG_ERR, "%s denied access to %s", host,
96 argp->req_path);
99 cln_fini(&cln);
102 void
103 nfsauth_func(void *cookie, char *dataptr, size_t arg_size,
104 door_desc_t *dp, uint_t n_desc)
107 nfsauth_arg_t *ap;
108 nfsauth_res_t res = {0};
109 XDR xdrs_a;
110 XDR xdrs_r;
111 size_t rbsz;
112 caddr_t rbuf;
113 varg_t varg = {0};
116 * Decode the inbound door data, so we can look at the cmd.
118 xdrmem_create(&xdrs_a, dataptr, arg_size, XDR_DECODE);
119 if (!xdr_varg(&xdrs_a, &varg)) {
121 * If the arguments can't be decoded, bail.
123 if (varg.vers == V_ERROR)
124 syslog(LOG_ERR, gettext("Arg version mismatch"));
125 res.stat = NFSAUTH_DR_DECERR;
126 goto encres;
130 * Now set the args pointer to the proper version of the args
132 switch (varg.vers) {
133 case V_PROTO:
134 ap = &varg.arg_u.arg;
135 break;
137 /* Additional arguments versions go here */
139 default:
140 syslog(LOG_ERR, gettext("Invalid args version"));
141 res.stat = NFSAUTH_DR_DECERR;
142 goto encres;
146 * Call the specified cmd
148 switch (ap->cmd) {
149 case NFSAUTH_ACCESS:
150 nfsauth_access(&ap->areq, &res.ares);
151 res.stat = NFSAUTH_DR_OKAY;
152 break;
153 default:
154 res.stat = NFSAUTH_DR_BADCMD;
155 break;
158 encres:
160 * Free space used to decode the args
162 xdr_free(xdr_varg, (char *)&varg);
163 xdr_destroy(&xdrs_a);
166 * Encode the results before passing thru door.
168 rbsz = xdr_sizeof(xdr_nfsauth_res, &res);
169 if (rbsz == 0)
170 goto failed;
171 rbuf = alloca(rbsz);
173 xdrmem_create(&xdrs_r, rbuf, rbsz, XDR_ENCODE);
174 if (!xdr_nfsauth_res(&xdrs_r, &res)) {
175 xdr_destroy(&xdrs_r);
176 failed:
177 xdr_free(xdr_nfsauth_res, (char *)&res);
179 * return only the status code
181 res.stat = NFSAUTH_DR_EFAIL;
182 rbsz = sizeof (uint_t);
183 rbuf = (caddr_t)&res.stat;
185 goto out;
187 xdr_destroy(&xdrs_r);
188 xdr_free(xdr_nfsauth_res, (char *)&res);
190 out:
191 (void) door_return((char *)rbuf, rbsz, NULL, 0);
192 (void) door_return(NULL, 0, NULL, 0);
193 /* NOTREACHED */