]> git.proxmox.com Git - mirror_frr.git/blob - lib/privs.h
Merge pull request #3772 from pguibert6WIND/vrf_backend_unknown
[mirror_frr.git] / lib / privs.h
1 /*
2 * Zebra privileges header.
3 *
4 * Copyright (C) 2003 Paul Jakma.
5 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #ifndef _ZEBRA_PRIVS_H
24 #define _ZEBRA_PRIVS_H
25
26 #include <pthread.h>
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 /* list of zebra capabilities */
33 typedef enum {
34 ZCAP_SETID,
35 ZCAP_BIND,
36 ZCAP_NET_ADMIN,
37 ZCAP_SYS_ADMIN,
38 ZCAP_NET_RAW,
39 ZCAP_CHROOT,
40 ZCAP_NICE,
41 ZCAP_PTRACE,
42 ZCAP_DAC_OVERRIDE,
43 ZCAP_READ_SEARCH,
44 ZCAP_FOWNER,
45 ZCAP_MAX
46 } zebra_capabilities_t;
47
48 typedef enum {
49 ZPRIVS_LOWERED,
50 ZPRIVS_RAISED,
51 ZPRIVS_UNKNOWN,
52 } zebra_privs_current_t;
53
54 typedef enum {
55 ZPRIVS_RAISE,
56 ZPRIVS_LOWER,
57 } zebra_privs_ops_t;
58
59 struct zebra_privs_t {
60 zebra_capabilities_t *caps_p; /* caps required for operation */
61 zebra_capabilities_t *caps_i; /* caps to allow inheritance of */
62 int cap_num_p; /* number of caps in arrays */
63 int cap_num_i;
64
65 /* Mutex and counter used to avoid race conditions in multi-threaded
66 * processes. The privs elevation is process-wide, so we need to
67 * avoid changing the privilege status across threads.
68 */
69 pthread_mutex_t mutex;
70 uint32_t refcount;
71
72 const char *user; /* user and group to run as */
73 const char *group;
74 const char *vty_group; /* group to chown vty socket to */
75 /* methods */
76 int (*change)(zebra_privs_ops_t); /* change privileges, 0 on success */
77 zebra_privs_current_t (*current_state)(
78 void); /* current privilege state */
79 const char *raised_in_funcname;
80 };
81
82 struct zprivs_ids_t {
83 /* -1 is undefined */
84 uid_t uid_priv; /* privileged uid */
85 uid_t uid_normal; /* normal uid */
86 gid_t gid_priv; /* privileged uid */
87 gid_t gid_normal; /* normal uid */
88 gid_t gid_vty; /* vty gid */
89 };
90
91 /* initialise zebra privileges */
92 extern void zprivs_preinit(struct zebra_privs_t *zprivs);
93 extern void zprivs_init(struct zebra_privs_t *zprivs);
94 /* drop all and terminate privileges */
95 extern void zprivs_terminate(struct zebra_privs_t *);
96 /* query for runtime uid's and gid's, eg vty needs this */
97 extern void zprivs_get_ids(struct zprivs_ids_t *);
98
99 /*
100 * Wrapper around zprivs, to be used as:
101 * frr_elevate_privs(&privs) {
102 * ... code ...
103 * if (error)
104 * break; -- break can be used to get out of the block
105 * ... code ...
106 * }
107 *
108 * The argument to frr_elevate_privs() can be NULL to leave privileges as-is
109 * (mostly useful for conditional privilege-raising, i.e.:)
110 * frr_elevate_privs(cond ? &privs : NULL) {}
111 *
112 * NB: The code block is always executed, regardless of whether privileges
113 * could be raised or not, or whether NULL was given or not. This is fully
114 * intentional; the user may have configured some RBAC or similar that we
115 * are not aware of, but that allows our code to proceed without privileges.
116 *
117 * The point of this wrapper is to prevent accidental bugs where privileges
118 * are elevated but then not dropped. This can happen when, for example, a
119 * "return", "goto" or "break" in the middle of the elevated-privilege code
120 * skips past the privilege dropping call.
121 *
122 * The macro below uses variable cleanup to drop privileges as soon as the
123 * code block is left in any way (and thus the _privs variable goes out of
124 * scope.) _once is just a trick to run the loop exactly once.
125 */
126 extern struct zebra_privs_t *_zprivs_raise(struct zebra_privs_t *privs,
127 const char *funcname);
128 extern void _zprivs_lower(struct zebra_privs_t **privs);
129
130 #define frr_elevate_privs(privs) \
131 for (struct zebra_privs_t *_once = NULL, \
132 *_privs __attribute__( \
133 (unused, cleanup(_zprivs_lower))) = \
134 _zprivs_raise(privs, __func__); \
135 _once == NULL; _once = (void *)1)
136
137 #ifdef __cplusplus
138 }
139 #endif
140
141 #endif /* _ZEBRA_PRIVS_H */