rootasrole 4.0.3

A better alternative to sudo(-rs)/su
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
mod convert;
mod json;

use std::{cell::RefCell, error::Error, rc::Rc};

use json::{
    cmd_setpolicy, cmd_whitelist_action, cred_caps, cred_set, cred_setpolicy, cred_unset,
    env_set_policylist, env_setlist_add, env_setpolicy, env_whitelist_set, grant_revoke,
    path_purge, path_set, path_setlist2, path_setpolicy, role_add_del, set_authentication,
    set_bounding, set_execinfo, set_privileged, set_timeout, set_umask, task_add_del,
    unset_timeout,
};

use log::debug;

use rootasrole_core::{
    database::{
        options::{Opt, OptType},
        structs::{IdTask, RoleGetter},
    },
    file::FileSettings,
};

use crate::cli::process::json::{
    workdir_purge, workdir_set_path, workdir_setlist, workdir_setpolicy,
};

use super::{
    data::{InputAction, Inputs},
    usage,
};

#[allow(clippy::too_many_lines)]
pub fn process_input(storage: &mut FileSettings, inputs: Inputs) -> Result<bool, Box<dyn Error>> {
    if inputs.action == InputAction::Convert {
        debug!("chsr convert");
        return convert::convert(
            storage,
            inputs
                .convertion
                .expect("Convertion data should be present"),
            inputs.convert_reconfigure,
        );
    }
    let rconfig = &storage.get_root().config;
    match inputs {
        Inputs {
            action: InputAction::Help,
            ..
        } => {
            usage::help();
            Ok(false)
        }
        Inputs {
            action: InputAction::List,
            options, // show options ?
            role_id,
            role_type,
            task_id,
            task_type,    // what to show
            options_type, // in json
            ..
        } => {
            debug!("chsr list");
            match json::list_json(
                rconfig,
                role_id.as_ref(),
                task_id,
                options,
                options_type,
                task_type,
                role_type,
            ) {
                Ok(()) => {
                    debug!("chsr list ok");
                    Ok(false)
                }
                Err(e) => {
                    debug!("chsr list err {e:?}");
                    Err(e)
                }
            }
        }
        Inputs {
            // chsr role r1 add|del
            action,
            role_id: Some(role_id),
            task_id: None,
            setlist_type: None,
            options: false,
            actors: None,
            role_type,
            ..
        } => role_add_del(rconfig, action, role_id, role_type),

        Inputs {
            // chsr role r1 grant|revoke -u u1 -u u2 -g g1,g2
            action,
            role_id: Some(role_id),
            actors: Some(actors),
            options: false,
            ..
        } => grant_revoke(rconfig, &role_id, action, actors),

        Inputs {
            // chsr role r1 task t1 add|del
            action,
            role_id: Some(role_id),
            task_id: Some(task_id),
            setlist_type: None,
            options: false,
            cmd_id: None,
            cred_caps: None,
            cred_setuid: None,
            cred_setgid: None,
            task_type,
            cmd_policy: None,
            cred_policy: None,
            ..
        } => task_add_del(rconfig, &role_id, action, task_id, task_type),

        Inputs {
            //chsr role r1 task t1 cred set --caps "cap_net_raw,cap_sys_admin"
            action: InputAction::Set,
            role_id: Some(role_id),
            task_id: Some(task_id),
            cred_caps,
            cred_setuid,
            cred_setgid,
            cmd_id: None,
            cmd_policy: None,
            cred_policy: None,
            options: false,
            ..
        } => cred_set(
            rconfig,
            &role_id,
            task_id,
            cred_caps,
            cred_setuid,
            cred_setgid,
        ),

        Inputs {
            //chsr role r1 task t1 cred unset --caps "cap_net_raw,cap_sys_admin"
            action: InputAction::Del,
            role_id: Some(role_id),
            task_id: Some(task_id),
            cred_caps,
            cred_setuid,
            cred_setgid,
            cmd_id: None,
            cmd_policy: None,
            options: false,
            setlist_type: None,
            ..
        } => cred_unset(
            rconfig,
            &role_id,
            task_id,
            cred_caps,
            cred_setuid.as_ref(),
            cred_setgid.as_ref(),
        ),

        Inputs {
            action,
            role_id: Some(role_id),
            task_id: Some(task_id),
            setlist_type: Some(setlist_type),
            cred_caps: Some(pcred_caps),
            cmd_policy: None,
            options: false,
            ..
        } => cred_caps(rconfig, &role_id, task_id, setlist_type, action, pcred_caps),
        Inputs {
            role_id: Some(role_id),
            task_id: Some(task_id),
            cred_policy: Some(cred_policy),
            options: false,
            ..
        } => cred_setpolicy(rconfig, &role_id, task_id, cred_policy),

        Inputs {
            // chsr role r1 task t1 command whitelist add c1
            action,
            role_id: Some(role_id),
            task_id: Some(task_id),
            cmd_id: Some(cmd_id),
            setlist_type: Some(setlist_type),
            ..
        } => cmd_whitelist_action(rconfig, &role_id, task_id, cmd_id, setlist_type, action),
        Inputs {
            role_id: Some(role_id),
            task_id: Some(task_id),
            cmd_policy: Some(cmd_policy),
            ..
        } => cmd_setpolicy(rconfig, &role_id, task_id, cmd_policy),

        // Set options
        Inputs {
            // chsr o env set A,B,C
            action: InputAction::Set,
            role_id,
            task_id,
            options_type: Some(OptType::Env),
            options_env_policy: Some(options_env_policy),
            options_key_env: Some(options_env),
            ..
        } => env_set_policylist(
            rconfig,
            role_id.as_ref(),
            task_id,
            &options_env,
            options_env_policy,
        ),
        Inputs {
            // chsr o root set privileged
            action: InputAction::Set,
            role_id,
            task_id,
            options_root: Some(options_root),
            ..
        } => set_privileged(rconfig, role_id.as_ref(), task_id, Some(options_root)),

        Inputs {
            // chsr o root del
            action: InputAction::Del,
            role_id,
            task_id,
            options_root: Some(_),
            ..
        } => set_privileged(rconfig, role_id.as_ref(), task_id, None),

        Inputs {
            // chsr o bounding set strict
            action: InputAction::Set,
            role_id,
            task_id,
            options_bounding: Some(options_bounding),
            ..
        } => set_bounding(rconfig, role_id.as_ref(), task_id, Some(options_bounding)),

        Inputs {
            // chsr o bounding del
            action: InputAction::Del,
            role_id,
            task_id,
            options_bounding: Some(_),
            ..
        } => set_bounding(rconfig, role_id.as_ref(), task_id, None),

        Inputs {
            // chsr o authentication perform
            action: InputAction::Set,
            role_id,
            task_id,
            options_auth: Some(options_auth),
            ..
        } => set_authentication(rconfig, role_id.as_ref(), task_id, Some(options_auth)),

        Inputs {
            // chsr o authentication del
            action: InputAction::Del,
            role_id,
            task_id,
            options_auth: Some(_),
            ..
        } => set_authentication(rconfig, role_id.as_ref(), task_id, None),

        Inputs {
            // chsr o execinfo hide|show
            action: InputAction::Set,
            role_id,
            task_id,
            options_execinfo: Some(options_execinfo),
            ..
        } => set_execinfo(rconfig, role_id.as_ref(), task_id, Some(options_execinfo)),

        Inputs {
            // chsr o execinfo del
            action: InputAction::Del,
            role_id,
            task_id,
            options_execinfo: Some(_),
            ..
        } => set_execinfo(rconfig, role_id.as_ref(), task_id, None),

        Inputs {
            action: InputAction::Set,
            role_id,
            task_id,
            options_umask: Some(options_umask),
            ..
        } => set_umask(rconfig, role_id.as_ref(), task_id, Some(options_umask)),

        Inputs {
            action: InputAction::Del,
            role_id,
            task_id,
            options_umask: Some(_),
            ..
        } => set_umask(rconfig, role_id.as_ref(), task_id, None),

        Inputs {
            // chsr o path whitelist set a:b:c
            action: InputAction::Set,
            role_id,
            task_id,
            options_path: Some(options_path),
            options_type: Some(OptType::Path),
            setlist_type,
            ..
        } => path_set(
            rconfig,
            role_id.as_ref(),
            task_id,
            setlist_type,
            &options_path,
        ),
        Inputs {
            // chsr o path whitelist set a:b:c
            action: InputAction::Purge,
            role_id,
            task_id,
            options_path: None,
            options_type: Some(OptType::Path),
            setlist_type,
            ..
        } => path_purge(rconfig, role_id.as_ref(), task_id, setlist_type),

        Inputs {
            // chsr o path whitelist set a:b:c
            action: InputAction::Purge,
            role_id,
            task_id,
            options_path: None,
            options_type: Some(OptType::Workdir),
            setlist_type,
            ..
        } => workdir_purge(rconfig, role_id.as_ref(), task_id, setlist_type),

        Inputs {
            // chsr o env whitelist set A,B,C
            action: InputAction::Set,
            role_id,
            task_id,
            options_key_env: Some(options_env),
            options_type: Some(OptType::Env),
            setlist_type,
            options_env_values: None,
            options_env_policy: None,
            ..
        } => env_whitelist_set(
            rconfig,
            role_id.as_ref(),
            task_id,
            setlist_type.as_ref(),
            &options_env,
        ),
        Inputs {
            // chsr o timeout unset --type  --duration  --max-usage
            action: InputAction::Del,
            role_id,
            task_id,
            options: true,
            timeout_arg: Some(timeout_arg),
            setlist_type: None,
            ..
        } => unset_timeout(rconfig, role_id.as_ref(), task_id, timeout_arg),

        Inputs {
            // chsr o timeout set --type tty --duration 00:00:00 --max-usage 1
            action: InputAction::Set,
            role_id,
            task_id,
            options: true,
            timeout_arg: Some(_),
            timeout_type,
            timeout_duration,
            timeout_max_usage,
            ..
        } => set_timeout(
            rconfig,
            role_id.as_ref(),
            task_id,
            timeout_type,
            timeout_duration,
            timeout_max_usage,
        ),

        Inputs {
            // chsr o path setpolicy delete-all
            action: InputAction::Set,
            role_id,
            task_id,
            options_type: Some(OptType::Path),
            options_path_policy: Some(options_path_policy),
            ..
        } => path_setpolicy(rconfig, role_id.as_ref(), task_id, options_path_policy),
        Inputs {
            // chsr o workdir setpolicy none
            action: InputAction::Set,
            role_id,
            task_id,
            options_type: Some(OptType::Workdir),
            options_workdir_policy: Some(options_workdir_policy),
            ..
        } => workdir_setpolicy(rconfig, role_id.as_ref(), task_id, options_workdir_policy),

        Inputs {
            // chsr o env whitelist add A
            action,
            role_id,
            task_id,
            options_key_env,
            options_env_values,
            options_type: Some(OptType::Env),
            setlist_type,
            options_env_policy: None,
            ..
        } => env_setlist_add(
            rconfig,
            role_id.as_ref(),
            task_id,
            setlist_type,
            action,
            options_key_env,
            options_env_values,
        ),

        Inputs {
            // chsr o path whitelist add path1:path2:path3
            action,
            role_id,
            task_id,
            options_path: Some(options_path),
            options_type: Some(OptType::Path),
            setlist_type,
            ..
        } => path_setlist2(
            rconfig,
            role_id.as_ref(),
            task_id,
            setlist_type,
            action,
            &options_path,
        ),

        Inputs {
            // chsr o workdir set /home/user
            action: InputAction::Set,
            role_id,
            task_id,
            options_path: Some(options_path),
            options_type: Some(OptType::Workdir),
            setlist_type: None,
            ..
        } => workdir_set_path(rconfig, role_id.as_ref(), task_id, options_path.as_str()),

        Inputs {
            // chsr o workdir whitelist add /home/user
            action,
            role_id,
            task_id,
            options_path: Some(options_path),
            options_type: Some(OptType::Workdir),
            setlist_type,
            ..
        } => workdir_setlist(
            rconfig,
            role_id.as_ref(),
            task_id,
            setlist_type,
            action,
            &options_path,
        ),

        Inputs {
            // chsr o env setpolicy delete-all
            role_id,
            task_id,
            options_type: Some(OptType::Env),
            options_env_policy: Some(options_env_policy),
            options_key_env: None,
            ..
        } => env_setpolicy(rconfig, role_id.as_ref(), task_id, options_env_policy),
        _ => Err("Unknown Input".into()),
    }
}
pub fn perform_on_target_opt(
    rconfig: &Rc<RefCell<rootasrole_core::database::structs::SPolicy>>,
    role_id: Option<&String>,
    task_id: Option<IdTask>,
    exec_on_opt: impl Fn(Rc<RefCell<Opt>>) -> Result<(), Box<dyn Error>>,
) -> Result<(), Box<dyn Error>> {
    // Helper function to execute on option or create a new one
    fn execute_or_create_option(
        exec_on_opt: impl Fn(Rc<RefCell<Opt>>) -> Result<(), Box<dyn Error>>,
        options: &mut Option<Rc<RefCell<Opt>>>,
    ) -> Result<(), Box<dyn Error>> {
        if let Some(opt) = options {
            exec_on_opt(opt.clone())
        } else {
            let new_opt = Rc::new(RefCell::new(Opt::default()));
            let result = exec_on_opt(new_opt.clone());
            *options = Some(new_opt);
            result
        }
    }

    // If role_id is provided, find the role
    if let Some(role_id) = role_id {
        let role = rconfig.role(role_id).ok_or("Role not found")?;
        let mut role_borrowed = role.as_ref().borrow_mut();

        // If task_id is provided, find the task
        if let Some(task_id) = task_id {
            let task = role_borrowed.task(&task_id).ok_or("Task not found")?;
            return execute_or_create_option(exec_on_opt, &mut task.as_ref().borrow_mut().options);
        }
        // No task_id, use role options
        return execute_or_create_option(exec_on_opt, &mut role_borrowed.options);
    }

    // No role_id, use global config options
    execute_or_create_option(exec_on_opt, &mut rconfig.as_ref().borrow_mut().options)
}