]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_privs.c
*: Convert event.h to frrevent.h
[mirror_frr.git] / tests / lib / test_privs.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 */
4
5 #include <zebra.h>
6
7 #include <lib/version.h>
8 #include "getopt.h"
9 #include "privs.h"
10 #include "memory.h"
11 #include "lib_vty.h"
12
13 zebra_capabilities_t _caps_p[] = {
14 ZCAP_NET_RAW, ZCAP_BIND, ZCAP_NET_ADMIN, ZCAP_DAC_OVERRIDE,
15 };
16
17 struct zebra_privs_t test_privs = {
18 #if defined(FRR_USER) && defined(FRR_GROUP)
19 .user = FRR_USER,
20 .group = FRR_GROUP,
21 #endif
22 #if defined(VTY_GROUP)
23 .vty_group = VTY_GROUP,
24 #endif
25 .caps_p = _caps_p,
26 .cap_num_p = array_size(_caps_p),
27 .cap_num_i = 0};
28
29 struct option longopts[] = {{"help", no_argument, NULL, 'h'},
30 {"user", required_argument, NULL, 'u'},
31 {"group", required_argument, NULL, 'g'},
32 {0}};
33
34 /* Help information display. */
35 static void usage(char *progname, int status)
36 {
37 if (status != 0)
38 fprintf(stderr, "Try `%s --help' for more information.\n",
39 progname);
40 else {
41 printf("Usage : %s [OPTION...]\n\
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\
47 Report bugs to %s\n",
48 progname, FRR_BUG_ADDRESS);
49 }
50 exit(status);
51 }
52
53 struct event_loop *master;
54 /* main routine. */
55 int main(int argc, char **argv)
56 {
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 }
91 }
92
93 /* Library inits. */
94 lib_cmd_init();
95 zprivs_preinit(&test_privs);
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());
102 frr_with_privs(&test_privs) {
103 printf("%s\n", PRIV_STATE());
104 }
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());
114 frr_with_privs(&test_privs) {
115 printf("%s\n", PRIV_STATE());
116 }
117
118 printf("%s\n", PRIV_STATE());
119 zprivs_get_ids(&ids);
120
121 printf("terminating\n");
122 return 0;
123 }