forked from trdtnguyen/mysql-pmem
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmysql_install_db.cc
1821 lines (1661 loc) · 51 KB
/
mysql_install_db.cc
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// C headers
#include <stdio.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pwd.h>
#include <signal.h>
#include <spawn.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
// MySQL headers
#include "my_global.h"
#include "my_default.h"
#include "my_getopt.h"
#include "welcome_copyright_notice.h"
#include "mysql_version.h"
#include "auth_utils.h"
#include "path.h"
#include "logger.h"
#include "infix_ostream_it.h"
#include "my_dir.h"
// Additional C++ headers
#include <string>
#include <algorithm>
#include <locale>
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <sstream>
#include <map>
#include <iomanip>
using namespace std;
#include "../scripts/sql_commands_system_tables.h"
#include "../scripts/sql_commands_system_data.h"
#include "../scripts/sql_commands_help_data.h"
#include "../scripts/sql_commands_sys_schema.h"
#define PROGRAM_NAME "mysql_install_db"
#define MYSQLD_EXECUTABLE "mysqld"
#if defined(HAVE_YASSL)
#define MYSQL_CERT_SETUP_EXECUTABLE "mysql_ssl_rsa_setup"
#endif /* HAVE_YASSL */
#define MAX_MYSQLD_ARGUMENTS 10
#define MAX_USER_NAME_LEN 32
char *opt_euid= 0;
char *opt_basedir= 0;
char *opt_datadir= 0;
char *opt_adminlogin= 0;
char *opt_loginpath= 0;
char default_loginpath[]= "client";
char *opt_sqlfile= 0;
char default_adminuser[]= "root";
char *opt_adminuser= 0;
char default_adminhost[]= "localhost";
char *opt_adminhost= 0;
char default_authplugin[]= "mysql_native_password";
char *opt_authplugin= 0;
char *opt_mysqldfile= 0;
#if defined (HAVE_YASSL)
char *opt_mysql_cert_setup_file= 0;
char default_mysql_cert_setup_file[]= MYSQL_CERT_SETUP_EXECUTABLE;
#endif
char *opt_randpwdfile= 0;
char default_randpwfile[]= ".mysql_secret";
char *opt_langpath= 0;
char *opt_lang= 0;
char default_lang[]= "en_US";
char *opt_defaults_file= 0;
char *opt_def_extra_file= 0;
char *opt_builddir= 0;
char *opt_srcdir= 0;
my_bool opt_no_defaults= FALSE;
my_bool opt_insecure= FALSE;
my_bool opt_verbose= FALSE;
my_bool opt_ssl= FALSE;
my_bool opt_skipsys= FALSE;
/**
Connection options.
@note first element must be 'help' and last element must be
the end token: {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
*/
static struct my_option my_connection_options[]=
{
{"help", '?', "Display this help and exit.", 0, 0, 0, GET_NO_ARG,
NO_ARG, 0, 0, 0, 0, 0, 0},
{"user", 'u', "The effective user id used when executing the bootstrap "
"sequence.", &opt_euid, &opt_euid, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0,
0, 0, 0},
{"builddir", 0, "For use with --srcdir and out-of-source builds. Set this to "
"the location of the directory where the built files reside.",
&opt_builddir, 0, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"srcdir", 0, "For internal use. This option specifies the directory under"
" which mysql_install_db looks for support files such as the error"
" message file and the file for populating the help tables.", &opt_srcdir,
0, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"basedir", 0, "The path to the MySQL installation directory.",
&opt_basedir, 0, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"datadir", 0, "The path to the MySQL data directory.", &opt_datadir,
0, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"login-path", 0, "Set the credential category to use with the MySQL password"
" store when setting default credentials. This option takes precedence over "
"admin-user, admin-host options.",
&opt_loginpath, 0, 0, GET_STR_ALLOC, REQUIRED_ARG,
(longlong)&default_loginpath, 0, 0, 0, 0, 0},
{"login-file", 0, "Use the MySQL password store at the specified location "
" to set the default password. This option takes precedence over admin-user, "
"admin-host options. Use the login-path option to change the default "
"credential category (default is 'client').",
&opt_adminlogin, 0, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"extra-sql-file", 'f', "Optional SQL file to execute during bootstrap.",
&opt_sqlfile, 0, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"admin-user", 0, "Username part of the default admin account.",
&opt_adminuser, 0, 0, GET_STR_ALLOC, REQUIRED_ARG,
(longlong)&default_adminuser, 0, 0, 0, 0, 0},
{"admin-host", 0, "Hostname part of the default admin account.",
&opt_adminhost, 0, 0, GET_STR_ALLOC,REQUIRED_ARG,
(longlong)&default_adminhost, 0, 0, 0, 0, 0},
{"admin-auth-plugin", 0, "Plugin to use for the default admin account.",
&opt_authplugin, 0, 0, GET_STR_ALLOC, REQUIRED_ARG,
(longlong)&default_authplugin, 0, 0, 0, 0, 0},
{"admin-require-ssl", 0, "Require SSL/TLS for the default admin account.",
&opt_ssl, 0, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"mysqld-file", 0, "Qualified path to the mysqld binary.", &opt_mysqldfile,
0, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
#if defined(HAVE_YASSL)
{"ssl-setup-file", 0, "Qualified path to the mysql_ssl_setup binary", &opt_mysql_cert_setup_file,
0, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
#endif
{"random-password-file", 0, "Specifies the qualified path to the "
".mysql_secret temporary password file.", &opt_randpwdfile,
0, 0, GET_STR_ALLOC, REQUIRED_ARG, 0,
0, 0, 0, 0, 0},
{"insecure", 0, "Disables random passwords for the default admin account.",
&opt_insecure, 0, 0, GET_BOOL, NO_ARG, 0, 0, 0,
0, 0, 0},
{"verbose", 'v', "Be more verbose when running program.",
&opt_verbose, 0, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"version", 'V', "Print program version and exit.",
&opt_verbose, 0, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"lc-messages-dir", 'l', "Specifies the path to the language files.",
&opt_langpath, 0, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"lc-messages", 0, "Specifies the language to use.", &opt_lang,
0, 0, GET_STR_ALLOC, REQUIRED_ARG, (longlong)&default_lang, 0, 0, 0, 0, 0},
{"skip-sys-schema", 0, "Skip installation of the sys schema.",
&opt_skipsys, 0, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
/* End token */
{0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
};
Log info(cout,"NOTE");
Log error(cerr,"ERROR");
Log warning(cout, "WARNING");
/**
Escapes quotes and backslash.
@param str The string which needs to be quoted
@note This is not a replacement for the mysql_real_escape_string() function
as it only does what is necessary for this application.
@return A quoted copy of the original string.
*/
string escape_string(string str)
{
string esc("'\"\\");
for(string::iterator it= esc.begin(); it != esc.end(); ++it)
{
string::size_type idx= 0;
while ((idx= str.find(*it, idx)) != string::npos)
{
str.insert(idx, 1, '\\');
idx +=2;
}
}
return str;
}
/**
*/
struct Proxy_user
{
Proxy_user(string opt_host, string opt_user) : host(opt_host), user(opt_user)
{}
string host;
string user;
void to_str(string *sql)
{
sql->clear();
sql->append("INSERT INTO proxies_priv VALUES ('");
sql->append(escape_string(host)).
append("','");
sql->append(escape_string(user)).
append("','','',TRUE,'',now());\n");
}
};
/**
A trivial container for attributes associated with the creation of a MySQL
user.
*/
struct Sql_user
{
Sql_user(string opt_host,
string opt_user,
string opt_password,
Access_privilege opt_priv,
string opt_ssl_type,
string opt_ssl_cipher,
string opt_x509_issuer,
string opt_x509_subject,
int opt_max_questions,
int opt_max_updates,
int opt_max_connections,
int opt_max_user_connections,
string opt_plugin,
string opt_authentication_string,
bool opt_password_expired,
int opt_password_lifetime) :
host(opt_host),
user(opt_user),
password(opt_password),
priv(opt_priv),
ssl_type(opt_ssl_type),
ssl_cipher(opt_ssl_cipher),
x509_issuer(opt_x509_issuer),
x509_subject(opt_x509_subject),
max_questions(opt_max_questions),
max_updates(opt_max_updates),
max_connections(opt_max_connections),
max_user_connections(opt_max_user_connections),
plugin(opt_plugin),
authentication_string(opt_authentication_string),
password_expired(opt_password_expired),
password_lifetime(opt_password_lifetime) {}
string host;
string user;
string password;
Access_privilege priv;
string ssl_type;
string ssl_cipher;
string x509_issuer;
string x509_subject;
int max_questions;
int max_updates;
int max_connections;
int max_user_connections;
string plugin;
string authentication_string;
bool password_expired;
int password_lifetime;
void to_sql(string *cmdstr)
{
stringstream set_passcmd,ss, flush_priv;
ss << "INSERT INTO mysql.user VALUES ("
<< "'" << escape_string(host) << "','" << escape_string(user) << "',";
uint64_t acl= priv.to_int();
for(int i= 0; i< NUM_ACLS; ++i)
{
if( (acl & (1L << i)) != 0 )
ss << "'Y',";
else
ss << "'N',";
}
ss << "'" << escape_string(ssl_type) << "',"
<< "'" << escape_string(ssl_cipher) << "',"
<< "'" << escape_string(x509_issuer) << "',"
<< "'" << escape_string(x509_subject) << "',"
<< max_questions << ","
<< max_updates << ","
<< max_connections << ","
<< max_user_connections << ","
<< "'" << plugin << "',";
ss << "'',";
if (password_expired)
ss << "'Y',";
else
ss << "'N',";
ss << "now(), NULL, 'N');\n";
flush_priv << "FLUSH PRIVILEGES;\n";
if (password_expired)
{
set_passcmd << "ALTER USER '" << escape_string(user) << "'@'"
<< escape_string(host) << "' IDENTIFIED BY '"
<< escape_string(password) << "' PASSWORD EXPIRE;\n";
}
cmdstr->append(ss.str()).append(flush_priv.str()).append(set_passcmd.str());
}
};
static const char *load_default_groups[]= { PROGRAM_NAME, 0 };
void print_version(const string &p)
{
cout << p
<< " Ver " << MYSQL_SERVER_VERSION << ", for "
<< SYSTEM_TYPE << " on "
<< MACHINE_TYPE << "\n";
}
void usage(const string &p)
{
print_version(p);
cout << ORACLE_WELCOME_COPYRIGHT_NOTICE("2015") << endl
<< "MySQL Database Deployment Utility." << endl
<< "Usage: "
<< p
<< " [OPTIONS]" << endl;
my_print_help(my_connection_options);
cout << endl
<< "The following options may be given as the first argument:"
<< endl
<< "--print-defaults Print the program argument list and exit."
<< endl
<< "--no-defaults Don't read default options from any option file,"
<< endl
<< " except for login file."
<< endl
<< "--defaults-file=# Only read default options from the given file #."
<< endl
<< "--defaults-extra-file=# Read this file after the global files are read."
<< endl;
my_print_variables(my_connection_options);
}
extern "C" my_bool
my_arguments_get_one_option(int optid,
const struct my_option *opt MY_ATTRIBUTE((unused)),
char *argument)
{
switch(optid)
{
case '?':
usage(PROGRAM_NAME);
exit(0);
case 'V':
print_version(PROGRAM_NAME);
exit(0);
}
return 0;
}
/**
The string class will break if constructed with a NULL pointer. This wrapper
provides a systematic protection when importing char pointers.
*/
string create_string(char *ptr)
{
if (ptr)
return string(ptr);
else
return string("");
}
template <class InputIterator, class UnaryPredicate >
bool my_all_of(InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first != last)
{
if (!pred(*first)) return false;
++first;
}
return true;
}
bool my_legal_username_chars(const char &c)
{
return isalnum(c) || c == '_';
}
bool my_legal_hostname_chars(const char &c)
{
return isalnum(c) || c == '_' || c == '.';
}
bool my_legal_plugin_chars(const char &c)
{
return isalnum(c) || c == '_';
}
// defined in auth_utils.cc
extern const string g_allowed_pwd_chars;
bool my_legal_password(const char &c)
{
return (get_allowed_pwd_chars().find(c) != string::npos);
}
/**
Verify that the default admin account follows the recommendations and
restrictions.
*/
bool assert_valid_root_account(const string &username, const string &host,
const string &plugin, bool ssl)
{
if( username.length() > MAX_USER_NAME_LEN || username.length() < 1)
{
error << "Username must be between 1 and "
<< MAX_USER_NAME_LEN
<< " characters in length."
<< endl;
return false;
}
if (!my_all_of(username.begin(), username.end(), my_legal_username_chars) ||
!my_all_of(host.begin(), host.end(), my_legal_hostname_chars))
{
error << "Recommended practice is to use only alpha-numericals in "
"the user / host name."
<< endl;
return false;
}
if (!my_all_of(plugin.begin(), plugin.end(), my_legal_plugin_chars))
{
error << "Only use alpha-numericals in the the plugin name."
<< endl;
return false;
}
if (plugin != "mysql_native_password" && plugin != "sha256_password")
{
error << "Unsupported authentication plugin specified."
<< endl;
return false;
}
return true;
}
bool assert_valid_datadir(const string &datadir, Path *target)
{
if (datadir.length() == 0)
{
error << "The data directory needs to be specified."
<< endl;
return false;
}
target->append(datadir);
if (target->exists())
{
if (!target->empty())
{
error << "The data directory '"
<< datadir.c_str()
<< "' already exist and is not empty." << endl;
return false;
}
}
return true;
}
class File_exists
{
public:
File_exists(const string *file, Path *qp) : m_file(file), m_qpath(qp)
{}
bool operator()(const Path &path)
{
Path tmp_path(path);
tmp_path.filename(*m_file);
if (tmp_path.exists())
{
m_qpath->path(path);
m_qpath->filename(*m_file);
return true;
}
return false;
}
private:
const string *m_file;
Path *m_qpath;
};
/**
Given a list of search paths; find the file.
If search_paths=0 then the filename is considered to be a qualified path.
If filename is empty then the qpath will be the first directory which
is found.
@param filename The file to look for
@search_paths paths to search
@qpath[out] The qualified path to the first found file
@return true if a file is found, false if not.
*/
bool locate_file(const string &filename, vector<Path > *search_paths,
Path *qpath)
{
if (search_paths == 0)
{
MY_STAT s;
if (my_stat(filename.c_str(), &s, MYF(0)) == NULL)
return false;
qpath->qpath(filename);
}
else
{
vector<Path>::iterator qpath_it=
find_if(search_paths->begin(), search_paths->end(),
File_exists(&filename, qpath));
if (qpath_it == search_paths->end())
return false;
}
return true;
}
void add_standard_search_paths(vector<Path > *spaths)
{
Path p;
if (!p.path_getcwd())
warning << "Can't determine current working directory." << endl;
spaths->push_back(p);
spaths->push_back(Path(p).append("/bin"));
spaths->push_back(Path("/usr/bin"));
spaths->push_back(Path("/usr/local/bin"));
spaths->push_back(Path("/opt/mysql/bin"));
#ifdef INSTALL_SBINDIR
spaths->push_back(Path(INSTALL_SBINDIR));
#endif
#ifdef INSTALL_BINDIR
spaths->push_back(Path(INSTALL_BINDIR));
#endif
}
/**
Attempts to locate the mysqld file.
If opt_mysqldfile is specified then the this assumed to be a correct qualified
path to the mysqld executable.
If opt_basedir is specified then opt_basedir+"/bin" is assumed to be a
candidate path for the mysqld executable.
If opt_srcdir is set then opt_srcdir+"/bin" is assumed to be a
candidate path for the mysqld executable.
If opt_builddir is set then opt_builddir+"/sql" is assumed to be a
candidate path for the mysqld executable.
If the executable isn't found in any of these locations,
attempt to search the local directory and "bin" and "sbin" subdirectories.
Finally check "/usr/bin","/usr/sbin", "/usr/local/bin","/usr/local/sbin",
"/opt/mysql/bin","/opt/mysql/sbin"
*/
bool assert_mysqld_exists(const string &opt_mysqldfile,
const string &opt_basedir,
const string &opt_builddir,
const string &opt_srcdir,
Path *qpath)
{
vector<Path > spaths;
if (opt_mysqldfile.length() > 0)
{
/* Use explicit option to file mysqld */
if (!locate_file(opt_mysqldfile, 0, qpath))
{
error << "No such file: " << opt_mysqldfile << endl;
return false;
}
}
else
{
if (opt_basedir.length() > 0)
{
spaths.push_back(Path(opt_basedir).
append("bin"));
/* cater for RPM installs : mysqld in sbin */
spaths.push_back(Path(opt_basedir).
append("sbin"));
}
if (opt_builddir.length() > 0)
{
spaths.push_back(Path(opt_builddir).
append("sql"));
}
add_standard_search_paths(&spaths);
if (!locate_file(MYSQLD_EXECUTABLE, &spaths, qpath))
{
error << "Can't locate the server executable (mysqld)." << endl;
info << "The following paths were searched: ";
copy(spaths.begin(), spaths.end(),
infix_ostream_iterator<Path >(info, ", "));
info << endl;
return false;
}
}
return true;
}
#if defined(HAVE_YASSL)
/**
Attempts to locate the mysql_ssl_rsa_setup file.
If opt_mysql_cert_setup_file is specified then the this assumed to be a
correct qualified path to the mysql_ssl_rsa_setup executable.
If opt_basedir is specified then opt_basedir+"/bin" is assumed to be a
candidate path for the mysql_ssl_rsa_setup executable.
If opt_srcdir is set then opt_srcdir+"/bin" is assumed to be a
candidate path for the mysql_ssl_rsa_setup executable.
If opt_builddir is set then opt_builddir+"/client" is assumed to be a
candidate path for the mysql_system_tables executable.
If the executable isn't found in any of these locations,
attempt to search the local directory and "bin" subdirectory.
Finally check "/usr/bin","/usr/sbin", "/usr/local/bin","/usr/local/sbin",
"/opt/mysql/bin","/opt/mysql/sbin"
*/
bool assert_cert_generator_exists(const string &opt_mysql_cert_setup_file,
const string &opt_basedir,
const string &opt_builddir,
const string &opt_srcdir,
Path *qpath)
{
vector<Path > spaths;
if (opt_mysql_cert_setup_file.length() > 0)
{
/* Use explicit option to file mysql_ssl_rsa_setup */
if (!locate_file(opt_mysql_cert_setup_file, 0, qpath))
{
error << "No such file: " << opt_mysql_cert_setup_file << endl;
return false;
}
}
else
{
if (opt_basedir.length() > 0)
{
spaths.push_back(Path(opt_basedir).
append("bin"));
}
if (opt_builddir.length() > 0)
{
spaths.push_back(Path(opt_builddir).
append("client"));
}
add_standard_search_paths(&spaths);
if (!locate_file(MYSQL_CERT_SETUP_EXECUTABLE, &spaths, qpath))
{
error << "Can't locate the server executable (mysql_ssl_rsa_setup)."
<< endl;
info << "The following paths were searched: ";
copy(spaths.begin(), spaths.end(),
infix_ostream_iterator<Path >(info, ", "));
info << endl;
return false;
}
}
return true;
}
#endif /* HAVE_YASSL */
bool assert_valid_language_directory(const string &opt_langpath,
const string &opt_basedir,
const string &opt_builddir,
const string &opt_srcdir,
Path *language_directory)
{
vector<Path > search_paths;
bool found_subdir= false;
if (opt_langpath.length() > 0)
{
search_paths.push_back(opt_langpath);
}
else
{
if(opt_basedir.length() > 0)
{
Path ld(opt_basedir);
ld.append("/share/english");
search_paths.push_back(ld);
/* cater for RPMs */
Path ld2(opt_basedir);
ld2.append("/share/mysql/english");
search_paths.push_back(ld2);
}
if (opt_builddir.length() > 0)
{
Path ld(opt_builddir);
ld.append("/sql/share/english");
search_paths.push_back(ld);
}
if (opt_srcdir.length() > 0)
{
Path ld(opt_srcdir);
ld.append("/sql/share/english");
search_paths.push_back(ld);
}
search_paths.push_back(Path("/usr/share/mysql/english"));
search_paths.push_back(Path("/opt/mysql/share/english"));
#ifdef INSTALL_MYSQLSHAREDIR
search_paths.push_back(Path(INSTALL_MYSQLSHAREDIR).append("/english"));
#endif
found_subdir= true;
}
if (!locate_file("", &search_paths, language_directory))
{
error << "Can't locate the language directory." << endl;
info << "Attempted the following paths: ";
copy(search_paths.begin(), search_paths.end(),
infix_ostream_iterator<Path>(info, ", "));
info << endl;
return false;
}
if (found_subdir)
language_directory->up();
return true;
}
/**
Parse the login.cnf file and extract the missing admin credentials.
If any of adminuser or adminhost contains information, it won't be overwritten
by new data. Password is always updated.
@return Error
@retval ALL_OK Reporting success
@retval ERR_FILE File not found
@retval ERR_ENCRYPTION Error while decrypting
@retval ERR_SYNTAX Error while parsing
*/
int get_admin_credentials(const string &opt_adminlogin,
const string &login_path,
string *adminuser,
string *adminhost,
string *password)
{
Path path;
int ret= ERR_OTHER;
if (!path.qpath(opt_adminlogin) || !path.exists())
return ERR_FILE;
ifstream fin(opt_adminlogin.c_str(), ifstream::binary);
stringstream sout;
if (decrypt_login_cnf_file(fin, sout) != ALL_OK)
return ERR_ENCRYPTION;
map<string, string > options;
if ((ret= parse_cnf_file(sout, &options, login_path)) != ALL_OK)
return ret;
for( map<string, string >::iterator it= options.begin();
it != options.end(); ++it)
{
if (it->first == "user")
*adminuser= it->second;
if (it->first == "host")
*adminhost= it->second;
if (it->first == "password")
*password= it->second;
}
return ALL_OK;
}
void create_ssl_policy(string *ssl_type, string *ssl_cipher,
string *x509_issuer, string *x509_subject)
{
/* TODO set up a specific SSL restriction on the default account */
*ssl_type= "ANY";
*ssl_cipher= "";
*x509_issuer= "";
*x509_subject= "";
}
#if defined(HAVE_YASSL)
class SSL_generator_writer
{
public:
bool operator()(int fh MY_ATTRIBUTE((unused)))
{
return true;
}
};
#endif /* HAVE_YASSL */
#define READ_BUFFER_SIZE 2048
#define TIMEOUT_IN_SEC 30
class Process_reader
{
public:
Process_reader(string *buffer) : m_buffer(buffer)
{}
bool operator()(int fh)
{
errno= 0;
char ch[READ_BUFFER_SIZE];
ssize_t n= 1;
int select_ret= 0;
fd_set rd, ex;
struct timeval tm;
tm.tv_sec = TIMEOUT_IN_SEC;
tm.tv_usec = 0;
int flags = fcntl(fh, F_GETFL, 0);
fcntl(fh, F_SETFL, flags | O_NONBLOCK);
FD_ZERO(&rd);
FD_ZERO(&ex);
FD_SET(fh, &rd);
FD_SET(fh, &ex);
errno= 0;
/* Wait for something to read */
if ((select_ret= select(fh + 1, &rd, NULL, &ex, &tm)) == 0)
{
/* if 30 s passed we attempt to read anyway */
warning << "select() timed out." << endl;
}
/* Read any error reports from the child process */
while((n= read(fh, ch, READ_BUFFER_SIZE)) > 0 && ch[0] != 0 && errno == 0)
{
m_buffer->append(ch, n);
}
return true;
}
private:
string *m_buffer;
};
struct Delimiter_parser
{
Delimiter_parser() : m_delimiter(";") {}
~Delimiter_parser() {}
bool operator()(std::string &line);
private:
string m_agg;
string m_delimiter;
};
bool Delimiter_parser::operator()(std::string &line)
{
if (line.empty() || line.size() == m_delimiter.size())
return false;
const string delimiter_tok("delimiter ");
string::size_type pos= line.find(delimiter_tok);
string::size_type curr_del_pos= line.find(m_delimiter);
if (pos != string::npos && curr_del_pos != string::npos)
{
/* replace old delimiter with new */
m_delimiter= line.substr(pos+delimiter_tok.size(), curr_del_pos - (pos+delimiter_tok.size()));
line.clear();
return false;
}
if (curr_del_pos != string::npos)
{
line.erase(curr_del_pos, m_delimiter.length());
line.append(";\n");
line= m_agg.append(line);
m_agg.clear();
}
else
{
m_agg.append(line.append(" "));
line.clear();
return false;
}
return true; /* line has delimiter */
}
struct Comment_extractor
{
Comment_extractor() : m_in_comment(false) {}
~Comment_extractor() {}
bool operator()(string &line);
private:
bool m_in_comment;
};
bool Comment_extractor::operator ()(string& line)
{
/* Are we in a multi comment? */
if (m_in_comment)
{
string::size_type i= line.find("*/");
if (i != string::npos)
{
m_in_comment= false;
string::iterator b= line.begin();
advance(b, i+2);
line.erase(line.begin(), b);
if (line.empty())
return true;
}
else
{
/* We're still in a multi comment clear the line */
line.clear();
}
}
else
{
string::size_type i= line.find("--");
if (i != string::npos)
{
string::iterator it= line.begin();
advance(it, i);
line.erase(it,line.end());
return true;
}
i= line.find("/*");
if (i != string::npos)
{
m_in_comment= true;
string::iterator a= line.begin();
advance(a, i);
string::iterator b;
string::size_type j= line.find("*/");
if (j != string::npos)
{
b= line.begin();
advance(b, j+2);
m_in_comment= false;
}
else
b= line.end();
line.erase(a, b);
if (line.empty())
return true;
}
}
return m_in_comment;
}
class Process_writer
{
public:
Process_writer(Sql_user *user, const string &opt_sqlfile) : m_user(user),
m_opt_sqlfile(opt_sqlfile) {}
bool operator()(int fh)
{
errno= 0;
info << "Creating system tables...";
string create_db("CREATE DATABASE mysql;\n");
string use_db("USE mysql;\n");
// ssize_t write() may be declared with attribute warn_unused_result
size_t w1= write(fh, create_db.c_str(), create_db.length());
size_t w2= write(fh, use_db.c_str(), use_db.length());
if (w1 != create_db.length() || w2 != use_db.length())
{
info << "failed." << endl;
return false;
}
unsigned s= 0;
s= sizeof(mysql_system_tables)/sizeof(*mysql_system_tables);
for(unsigned i=0, n= 1; i< s && errno != EPIPE && n != 0 &&
mysql_system_tables[i] != NULL; ++i)