diff options
author | Stefan Metzmacher <metze@samba.org> | 2010-02-25 15:58:38 +0100 |
---|---|---|
committer | Stefan Metzmacher <metze@samba.org> | 2010-02-26 08:57:28 +0100 |
commit | 792dd38d7c0be5319db1eef625c142a6490cd12b (patch) | |
tree | bd7c95124cdb5cec4a17b3523fc74966aec543df | |
parent | c2edea5ccfb1eadabc3fd4a6d1eb9c5c0a8251e2 (diff) | |
download | samba-792dd38d7c0be5319db1eef625c142a6490cd12b.tar.gz samba-792dd38d7c0be5319db1eef625c142a6490cd12b.tar.bz2 samba-792dd38d7c0be5319db1eef625c142a6490cd12b.zip |
lib/util: add generate_random_password()
metze
-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); |