2 * This file is part of secnet.
3 * See README for full list of copyright holders.
5 * secnet is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * secnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * version 3 along with secnet; if not, see
17 * https://www.gnu.org/licenses/gpl.html.
35 static random_fn random_generate
;
36 static bool_t
random_generate(void *data
, int32_t bytes
, uint8_t *buff
)
38 struct rgen_data
*st
=data
;
41 r
= read(st
->fd
,buff
,bytes
);
44 /* This is totally crap error checking, but AFAICT many callers of
45 * this function do not check the return value. This is a minimal
46 * change to make the code not fail silently-but-insecurely.
48 * A proper fix requires either:
49 * - Declare all random number generation failures as fatal
50 * errors, and make this return void, and fix all callers,
51 * and make this call some appropriate function if it fails.
52 * - Make this have proper error checking (and reporting!)
53 * and make all callers check the error (and report!);
54 * this will be tricky, I think, because you have to report
55 * the errno somewhere.
57 * There's also the issue that this is only one possible
58 * implementation of a random number source; others may not rely
59 * on reading from a file descriptor, and may not produce
60 * appropriate settings of errno.
66 static list_t
*random_apply(closure_t
*self
, struct cloc loc
,
67 dict_t
*context
, list_t
*args
)
71 string_t filename
=NULL
;
75 st
->cl
.description
="randomsource";
76 st
->cl
.type
=CL_RANDOMSRC
;
78 st
->cl
.interface
=&st
->ops
;
80 st
->ops
.blocking
=False
;
81 st
->ops
.generate
=random_generate
;
84 arg1
=list_elem(args
,0);
85 arg2
=list_elem(args
,1);
88 cfgfatal(loc
,"randomsource","requires a filename\n");
90 if (arg1
->type
!= t_string
) {
91 cfgfatal(arg1
->loc
,"randomsource",
92 "filename (arg1) must be a string\n");
94 filename
=arg1
->data
.string
;
97 if (arg2
->type
!= t_bool
) {
98 cfgfatal(arg2
->loc
,"randomsource",
99 "blocking parameter (arg2) must be bool\n");
101 st
->ops
.blocking
=arg2
->data
.bool;
105 cfgfatal(loc
,"randomsource","requires a filename\n");
107 st
->fd
=open(filename
,O_RDONLY
);
109 fatal_perror("randomsource (%s:%d): cannot open %s",arg1
->loc
.file
,
110 arg1
->loc
.line
,filename
);
112 return new_closure(&st
->cl
);
115 void random_module(dict_t
*d
)
117 add_closure(d
,"randomfile",random_apply
);