Skip to content

Commit f23bf39

Browse files
author
Kristofer Pettersson
committed
* Datadir now created with the euid owner if specified and running with sufficient priv.
* Disabled default signal handler for SIGPIPE * Adjusted process handling for stability. Non-blocking mode seems to cause saturation and EPIPE failure. Using blocking mode seems to work better. * Language dir is not identified by the mandatory "english" subdirectory * Fixed File_exist functor so that path isn't modified if the target isn't found * introduced proper escaping of strings when creating sql * Created defines for constants * moved Access_privileges and option parser code to auth_utils.cc * moved Path helper class to path.h * Added unittest for auth_utils and Path helper class
1 parent eafb818 commit f23bf39

File tree

8 files changed

+844
-438
lines changed

8 files changed

+844
-438
lines changed

client/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ TARGET_LINK_LIBRARIES(mysql_config_editor mysqlclient)
7878
MYSQL_ADD_EXECUTABLE(mysql_secure_installation mysql_secure_installation.cc)
7979
TARGET_LINK_LIBRARIES(mysql_secure_installation mysqlclient)
8080

81-
MYSQL_ADD_EXECUTABLE(mysql_install_db mysql_install_db.cc)
81+
MYSQL_ADD_EXECUTABLE(mysql_install_db mysql_install_db.cc auth_utils.cc)
8282
TARGET_LINK_LIBRARIES(mysql_install_db mysqlclient)
8383

8484
# "WIN32" also covers 64 bit. "echo" is used in some files below "mysql-test/".

client/auth_utils.cc

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 <fstream>
18+
#include <stdint.h>
19+
#include "auth_utils.h"
20+
21+
using namespace std;
22+
/**
23+
Lazy whitespace trimmer
24+
*/
25+
void trim(string *s)
26+
{
27+
stringstream trimmer;
28+
trimmer << *s;
29+
s->clear();
30+
trimmer >> *s;
31+
}
32+
33+
int parse_cnf_file(istream &sin, map<string, string > *options,
34+
const string &header)
35+
{
36+
string option_name;
37+
string option_value;
38+
string token_header;
39+
token_header.append("[").append(header).append("]");
40+
try{
41+
while(true)
42+
{
43+
string row;
44+
getline(sin, row);
45+
trim(&row);
46+
if (row == token_header)
47+
break;
48+
else if (sin.eof())
49+
return ERR_SYNTAX;
50+
}
51+
52+
while (!getline(sin, option_name, '=').eof())
53+
{
54+
trim(&option_name);
55+
if (option_name[0] == '[')
56+
break;
57+
getline(sin, option_value);
58+
trim(&option_value);
59+
if (option_name.length() > 0)
60+
options->insert(make_pair<string, string >(option_name, option_value));
61+
}
62+
return ALL_OK;
63+
} catch(...)
64+
{
65+
return ERR_SYNTAX;
66+
}
67+
}
68+
69+
int decrypt_login_cnf_file(istream &fin, ostream &sout)
70+
{
71+
try {
72+
fin.seekg(4, fin.beg);
73+
char rkey[20];
74+
fin.read(rkey, 20);
75+
while(true)
76+
{
77+
uint32_t len;
78+
fin.read((char*)&len,4);
79+
if (len == 0 || fin.eof())
80+
break;
81+
char *cipher= new char[len];
82+
fin.read(cipher, len);
83+
char plain[1024];
84+
int aes_length;
85+
aes_length= my_aes_decrypt((const unsigned char *) cipher, len,
86+
(unsigned char *) plain,
87+
(const unsigned char *) rkey,
88+
20, my_aes_128_ecb, NULL);
89+
plain[aes_length]= 0;
90+
sout << plain;
91+
92+
}
93+
return ALL_OK;
94+
95+
} catch(...)
96+
{
97+
return ERR_ENCRYPTION;
98+
}
99+
}
100+
101+
const string g_allowed_pwd_chars("qwertyuiopasdfghjklzxcvbnm,.-1234567890+*"
102+
"QWERTYUIOPASDFGHJKLZXCVBNM;:_!#%&/()=?><");
103+
const string get_allowed_pwd_chars() { return g_allowed_pwd_chars; }
104+
105+
void generate_password(string *password, int size)
106+
{
107+
stringstream ss;
108+
rand_struct srnd;
109+
while(size > 0)
110+
{
111+
int ch= ((int)(my_rnd_ssl(&srnd)*100))%get_allowed_pwd_chars().size();
112+
ss << get_allowed_pwd_chars()[ch];
113+
--size;
114+
}
115+
password->assign(ss.str());
116+
}

client/auth_utils.h

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)