agent: Keep CAP_DAC_OVERRIDE to connect to ssh-agent socket
[strongswan.git] / src / libstrongswan / utils / capabilities.h
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 * Copyright (C) 2012 Martin Willi
5 * Copyright (C) 2012 revosec AG
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 /**
19 * @defgroup capabilities capabilities
20 * @{ @ingroup utils
21 */
22
23 #ifndef CAPABILITIES_H_
24 #define CAPABILITIES_H_
25
26 typedef struct capabilities_t capabilities_t;
27
28 #include <library.h>
29 #ifdef HAVE_SYS_CAPABILITY_H
30 # include <sys/capability.h>
31 #elif defined(CAPABILITIES_NATIVE)
32 # include <linux/capability.h>
33 #endif
34
35 #ifndef CAP_CHOWN
36 # define CAP_CHOWN 0
37 #endif
38 #ifndef CAP_NET_BIND_SERVICE
39 # define CAP_NET_BIND_SERVICE 10
40 #endif
41 #ifndef CAP_NET_ADMIN
42 # define CAP_NET_ADMIN 12
43 #endif
44 #ifndef CAP_NET_RAW
45 # define CAP_NET_RAW 13
46 #endif
47 #ifndef CAP_DAC_OVERRIDE
48 # define CAP_DAC_OVERRIDE 1
49 #endif
50
51 /**
52 * POSIX capability dropping abstraction layer.
53 */
54 struct capabilities_t {
55
56 /**
57 * Register a capability to keep while calling drop(). Verifies that the
58 * capability is currently held.
59 *
60 * @note CAP_CHOWN is handled specially as it might not be required.
61 *
62 * @param cap capability to keep
63 * @return FALSE if the capability is currently not held
64 */
65 bool (*keep)(capabilities_t *this,
66 u_int cap) __attribute__((warn_unused_result));
67
68 /**
69 * Check if the given capability is currently held.
70 *
71 * @note CAP_CHOWN is handled specially as it might not be required.
72 *
73 * @param cap capability to check
74 * @return TRUE if the capability is currently held
75 */
76 bool (*check)(capabilities_t *this, u_int cap);
77
78 /**
79 * Get the user ID set through set_uid/resolve_uid.
80 *
81 * @return currently set user ID
82 */
83 uid_t (*get_uid)(capabilities_t *this);
84
85 /**
86 * Get the group ID set through set_gid/resolve_gid.
87 *
88 * @return currently set group ID
89 */
90 gid_t (*get_gid)(capabilities_t *this);
91
92 /**
93 * Set the numerical user ID to use during rights dropping.
94 *
95 * @param uid user ID to use
96 */
97 void (*set_uid)(capabilities_t *this, uid_t uid);
98
99 /**
100 * Set the numerical group ID to use during rights dropping.
101 *
102 * @param gid group ID to use
103 */
104 void (*set_gid)(capabilities_t *this, gid_t gid);
105
106 /**
107 * Resolve a username and set the user ID accordingly.
108 *
109 * @param username username get the uid for
110 * @return TRUE if username resolved and uid set
111 */
112 bool (*resolve_uid)(capabilities_t *this, char *username);
113
114 /**
115 * Resolve a groupname and set the group ID accordingly.
116 *
117 * @param groupname groupname to get the gid for
118 * @return TRUE if groupname resolved and gid set
119 */
120 bool (*resolve_gid)(capabilities_t *this, char *groupname);
121
122 /**
123 * Drop all capabilities not previously passed to keep(), switch to UID/GID.
124 *
125 * @return TRUE if capability drop successful
126 */
127 bool (*drop)(capabilities_t *this);
128
129 /**
130 * Destroy a capabilities_t.
131 */
132 void (*destroy)(capabilities_t *this);
133 };
134
135 /**
136 * Create a capabilities instance.
137 */
138 capabilities_t *capabilities_create();
139
140 #endif /** CAPABILITIES_H_ @}*/