]> git.proxmox.com Git - mirror_frr.git/blame - lib/privs.c
Merge pull request #9846 from idryzhov/lib-zebra-netns
[mirror_frr.git] / lib / privs.c
CommitLineData
d62a17ae 1/*
01245821 2 * Zebra privileges.
3 *
4 * Copyright (C) 2003 Paul Jakma.
01070b9e 5 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
01245821 6 *
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 *
896014f4
DL
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
01245821 22 */
01245821 23#include <zebra.h>
24#include "log.h"
25#include "privs.h"
26#include "memory.h"
00dffa8c 27#include "frr_pthread.h"
ff245f0e 28#include "lib_errors.h"
8875d051 29#include "lib/queue.h"
01245821 30
bf8d3d6a 31DEFINE_MTYPE_STATIC(LIB, PRIVS, "Privilege information");
8875d051
MS
32
33/*
34 * Different capabilities/privileges apis have different characteristics: some
35 * are process-wide, and some are per-thread.
36 */
ceacedba 37#ifdef HAVE_CAPABILITIES
8875d051
MS
38#ifdef HAVE_LCAPS
39static const bool privs_per_process; /* = false */
8875d051
MS
40#else
41static const bool privs_per_process = true;
cae8bc96
DS
42#endif /* HAVE_LCAPS */
43#else /* HAVE_CAPABILITIES */
44static const bool privs_per_process = true;
45#endif
4a1ab8e4 46
8875d051 47#ifdef HAVE_CAPABILITIES
4a1ab8e4 48
ceacedba 49/* sort out some generic internal types for:
50 *
51 * privilege values (cap_value_t, priv_t) -> pvalue_t
52 * privilege set (..., priv_set_t) -> pset_t
53 * privilege working storage (cap_t, ...) -> pstorage_t
54 *
55 * values we think of as numeric (they're ints really, but we dont know)
56 * sets are mostly opaque, to hold a set of privileges, related in some way.
57 * storage binds together a set of sets we're interested in.
58 * (in reality: cap_value_t and priv_t are ints)
d62a17ae 59 */
ceacedba 60#ifdef HAVE_LCAPS
61/* Linux doesn't have a 'set' type: a set of related privileges */
62struct _pset {
d62a17ae 63 int num;
64 cap_value_t *caps;
ceacedba 65};
66typedef cap_value_t pvalue_t;
67typedef struct _pset pset_t;
68typedef cap_t pstorage_t;
6b0655a2 69
cae8bc96 70#else /* no LCAPS */
ceacedba 71#error "HAVE_CAPABILITIES defined, but neither LCAPS nor Solaris Capabilties!"
72#endif /* HAVE_LCAPS */
73#endif /* HAVE_CAPABILITIES */
6b0655a2 74
ceacedba 75/* the default NULL state we report is RAISED, but could be LOWERED if
76 * zprivs_terminate is called and the NULL handler is installed.
77 */
78static zebra_privs_current_t zprivs_null_state = ZPRIVS_RAISED;
79
01245821 80/* internal privileges state */
d62a17ae 81static struct _zprivs_t {
ceacedba 82#ifdef HAVE_CAPABILITIES
d62a17ae 83 pstorage_t caps; /* working storage */
84 pset_t *syscaps_p; /* system-type requested permitted caps */
85 pset_t *syscaps_i; /* system-type requested inheritable caps */
86#endif /* HAVE_CAPABILITIES */
87 uid_t zuid, /* uid to run as */
88 zsuid; /* saved uid */
89 gid_t zgid; /* gid to run as */
90 gid_t vtygrp; /* gid for vty sockets */
01245821 91} zprivs_state;
92
93/* externally exported but not directly accessed functions */
ceacedba 94#ifdef HAVE_CAPABILITIES
d62a17ae 95int zprivs_change_caps(zebra_privs_ops_t);
96zebra_privs_current_t zprivs_state_caps(void);
ceacedba 97#endif /* HAVE_CAPABILITIES */
d62a17ae 98int zprivs_change_uid(zebra_privs_ops_t);
99zebra_privs_current_t zprivs_state_uid(void);
100int zprivs_change_null(zebra_privs_ops_t);
101zebra_privs_current_t zprivs_state_null(void);
01245821 102
ceacedba 103#ifdef HAVE_CAPABILITIES
104/* internal capability API */
d62a17ae 105static pset_t *zcaps2sys(zebra_capabilities_t *, int);
106static void zprivs_caps_init(struct zebra_privs_t *);
107static void zprivs_caps_terminate(void);
ceacedba 108
109/* Map of Quagga abstract capabilities to system capabilities */
d62a17ae 110static struct {
111 int num;
112 pvalue_t *system_caps;
113} cap_map[ZCAP_MAX] = {
ceacedba 114#ifdef HAVE_LCAPS /* Quagga -> Linux capabilities mappings */
9d303b37
DL
115 [ZCAP_SETID] =
116 {
117 2, (pvalue_t[]){CAP_SETGID, CAP_SETUID},
118 },
119 [ZCAP_BIND] =
120 {
121 1, (pvalue_t[]){CAP_NET_BIND_SERVICE},
122 },
123 [ZCAP_NET_ADMIN] =
124 {
125 1, (pvalue_t[]){CAP_NET_ADMIN},
126 },
127 [ZCAP_NET_RAW] =
128 {
129 1, (pvalue_t[]){CAP_NET_RAW},
130 },
131 [ZCAP_CHROOT] =
132 {
133 1,
134 (pvalue_t[]){
135 CAP_SYS_CHROOT,
136 },
137 },
138 [ZCAP_NICE] =
139 {
140 1, (pvalue_t[]){CAP_SYS_NICE},
141 },
142 [ZCAP_PTRACE] =
143 {
144 1, (pvalue_t[]){CAP_SYS_PTRACE},
145 },
146 [ZCAP_DAC_OVERRIDE] =
147 {
148 1, (pvalue_t[]){CAP_DAC_OVERRIDE},
149 },
150 [ZCAP_READ_SEARCH] =
151 {
152 1, (pvalue_t[]){CAP_DAC_READ_SEARCH},
153 },
154 [ZCAP_SYS_ADMIN] =
155 {
156 1, (pvalue_t[]){CAP_SYS_ADMIN},
157 },
158 [ZCAP_FOWNER] =
159 {
160 1, (pvalue_t[]){CAP_FOWNER},
d62a17ae 161 },
575a2597
DS
162 [ZCAP_IPC_LOCK] =
163 {
164 1, (pvalue_t[]){CAP_IPC_LOCK},
165 },
cae8bc96 166#endif /* HAVE_LCAPS */
01245821 167};
6b0655a2 168
ceacedba 169#ifdef HAVE_LCAPS
170/* Linux forms of capabilities methods */
01245821 171/* convert zebras privileges to system capabilities */
d62a17ae 172static pset_t *zcaps2sys(zebra_capabilities_t *zcaps, int num)
01245821 173{
d62a17ae 174 pset_t *syscaps;
175 int i, j = 0, count = 0;
176
177 if (!num)
178 return NULL;
179
180 /* first count up how many system caps we have */
181 for (i = 0; i < num; i++)
182 count += cap_map[zcaps[i]].num;
183
184 if ((syscaps = XCALLOC(MTYPE_PRIVS, (sizeof(pset_t) * num))) == NULL) {
185 fprintf(stderr, "%s: could not allocate syscaps!", __func__);
186 return NULL;
187 }
188
189 syscaps->caps = XCALLOC(MTYPE_PRIVS, (sizeof(pvalue_t) * count));
190
191 if (!syscaps->caps) {
192 fprintf(stderr, "%s: could not XCALLOC caps!", __func__);
193 return NULL;
194 }
195
196 /* copy the capabilities over */
197 count = 0;
198 for (i = 0; i < num; i++)
199 for (j = 0; j < cap_map[zcaps[i]].num; j++)
200 syscaps->caps[count++] =
201 cap_map[zcaps[i]].system_caps[j];
202
203 /* iterations above should be exact same as previous count, obviously..
204 */
205 syscaps->num = count;
206
207 return syscaps;
01245821 208}
209
210/* set or clear the effective capabilities to/from permitted */
d62a17ae 211int zprivs_change_caps(zebra_privs_ops_t op)
01245821 212{
d62a17ae 213 cap_flag_value_t cflag;
214
215 /* should be no possibility of being called without valid caps */
216 assert(zprivs_state.syscaps_p && zprivs_state.caps);
217 if (!(zprivs_state.syscaps_p && zprivs_state.caps))
218 exit(1);
219
220 if (op == ZPRIVS_RAISE)
221 cflag = CAP_SET;
222 else if (op == ZPRIVS_LOWER)
223 cflag = CAP_CLEAR;
224 else
225 return -1;
226
227 if (!cap_set_flag(zprivs_state.caps, CAP_EFFECTIVE,
228 zprivs_state.syscaps_p->num,
229 zprivs_state.syscaps_p->caps, cflag))
230 return cap_set_proc(zprivs_state.caps);
231 return -1;
01245821 232}
233
d62a17ae 234zebra_privs_current_t zprivs_state_caps(void)
01245821 235{
d62a17ae 236 int i;
237 cap_flag_value_t val;
238
239 /* should be no possibility of being called without valid caps */
240 assert(zprivs_state.syscaps_p && zprivs_state.caps);
241 if (!(zprivs_state.syscaps_p && zprivs_state.caps))
242 exit(1);
243
244 for (i = 0; i < zprivs_state.syscaps_p->num; i++) {
245 if (cap_get_flag(zprivs_state.caps,
246 zprivs_state.syscaps_p->caps[i], CAP_EFFECTIVE,
247 &val)) {
ff245f0e 248 flog_err(
450971aa 249 EC_LIB_SYSTEM_CALL,
d62a17ae 250 "zprivs_state_caps: could not cap_get_flag, %s",
251 safe_strerror(errno));
252 return ZPRIVS_UNKNOWN;
253 }
254 if (val == CAP_SET)
255 return ZPRIVS_RAISED;
256 }
257 return ZPRIVS_LOWERED;
01245821 258}
259
d62a17ae 260static void zprivs_caps_init(struct zebra_privs_t *zprivs)
ceacedba 261{
d62a17ae 262 zprivs_state.syscaps_p = zcaps2sys(zprivs->caps_p, zprivs->cap_num_p);
263 zprivs_state.syscaps_i = zcaps2sys(zprivs->caps_i, zprivs->cap_num_i);
264
265 /* Tell kernel we want caps maintained across uid changes */
266 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1) {
267 fprintf(stderr,
268 "privs_init: could not set PR_SET_KEEPCAPS, %s\n",
269 safe_strerror(errno));
270 exit(1);
271 }
272
273 /* we have caps, we have no need to ever change back the original user
274 */
275 /* only change uid if we don't have the correct one */
276 if ((zprivs_state.zuid) && (zprivs_state.zsuid != zprivs_state.zuid)) {
277 if (setreuid(zprivs_state.zuid, zprivs_state.zuid)) {
278 fprintf(stderr,
279 "zprivs_init (cap): could not setreuid, %s\n",
280 safe_strerror(errno));
281 exit(1);
282 }
283 }
284
285 if (!zprivs_state.syscaps_p)
286 return;
287
288 if (!(zprivs_state.caps = cap_init())) {
289 fprintf(stderr, "privs_init: failed to cap_init, %s\n",
290 safe_strerror(errno));
291 exit(1);
292 }
293
294 if (cap_clear(zprivs_state.caps)) {
295 fprintf(stderr, "privs_init: failed to cap_clear, %s\n",
296 safe_strerror(errno));
297 exit(1);
298 }
299
300 /* set permitted caps */
301 cap_set_flag(zprivs_state.caps, CAP_PERMITTED,
302 zprivs_state.syscaps_p->num, zprivs_state.syscaps_p->caps,
303 CAP_SET);
304
305 /* set inheritable caps, if any */
306 if (zprivs_state.syscaps_i && zprivs_state.syscaps_i->num) {
307 cap_set_flag(zprivs_state.caps, CAP_INHERITABLE,
308 zprivs_state.syscaps_i->num,
309 zprivs_state.syscaps_i->caps, CAP_SET);
310 }
311
312 /* apply caps. CAP_EFFECTIVE is cleared. we'll raise the caps as
313 * and when, and only when, they are needed.
314 */
315 if (cap_set_proc(zprivs_state.caps)) {
316 cap_t current_caps;
317 char *current_caps_text = NULL;
318 char *wanted_caps_text = NULL;
319
320 fprintf(stderr, "privs_init: initial cap_set_proc failed: %s\n",
321 safe_strerror(errno));
322
323 current_caps = cap_get_proc();
324 if (current_caps) {
325 current_caps_text = cap_to_text(current_caps, NULL);
326 cap_free(current_caps);
327 }
328
329 wanted_caps_text = cap_to_text(zprivs_state.caps, NULL);
330 fprintf(stderr, "Wanted caps: %s\n",
331 wanted_caps_text ? wanted_caps_text : "???");
332 fprintf(stderr, "Have caps: %s\n",
333 current_caps_text ? current_caps_text : "???");
334 if (current_caps_text)
335 cap_free(current_caps_text);
336 if (wanted_caps_text)
337 cap_free(wanted_caps_text);
338
339 exit(1);
340 }
341
342 /* set methods for the caller to use */
343 zprivs->change = zprivs_change_caps;
344 zprivs->current_state = zprivs_state_caps;
ceacedba 345}
346
d62a17ae 347static void zprivs_caps_terminate(void)
ceacedba 348{
d617669d 349 /* Clear all capabilities, if we have any. */
d62a17ae 350 if (zprivs_state.caps)
351 cap_clear(zprivs_state.caps);
d617669d
MS
352 else
353 return;
d62a17ae 354
355 /* and boom, capabilities are gone forever */
356 if (cap_set_proc(zprivs_state.caps)) {
357 fprintf(stderr, "privs_terminate: cap_set_proc failed, %s",
358 safe_strerror(errno));
359 exit(1);
360 }
361
362 /* free up private state */
363 if (zprivs_state.syscaps_p->num) {
364 XFREE(MTYPE_PRIVS, zprivs_state.syscaps_p->caps);
365 XFREE(MTYPE_PRIVS, zprivs_state.syscaps_p);
366 }
367
368 if (zprivs_state.syscaps_i && zprivs_state.syscaps_i->num) {
369 XFREE(MTYPE_PRIVS, zprivs_state.syscaps_i->caps);
370 XFREE(MTYPE_PRIVS, zprivs_state.syscaps_i);
371 }
372
373 cap_free(zprivs_state.caps);
ceacedba 374}
cae8bc96
DS
375#else /* !HAVE_LCAPS */
376#error "no Linux capabilities, dazed and confused..."
ceacedba 377#endif /* HAVE_LCAPS */
378#endif /* HAVE_CAPABILITIES */
6b0655a2 379
d62a17ae 380int zprivs_change_uid(zebra_privs_ops_t op)
01245821 381{
d62a17ae 382 if (zprivs_state.zsuid == zprivs_state.zuid)
383 return 0;
384 if (op == ZPRIVS_RAISE)
385 return seteuid(zprivs_state.zsuid);
386 else if (op == ZPRIVS_LOWER)
387 return seteuid(zprivs_state.zuid);
388 else
389 return -1;
01245821 390}
391
d62a17ae 392zebra_privs_current_t zprivs_state_uid(void)
01245821 393{
d62a17ae 394 return ((zprivs_state.zuid == geteuid()) ? ZPRIVS_LOWERED
395 : ZPRIVS_RAISED);
01245821 396}
397
d62a17ae 398int zprivs_change_null(zebra_privs_ops_t op)
01245821 399{
d62a17ae 400 return 0;
01245821 401}
402
d62a17ae 403zebra_privs_current_t zprivs_state_null(void)
01245821 404{
d62a17ae 405 return zprivs_null_state;
01245821 406}
407
4a9ea50e
DL
408#ifndef HAVE_GETGROUPLIST
409/* Solaris 11 has no getgrouplist() */
d62a17ae 410static int getgrouplist(const char *user, gid_t group, gid_t *groups,
411 int *ngroups)
4a9ea50e 412{
d62a17ae 413 struct group *grp;
414 size_t usridx;
415 int pos = 0, ret;
416
417 if (pos < *ngroups)
418 groups[pos] = group;
419 pos++;
420
421 setgrent();
422 while ((grp = getgrent())) {
423 if (grp->gr_gid == group)
424 continue;
425 for (usridx = 0; grp->gr_mem[usridx] != NULL; usridx++)
426 if (!strcmp(grp->gr_mem[usridx], user)) {
427 if (pos < *ngroups)
428 groups[pos] = grp->gr_gid;
429 pos++;
430 break;
431 }
432 }
433 endgrent();
434
435 ret = (pos <= *ngroups) ? pos : -1;
436 *ngroups = pos;
437 return ret;
4a9ea50e
DL
438}
439#endif /* HAVE_GETGROUPLIST */
440
8875d051
MS
441/*
442 * Helper function that locates a refcounting object to use: a process-wide
443 * object or a per-pthread object.
444 */
445static struct zebra_privs_refs_t *get_privs_refs(struct zebra_privs_t *privs)
446{
447 struct zebra_privs_refs_t *temp, *refs = NULL;
448 pthread_t tid;
449
450 if (privs_per_process)
451 refs = &(privs->process_refs);
452 else {
453 /* Locate - or create - the object for the current pthread. */
454 tid = pthread_self();
455
456 STAILQ_FOREACH(temp, &(privs->thread_refs), entry) {
457 if (pthread_equal(temp->tid, tid)) {
458 refs = temp;
459 break;
460 }
461 }
462
463 /* Need to create a new refcounting object. */
464 if (refs == NULL) {
465 refs = XCALLOC(MTYPE_PRIVS,
466 sizeof(struct zebra_privs_refs_t));
467 refs->tid = tid;
468 STAILQ_INSERT_TAIL(&(privs->thread_refs), refs, entry);
469 }
470 }
471
472 return refs;
473}
474
6017c3a2
DL
475struct zebra_privs_t *_zprivs_raise(struct zebra_privs_t *privs,
476 const char *funcname)
477{
478 int save_errno = errno;
8875d051 479 struct zebra_privs_refs_t *refs;
6017c3a2
DL
480
481 if (!privs)
482 return NULL;
483
8875d051
MS
484 /*
485 * Serialize 'raise' operations; particularly important for
486 * OSes where privs are process-wide.
487 */
00dffa8c 488 frr_with_mutex(&(privs->mutex)) {
8875d051
MS
489 /* Locate ref-counting object to use */
490 refs = get_privs_refs(privs);
491
492 if (++(refs->refcount) == 1) {
064e2f32
MS
493 errno = 0;
494 if (privs->change(ZPRIVS_RAISE)) {
495 zlog_err("%s: Failed to raise privileges (%s)",
496 funcname, safe_strerror(errno));
497 }
498 errno = save_errno;
8875d051 499 refs->raised_in_funcname = funcname;
064e2f32 500 }
c5c44d4b 501 }
c5c44d4b 502
6017c3a2
DL
503 return privs;
504}
505
506void _zprivs_lower(struct zebra_privs_t **privs)
507{
508 int save_errno = errno;
8875d051 509 struct zebra_privs_refs_t *refs;
6017c3a2
DL
510
511 if (!*privs)
512 return;
513
8875d051
MS
514 /* Serialize 'lower privs' operation - particularly important
515 * when OS privs are process-wide.
516 */
00dffa8c 517 frr_with_mutex(&(*privs)->mutex) {
8875d051
MS
518 refs = get_privs_refs(*privs);
519
520 if (--(refs->refcount) == 0) {
064e2f32
MS
521 errno = 0;
522 if ((*privs)->change(ZPRIVS_LOWER)) {
523 zlog_err("%s: Failed to lower privileges (%s)",
8875d051 524 refs->raised_in_funcname,
064e2f32
MS
525 safe_strerror(errno));
526 }
527 errno = save_errno;
8875d051 528 refs->raised_in_funcname = NULL;
064e2f32 529 }
c5c44d4b 530 }
c5c44d4b 531
6017c3a2
DL
532 *privs = NULL;
533}
534
37a1f2fb 535void zprivs_preinit(struct zebra_privs_t *zprivs)
01245821 536{
d62a17ae 537 struct passwd *pwentry = NULL;
538 struct group *grentry = NULL;
d62a17ae 539
540 if (!zprivs) {
541 fprintf(stderr, "zprivs_init: called with NULL arg!\n");
542 exit(1);
543 }
544
c5c44d4b 545 pthread_mutex_init(&(zprivs->mutex), NULL);
8875d051
MS
546 zprivs->process_refs.refcount = 0;
547 zprivs->process_refs.raised_in_funcname = NULL;
548 STAILQ_INIT(&zprivs->thread_refs);
c5c44d4b 549
d62a17ae 550 if (zprivs->vty_group) {
551 /* in a "NULL" setup, this is allowed to fail too, but still
552 * try. */
553 if ((grentry = getgrnam(zprivs->vty_group)))
554 zprivs_state.vtygrp = grentry->gr_gid;
555 else
556 zprivs_state.vtygrp = (gid_t)-1;
557 }
558
559 /* NULL privs */
560 if (!(zprivs->user || zprivs->group || zprivs->cap_num_p
561 || zprivs->cap_num_i)) {
562 zprivs->change = zprivs_change_null;
563 zprivs->current_state = zprivs_state_null;
564 return;
565 }
566
567 if (zprivs->user) {
568 if ((pwentry = getpwnam(zprivs->user)) == NULL) {
569 /* cant use log.h here as it depends on vty */
570 fprintf(stderr,
571 "privs_init: could not lookup user %s\n",
572 zprivs->user);
573 exit(1);
574 }
575
576 zprivs_state.zuid = pwentry->pw_uid;
577 zprivs_state.zgid = pwentry->pw_gid;
578 }
579
580 grentry = NULL;
581
582 if (zprivs->group) {
583 if ((grentry = getgrnam(zprivs->group)) == NULL) {
584 fprintf(stderr,
585 "privs_init: could not lookup group %s\n",
586 zprivs->group);
587 exit(1);
588 }
589
590 zprivs_state.zgid = grentry->gr_gid;
591 }
37a1f2fb
DL
592}
593
877057b6
DL
594struct zebra_privs_t *lib_privs;
595
37a1f2fb
DL
596void zprivs_init(struct zebra_privs_t *zprivs)
597{
1c77d034 598 gid_t groups[NGROUPS_MAX] = {};
37a1f2fb
DL
599 int i, ngroups = 0;
600 int found = 0;
601
602 /* NULL privs */
603 if (!(zprivs->user || zprivs->group || zprivs->cap_num_p
604 || zprivs->cap_num_i))
605 return;
d62a17ae 606
877057b6
DL
607 lib_privs = zprivs;
608
d62a17ae 609 if (zprivs->user) {
72de5f4b 610 ngroups = array_size(groups);
d62a17ae 611 if (getgrouplist(zprivs->user, zprivs_state.zgid, groups,
612 &ngroups)
613 < 0) {
614 /* cant use log.h here as it depends on vty */
615 fprintf(stderr,
616 "privs_init: could not getgrouplist for user %s\n",
617 zprivs->user);
618 exit(1);
619 }
620 }
621
622 if (zprivs->vty_group)
623 /* Add the vty_group to the supplementary groups so it can be chowned to
9d303b37 624 */
d62a17ae 625 {
626 if (zprivs_state.vtygrp == (gid_t)-1) {
627 fprintf(stderr,
628 "privs_init: could not lookup vty group %s\n",
629 zprivs->vty_group);
630 exit(1);
631 }
632
633 for (i = 0; i < ngroups; i++)
634 if (groups[i] == zprivs_state.vtygrp) {
635 found++;
636 break;
637 }
638
639 if (!found) {
640 fprintf(stderr,
641 "privs_init: user(%s) is not part of vty group specified(%s)\n",
642 zprivs->user, zprivs->vty_group);
643 exit(1);
644 }
7e3a1ec7 645 if (i >= ngroups && ngroups < (int)array_size(groups)) {
d62a17ae 646 groups[i] = zprivs_state.vtygrp;
647 }
648 }
649
650 zprivs_state.zsuid = geteuid(); /* initial uid */
651 /* add groups only if we changed uid - otherwise skip */
652 if ((ngroups) && (zprivs_state.zsuid != zprivs_state.zuid)) {
653 if (setgroups(ngroups, groups)) {
654 fprintf(stderr, "privs_init: could not setgroups, %s\n",
655 safe_strerror(errno));
656 exit(1);
657 }
658 }
659
660 /* change gid only if we changed uid - otherwise skip */
661 if ((zprivs_state.zgid) && (zprivs_state.zsuid != zprivs_state.zuid)) {
662 /* change group now, forever. uid we do later */
663 if (setregid(zprivs_state.zgid, zprivs_state.zgid)) {
664 fprintf(stderr, "zprivs_init: could not setregid, %s\n",
665 safe_strerror(errno));
666 exit(1);
667 }
668 }
669
ceacedba 670#ifdef HAVE_CAPABILITIES
d62a17ae 671 zprivs_caps_init(zprivs);
8e04538c
DS
672
673 /*
674 * If we have initialized the system with no requested
675 * capabilities, change will not have been set
676 * to anything by zprivs_caps_init, As such
677 * we should make sure that when we attempt
678 * to raize privileges that we actually have
679 * a do nothing function to call instead of a
680 * crash :).
681 */
682 if (!zprivs->change)
683 zprivs->change = zprivs_change_null;
684
d62a17ae 685#else /* !HAVE_CAPABILITIES */
686 /* we dont have caps. we'll need to maintain rid and saved uid
687 * and change euid back to saved uid (who we presume has all neccessary
688 * privileges) whenever we are asked to raise our privileges.
689 *
690 * This is not worth that much security wise, but all we can do.
691 */
692 zprivs_state.zsuid = geteuid();
693 /* only change uid if we don't have the correct one */
694 if ((zprivs_state.zuid) && (zprivs_state.zsuid != zprivs_state.zuid)) {
695 if (setreuid(-1, zprivs_state.zuid)) {
696 fprintf(stderr,
697 "privs_init (uid): could not setreuid, %s\n",
698 safe_strerror(errno));
699 exit(1);
700 }
701 }
702
703 zprivs->change = zprivs_change_uid;
704 zprivs->current_state = zprivs_state_uid;
ceacedba 705#endif /* HAVE_CAPABILITIES */
01245821 706}
707
d62a17ae 708void zprivs_terminate(struct zebra_privs_t *zprivs)
01245821 709{
8875d051
MS
710 struct zebra_privs_refs_t *refs;
711
877057b6
DL
712 lib_privs = NULL;
713
d62a17ae 714 if (!zprivs) {
715 fprintf(stderr, "%s: no privs struct given, terminating",
716 __func__);
717 exit(0);
718 }
719
ceacedba 720#ifdef HAVE_CAPABILITIES
4093d47b
DL
721 if (zprivs->user || zprivs->group || zprivs->cap_num_p
722 || zprivs->cap_num_i)
723 zprivs_caps_terminate();
d62a17ae 724#else /* !HAVE_CAPABILITIES */
725 /* only change uid if we don't have the correct one */
726 if ((zprivs_state.zuid) && (zprivs_state.zsuid != zprivs_state.zuid)) {
727 if (setreuid(zprivs_state.zuid, zprivs_state.zuid)) {
728 fprintf(stderr,
729 "privs_terminate: could not setreuid, %s",
730 safe_strerror(errno));
731 exit(1);
732 }
733 }
01245821 734#endif /* HAVE_LCAPS */
ceacedba 735
8875d051
MS
736 while ((refs = STAILQ_FIRST(&(zprivs->thread_refs))) != NULL) {
737 STAILQ_REMOVE_HEAD(&(zprivs->thread_refs), entry);
738 XFREE(MTYPE_PRIVS, refs);
739 }
740
d62a17ae 741 zprivs->change = zprivs_change_null;
742 zprivs->current_state = zprivs_state_null;
743 zprivs_null_state = ZPRIVS_LOWERED;
744 return;
01245821 745}
ba3a0bc5 746
d62a17ae 747void zprivs_get_ids(struct zprivs_ids_t *ids)
ba3a0bc5 748{
749
d62a17ae 750 ids->uid_priv = getuid();
751 (zprivs_state.zuid) ? (ids->uid_normal = zprivs_state.zuid)
e117b7c5 752 : (ids->uid_normal = (uid_t)-1);
d62a17ae 753 (zprivs_state.zgid) ? (ids->gid_normal = zprivs_state.zgid)
e117b7c5 754 : (ids->gid_normal = (uid_t)-1);
d62a17ae 755 (zprivs_state.vtygrp) ? (ids->gid_vty = zprivs_state.vtygrp)
e117b7c5 756 : (ids->gid_vty = (uid_t)-1);
d62a17ae 757
758 return;
ba3a0bc5 759}