NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * linux/fs/nfsd/nfs4recover.c |
| 3 | * |
| 4 | * Copyright (c) 2004 The Regents of the University of Michigan. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Andy Adamson <andros@citi.umich.edu> |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 3. Neither the name of the University nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 29 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 31 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 32 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | |
| 37 | #include <linux/sunrpc/svc.h> |
| 38 | #include <linux/nfsd/nfsd.h> |
| 39 | #include <linux/nfs4.h> |
| 40 | #include <linux/nfsd/state.h> |
| 41 | #include <linux/nfsd/xdr4.h> |
| 42 | #include <asm/uaccess.h> |
| 43 | #include <asm/scatterlist.h> |
| 44 | #include <linux/crypto.h> |
| 45 | |
| 46 | |
| 47 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 48 | |
| 49 | static void |
| 50 | md5_to_hex(char *out, char *md5) |
| 51 | { |
| 52 | int i; |
| 53 | |
| 54 | for (i=0; i<16; i++) { |
| 55 | unsigned char c = md5[i]; |
| 56 | |
| 57 | *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1); |
| 58 | *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1); |
| 59 | } |
| 60 | *out = '\0'; |
| 61 | } |
| 62 | |
| 63 | int |
| 64 | nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname) |
| 65 | { |
| 66 | struct xdr_netobj cksum; |
| 67 | struct crypto_tfm *tfm; |
| 68 | struct scatterlist sg[1]; |
| 69 | int status = nfserr_resource; |
| 70 | |
| 71 | dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n", |
| 72 | clname->len, clname->data); |
| 73 | tfm = crypto_alloc_tfm("md5", 0); |
| 74 | if (tfm == NULL) |
| 75 | goto out; |
| 76 | cksum.len = crypto_tfm_alg_digestsize(tfm); |
| 77 | cksum.data = kmalloc(cksum.len, GFP_KERNEL); |
| 78 | if (cksum.data == NULL) |
| 79 | goto out; |
| 80 | crypto_digest_init(tfm); |
| 81 | |
| 82 | sg[0].page = virt_to_page(clname->data); |
| 83 | sg[0].offset = offset_in_page(clname->data); |
| 84 | sg[0].length = clname->len; |
| 85 | |
| 86 | crypto_digest_update(tfm, sg, 1); |
| 87 | crypto_digest_final(tfm, cksum.data); |
| 88 | |
| 89 | md5_to_hex(dname, cksum.data); |
| 90 | |
| 91 | kfree(cksum.data); |
| 92 | status = nfs_ok; |
| 93 | out: |
| 94 | if (tfm) |
| 95 | crypto_free_tfm(tfm); |
| 96 | return status; |
| 97 | } |