s3:modules: Add hash_inode() function based on SHA1
[samba.git] / source3 / modules / hash_inode.c
1 /*
2  * Unix SMB/Netbios implementation.
3  *
4  * Copyright (c) 2019      Andreas Schneider <asn@samba.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "hash_inode.h"
22
23 #include <gnutls/gnutls.h>
24 #include <gnutls/crypto.h>
25
26 SMB_INO_T hash_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
27 {
28         gnutls_hash_hd_t hash_hnd = NULL;
29         uint8_t digest[gnutls_hash_get_len(GNUTLS_DIG_SHA1)];
30         char *upper_sname = NULL;
31         SMB_INO_T result = 0;
32         int rc;
33
34         DBG_DEBUG("hash_inode called for %ju/%ju [%s]\n",
35                   (uintmax_t)sbuf->st_ex_dev,
36                   (uintmax_t)sbuf->st_ex_ino,
37                   sname);
38
39         upper_sname = talloc_strdup_upper(talloc_tos(), sname);
40         SMB_ASSERT(upper_sname != NULL);
41
42         rc = gnutls_hash_init(&hash_hnd, GNUTLS_DIG_SHA1);
43         if (rc < 0) {
44                 goto out;
45         }
46
47         rc = gnutls_hash(hash_hnd,
48                          &(sbuf->st_ex_dev),
49                          sizeof(sbuf->st_ex_dev));
50         if (rc < 0) {
51                 gnutls_hash_deinit(hash_hnd, NULL);
52                 goto out;
53         }
54         rc = gnutls_hash(hash_hnd,
55                          &(sbuf->st_ex_ino),
56                          sizeof(sbuf->st_ex_ino));
57         if (rc < 0) {
58                 gnutls_hash_deinit(hash_hnd, NULL);
59                 goto out;
60         }
61         rc = gnutls_hash(hash_hnd,
62                          upper_sname,
63                          talloc_get_size(upper_sname) - 1);
64         if (rc < 0) {
65                 gnutls_hash_deinit(hash_hnd, NULL);
66                 goto out;
67         }
68
69         gnutls_hash_deinit(hash_hnd, digest);
70
71         memcpy(&result, digest, sizeof(result));
72         DBG_DEBUG("fruit_inode \"%s\": ino=%ju\n",
73                   sname, (uintmax_t)result);
74
75 out:
76         TALLOC_FREE(upper_sname);
77
78         DBG_DEBUG("hash_inode '%s': ino=%ju\n",
79                   sname,
80                   (uintmax_t)result);
81
82         return result;
83 }