diff -Naur pgsql.alt/contrib/randomstr/Makefile pgsql.dev/contrib/randomstr/Makefile --- pgsql.alt/contrib/randomstr/Makefile Thu Jan 1 00:00:00 1970 +++ pgsql.dev/contrib/randomstr/Makefile Tue Aug 7 20:32:50 2001 @@ -0,0 +1,41 @@ +subdir = contrib/randomstr +top_builddir = ../.. +include $(top_builddir)/src/Makefile.global + +# override libdir to install shlib in contrib not main directory +libdir := $(libdir)/contrib + +# shared library parameters +NAME= randomstr +SO_MAJOR_VERSION= 0 +SO_MINOR_VERSION= 1 + +override CPPFLAGS := -I$(srcdir)/src/include $(CPPFLAGS) + +OBJS= randomstr.o + +all: all-lib $(NAME).sql + +# Shared library stuff +include $(top_srcdir)/src/Makefile.shlib + + +$(NAME).sql: $(NAME).sql.in + sed -e 's:MODULE_PATHNAME:$(libdir)/$(shlib):g' < $< > $@ + +install: all installdirs install-lib + +installdirs: + $(mkinstalldirs) $(DESTDIR)$(libdir) + +uninstall: uninstall-lib + +clean distclean maintainer-clean: clean-lib + rm -f $(OBJS) $(NAME).sql + +depend dep: + $(CC) -MM $(CFLAGS) *.c >depend + +ifeq (depend,$(wildcard depend)) +include depend +endif diff -Naur pgsql.alt/contrib/randomstr/README.randomstr pgsql.dev/contrib/randomstr/README.randomstr --- pgsql.alt/contrib/randomstr/README.randomstr Thu Jan 1 00:00:00 1970 +++ pgsql.dev/contrib/randomstr/README.randomstr Wed Aug 8 10:34:30 2001 @@ -0,0 +1,102 @@ +/* + * randomstr + * + * Functions for generating "good" random strings for IV's, session keys, etc + * + * Copyright (c) Joseph Conway , 2001; + * + * randomstr_hex() + * ------------- + * Generates string with a requested number of random bytes + * from /dev/urandom. The result is returned as hexidecimal (text) + * + * randomstr_bytea() + * ------------- + * Generates string with a requested number of random bytes + * from /dev/urandom. The result is returned as bytea + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose, without fee, and without a written agreement + * is hereby granted, provided that the above copyright notice and this + * paragraph and the following two paragraphs appear in all copies. + * + * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING + * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS + * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + */ + + +Version 0.1 (8 August, 2001): + Functions for generating "good" random strings for IV's, session keys, etc + Tested under Linux (Red Hat 6.2 and 7.0) and PostgreSQL 7.2devel + +Release Notes: + + Version 0.1 + - initial release + +Installation: + Place these files in a directory called 'randomstr' under 'contrib' in the PostgreSQL source tree. Then run: + + make + make install + + You can use randomstr.sql to create the functions in your database of choice, e.g. + + psql -U postgres template1 < randomstr.sql + + installs following functions into database template1: + + randomstr_hex(int) + randomstr_bytea(int) + +Documentation +================================================================== +Name + +randomstr_hex(int) - generates random string from /dev/urandom +randomstr_bytea(int) + +Synopsis + +randomstr_hex(int binlen) +randomstr_bytea(int binlen) + +Inputs + + binlen - requested string length, in bytes + +Outputs + + randomstr_hex result is a random string of binlen bytes converted to + hex; thus the actual output length is twice the requested length + randomstr_bytea result is a random string of binlen bytes converted + to bytea; thus the actual output length varies depending on the + number of escaped bytes + +Example usage + +test=# select randomstr_hex(3); + randomstr_hex +--------------- + 16b084 +(1 row) + +test=# select randomstr_bytea(8); + randomstr_bytea +------------------------- + w\365\342\361@\331C\302 +(1 row) + +================================================================== +-- Joe Conway + diff -Naur pgsql.alt/contrib/randomstr/randomstr.c pgsql.dev/contrib/randomstr/randomstr.c --- pgsql.alt/contrib/randomstr/randomstr.c Thu Jan 1 00:00:00 1970 +++ pgsql.dev/contrib/randomstr/randomstr.c Wed Aug 8 10:00:04 2001 @@ -0,0 +1,183 @@ +/* + * randomstr.c + * + * Functions for generating "good" random strings for IV's, session keys, etc + * + * Copyright (c) Joseph Conway , 2001; + * + * randomstr() + * ------------- + * Generates string with a requested number of random bytes + * from /dev/urandom. The result is returned as binary (bytea) + * or hexidecimal (text) + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose, without fee, and without a written agreement + * is hereby granted, provided that the above copyright notice and this + * paragraph and the following two paragraphs appear in all copies. + * + * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING + * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS + * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + */ + +#include "randomstr.h" + +PG_FUNCTION_INFO_V1(randomstr_hex); +Datum +randomstr_hex(PG_FUNCTION_ARGS) +{ + int reqlen; + unsigned char *randomstr; + unsigned char *randomstr_hex; + text *randomstr_text; + + reqlen = PG_GETARG_INT32(0); + if (!(reqlen > 0)) + elog(ERROR, "randomstr: Requested output length must be > 0"); + + randomstr = gen_rand_str(reqlen); + randomstr_hex = bin2hex(randomstr, reqlen); + randomstr_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(randomstr_hex))); + + PG_RETURN_TEXT_P(randomstr_text); +} + +PG_FUNCTION_INFO_V1(randomstr_bytea); +Datum +randomstr_bytea(PG_FUNCTION_ARGS) +{ + int reqlen; + unsigned char *randomstr; + unsigned char *randomstr_esc; + bytea *randomstr_bytea; + + reqlen = PG_GETARG_INT32(0); + if (!(reqlen > 0)) + elog(ERROR, "randomstr: Requested output length must be > 0"); + + randomstr = gen_rand_str(reqlen); + randomstr_esc = bytea_esc(randomstr, reqlen); + randomstr_bytea = DatumGetByteaP(DirectFunctionCall1(byteain, CStringGetDatum(randomstr_esc))); + + PG_RETURN_BYTEA_P(randomstr_bytea); +} + + +/* + * gen_rand_str: used to produce a random string of any desired + * length for use as an IV or a session key + */ +unsigned char * +gen_rand_str(int rand_str_len) +{ + FILE *ptr; + size_t total_bytes_read = 0; + size_t bytes_read = 0; + unsigned char *rand_str; + + if (rand_str_len < 1) + elog(ERROR, "gen_rand_str: function called with requested random string length of zero"); + + ptr = fopen("/dev/urandom", "r"); + if (ptr == NULL) + elog(NOTICE, "/dev/urandom is not available"); + + rand_str = palloc(rand_str_len); + memset(rand_str, '\0', rand_str_len); + + while (total_bytes_read < rand_str_len) + { + bytes_read = fread(&rand_str[total_bytes_read], sizeof(char), rand_str_len - total_bytes_read, ptr); + if (bytes_read < 0) + break; + total_bytes_read += bytes_read; + } + fclose(ptr); + + return rand_str; +} + +unsigned char * +bin2hex(unsigned char *binstr, size_t binstrlen) +{ + size_t i; + int asc_ptr = 0; + unsigned char ms_nib; + unsigned char ls_nib; + unsigned char *hexstr = NULL; + size_t hexstrlen; + + hexstrlen = binstrlen * 2 * sizeof(char) + 1; + hexstr = palloc(hexstrlen); + if (hexstr == NULL) + elog(ERROR, "bin2hex: failed to allocate memory"); + + memset(hexstr, '\0', hexstrlen); + + for(i = 0; i < binstrlen; i++) + { + ms_nib = (unsigned char) ((binstr[i] >> 4) & 0x0f); + ls_nib = (unsigned char) (binstr[i] & 0x0f); + hexstr[asc_ptr++] = (unsigned char) (ms_nib > 9 ? ms_nib + 'a' - 10: ms_nib + '0'); + hexstr[asc_ptr++] = (unsigned char) (ls_nib > 9 ? ls_nib + 'a' - 10: ls_nib + '0'); + } + return hexstr; +} + +unsigned char * +bytea_esc(unsigned char *binstr, size_t binstrlen) +{ + size_t i; + unsigned char *vp; + size_t len = 0; + unsigned char *rp; + unsigned char *result; + + vp = binstr; + for (i = 0; i < binstrlen; i++, vp++) + { + if (*vp == '\0') + len += 4; + else if (*vp == '\\') + len += 2; + else + len += 1; + } + + result = rp = (unsigned char *) palloc(len + 1); + if (rp == NULL) + elog(ERROR, "bytea_esc: failed to allocate memory"); + + memset(rp, '\0', len + 1); + + vp = binstr; + for (i = 0; i < binstrlen; i++, vp++) + { + if (*vp == '\0') + { + *rp++ = '\\'; + *rp++ = '0'; + *rp++ = '0'; + *rp++ = '0'; + } + else if (*vp == '\\') + { + *rp++ = '\\'; + *rp++ = '\\'; + } + else + *rp++ = *vp; + } + + return result; +} diff -Naur pgsql.alt/contrib/randomstr/randomstr.h pgsql.dev/contrib/randomstr/randomstr.h --- pgsql.alt/contrib/randomstr/randomstr.h Thu Jan 1 00:00:00 1970 +++ pgsql.dev/contrib/randomstr/randomstr.h Wed Aug 8 09:24:49 2001 @@ -0,0 +1,60 @@ +/* + * randomstr.h + * + * Functions for generating "good" random strings for IV's, session keys, etc + * + * Copyright (c) Joseph Conway , 2001; + * + * randomstr() + * ------------- + * Generates string with a requested number of random bytes + * from /dev/urandom. The result is returned as binary (bytea) + * or hexidecimal (text) + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose, without fee, and without a written agreement + * is hereby granted, provided that the above copyright notice and this + * paragraph and the following two paragraphs appear in all copies. + * + * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING + * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS + * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + */ + +#ifndef RANDOMSTR_H +#define RANDOMSTR_H + +#include +#include +#include + +#include "postgres.h" +#include "fmgr.h" +#include "utils/builtins.h" + + + +/* + * External declarations + */ +extern Datum randomstr_hex(PG_FUNCTION_ARGS); +extern Datum randomstr_bytea(PG_FUNCTION_ARGS); + +/* + * internal + */ +unsigned char *gen_rand_str(int rand_str_len); +unsigned char *bin2hex(unsigned char *binstr, size_t binstrlen); +unsigned char *bytea_esc(unsigned char *binstr, size_t binstrlen); + + +#endif /* RANDOMSTR_H */ diff -Naur pgsql.alt/contrib/randomstr/randomstr.sql.in pgsql.dev/contrib/randomstr/randomstr.sql.in --- pgsql.alt/contrib/randomstr/randomstr.sql.in Thu Jan 1 00:00:00 1970 +++ pgsql.dev/contrib/randomstr/randomstr.sql.in Wed Aug 8 09:22:12 2001 @@ -0,0 +1,5 @@ +CREATE FUNCTION randomstr_hex (int) RETURNS text + AS 'MODULE_PATHNAME','randomstr_hex' LANGUAGE 'c' with (isstrict); + +CREATE FUNCTION randomstr_bytea (int) RETURNS bytea + AS 'MODULE_PATHNAME','randomstr_bytea' LANGUAGE 'c' with (isstrict);