Skip to content

Commit 2198eef

Browse files
committed
Generate random passwords for Kickstart's rootpw section
While the password is pseudo-random it is still better than accidentally having a server with 'changeme' password.
1 parent 66b58f1 commit 2198eef

4 files changed

Lines changed: 39 additions & 2 deletions

File tree

src/XCCDF_POLICY/xccdf_policy_remediate.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,17 +1776,21 @@ static int _xccdf_policy_generate_fix_kickstart(struct oscap_list *rules_to_fix,
17761776
oscap_iterator_free(rules_to_fix_it);
17771777

17781778
_write_text_to_fd(output_fd, "\n");
1779-
const char *common = (
1779+
const char *common_template = (
17801780
"# Default values for automated installation\n"
17811781
"lang en_US.UTF-8\n"
17821782
"keyboard --vckeymap us\n"
17831783
"timezone --utc America/New_York\n"
17841784
"\n"
17851785
"# Root password is required for system rescue tasks\n"
1786-
"rootpw changeme\n"
1786+
"rootpw %s\n"
17871787
"\n"
17881788
);
1789+
char *password = oscap_generate_random_string(24, NULL);
1790+
char *common = oscap_sprintf(common_template, password);
17891791
_write_text_to_fd(output_fd, common);
1792+
free(password);
1793+
free(common);
17901794

17911795
_generate_kickstart_pre(&cmds, output_fd);
17921796

src/common/util.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <config.h>
2626
#endif
2727

28+
#include <time.h>
2829
#include <fcntl.h>
2930
#include <string.h>
3031
#include <ctype.h>
@@ -50,6 +51,24 @@
5051

5152
#define PATH_SEPARATOR '/'
5253

54+
char *oscap_generate_random_string(size_t len, char *charset)
55+
{
56+
char default_charset[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
57+
char *res = NULL;
58+
charset = (charset != NULL && strlen(charset) > 0) ? charset : default_charset;
59+
size_t charset_len = strlen(charset);
60+
if (len > 0) {
61+
srand(time(NULL));
62+
res = malloc(len+1);
63+
res[len] = 0;
64+
while (len-- > 0) {
65+
size_t index = (double) rand() / RAND_MAX * (charset_len-1);
66+
res[len] = charset[index];
67+
}
68+
}
69+
return res;
70+
}
71+
5372
int oscap_string_to_enum(const struct oscap_string_map *map, const char *str)
5473
{
5574
__attribute__nonnull__(map);

src/common/util.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,18 @@ char *oscap_trim(char *str);
384384
/// Print to a newly allocated string using a va_list.
385385
char *oscap_vsprintf(const char *fmt, va_list ap);
386386

387+
/**
388+
* Generates a pseudorandom string of a given length.
389+
* If charset string is not NULL and its length is greater than 0,
390+
* it will be used as a dictionary, otherwise a default alphanumeric set
391+
* will be the base for the generated string.
392+
* Caller is responsible for freeing the returned string.
393+
* @param len desired string length (must be greater than 0)
394+
* @param charset a dictionary string, could be NULL
395+
* @return A random string of desired length.
396+
*/
397+
char *oscap_generate_random_string(size_t len, char *charset);
398+
387399
/**
388400
* Join 2 paths in an intelligent way.
389401
* Both paths are allowed to be NULL.

tests/API/XCCDF/unittests/test_remediation_kickstart.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ function test_normal {
1010
kickstart_modified=$(mktemp)
1111

1212
sed "/This file was generated by OpenSCAP .* using:/d" "$srcdir/test_remediation_kickstart_expected.cfg" > "$expected_modified"
13+
sed "/rootpw .*/d" "$srcdir/test_remediation_kickstart_expected.cfg" > "$expected_modified"
1314
sed -i "s;TEST_DATA_STREAM_PATH;$srcdir/test_remediation_kickstart.ds.xml;" "$expected_modified"
1415

1516
$OSCAP xccdf generate fix --fix-type kickstart --output "$kickstart" --profile common "$srcdir/test_remediation_kickstart.ds.xml"
1617

1718
sed "/This file was generated by OpenSCAP .* using:/d" "$kickstart" > "$kickstart_modified"
19+
sed "/rootpw .*/d" "$kickstart" > "$kickstart_modified"
1820

1921
diff -u "$expected_modified" "$kickstart_modified"
2022

0 commit comments

Comments
 (0)