Whamcloud - gitweb
b=20595
[fs/lustre-release.git] / lustre / obdfilter / filter_capa.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdfilter/filter_capa.c
37  *
38  * Author: Lai Siyao <lsy@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_FILTER
42
43 #include <linux/fs.h>
44 #include <linux/version.h>
45 #include <asm/uaccess.h>
46 #include <linux/file.h>
47 #include <linux/kmod.h>
48
49 #include <lustre_fsfilt.h>
50 #include <lustre_capa.h>
51
52 #include "filter_internal.h"
53
54 static inline __u32 filter_ck_keyid(struct filter_capa_key *key)
55 {
56         return key->k_key.lk_keyid;
57 }
58
59 int filter_update_capa_key(struct obd_device *obd, struct lustre_capa_key *new)
60 {
61         struct filter_obd *filter = &obd->u.filter;
62         struct filter_capa_key *k, *keys[2] = { NULL, NULL };
63         int i;
64
65         spin_lock(&capa_lock);
66         list_for_each_entry(k, &filter->fo_capa_keys, k_list) {
67                 if (k->k_key.lk_mdsid != new->lk_mdsid)
68                         continue;
69
70                 if (keys[0]) {
71                         keys[1] = k;
72                         if (filter_ck_keyid(keys[1]) > filter_ck_keyid(keys[0]))
73                                 keys[1] = keys[0], keys[0] = k;
74                 } else {
75                         keys[0] = k;
76                 }
77         }
78         spin_unlock(&capa_lock);
79
80         for (i = 0; i < 2; i++) {
81                 if (!keys[i])
82                         continue;
83                 if (filter_ck_keyid(keys[i]) != new->lk_keyid)
84                         continue;
85                 /* maybe because of recovery or other reasons, MDS sent the
86                  * the old capability key again.
87                  */
88                 spin_lock(&capa_lock);
89                 keys[i]->k_key = *new;
90                 spin_unlock(&capa_lock);
91
92                 RETURN(0);
93         }
94
95         if (keys[1]) {
96                 /* if OSS already have two keys, update the old one */
97                 k = keys[1];
98         } else {
99                 OBD_ALLOC_PTR(k);
100                 if (!k)
101                         RETURN(-ENOMEM);
102                 CFS_INIT_LIST_HEAD(&k->k_list);
103         }
104
105         spin_lock(&capa_lock);
106         k->k_key = *new;
107         if (list_empty(&k->k_list))
108                 list_add(&k->k_list, &filter->fo_capa_keys);
109         spin_unlock(&capa_lock);
110
111         DEBUG_CAPA_KEY(D_SEC, new, "new");
112         RETURN(0);
113 }
114
115 int filter_auth_capa(struct obd_export *exp, struct lu_fid *fid, __u64 mdsid,
116                      struct lustre_capa *capa, __u64 opc)
117 {
118         struct obd_device *obd = exp->exp_obd;
119         struct filter_obd *filter = &obd->u.filter;
120         struct filter_capa_key *k;
121         struct lustre_capa_key key;
122         struct obd_capa *oc;
123         __u8 *hmac;
124         int keys_ready = 0, key_found = 0, rc = 0;
125         ENTRY;
126
127         /* capability is disabled */
128         if (!filter->fo_fl_oss_capa)
129                 RETURN(0);
130
131         if (!(exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
132                 RETURN(0);
133
134         if (capa == NULL) {
135                 if (fid)
136                         CERROR("mdsno/fid/opc "LPU64"/"DFID"/"LPX64
137                                ": no capability has been passed\n",
138                                mdsid, PFID(fid), opc);
139                 else
140                         CERROR("mdsno/opc "LPU64"/"LPX64
141                                ": no capability has been passed\n",
142                                mdsid, opc);
143                 RETURN(-EACCES);
144         }
145
146         if (opc == CAPA_OPC_OSS_READ) {
147                 if (!(capa->lc_opc & CAPA_OPC_OSS_RW))
148                         rc = -EACCES;
149         } else if (!capa_opc_supported(capa, opc)) {
150                 rc = -EACCES;
151         }
152         if (rc) {
153                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
154                 RETURN(rc);
155         }
156
157         oc = capa_lookup(filter->fo_capa_hash, capa, 0);
158         if (oc) {
159                 spin_lock(&oc->c_lock);
160                 if (capa_is_expired(oc)) {
161                         DEBUG_CAPA(D_ERROR, capa, "expired");
162                         rc = -ESTALE;
163                 }
164                 spin_unlock(&oc->c_lock);
165
166                 capa_put(oc);
167                 RETURN(rc);
168         }
169
170         if (capa_is_expired_sec(capa)) {
171                 DEBUG_CAPA(D_ERROR, capa, "expired");
172                 RETURN(-ESTALE);
173         }
174
175         spin_lock(&capa_lock);
176         list_for_each_entry(k, &filter->fo_capa_keys, k_list) {
177                 if (k->k_key.lk_mdsid == mdsid) {
178                         keys_ready = 1;
179                         if (k->k_key.lk_keyid == capa_keyid(capa)) {
180                                 key = k->k_key;
181                                 key_found = 1;
182                                 break;
183                         }
184                 }
185         }
186         spin_unlock(&capa_lock);
187
188         if (!keys_ready) {
189                 CDEBUG(D_SEC, "MDS hasn't propagated capability keys yet, "
190                        "ignore check!\n");
191                 RETURN(0);
192         }
193
194        if (!key_found) {
195                 DEBUG_CAPA(D_ERROR, capa, "no matched capability key for");
196                 RETURN(-ESTALE);
197         }
198
199         OBD_ALLOC(hmac, CAPA_HMAC_MAX_LEN);
200         if (hmac == NULL)
201                 RETURN(-ENOMEM);
202
203         rc = capa_hmac(hmac, capa, key.lk_key);
204         if (rc) {
205                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: rc %d", rc);
206                 OBD_FREE(hmac, CAPA_HMAC_MAX_LEN);
207                 RETURN(rc);
208         }
209
210         rc = memcmp(hmac, capa->lc_hmac, CAPA_HMAC_MAX_LEN);
211         OBD_FREE(hmac, CAPA_HMAC_MAX_LEN);
212         if (rc) {
213                 DEBUG_CAPA_KEY(D_ERROR, &key, "calculate HMAC with ");
214                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
215                 RETURN(-EACCES);
216         }
217
218         /* store in capa hash */
219         oc = capa_add(filter->fo_capa_hash, capa);
220         capa_put(oc);
221         RETURN(0);
222 }
223
224 int filter_capa_fixoa(struct obd_export *exp, struct obdo *oa, __u64 mdsid,
225                       struct lustre_capa *capa)
226 {
227         int rc = 0;
228         ENTRY;
229
230         if (!(exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
231                 RETURN(0);
232
233         if (unlikely(!capa))
234                 RETURN(-EACCES);
235
236         if (capa_flags(capa) == LC_ID_CONVERT) {
237                 struct obd_device *obd = exp->exp_obd;
238                 struct filter_obd *filter = &obd->u.filter;
239                 struct filter_capa_key *k;
240                 int found = 0;
241
242                 spin_lock(&capa_lock);
243                 list_for_each_entry(k, &filter->fo_capa_keys, k_list) {
244                         if (k->k_key.lk_mdsid == mdsid &&
245                             k->k_key.lk_keyid == capa_keyid(capa)) {
246                                 found = 1;
247                                 break;
248                         }
249                 }
250                 spin_unlock(&capa_lock);
251
252                 if (found) {
253                         union {
254                                 __u64 id64;
255                                 __u32 id32[2];
256                         } uid, gid;
257                         __u32 d[4], s[4];
258
259                         uid.id64 = capa_uid(capa);
260                         gid.id64 = capa_gid(capa);
261                         s[0] = uid.id32[0];
262                         s[1] = uid.id32[1];
263                         s[2] = gid.id32[0];
264                         s[3] = gid.id32[1];
265
266                         rc = capa_decrypt_id(d, s, k->k_key.lk_key,
267                                              CAPA_HMAC_KEY_MAX_LEN);
268                         if (unlikely(rc))
269                                 RETURN(rc);
270
271                         oa->o_uid = d[0];
272                         oa->o_gid = d[2];
273                 } else {
274                         DEBUG_CAPA(D_ERROR, capa, "no matched capability key for");
275                         rc = -ESTALE;
276                 }
277         }
278
279         RETURN(rc);
280 }
281
282 void filter_free_capa_keys(struct filter_obd *filter)
283 {
284         struct filter_capa_key *key, *n;
285
286         spin_lock(&capa_lock);
287         list_for_each_entry_safe(key, n, &filter->fo_capa_keys, k_list) {
288                 list_del_init(&key->k_list);
289                 OBD_FREE(key, sizeof(*key));
290         }
291         spin_unlock(&capa_lock);
292 }