summaryrefslogtreecommitdiffstats
path: root/contrib/pam_modules/pam_passwdqc/passwdqc_random.c
blob: 0d9a04bc1c208b21bc981fa1920f5df2b5a571b1 (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
/*
 * Copyright (c) 2000-2002 by Solar Designer. See LICENSE.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

#include "passwdqc.h"

#define SEPARATORS			"_,.;:-!&"

static int read_loop(int fd, char *buffer, int count)
{
	int offset, block;

	offset = 0;
	while (count > 0) {
		block = read(fd, &buffer[offset], count);

		if (block < 0) {
			if (errno == EINTR) continue;
			return block;
		}
		if (!block) return offset;

		offset += block;
		count -= block;
	}

	return offset;
}

char *_passwdqc_random(passwdqc_params_t *params)
{
	static char output[0x100];
	int bits;
	int use_separators, count, i;
	unsigned int length;
	char *start, *end;
	int fd;
	unsigned char bytes[2];

	if (!(bits = params->random_bits))
		return NULL;

	count = 1 + ((bits - 12) + 14) / 15;
	use_separators = ((bits + 11) / 12 != count);

	length = count * 7 - 1;
	if (length >= sizeof(output) || (int)length > params->max)
		return NULL;

	if ((fd = open("/dev/urandom", O_RDONLY)) < 0) return NULL;

	length = 0;
	do {
		if (read_loop(fd, bytes, sizeof(bytes)) != sizeof(bytes)) {
			close(fd);
			return NULL;
		}

		i = (((int)bytes[1] & 0x0f) << 8) | (int)bytes[0];
		start = _passwdqc_wordset_4k[i];
		end = memchr(start, '\0', 6);
		if (!end) end = start + 6;
		if (length + (end - start) >= sizeof(output) - 1) {
			close(fd);
			return NULL;
		}
		memcpy(&output[length], start, end - start);
		length += end - start;
		bits -= 12;

		if (use_separators && bits > 3) {
			i = ((int)bytes[1] & 0x70) >> 4;
			output[length++] = SEPARATORS[i];
			bits -= 3;
		} else
		if (bits > 0)
			output[length++] = ' ';
	} while (bits > 0);

	memset(bytes, 0, sizeof(bytes));
	output[length] = '\0';

	close(fd);

	return output;
}
OpenPOWER on IntegriCloud