]> git.proxmox.com Git - mirror_frr.git/blob - lib/privs.h
Merge pull request #3743 from NaveenThanikachalam/2990_New
[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 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 /* list of zebra capabilities */
31 typedef enum {
32 ZCAP_SETID,
33 ZCAP_BIND,
34 ZCAP_NET_ADMIN,
35 ZCAP_SYS_ADMIN,
36 ZCAP_NET_RAW,
37 ZCAP_CHROOT,
38 ZCAP_NICE,
39 ZCAP_PTRACE,
40 ZCAP_DAC_OVERRIDE,
41 ZCAP_READ_SEARCH,
42 ZCAP_FOWNER,
43 ZCAP_MAX
44 } zebra_capabilities_t;
45
46 typedef enum {
47 ZPRIVS_LOWERED,
48 ZPRIVS_RAISED,
49 ZPRIVS_UNKNOWN,
50 } zebra_privs_current_t;
51
52 typedef enum {
53 ZPRIVS_RAISE,
54 ZPRIVS_LOWER,
55 } zebra_privs_ops_t;
56
57 struct zebra_privs_t {
58 zebra_capabilities_t *caps_p; /* caps required for operation */
59 zebra_capabilities_t *caps_i; /* caps to allow inheritance of */
60 int cap_num_p; /* number of caps in arrays */
61 int cap_num_i;
62 const char *user; /* user and group to run as */
63 const char *group;
64 const char *vty_group; /* group to chown vty socket to */
65 /* methods */
66 int (*change)(zebra_privs_ops_t); /* change privileges, 0 on success */
67 zebra_privs_current_t (*current_state)(
68 void); /* current privilege state */
69 const char *raised_in_funcname;
70 };
71
72 struct zprivs_ids_t {
73 /* -1 is undefined */
74 uid_t uid_priv; /* privileged uid */
75 uid_t uid_normal; /* normal uid */
76 gid_t gid_priv; /* privileged uid */
77 gid_t gid_normal; /* normal uid */
78 gid_t gid_vty; /* vty gid */
79 };
80
81 /* initialise zebra privileges */
82 extern void zprivs_preinit(struct zebra_privs_t *zprivs);
83 extern void zprivs_init(struct zebra_privs_t *zprivs);
84 /* drop all and terminate privileges */
85 extern void zprivs_terminate(struct zebra_privs_t *);
86 /* query for runtime uid's and gid's, eg vty needs this */
87 extern void zprivs_get_ids(struct zprivs_ids_t *);
88
89 /*
90 * Wrapper around zprivs, to be used as:
91 * frr_elevate_privs(&privs) {
92 * ... code ...
93 * if (error)
94 * break; -- break can be used to get out of the block
95 * ... code ...
96 * }
97 *
98 * The argument to frr_elevate_privs() can be NULL to leave privileges as-is
99 * (mostly useful for conditional privilege-raising, i.e.:)
100 * frr_elevate_privs(cond ? &privs : NULL) {}
101 *
102 * NB: The code block is always executed, regardless of whether privileges
103 * could be raised or not, or whether NULL was given or not. This is fully
104 * intentional; the user may have configured some RBAC or similar that we
105 * are not aware of, but that allows our code to proceed without privileges.
106 *
107 * The point of this wrapper is to prevent accidental bugs where privileges
108 * are elevated but then not dropped. This can happen when, for example, a
109 * "return", "goto" or "break" in the middle of the elevated-privilege code
110 * skips past the privilege dropping call.
111 *
112 * The macro below uses variable cleanup to drop privileges as soon as the
113 * code block is left in any way (and thus the _privs variable goes out of
114 * scope.) _once is just a trick to run the loop exactly once.
115 */
116 extern struct zebra_privs_t *_zprivs_raise(struct zebra_privs_t *privs,
117 const char *funcname);
118 extern void _zprivs_lower(struct zebra_privs_t **privs);
119
120 #define frr_elevate_privs(privs) \
121 for (struct zebra_privs_t *_once = NULL, \
122 *_privs __attribute__( \
123 (unused, cleanup(_zprivs_lower))) = \
124 _zprivs_raise(privs, __func__); \
125 _once == NULL; _once = (void *)1)
126
127 #ifdef __cplusplus
128 }
129 #endif
130
131 #endif /* _ZEBRA_PRIVS_H */