drop CHECK_SYMBOLS
[heimdal.git] / lib / hcrypto / ui.c
blob144f65b1fde162495391dc0647f6f6aeabaa72a5
1 /*
2 * Copyright (c) 1997 - 2000, 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 RCSID("$Id$");
37 #endif
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <signal.h>
43 #include <termios.h>
44 #include <roken.h>
46 #include <ui.h>
48 static sig_atomic_t intr_flag;
50 static void
51 intr(int sig)
53 intr_flag++;
56 #ifndef NSIG
57 #define NSIG 47
58 #endif
60 static int
61 read_string(const char *preprompt, const char *prompt,
62 char *buf, size_t len, int echo)
64 struct sigaction sigs[NSIG];
65 int oksigs[NSIG];
66 struct sigaction sa;
67 FILE *tty;
68 int ret = 0;
69 int of = 0;
70 int i;
71 int c;
72 char *p;
74 struct termios t_new, t_old;
76 memset(&oksigs, 0, sizeof(oksigs));
78 memset(&sa, 0, sizeof(sa));
79 sa.sa_handler = intr;
80 sigemptyset(&sa.sa_mask);
81 sa.sa_flags = 0;
82 for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
83 if (i != SIGALRM)
84 if (sigaction(i, &sa, &sigs[i]) == 0)
85 oksigs[i] = 1;
87 if((tty = fopen("/dev/tty", "r")) == NULL)
88 tty = stdin;
90 fprintf(stderr, "%s%s", preprompt, prompt);
91 fflush(stderr);
93 if(echo == 0){
94 tcgetattr(fileno(tty), &t_old);
95 memcpy(&t_new, &t_old, sizeof(t_new));
96 t_new.c_lflag &= ~ECHO;
97 tcsetattr(fileno(tty), TCSANOW, &t_new);
99 intr_flag = 0;
100 p = buf;
101 while(intr_flag == 0){
102 c = getc(tty);
103 if(c == EOF){
104 if(!ferror(tty))
105 ret = 1;
106 break;
108 if(c == '\n')
109 break;
110 if(of == 0)
111 *p++ = c;
112 of = (p == buf + len);
114 if(of)
115 p--;
116 *p = 0;
118 if(echo == 0){
119 printf("\n");
120 tcsetattr(fileno(tty), TCSANOW, &t_old);
123 if(tty != stdin)
124 fclose(tty);
126 for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
127 if (oksigs[i])
128 sigaction(i, &sigs[i], NULL);
130 if(ret)
131 return -3;
132 if(intr_flag)
133 return -2;
134 if(of)
135 return -1;
136 return 0;
140 UI_UTIL_read_pw_string(char *buf, int length, const char *prompt, int verify)
142 int ret;
144 ret = read_string("", prompt, buf, length, 0);
145 if (ret)
146 return ret;
148 if (verify) {
149 char *buf2;
150 buf2 = malloc(length);
151 if (buf2 == NULL)
152 return 1;
154 ret = read_string("Verify password - ", prompt, buf2, length, 0);
155 if (ret) {
156 free(buf2);
157 return ret;
159 if (strcmp(buf2, buf) != 0)
160 ret = 1;
161 free(buf2);
163 return ret;