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]
23 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
25 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 * Portions of this source code were derived from Berkeley 4.3 BSD
33 * under license from the Regents of the University of California.
37 #include <stdio_ext.h>
40 #include <sys/types.h>
43 #include <sys/param.h>
46 #include <netconfig.h>
50 #include <sys/errno.h>
51 #include <rpcsvc/mount.h>
52 #include <sys/pathconf.h>
53 #include <sys/systeminfo.h>
54 #include <sys/utsname.h>
56 #include <sys/resource.h>
61 #include <sys/socket.h>
62 #include <netinet/in.h>
63 #include <arpa/inet.h>
67 #include <priv_utils.h>
69 #include <nfs/nfssys.h>
71 #include <nfs/nfs_sec.h>
72 #include <rpcsvc/daemon_utils.h>
74 #include "../../fslib.h"
75 #include <sharefs/share.h>
76 #include <sharefs/sharetab.h>
77 #include "../lib/sharetab.h"
82 #include <sys/nvpair.h>
89 extern int daemonize_init(void);
90 extern void daemonize_fini(int);
92 extern int _nfssys(int, void *);
94 struct sh_list
*share_list
;
96 rwlock_t sharetab_lock
; /* lock to protect the cached sharetab */
97 static mutex_t mnttab_lock
; /* prevent concurrent mnttab readers */
99 static mutex_t logging_queue_lock
;
100 static cond_t logging_queue_cv
;
102 static share_t
*find_lofsentry(char *, int *);
103 static int getclientsflavors_old(share_t
*, struct cln
*, int *);
104 static int getclientsflavors_new(share_t
*, struct cln
*, int *);
105 static int check_client_old(share_t
*, struct cln
*, int, uid_t
, gid_t
, uint_t
,
106 gid_t
*, uid_t
*, gid_t
*, uint_t
*, gid_t
**);
107 static int check_client_new(share_t
*, struct cln
*, int, uid_t
, gid_t
, uint_t
,
108 gid_t
*, uid_t
*, gid_t
*i
, uint_t
*, gid_t
**);
109 static void mnt(struct svc_req
*, SVCXPRT
*);
110 static void mnt_pathconf(struct svc_req
*);
111 static int mount(struct svc_req
*r
);
112 static void sh_free(struct sh_list
*);
113 static void umount(struct svc_req
*);
114 static void umountall(struct svc_req
*);
115 static int newopts(char *);
119 static int rejecting
;
120 static int mount_vers_min
= MOUNTVERS
;
121 static int mount_vers_max
= MOUNTVERS3
;
122 static int mountd_port
= 0;
124 extern void nfscmd_func(void *, char *, size_t, door_desc_t
*, uint_t
);
126 thread_t nfsauth_thread
;
128 thread_t logging_thread
;
130 typedef struct logging_data
{
136 struct netbuf
*ld_nb
;
137 struct logging_data
*ld_next
;
140 static logging_data
*logging_head
= NULL
;
141 static logging_data
*logging_tail
= NULL
;
144 * Our copy of some system variables obtained using sysconf(3c)
146 static long ngroups_max
; /* _SC_NGROUPS_MAX */
147 static long pw_size
; /* _SC_GETPW_R_SIZE_MAX */
151 nfsauth_svc(void *arg
)
159 if ((doorfd
= door_create(nfsauth_func
, NULL
,
160 DOOR_REFUSE_DESC
| DOOR_NO_CANCEL
)) == -1) {
161 syslog(LOG_ERR
, "Unable to create door: %m\n");
167 * Create a file system path for the door
169 if ((dfd
= open(MOUNTD_DOOR
, O_RDWR
|O_CREAT
|O_TRUNC
,
170 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
)) == -1) {
171 syslog(LOG_ERR
, "Unable to open %s: %m\n", MOUNTD_DOOR
);
172 (void) close(doorfd
);
177 * Clean up any stale namespace associations
179 (void) fdetach(MOUNTD_DOOR
);
182 * Register in namespace to pass to the kernel to door_ki_open
184 if (fattach(doorfd
, MOUNTD_DOOR
) == -1) {
185 syslog(LOG_ERR
, "Unable to fattach door: %m\n");
187 (void) close(doorfd
);
194 * Must pass the doorfd down to the kernel.
197 (void) _nfssys(MOUNTD_ARGS
, &darg
);
200 * Wait for incoming calls
207 syslog(LOG_ERR
, gettext("Door server exited"));
212 * NFS command service thread code for setup and handling of the
213 * nfs_cmd requests for character set conversion and other future
223 if ((doorfd
= door_create(nfscmd_func
, NULL
,
224 DOOR_REFUSE_DESC
| DOOR_NO_CANCEL
)) == -1) {
225 syslog(LOG_ERR
, "Unable to create cmd door: %m\n");
230 * Must pass the doorfd down to the kernel.
233 (void) _nfssys(NFSCMD_ARGS
, &darg
);
236 * Wait for incoming calls
243 syslog(LOG_ERR
, gettext("Cmd door server exited"));
248 free_logging_data(logging_data
*lq
)
254 if (lq
->ld_nb
!= NULL
) {
255 free(lq
->ld_nb
->buf
);
266 static logging_data
*
267 remove_head_of_queue(void)
272 * Pull it off the queue.
276 logging_head
= lq
->ld_next
;
281 if (logging_head
== NULL
) {
290 do_logging_queue(logging_data
*lq
)
298 if (lq
->ld_host
== NULL
) {
299 DTRACE_PROBE(mountd
, name_by_lazy
);
300 cln_init_lazy(&cln
, lq
->ld_netid
, lq
->ld_nb
);
301 host
= cln_gethost(&cln
);
305 /* add entry to mount list */
307 mntlist_new(host
, lq
->ld_rpath
);
309 if (lq
->ld_host
== NULL
)
312 free_logging_data(lq
);
315 (void) mutex_lock(&logging_queue_lock
);
316 lq
= remove_head_of_queue();
317 (void) mutex_unlock(&logging_queue_lock
);
320 DTRACE_PROBE1(mountd
, logging_cleared
, cleared
);
324 logging_svc(void *arg
)
329 (void) mutex_lock(&logging_queue_lock
);
330 while (logging_head
== NULL
) {
331 (void) cond_wait(&logging_queue_cv
,
332 &logging_queue_lock
);
335 lq
= remove_head_of_queue();
336 (void) mutex_unlock(&logging_queue_lock
);
338 do_logging_queue(lq
);
342 syslog(LOG_ERR
, gettext("Logging server exited"));
347 convert_int(int *val
, char *str
)
351 if (str
== NULL
|| !isdigit(*str
))
354 lval
= strtol(str
, &str
, 10);
355 if (*str
!= '\0' || lval
> INT_MAX
)
363 * This function is called for each configured network type to
364 * bind and register our RPC service programs.
366 * On TCP or UDP, we may want to bind MOUNTPROG on a specific port
367 * (when mountd_port is specified) in which case we'll use the
368 * variant of svc_tp_create() that lets us pass a bind address.
371 md_svc_tp_create(struct netconfig
*nconf
)
374 struct nd_hostserv hs
;
375 struct nd_addrlist
*al
= NULL
;
376 SVCXPRT
*xprt
= NULL
;
379 vers
= mount_vers_max
;
382 * If mountd_port is set and this is an inet transport,
383 * bind this service on the specified port. The TLI way
384 * to create such a bind address is netdir_getbyname()
385 * with the special "host" HOST_SELF_BIND. This builds
386 * an all-zeros IP address with the specified port.
388 if (mountd_port
!= 0 &&
389 (strcmp(nconf
->nc_protofmly
, NC_INET
) == 0 ||
390 strcmp(nconf
->nc_protofmly
, NC_INET6
) == 0)) {
393 snprintf(port_str
, sizeof (port_str
), "%u",
394 (unsigned short)mountd_port
);
396 hs
.h_host
= HOST_SELF_BIND
;
397 hs
.h_serv
= port_str
;
398 err
= netdir_getbyname((struct netconfig
*)nconf
, &hs
, &al
);
399 if (err
== 0 && al
!= NULL
) {
400 xprt
= svc_tp_create_addr(mnt
, MOUNTPROG
, vers
,
402 netdir_free(al
, ND_ADDRLIST
);
405 syslog(LOG_ERR
, "mountd: unable to create "
406 "(MOUNTD,%d) on transport %s (port %d)",
407 vers
, nconf
->nc_netid
, mountd_port
);
409 /* fall-back to default bind */
413 * Had mountd_port=0, or non-inet transport,
414 * or the bind to a specific port failed.
417 xprt
= svc_tp_create(mnt
, MOUNTPROG
, vers
, nconf
);
420 syslog(LOG_ERR
, "mountd: unable to create "
421 "(MOUNTD,%d) on transport %s",
422 vers
, nconf
->nc_netid
);
427 * Register additional versions on this transport.
429 while (--vers
>= mount_vers_min
) {
430 if (!svc_reg(xprt
, MOUNTPROG
, vers
, mnt
, nconf
)) {
431 (void) syslog(LOG_ERR
, "mountd: "
432 "failed to register vers %d on %s",
433 vers
, nconf
->nc_netid
);
439 main(int argc
, char *argv
[])
443 int rpc_svc_fdunlim
= 1;
444 int rpc_svc_mode
= RPC_SVC_MT_AUTO
;
445 int maxrecsz
= RPC_MAXDATASIZE
;
446 bool_t exclbind
= TRUE
;
447 long thr_flags
= (THR_NEW_LWP
|THR_DAEMON
);
449 int defvers
, ret
, bufsz
;
451 int listen_backlog
= 0;
454 struct netconfig
*nconf
;
460 * Mountd requires uid 0 for:
461 * /etc/rmtab updates (we could chown it to daemon)
462 * /etc/dfs/dfstab reading (it wants to lock out share which
463 * doesn't do any locking before first truncate;
464 * NFS share does; should use fcntl locking instead)
467 * file dac search (so it can stat all files)
469 if (__init_daemon_priv(PU_RESETGROUPS
|PU_CLEARLIMITSET
, -1, -1,
470 PRIV_SYS_NFS
, PRIV_FILE_DAC_SEARCH
,
471 PRIV_NET_PRIVADDR
, NULL
) == -1) {
472 (void) fprintf(stderr
,
473 "%s: must be run with sufficient privileges\n",
478 if (getrlimit(RLIMIT_NOFILE
, &rl
) != 0) {
479 syslog(LOG_ERR
, "getrlimit failed");
481 rl
.rlim_cur
= rl
.rlim_max
;
482 if (setrlimit(RLIMIT_NOFILE
, &rl
) != 0)
483 syslog(LOG_ERR
, "setrlimit failed");
486 (void) enable_extended_FILE_stdio(-1, -1);
488 ret
= nfs_smf_get_iprop("mountd_max_threads", &max_threads
,
489 DEFAULT_INSTANCE
, SCF_TYPE_INTEGER
, NFSD
);
491 syslog(LOG_ERR
, "Reading of mountd_max_threads from SMF "
492 "failed, using default value");
495 ret
= nfs_smf_get_iprop("mountd_port", &mountd_port
,
496 DEFAULT_INSTANCE
, SCF_TYPE_INTEGER
, NFSD
);
498 syslog(LOG_ERR
, "Reading of mountd_port from SMF "
499 "failed, using default value");
502 while ((c
= getopt(argc
, argv
, "dvrm:p:")) != EOF
) {
514 if (convert_int(&tmp
, optarg
) != 0 || tmp
< 1) {
515 (void) fprintf(stderr
, "%s: invalid "
516 "max_threads option, using defaults\n",
523 if (convert_int(&tmp
, optarg
) != 0 || tmp
< 1 ||
525 (void) fprintf(stderr
, "%s: invalid port "
526 "number\n", argv
[0]);
532 fprintf(stderr
, "usage: mountd [-v] [-r]\n");
538 * Read in the NFS version values from config file.
541 ret
= nfs_smf_get_prop("server_versmin", defval
, DEFAULT_INSTANCE
,
542 SCF_TYPE_INTEGER
, NFSD
, &bufsz
);
545 defvers
= strtol(defval
, (char **)NULL
, 10);
547 mount_vers_min
= defvers
;
549 * special because NFSv2 is
550 * supported by mount v1 & v2
552 if (defvers
== NFS_VERSION
)
553 mount_vers_min
= MOUNTVERS
;
558 ret
= nfs_smf_get_prop("server_versmax", defval
, DEFAULT_INSTANCE
,
559 SCF_TYPE_INTEGER
, NFSD
, &bufsz
);
562 defvers
= strtol(defval
, (char **)NULL
, 10);
564 mount_vers_max
= defvers
;
568 ret
= nfs_smf_get_iprop("mountd_listen_backlog", &listen_backlog
,
569 DEFAULT_INSTANCE
, SCF_TYPE_INTEGER
, NFSD
);
571 syslog(LOG_ERR
, "Reading of mountd_listen_backlog from SMF "
572 "failed, using default value");
576 * Sanity check versions,
577 * even though we may get versions > MOUNTVERS3, we still need
578 * to start nfsauth service, so continue on regardless of values.
580 if (mount_vers_max
> MOUNTVERS3
)
581 mount_vers_max
= MOUNTVERS3
;
582 if (mount_vers_min
> mount_vers_max
) {
583 fprintf(stderr
, "server_versmin > server_versmax\n");
584 mount_vers_max
= mount_vers_min
;
586 (void) setlocale(LC_ALL
, "");
587 (void) rwlock_init(&sharetab_lock
, USYNC_THREAD
, NULL
);
588 (void) mutex_init(&mnttab_lock
, USYNC_THREAD
, NULL
);
589 (void) mutex_init(&logging_queue_lock
, USYNC_THREAD
, NULL
);
590 (void) cond_init(&logging_queue_cv
, USYNC_THREAD
, NULL
);
594 #if !defined(TEXT_DOMAIN)
595 #define TEXT_DOMAIN "SYS_TEST"
597 (void) textdomain(TEXT_DOMAIN
);
599 /* Don't drop core if the NFS module isn't loaded. */
600 (void) signal(SIGSYS
, SIG_IGN
);
603 pipe_fd
= daemonize_init();
606 * If we coredump it'll be in /core
609 fprintf(stderr
, "chdir /: %s\n", strerror(errno
));
612 openlog("mountd", LOG_PID
, LOG_DAEMON
);
615 * establish our lock on the lock file and write our pid to it.
616 * exit if some other process holds the lock, or if there's any
617 * error in writing/locking the file.
619 pid
= _enter_daemon_lock(MOUNTD
);
624 fprintf(stderr
, "error locking for %s: %s\n", MOUNTD
,
628 /* daemon was already running */
633 * Get required system variables
635 if ((ngroups_max
= sysconf(_SC_NGROUPS_MAX
)) == -1) {
636 syslog(LOG_ERR
, "Unable to get _SC_NGROUPS_MAX");
639 if ((pw_size
= sysconf(_SC_GETPW_R_SIZE_MAX
)) == -1) {
640 syslog(LOG_ERR
, "Unable to get _SC_GETPW_R_SIZE_MAX");
645 * Set number of file descriptors to unlimited
647 if (!rpc_control(RPC_SVC_USE_POLLFD
, &rpc_svc_fdunlim
)) {
648 syslog(LOG_INFO
, "unable to set number of FDs to unlimited");
652 * Tell RPC that we want automatic thread mode.
653 * A new thread will be spawned for each request.
655 if (!rpc_control(RPC_SVC_MTMODE_SET
, &rpc_svc_mode
)) {
656 fprintf(stderr
, "unable to set automatic MT mode\n");
661 * Enable non-blocking mode and maximum record size checks for
662 * connection oriented transports.
664 if (!rpc_control(RPC_SVC_CONNMAXREC_SET
, &maxrecsz
)) {
665 fprintf(stderr
, "unable to set RPC max record size\n");
669 * Prevent our non-priv udp and tcp ports bound w/wildcard addr
670 * from being hijacked by a bind to a more specific addr.
672 if (!rpc_control(__RPC_SVC_EXCLBIND_SET
, &exclbind
)) {
673 fprintf(stderr
, "warning: unable to set udp/tcp EXCLBIND\n");
677 * Set the maximum number of outstanding connection
678 * indications (listen backlog) to the value specified.
680 if (listen_backlog
> 0 && !rpc_control(__RPC_SVC_LSTNBKLOG_SET
,
682 fprintf(stderr
, "unable to set listen backlog\n");
687 * If max_threads was specified, then set the
688 * maximum number of threads to the value specified.
690 if (max_threads
> 0 && !rpc_control(RPC_SVC_THRMAX_SET
, &max_threads
)) {
691 fprintf(stderr
, "unable to set max_threads\n");
695 if (mountd_port
< 0 || mountd_port
> UINT16_MAX
) {
696 fprintf(stderr
, "unable to use specified port\n");
701 * Make sure to unregister any previous versions in case the
702 * user is reconfiguring the server in interesting ways.
704 svc_unreg(MOUNTPROG
, MOUNTVERS
);
705 svc_unreg(MOUNTPROG
, MOUNTVERS_POSIX
);
706 svc_unreg(MOUNTPROG
, MOUNTVERS3
);
709 * Create the nfsauth thread with same signal disposition
710 * as the main thread. We need to create a separate thread
711 * since mountd() will be both an RPC server (for remote
712 * traffic) _and_ a doors server (for kernel upcalls).
714 if (thr_create(NULL
, 0, nfsauth_svc
, 0, thr_flags
, &nfsauth_thread
)) {
716 gettext("Failed to create NFSAUTH svc thread\n"));
721 * Create the cmd service thread with same signal disposition
722 * as the main thread. We need to create a separate thread
723 * since mountd() will be both an RPC server (for remote
724 * traffic) _and_ a doors server (for kernel upcalls).
726 if (thr_create(NULL
, 0, cmd_svc
, 0, thr_flags
, &cmd_thread
)) {
727 syslog(LOG_ERR
, gettext("Failed to create CMD svc thread"));
732 * Create an additional thread to service the rmtab
733 * logging for mount requests. Use the same
734 * signal disposition as the main thread. We create
735 * a separate thread to allow the mount request threads to
736 * clear as soon as possible.
738 if (thr_create(NULL
, 0, logging_svc
, 0, thr_flags
, &logging_thread
)) {
739 syslog(LOG_ERR
, gettext("Failed to create LOGGING svc thread"));
744 * Enumerate network transports and create service listeners
745 * as appropriate for each.
747 if ((nc
= setnetconfig()) == NULL
) {
748 syslog(LOG_ERR
, "setnetconfig failed: %m");
751 while ((nconf
= getnetconfig(nc
)) != NULL
) {
753 * Skip things like tpi_raw, invisible...
755 if ((nconf
->nc_flag
& NC_VISIBLE
) == 0)
757 if (nconf
->nc_semantics
!= NC_TPI_CLTS
&&
758 nconf
->nc_semantics
!= NC_TPI_COTS
&&
759 nconf
->nc_semantics
!= NC_TPI_COTS_ORD
)
762 md_svc_tp_create(nconf
);
764 (void) endnetconfig(nc
);
771 daemonize_fini(pipe_fd
);
773 /* Get rid of the most dangerous basic privileges. */
774 __fini_daemon_priv(PRIV_PROC_EXEC
, PRIV_PROC_INFO
, PRIV_PROC_SESSION
,
778 syslog(LOG_ERR
, "Error: svc_run shouldn't have returned");
786 * Server procedure switch routine
789 mnt(struct svc_req
*rqstp
, SVCXPRT
*transp
)
791 switch (rqstp
->rq_proc
) {
794 if (!svc_sendreply(transp
, xdr_void
, (char *)0))
795 log_cant_reply(transp
);
803 mntlist_send(transp
);
810 case MOUNTPROC_UMNTALL
:
814 case MOUNTPROC_EXPORT
:
815 case MOUNTPROC_EXPORTALL
:
819 case MOUNTPROC_PATHCONF
:
820 if (rqstp
->rq_vers
== MOUNTVERS_POSIX
)
823 svcerr_noproc(transp
);
827 svcerr_noproc(transp
);
833 log_cant_reply_cln(struct cln
*cln
)
838 saverrno
= errno
; /* save error code */
840 host
= cln_gethost(cln
);
846 syslog(LOG_ERR
, "couldn't send reply to %s", host
);
848 syslog(LOG_ERR
, "couldn't send reply to %s: %m", host
);
852 log_cant_reply(SVCXPRT
*transp
)
857 saverrno
= errno
; /* save error code */
858 cln_init(&cln
, transp
);
861 log_cant_reply_cln(&cln
);
867 * Answer pathconf questions for the mount point fs
870 mnt_pathconf(struct svc_req
*rqstp
)
874 char *path
, rpath
[MAXPATHLEN
];
877 transp
= rqstp
->rq_xprt
;
879 (void) memset((caddr_t
)&p
, 0, sizeof (p
));
881 if (!svc_getargs(transp
, xdr_dirpath
, (caddr_t
)&path
)) {
882 svcerr_decode(transp
);
885 if (lstat(path
, &st
) < 0) {
886 _PC_SET(_PC_ERROR
, p
.pc_mask
);
890 * Get a path without symbolic links.
892 if (realpath(path
, rpath
) == NULL
) {
894 "mount request: realpath failed on %s: %m",
896 _PC_SET(_PC_ERROR
, p
.pc_mask
);
899 (void) memset((caddr_t
)&p
, 0, sizeof (p
));
901 * can't ask about devices over NFS
903 _PC_SET(_PC_MAX_CANON
, p
.pc_mask
);
904 _PC_SET(_PC_MAX_INPUT
, p
.pc_mask
);
905 _PC_SET(_PC_PIPE_BUF
, p
.pc_mask
);
906 _PC_SET(_PC_VDISABLE
, p
.pc_mask
);
909 p
.pc_link_max
= pathconf(rpath
, _PC_LINK_MAX
);
911 _PC_SET(_PC_LINK_MAX
, p
.pc_mask
);
912 p
.pc_name_max
= pathconf(rpath
, _PC_NAME_MAX
);
914 _PC_SET(_PC_NAME_MAX
, p
.pc_mask
);
915 p
.pc_path_max
= pathconf(rpath
, _PC_PATH_MAX
);
917 _PC_SET(_PC_PATH_MAX
, p
.pc_mask
);
918 if (pathconf(rpath
, _PC_NO_TRUNC
) == 1)
919 _PC_SET(_PC_NO_TRUNC
, p
.pc_mask
);
920 if (pathconf(rpath
, _PC_CHOWN_RESTRICTED
) == 1)
921 _PC_SET(_PC_CHOWN_RESTRICTED
, p
.pc_mask
);
925 if (!svc_sendreply(transp
, xdr_ppathcnf
, (char *)&p
))
926 log_cant_reply(transp
);
928 svc_freeargs(transp
, xdr_dirpath
, (caddr_t
)&path
);
932 * If the rootmount (export) option is specified, the all mount requests for
933 * subdirectories return EACCES.
936 checkrootmount(share_t
*sh
, char *rpath
)
940 if ((val
= getshareopt(sh
->sh_opts
, SHOPT_NOSUB
)) != NULL
) {
942 if (strcmp(sh
->sh_path
, rpath
) != 0)
950 #define MAX_FLAVORS 128
953 * Return only EACCES if client does not have access
955 * "If the server exports only /a/b, an attempt to
956 * mount a/b/c will fail with ENOENT if the directory
957 * does not exist"... However, if the client
958 * does not have access to /a/b, an attacker can
959 * determine whether the directory exists.
960 * This routine checks either existence of the file or
961 * existence of the file name entry in the mount table.
962 * If the file exists and there is no file name entry,
963 * the error returned should be EACCES.
964 * If the file does not exist, it must be determined
965 * whether the client has access to a parent
966 * directory. If the client has access to a parent
967 * directory, the error returned should be ENOENT,
971 mount_enoent_error(struct cln
*cln
, char *path
, char *rpath
, int *flavor_list
)
973 char *checkpath
, *dp
;
975 int realpath_error
= ENOENT
, reply_error
= EACCES
, lofs_tried
= 0;
978 checkpath
= strdup(path
);
979 if (checkpath
== NULL
) {
980 syslog(LOG_ERR
, "mount_enoent: no memory");
991 if ((sh
= findentry(rpath
)) == NULL
&&
992 (sh
= find_lofsentry(rpath
, &lofs_tried
)) == NULL
) {
994 * There is no file name entry.
995 * If the file (with symbolic links resolved) exists,
996 * the error returned should be EACCES.
998 if (realpath_error
== 0)
1000 } else if (checkrootmount(sh
, rpath
) == 0) {
1002 * This is a "nosub" only export, in which case,
1003 * mounting subdirectories isn't allowed.
1004 * If the file (with symbolic links resolved) exists,
1005 * the error returned should be EACCES.
1007 if (realpath_error
== 0)
1011 * Check permissions in mount table.
1013 if (newopts(sh
->sh_opts
))
1014 flavor_count
= getclientsflavors_new(sh
, cln
,
1017 flavor_count
= getclientsflavors_old(sh
, cln
,
1019 if (flavor_count
!= 0) {
1021 * Found entry in table and
1022 * client has correct permissions.
1024 reply_error
= ENOENT
;
1030 * Check all parent directories.
1032 dp
= strrchr(checkpath
, '/');
1036 if (strlen(checkpath
) == 0)
1039 * Get the real path (no symbolic links in it)
1041 if (realpath(checkpath
, rpath
) == NULL
) {
1042 if (errno
!= ENOENT
)
1052 return (reply_error
);
1056 * We need to inform the caller whether or not we were
1057 * able to add a node to the queue. If we are not, then
1058 * it is up to the caller to go ahead and log the data.
1061 enqueue_logging_data(char *host
, SVCXPRT
*transp
, char *path
,
1062 char *rpath
, int status
, int error
)
1067 lq
= (logging_data
*)calloc(1, sizeof (logging_data
));
1072 * We might not yet have the host...
1075 DTRACE_PROBE1(mountd
, log_host
, host
);
1076 lq
->ld_host
= strdup(host
);
1077 if (lq
->ld_host
== NULL
)
1080 DTRACE_PROBE(mountd
, log_no_host
);
1082 lq
->ld_netid
= strdup(transp
->xp_netid
);
1083 if (lq
->ld_netid
== NULL
)
1086 lq
->ld_nb
= calloc(1, sizeof (struct netbuf
));
1087 if (lq
->ld_nb
== NULL
)
1090 nb
= svc_getrpccaller(transp
);
1092 DTRACE_PROBE(mountd
, e__nb__enqueue
);
1096 DTRACE_PROBE(mountd
, nb_set_enqueue
);
1098 lq
->ld_nb
->maxlen
= nb
->maxlen
;
1099 lq
->ld_nb
->len
= nb
->len
;
1101 lq
->ld_nb
->buf
= malloc(lq
->ld_nb
->len
);
1102 if (lq
->ld_nb
->buf
== NULL
)
1105 bcopy(nb
->buf
, lq
->ld_nb
->buf
, lq
->ld_nb
->len
);
1108 lq
->ld_path
= strdup(path
);
1109 if (lq
->ld_path
== NULL
)
1113 lq
->ld_rpath
= strdup(rpath
);
1114 if (lq
->ld_rpath
== NULL
)
1118 lq
->ld_status
= status
;
1121 * Add to the tail of the logging queue.
1123 (void) mutex_lock(&logging_queue_lock
);
1124 if (logging_tail
== NULL
) {
1125 logging_tail
= logging_head
= lq
;
1127 logging_tail
->ld_next
= lq
;
1130 (void) cond_signal(&logging_queue_cv
);
1131 (void) mutex_unlock(&logging_queue_lock
);
1137 free_logging_data(lq
);
1143 #define CLN_CLNAMES (1 << 0)
1144 #define CLN_HOST (1 << 1)
1147 cln_init_common(struct cln
*cln
, SVCXPRT
*transp
, char *netid
,
1148 struct netbuf
*nbuf
)
1150 if ((cln
->transp
= transp
) != NULL
) {
1151 assert(netid
== NULL
&& nbuf
== NULL
);
1152 cln
->netid
= transp
->xp_netid
;
1153 cln
->nbuf
= svc_getrpccaller(transp
);
1160 cln
->clnames
= NULL
;
1167 cln_init(struct cln
*cln
, SVCXPRT
*transp
)
1169 cln_init_common(cln
, transp
, NULL
, NULL
);
1173 cln_init_lazy(struct cln
*cln
, char *netid
, struct netbuf
*nbuf
)
1175 cln_init_common(cln
, NULL
, netid
, nbuf
);
1179 cln_fini(struct cln
*cln
)
1181 if (cln
->nconf
!= NULL
)
1182 freenetconfigent(cln
->nconf
);
1184 if (cln
->clnames
!= NULL
)
1185 netdir_free(cln
->clnames
, ND_HOSTSERVLIST
);
1191 cln_getnbuf(struct cln
*cln
)
1196 struct nd_hostservlist
*
1197 cln_getclientsnames(struct cln
*cln
)
1199 if ((cln
->flags
& CLN_CLNAMES
) == 0) {
1201 * nconf is not needed if we do not have nbuf (see
1202 * cln_gethost() too), so we check for nbuf and in a case it is
1203 * NULL we do not try to get nconf.
1205 if (cln
->netid
!= NULL
&& cln
->nbuf
!= NULL
) {
1206 cln
->nconf
= getnetconfigent(cln
->netid
);
1207 if (cln
->nconf
== NULL
)
1208 syslog(LOG_ERR
, "%s: getnetconfigent failed",
1212 if (cln
->nconf
!= NULL
&& cln
->nbuf
!= NULL
)
1213 (void) __netdir_getbyaddr_nosrv(cln
->nconf
,
1214 &cln
->clnames
, cln
->nbuf
);
1216 cln
->flags
|= CLN_CLNAMES
;
1219 return (cln
->clnames
);
1223 * Return B_TRUE if the host is already available at no cost
1226 cln_havehost(struct cln
*cln
)
1228 return ((cln
->flags
& (CLN_CLNAMES
| CLN_HOST
)) != 0);
1232 cln_gethost(struct cln
*cln
)
1234 if (cln_getclientsnames(cln
) != NULL
)
1235 return (cln
->clnames
->h_hostservs
[0].h_host
);
1237 if ((cln
->flags
& CLN_HOST
) == 0) {
1238 if (cln
->nconf
== NULL
|| cln
->nbuf
== NULL
) {
1239 cln
->host
= strdup("(anon)");
1241 char host
[MAXIPADDRLEN
];
1243 if (strcmp(cln
->nconf
->nc_protofmly
, NC_INET
) == 0) {
1244 struct sockaddr_in
*sa
;
1246 /* LINTED pointer alignment */
1247 sa
= (struct sockaddr_in
*)(cln
->nbuf
->buf
);
1248 (void) inet_ntoa_r(sa
->sin_addr
, host
);
1250 cln
->host
= strdup(host
);
1251 } else if (strcmp(cln
->nconf
->nc_protofmly
,
1253 struct sockaddr_in6
*sa
;
1255 /* LINTED pointer alignment */
1256 sa
= (struct sockaddr_in6
*)(cln
->nbuf
->buf
);
1257 (void) inet_ntop(AF_INET6
,
1258 sa
->sin6_addr
.s6_addr
,
1259 host
, INET6_ADDRSTRLEN
);
1261 cln
->host
= strdup(host
);
1263 syslog(LOG_ERR
, gettext("Client's address is "
1264 "neither IPv4 nor IPv6"));
1266 cln
->host
= strdup("(anon)");
1270 cln
->flags
|= CLN_HOST
;
1277 * Check mount requests, add to mounted list if ok
1280 mount(struct svc_req
*rqstp
)
1284 struct fhstatus fhs
;
1285 struct mountres3 mountres3
;
1288 char *path
, rpath
[MAXPATHLEN
];
1292 int error
= 0, lofs_tried
= 0, enqueued
;
1293 int flavor_list
[MAX_FLAVORS
];
1299 transp
= rqstp
->rq_xprt
;
1300 version
= rqstp
->rq_vers
;
1303 if (!svc_getargs(transp
, xdr_dirpath
, (caddr_t
)&path
)) {
1304 svcerr_decode(transp
);
1308 cln_init(&cln
, transp
);
1311 * Put off getting the name for the client until we
1312 * need it. This is a performance gain. If we are logging,
1313 * then we don't care about performance and might as well
1314 * get the host name now in case we need to spit out an
1318 DTRACE_PROBE(mountd
, name_by_verbose
);
1319 if ((host
= cln_gethost(&cln
)) == NULL
) {
1321 * We failed to get a name for the client, even
1322 * 'anon', probably because we ran out of memory.
1323 * In this situation it doesn't make sense to
1324 * allow the mount to succeed.
1332 * If the version being used is less than the minimum version,
1333 * the filehandle translation should not be provided to the
1336 if (rejecting
|| version
< mount_vers_min
) {
1338 syslog(LOG_NOTICE
, "Rejected mount: %s for %s",
1345 * Get the real path (no symbolic links in it)
1347 if (realpath(path
, rpath
) == NULL
) {
1351 "mount request: realpath: %s: %m", path
);
1352 if (error
== ENOENT
)
1353 error
= mount_enoent_error(&cln
, path
, rpath
,
1358 if ((sh
= findentry(rpath
)) == NULL
&&
1359 (sh
= find_lofsentry(rpath
, &lofs_tried
)) == NULL
) {
1365 * Check if this is a "nosub" only export, in which case, mounting
1366 * subdirectories isn't allowed. Bug 1184573.
1368 if (checkrootmount(sh
, rpath
) == 0) {
1373 if (newopts(sh
->sh_opts
))
1374 flavor_count
= getclientsflavors_new(sh
, &cln
, flavor_list
);
1376 flavor_count
= getclientsflavors_old(sh
, &cln
, flavor_list
);
1378 if (flavor_count
== 0) {
1384 * Now get the filehandle.
1386 * NFS V2 clients get a 32 byte filehandle.
1387 * NFS V3 clients get a 32 or 64 byte filehandle, depending on
1388 * the embedded FIDs.
1390 vers
= (version
== MOUNTVERS3
) ? NFS_V3
: NFS_VERSION
;
1392 /* LINTED pointer alignment */
1393 while (nfs_getfh(rpath
, vers
, &len
, fh
) < 0) {
1394 if (errno
== EINVAL
&&
1395 (sh
= find_lofsentry(rpath
, &lofs_tried
)) != NULL
) {
1399 error
= errno
== EINVAL
? EACCES
: errno
;
1400 syslog(LOG_DEBUG
, "mount request: getfh failed on %s: %m",
1405 if (version
== MOUNTVERS3
) {
1406 mountres3
.mountres3_u
.mountinfo
.fhandle
.fhandle3_len
= len
;
1407 mountres3
.mountres3_u
.mountinfo
.fhandle
.fhandle3_val
= fh
;
1409 bcopy(fh
, &fhs
.fhstatus_u
.fhs_fhandle
, NFS_FHSIZE
);
1418 case MOUNTVERS_POSIX
:
1419 if (error
== EINVAL
)
1420 fhs
.fhs_status
= NFSERR_ACCES
;
1421 else if (error
== EREMOTE
)
1422 fhs
.fhs_status
= NFSERR_REMOTE
;
1424 fhs
.fhs_status
= error
;
1426 if (!svc_sendreply(transp
, xdr_fhstatus
, (char *)&fhs
))
1427 log_cant_reply_cln(&cln
);
1429 status
= fhs
.fhs_status
;
1434 mountres3
.mountres3_u
.mountinfo
.auth_flavors
.auth_flavors_val
=
1436 mountres3
.mountres3_u
.mountinfo
.auth_flavors
.auth_flavors_len
=
1439 } else if (error
== ENAMETOOLONG
)
1440 error
= MNT3ERR_NAMETOOLONG
;
1442 mountres3
.fhs_status
= error
;
1443 if (!svc_sendreply(transp
, xdr_mountres3
, (char *)&mountres3
))
1444 log_cant_reply_cln(&cln
);
1446 status
= mountres3
.fhs_status
;
1450 if (cln_havehost(&cln
))
1451 host
= cln_gethost(&cln
);
1454 syslog(LOG_NOTICE
, "MOUNT: %s %s %s",
1455 (host
== NULL
) ? "unknown host" : host
,
1456 error
? "denied" : "mounted", path
);
1459 * If we can not create a queue entry, go ahead and do it
1460 * in the context of this thread.
1462 enqueued
= enqueue_logging_data(host
, transp
, path
, rpath
,
1464 if (enqueued
== FALSE
) {
1466 DTRACE_PROBE(mountd
, name_by_in_thread
);
1467 host
= cln_gethost(&cln
);
1470 DTRACE_PROBE(mountd
, logged_in_thread
);
1472 mntlist_new(host
, rpath
); /* add entry to mount list */
1476 svc_freeargs(transp
, xdr_dirpath
, (caddr_t
)&path
);
1487 * Determine whether two paths are within the same file system.
1488 * Returns nonzero (true) if paths are the same, zero (false) if
1489 * they are different. If an error occurs, return false.
1491 * Use the actual FSID if it's available (via getattrat()); otherwise,
1492 * fall back on st_dev.
1494 * With ZFS snapshots, st_dev differs from the regular file system
1495 * versus the snapshot. But the fsid is the same throughout. Thus
1496 * the fsid is a better test.
1499 same_file_system(const char *path1
, const char *path2
)
1501 uint64_t fsid1
, fsid2
;
1502 struct stat st1
, st2
;
1503 nvlist_t
*nvl1
= NULL
;
1504 nvlist_t
*nvl2
= NULL
;
1506 if ((getattrat(AT_FDCWD
, XATTR_VIEW_READONLY
, path1
, &nvl1
) == 0) &&
1507 (getattrat(AT_FDCWD
, XATTR_VIEW_READONLY
, path2
, &nvl2
) == 0) &&
1508 (nvlist_lookup_uint64(nvl1
, A_FSID
, &fsid1
) == 0) &&
1509 (nvlist_lookup_uint64(nvl2
, A_FSID
, &fsid2
) == 0)) {
1513 * We have found fsid's for both paths.
1526 * We were unable to find fsid's for at least one of the paths.
1527 * fall back on st_dev.
1530 if (stat(path1
, &st1
) < 0) {
1531 syslog(LOG_NOTICE
, "%s: %m", path1
);
1534 if (stat(path2
, &st2
) < 0) {
1535 syslog(LOG_NOTICE
, "%s: %m", path2
);
1539 if (st1
.st_dev
== st2
.st_dev
)
1546 findentry(char *path
)
1549 struct sh_list
*shp
;
1554 (void) rw_rdlock(&sharetab_lock
);
1556 for (shp
= share_list
; shp
; shp
= shp
->shl_next
) {
1558 for (p1
= sh
->sh_path
, p2
= path
; *p1
== *p2
; p1
++, p2
++)
1560 goto done
; /* exact match */
1563 * Now compare the pathnames for three cases:
1565 * Parent: /export/foo (no trailing slash on parent)
1566 * Child: /export/foo/bar
1568 * Parent: /export/foo/ (trailing slash on parent)
1569 * Child: /export/foo/bar
1571 * Parent: /export/foo/ (no trailing slash on child)
1572 * Child: /export/foo
1574 if ((*p1
== '\0' && *p2
== '/') ||
1575 (*p1
== '\0' && *(p1
-1) == '/') ||
1576 (*p2
== '\0' && *p1
== '/' && *(p1
+1) == '\0')) {
1578 * We have a subdirectory. Test whether the
1579 * subdirectory is in the same file system.
1581 if (same_file_system(path
, sh
->sh_path
))
1586 sh
= shp
? sharedup(sh
) : NULL
;
1588 (void) rw_unlock(&sharetab_lock
);
1595 is_substring(char **mntp
, char **path
)
1597 char *p1
= *mntp
, *p2
= *path
;
1599 if (*p1
== '\0' && *p2
== '\0') /* exact match */
1601 else if (*p1
== '\0' && *p2
== '/')
1603 else if (*p1
== '\0' && *(p1
-1) == '/') {
1604 *path
= --p2
; /* we need the slash in p2 */
1606 } else if (*p2
== '\0') {
1609 if (*p1
== '\0') /* exact match */
1616 * find_lofsentry() searches for the real path which this requested LOFS path
1617 * (rpath) shadows. If found, it will return the sharetab entry of
1618 * the real path that corresponds to the LOFS path.
1619 * We first search mnttab to see if the requested path is an automounted
1620 * path. If it is an automounted path, it will trigger the mount by stat()ing
1621 * the requested path. Note that it is important to check that this path is
1622 * actually an automounted path, otherwise we would stat() a path which may
1623 * turn out to be NFS and block indefinitely on a dead server. The automounter
1624 * times-out if the server is dead, so there's no risk of hanging this
1625 * thread waiting for stat().
1626 * After the mount has been triggered (if necessary), we look for a
1627 * mountpoint of type LOFS (by searching /etc/mnttab again) which
1628 * is a substring of the rpath. If found, we construct a new path by
1629 * concatenating the mnt_special and the remaining of rpath, call findentry()
1630 * to make sure the 'real path' is shared.
1633 find_lofsentry(char *rpath
, int *done_flag
)
1635 struct stat r_stbuf
;
1636 mntlist_t
*ml
, *mntl
, *mntpnt
= NULL
;
1637 share_t
*retcode
= NULL
;
1638 char tmp_path
[MAXPATHLEN
];
1639 int mntpnt_len
= 0, tmp
;
1646 * While fsgetmntlist() uses lockf() to
1647 * lock the mnttab before reading it in,
1648 * the lock ignores threads in the same process.
1649 * Read in the mnttab with the protection of a mutex.
1651 (void) mutex_lock(&mnttab_lock
);
1652 mntl
= fsgetmntlist();
1653 (void) mutex_unlock(&mnttab_lock
);
1656 * Obtain the mountpoint for the requested path.
1658 for (ml
= mntl
; ml
; ml
= ml
->mntl_next
) {
1659 for (p1
= ml
->mntl_mnt
->mnt_mountp
, p2
= rpath
;
1660 *p1
== *p2
&& *p1
; p1
++, p2
++)
1662 if (is_substring(&p1
, &p2
) &&
1663 (tmp
= strlen(ml
->mntl_mnt
->mnt_mountp
)) >= mntpnt_len
) {
1670 * If the path needs to be autoFS mounted, trigger the mount by
1671 * stat()ing it. This is determined by checking whether the
1672 * mountpoint we just found is of type autofs.
1674 if (mntpnt
!= NULL
&&
1675 strcmp(mntpnt
->mntl_mnt
->mnt_fstype
, "autofs") == 0) {
1677 * The requested path is a substring of an autoFS filesystem.
1678 * Trigger the mount.
1680 if (stat(rpath
, &r_stbuf
) < 0) {
1682 syslog(LOG_NOTICE
, "%s: %m", rpath
);
1685 if ((r_stbuf
.st_mode
& S_IFMT
) == S_IFDIR
) {
1687 * The requested path is a directory, stat(2) it
1688 * again with a trailing '.' to force the autoFS
1689 * module to trigger the mount of indirect
1690 * automount entries, such as /net/jurassic/.
1692 if (strlen(rpath
) + 2 > MAXPATHLEN
) {
1695 "%s/.: exceeds MAXPATHLEN %d",
1700 (void) strcpy(tmp_path
, rpath
);
1701 (void) strcat(tmp_path
, "/.");
1703 if (stat(tmp_path
, &r_stbuf
) < 0) {
1705 syslog(LOG_NOTICE
, "%s: %m", tmp_path
);
1711 * The mount has been triggered, re-read mnttab to pick up
1712 * the changes made by autoFS.
1714 fsfreemntlist(mntl
);
1715 (void) mutex_lock(&mnttab_lock
);
1716 mntl
= fsgetmntlist();
1717 (void) mutex_unlock(&mnttab_lock
);
1721 * The autoFS mountpoint has been triggered if necessary,
1722 * now search mnttab again to determine if the requested path
1723 * is an LOFS mount of a shared path.
1726 for (ml
= mntl
; ml
; ml
= ml
->mntl_next
) {
1727 if (strcmp(ml
->mntl_mnt
->mnt_fstype
, "lofs"))
1730 for (p1
= ml
->mntl_mnt
->mnt_mountp
, p2
= rpath
;
1731 *p1
== *p2
&& *p1
; p1
++, p2
++)
1734 if (is_substring(&p1
, &p2
) &&
1735 ((tmp
= strlen(ml
->mntl_mnt
->mnt_mountp
)) >= mntpnt_len
)) {
1738 if ((strlen(ml
->mntl_mnt
->mnt_special
) + strlen(p2
)) >
1741 syslog(LOG_NOTICE
, "%s%s: exceeds %d",
1742 ml
->mntl_mnt
->mnt_special
, p2
,
1751 (void) strcpy(tmp_path
, ml
->mntl_mnt
->mnt_special
);
1752 (void) strcat(tmp_path
, p2
);
1755 retcode
= findentry(tmp_path
);
1760 assert(strlen(tmp_path
) > 0);
1761 (void) strcpy(rpath
, tmp_path
);
1765 fsfreemntlist(mntl
);
1770 * Determine whether an access list grants rights to a particular host.
1771 * We match on aliases of the hostname as well as on the canonical name.
1772 * Names in the access list may be either hosts or netgroups; they're
1773 * not distinguished syntactically. We check for hosts first because
1774 * it's cheaper, then try netgroups.
1777 * 1 - access is granted
1778 * 0 - access is denied
1779 * -1 - an error occured
1782 in_access_list(struct cln
*cln
,
1783 char *access_list
) /* N.B. we clobber this "input" parameter */
1785 char addr
[INET_ADDRSTRLEN
];
1788 char *cstr
= access_list
;
1789 char *gr
= access_list
;
1794 struct nd_hostservlist
*clnames
= NULL
;
1796 /* If no access list - then it's unrestricted */
1797 if (access_list
== NULL
|| *access_list
== '\0')
1800 if ((pnb
= cln_getnbuf(cln
)) == NULL
)
1804 if ((cstr
= strpbrk(cstr
, "[:")) != NULL
) {
1808 assert(*cstr
== '[');
1809 cstr
= strchr(cstr
+ 1, ']');
1818 * If the list name has a '-' prepended then a match of
1819 * the following name implies failure instead of success.
1829 * First check if we have '@' entry, as it doesn't
1830 * require client hostname.
1835 /* Netname support */
1836 if (!isdigit(*gr
) && *gr
!= '[') {
1837 struct netent n
, *np
;
1839 if ((np
= getnetbyname_r(gr
, &n
, buff
,
1840 sizeof (buff
))) != NULL
&&
1842 while ((np
->n_net
& 0xFF000000u
) == 0)
1844 np
->n_net
= htonl(np
->n_net
);
1845 if (inet_ntop(AF_INET
, &np
->n_net
, addr
,
1846 INET_ADDRSTRLEN
) == NULL
)
1848 ret
= inet_matchaddr(pnb
->buf
, addr
);
1850 if (errno
== EINVAL
) {
1857 } else if (ret
== 1) {
1862 ret
= inet_matchaddr(pnb
->buf
, gr
);
1864 if (errno
== EINVAL
) {
1866 "invalid access list "
1870 } else if (ret
== 1) {
1879 * No other checks can be performed if client address
1880 * can't be resolved.
1882 if ((clnames
= cln_getclientsnames(cln
)) == NULL
)
1885 /* Otherwise loop through all client hostname aliases */
1886 for (i
= 0; i
< clnames
->h_cnt
; i
++) {
1887 char *host
= clnames
->h_hostservs
[i
].h_host
;
1890 * If the list name begins with a dot then
1891 * do a domain name suffix comparison.
1892 * A single dot matches any name with no
1896 if (*(gr
+ 1) == '\0') { /* single dot */
1897 if (strchr(host
, '.') == NULL
)
1900 int off
= strlen(host
) - strlen(gr
);
1902 strcasecmp(host
+ off
, gr
) == 0) {
1907 /* Just do a hostname match */
1908 if (strcasecmp(gr
, host
) == 0)
1922 if (clnames
== NULL
)
1925 return (netgroup_check(clnames
, access_list
, nentries
));
1929 static char *optlist
[] = {
1936 #define OPT_SECURE 3
1940 #define OPT_WINDOW 5
1942 #define OPT_NOSUID 6
1950 #define OPT_UIDMAP 10
1952 #define OPT_GIDMAP 11
1958 map_flavor(char *str
)
1962 if (nfs_getseconfig_byname(str
, &sec
))
1965 return (sec
.sc_nfsnum
);
1969 * If the option string contains a "sec="
1970 * option, then use new option syntax.
1975 char *head
, *p
, *val
;
1977 if (!opts
|| *opts
== '\0')
1980 head
= strdup(opts
);
1982 syslog(LOG_ERR
, "opts: no memory");
1988 if (getsubopt(&p
, optlist
, &val
) == OPT_SEC
) {
1999 * Given an export and the clients hostname(s)
2000 * determine the security flavors that this
2001 * client is permitted to use.
2003 * This routine is called only for "old" syntax, i.e.
2004 * only one security flavor is allowed. So we need
2005 * to determine two things: the particular flavor,
2006 * and whether the client is allowed to use this
2007 * flavor, i.e. is in the access list.
2009 * Note that if there is no access list, then the
2010 * default is that access is granted.
2013 getclientsflavors_old(share_t
*sh
, struct cln
*cln
, int *flavors
)
2015 char *opts
, *p
, *val
;
2016 boolean_t ok
= B_FALSE
;
2017 int defaultaccess
= 1;
2018 boolean_t reject
= B_FALSE
;
2020 opts
= strdup(sh
->sh_opts
);
2022 syslog(LOG_ERR
, "getclientsflavors: no memory");
2026 flavors
[0] = AUTH_SYS
;
2031 switch (getsubopt(&p
, optlist
, &val
)) {
2033 flavors
[0] = AUTH_DES
;
2039 if (in_access_list(cln
, val
) > 0)
2045 if (in_access_list(cln
, val
) > 0)
2052 /* none takes precedence over everything else */
2056 return (defaultaccess
|| ok
);
2060 * Given an export and the clients hostname(s)
2061 * determine the security flavors that this
2062 * client is permitted to use.
2064 * This is somewhat more complicated than the "old"
2065 * routine because the options may contain multiple
2066 * security flavors (sec=) each with its own access
2067 * lists. So a client could be granted access based
2068 * on a number of security flavors. Note that the
2069 * type of access might not always be the same, the
2070 * client may get readonly access with one flavor
2071 * and readwrite with another, however the client
2072 * is not told this detail, it gets only the list
2073 * of flavors, and only if the client is using
2074 * version 3 of the mount protocol.
2077 getclientsflavors_new(share_t
*sh
, struct cln
*cln
, int *flavors
)
2079 char *opts
, *p
, *val
;
2082 boolean_t defaultaccess
= B_TRUE
; /* default access is rw */
2083 boolean_t access_ok
= B_FALSE
;
2085 boolean_t reject
= B_FALSE
;
2087 opts
= strdup(sh
->sh_opts
);
2089 syslog(LOG_ERR
, "getclientsflavors: no memory");
2097 switch (getsubopt(&p
, optlist
, &val
)) {
2100 access_ok
= B_FALSE
;
2103 * Before a new sec=xxx option, check if we need
2104 * to move the c index back to the previous count.
2106 if (!defaultaccess
&& !access_ok
) {
2110 /* get all the sec=f1[:f2] flavors */
2111 while ((f
= strtok_r(val
, ":", &lasts
)) != NULL
) {
2112 flavors
[c
++] = map_flavor(f
);
2116 /* for a new sec=xxx option, default is rw access */
2117 defaultaccess
= B_TRUE
;
2118 access_ok
= B_FALSE
;
2124 defaultaccess
= B_FALSE
;
2125 if (in_access_list(cln
, val
) > 0)
2130 defaultaccess
= B_FALSE
;
2131 if (in_access_list(cln
, val
) > 0)
2132 reject
= B_TRUE
; /* none overides rw/ro */
2138 access_ok
= B_FALSE
;
2140 if (!defaultaccess
&& !access_ok
)
2149 * This is a tricky piece of code that parses the
2150 * share options looking for a match on the auth
2151 * flavor that the client is using. If it finds
2152 * a match, then the client is given ro, rw, or
2153 * no access depending whether it is in the access
2154 * list. There is a special case for "secure"
2155 * flavor. Other flavors are values of the new "sec=" option.
2158 check_client(share_t
*sh
, struct cln
*cln
, int flavor
, uid_t clnt_uid
,
2159 gid_t clnt_gid
, uint_t clnt_ngids
, gid_t
*clnt_gids
, uid_t
*srv_uid
,
2160 gid_t
*srv_gid
, uint_t
*srv_ngids
, gid_t
**srv_gids
)
2162 if (newopts(sh
->sh_opts
))
2163 return (check_client_new(sh
, cln
, flavor
, clnt_uid
, clnt_gid
,
2164 clnt_ngids
, clnt_gids
, srv_uid
, srv_gid
, srv_ngids
,
2167 return (check_client_old(sh
, cln
, flavor
, clnt_uid
, clnt_gid
,
2168 clnt_ngids
, clnt_gids
, srv_uid
, srv_gid
, srv_ngids
,
2172 extern int _getgroupsbymember(const char *, gid_t
[], int, int);
2175 * Get supplemental groups for uid
2178 getusergroups(uid_t uid
, uint_t
*ngrps
, gid_t
**grps
)
2181 char *pwbuf
= alloca(pw_size
);
2182 gid_t
*tmpgrps
= alloca(ngroups_max
* sizeof (gid_t
));
2184 struct passwd
*result
;
2186 getpwuid_r(uid
, &pwd
, pwbuf
, pw_size
, &result
);
2190 tmpgrps
[0] = pwd
.pw_gid
;
2192 tmpngrps
= _getgroupsbymember(pwd
.pw_name
, tmpgrps
, ngroups_max
, 1);
2193 if (tmpngrps
<= 0) {
2195 "getusergroups(): Unable to get groups for user %s",
2201 *grps
= malloc(tmpngrps
* sizeof (gid_t
));
2202 if (*grps
== NULL
) {
2204 "getusergroups(): Memory allocation failed: %m");
2210 (void) memcpy(*grps
, tmpgrps
, tmpngrps
* sizeof (gid_t
));
2216 * is_a_number(number)
2218 * is the string a number in one of the forms we want to use?
2222 is_a_number(char *number
)
2227 if (strncmp(number
, "0x", 2) == 0) {
2230 } else if (*number
== '-') {
2231 number
++; /* skip the minus */
2233 while (ret
== 1 && *number
!= '\0') {
2235 ret
= isxdigit(*number
++);
2237 ret
= isdigit(*number
++);
2244 get_uid(char *value
, uid_t
*uid
)
2246 if (!is_a_number(value
)) {
2249 * in this case it would have to be a
2252 pw
= getpwnam(value
);
2259 intval
= strtoull(value
, NULL
, 0);
2260 if (intval
> UID_MAX
&& intval
!= -1)
2262 *uid
= (uid_t
)intval
;
2269 get_gid(char *value
, gid_t
*gid
)
2271 if (!is_a_number(value
)) {
2274 * in this case it would have to be a
2277 gr
= getgrnam(value
);
2284 intval
= strtoull(value
, NULL
, 0);
2285 if (intval
> UID_MAX
&& intval
!= -1)
2287 *gid
= (gid_t
)intval
;
2294 check_client_old(share_t
*sh
, struct cln
*cln
, int flavor
, uid_t clnt_uid
,
2295 gid_t clnt_gid
, uint_t clnt_ngids
, gid_t
*clnt_gids
, uid_t
*srv_uid
,
2296 gid_t
*srv_gid
, uint_t
*srv_ngids
, gid_t
**srv_gids
)
2298 char *opts
, *p
, *val
;
2299 int match
; /* Set when a flavor is matched */
2300 int perm
= 0; /* Set when "ro", "rw" or "root" is matched */
2301 int list
= 0; /* Set when "ro", "rw" is found */
2302 int ro_val
= 0; /* Set if ro option is 'ro=' */
2303 int rw_val
= 0; /* Set if rw option is 'rw=' */
2305 boolean_t map_deny
= B_FALSE
;
2307 opts
= strdup(sh
->sh_opts
);
2309 syslog(LOG_ERR
, "check_client: no memory");
2314 * If client provided 16 supplemental groups with AUTH_SYS, lookup
2315 * locally for all of them
2317 if (flavor
== AUTH_SYS
&& clnt_ngids
== NGRPS
&& ngroups_max
> NGRPS
)
2318 if (getusergroups(clnt_uid
, srv_ngids
, srv_gids
) == 0)
2319 perm
|= NFSAUTH_GROUPS
;
2325 switch (getsubopt(&p
, optlist
, &val
)) {
2330 if (perm
& NFSAUTH_GROUPS
) {
2334 perm
&= ~NFSAUTH_GROUPS
;
2343 if (in_access_list(cln
, val
) > 0)
2351 if (in_access_list(cln
, val
) > 0)
2357 * Check if the client is in
2358 * the root list. Only valid
2361 if (flavor
!= AUTH_SYS
)
2364 if (val
== NULL
|| *val
== '\0')
2370 if (in_access_list(cln
, val
) > 0) {
2371 perm
|= NFSAUTH_ROOT
;
2372 perm
|= NFSAUTH_UIDMAP
| NFSAUTH_GIDMAP
;
2375 if (perm
& NFSAUTH_GROUPS
) {
2379 perm
&= ~NFSAUTH_GROUPS
;
2386 * Check if the client should have no access
2387 * to this share at all. This option behaves
2388 * more like "root" than either "rw" or "ro".
2390 if (in_access_list(cln
, val
) > 0)
2391 perm
|= NFSAUTH_DENIED
;
2399 * The uidmap is supported for AUTH_SYS only.
2401 if (flavor
!= AUTH_SYS
)
2404 if (perm
& NFSAUTH_UIDMAP
|| map_deny
)
2407 for (c
= val
; c
!= NULL
; c
= n
) {
2419 al
= strchr(s
, ':');
2424 if (s
== NULL
|| al
== NULL
)
2428 if (clnt_uid
!= (uid_t
)-1)
2430 } else if (strcmp(c
, "*") != 0) {
2433 if (!get_uid(c
, &clnt
))
2436 if (clnt_uid
!= clnt
)
2442 else if (!get_uid(s
, &srv
))
2444 else if (srv
== (uid_t
)-1) {
2449 if (in_access_list(cln
, al
) > 0) {
2451 perm
|= NFSAUTH_UIDMAP
;
2453 if (perm
& NFSAUTH_GROUPS
) {
2457 perm
&= ~NFSAUTH_GROUPS
;
2472 * The gidmap is supported for AUTH_SYS only.
2474 if (flavor
!= AUTH_SYS
)
2477 if (perm
& NFSAUTH_GIDMAP
|| map_deny
)
2480 for (c
= val
; c
!= NULL
; c
= n
) {
2492 al
= strchr(s
, ':');
2497 if (s
== NULL
|| al
== NULL
)
2501 if (clnt_gid
!= (gid_t
)-1)
2503 } else if (strcmp(c
, "*") != 0) {
2506 if (!get_gid(c
, &clnt
))
2509 if (clnt_gid
!= clnt
)
2515 else if (!get_gid(s
, &srv
))
2517 else if (srv
== (gid_t
)-1) {
2522 if (in_access_list(cln
, al
) > 0) {
2524 perm
|= NFSAUTH_GIDMAP
;
2526 if (perm
& NFSAUTH_GROUPS
) {
2530 perm
&= ~NFSAUTH_GROUPS
;
2547 if (perm
& NFSAUTH_ROOT
) {
2553 perm
|= NFSAUTH_DENIED
;
2555 if (!(perm
& NFSAUTH_UIDMAP
))
2556 *srv_uid
= clnt_uid
;
2557 if (!(perm
& NFSAUTH_GIDMAP
))
2558 *srv_gid
= clnt_gid
;
2560 if (flavor
!= match
|| perm
& NFSAUTH_DENIED
)
2561 return (NFSAUTH_DENIED
);
2565 * If the client doesn't match an "ro" or "rw"
2566 * list then set no access.
2568 if ((perm
& (NFSAUTH_RO
| NFSAUTH_RW
)) == 0)
2569 perm
|= NFSAUTH_DENIED
;
2572 * The client matched a flavor entry that
2573 * has no explicit "rw" or "ro" determination.
2574 * Default it to "rw".
2580 * The client may show up in both ro= and rw=
2581 * lists. If so, then turn off the RO access
2582 * bit leaving RW access.
2584 if (perm
& NFSAUTH_RO
&& perm
& NFSAUTH_RW
) {
2586 * Logically cover all permutations of rw=,ro=.
2587 * In the case where, rw,ro=<host> we would like
2588 * to remove RW access for the host. In all other cases
2589 * RW wins the precedence battle.
2591 if (!rw_val
&& ro_val
) {
2592 perm
&= ~(NFSAUTH_RW
);
2594 perm
&= ~(NFSAUTH_RO
);
2602 * Check if the client has access by using a flavor different from
2603 * the given "flavor". If "flavor" is not in the flavor list,
2604 * return TRUE to indicate that this "flavor" is a wrong sec.
2607 is_wrongsec(share_t
*sh
, struct cln
*cln
, int flavor
)
2609 int flavor_list
[MAX_FLAVORS
];
2610 int flavor_count
, i
;
2612 /* get the flavor list that the client has access with */
2613 flavor_count
= getclientsflavors_new(sh
, cln
, flavor_list
);
2615 if (flavor_count
== 0)
2619 * Check if the given "flavor" is in the flavor_list.
2621 for (i
= 0; i
< flavor_count
; i
++) {
2622 if (flavor
== flavor_list
[i
])
2627 * If "flavor" is not in the flavor_list, return TRUE to indicate
2628 * that the client should have access by using a security flavor
2629 * different from this "flavor".
2635 * Given an export and the client's hostname, we
2636 * check the security options to see whether the
2637 * client is allowed to use the given security flavor.
2639 * The strategy is to proceed through the options looking
2640 * for a flavor match, then pay attention to the ro, rw,
2643 * Note that an entry may list several flavors in a
2644 * single entry, e.g.
2646 * sec=krb5,rw=clnt1:clnt2,ro,sec=sys,ro
2651 check_client_new(share_t
*sh
, struct cln
*cln
, int flavor
, uid_t clnt_uid
,
2652 gid_t clnt_gid
, uint_t clnt_ngids
, gid_t
*clnt_gids
, uid_t
*srv_uid
,
2653 gid_t
*srv_gid
, uint_t
*srv_ngids
, gid_t
**srv_gids
)
2655 char *opts
, *p
, *val
;
2658 int match
= 0; /* Set when a flavor is matched */
2659 int perm
= 0; /* Set when "ro", "rw" or "root" is matched */
2660 int list
= 0; /* Set when "ro", "rw" is found */
2661 int ro_val
= 0; /* Set if ro option is 'ro=' */
2662 int rw_val
= 0; /* Set if rw option is 'rw=' */
2664 boolean_t map_deny
= B_FALSE
;
2666 opts
= strdup(sh
->sh_opts
);
2668 syslog(LOG_ERR
, "check_client: no memory");
2673 * If client provided 16 supplemental groups with AUTH_SYS, lookup
2674 * locally for all of them
2676 if (flavor
== AUTH_SYS
&& clnt_ngids
== NGRPS
&& ngroups_max
> NGRPS
)
2677 if (getusergroups(clnt_uid
, srv_ngids
, srv_gids
) == 0)
2678 perm
|= NFSAUTH_GROUPS
;
2683 switch (getsubopt(&p
, optlist
, &val
)) {
2689 while ((f
= strtok_r(val
, ":", &lasts
))
2691 if (flavor
== map_flavor(f
)) {
2706 if (in_access_list(cln
, val
) > 0)
2717 if (in_access_list(cln
, val
) > 0)
2723 * Check if the client is in
2724 * the root list. Only valid
2727 if (flavor
!= AUTH_SYS
)
2733 if (val
== NULL
|| *val
== '\0')
2739 if (in_access_list(cln
, val
) > 0) {
2740 perm
|= NFSAUTH_ROOT
;
2741 perm
|= NFSAUTH_UIDMAP
| NFSAUTH_GIDMAP
;
2744 if (perm
& NFSAUTH_GROUPS
) {
2748 perm
&= ~NFSAUTH_GROUPS
;
2755 * Check if the client should have no access
2756 * to this share at all. This option behaves
2757 * more like "root" than either "rw" or "ro".
2759 if (in_access_list(cln
, val
) > 0)
2760 perm
|= NFSAUTH_DENIED
;
2768 * The uidmap is supported for AUTH_SYS only.
2770 if (flavor
!= AUTH_SYS
)
2773 if (!match
|| perm
& NFSAUTH_UIDMAP
|| map_deny
)
2776 for (c
= val
; c
!= NULL
; c
= n
) {
2788 al
= strchr(s
, ':');
2793 if (s
== NULL
|| al
== NULL
)
2797 if (clnt_uid
!= (uid_t
)-1)
2799 } else if (strcmp(c
, "*") != 0) {
2802 if (!get_uid(c
, &clnt
))
2805 if (clnt_uid
!= clnt
)
2811 else if (!get_uid(s
, &srv
))
2813 else if (srv
== (uid_t
)-1) {
2818 if (in_access_list(cln
, al
) > 0) {
2820 perm
|= NFSAUTH_UIDMAP
;
2822 if (perm
& NFSAUTH_GROUPS
) {
2826 perm
&= ~NFSAUTH_GROUPS
;
2841 * The gidmap is supported for AUTH_SYS only.
2843 if (flavor
!= AUTH_SYS
)
2846 if (!match
|| perm
& NFSAUTH_GIDMAP
|| map_deny
)
2849 for (c
= val
; c
!= NULL
; c
= n
) {
2861 al
= strchr(s
, ':');
2866 if (s
== NULL
|| al
== NULL
)
2870 if (clnt_gid
!= (gid_t
)-1)
2872 } else if (strcmp(c
, "*") != 0) {
2875 if (!get_gid(c
, &clnt
))
2878 if (clnt_gid
!= clnt
)
2884 else if (!get_gid(s
, &srv
))
2886 else if (srv
== (gid_t
)-1) {
2891 if (in_access_list(cln
, al
) > 0) {
2893 perm
|= NFSAUTH_GIDMAP
;
2895 if (perm
& NFSAUTH_GROUPS
) {
2899 perm
&= ~NFSAUTH_GROUPS
;
2915 if (perm
& NFSAUTH_ROOT
) {
2921 perm
|= NFSAUTH_DENIED
;
2923 if (!(perm
& NFSAUTH_UIDMAP
))
2924 *srv_uid
= clnt_uid
;
2925 if (!(perm
& NFSAUTH_GIDMAP
))
2926 *srv_gid
= clnt_gid
;
2929 * If no match then set the perm accordingly
2931 if (!match
|| perm
& NFSAUTH_DENIED
) {
2933 return (NFSAUTH_DENIED
);
2938 * If the client doesn't match an "ro" or "rw" list then
2939 * check if it may have access by using a different flavor.
2940 * If so, return NFSAUTH_WRONGSEC.
2941 * If not, return NFSAUTH_DENIED.
2943 if ((perm
& (NFSAUTH_RO
| NFSAUTH_RW
)) == 0) {
2944 if (is_wrongsec(sh
, cln
, flavor
))
2945 perm
|= NFSAUTH_WRONGSEC
;
2947 perm
|= NFSAUTH_DENIED
;
2951 * The client matched a flavor entry that
2952 * has no explicit "rw" or "ro" determination.
2953 * Make sure it defaults to "rw".
2959 * The client may show up in both ro= and rw=
2960 * lists. If so, then turn off the RO access
2961 * bit leaving RW access.
2963 if (perm
& NFSAUTH_RO
&& perm
& NFSAUTH_RW
) {
2965 * Logically cover all permutations of rw=,ro=.
2966 * In the case where, rw,ro=<host> we would like
2967 * to remove RW access for the host. In all other cases
2968 * RW wins the precedence battle.
2970 if (!rw_val
&& ro_val
) {
2971 perm
&= ~(NFSAUTH_RW
);
2973 perm
&= ~(NFSAUTH_RO
);
2987 static timestruc_t last_sharetab_time
;
2988 timestruc_t prev_sharetab_time
;
2990 struct sh_list
*shp
, *shp_prev
;
2994 * read in /etc/dfs/sharetab if it has changed
2996 if (stat(SHARETAB
, &st
) != 0) {
2997 syslog(LOG_ERR
, "Cannot stat %s: %m", SHARETAB
);
3001 if (st
.st_mtim
.tv_sec
== last_sharetab_time
.tv_sec
&&
3002 st
.st_mtim
.tv_nsec
== last_sharetab_time
.tv_nsec
) {
3010 * Remember the mod time, then after getting the
3011 * write lock check again. If another thread
3012 * already did the update, then there's no
3015 prev_sharetab_time
= last_sharetab_time
;
3017 (void) rw_wrlock(&sharetab_lock
);
3019 if (prev_sharetab_time
.tv_sec
!= last_sharetab_time
.tv_sec
||
3020 prev_sharetab_time
.tv_nsec
!= last_sharetab_time
.tv_nsec
) {
3021 (void) rw_unlock(&sharetab_lock
);
3026 * Note that since the sharetab is now in memory
3027 * and a snapshot is taken, we no longer have to
3030 f
= fopen(SHARETAB
, "r");
3032 syslog(LOG_ERR
, "Cannot open %s: %m", SHARETAB
);
3033 (void) rw_unlock(&sharetab_lock
);
3038 * Once we are sure /etc/dfs/sharetab has been
3039 * modified, flush netgroup cache entries.
3041 netgrp_cache_flush();
3043 sh_free(share_list
); /* free old list */
3046 while ((res
= getshare(f
, &sh
)) > 0) {
3048 if (strcmp(sh
->sh_fstype
, "nfs") != 0)
3051 shp
= malloc(sizeof (*shp
));
3054 if (share_list
== NULL
)
3057 /* LINTED not used before set */
3058 shp_prev
->shl_next
= shp
;
3060 shp
->shl_next
= NULL
;
3061 shp
->shl_sh
= sharedup(sh
);
3062 if (shp
->shl_sh
== NULL
)
3067 syslog(LOG_ERR
, "%s: invalid at line %d\n",
3070 if (stat(SHARETAB
, &st
) != 0) {
3071 syslog(LOG_ERR
, "Cannot stat %s: %m", SHARETAB
);
3073 (void) rw_unlock(&sharetab_lock
);
3077 last_sharetab_time
= st
.st_mtim
;
3079 (void) rw_unlock(&sharetab_lock
);
3085 syslog(LOG_ERR
, "check_sharetab: no memory");
3086 sh_free(share_list
);
3089 (void) rw_unlock(&sharetab_lock
);
3093 sh_free(struct sh_list
*shp
)
3095 struct sh_list
*next
;
3098 sharefree(shp
->shl_sh
);
3099 next
= shp
->shl_next
;
3107 * Remove an entry from mounted list
3110 umount(struct svc_req
*rqstp
)
3112 char *host
, *path
, *remove_path
;
3113 char rpath
[MAXPATHLEN
];
3117 transp
= rqstp
->rq_xprt
;
3119 if (!svc_getargs(transp
, xdr_dirpath
, (caddr_t
)&path
)) {
3120 svcerr_decode(transp
);
3124 cln_init(&cln
, transp
);
3127 if (!svc_sendreply(transp
, xdr_void
, NULL
))
3128 log_cant_reply_cln(&cln
);
3130 host
= cln_gethost(&cln
);
3133 * Without the hostname we can't delete this host from the
3136 svc_freeargs(transp
, xdr_dirpath
, (caddr_t
)&path
);
3141 syslog(LOG_NOTICE
, "UNMOUNT: %s unmounted %s", host
, path
);
3143 remove_path
= rpath
; /* assume we will use the cannonical path */
3144 if (realpath(path
, rpath
) == NULL
) {
3146 syslog(LOG_WARNING
, "UNMOUNT: realpath: %s: %m ", path
);
3147 remove_path
= path
; /* use path provided instead */
3150 mntlist_delete(host
, remove_path
); /* remove from mount list */
3154 svc_freeargs(transp
, xdr_dirpath
, (caddr_t
)&path
);
3158 * Remove all entries for one machine from mounted list
3161 umountall(struct svc_req
*rqstp
)
3167 transp
= rqstp
->rq_xprt
;
3168 if (!svc_getargs(transp
, xdr_void
, NULL
)) {
3169 svcerr_decode(transp
);
3173 * We assume that this call is asynchronous and made via rpcbind
3174 * callit routine. Therefore return control immediately. The error
3175 * causes rpcbind to remain silent, as opposed to every machine
3176 * on the net blasting the requester with a response.
3178 svcerr_systemerr(transp
);
3180 cln_init(&cln
, transp
);
3182 host
= cln_gethost(&cln
);
3184 /* Can't do anything without the name of the client */
3189 * Remove all hosts entries from mount list
3191 mntlist_delete_all(host
);
3194 syslog(LOG_NOTICE
, "UNMOUNTALL: from %s", host
);
3200 exmalloc(size_t size
)
3204 if ((ret
= malloc(size
)) == NULL
) {
3205 syslog(LOG_ERR
, "Out of memory");