blob: 3355f24a73499ed2ad2dc54ce1806fdfbd839be7 [file] [log] [blame]
paul01245822003-05-20 01:22:17 +00001/*
2 * Zebra privileges.
3 *
4 * Copyright (C) 2003 Paul Jakma.
Brian Bennett5cd0e5c2015-02-17 23:24:15 +00005 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
paul01245822003-05-20 01:22:17 +00006 *
7 * This file is part of GNU Zebra.
8 *
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with GNU Zebra; see the file COPYING. If not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 * 02111-1307, USA.
23 */
paul01245822003-05-20 01:22:17 +000024#include <zebra.h>
25#include "log.h"
26#include "privs.h"
27#include "memory.h"
paul01245822003-05-20 01:22:17 +000028
paulceacedb2005-09-29 14:39:32 +000029#ifdef HAVE_CAPABILITIES
30/* sort out some generic internal types for:
31 *
32 * privilege values (cap_value_t, priv_t) -> pvalue_t
33 * privilege set (..., priv_set_t) -> pset_t
34 * privilege working storage (cap_t, ...) -> pstorage_t
35 *
36 * values we think of as numeric (they're ints really, but we dont know)
37 * sets are mostly opaque, to hold a set of privileges, related in some way.
38 * storage binds together a set of sets we're interested in.
39 * (in reality: cap_value_t and priv_t are ints)
40 */
41#ifdef HAVE_LCAPS
42/* Linux doesn't have a 'set' type: a set of related privileges */
43struct _pset {
44 int num;
45 cap_value_t *caps;
46};
47typedef cap_value_t pvalue_t;
48typedef struct _pset pset_t;
49typedef cap_t pstorage_t;
David Lamparter6b0655a2014-06-04 06:53:35 +020050
paulceacedb2005-09-29 14:39:32 +000051#elif defined (HAVE_SOLARIS_CAPABILITIES)
52typedef priv_t pvalue_t;
53typedef priv_set_t pset_t;
54typedef priv_set_t *pstorage_t;
55#else /* neither LCAPS nor SOLARIS_CAPABILITIES */
56#error "HAVE_CAPABILITIES defined, but neither LCAPS nor Solaris Capabilties!"
57#endif /* HAVE_LCAPS */
58#endif /* HAVE_CAPABILITIES */
David Lamparter6b0655a2014-06-04 06:53:35 +020059
paulceacedb2005-09-29 14:39:32 +000060/* the default NULL state we report is RAISED, but could be LOWERED if
61 * zprivs_terminate is called and the NULL handler is installed.
62 */
63static zebra_privs_current_t zprivs_null_state = ZPRIVS_RAISED;
64
paul01245822003-05-20 01:22:17 +000065/* internal privileges state */
66static struct _zprivs_t
67{
paulceacedb2005-09-29 14:39:32 +000068#ifdef HAVE_CAPABILITIES
69 pstorage_t caps; /* working storage */
70 pset_t *syscaps_p; /* system-type requested permitted caps */
71 pset_t *syscaps_i; /* system-type requested inheritable caps */
72#endif /* HAVE_CAPABILITIES */
paul01245822003-05-20 01:22:17 +000073 uid_t zuid, /* uid to run as */
74 zsuid; /* saved uid */
75 gid_t zgid; /* gid to run as */
hassoba3a0bc2003-06-04 17:41:54 +000076 gid_t vtygrp; /* gid for vty sockets */
paul01245822003-05-20 01:22:17 +000077} zprivs_state;
78
79/* externally exported but not directly accessed functions */
paulceacedb2005-09-29 14:39:32 +000080#ifdef HAVE_CAPABILITIES
paul01245822003-05-20 01:22:17 +000081int zprivs_change_caps (zebra_privs_ops_t);
82zebra_privs_current_t zprivs_state_caps (void);
paulceacedb2005-09-29 14:39:32 +000083#endif /* HAVE_CAPABILITIES */
paul01245822003-05-20 01:22:17 +000084int zprivs_change_uid (zebra_privs_ops_t);
85zebra_privs_current_t zprivs_state_uid (void);
86int zprivs_change_null (zebra_privs_ops_t);
87zebra_privs_current_t zprivs_state_null (void);
paul01245822003-05-20 01:22:17 +000088
paulceacedb2005-09-29 14:39:32 +000089#ifdef HAVE_CAPABILITIES
90/* internal capability API */
91static pset_t *zcaps2sys (zebra_capabilities_t *, int);
92static void zprivs_caps_init (struct zebra_privs_t *);
93static void zprivs_caps_terminate (void);
94
95/* Map of Quagga abstract capabilities to system capabilities */
96static struct
paul01245822003-05-20 01:22:17 +000097{
paulceacedb2005-09-29 14:39:32 +000098 int num;
99 pvalue_t *system_caps;
100} cap_map [ZCAP_MAX] =
101{
102#ifdef HAVE_LCAPS /* Quagga -> Linux capabilities mappings */
103 [ZCAP_SETID] = { 2, (pvalue_t []) { CAP_SETGID,
104 CAP_SETUID }, },
105 [ZCAP_BIND] = { 2, (pvalue_t []) { CAP_NET_BIND_SERVICE,
106 CAP_NET_BROADCAST }, },
107 [ZCAP_NET_ADMIN] = { 1, (pvalue_t []) { CAP_NET_ADMIN }, },
108 [ZCAP_NET_RAW] = { 1, (pvalue_t []) { CAP_NET_RAW }, },
109 [ZCAP_CHROOT] = { 1, (pvalue_t []) { CAP_SYS_CHROOT, }, },
110 [ZCAP_NICE] = { 1, (pvalue_t []) { CAP_SYS_NICE }, },
111 [ZCAP_PTRACE] = { 1, (pvalue_t []) { CAP_SYS_PTRACE }, },
112 [ZCAP_DAC_OVERRIDE] = { 1, (pvalue_t []) { CAP_DAC_OVERRIDE }, },
113 [ZCAP_READ_SEARCH] = { 1, (pvalue_t []) { CAP_DAC_READ_SEARCH }, },
114 [ZCAP_SYS_ADMIN] = { 1, (pvalue_t []) { CAP_SYS_ADMIN }, },
115 [ZCAP_FOWNER] = { 1, (pvalue_t []) { CAP_FOWNER }, },
116#elif defined(HAVE_SOLARIS_CAPABILITIES) /* HAVE_LCAPS */
117 /* Quagga -> Solaris privilege mappings */
118 [ZCAP_SETID] = { 1, (pvalue_t []) { PRIV_PROC_SETID }, },
119 [ZCAP_BIND] = { 1, (pvalue_t []) { PRIV_NET_PRIVADDR }, },
Paul Jakma6b148fa2007-09-18 18:07:18 +0000120 /* IP_CONFIG is a subset of NET_CONFIG and is allowed in zones */
121#ifdef PRIV_SYS_IP_CONFIG
122 [ZCAP_NET_ADMIN] = { 1, (pvalue_t []) { PRIV_SYS_IP_CONFIG }, },
123#else
paulceacedb2005-09-29 14:39:32 +0000124 [ZCAP_NET_ADMIN] = { 1, (pvalue_t []) { PRIV_SYS_NET_CONFIG }, },
Paul Jakma6b148fa2007-09-18 18:07:18 +0000125#endif
paulceacedb2005-09-29 14:39:32 +0000126 [ZCAP_NET_RAW] = { 2, (pvalue_t []) { PRIV_NET_RAWACCESS,
127 PRIV_NET_ICMPACCESS }, },
128 [ZCAP_CHROOT] = { 1, (pvalue_t []) { PRIV_PROC_CHROOT }, },
129 [ZCAP_NICE] = { 1, (pvalue_t []) { PRIV_PROC_PRIOCNTL }, },
130 [ZCAP_PTRACE] = { 1, (pvalue_t []) { PRIV_PROC_SESSION }, },
131 [ZCAP_DAC_OVERRIDE] = { 2, (pvalue_t []) { PRIV_FILE_DAC_EXECUTE,
132 PRIV_FILE_DAC_READ,
133 PRIV_FILE_DAC_SEARCH,
134 PRIV_FILE_DAC_WRITE,
135 PRIV_FILE_DAC_SEARCH }, },
136 [ZCAP_READ_SEARCH] = { 2, (pvalue_t []) { PRIV_FILE_DAC_SEARCH,
137 PRIV_FILE_DAC_READ }, },
138 [ZCAP_SYS_ADMIN] = { 1, (pvalue_t []) { PRIV_SYS_ADMIN }, },
139 [ZCAP_FOWNER] = { 1, (pvalue_t []) { PRIV_FILE_OWNER }, },
140#endif /* HAVE_SOLARIS_CAPABILITIES */
paul01245822003-05-20 01:22:17 +0000141};
David Lamparter6b0655a2014-06-04 06:53:35 +0200142
paulceacedb2005-09-29 14:39:32 +0000143#ifdef HAVE_LCAPS
144/* Linux forms of capabilities methods */
paul01245822003-05-20 01:22:17 +0000145/* convert zebras privileges to system capabilities */
paulceacedb2005-09-29 14:39:32 +0000146static pset_t *
paul01245822003-05-20 01:22:17 +0000147zcaps2sys (zebra_capabilities_t *zcaps, int num)
148{
paulceacedb2005-09-29 14:39:32 +0000149 pset_t *syscaps;
150 int i, j = 0, count = 0;
paul01245822003-05-20 01:22:17 +0000151
152 if (!num)
153 return NULL;
paulceacedb2005-09-29 14:39:32 +0000154
155 /* first count up how many system caps we have */
156 for (i= 0; i < num; i++)
157 count += cap_map[zcaps[i]].num;
158
159 if ( (syscaps = XCALLOC (MTYPE_PRIVS, (sizeof(pset_t) * num))) == NULL)
paul01245822003-05-20 01:22:17 +0000160 {
paulceacedb2005-09-29 14:39:32 +0000161 fprintf (stderr, "%s: could not allocate syscaps!", __func__);
paul01245822003-05-20 01:22:17 +0000162 return NULL;
163 }
164
paulceacedb2005-09-29 14:39:32 +0000165 syscaps->caps = XCALLOC (MTYPE_PRIVS, (sizeof (pvalue_t) * count));
166
167 if (!syscaps->caps)
paul01245822003-05-20 01:22:17 +0000168 {
paulceacedb2005-09-29 14:39:32 +0000169 fprintf (stderr, "%s: could not XCALLOC caps!", __func__);
170 return NULL;
paul01245822003-05-20 01:22:17 +0000171 }
paulceacedb2005-09-29 14:39:32 +0000172
173 /* copy the capabilities over */
174 count = 0;
175 for (i=0; i < num; i++)
176 for (j = 0; j < cap_map[zcaps[i]].num; j++)
177 syscaps->caps[count++] = cap_map[zcaps[i]].system_caps[j];
178
179 /* iterations above should be exact same as previous count, obviously.. */
180 syscaps->num = count;
181
paul01245822003-05-20 01:22:17 +0000182 return syscaps;
183}
184
185/* set or clear the effective capabilities to/from permitted */
186int
187zprivs_change_caps (zebra_privs_ops_t op)
188{
189 cap_flag_value_t cflag;
190
paulceacedb2005-09-29 14:39:32 +0000191 /* should be no possibility of being called without valid caps */
192 assert (zprivs_state.syscaps_p && zprivs_state.caps);
193 if (! (zprivs_state.syscaps_p && zprivs_state.caps))
194 exit (1);
195
paul01245822003-05-20 01:22:17 +0000196 if (op == ZPRIVS_RAISE)
197 cflag = CAP_SET;
198 else if (op == ZPRIVS_LOWER)
199 cflag = CAP_CLEAR;
200 else
201 return -1;
202
203 if ( !cap_set_flag (zprivs_state.caps, CAP_EFFECTIVE,
paulceacedb2005-09-29 14:39:32 +0000204 zprivs_state.syscaps_p->num,
205 zprivs_state.syscaps_p->caps,
206 cflag))
paul01245822003-05-20 01:22:17 +0000207 return cap_set_proc (zprivs_state.caps);
208 return -1;
209}
210
211zebra_privs_current_t
212zprivs_state_caps (void)
213{
214 int i;
paul01245822003-05-20 01:22:17 +0000215 cap_flag_value_t val;
paulceacedb2005-09-29 14:39:32 +0000216
217 /* should be no possibility of being called without valid caps */
218 assert (zprivs_state.syscaps_p && zprivs_state.caps);
219 if (! (zprivs_state.syscaps_p && zprivs_state.caps))
220 exit (1);
paul01245822003-05-20 01:22:17 +0000221
paulceacedb2005-09-29 14:39:32 +0000222 for (i=0; i < zprivs_state.syscaps_p->num; i++)
paul01245822003-05-20 01:22:17 +0000223 {
paulceacedb2005-09-29 14:39:32 +0000224 if ( cap_get_flag (zprivs_state.caps, zprivs_state.syscaps_p->caps[i],
paul01245822003-05-20 01:22:17 +0000225 CAP_EFFECTIVE, &val) )
paulceacedb2005-09-29 14:39:32 +0000226 {
227 zlog_warn ("zprivs_state_caps: could not cap_get_flag, %s",
228 safe_strerror (errno) );
229 return ZPRIVS_UNKNOWN;
230 }
paul01245822003-05-20 01:22:17 +0000231 if (val == CAP_SET)
paul33b72942003-05-20 02:22:42 +0000232 return ZPRIVS_RAISED;
paul01245822003-05-20 01:22:17 +0000233 }
234 return ZPRIVS_LOWERED;
235}
236
paulceacedb2005-09-29 14:39:32 +0000237static void
238zprivs_caps_init (struct zebra_privs_t *zprivs)
239{
240 zprivs_state.syscaps_p = zcaps2sys (zprivs->caps_p, zprivs->cap_num_p);
241 zprivs_state.syscaps_i = zcaps2sys (zprivs->caps_i, zprivs->cap_num_i);
paul01245822003-05-20 01:22:17 +0000242
paulceacedb2005-09-29 14:39:32 +0000243 /* Tell kernel we want caps maintained across uid changes */
244 if ( prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1 )
245 {
246 fprintf (stderr, "privs_init: could not set PR_SET_KEEPCAPS, %s\n",
247 safe_strerror (errno) );
248 exit(1);
249 }
250
251 if ( !zprivs_state.syscaps_p )
252 {
253 fprintf (stderr, "privs_init: capabilities enabled, "
254 "but no capabilities supplied\n");
255 }
256
paulceacedb2005-09-29 14:39:32 +0000257 /* we have caps, we have no need to ever change back the original user */
258 if (zprivs_state.zuid)
259 {
260 if ( setreuid (zprivs_state.zuid, zprivs_state.zuid) )
261 {
262 fprintf (stderr, "zprivs_init (cap): could not setreuid, %s\n",
263 safe_strerror (errno));
264 exit (1);
265 }
266 }
267
Paul Jakma924f9d32006-03-30 13:25:52 +0000268 if ( !(zprivs_state.caps = cap_init()) )
269 {
270 fprintf (stderr, "privs_init: failed to cap_init, %s\n",
271 safe_strerror (errno));
272 exit (1);
273 }
274
paulceacedb2005-09-29 14:39:32 +0000275 if ( cap_clear (zprivs_state.caps) )
276 {
277 fprintf (stderr, "privs_init: failed to cap_clear, %s\n",
278 safe_strerror (errno));
279 exit (1);
280 }
281
282 /* set permitted caps */
283 cap_set_flag(zprivs_state.caps, CAP_PERMITTED,
284 zprivs_state.syscaps_p->num,
285 zprivs_state.syscaps_p->caps,
286 CAP_SET);
287
288 /* set inheritable caps, if any */
289 if (zprivs_state.syscaps_i && zprivs_state.syscaps_i->num)
290 {
291 cap_set_flag(zprivs_state.caps, CAP_INHERITABLE,
292 zprivs_state.syscaps_i->num,
293 zprivs_state.syscaps_i->caps,
294 CAP_SET);
295 }
296
297 /* apply caps. CAP_EFFECTIVE is cleared. we'll raise the caps as
298 * and when, and only when, they are needed.
299 */
300 if ( cap_set_proc (zprivs_state.caps) )
301 {
Christian Franke30657772015-05-13 13:59:17 +0200302 cap_t current_caps;
303 char *current_caps_text = NULL;
304 char *wanted_caps_text = NULL;
305
306 fprintf(stderr, "privs_init: initial cap_set_proc failed: %s\n",
307 safe_strerror(errno));
308
309 current_caps = cap_get_proc();
310 if (current_caps)
311 current_caps_text = cap_to_text(current_caps, NULL);
312
313 wanted_caps_text = cap_to_text(zprivs_state.caps, NULL);
314 fprintf(stderr, "Wanted caps: %s\n", wanted_caps_text ? wanted_caps_text : "???");
315 fprintf(stderr, "Have caps: %s\n", current_caps_text ? current_caps_text : "???");
316
paulceacedb2005-09-29 14:39:32 +0000317 exit (1);
318 }
319
320 /* set methods for the caller to use */
321 zprivs->change = zprivs_change_caps;
322 zprivs->current_state = zprivs_state_caps;
323}
324
325static void
326zprivs_caps_terminate (void)
327{
328 /* clear all capabilities */
329 if (zprivs_state.caps)
330 cap_clear (zprivs_state.caps);
331
332 /* and boom, capabilities are gone forever */
333 if ( cap_set_proc (zprivs_state.caps) )
334 {
335 fprintf (stderr, "privs_terminate: cap_set_proc failed, %s",
336 safe_strerror (errno) );
337 exit (1);
338 }
339
340 /* free up private state */
341 if (zprivs_state.syscaps_p->num)
342 {
343 XFREE (MTYPE_PRIVS, zprivs_state.syscaps_p->caps);
344 XFREE (MTYPE_PRIVS, zprivs_state.syscaps_p);
345 }
346
347 if (zprivs_state.syscaps_i && zprivs_state.syscaps_i->num)
348 {
349 XFREE (MTYPE_PRIVS, zprivs_state.syscaps_i->caps);
350 XFREE (MTYPE_PRIVS, zprivs_state.syscaps_i);
351 }
352
353 cap_free (zprivs_state.caps);
354}
355#elif defined (HAVE_SOLARIS_CAPABILITIES) /* !HAVE_LCAPS */
David Lamparter6b0655a2014-06-04 06:53:35 +0200356
paulceacedb2005-09-29 14:39:32 +0000357/* Solaris specific capability/privilege methods
358 *
359 * Resources:
360 * - the 'privileges' man page
361 * - http://cvs.opensolaris.org
362 * - http://blogs.sun.com/roller/page/gbrunett?entry=privilege_enabling_set_id_programs1
363 */
364
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000365static pset_t *
366zprivs_caps_minimal ()
367{
368 pset_t *minimal;
369
370 if ((minimal = priv_str_to_set("basic", ",", NULL)) == NULL)
371 {
372 fprintf (stderr, "%s: couldn't get basic set!\n", __func__);
373 exit (1);
374 }
375
376 /* create a minimal privilege set from the basic set */
377 (void) priv_delset(minimal, PRIV_PROC_EXEC);
378 (void) priv_delset(minimal, PRIV_PROC_INFO);
379 (void) priv_delset(minimal, PRIV_PROC_SESSION);
380 (void) priv_delset(minimal, PRIV_FILE_LINK_ANY);
381
382 return minimal;
383}
384
paulceacedb2005-09-29 14:39:32 +0000385/* convert zebras privileges to system capabilities */
386static pset_t *
387zcaps2sys (zebra_capabilities_t *zcaps, int num)
388{
389 pset_t *syscaps;
paul6e0f1b92005-11-24 12:47:17 +0000390 int i, j = 0;
paulceacedb2005-09-29 14:39:32 +0000391
392 if ((syscaps = priv_allocset()) == NULL)
393 {
394 fprintf (stderr, "%s: could not allocate syscaps!\n", __func__);
395 exit (1);
396 }
397
398 priv_emptyset (syscaps);
399
400 for (i=0; i < num; i++)
401 for (j = 0; j < cap_map[zcaps[i]].num; j++)
402 priv_addset (syscaps, cap_map[zcaps[i]].system_caps[j]);
403
404 return syscaps;
405}
406
407/* callback exported to users to RAISE and LOWER effective privileges
408 * from nothing to the given permitted set and back down
409 */
410int
411zprivs_change_caps (zebra_privs_ops_t op)
412{
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000413 pset_t *privset;
paulceacedb2005-09-29 14:39:32 +0000414
415 /* should be no possibility of being called without valid caps */
416 assert (zprivs_state.syscaps_p);
417 if (!zprivs_state.syscaps_p)
418 {
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000419 fprintf (stderr, "%s: Eek, missing privileged caps!", __func__);
420 exit (1);
421 }
422
423 assert (zprivs_state.caps);
424 if (!zprivs_state.caps)
425 {
paulceacedb2005-09-29 14:39:32 +0000426 fprintf (stderr, "%s: Eek, missing caps!", __func__);
427 exit (1);
428 }
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000429
430 /* to raise: copy original permitted as our working effective set
431 * to lower: copy regular effective set stored in zprivs_state.caps
paulceacedb2005-09-29 14:39:32 +0000432 */
433 if (op == ZPRIVS_RAISE)
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000434 privset = zprivs_state.syscaps_p;
paulceacedb2005-09-29 14:39:32 +0000435 else if (op == ZPRIVS_LOWER)
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000436 privset = zprivs_state.caps;
paulceacedb2005-09-29 14:39:32 +0000437 else
438 return -1;
439
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000440 if (setppriv (PRIV_SET, PRIV_EFFECTIVE, privset) != 0)
paulceacedb2005-09-29 14:39:32 +0000441 return -1;
442
443 return 0;
444}
445
446/* Retrieve current privilege state, is it RAISED or LOWERED? */
447zebra_privs_current_t
448zprivs_state_caps (void)
449{
450 zebra_privs_current_t result;
451 pset_t *effective;
452
453 if ( (effective = priv_allocset()) == NULL)
454 {
paul6e0f1b92005-11-24 12:47:17 +0000455 fprintf (stderr, "%s: failed to get priv_allocset! %s\n", __func__,
paulceacedb2005-09-29 14:39:32 +0000456 safe_strerror (errno));
457 return ZPRIVS_UNKNOWN;
458 }
459
460 if (getppriv (PRIV_EFFECTIVE, effective))
461 {
paul6e0f1b92005-11-24 12:47:17 +0000462 fprintf (stderr, "%s: failed to get state! %s\n", __func__,
paulceacedb2005-09-29 14:39:32 +0000463 safe_strerror (errno));
464 result = ZPRIVS_UNKNOWN;
465 }
466 else
467 {
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000468 if (priv_isequalset (effective, zprivs_state.syscaps_p))
469 result = ZPRIVS_RAISED;
470 else if (priv_isequalset (effective, zprivs_state.caps))
paulceacedb2005-09-29 14:39:32 +0000471 result = ZPRIVS_LOWERED;
472 else
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000473 result = ZPRIVS_UNKNOWN;
paulceacedb2005-09-29 14:39:32 +0000474 }
475
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000476 priv_freeset (effective);
paulceacedb2005-09-29 14:39:32 +0000477 return result;
478}
479
480static void
481zprivs_caps_init (struct zebra_privs_t *zprivs)
482{
483 pset_t *basic;
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000484 pset_t *minimal;
paulceacedb2005-09-29 14:39:32 +0000485
486 /* the specified sets */
487 zprivs_state.syscaps_p = zcaps2sys (zprivs->caps_p, zprivs->cap_num_p);
488 zprivs_state.syscaps_i = zcaps2sys (zprivs->caps_i, zprivs->cap_num_i);
489
490 /* nonsensical to have gotten here but not have capabilities */
491 if (!zprivs_state.syscaps_p)
492 {
493 fprintf (stderr, "%s: capabilities enabled, "
494 "but no valid capabilities supplied\n",
495 __func__);
496 }
497
498 /* We retain the basic set in our permitted set, as Linux has no
499 * equivalent. The basic set on Linux hence is implicit, always
500 * there.
501 */
502 if ((basic = priv_str_to_set("basic", ",", NULL)) == NULL)
503 {
504 fprintf (stderr, "%s: couldn't get basic set!\n", __func__);
505 exit (1);
506 }
507
508 /* Add the basic set to the permitted set */
509 priv_union (basic, zprivs_state.syscaps_p);
510 priv_freeset (basic);
511
paulceacedb2005-09-29 14:39:32 +0000512 /* Hey kernel, we know about privileges!
513 * this isn't strictly required, use of setppriv should have same effect
514 */
515 if (setpflags (PRIV_AWARE, 1))
516 {
517 fprintf (stderr, "%s: error setting PRIV_AWARE!, %s\n", __func__,
518 safe_strerror (errno) );
519 exit (1);
520 }
521
522 /* need either valid or empty sets for both p and i.. */
523 assert (zprivs_state.syscaps_i && zprivs_state.syscaps_p);
524
Paul Jakma924f9d32006-03-30 13:25:52 +0000525 /* we have caps, we have no need to ever change back the original user
526 * change real, effective and saved to the specified user.
527 */
528 if (zprivs_state.zuid)
529 {
530 if ( setreuid (zprivs_state.zuid, zprivs_state.zuid) )
531 {
532 fprintf (stderr, "%s: could not setreuid, %s\n",
533 __func__, safe_strerror (errno));
534 exit (1);
535 }
536 }
537
paulceacedb2005-09-29 14:39:32 +0000538 /* set the permitted set */
539 if (setppriv (PRIV_SET, PRIV_PERMITTED, zprivs_state.syscaps_p))
540 {
541 fprintf (stderr, "%s: error setting permitted set!, %s\n", __func__,
542 safe_strerror (errno) );
543 exit (1);
544 }
545
546 /* set the inheritable set */
547 if (setppriv (PRIV_SET, PRIV_INHERITABLE, zprivs_state.syscaps_i))
548 {
549 fprintf (stderr, "%s: error setting inheritable set!, %s\n", __func__,
550 safe_strerror (errno) );
551 exit (1);
552 }
553
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000554 /* we need a minimal basic set for 'effective', potentially for inheritable too */
555 minimal = zprivs_caps_minimal();
556
557 /* now set the effective set with a subset of basic privileges */
558 if (setppriv (PRIV_SET, PRIV_EFFECTIVE, minimal))
paulceacedb2005-09-29 14:39:32 +0000559 {
560 fprintf (stderr, "%s: error setting effective set!, %s\n", __func__,
561 safe_strerror (errno) );
562 exit (1);
563 }
564
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000565 /* we'll use the minimal set as our working-storage privset */
566 zprivs_state.caps = minimal;
paulceacedb2005-09-29 14:39:32 +0000567
568 /* set methods for the caller to use */
569 zprivs->change = zprivs_change_caps;
570 zprivs->current_state = zprivs_state_caps;
571}
572
573static void
574zprivs_caps_terminate (void)
575{
576 assert (zprivs_state.caps);
577
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000578 /* clear all capabilities by using working-storage privset */
paulceacedb2005-09-29 14:39:32 +0000579 setppriv (PRIV_SET, PRIV_EFFECTIVE, zprivs_state.caps);
580 setppriv (PRIV_SET, PRIV_PERMITTED, zprivs_state.caps);
581 setppriv (PRIV_SET, PRIV_INHERITABLE, zprivs_state.caps);
582
583 /* free up private state */
584 if (zprivs_state.syscaps_p)
585 priv_freeset (zprivs_state.syscaps_p);
586 if (zprivs_state.syscaps_i)
587 priv_freeset (zprivs_state.syscaps_i);
588
589 priv_freeset (zprivs_state.caps);
590}
591#else /* !HAVE_LCAPS && ! HAVE_SOLARIS_CAPABILITIES */
592#error "Neither Solaris nor Linux capabilities, dazed and confused..."
593#endif /* HAVE_LCAPS */
594#endif /* HAVE_CAPABILITIES */
David Lamparter6b0655a2014-06-04 06:53:35 +0200595
paul01245822003-05-20 01:22:17 +0000596int
597zprivs_change_uid (zebra_privs_ops_t op)
598{
paul28efaa32003-05-20 03:49:43 +0000599
paul01245822003-05-20 01:22:17 +0000600 if (op == ZPRIVS_RAISE)
601 return seteuid (zprivs_state.zsuid);
602 else if (op == ZPRIVS_LOWER)
603 return seteuid (zprivs_state.zuid);
604 else
605 return -1;
606}
607
608zebra_privs_current_t
609zprivs_state_uid (void)
610{
611 return ( (zprivs_state.zuid == geteuid()) ? ZPRIVS_LOWERED : ZPRIVS_RAISED);
612}
613
614int
615zprivs_change_null (zebra_privs_ops_t op)
616{
617 return 0;
618}
619
620zebra_privs_current_t
621zprivs_state_null (void)
622{
paulceacedb2005-09-29 14:39:32 +0000623 return zprivs_null_state;
paul01245822003-05-20 01:22:17 +0000624}
625
paul01245822003-05-20 01:22:17 +0000626void
627zprivs_init(struct zebra_privs_t *zprivs)
628{
629 struct passwd *pwentry = NULL;
630 struct group *grentry = NULL;
631
hassoba3a0bc2003-06-04 17:41:54 +0000632 if (!zprivs)
633 {
paul58a9d812003-06-11 05:12:40 +0000634 fprintf (stderr, "zprivs_init: called with NULL arg!\n");
hassoba3a0bc2003-06-04 17:41:54 +0000635 exit (1);
636 }
637
paul01245822003-05-20 01:22:17 +0000638 /* NULL privs */
639 if (! (zprivs->user || zprivs->group
640 || zprivs->cap_num_p || zprivs->cap_num_i) )
641 {
642 zprivs->change = zprivs_change_null;
643 zprivs->current_state = zprivs_state_null;
644 return;
645 }
646
647 if (zprivs->user)
648 {
649 if ( (pwentry = getpwnam (zprivs->user)) )
hassoba3a0bc2003-06-04 17:41:54 +0000650 {
651 zprivs_state.zuid = pwentry->pw_uid;
652 }
653 else
654 {
paul58a9d812003-06-11 05:12:40 +0000655 /* cant use log.h here as it depends on vty */
656 fprintf (stderr, "privs_init: could not lookup user %s\n",
657 zprivs->user);
hassoba3a0bc2003-06-04 17:41:54 +0000658 exit (1);
659 }
660 }
661
662 grentry = NULL;
663
664 if (zprivs->vty_group)
665 /* Add the vty_group to the supplementary groups so it can be chowned to */
666 {
667 if ( (grentry = getgrnam (zprivs->vty_group)) )
668 {
669 zprivs_state.vtygrp = grentry->gr_gid;
670 if ( setgroups (1, &zprivs_state.vtygrp) )
671 {
paul58a9d812003-06-11 05:12:40 +0000672 fprintf (stderr, "privs_init: could not setgroups, %s\n",
ajs6099b3b2004-11-20 02:06:59 +0000673 safe_strerror (errno) );
hassoba3a0bc2003-06-04 17:41:54 +0000674 exit (1);
675 }
676 }
paul01245822003-05-20 01:22:17 +0000677 else
678 {
paul58a9d812003-06-11 05:12:40 +0000679 fprintf (stderr, "privs_init: could not lookup vty group %s\n",
680 zprivs->vty_group);
paul01245822003-05-20 01:22:17 +0000681 exit (1);
682 }
683 }
684
685 if (zprivs->group)
686 {
hassoba3a0bc2003-06-04 17:41:54 +0000687 if ( (grentry = getgrnam (zprivs->group)) )
688 {
689 zprivs_state.zgid = grentry->gr_gid;
690 }
paul01245822003-05-20 01:22:17 +0000691 else
692 {
paul58a9d812003-06-11 05:12:40 +0000693 fprintf (stderr, "privs_init: could not lookup group %s\n",
694 zprivs->group);
paul01245822003-05-20 01:22:17 +0000695 exit (1);
696 }
paul01245822003-05-20 01:22:17 +0000697 /* change group now, forever. uid we do later */
698 if ( setregid (zprivs_state.zgid, zprivs_state.zgid) )
699 {
paul58a9d812003-06-11 05:12:40 +0000700 fprintf (stderr, "zprivs_init: could not setregid, %s\n",
ajs6099b3b2004-11-20 02:06:59 +0000701 safe_strerror (errno) );
paul01245822003-05-20 01:22:17 +0000702 exit (1);
703 }
704 }
705
paulceacedb2005-09-29 14:39:32 +0000706#ifdef HAVE_CAPABILITIES
707 zprivs_caps_init (zprivs);
708#else /* !HAVE_CAPABILITIES */
paul01245822003-05-20 01:22:17 +0000709 /* we dont have caps. we'll need to maintain rid and saved uid
710 * and change euid back to saved uid (who we presume has all neccessary
711 * privileges) whenever we are asked to raise our privileges.
paulceacedb2005-09-29 14:39:32 +0000712 *
713 * This is not worth that much security wise, but all we can do.
paul01245822003-05-20 01:22:17 +0000714 */
715 zprivs_state.zsuid = geteuid();
716 if ( zprivs_state.zuid )
717 {
718 if ( setreuid (-1, zprivs_state.zuid) )
719 {
paul58a9d812003-06-11 05:12:40 +0000720 fprintf (stderr, "privs_init (uid): could not setreuid, %s\n",
ajs6099b3b2004-11-20 02:06:59 +0000721 safe_strerror (errno));
paul01245822003-05-20 01:22:17 +0000722 exit (1);
723 }
724 }
725
726 zprivs->change = zprivs_change_uid;
727 zprivs->current_state = zprivs_state_uid;
paulceacedb2005-09-29 14:39:32 +0000728#endif /* HAVE_CAPABILITIES */
paul01245822003-05-20 01:22:17 +0000729}
730
731void
paulceacedb2005-09-29 14:39:32 +0000732zprivs_terminate (struct zebra_privs_t *zprivs)
paul01245822003-05-20 01:22:17 +0000733{
paulceacedb2005-09-29 14:39:32 +0000734 if (!zprivs)
paul01245822003-05-20 01:22:17 +0000735 {
paulceacedb2005-09-29 14:39:32 +0000736 fprintf (stderr, "%s: no privs struct given, terminating", __func__);
737 exit (0);
738 }
paul01245822003-05-20 01:22:17 +0000739
paulceacedb2005-09-29 14:39:32 +0000740#ifdef HAVE_CAPABILITIES
741 zprivs_caps_terminate();
742#else /* !HAVE_CAPABILITIES */
paul01245822003-05-20 01:22:17 +0000743 if (zprivs_state.zuid)
744 {
745 if ( setreuid (zprivs_state.zuid, zprivs_state.zuid) )
746 {
paulceacedb2005-09-29 14:39:32 +0000747 fprintf (stderr, "privs_terminate: could not setreuid, %s",
ajs6099b3b2004-11-20 02:06:59 +0000748 safe_strerror (errno) );
paul01245822003-05-20 01:22:17 +0000749 exit (1);
750 }
751 }
752#endif /* HAVE_LCAPS */
paulceacedb2005-09-29 14:39:32 +0000753
754 zprivs->change = zprivs_change_null;
755 zprivs->current_state = zprivs_state_null;
756 zprivs_null_state = ZPRIVS_LOWERED;
paul01245822003-05-20 01:22:17 +0000757 return;
758}
hassoba3a0bc2003-06-04 17:41:54 +0000759
760void
761zprivs_get_ids(struct zprivs_ids_t *ids)
762{
763
764 ids->uid_priv = getuid();
765 (zprivs_state.zuid) ? (ids->uid_normal = zprivs_state.zuid)
766 : (ids->uid_normal = -1);
767 (zprivs_state.zgid) ? (ids->gid_normal = zprivs_state.zgid)
768 : (ids->gid_normal = -1);
769 (zprivs_state.vtygrp) ? (ids->gid_vty = zprivs_state.vtygrp)
770 : (ids->gid_vty = -1);
771
772 return;
773}