drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
get_password.cc
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/*
21** Ask for a password from tty
22** This is an own file to avoid conflicts with curses
23*/
24
25#include <config.h>
26#include <client/get_password.h>
27
28#include <string>
29
30#include <string.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <ctype.h>
34#include <unistd.h>
35
36#include <sys/ioctl.h>
37# include <termios.h>
38# define TERMIO struct termios
39
40using namespace std;
41
42bool tty_password= false;
43const std::string PASSWORD_SENTINEL("\0\0\0\0\0", 5);
44
45/*
46 Can't use fgets, because readline will get confused
47 length is max number of chars in to, not counting \0
48 to will not include the eol characters.
49*/
50
51static void get_password(char *to, uint32_t length,int fd, bool echo)
52{
53 char *pos=to,*end=to+length;
54
55 for (;;)
56 {
57 char tmp;
58 if (read(fd,&tmp,1) != 1)
59 break;
60 if (tmp == '\b' || (int) tmp == 127)
61 {
62 if (pos != to)
63 {
64 if (echo)
65 {
66 fputs("\b \b",stderr);
67 fflush(stderr);
68 }
69 pos--;
70 continue;
71 }
72 }
73 if (tmp == '\n' || tmp == '\r' || tmp == 3)
74 break;
75 if (iscntrl(tmp) || pos == end)
76 continue;
77 *(pos++) = tmp;
78 }
79 while (pos != to && isspace(pos[-1]) == ' ')
80 pos--; /* Allow dummy space at end */
81 *pos=0;
82 return;
83}
84
85
86char *client_get_tty_password(const char *opt_message)
87{
88 TERMIO org,tmp;
89 char buff[80];
90
91 if (isatty(fileno(stderr)))
92 {
93 fputs(opt_message ? opt_message : "Enter password: ",stderr);
94 fflush(stderr);
95 }
96 tcgetattr(fileno(stdin), &org);
97 tmp = org;
98 tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
99 tmp.c_cc[VMIN] = 1;
100 tmp.c_cc[VTIME] = 0;
101 tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
102 get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stderr)));
103 tcsetattr(fileno(stdin), TCSADRAIN, &org);
104 if (isatty(fileno(stderr)))
105 fputc('\n',stderr);
106
107 return strdup(buff);
108}
109
110pair<string, string> parse_password_arg(string s)
111{
112 if (s.find("--password") == 0)
113 {
114 if (s == "--password")
115 {
116 tty_password= true;
117 //check if no argument is passed.
118 return make_pair("password", PASSWORD_SENTINEL);
119 }
120
121 if (s.substr(10,3) == "=\"\"" || s.substr(10,3) == "=''")
122 {
123 // Check if --password="" or --password=''
124 return make_pair("password", PASSWORD_SENTINEL);
125 }
126
127 if(s.substr(10) == "=" && s.length() == 11)
128 {
129 // check if --password= and return a default value
130 return make_pair("password", PASSWORD_SENTINEL);
131 }
132 }
133
134 else if (s.find("-P") == 0)
135 {
136 if (s == "-P")
137 {
138 tty_password= true;
139 //check if no argument is passed.
140 return make_pair("password", PASSWORD_SENTINEL);
141 }
142
143 if (s.substr(2,2) == "\"\"" || s.substr(2,2) == "''")
144 {
145 // Check if -P"" or -P''
146 return make_pair("password", PASSWORD_SENTINEL);
147 }
148 }
149 return make_pair(string(""), string(""));
150}
151
STL namespace.