1*0a6a1f1dSLionel Sambuc /* $NetBSD: ui.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1997 - 2000, 2005 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include <config.h>
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc #include <stdio.h>
39ebfedea0SLionel Sambuc #include <stdlib.h>
40ebfedea0SLionel Sambuc #include <string.h>
41ebfedea0SLionel Sambuc #include <signal.h>
42ebfedea0SLionel Sambuc #ifdef HAVE_TERMIOS_H
43ebfedea0SLionel Sambuc #include <termios.h>
44ebfedea0SLionel Sambuc #endif
45ebfedea0SLionel Sambuc #include <krb5/roken.h>
46ebfedea0SLionel Sambuc
47ebfedea0SLionel Sambuc #include <ui.h>
48ebfedea0SLionel Sambuc #ifdef HAVE_CONIO_H
49ebfedea0SLionel Sambuc #include <conio.h>
50ebfedea0SLionel Sambuc #endif
51ebfedea0SLionel Sambuc
52ebfedea0SLionel Sambuc static sig_atomic_t intr_flag;
53ebfedea0SLionel Sambuc
54ebfedea0SLionel Sambuc static void
intr(int sig)55ebfedea0SLionel Sambuc intr(int sig)
56ebfedea0SLionel Sambuc {
57ebfedea0SLionel Sambuc intr_flag++;
58ebfedea0SLionel Sambuc }
59ebfedea0SLionel Sambuc
60ebfedea0SLionel Sambuc #ifdef HAVE_CONIO_H
61ebfedea0SLionel Sambuc
62ebfedea0SLionel Sambuc /*
63ebfedea0SLionel Sambuc * Windows does console slightly different then then unix case.
64ebfedea0SLionel Sambuc */
65ebfedea0SLionel Sambuc
66ebfedea0SLionel Sambuc static int
read_string(const char * preprompt,const char * prompt,char * buf,size_t len,int echo)67ebfedea0SLionel Sambuc read_string(const char *preprompt, const char *prompt,
68ebfedea0SLionel Sambuc char *buf, size_t len, int echo)
69ebfedea0SLionel Sambuc {
70ebfedea0SLionel Sambuc int of = 0;
71ebfedea0SLionel Sambuc int c;
72ebfedea0SLionel Sambuc char *p;
73ebfedea0SLionel Sambuc void (*oldsigintr)(int);
74ebfedea0SLionel Sambuc
75ebfedea0SLionel Sambuc _cprintf("%s%s", preprompt, prompt);
76ebfedea0SLionel Sambuc
77ebfedea0SLionel Sambuc oldsigintr = signal(SIGINT, intr);
78ebfedea0SLionel Sambuc
79ebfedea0SLionel Sambuc p = buf;
80ebfedea0SLionel Sambuc while(intr_flag == 0){
81ebfedea0SLionel Sambuc c = ((echo)? _getche(): _getch());
82ebfedea0SLionel Sambuc if(c == '\n' || c == '\r')
83ebfedea0SLionel Sambuc break;
84ebfedea0SLionel Sambuc if(of == 0)
85ebfedea0SLionel Sambuc *p++ = c;
86ebfedea0SLionel Sambuc of = (p == buf + len);
87ebfedea0SLionel Sambuc }
88ebfedea0SLionel Sambuc if(of)
89ebfedea0SLionel Sambuc p--;
90ebfedea0SLionel Sambuc *p = 0;
91ebfedea0SLionel Sambuc
92ebfedea0SLionel Sambuc if(echo == 0){
93ebfedea0SLionel Sambuc printf("\n");
94ebfedea0SLionel Sambuc }
95ebfedea0SLionel Sambuc
96ebfedea0SLionel Sambuc signal(SIGINT, oldsigintr);
97ebfedea0SLionel Sambuc
98ebfedea0SLionel Sambuc if(intr_flag)
99ebfedea0SLionel Sambuc return -2;
100ebfedea0SLionel Sambuc if(of)
101ebfedea0SLionel Sambuc return -1;
102ebfedea0SLionel Sambuc return 0;
103ebfedea0SLionel Sambuc }
104ebfedea0SLionel Sambuc
105ebfedea0SLionel Sambuc #else /* !HAVE_CONIO_H */
106ebfedea0SLionel Sambuc
107ebfedea0SLionel Sambuc #ifndef NSIG
108ebfedea0SLionel Sambuc #define NSIG 47
109ebfedea0SLionel Sambuc #endif
110ebfedea0SLionel Sambuc
111ebfedea0SLionel Sambuc static int
read_string(const char * preprompt,const char * prompt,char * buf,size_t len,int echo)112ebfedea0SLionel Sambuc read_string(const char *preprompt, const char *prompt,
113ebfedea0SLionel Sambuc char *buf, size_t len, int echo)
114ebfedea0SLionel Sambuc {
115ebfedea0SLionel Sambuc struct sigaction sigs[NSIG];
116ebfedea0SLionel Sambuc int oksigs[NSIG];
117ebfedea0SLionel Sambuc struct sigaction sa;
118ebfedea0SLionel Sambuc FILE *tty;
119ebfedea0SLionel Sambuc int ret = 0;
120ebfedea0SLionel Sambuc int of = 0;
121ebfedea0SLionel Sambuc int i;
122ebfedea0SLionel Sambuc int c;
123ebfedea0SLionel Sambuc char *p;
124ebfedea0SLionel Sambuc
125ebfedea0SLionel Sambuc struct termios t_new, t_old;
126ebfedea0SLionel Sambuc
127ebfedea0SLionel Sambuc memset(&oksigs, 0, sizeof(oksigs));
128ebfedea0SLionel Sambuc
129ebfedea0SLionel Sambuc memset(&sa, 0, sizeof(sa));
130ebfedea0SLionel Sambuc sa.sa_handler = intr;
131ebfedea0SLionel Sambuc sigemptyset(&sa.sa_mask);
132ebfedea0SLionel Sambuc sa.sa_flags = 0;
133ebfedea0SLionel Sambuc for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
134ebfedea0SLionel Sambuc if (i != SIGALRM)
135ebfedea0SLionel Sambuc if (sigaction(i, &sa, &sigs[i]) == 0)
136ebfedea0SLionel Sambuc oksigs[i] = 1;
137ebfedea0SLionel Sambuc
138ebfedea0SLionel Sambuc if((tty = fopen("/dev/tty", "r")) != NULL)
139ebfedea0SLionel Sambuc rk_cloexec_file(tty);
140ebfedea0SLionel Sambuc else
141ebfedea0SLionel Sambuc tty = stdin;
142ebfedea0SLionel Sambuc
143ebfedea0SLionel Sambuc fprintf(stderr, "%s%s", preprompt, prompt);
144ebfedea0SLionel Sambuc fflush(stderr);
145ebfedea0SLionel Sambuc
146ebfedea0SLionel Sambuc if(echo == 0){
147ebfedea0SLionel Sambuc tcgetattr(fileno(tty), &t_old);
148ebfedea0SLionel Sambuc memcpy(&t_new, &t_old, sizeof(t_new));
149ebfedea0SLionel Sambuc t_new.c_lflag &= ~ECHO;
150ebfedea0SLionel Sambuc tcsetattr(fileno(tty), TCSANOW, &t_new);
151ebfedea0SLionel Sambuc }
152ebfedea0SLionel Sambuc intr_flag = 0;
153ebfedea0SLionel Sambuc p = buf;
154ebfedea0SLionel Sambuc while(intr_flag == 0){
155ebfedea0SLionel Sambuc c = getc(tty);
156ebfedea0SLionel Sambuc if(c == EOF){
157ebfedea0SLionel Sambuc if(!ferror(tty))
158ebfedea0SLionel Sambuc ret = 1;
159ebfedea0SLionel Sambuc break;
160ebfedea0SLionel Sambuc }
161ebfedea0SLionel Sambuc if(c == '\n')
162ebfedea0SLionel Sambuc break;
163ebfedea0SLionel Sambuc if(of == 0)
164ebfedea0SLionel Sambuc *p++ = c;
165ebfedea0SLionel Sambuc of = (p == buf + len);
166ebfedea0SLionel Sambuc }
167ebfedea0SLionel Sambuc if(of)
168ebfedea0SLionel Sambuc p--;
169ebfedea0SLionel Sambuc *p = 0;
170ebfedea0SLionel Sambuc
171ebfedea0SLionel Sambuc if(echo == 0){
172ebfedea0SLionel Sambuc fprintf(stderr, "\n");
173ebfedea0SLionel Sambuc tcsetattr(fileno(tty), TCSANOW, &t_old);
174ebfedea0SLionel Sambuc }
175ebfedea0SLionel Sambuc
176ebfedea0SLionel Sambuc if(tty != stdin)
177ebfedea0SLionel Sambuc fclose(tty);
178ebfedea0SLionel Sambuc
179ebfedea0SLionel Sambuc for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
180ebfedea0SLionel Sambuc if (oksigs[i])
181ebfedea0SLionel Sambuc sigaction(i, &sigs[i], NULL);
182ebfedea0SLionel Sambuc
183ebfedea0SLionel Sambuc if(ret)
184ebfedea0SLionel Sambuc return -3;
185ebfedea0SLionel Sambuc if(intr_flag)
186ebfedea0SLionel Sambuc return -2;
187ebfedea0SLionel Sambuc if(of)
188ebfedea0SLionel Sambuc return -1;
189ebfedea0SLionel Sambuc return 0;
190ebfedea0SLionel Sambuc }
191ebfedea0SLionel Sambuc
192ebfedea0SLionel Sambuc #endif /* HAVE_CONIO_H */
193ebfedea0SLionel Sambuc
194ebfedea0SLionel Sambuc int
UI_UTIL_read_pw_string(char * buf,int length,const char * prompt,int verify)195ebfedea0SLionel Sambuc UI_UTIL_read_pw_string(char *buf, int length, const char *prompt, int verify)
196ebfedea0SLionel Sambuc {
197ebfedea0SLionel Sambuc int ret;
198ebfedea0SLionel Sambuc
199ebfedea0SLionel Sambuc ret = read_string("", prompt, buf, length, 0);
200ebfedea0SLionel Sambuc if (ret)
201ebfedea0SLionel Sambuc return ret;
202ebfedea0SLionel Sambuc
203ebfedea0SLionel Sambuc if (verify) {
204ebfedea0SLionel Sambuc char *buf2;
205ebfedea0SLionel Sambuc buf2 = malloc(length);
206ebfedea0SLionel Sambuc if (buf2 == NULL)
207ebfedea0SLionel Sambuc return 1;
208ebfedea0SLionel Sambuc
209ebfedea0SLionel Sambuc ret = read_string("Verify password - ", prompt, buf2, length, 0);
210ebfedea0SLionel Sambuc if (ret) {
211ebfedea0SLionel Sambuc free(buf2);
212ebfedea0SLionel Sambuc return ret;
213ebfedea0SLionel Sambuc }
214ebfedea0SLionel Sambuc if (strcmp(buf2, buf) != 0)
215ebfedea0SLionel Sambuc ret = 1;
216ebfedea0SLionel Sambuc free(buf2);
217ebfedea0SLionel Sambuc }
218ebfedea0SLionel Sambuc return ret;
219ebfedea0SLionel Sambuc }
220