From 75c99eca68163d2e7fee2911992884c0def689c4 Mon Sep 17 00:00:00 2001 From: Paul Park Date: Tue, 16 May 1995 18:04:26 +0000 Subject: [PATCH] Use available random number generator and free all allocated memory git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@5823 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/kadm/ChangeLog | 8 +++++ src/lib/kadm/configure.in | 2 ++ src/lib/kadm/t_dbentry.c | 66 ++++++++++++++++++++++++++------------- src/lib/kadm/t_ktentry.c | 45 ++++++++++++++++++++------ 4 files changed, 90 insertions(+), 31 deletions(-) diff --git a/src/lib/kadm/ChangeLog b/src/lib/kadm/ChangeLog index 76323c3a8..f41ee3fe5 100644 --- a/src/lib/kadm/ChangeLog +++ b/src/lib/kadm/ChangeLog @@ -1,4 +1,12 @@ +Tue May 16 13:58:30 EDT 1995 Paul Park (pjpark@mit.edu) + * configure.in - Check for srand48, srand and srandom along with + network libraries. + * t_ktentry.c, t_dbentry.c - Use available random number generator + and also make sure memory is freed so we don't + chew up memory in tester. + + Tue May 16 13:19:04 EDT 1995 Paul Park (pjpark@mit.edu) * t_dbentry.c - Change isset to is_a_set to keep Ultrix happy. Also correctly calculate the length of the standard pwd. diff --git a/src/lib/kadm/configure.in b/src/lib/kadm/configure.in index e15eed384..32fc20617 100644 --- a/src/lib/kadm/configure.in +++ b/src/lib/kadm/configure.in @@ -2,11 +2,13 @@ AC_INIT(configure.in) WITH_CCOPTS CONFIG_RULES AC_SET_BUILDTOP +WITH_NETLIB AC_PROG_ARCHIVE AC_PROG_ARCHIVE_ADD AC_PROG_RANLIB AC_PROG_INSTALL AC_HAVE_HEADERS(pwd.h) +AC_HAVE_FUNCS(srand48 srand srandom) AppendRule([all:: libkadm.a]) KRB_INCLUDE WITH_KRB5ROOT diff --git a/src/lib/kadm/t_dbentry.c b/src/lib/kadm/t_dbentry.c index ecdc1b870..33f61402d 100644 --- a/src/lib/kadm/t_dbentry.c +++ b/src/lib/kadm/t_dbentry.c @@ -30,10 +30,32 @@ #include "adm.h" #include "adm_proto.h" +#if HAVE_SRAND48 +#define SRAND srand48 +#define RAND lrand48 +#define RAND_TYPE long +#endif /* HAVE_SRAND48 */ + +#if !defined(RAND_TYPE) && defined(HAVE_SRAND) +#define SRAND srand +#define RAND rand +#define RAND_TYPE int +#endif /* !defined(RAND_TYPE) && defined(HAVE_SRAND) */ + +#if !defined(RAND_TYPE) && defined(HAVE_SRANDOM) +#define SRAND srandom +#define RAND random +#define RAND_TYPE long +#endif /* !defined(RAND_TYPE) && defined(HAVE_SRANDOM) */ + +#if !defined(RAND_TYPE) +There is no random number generator. +#endif /* !defined(RAND_TYPE) */ + /* * Generate a random event that has an a/b chance of succeeding */ -#define RANDOM_EVENT(a,b) ((random() % b) < a) +#define RANDOM_EVENT(a,b) ((RAND() % b) < a) /* Define probabilities of generating each attribute type */ #define PASSWORD_EVENT RANDOM_EVENT(3,5) #define KVNO_EVENT RANDOM_EVENT(2,5) @@ -93,12 +115,12 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) /* Do password on set */ if (isrand) { if (PASSWORD_EVENT) { - pwlen = 9 + (random() % 56); + pwlen = 9 + (RAND() % 56); *pwdp = (char *) malloc(pwlen); for (i=0; ikvno = random(); + dbentp->kvno = RAND(); *validp |= KRB5_ADM_M_KVNO; } } @@ -127,7 +149,7 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) /* Do maxlife */ if (isrand) { if (MAXLIFE_EVENT) { - dbentp->max_life = random(); + dbentp->max_life = RAND(); *validp |= KRB5_ADM_M_MAXLIFE; } } @@ -139,7 +161,7 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) /* Do maxrenewlife */ if (isrand) { if (MAXRENEWLIFE_EVENT) { - dbentp->max_renewable_life = random(); + dbentp->max_renewable_life = RAND(); *validp |= KRB5_ADM_M_MAXRENEWLIFE; } } @@ -151,7 +173,7 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) /* Do expiration */ if (isrand) { if (EXPIRATION_EVENT) { - dbentp->expiration = random(); + dbentp->expiration = RAND(); *validp |= KRB5_ADM_M_EXPIRATION; } } @@ -163,7 +185,7 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) /* Do pw_expiration */ if (isrand) { if (PWEXPIRATION_EVENT) { - dbentp->pw_expiration = random(); + dbentp->pw_expiration = RAND(); *validp |= KRB5_ADM_M_PWEXPIRATION; } } @@ -180,7 +202,7 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) /* Do flags */ if (isrand) { if (FLAGS_EVENT) { - dbentp->attributes = random(); + dbentp->attributes = RAND(); *validp |= KRB5_ADM_M_FLAGS; } } @@ -192,8 +214,8 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) /* Do salts */ if (isrand) { if (SALT_EVENT) { - dbentp->salt_type = (random() % 1); - dbentp->alt_salt_type = (random() % 1); + dbentp->salt_type = (RAND() % 1); + dbentp->alt_salt_type = (RAND() % 1); *validp |= KRB5_ADM_M_SALTTYPE; } } @@ -205,7 +227,7 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) /* Do mkvno */ if (isrand) { if (MKVNO_EVENT) { - dbentp->mkvno = random(); + dbentp->mkvno = RAND(); *validp |= KRB5_ADM_M_MKVNO; } } @@ -219,7 +241,7 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) /* Do lastpwchange */ if (isrand) { if (LASTPWCHANGE_EVENT) { - dbentp->last_pwd_change = random(); + dbentp->last_pwd_change = RAND(); *validp |= KRB5_ADM_M_LASTPWCHANGE; } } @@ -233,7 +255,7 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) /* Do lastsuccess */ if (isrand) { if (LASTSUCCESS_EVENT) { - dbentp->last_success = random(); + dbentp->last_success = RAND(); *validp |= KRB5_ADM_M_LASTSUCCESS; } } @@ -247,7 +269,7 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) /* Do lastfailed */ if (isrand) { if (LASTFAILED_EVENT) { - dbentp->last_failed = random(); + dbentp->last_failed = RAND(); *validp |= KRB5_ADM_M_LASTFAILED; } } @@ -261,7 +283,7 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) /* Do failcount */ if (isrand) { if (FAILCOUNT_EVENT) { - dbentp->fail_auth_count = random(); + dbentp->fail_auth_count = RAND(); *validp |= KRB5_ADM_M_FAILCOUNT; } } @@ -291,7 +313,7 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) /* Do mod_date */ if (isrand) { if (MODDATE_EVENT) { - dbentp->mod_date = random(); + dbentp->mod_date = RAND(); *validp |= KRB5_ADM_M_MODDATE; } } @@ -304,7 +326,7 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) if (is_set) { /* Only 25% may fail at most */ - if (isrand && ((random() % 100) < 75)) { + if (isrand && ((RAND() % 100) < 75)) { *validp &= KRB5_ADM_M_SET_VALID; } #ifdef notdef @@ -315,7 +337,7 @@ gen_dbent(kcontext, dbentp, isrand, validp, pwdp, expectp) } else { /* Only 25% may fail at most */ - if (isrand && ((random() % 100) < 75)) + if (isrand && ((RAND() % 100) < 75)) *validp &= KRB5_ADM_M_GET_VALID; *expectp = ((*validp & ~KRB5_ADM_M_GET_VALID) != 0) ? 1 : 0; } @@ -704,7 +726,7 @@ do_test(pname, verbose, isrand, is_a_set, title, passno) kret = ENOMEM; } - krb5_xfree(kcontext); + krb5_free_context(kcontext); if (verbose) { printf("* End %s ", title); if (isrand) @@ -740,7 +762,7 @@ main(argc, argv) programname = argv[0]; now = time((time_t *) NULL); - srandom((unsigned) now); + SRAND((RAND_TYPE) now); while ((option = getopt(argc, argv, "r:v")) != EOF) { switch (option) { case 'r': diff --git a/src/lib/kadm/t_ktentry.c b/src/lib/kadm/t_ktentry.c index 1e1a52c9c..0b160d033 100644 --- a/src/lib/kadm/t_ktentry.c +++ b/src/lib/kadm/t_ktentry.c @@ -30,6 +30,28 @@ #include "adm.h" #include "adm_proto.h" +#if HAVE_SRAND48 +#define SRAND srand48 +#define RAND lrand48 +#define RAND_TYPE long +#endif /* HAVE_SRAND48 */ + +#if !defined(RAND_TYPE) && defined(HAVE_SRAND) +#define SRAND srand +#define RAND rand +#define RAND_TYPE int +#endif /* !defined(RAND_TYPE) && defined(HAVE_SRAND) */ + +#if !defined(RAND_TYPE) && defined(HAVE_SRANDOM) +#define SRAND srandom +#define RAND random +#define RAND_TYPE long +#endif /* !defined(RAND_TYPE) && defined(HAVE_SRANDOM) */ + +#if !defined(RAND_TYPE) +There is no random number generator. +#endif /* !defined(RAND_TYPE) */ + /* * Generate a principal name. */ @@ -51,16 +73,16 @@ gen_princname(isrand) compsize[i] = 0; complist[i] = (char *) NULL; } - ncomps = 2 + (random() % 7); + ncomps = 2 + (RAND() % 7); totsize = 0; for (i=0; ikey.contents = (krb5_octet *) malloc(keylen); if (ktentp->key.contents) { ktentp->key.length = keylen; for (i=0; ikey.contents[i] = random() & 255; + ktentp->key.contents[i] = RAND() & 255; } } else { @@ -139,8 +161,9 @@ gen_ktent(kcontext, ktentp, isrand) princname, &ktentp->principal) ) { - ktentp->vno = (isrand) ? random() : 1; + ktentp->vno = (isrand) ? RAND() : 1; gen_key(ktentp, isrand); + free(princname); } } @@ -293,9 +316,13 @@ do_test(pname, verbose, isrand, title, passno) /* Cleanup */ if (in_ktent->principal) krb5_free_principal(kcontext, in_ktent->principal); + if (in_ktent->key.contents) + free(in_ktent->key.contents); free(in_ktent); if (out_ktent->principal) krb5_free_principal(kcontext, out_ktent->principal); + if (out_ktent->key.contents) + free(out_ktent->key.contents); free(out_ktent); } else { @@ -303,7 +330,7 @@ do_test(pname, verbose, isrand, title, passno) kret = ENOMEM; } - krb5_xfree(kcontext); + krb5_free_context(kcontext); if (verbose) { printf("* End %s ", title); if (isrand) @@ -339,7 +366,7 @@ main(argc, argv) programname = argv[0]; now = time((time_t *) NULL); - srandom((unsigned) now); + SRAND((RAND_TYPE) now); while ((option = getopt(argc, argv, "r:v")) != EOF) { switch (option) { case 'r': -- 2.26.2