diff options
Diffstat (limited to 'lib/util')
-rw-r--r-- | lib/util/genrand.c | 42 | ||||
-rw-r--r-- | lib/util/util.h | 5 |
2 files changed, 47 insertions, 0 deletions
diff --git a/lib/util/genrand.c b/lib/util/genrand.c index f0544023f1..02b8d8b478 100644 --- a/lib/util/genrand.c +++ b/lib/util/genrand.c @@ -362,6 +362,48 @@ again: } /** + * Generate a random text password. + */ + +_PUBLIC_ char *generate_random_password(TALLOC_CTX *mem_ctx, size_t min, size_t max) +{ + char *retstr; + const char *c_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_-#.,@$%&!?:;<=>(){}[]~"; + size_t len = max; + size_t diff; + + if (min > max) { + errno = EINVAL; + return NULL; + } + + diff = max - min; + + if (diff > 0 ) { + size_t tmp; + + generate_random_buffer((uint8_t *)&tmp, sizeof(tmp)); + + tmp %= diff; + + len = min + tmp; + } + +again: + retstr = generate_random_str_list(mem_ctx, len, c_list); + if (!retstr) return NULL; + + /* we need to make sure the random string passes basic quality tests + or it might be rejected by windows as a password */ + if (len >= 7 && !check_password_quality(retstr)) { + talloc_free(retstr); + goto again; + } + + return retstr; +} + +/** * Generate an array of unique text strings all of the same length. * The returned string will be allocated. * Returns NULL if the number of unique combinations cannot be created. diff --git a/lib/util/util.h b/lib/util/util.h index e1608a8ac4..264396efe6 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -178,6 +178,11 @@ _PUBLIC_ uint32_t generate_random(void); _PUBLIC_ bool check_password_quality(const char *s); /** + * Generate a random text password. + */ +_PUBLIC_ char *generate_random_password(TALLOC_CTX *mem_ctx, size_t min, size_t max); + +/** Use the random number generator to generate a random string. **/ _PUBLIC_ char *generate_random_str_list(TALLOC_CTX *mem_ctx, size_t len, const char *list); |