6f1bf5767744e2653a897e044647c9808b06d490
[openafs.git] / src / afs / LINUX / osi_nfssrv.c
1 /*
2  * vi:set cin noet sw=4 tw=70:
3  * Copyright 2006, International Business Machines Corporation and others.
4  * All Rights Reserved.
5  * 
6  * This software has been released under the terms of the IBM Public
7  * License.  For details, see the LICENSE file in the top-level source
8  * directory or online at http://www.openafs.org/dl/license10.html
9  */
10
11 /*
12  * Filesystem export operations for Linux
13  */
14 #include <afsconfig.h>
15 #include "afs/param.h"
16
17
18 #if !defined(AFS_NONFSTRANS) || defined(AFS_AIX_IAUTH_ENV)
19 #include <linux/module.h> /* early to avoid printf->printk mapping */
20 #include <linux/fs.h>
21 #include "afs/sysincludes.h"
22 #include "afsincludes.h"
23 #include "nfsclient.h"
24 #include "h/smp_lock.h"
25 #include <linux/sunrpc/svc.h>
26 #include <linux/sunrpc/svcauth.h>
27
28 static unsigned long authtab_addr = 0;
29 #if defined(module_param) && LINUX_VERSION_CODE > KERNEL_VERSION(2,6,9)
30 module_param(authtab_addr, long, 0);
31 #else
32 MODULE_PARM(authtab_addr, "l");
33 #endif
34 MODULE_PARM_DESC(authtab_addr, "Address of the authtab array.");
35
36 extern struct auth_ops *authtab[] __attribute__((weak));
37 static struct auth_ops **afs_authtab;
38 static struct auth_ops *afs_new_authtab[RPC_AUTH_MAXFLAVOR];
39 static struct auth_ops *afs_orig_authtab[RPC_AUTH_MAXFLAVOR];
40
41 static int whine_memory = 0;
42
43 afs_lock_t afs_xnfssrv;
44
45 struct nfs_server_thread {
46     struct nfs_server_thread *next;     /* next in chain */
47     pid_t pid;                          /* pid of this thread */
48     int active;                         /* this thread is servicing an RPC */
49     struct sockaddr_in client_addr;     /* latest client of this thread */
50     int client_addrlen;
51     afs_int32 uid;                      /* AFS UID/PAG for this thread */
52     afs_int32 code;                     /* What should InitReq return? */
53     int flavor;                         /* auth flavor */
54     uid_t client_uid;                   /* UID claimed by client */
55     gid_t client_gid;                   /* GID claimed by client */
56     gid_t client_g0, client_g1;         /* groups claimed by client */
57 };
58
59 static struct nfs_server_thread *nfssrv_list = 0;
60
61 static struct nfs_server_thread *find_nfs_thread(int create)
62 {
63     struct nfs_server_thread *this;
64
65     /* Check that this is an nfsd kernel thread */
66     if (current->files != init_task.files || strcmp(current->comm, "nfsd"))
67         return 0;
68
69     ObtainWriteLock(&afs_xnfssrv, 804);
70     for (this = nfssrv_list; this; this = this->next)
71         if (this->pid == current->pid)
72             break;
73     if (!this && create) {
74         this = afs_osi_Alloc(sizeof(struct nfs_server_thread));
75         if (this) {
76             this->next = nfssrv_list;
77             this->pid  = current->pid;
78             this->client_addrlen = 0;
79             nfssrv_list = this;
80             printk("afs: added nfsd task %d/%d\n",
81                    current->tgid, current->pid);
82         } else if (!whine_memory) {
83             whine_memory = 1;
84             printk("afs: failed to allocate memory for nfsd task %d/%d\n",
85                    current->tgid, current->pid);
86         }
87     }
88     ReleaseWriteLock(&afs_xnfssrv);
89     return this;
90 }
91
92 static int
93 svcauth_afs_accept(struct svc_rqst *rqstp, u32 *authp)
94 {
95     struct nfs_server_thread *ns;
96     struct afs_exporter *outexp;
97     struct AFS_UCRED *credp;
98     struct sockaddr_in *addr;
99     int code;
100
101     code = afs_orig_authtab[rqstp->rq_authop->flavour]->accept(rqstp, authp);
102     if (code != SVC_OK)
103         return code;
104
105     AFS_GLOCK();
106     ns = find_nfs_thread(1);
107     if (!ns) {
108         AFS_GUNLOCK();
109         /* XXX maybe we should fail this with rpc_system_err? */
110         return SVC_OK;
111     }
112 #if HAVE_SVC_ADDR_IN
113     addr = svc_addr_in(rqstp);
114 #else
115     addr = &rqstp->rq_addr;
116 #endif
117
118     ns->active          = 1;
119     ns->flavor          = rqstp->rq_authop->flavour;
120     ns->code            = EACCES;
121     ns->client_addr     = *addr;
122     ns->client_addrlen  = rqstp->rq_addrlen;
123     ns->client_uid      = rqstp->rq_cred.cr_uid;
124     ns->client_gid      = rqstp->rq_cred.cr_gid;
125     if (rqstp->rq_cred.cr_group_info->ngroups > 0)
126         ns->client_g0   = GROUP_AT(rqstp->rq_cred.cr_group_info, 0);
127     else
128         ns->client_g0   = -1;
129     if (rqstp->rq_cred.cr_group_info->ngroups > 1)
130         ns->client_g1   = GROUP_AT(rqstp->rq_cred.cr_group_info, 1);
131     else
132         ns->client_g1   = -1;
133
134     if (addr->sin_family != AF_INET) {
135         printk("afs: NFS request from non-IPv4 client (family %d len %d)\n",
136                addr->sin_family, rqstp->rq_addrlen);
137         goto done;
138     }
139
140     credp = crget();
141     credp->cr_uid = rqstp->rq_cred.cr_uid;
142     credp->cr_gid = rqstp->rq_cred.cr_gid;
143     get_group_info(rqstp->rq_cred.cr_group_info);
144     credp->cr_group_info = rqstp->rq_cred.cr_group_info;
145
146     /* avoid creating wildcard entries by mapping anonymous
147      * clients to afs_nobody */
148     if (credp->cr_uid == -1)
149         credp->cr_uid = -2;
150     code = afs_nfsclient_reqhandler(0, &credp, addr->sin_addr.s_addr,
151                                     &ns->uid, &outexp);
152     if (!code && outexp) EXP_RELE(outexp);
153     if (!code) ns->code = 0;
154     if (code)
155         printk("afs: svcauth_afs_accept: afs_nfsclient_reqhandler: %d\n", code);
156     crfree(credp);
157
158 done:
159     AFS_GUNLOCK();
160     return SVC_OK;
161 }
162
163
164 #if 0
165 /* This doesn't work, because they helpfully NULL out rqstp->authop
166  * before calling us, so we have no way to tell what the original
167  * auth flavor was.
168  */
169 static int
170 svcauth_afs_release(struct svc_rqst *rqstp)
171 {
172     struct nfs_server_thread *ns;
173
174     AFS_GLOCK();
175     ns = find_nfs_thread(0);
176     if (ns) ns->active = 0;
177     AFS_GUNLOCK();
178
179     return afs_orig_authtab[rqstp->rq_authop->flavour]->release(rqstp);
180 }
181 #endif
182
183
184 int osi_linux_nfs_initreq(struct vrequest *av, struct AFS_UCRED *cr, int *code)
185 {
186     struct nfs_server_thread *ns;
187
188     ns = find_nfs_thread(0);
189     if (!ns || !ns->active)
190         return 0;
191
192     *code = ns->code;
193     if (!ns->code) {
194         cr->cr_ruid = NFSXLATOR_CRED;
195         av->uid = ns->uid;
196     }
197     return 1;
198 }
199
200 void osi_linux_nfssrv_init(void)
201 {
202     int i;
203
204     nfssrv_list = 0;
205     AFS_RWLOCK_INIT(&afs_xnfssrv, "afs_xnfssrv");
206
207     if (authtab && !IS_ERR(authtab))
208            afs_authtab = authtab;
209     else if (authtab_addr) afs_authtab = (struct auth_ops **)authtab_addr;
210     else {
211         printk("Warning: Unable to find the address of authtab\n");
212         printk("NFS Translator hooks will not be installed\n");
213         printk("To correct, specify authtab_addr=<authtab>\n");
214         afs_authtab = 0;
215         return;
216     }
217
218     for (i = 0; i < RPC_AUTH_MAXFLAVOR; i++) {
219         afs_orig_authtab[i] = afs_authtab[i];
220         if (!afs_orig_authtab[i] || afs_orig_authtab[i]->flavour != i ||
221             !try_module_get(afs_orig_authtab[i]->owner)) {
222             afs_orig_authtab[i] = 0;
223             continue;
224         }
225
226         afs_new_authtab[i] = afs_osi_Alloc(sizeof(struct auth_ops));
227         *(afs_new_authtab[i]) = *(afs_orig_authtab[i]);
228         afs_new_authtab[i]->owner = THIS_MODULE;
229         afs_new_authtab[i]->accept = svcauth_afs_accept;
230         /* afs_new_authtab[i]->release = svcauth_afs_release; */
231         svc_auth_unregister(i);
232         svc_auth_register(i, afs_new_authtab[i]);
233     }
234 }
235
236 void osi_linux_nfssrv_shutdown(void)
237 {
238     struct nfs_server_thread *next;
239     int i;
240
241     if (afs_authtab) {
242         for (i = 0; i < RPC_AUTH_MAXFLAVOR; i++) {
243             if (!afs_orig_authtab[i])
244                 continue;
245             svc_auth_unregister(i);
246             svc_auth_register(i, afs_orig_authtab[i]);
247             module_put(afs_orig_authtab[i]->owner);
248             afs_osi_Free(afs_new_authtab[i], sizeof(struct auth_ops));
249         }
250     }
251
252     AFS_GLOCK();
253     ObtainWriteLock(&afs_xnfssrv, 805);
254     while (nfssrv_list) {
255         next = nfssrv_list->next;
256         afs_osi_Free(nfssrv_list, sizeof(struct nfs_server_thread));
257         nfssrv_list = next;
258     }
259     ReleaseWriteLock(&afs_xnfssrv);
260     AFS_GUNLOCK();
261 }
262 #endif /* AFS_NONFSTRANS */
263