1/*
2 * ntfs_secure.h - Defines for security ($Secure) handling in the NTFS kernel
3 *		   driver.
4 *
5 * Copyright (c) 2006-2008 Anton Altaparmakov.  All Rights Reserved.
6 * Portions Copyright (c) 2006-2008 Apple Inc.  All Rights Reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 *    this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 *    this list of conditions and the following disclaimer in the documentation
15 *    and/or other materials provided with the distribution.
16 * 3. Neither the name of Apple Inc. ("Apple") nor the names of its
17 *    contributors may be used to endorse or promote products derived from this
18 *    software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * ALTERNATIVELY, provided that this notice and licensing terms are retained in
32 * full, this file may be redistributed and/or modified under the terms of the
33 * GNU General Public License (GPL) Version 2, in which case the provisions of
34 * that version of the GPL will apply to you instead of the license terms
35 * above.  You can obtain a copy of the GPL Version 2 at
36 * http://developer.apple.com/opensource/licenses/gpl-2.txt.
37 */
38
39#ifndef _OSX_NTFS_SECURE_H
40#define _OSX_NTFS_SECURE_H
41
42#include <sys/errno.h>
43#include <sys/ucred.h>
44#include <sys/vnode.h>
45
46#include "ntfs_types.h"
47#include "ntfs_endian.h"
48#include "ntfs_layout.h"
49#include "ntfs_volume.h"
50
51__private_extern__ SDS_ENTRY *ntfs_file_sds_entry;
52__private_extern__ SDS_ENTRY *ntfs_dir_sds_entry;
53__private_extern__ SDS_ENTRY *ntfs_file_sds_entry_old;
54__private_extern__ SDS_ENTRY *ntfs_dir_sds_entry_old;
55
56/**
57 * ntfs_rol32 - rotate a value to the left
58 * @x:		value whose bits to rotate to the left
59 * @n:		number of bits to rotate @x by
60 *
61 * Rotate the bits of @x to the left by @n bits.
62 *
63 * Return the rotated value.
64 */
65static inline u32 ntfs_rol32(const u32 x, const unsigned n)
66{
67	return (x << n) | (x >> (32 - n));
68}
69
70/**
71 * ntfs_security_hash - calculate the hash of a security descriptor
72 * @sd:		self-relative security descriptor whose hash to calculate
73 * @length:	size in bytes of the security descritor @sd
74 *
75 * Calculate the hash of the self-relative security descriptor @sd of length
76 * @length bytes.
77 *
78 * This hash is used in the $Secure system file as the primary key for the $SDH
79 * index and is also stored in the header of each security descriptor in the
80 * $SDS data stream as well as in the index data of both the $SII and $SDH
81 * indexes.  In all three cases it forms part of the SDS_ENTRY_HEADER
82 * structure.
83 *
84 * Return the calculated security hash in little endian.
85 */
86static inline le32 ntfs_security_hash(SECURITY_DESCRIPTOR_RELATIVE *sd,
87	const u32 length)
88{
89	le32 *pos, *end;
90	u32 hash;
91
92	pos = (le32*)sd;
93	end = (le32*)sd + (length / sizeof(le32));
94	for (hash = 0; pos < end; pos++)
95		hash = le32_to_cpup(pos) + ntfs_rol32(hash, 3);
96	return cpu_to_le32(hash);
97}
98
99__private_extern__ errno_t ntfs_default_sds_entries_init(void);
100
101__private_extern__ errno_t ntfs_next_security_id_init(ntfs_volume *vol,
102		le32 *next_security_id);
103
104__private_extern__ errno_t ntfs_default_security_id_init(ntfs_volume *vol,
105		struct vnode_attr *va);
106
107#endif /* _OSX_NTFS_SECURE_H */
108