]>
Commit | Line | Data |
---|---|---|
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 | ||
c5c44d4b | 26 | #include <pthread.h> |
8875d051 | 27 | #include "lib/queue.h" |
c5c44d4b | 28 | |
5e244469 RW |
29 | #ifdef __cplusplus |
30 | extern "C" { | |
31 | #endif | |
32 | ||
01245821 | 33 | /* list of zebra capabilities */ |
d62a17ae | 34 | typedef enum { |
35 | ZCAP_SETID, | |
36 | ZCAP_BIND, | |
37 | ZCAP_NET_ADMIN, | |
38 | ZCAP_SYS_ADMIN, | |
39 | ZCAP_NET_RAW, | |
40 | ZCAP_CHROOT, | |
41 | ZCAP_NICE, | |
42 | ZCAP_PTRACE, | |
43 | ZCAP_DAC_OVERRIDE, | |
44 | ZCAP_READ_SEARCH, | |
45 | ZCAP_FOWNER, | |
46 | ZCAP_MAX | |
01245821 | 47 | } zebra_capabilities_t; |
48 | ||
d62a17ae | 49 | typedef enum { |
50 | ZPRIVS_LOWERED, | |
51 | ZPRIVS_RAISED, | |
52 | ZPRIVS_UNKNOWN, | |
01245821 | 53 | } zebra_privs_current_t; |
54 | ||
d62a17ae | 55 | typedef enum { |
56 | ZPRIVS_RAISE, | |
57 | ZPRIVS_LOWER, | |
01245821 | 58 | } zebra_privs_ops_t; |
59 | ||
8875d051 MS |
60 | struct zebra_privs_refs_t { |
61 | STAILQ_ENTRY(zebra_privs_refs_t) entry; | |
62 | pthread_t tid; | |
63 | uint32_t refcount; | |
64 | const char *raised_in_funcname; | |
65 | }; | |
66 | ||
d62a17ae | 67 | struct zebra_privs_t { |
68 | zebra_capabilities_t *caps_p; /* caps required for operation */ | |
69 | zebra_capabilities_t *caps_i; /* caps to allow inheritance of */ | |
70 | int cap_num_p; /* number of caps in arrays */ | |
71 | int cap_num_i; | |
c5c44d4b MS |
72 | |
73 | /* Mutex and counter used to avoid race conditions in multi-threaded | |
8875d051 MS |
74 | * processes. If privs status is process-wide, we need to |
75 | * control changes to the privilege status among threads. | |
76 | * If privs changes are per-thread, we need to be able to | |
77 | * manage that too. | |
c5c44d4b MS |
78 | */ |
79 | pthread_mutex_t mutex; | |
8875d051 MS |
80 | struct zebra_privs_refs_t process_refs; |
81 | ||
82 | STAILQ_HEAD(thread_refs_q, zebra_privs_refs_t) thread_refs; | |
c5c44d4b | 83 | |
d62a17ae | 84 | const char *user; /* user and group to run as */ |
85 | const char *group; | |
86 | const char *vty_group; /* group to chown vty socket to */ | |
87 | /* methods */ | |
88 | int (*change)(zebra_privs_ops_t); /* change privileges, 0 on success */ | |
89 | zebra_privs_current_t (*current_state)( | |
90 | void); /* current privilege state */ | |
01245821 | 91 | }; |
92 | ||
d62a17ae | 93 | struct zprivs_ids_t { |
94 | /* -1 is undefined */ | |
95 | uid_t uid_priv; /* privileged uid */ | |
96 | uid_t uid_normal; /* normal uid */ | |
97 | gid_t gid_priv; /* privileged uid */ | |
98 | gid_t gid_normal; /* normal uid */ | |
99 | gid_t gid_vty; /* vty gid */ | |
ba3a0bc5 | 100 | }; |
101 | ||
d62a17ae | 102 | /* initialise zebra privileges */ |
37a1f2fb | 103 | extern void zprivs_preinit(struct zebra_privs_t *zprivs); |
d62a17ae | 104 | extern void zprivs_init(struct zebra_privs_t *zprivs); |
105 | /* drop all and terminate privileges */ | |
106 | extern void zprivs_terminate(struct zebra_privs_t *); | |
107 | /* query for runtime uid's and gid's, eg vty needs this */ | |
8cc4198f | 108 | extern void zprivs_get_ids(struct zprivs_ids_t *); |
01245821 | 109 | |
6017c3a2 DL |
110 | /* |
111 | * Wrapper around zprivs, to be used as: | |
112 | * frr_elevate_privs(&privs) { | |
113 | * ... code ... | |
114 | * if (error) | |
115 | * break; -- break can be used to get out of the block | |
116 | * ... code ... | |
117 | * } | |
118 | * | |
119 | * The argument to frr_elevate_privs() can be NULL to leave privileges as-is | |
120 | * (mostly useful for conditional privilege-raising, i.e.:) | |
121 | * frr_elevate_privs(cond ? &privs : NULL) {} | |
122 | * | |
123 | * NB: The code block is always executed, regardless of whether privileges | |
124 | * could be raised or not, or whether NULL was given or not. This is fully | |
125 | * intentional; the user may have configured some RBAC or similar that we | |
126 | * are not aware of, but that allows our code to proceed without privileges. | |
127 | * | |
128 | * The point of this wrapper is to prevent accidental bugs where privileges | |
129 | * are elevated but then not dropped. This can happen when, for example, a | |
130 | * "return", "goto" or "break" in the middle of the elevated-privilege code | |
131 | * skips past the privilege dropping call. | |
132 | * | |
133 | * The macro below uses variable cleanup to drop privileges as soon as the | |
134 | * code block is left in any way (and thus the _privs variable goes out of | |
135 | * scope.) _once is just a trick to run the loop exactly once. | |
136 | */ | |
137 | extern struct zebra_privs_t *_zprivs_raise(struct zebra_privs_t *privs, | |
138 | const char *funcname); | |
139 | extern void _zprivs_lower(struct zebra_privs_t **privs); | |
140 | ||
141 | #define frr_elevate_privs(privs) \ | |
142 | for (struct zebra_privs_t *_once = NULL, \ | |
143 | *_privs __attribute__( \ | |
144 | (unused, cleanup(_zprivs_lower))) = \ | |
145 | _zprivs_raise(privs, __func__); \ | |
146 | _once == NULL; _once = (void *)1) | |
147 | ||
5e244469 RW |
148 | #ifdef __cplusplus |
149 | } | |
150 | #endif | |
151 | ||
01245821 | 152 | #endif /* _ZEBRA_PRIVS_H */ |