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
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
29 #ifdef HAVE_CAPABILITIES
31 DEFINE_MTYPE_STATIC(LIB
, PRIVS
, "Privilege information")
33 /* sort out some generic internal types for:
35 * privilege values (cap_value_t, priv_t) -> pvalue_t
36 * privilege set (..., priv_set_t) -> pset_t
37 * privilege working storage (cap_t, ...) -> pstorage_t
39 * values we think of as numeric (they're ints really, but we dont know)
40 * sets are mostly opaque, to hold a set of privileges, related in some way.
41 * storage binds together a set of sets we're interested in.
42 * (in reality: cap_value_t and priv_t are ints)
45 /* Linux doesn't have a 'set' type: a set of related privileges */
50 typedef cap_value_t pvalue_t
;
51 typedef struct _pset pset_t
;
52 typedef cap_t pstorage_t
;
54 #elif defined (HAVE_SOLARIS_CAPABILITIES)
55 typedef priv_t pvalue_t
;
56 typedef priv_set_t pset_t
;
57 typedef priv_set_t
*pstorage_t
;
58 #else /* neither LCAPS nor SOLARIS_CAPABILITIES */
59 #error "HAVE_CAPABILITIES defined, but neither LCAPS nor Solaris Capabilties!"
60 #endif /* HAVE_LCAPS */
61 #endif /* HAVE_CAPABILITIES */
63 /* the default NULL state we report is RAISED, but could be LOWERED if
64 * zprivs_terminate is called and the NULL handler is installed.
66 static zebra_privs_current_t zprivs_null_state
= ZPRIVS_RAISED
;
68 /* internal privileges state */
69 static struct _zprivs_t
71 #ifdef HAVE_CAPABILITIES
72 pstorage_t caps
; /* working storage */
73 pset_t
*syscaps_p
; /* system-type requested permitted caps */
74 pset_t
*syscaps_i
; /* system-type requested inheritable caps */
75 #endif /* HAVE_CAPABILITIES */
76 uid_t zuid
, /* uid to run as */
77 zsuid
; /* saved uid */
78 gid_t zgid
; /* gid to run as */
79 gid_t vtygrp
; /* gid for vty sockets */
82 /* externally exported but not directly accessed functions */
83 #ifdef HAVE_CAPABILITIES
84 int zprivs_change_caps (zebra_privs_ops_t
);
85 zebra_privs_current_t
zprivs_state_caps (void);
86 #endif /* HAVE_CAPABILITIES */
87 int zprivs_change_uid (zebra_privs_ops_t
);
88 zebra_privs_current_t
zprivs_state_uid (void);
89 int zprivs_change_null (zebra_privs_ops_t
);
90 zebra_privs_current_t
zprivs_state_null (void);
92 #ifdef HAVE_CAPABILITIES
93 /* internal capability API */
94 static pset_t
*zcaps2sys (zebra_capabilities_t
*, int);
95 static void zprivs_caps_init (struct zebra_privs_t
*);
96 static void zprivs_caps_terminate (void);
98 /* Map of Quagga abstract capabilities to system capabilities */
102 pvalue_t
*system_caps
;
103 } cap_map
[ZCAP_MAX
] =
105 #ifdef HAVE_LCAPS /* Quagga -> Linux capabilities mappings */
106 [ZCAP_SETID
] = { 2, (pvalue_t
[]) { CAP_SETGID
,
108 [ZCAP_BIND
] = { 2, (pvalue_t
[]) { CAP_NET_BIND_SERVICE
}, },
109 [ZCAP_NET_ADMIN
] = { 1, (pvalue_t
[]) { CAP_NET_ADMIN
}, },
110 [ZCAP_NET_RAW
] = { 1, (pvalue_t
[]) { CAP_NET_RAW
}, },
111 [ZCAP_CHROOT
] = { 1, (pvalue_t
[]) { CAP_SYS_CHROOT
, }, },
112 [ZCAP_NICE
] = { 1, (pvalue_t
[]) { CAP_SYS_NICE
}, },
113 [ZCAP_PTRACE
] = { 1, (pvalue_t
[]) { CAP_SYS_PTRACE
}, },
114 [ZCAP_DAC_OVERRIDE
] = { 1, (pvalue_t
[]) { CAP_DAC_OVERRIDE
}, },
115 [ZCAP_READ_SEARCH
] = { 1, (pvalue_t
[]) { CAP_DAC_READ_SEARCH
}, },
116 [ZCAP_SYS_ADMIN
] = { 1, (pvalue_t
[]) { CAP_SYS_ADMIN
}, },
117 [ZCAP_FOWNER
] = { 1, (pvalue_t
[]) { CAP_FOWNER
}, },
118 #elif defined(HAVE_SOLARIS_CAPABILITIES) /* HAVE_LCAPS */
119 /* Quagga -> Solaris privilege mappings */
120 [ZCAP_SETID
] = { 1, (pvalue_t
[]) { PRIV_PROC_SETID
}, },
121 [ZCAP_BIND
] = { 1, (pvalue_t
[]) { PRIV_NET_PRIVADDR
}, },
122 /* IP_CONFIG is a subset of NET_CONFIG and is allowed in zones */
123 #ifdef PRIV_SYS_IP_CONFIG
124 [ZCAP_NET_ADMIN
] = { 1, (pvalue_t
[]) { PRIV_SYS_IP_CONFIG
}, },
126 [ZCAP_NET_ADMIN
] = { 1, (pvalue_t
[]) { PRIV_SYS_NET_CONFIG
}, },
128 [ZCAP_NET_RAW
] = { 2, (pvalue_t
[]) { PRIV_NET_RAWACCESS
,
129 PRIV_NET_ICMPACCESS
}, },
130 [ZCAP_CHROOT
] = { 1, (pvalue_t
[]) { PRIV_PROC_CHROOT
}, },
131 [ZCAP_NICE
] = { 1, (pvalue_t
[]) { PRIV_PROC_PRIOCNTL
}, },
132 [ZCAP_PTRACE
] = { 1, (pvalue_t
[]) { PRIV_PROC_SESSION
}, },
133 [ZCAP_DAC_OVERRIDE
] = { 2, (pvalue_t
[]) { PRIV_FILE_DAC_EXECUTE
,
135 PRIV_FILE_DAC_SEARCH
,
137 PRIV_FILE_DAC_SEARCH
}, },
138 [ZCAP_READ_SEARCH
] = { 2, (pvalue_t
[]) { PRIV_FILE_DAC_SEARCH
,
139 PRIV_FILE_DAC_READ
}, },
140 [ZCAP_SYS_ADMIN
] = { 1, (pvalue_t
[]) { PRIV_SYS_ADMIN
}, },
141 [ZCAP_FOWNER
] = { 1, (pvalue_t
[]) { PRIV_FILE_OWNER
}, },
142 #endif /* HAVE_SOLARIS_CAPABILITIES */
146 /* Linux forms of capabilities methods */
147 /* convert zebras privileges to system capabilities */
149 zcaps2sys (zebra_capabilities_t
*zcaps
, int num
)
152 int i
, j
= 0, count
= 0;
157 /* first count up how many system caps we have */
158 for (i
= 0; i
< num
; i
++)
159 count
+= cap_map
[zcaps
[i
]].num
;
161 if ( (syscaps
= XCALLOC (MTYPE_PRIVS
, (sizeof(pset_t
) * num
))) == NULL
)
163 fprintf (stderr
, "%s: could not allocate syscaps!", __func__
);
167 syscaps
->caps
= XCALLOC (MTYPE_PRIVS
, (sizeof (pvalue_t
) * count
));
171 fprintf (stderr
, "%s: could not XCALLOC caps!", __func__
);
175 /* copy the capabilities over */
177 for (i
=0; i
< num
; i
++)
178 for (j
= 0; j
< cap_map
[zcaps
[i
]].num
; j
++)
179 syscaps
->caps
[count
++] = cap_map
[zcaps
[i
]].system_caps
[j
];
181 /* iterations above should be exact same as previous count, obviously.. */
182 syscaps
->num
= count
;
187 /* set or clear the effective capabilities to/from permitted */
189 zprivs_change_caps (zebra_privs_ops_t op
)
191 cap_flag_value_t cflag
;
193 /* should be no possibility of being called without valid caps */
194 assert (zprivs_state
.syscaps_p
&& zprivs_state
.caps
);
195 if (! (zprivs_state
.syscaps_p
&& zprivs_state
.caps
))
198 if (op
== ZPRIVS_RAISE
)
200 else if (op
== ZPRIVS_LOWER
)
205 if ( !cap_set_flag (zprivs_state
.caps
, CAP_EFFECTIVE
,
206 zprivs_state
.syscaps_p
->num
,
207 zprivs_state
.syscaps_p
->caps
,
209 return cap_set_proc (zprivs_state
.caps
);
213 zebra_privs_current_t
214 zprivs_state_caps (void)
217 cap_flag_value_t val
;
219 /* should be no possibility of being called without valid caps */
220 assert (zprivs_state
.syscaps_p
&& zprivs_state
.caps
);
221 if (! (zprivs_state
.syscaps_p
&& zprivs_state
.caps
))
224 for (i
=0; i
< zprivs_state
.syscaps_p
->num
; i
++)
226 if ( cap_get_flag (zprivs_state
.caps
, zprivs_state
.syscaps_p
->caps
[i
],
227 CAP_EFFECTIVE
, &val
) )
229 zlog_warn ("zprivs_state_caps: could not cap_get_flag, %s",
230 safe_strerror (errno
) );
231 return ZPRIVS_UNKNOWN
;
234 return ZPRIVS_RAISED
;
236 return ZPRIVS_LOWERED
;
240 zprivs_caps_init (struct zebra_privs_t
*zprivs
)
242 zprivs_state
.syscaps_p
= zcaps2sys (zprivs
->caps_p
, zprivs
->cap_num_p
);
243 zprivs_state
.syscaps_i
= zcaps2sys (zprivs
->caps_i
, zprivs
->cap_num_i
);
245 /* Tell kernel we want caps maintained across uid changes */
246 if ( prctl(PR_SET_KEEPCAPS
, 1, 0, 0, 0) == -1 )
248 fprintf (stderr
, "privs_init: could not set PR_SET_KEEPCAPS, %s\n",
249 safe_strerror (errno
) );
253 if ( !zprivs_state
.syscaps_p
)
255 fprintf (stderr
, "privs_init: capabilities enabled, "
256 "but no capabilities supplied\n");
259 /* we have caps, we have no need to ever change back the original user */
260 if (zprivs_state
.zuid
)
262 if ( setreuid (zprivs_state
.zuid
, zprivs_state
.zuid
) )
264 fprintf (stderr
, "zprivs_init (cap): could not setreuid, %s\n",
265 safe_strerror (errno
));
270 if ( !(zprivs_state
.caps
= cap_init()) )
272 fprintf (stderr
, "privs_init: failed to cap_init, %s\n",
273 safe_strerror (errno
));
277 if ( cap_clear (zprivs_state
.caps
) )
279 fprintf (stderr
, "privs_init: failed to cap_clear, %s\n",
280 safe_strerror (errno
));
284 /* set permitted caps */
285 cap_set_flag(zprivs_state
.caps
, CAP_PERMITTED
,
286 zprivs_state
.syscaps_p
->num
,
287 zprivs_state
.syscaps_p
->caps
,
290 /* set inheritable caps, if any */
291 if (zprivs_state
.syscaps_i
&& zprivs_state
.syscaps_i
->num
)
293 cap_set_flag(zprivs_state
.caps
, CAP_INHERITABLE
,
294 zprivs_state
.syscaps_i
->num
,
295 zprivs_state
.syscaps_i
->caps
,
299 /* apply caps. CAP_EFFECTIVE is cleared. we'll raise the caps as
300 * and when, and only when, they are needed.
302 if ( cap_set_proc (zprivs_state
.caps
) )
305 char *current_caps_text
= NULL
;
306 char *wanted_caps_text
= NULL
;
308 fprintf(stderr
, "privs_init: initial cap_set_proc failed: %s\n",
309 safe_strerror(errno
));
311 current_caps
= cap_get_proc();
314 current_caps_text
= cap_to_text(current_caps
, NULL
);
315 cap_free(current_caps
);
318 wanted_caps_text
= cap_to_text(zprivs_state
.caps
, NULL
);
319 fprintf(stderr
, "Wanted caps: %s\n", wanted_caps_text
? wanted_caps_text
: "???");
320 fprintf(stderr
, "Have caps: %s\n", current_caps_text
? current_caps_text
: "???");
321 if (current_caps_text
)
322 cap_free(current_caps_text
);
323 if (wanted_caps_text
)
324 cap_free(wanted_caps_text
);
329 /* set methods for the caller to use */
330 zprivs
->change
= zprivs_change_caps
;
331 zprivs
->current_state
= zprivs_state_caps
;
335 zprivs_caps_terminate (void)
337 /* clear all capabilities */
338 if (zprivs_state
.caps
)
339 cap_clear (zprivs_state
.caps
);
341 /* and boom, capabilities are gone forever */
342 if ( cap_set_proc (zprivs_state
.caps
) )
344 fprintf (stderr
, "privs_terminate: cap_set_proc failed, %s",
345 safe_strerror (errno
) );
349 /* free up private state */
350 if (zprivs_state
.syscaps_p
->num
)
352 XFREE (MTYPE_PRIVS
, zprivs_state
.syscaps_p
->caps
);
353 XFREE (MTYPE_PRIVS
, zprivs_state
.syscaps_p
);
356 if (zprivs_state
.syscaps_i
&& zprivs_state
.syscaps_i
->num
)
358 XFREE (MTYPE_PRIVS
, zprivs_state
.syscaps_i
->caps
);
359 XFREE (MTYPE_PRIVS
, zprivs_state
.syscaps_i
);
362 cap_free (zprivs_state
.caps
);
364 #elif defined (HAVE_SOLARIS_CAPABILITIES) /* !HAVE_LCAPS */
366 /* Solaris specific capability/privilege methods
369 * - the 'privileges' man page
370 * - http://cvs.opensolaris.org
371 * - http://blogs.sun.com/roller/page/gbrunett?entry=privilege_enabling_set_id_programs1
375 zprivs_caps_minimal ()
379 if ((minimal
= priv_str_to_set("basic", ",", NULL
)) == NULL
)
381 fprintf (stderr
, "%s: couldn't get basic set!\n", __func__
);
385 /* create a minimal privilege set from the basic set */
386 (void) priv_delset(minimal
, PRIV_PROC_EXEC
);
387 (void) priv_delset(minimal
, PRIV_PROC_INFO
);
388 (void) priv_delset(minimal
, PRIV_PROC_SESSION
);
389 (void) priv_delset(minimal
, PRIV_FILE_LINK_ANY
);
394 /* convert zebras privileges to system capabilities */
396 zcaps2sys (zebra_capabilities_t
*zcaps
, int num
)
401 if ((syscaps
= priv_allocset()) == NULL
)
403 fprintf (stderr
, "%s: could not allocate syscaps!\n", __func__
);
407 priv_emptyset (syscaps
);
409 for (i
=0; i
< num
; i
++)
410 for (j
= 0; j
< cap_map
[zcaps
[i
]].num
; j
++)
411 priv_addset (syscaps
, cap_map
[zcaps
[i
]].system_caps
[j
]);
416 /* callback exported to users to RAISE and LOWER effective privileges
417 * from nothing to the given permitted set and back down
420 zprivs_change_caps (zebra_privs_ops_t op
)
424 /* should be no possibility of being called without valid caps */
425 assert (zprivs_state
.syscaps_p
);
426 if (!zprivs_state
.syscaps_p
)
428 fprintf (stderr
, "%s: Eek, missing privileged caps!", __func__
);
432 assert (zprivs_state
.caps
);
433 if (!zprivs_state
.caps
)
435 fprintf (stderr
, "%s: Eek, missing caps!", __func__
);
439 /* to raise: copy original permitted as our working effective set
440 * to lower: copy regular effective set stored in zprivs_state.caps
442 if (op
== ZPRIVS_RAISE
)
443 privset
= zprivs_state
.syscaps_p
;
444 else if (op
== ZPRIVS_LOWER
)
445 privset
= zprivs_state
.caps
;
449 if (setppriv (PRIV_SET
, PRIV_EFFECTIVE
, privset
) != 0)
455 /* Retrieve current privilege state, is it RAISED or LOWERED? */
456 zebra_privs_current_t
457 zprivs_state_caps (void)
459 zebra_privs_current_t result
;
462 if ( (effective
= priv_allocset()) == NULL
)
464 fprintf (stderr
, "%s: failed to get priv_allocset! %s\n", __func__
,
465 safe_strerror (errno
));
466 return ZPRIVS_UNKNOWN
;
469 if (getppriv (PRIV_EFFECTIVE
, effective
))
471 fprintf (stderr
, "%s: failed to get state! %s\n", __func__
,
472 safe_strerror (errno
));
473 result
= ZPRIVS_UNKNOWN
;
477 if (priv_isequalset (effective
, zprivs_state
.syscaps_p
))
478 result
= ZPRIVS_RAISED
;
479 else if (priv_isequalset (effective
, zprivs_state
.caps
))
480 result
= ZPRIVS_LOWERED
;
482 result
= ZPRIVS_UNKNOWN
;
485 priv_freeset (effective
);
490 zprivs_caps_init (struct zebra_privs_t
*zprivs
)
495 /* the specified sets */
496 zprivs_state
.syscaps_p
= zcaps2sys (zprivs
->caps_p
, zprivs
->cap_num_p
);
497 zprivs_state
.syscaps_i
= zcaps2sys (zprivs
->caps_i
, zprivs
->cap_num_i
);
499 /* nonsensical to have gotten here but not have capabilities */
500 if (!zprivs_state
.syscaps_p
)
502 fprintf (stderr
, "%s: capabilities enabled, "
503 "but no valid capabilities supplied\n",
507 /* We retain the basic set in our permitted set, as Linux has no
508 * equivalent. The basic set on Linux hence is implicit, always
511 if ((basic
= priv_str_to_set("basic", ",", NULL
)) == NULL
)
513 fprintf (stderr
, "%s: couldn't get basic set!\n", __func__
);
517 /* Add the basic set to the permitted set */
518 priv_union (basic
, zprivs_state
.syscaps_p
);
519 priv_freeset (basic
);
521 /* Hey kernel, we know about privileges!
522 * this isn't strictly required, use of setppriv should have same effect
524 if (setpflags (PRIV_AWARE
, 1))
526 fprintf (stderr
, "%s: error setting PRIV_AWARE!, %s\n", __func__
,
527 safe_strerror (errno
) );
531 /* need either valid or empty sets for both p and i.. */
532 assert (zprivs_state
.syscaps_i
&& zprivs_state
.syscaps_p
);
534 /* we have caps, we have no need to ever change back the original user
535 * change real, effective and saved to the specified user.
537 if (zprivs_state
.zuid
)
539 if ( setreuid (zprivs_state
.zuid
, zprivs_state
.zuid
) )
541 fprintf (stderr
, "%s: could not setreuid, %s\n",
542 __func__
, safe_strerror (errno
));
547 /* set the permitted set */
548 if (setppriv (PRIV_SET
, PRIV_PERMITTED
, zprivs_state
.syscaps_p
))
550 fprintf (stderr
, "%s: error setting permitted set!, %s\n", __func__
,
551 safe_strerror (errno
) );
555 /* set the inheritable set */
556 if (setppriv (PRIV_SET
, PRIV_INHERITABLE
, zprivs_state
.syscaps_i
))
558 fprintf (stderr
, "%s: error setting inheritable set!, %s\n", __func__
,
559 safe_strerror (errno
) );
563 /* we need a minimal basic set for 'effective', potentially for inheritable too */
564 minimal
= zprivs_caps_minimal();
566 /* now set the effective set with a subset of basic privileges */
567 if (setppriv (PRIV_SET
, PRIV_EFFECTIVE
, minimal
))
569 fprintf (stderr
, "%s: error setting effective set!, %s\n", __func__
,
570 safe_strerror (errno
) );
574 /* we'll use the minimal set as our working-storage privset */
575 zprivs_state
.caps
= minimal
;
577 /* set methods for the caller to use */
578 zprivs
->change
= zprivs_change_caps
;
579 zprivs
->current_state
= zprivs_state_caps
;
583 zprivs_caps_terminate (void)
585 assert (zprivs_state
.caps
);
587 /* clear all capabilities by using working-storage privset */
588 setppriv (PRIV_SET
, PRIV_EFFECTIVE
, zprivs_state
.caps
);
589 setppriv (PRIV_SET
, PRIV_PERMITTED
, zprivs_state
.caps
);
590 setppriv (PRIV_SET
, PRIV_INHERITABLE
, zprivs_state
.caps
);
592 /* free up private state */
593 if (zprivs_state
.syscaps_p
)
594 priv_freeset (zprivs_state
.syscaps_p
);
595 if (zprivs_state
.syscaps_i
)
596 priv_freeset (zprivs_state
.syscaps_i
);
598 priv_freeset (zprivs_state
.caps
);
600 #else /* !HAVE_LCAPS && ! HAVE_SOLARIS_CAPABILITIES */
601 #error "Neither Solaris nor Linux capabilities, dazed and confused..."
602 #endif /* HAVE_LCAPS */
603 #endif /* HAVE_CAPABILITIES */
606 zprivs_change_uid (zebra_privs_ops_t op
)
609 if (op
== ZPRIVS_RAISE
)
610 return seteuid (zprivs_state
.zsuid
);
611 else if (op
== ZPRIVS_LOWER
)
612 return seteuid (zprivs_state
.zuid
);
617 zebra_privs_current_t
618 zprivs_state_uid (void)
620 return ( (zprivs_state
.zuid
== geteuid()) ? ZPRIVS_LOWERED
: ZPRIVS_RAISED
);
624 zprivs_change_null (zebra_privs_ops_t op
)
629 zebra_privs_current_t
630 zprivs_state_null (void)
632 return zprivs_null_state
;
635 #ifndef HAVE_GETGROUPLIST
636 /* Solaris 11 has no getgrouplist() */
638 getgrouplist(const char *user
, gid_t group
, gid_t
*groups
, int *ngroups
)
649 while ((grp
= getgrent()))
651 if (grp
->gr_gid
== group
)
653 for (usridx
= 0; grp
->gr_mem
[usridx
] != NULL
; usridx
++)
654 if (!strcmp (grp
->gr_mem
[usridx
], user
))
657 groups
[pos
] = grp
->gr_gid
;
664 ret
= (pos
<= *ngroups
) ? pos
: -1;
668 #endif /* HAVE_GETGROUPLIST */
671 zprivs_init(struct zebra_privs_t
*zprivs
)
673 struct passwd
*pwentry
= NULL
;
674 struct group
*grentry
= NULL
;
675 gid_t groups
[NGROUPS_MAX
];
681 fprintf (stderr
, "zprivs_init: called with NULL arg!\n");
686 if (! (zprivs
->user
|| zprivs
->group
687 || zprivs
->cap_num_p
|| zprivs
->cap_num_i
) )
689 zprivs
->change
= zprivs_change_null
;
690 zprivs
->current_state
= zprivs_state_null
;
696 if ( (pwentry
= getpwnam (zprivs
->user
)) == NULL
)
698 /* cant use log.h here as it depends on vty */
699 fprintf (stderr
, "privs_init: could not lookup user %s\n",
704 zprivs_state
.zuid
= pwentry
->pw_uid
;
705 zprivs_state
.zgid
= pwentry
->pw_gid
;
712 if ( (grentry
= getgrnam (zprivs
->group
)) == NULL
)
714 fprintf (stderr
, "privs_init: could not lookup group %s\n",
719 zprivs_state
.zgid
= grentry
->gr_gid
;
724 ngroups
= sizeof(groups
);
725 if ( (ngroups
= getgrouplist (zprivs
->user
, zprivs_state
.zgid
, groups
, &ngroups
)) < 0 )
727 /* cant use log.h here as it depends on vty */
728 fprintf (stderr
, "privs_init: could not getgrouplist for user %s\n",
734 if (zprivs
->vty_group
)
735 /* Add the vty_group to the supplementary groups so it can be chowned to */
737 if ( (grentry
= getgrnam (zprivs
->vty_group
)) )
739 zprivs_state
.vtygrp
= grentry
->gr_gid
;
741 for ( i
= 0; i
< ngroups
; i
++ )
742 if ( groups
[i
] == zprivs_state
.vtygrp
)
750 fprintf (stderr
, "privs_init: user(%s) is not part of vty group specified(%s)\n",
751 zprivs
->user
, zprivs
->vty_group
);
754 if ( i
>= ngroups
&& ngroups
< (int) ZEBRA_NUM_OF(groups
) )
756 groups
[i
] = zprivs_state
.vtygrp
;
761 fprintf (stderr
, "privs_init: could not lookup vty group %s\n",
769 if ( setgroups (ngroups
, groups
) )
771 fprintf (stderr
, "privs_init: could not setgroups, %s\n",
772 safe_strerror (errno
) );
777 if (zprivs_state
.zgid
)
779 /* change group now, forever. uid we do later */
780 if ( setregid (zprivs_state
.zgid
, zprivs_state
.zgid
) )
782 fprintf (stderr
, "zprivs_init: could not setregid, %s\n",
783 safe_strerror (errno
) );
788 #ifdef HAVE_CAPABILITIES
789 zprivs_caps_init (zprivs
);
790 #else /* !HAVE_CAPABILITIES */
791 /* we dont have caps. we'll need to maintain rid and saved uid
792 * and change euid back to saved uid (who we presume has all neccessary
793 * privileges) whenever we are asked to raise our privileges.
795 * This is not worth that much security wise, but all we can do.
797 zprivs_state
.zsuid
= geteuid();
798 if ( zprivs_state
.zuid
)
800 if ( setreuid (-1, zprivs_state
.zuid
) )
802 fprintf (stderr
, "privs_init (uid): could not setreuid, %s\n",
803 safe_strerror (errno
));
808 zprivs
->change
= zprivs_change_uid
;
809 zprivs
->current_state
= zprivs_state_uid
;
810 #endif /* HAVE_CAPABILITIES */
814 zprivs_terminate (struct zebra_privs_t
*zprivs
)
818 fprintf (stderr
, "%s: no privs struct given, terminating", __func__
);
822 #ifdef HAVE_CAPABILITIES
823 zprivs_caps_terminate();
824 #else /* !HAVE_CAPABILITIES */
825 if (zprivs_state
.zuid
)
827 if ( setreuid (zprivs_state
.zuid
, zprivs_state
.zuid
) )
829 fprintf (stderr
, "privs_terminate: could not setreuid, %s",
830 safe_strerror (errno
) );
834 #endif /* HAVE_LCAPS */
836 zprivs
->change
= zprivs_change_null
;
837 zprivs
->current_state
= zprivs_state_null
;
838 zprivs_null_state
= ZPRIVS_LOWERED
;
843 zprivs_get_ids(struct zprivs_ids_t
*ids
)
846 ids
->uid_priv
= getuid();
847 (zprivs_state
.zuid
) ? (ids
->uid_normal
= zprivs_state
.zuid
)
848 : (ids
->uid_normal
= -1);
849 (zprivs_state
.zgid
) ? (ids
->gid_normal
= zprivs_state
.zgid
)
850 : (ids
->gid_normal
= -1);
851 (zprivs_state
.vtygrp
) ? (ids
->gid_vty
= zprivs_state
.vtygrp
)
852 : (ids
->gid_vty
= -1);