1/*
2 * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef KEYCTL_H__
19#define KEYCTL_H__
20
21#include "config.h"
22
23#if defined(HAVE_KEYUTILS_H) && defined(HAVE_LIBKEYUTILS)
24# include <keyutils.h>
25#else
26# ifdef HAVE_LINUX_KEYCTL_H
27#  include <linux/keyctl.h>
28# endif /* HAVE_LINUX_KEYCTL_H */
29
30# include <stdarg.h>
31# include <stdint.h>
32# include "lapi/syscalls.h"
33typedef int32_t key_serial_t;
34
35static inline key_serial_t add_key(const char *type,
36				   const char *description,
37				   const void *payload,
38				   size_t plen,
39				   key_serial_t ringid)
40{
41	return tst_syscall(__NR_add_key,
42		type, description, payload, plen, ringid);
43}
44
45static inline key_serial_t request_key(const char *type,
46				       const char *description,
47				       const char *callout_info,
48				       key_serial_t destringid)
49{
50	return tst_syscall(__NR_request_key,
51		type, description, callout_info, destringid);
52}
53
54static inline long keyctl(int cmd, ...)
55{
56	va_list va;
57	unsigned long arg2, arg3, arg4, arg5;
58
59	va_start(va, cmd);
60	arg2 = va_arg(va, unsigned long);
61	arg3 = va_arg(va, unsigned long);
62	arg4 = va_arg(va, unsigned long);
63	arg5 = va_arg(va, unsigned long);
64	va_end(va);
65
66	return tst_syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
67}
68
69static inline key_serial_t keyctl_join_session_keyring(const char *name) {
70	return keyctl(KEYCTL_JOIN_SESSION_KEYRING, name);
71}
72
73#endif /* defined(HAVE_KEYUTILS_H) && defined(HAVE_LIBKEYUTILS) */
74
75/* special process keyring shortcut IDs */
76#ifndef KEY_SPEC_THREAD_KEYRING
77# define KEY_SPEC_THREAD_KEYRING -1
78#endif
79
80#ifndef KEY_SPEC_PROCESS_KEYRING
81# define KEY_SPEC_PROCESS_KEYRING -2
82#endif
83
84#ifndef KEY_SPEC_SESSION_KEYRING
85# define KEY_SPEC_SESSION_KEYRING -3
86#endif
87
88#ifndef KEY_SPEC_USER_KEYRING
89# define KEY_SPEC_USER_KEYRING -4
90#endif
91
92
93#ifndef KEY_SPEC_USER_SESSION_KEYRING
94# define KEY_SPEC_USER_SESSION_KEYRING -5
95#endif
96
97/* request-key default keyrings */
98#ifndef KEY_REQKEY_DEFL_THREAD_KEYRING
99# define KEY_REQKEY_DEFL_THREAD_KEYRING 1
100#endif
101
102#ifndef KEY_REQKEY_DEFL_DEFAULT
103# define KEY_REQKEY_DEFL_DEFAULT	0
104#endif
105
106/* keyctl commands */
107#ifndef KEYCTL_GET_KEYRING_ID
108# define KEYCTL_GET_KEYRING_ID 0
109#endif
110
111#ifndef KEYCTL_JOIN_SESSION_KEYRING
112# define KEYCTL_JOIN_SESSION_KEYRING 1
113#endif
114
115#ifndef KEYCTL_UPDATE
116# define KEYCTL_UPDATE 2
117#endif
118
119#ifndef KEYCTL_REVOKE
120# define KEYCTL_REVOKE 3
121#endif
122
123#ifndef KEYCTL_SETPERM
124# define KEYCTL_SETPERM 5
125#endif
126
127#ifndef KEYCTL_CLEAR
128# define KEYCTL_CLEAR 7
129#endif
130
131#ifndef KEYCTL_UNLINK
132# define KEYCTL_UNLINK 9
133#endif
134
135#ifndef KEYCTL_READ
136# define KEYCTL_READ 11
137#endif
138
139#ifndef KEYCTL_SET_REQKEY_KEYRING
140# define KEYCTL_SET_REQKEY_KEYRING 14
141#endif
142
143#ifndef KEYCTL_SET_TIMEOUT
144# define KEYCTL_SET_TIMEOUT 15
145#endif
146
147#endif	/* KEYCTL_H__ */
148