made "hide files" and "veto files" into per-service parameter sections,
[Samba.git] / source / lib / getsmbpass.c
blob7ee8c18788543dbf3b15ee594175badb665090c6
1 /* Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 /* Modified to use with samba by Jeremy Allison, 8th July 1995. */
21 #include "includes.h"
23 #ifdef REPLACE_GETPASS
25 #ifdef SYSV_TERMIO
27 /* SYSTEM V TERMIO HANDLING */
29 static struct termio t;
31 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
32 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
33 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
35 #ifndef TCSAFLUSH
36 #define TCSAFLUSH 1
37 #endif
39 #ifndef TCSANOW
40 #define TCSANOW 0
41 #endif
43 int tcgetattr(int fd, struct termio *t)
45 return ioctl(fd, TCGETA, t);
48 int tcsetattr(int fd, int flags, const struct termio *t)
50 if(flags & TCSAFLUSH)
51 ioctl(fd, TCFLSH, TCIOFLUSH);
52 return ioctl(fd, TCSETS, t);
55 #else /* SYSV_TERMIO */
56 #ifdef BSD_TERMIO
58 /* BSD TERMIO HANDLING */
60 static struct sgttyb t;
62 #define ECHO_IS_ON(t) ((t).sg_flags & ECHO)
63 #define TURN_ECHO_OFF(t) ((t).sg_flags &= ~ECHO)
64 #define TURN_ECHO_ON(t) ((t).sg_flags |= ECHO)
66 #ifndef TCSAFLUSH
67 #define TCSAFLUSH 1
68 #endif
70 #ifndef TCSANOW
71 #define TCSANOW 0
72 #endif
74 int tcgetattr(int fd, struct sgttyb *t)
76 return ioctl(fd, TIOCGETP, (char *)t);
79 int tcsetattr(int fd, int flags, const struct sgttyb *t)
81 return ioctl(fd, TIOCSETP, (char *)t);
84 #else /* BSD_TERMIO */
86 /* POSIX TERMIO HANDLING */
87 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
88 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
89 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
91 static struct termios t;
92 #endif /* BSD_TERMIO */
93 #endif /* SYSV_TERMIO */
95 char *getsmbpass(char *prompt)
97 FILE *in, *out;
98 int echo_off;
99 static char buf[256];
100 static size_t bufsize = sizeof(buf);
101 size_t nread;
103 /* Catch problematic signals */
104 signal(SIGINT, SIGNAL_CAST SIG_IGN);
106 /* Try to write to and read from the terminal if we can.
107 If we can't open the terminal, use stderr and stdin. */
109 in = fopen ("/dev/tty", "w+");
110 if (in == NULL)
112 in = stdin;
113 out = stderr;
115 else
116 out = in;
118 setvbuf(in, NULL, _IONBF, 0);
120 /* Turn echoing off if it is on now. */
122 if (tcgetattr (fileno (in), &t) == 0)
124 if (ECHO_IS_ON(t))
126 TURN_ECHO_OFF(t);
127 echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0;
128 TURN_ECHO_ON(t);
130 else
131 echo_off = 0;
133 else
134 echo_off = 0;
136 /* Write the prompt. */
137 fputs (prompt, out);
138 fflush (out);
140 /* Read the password. */
141 buf[0] = 0;
142 fgets(buf, bufsize, in);
143 nread = strlen(buf);
144 if (buf[nread - 1] == '\n')
145 buf[nread - 1] = '\0';
147 /* Restore echoing. */
148 if (echo_off)
149 (void) tcsetattr (fileno (in), TCSANOW, &t);
151 if (in != stdin)
152 /* We opened the terminal; now close it. */
153 fclose (in);
155 /* Catch problematic signals */
156 signal(SIGINT, SIGNAL_CAST SIG_DFL);
158 printf("\n");
159 return buf;
162 #else
164 void getsmbpasswd_dummy() {;}
165 #endif