~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to source4/heimdal/lib/hcrypto/ui.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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$");
 
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
        rk_cloexec_file(tty);
 
89
    else
 
90
        tty = stdin;
 
91
 
 
92
    fprintf(stderr, "%s%s", preprompt, prompt);
 
93
    fflush(stderr);
 
94
 
 
95
    if(echo == 0){
 
96
        tcgetattr(fileno(tty), &t_old);
 
97
        memcpy(&t_new, &t_old, sizeof(t_new));
 
98
        t_new.c_lflag &= ~ECHO;
 
99
        tcsetattr(fileno(tty), TCSANOW, &t_new);
 
100
    }
 
101
    intr_flag = 0;
 
102
    p = buf;
 
103
    while(intr_flag == 0){
 
104
        c = getc(tty);
 
105
        if(c == EOF){
 
106
            if(!ferror(tty))
 
107
                ret = 1;
 
108
            break;
 
109
        }
 
110
        if(c == '\n')
 
111
            break;
 
112
        if(of == 0)
 
113
            *p++ = c;
 
114
        of = (p == buf + len);
 
115
    }
 
116
    if(of)
 
117
        p--;
 
118
    *p = 0;
 
119
 
 
120
    if(echo == 0){
 
121
        fprintf(stderr, "\n");
 
122
        tcsetattr(fileno(tty), TCSANOW, &t_old);
 
123
    }
 
124
 
 
125
    if(tty != stdin)
 
126
        fclose(tty);
 
127
 
 
128
    for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
 
129
        if (oksigs[i])
 
130
            sigaction(i, &sigs[i], NULL);
 
131
 
 
132
    if(ret)
 
133
        return -3;
 
134
    if(intr_flag)
 
135
        return -2;
 
136
    if(of)
 
137
        return -1;
 
138
    return 0;
 
139
}
 
140
 
 
141
int
 
142
UI_UTIL_read_pw_string(char *buf, int length, const char *prompt, int verify)
 
143
{
 
144
    int ret;
 
145
 
 
146
    ret = read_string("", prompt, buf, length, 0);
 
147
    if (ret)
 
148
        return ret;
 
149
 
 
150
    if (verify) {
 
151
        char *buf2;
 
152
        buf2 = malloc(length);
 
153
        if (buf2 == NULL)
 
154
            return 1;
 
155
 
 
156
        ret = read_string("Verify password - ", prompt, buf2, length, 0);
 
157
        if (ret) {
 
158
            free(buf2);
 
159
            return ret;
 
160
        }
 
161
        if (strcmp(buf2, buf) != 0)
 
162
            ret = 1;
 
163
        free(buf2);
 
164
    }
 
165
    return ret;
 
166
}