privs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * Zebra privileges.
  3. *
  4. * Copyright (C) 2003 Paul Jakma.
  5. * Copyright (C) 2005 Sun Microsystems, Inc.
  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. *
  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. */
  24. #include <zebra.h>
  25. #include "log.h"
  26. #include "privs.h"
  27. #include "memory.h"
  28. #ifdef HAVE_CAPABILITIES
  29. /* sort out some generic internal types for:
  30. *
  31. * privilege values (cap_value_t, priv_t) -> pvalue_t
  32. * privilege set (..., priv_set_t) -> pset_t
  33. * privilege working storage (cap_t, ...) -> pstorage_t
  34. *
  35. * values we think of as numeric (they're ints really, but we dont know)
  36. * sets are mostly opaque, to hold a set of privileges, related in some way.
  37. * storage binds together a set of sets we're interested in.
  38. * (in reality: cap_value_t and priv_t are ints)
  39. */
  40. #ifdef HAVE_LCAPS
  41. /* Linux doesn't have a 'set' type: a set of related privileges */
  42. struct _pset {
  43. int num;
  44. cap_value_t *caps;
  45. };
  46. typedef cap_value_t pvalue_t;
  47. typedef struct _pset pset_t;
  48. typedef cap_t pstorage_t;
  49. #elif defined (HAVE_SOLARIS_CAPABILITIES)
  50. typedef priv_t pvalue_t;
  51. typedef priv_set_t pset_t;
  52. typedef priv_set_t *pstorage_t;
  53. #else /* neither LCAPS nor SOLARIS_CAPABILITIES */
  54. #error "HAVE_CAPABILITIES defined, but neither LCAPS nor Solaris Capabilties!"
  55. #endif /* HAVE_LCAPS */
  56. #endif /* HAVE_CAPABILITIES */
  57. /* the default NULL state we report is RAISED, but could be LOWERED if
  58. * zprivs_terminate is called and the NULL handler is installed.
  59. */
  60. static zebra_privs_current_t zprivs_null_state = ZPRIVS_RAISED;
  61. /* internal privileges state */
  62. static struct _zprivs_t
  63. {
  64. #ifdef HAVE_CAPABILITIES
  65. pstorage_t caps; /* working storage */
  66. pset_t *syscaps_p; /* system-type requested permitted caps */
  67. pset_t *syscaps_i; /* system-type requested inheritable caps */
  68. #endif /* HAVE_CAPABILITIES */
  69. uid_t zuid, /* uid to run as */
  70. zsuid; /* saved uid */
  71. gid_t zgid; /* gid to run as */
  72. gid_t vtygrp; /* gid for vty sockets */
  73. } zprivs_state;
  74. /* externally exported but not directly accessed functions */
  75. #ifdef HAVE_CAPABILITIES
  76. int zprivs_change_caps (zebra_privs_ops_t);
  77. zebra_privs_current_t zprivs_state_caps (void);
  78. #endif /* HAVE_CAPABILITIES */
  79. int zprivs_change_uid (zebra_privs_ops_t);
  80. zebra_privs_current_t zprivs_state_uid (void);
  81. int zprivs_change_null (zebra_privs_ops_t);
  82. zebra_privs_current_t zprivs_state_null (void);
  83. #ifdef HAVE_CAPABILITIES
  84. /* internal capability API */
  85. static pset_t *zcaps2sys (zebra_capabilities_t *, int);
  86. static void zprivs_caps_init (struct zebra_privs_t *);
  87. static void zprivs_caps_terminate (void);
  88. /* Map of Quagga abstract capabilities to system capabilities */
  89. static struct
  90. {
  91. int num;
  92. pvalue_t *system_caps;
  93. } cap_map [ZCAP_MAX] =
  94. {
  95. #ifdef HAVE_LCAPS /* Quagga -> Linux capabilities mappings */
  96. [ZCAP_SETID] = { 2, (pvalue_t []) { CAP_SETGID,
  97. CAP_SETUID }, },
  98. [ZCAP_BIND] = { 2, (pvalue_t []) { CAP_NET_BIND_SERVICE,
  99. CAP_NET_BROADCAST }, },
  100. [ZCAP_NET_ADMIN] = { 1, (pvalue_t []) { CAP_NET_ADMIN }, },
  101. [ZCAP_NET_RAW] = { 1, (pvalue_t []) { CAP_NET_RAW }, },
  102. [ZCAP_CHROOT] = { 1, (pvalue_t []) { CAP_SYS_CHROOT, }, },
  103. [ZCAP_NICE] = { 1, (pvalue_t []) { CAP_SYS_NICE }, },
  104. [ZCAP_PTRACE] = { 1, (pvalue_t []) { CAP_SYS_PTRACE }, },
  105. [ZCAP_DAC_OVERRIDE] = { 1, (pvalue_t []) { CAP_DAC_OVERRIDE }, },
  106. [ZCAP_READ_SEARCH] = { 1, (pvalue_t []) { CAP_DAC_READ_SEARCH }, },
  107. [ZCAP_SYS_ADMIN] = { 1, (pvalue_t []) { CAP_SYS_ADMIN }, },
  108. [ZCAP_FOWNER] = { 1, (pvalue_t []) { CAP_FOWNER }, },
  109. #elif defined(HAVE_SOLARIS_CAPABILITIES) /* HAVE_LCAPS */
  110. /* Quagga -> Solaris privilege mappings */
  111. [ZCAP_SETID] = { 1, (pvalue_t []) { PRIV_PROC_SETID }, },
  112. [ZCAP_BIND] = { 1, (pvalue_t []) { PRIV_NET_PRIVADDR }, },
  113. /* IP_CONFIG is a subset of NET_CONFIG and is allowed in zones */
  114. #ifdef PRIV_SYS_IP_CONFIG
  115. [ZCAP_NET_ADMIN] = { 1, (pvalue_t []) { PRIV_SYS_IP_CONFIG }, },
  116. #else
  117. [ZCAP_NET_ADMIN] = { 1, (pvalue_t []) { PRIV_SYS_NET_CONFIG }, },
  118. #endif
  119. [ZCAP_NET_RAW] = { 2, (pvalue_t []) { PRIV_NET_RAWACCESS,
  120. PRIV_NET_ICMPACCESS }, },
  121. [ZCAP_CHROOT] = { 1, (pvalue_t []) { PRIV_PROC_CHROOT }, },
  122. [ZCAP_NICE] = { 1, (pvalue_t []) { PRIV_PROC_PRIOCNTL }, },
  123. [ZCAP_PTRACE] = { 1, (pvalue_t []) { PRIV_PROC_SESSION }, },
  124. [ZCAP_DAC_OVERRIDE] = { 2, (pvalue_t []) { PRIV_FILE_DAC_EXECUTE,
  125. PRIV_FILE_DAC_READ,
  126. PRIV_FILE_DAC_SEARCH,
  127. PRIV_FILE_DAC_WRITE,
  128. PRIV_FILE_DAC_SEARCH }, },
  129. [ZCAP_READ_SEARCH] = { 2, (pvalue_t []) { PRIV_FILE_DAC_SEARCH,
  130. PRIV_FILE_DAC_READ }, },
  131. [ZCAP_SYS_ADMIN] = { 1, (pvalue_t []) { PRIV_SYS_ADMIN }, },
  132. [ZCAP_FOWNER] = { 1, (pvalue_t []) { PRIV_FILE_OWNER }, },
  133. #endif /* HAVE_SOLARIS_CAPABILITIES */
  134. };
  135. #ifdef HAVE_LCAPS
  136. /* Linux forms of capabilities methods */
  137. /* convert zebras privileges to system capabilities */
  138. static pset_t *
  139. zcaps2sys (zebra_capabilities_t *zcaps, int num)
  140. {
  141. pset_t *syscaps;
  142. int i, j = 0, count = 0;
  143. if (!num)
  144. return NULL;
  145. /* first count up how many system caps we have */
  146. for (i= 0; i < num; i++)
  147. count += cap_map[zcaps[i]].num;
  148. if ( (syscaps = XCALLOC (MTYPE_PRIVS, (sizeof(pset_t) * num))) == NULL)
  149. {
  150. fprintf (stderr, "%s: could not allocate syscaps!", __func__);
  151. return NULL;
  152. }
  153. syscaps->caps = XCALLOC (MTYPE_PRIVS, (sizeof (pvalue_t) * count));
  154. if (!syscaps->caps)
  155. {
  156. fprintf (stderr, "%s: could not XCALLOC caps!", __func__);
  157. return NULL;
  158. }
  159. /* copy the capabilities over */
  160. count = 0;
  161. for (i=0; i < num; i++)
  162. for (j = 0; j < cap_map[zcaps[i]].num; j++)
  163. syscaps->caps[count++] = cap_map[zcaps[i]].system_caps[j];
  164. /* iterations above should be exact same as previous count, obviously.. */
  165. syscaps->num = count;
  166. return syscaps;
  167. }
  168. /* set or clear the effective capabilities to/from permitted */
  169. int
  170. zprivs_change_caps (zebra_privs_ops_t op)
  171. {
  172. cap_flag_value_t cflag;
  173. /* should be no possibility of being called without valid caps */
  174. assert (zprivs_state.syscaps_p && zprivs_state.caps);
  175. if (! (zprivs_state.syscaps_p && zprivs_state.caps))
  176. exit (1);
  177. if (op == ZPRIVS_RAISE)
  178. cflag = CAP_SET;
  179. else if (op == ZPRIVS_LOWER)
  180. cflag = CAP_CLEAR;
  181. else
  182. return -1;
  183. if ( !cap_set_flag (zprivs_state.caps, CAP_EFFECTIVE,
  184. zprivs_state.syscaps_p->num,
  185. zprivs_state.syscaps_p->caps,
  186. cflag))
  187. return cap_set_proc (zprivs_state.caps);
  188. return -1;
  189. }
  190. zebra_privs_current_t
  191. zprivs_state_caps (void)
  192. {
  193. int i;
  194. cap_flag_value_t val;
  195. /* should be no possibility of being called without valid caps */
  196. assert (zprivs_state.syscaps_p && zprivs_state.caps);
  197. if (! (zprivs_state.syscaps_p && zprivs_state.caps))
  198. exit (1);
  199. for (i=0; i < zprivs_state.syscaps_p->num; i++)
  200. {
  201. if ( cap_get_flag (zprivs_state.caps, zprivs_state.syscaps_p->caps[i],
  202. CAP_EFFECTIVE, &val) )
  203. {
  204. zlog_warn ("zprivs_state_caps: could not cap_get_flag, %s",
  205. safe_strerror (errno) );
  206. return ZPRIVS_UNKNOWN;
  207. }
  208. if (val == CAP_SET)
  209. return ZPRIVS_RAISED;
  210. }
  211. return ZPRIVS_LOWERED;
  212. }
  213. static void
  214. zprivs_caps_init (struct zebra_privs_t *zprivs)
  215. {
  216. zprivs_state.syscaps_p = zcaps2sys (zprivs->caps_p, zprivs->cap_num_p);
  217. zprivs_state.syscaps_i = zcaps2sys (zprivs->caps_i, zprivs->cap_num_i);
  218. /* Tell kernel we want caps maintained across uid changes */
  219. if ( prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1 )
  220. {
  221. fprintf (stderr, "privs_init: could not set PR_SET_KEEPCAPS, %s\n",
  222. safe_strerror (errno) );
  223. exit(1);
  224. }
  225. if ( !zprivs_state.syscaps_p )
  226. {
  227. fprintf (stderr, "privs_init: capabilities enabled, "
  228. "but no capabilities supplied\n");
  229. }
  230. /* we have caps, we have no need to ever change back the original user */
  231. if (zprivs_state.zuid)
  232. {
  233. if ( setreuid (zprivs_state.zuid, zprivs_state.zuid) )
  234. {
  235. fprintf (stderr, "zprivs_init (cap): could not setreuid, %s\n",
  236. safe_strerror (errno));
  237. exit (1);
  238. }
  239. }
  240. if ( !(zprivs_state.caps = cap_init()) )
  241. {
  242. fprintf (stderr, "privs_init: failed to cap_init, %s\n",
  243. safe_strerror (errno));
  244. exit (1);
  245. }
  246. if ( cap_clear (zprivs_state.caps) )
  247. {
  248. fprintf (stderr, "privs_init: failed to cap_clear, %s\n",
  249. safe_strerror (errno));
  250. exit (1);
  251. }
  252. /* set permitted caps */
  253. cap_set_flag(zprivs_state.caps, CAP_PERMITTED,
  254. zprivs_state.syscaps_p->num,
  255. zprivs_state.syscaps_p->caps,
  256. CAP_SET);
  257. /* set inheritable caps, if any */
  258. if (zprivs_state.syscaps_i && zprivs_state.syscaps_i->num)
  259. {
  260. cap_set_flag(zprivs_state.caps, CAP_INHERITABLE,
  261. zprivs_state.syscaps_i->num,
  262. zprivs_state.syscaps_i->caps,
  263. CAP_SET);
  264. }
  265. /* apply caps. CAP_EFFECTIVE is cleared. we'll raise the caps as
  266. * and when, and only when, they are needed.
  267. */
  268. if ( cap_set_proc (zprivs_state.caps) )
  269. {
  270. fprintf (stderr, "privs_init: initial cap_set_proc failed\n");
  271. exit (1);
  272. }
  273. /* set methods for the caller to use */
  274. zprivs->change = zprivs_change_caps;
  275. zprivs->current_state = zprivs_state_caps;
  276. }
  277. static void
  278. zprivs_caps_terminate (void)
  279. {
  280. /* clear all capabilities */
  281. if (zprivs_state.caps)
  282. cap_clear (zprivs_state.caps);
  283. /* and boom, capabilities are gone forever */
  284. if ( cap_set_proc (zprivs_state.caps) )
  285. {
  286. fprintf (stderr, "privs_terminate: cap_set_proc failed, %s",
  287. safe_strerror (errno) );
  288. exit (1);
  289. }
  290. /* free up private state */
  291. if (zprivs_state.syscaps_p->num)
  292. {
  293. XFREE (MTYPE_PRIVS, zprivs_state.syscaps_p->caps);
  294. XFREE (MTYPE_PRIVS, zprivs_state.syscaps_p);
  295. }
  296. if (zprivs_state.syscaps_i && zprivs_state.syscaps_i->num)
  297. {
  298. XFREE (MTYPE_PRIVS, zprivs_state.syscaps_i->caps);
  299. XFREE (MTYPE_PRIVS, zprivs_state.syscaps_i);
  300. }
  301. cap_free (zprivs_state.caps);
  302. }
  303. #elif defined (HAVE_SOLARIS_CAPABILITIES) /* !HAVE_LCAPS */
  304. /* Solaris specific capability/privilege methods
  305. *
  306. * Resources:
  307. * - the 'privileges' man page
  308. * - http://cvs.opensolaris.org
  309. * - http://blogs.sun.com/roller/page/gbrunett?entry=privilege_enabling_set_id_programs1
  310. */
  311. /* convert zebras privileges to system capabilities */
  312. static pset_t *
  313. zcaps2sys (zebra_capabilities_t *zcaps, int num)
  314. {
  315. pset_t *syscaps;
  316. int i, j = 0;
  317. if ((syscaps = priv_allocset()) == NULL)
  318. {
  319. fprintf (stderr, "%s: could not allocate syscaps!\n", __func__);
  320. exit (1);
  321. }
  322. priv_emptyset (syscaps);
  323. for (i=0; i < num; i++)
  324. for (j = 0; j < cap_map[zcaps[i]].num; j++)
  325. priv_addset (syscaps, cap_map[zcaps[i]].system_caps[j]);
  326. return syscaps;
  327. }
  328. /* callback exported to users to RAISE and LOWER effective privileges
  329. * from nothing to the given permitted set and back down
  330. */
  331. int
  332. zprivs_change_caps (zebra_privs_ops_t op)
  333. {
  334. /* should be no possibility of being called without valid caps */
  335. assert (zprivs_state.syscaps_p);
  336. if (!zprivs_state.syscaps_p)
  337. {
  338. fprintf (stderr, "%s: Eek, missing caps!", __func__);
  339. exit (1);
  340. }
  341. /* to raise: copy original permitted into our working effective set
  342. * to lower: just clear the working effective set
  343. */
  344. if (op == ZPRIVS_RAISE)
  345. priv_copyset (zprivs_state.syscaps_p, zprivs_state.caps);
  346. else if (op == ZPRIVS_LOWER)
  347. priv_emptyset (zprivs_state.caps);
  348. else
  349. return -1;
  350. if (setppriv (PRIV_SET, PRIV_EFFECTIVE, zprivs_state.caps) != 0)
  351. return -1;
  352. return 0;
  353. }
  354. /* Retrieve current privilege state, is it RAISED or LOWERED? */
  355. zebra_privs_current_t
  356. zprivs_state_caps (void)
  357. {
  358. zebra_privs_current_t result;
  359. pset_t *effective;
  360. if ( (effective = priv_allocset()) == NULL)
  361. {
  362. fprintf (stderr, "%s: failed to get priv_allocset! %s\n", __func__,
  363. safe_strerror (errno));
  364. return ZPRIVS_UNKNOWN;
  365. }
  366. if (getppriv (PRIV_EFFECTIVE, effective))
  367. {
  368. fprintf (stderr, "%s: failed to get state! %s\n", __func__,
  369. safe_strerror (errno));
  370. result = ZPRIVS_UNKNOWN;
  371. }
  372. else
  373. {
  374. if (priv_isemptyset (effective) == B_TRUE)
  375. result = ZPRIVS_LOWERED;
  376. else
  377. result = ZPRIVS_RAISED;
  378. }
  379. if (effective)
  380. priv_freeset (effective);
  381. return result;
  382. }
  383. static void
  384. zprivs_caps_init (struct zebra_privs_t *zprivs)
  385. {
  386. pset_t *basic;
  387. pset_t *empty;
  388. /* the specified sets */
  389. zprivs_state.syscaps_p = zcaps2sys (zprivs->caps_p, zprivs->cap_num_p);
  390. zprivs_state.syscaps_i = zcaps2sys (zprivs->caps_i, zprivs->cap_num_i);
  391. /* nonsensical to have gotten here but not have capabilities */
  392. if (!zprivs_state.syscaps_p)
  393. {
  394. fprintf (stderr, "%s: capabilities enabled, "
  395. "but no valid capabilities supplied\n",
  396. __func__);
  397. }
  398. /* We retain the basic set in our permitted set, as Linux has no
  399. * equivalent. The basic set on Linux hence is implicit, always
  400. * there.
  401. */
  402. if ((basic = priv_str_to_set("basic", ",", NULL)) == NULL)
  403. {
  404. fprintf (stderr, "%s: couldn't get basic set!\n", __func__);
  405. exit (1);
  406. }
  407. /* Add the basic set to the permitted set */
  408. priv_union (basic, zprivs_state.syscaps_p);
  409. priv_freeset (basic);
  410. /* we need an empty set for 'effective', potentially for inheritable too */
  411. if ( (empty = priv_allocset()) == NULL)
  412. {
  413. fprintf (stderr, "%s: couldn't get empty set!\n", __func__);
  414. exit (1);
  415. }
  416. priv_emptyset (empty);
  417. /* Hey kernel, we know about privileges!
  418. * this isn't strictly required, use of setppriv should have same effect
  419. */
  420. if (setpflags (PRIV_AWARE, 1))
  421. {
  422. fprintf (stderr, "%s: error setting PRIV_AWARE!, %s\n", __func__,
  423. safe_strerror (errno) );
  424. exit (1);
  425. }
  426. /* need either valid or empty sets for both p and i.. */
  427. assert (zprivs_state.syscaps_i && zprivs_state.syscaps_p);
  428. /* we have caps, we have no need to ever change back the original user
  429. * change real, effective and saved to the specified user.
  430. */
  431. if (zprivs_state.zuid)
  432. {
  433. if ( setreuid (zprivs_state.zuid, zprivs_state.zuid) )
  434. {
  435. fprintf (stderr, "%s: could not setreuid, %s\n",
  436. __func__, safe_strerror (errno));
  437. exit (1);
  438. }
  439. }
  440. /* set the permitted set */
  441. if (setppriv (PRIV_SET, PRIV_PERMITTED, zprivs_state.syscaps_p))
  442. {
  443. fprintf (stderr, "%s: error setting permitted set!, %s\n", __func__,
  444. safe_strerror (errno) );
  445. exit (1);
  446. }
  447. /* set the inheritable set */
  448. if (setppriv (PRIV_SET, PRIV_INHERITABLE, zprivs_state.syscaps_i))
  449. {
  450. fprintf (stderr, "%s: error setting inheritable set!, %s\n", __func__,
  451. safe_strerror (errno) );
  452. exit (1);
  453. }
  454. /* now clear the effective set and we're ready to go */
  455. if (setppriv (PRIV_SET, PRIV_EFFECTIVE, empty))
  456. {
  457. fprintf (stderr, "%s: error setting effective set!, %s\n", __func__,
  458. safe_strerror (errno) );
  459. exit (1);
  460. }
  461. /* we'll use this as our working-storage privset */
  462. zprivs_state.caps = empty;
  463. /* set methods for the caller to use */
  464. zprivs->change = zprivs_change_caps;
  465. zprivs->current_state = zprivs_state_caps;
  466. }
  467. static void
  468. zprivs_caps_terminate (void)
  469. {
  470. assert (zprivs_state.caps);
  471. /* clear all capabilities */
  472. priv_emptyset (zprivs_state.caps);
  473. setppriv (PRIV_SET, PRIV_EFFECTIVE, zprivs_state.caps);
  474. setppriv (PRIV_SET, PRIV_PERMITTED, zprivs_state.caps);
  475. setppriv (PRIV_SET, PRIV_INHERITABLE, zprivs_state.caps);
  476. /* free up private state */
  477. if (zprivs_state.syscaps_p)
  478. priv_freeset (zprivs_state.syscaps_p);
  479. if (zprivs_state.syscaps_i)
  480. priv_freeset (zprivs_state.syscaps_i);
  481. priv_freeset (zprivs_state.caps);
  482. }
  483. #else /* !HAVE_LCAPS && ! HAVE_SOLARIS_CAPABILITIES */
  484. #error "Neither Solaris nor Linux capabilities, dazed and confused..."
  485. #endif /* HAVE_LCAPS */
  486. #endif /* HAVE_CAPABILITIES */
  487. int
  488. zprivs_change_uid (zebra_privs_ops_t op)
  489. {
  490. if (op == ZPRIVS_RAISE)
  491. return seteuid (zprivs_state.zsuid);
  492. else if (op == ZPRIVS_LOWER)
  493. return seteuid (zprivs_state.zuid);
  494. else
  495. return -1;
  496. }
  497. zebra_privs_current_t
  498. zprivs_state_uid (void)
  499. {
  500. return ( (zprivs_state.zuid == geteuid()) ? ZPRIVS_LOWERED : ZPRIVS_RAISED);
  501. }
  502. int
  503. zprivs_change_null (zebra_privs_ops_t op)
  504. {
  505. return 0;
  506. }
  507. zebra_privs_current_t
  508. zprivs_state_null (void)
  509. {
  510. return zprivs_null_state;
  511. }
  512. void
  513. zprivs_init(struct zebra_privs_t *zprivs)
  514. {
  515. struct passwd *pwentry = NULL;
  516. struct group *grentry = NULL;
  517. if (!zprivs)
  518. {
  519. fprintf (stderr, "zprivs_init: called with NULL arg!\n");
  520. exit (1);
  521. }
  522. /* NULL privs */
  523. if (! (zprivs->user || zprivs->group
  524. || zprivs->cap_num_p || zprivs->cap_num_i) )
  525. {
  526. zprivs->change = zprivs_change_null;
  527. zprivs->current_state = zprivs_state_null;
  528. return;
  529. }
  530. if (zprivs->user)
  531. {
  532. if ( (pwentry = getpwnam (zprivs->user)) )
  533. {
  534. zprivs_state.zuid = pwentry->pw_uid;
  535. }
  536. else
  537. {
  538. /* cant use log.h here as it depends on vty */
  539. fprintf (stderr, "privs_init: could not lookup user %s\n",
  540. zprivs->user);
  541. exit (1);
  542. }
  543. }
  544. grentry = NULL;
  545. if (zprivs->vty_group)
  546. /* Add the vty_group to the supplementary groups so it can be chowned to */
  547. {
  548. if ( (grentry = getgrnam (zprivs->vty_group)) )
  549. {
  550. zprivs_state.vtygrp = grentry->gr_gid;
  551. if ( setgroups (1, &zprivs_state.vtygrp) )
  552. {
  553. fprintf (stderr, "privs_init: could not setgroups, %s\n",
  554. safe_strerror (errno) );
  555. exit (1);
  556. }
  557. }
  558. else
  559. {
  560. fprintf (stderr, "privs_init: could not lookup vty group %s\n",
  561. zprivs->vty_group);
  562. exit (1);
  563. }
  564. }
  565. if (zprivs->group)
  566. {
  567. if ( (grentry = getgrnam (zprivs->group)) )
  568. {
  569. zprivs_state.zgid = grentry->gr_gid;
  570. }
  571. else
  572. {
  573. fprintf (stderr, "privs_init: could not lookup group %s\n",
  574. zprivs->group);
  575. exit (1);
  576. }
  577. /* change group now, forever. uid we do later */
  578. if ( setregid (zprivs_state.zgid, zprivs_state.zgid) )
  579. {
  580. fprintf (stderr, "zprivs_init: could not setregid, %s\n",
  581. safe_strerror (errno) );
  582. exit (1);
  583. }
  584. }
  585. #ifdef HAVE_CAPABILITIES
  586. zprivs_caps_init (zprivs);
  587. #else /* !HAVE_CAPABILITIES */
  588. /* we dont have caps. we'll need to maintain rid and saved uid
  589. * and change euid back to saved uid (who we presume has all neccessary
  590. * privileges) whenever we are asked to raise our privileges.
  591. *
  592. * This is not worth that much security wise, but all we can do.
  593. */
  594. zprivs_state.zsuid = geteuid();
  595. if ( zprivs_state.zuid )
  596. {
  597. if ( setreuid (-1, zprivs_state.zuid) )
  598. {
  599. fprintf (stderr, "privs_init (uid): could not setreuid, %s\n",
  600. safe_strerror (errno));
  601. exit (1);
  602. }
  603. }
  604. zprivs->change = zprivs_change_uid;
  605. zprivs->current_state = zprivs_state_uid;
  606. #endif /* HAVE_CAPABILITIES */
  607. }
  608. void
  609. zprivs_terminate (struct zebra_privs_t *zprivs)
  610. {
  611. if (!zprivs)
  612. {
  613. fprintf (stderr, "%s: no privs struct given, terminating", __func__);
  614. exit (0);
  615. }
  616. #ifdef HAVE_CAPABILITIES
  617. zprivs_caps_terminate();
  618. #else /* !HAVE_CAPABILITIES */
  619. if (zprivs_state.zuid)
  620. {
  621. if ( setreuid (zprivs_state.zuid, zprivs_state.zuid) )
  622. {
  623. fprintf (stderr, "privs_terminate: could not setreuid, %s",
  624. safe_strerror (errno) );
  625. exit (1);
  626. }
  627. }
  628. #endif /* HAVE_LCAPS */
  629. zprivs->change = zprivs_change_null;
  630. zprivs->current_state = zprivs_state_null;
  631. zprivs_null_state = ZPRIVS_LOWERED;
  632. return;
  633. }
  634. void
  635. zprivs_get_ids(struct zprivs_ids_t *ids)
  636. {
  637. ids->uid_priv = getuid();
  638. (zprivs_state.zuid) ? (ids->uid_normal = zprivs_state.zuid)
  639. : (ids->uid_normal = -1);
  640. (zprivs_state.zgid) ? (ids->gid_normal = zprivs_state.zgid)
  641. : (ids->gid_normal = -1);
  642. (zprivs_state.vtygrp) ? (ids->gid_vty = zprivs_state.vtygrp)
  643. : (ids->gid_vty = -1);
  644. return;
  645. }