test-privs.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * $Id: test-privs.c,v 1.1 2005/10/11 03:48:28 paul Exp $
  3. *
  4. * This file is part of Quagga.
  5. *
  6. * Quagga is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * Quagga is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Quagga; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include <lib/version.h>
  23. #include "getopt.h"
  24. #include "privs.h"
  25. #include "memory.h"
  26. zebra_capabilities_t _caps_p [] =
  27. {
  28. ZCAP_NET_RAW,
  29. ZCAP_BIND,
  30. ZCAP_NET_ADMIN,
  31. ZCAP_DAC_OVERRIDE,
  32. };
  33. struct zebra_privs_t test_privs =
  34. {
  35. #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
  36. .user = QUAGGA_USER,
  37. .group = QUAGGA_GROUP,
  38. #endif
  39. #if defined(VTY_GROUP)
  40. .vty_group = VTY_GROUP,
  41. #endif
  42. .caps_p = _caps_p,
  43. .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
  44. .cap_num_i = 0
  45. };
  46. struct option longopts[] =
  47. {
  48. { "help", no_argument, NULL, 'h'},
  49. { "user", required_argument, NULL, 'u'},
  50. { "group", required_argument, NULL, 'g'},
  51. { 0 }
  52. };
  53. /* Help information display. */
  54. static void
  55. usage (char *progname, int status)
  56. {
  57. if (status != 0)
  58. fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  59. else
  60. {
  61. printf ("Usage : %s [OPTION...]\n\
  62. Daemon which does 'slow' things.\n\n\
  63. -u, --user User to run as\n\
  64. -g, --group Group to run as\n\
  65. -h, --help Display this help and exit\n\
  66. \n\
  67. Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  68. }
  69. exit (status);
  70. }
  71. struct thread_master *master;
  72. /* main routine. */
  73. int
  74. main (int argc, char **argv)
  75. {
  76. char *p;
  77. char *progname;
  78. struct zprivs_ids_t ids;
  79. /* Set umask before anything for security */
  80. umask (0027);
  81. /* get program name */
  82. progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  83. while (1)
  84. {
  85. int opt;
  86. opt = getopt_long (argc, argv, "hu:g:", longopts, 0);
  87. if (opt == EOF)
  88. break;
  89. switch (opt)
  90. {
  91. case 0:
  92. break;
  93. case 'u':
  94. test_privs.user = optarg;
  95. break;
  96. case 'g':
  97. test_privs.group = optarg;
  98. break;
  99. case 'h':
  100. usage (progname, 0);
  101. break;
  102. default:
  103. usage (progname, 1);
  104. break;
  105. }
  106. }
  107. /* Library inits. */
  108. memory_init ();
  109. zprivs_init (&test_privs);
  110. #define PRIV_STATE() \
  111. ((test_privs.current_state() == ZPRIVS_RAISED) ? "Raised" : "Lowered")
  112. printf ("%s\n", PRIV_STATE());
  113. test_privs.change(ZPRIVS_RAISE);
  114. printf ("%s\n", PRIV_STATE());
  115. test_privs.change(ZPRIVS_LOWER);
  116. printf ("%s\n", PRIV_STATE());
  117. zprivs_get_ids (&ids);
  118. /* terminate privileges */
  119. zprivs_terminate(&test_privs);
  120. /* but these should continue to work... */
  121. printf ("%s\n", PRIV_STATE());
  122. test_privs.change(ZPRIVS_RAISE);
  123. printf ("%s\n", PRIV_STATE());
  124. test_privs.change(ZPRIVS_LOWER);
  125. printf ("%s\n", PRIV_STATE());
  126. zprivs_get_ids (&ids);
  127. printf ("terminating\n");
  128. return 0;
  129. }