~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/get_password.c

  • Committer: Brian Aker
  • Date: 2009-05-11 17:50:22 UTC
  • Revision ID: brian@gaz-20090511175022-y35q9ky6uh9ldcjt
Replacing Sun employee copyright headers (aka... anything done by a Sun
employee is copyright by Sun).

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#include "libdrizzle.h"
25
 
 
26
 
#include <string.h>
27
 
#include <stdio.h>
28
 
#include <stdlib.h>
29
 
#include <ctype.h>
30
 
 
31
 
#include <sys/ioctl.h>
32
 
#ifdef HAVE_TERMIOS_H                           /* For tty-password */
33
 
# include       <termios.h>
34
 
#  define TERMIO        struct termios
35
 
#else
36
 
#  ifdef HAVE_TERMIO_H                          /* For tty-password */
37
 
#    include    <termio.h>
38
 
#    define TERMIO      struct termio
39
 
#  else
40
 
#    include    <sgtty.h>
41
 
#    define TERMIO      struct sgttyb
42
 
#  endif
43
 
#endif
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
 
 
51
 
static 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",stdout);
67
 
          fflush(stdout);
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
 
    if (echo)
78
 
    {
79
 
      fputc('*',stdout);
80
 
      fflush(stdout);
81
 
    }
82
 
    *(pos++) = tmp;
83
 
  }
84
 
  while (pos != to && isspace(pos[-1]) == ' ')
85
 
    pos--;                                      /* Allow dummy space at end */
86
 
  *pos=0;
87
 
  return;
88
 
}
89
 
 
90
 
 
91
 
char *get_tty_password(const char *opt_message)
92
 
{
93
 
  TERMIO org,tmp;
94
 
  char buff[80];
95
 
 
96
 
  if (isatty(fileno(stdout)))
97
 
  {
98
 
    fputs(opt_message ? opt_message : "Enter password: ",stdout);
99
 
    fflush(stdout);
100
 
  }
101
 
#  if defined(HAVE_TERMIOS_H)
102
 
  tcgetattr(fileno(stdin), &org);
103
 
  tmp = org;
104
 
  tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
105
 
  tmp.c_cc[VMIN] = 1;
106
 
  tmp.c_cc[VTIME] = 0;
107
 
  tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
108
 
  get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
109
 
  tcsetattr(fileno(stdin), TCSADRAIN, &org);
110
 
#  elif defined(HAVE_TERMIO_H)
111
 
  ioctl(fileno(stdin), (int) TCGETA, &org);
112
 
  tmp=org;
113
 
  tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
114
 
  tmp.c_cc[VMIN] = 1;
115
 
  tmp.c_cc[VTIME]= 0;
116
 
  ioctl(fileno(stdin),(int) TCSETA, &tmp);
117
 
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
118
 
  ioctl(fileno(stdin),(int) TCSETA, &org);
119
 
#  else
120
 
  gtty(fileno(stdin), &org);
121
 
  tmp=org;
122
 
  tmp.sg_flags &= ~ECHO;
123
 
  tmp.sg_flags |= RAW;
124
 
  stty(fileno(stdin), &tmp);
125
 
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
126
 
  stty(fileno(stdin), &org);
127
 
#  endif
128
 
  if (isatty(fileno(stdout)))
129
 
    fputc('\n',stdout);
130
 
 
131
 
  return strdup(buff);
132
 
}