aboutsummaryrefslogtreecommitdiffstats
path: root/demo-login.c
blob: fd3c75009642c0bd9036cc27cf950f81f61fc40a (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* See LICENSE file for copyright and license details. */
#include "libsecauth.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define PEPPER "pepper"


static char *
server_lookup_hash(void)
{
	char *settings;
	size_t size, len;
	ssize_t r;
	int fd;

	fd = open("democonfig", O_RDONLY);
	if (fd < 0) {
		perror("open");
		return NULL;
	}
	settings = NULL;
	size = 0;
	for (len = 0;; len += (size_t)r) {
		if (len == size) {
			settings = realloc(settings, size += 128);
			if (!settings) {
				perror("realloc");
				return NULL;
			}
		}
		r = read(fd, &settings[len], size - len);
		if (r <= 0) {
			if (!r)
				break;
			perror("read");
			return NULL;
		}
	}
	close(fd);
	if (!len || settings[len - 1] != '\n') {
		fprintf(stderr, "./democonfig is corrupt\n");
		return NULL;
	}
	settings[len - 1] = '\0';

	return settings;
}

int
main(int argc, char *argv[])
{
	struct libsecauth_spec spec;
	char *message, *settings, *expected;
	size_t size, off;
	int fd;
	ssize_t w;
	int r;

	if (argc != 2) {
		fprintf(stderr, "usage: %s password", argv[0]);
		return 1;
	}

	/* -- SERVER -- */

	settings = server_lookup_hash();
	if (!settings)
		return 1;
	if (libsecauth_parse_spec(&spec, settings)) {
		perror("libsecauth_parse_spec");
		return 1;
	}
	spec.posthash = NULL;
	spec.expected = NULL;
	spec.client_rounds -= spec.client_rounds > 0;
	spec.server_rounds = 0;

	size = libsecauth_format_spec(&spec, NULL, 0);
	message = malloc(size);
	if (!message) {
		perror("malloc");
		return 1;
	}
	libsecauth_format_spec(&spec, message, size);
	free(settings);

	/* -- CLIENT -- */

	settings = message;
	if (libsecauth_parse_spec(&spec, settings)) {
		perror("libsecauth_parse_spec");
		return 1;
	}
	message = libsecauth_client_hash(&spec, argv[1]);
	if (!message) {
		perror("libsecauth_client_hash");
		return 1;
	}
	free(settings);

	/* -- SERVER -- */

	settings = server_lookup_hash();
	if (!settings)
		return 1;
	if (libsecauth_parse_spec(&spec, settings)) {
		perror("libsecauth_parse_spec");
		return 1;
	}
	spec.server_rounds += spec.client_rounds > 0;
	spec.client_rounds -= spec.client_rounds > 0;
	r = libsecauth_server_hash(&spec, message, PEPPER, NULL);
	if (r < 0) {
		perror("libsecauth_server_hash");
		return 1;
	} else if (!r) {
		free(settings);
		free(message);
		printf("Incorrect password\n");
		return 0;
	}
	printf("Login OK\n");

	expected = NULL;
	if (!spec.client_rounds) {
		spec.xferhash = "$1$newsalt1$";
		spec.posthash = "$6$rounds=1000$newsalt2$";
		spec.expected = NULL;
		spec.server_rounds = 100;
		if (libsecauth_server_hash(&spec, message, PEPPER, &expected) < 0) {
			perror("libsecauth_server_hash");
			return 1;
		}
		spec.client_rounds = spec.server_rounds;
		spec.server_rounds = 0;
		spec.expected = expected;
		printf("password rehashed\n");
	}
	free(message);
	size = libsecauth_format_spec(&spec, NULL, 0);
	message = malloc(size);
	if (!message) {
		perror("malloc");
		return 1;
	}
	libsecauth_format_spec(&spec, message, size);
	free(settings);
	free(expected);

	message[size - 1] = '\n';
	fd = open("democonfig", O_WRONLY | O_TRUNC, 0600);
	if (fd < 0) {
		perror("open");
		return 1;
	}
	for (off = 0; off < size; off += (size_t)w) {
		w = write(fd, &message[off], size - off);
		if (w <= 0) {
			perror("write");
			return 1;
		}
	}
	close(fd);

	free(message);

	return 0;
}