blob: 013bb922bd03e704cf86e411a5096254649a95ee (
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
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
|
/*************************************************
* FTW EntropySource Source File *
* (C) 1999-2008 Jack Lloyd *
*************************************************/
#include <botan/es_ftw.h>
#include <botan/secmem.h>
#include <botan/util.h>
#include <cstring>
#include <deque>
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199309
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
namespace Botan {
namespace {
class Directory_Walker : public FTW_EntropySource::File_Descriptor_Source
{
public:
Directory_Walker(const std::string& root) { add_directory(root); }
~Directory_Walker();
int next_fd();
private:
void add_directory(const std::string&);
std::deque<std::pair<DIR*, std::string> > dirs;
};
void Directory_Walker::add_directory(const std::string& dirname)
{
DIR* dir = ::opendir(dirname.c_str());
if(dir)
dirs.push_back(std::make_pair(dir, dirname));
}
Directory_Walker::~Directory_Walker()
{
while(dirs.size())
{
::closedir(dirs[0].first);
dirs.pop_front();
}
}
int Directory_Walker::next_fd()
{
while(dirs.size())
{
std::pair<DIR*, std::string> dirinfo = dirs[0];
struct dirent* entry = ::readdir(dirinfo.first);
if(!entry)
{
::closedir(dirinfo.first);
dirs.pop_front();
continue;
}
const std::string filename = entry->d_name;
if(filename == "." || filename == "..")
continue;
const std::string full_path = dirinfo.second + '/' + filename;
struct stat stat_buf;
if(::lstat(full_path.c_str(), &stat_buf) == -1)
continue;
if(S_ISDIR(stat_buf.st_mode))
add_directory(full_path);
else if(S_ISREG(stat_buf.st_mode))
{
int fd = ::open(full_path.c_str(), O_RDONLY | O_NOCTTY);
if(fd > 0)
return fd;
}
}
return -1;
}
}
/**
* FTW_EntropySource Constructor
*/
FTW_EntropySource::FTW_EntropySource(const std::string& p) : path(p)
{
dir = 0;
}
/**
* FTW_EntropySource Destructor
*/
FTW_EntropySource::~FTW_EntropySource()
{
delete dir;
}
u32bit FTW_EntropySource::slow_poll(byte buf[], u32bit length)
{
if(!dir)
dir = new Directory_Walker(path);
SecureVector<byte> read_buf(4096);
u32bit bytes_read = 0;
while(bytes_read < length * 32)
{
int fd = dir->next_fd();
if(fd == -1)
{
delete dir;
dir = new Directory_Walker(path);
fd = dir->next_fd();
if(fd == -1) // still fails (directory not mounted, etc) -> fail
return 0;
}
ssize_t got = ::read(fd, read_buf.begin(), read_buf.size());
if(got > 0 && got <= read_buf.size())
{
for(ssize_t i = 0; i != got; ++i)
buf[i % length] ^= read_buf[i];
// never count any one file for more than 128 bytes
bytes_read += std::min<u32bit>(got, 128);
}
::close(fd);
}
return length;
}
u32bit FTW_EntropySource::fast_poll(byte[], u32bit)
{
return 0; // no op
}
}
|