]>
Commit | Line | Data |
---|---|---|
09281ad7 DM |
1 | package PVE::CLI::pveum; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
042eaa3d | 5 | |
66d1b615 | 6 | use PVE::AccessControl; |
09281ad7 DM |
7 | use PVE::RPCEnvironment; |
8 | use PVE::API2::User; | |
9 | use PVE::API2::Group; | |
10 | use PVE::API2::Role; | |
11 | use PVE::API2::ACL; | |
12 | use PVE::API2::AccessControl; | |
f28a69a0 | 13 | use PVE::API2::Domains; |
dc547a13 | 14 | use PVE::API2::TFA; |
9d06f603 | 15 | use PVE::Cluster qw(cfs_read_file cfs_write_file); |
369851ac | 16 | use PVE::CLIFormatter; |
09281ad7 | 17 | use PVE::CLIHandler; |
66d1b615 | 18 | use PVE::JSONSchema qw(get_standard_option); |
b34d76e7 | 19 | use PVE::PTY; |
369851ac | 20 | use PVE::RESTHandler; |
66d1b615 | 21 | use PVE::Tools qw(extract_param); |
09281ad7 DM |
22 | |
23 | use base qw(PVE::CLIHandler); | |
24 | ||
e623414a DM |
25 | sub setup_environment { |
26 | PVE::RPCEnvironment->setup_default_cli_env(); | |
27 | } | |
28 | ||
b34d76e7 DC |
29 | sub param_mapping { |
30 | my ($name) = @_; | |
98007830 | 31 | |
b34d76e7 DC |
32 | my $mapping = { |
33 | 'change_password' => [ | |
34 | PVE::CLIHandler::get_standard_mapping('pve-password'), | |
35 | ], | |
36 | 'create_ticket' => [ | |
37 | PVE::CLIHandler::get_standard_mapping('pve-password', { | |
38 | func => sub { | |
39 | # do not accept values given on cmdline | |
40 | return PVE::PTY::read_password('Enter password: '); | |
41 | }, | |
42 | }), | |
43 | ] | |
44 | }; | |
45 | ||
46 | return $mapping->{$name}; | |
98007830 DM |
47 | } |
48 | ||
369851ac FG |
49 | my $print_api_result = sub { |
50 | my ($data, $schema, $options) = @_; | |
51 | PVE::CLIFormatter::print_api_result($data, $schema, undef, $options); | |
52 | }; | |
53 | ||
66d1b615 FG |
54 | my $print_perm_result = sub { |
55 | my ($data, $schema, $options) = @_; | |
56 | ||
57 | if (!defined($options->{'output-format'}) || $options->{'output-format'} eq 'text') { | |
58 | my $table_schema = { | |
59 | type => 'array', | |
60 | items => { | |
61 | type => 'object', | |
62 | properties => { | |
63 | 'path' => { type => 'string', title => 'ACL path' }, | |
64 | 'permissions' => { type => 'string', title => 'Permissions' }, | |
65 | }, | |
66 | }, | |
67 | }; | |
68 | my $table_data = []; | |
69 | foreach my $path (sort keys %$data) { | |
70 | my $value = ''; | |
71 | my $curr = $data->{$path}; | |
72 | foreach my $perm (sort keys %$curr) { | |
73 | $value .= "\n" if $value; | |
74 | $value .= $perm; | |
75 | $value .= " (*)" if $curr->{$perm}; | |
76 | } | |
77 | push @$table_data, { path => $path, permissions => $value }; | |
78 | } | |
79 | PVE::CLIFormatter::print_api_result($table_data, $table_schema, undef, $options); | |
80 | print "Permissions marked with '(*)' have the 'propagate' flag set.\n"; | |
81 | } else { | |
82 | PVE::CLIFormatter::print_api_result($data, $schema, undef, $options); | |
83 | } | |
84 | }; | |
85 | ||
86 | __PACKAGE__->register_method({ | |
87 | name => 'token_permissions', | |
88 | path => 'token_permissions', | |
89 | method => 'GET', | |
90 | description => 'Retrieve effective permissions of given token.', | |
91 | parameters => { | |
92 | additionalProperties => 0, | |
93 | properties => { | |
94 | userid => get_standard_option('userid'), | |
95 | tokenid => get_standard_option('token-subid'), | |
96 | path => get_standard_option('acl-path', { | |
97 | description => "Only dump this specific path, not the whole tree.", | |
98 | optional => 1, | |
99 | }), | |
100 | }, | |
101 | }, | |
102 | returns => { | |
103 | type => 'object', | |
104 | description => 'Hash of structure "path" => "privilege" => "propagate boolean".', | |
105 | }, | |
106 | code => sub { | |
107 | my ($param) = @_; | |
108 | ||
109 | my $token_subid = extract_param($param, "tokenid"); | |
110 | $param->{userid} = PVE::AccessControl::join_tokenid($param->{userid}, $token_subid); | |
111 | ||
112 | return PVE::API2::AccessControl->permissions($param); | |
113 | }}); | |
114 | ||
9d06f603 WB |
115 | __PACKAGE__->register_method({ |
116 | name => 'delete_tfa', | |
117 | path => 'delete_tfa', | |
118 | method => 'PUT', | |
119 | description => 'Delete TFA entries from a user.', | |
120 | parameters => { | |
121 | additionalProperties => 0, | |
122 | properties => { | |
123 | userid => get_standard_option('userid'), | |
124 | id => { | |
125 | description => "The TFA ID, if none provided, all TFA entries will be deleted.", | |
126 | type => 'string', | |
127 | optional => 1, | |
128 | }, | |
129 | }, | |
130 | }, | |
131 | returns => { type => 'null' }, | |
132 | code => sub { | |
133 | my ($param) = @_; | |
134 | ||
135 | my $userid = extract_param($param, "userid"); | |
136 | my $tfa_id = extract_param($param, "id"); | |
137 | ||
138 | PVE::AccessControl::assert_new_tfa_config_available(); | |
139 | ||
140 | PVE::AccessControl::lock_tfa_config(sub { | |
141 | my $tfa_cfg = cfs_read_file('priv/tfa.cfg'); | |
142 | if (defined($tfa_id)) { | |
143 | $tfa_cfg->api_delete_tfa($userid, $tfa_id); | |
144 | } else { | |
145 | $tfa_cfg->remove_user($userid); | |
146 | } | |
147 | cfs_write_file('priv/tfa.cfg', $tfa_cfg); | |
148 | }); | |
149 | return; | |
150 | }}); | |
151 | ||
09281ad7 | 152 | our $cmddef = { |
1e41cdc9 PA |
153 | user => { |
154 | add => [ 'PVE::API2::User', 'create_user', ['userid'] ], | |
155 | modify => [ 'PVE::API2::User', 'update_user', ['userid'] ], | |
156 | delete => [ 'PVE::API2::User', 'delete_user', ['userid'] ], | |
369851ac | 157 | list => [ 'PVE::API2::User', 'index', [], {}, $print_api_result, $PVE::RESTHandler::standard_output_options], |
66d1b615 | 158 | permissions => [ 'PVE::API2::AccessControl', 'permissions', ['userid'], {}, $print_perm_result, $PVE::RESTHandler::standard_output_options], |
618d112b | 159 | tfa => { |
9d06f603 | 160 | delete => [ __PACKAGE__, 'delete_tfa', ['userid'] ], |
618d112b | 161 | }, |
084c149a FG |
162 | token => { |
163 | add => [ 'PVE::API2::User', 'generate_token', ['userid', 'tokenid'], {}, $print_api_result, $PVE::RESTHandler::standard_output_options ], | |
ccaecac1 | 164 | modify => [ 'PVE::API2::User', 'update_token_info', ['userid', 'tokenid'], {}, $print_api_result, $PVE::RESTHandler::standard_output_options ], |
084c149a FG |
165 | remove => [ 'PVE::API2::User', 'remove_token', ['userid', 'tokenid'], {}, $print_api_result, $PVE::RESTHandler::standard_output_options ], |
166 | list => [ 'PVE::API2::User', 'token_index', ['userid'], {}, $print_api_result, $PVE::RESTHandler::standard_output_options], | |
66d1b615 | 167 | permissions => [ __PACKAGE__, 'token_permissions', ['userid', 'tokenid'], {}, $print_perm_result, $PVE::RESTHandler::standard_output_options], |
084c149a | 168 | } |
1e41cdc9 PA |
169 | }, |
170 | group => { | |
171 | add => [ 'PVE::API2::Group', 'create_group', ['groupid'] ], | |
172 | modify => [ 'PVE::API2::Group', 'update_group', ['groupid'] ], | |
173 | delete => [ 'PVE::API2::Group', 'delete_group', ['groupid'] ], | |
369851ac | 174 | list => [ 'PVE::API2::Group', 'index', [], {}, $print_api_result, $PVE::RESTHandler::standard_output_options], |
1e41cdc9 PA |
175 | }, |
176 | role => { | |
177 | add => [ 'PVE::API2::Role', 'create_role', ['roleid'] ], | |
178 | modify => [ 'PVE::API2::Role', 'update_role', ['roleid'] ], | |
179 | delete => [ 'PVE::API2::Role', 'delete_role', ['roleid'] ], | |
369851ac | 180 | list => [ 'PVE::API2::Role', 'index', [], {}, $print_api_result, $PVE::RESTHandler::standard_output_options], |
1e41cdc9 PA |
181 | }, |
182 | acl => { | |
183 | modify => [ 'PVE::API2::ACL', 'update_acl', ['path'], { delete => 0 }], | |
184 | delete => [ 'PVE::API2::ACL', 'update_acl', ['path'], { delete => 1 }], | |
369851ac | 185 | list => [ 'PVE::API2::ACL', 'read_acl', [], {}, $print_api_result, $PVE::RESTHandler::standard_output_options], |
1e41cdc9 | 186 | }, |
f28a69a0 DC |
187 | realm => { |
188 | add => [ 'PVE::API2::Domains', 'create', ['realm'] ], | |
189 | modify => [ 'PVE::API2::Domains', 'update', ['realm'] ], | |
190 | delete => [ 'PVE::API2::Domains', 'delete', ['realm'] ], | |
191 | list => [ 'PVE::API2::Domains', 'index', [], {}, $print_api_result, $PVE::RESTHandler::standard_output_options], | |
673d2bf2 | 192 | sync => [ 'PVE::API2::Domains', 'sync', ['realm'], ], |
f28a69a0 DC |
193 | }, |
194 | ||
09281ad7 DM |
195 | ticket => [ 'PVE::API2::AccessControl', 'create_ticket', ['username'], undef, |
196 | sub { | |
197 | my ($res) = @_; | |
198 | print "$res->{ticket}\n"; | |
199 | }], | |
200 | ||
765305e2 | 201 | passwd => [ 'PVE::API2::AccessControl', 'change_password', ['userid'] ], |
09281ad7 | 202 | |
1e41cdc9 PA |
203 | useradd => { alias => 'user add' }, |
204 | usermod => { alias => 'user modify' }, | |
205 | userdel => { alias => 'user delete' }, | |
09281ad7 | 206 | |
1e41cdc9 PA |
207 | groupadd => { alias => 'group add' }, |
208 | groupmod => { alias => 'group modify' }, | |
209 | groupdel => { alias => 'group delete' }, | |
09281ad7 | 210 | |
1e41cdc9 PA |
211 | roleadd => { alias => 'role add' }, |
212 | rolemod => { alias => 'role modify' }, | |
213 | roledel => { alias => 'role delete' }, | |
09281ad7 | 214 | |
1e41cdc9 PA |
215 | aclmod => { alias => 'acl modify' }, |
216 | acldel => { alias => 'acl delete' }, | |
09281ad7 DM |
217 | }; |
218 | ||
3470fad8 TL |
219 | # FIXME: HACK! The pool API is in pve-manager as it needs access to storage guest and RRD stats, |
220 | # so we only add the pool commands if the API module is available (required for boots-trapping) | |
221 | my $have_pool_api; | |
222 | eval { | |
223 | require PVE::API2::Pool; | |
224 | PVE::API2::Pool->import(); | |
225 | $have_pool_api = 1; | |
226 | }; | |
227 | ||
228 | if ($have_pool_api) { | |
229 | $cmddef->{pool} = { | |
230 | add => [ 'PVE::API2::Pool', 'create_pool', ['poolid'] ], | |
231 | modify => [ 'PVE::API2::Pool', 'update_pool', ['poolid'] ], | |
232 | delete => [ 'PVE::API2::Pool', 'delete_pool', ['poolid'] ], | |
233 | list => [ 'PVE::API2::Pool', 'index', [], {}, $print_api_result, $PVE::RESTHandler::standard_output_options], | |
234 | }; | |
235 | } | |
236 | ||
09281ad7 | 237 | 1; |