~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/get_password.c

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; either version 2 of the License, or
9
 
 *  (at your option) any later version.
 
8
 *  the Free Software Foundation; version 2 of the License.
10
9
 *
11
10
 *  This program is distributed in the hope that it will be useful,
12
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
21
** Ask for a password from tty
23
22
** This is an own file to avoid conflicts with curses
24
23
*/
25
 
#include <drizzled/global.h>
 
24
#include <config.h>
26
25
#include "libdrizzle.h"
27
 
 
28
 
#if defined(HAVE_BROKEN_GETPASS) && !defined(HAVE_GETPASSPHRASE)
29
 
#undef HAVE_GETPASS
30
 
#endif
31
 
 
32
 
#ifdef HAVE_GETPASS
33
 
# ifdef HAVE_PWD_H
34
 
#   include <pwd.h>
35
 
# endif /* HAVE_PWD_H */
36
 
#else /* ! HAVE_GETPASS */
37
 
# if !defined(__WIN__) && !defined(__NETWARE__)
38
 
#   include <sys/ioctl.h>
39
 
#   ifdef HAVE_TERMIOS_H                                /* For tty-password */
40
 
#     include   <termios.h>
41
 
#     define TERMIO     struct termios
42
 
#   else
43
 
#     ifdef HAVE_TERMIO_H                               /* For tty-password */
44
 
#       include <termio.h>
45
 
#       define TERMIO   struct termio
46
 
#     else
47
 
#       include <sgtty.h>
48
 
#       define TERMIO   struct sgttyb
49
 
#     endif
50
 
#   endif
51
 
#   ifdef alpha_linux_port
52
 
#     include <asm/ioctls.h>                            /* QQ; Fix this in configure */
53
 
#     include <asm/termiobits.h>
54
 
#   endif
55
 
# else
56
 
#   ifndef __NETWARE__
57
 
#     include <conio.h>
58
 
#   endif /* ! __NETWARE__ */
59
 
# endif /* ! __WIN__ AND ! __NETWARE__ */
60
 
#endif /* HAVE_GETPASS */
61
 
 
62
 
#ifdef HAVE_GETPASSPHRASE                       /* For Solaris */
63
 
#define getpass(A) getpassphrase(A)
64
 
#endif
65
 
 
66
 
#if defined( __WIN__)
67
 
char *get_tty_password(const char *opt_message)
68
 
{
69
 
  char to[80];
70
 
  char *pos=to,*end=to+sizeof(to)-1;
71
 
  int i=0;
72
 
  _cputs(opt_message ? opt_message : "Enter password: ");
73
 
  for (;;)
74
 
  {
75
 
    char tmp;
76
 
    tmp=_getch();
77
 
    if (tmp == '\b' || (int) tmp == 127)
78
 
    {
79
 
      if (pos != to)
80
 
      {
81
 
        _cputs("\b \b");
82
 
        pos--;
83
 
        continue;
84
 
      }
85
 
    }
86
 
    if (tmp == '\n' || tmp == '\r' || tmp == 3)
87
 
      break;
88
 
    if (iscntrl(tmp) || pos == end)
89
 
      continue;
90
 
    _cputs("*");
91
 
    *(pos++) = tmp;
92
 
  }
93
 
  while (pos != to && isspace(pos[-1]) == ' ')
94
 
    pos--;                                      /* Allow dummy space at end */
95
 
  *pos=0;
96
 
  _cputs("\n");
97
 
  return(my_strdup(to,MYF(MY_FAE)));
98
 
}
99
 
 
 
26
#include <libdrizzle/get_password.h>
 
27
 
 
28
#include <string.h>
 
29
#include <stdio.h>
 
30
#include <stdlib.h>
 
31
#include <ctype.h>
 
32
#include <unistd.h>
 
33
 
 
34
#include <sys/ioctl.h>
 
35
#ifdef HAVE_TERMIOS_H                           /* For tty-password */
 
36
# include       <termios.h>
 
37
#  define TERMIO        struct termios
100
38
#else
 
39
#  ifdef HAVE_TERMIO_H                          /* For tty-password */
 
40
#    include    <termio.h>
 
41
#    define TERMIO      struct termio
 
42
#  else
 
43
#    include    <sgtty.h>
 
44
#    define TERMIO      struct sgttyb
 
45
#  endif
 
46
#endif
101
47
 
102
 
# ifndef HAVE_GETPASS
103
48
/*
104
49
  Can't use fgets, because readline will get confused
105
50
  length is max number of chars in to, not counting \0
106
51
  to will not include the eol characters.
107
52
*/
108
53
 
109
 
static void get_password(char *to,uint length,int fd, bool echo)
 
54
static void get_password(char *to, uint32_t length,int fd, bool echo)
110
55
{
111
56
  char *pos=to,*end=to+length;
112
57
 
113
58
  for (;;)
114
59
  {
115
60
    char tmp;
116
 
    if (my_read(fd,&tmp,1,MYF(0)) != 1)
 
61
    if (read(fd,&tmp,1) != 1)
117
62
      break;
118
63
    if (tmp == '\b' || (int) tmp == 127)
119
64
    {
144
89
  *pos=0;
145
90
  return;
146
91
}
147
 
# endif /* ! HAVE_GETPASS */
148
92
 
149
93
 
150
94
char *get_tty_password(const char *opt_message)
151
95
{
152
 
# ifdef HAVE_GETPASS
153
 
  char *passbuff;
154
 
# else /* ! HAVE_GETPASS */
155
96
  TERMIO org,tmp;
156
 
# endif /* HAVE_GETPASS */
157
97
  char buff[80];
158
98
 
159
 
# ifdef HAVE_GETPASS
160
 
  passbuff = getpass(opt_message ? opt_message : "Enter password: ");
161
 
 
162
 
  /* copy the password to buff and clear original (static) buffer */
163
 
  stpncpy(buff, passbuff, sizeof(buff) - 1);
164
 
#   ifdef _PASSWORD_LEN
165
 
  memset(passbuff, 0, _PASSWORD_LEN);
166
 
#   endif
167
 
# else /* ! HAVE_GETPASS */
168
99
  if (isatty(fileno(stdout)))
169
100
  {
170
101
    fputs(opt_message ? opt_message : "Enter password: ",stdout);
171
102
    fflush(stdout);
172
103
  }
173
 
#   if defined(HAVE_TERMIOS_H)
 
104
#  if defined(HAVE_TERMIOS_H)
174
105
  tcgetattr(fileno(stdin), &org);
175
106
  tmp = org;
176
107
  tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
179
110
  tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
180
111
  get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
181
112
  tcsetattr(fileno(stdin), TCSADRAIN, &org);
182
 
#   elif defined(HAVE_TERMIO_H)
 
113
#  elif defined(HAVE_TERMIO_H)
183
114
  ioctl(fileno(stdin), (int) TCGETA, &org);
184
115
  tmp=org;
185
116
  tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
188
119
  ioctl(fileno(stdin),(int) TCSETA, &tmp);
189
120
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
190
121
  ioctl(fileno(stdin),(int) TCSETA, &org);
191
 
#   else
 
122
#  else
192
123
  gtty(fileno(stdin), &org);
193
124
  tmp=org;
194
125
  tmp.sg_flags &= ~ECHO;
196
127
  stty(fileno(stdin), &tmp);
197
128
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
198
129
  stty(fileno(stdin), &org);
199
 
#   endif
 
130
#  endif
200
131
  if (isatty(fileno(stdout)))
201
132
    fputc('\n',stdout);
202
 
# endif /* HAVE_GETPASS */
203
133
 
204
134
  return strdup(buff);
205
135
}
206
 
#endif /*__WIN__*/