summaryrefslogtreecommitdiff
path: root/sys/src/cmd/auth/lib/readln.c
blob: ee470a52c9a04f4e8ceea596a49c0ebbd6fee852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <u.h>
#include <libc.h>
#include <authsrv.h>
#include <bio.h>
#include "authcmdlib.h"

void
getpass(char *key, char *pass, int check, int confirm)
{
	char rpass[32], npass[32];
	char *err;

	if(pass == nil)
		pass = npass;

	for(;;){
		readln("Password: ", pass, sizeof npass, 1);
		if(confirm){
			readln("Confirm password: ", rpass, sizeof rpass, 1);
			if(strcmp(pass, rpass) != 0){
				print("mismatch, try again\n");
				continue;
			}
		}
		if(!passtokey(key, pass)){
			print("bad password, try again\n");
			continue;
		}
		if(check)
			if(err = okpasswd(pass)){
				print("%s, try again\n", err);
				continue;
			}
		break;
	}
}

int
getsecret(int passvalid, char *p9pass)
{
	char answer[32];

	readln("assign Inferno/POP secret? (y/n) ", answer, sizeof answer, 0);
	if(*answer != 'y' && *answer != 'Y')
		return 0;

	if(passvalid){
		readln("make it the same as the plan 9 password? (y/n) ",
			answer, sizeof answer, 0);
		if(*answer == 'y' || *answer == 'Y')
			return 1;
	}

	for(;;){
		readln("Secret(0 to 256 characters): ", p9pass,
			sizeof answer, 1);
		readln("Confirm: ", answer, sizeof answer, 1);
		if(strcmp(p9pass, answer) == 0)
			break;
		print("mismatch, try again\n");
	}
	return 1;
}

void
readln(char *prompt, char *line, int len, int raw)
{
	char *p;
	int fdin, fdout, ctl, n, nr;

	fdin = open("/dev/cons", OREAD);
	fdout = open("/dev/cons", OWRITE);
	fprint(fdout, "%s", prompt);
	if(raw){
		ctl = open("/dev/consctl", OWRITE);
		if(ctl < 0)
			error("couldn't set raw mode");
		write(ctl, "rawon", 5);
	} else
		ctl = -1;
	nr = 0;
	p = line;
	for(;;){
		n = read(fdin, p, 1);
		if(n < 0){
			close(ctl);
			error("can't read cons\n");
		}
		if(*p == 0x7f)
			exits(0);
		if(n == 0 || *p == '\n' || *p == '\r'){
			*p = '\0';
			if(raw){
				write(ctl, "rawoff", 6);
				write(fdout, "\n", 1);
			}
			close(ctl);
			return;
		}
		if(*p == '\b'){
			if(nr > 0){
				nr--;
				p--;
			}
		}else{
			nr++;
			p++;
		}
		if(nr == len){
			fprint(fdout, "line too long; try again\n");
			nr = 0;
			p = line;
		}
	}
}