1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3 * Copyright 2016 Couchbase, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 #pragma once
18
19 #include <platform/sized_buffer.h>
20 #include <xattr/visibility.h>
21
22 /**
23 * Validate that a key is legal according to the XATTR spec
24 *
25 * The XATTR key limitations:
26 * <ul>
27 * <li>An XATTR consists of a key/value pair.</li>
28 * <li>The XATTR key (X-Key) is a Modified UTF-8 string up to 16 bytes in
29 * length.</li>
30 * <li>X-Keys starting with the following characters are reserved and
31 * cannot be used:
32 * <ul>
33 * <li>ispunct(), excluding underscore</li>
34 * <li>iscntrl()</li>
35 * </ul>
36 * <li>X-Keys starting with a leading underscore ('_', 0x5F) are considered
37 * system XATTRs and can only be accessed if the client holds the
38 * SYSTEM_XATTR read / write privilege.</li>
39 * <li>X-keys starting with a leading dollar sign ('$', 0x25) are considered
40 * virtual xattrs</li>
41 * </ul>
42 *
43 * @param path The path to check
44 * @param key_length The length of the key component (out)
45 * @return true if the key part of the provided path meets the spec
46 */
47 XATTR_PUBLIC_API
48 bool is_valid_xattr_key(cb::const_char_buffer path, size_t& key_length);
49
50 // Wrapper function if you couldn't care less about the key length
is_valid_xattr_key(cb::const_char_buffer path)51 inline bool is_valid_xattr_key(cb::const_char_buffer path) {
52 size_t len;
53 return is_valid_xattr_key(path, len);
54 }
55