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