1 
2 /*
3    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License, version 2.0,
7    as published by the Free Software Foundation.
8 
9    This program is also distributed with certain software (including
10    but not limited to OpenSSL) that is licensed under separate terms,
11    as designated in a particular file or component or in included license
12    documentation.  The authors of MySQL hereby grant you an additional
13    permission to link the program and your derivative works with the
14    separately licensed software that they have included with MySQL.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License, version 2.0, for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 */
25 
26 /*
27 **  print_default.c:
28 **  Print all parameters in a default file that will be given to some program.
29 **
30 **  Written by Monty
31 */
32 
33 #include <my_global.h>
34 #include <my_sys.h>
35 #include <m_string.h>
36 #include <my_getopt.h>
37 #include "my_default.h"
38 
39 
40 const char *config_file="my";			/* Default config file */
41 static char *my_login_path;
42 static my_bool *show_passwords;
43 uint verbose= 0, opt_defaults_file_used= 0;
44 const char *default_dbug_option="d:t:o,/tmp/my_print_defaults.trace";
45 
46 static struct my_option my_long_options[] =
47 {
48   /*
49     NB: --config-file is troublesome, because get_defaults_options() doesn't
50     know about it, but we pretend --config-file is like --defaults-file.  In
51     fact they behave differently: see the comments at the top of
52     mysys/default.c for how --defaults-file should behave.
53 
54     This --config-file option behaves as:
55     - If it has a directory name part (absolute or relative), then only this
56       file is read; no error is given if the file doesn't exist
57     - If the file has no directory name part, the standard locations are
58       searched for a file of this name (and standard filename extensions are
59       added if the file has no extension)
60   */
61   {"config-file", 'c', "Deprecated, please use --defaults-file instead. "
62    "Name of config file to read; if no extension is given, default "
63    "extension (e.g., .ini or .cnf) will be added",
64    &config_file, &config_file, 0, GET_STR, REQUIRED_ARG,
65    0, 0, 0, 0, 0, 0},
66 #ifdef DBUG_OFF
67   {"debug", '#', "This is a non-debug version. Catch this and exit",
68    0,0, 0, GET_DISABLED, OPT_ARG, 0, 0, 0, 0, 0, 0},
69 #else
70   {"debug", '#', "Output debug log", &default_dbug_option,
71    &default_dbug_option, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
72 #endif
73   {"defaults-file", 'c', "Like --config-file, except: if first option, "
74    "then read this file only, do not read global or per-user config "
75    "files; should be the first option",
76    &config_file, &config_file, 0, GET_STR, REQUIRED_ARG,
77    0, 0, 0, 0, 0, 0},
78   {"defaults-extra-file", 'e',
79    "Read this file after the global config file and before the config "
80    "file in the users home directory; should be the first option",
81    &my_defaults_extra_file, &my_defaults_extra_file, 0,
82    GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
83   {"defaults-group-suffix", 'g',
84    "In addition to the given groups, read also groups with this suffix",
85    &my_defaults_group_suffix, &my_defaults_group_suffix,
86    0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
87   {"extra-file", 'e',
88    "Deprecated. Synonym for --defaults-extra-file.",
89    &my_defaults_extra_file,
90    &my_defaults_extra_file, 0, GET_STR,
91    REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
92   {"no-defaults", 'n', "Ignore reading of default option file(s), "
93    "except for login file.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0,
94    0, 0, 0},
95   {"login-path", 'l', "Path to be read from under the login file.",
96    &my_login_path, &my_login_path, 0, GET_STR, REQUIRED_ARG,
97    0, 0, 0, 0, 0, 0},
98   {"show", 's', "Show passwords in plain text.",
99    &show_passwords, &show_passwords, 0, GET_BOOL, NO_ARG,
100    0, 0, 0, 0, 0, 0},
101   {"help", '?', "Display this help message and exit.",
102    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
103   {"verbose", 'v', "Increase the output level",
104    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
105   {"version", 'V', "Output version information and exit.",
106    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
107   {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
108 };
109 
110 
usage(my_bool version)111 static void usage(my_bool version)
112 {
113   printf("%s  Ver 1.6 for %s at %s\n",my_progname,SYSTEM_TYPE,
114 	 MACHINE_TYPE);
115   if (version)
116     return;
117   puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\nand you are welcome to modify and redistribute it under the GPL license\n");
118   puts("Prints all arguments that is give to some program using the default files");
119   printf("Usage: %s [OPTIONS] groups\n", my_progname);
120   my_print_help(my_long_options);
121   my_print_default_files(config_file);
122   my_print_variables(my_long_options);
123   printf("\nExample usage:\n%s --defaults-file=example.cnf client mysql\n", my_progname);
124 }
125 
126 
127 static my_bool
get_one_option(int optid,const struct my_option * opt MY_ATTRIBUTE ((unused)),char * argument MY_ATTRIBUTE ((unused)))128 get_one_option(int optid, const struct my_option *opt MY_ATTRIBUTE((unused)),
129 	       char *argument MY_ATTRIBUTE((unused)))
130 {
131   switch (optid) {
132     case 'c':
133       opt_defaults_file_used= 1;
134       break;
135     case 'n':
136       break;
137     case 'I':
138     case '?':
139     usage(0);
140     exit(0);
141     case 'v':
142       verbose++;
143       break;
144     case 'V':
145     usage(1);
146     exit(0);
147     case '#':
148       DBUG_PUSH(argument ? argument : default_dbug_option);
149       break;
150   }
151   return 0;
152 }
153 
154 
get_options(int * argc,char *** argv)155 static int get_options(int *argc,char ***argv)
156 {
157   int ho_error;
158 
159   if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
160     exit(ho_error);
161 
162   if (*argc < 1)
163   {
164     usage(0);
165     return 1;
166   }
167   return 0;
168 }
169 
170 
main(int argc,char ** argv)171 int main(int argc, char **argv)
172 {
173   int count, error, args_used;
174   char **load_default_groups, *tmp_arguments[6];
175   char **argument, **arguments, **org_argv;
176   char *defaults, *extra_defaults, *group_suffix, *login_path;
177 
178   MY_INIT(argv[0]);
179 
180   org_argv= argv;
181   args_used= get_defaults_options(argc, argv, &defaults, &extra_defaults,
182                                   &group_suffix, &login_path, FALSE);
183 
184   /* Copy defaults-xxx arguments & program name */
185   count=args_used+1;
186   arguments= tmp_arguments;
187   memcpy((char*) arguments, (char*) org_argv, count * sizeof(*org_argv));
188   arguments[count]= 0;
189 
190   /* Check out the args */
191   if (!(load_default_groups=(char**) my_malloc((argc+1)*sizeof(char*),
192 					       MYF(MY_WME))))
193     exit(1);
194   if (get_options(&argc,&argv))
195     exit(1);
196   memcpy((char*) load_default_groups, (char*) argv, (argc + 1) * sizeof(*argv));
197 
198   if ((error= load_defaults(config_file, (const char **) load_default_groups,
199 			   &count, &arguments)))
200   {
201     if (verbose && opt_defaults_file_used)
202     {
203       if (error == 1)
204 	fprintf(stderr, "WARNING: Defaults file '%s' not found!\n",
205 		config_file);
206       /* This error is not available now. For the future */
207       if (error == 2)
208 	fprintf(stderr, "WARNING: Defaults file '%s' is not a regular file!\n",
209 		config_file);
210     }
211     error= 2;
212     exit(error);
213   }
214 
215   for (argument= arguments+1 ; *argument ; argument++)
216     if (!my_getopt_is_args_separator(*argument))           /* skip arguments separator */
217     {
218       if (!(show_passwords) && strncmp(*argument, "--password", 10) == 0)
219         puts("--password=*****");
220       else
221         puts(*argument);
222     }
223   my_free(load_default_groups);
224   free_defaults(arguments);
225 
226   exit(0);
227 }
228