]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_privs.c
Merge pull request #2848 from donaldsharp/more_init
[mirror_frr.git] / tests / lib / test_privs.c
1 /*
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 *
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
17 */
18
19 #include <zebra.h>
20
21 #include <lib/version.h>
22 #include "getopt.h"
23 #include "privs.h"
24 #include "memory.h"
25 #include "memory_vty.h"
26
27 zebra_capabilities_t _caps_p[] = {
28 ZCAP_NET_RAW, ZCAP_BIND, ZCAP_NET_ADMIN, ZCAP_DAC_OVERRIDE,
29 };
30
31 struct zebra_privs_t test_privs = {
32 #if defined(FRR_USER) && defined(FRR_GROUP)
33 .user = FRR_USER,
34 .group = FRR_GROUP,
35 #endif
36 #if defined(VTY_GROUP)
37 .vty_group = VTY_GROUP,
38 #endif
39 .caps_p = _caps_p,
40 .cap_num_p = sizeof(_caps_p) / sizeof(_caps_p[0]),
41 .cap_num_i = 0};
42
43 struct option longopts[] = {{"help", no_argument, NULL, 'h'},
44 {"user", required_argument, NULL, 'u'},
45 {"group", required_argument, NULL, 'g'},
46 {0}};
47
48 /* Help information display. */
49 static void usage(char *progname, int status)
50 {
51 if (status != 0)
52 fprintf(stderr, "Try `%s --help' for more information.\n",
53 progname);
54 else {
55 printf("Usage : %s [OPTION...]\n\
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\
61 Report bugs to %s\n",
62 progname, FRR_BUG_ADDRESS);
63 }
64 exit(status);
65 }
66
67 struct thread_master *master;
68 /* main routine. */
69 int main(int argc, char **argv)
70 {
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 }
105 }
106
107 /* Library inits. */
108 memory_init();
109 zprivs_preinit(&test_privs);
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());
116 frr_elevate_privs(&test_privs) {
117 printf("%s\n", PRIV_STATE());
118 }
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());
128 frr_elevate_privs(&test_privs) {
129 printf("%s\n", PRIV_STATE());
130 }
131
132 printf("%s\n", PRIV_STATE());
133 zprivs_get_ids(&ids);
134
135 printf("terminating\n");
136 return 0;
137 }