]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
84771ee0 | 2 | /* |
84771ee0 | 3 | */ |
4 | ||
5 | #include <zebra.h> | |
6 | ||
7 | #include <lib/version.h> | |
8 | #include "getopt.h" | |
9 | #include "privs.h" | |
10 | #include "memory.h" | |
1c0d8808 | 11 | #include "lib_vty.h" |
84771ee0 | 12 | |
d62a17ae | 13 | zebra_capabilities_t _caps_p[] = { |
9d303b37 | 14 | ZCAP_NET_RAW, ZCAP_BIND, ZCAP_NET_ADMIN, ZCAP_DAC_OVERRIDE, |
84771ee0 | 15 | }; |
16 | ||
d62a17ae | 17 | struct zebra_privs_t test_privs = { |
b2f36157 | 18 | #if defined(FRR_USER) && defined(FRR_GROUP) |
d62a17ae | 19 | .user = FRR_USER, |
20 | .group = FRR_GROUP, | |
84771ee0 | 21 | #endif |
22 | #if defined(VTY_GROUP) | |
d62a17ae | 23 | .vty_group = VTY_GROUP, |
84771ee0 | 24 | #endif |
d62a17ae | 25 | .caps_p = _caps_p, |
97b5d752 | 26 | .cap_num_p = array_size(_caps_p), |
d62a17ae | 27 | .cap_num_i = 0}; |
84771ee0 | 28 | |
d62a17ae | 29 | struct option longopts[] = {{"help", no_argument, NULL, 'h'}, |
30 | {"user", required_argument, NULL, 'u'}, | |
31 | {"group", required_argument, NULL, 'g'}, | |
32 | {0}}; | |
84771ee0 | 33 | |
34 | /* Help information display. */ | |
d62a17ae | 35 | static void usage(char *progname, int status) |
84771ee0 | 36 | { |
d62a17ae | 37 | if (status != 0) |
38 | fprintf(stderr, "Try `%s --help' for more information.\n", | |
39 | progname); | |
40 | else { | |
41 | printf("Usage : %s [OPTION...]\n\ | |
84771ee0 | 42 | Daemon which does 'slow' things.\n\n\ |
43 | -u, --user User to run as\n\ | |
44 | -g, --group Group to run as\n\ | |
45 | -h, --help Display this help and exit\n\ | |
46 | \n\ | |
d62a17ae | 47 | Report bugs to %s\n", |
48 | progname, FRR_BUG_ADDRESS); | |
49 | } | |
50 | exit(status); | |
84771ee0 | 51 | } |
6b0655a2 | 52 | |
cd9d0537 | 53 | struct event_loop *master; |
84771ee0 | 54 | /* main routine. */ |
d62a17ae | 55 | int main(int argc, char **argv) |
84771ee0 | 56 | { |
d62a17ae | 57 | char *p; |
58 | char *progname; | |
59 | struct zprivs_ids_t ids; | |
60 | ||
61 | /* Set umask before anything for security */ | |
62 | umask(0027); | |
63 | ||
64 | /* get program name */ | |
65 | progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]); | |
66 | ||
67 | while (1) { | |
68 | int opt; | |
69 | ||
70 | opt = getopt_long(argc, argv, "hu:g:", longopts, 0); | |
71 | ||
72 | if (opt == EOF) | |
73 | break; | |
74 | ||
75 | switch (opt) { | |
76 | case 0: | |
77 | break; | |
78 | case 'u': | |
79 | test_privs.user = optarg; | |
80 | break; | |
81 | case 'g': | |
82 | test_privs.group = optarg; | |
83 | break; | |
84 | case 'h': | |
85 | usage(progname, 0); | |
86 | break; | |
87 | default: | |
88 | usage(progname, 1); | |
89 | break; | |
90 | } | |
84771ee0 | 91 | } |
d62a17ae | 92 | |
93 | /* Library inits. */ | |
1c0d8808 | 94 | lib_cmd_init(); |
37a1f2fb | 95 | zprivs_preinit(&test_privs); |
d62a17ae | 96 | zprivs_init(&test_privs); |
97 | ||
98 | #define PRIV_STATE() \ | |
99 | ((test_privs.current_state() == ZPRIVS_RAISED) ? "Raised" : "Lowered") | |
100 | ||
101 | printf("%s\n", PRIV_STATE()); | |
0cf6db21 | 102 | frr_with_privs(&test_privs) { |
6bb30c2c DL |
103 | printf("%s\n", PRIV_STATE()); |
104 | } | |
d62a17ae | 105 | |
106 | printf("%s\n", PRIV_STATE()); | |
107 | zprivs_get_ids(&ids); | |
108 | ||
109 | /* terminate privileges */ | |
110 | zprivs_terminate(&test_privs); | |
111 | ||
112 | /* but these should continue to work... */ | |
113 | printf("%s\n", PRIV_STATE()); | |
0cf6db21 | 114 | frr_with_privs(&test_privs) { |
6bb30c2c DL |
115 | printf("%s\n", PRIV_STATE()); |
116 | } | |
d62a17ae | 117 | |
118 | printf("%s\n", PRIV_STATE()); | |
119 | zprivs_get_ids(&ids); | |
120 | ||
121 | printf("terminating\n"); | |
122 | return 0; | |
84771ee0 | 123 | } |