aboutsummaryrefslogtreecommitdiff
path: root/mariadb-connector-c-v_2.3.7/libmariadb/get_password.c
blob: 3637205c38153836d2be5c5ebb21d52eac4d0e61 (plain)
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
/************************************************************************************
   Copyright (C) 2014 MariaDB Corporation AB
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library 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
   Library General Public License for more details.
   
   You should have received a copy of the GNU Library General Public
   License along with this library; if not see <http://www.gnu.org/licenses>
   or write to the Free Software Foundation, Inc., 
   51 Franklin St., Fifth Floor, Boston, MA 02110, USA
*************************************************************************************/
#include <my_global.h>
#include <my_sys.h>
#include "mysql.h"
#include <m_string.h>
#include <m_ctype.h>
#include <stdio.h>
#include <memory.h>

#ifndef _WIN32
#include <termios.h>
#else
#include <conio.h>
#endif /* _WIN32 */

/* {{{ static char *get_password() */
/*
   read password from device

   SYNOPSIS
     get_password
     Hdl/file          file handle
     buffer            input buffer
     length            buffer length

   RETURN
     buffer            zero terminated input buffer
*/   
#ifdef _WIN32
static char *get_password(HANDLE Hdl, char *buffer, DWORD length)
#else
static char *get_password(FILE *file, char *buffer, int length)
#endif
{
  char inChar;
  int  CharsProcessed= 1;
#ifdef _WIN32
  DWORD Offset= 0;
#else
  int  Offset= 0;
#endif

  memset(buffer, 0, length);

  do
  {
#ifdef _WIN32
    if (!ReadConsole(Hdl, &inChar, 1, &CharsProcessed, NULL) ||
        !CharsProcessed)
      break;
#else
    inChar= fgetc(file);
#endif

    switch(inChar) {
    case '\b': /* backslash */
      if (Offset)
      {
        /* cursor is always at the end */
        Offset--;
        buffer[Offset]= 0;
#ifdef _WIN32
        _cputs("\b \b");
#endif
      }
      break;
    case '\n':
    case '\r':
      break;
    default:
      buffer[Offset]= inChar;
      if (Offset < length - 2)
        Offset++;
#ifdef _WIN32
      _cputs("*");
#endif
      break;
    }  
  } while (CharsProcessed && inChar != '\n' && inChar != '\r');
  return buffer;
}
/* }}} */

/* {{{ static char* get_tty_password */
/*
   reads password from tty/console

   SYNOPSIS
     get_tty_password()
     buffer           input buffer
     length           length of input buffer

   DESCRIPTION
     reads a password from console (Windows) or tty without echoing
     it's characters. Input buffer must be allocated by calling function.

   RETURNS
     buffer           pointer to input buffer
*/
char* get_tty_password(char *prompt, char *buffer, int length)
{
#ifdef _WIN32
  DWORD  SaveState;
  HANDLE Hdl;
  int    Offset= 0;
  DWORD  CharsProcessed=  0;

  if (prompt)
    fprintf(stderr, "%s", prompt);

  if (!(Hdl= CreateFile("CONIN$", 
                        GENERIC_READ | GENERIC_WRITE,
                        FILE_SHARE_READ,
                        NULL,
                        OPEN_EXISTING, 0, NULL)))
  {
    /* todo: provide a graphical dialog */
    return buffer;
  }
  /* Save ConsoleMode and set ENABLE_PROCESSED_INPUT:
     CTRL+C is processed by the system and is not placed in the input buffer */
  GetConsoleMode(Hdl, &SaveState);
  SetConsoleMode(Hdl, ENABLE_PROCESSED_INPUT);

  buffer= get_password(Hdl, buffer, length);
  SetConsoleMode(Hdl, SaveState);
  CloseHandle(Hdl);
  return buffer;
#else
  struct termios term_old, 
                 term_new;
  FILE *readfrom;

  if (prompt && isatty(fileno(stderr)))
    fputs(prompt, stderr);

  if (!(readfrom= fopen("/dev/tty", "r")))
    readfrom= stdin;

  /* try to disable echo */
  tcgetattr(fileno(readfrom), &term_old);
  term_new= term_old;
  term_new.c_cc[VMIN] = 1;
  term_new.c_cc[VTIME]= 0;
  term_new.c_lflag&= ~(ECHO | ISIG | ICANON | ECHONL);
  tcsetattr(fileno(readfrom), TCSADRAIN, &term_new);

  buffer= get_password(readfrom, buffer, length);

  if (isatty(fileno(readfrom)))
    tcsetattr(fileno(readfrom), TCSADRAIN, &term_old);

  fclose(readfrom);

  return buffer;
#endif
}
/* }}} */