|
| 1 | +/* |
| 2 | + Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. |
| 3 | +
|
| 4 | + This program is free software; you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation; version 2 of the License. |
| 7 | +
|
| 8 | + This program is distributed in the hope that it will be useful, |
| 9 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + GNU General Public License for more details. |
| 12 | +
|
| 13 | + You should have received a copy of the GNU General Public License |
| 14 | + along with this program; if not, write to the Free Software |
| 15 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | +*/ |
| 17 | +#include <string> |
| 18 | +#include <iostream> |
| 19 | +#include <sstream> |
| 20 | +#include <map> |
| 21 | + |
| 22 | +#include <my_rnd.h> |
| 23 | +#include "my_aes.h" |
| 24 | + |
| 25 | +#define ERR_FILE 1 // File related error |
| 26 | +#define ERR_ENCRYPTION 2 // Encryption related error |
| 27 | +#define ERR_SYNTAX 3 // Syntax and parsing related error |
| 28 | +#define ERR_OTHER 4 // Unspecified error |
| 29 | +#define ALL_OK 0 // Reporting success and good fortune |
| 30 | + |
| 31 | +/** |
| 32 | + Trivial parser for the login.cnf file which assumes that first entry |
| 33 | + is a [client] header followed by some attribute/value -pairs |
| 34 | +
|
| 35 | + @param sin Input stream |
| 36 | + @param[out] options Output map |
| 37 | + @return success rate |
| 38 | + @retval ALL_OK Reporting success and good fortune |
| 39 | + @retval ERR_SYNTAX Failed to parse the stream |
| 40 | +*/ |
| 41 | +int parse_cnf_file(std::istream &sin, |
| 42 | + std::map<std::string, std::string > *options, |
| 43 | + const std::string &header); |
| 44 | +/** |
| 45 | + Decrypts a file and produces a stringstream. |
| 46 | +
|
| 47 | + @param fin Input stream |
| 48 | + @param[out] sout Output stream |
| 49 | + @return success rate |
| 50 | + @retval ALL_OK Reporting success and good fortune |
| 51 | + @retval ERR_ENCRYPTION Failed to decrypt the input stream |
| 52 | +*/ |
| 53 | +int decrypt_login_cnf_file(std::istream &fin, std::ostream &sout); |
| 54 | + |
| 55 | +void generate_password(std::string *password, int size); |
| 56 | +void trim(std::string *s); |
| 57 | +const std::string get_allowed_pwd_chars(); |
| 58 | + |
| 59 | +/** |
| 60 | + An experimental uniform representation of access privileges in MySQL |
| 61 | +*/ |
| 62 | +class Access_privilege |
| 63 | +{ |
| 64 | +public: |
| 65 | + Access_privilege() : m_priv(0) {} |
| 66 | + Access_privilege(uint64_t privileges) : m_priv(privileges) {} |
| 67 | + Access_privilege(const Access_privilege &priv) : m_priv(priv.m_priv) {} |
| 68 | + bool has_select_ac() { return (m_priv & (1L)) > 0; } |
| 69 | + bool has_insert_ac() { return (m_priv & (1L << 1)) > 0; } |
| 70 | + bool has_update_ac() { return (m_priv & (1L << 2)) > 0; } |
| 71 | + bool has_delete_ac() { return (m_priv & (1L << 3)) > 0; } |
| 72 | + bool has_create_ac() { return (m_priv & (1L << 4)) > 0; } |
| 73 | + bool has_drop_ac() { return (m_priv & (1L << 5)) > 0; } |
| 74 | + bool has_relead_ac() { return (m_priv & (1L << 6)) > 0; } |
| 75 | + bool has_shutdown_ac() { return (m_priv & (1L << 7)) > 0; } |
| 76 | + bool has_process_ac() { return (m_priv & (1L << 8)) > 0; } |
| 77 | + bool has_file_ac() { return (m_priv & (1L << 9)) > 0; } |
| 78 | + bool has_grant_ac() { return (m_priv & (1L << 10)) > 0; } |
| 79 | + bool has_references_ac() { return (m_priv & (1L << 11)) > 0; } |
| 80 | + bool has_index_ac() { return (m_priv & (1L << 12)) > 0; } |
| 81 | + bool has_alter_ac() { return (m_priv & (1L << 13)) > 0; } |
| 82 | + bool has_show_db_ac() { return (m_priv & (1L << 14)) > 0; } |
| 83 | + bool has_super_ac() { return (m_priv & (1L << 15)) > 0; } |
| 84 | + bool has_create_tmp_ac() { return (m_priv & (1L << 16)) > 0; } |
| 85 | + bool has_lock_tables_ac() { return (m_priv & (1L << 17)) > 0; } |
| 86 | + bool has_execute_ac() { return (m_priv & (1L << 18)) > 0; } |
| 87 | + bool has_repl_slave_ac() { return (m_priv & (1L << 19)) > 0; } |
| 88 | + bool has_repl_client_ac() { return (m_priv & (1L << 20)) > 0; } |
| 89 | + bool has_create_view_ac() { return (m_priv & (1L << 21)) > 0; } |
| 90 | + bool has_show_view_ac() { return (m_priv & (1L << 22)) > 0; } |
| 91 | + bool has_create_proc_ac() { return (m_priv & (1L << 23)) > 0; } |
| 92 | + bool has_alter_proc_ac() { return (m_priv & (1L << 24)) > 0; } |
| 93 | + bool has_create_user_ac() { return (m_priv & (1L << 25)) > 0; } |
| 94 | + bool has_event_ac() { return (m_priv & (1L << 26)) > 0; } |
| 95 | + bool has_trigger_ac() { return (m_priv & (1L << 27)) > 0; } |
| 96 | + bool has_create_tablespace_ac() { return (m_priv & (1L << 28)) > 0; } |
| 97 | + inline static uint64_t select_ac() { return (1L); } |
| 98 | + inline uint64_t insert_ac() { return (1L << 1); } |
| 99 | + inline uint64_t update_ac() { return (1L << 2); } |
| 100 | + inline uint64_t delete_ac() { return (1L << 3); } |
| 101 | + inline static uint64_t create_ac() { return (1L << 4); } |
| 102 | + inline static uint64_t drop_ac() { return (1L << 5); } |
| 103 | + inline static uint64_t relead_ac() { return (1L << 6); } |
| 104 | + inline static uint64_t shutdown_ac() { return (1L << 7); } |
| 105 | + inline static uint64_t process_ac() { return (1L << 8); } |
| 106 | + inline static uint64_t file_ac() { return (1L << 9); } |
| 107 | + inline static uint64_t grant_ac() { return (1L << 10); } |
| 108 | + inline static uint64_t references_ac() { return (1L << 11); } |
| 109 | + inline static uint64_t index_ac() { return (1L << 12); } |
| 110 | + inline static uint64_t alter_ac() { return (1L << 13); } |
| 111 | + inline static uint64_t show_db_ac() { return (1L << 14); } |
| 112 | + inline static uint64_t super_ac() { return (1L << 15); } |
| 113 | + inline static uint64_t create_tmp_ac() { return (1L << 16); } |
| 114 | + inline static uint64_t lock_tables_ac() { return (1L << 17); } |
| 115 | + inline static uint64_t execute_ac() { return (1L << 18); } |
| 116 | + inline static uint64_t repl_slave_ac() { return (1L << 19); } |
| 117 | + inline static uint64_t repl_client_ac() { return (1L << 20); } |
| 118 | + inline static uint64_t create_view_ac() { return (1L << 21); } |
| 119 | + inline static uint64_t show_view_ac() { return (1L << 22); } |
| 120 | + inline static uint64_t create_proc_ac() { return (1L << 23); } |
| 121 | + inline static uint64_t alter_proc_ac() { return (1L << 24); } |
| 122 | + inline static uint64_t create_user_ac() { return (1L << 25); } |
| 123 | + inline static uint64_t event_ac() { return (1L << 26); } |
| 124 | + inline static uint64_t trigger_ac() { return (1L << 27); } |
| 125 | + inline static uint64_t create_tablespace_ac() { return (1L << 28); } |
| 126 | + inline static uint64_t acl_all() { return 0xfffffff; } |
| 127 | + uint64_t to_int() const { return m_priv; }; |
| 128 | +private: |
| 129 | + uint64_t m_priv; |
| 130 | +}; |
0 commit comments