Skip to content

Commit 7a988df

Browse files
committed
MXS-955: Put back proper .maxadmin handling
Now MaxScale again supports having 'hostname', 'user', 'port' and 'passwd' in the .maxadmin file in addition to 'socket'.
1 parent f1acc1f commit 7a988df

File tree

2 files changed

+78
-35
lines changed

2 files changed

+78
-35
lines changed

Documentation/Reference/MaxAdmin.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,22 @@ Then simply set this file to have execute permissions and it may be run like any
287287

288288
## The .maxadmin file
289289

290-
MaxAdmin supports a mechanism to set defaults for all the command line switches via a file in the home directory of the user. If a file named .maxadmin exists it will be read and parameters set according to the lies in this files. The parameter that can be set is: socket. An example of a .maxadmin file that will alter the default password and user name arguments would be
290+
MaxAdmin supports a mechanism to set defaults for the command line switches via a file in the home directory of the user. If a file named `.maxadmin` exists, it will be read and parameters set according to the entries in that file.
291+
292+
This mechanism can be used to provide defaults to the command line options. If a command line option is provided, it will still override the value in the `.maxadmin` file.
293+
294+
The parameters than can be set are:
295+
* `1.4`: _hostname_, _port_, _user_ and _passwd_
296+
* `2.0.0` and `2.0.1`: _socket_
297+
* `2.0.2` onwards: _socket_, _hostname_, _port_, _user_ and _passwd_ (and as synonym _password_)
298+
299+
An example of a `.maxadmin` file that will alter the default socket path is:
291300

292301
socket=/somepath/maxadmin.socket
293302

294-
This mechanism can be used to provide a means of passwords entry into maxadmin or to override any of the command line option defaults. If a command line option is given that will still override the value in the .maxadmin file.
303+
Note that if in `2.0.2` or later, a value for _socket_ as well as any of the internet socket related options, such as _hostname_, is provided in `.maxadmin`, then _socket_ takes precedense. In that case, provide at least one internet socket related option on the command line to force MaxAdmin to use an internet socket and thus the internet socket related options from `.maxadmin`.
295304

296-
The .maxadmin file may be made read only to protect any passwords written to that file.
305+
The `.maxadmin` file may be made read only to protect any passwords written to that file.
297306

298307
<a name="help"></a>
299308
# Getting Help

client/maxadmin.c

+66-32
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ static void DoSource(int so, char *cmd);
7575
static void DoUsage(const char*);
7676
static int isquit(char *buf);
7777
static void PrintVersion(const char *progname);
78-
static void read_inifile(char **, int*);
78+
static void read_inifile(char **socket,
79+
char **hostname, char **port, char **user, char **passwd,
80+
int *editor);
7981
static bool getPassword(char *password, size_t length);
8082

8183
#ifdef HISTORY
@@ -116,10 +118,6 @@ static struct option long_options[] =
116118
int
117119
main(int argc, char **argv)
118120
{
119-
const char* vi = "vi";
120-
const char* emacs = "emacs";
121-
122-
int i, num, rv;
123121
#ifdef HISTORY
124122
char *buf;
125123
EditLine *el = NULL;
@@ -133,39 +131,45 @@ main(int argc, char **argv)
133131
char *port = NULL;
134132
char *user = NULL;
135133
char *passwd = NULL;
136-
char *conn_socket = NULL;
137-
char *default_socket = MAXADMIN_DEFAULT_SOCKET;
134+
char *socket_path = NULL;
138135
int use_emacs = 0;
139-
int so;
140-
int option_index = 0;
141-
char c;
142136

143-
read_inifile(&conn_socket, &use_emacs);
137+
read_inifile(&socket_path, &hostname, &port, &user, &passwd, &use_emacs);
138+
139+
bool use_inet_socket = false;
140+
bool use_unix_socket = false;
144141

142+
int option_index = 0;
143+
char c;
145144
while ((c = getopt_long(argc, argv, "h:p:P:u:S:v?e",
146145
long_options, &option_index)) >= 0)
147146
{
148147
switch (c)
149148
{
150149
case 'h':
150+
use_inet_socket = true;
151151
hostname = strdup(optarg);
152152
break;
153153

154154
case 'p':
155+
use_inet_socket = true;
155156
passwd = strdup(optarg);
156157
memset(optarg, '\0', strlen(optarg));
157158
break;
158159

159160
case 'P':
161+
use_inet_socket = true;
160162
port = strdup(optarg);
161163
break;
162164

163165
case 'u':
166+
use_inet_socket = true;
164167
user = strdup(optarg);
165168
break;
166169

167170
case 'S':
168-
conn_socket = strdup(optarg);
171+
use_unix_socket = true;
172+
socket_path = strdup(optarg);
169173
break;
170174

171175
case 'v':
@@ -182,16 +186,20 @@ main(int argc, char **argv)
182186
}
183187
}
184188

185-
if ((hostname || port || user || passwd) && (conn_socket))
189+
if (use_inet_socket && use_unix_socket)
186190
{
187-
// Either socket or any parameters related to hostname/port.
191+
// Both unix socket path and at least of the internet socket
192+
// options have been provided.
188193
DoUsage(argv[0]);
189194
exit(EXIT_FAILURE);
190195
}
191196

192-
if (hostname || port || user || passwd)
197+
if (use_inet_socket || (!socket_path && (hostname || port || user || passwd)))
193198
{
194-
assert(!conn_socket);
199+
// If any of the internet socket options have explicitly been provided, or
200+
// .maxadmin does not contain "socket" but does contain at least one of
201+
// the internet socket options, we use an internet socket. Note that if
202+
// -S is provided, then socket_path will be non-NULL.
195203

196204
if (!hostname)
197205
{
@@ -210,23 +218,29 @@ main(int argc, char **argv)
210218
}
211219
else
212220
{
213-
if (!conn_socket)
221+
use_unix_socket = true;
222+
223+
if (!socket_path)
214224
{
215-
conn_socket = MAXADMIN_DEFAULT_SOCKET;
225+
socket_path = MAXADMIN_DEFAULT_SOCKET;
216226
}
217227
}
218228

219-
assert(!((hostname || port) && conn_socket));
229+
int so;
220230

221-
if (conn_socket)
231+
if (use_unix_socket)
222232
{
223-
if ((so = connectUsingUnixSocket(conn_socket)) == -1)
233+
assert(socket_path);
234+
235+
if ((so = connectUsingUnixSocket(socket_path)) == -1)
224236
{
225237
exit(EXIT_FAILURE);
226238
}
227239
}
228240
else
229241
{
242+
assert(hostname && user && port);
243+
230244
char password[MAX_PASSWORD_LEN];
231245

232246
if (passwd == NULL)
@@ -301,11 +315,11 @@ main(int argc, char **argv)
301315

302316
if (use_emacs)
303317
{
304-
el_set(el, EL_EDITOR, emacs); /** Editor is emacs */
318+
el_set(el, EL_EDITOR, "emacs"); /** Editor is emacs */
305319
}
306320
else
307321
{
308-
el_set(el, EL_EDITOR, vi); /* Default editor is vi */
322+
el_set(el, EL_EDITOR, "vi"); /* Default editor is vi */
309323
}
310324
el_set(el, EL_SIGNAL, 1); /* Handle signals gracefully */
311325
el_set(el, EL_PROMPT, prompt); /* Set the prompt function */
@@ -325,6 +339,7 @@ main(int argc, char **argv)
325339
*/
326340
el_source(el, NULL);
327341

342+
int num;
328343
while ((buf = (char *) el_gets(el, &num)) != NULL && num != 0)
329344
{
330345
#else
@@ -333,7 +348,7 @@ main(int argc, char **argv)
333348
num = strlen(buf);
334349
#endif
335350
/* Strip trailing \n\r */
336-
for (i = num - 1; buf[i] == '\r' || buf[i] == '\n'; i--)
351+
for (int i = num - 1; buf[i] == '\r' || buf[i] == '\n'; i--)
337352
{
338353
buf[i] = 0;
339354
}
@@ -350,6 +365,7 @@ main(int argc, char **argv)
350365
else if (!strcasecmp(buf, "history"))
351366
{
352367
#ifdef HISTORY
368+
int rv;
353369
for (rv = history(hist, &ev, H_LAST); rv != -1;
354370
rv = history(hist, &ev, H_PREV))
355371
{
@@ -394,11 +410,11 @@ main(int argc, char **argv)
394410
/**
395411
* Connect to the MaxScale server
396412
*
397-
* @param conn_socket The UNIX socket to connect to
413+
* @param socket_path The UNIX socket to connect to
398414
* @return The connected socket or -1 on error
399415
*/
400416
static int
401-
connectUsingUnixSocket(const char *conn_socket)
417+
connectUsingUnixSocket(const char *socket_path)
402418
{
403419
int so = -1;
404420

@@ -408,7 +424,7 @@ connectUsingUnixSocket(const char *conn_socket)
408424

409425
memset(&local_addr, 0, sizeof local_addr);
410426
local_addr.sun_family = AF_UNIX;
411-
strncpy(local_addr.sun_path, conn_socket, sizeof(local_addr.sun_path) - 1);
427+
strncpy(local_addr.sun_path, socket_path, sizeof(local_addr.sun_path) - 1);
412428

413429
if (connect(so, (struct sockaddr *) &local_addr, sizeof(local_addr)) == 0)
414430
{
@@ -441,7 +457,7 @@ connectUsingUnixSocket(const char *conn_socket)
441457
{
442458
char errbuf[STRERROR_BUFLEN];
443459
fprintf(stderr, "Unable to connect to MaxScale at %s: %s\n",
444-
conn_socket, strerror_r(errno, errbuf, sizeof(errbuf)));
460+
socket_path, strerror_r(errno, errbuf, sizeof(errbuf)));
445461
close(so);
446462
so = -1;
447463
}
@@ -853,13 +869,16 @@ rtrim(char *str)
853869
* Read defaults for hostname, port, user and password from
854870
* the .maxadmin file in the users home directory.
855871
*
856-
* @param hostname Pointer the hostname to be updated
872+
* @param socket Pointer to the socket to be updated.
873+
* @param hostname Pointer to the hostname to be updated
857874
* @param port Pointer to the port to be updated
858875
* @param user Pointer to the user to be updated
859876
* @param passwd Pointer to the password to be updated
860877
*/
861878
static void
862-
read_inifile(char **conn_socket, int* editor)
879+
read_inifile(char **socket,
880+
char **hostname, char** port, char **user, char **passwd,
881+
int* editor)
863882
{
864883
char pathname[400];
865884
char *home, *brkt;
@@ -893,11 +912,26 @@ read_inifile(char **conn_socket, int* editor)
893912
{
894913
if (strcmp(name, "socket") == 0)
895914
{
896-
*conn_socket = strdup(value);
915+
*socket = strdup(value);
916+
}
917+
else if (strcmp(name, "hostname") == 0)
918+
{
919+
*hostname = strdup(value);
920+
}
921+
else if (strcmp(name, "port") == 0)
922+
{
923+
*port = strdup(value);
924+
}
925+
else if (strcmp(name, "user") == 0)
926+
{
927+
*user = strdup(value);
928+
}
929+
else if ((strcmp(name, "passwd") == 0) || (strcmp(name, "password") == 0))
930+
{
931+
*passwd = strdup(value);
897932
}
898933
else if (strcmp(name, "editor") == 0)
899934
{
900-
901935
if (strcmp(value, "vi") == 0)
902936
{
903937
*editor = 0;

0 commit comments

Comments
 (0)