diff --git a/srogue/configure.ac b/srogue/configure.ac index 7db64e5..b212e4f 100644 --- a/srogue/configure.ac +++ b/srogue/configure.ac @@ -24,7 +24,7 @@ AC_PROG_GCC_TRADITIONAL AC_TYPE_SIGNAL AC_FUNC_STAT AC_FUNC_VPRINTF -AC_CHECK_FUNCS([erasechar killchar alarm getpass memset setenv strchr nlist _spawnl spawnl getpwuid loadav getloadavg strerror setgid setuid getuid getgid]) +AC_CHECK_FUNCS([erasechar killchar alarm getpass memset setenv strchr nlist _spawnl spawnl getpwuid loadav getloadavg strerror setgid setuid getuid getgid lrand48 random srand48 srandom]) AC_PROG_INSTALL AC_ARG_WITH(program-name, AC_HELP_STRING([--with-program-name=NAME],[alternate executable name]),[progname="$withval" ], [progname="srogue"] ) diff --git a/srogue/main.c b/srogue/main.c index c68a3d6..816d647 100644 --- a/srogue/main.c +++ b/srogue/main.c @@ -173,7 +173,7 @@ char **envp; fflush(stdout); seed = dnum; - srandom(seed); /* init rnd number gen */ + md_srandom(seed); /* init rnd number gen */ md_onsignal_exit(); /* just in case */ @@ -354,7 +354,7 @@ int range; if (range == 0) wh = 0; else { - wh = random() % range; + wh = md_random() % range; wh &= 0x7FFFFFFF; } return wh; diff --git a/srogue/mdport.c b/srogue/mdport.c index 57e5d31..b9156e1 100644 --- a/srogue/mdport.c +++ b/srogue/mdport.c @@ -1567,3 +1567,27 @@ md_resetpriv(void) setgid(playgid); #endif } + +int +md_random(void) +{ +#if defined (HAVE_LRAND48) + return lrand48(); +#elif defined (HAVE_RANDOM) + return random(); +#else + return rand(); +#endif +} + +void +md_srandom(unsigned int seed) +{ +#if defined (HAVE_SRAND48) + srand48(seed); +#elif defined (HAVE_SRANDOM) + srandom(seed); +#else + srand(seed); +#endif +} diff --git a/srogue/rogue.h b/srogue/rogue.h index b66191c..831b3ad 100644 --- a/srogue/rogue.h +++ b/srogue/rogue.h @@ -71,6 +71,8 @@ int md_issymlink(char *sp); long md_memused(void); void md_droppriv(void); void md_resetpriv(void); +int md_random(void); +void md_srandom(unsigned int seed); char *xcrypt(const char *key, const char *setting); #define reg register /* register abbr. */ diff --git a/srogue/save.c b/srogue/save.c index 1a97d00..ce82666 100644 --- a/srogue/save.c +++ b/srogue/save.c @@ -287,7 +287,7 @@ char *file, **envp; #endif if (!wizard) { -#ifndef __DJGPP__ +#if defined(HAVE_WORKING_FORK) endwin(); if (!use_savedir) { @@ -344,6 +344,6 @@ char *file, **envp; strcpy(file_name, file); setup(); restscr(cw); - srandom(getpid()); + md_srandom(getpid()); playit(); } diff --git a/srogue/xcrypt.c b/srogue/xcrypt.c index 7bad484..f288adc 100644 --- a/srogue/xcrypt.c +++ b/srogue/xcrypt.c @@ -1,32 +1,32 @@ /* - * FreeSec: libcrypt + * FreeSec: libcrypt * - * Copyright (C) 1994 David Burren - * All rights reserved. + * Copyright (C) 1994 David Burren + * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name(s) of the author(s) nor the names of other contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name(s) of the author(s) nor the names of other contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. * * * This is an original implementation of the DES and the crypt(3) interfaces @@ -50,12 +50,34 @@ */ #include -#include #include -#ifdef DEBUG -# include -#endif +static unsigned int md_endian = 0x01020304; + +unsigned int +xntohl(unsigned int x) +{ + if ( *((char *)&md_endian) == 0x01 ) + return(x); + else + return( ((x & 0x000000ffU) << 24) | + ((x & 0x0000ff00U) << 8) | + ((x & 0x00ff0000U) >> 8) | + ((x & 0xff000000U) >> 24) ); +} + +unsigned int +xhtonl(unsigned int x) +{ + if ( *((char *)&md_endian) == 0x01 ) + return(x); + else + return( ((x & 0x000000ffU) << 24) | + ((x & 0x0000ff00U) << 8) | + ((x & 0x00ff0000U) >> 8) | + ((x & 0xff000000U) >> 24) ); +} + #define _PASSWORD_EFMT1 '_' static unsigned char IP[64] = { @@ -182,8 +204,7 @@ static unsigned char ascii64[] = /* 0123456789012345678901234567890123456789012345678901234567890123 */ static __inline int -ascii_to_bin(ch) - char ch; +ascii_to_bin(int ch) { if (ch > 'z') return(0); @@ -201,7 +222,7 @@ ascii_to_bin(ch) } static void -des_init() +des_init(void) { int i, j, b, k, inbit, obit; unsigned int *p, *il, *ir, *fl, *fr; @@ -236,7 +257,7 @@ des_init() * initialise the inverted key permutation. */ for (i = 0; i < 64; i++) { - init_perm[final_perm[i] = IP[i] - 1] = i; + init_perm[final_perm[i] = IP[i] - 1] = (unsigned char) i; inv_key_perm[i] = 255; } @@ -245,7 +266,7 @@ des_init() * compression permutation. */ for (i = 0; i < 56; i++) { - inv_key_perm[key_perm[i] - 1] = i; + inv_key_perm[key_perm[i] - 1] = (unsigned char) i; inv_comp_perm[i] = 255; } @@ -253,7 +274,7 @@ des_init() * Invert the key compression permutation. */ for (i = 0; i < 48; i++) { - inv_comp_perm[comp_perm[i] - 1] = i; + inv_comp_perm[comp_perm[i] - 1] = (unsigned char) i; } /* @@ -315,7 +336,7 @@ des_init() * handling the output of the S-box arrays setup above. */ for (i = 0; i < 32; i++) - un_pbox[pbox[i] - 1] = i; + un_pbox[pbox[i] - 1] = (unsigned char) i; for (b = 0; b < 4; b++) for (i = 0; i < 256; i++) { @@ -330,8 +351,7 @@ des_init() } static void -setup_salt(salt) - int salt; +setup_salt(int salt) { unsigned int obit, saltbit; int i; @@ -352,8 +372,7 @@ setup_salt(salt) } static int -des_setkey(key) - const char *key; +des_setkey(const char *key) { unsigned int k0, k1, rawkey0, rawkey1; int shifts, round; @@ -361,8 +380,8 @@ des_setkey(key) if (!des_initialised) des_init(); - rawkey0 = ntohl(*(unsigned int *) key); - rawkey1 = ntohl(*(unsigned int *) (key + 4)); + rawkey0 = xntohl(*(unsigned int *) key); + rawkey1 = xntohl(*(unsigned int *) (key + 4)); if ((rawkey0 | rawkey1) && rawkey0 == old_rawkey0 @@ -433,15 +452,14 @@ des_setkey(key) } static int -do_des(l_in, r_in, l_out, r_out, count) - unsigned int l_in, r_in, *l_out, *r_out; - int count; +do_des(unsigned int l_in, unsigned int r_in, unsigned int *l_out, + unsigned int *r_out, int count) { /* * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. */ unsigned int l, r, *kl, *kr, *kl1, *kr1; - unsigned int f, r48l, r48r; + unsigned int f = 0, r48l, r48r; int round; if (count == 0) { @@ -551,11 +569,7 @@ do_des(l_in, r_in, l_out, r_out, count) } static int -des_cipher(in, out, salt, count) - const char *in; - char *out; - int salt; - int count; +des_cipher(const char *in, char *out, int salt, int count) { unsigned int l_out, r_out, rawl, rawr; unsigned int x[2]; @@ -567,20 +581,18 @@ des_cipher(in, out, salt, count) setup_salt(salt); memcpy(x, in, sizeof x); - rawl = ntohl(x[0]); - rawr = ntohl(x[1]); + rawl = xntohl(x[0]); + rawr = xntohl(x[1]); retval = do_des(rawl, rawr, &l_out, &r_out, count); - x[0] = htonl(l_out); - x[1] = htonl(r_out); + x[0] = xhtonl(l_out); + x[1] = xhtonl(r_out); memcpy(out, x, sizeof x); return(retval); } char * -xcrypt(key, setting) - const char *key; - const char *setting; +xcrypt(const char *key, const char *setting) { int i; unsigned int count, salt, l, r0, r1, keybuf[2]; @@ -599,7 +611,7 @@ xcrypt(key, setting) if ((*q++ = *key << 1)) key++; } - if (des_setkey((unsigned char *) keybuf)) + if (des_setkey((const char *) keybuf)) return(NULL); if (*setting == _PASSWORD_EFMT1) { @@ -618,7 +630,7 @@ xcrypt(key, setting) /* * Encrypt the key with itself. */ - if (des_cipher((unsigned char*)keybuf, (unsigned char*)keybuf, 0, 1)) + if (des_cipher((const char*)keybuf, (char*)keybuf, 0, 1)) return(NULL); /* * And XOR with the next 8 characters of the key. @@ -628,7 +640,7 @@ xcrypt(key, setting) *key) *q++ ^= *key++ << 1; - if (des_setkey((unsigned char *) keybuf)) + if (des_setkey((const char *) keybuf)) return(NULL); } strncpy((char *)output, setting, 9);