paul | 84771ee | 2005-10-11 03:48:28 +0000 | [diff] [blame] | 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 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #include <lib/version.h> |
| 25 | #include "getopt.h" |
| 26 | #include "privs.h" |
| 27 | #include "memory.h" |
| 28 | |
| 29 | zebra_capabilities_t _caps_p [] = |
| 30 | { |
| 31 | ZCAP_NET_RAW, |
| 32 | ZCAP_BIND, |
| 33 | ZCAP_NET_ADMIN, |
| 34 | ZCAP_DAC_OVERRIDE, |
| 35 | }; |
| 36 | |
| 37 | struct zebra_privs_t test_privs = |
| 38 | { |
| 39 | #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP) |
| 40 | .user = QUAGGA_USER, |
| 41 | .group = QUAGGA_GROUP, |
| 42 | #endif |
| 43 | #if defined(VTY_GROUP) |
| 44 | .vty_group = VTY_GROUP, |
| 45 | #endif |
| 46 | .caps_p = _caps_p, |
| 47 | .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]), |
| 48 | .cap_num_i = 0 |
| 49 | }; |
| 50 | |
| 51 | struct option longopts[] = |
| 52 | { |
| 53 | { "help", no_argument, NULL, 'h'}, |
| 54 | { "user", required_argument, NULL, 'u'}, |
| 55 | { "group", required_argument, NULL, 'g'}, |
| 56 | { 0 } |
| 57 | }; |
| 58 | |
| 59 | /* Help information display. */ |
| 60 | static void |
| 61 | usage (char *progname, int status) |
| 62 | { |
| 63 | if (status != 0) |
| 64 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 65 | else |
| 66 | { |
| 67 | printf ("Usage : %s [OPTION...]\n\ |
| 68 | Daemon which does 'slow' things.\n\n\ |
| 69 | -u, --user User to run as\n\ |
| 70 | -g, --group Group to run as\n\ |
| 71 | -h, --help Display this help and exit\n\ |
| 72 | \n\ |
| 73 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 74 | } |
| 75 | exit (status); |
| 76 | } |
| 77 | |
| 78 | struct thread_master *master; |
| 79 | /* main routine. */ |
| 80 | int |
| 81 | main (int argc, char **argv) |
| 82 | { |
| 83 | char *p; |
| 84 | char *progname; |
| 85 | struct zprivs_ids_t ids; |
| 86 | |
| 87 | /* Set umask before anything for security */ |
| 88 | umask (0027); |
| 89 | |
| 90 | /* get program name */ |
| 91 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 92 | |
| 93 | while (1) |
| 94 | { |
| 95 | int opt; |
| 96 | |
| 97 | opt = getopt_long (argc, argv, "hu:g:", longopts, 0); |
| 98 | |
| 99 | if (opt == EOF) |
| 100 | break; |
| 101 | |
| 102 | switch (opt) |
| 103 | { |
| 104 | case 0: |
| 105 | break; |
| 106 | case 'u': |
| 107 | test_privs.user = optarg; |
| 108 | break; |
| 109 | case 'g': |
| 110 | test_privs.group = optarg; |
| 111 | break; |
| 112 | case 'h': |
| 113 | usage (progname, 0); |
| 114 | break; |
| 115 | default: |
| 116 | usage (progname, 1); |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* Library inits. */ |
| 122 | memory_init (); |
| 123 | zprivs_init (&test_privs); |
| 124 | |
| 125 | #define PRIV_STATE() \ |
| 126 | ((test_privs.current_state() == ZPRIVS_RAISED) ? "Raised" : "Lowered") |
| 127 | |
| 128 | printf ("%s\n", PRIV_STATE()); |
| 129 | test_privs.change(ZPRIVS_RAISE); |
| 130 | |
| 131 | printf ("%s\n", PRIV_STATE()); |
| 132 | test_privs.change(ZPRIVS_LOWER); |
| 133 | |
| 134 | printf ("%s\n", PRIV_STATE()); |
| 135 | zprivs_get_ids (&ids); |
| 136 | |
| 137 | /* terminate privileges */ |
| 138 | zprivs_terminate(&test_privs); |
| 139 | |
| 140 | /* but these should continue to work... */ |
| 141 | printf ("%s\n", PRIV_STATE()); |
| 142 | test_privs.change(ZPRIVS_RAISE); |
| 143 | |
| 144 | printf ("%s\n", PRIV_STATE()); |
| 145 | test_privs.change(ZPRIVS_LOWER); |
| 146 | |
| 147 | printf ("%s\n", PRIV_STATE()); |
| 148 | zprivs_get_ids (&ids); |
| 149 | |
| 150 | printf ("terminating\n"); |
| 151 | return 0; |
| 152 | } |