xref: /dragonfly/sys/vfs/nfs/nfs_kerb.c (revision cecb9aae)
1 /*
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)nfs_nqlease.c	8.9 (Berkeley) 5/20/95
37  * $FreeBSD: src/sys/nfs/nfs_nqlease.c,v 1.50 2000/02/13 03:32:05 peter Exp $
38  * $DragonFly: src/sys/vfs/nfs/nfs_kerb.c,v 1.3 2006/09/05 00:55:50 dillon Exp $
39  */
40 
41 #include <sys/param.h>
42 #include <sys/vnode.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 
53 #include <netinet/in.h>
54 #include "rpcv2.h"
55 #include "nfsproto.h"
56 #include "nfs.h"
57 #include "nfsm_subs.h"
58 #include "xdr_subs.h"
59 #include "nfsmount.h"
60 #include "nfsnode.h"
61 
62 #include <sys/thread2.h>
63 
64 #define TRUE	1
65 #define	FALSE	0
66 
67 #ifndef NFS_NOSERVER
68 
69 /*
70  * Nqnfs client helper daemon. Runs once a second to expire leases.
71  * It also get authorization strings for "kerb" mounts.
72  * It must start at the beginning of the list again after any potential
73  * "sleep" since nfs_reclaim() called from vclean() can pull a node off
74  * the list asynchronously.
75  */
76 int
77 nfs_clientd(struct nfsmount *nmp, struct ucred *cred, struct nfsd_cargs *ncd,
78 	    int flag, caddr_t argp, struct thread *td)
79 {
80 	struct nfsuid *nuidp, *nnuidp;
81 	int error = 0;
82 
83 	/*
84 	 * If an authorization string is being passed in, get it.
85 	 */
86 	if ((flag & NFSSVC_GOTAUTH) &&
87 	    (nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_DISMNT)) == 0) {
88 	    if (nmp->nm_state & NFSSTA_HASAUTH)
89 		panic("cld kerb");
90 	    if ((flag & NFSSVC_AUTHINFAIL) == 0) {
91 		if (ncd->ncd_authlen <= nmp->nm_authlen &&
92 		    ncd->ncd_verflen <= nmp->nm_verflen &&
93 		    !copyin(ncd->ncd_authstr,nmp->nm_authstr,ncd->ncd_authlen)&&
94 		    !copyin(ncd->ncd_verfstr,nmp->nm_verfstr,ncd->ncd_verflen)){
95 		    nmp->nm_authtype = ncd->ncd_authtype;
96 		    nmp->nm_authlen = ncd->ncd_authlen;
97 		    nmp->nm_verflen = ncd->ncd_verflen;
98 #ifdef NFSKERB
99 		    nmp->nm_key = ncd->ncd_key;
100 #endif
101 		} else
102 		    nmp->nm_state |= NFSSTA_AUTHERR;
103 	    } else
104 		nmp->nm_state |= NFSSTA_AUTHERR;
105 	    nmp->nm_state |= NFSSTA_HASAUTH;
106 	    wakeup((caddr_t)&nmp->nm_authlen);
107 	} else
108 	    nmp->nm_state |= NFSSTA_WAITAUTH;
109 
110 	/*
111 	 * Loop every second updating queue until there is a termination sig.
112 	 */
113 	while ((nmp->nm_state & NFSSTA_DISMNT) == 0) {
114 	    /*
115 	     * Get an authorization string, if required.
116 	     */
117 	    if ((nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_DISMNT | NFSSTA_HASAUTH)) == 0) {
118 		ncd->ncd_authuid = nmp->nm_authuid;
119 		if (copyout((caddr_t)ncd, argp, sizeof (struct nfsd_cargs)))
120 			nmp->nm_state |= NFSSTA_WAITAUTH;
121 		else
122 			return (ENEEDAUTH);
123 	    }
124 
125 	    /*
126 	     * Wait a bit (no pun) and do it again.
127 	     */
128 	    if ((nmp->nm_state & NFSSTA_DISMNT) == 0 &&
129 		(nmp->nm_state & (NFSSTA_WAITAUTH | NFSSTA_HASAUTH))) {
130 		    error = tsleep((caddr_t)&nmp->nm_authstr, PCATCH,
131 			"nqnfstimr", hz / 3);
132 		    if (error == EINTR || error == ERESTART)
133 			(void) dounmount(nmp->nm_mountp, 0);
134 	    }
135 	}
136 
137 	/*
138 	 * Finally, we can free up the mount structure.
139 	 */
140 	TAILQ_FOREACH_MUTABLE(nuidp, &nmp->nm_uidlruhead, nu_lru, nnuidp) {
141 		LIST_REMOVE(nuidp, nu_hash);
142 		TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
143 		kfree((caddr_t)nuidp, M_NFSUID);
144 	}
145 	nfs_free_mount(nmp);
146 	if (error == EWOULDBLOCK)
147 		error = 0;
148 	return (error);
149 }
150 
151 #endif /* NFS_NOSERVER */
152 
153