mysqladm/cli/mysql_admutils_compatibility/
mysql_dbadm.rs

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
use clap::Parser;
use futures_util::{SinkExt, StreamExt};
use std::os::unix::net::UnixStream as StdUnixStream;
use std::path::PathBuf;
use tokio::net::UnixStream as TokioUnixStream;

use crate::{
    cli::{
        common::erroneous_server_response,
        database_command,
        mysql_admutils_compatibility::{
            common::trim_db_name_to_32_chars,
            error_messages::{
                format_show_database_error_message, handle_create_database_error,
                handle_drop_database_error,
            },
        },
    },
    core::{
        bootstrap::bootstrap_server_connection_and_drop_privileges,
        protocol::{
            create_client_to_server_message_stream, ClientToServerMessageStream,
            GetDatabasesPrivilegeDataError, MySQLDatabase, Request, Response,
        },
    },
    server::sql::database_privilege_operations::DatabasePrivilegeRow,
};

const HELP_DB_PERM: &str = r#"
Edit permissions for the DATABASE(s). Running this command will
spawn the editor stored in the $EDITOR environment variable.
(pico will be used if the variable is unset)

The file should contain one line per user, starting with the
username and followed by ten Y/N-values seperated by whitespace.
Lines starting with # are ignored.

The Y/N-values corresponds to the following mysql privileges:
  Select     - Enables use of SELECT
  Insert     - Enables use of INSERT
  Update     - Enables use of UPDATE
  Delete     - Enables use of DELETE
  Create     - Enables use of CREATE TABLE
  Drop       - Enables use of DROP TABLE
  Alter      - Enables use of ALTER TABLE
  Index      - Enables use of CREATE INDEX and DROP INDEX
  Temp       - Enables use of CREATE TEMPORARY TABLE
  Lock       - Enables use of LOCK TABLE
  References - Enables use of REFERENCES
"#;

/// Create, drop or edit permissions for the DATABASE(s),
/// as determined by the COMMAND.
///
/// This is a compatibility layer for the mysql-dbadm command.
/// Please consider using the newer mysqladm command instead.
#[derive(Parser)]
#[command(
    bin_name = "mysql-dbadm",
    version,
    about,
    disable_help_subcommand = true,
    verbatim_doc_comment
)]
pub struct Args {
    #[command(subcommand)]
    pub command: Option<Command>,

    /// Path to the socket of the server, if it already exists.
    #[arg(
        short,
        long,
        value_name = "PATH",
        global = true,
        hide_short_help = true
    )]
    server_socket_path: Option<PathBuf>,

    /// Config file to use for the server.
    #[arg(
        short,
        long,
        value_name = "PATH",
        global = true,
        hide_short_help = true
    )]
    config: Option<PathBuf>,

    /// Print help for the 'editperm' subcommand.
    #[arg(long, global = true)]
    pub help_editperm: bool,
}

// NOTE: mysql-dbadm explicitly calls privileges "permissions".
//       This is something we're trying to move away from.
//       See https://git.pvv.ntnu.no/Projects/mysqladm-rs/issues/29
#[derive(Parser)]
pub enum Command {
    /// create the DATABASE(s).
    Create(CreateArgs),

    /// delete the DATABASE(s).
    Drop(DatabaseDropArgs),

    /// give information about the DATABASE(s), or, if
    /// none are given, all the ones you own.
    Show(DatabaseShowArgs),

    // TODO: make this output more verbatim_doc_comment-like,
    //       without messing up the indentation.
    /// change permissions for the DATABASE(s). Your
    /// favorite editor will be started, allowing you
    /// to make changes to the permission table.
    /// Run 'mysql-dbadm --help-editperm' for more
    /// information.
    Editperm(EditPermArgs),
}

#[derive(Parser)]
pub struct CreateArgs {
    /// The name of the DATABASE(s) to create.
    #[arg(num_args = 1..)]
    name: Vec<MySQLDatabase>,
}

#[derive(Parser)]
pub struct DatabaseDropArgs {
    /// The name of the DATABASE(s) to drop.
    #[arg(num_args = 1..)]
    name: Vec<MySQLDatabase>,
}

#[derive(Parser)]
pub struct DatabaseShowArgs {
    /// The name of the DATABASE(s) to show.
    #[arg(num_args = 0..)]
    name: Vec<MySQLDatabase>,
}

#[derive(Parser)]
pub struct EditPermArgs {
    /// The name of the DATABASE to edit permissions for.
    pub database: MySQLDatabase,
}

pub fn main() -> anyhow::Result<()> {
    let args: Args = Args::parse();

    if args.help_editperm {
        println!("{}", HELP_DB_PERM);
        return Ok(());
    }

    let server_connection =
        bootstrap_server_connection_and_drop_privileges(args.server_socket_path, args.config)?;

    let command = match args.command {
        Some(command) => command,
        None => {
            println!(
                "Try `{} --help' for more information.",
                std::env::args().next().unwrap_or("mysql-dbadm".to_string())
            );
            return Ok(());
        }
    };

    tokio_run_command(command, server_connection)?;

    Ok(())
}

fn tokio_run_command(command: Command, server_connection: StdUnixStream) -> anyhow::Result<()> {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let tokio_socket = TokioUnixStream::from_std(server_connection)?;
            let message_stream = create_client_to_server_message_stream(tokio_socket);
            match command {
                Command::Create(args) => create_databases(args, message_stream).await,
                Command::Drop(args) => drop_databases(args, message_stream).await,
                Command::Show(args) => show_databases(args, message_stream).await,
                Command::Editperm(args) => {
                    let edit_privileges_args = database_command::DatabaseEditPrivsArgs {
                        name: Some(args.database),
                        privs: vec![],
                        json: false,
                        editor: None,
                        yes: false,
                    };

                    database_command::edit_database_privileges(edit_privileges_args, message_stream)
                        .await
                }
            }
        })
}

async fn create_databases(
    args: CreateArgs,
    mut server_connection: ClientToServerMessageStream,
) -> anyhow::Result<()> {
    let database_names = args.name.iter().map(trim_db_name_to_32_chars).collect();

    let message = Request::CreateDatabases(database_names);
    server_connection.send(message).await?;

    let result = match server_connection.next().await {
        Some(Ok(Response::CreateDatabases(result))) => result,
        response => return erroneous_server_response(response),
    };

    server_connection.send(Request::Exit).await?;

    for (name, result) in result {
        match result {
            Ok(()) => println!("Database {} created.", name),
            Err(err) => handle_create_database_error(err, &name),
        }
    }

    Ok(())
}

async fn drop_databases(
    args: DatabaseDropArgs,
    mut server_connection: ClientToServerMessageStream,
) -> anyhow::Result<()> {
    let database_names = args.name.iter().map(trim_db_name_to_32_chars).collect();

    let message = Request::DropDatabases(database_names);
    server_connection.send(message).await?;

    let result = match server_connection.next().await {
        Some(Ok(Response::DropDatabases(result))) => result,
        response => return erroneous_server_response(response),
    };

    server_connection.send(Request::Exit).await?;

    for (name, result) in result {
        match result {
            Ok(()) => println!("Database {} dropped.", name),
            Err(err) => handle_drop_database_error(err, &name),
        }
    }

    Ok(())
}

async fn show_databases(
    args: DatabaseShowArgs,
    mut server_connection: ClientToServerMessageStream,
) -> anyhow::Result<()> {
    let database_names: Vec<MySQLDatabase> =
        args.name.iter().map(trim_db_name_to_32_chars).collect();

    let message = if database_names.is_empty() {
        let message = Request::ListDatabases(None);
        server_connection.send(message).await?;
        let response = server_connection.next().await;
        let databases = match response {
            Some(Ok(Response::ListAllDatabases(databases))) => databases.unwrap_or(vec![]),
            response => return erroneous_server_response(response),
        };

        let database_names = databases.into_iter().map(|db| db.database).collect();

        Request::ListPrivileges(Some(database_names))
    } else {
        Request::ListPrivileges(Some(database_names))
    };
    server_connection.send(message).await?;

    let response = server_connection.next().await;

    server_connection.send(Request::Exit).await?;

    // NOTE: mysql-dbadm show has a quirk where valid database names
    //       for non-existent databases will report with no users.
    let results: Vec<Result<(MySQLDatabase, Vec<DatabasePrivilegeRow>), String>> = match response {
        Some(Ok(Response::ListPrivileges(result))) => result
            .into_iter()
            .map(
                |(name, rows)| match rows.map(|rows| (name.to_owned(), rows)) {
                    Ok(rows) => Ok(rows),
                    Err(GetDatabasesPrivilegeDataError::DatabaseDoesNotExist) => Ok((name, vec![])),
                    Err(err) => Err(format_show_database_error_message(err, &name)),
                },
            )
            .collect(),
        response => return erroneous_server_response(response),
    };

    results.into_iter().try_for_each(|result| match result {
        Ok((name, rows)) => print_db_privs(&name, rows),
        Err(err) => {
            eprintln!("{}", err);
            Ok(())
        }
    })?;

    Ok(())
}

#[inline]
fn yn(value: bool) -> &'static str {
    if value {
        "Y"
    } else {
        "N"
    }
}

fn print_db_privs(name: &str, rows: Vec<DatabasePrivilegeRow>) -> anyhow::Result<()> {
    println!(
        concat!(
            "Database '{}':\n",
            "# User                Select  Insert  Update  Delete  Create   Drop   Alter   Index    Temp    Lock  References\n",
            "# ----------------    ------  ------  ------  ------  ------   ----   -----   -----    ----    ----  ----------"
        ),
        name,
    );
    if rows.is_empty() {
        println!("# (no permissions currently granted to any users)");
    } else {
        for privilege in rows {
            println!(
                "  {:<16}      {:<7} {:<7} {:<7} {:<7} {:<7} {:<7} {:<7} {:<7} {:<7} {:<7} {}",
                privilege.user,
                yn(privilege.select_priv),
                yn(privilege.insert_priv),
                yn(privilege.update_priv),
                yn(privilege.delete_priv),
                yn(privilege.create_priv),
                yn(privilege.drop_priv),
                yn(privilege.alter_priv),
                yn(privilege.index_priv),
                yn(privilege.create_tmp_table_priv),
                yn(privilege.lock_tables_priv),
                yn(privilege.references_priv)
            );
        }
    }

    Ok(())
}