]> granicus.if.org Git - fcron/blob - read_string.c
Updated to-do list
[fcron] / read_string.c
1 /*
2  * FCRON - periodic command scheduler 
3  *
4  *  Copyright 2000-2014 Thibault Godouet <fcron@free.fr>
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; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  * 
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * 
20  *  The GNU General Public License can also be found in the file
21  *  `LICENSE' that comes with the fcron source distribution.
22  */
23
24
25
26 /* read a string (password, etc ...) securely from a tty */
27
28 #include "global.h"
29 #include "read_string.h"
30 #include "log.h"
31 #include "mem.h"
32
33 extern char debug_opt;
34
35 /* Derived from Andrew Morgan <morgan@linux.kernel.org> work in Linux PAM misc lib. */
36
37 /* may be used in fcrondyn without pam : */
38 #ifndef HAVE_LIBPAM
39 #define PAM_MAX_MSG_SIZE LINE_LEN
40 #endif
41
42 char *
43 read_string(int echo, const char *prompt)
44     /* read a line of input string, giving prompt when appropriate */
45 {
46     struct termios term_before, term_tmp;
47     char line[PAM_MAX_MSG_SIZE];
48     int nc, have_term = 0;
49
50     debug("called with echo='%s', prompt='%s'.", echo ? "ON" : "OFF", prompt);
51
52     if (isatty(STDIN_FILENO)) { /* terminal state */
53
54         /* is a terminal so record settings and flush it */
55         if (tcgetattr(STDIN_FILENO, &term_before) != 0) {
56             debug("error: failed to get terminal settings");
57             return NULL;
58         }
59         memcpy(&term_tmp, &term_before, sizeof(term_tmp));
60         if (!echo)
61             term_tmp.c_lflag &= ~(ECHO);
62         have_term = 1;
63
64     }
65     else if (!echo)
66         debug("warning: cannot turn echo off");
67
68     /* reading the line */
69     while (1) {
70
71         fprintf(stderr, "%s", prompt);
72         /* this may, or may not set echo off -- drop pending input */
73         if (have_term)
74             (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_tmp);
75
76         nc = read(STDIN_FILENO, line, sizeof(line) - 1);
77         if (have_term) {
78             (void)tcsetattr(STDIN_FILENO, TCSADRAIN, &term_before);
79             if (!echo)          /* do we need a newline? */
80                 fprintf(stderr, "\n");
81         }
82         if (nc > 0) {           /* we got some user input */
83             char *input;
84
85             if (nc > 0 && line[nc - 1] == '\n') {       /* <NUL> terminate */
86                 line[--nc] = '\0';
87             }
88             else {
89                 line[nc] = '\0';
90             }
91             input = strdup2(line);
92             Overwrite(line);
93
94             return input;       /* return malloc()ed string */
95         }
96         else if (nc == 0) {     /* Ctrl-D */
97             debug("user did not want to type anything");
98             fprintf(stderr, "\n");
99             break;
100         }
101     }
102
103     if (have_term)
104         (void)tcsetattr(STDIN_FILENO, TCSADRAIN, &term_before);
105
106     memset(line, 0, PAM_MAX_MSG_SIZE);  /* clean up */
107     return NULL;
108 }