aboutsummaryrefslogtreecommitdiff
path: root/src/auth.c
blob: e9abbe4b2251ef6ed2fb24be8091e20e351b06d5 (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
/* This file is part of varnish-mib -*- c -*-
   Copyright (C) 2014-2015 Sergey Poznyakoff

   Varnish-mib is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   Varnish-mib is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with varnish-mib.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "varnish_mib.h"
#include "sha256.h"
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>

void
varnish_auth_response_fd(int fd, const char *challenge,
			 char response[CLI_AUTH_RESPONSE_LEN + 1])
{
	struct sha256_ctx ctx;
	uint8_t buf[BUFSIZ];
	int i;

	assert(CLI_AUTH_RESPONSE_LEN == (SHA256_DIGEST_SIZE * 2));

	sha256_init_ctx(&ctx);
	sha256_process_bytes(challenge, 32, &ctx);
	sha256_process_bytes("\n", 1, &ctx);
	do {
		i = read(fd, buf, sizeof buf);
		if (i > 0)
			sha256_process_bytes(buf, i, &ctx);
	} while (i > 0);
	sha256_process_bytes(challenge, 32, &ctx);
 	sha256_process_bytes("\n", 1, &ctx);
	sha256_finish_ctx(&ctx, buf);
	for (i = 0; i < SHA256_DIGEST_SIZE; i++)
		sprintf(response + 2 * i, "%02x", buf[i]);
}

int
varnish_auth_response(const char *file, const char *challenge,
		      char response[CLI_AUTH_RESPONSE_LEN + 1])
{
	int fd = open(file, O_RDONLY);
	if (fd == -1) {
		snmp_log(LOG_ERR, "can't open secret file %s: %s\n",
			 file, strerror(errno));
		return -1;
	}
	varnish_auth_response_fd(fd, challenge, response);
	close(fd);
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.