source: trunk-3.0/source/lib/replace/getpass.c@ 101

Last change on this file since 101 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 4.6 KB
Line 
1/* Copyright (C) 1992-1998 Free Software Foundation, Inc.
2This file is part of the GNU C Library.
3
4The GNU C Library is free software; you can redistribute it and/or
5modify it under the terms of the GNU Library General Public License as
6published by the Free Software Foundation; either version 2 of the
7License, or (at your option) any later version.
8
9The GNU C Library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public
15License along with the GNU C Library; see the file COPYING.LIB. If
16not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17Cambridge, MA 02139, USA. */
18
19/* Modified to use with samba by Jeremy Allison, 8th July 1995. */
20
21#include "replace.h"
22
23#if defined(HAVE_TERMIOS_H)
24/* POSIX terminal handling. */
25#include <termios.h>
26#elif defined(HAVE_TERMIO_H)
27/* Older SYSV terminal handling - don't use if we can avoid it. */
28#include <termio.h>
29#elif defined(HAVE_SYS_TERMIO_H)
30/* Older SYSV terminal handling - don't use if we can avoid it. */
31#include <sys/termio.h>
32#endif
33
34#ifdef HAVE_SYS_WAIT_H
35#include <sys/wait.h>
36#endif
37
38/*
39 * Define additional missing types
40 */
41#ifndef HAVE_SIG_ATOMIC_T_TYPE
42typedef int sig_atomic_t;
43#endif
44
45#ifndef SIGCLD
46#define SIGCLD SIGCHLD
47#endif
48
49#ifndef SIGNAL_CAST
50#define SIGNAL_CAST (RETSIGTYPE (*)(int))
51#endif
52
53#ifdef REPLACE_GETPASS
54
55#ifdef SYSV_TERMIO
56
57/* SYSTEM V TERMIO HANDLING */
58
59static struct termio t;
60
61#define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
62#define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
63#define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
64
65#ifndef TCSAFLUSH
66#define TCSAFLUSH 1
67#endif
68
69#ifndef TCSANOW
70#define TCSANOW 0
71#endif
72
73static int tcgetattr(int fd, struct termio *_t)
74{
75 return ioctl(fd, TCGETA, _t);
76}
77
78static int tcsetattr(int fd, int flags, struct termio *_t)
79{
80 if(flags & TCSAFLUSH)
81 ioctl(fd, TCFLSH, TCIOFLUSH);
82 return ioctl(fd, TCSETS, _t);
83}
84
85#elif !defined(TCSAFLUSH)
86
87/* BSD TERMIO HANDLING */
88
89static struct sgttyb t;
90
91#define ECHO_IS_ON(t) ((t).sg_flags & ECHO)
92#define TURN_ECHO_OFF(t) ((t).sg_flags &= ~ECHO)
93#define TURN_ECHO_ON(t) ((t).sg_flags |= ECHO)
94
95#define TCSAFLUSH 1
96#define TCSANOW 0
97
98static int tcgetattr(int fd, struct sgttyb *_t)
99{
100 return ioctl(fd, TIOCGETP, (char *)_t);
101}
102
103static int tcsetattr(int fd, int flags, struct sgttyb *_t)
104{
105 return ioctl(fd, TIOCSETP, (char *)_t);
106}
107
108#else /* POSIX TERMIO HANDLING */
109#define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
110#define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
111#define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
112
113static struct termios t;
114#endif /* SYSV_TERMIO */
115
116static void catch_signal(int signum,void (*handler)(int ))
117{
118#ifdef HAVE_SIGACTION
119 struct sigaction act;
120 struct sigaction oldact;
121
122 memset(&act, 0, sizeof(act));
123
124 act.sa_handler = handler;
125#ifdef SA_RESTART
126 /*
127 * We *want* SIGALRM to interrupt a system call.
128 */
129 if(signum != SIGALRM)
130 act.sa_flags = SA_RESTART;
131#endif
132 sigemptyset(&act.sa_mask);
133 sigaddset(&act.sa_mask,signum);
134 sigaction(signum,&act,&oldact);
135 return oldact.sa_handler;
136#else /* !HAVE_SIGACTION */
137 /* FIXME: need to handle sigvec and systems with broken signal() */
138 return signal(signum, handler);
139#endif
140}
141
142char *getsmbpass(const char *prompt)
143{
144 FILE *in, *out;
145 int echo_off;
146 static char buf[256];
147 static size_t bufsize = sizeof(buf);
148 size_t nread;
149
150 /* Catch problematic signals */
151 catch_signal(SIGINT, SIGNAL_CAST SIG_IGN);
152
153 /* Try to write to and read from the terminal if we can.
154 If we can't open the terminal, use stderr and stdin. */
155
156 in = fopen ("/dev/tty", "w+");
157 if (in == NULL)
158 {
159 in = stdin;
160 out = stderr;
161 }
162 else
163 out = in;
164
165 setvbuf(in, NULL, _IONBF, 0);
166
167 /* Turn echoing off if it is on now. */
168
169 if (tcgetattr (fileno (in), &t) == 0)
170 {
171 if (ECHO_IS_ON(t))
172 {
173 TURN_ECHO_OFF(t);
174 echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0;
175 TURN_ECHO_ON(t);
176 }
177 else
178 echo_off = 0;
179 }
180 else
181 echo_off = 0;
182
183 /* Write the prompt. */
184 fputs (prompt, out);
185 fflush (out);
186
187 /* Read the password. */
188 buf[0] = 0;
189 fgets(buf, bufsize, in);
190 nread = strlen(buf);
191 if (buf[nread - 1] == '\n')
192 buf[nread - 1] = '\0';
193
194 /* Restore echoing. */
195 if (echo_off)
196 (void) tcsetattr (fileno (in), TCSANOW, &t);
197
198 if (in != stdin)
199 /* We opened the terminal; now close it. */
200 fclose (in);
201
202 /* Catch problematic signals */
203 catch_signal(SIGINT, SIGNAL_CAST SIG_DFL);
204
205 printf("\n");
206 return buf;
207}
208
209#else
210 void getsmbpasswd_dummy(void);
211 void getsmbpasswd_dummy(void) {;}
212#endif
Note: See TracBrowser for help on using the repository browser.