4 * Copyright (C) 2003 Paul Jakma.
5 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
7 * This file is part of GNU Zebra.
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
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.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; see the file COPYING; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #ifdef HAVE_CAPABILITIES
30 DEFINE_MTYPE_STATIC(LIB
, PRIVS
, "Privilege information")
32 /* sort out some generic internal types for:
34 * privilege values (cap_value_t, priv_t) -> pvalue_t
35 * privilege set (..., priv_set_t) -> pset_t
36 * privilege working storage (cap_t, ...) -> pstorage_t
38 * values we think of as numeric (they're ints really, but we dont know)
39 * sets are mostly opaque, to hold a set of privileges, related in some way.
40 * storage binds together a set of sets we're interested in.
41 * (in reality: cap_value_t and priv_t are ints)
44 /* Linux doesn't have a 'set' type: a set of related privileges */
49 typedef cap_value_t pvalue_t
;
50 typedef struct _pset pset_t
;
51 typedef cap_t pstorage_t
;
53 #elif defined(HAVE_SOLARIS_CAPABILITIES)
54 typedef priv_t pvalue_t
;
55 typedef priv_set_t pset_t
;
56 typedef priv_set_t
*pstorage_t
;
57 #else /* neither LCAPS nor SOLARIS_CAPABILITIES */
58 #error "HAVE_CAPABILITIES defined, but neither LCAPS nor Solaris Capabilties!"
59 #endif /* HAVE_LCAPS */
60 #endif /* HAVE_CAPABILITIES */
62 /* the default NULL state we report is RAISED, but could be LOWERED if
63 * zprivs_terminate is called and the NULL handler is installed.
65 static zebra_privs_current_t zprivs_null_state
= ZPRIVS_RAISED
;
67 /* internal privileges state */
68 static struct _zprivs_t
{
69 #ifdef HAVE_CAPABILITIES
70 pstorage_t caps
; /* working storage */
71 pset_t
*syscaps_p
; /* system-type requested permitted caps */
72 pset_t
*syscaps_i
; /* system-type requested inheritable caps */
73 #endif /* HAVE_CAPABILITIES */
74 uid_t zuid
, /* uid to run as */
75 zsuid
; /* saved uid */
76 gid_t zgid
; /* gid to run as */
77 gid_t vtygrp
; /* gid for vty sockets */
80 /* externally exported but not directly accessed functions */
81 #ifdef HAVE_CAPABILITIES
82 int zprivs_change_caps(zebra_privs_ops_t
);
83 zebra_privs_current_t
zprivs_state_caps(void);
84 #endif /* HAVE_CAPABILITIES */
85 int zprivs_change_uid(zebra_privs_ops_t
);
86 zebra_privs_current_t
zprivs_state_uid(void);
87 int zprivs_change_null(zebra_privs_ops_t
);
88 zebra_privs_current_t
zprivs_state_null(void);
90 #ifdef HAVE_CAPABILITIES
91 /* internal capability API */
92 static pset_t
*zcaps2sys(zebra_capabilities_t
*, int);
93 static void zprivs_caps_init(struct zebra_privs_t
*);
94 static void zprivs_caps_terminate(void);
96 /* Map of Quagga abstract capabilities to system capabilities */
99 pvalue_t
*system_caps
;
100 } cap_map
[ZCAP_MAX
] = {
101 #ifdef HAVE_LCAPS /* Quagga -> Linux capabilities mappings */
105 (pvalue_t
[]){CAP_SETGID
, CAP_SETUID
},
110 (pvalue_t
[]){CAP_NET_BIND_SERVICE
},
115 (pvalue_t
[]){CAP_NET_ADMIN
},
120 (pvalue_t
[]){CAP_NET_RAW
},
132 (pvalue_t
[]){CAP_SYS_NICE
},
137 (pvalue_t
[]){CAP_SYS_PTRACE
},
139 [ZCAP_DAC_OVERRIDE
] =
142 (pvalue_t
[]){CAP_DAC_OVERRIDE
},
147 (pvalue_t
[]){CAP_DAC_READ_SEARCH
},
152 (pvalue_t
[]){CAP_SYS_ADMIN
},
157 (pvalue_t
[]){CAP_FOWNER
},
159 #elif defined(HAVE_SOLARIS_CAPABILITIES) /* HAVE_LCAPS */
160 /* Quagga -> Solaris privilege mappings */
164 (pvalue_t
[]){PRIV_PROC_SETID
},
169 (pvalue_t
[]){PRIV_NET_PRIVADDR
},
171 /* IP_CONFIG is a subset of NET_CONFIG and is allowed in zones */
172 #ifdef PRIV_SYS_IP_CONFIG
176 (pvalue_t
[]){PRIV_SYS_IP_CONFIG
},
182 (pvalue_t
[]){PRIV_SYS_NET_CONFIG
},
188 (pvalue_t
[]){PRIV_NET_RAWACCESS
, PRIV_NET_ICMPACCESS
},
193 (pvalue_t
[]){PRIV_PROC_CHROOT
},
198 (pvalue_t
[]){PRIV_PROC_PRIOCNTL
},
203 (pvalue_t
[]){PRIV_PROC_SESSION
},
205 [ZCAP_DAC_OVERRIDE
] =
208 (pvalue_t
[]){PRIV_FILE_DAC_EXECUTE
, PRIV_FILE_DAC_READ
,
209 PRIV_FILE_DAC_SEARCH
, PRIV_FILE_DAC_WRITE
,
210 PRIV_FILE_DAC_SEARCH
},
215 (pvalue_t
[]){PRIV_FILE_DAC_SEARCH
, PRIV_FILE_DAC_READ
},
220 (pvalue_t
[]){PRIV_SYS_ADMIN
},
225 (pvalue_t
[]){PRIV_FILE_OWNER
},
227 #endif /* HAVE_SOLARIS_CAPABILITIES */
231 /* Linux forms of capabilities methods */
232 /* convert zebras privileges to system capabilities */
233 static pset_t
*zcaps2sys(zebra_capabilities_t
*zcaps
, int num
)
236 int i
, j
= 0, count
= 0;
241 /* first count up how many system caps we have */
242 for (i
= 0; i
< num
; i
++)
243 count
+= cap_map
[zcaps
[i
]].num
;
245 if ((syscaps
= XCALLOC(MTYPE_PRIVS
, (sizeof(pset_t
) * num
))) == NULL
) {
246 fprintf(stderr
, "%s: could not allocate syscaps!", __func__
);
250 syscaps
->caps
= XCALLOC(MTYPE_PRIVS
, (sizeof(pvalue_t
) * count
));
252 if (!syscaps
->caps
) {
253 fprintf(stderr
, "%s: could not XCALLOC caps!", __func__
);
257 /* copy the capabilities over */
259 for (i
= 0; i
< num
; i
++)
260 for (j
= 0; j
< cap_map
[zcaps
[i
]].num
; j
++)
261 syscaps
->caps
[count
++] =
262 cap_map
[zcaps
[i
]].system_caps
[j
];
264 /* iterations above should be exact same as previous count, obviously..
266 syscaps
->num
= count
;
271 /* set or clear the effective capabilities to/from permitted */
272 int zprivs_change_caps(zebra_privs_ops_t op
)
274 cap_flag_value_t cflag
;
276 /* should be no possibility of being called without valid caps */
277 assert(zprivs_state
.syscaps_p
&& zprivs_state
.caps
);
278 if (!(zprivs_state
.syscaps_p
&& zprivs_state
.caps
))
281 if (op
== ZPRIVS_RAISE
)
283 else if (op
== ZPRIVS_LOWER
)
288 if (!cap_set_flag(zprivs_state
.caps
, CAP_EFFECTIVE
,
289 zprivs_state
.syscaps_p
->num
,
290 zprivs_state
.syscaps_p
->caps
, cflag
))
291 return cap_set_proc(zprivs_state
.caps
);
295 zebra_privs_current_t
zprivs_state_caps(void)
298 cap_flag_value_t val
;
300 /* should be no possibility of being called without valid caps */
301 assert(zprivs_state
.syscaps_p
&& zprivs_state
.caps
);
302 if (!(zprivs_state
.syscaps_p
&& zprivs_state
.caps
))
305 for (i
= 0; i
< zprivs_state
.syscaps_p
->num
; i
++) {
306 if (cap_get_flag(zprivs_state
.caps
,
307 zprivs_state
.syscaps_p
->caps
[i
], CAP_EFFECTIVE
,
310 "zprivs_state_caps: could not cap_get_flag, %s",
311 safe_strerror(errno
));
312 return ZPRIVS_UNKNOWN
;
315 return ZPRIVS_RAISED
;
317 return ZPRIVS_LOWERED
;
320 static void zprivs_caps_init(struct zebra_privs_t
*zprivs
)
322 zprivs_state
.syscaps_p
= zcaps2sys(zprivs
->caps_p
, zprivs
->cap_num_p
);
323 zprivs_state
.syscaps_i
= zcaps2sys(zprivs
->caps_i
, zprivs
->cap_num_i
);
325 /* Tell kernel we want caps maintained across uid changes */
326 if (prctl(PR_SET_KEEPCAPS
, 1, 0, 0, 0) == -1) {
328 "privs_init: could not set PR_SET_KEEPCAPS, %s\n",
329 safe_strerror(errno
));
333 /* we have caps, we have no need to ever change back the original user
335 /* only change uid if we don't have the correct one */
336 if ((zprivs_state
.zuid
) && (zprivs_state
.zsuid
!= zprivs_state
.zuid
)) {
337 if (setreuid(zprivs_state
.zuid
, zprivs_state
.zuid
)) {
339 "zprivs_init (cap): could not setreuid, %s\n",
340 safe_strerror(errno
));
345 if (!zprivs_state
.syscaps_p
)
348 if (!(zprivs_state
.caps
= cap_init())) {
349 fprintf(stderr
, "privs_init: failed to cap_init, %s\n",
350 safe_strerror(errno
));
354 if (cap_clear(zprivs_state
.caps
)) {
355 fprintf(stderr
, "privs_init: failed to cap_clear, %s\n",
356 safe_strerror(errno
));
360 /* set permitted caps */
361 cap_set_flag(zprivs_state
.caps
, CAP_PERMITTED
,
362 zprivs_state
.syscaps_p
->num
, zprivs_state
.syscaps_p
->caps
,
365 /* set inheritable caps, if any */
366 if (zprivs_state
.syscaps_i
&& zprivs_state
.syscaps_i
->num
) {
367 cap_set_flag(zprivs_state
.caps
, CAP_INHERITABLE
,
368 zprivs_state
.syscaps_i
->num
,
369 zprivs_state
.syscaps_i
->caps
, CAP_SET
);
372 /* apply caps. CAP_EFFECTIVE is cleared. we'll raise the caps as
373 * and when, and only when, they are needed.
375 if (cap_set_proc(zprivs_state
.caps
)) {
377 char *current_caps_text
= NULL
;
378 char *wanted_caps_text
= NULL
;
380 fprintf(stderr
, "privs_init: initial cap_set_proc failed: %s\n",
381 safe_strerror(errno
));
383 current_caps
= cap_get_proc();
385 current_caps_text
= cap_to_text(current_caps
, NULL
);
386 cap_free(current_caps
);
389 wanted_caps_text
= cap_to_text(zprivs_state
.caps
, NULL
);
390 fprintf(stderr
, "Wanted caps: %s\n",
391 wanted_caps_text
? wanted_caps_text
: "???");
392 fprintf(stderr
, "Have caps: %s\n",
393 current_caps_text
? current_caps_text
: "???");
394 if (current_caps_text
)
395 cap_free(current_caps_text
);
396 if (wanted_caps_text
)
397 cap_free(wanted_caps_text
);
402 /* set methods for the caller to use */
403 zprivs
->change
= zprivs_change_caps
;
404 zprivs
->current_state
= zprivs_state_caps
;
407 static void zprivs_caps_terminate(void)
409 /* clear all capabilities */
410 if (zprivs_state
.caps
)
411 cap_clear(zprivs_state
.caps
);
413 /* and boom, capabilities are gone forever */
414 if (cap_set_proc(zprivs_state
.caps
)) {
415 fprintf(stderr
, "privs_terminate: cap_set_proc failed, %s",
416 safe_strerror(errno
));
420 /* free up private state */
421 if (zprivs_state
.syscaps_p
->num
) {
422 XFREE(MTYPE_PRIVS
, zprivs_state
.syscaps_p
->caps
);
423 XFREE(MTYPE_PRIVS
, zprivs_state
.syscaps_p
);
426 if (zprivs_state
.syscaps_i
&& zprivs_state
.syscaps_i
->num
) {
427 XFREE(MTYPE_PRIVS
, zprivs_state
.syscaps_i
->caps
);
428 XFREE(MTYPE_PRIVS
, zprivs_state
.syscaps_i
);
431 cap_free(zprivs_state
.caps
);
433 #elif defined(HAVE_SOLARIS_CAPABILITIES) /* !HAVE_LCAPS */
435 /* Solaris specific capability/privilege methods
438 * - the 'privileges' man page
439 * - http://cvs.opensolaris.org
441 * http://blogs.sun.com/roller/page/gbrunett?entry=privilege_enabling_set_id_programs1
444 static pset_t
*zprivs_caps_minimal()
448 if ((minimal
= priv_str_to_set("basic", ",", NULL
)) == NULL
) {
449 fprintf(stderr
, "%s: couldn't get basic set!\n", __func__
);
453 /* create a minimal privilege set from the basic set */
454 (void)priv_delset(minimal
, PRIV_PROC_EXEC
);
455 (void)priv_delset(minimal
, PRIV_PROC_INFO
);
456 (void)priv_delset(minimal
, PRIV_PROC_SESSION
);
457 (void)priv_delset(minimal
, PRIV_FILE_LINK_ANY
);
462 /* convert zebras privileges to system capabilities */
463 static pset_t
*zcaps2sys(zebra_capabilities_t
*zcaps
, int num
)
468 if ((syscaps
= priv_allocset()) == NULL
) {
469 fprintf(stderr
, "%s: could not allocate syscaps!\n", __func__
);
473 priv_emptyset(syscaps
);
475 for (i
= 0; i
< num
; i
++)
476 for (j
= 0; j
< cap_map
[zcaps
[i
]].num
; j
++)
477 priv_addset(syscaps
, cap_map
[zcaps
[i
]].system_caps
[j
]);
482 /* callback exported to users to RAISE and LOWER effective privileges
483 * from nothing to the given permitted set and back down
485 int zprivs_change_caps(zebra_privs_ops_t op
)
489 /* should be no possibility of being called without valid caps */
490 assert(zprivs_state
.syscaps_p
);
491 if (!zprivs_state
.syscaps_p
) {
492 fprintf(stderr
, "%s: Eek, missing privileged caps!", __func__
);
496 assert(zprivs_state
.caps
);
497 if (!zprivs_state
.caps
) {
498 fprintf(stderr
, "%s: Eek, missing caps!", __func__
);
502 /* to raise: copy original permitted as our working effective set
503 * to lower: copy regular effective set stored in zprivs_state.caps
505 if (op
== ZPRIVS_RAISE
)
506 privset
= zprivs_state
.syscaps_p
;
507 else if (op
== ZPRIVS_LOWER
)
508 privset
= zprivs_state
.caps
;
512 if (setppriv(PRIV_SET
, PRIV_EFFECTIVE
, privset
) != 0)
518 /* Retrieve current privilege state, is it RAISED or LOWERED? */
519 zebra_privs_current_t
zprivs_state_caps(void)
521 zebra_privs_current_t result
;
524 if ((effective
= priv_allocset()) == NULL
) {
525 fprintf(stderr
, "%s: failed to get priv_allocset! %s\n",
526 __func__
, safe_strerror(errno
));
527 return ZPRIVS_UNKNOWN
;
530 if (getppriv(PRIV_EFFECTIVE
, effective
)) {
531 fprintf(stderr
, "%s: failed to get state! %s\n", __func__
,
532 safe_strerror(errno
));
533 result
= ZPRIVS_UNKNOWN
;
535 if (priv_isequalset(effective
, zprivs_state
.syscaps_p
))
536 result
= ZPRIVS_RAISED
;
537 else if (priv_isequalset(effective
, zprivs_state
.caps
))
538 result
= ZPRIVS_LOWERED
;
540 result
= ZPRIVS_UNKNOWN
;
543 priv_freeset(effective
);
547 static void zprivs_caps_init(struct zebra_privs_t
*zprivs
)
552 /* the specified sets */
553 zprivs_state
.syscaps_p
= zcaps2sys(zprivs
->caps_p
, zprivs
->cap_num_p
);
554 zprivs_state
.syscaps_i
= zcaps2sys(zprivs
->caps_i
, zprivs
->cap_num_i
);
556 /* nonsensical to have gotten here but not have capabilities */
557 if (!zprivs_state
.syscaps_p
) {
559 "%s: capabilities enabled, "
560 "but no valid capabilities supplied\n",
564 /* We retain the basic set in our permitted set, as Linux has no
565 * equivalent. The basic set on Linux hence is implicit, always
568 if ((basic
= priv_str_to_set("basic", ",", NULL
)) == NULL
) {
569 fprintf(stderr
, "%s: couldn't get basic set!\n", __func__
);
573 /* Add the basic set to the permitted set */
574 priv_union(basic
, zprivs_state
.syscaps_p
);
577 /* Hey kernel, we know about privileges!
578 * this isn't strictly required, use of setppriv should have same effect
580 if (setpflags(PRIV_AWARE
, 1)) {
581 fprintf(stderr
, "%s: error setting PRIV_AWARE!, %s\n", __func__
,
582 safe_strerror(errno
));
586 /* need either valid or empty sets for both p and i.. */
587 assert(zprivs_state
.syscaps_i
&& zprivs_state
.syscaps_p
);
589 /* we have caps, we have no need to ever change back the original user
590 * change real, effective and saved to the specified user.
592 /* only change uid if we don't have the correct one */
593 if ((zprivs_state
.zuid
) && (zprivs_state
.zsuid
!= zprivs_state
.zuid
)) {
594 if (setreuid(zprivs_state
.zuid
, zprivs_state
.zuid
)) {
595 fprintf(stderr
, "%s: could not setreuid, %s\n",
596 __func__
, safe_strerror(errno
));
601 /* set the permitted set */
602 if (setppriv(PRIV_SET
, PRIV_PERMITTED
, zprivs_state
.syscaps_p
)) {
603 fprintf(stderr
, "%s: error setting permitted set!, %s\n",
604 __func__
, safe_strerror(errno
));
608 /* set the inheritable set */
609 if (setppriv(PRIV_SET
, PRIV_INHERITABLE
, zprivs_state
.syscaps_i
)) {
610 fprintf(stderr
, "%s: error setting inheritable set!, %s\n",
611 __func__
, safe_strerror(errno
));
615 /* we need a minimal basic set for 'effective', potentially for
617 minimal
= zprivs_caps_minimal();
619 /* now set the effective set with a subset of basic privileges */
620 if (setppriv(PRIV_SET
, PRIV_EFFECTIVE
, minimal
)) {
621 fprintf(stderr
, "%s: error setting effective set!, %s\n",
622 __func__
, safe_strerror(errno
));
626 /* we'll use the minimal set as our working-storage privset */
627 zprivs_state
.caps
= minimal
;
629 /* set methods for the caller to use */
630 zprivs
->change
= zprivs_change_caps
;
631 zprivs
->current_state
= zprivs_state_caps
;
634 static void zprivs_caps_terminate(void)
636 assert(zprivs_state
.caps
);
638 /* clear all capabilities by using working-storage privset */
639 setppriv(PRIV_SET
, PRIV_EFFECTIVE
, zprivs_state
.caps
);
640 setppriv(PRIV_SET
, PRIV_PERMITTED
, zprivs_state
.caps
);
641 setppriv(PRIV_SET
, PRIV_INHERITABLE
, zprivs_state
.caps
);
643 /* free up private state */
644 if (zprivs_state
.syscaps_p
)
645 priv_freeset(zprivs_state
.syscaps_p
);
646 if (zprivs_state
.syscaps_i
)
647 priv_freeset(zprivs_state
.syscaps_i
);
649 priv_freeset(zprivs_state
.caps
);
651 #else /* !HAVE_LCAPS && ! HAVE_SOLARIS_CAPABILITIES */
652 #error "Neither Solaris nor Linux capabilities, dazed and confused..."
653 #endif /* HAVE_LCAPS */
654 #endif /* HAVE_CAPABILITIES */
656 int zprivs_change_uid(zebra_privs_ops_t op
)
658 if (zprivs_state
.zsuid
== zprivs_state
.zuid
)
660 if (op
== ZPRIVS_RAISE
)
661 return seteuid(zprivs_state
.zsuid
);
662 else if (op
== ZPRIVS_LOWER
)
663 return seteuid(zprivs_state
.zuid
);
668 zebra_privs_current_t
zprivs_state_uid(void)
670 return ((zprivs_state
.zuid
== geteuid()) ? ZPRIVS_LOWERED
674 int zprivs_change_null(zebra_privs_ops_t op
)
679 zebra_privs_current_t
zprivs_state_null(void)
681 return zprivs_null_state
;
684 #ifndef HAVE_GETGROUPLIST
685 /* Solaris 11 has no getgrouplist() */
686 static int getgrouplist(const char *user
, gid_t group
, gid_t
*groups
,
698 while ((grp
= getgrent())) {
699 if (grp
->gr_gid
== group
)
701 for (usridx
= 0; grp
->gr_mem
[usridx
] != NULL
; usridx
++)
702 if (!strcmp(grp
->gr_mem
[usridx
], user
)) {
704 groups
[pos
] = grp
->gr_gid
;
711 ret
= (pos
<= *ngroups
) ? pos
: -1;
715 #endif /* HAVE_GETGROUPLIST */
717 void zprivs_init(struct zebra_privs_t
*zprivs
)
719 struct passwd
*pwentry
= NULL
;
720 struct group
*grentry
= NULL
;
721 gid_t groups
[NGROUPS_MAX
];
726 fprintf(stderr
, "zprivs_init: called with NULL arg!\n");
730 if (zprivs
->vty_group
) {
731 /* in a "NULL" setup, this is allowed to fail too, but still
733 if ((grentry
= getgrnam(zprivs
->vty_group
)))
734 zprivs_state
.vtygrp
= grentry
->gr_gid
;
736 zprivs_state
.vtygrp
= (gid_t
)-1;
740 if (!(zprivs
->user
|| zprivs
->group
|| zprivs
->cap_num_p
741 || zprivs
->cap_num_i
)) {
742 zprivs
->change
= zprivs_change_null
;
743 zprivs
->current_state
= zprivs_state_null
;
748 if ((pwentry
= getpwnam(zprivs
->user
)) == NULL
) {
749 /* cant use log.h here as it depends on vty */
751 "privs_init: could not lookup user %s\n",
756 zprivs_state
.zuid
= pwentry
->pw_uid
;
757 zprivs_state
.zgid
= pwentry
->pw_gid
;
763 if ((grentry
= getgrnam(zprivs
->group
)) == NULL
) {
765 "privs_init: could not lookup group %s\n",
770 zprivs_state
.zgid
= grentry
->gr_gid
;
774 ngroups
= sizeof(groups
);
775 if (getgrouplist(zprivs
->user
, zprivs_state
.zgid
, groups
,
778 /* cant use log.h here as it depends on vty */
780 "privs_init: could not getgrouplist for user %s\n",
786 if (zprivs
->vty_group
)
787 /* Add the vty_group to the supplementary groups so it can be chowned to
790 if (zprivs_state
.vtygrp
== (gid_t
)-1) {
792 "privs_init: could not lookup vty group %s\n",
797 for (i
= 0; i
< ngroups
; i
++)
798 if (groups
[i
] == zprivs_state
.vtygrp
) {
805 "privs_init: user(%s) is not part of vty group specified(%s)\n",
806 zprivs
->user
, zprivs
->vty_group
);
809 if (i
>= ngroups
&& ngroups
< (int)ZEBRA_NUM_OF(groups
)) {
810 groups
[i
] = zprivs_state
.vtygrp
;
814 zprivs_state
.zsuid
= geteuid(); /* initial uid */
815 /* add groups only if we changed uid - otherwise skip */
816 if ((ngroups
) && (zprivs_state
.zsuid
!= zprivs_state
.zuid
)) {
817 if (setgroups(ngroups
, groups
)) {
818 fprintf(stderr
, "privs_init: could not setgroups, %s\n",
819 safe_strerror(errno
));
824 /* change gid only if we changed uid - otherwise skip */
825 if ((zprivs_state
.zgid
) && (zprivs_state
.zsuid
!= zprivs_state
.zuid
)) {
826 /* change group now, forever. uid we do later */
827 if (setregid(zprivs_state
.zgid
, zprivs_state
.zgid
)) {
828 fprintf(stderr
, "zprivs_init: could not setregid, %s\n",
829 safe_strerror(errno
));
834 #ifdef HAVE_CAPABILITIES
835 zprivs_caps_init(zprivs
);
836 #else /* !HAVE_CAPABILITIES */
837 /* we dont have caps. we'll need to maintain rid and saved uid
838 * and change euid back to saved uid (who we presume has all neccessary
839 * privileges) whenever we are asked to raise our privileges.
841 * This is not worth that much security wise, but all we can do.
843 zprivs_state
.zsuid
= geteuid();
844 /* only change uid if we don't have the correct one */
845 if ((zprivs_state
.zuid
) && (zprivs_state
.zsuid
!= zprivs_state
.zuid
)) {
846 if (setreuid(-1, zprivs_state
.zuid
)) {
848 "privs_init (uid): could not setreuid, %s\n",
849 safe_strerror(errno
));
854 zprivs
->change
= zprivs_change_uid
;
855 zprivs
->current_state
= zprivs_state_uid
;
856 #endif /* HAVE_CAPABILITIES */
859 void zprivs_terminate(struct zebra_privs_t
*zprivs
)
862 fprintf(stderr
, "%s: no privs struct given, terminating",
867 #ifdef HAVE_CAPABILITIES
868 zprivs_caps_terminate();
869 #else /* !HAVE_CAPABILITIES */
870 /* only change uid if we don't have the correct one */
871 if ((zprivs_state
.zuid
) && (zprivs_state
.zsuid
!= zprivs_state
.zuid
)) {
872 if (setreuid(zprivs_state
.zuid
, zprivs_state
.zuid
)) {
874 "privs_terminate: could not setreuid, %s",
875 safe_strerror(errno
));
879 #endif /* HAVE_LCAPS */
881 zprivs
->change
= zprivs_change_null
;
882 zprivs
->current_state
= zprivs_state_null
;
883 zprivs_null_state
= ZPRIVS_LOWERED
;
887 void zprivs_get_ids(struct zprivs_ids_t
*ids
)
890 ids
->uid_priv
= getuid();
891 (zprivs_state
.zuid
) ? (ids
->uid_normal
= zprivs_state
.zuid
)
892 : (ids
->uid_normal
= -1);
893 (zprivs_state
.zgid
) ? (ids
->gid_normal
= zprivs_state
.zgid
)
894 : (ids
->gid_normal
= -1);
895 (zprivs_state
.vtygrp
) ? (ids
->gid_vty
= zprivs_state
.vtygrp
)
896 : (ids
->gid_vty
= -1);