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