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
#![allow(bare_trait_objects)]
#![allow(renamed_and_removed_lints)]
#![allow(single_use_lifetimes)]
#![allow(variant_size_differences)]
use crate::version::Version;
use std::fmt;
use sudo_plugin_sys as sys;
use libc::c_int;
use error_chain::*;
pub use error_chain::bail;
#[derive(Clone, Copy, Debug)]
pub enum IoFacility {
PluginPrintf,
Conversation,
}
impl fmt::Display for IoFacility {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
IoFacility::PluginPrintf => write!(f, "plugin_printf"),
IoFacility::Conversation => write!(f, "conversation"),
}
}
}
error_chain! {
errors {
UnsupportedApiVersion(cur: Version) {
description("sudo doesn't support the minimum plugin API version required by this plugin"),
display("sudo called this plugin with an API version of {}, but a minimum of {} is required", cur, Version::minimum()),
}
Uninitialized {
description("the plugin failed to initialize"),
display("the plugin failed to initialize"),
}
Unauthorized {
description("command unauthorized"),
display("command unauthorized"),
}
}
}
pub trait AsSudoPluginRetval {
fn as_sudo_io_plugin_open_retval(&self) -> c_int;
fn as_sudo_io_plugin_log_retval(&self) -> c_int;
}
impl<T, E: AsSudoPluginRetval> AsSudoPluginRetval for ::std::result::Result<T, E> {
fn as_sudo_io_plugin_open_retval(&self) -> c_int {
match *self {
Ok(_) => sys::SUDO_PLUGIN_OPEN_SUCCESS,
Err(ref e) => e.as_sudo_io_plugin_open_retval(),
}
}
fn as_sudo_io_plugin_log_retval(&self) -> c_int {
match *self {
Ok(_) => sys::SUDO_PLUGIN_OPEN_SUCCESS,
Err(ref e) => e.as_sudo_io_plugin_log_retval(),
}
}
}
impl AsSudoPluginRetval for Error {
fn as_sudo_io_plugin_open_retval(&self) -> c_int {
match *self {
Error(ErrorKind::Unauthorized, _) => sys::SUDO_PLUGIN_OPEN_GENERAL_ERROR,
Error(_, _) => sys::SUDO_PLUGIN_OPEN_FAILURE,
}
}
fn as_sudo_io_plugin_log_retval(&self) -> c_int {
match *self {
Error(ErrorKind::Unauthorized, _) => sys::SUDO_PLUGIN_OPEN_FAILURE,
Error(_, _) => sys::SUDO_PLUGIN_OPEN_GENERAL_ERROR,
}
}
}