-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathTotpexceptionHandler.php
More file actions
314 lines (280 loc) · 10.4 KB
/
Copy pathTotpexceptionHandler.php
File metadata and controls
314 lines (280 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
* Handler for TOTP exception addresses.
*
* Manages IP addresses that are exempt from TOTP requirements.
* Exceptions can be scoped to a specific user, a domain, or global (NULL username).
*
* Visibility rules:
* - Superadmins see all exceptions
* - Admins see exceptions for users/domains they manage
* - Users see exceptions for their username, domain, and global (NULL)
*/
class TotpexceptionHandler extends PFAHandler
{
protected string $db_table = 'totp_exception_address';
protected string $id_field = 'id';
protected string $label_field = 'ip';
protected ?string $domain_field = '';
protected string $order_by = 'username, ip';
protected ?string $user_field = 'username';
/**
* TOTP exceptions are not domain-scoped in the traditional sense.
* Visibility is handled in read_from_db_postprocess().
*/
protected function no_domain_field()
{
$this->allowed_domains = [];
}
/** @return void */
protected function initStruct()
{
$this->struct = array(
# field name allow display in... type $PALANG label $PALANG description default / options / ...
# editing? form list
'id' => self::pacol(0, 0, 1, 'num', 'pFetchmail_field_id', '', '', array(), 1),
'username' => self::pacol(1, 1, 1, 'text', 'pTotp_exceptions_user', ''),
'ip' => self::pacol(1, 1, 1, 'text', 'pTotp_exceptions_address', ''),
'description' => self::pacol(1, 1, 1, 'text', 'pTotp_exceptions_description', ''),
);
}
protected function initMsg()
{
$this->msg['error_already_exists'] = 'pTotp_exception_result_error';
$this->msg['error_does_not_exist'] = 'pTotp_exception_result_error';
$this->msg['confirm_delete'] = 'confirm';
if ($this->new) {
$this->msg['logname'] = 'add_totp_exception';
$this->msg['store_error'] = 'pTotp_exception_result_error';
$this->msg['successmessage'] = 'pTotp_exception_result_success';
} else {
$this->msg['logname'] = 'edit_totp_exception';
$this->msg['store_error'] = 'pEdit_totp_exception_result_error';
$this->msg['successmessage'] = 'pTotp_exception_result_success';
}
}
/** @return array<string, mixed> */
public function webformConfig()
{
return array(
'formtitle_create' => 'pTotp_exceptions_welcome',
'formtitle_edit' => 'pTotp_exceptions_welcome',
'create_button' => 'pTotp_exceptions_add',
'required_role' => 'admin',
'listview' => 'list.php?table=totpexception',
'early_init' => 0,
'user_hardcoded_field' => 'username',
);
}
/** @return bool */
protected function validate_new_id()
{
if ($this->id != '') {
$this->errormsg[$this->id_field] = 'auto_increment value, you must pass an empty string!';
return false;
}
return true;
}
/**
* Validate IP address field.
*
* @param string $field field name
* @param string $value submitted value
* @return bool true if valid
*/
protected function _validate_ip(string $field, string $value): bool
{
if (trim($value) === '') {
$this->errormsg[$field] = Config::Lang('pException_ip_empty_error');
return false;
}
if (!filter_var($value, FILTER_VALIDATE_IP)) {
$this->errormsg[$field] = Config::Lang('pException_ip_error');
return false;
}
return true;
}
/**
* Validate username field and enforce permissions based on role.
* Empty username is normalised to NULL (global exception, superadmin only).
*
* @param string $field field name
* @param string $value submitted value
* @return bool true if valid
*/
protected function _validate_username(string $field, string $value): bool
{
// Normalize empty string to NULL for global exceptions
if ($value === '') {
if ($this->is_superadmin) {
$this->values['username'] = null;
return true;
}
$this->errormsg[$field] = Config::Lang('pException_user_global_error');
return false;
}
// Users can only set exceptions for themselves
if (!$this->is_admin) {
$this->values['username'] = $this->username;
return true;
}
// Superadmins can set for anyone
if ($this->is_superadmin) {
return true;
}
// Admins can set for users/domains they manage
if (strpos($value, '@')) {
list($_, $exception_domain) = explode('@', $value);
} else {
$exception_domain = $value;
}
if (!in_array($exception_domain, $this->allowed_domains)) {
$this->errormsg[$field] = Config::Lang('pException_user_entire_domain_error');
return false;
}
return true;
}
/**
* Filter results based on user role after reading from DB.
* - Superadmins see all
* - Admins see exceptions for their managed domains + own username
* - Users see exceptions for their username, domain, and global (NULL)
*
* @param array $db_result rows keyed by ID
* @return array filtered rows
*/
protected function read_from_db_postprocess($db_result)
{
if ($this->is_superadmin) {
return $db_result;
}
$filtered = [];
if ($this->is_admin) {
foreach ($db_result as $key => $row) {
$ex_username = $row['username'] ?? '';
if ($ex_username === $this->admin_username) {
$filtered[$key] = $row;
continue;
}
if (strpos($ex_username, '@')) {
list($_, $ex_domain) = explode('@', $ex_username);
} else {
$ex_domain = $ex_username;
}
if (in_array($ex_domain, $this->allowed_domains)) {
$filtered[$key] = $row;
}
}
} else {
// User mode
list($_, $domain) = explode('@', $this->username);
foreach ($db_result as $key => $row) {
$ex_username = $row['username'] ?? null;
if ($ex_username === $this->username || $ex_username === $domain || $ex_username === null) {
$filtered[$key] = $row;
}
}
}
return $filtered;
}
/**
* Run post-creation script if configured.
*
* @return bool true on success
*/
protected function postSave(): bool
{
if (!$this->new) {
return true;
}
$cmd = Config::read('mailbox_post_totp_exception_add_script');
if (empty($cmd)) {
return true;
}
$cmdarg1 = escapeshellarg($this->values['username'] ?? '');
$cmdarg2 = escapeshellarg($this->values['ip'] ?? '');
$command = "$cmd $cmdarg1 $cmdarg2 2>&1";
return $this->run_post_script($command, Config::Lang('mailbox_post_totp_exception_add_failed'));
}
/**
* Delete a TOTP exception with role-based permission checks.
*
* @return bool true on success
*/
public function delete()
{
if (!$this->view()) {
$this->errormsg[] = Config::Lang($this->msg['error_does_not_exist']);
return false;
}
$exception = $this->result;
$ex_username = $exception['username'] ?? '';
// Check permissions
if ($this->is_superadmin) {
// can delete anything
} elseif ($this->is_admin) {
if ($ex_username !== $this->admin_username) {
if (strpos($ex_username, '@')) {
list($_, $ex_domain) = explode('@', $ex_username);
} else {
$ex_domain = $ex_username;
}
if (!in_array($ex_domain, $this->allowed_domains)) {
$this->errormsg[] = Config::Lang('pException_user_entire_domain_error');
return false;
}
}
} else {
if ($ex_username !== $this->username) {
$this->errormsg[] = Config::lang('pEdit_totp_exception_result_error');
return false;
}
}
db_delete($this->db_table, $this->id_field, $this->id);
// Run post-delete script
$cmd = Config::read('mailbox_post_totp_exception_delete_script');
if (!empty($cmd)) {
$cmdarg1 = escapeshellarg($ex_username);
$cmdarg2 = escapeshellarg($exception['ip'] ?? '');
$command = "$cmd $cmdarg1 $cmdarg2 2>&1";
$this->run_post_script($command, Config::Lang('mailbox_post_totp_exception_delete_failed'));
}
db_log($this->admin_username ?: $this->username, 'delete_totp_exception', $exception['ip'] ?? '');
$this->infomsg[] = Config::Lang_f('pDelete_delete_success', $exception['ip'] ?? $this->id);
return true;
}
/**
* Run a post-save/delete script via proc_open.
*
* @param string $command full shell command to execute
* @param string $warnmsg error message to display on failure
* @return bool true on success
*/
private function run_post_script(string $command, string $warnmsg): bool
{
$spec = [0 => ["pipe", "r"], 1 => ["pipe", "w"]];
$proc = proc_open($command, $spec, $pipes);
if (!$proc) {
error_log("can't proc_open: $command");
$this->errormsg[] = $warnmsg;
return false;
}
fclose($pipes[0]);
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$retval = proc_close($proc);
if (0 != $retval) {
error_log("Running $command yielded return value=$retval, output was: " . json_encode($output));
$this->errormsg[] = $warnmsg;
return false;
}
return true;
}
/**
* @return string empty string, TOTP exceptions are not domain-scoped
*/
public function domain_from_id()
{
return '';
}
}