2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2019-01-30 14:14:20 +00:00
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
2020-04-17 09:04:36 +00:00
|
|
|
use proxmox::api::{api, RpcEnvironment, Permission, UserInformation};
|
2020-01-21 11:28:01 +00:00
|
|
|
use proxmox::api::router::{Router, SubdirMap};
|
2020-04-09 11:34:07 +00:00
|
|
|
use proxmox::{sortable, identity};
|
2020-01-21 11:28:01 +00:00
|
|
|
use proxmox::{http_err, list_subdirs_api_method};
|
2019-11-21 12:10:49 +00:00
|
|
|
|
2019-01-30 14:14:20 +00:00
|
|
|
use crate::tools;
|
|
|
|
use crate::tools::ticket::*;
|
|
|
|
use crate::auth_helpers::*;
|
2020-04-09 08:19:38 +00:00
|
|
|
use crate::api2::types::*;
|
2020-04-17 09:04:36 +00:00
|
|
|
|
2020-04-16 08:01:59 +00:00
|
|
|
use crate::config::cached_user_info::CachedUserInfo;
|
2020-07-21 09:10:37 +00:00
|
|
|
use crate::config::acl::{PRIVILEGES, PRIV_PERMISSIONS_MODIFY};
|
2020-04-09 08:19:38 +00:00
|
|
|
|
|
|
|
pub mod user;
|
2020-04-09 09:36:45 +00:00
|
|
|
pub mod domain;
|
2020-04-13 09:09:44 +00:00
|
|
|
pub mod acl;
|
2020-04-17 12:03:24 +00:00
|
|
|
pub mod role;
|
2019-01-30 14:14:20 +00:00
|
|
|
|
2020-07-21 09:10:37 +00:00
|
|
|
/// returns Ok(true) if a ticket has to be created
|
|
|
|
/// and Ok(false) if not
|
|
|
|
fn authenticate_user(
|
|
|
|
username: &str,
|
|
|
|
password: &str,
|
|
|
|
path: Option<String>,
|
|
|
|
privs: Option<String>,
|
|
|
|
port: Option<u16>,
|
|
|
|
) -> Result<bool, Error> {
|
2020-04-16 08:01:59 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
|
|
|
|
|
|
|
if !user_info.is_active_user(&username) {
|
|
|
|
bail!("user account disabled or expired.");
|
|
|
|
}
|
|
|
|
|
2019-03-05 11:53:59 +00:00
|
|
|
let ticket_lifetime = tools::ticket::TICKET_LIFETIME;
|
|
|
|
|
|
|
|
if password.starts_with("PBS:") {
|
|
|
|
if let Ok((_age, Some(ticket_username))) = tools::ticket::verify_rsa_ticket(public_auth_key(), "PBS", password, None, -300, ticket_lifetime) {
|
|
|
|
if ticket_username == username {
|
2020-07-21 09:10:37 +00:00
|
|
|
return Ok(true);
|
2019-03-05 11:53:59 +00:00
|
|
|
} else {
|
|
|
|
bail!("ticket login failed - wrong username");
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 09:10:37 +00:00
|
|
|
} else if password.starts_with("PBSTERM:") {
|
|
|
|
if path.is_none() || privs.is_none() || port.is_none() {
|
|
|
|
bail!("cannot check termnal ticket without path, priv and port");
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = path.unwrap();
|
|
|
|
let privilege_name = privs.unwrap();
|
|
|
|
let port = port.unwrap();
|
|
|
|
|
|
|
|
if let Ok((_age, _data)) =
|
|
|
|
tools::ticket::verify_term_ticket(public_auth_key(), &username, &path, port, password)
|
|
|
|
{
|
|
|
|
for (name, privilege) in PRIVILEGES {
|
|
|
|
if *name == privilege_name {
|
|
|
|
let mut path_vec = Vec::new();
|
|
|
|
for part in path.split('/') {
|
|
|
|
if part != "" {
|
|
|
|
path_vec.push(part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
user_info.check_privs(username, &path_vec, *privilege, false)?;
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bail!("No such privilege");
|
|
|
|
}
|
2019-03-05 11:53:59 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 09:10:37 +00:00
|
|
|
let _ = crate::auth::authenticate_user(username, password)?;
|
|
|
|
Ok(true)
|
2019-01-30 14:14:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 13:19:36 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
username: {
|
2020-04-09 08:19:38 +00:00
|
|
|
schema: PROXMOX_USER_ID_SCHEMA,
|
2019-11-27 13:19:36 +00:00
|
|
|
},
|
|
|
|
password: {
|
2020-04-09 08:19:38 +00:00
|
|
|
schema: PASSWORD_SCHEMA,
|
2019-11-27 13:19:36 +00:00
|
|
|
},
|
2020-07-21 09:10:37 +00:00
|
|
|
path: {
|
|
|
|
type: String,
|
|
|
|
description: "Path for verifying terminal tickets.",
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
privs: {
|
|
|
|
type: String,
|
|
|
|
description: "Privilege for verifying terminal tickets.",
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
port: {
|
|
|
|
type: Integer,
|
|
|
|
description: "Port for verifying terminal tickets.",
|
|
|
|
optional: true,
|
|
|
|
},
|
2019-11-27 09:05:37 +00:00
|
|
|
},
|
|
|
|
},
|
2019-11-27 13:19:36 +00:00
|
|
|
returns: {
|
|
|
|
properties: {
|
|
|
|
username: {
|
|
|
|
type: String,
|
|
|
|
description: "User name.",
|
|
|
|
},
|
|
|
|
ticket: {
|
|
|
|
type: String,
|
|
|
|
description: "Auth ticket.",
|
|
|
|
},
|
|
|
|
CSRFPreventionToken: {
|
|
|
|
type: String,
|
|
|
|
description: "Cross Site Request Forgery Prevention Token.",
|
|
|
|
},
|
2019-11-27 09:05:37 +00:00
|
|
|
},
|
|
|
|
},
|
2019-11-27 13:19:36 +00:00
|
|
|
protected: true,
|
2020-04-16 08:01:59 +00:00
|
|
|
access: {
|
|
|
|
permission: &Permission::World,
|
|
|
|
},
|
2019-11-27 13:19:36 +00:00
|
|
|
)]
|
2019-11-27 09:05:37 +00:00
|
|
|
/// Create or verify authentication ticket.
|
|
|
|
///
|
|
|
|
/// Returns: An authentication ticket with additional infos.
|
2020-07-21 09:10:37 +00:00
|
|
|
fn create_ticket(
|
|
|
|
username: String,
|
|
|
|
password: String,
|
|
|
|
path: Option<String>,
|
|
|
|
privs: Option<String>,
|
|
|
|
port: Option<u16>,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
match authenticate_user(&username, &password, path, privs, port) {
|
|
|
|
Ok(true) => {
|
|
|
|
let ticket = assemble_rsa_ticket(private_auth_key(), "PBS", Some(&username), None)?;
|
2019-01-30 14:14:20 +00:00
|
|
|
|
2019-11-29 08:51:27 +00:00
|
|
|
let token = assemble_csrf_prevention_token(csrf_secret(), &username);
|
2019-01-30 14:14:20 +00:00
|
|
|
|
|
|
|
log::info!("successful auth for user '{}'", username);
|
|
|
|
|
2019-10-26 09:36:01 +00:00
|
|
|
Ok(json!({
|
2019-01-30 14:14:20 +00:00
|
|
|
"username": username,
|
|
|
|
"ticket": ticket,
|
|
|
|
"CSRFPreventionToken": token,
|
2019-10-26 09:36:01 +00:00
|
|
|
}))
|
2019-01-30 14:14:20 +00:00
|
|
|
}
|
2020-07-21 09:10:37 +00:00
|
|
|
Ok(false) => Ok(json!({
|
|
|
|
"username": username,
|
|
|
|
})),
|
2019-01-30 14:14:20 +00:00
|
|
|
Err(err) => {
|
2019-11-29 08:52:15 +00:00
|
|
|
let client_ip = "unknown"; // $rpcenv->get_client_ip() || '';
|
2019-01-30 14:14:20 +00:00
|
|
|
log::error!("authentication failure; rhost={} user={} msg={}", client_ip, username, err.to_string());
|
2019-10-26 09:36:01 +00:00
|
|
|
Err(http_err!(UNAUTHORIZED, "permission check failed.".into()))
|
2019-01-30 14:14:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 08:19:38 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
userid: {
|
|
|
|
schema: PROXMOX_USER_ID_SCHEMA,
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
schema: PASSWORD_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-04-16 08:01:59 +00:00
|
|
|
access: {
|
2020-04-17 09:04:36 +00:00
|
|
|
description: "Anybody is allowed to change there own password. In addition, users with 'Permissions:Modify' privilege may change any password.",
|
2020-04-16 08:01:59 +00:00
|
|
|
permission: &Permission::Anybody,
|
|
|
|
},
|
|
|
|
|
2020-04-09 08:19:38 +00:00
|
|
|
)]
|
|
|
|
/// Change user password
|
|
|
|
///
|
|
|
|
/// Each user is allowed to change his own password. Superuser
|
|
|
|
/// can change all passwords.
|
|
|
|
fn change_password(
|
|
|
|
userid: String,
|
|
|
|
password: String,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let current_user = rpcenv.get_user()
|
|
|
|
.ok_or_else(|| format_err!("unknown user"))?;
|
|
|
|
|
|
|
|
let mut allowed = userid == current_user;
|
|
|
|
|
|
|
|
if userid == "root@pam" { allowed = true; }
|
|
|
|
|
2020-04-17 09:04:36 +00:00
|
|
|
if !allowed {
|
|
|
|
let user_info = CachedUserInfo::new()?;
|
|
|
|
let privs = user_info.lookup_privs(¤t_user, &[]);
|
|
|
|
if (privs & PRIV_PERMISSIONS_MODIFY) != 0 { allowed = true; }
|
|
|
|
}
|
|
|
|
|
2020-04-09 08:19:38 +00:00
|
|
|
if !allowed {
|
|
|
|
bail!("you are not authorized to change the password.");
|
|
|
|
}
|
|
|
|
|
|
|
|
let (username, realm) = crate::auth::parse_userid(&userid)?;
|
|
|
|
let authenticator = crate::auth::lookup_authenticator(&realm)?;
|
|
|
|
authenticator.store_password(&username, &password)?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2020-04-09 11:34:07 +00:00
|
|
|
const SUBDIRS: SubdirMap = &sorted!([
|
2020-04-13 09:09:44 +00:00
|
|
|
("acl", &acl::ROUTER),
|
2020-04-09 08:19:38 +00:00
|
|
|
(
|
|
|
|
"password", &Router::new()
|
|
|
|
.put(&API_METHOD_CHANGE_PASSWORD)
|
|
|
|
),
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
|
|
|
"ticket", &Router::new()
|
2019-11-27 09:05:37 +00:00
|
|
|
.post(&API_METHOD_CREATE_TICKET)
|
2020-04-09 08:19:38 +00:00
|
|
|
),
|
2020-04-09 09:36:45 +00:00
|
|
|
("domains", &domain::ROUTER),
|
2020-04-17 12:03:24 +00:00
|
|
|
("roles", &role::ROUTER),
|
2020-04-09 08:19:38 +00:00
|
|
|
("users", &user::ROUTER),
|
2020-04-09 11:34:07 +00:00
|
|
|
]);
|
2019-11-21 08:36:41 +00:00
|
|
|
|
|
|
|
pub const ROUTER: Router = Router::new()
|
|
|
|
.get(&list_subdirs_api_method!(SUBDIRS))
|
|
|
|
.subdirs(SUBDIRS);
|