]>
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; |
369851ac | 14 | use PVE::CLIFormatter; |
09281ad7 | 15 | use PVE::CLIHandler; |
66d1b615 | 16 | use PVE::JSONSchema qw(get_standard_option); |
b34d76e7 | 17 | use PVE::PTY; |
369851ac | 18 | use PVE::RESTHandler; |
66d1b615 | 19 | use PVE::Tools qw(extract_param); |
09281ad7 DM |
20 | |
21 | use base qw(PVE::CLIHandler); | |
22 | ||
e623414a DM |
23 | sub setup_environment { |
24 | PVE::RPCEnvironment->setup_default_cli_env(); | |
25 | } | |
26 | ||
b34d76e7 DC |
27 | sub param_mapping { |
28 | my ($name) = @_; | |
98007830 | 29 | |
b34d76e7 DC |
30 | my $mapping = { |
31 | 'change_password' => [ | |
32 | PVE::CLIHandler::get_standard_mapping('pve-password'), | |
33 | ], | |
34 | 'create_ticket' => [ | |
35 | PVE::CLIHandler::get_standard_mapping('pve-password', { | |
36 | func => sub { | |
37 | # do not accept values given on cmdline | |
38 | return PVE::PTY::read_password('Enter password: '); | |
39 | }, | |
40 | }), | |
41 | ] | |
42 | }; | |
43 | ||
44 | return $mapping->{$name}; | |
98007830 DM |
45 | } |
46 | ||
369851ac FG |
47 | my $print_api_result = sub { |
48 | my ($data, $schema, $options) = @_; | |
49 | PVE::CLIFormatter::print_api_result($data, $schema, undef, $options); | |
50 | }; | |
51 | ||
66d1b615 FG |
52 | my $print_perm_result = sub { |
53 | my ($data, $schema, $options) = @_; | |
54 | ||
55 | if (!defined($options->{'output-format'}) || $options->{'output-format'} eq 'text') { | |
56 | my $table_schema = { | |
57 | type => 'array', | |
58 | items => { | |
59 | type => 'object', | |
60 | properties => { | |
61 | 'path' => { type => 'string', title => 'ACL path' }, | |
62 | 'permissions' => { type => 'string', title => 'Permissions' }, | |
63 | }, | |
64 | }, | |
65 | }; | |
66 | my $table_data = []; | |
67 | foreach my $path (sort keys %$data) { | |
68 | my $value = ''; | |
69 | my $curr = $data->{$path}; | |
70 | foreach my $perm (sort keys %$curr) { | |
71 | $value .= "\n" if $value; | |
72 | $value .= $perm; | |
73 | $value .= " (*)" if $curr->{$perm}; | |
74 | } | |
75 | push @$table_data, { path => $path, permissions => $value }; | |
76 | } | |
77 | PVE::CLIFormatter::print_api_result($table_data, $table_schema, undef, $options); | |
78 | print "Permissions marked with '(*)' have the 'propagate' flag set.\n"; | |
79 | } else { | |
80 | PVE::CLIFormatter::print_api_result($data, $schema, undef, $options); | |
81 | } | |
82 | }; | |
83 | ||
84 | __PACKAGE__->register_method({ | |
85 | name => 'token_permissions', | |
86 | path => 'token_permissions', | |
87 | method => 'GET', | |
88 | description => 'Retrieve effective permissions of given token.', | |
89 | parameters => { | |
90 | additionalProperties => 0, | |
91 | properties => { | |
92 | userid => get_standard_option('userid'), | |
93 | tokenid => get_standard_option('token-subid'), | |
94 | path => get_standard_option('acl-path', { | |
95 | description => "Only dump this specific path, not the whole tree.", | |
96 | optional => 1, | |
97 | }), | |
98 | }, | |
99 | }, | |
100 | returns => { | |
101 | type => 'object', | |
102 | description => 'Hash of structure "path" => "privilege" => "propagate boolean".', | |
103 | }, | |
104 | code => sub { | |
105 | my ($param) = @_; | |
106 | ||
107 | my $token_subid = extract_param($param, "tokenid"); | |
108 | $param->{userid} = PVE::AccessControl::join_tokenid($param->{userid}, $token_subid); | |
109 | ||
110 | return PVE::API2::AccessControl->permissions($param); | |
111 | }}); | |
112 | ||
09281ad7 | 113 | our $cmddef = { |
1e41cdc9 PA |
114 | user => { |
115 | add => [ 'PVE::API2::User', 'create_user', ['userid'] ], | |
116 | modify => [ 'PVE::API2::User', 'update_user', ['userid'] ], | |
117 | delete => [ 'PVE::API2::User', 'delete_user', ['userid'] ], | |
369851ac | 118 | list => [ 'PVE::API2::User', 'index', [], {}, $print_api_result, $PVE::RESTHandler::standard_output_options], |
66d1b615 | 119 | permissions => [ 'PVE::API2::AccessControl', 'permissions', ['userid'], {}, $print_perm_result, $PVE::RESTHandler::standard_output_options], |
084c149a FG |
120 | token => { |
121 | add => [ 'PVE::API2::User', 'generate_token', ['userid', 'tokenid'], {}, $print_api_result, $PVE::RESTHandler::standard_output_options ], | |
ccaecac1 | 122 | modify => [ 'PVE::API2::User', 'update_token_info', ['userid', 'tokenid'], {}, $print_api_result, $PVE::RESTHandler::standard_output_options ], |
084c149a FG |
123 | remove => [ 'PVE::API2::User', 'remove_token', ['userid', 'tokenid'], {}, $print_api_result, $PVE::RESTHandler::standard_output_options ], |
124 | list => [ 'PVE::API2::User', 'token_index', ['userid'], {}, $print_api_result, $PVE::RESTHandler::standard_output_options], | |
66d1b615 | 125 | permissions => [ __PACKAGE__, 'token_permissions', ['userid', 'tokenid'], {}, $print_perm_result, $PVE::RESTHandler::standard_output_options], |
084c149a | 126 | } |
1e41cdc9 PA |
127 | }, |
128 | group => { | |
129 | add => [ 'PVE::API2::Group', 'create_group', ['groupid'] ], | |
130 | modify => [ 'PVE::API2::Group', 'update_group', ['groupid'] ], | |
131 | delete => [ 'PVE::API2::Group', 'delete_group', ['groupid'] ], | |
369851ac | 132 | list => [ 'PVE::API2::Group', 'index', [], {}, $print_api_result, $PVE::RESTHandler::standard_output_options], |
1e41cdc9 PA |
133 | }, |
134 | role => { | |
135 | add => [ 'PVE::API2::Role', 'create_role', ['roleid'] ], | |
136 | modify => [ 'PVE::API2::Role', 'update_role', ['roleid'] ], | |
137 | delete => [ 'PVE::API2::Role', 'delete_role', ['roleid'] ], | |
369851ac | 138 | list => [ 'PVE::API2::Role', 'index', [], {}, $print_api_result, $PVE::RESTHandler::standard_output_options], |
1e41cdc9 PA |
139 | }, |
140 | acl => { | |
141 | modify => [ 'PVE::API2::ACL', 'update_acl', ['path'], { delete => 0 }], | |
142 | delete => [ 'PVE::API2::ACL', 'update_acl', ['path'], { delete => 1 }], | |
369851ac | 143 | list => [ 'PVE::API2::ACL', 'read_acl', [], {}, $print_api_result, $PVE::RESTHandler::standard_output_options], |
1e41cdc9 | 144 | }, |
f28a69a0 DC |
145 | |
146 | realm => { | |
147 | add => [ 'PVE::API2::Domains', 'create', ['realm'] ], | |
148 | modify => [ 'PVE::API2::Domains', 'update', ['realm'] ], | |
149 | delete => [ 'PVE::API2::Domains', 'delete', ['realm'] ], | |
150 | list => [ 'PVE::API2::Domains', 'index', [], {}, $print_api_result, $PVE::RESTHandler::standard_output_options], | |
673d2bf2 | 151 | sync => [ 'PVE::API2::Domains', 'sync', ['realm'], ], |
f28a69a0 DC |
152 | }, |
153 | ||
09281ad7 DM |
154 | ticket => [ 'PVE::API2::AccessControl', 'create_ticket', ['username'], undef, |
155 | sub { | |
156 | my ($res) = @_; | |
157 | print "$res->{ticket}\n"; | |
158 | }], | |
159 | ||
765305e2 | 160 | passwd => [ 'PVE::API2::AccessControl', 'change_password', ['userid'] ], |
09281ad7 | 161 | |
1e41cdc9 PA |
162 | useradd => { alias => 'user add' }, |
163 | usermod => { alias => 'user modify' }, | |
164 | userdel => { alias => 'user delete' }, | |
09281ad7 | 165 | |
1e41cdc9 PA |
166 | groupadd => { alias => 'group add' }, |
167 | groupmod => { alias => 'group modify' }, | |
168 | groupdel => { alias => 'group delete' }, | |
09281ad7 | 169 | |
1e41cdc9 PA |
170 | roleadd => { alias => 'role add' }, |
171 | rolemod => { alias => 'role modify' }, | |
172 | roledel => { alias => 'role delete' }, | |
09281ad7 | 173 | |
1e41cdc9 PA |
174 | aclmod => { alias => 'acl modify' }, |
175 | acldel => { alias => 'acl delete' }, | |
09281ad7 DM |
176 | }; |
177 | ||
178 | 1; |