--- deliantra/server/common/utils.C 2008/05/04 14:12:37 1.77 +++ deliantra/server/common/utils.C 2009/11/03 23:18:23 1.92 @@ -5,24 +5,25 @@ * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team * Copyright (©) 1992,2007 Frank Tore Johansen * - * Deliantra is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * Deliantra is free software: you can redistribute it and/or modify it under + * the terms of the Affero GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * You should have received a copy of the Affero GNU General Public License + * and the GNU General Public License along with this program. If not, see + * . * * The authors can be reached via e-mail to */ /* - * General convenience functions for crossfire. + * General convenience functions for deliantra. */ #include @@ -34,6 +35,10 @@ #include #include +#include + +#include +#include #include @@ -50,7 +55,7 @@ state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U; for (int i = 11; --i; ) - operator ()(); + next (); } uint32_t @@ -64,19 +69,24 @@ return state [0] ^ state [1] ^ state [2] ^ state [3]; } +template uint32_t -tausworthe_random_generator::get_range (uint32_t num) +random_number_generator::get_range (uint32_t num) { - return (next () * (uint64_t)num) >> 32U; + return (this->next () * (uint64_t)num) >> 32U; } // return a number within (min .. max) +template int -tausworthe_random_generator::get_range (int r_min, int r_max) +random_number_generator::get_range (int r_min, int r_max) { return r_min + get_range (max (r_max - r_min + 1, 0)); } +template struct random_number_generator; +template struct random_number_generator; + /* * The random functions here take luck into account when rolling random * dice or numbers. This function has less of an impact the larger the @@ -95,13 +105,9 @@ int random_roll (int r_min, int r_max, const object *op, int goodbad) { - int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ + r_max = max (r_min, r_max); - if (r_max < r_min) - { - LOG (llevError | logBacktrace, "Calling random_roll with min=%d max=%d\n", r_min, r_max); - return r_min; - } + int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ if (op->type == PLAYER) { @@ -121,17 +127,11 @@ * for exp loss calculations for players changing religions. */ sint64 -random_roll64 (sint64 min, sint64 max, const object *op, int goodbad) +random_roll64 (sint64 r_min, sint64 r_max, const object *op, int goodbad) { - sint64 omin = min; - sint64 diff = max - min + 1; - int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ - - if (diff < 0) - { - LOG (llevError | logBacktrace, "Calling random_roll64 with min=%" PRId64 " max=%" PRId64 "\n", min, max); - return (min); /* avoids a float exception */ - } + sint64 omin = r_min; + sint64 range = max (0, r_max - r_min + 1); + int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ /* * Make a call to get two 32 bit unsigned random numbers, and just to @@ -140,24 +140,24 @@ sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); if (op->type != PLAYER) - return ((ran % diff) + min); + return ((ran % range) + r_min); int luck = op->stats.luck; - if (rndm (base) < MIN (10, abs (luck))) + if (rndm (base) < min (10, abs (luck))) { /* we have a winner */ ((luck > 0) ? (luck = 1) : (luck = -1)); - diff -= luck; - if (diff < 1) + range -= luck; + if (range < 1) return (omin); /*check again */ - ((goodbad) ? (min += luck) : (diff)); + ((goodbad) ? (r_min += luck) : (range)); - return (MAX (omin, MIN (max, (ran % diff) + min))); + return (max (omin, min (r_max, (ran % range) + r_min))); } - return ran % diff + min; + return ran % range + r_min; } /* @@ -209,9 +209,9 @@ /* convert materialname to materialtype_t */ materialtype_t * -name_to_material (const shstr &name) +name_to_material (const shstr_cmp name) { - for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next) + for (materialtype_t *mt = materialt; mt; mt = mt->next) if (name == mt->name) return mt; @@ -227,10 +227,10 @@ materialtype_t *mt; int j; - if (op->materialname == NULL) + if (!op->materialname) return; - if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) + if (op->materialname != change->materialname) return; if (!op->is_armor ()) @@ -260,14 +260,16 @@ { materialtype_t *mt, *lmt; - if (op->materialname != NULL) + if (!op->materialname) return; - if (nmt == NULL) + if (nmt) + lmt = nmt; + else { - lmt = NULL; + lmt = 0; - for (mt = materialt; mt && mt->next; mt = mt->next) + for (mt = materialt; mt; mt = mt->next) if (op->materials & mt->material && rndm (1, 100) <= mt->chance && difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) { @@ -276,10 +278,8 @@ break; } } - else - lmt = nmt; - if (lmt != NULL) + if (lmt) { if (op->stats.dam && op->is_weapon ()) { @@ -412,43 +412,26 @@ result[resultlen] = '\0'; } -/** - * Taking a string as an argument, mutate it into a string that looks like a list. - * a 'list' for the purposes here, is a string of items, seperated by commas, except - * for the last entry, which has an 'and' before it, and a full stop (period) after it. - * This function will also strip all trailing non alphanumeric characters. - * It does not insert an oxford comma. +/******************************************************************************/ + +/* Checks a player-provided string which will become the msg property of + * an object for dangerous input. */ -void -make_list_like (char *input) +bool +msg_is_safe (const char *msg) { - char *p, tmp[MAX_BUF]; - int i; + bool safe = true; - if (!input || strlen (input) > MAX_BUF - 5) - return; - /* bad stuff would happen if we continued here, the -5 is to make space for ' and ' */ + /* Trying to cheat by getting data into the object */ + if (!strncmp (msg, "endmsg", sizeof ("endmsg") - 1) + || strstr (msg, "\nendmsg")) + safe = false; - strncpy (tmp, input, MAX_BUF - 5); - /*trim all trailing commas, spaces etc. */ - for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) - tmp[i] = '\0'; - - strcat (tmp, "."); - - p = strrchr (tmp, ','); - if (p) - { - *p = '\0'; - strcpy (input, tmp); - p++; - strcat (input, " and"); - strcat (input, p); - } - else - strcpy (input, tmp); + /* Trying to make the object talk, and potentially access arbitrary code */ + if (object::msg_has_dialogue (msg)) + safe = false; - return; + return safe; } ///////////////////////////////////////////////////////////////////////////// @@ -458,10 +441,39 @@ { if (!fork ()) { - signal (SIGABRT, SIG_DFL); + signal (SIGINT , SIG_IGN); + signal (SIGTERM, SIG_IGN); + signal (SIGABRT, SIG_IGN); + + signal (SIGSEGV, SIG_DFL); + signal (SIGBUS , SIG_DFL); + signal (SIGILL , SIG_DFL); + signal (SIGTRAP, SIG_DFL); + // try to put corefiles into a subdirectory, if existing, to allow // an administrator to reduce the I/O load. chdir ("cores"); + + // try to detach us from as many external dependencies as possible + // as coredumping can take time by closing all fd's. + { + struct rlimit lim; + + if (getrlimit (RLIMIT_NOFILE, &lim)) + lim.rlim_cur = 1024; + + for (int i = 0; i < lim.rlim_cur; ++i) + close (i); + } + + { + sigset_t empty; + sigemptyset (&empty); + sigprocmask (SIG_SETMASK, &empty, 0); + } + + // try to coredump with SIGTRAP + kill (getpid (), SIGTRAP); abort (); } @@ -534,40 +546,49 @@ /******************************************************************************/ -void assign (char *dst, const char *src, int maxlen) +int +assign (char *dst, const char *src, int maxsize) { if (!src) src = ""; int len = strlen (src); - if (len >= maxlen - 1) + if (len >= maxsize) { - if (maxlen <= 4) + if (maxsize <= 4) { - memset (dst, '.', maxlen - 1); - dst [maxlen - 1] = 0; + memset (dst, '.', maxsize - 2); + dst [maxsize - 1] = 0; } else { - memcpy (dst, src, maxlen - 4); - memcpy (dst + maxlen - 4, "...", 4); + memcpy (dst, src, maxsize - 4); + memcpy (dst + maxsize - 4, "...", 4); } + + len = maxsize; } else - memcpy (dst, src, len + 1); + memcpy (dst, src, ++len); + + return len; } -const char * -format (const char *format, ...) +char * +vformat (const char *format, va_list ap) { - static dynbuf_text buf; - - buf.clear (); + static dynbuf_text buf; buf.clear (); + buf.vprintf (format, ap); + return buf; +} +char * +format (const char *format, ...) +{ va_list ap; va_start (ap, format); - buf.vprintf (format, ap); + char *buf = vformat (format, ap); va_end (ap); return buf;