aboutsummaryrefslogtreecommitdiffstats
path: root/demo-setup.c
blob: fa18c05943c654787451a89b5d7690a484a6d820 (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
/* 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"


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

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

	/* -- SERVER -- */

	memset(&spec, 0, sizeof(spec));
	spec.prehash = "$6$rounds=2000$salt1$";
	spec.xferhash = "$1$salt2$";
	spec.client_rounds = 100;
	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);

	/* -- CLIENT -- */

	memset(&spec, 0, sizeof(spec));
	if (libsecauth_parse_spec(&spec, message)) {
		perror("libsecauth_parse_spec");
		return 1;
	}

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

	/* -- SERVER -- */

	memset(&spec, 0, sizeof(spec));
	spec.prehash = "$6$rounds=2000$salt1$";
	spec.xferhash = "$1$salt2$";
	spec.client_rounds = 100;
	spec.server_rounds = 0;
	spec.posthash = "$6$rounds=1000$salt3$";
	if (libsecauth_server_hash(&spec, message, PEPPER, &hash) < 0) {
		perror("libsecauth_server_hash");
		return 1;
	}
	spec.expected = hash;
	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(hash);

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

	return 0;
}