r23455: These buffers may not be null terminated. Ensure we don't run past the
[sfrench/samba-autobuild/.git] / source4 / heimdal / lib / des / ui.c
1 /*
2  * Copyright (c) 1997 - 2000, 2005 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
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. 
16  *
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. 
20  *
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. 
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 RCSID("$Id: ui.c,v 1.6 2006/09/22 15:45:57 lha Exp $");
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <signal.h>
43 #include <termios.h>
44 #include <roken.h>
45
46 #include <ui.h>
47
48 static sig_atomic_t intr_flag;
49
50 static void
51 intr(int sig)
52 {
53     intr_flag++;
54 }
55
56 #ifndef NSIG
57 #define NSIG 47
58 #endif
59
60 static int
61 read_string(const char *preprompt, const char *prompt, 
62             char *buf, size_t len, int echo)
63 {
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;
73
74     struct termios t_new, t_old;
75
76     memset(&oksigs, 0, sizeof(oksigs));
77
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;
86
87     if((tty = fopen("/dev/tty", "r")) == NULL)
88         tty = stdin;
89        
90     fprintf(stderr, "%s%s", preprompt, prompt);
91     fflush(stderr);
92
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);
98     }
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;
107         }
108         if(c == '\n')
109             break;
110         if(of == 0)
111             *p++ = c;
112         of = (p == buf + len);
113     }
114     if(of)
115         p--;
116     *p = 0;
117     
118     if(echo == 0){
119         printf("\n");
120         tcsetattr(fileno(tty), TCSANOW, &t_old);
121     }
122     
123     if(tty != stdin)
124         fclose(tty);
125
126     for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
127         if (oksigs[i])
128             sigaction(i, &sigs[i], NULL);
129     
130     if(ret)
131         return -3;
132     if(intr_flag)
133         return -2;
134     if(of)
135         return -1;
136     return 0;
137 }
138
139 int
140 UI_UTIL_read_pw_string(char *buf, int length, const char *prompt, int verify)
141 {
142     int ret;
143
144     ret = read_string("", prompt, buf, length, 0);
145     if (ret)
146         return ret;
147
148     if (verify) {
149         char *buf2;
150         buf2 = malloc(length);
151         if (buf2 == NULL)
152             return 1;
153
154         ret = read_string("Verify password - ", prompt, buf2, length, 0);
155         if (ret) {
156             free(buf2);
157             return ret;
158         }
159         if (strcmp(buf2, buf) != 0)
160             ret = 1;
161         free(buf2);
162     }
163     return ret;
164 }