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