├── doc ├── .gitignore ├── frag-config-man.md ├── frag-usage-man.md ├── filter.py └── Makefile ├── win32 ├── MSG00001.bin ├── eventmsg.rc ├── eventmsg.mc ├── Makefile ├── win32support.h └── win32ver.rc ├── lib ├── autogen.sh ├── .gitattributes ├── test │ ├── ssl │ │ ├── ca1_client1.crt.sha1 │ │ ├── ca1_root.crt.sha1 │ │ ├── ca1_server1.crt.sha1 │ │ ├── ca2_client2.crt.sha1 │ │ ├── ca2_root.crt.sha1 │ │ ├── ca2_server2.crt.sha1 │ │ ├── ca1_complex1.crt.sha1 │ │ ├── ca2_complex2.crt.sha1 │ │ ├── ca1_root.crt.sha256 │ │ ├── ca2_root.crt.sha256 │ │ ├── ca1_client1.crt.sha256 │ │ ├── ca1_complex1.crt.sha256 │ │ ├── ca1_server1.crt.sha256 │ │ ├── ca2_client2.crt.sha256 │ │ ├── ca2_complex2.crt.sha256 │ │ ├── ca2_server2.crt.sha256 │ │ ├── ca1_client1.key │ │ ├── ca1_root.key │ │ ├── ca1_complex1.key │ │ ├── ca1_server1.key │ │ ├── ca1_client1.crt │ │ ├── ca1_server1.crt │ │ ├── ca1_root.crt │ │ ├── ca1_complex1.crt │ │ ├── ca2_root.crt │ │ ├── ca2_server2.crt │ │ ├── ca2_client2.crt │ │ ├── ca2_client2.key │ │ ├── ca2_root.key │ │ ├── ca2_server2.key │ │ └── ca2_complex2.crt │ ├── test_cfparser.ini │ ├── coverage.mk │ ├── attregex │ │ ├── run.sh │ │ ├── data │ │ │ ├── leftassoc.dat │ │ │ ├── rightassoc.dat │ │ │ ├── forcedassoc.dat │ │ │ └── nullsubexpr.dat │ │ └── Makefile │ ├── force_compat.sed │ ├── coverage.sh │ ├── try_locales.sh │ ├── awk_test.sh │ ├── test_netdb.c │ ├── test_time.c │ ├── test_ctype.c │ ├── test_strpool.c │ ├── test_mdict.c │ ├── test_fileutil.c │ ├── compile.c │ ├── test_socket.c │ ├── test_common.c │ ├── test_hashing.c │ └── test_common.h ├── doc │ └── setup.dox ├── README ├── build.mk.in ├── mk │ ├── safe-headers.sed │ ├── Makefile.am │ ├── libusual.pc.in │ ├── amext-cxx.mk │ ├── amext-msvc.mk │ ├── temos │ │ ├── index.txt │ │ ├── src │ │ │ ├── libusual1.temo │ │ │ ├── libusual5.temo │ │ │ ├── antimake1.temo │ │ │ ├── antimake4.temo │ │ │ ├── libusual4.temo │ │ │ ├── libusual2.temo │ │ │ └── libusual3.temo │ │ ├── Makefile │ │ ├── expected │ │ │ ├── libusual1.txt │ │ │ └── libusual5.txt │ │ └── libtemo.sh │ └── amext-libusual.mk ├── m4 │ └── antimake.m4 ├── build_msvc.mk ├── usual │ ├── hashing │ │ ├── lookup3.h │ │ ├── spooky.h │ │ ├── crc32.h │ │ ├── siphash.h │ │ ├── memhash.h │ │ ├── memhash.c │ │ ├── lookup3.c │ │ └── xxhash.h │ ├── mbuf.c │ ├── mempool.h │ ├── daemon.h │ ├── crypto │ │ ├── entropy.h │ │ ├── csrandom.h │ │ ├── md5.h │ │ ├── sha1.h │ │ ├── hmac.h │ │ ├── keccak_prng.c │ │ ├── keccak_prng.h │ │ ├── chacha.h │ │ └── digest.c │ ├── wchar.h │ ├── dlfcn.c │ ├── dlfcn.h │ ├── pgutil.h │ ├── misc.h │ ├── safeio.h │ ├── cxextra.h │ ├── fnmatch.h │ ├── base.c │ ├── fileutil.h │ ├── psrandom.h │ ├── strpool.h │ ├── mempool.c │ ├── pgutil_kwlookup.g │ ├── utf8.h │ ├── err.h │ ├── netdb.h │ └── pthread.h ├── .gitignore ├── .appveyor.yml ├── COPYRIGHT ├── configure.ac ├── find_modules.sh └── config.mak.in ├── autogen.sh ├── etc ├── userlist.txt ├── optscan.sh ├── pgbouncer.socket ├── pgbouncer-minimal.ini ├── mkauth.py ├── import-common.sh ├── example.debian.init.sh └── pgbouncer.service ├── test ├── run-conntest.sh ├── ssl │ ├── Makefile │ ├── newsite.sh │ ├── test.ini │ ├── lib.sh │ └── newca.sh ├── userlist.txt ├── ctest6000.ini ├── ctest7000.ini ├── Makefile ├── conntest.sh ├── hba_test.rules ├── README.md ├── hba_test.eval └── test.ini ├── bpf ├── start_tc_test.sh ├── stop_tc_test.sh ├── mirror_tcp.bpf.c ├── mp_common.h ├── LICENSE ├── mirror_udp.bpf.c ├── Makefile └── mp_bouncer.bpf.h ├── include ├── mpbouncer.h ├── common │ ├── base64.h │ ├── saslprep.h │ ├── postgres_compat.h │ └── unicode_norm.h ├── varcache.h ├── hba.h ├── takeover.h ├── janitor.h ├── client.h ├── loader.h ├── pooler.h ├── stats.h ├── server.h ├── pam.h ├── admin.h ├── system.h └── dnslookup.h ├── .gitignore ├── COPYRIGHT ├── AUTHORS └── config.mak.in /doc/.gitignore: -------------------------------------------------------------------------------- 1 | pgbouncer.1 2 | pgbouncer.5 3 | -------------------------------------------------------------------------------- /win32/MSG00001.bin: -------------------------------------------------------------------------------- 1 | %1 2 | -------------------------------------------------------------------------------- /lib/autogen.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | ./mk/std-autogen.sh . 4 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | ./lib/mk/std-autogen.sh ./lib 4 | -------------------------------------------------------------------------------- /win32/eventmsg.rc: -------------------------------------------------------------------------------- 1 | LANGUAGE 0x9,0x1 2 | 1 11 "MSG00001.bin" 3 | -------------------------------------------------------------------------------- /lib/.gitattributes: -------------------------------------------------------------------------------- 1 | mk/temos/expected/*.txt whitespace=-blank-at-eof 2 | -------------------------------------------------------------------------------- /lib/test/ssl/ca1_client1.crt.sha1: -------------------------------------------------------------------------------- 1 | 0d473ef9adc6d7465826deb1d895ded592a1d91b -------------------------------------------------------------------------------- /lib/test/ssl/ca1_root.crt.sha1: -------------------------------------------------------------------------------- 1 | 1d69bf1326ac6059953b6f2a835365e2a65018ab -------------------------------------------------------------------------------- /lib/test/ssl/ca1_server1.crt.sha1: -------------------------------------------------------------------------------- 1 | b14bca21d635dfb2ad99a9c80b7867131a3243a2 -------------------------------------------------------------------------------- /lib/test/ssl/ca2_client2.crt.sha1: -------------------------------------------------------------------------------- 1 | 57324f22f6d4b8dd0b15139b5185a14c16611bad -------------------------------------------------------------------------------- /lib/test/ssl/ca2_root.crt.sha1: -------------------------------------------------------------------------------- 1 | 3639ed69198689c73f7007c3dfd28997d08b72df -------------------------------------------------------------------------------- /lib/test/ssl/ca2_server2.crt.sha1: -------------------------------------------------------------------------------- 1 | c1ab5cfc782fff7419ccb85dffe4aba850b491b1 -------------------------------------------------------------------------------- /lib/test/ssl/ca1_complex1.crt.sha1: -------------------------------------------------------------------------------- 1 | 3f15e85609cb64f56316033857f7bbdfd20b06ea -------------------------------------------------------------------------------- /lib/test/ssl/ca2_complex2.crt.sha1: -------------------------------------------------------------------------------- 1 | a14e38a48e62fb389ef048b72e9d5d3e875965a5 -------------------------------------------------------------------------------- /etc/userlist.txt: -------------------------------------------------------------------------------- 1 | "marko" "asdasd" 2 | "postgres" "asdasd" 3 | "pgbouncer" "fake" 4 | -------------------------------------------------------------------------------- /lib/doc/setup.dox: -------------------------------------------------------------------------------- 1 | /** 2 | * @page setup How to setup libusual 3 | * 4 | * Foo 5 | */ 6 | -------------------------------------------------------------------------------- /lib/README: -------------------------------------------------------------------------------- 1 | = libusual = 2 | 3 | Collection of various code useful for writing server code. 4 | -------------------------------------------------------------------------------- /lib/test/ssl/ca1_root.crt.sha256: -------------------------------------------------------------------------------- 1 | 454ddb4e73e166e0b77ddbafd55ced132a2378cacc3a6acb5098e9d6ffe53bab -------------------------------------------------------------------------------- /lib/test/ssl/ca2_root.crt.sha256: -------------------------------------------------------------------------------- 1 | 6c00191c102c3dad976569af525bb1f5e48f2e501e0ddb0e5265d7aa42ed7a9e -------------------------------------------------------------------------------- /win32/eventmsg.mc: -------------------------------------------------------------------------------- 1 | MessageId=0 2 | SymbolicName=MSG_PGBOUNCER 3 | Language=English 4 | %1 5 | . 6 | -------------------------------------------------------------------------------- /lib/build.mk.in: -------------------------------------------------------------------------------- 1 | include @abs_top_builddir@/config.mak 2 | include $(abs_top_srcdir)/mk/antimake.mk 3 | -------------------------------------------------------------------------------- /lib/test/ssl/ca1_client1.crt.sha256: -------------------------------------------------------------------------------- 1 | 5ae934f6bf06028d9129aa7938700b2a71ad0f55cd654349172b1327caf6e1fc -------------------------------------------------------------------------------- /lib/test/ssl/ca1_complex1.crt.sha256: -------------------------------------------------------------------------------- 1 | 8fad1912e13741649f67246bef3d10dacf0480d3d9d9ddb9a26280e94865c17c -------------------------------------------------------------------------------- /lib/test/ssl/ca1_server1.crt.sha256: -------------------------------------------------------------------------------- 1 | 78658a75ed3f712f00aef5620f1dde6ce0ef8e1db5e13e683abab185abae3fa7 -------------------------------------------------------------------------------- /lib/test/ssl/ca2_client2.crt.sha256: -------------------------------------------------------------------------------- 1 | ec648d90573582945203c082a0679e9870c276c66220ec80f24aacdd757cc6ed -------------------------------------------------------------------------------- /lib/test/ssl/ca2_complex2.crt.sha256: -------------------------------------------------------------------------------- 1 | 2372409ea5b9b24561fae3347f28365c55e6e1fbceb9e0a2565cc64e39381faa -------------------------------------------------------------------------------- /lib/test/ssl/ca2_server2.crt.sha256: -------------------------------------------------------------------------------- 1 | f369be9db3cf821ebf5b6512dc3fcd1181b6bf60d9afce96fbf4f9ed3b8a49af -------------------------------------------------------------------------------- /win32/Makefile: -------------------------------------------------------------------------------- 1 | # create eventmsg.rc + MSG*.bin 2 | eventmsg.rc: eventmsg.mc 3 | mc.exe eventmsg.mc 4 | -------------------------------------------------------------------------------- /win32/win32support.h: -------------------------------------------------------------------------------- 1 | /* redirect main() */ 2 | #define main(a,b) real_main(a,b) 3 | int real_main(int argc, char *argv[]); 4 | -------------------------------------------------------------------------------- /doc/frag-config-man.md: -------------------------------------------------------------------------------- 1 | % PGBOUNCER.INI(5) @PACKAGE_VERSION@ | Databases 2 | 3 | ## Name 4 | 5 | pgbouncer.ini - configuration file for pgbouncer 6 | -------------------------------------------------------------------------------- /doc/frag-usage-man.md: -------------------------------------------------------------------------------- 1 | % PGBOUNCER(1) @PACKAGE_VERSION@ | Databases 2 | 3 | ## Name 4 | 5 | pgbouncer - lightweight connection pooler for PostgreSQL 6 | -------------------------------------------------------------------------------- /lib/mk/safe-headers.sed: -------------------------------------------------------------------------------- 1 | s/HAVE_/USUAL_&/g 2 | s/PACKAGE_/USUAL_&/g 3 | s/LT_OBJ/USUAL_&/g 4 | s/STRERROR_/USUAL_&/g 5 | s/CASSERT/USUAL_&/g 6 | s/WORDS_BIGENDIAN/USUAL_&/g 7 | -------------------------------------------------------------------------------- /lib/test/test_cfparser.ini: -------------------------------------------------------------------------------- 1 | [one] 2 | 3 | str1 = val1 4 | 5 | int = 5 6 | 7 | bool = 1 8 | 9 | [two] 10 | 11 | str2 = val2 12 | 13 | time1 = 1.5 14 | time2 = 2.5 15 | -------------------------------------------------------------------------------- /test/run-conntest.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | createdb conntest 4 | 5 | ../pgbouncer -d ctest6000.ini 6 | ../pgbouncer -d ctest7000.ini 7 | 8 | ./asynctest 9 | 10 | # now run conntest.sh on another console 11 | -------------------------------------------------------------------------------- /test/ssl/Makefile: -------------------------------------------------------------------------------- 1 | EXTRA_DIST = lib.sh newca.sh newsite.sh test.ini test.sh Makefile 2 | 3 | SUBLOC = test/ssl 4 | 5 | include ../../config.mak 6 | include ../../lib/mk/antimake.mk 7 | 8 | check: all 9 | ./test.sh 10 | -------------------------------------------------------------------------------- /lib/test/ssl/ca1_client1.key: -------------------------------------------------------------------------------- 1 | -----BEGIN EC PRIVATE KEY----- 2 | MF8CAQEEGBldG0ssG/XkIMKF+s8zo0lvSMUnLs6MlaAKBggqhkjOPQMBAaE0AzIA 3 | BH+kw0Yhur/cRmUAXhWR/siLKHfr/+sYVya2CCwPKInmRYh0wEAR64vhmHffuEqw 4 | ww== 5 | -----END EC PRIVATE KEY----- 6 | -------------------------------------------------------------------------------- /lib/mk/Makefile.am: -------------------------------------------------------------------------------- 1 | pkgconfig_DATA = libusual.pc 2 | dist_pkgdata_DATA = amext-libusual.mk amext-modes.mk amext-msvc.mk amext-cxx.mk 3 | dist_pkgdata_SCRIPTS = antimake.mk std-autogen.sh 4 | 5 | DISTCLEANFILES = libusual.pc 6 | 7 | EXTRA_DIST = antimake.txt safe-headers.sed 8 | -------------------------------------------------------------------------------- /lib/m4/antimake.m4: -------------------------------------------------------------------------------- 1 | dnl 2 | dnl AMK_INIT: Generate initial makefile 3 | dnl 4 | 5 | AC_DEFUN([AMK_INIT], [ 6 | 7 | # if building separately from srcdir, write top-level makefile 8 | if test "$srcdir" != "."; then 9 | echo "include $srcdir/Makefile" > Makefile 10 | fi 11 | 12 | ]) 13 | -------------------------------------------------------------------------------- /bpf/start_tc_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -u 4 | set -e 5 | sudo --validate 6 | 7 | sudo tc qdisc add dev enp24s0f0 clsact 8 | sudo tc filter add dev enp24s0f0 egress bpf direct-action obj tc_test.bpf.o sec 'classifier/tc_test' 9 | sudo bpftool map pin name mirror_ports /sys/fs/bpf/mirror_ports 10 | -------------------------------------------------------------------------------- /lib/test/ssl/ca1_root.key: -------------------------------------------------------------------------------- 1 | -----BEGIN EC PRIVATE KEY----- 2 | MIGkAgEBBDCJ+2M96c7hAFu13MlMhUzpPXhAca03KEL983b6iHGe6WyUuLY6HHZV 3 | itv7yWdf+36gBwYFK4EEACKhZANiAAR9YXoYyI8dTStzN/jFQCAhXtnXcRwCgIwI 4 | uWleOcHoF7zTF7VlpWruun/fCMd9ryX/d3tzV2m9GEPU8rsKvWSYNvjrkWB60nsb 5 | gnJiiB0rTjp4h4JflBSfT1WsgvEmMfc= 6 | -----END EC PRIVATE KEY----- 7 | -------------------------------------------------------------------------------- /lib/test/ssl/ca1_complex1.key: -------------------------------------------------------------------------------- 1 | -----BEGIN EC PRIVATE KEY----- 2 | MIGkAgEBBDA5fPnRk+k0RduJxnqU9o1iSmrPc/mbf+sw8sJr7BxcnYhZhMBdwaE9 3 | MiGxPIBwlqugBwYFK4EEACKhZANiAAQnY6qXCoi7tT9vg3pLteYHOl6DaJ/EA2cb 4 | 857629rPaDp6i7pCD844xNroqx2tL+AxExc2cWmJ8w3ZIm1MNQnH3BpyGlXkQAmX 5 | V95otU2uefETpKWtbM0RqekeG+ssM7I= 6 | -----END EC PRIVATE KEY----- 7 | -------------------------------------------------------------------------------- /lib/test/ssl/ca1_server1.key: -------------------------------------------------------------------------------- 1 | -----BEGIN EC PRIVATE KEY----- 2 | MIGkAgEBBDCFKsd382QBCCMZJ+M/W3QJbhmbUV7WW9ix+P+TWwIeX091PgSE009z 3 | GKwa+TITk0GgBwYFK4EEACKhZANiAAQQET9BdmaPj8x2sFkb3LDI56+HDsVSqSv/ 4 | iocrxJl7STa5V/ZkGeaMsJRbUSPzS+Eq+3VS2FHT4Rvw3SS0nNclHTOlTVVPWiSW 5 | j3AtJZhm7/qxiL3j8wOTPzsLeAdnGUU= 6 | -----END EC PRIVATE KEY----- 7 | -------------------------------------------------------------------------------- /bpf/stop_tc_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -u 4 | set -e 5 | sudo --validate 6 | 7 | sudo tc filter del dev enp24s0f0 egress 8 | sudo tc qdisc del dev enp24s0f0 clsact 9 | sudo rm -r /sys/fs/bpf/ip || true 10 | sudo rm -r /sys/fs/bpf/tc || true 11 | sudo rm -r /sys/fs/bpf/xdp || true 12 | sudo rm /sys/fs/bpf/mirror_ports || true 13 | -------------------------------------------------------------------------------- /lib/build_msvc.mk: -------------------------------------------------------------------------------- 1 | AM_FEATURES = msvc 2 | 3 | # make it work also when included from test/Makefile 4 | top_srcdir = $(dir $(filter %build.mk, $(MAKEFILE_LIST))) 5 | top_builddir = $(top_srcdir) 6 | abs_top_srcdir := $(abspath $(top_srcdir)) 7 | abs_top_builddir := $(abs_top_srcdir) 8 | 9 | include $(abs_top_srcdir)/mk/antimake.mk 10 | -------------------------------------------------------------------------------- /lib/test/coverage.mk: -------------------------------------------------------------------------------- 1 | AM_FEATURES = libusual 2 | USUAL_DIR = . 3 | 4 | noinst_PROGRAMS = covtest 5 | 6 | covtest_SOURCES := $(wildcard test/test_*.[ch]) test/tinytest.c test/tinytest.h test/tinytest_macros.h 7 | covtest_CPPFLAGS = -Itest -I. -DUSUAL_TEST_CONFIG 8 | covtest_LDFLAGS = 9 | covtest_EMBED_LIBUSUAL = 1 10 | 11 | include build.mk 12 | -------------------------------------------------------------------------------- /lib/usual/hashing/lookup3.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * 4 | * Jenkins' lookup3 non-cryptographic hash. 5 | */ 6 | #ifndef _USUAL_HASHING_LOOKUP3_H_ 7 | #define _USUAL_HASHING_LOOKUP3_H_ 8 | 9 | #include 10 | 11 | /** 12 | * Calculate 64-bit hash over data 13 | */ 14 | uint64_t hash_lookup3(const void *data, size_t len); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /lib/mk/libusual.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=@exec_prefix@ 3 | libdir=@libdir@ 4 | includedir=@includedir@ 5 | datarootdir=@datarootdir@ 6 | pkgdatadir=@pkgdatadir@ 7 | antimake=@pkgdatadir@/antimake.mk 8 | 9 | Name: libusual 10 | Description: Usual utility library for C 11 | Version: @PACKAGE_VERSION@ 12 | Cflags: -I${includedir} 13 | Libs: -L${libdir} -lusual 14 | -------------------------------------------------------------------------------- /include/mpbouncer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | typedef struct PgSocket PgSocket; 6 | 7 | enum socket_type { 8 | CLIENT = 0, 9 | SERVER = 1, 10 | MIRROR = 2 11 | }; 12 | 13 | void add_socket_to_sockmap(PgSocket *, enum socket_type); 14 | void remove_socket_from_sockmap(PgSocket *, enum socket_type); 15 | PgSocket *get_client_link(const PgSocket *socket); 16 | -------------------------------------------------------------------------------- /lib/usual/hashing/spooky.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * 4 | * Jenkins SpookyHash V2 - fast hash for 64-bit CPUs. 5 | */ 6 | #ifndef _USUAL_HASHING_SPOOKY_H_ 7 | #define _USUAL_HASHING_SPOOKY_H_ 8 | 9 | #include 10 | 11 | /** 12 | * Run SpookyHash on data. 13 | */ 14 | void spookyhash(const void *message, size_t length, uint64_t *hash1, uint64_t *hash2); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /etc/optscan.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | # Check if all options in main.c are defined in sample ini and docs 4 | 5 | sources="src/main.c" 6 | targets="doc/config.md etc/pgbouncer.ini" 7 | 8 | status=0 9 | 10 | for opt in `grep CF_ABS "$sources" | sed -r 's/^[^"]*"([^"]*)".*/\1/'`; do 11 | for conf in $targets; do 12 | if ! grep -q "$opt" "$conf"; then 13 | echo "$opt is missing in $conf" 1>&2 14 | status=1 15 | fi 16 | done 17 | done 18 | 19 | exit $status 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /aclocal.m4 2 | /config.guess 3 | /config.log 4 | /config.mak 5 | /config.status 6 | /config.sub 7 | /configure 8 | /install-sh 9 | /pgbouncer 10 | /.objs 11 | /tags 12 | /test/.objs 13 | /test/hba_test 14 | /test/log 15 | /test/pgdata/ 16 | /test/test.log 17 | /test/test.pid 18 | /test/ssl/log 19 | /test/ssl/tmp/ 20 | /test/ssl/pgdata/ 21 | /test/ssl/test.log 22 | /test/ssl/test.pid 23 | /test/ssl/TestCA[1-9]/ 24 | 25 | *.html 26 | *.xml 27 | *.exe 28 | *.gz 29 | *.swp 30 | *.DS_Store 31 | -------------------------------------------------------------------------------- /lib/.gitignore: -------------------------------------------------------------------------------- 1 | configure 2 | usual/config.h* 3 | config.status 4 | config.log 5 | config.mak 6 | obj 7 | test/obj 8 | test/test_config.h 9 | test/regtest_* 10 | test/compile 11 | test/connect 12 | doc/html 13 | *.[oas] 14 | *.gc[odn][aov] 15 | *.la 16 | *.pc 17 | *.log 18 | *.substvars 19 | *.debhelper 20 | *.diff 21 | zconf.* 22 | 23 | ./install-sh 24 | config.guess 25 | config.sub 26 | ltmain.sh 27 | libtool 28 | build.mk 29 | .objs 30 | .libs 31 | .deps 32 | install-sh 33 | tmp 34 | tags 35 | 36 | mk/temos/html 37 | mk/temos/output 38 | -------------------------------------------------------------------------------- /lib/test/attregex/run.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | rxtest=./testregex.libc 4 | rxtest=./testregex.usual 5 | 6 | tests="basic.dat categorize.dat nullsubexpr.dat" 7 | tests="$tests rightassoc.dat" 8 | #tests="$tests leftassoc.dat" 9 | tests="$tests forcedassoc.dat" 10 | tests="$tests repetition.dat" 11 | tests="$tests interpretation.dat" 12 | 13 | for t in $tests; do 14 | printf "%-20s" "$t" 15 | #$rxtest < data/$t | grep -vE '(NOTE|Research)' 16 | $rxtest < data/$t | tail -n +4 | grep -vE 'haskell|mimi|NOTE' 17 | done 18 | 19 | #$rxtest < data/categorize.dat | tail -n +4 20 | -------------------------------------------------------------------------------- /include/common/base64.h: -------------------------------------------------------------------------------- 1 | /* 2 | * base64.h 3 | * Encoding and decoding routines for base64 without whitespace 4 | * support. 5 | * 6 | * Portions Copyright (c) 2001-2020, PostgreSQL Global Development Group 7 | * 8 | * src/include/common/base64.h 9 | */ 10 | #ifndef BASE64_H 11 | #define BASE64_H 12 | 13 | /* base 64 */ 14 | extern int pg_b64_encode(const char *src, int len, char *dst, int dstlen); 15 | extern int pg_b64_decode(const char *src, int len, char *dst, int dstlen); 16 | extern int pg_b64_enc_len(int srclen); 17 | extern int pg_b64_dec_len(int srclen); 18 | 19 | #endif /* BASE64_H */ 20 | -------------------------------------------------------------------------------- /lib/test/force_compat.sed: -------------------------------------------------------------------------------- 1 | /^#define.*FFS/s,.*,/* & */, 2 | /^#define.*FLS/s,.*,/* & */, 3 | /^#define.*STRLCPY/s,.*,/* & */, 4 | /^#define.*STRLCAT/s,.*,/* & */, 5 | /^#define.*BASENAME/s,.*,/* & */, 6 | /^#define.*DIRNAME/s,.*,/* & */, 7 | /^#define.*REGCOMP/s,.*,/* & */, 8 | /^#define.*GETADDRINFO_A/s,.*,/* & */, 9 | /^#define.*INET_NTOP/s,.*,/* & */, 10 | /^#define.*INET_PTON/s,.*,/* & */, 11 | /^#define.*GETOPT/s,.*,/* & */, 12 | /^#define.*CTYPE_ON_CHAR/s,.*,/* & */, 13 | /^#define.*FNMATCH/s,.*,/* & */, 14 | /^#define.*MBSNRTOWCS/s,.*,/* & */, 15 | /^#define.*GETENTROPY/s,.*,/* & */, 16 | /^#define.*ARC4RANDOM/s,.*,/* & */, 17 | -------------------------------------------------------------------------------- /doc/filter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import fileinput 4 | import os 5 | import sys 6 | 7 | for line in fileinput.input(): 8 | # substitute package version 9 | if line.startswith('% '): 10 | line = line.replace('@PACKAGE_VERSION@', os.environ['PACKAGE_VERSION']) 11 | # drop level-1 header 12 | if line.startswith('# '): 13 | continue 14 | # decrease level of all headers by 1 15 | if line.startswith('##'): 16 | line = line.replace('#', '', 1) 17 | # convert level-1 headers to uppercase 18 | if line.startswith('# '): 19 | line = line.upper() 20 | sys.stdout.write(line) 21 | -------------------------------------------------------------------------------- /bpf/mirror_tcp.bpf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "mp_bouncer.bpf.h" 4 | #include "mp_common.h" 5 | 6 | SEC("sk_skb/mp_mirror_tcp") 7 | int32_t _mp_mirror_tcp(__attribute__((unused)) struct __sk_buff *const skb) { 8 | // bpf_printk(""); 9 | // bpf_printk("mirror_tcp"); 10 | // bpf_printk("len: %u", skb->len); 11 | // bpf_printk("local_port: %u", skb->local_port); 12 | // bpf_printk("remote_port: %u", bpf_ntohl(skb->remote_port)); 13 | 14 | // TODO(Matt): In the scenario where we want the first response back to be forwarded to the client, handle that here? 15 | 16 | return SK_DROP; 17 | } 18 | 19 | char LICENSE[] SEC("license") = "Dual MIT/GPL"; 20 | -------------------------------------------------------------------------------- /include/varcache.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | enum VarCacheIdx { 4 | VDateStyle = 0, 5 | VClientEncoding, 6 | VTimeZone, 7 | VStdStr, 8 | VAppName, 9 | NumVars 10 | }; 11 | 12 | typedef struct VarCache VarCache; 13 | 14 | struct VarCache { 15 | struct PStr *var_list[NumVars]; 16 | }; 17 | 18 | bool varcache_set(VarCache *cache, const char *key, const char *value) /* _MUSTCHECK */; 19 | bool varcache_apply(PgSocket *server, PgSocket *client, bool *changes_p) _MUSTCHECK; 20 | void varcache_fill_unset(VarCache *src, PgSocket *dst); 21 | void varcache_clean(VarCache *cache); 22 | void varcache_add_params(PktBuf *pkt, VarCache *vars); 23 | void varcache_deinit(void); 24 | -------------------------------------------------------------------------------- /test/ssl/newsite.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | # new server key + cert under some CA 4 | 5 | test -n "$2" || { 6 | echo "usage: $0 " 7 | exit 1 8 | } 9 | 10 | test -f "$1/ca.key" || { 11 | echo "CA $1 does not exist" 12 | exit 1 13 | } 14 | 15 | days=1024 16 | 17 | . ./lib.sh 18 | 19 | CaName="$1" 20 | DstName="$2" 21 | shift 2 22 | 23 | ser=`cat $CaName/serial` 24 | 25 | pfx=$CaName/sites/${ser}-$DstName 26 | 27 | run openssl ecparam -genkey -name prime256v1 -out $pfx.key 28 | 29 | # cert reqs 30 | run_req -new -key "$pfx.key" -out "$pfx.csr" -- CN="$DstName" "$@" 31 | 32 | # accept certs 33 | run_ca -days $days -policy pol-server -in "$pfx.csr" -out "$pfx.crt" 34 | -------------------------------------------------------------------------------- /lib/test/attregex/data/leftassoc.dat: -------------------------------------------------------------------------------- 1 | NOTE left-assoc:pass-all right-assoc:pass-none : 2002-04-29 2 | 3 | E (a|ab)(c|bcd)(d*) abcd (0,4)(0,1)(1,4)(4,4) 4 | E (a|ab)(bcd|c)(d*) abcd (0,4)(0,1)(1,4)(4,4) 5 | E (ab|a)(c|bcd)(d*) abcd (0,4)(0,1)(1,4)(4,4) 6 | E (ab|a)(bcd|c)(d*) abcd (0,4)(0,1)(1,4)(4,4) 7 | 8 | E (a*)(b|abc)(c*) abc (0,3)(0,0)(0,3)(3,3) 9 | E (a*)(abc|b)(c*) abc (0,3)(0,0)(0,3)(3,3) 10 | E (a*)(b|abc)(c*) abc (0,3)(0,0)(0,3)(3,3) 11 | E (a*)(abc|b)(c*) abc (0,3)(0,0)(0,3)(3,3) 12 | 13 | E (a|ab)(c|bcd)(d|.*) abcd (0,4)(0,1)(1,4)(4,4) 14 | E (a|ab)(bcd|c)(d|.*) abcd (0,4)(0,1)(1,4)(4,4) 15 | E (ab|a)(c|bcd)(d|.*) abcd (0,4)(0,1)(1,4)(4,4) 16 | E (ab|a)(bcd|c)(d|.*) abcd (0,4)(0,1)(1,4)(4,4) 17 | -------------------------------------------------------------------------------- /lib/test/attregex/data/rightassoc.dat: -------------------------------------------------------------------------------- 1 | NOTE left-assoc:pass-none right-assoc:pass-all : 2002-04-29 2 | 3 | E (a|ab)(c|bcd)(d*) abcd (0,4)(0,2)(2,3)(3,4) 4 | E (a|ab)(bcd|c)(d*) abcd (0,4)(0,2)(2,3)(3,4) 5 | E (ab|a)(c|bcd)(d*) abcd (0,4)(0,2)(2,3)(3,4) 6 | E (ab|a)(bcd|c)(d*) abcd (0,4)(0,2)(2,3)(3,4) 7 | 8 | E (a*)(b|abc)(c*) abc (0,3)(0,1)(1,2)(2,3) 9 | E (a*)(abc|b)(c*) abc (0,3)(0,1)(1,2)(2,3) 10 | E (a*)(b|abc)(c*) abc (0,3)(0,1)(1,2)(2,3) 11 | E (a*)(abc|b)(c*) abc (0,3)(0,1)(1,2)(2,3) 12 | 13 | E (a|ab)(c|bcd)(d|.*) abcd (0,4)(0,2)(2,3)(3,4) 14 | E (a|ab)(bcd|c)(d|.*) abcd (0,4)(0,2)(2,3)(3,4) 15 | E (ab|a)(c|bcd)(d|.*) abcd (0,4)(0,2)(2,3)(3,4) 16 | E (ab|a)(bcd|c)(d|.*) abcd (0,4)(0,2)(2,3)(3,4) 17 | -------------------------------------------------------------------------------- /test/ssl/test.ini: -------------------------------------------------------------------------------- 1 | [databases] 2 | p0 = port=6666 host=localhost dbname=p0 user=bouncer pool_size=2 3 | p1 = port=6666 host=localhost dbname=p1 user=bouncer 4 | 5 | [pgbouncer] 6 | logfile = test.log 7 | pidfile = test.pid 8 | 9 | listen_addr = 127.0.0.1 10 | listen_port = 6667 11 | unix_socket_dir = /tmp 12 | 13 | ;auth_type = trust 14 | auth_file = tmp/userlist.txt 15 | 16 | pool_mode = statement 17 | 18 | server_check_delay = 10 19 | max_client_conn = 10 20 | default_pool_size = 5 21 | 22 | server_lifetime = 120 23 | server_idle_timeout = 60 24 | 25 | ;; Accept even old TLS versions so that builds with older OpenSSL can 26 | ;; run the test suite. 27 | client_tls_protocols = all 28 | server_tls_protocols = all 29 | -------------------------------------------------------------------------------- /test/userlist.txt: -------------------------------------------------------------------------------- 1 | "marko" "kama" 2 | "postgres" "asdasd" 3 | ;Commented out line should be ignored. 4 | "pgbouncer" "fake" 5 | "pswcheck" "pgbouncer-check" 6 | "maxedout" "" 7 | 8 | ;the following pairs of passwords are "foo" and "bar" 9 | 10 | "puser1" "foo" 11 | "puser2" "bar" 12 | 13 | "muser1" "md5ab8b744ff66bee42dc47bae34ca17959" 14 | "muser2" "md598455b3585818e23c2653a59f6d84551" 15 | 16 | "scramuser1" "SCRAM-SHA-256$4096:D76gvGUVj9Z4DNiGoabOBg==$RukL0Xo3Ql/2F9FsD7mcQ3GATG2fD3PA71qY1JagGDs=:BhKUwyyivFm7Tq2jDJVXSVRbRDgTWyBilZKgg6DDuYU=" 17 | "scramuser2" "SCRAM-SHA-256$4096:BUYIT9YAsWf036OHLPfv2Q==$zuk7pzdEc5tvftsRIuwlStNxVc+wIE/xfDs7ahsKN0k=:Lov6mndE0DmlgtY60e1LyDsLctQyoH+Bvkm9K9njSa8=" 18 | 19 | "scramuser3" "baz" 20 | -------------------------------------------------------------------------------- /lib/.appveyor.yml: -------------------------------------------------------------------------------- 1 | image: Visual Studio 2019 2 | 3 | environment: 4 | matrix: 5 | - MSYSTEM: MINGW32 6 | - MSYSTEM: MINGW64 7 | 8 | install: 9 | - set PATH=C:/msys64/usr/bin;%PATH% 10 | - sh -l -c "pacman --noconfirm -S --needed base-devel ${MINGW_PACKAGE_PREFIX}-toolchain ${MINGW_PACKAGE_PREFIX}-libevent autoconf automake libtool" 11 | 12 | build_script: 13 | - set HOME=. 14 | - set PATH=C:/msys64/usr/bin;%PATH% 15 | - sh -l -c "./autogen.sh" 16 | - sh -l -c "./configure --enable-werror || { cat config.log; exit 1; }" 17 | - sh -l -c "make" 18 | - sh -l -c "make install" 19 | 20 | test_script: 21 | - set HOME=. 22 | - set PATH=C:/msys64/usr/bin;%PATH% 23 | - sh -l -c "make -C test all" 24 | #TODO: - sh -l -c "make -C test run" 25 | -------------------------------------------------------------------------------- /lib/test/attregex/Makefile: -------------------------------------------------------------------------------- 1 | SUBLOC = test/attregex 2 | 3 | AM_FEATURES = libusual 4 | 5 | AM_CPPFLAGS = -I$(top_srcdir) -I$(top_builddir) 6 | VPATH = $(top_srcdir) 7 | USUAL_DIR = $(top_srcdir) 8 | 9 | EXTRA_PROGRAMS = testregex.usual testregex.libc 10 | 11 | testregex_usual_SOURCES = testregex.c 12 | testregex_usual_DEFS = -DUSUAL -DUSE_INTERNAL_REGEX 13 | testregex_usual_EMBED_LIBUSUAL = 1 14 | 15 | testregex_libc_SOURCES = testregex.c 16 | 17 | EXTRA_DIST = Makefile run.sh \ 18 | data/basic.dat \ 19 | data/categorize.dat \ 20 | data/forcedassoc.dat \ 21 | data/interpretation.dat \ 22 | data/leftassoc.dat \ 23 | data/nullsubexpr.dat \ 24 | data/repetition.dat \ 25 | data/rightassoc.dat 26 | 27 | test: $(EXTRA_PROGRAMS) 28 | 29 | include ../../build.mk 30 | -------------------------------------------------------------------------------- /lib/test/coverage.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | set -e 4 | 5 | test -f test_common.h || { 6 | echo "wrong dir" 7 | exit 1 8 | } 9 | 10 | cd .. 11 | 12 | rm -rf .objs test/.objs 13 | rm -rf test/lcov/* 14 | 15 | make -C test test_config.h 16 | make -f test/coverage.mk CC="gcc -fprofile-arcs -ftest-coverage" CFLAGS="-O0 -g" 17 | 18 | ./covtest || true 19 | 20 | echo 'Running lcov' 21 | lcov -q --capture --directory .objs/covtest -b . --output-file coverage.info.tmp 22 | 23 | echo 'Fixing filenames' 24 | sed -e '/SF:/s,/\./,/,' coverage.info.tmp > coverage.info 25 | 26 | echo 'Running genhtml' 27 | genhtml -q coverage.info --output-directory test/lcov 28 | echo 'Result: test/lcov/index.html' 29 | rm -f coverage.info.tmp coverage.info 30 | rm -f covtest 31 | rm -rf .objs/covtest 32 | -------------------------------------------------------------------------------- /lib/usual/mbuf.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Safe and easy access to memory buffer. 4 | */ 5 | 6 | #include 7 | 8 | bool mbuf_make_room(struct MBuf *buf, unsigned len) 9 | { 10 | unsigned new_alloc = buf->alloc_len; 11 | void *ptr; 12 | 13 | /* is it a dynamic buffer */ 14 | if (buf->reader || buf->fixed) 15 | return false; 16 | 17 | /* maybe there is enough room already */ 18 | if (buf->write_pos + len <= buf->alloc_len) 19 | return true; 20 | 21 | if (new_alloc == 0) 22 | new_alloc = 128; 23 | 24 | /* calc new alloc size */ 25 | while (new_alloc < buf->write_pos + len) 26 | new_alloc *= 2; 27 | 28 | /* realloc */ 29 | ptr = realloc(buf->data, new_alloc); 30 | if (!ptr) 31 | return false; 32 | buf->data = ptr; 33 | buf->alloc_len = new_alloc; 34 | return true; 35 | } 36 | -------------------------------------------------------------------------------- /lib/test/try_locales.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | make -C .. && make all regtest_compat 4 | 5 | echo "" 6 | echo "# regtest_compat, no locale" 7 | ./regtest_compat --quiet 8 | echo "# regtest_system, no locale" 9 | ./regtest_system --quiet 10 | 11 | export USE_LOCALE=1 12 | 13 | lclist="en_US.UTF-8 ru_RU.UTF-8 et_EE.UTF-8 fa_IR.UTF-8 ps_AF.UTF-8 aa_ER.UTF-8 ja_JP.UTF-8" 14 | lclist="$lclist et_EE.ISO-8859-1 ru_RU.koi-8r ja_JP.EUC-JP zh_CN.BIG5" 15 | 16 | for lc in $lclist; do 17 | if locale -a | grep -i "`echo $lc|sed 's/-//g'`" > /dev/null; then 18 | LC_ALL=$lc 19 | export LC_ALL 20 | echo "# regtest_compat, LC_ALL=$LC_ALL" 21 | ./regtest_compat --quiet 22 | echo "# regtest_system, LC_ALL=$LC_ALL" 23 | ./regtest_system --quiet 24 | else 25 | echo "### $lc not available ###" 26 | fi 27 | done 28 | -------------------------------------------------------------------------------- /etc/pgbouncer.socket: -------------------------------------------------------------------------------- 1 | # Example systemd socket unit for PgBouncer 2 | # 3 | # Note that /var/run/postgresql/ would typically be owned or managed 4 | # by the respective PostgreSQL package. If you specify it here, 5 | # systemd will create it first, which might upset the PostgreSQL 6 | # package (or not). See pgbouncer.service for further context. 7 | # 8 | [Unit] 9 | Description=sockets for PgBouncer 10 | 11 | [Socket] 12 | ListenStream=/tmp/.s.PGSQL.6432 13 | #ListenStream=/var/run/postgresql/.s.PGSQL.6432 14 | #ListenStream=6432 15 | 16 | # additional settings that might be useful 17 | #Backlog= 18 | #SocketUser= 19 | #SocketGroup= 20 | #SocketMode= 21 | #KeepAlive= 22 | #KeepAliveTimeSec= 23 | #KeepAliveIntervalSec= 24 | #KeepAliveProbes= 25 | #DeferAcceptSec= 26 | #ReusePort= 27 | 28 | [Install] 29 | WantedBy=sockets.target 30 | -------------------------------------------------------------------------------- /lib/test/ssl/ca1_client1.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIICCzCCAZGgAwIBAgIURum2wMZEe2lVQ2LG7ZgF6HwsiMswCgYIKoZIzj0EAwIw 3 | TzEQMA4GA1UEAwwHVGVzdENBMTELMAkGA1UEBhMCQUExDjAMBgNVBAcMBUNpdHkx 4 | MQ8wDQYDVQQIDAZTdGF0ZTExDTALBgNVBAoMBE9yZzEwIBcNMTAwMTAxMDgwNTAw 5 | WhgPMjA2MDEyMzEyMzU1MDBaMBIxEDAOBgNVBAMMB2NsaWVudDEwSTATBgcqhkjO 6 | PQIBBggqhkjOPQMBAQMyAAR/pMNGIbq/3EZlAF4Vkf7Iiyh36//rGFcmtggsDyiJ 7 | 5kWIdMBAEeuL4Zh337hKsMOjgZUwgZIwDAYDVR0TAQH/BAIwADAOBgNVHQ8BAf8E 8 | BAMCBaAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwHQYDVR0OBBYEFLUBHAAk7HIVtxWA 9 | tn2xcO5HiwL0MB8GA1UdIwQYMBaAFDe6v+g/tpngh7mWJ8Vt+krMVxe4MB0GA1Ud 10 | EQQWMBSBEmNsaWVudEBjb21wYW55LmNvbTAKBggqhkjOPQQDAgNoADBlAjAPqBHw 11 | uMDDC7dRxCAwHtqqTJTsrBvLcMjekMuDzz7vl/jX5pAK/WcT7E4/WdoEHJwCMQCM 12 | PEh7ROf9+suvjDAo9xoLMUOuoHJDVazMQAg9i2nstmoPiMPrV8Cx8iRXAm1pcvI= 13 | -----END CERTIFICATE----- 14 | -------------------------------------------------------------------------------- /bpf/mp_common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define likely(x) __builtin_expect((x), 1) 4 | #define unlikely(x) __builtin_expect((x), 0) 5 | 6 | enum PoolingMode { 7 | SESSION_POOLING = 0, 8 | TRANSACTION_POOLING = 1, // TODO(Matt): Not sure we care about statement pooling. 9 | }; 10 | 11 | typedef struct { 12 | char type_; 13 | int32_t length_; 14 | } __attribute__((packed)) PostgresMessageHeader; 15 | // __attribute__((packed)) is necessary to avoid padding between type_ and length_ 16 | 17 | typedef struct { 18 | int64_t offset_; 19 | uint8_t split_header_[4]; // PostgresMessageHeader is 5 bytes. We need at most 4 bytes to hold a partial read. 20 | uint32_t sink_; 21 | } SocketState; 22 | 23 | typedef struct { 24 | // TODO(Matt): Maybe keep these network order to there's not constant translation. 25 | uint32_t udp_port_; 26 | uint32_t tcp_port_; 27 | } MirrorPorts; 28 | -------------------------------------------------------------------------------- /doc/Makefile: -------------------------------------------------------------------------------- 1 | -include ../config.mak 2 | 3 | manpages = pgbouncer.1 pgbouncer.5 4 | 5 | dist_man_MANS = $(manpages) 6 | 7 | EXTRA_DIST = config.md usage.md Makefile $(manpages) \ 8 | frag-config-man.md frag-usage-man.md \ 9 | filter.py 10 | 11 | CLEANFILES = pgbouncer_1.md pgbouncer_5.md 12 | 13 | # make maintainer-clean removes those 14 | MAINTAINERCLEANFILES = $(manpages) 15 | 16 | SUBLOC = doc 17 | abs_top_srcdir ?= $(CURDIR)/.. 18 | include $(abs_top_srcdir)/lib/mk/antimake.mk 19 | 20 | export PACKAGE_VERSION 21 | 22 | pgbouncer.%: pgbouncer_%.md 23 | $(PANDOC) -s -t man -o $@ $< 24 | 25 | pgbouncer_1.md: filter.py frag-usage-man.md usage.md 26 | $(PYTHON) $^ >$@ 27 | 28 | pgbouncer_5.md: filter.py frag-config-man.md config.md 29 | $(PYTHON) $^ >$@ 30 | 31 | .INTERMEDIATE: pgbouncer_1.md pgbouncer_5.md 32 | 33 | web: 34 | $(MAKE) -C ../../pgbouncer.github.io 35 | -------------------------------------------------------------------------------- /COPYRIGHT: -------------------------------------------------------------------------------- 1 | PgBouncer - Lightweight connection pooler for PostgreSQL. 2 | 3 | ISC License 4 | 5 | Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 6 | 7 | Permission to use, copy, modify, and/or distribute this software for any 8 | purpose with or without fee is hereby granted, provided that the above 9 | copyright notice and this permission notice appear in all copies. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 | -------------------------------------------------------------------------------- /lib/COPYRIGHT: -------------------------------------------------------------------------------- 1 | /* 2 | * libusual - Utility library for C 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | -------------------------------------------------------------------------------- /lib/test/ssl/ca1_server1.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIICRTCCAcygAwIBAgIUNDUMgIEkwEb+W9tX0LoaSV6cPOowCgYIKoZIzj0EAwIw 3 | TzEQMA4GA1UEAwwHVGVzdENBMTELMAkGA1UEBhMCQUExDjAMBgNVBAcMBUNpdHkx 4 | MQ8wDQYDVQQIDAZTdGF0ZTExDTALBgNVBAoMBE9yZzEwIBcNMTAwMTAxMDgwNTAw 5 | WhgPMjA2MDEyMzEyMzU1MDBaMBYxFDASBgNVBAMMC3NlcnZlcjEuY29tMHYwEAYH 6 | KoZIzj0CAQYFK4EEACIDYgAEEBE/QXZmj4/MdrBZG9ywyOevhw7FUqkr/4qHK8SZ 7 | e0k2uVf2ZBnmjLCUW1Ej80vhKvt1UthR0+Eb8N0ktJzXJR0zpU1VT1oklo9wLSWY 8 | Zu/6sYi94/MDkz87C3gHZxlFo4GfMIGcMAwGA1UdEwEB/wQCMAAwDgYDVR0PAQH/ 9 | BAQDAgWgMBMGA1UdJQQMMAoGCCsGAQUFBwMBMB0GA1UdDgQWBBQoTuCguJ63iLL/ 10 | NBMhchnhfwVy4zAfBgNVHSMEGDAWgBQ3ur/oP7aZ4Ie5lifFbfpKzFcXuDAnBgNV 11 | HREEIDAeggtzZXJ2ZXIxLmNvbYIPd3d3LnNlcnZlcjEuY29tMAoGCCqGSM49BAMC 12 | A2cAMGQCMD1hxC1rYKUn+PM1gPTVMhSRVUCwJmTTLpw/G/YrWxl9Wy6ZqLdORh+Q 13 | tr5FjZOagAIwXIe2D76ywLFT/nFleDEJPJPB54YQadJXjTs0YvH8fKQaIG8WWRuu 14 | ucq2fdDLDaJM 15 | -----END CERTIFICATE----- 16 | -------------------------------------------------------------------------------- /etc/pgbouncer-minimal.ini: -------------------------------------------------------------------------------- 1 | ;;; This is an almost minimal starter configuration file that only 2 | ;;; contains the settings that are either mandatory or almost always 3 | ;;; useful. All settings show their default value. 4 | 5 | [databases] 6 | ;; add yours here 7 | 8 | ;; fallback 9 | ;* = 10 | 11 | [pgbouncer] 12 | ;; required in daemon mode unless syslog is used 13 | ;logfile = 14 | ;; required in daemon mode 15 | ;pidfile = 16 | 17 | ;syslog = 0 18 | 19 | ;; set to enable TCP/IP connections 20 | ;listen_addr = 21 | ;; PgBouncer port 22 | ;listen_port = 6432 23 | 24 | ;; some systems prefer /var/run/postgresql 25 | ;unix_socket_dir = /tmp 26 | 27 | ;; change to taste 28 | ;auth_type = md5 29 | ;; probably need this 30 | ;auth_file = 31 | 32 | ;; pool settings are perhaps best done per pool 33 | ;pool_mode = session 34 | ;default_pool_size = 20 35 | 36 | ;; should probably be raised for production 37 | ;max_client_conn = 100 38 | -------------------------------------------------------------------------------- /win32/win32ver.rc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "usual/config.h" 3 | 4 | // https://docs.microsoft.com/en-us/windows/win32/menurc/versioninfo-resource 5 | 6 | VS_VERSION_INFO VERSIONINFO 7 | FILEVERSION PACKAGE_VERSION_4B 8 | PRODUCTVERSION PACKAGE_VERSION_4B 9 | FILEFLAGSMASK VS_FFI_FILEFLAGSMASK 10 | FILEFLAGS 0x0L 11 | FILEOS VOS_NT_WINDOWS32 12 | FILETYPE VFT_APP 13 | FILESUBTYPE 0x0L 14 | BEGIN 15 | BLOCK "StringFileInfo" 16 | BEGIN 17 | BLOCK "040904B0" // U.S. English, Unicode 18 | BEGIN 19 | VALUE "CompanyName", "PgBouncer developers" 20 | VALUE "FileDescription", "connection pooler for PostgreSQL" 21 | VALUE "FileVersion", PACKAGE_VERSION 22 | VALUE "ProductName", PACKAGE_NAME 23 | VALUE "ProductVersion", PACKAGE_VERSION 24 | END 25 | END 26 | BLOCK "VarFileInfo" 27 | BEGIN 28 | VALUE "Translation", 0x0409, 1200 // U.S. English, Unicode 29 | END 30 | END 31 | -------------------------------------------------------------------------------- /lib/test/awk_test.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | # test find_modules.sh vs. various awks 4 | 5 | # random awks that may be around 6 | awk_list="mawk gawk nawk oawk" 7 | awk_list="$awk_list heirloom-nawk heirloom-oawk" 8 | awk_list="$awk_list original-awk plan9-awk" 9 | 10 | fmod=../find_modules.sh 11 | dir=fmod_test 12 | usual_dir=.. 13 | 14 | rm -rf $dir 15 | mkdir $dir 16 | 17 | ok=1 18 | for f in *.c; do 19 | printf "$f .. " 20 | 21 | # write reference with default 'awk' 22 | ref=$dir/$f.awk 23 | $fmod $usual_dir $f > $ref 2>&1 24 | 25 | for a in $awk_list; do 26 | which $a > /dev/null || continue 27 | printf "$a " 28 | out=$dir/$f.$a 29 | AWK=$a \ 30 | $fmod $usual_dir $f > $out 2>&1 31 | cmp -s $ref $out || { 32 | printf "(FAIL) " 33 | ok=0 34 | } 35 | done 36 | echo "" 37 | done 38 | 39 | if test $ok = 1; then 40 | echo "All OK" 41 | else 42 | echo "FAIL: not all tests passed" 43 | exit 1 44 | fi 45 | -------------------------------------------------------------------------------- /lib/test/ssl/ca1_root.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIICZzCCAeygAwIBAgIUOUm5mSvUgsWMUok80oDSn4344U8wCgYIKoZIzj0EAwIw 3 | TzEQMA4GA1UEAwwHVGVzdENBMTELMAkGA1UEBhMCQUExDjAMBgNVBAcMBUNpdHkx 4 | MQ8wDQYDVQQIDAZTdGF0ZTExDTALBgNVBAoMBE9yZzEwIBcNMTAwMTAxMDgwNTAw 5 | WhgPMjA2MDEyMzEyMzU1MDBaME8xEDAOBgNVBAMMB1Rlc3RDQTExCzAJBgNVBAYT 6 | AkFBMQ4wDAYDVQQHDAVDaXR5MTEPMA0GA1UECAwGU3RhdGUxMQ0wCwYDVQQKDARP 7 | cmcxMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEfWF6GMiPHU0rczf4xUAgIV7Z13Ec 8 | AoCMCLlpXjnB6Be80xe1ZaVq7rp/3wjHfa8l/3d7c1dpvRhD1PK7Cr1kmDb465Fg 9 | etJ7G4JyYogdK046eIeCX5QUn09VrILxJjH3o4GGMIGDMBIGA1UdEwEB/wQIMAYB 10 | Af8CAQEwDgYDVR0PAQH/BAQDAgGGMB0GA1UdJQQWMBQGCCsGAQUFBwMCBggrBgEF 11 | BQcDATAdBgNVHQ4EFgQUN7q/6D+2meCHuZYnxW36SsxXF7gwHwYDVR0jBBgwFoAU 12 | N7q/6D+2meCHuZYnxW36SsxXF7gwCgYIKoZIzj0EAwIDaQAwZgIxAPytjQu5Fc9P 13 | wpwAgqTnbq7EEdjAQt8rlj898fT9715qQXxcSKlI9D2NZ7frtLg+KwIxAJUUsQvC 14 | 6nqrizhONx+kyTEYbWFUmMy/6i+nigp9fdVao03aQiNn3uQfzfuG5fvkTA== 15 | -----END CERTIFICATE----- 16 | -------------------------------------------------------------------------------- /test/ctest6000.ini: -------------------------------------------------------------------------------- 1 | ;; database name = connect string 2 | [databases] 3 | 4 | ; redirect bardb to bazdb on localhost 5 | conntest = host=127.0.0.1 port=7000 dbname=conntest 6 | 7 | ;; Configuation section 8 | [pgbouncer] 9 | 10 | logfile = ctest6000.log 11 | pidfile = ctest6000.pid 12 | 13 | listen_addr = 127.0.0.1 14 | listen_port = 6000 15 | unix_socket_dir = /tmp 16 | 17 | auth_type = md5 18 | auth_file = userlist.txt 19 | 20 | admin_users = marko 21 | stats_users = stats 22 | 23 | pool_mode = transaction 24 | 25 | server_reset_query = reset all 26 | server_check_query = select 1 27 | server_check_delay = 2 28 | 29 | max_client_conn = 1000 30 | default_pool_size = 20 31 | 32 | log_connections = 0 33 | log_disconnections = 0 34 | log_pooler_errors = 0 35 | 36 | server_lifetime = 30 37 | server_idle_timeout = 3 38 | server_connect_timeout = 2 39 | server_login_retry = 5 40 | query_timeout = 5 41 | 42 | client_idle_timeout = 10 43 | client_login_timeout = 50 44 | -------------------------------------------------------------------------------- /test/ctest7000.ini: -------------------------------------------------------------------------------- 1 | ;; database name = connect string 2 | [databases] 3 | 4 | ; redirect bardb to bazdb on localhost 5 | conntest = host=127.0.0.1 port=5432 dbname=marko password=kama 6 | 7 | ;; Configuation section 8 | [pgbouncer] 9 | 10 | logfile = ctest7000.log 11 | pidfile = ctest7000.pid 12 | 13 | listen_addr = 127.0.0.1 14 | listen_port = 7000 15 | unix_socket_dir = /tmp 16 | 17 | auth_type = md5 18 | auth_file = userlist.txt 19 | 20 | admin_users = marko 21 | stats_users = stats 22 | 23 | pool_mode = transaction 24 | 25 | server_reset_query = reset all 26 | server_check_query = select 1 27 | server_check_delay = 2 28 | 29 | max_client_conn = 5000 30 | default_pool_size = 20 31 | 32 | log_connections = 0 33 | log_disconnections = 0 34 | log_pooler_errors = 0 35 | 36 | server_lifetime = 30 37 | server_idle_timeout = 3 38 | server_connect_timeout = 2 39 | server_login_retry = 5 40 | query_timeout = 5 41 | 42 | client_idle_timeout = 10 43 | client_login_timeout = 50 44 | -------------------------------------------------------------------------------- /etc/mkauth.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python3 2 | 3 | import sys 4 | import os 5 | import tempfile 6 | import psycopg2 7 | 8 | if len(sys.argv) != 3: 9 | print('usage: mkauth DSTFN CONNSTR') 10 | sys.exit(1) 11 | 12 | # read old file 13 | fn = sys.argv[1] 14 | try: 15 | old = open(fn, 'r').read() 16 | except IOError: 17 | old = '' 18 | 19 | # create new file data 20 | db = psycopg2.connect(sys.argv[2]) 21 | curs = db.cursor() 22 | curs.execute("select usename, passwd from pg_shadow order by 1") 23 | lines = [] 24 | for user, psw in curs.fetchall(): 25 | user = user.replace('"', '""') 26 | if not psw: 27 | psw = '' 28 | psw = psw.replace('"', '""') 29 | lines.append('"%s" "%s" ""\n' % (user, psw)) 30 | db.commit() 31 | cur = "".join(lines) 32 | 33 | # if changed, replace data securely 34 | if old != cur: 35 | fd, tmpfn = tempfile.mkstemp(dir=os.path.split(fn)[0]) 36 | f = os.fdopen(fd, 'w') 37 | f.write(cur) 38 | f.close() 39 | os.rename(tmpfn, fn) 40 | -------------------------------------------------------------------------------- /include/common/saslprep.h: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | * 3 | * saslprep.h 4 | * SASLprep normalization, for SCRAM authentication 5 | * 6 | * These definitions are used by both frontend and backend code. 7 | * 8 | * Copyright (c) 2017-2020, PostgreSQL Global Development Group 9 | * 10 | * src/include/common/saslprep.h 11 | * 12 | *------------------------------------------------------------------------- 13 | */ 14 | #ifndef SASLPREP_H 15 | #define SASLPREP_H 16 | 17 | /* 18 | * Return codes for pg_saslprep() function. 19 | */ 20 | typedef enum 21 | { 22 | SASLPREP_SUCCESS = 0, 23 | SASLPREP_OOM = -1, /* out of memory (only in frontend) */ 24 | SASLPREP_INVALID_UTF8 = -2, /* input is not a valid UTF-8 string */ 25 | SASLPREP_PROHIBITED = -3 /* output would contain prohibited characters */ 26 | } pg_saslprep_rc; 27 | 28 | extern pg_saslprep_rc pg_saslprep(const char *input, char **output); 29 | 30 | #endif /* SASLPREP_H */ 31 | -------------------------------------------------------------------------------- /lib/configure.ac: -------------------------------------------------------------------------------- 1 | dnl Process this file with autoconf to produce a configure script. 2 | 3 | AC_INIT([libusual],[1.0],[https://libusual.github.com]) 4 | AC_CONFIG_SRCDIR(usual/base.h) 5 | AC_CONFIG_HEADERS([usual/config.h]) 6 | AC_PREREQ([2.59]) 7 | 8 | AC_USUAL_INIT 9 | 10 | AC_USUAL_PROGRAM_CHECK 11 | 12 | LT_INIT 13 | 14 | AC_USUAL_HEADER_CHECK 15 | 16 | AC_USUAL_TYPE_CHECK 17 | 18 | AC_USUAL_FUNCTION_CHECK 19 | 20 | AC_USUAL_CASSERT 21 | 22 | AC_USUAL_WERROR 23 | 24 | AC_USUAL_DEBUG 25 | 26 | AC_USUAL_UREGEX 27 | 28 | PKG_CHECK_MODULES(LIBEVENT, libevent) 29 | 30 | AC_USUAL_GETADDRINFO_A 31 | 32 | 33 | dnl search for common libraries 34 | # Required for infinite() on FreeBSD: 35 | AC_CHECK_LIB(m, main) 36 | AC_SEARCH_LIBS(clock_gettime, rt) 37 | AC_SEARCH_LIBS(getsockname, socket) 38 | AC_SEARCH_LIBS(gethostbyname, nsl) 39 | AC_SEARCH_LIBS(hstrerror, resolv) 40 | 41 | AC_USUAL_TLS 42 | 43 | dnl Output findings 44 | AC_CONFIG_FILES([config.mak mk/libusual.pc build.mk]) 45 | AC_OUTPUT 46 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Maintainers 2 | ----------- 3 | Peter Eisentraut 4 | 5 | Maintainers emeriti 6 | ------------------- 7 | Marko Kreen 8 | Petr Jelinek 9 | 10 | Contributors 11 | ------------ 12 | Alexander Schöcke 13 | Andrew Dunstan 14 | Bjoern Metzdorf 15 | Bob Poekert 16 | Christoph Berg 17 | Cody Cutrer 18 | Dan McGee 19 | David Fetter 20 | David Galoyan 21 | David Sommerseth 22 | Devrim GÜNDÜZ 23 | Dimitri Fontaine 24 | Dmitriy Olshevskiy 25 | Dominique Hermsdorff 26 | Emmanuel Courreges 27 | Eric Radman 28 | Euler Taveira 29 | Filip Rembiałkowski 30 | Giorgio Valoti 31 | Guillaume Aubert 32 | Guillaume Lelarge 33 | Greg Sabino Mullane 34 | Hannu Krosing 35 | Heikki Linnakangas 36 | Hiroshi Saito 37 | Hubert Depesz Lubaczewski 38 | Jacob Coby 39 | James Pye 40 | Jørgen Austvik 41 | Lou Picciano 42 | Magne Mæhre 43 | Magnus Hagander 44 | Martin Pihlak 45 | Mathieu Fenniak 46 | Michael Tharp 47 | Michał Trojnara 48 | Pavel Stehule 49 | Pierre-Emmanuel André 50 | Rich Schaaf 51 | Robert Gogolok 52 | Sam McLeod 53 | Teodor Sigaev 54 | William Grant 55 | -------------------------------------------------------------------------------- /test/ssl/lib.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | # PEM format 4 | 5 | # req fields 6 | # C = Country 7 | # ST = State/Province 8 | # L = Locality 9 | # O = Organization 10 | # OU = Org Unit 11 | # CN = commonName 12 | # ? = emailAddress 13 | 14 | umask 077 15 | 16 | run() { 17 | echo '$' "$@" 18 | "$@" 2>&1 | sed 's/^/ > /' 19 | } 20 | 21 | # key -> csr 22 | run_req() { 23 | tmp="csr.template" 24 | args="" 25 | while test "$1" != '--'; do 26 | args="$args $1" 27 | shift 28 | done 29 | shift 30 | 31 | ( 32 | echo "[req]" 33 | echo "prompt=no" 34 | echo "distinguished_name=req_distinguished_name" 35 | echo "[req_distinguished_name]" 36 | for arg; do echo "$arg"; done 37 | ) > "$tmp" 38 | run openssl req $args -config "$tmp" 39 | rm -f csr.template 40 | } 41 | 42 | run_ca() { 43 | ser=`cat ${CaName}/serial` 44 | run openssl ca -batch -config "${CaName}/config.ini" "$@" 45 | while test "$1" != '-out'; do 46 | shift 47 | done 48 | if test "$1" = '-out'; then 49 | cp "${CaName}/certs/$ser.pem" "$2" 50 | fi 51 | } 52 | -------------------------------------------------------------------------------- /etc/import-common.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | set -eu 4 | 5 | # Script to import common files from PostgreSQL source tree for SCRAM 6 | # support. Run this from time to time, ideally against the latest 7 | # tagged and released stable minor version of PostgreSQL. 8 | # 9 | # The files are not taken verbatim. Especially the header file 10 | # includes are adjusted. So go through the changes with "git add -p" 11 | # or similar after this. 12 | 13 | pgsrcdir=$1 14 | 15 | common_include=' 16 | src/include/common/base64.h 17 | src/include/mb/pg_wchar.h 18 | src/include/common/saslprep.h 19 | src/include/common/scram-common.h 20 | src/include/common/unicode_combining_table.h 21 | src/include/common/unicode_norm.h 22 | src/include/common/unicode_norm_table.h 23 | ' 24 | 25 | common_src=' 26 | src/common/base64.c 27 | src/common/saslprep.c 28 | src/common/scram-common.c 29 | src/common/unicode_norm.c 30 | src/common/wchar.c 31 | ' 32 | 33 | for file in $common_include; do cp -v $pgsrcdir/$file include/common/; done 34 | for file in $common_src; do cp -v $pgsrcdir/$file src/common; done 35 | -------------------------------------------------------------------------------- /include/common/postgres_compat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Various things to allow source files from postgresql code to be 3 | * used in pgbouncer. pgbouncer's system.h needs to be included 4 | * before this. 5 | */ 6 | 7 | /* from c.h */ 8 | 9 | #include 10 | 11 | #define int8 int8_t 12 | #define uint8 uint8_t 13 | #define uint16 uint16_t 14 | #define uint32 uint32_t 15 | 16 | #define lengthof(array) (sizeof (array) / sizeof ((array)[0])) 17 | #define pg_hton32(x) htobe32(x) 18 | 19 | #define pg_attribute_noreturn() _NORETURN 20 | 21 | #define HIGHBIT (0x80) 22 | #define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT) 23 | 24 | 25 | /* sha2.h compat */ 26 | #define pg_sha256_ctx struct sha256_ctx 27 | #define PG_SHA256_BLOCK_LENGTH SHA256_BLOCK_SIZE 28 | #define PG_SHA256_DIGEST_LENGTH SHA256_DIGEST_LENGTH 29 | #define pg_sha256_init(ctx) sha256_reset(ctx) 30 | #define pg_sha256_update(ctx, data, len) sha256_update(ctx, data, len) 31 | #define pg_sha256_final(ctx, dst) sha256_final(ctx, dst) 32 | 33 | 34 | /* define this to use non-server code paths */ 35 | #define FRONTEND 36 | -------------------------------------------------------------------------------- /include/hba.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Host-Based-Access-control file support. 3 | * 4 | * Copyright (c) 2015 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | struct HBA; 20 | 21 | struct HBA *hba_load_rules(const char *fn); 22 | void hba_free(struct HBA *hba); 23 | int hba_eval(struct HBA *hba, PgAddr *addr, bool is_tls, const char *dbname, const char *username); 24 | -------------------------------------------------------------------------------- /include/takeover.h: -------------------------------------------------------------------------------- 1 | /* 2 | * PgBouncer - Lightweight connection pooler for PostgreSQL. 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | void takeover_init(void); 20 | bool takeover_login(PgSocket *bouncer) _MUSTCHECK; 21 | void takeover_login_failed(void); 22 | void takeover_finish(void); 23 | -------------------------------------------------------------------------------- /lib/test/ssl/ca1_complex1.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIC0DCCAlegAwIBAgIUYYnYzF1VUN7Qxf9jhuXvhfIM4OEwCgYIKoZIzj0EAwIw 3 | TzEQMA4GA1UEAwwHVGVzdENBMTELMAkGA1UEBhMCQUExDjAMBgNVBAcMBUNpdHkx 4 | MQ8wDQYDVQQIDAZTdGF0ZTExDTALBgNVBAoMBE9yZzEwIBcNMTAwMTAxMDgwNTAw 5 | WhgPMjA2MDEyMzEyMzU1MDBaMGExFTATBgNVBAMMDGNvbXBsZXgxLmNvbTEQMA4G 6 | A1UEBwwHS8O1enrDpDE2MDQGA1UECAwt5qeY44CF44Gq6KuW5LqJ44KS5byV44GN 7 | 6LW344GT44GX44Gm44GN44Gf44CCMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEJ2Oq 8 | lwqIu7U/b4N6S7XmBzpeg2ifxANnG/Oe+tvaz2g6eou6Qg/OOMTa6KsdrS/gMRMX 9 | NnFpifMN2SJtTDUJx9wachpV5EAJl1feaLVNrnnxE6SlrWzNEanpHhvrLDOyo4Hf 10 | MIHcMAwGA1UdEwEB/wQCMAAwDgYDVR0PAQH/BAQDAgWgMBMGA1UdJQQMMAoGCCsG 11 | AQUFBwMBMB0GA1UdDgQWBBRWZQvJcfet2iUZjI4eUT3wsz3PgDAfBgNVHSMEGDAW 12 | gBQ3ur/oP7aZ4Ie5lifFbfpKzFcXuDBnBgNVHREEYDBeggxjb21wbGV4MS5jb22C 13 | EHd3dy5jb21wbGV4MS5jb22HBH8AAAGHEP/+AAAAAAAAAAAAAAAAAAGGEWh0dHA6 14 | Ly9sb2NhbGhvc3QvgRFmb294YUBleGFtcGxlLmNvbTAKBggqhkjOPQQDAgNnADBk 15 | AjASbSI66ooyx6P/5j4XBVAXowUUxfBf/6DPZ1vYl6wlack/Suq6jV1+rBODxE7t 16 | PSACMBYtxXNLnXCku6YPvczUFWSc7Y3U1iTUPtQJExgTrp/JjM03vd1BOlbfKV5m 17 | tgJ5/A== 18 | -----END CERTIFICATE----- 19 | -------------------------------------------------------------------------------- /test/Makefile: -------------------------------------------------------------------------------- 1 | 2 | PG_CPPFLAGS = -I$(shell pg_config --includedir) 3 | PG_LIBS = -lpq 4 | PG_LDFLAGS = -L$(shell pg_config --libdir) 5 | 6 | USUAL_DIR = ../lib 7 | 8 | SUBLOC = test 9 | DIST_SUBDIRS = ssl 10 | 11 | EXTRA_DIST = conntest.sh ctest6000.ini ctest7000.ini run-conntest.sh \ 12 | hba_test.eval hba_test.rules Makefile \ 13 | test.ini test.sh stress.py userlist.txt 14 | 15 | noinst_PROGRAMS = hba_test 16 | hba_test_CPPFLAGS = -I../include $(LIBEVENT_CFLAGS) 17 | hba_test_LDADD = $(LIBEVENT_LIBS) 18 | hba_test_CFLAGS = -O0 19 | hba_test_SOURCES = hba_test.c ../src/hba.c ../src/util.c 20 | hba_test_EMBED_LIBUSUAL = 1 21 | 22 | EXTRA_PROGRAMS = asynctest 23 | asynctest_CPPFLAGS = -I../include $(PG_CPPFLAGS) $(LIBEVENT_CFLAGS) 24 | asynctest_LDFLAGS = $(PG_LDFLAGS) 25 | asynctest_LDADD = $(PG_LIBS) $(LIBEVENT_LIBS) 26 | asynctest_SOURCES = asynctest.c 27 | asynctest_EMBED_LIBUSUAL = 1 28 | 29 | AM_FEATURES = libusual 30 | 31 | 32 | include ../config.mak 33 | include ../lib/mk/antimake.mk 34 | 35 | check: all 36 | ./test.sh 37 | ./hba_test 38 | ifeq ($(tls_support),yes) 39 | $(MAKE) -C ssl check 40 | endif 41 | -------------------------------------------------------------------------------- /bpf/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Matt Butrovich 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/usual/hashing/crc32.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009 Marko Kreen 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | /** 18 | * @file 19 | * 20 | * CRC32 checksum. 21 | */ 22 | #ifndef _USUAL_HASHING_CRC32_H_ 23 | #define _USUAL_HASHING_CRC32_H_ 24 | 25 | #include 26 | 27 | /** Calculate CRC32 checksum */ 28 | uint32_t calc_crc32(const void *data, size_t len, uint32_t init); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /lib/mk/amext-cxx.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Support for C++ language 3 | # 4 | # - extensions: .cc, .cpp, cxx 5 | # - CXX, CXXFLAGS 6 | # - AM_CXXFLAGS, _CXXFLAGS 7 | # 8 | 9 | # autoconfigurable values 10 | ifneq ($(filter-out @%,@CXX@),) 11 | CXX = @CXX@ 12 | CXXFLAGS = @CXXFLAGS@ 13 | endif 14 | CXX ?= c++ 15 | CXXFLAGS ?= -O -g 16 | 17 | # fixme: add warning flags to CXXFLAGS 18 | CXXFLAGS += $(WFLAGS) 19 | 20 | # helper variables 21 | CXXLD ?= $(CXX) 22 | CXXCOMPILE ?= $(CXX) $(AM_DEFS) $(DEFS) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) 23 | CXXLINK ?= $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@ 24 | 25 | # full compile command 26 | define AM_LANG_CXX_COMPILE 27 | $(E) "CXX" $< 28 | $(Q) $(LTCOMPILE) $(CXXCOMPILE) $(OBJDEPS) -c -o $@ $< 29 | endef 30 | 31 | # full link command 32 | define AM_LANG_CXX_LINK 33 | $(E) "CXXLD" $@ 34 | $(Q) $(LTLINK) $(CXXLINK) $^ $(AM_LIBS) $(LIBS) $(AM_LT_RPATH) 35 | endef 36 | 37 | # source file extensions for c++ 38 | AM_LANG_CXX_SRCEXTS = .cc .cpp cxx 39 | 40 | # register per-target variable 41 | AM_TARGET_VARIABLES += CXXFLAGS 42 | 43 | # register new language 44 | AM_LANGUAGES += CXX 45 | -------------------------------------------------------------------------------- /lib/test/test_netdb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include "test_common.h" 8 | 9 | static int gotres; 10 | 11 | static void cb_func(union sigval v) 12 | { 13 | gotres++; 14 | } 15 | 16 | static void test_gai(void *p) 17 | { 18 | int res; 19 | struct sigevent sev; 20 | struct gaicb req; 21 | struct gaicb *rlist[] = { &req }; 22 | 23 | memset(&req, 0, sizeof(req)); 24 | req.ar_name = "localhost"; 25 | 26 | memset(&sev, 0, sizeof(sev)); 27 | sev.sigev_notify = SIGEV_THREAD; 28 | sev.sigev_notify_function = cb_func; 29 | 30 | res = getaddrinfo_a(GAI_NOWAIT, rlist, 1, &sev); 31 | if (res == EAI_SYSTEM && errno == ENOSYS) { 32 | /* ok - no impl */ 33 | goto end; 34 | } else { 35 | int_check(res, 0); 36 | } 37 | 38 | while (gai_error(&req) == EAI_INPROGRESS || gotres == 0) 39 | usleep(10000); 40 | 41 | int_check(gai_error(&req), 0); 42 | 43 | freeaddrinfo(req.ar_result); 44 | 45 | int_check(gotres, 1); 46 | end:; 47 | } 48 | 49 | struct testcase_t netdb_tests[] = { 50 | { "getaddrinfo_a", test_gai }, 51 | END_OF_TESTCASES 52 | }; 53 | -------------------------------------------------------------------------------- /bpf/mirror_udp.bpf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "mp_bouncer.bpf.h" 4 | #include "mp_common.h" 5 | 6 | SEC("sk_skb/mp_mirror_udp") 7 | int32_t _mp_mirror_udp(struct __sk_buff *const skb) { 8 | // bpf_printk(""); 9 | // bpf_printk("mirror_udp"); 10 | // bpf_printk("len: %u", skb->len); 11 | // bpf_printk("local_port: %u", skb->local_port); 12 | // bpf_printk("remote_port: %u", bpf_ntohl(skb->remote_port)); 13 | 14 | // Read the stashed data offset. 15 | uint8_t data_offset; 16 | bpf_skb_load_bytes(skb, 0, &data_offset, 1); 17 | // bpf_printk("data offset: %u", data_offset); 18 | // Read the stashed TCP destination port. 19 | uint32_t tcp_port; 20 | bpf_skb_load_bytes(skb, 1, &tcp_port, 4); 21 | 22 | // Trim the buffer to remove any leftover bits of TCP header and stashed metadata. 23 | bpf_skb_adjust_room(skb, -data_offset, 0, 0); 24 | // bpf_printk("len: %u", skb->len); 25 | 26 | // Redirect the buffer to the correct TCP port. 27 | // bpf_printk("bouncing to %u", tcp_port); 28 | return bpf_sk_redirect_map(skb, &mirror_tcp_sockets, tcp_port, 0); 29 | } 30 | 31 | char LICENSE[] SEC("license") = "Dual MIT/GPL"; 32 | -------------------------------------------------------------------------------- /test/conntest.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | fw_drop_port() { 4 | echo "fw_drop_port" 5 | case `uname` in 6 | Linux) 7 | sudo iptables -A OUTPUT -p tcp --dport $1 -j DROP;; 8 | Darwin|OpenBSD) 9 | echo "block drop out proto tcp from any to 127.0.0.1 port $1" \ 10 | | sudo pfctl -a pgbouncer -f -;; 11 | *) 12 | echo "Unknown OS"; exit 1;; 13 | esac 14 | } 15 | fw_reject_port() { 16 | echo "fw_reject_port" 17 | case `uname` in 18 | Linux) 19 | sudo iptables -A OUTPUT -p tcp --dport $1 -j REJECT --reject-with tcp-reset;; 20 | Darwin|OpenBSD) 21 | echo "block return-rst out proto tcp from any to 127.0.0.1 port $1" \ 22 | | sudo pfctl -a pgbouncer -f -;; 23 | *) 24 | echo "Unknown OS"; exit 1;; 25 | esac 26 | } 27 | 28 | fw_reset() { 29 | echo "fw_reset" 30 | case `uname` in 31 | Linux) 32 | sudo iptables -F;; 33 | Darwin|OpenBSD) 34 | sudo pfctl -a pgbouncer -F all;; 35 | *) 36 | echo "Unknown OS"; exit 1;; 37 | esac 38 | } 39 | 40 | port=5432 41 | port=7000 42 | 43 | fw_reset 44 | 45 | while true; do 46 | fw_drop_port $port 47 | sleep 12 48 | fw_reset 49 | sleep 12 50 | fw_reject_port $port 51 | sleep 3 52 | fw_reset 53 | sleep 6 54 | done 55 | -------------------------------------------------------------------------------- /include/janitor.h: -------------------------------------------------------------------------------- 1 | /* 2 | * PgBouncer - Lightweight connection pooler for PostgreSQL. 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | void janitor_setup(void); 20 | void config_postprocess(void); 21 | void resume_all(void); 22 | void per_loop_maint(void); 23 | bool suspend_socket(PgSocket *sk, bool force) _MUSTCHECK; 24 | void kill_pool(PgPool *pool); 25 | void kill_database(PgDatabase *db); 26 | -------------------------------------------------------------------------------- /include/common/unicode_norm.h: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | * 3 | * unicode_norm.h 4 | * Routines for normalizing Unicode strings 5 | * 6 | * These definitions are used by both frontend and backend code. 7 | * 8 | * Copyright (c) 2017-2020, PostgreSQL Global Development Group 9 | * 10 | * src/include/common/unicode_norm.h 11 | * 12 | *------------------------------------------------------------------------- 13 | */ 14 | #ifndef UNICODE_NORM_H 15 | #define UNICODE_NORM_H 16 | 17 | #include "common/postgres_compat.h" 18 | #include "common/pg_wchar.h" 19 | 20 | typedef enum 21 | { 22 | UNICODE_NFC = 0, 23 | UNICODE_NFD = 1, 24 | UNICODE_NFKC = 2, 25 | UNICODE_NFKD = 3, 26 | } UnicodeNormalizationForm; 27 | 28 | /* see UAX #15 */ 29 | typedef enum 30 | { 31 | UNICODE_NORM_QC_NO = 0, 32 | UNICODE_NORM_QC_YES = 1, 33 | UNICODE_NORM_QC_MAYBE = -1, 34 | } UnicodeNormalizationQC; 35 | 36 | extern pg_wchar *unicode_normalize(UnicodeNormalizationForm form, const pg_wchar *input); 37 | 38 | extern UnicodeNormalizationQC unicode_is_normalized_quickcheck(UnicodeNormalizationForm form, const pg_wchar *input); 39 | 40 | #endif /* UNICODE_NORM_H */ 41 | -------------------------------------------------------------------------------- /lib/find_modules.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | set -e 4 | 5 | top="$1" 6 | 7 | # sanity check 8 | test -n "$top" || { 9 | echo "usage: $0 USUAL_DIR SRC ..." >&2 10 | exit 1 11 | } 12 | test -f "$top/usual/base.h" || { 13 | echo "usage: $0 USUAL_DIR SRC ..." >&2 14 | exit 1 15 | } 16 | 17 | shift 18 | test -n "$1" || exit 0 19 | 20 | test -n "$AWK" || AWK=awk 21 | 22 | # return uniq module names, exclude already found ones 23 | grep_usual() { 24 | excl='excl["config"]=1' 25 | for m in $m_done; do 26 | excl="$excl;excl[\"$m\"]=1" 27 | done 28 | prog=' 29 | BEGIN { '"$excl"' } 30 | /^#include[ \t]*[<"]usual\// { 31 | p1 = index($0, "/"); 32 | p2 = index($0, "."); 33 | m = substr($0, p1+1, p2-p1-1); 34 | if (!excl[m]) print m; 35 | }' 36 | $AWK "$prog" "$@" | sort -u 37 | } 38 | 39 | # return module filename globs 40 | make_pats() { 41 | for m in "$@"; do 42 | echo "$top/usual/$m*.[ch]" 43 | done 44 | } 45 | 46 | # loop over grep until all mods are found 47 | m_done="" 48 | m_tocheck=`grep_usual "$@"` 49 | while test -n "$m_tocheck"; do 50 | m_done="$m_done $m_tocheck" 51 | pats=`make_pats $m_tocheck` 52 | m_tocheck=`grep_usual $pats` 53 | done 54 | 55 | # done 56 | echo $m_done 57 | -------------------------------------------------------------------------------- /include/client.h: -------------------------------------------------------------------------------- 1 | /* 2 | * PgBouncer - Lightweight connection pooler for PostgreSQL. 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | bool client_proto(SBuf *sbuf, SBufEvent evtype, struct MBuf *pkt) _MUSTCHECK; 20 | bool set_pool(PgSocket *client, const char *dbname, const char *username, const char *password, bool takeover) _MUSTCHECK; 21 | bool handle_auth_query_response(PgSocket *client, PktHdr *pkt); 22 | -------------------------------------------------------------------------------- /lib/usual/hashing/siphash.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012 Marko Kreen 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | /** 18 | * @file 19 | * 20 | * SipHash-2-4 21 | */ 22 | #ifndef _USUAL_HASHING_SIPHASH_H_ 23 | #define _USUAL_HASHING_SIPHASH_H_ 24 | 25 | #include 26 | 27 | /** Calculate SipHash-2-4 checksum */ 28 | uint64_t siphash24(const void *data, size_t len, uint64_t k0, uint64_t k1); 29 | 30 | uint64_t siphash24_secure(const void *data, size_t len); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /lib/test/ssl/ca2_root.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDOzCCAiOgAwIBAgIUdG3AMfOjbP0EO5ra0KUlvRmmhFkwDQYJKoZIhvcNAQEL 3 | BQAwEjEQMA4GA1UEAwwHVGVzdENBMjAgFw0xMDAxMDEwODA1MDBaGA8yMDYwMTIz 4 | MTIzNTUwMFowEjEQMA4GA1UEAwwHVGVzdENBMjCCASIwDQYJKoZIhvcNAQEBBQAD 5 | ggEPADCCAQoCggEBANX4cMlvKEjzD8TlX/0Vf2OFrMiIOAh+IGMXxIzE4YFpFO4W 6 | fCGBN36R0I+cvhSxNP8+qAcm3sE/c6UzuT6uk/bQVBiH6pN//hT+ooDEWTDTbSwi 7 | XkAG1pzgQjjmfFbojE1i41K5GINN5B7Go5fxi5f+cdSE5UJvVo0mV4YjkN7ezZlB 8 | ThRZKnidIV6OPMd3ziUxAbaC4w7b6L4cVdqKdHCp++35IMHzVeYiZGaQJwcCo4V+ 9 | tc8fcD++EPWeov8fDzLT3nNFuU6agGiOs1heWt+7PcihZ5mhBa8odvvqxfK7h2Ys 10 | 9s97YtKcHREdhvOj8KURx60u6NKNjgGc//N3PEMCAwEAAaOBhjCBgzASBgNVHRMB 11 | Af8ECDAGAQH/AgEBMA4GA1UdDwEB/wQEAwIBhjAdBgNVHSUEFjAUBggrBgEFBQcD 12 | AgYIKwYBBQUHAwEwHQYDVR0OBBYEFB/pEcCfp/40X0kUoGC1es1Bi08qMB8GA1Ud 13 | IwQYMBaAFB/pEcCfp/40X0kUoGC1es1Bi08qMA0GCSqGSIb3DQEBCwUAA4IBAQAw 14 | cSuqOcptKNSbFSK92Qrjbx2q3IHIDp6YBZOF1c38hd444By3wUVe1Vi4gcjr9Rqs 15 | uMkQjIqeu0jm8cVev4q+unypkqaZHlJPuDiHC7NLbt1j2dI/8oHrKNcjov7Tn9Fa 16 | owycXm7MvysWYzMQFLKk+7IsL4ifm25jsP9ddJn/q/8OfRKnrZ8XNj+y3ClVR4pW 17 | /u337whclWAymgMKdq9RbwghG0fUh3XQiNxf+3KP12v44wA1Cq5fNNOlboTXERQp 18 | Q/BpM+TGRFxMks9QtNupJ8AO8j7DxQaEAXJL70W31+7GcQU1G9mqGJWvm9AVwQcR 19 | Urg0kHUzwfmL6YgD476o 20 | -----END CERTIFICATE----- 21 | -------------------------------------------------------------------------------- /lib/test/test_time.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include "test_common.h" 6 | 7 | 8 | static void test_get_time(void *p) 9 | { 10 | usec_t t, t2; 11 | usec_t ct, ct2; 12 | 13 | t = get_time_usec(); 14 | ct = get_cached_time(); 15 | 16 | usleep(USEC / 4); 17 | t2 = get_time_usec(); 18 | tt_assert(t + USEC / 4 <= t2); 19 | 20 | ct2 = get_cached_time(); 21 | tt_assert(ct2 == ct); 22 | reset_time_cache(); 23 | ct2 = get_cached_time(); 24 | tt_assert(ct2 != ct); 25 | end:; 26 | } 27 | 28 | static void test_time_format(void *p) 29 | { 30 | char buf[128]; 31 | usec_t t; 32 | 33 | #ifdef WIN32 34 | tt_assert(_putenv("TZ=GMT") >= 0); 35 | _tzset(); 36 | printf( "_daylight = %d\n", _daylight ); 37 | printf( "_timezone = %ld\n", _timezone ); 38 | printf( "_tzname[0] = %s\n", _tzname[0] ); 39 | 40 | #else 41 | setenv("TZ", "GMT", 1); 42 | tzset(); 43 | #endif 44 | 45 | t = 1226059006841546; 46 | str_check(format_time_ms(t, buf, sizeof(buf)), "2008-11-07 11:56:46.841 GMT"); 47 | str_check(format_time_s(t, buf, sizeof(buf)), "2008-11-07 11:56:46 GMT"); 48 | end:; 49 | } 50 | 51 | struct testcase_t time_tests[] = { 52 | { "gettime", test_get_time }, 53 | { "format", test_time_format }, 54 | END_OF_TESTCASES 55 | }; 56 | -------------------------------------------------------------------------------- /lib/test/test_ctype.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include "test_common.h" 5 | 6 | #include 7 | 8 | /* 9 | * if char works 10 | */ 11 | 12 | static void test_ctype_char(void *p) 13 | { 14 | int c, cx; 15 | for (c = 0; c < 256; c++) { 16 | cx = (int)(char)c; 17 | int_check(isalnum(c), isalnum(cx)); 18 | int_check(isalpha(c), isalpha(cx)); 19 | int_check(isascii(c), isascii(cx)); 20 | int_check(isblank(c), isblank(cx)); 21 | int_check(iscntrl(c), iscntrl(cx)); 22 | int_check(isdigit(c), isdigit(cx)); 23 | int_check(islower(c), islower(cx)); 24 | int_check(isgraph(c), isgraph(cx)); 25 | int_check(isprint(c), isprint(cx)); 26 | int_check(ispunct(c), ispunct(cx)); 27 | int_check(isspace(c), isspace(cx)); 28 | int_check(isupper(c), isupper(cx)); 29 | int_check(isxdigit(c), isxdigit(cx)); 30 | if (c == 255) { 31 | int_check(toupper(c), (unsigned char)toupper(cx)); 32 | int_check(tolower(c), (unsigned char)tolower(cx)); 33 | } else { 34 | int_check(toupper(c), toupper(cx)); 35 | int_check(tolower(c), tolower(cx)); 36 | } 37 | } 38 | end:; 39 | } 40 | 41 | 42 | /* 43 | * Describe 44 | */ 45 | 46 | struct testcase_t ctype_tests[] = { 47 | { "ctype_char", test_ctype_char }, 48 | END_OF_TESTCASES 49 | }; 50 | -------------------------------------------------------------------------------- /include/loader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * PgBouncer - Lightweight connection pooler for PostgreSQL. 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /* connstring parsing */ 20 | bool parse_database(void *base, const char *name, const char *connstr) _MUSTCHECK; 21 | 22 | bool parse_user(void *base, const char *name, const char *params) _MUSTCHECK; 23 | 24 | /* user file parsing */ 25 | bool load_auth_file(const char *fn) /* _MUSTCHECK */; 26 | bool loader_users_check(void) /* _MUSTCHECK */; 27 | -------------------------------------------------------------------------------- /include/pooler.h: -------------------------------------------------------------------------------- 1 | /* 2 | * PgBouncer - Lightweight connection pooler for PostgreSQL. 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | void pooler_setup(void); 20 | bool use_pooler_socket(int fd, bool is_unix) _MUSTCHECK; 21 | void resume_pooler(void); 22 | void suspend_pooler(void); 23 | void per_loop_pooler_maint(void); 24 | void pooler_tune_accept(bool on); 25 | 26 | typedef bool (*pooler_cb)(void *arg, int fd, const PgAddr *addr); 27 | bool for_each_pooler_fd(pooler_cb cb, void *arg); 28 | -------------------------------------------------------------------------------- /lib/usual/mempool.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009 Marko Kreen 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | /** 18 | * @file 19 | * Simple memory pool for variable-length allocations. 20 | */ 21 | #ifndef _USUAL_MEMPOOL_H_ 22 | #define _USUAL_MEMPOOL_H_ 23 | 24 | #include 25 | 26 | /** Pool Reference */ 27 | struct MemPool; 28 | 29 | /** Allocate from pool */ 30 | void *mempool_alloc(struct MemPool **pool, unsigned size) _MALLOC; 31 | 32 | /** Release all memory in pool */ 33 | void mempool_destroy(struct MemPool **pool); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /lib/usual/daemon.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * Daemonization & pidfile handling. 3 | */ 4 | /* 5 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 6 | * 7 | * Permission to use, copy, modify, and/or distribute this software for any 8 | * purpose with or without fee is hereby granted, provided that the above 9 | * copyright notice and this permission notice appear in all copies. 10 | * 11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 | */ 19 | 20 | #ifndef _USUAL_DAEMON_H_ 21 | #define _USUAL_DAEMON_H_ 22 | 23 | #include 24 | 25 | /** 26 | * Read a pid from pidfile and send a signal to it. 27 | */ 28 | bool signal_pidfile(const char *pidfile, int sig); 29 | 30 | /** 31 | * Daemonize process and write pidfile. 32 | */ 33 | void daemonize(const char *pidfile, bool go_background); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /lib/test/ssl/ca2_server2.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDWDCCAkCgAwIBAgIUVsbj1rz8O0oebRqntyht2uiAX2swDQYJKoZIhvcNAQEL 3 | BQAwEjEQMA4GA1UEAwwHVGVzdENBMjAgFw0xMDAxMDEwODA1MDBaGA8yMDYwMTIz 4 | MTIzNTUwMFowFjEUMBIGA1UEAwwLc2VydmVyMi5jb20wggEiMA0GCSqGSIb3DQEB 5 | AQUAA4IBDwAwggEKAoIBAQDH6FQkCMpiRyKZmruE+RemvyemZ7OeTzlnXFmO41D7 6 | iRkM33oT1SBPrfgmib1WI2tGjH2/pNQK1QjED+UFlvNIFk8HUjTChgB5f+FP0P12 7 | 9VQLmaFQyROjhprE3V3YvnvNKWCCD8Ajtt1WDAvYGenpdvutaM7zhHQ67PDtj1+Z 8 | ieMXvjVwHnfx5i4zZy1+IbJXUxIjo0Tf+5TpOGApM5q3rFPtymMskWbvj6HHZhI8 9 | suju6ixteoQuHxOu/sl8EHYKDsDLIk8mJXm520cMTHpEo4Smh+i0M5NQshRqmNrU 10 | 53c6sgMRFgDQEO91Xjmtd60mra3ZvBqG0aZYLsBLpa1jAgMBAAGjgZ8wgZwwDAYD 11 | VR0TAQH/BAIwADAOBgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAwwCgYIKwYBBQUHAwEw 12 | HQYDVR0OBBYEFE4WQrsyNCQwrDUDZhw6gJ+pQU0jMB8GA1UdIwQYMBaAFB/pEcCf 13 | p/40X0kUoGC1es1Bi08qMCcGA1UdEQQgMB6CC3NlcnZlcjIuY29tgg93d3cuc2Vy 14 | dmVyMi5jb20wDQYJKoZIhvcNAQELBQADggEBAACx/xZ138xHwKKFq6reYZnFE1l+ 15 | FcsPwxCo3+0pzlNpwkG4yAJrHipVZ8scglN+WzkAjnqwlCucgPbStRR4IsgEz5EP 16 | 0Nw6cT8tNHa/1sNIqaKSSuPxf+RCL3YZ8Kvc6bubTHCNcPgsB4WSesNFKU+eC1Tg 17 | ANJ/FnbxR7Q2kRciuajmOh5jrIBq7PnGE3pvfPdzirER463xqFdlOhqXI3/rcD2o 18 | oENN3y3ecr5HiW5NHXZepCf/csaOTHoJQ+2+L4GKbvP+aAc+D1ZmfOy5ZHi8/f+q 19 | 6K70Zd64aj0y0iELokra2adO2TTimRZXH9/X9mz0qXTDXMTMvR4lhYbjZpg= 20 | -----END CERTIFICATE----- 21 | -------------------------------------------------------------------------------- /etc/example.debian.init.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # pgbouncer Start the PgBouncer PostgreSQL pooler. 4 | # 5 | # The variables below are NOT to be changed. They are there to make the 6 | # script more readable. 7 | 8 | NAME=pgbouncer 9 | DAEMON=/usr/bin/$NAME 10 | PIDFILE=/var/run/$NAME.pid 11 | CONF=/etc/$NAME.ini 12 | OPTS="-d $CONF" 13 | # note: SSD is required only at startup of the daemon. 14 | SSD=`command -v start-stop-daemon` 15 | ENV="env -i LANG=C PATH=/bin:/usr/bin:/usr/local/bin" 16 | 17 | trap "" 1 18 | 19 | # Check if configuration exists 20 | test -f $CONF || exit 0 21 | 22 | case "$1" in 23 | start) 24 | echo -n "Starting server: $NAME" 25 | $ENV $SSD --start --pidfile $PIDFILE --exec $DAEMON -- $OPTS > /dev/null 26 | ;; 27 | 28 | stop) 29 | echo -n "Stopping server: $NAME" 30 | start-stop-daemon --stop --pidfile $PIDFILE 31 | ;; 32 | 33 | reload | force-reload) 34 | echo -n "Reloading $NAME configuration" 35 | start-stop-daemon --stop --pidfile $PIDFILE --signal HUP 36 | ;; 37 | 38 | restart) 39 | $0 stop 40 | $0 start 41 | ;; 42 | 43 | *) 44 | echo "Usage: /etc/init.d/$NAME {start|stop|reload|restart}" 45 | exit 1 46 | ;; 47 | esac 48 | 49 | if [ $? -eq 0 ]; then 50 | echo . 51 | exit 0 52 | else 53 | echo " failed" 54 | exit 1 55 | fi 56 | -------------------------------------------------------------------------------- /lib/mk/amext-msvc.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Support for MSVC toolchain. 3 | # 4 | # Usage: 5 | # 1. Install coreutils (printf, tail) and make from gnuwin32. 6 | # 2. Make sure VC env variables are loaded (PATH) 7 | # 8 | 9 | SHELL = cmd.exe 10 | ShellQuote = "$(subst $$, \$$, $(subst ",\",$(subst \,\\,$(1))))" 11 | 12 | EXEEXT = .exe 13 | LIBEXT = .lib 14 | OBJEXT = .obj 15 | 16 | CC = cl -nologo 17 | CFLAGS = -O2 $(WFLAGS) 18 | WFLAGS = -W2 -w24013 19 | CPP = $(CC) -E 20 | 21 | LDFLAGS = 22 | LIBS = -lws2_32 -ladvapi32 23 | 24 | AR = lib 25 | ARFLAGS = -nologo 26 | 27 | LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -Fe$(call vcFixPath,$@) 28 | 29 | Printf = printf $(subst %,%%,$(1)) $(2) 30 | MKDIR_P = md 31 | 32 | MkDir = if not exist $(call vcFixPath,$(1)) $(MKDIR_P) $(call vcFixPath,$(1)) 33 | 34 | vcFixPath = $(subst /,\,$(1)) 35 | vcFixLibs = $(patsubst %.a,%.lib,$(patsubst -l%,%.lib,$(1))) 36 | vcFixAll = $(call vcFixPath,$(call vcFixLibs,$(1))) 37 | 38 | define AM_LANG_C_COMPILE 39 | $(E) "CC" $< 40 | $(Q) $(COMPILE) -c -Fo$(call vcFixPath,$@) $< | tail -n+2 41 | endef 42 | 43 | define AM_LANG_C_LINK 44 | $(E) "CCLD" $@ 45 | $(Q) $(LINK) $(call vcFixAll,$^ $(AM_LIBS) $(LIBS)) $(AM_LT_RPATH) 46 | endef 47 | 48 | define ar_lib 49 | $(E) "LIB" $@ 50 | $(Q) $(AR) $(ARFLAGS) -out:$(call vcFixPath,$@) $^ 51 | endef 52 | -------------------------------------------------------------------------------- /include/stats.h: -------------------------------------------------------------------------------- 1 | /* 2 | * PgBouncer - Lightweight connection pooler for PostgreSQL. 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | void stats_setup(void); 20 | 21 | bool admin_database_stats(PgSocket *client, struct StatList *pool_list) _MUSTCHECK; 22 | bool admin_database_stats_totals(PgSocket *client, struct StatList *pool_list) _MUSTCHECK; 23 | bool admin_database_stats_averages(PgSocket *client, struct StatList *pool_list) _MUSTCHECK; 24 | bool show_stat_totals(PgSocket *client, struct StatList *pool_list) _MUSTCHECK; 25 | -------------------------------------------------------------------------------- /lib/usual/crypto/entropy.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Load entropy from kernel. 3 | * 4 | * Copyright (c) 2014 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * @file 21 | * Load entropy from OS. 22 | */ 23 | 24 | #ifndef _USUAL_CRYPTO_ENTROPY_H_ 25 | #define _USUAL_CRYPTO_ENTROPY_H_ 26 | 27 | #include 28 | 29 | #ifndef HAVE_GETENTROPY 30 | #define getentropy(dst, len) usual_getentropy(dst, len) 31 | 32 | /** 33 | * Fetch entropy from OS kernel. 34 | */ 35 | int getentropy(void *dst, size_t len); 36 | 37 | #endif /* !HAVE_GETENTROPY */ 38 | #endif /* _USUAL_CRYPTO_ENTROPY_H_ */ 39 | -------------------------------------------------------------------------------- /lib/test/ssl/ca2_client2.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDiDCCAnCgAwIBAgIUeYnZ6Qe/BTQNDAceDBdsdUaOaDcwDQYJKoZIhvcNAQEL 3 | BQAwEjEQMA4GA1UEAwwHVGVzdENBMjAgFw0xMDAxMDEwODA1MDBaGA8yMDYwMTIz 4 | MTIzNTUwMFowTzEQMA4GA1UEAwwHY2xpZW50MjELMAkGA1UEBhMCWFgxDjAMBgNV 5 | BAcMBUNpdHkyMQ8wDQYDVQQIDAZTdGF0ZTIxDTALBgNVBAoMBE9yZzIwggEiMA0G 6 | CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDTEPFSsFp92xIwnrv+v1lhkJsnK6IN 7 | O2fM4PitBZsQtqBCm0WqnMh6e/h6v73ZYnkHPj7lbf81xh4FC3ZeLOfzrQzInsiL 8 | RnPVmrlqw4dg/f0Ch4jBWZCmzGGf2cTb2qDT/08d+RVUxlm2QS15b+p5VBll2u8D 9 | WBkmId9RWCzqHz8w0chjGXD9aOGYdgqgiRxEdMqHql5BHZUhKjBKlczx6S3wYD4i 10 | PX8sficb7N1v2wNJroJ+8R35qBMMKP6u0H85a60A7zTlFOsGsWYWC5tvonWbPGno 11 | vK4SzzP0ucZVzCDmpXy+HjpDzFU2uBNHsZmylx7+9WRKj2fb/ckWh3mzAgMBAAGj 12 | gZYwgZMwDAYDVR0TAQH/BAIwADAOBgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAwwCgYI 13 | KwYBBQUHAwIwHQYDVR0OBBYEFPENUzs24XQ3sKU2UZjSXh08SSF0MB8GA1UdIwQY 14 | MBaAFB/pEcCfp/40X0kUoGC1es1Bi08qMB4GA1UdEQQXMBWBE2NsaWVudDJAY29t 15 | cGFueS5jb20wDQYJKoZIhvcNAQELBQADggEBAKsWzcfWiPPB2YgHIZgfDakpSQnq 16 | TH0obarZbBIsBSovCSno9a91yak5gdc+XorxoVeIRXK+wiflpV5h5rHNBTqoUY2c 17 | EobvolSLTP69asvOw320g4xgD0tGRyPqsg/LZO0ZhMpucK2FFVoaVMO1aNu1OhVQ 18 | +K7u+F5Ay53VYz6k6hDFsM8lUejhaKNXFh2aIDCdxy2/BpnBpwwCEfpCkq9c2FZd 19 | HYCJWG2KcGfbochN84MHhYOba0HJgPKVKqeiH7lJsQwDse6IEdygaAOQn6ARR3OO 20 | cQ0hfkoX1MKy0yLi9kIXoF1YRdwbbqWeCrGdHOWLpNXz5wxHHRD66YAUGEw= 21 | -----END CERTIFICATE----- 22 | -------------------------------------------------------------------------------- /lib/usual/crypto/csrandom.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Cryptographically Secure Randomness. 3 | * 4 | * Copyright (c) 2014 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * @file 21 | * 22 | * Cryptographically Secure Randomness. 23 | */ 24 | 25 | #ifndef _USUAL_CRYPTO_CSRANDOM_H_ 26 | #define _USUAL_CRYPTO_CSRANDOM_H_ 27 | 28 | #include 29 | 30 | /** 31 | * Return random uint32_t. 32 | */ 33 | uint32_t csrandom(void); 34 | 35 | /** 36 | * Return unsigned integer in range. 37 | */ 38 | uint32_t csrandom_range(uint32_t upper_bound); 39 | 40 | /** 41 | * Fill buffer with random bytes. 42 | */ 43 | void csrandom_bytes(void *buf, size_t nbytes); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /lib/test/attregex/data/forcedassoc.dat: -------------------------------------------------------------------------------- 1 | NOTE left-assoc:pass-all right-assoc:pass-all : 2002-04-29 2 | 3 | E (a|ab)(c|bcd) abcd (0,4)(0,1)(1,4) 4 | E (a|ab)(bcd|c) abcd (0,4)(0,1)(1,4) 5 | E (ab|a)(c|bcd) abcd (0,4)(0,1)(1,4) 6 | E (ab|a)(bcd|c) abcd (0,4)(0,1)(1,4) 7 | E ((a|ab)(c|bcd))(d*) abcd (0,4)(0,4)(0,1)(1,4)(4,4) 8 | E ((a|ab)(bcd|c))(d*) abcd (0,4)(0,4)(0,1)(1,4)(4,4) 9 | E ((ab|a)(c|bcd))(d*) abcd (0,4)(0,4)(0,1)(1,4)(4,4) 10 | E ((ab|a)(bcd|c))(d*) abcd (0,4)(0,4)(0,1)(1,4)(4,4) 11 | E (a|ab)((c|bcd)(d*)) abcd (0,4)(0,2)(2,4)(2,3)(3,4) 12 | E (a|ab)((bcd|c)(d*)) abcd (0,4)(0,2)(2,4)(2,3)(3,4) 13 | E (ab|a)((c|bcd)(d*)) abcd (0,4)(0,2)(2,4)(2,3)(3,4) 14 | E (ab|a)((bcd|c)(d*)) abcd (0,4)(0,2)(2,4)(2,3)(3,4) 15 | E (a*)(b|abc) abc (0,3)(0,0)(0,3) 16 | E (a*)(abc|b) abc (0,3)(0,0)(0,3) 17 | E ((a*)(b|abc))(c*) abc (0,3)(0,3)(0,0)(0,3)(3,3) 18 | E ((a*)(abc|b))(c*) abc (0,3)(0,3)(0,0)(0,3)(3,3) 19 | E (a*)((b|abc)(c*)) abc (0,3)(0,1)(1,3)(1,2)(2,3) 20 | E (a*)((abc|b)(c*)) abc (0,3)(0,1)(1,3)(1,2)(2,3) 21 | E (a*)(b|abc) abc (0,3)(0,0)(0,3) 22 | E (a*)(abc|b) abc (0,3)(0,0)(0,3) 23 | E ((a*)(b|abc))(c*) abc (0,3)(0,3)(0,0)(0,3)(3,3) 24 | E ((a*)(abc|b))(c*) abc (0,3)(0,3)(0,0)(0,3)(3,3) 25 | E (a*)((b|abc)(c*)) abc (0,3)(0,1)(1,3)(1,2)(2,3) 26 | E (a*)((abc|b)(c*)) abc (0,3)(0,1)(1,3)(1,2)(2,3) 27 | E (a|ab) ab (0,2)(0,2) 28 | E (ab|a) ab (0,2)(0,2) 29 | E (a|ab)(b*) ab (0,2)(0,2)(2,2) 30 | E (ab|a)(b*) ab (0,2)(0,2)(2,2) 31 | -------------------------------------------------------------------------------- /lib/test/test_strpool.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "test_common.h" 4 | 5 | #include 6 | 7 | static void test_strpool(void *p) 8 | { 9 | struct StrPool *pool; 10 | struct PStr *s; 11 | 12 | pool = strpool_create(NULL); 13 | tt_assert(pool); 14 | strpool_free(pool); 15 | 16 | pool = strpool_create(NULL); 17 | tt_assert(pool); 18 | int_check(strpool_total(pool), 0); 19 | 20 | s = strpool_get(pool, "foo", -1); 21 | str_check(s->str, "foo"); 22 | int_check(s->refcnt, 1); 23 | int_check(s->len, 3); 24 | int_check(strpool_total(pool), 1); 25 | 26 | tt_assert(s == strpool_get(pool, "fooTAIL", 3)); 27 | int_check(s->refcnt, 2); 28 | int_check(strpool_total(pool), 1); 29 | 30 | strpool_incref(s); 31 | int_check(s->refcnt, 3); 32 | 33 | strpool_decref(s); 34 | int_check(s->refcnt, 2); 35 | strpool_decref(s); 36 | int_check(s->refcnt, 1); 37 | int_check(strpool_total(pool), 1); 38 | strpool_decref(s); 39 | int_check(strpool_total(pool), 0); 40 | 41 | strpool_free(pool); 42 | 43 | /* free strc with strings */ 44 | pool = strpool_create(NULL); 45 | tt_assert(pool); 46 | s = strpool_get(pool, "foo", -1); 47 | s = strpool_get(pool, "bar", 3); 48 | int_check(strpool_total(pool), 2); 49 | strpool_free(pool); 50 | 51 | end:; 52 | } 53 | 54 | 55 | /* 56 | * Describe 57 | */ 58 | 59 | struct testcase_t strpool_tests[] = { 60 | { "strpool", test_strpool }, 61 | END_OF_TESTCASES 62 | }; 63 | -------------------------------------------------------------------------------- /include/server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * PgBouncer - Lightweight connection pooler for PostgreSQL. 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | bool server_proto(SBuf *sbuf, SBufEvent evtype, struct MBuf *pkt) _MUSTCHECK; 20 | void kill_pool_logins(PgPool *pool, const char *msg); 21 | int pool_pool_mode(PgPool *pool) _MUSTCHECK; 22 | int pool_pool_size(PgPool *pool) _MUSTCHECK; 23 | int pool_min_pool_size(PgPool *pool) _MUSTCHECK; 24 | int pool_res_pool_size(PgPool *pool) _MUSTCHECK; 25 | int database_max_connections(PgDatabase *db) _MUSTCHECK; 26 | int user_max_connections(PgUser *user) _MUSTCHECK; 27 | int pool_bpf_pool_size(PgPool *pool); 28 | -------------------------------------------------------------------------------- /include/pam.h: -------------------------------------------------------------------------------- 1 | /* 2 | * PgBouncer - Lightweight connection pooler for PostgreSQL. 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /* 20 | * PAM support. 21 | */ 22 | 23 | /* Name of the service to be passed to PAM */ 24 | #define PGBOUNCER_PAM_SERVICE "pgbouncer" 25 | 26 | /* 27 | * Defines how many authentication requests can be placed to the waiting queue. 28 | * When the queue is full calls to pam_auth_begin() will block until there is 29 | * free space in the queue. 30 | */ 31 | #define PAM_REQUEST_QUEUE_SIZE 20 32 | 33 | void pam_init(void); 34 | void pam_auth_begin(PgSocket *client, const char *passwd); 35 | int pam_poll(void); 36 | -------------------------------------------------------------------------------- /lib/usual/wchar.h: -------------------------------------------------------------------------------- 1 | /* 2 | * wchar.h - wchar_t utilities. 3 | * 4 | * Copyright (c) 2012 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | #ifndef _USUAL_WCHAR_H_ 20 | #define _USUAL_WCHAR_H_ 21 | 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | wchar_t *mbstr_decode(const char *str, int str_len, int *wlen_p, wchar_t *wbuf, int wbuf_len, bool allow_invalid); 28 | 29 | wctype_t wctype_wcsn(const wchar_t *name, unsigned int namelen); 30 | 31 | #ifndef HAVE_MBSNRTOWCS 32 | #define mbsnrtowcs(a,b,c,d,e) usual_mbsnrtowcs(a,b,c,d,e) 33 | size_t mbsnrtowcs(wchar_t *dst, const char **src_p, size_t srclen, size_t dstlen, mbstate_t *ps); 34 | #endif 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /etc/pgbouncer.service: -------------------------------------------------------------------------------- 1 | # Example systemd service unit for PgBouncer 2 | # 3 | # - Adjust the paths in ExecStart for your installation. 4 | # 5 | # - The User setting requires careful consideration. PgBouncer needs 6 | # to be able to place a Unix-domain socket file where PostgreSQL 7 | # clients will look for it. In the olden days, this was in /tmp, 8 | # but systems using systemd now prefer something like 9 | # /var/run/postgresql/. But then some systems also lock down that 10 | # directory so that only the postgres user can write to it. That 11 | # means you need to either 12 | # 13 | # - run PgBouncer as the postgres user, or 14 | # 15 | # - create a separate user and add it to the postgres group and 16 | # make /var/run/postgresql/ group-writable, or 17 | # 18 | # - use systemd to create the sockets; see pgbouncer.socket nearby. 19 | # 20 | # For packagers and deployment systems, this requires some 21 | # coordination between the PgBouncer and the PostgreSQL 22 | # packages/components. 23 | # 24 | [Unit] 25 | Description=connection pooler for PostgreSQL 26 | Documentation=man:pgbouncer(1) 27 | Documentation=https://www.pgbouncer.org/ 28 | After=network.target 29 | #Requires=pgbouncer.socket 30 | 31 | [Service] 32 | Type=notify 33 | User=postgres 34 | ExecStart=/usr/bin/pgbouncer /etc/pgbouncer/pgbouncer.ini 35 | ExecReload=/bin/kill -HUP $MAINPID 36 | KillSignal=SIGINT 37 | #LimitNOFILE=1024 38 | 39 | [Install] 40 | WantedBy=multi-user.target 41 | -------------------------------------------------------------------------------- /test/ssl/newca.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | # create new CA 4 | 5 | set -e 6 | 7 | test -n "$1" || { 8 | echo "usage: $0 CaName [K=V]*" 9 | exit 1 10 | } 11 | 12 | test -d "$1" && { 13 | echo "CA '$1' already exists" 14 | exit 1 15 | } 16 | 17 | name="$1" 18 | shift 19 | 20 | mkdir -p "$name"/certs 21 | mkdir -p "$name"/sites 22 | touch "$name"/index.txt 23 | echo 01 > "$name"/serial 24 | 25 | . ./lib.sh 26 | 27 | days=10240 28 | 29 | #run openssl genrsa -out "$name/ca.key" $ksize 30 | run openssl ecparam -name prime256v1 -genkey -out "$name/ca.key" 31 | 32 | # self-signed cert 33 | run_req -new -x509 -days $days -key "$name/ca.key" -out "$name/ca.crt" -- "$@" 34 | 35 | 36 | cat > "$name"/config.ini < $@ 11 | 12 | mp_client: client.bpf.c vmlinux.h mp_bouncer.bpf.h mp_common.h 13 | $(CC) $(CFLAGS) -target bpf -c client.bpf.c -o mp_client.bpf.o 14 | 15 | mp_server: server.bpf.c vmlinux.h mp_bouncer.bpf.h mp_common.h 16 | $(CC) $(CFLAGS) -target bpf -c server.bpf.c -o mp_server.bpf.o 17 | 18 | mp_mirror_udp: mirror_udp.bpf.c vmlinux.h mp_common.h 19 | $(CC) $(CFLAGS) -target bpf -c mirror_udp.bpf.c -o mp_mirror_udp.bpf.o 20 | 21 | mp_mirror_tcp: mirror_tcp.bpf.c vmlinux.h mp_common.h 22 | $(CC) $(CFLAGS) -target bpf -c mirror_tcp.bpf.c -o mp_mirror_tcp.bpf.o 23 | 24 | tc_test: tc_test.bpf.c vmlinux.h mp_common.h 25 | $(CC) $(CFLAGS) -target bpf -c tc_test.bpf.c -o tc_test.bpf.o 26 | 27 | skeletons: mp_client mp_server mp_mirror_udp mp_mirror_tcp 28 | bpftool gen skeleton ./mp_client.bpf.o > ./client.skel.h 29 | bpftool gen skeleton ./mp_server.bpf.o > ./server.skel.h 30 | bpftool gen skeleton ./mp_mirror_udp.bpf.o > ./mirror_udp.skel.h 31 | bpftool gen skeleton ./mp_mirror_tcp.bpf.o > ./mirror_tcp.skel.h 32 | 33 | mp_bouncer: mp_bouncer.c skeletons mp_common.h tc_test 34 | $(CC) $(CFLAGS) mp_bouncer.c -o mp_bouncer -lbpf 35 | 36 | .PHONY: all clean 37 | 38 | .DELETE_ON_ERROR: 39 | .SECONDARY: 40 | -------------------------------------------------------------------------------- /lib/mk/temos/index.txt: -------------------------------------------------------------------------------- 1 | = Libusual/Antimake Build Demos = 2 | 3 | Here are few demos showing how to use both Libusual and Antimake 4 | in various scenarios. If you wonder why there is so many of them, 5 | the reason is that these demos are also used as regressions tests 6 | for various functionality. (Thus, 'temos') 7 | 8 | They are ordered in ascending complexity, so pick first one if you 9 | want simplest overview. 10 | 11 | == Reference Documentation == 12 | 13 | - link:antimake.html[AntiMake documentation] 14 | 15 | == Using Antimake == 16 | 17 | - link:antimake1.html[antimake1] - Project without subdirs 18 | - link:antimake2.html[antimake2] - Recursive subdirectories (SUBDIRS) 19 | - link:antimake3.html[antimake3] - Non-recursive subdirectories (EMBED_SUBDIRS) 20 | - link:antimake4.html[antimake4] - Using Autoconf. 21 | - link:antimake5.html[antimake5] - Shared libraries with Autoconf. 22 | - link:antimake6.html[antimake5] - Subdir stress test. 23 | 24 | == Using libusual == 25 | 26 | - link:libusual1.html[libusual1] - Local libusual: Linking against libusual.a with plain Make 27 | - link:libusual2.html[libusual2] - Local libusual: Embedding libusual modules with/witout Antimake 28 | - link:libusual3.html[libusual3] - Local libusual: Using top-level autoconf with Antimake 29 | - link:libusual4.html[libusual4] - Installed libusual: Plain make 30 | - link:libusual5.html[libusual5] - Installed libusual: Antimake 31 | - link:libusual6.html[libusual6] - Installed libusual: Autoconf 32 | -------------------------------------------------------------------------------- /lib/usual/dlfcn.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Dynamic library loading. 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | #ifdef _WIN32 20 | 21 | #include 22 | 23 | /* 24 | * win32: Minimal dlopen, dlsym, dlclose, dlerror compat. 25 | */ 26 | 27 | void *dlopen(const char *fn, int flag) 28 | { 29 | HMODULE h = LoadLibraryEx(fn, NULL, 0); 30 | return h; 31 | } 32 | 33 | void *dlsym(void *hptr, const char *fname) 34 | { 35 | HMODULE h = hptr; 36 | FARPROC f = GetProcAddress(h, fname); 37 | return f; 38 | } 39 | 40 | int dlclose(void *hptr) 41 | { 42 | HMODULE h = hptr; 43 | return FreeLibrary(h) ? 0 : -1; 44 | } 45 | 46 | const char *dlerror(void) 47 | { 48 | return strerror(GetLastError()); 49 | } 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /include/admin.h: -------------------------------------------------------------------------------- 1 | /* 2 | * PgBouncer - Lightweight connection pooler for PostgreSQL. 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | bool admin_handle_client(PgSocket *client, PktHdr *pkt) _MUSTCHECK; 19 | bool admin_pre_login(PgSocket *client, const char *username) _MUSTCHECK; 20 | bool admin_post_login(PgSocket *client) _MUSTCHECK; 21 | void admin_setup(void); 22 | bool admin_error(PgSocket *console, const char *fmt, ...) _PRINTF(2, 3) /* _MUSTCHECK */; 23 | void admin_pause_done(void); 24 | void admin_wait_close_done(void); 25 | bool admin_flush(PgSocket *admin, PktBuf *buf, const char *desc) /* _MUSTCHECK */; 26 | bool admin_ready(PgSocket *admin, const char *desc) _MUSTCHECK; 27 | void admin_handle_cancel(PgSocket *client); 28 | void admin_cleanup(void); 29 | -------------------------------------------------------------------------------- /lib/usual/dlfcn.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Dynamic library loading. 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | #ifndef _USUAL_DLFCN_H_ 20 | #define _USUAL_DLFCN_H_ 21 | 22 | #ifdef HAVE_DLFCN_H 23 | 24 | #include 25 | 26 | #elif defined(_WIN32) 27 | 28 | #define dlopen(a,b) usual_dlopen(a,b) 29 | #define dlsym(a,b) usual_dlsym(a,b) 30 | #define dlclose(a) usual_dlclose(a) 31 | #define dlerror(...) usual_dlerror(__VA_ARGS__) 32 | 33 | /* 34 | * win32: Minimal dlopen, dlsym, dlclose, dlerror compat. 35 | */ 36 | 37 | #define RTLD_LAZY 1 38 | #define RTLD_NOW 2 39 | 40 | void *dlopen(const char *fn, int flag); 41 | void *dlsym(void *hptr, const char *fname); 42 | int dlclose(void *hptr); 43 | const char *dlerror(void); 44 | 45 | #endif /* _WIN32 */ 46 | #endif /* !_USUAL_DLFCN_H_ */ 47 | -------------------------------------------------------------------------------- /lib/usual/hashing/memhash.h: -------------------------------------------------------------------------------- 1 | /* 2 | * memhash.h - Randomized in-memory hashing. 3 | * 4 | * Copyright (c) 2014 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * @file 21 | * 22 | * Randomized in-memory hashing. 23 | * 24 | * Functions here use randomized seed and pick fastest hash 25 | * for current CPU. 26 | */ 27 | 28 | #ifndef _USUAL_HASHING_MEMHASH_H_ 29 | #define _USUAL_HASHING_MEMHASH_H_ 30 | 31 | #include 32 | 33 | /** 34 | * Hash data. 35 | */ 36 | uint32_t memhash(const void *data, size_t len); 37 | 38 | /** 39 | * Hash zero-terminated string. 40 | */ 41 | uint32_t memhash_string(const char *s); 42 | 43 | /** 44 | * Hash with given seed. Result is not randomized, 45 | * but still may vary on different CPU-s. 46 | */ 47 | uint32_t memhash_seed(const void *data, size_t len, uint32_t seed); 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /lib/test/test_mdict.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include "test_common.h" 6 | 7 | static const char *xget(struct MDict *d, const char *k) 8 | { 9 | const char *val = mdict_get(d, k); 10 | return val ? val : "NULL"; 11 | } 12 | 13 | static void test_mdict(void *p) 14 | { 15 | struct MDict *d; 16 | struct MBuf buf; 17 | const char *s; 18 | 19 | d = mdict_new(NULL); 20 | str_check(xget(d, "key"), "NULL"); 21 | int_check(mdict_put(d, "key", "val"), 1); 22 | int_check(mdict_put(d, "key2", "foo"), 1); 23 | int_check(mdict_put(d, "key2", ""), 1); 24 | int_check(mdict_put(d, "key3", NULL), 1); 25 | int_check(mdict_put(d, "key4", "v1"), 1); 26 | int_check(mdict_del(d, "key4"), 1); 27 | str_check(xget(d, "key"), "val"); 28 | str_check(xget(d, "key2"), ""); 29 | str_check(xget(d, "key3"), "NULL"); 30 | str_check(xget(d, "key4"), "NULL"); 31 | str_check(xget(d, "key5"), "NULL"); 32 | int_check(mdict_del(d, "key5"), 0); 33 | 34 | mbuf_init_dynamic(&buf); 35 | int_check(mdict_urlencode(d, &buf), 1); 36 | int_check(mbuf_write_byte(&buf, 0), 1); 37 | str_check(mbuf_data(&buf), "key=val&key2=&key3"); 38 | mbuf_free(&buf); 39 | 40 | mdict_free(d); 41 | 42 | d = mdict_new(NULL); 43 | s = "key=val&key2=&key3"; 44 | int_check(mdict_urldecode(d, s, strlen(s)), 1); 45 | str_check(xget(d, "key"), "val"); 46 | str_check(xget(d, "key2"), ""); 47 | str_check(xget(d, "key3"), "NULL"); 48 | mdict_free(d); 49 | end:; 50 | } 51 | 52 | 53 | /* 54 | * Describe 55 | */ 56 | 57 | struct testcase_t mdict_tests[] = { 58 | { "basic", test_mdict }, 59 | END_OF_TESTCASES 60 | }; 61 | -------------------------------------------------------------------------------- /test/hba_test.rules: -------------------------------------------------------------------------------- 1 | # METHOD: trust, reject, md5, password, peer, cert 2 | 3 | # local DATABASE USER METHOD [OPTIONS] 4 | # host DATABASE USER ADDRESS METHOD [OPTIONS] 5 | # hostssl DATABASE USER ADDRESS METHOD [OPTIONS] 6 | # hostnossl DATABASE USER ADDRESS METHOD [OPTIONS] 7 | 8 | # ws 9 | 10 | # z 11 | 12 | # testing 13 | 14 | local dbp all peer 15 | local all userp password 16 | local dbz userz trust 17 | local dbs users scram-sha-256 18 | local all all md5 19 | 20 | hostssl all all 10.0.0.0/8 cert 21 | hostnossl all all 11.0.0.0/8 md5 22 | host all all 127.0.0.0 255.255.255.0 password 23 | 24 | host db1 all 0.0.0.0/0 md5 25 | host all user1 15.0.0.0/8 md5 26 | 27 | host tmp1,all user1,user2 , user3 16.0.0.0/8 md5 28 | host tmp2,all u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u2 17.0.0.0/8 md5 29 | host d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11 t18user 18.0.0.0/8 trust # comment 30 | 31 | host "all" "all" 19.0.0.0/8 cert 32 | host "q1""q2" "a , b" 19.0.0.0/8 cert 33 | 34 | # mask 35 | host mdb muser 199.199.199.199/32 cert 36 | 37 | host mdb2 muser 128.0.0.0/9 trust 38 | host mdb2 muser 128.0.0.0/8 md5 39 | host mdb2 muser 128.0.0.0/7 cert 40 | host mdb2 muser 128.0.0.0/6 password 41 | host mdb2 muser 128.0.0.0/5 cert 42 | host mdb2 muser 128.0.0.0/4 trust 43 | host mdb2 muser 128.0.0.0/3 md5 44 | host mdb2 muser 128.0.0.0/2 password 45 | host mdb2 muser 128.0.0.0/1 cert 46 | 47 | # ipv6 48 | host mdb muser ff11::0/16 md5 49 | host mdb muser ff20::/12 md5 50 | host mdb muser ::1/128 trust 51 | -------------------------------------------------------------------------------- /lib/config.mak.in: -------------------------------------------------------------------------------- 1 | PACKAGE_NAME = @PACKAGE_NAME@ 2 | PACKAGE_TARNAME = @PACKAGE_TARNAME@ 3 | PACKAGE_VERSION = @PACKAGE_VERSION@ 4 | PACKAGE_STRING = @PACKAGE_STRING@ 5 | PACKAGE_URL = @PACKAGE_URL@ 6 | PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ 7 | PORTNAME = @PORTNAME@ 8 | EXEEXT = @EXEEXT@ 9 | HAVE_CC_DEPFLAG = @HAVE_CC_DEPFLAG@ 10 | CC = @CC@ 11 | CPP = @CPP@ 12 | CPPFLAGS = @CPPFLAGS@ 13 | CFLAGS = @CFLAGS@ 14 | DEFS = @DEFS@ 15 | WFLAGS = @WFLAGS@ 16 | LD = @LD@ 17 | LDFLAGS = @LDFLAGS@ 18 | LIBS = @LIBS@ 19 | AR = @AR@ 20 | ARFLAGS = @ARFLAGS@ 21 | RANLIB = @RANLIB@ 22 | LIBTOOL = @LIBTOOL@ 23 | SHELL = @SHELL@ 24 | INSTALL = @INSTALL@ 25 | INSTALL_PROGRAM = @INSTALL_PROGRAM@ 26 | INSTALL_SCRIPT = @INSTALL_SCRIPT@ 27 | INSTALL_DATA = @INSTALL_DATA@ 28 | MKDIR_P = @MKDIR_P@ 29 | SED = @SED@ 30 | AWK = @AWK@ 31 | GREP = @GREP@ 32 | EGREP = @EGREP@ 33 | STRIP = @STRIP@ 34 | 35 | LIBEVENT_CFLAGS = @LIBEVENT_CFLAGS@ 36 | LIBEVENT_LIBS = @LIBEVENT_LIBS@ 37 | 38 | TLS_CPPFLAGS = @TLS_CPPFLAGS@ 39 | TLS_LDFLAGS = @TLS_LDFLAGS@ 40 | TLS_LIBS = @TLS_LIBS@ 41 | 42 | prefix = @prefix@ 43 | exec_prefix = @exec_prefix@ 44 | bindir = @bindir@ 45 | includedir = @includedir@ 46 | sbindir = @sbindir@ 47 | libexecdir = @libexecdir@ 48 | datarootdir = @datarootdir@ 49 | datadir = @datadir@ 50 | sysconfdir = @sysconfdir@ 51 | docdir = @docdir@ 52 | mandir = @mandir@ 53 | libdir = @libdir@ 54 | localedir = @localedir@ 55 | pkgdatadir = @pkgdatadir@ 56 | pkgconfigdir = @pkgconfigdir@ 57 | abs_top_srcdir ?= @abs_top_srcdir@ 58 | abs_top_builddir ?= @abs_top_builddir@ 59 | nosub_top_srcdir ?= @top_srcdir@ 60 | nosub_top_builddir ?= @top_builddir@ 61 | 62 | enable_debug = @enable_debug@ 63 | -------------------------------------------------------------------------------- /lib/mk/temos/src/libusual1.temo: -------------------------------------------------------------------------------- 1 | 2 | . ./libtemo.sh || exit 1 3 | 4 | test_start 5 | 6 | title Use libusual the simplest way 7 | 8 | longmsg <<-MSG 9 | Simplest usage would be to configure and build libusual 10 | locally and point your projects CPPFLAGS and LDFLAGS there. 11 | 12 | That way you get access to not only code but also 13 | various autoconfigued symbols without any complexities 14 | in your project. 15 | MSG 16 | 17 | title2 Build libusual 18 | 19 | usual_clone lib 20 | 21 | run cd lib 22 | 23 | runq ./autogen.sh 24 | runq ./configure --disable-shared --prefix=/opt 25 | runq make 26 | runq make install 'DESTDIR=`pwd`/../inst' 27 | 28 | run cd .. 29 | 30 | title2 Build our own code 31 | 32 | msg Now we prepare our own code. 33 | 34 | msg First, this is the source file: 35 | cat_file prog.c <<"EOF" 36 | #include 37 | #include 38 | #include 39 | 40 | int main(void) 41 | { 42 | const char *data = "CECSFXX"; 43 | uint32_t crc; 44 | 45 | crc = calc_crc32(data, strlen(data), 0); 46 | printf("crc: %08x\n", crc); 47 | return 0; 48 | } 49 | EOF 50 | 51 | msg Here is corresponding Makefile: 52 | cat_file Makefile <<"EOF" 53 | # here we describe our program 54 | SRCS = prog.c 55 | OBJS = $(SRCS:.c=.o) 56 | 57 | # here we link to libusual 58 | CPPFLAGS = -I./inst/opt/include 59 | LDFLAGS = -L./inst/opt/lib 60 | LIBS = -lusual 61 | 62 | CC = gcc 63 | CFLAGS = -O -g -Wall 64 | 65 | all: prog 66 | 67 | prog: $(OBJS) 68 | $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@ 69 | EOF 70 | 71 | msg Build the project 72 | 73 | run make 74 | run ls 75 | run ./prog 76 | 77 | msg Done! 78 | -------------------------------------------------------------------------------- /lib/usual/crypto/md5.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MD5 implementation based on RFC1321. 3 | * 4 | * Copyright (c) 2008 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * @file 21 | * 22 | * MD5 cryptographic hash. 23 | */ 24 | 25 | #ifndef _USUAL_CRYPTO_MD5_H_ 26 | #define _USUAL_CRYPTO_MD5_H_ 27 | 28 | #include 29 | 30 | /** Block length for MD5 */ 31 | #define MD5_BLOCK_LENGTH 64 32 | 33 | /** Result length for MD5 */ 34 | #define MD5_DIGEST_LENGTH 16 35 | 36 | /** MD5 state */ 37 | struct md5_ctx { 38 | uint64_t nbytes; 39 | uint32_t a, b, c, d; 40 | uint32_t buf[16]; 41 | }; 42 | 43 | /** Clean state */ 44 | void md5_reset(struct md5_ctx *ctx); 45 | 46 | /** Update state with more data */ 47 | void md5_update(struct md5_ctx *ctx, const void *data, unsigned int len); 48 | 49 | /** Get final result */ 50 | void md5_final(struct md5_ctx *ctx, uint8_t *dst); 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /lib/usual/crypto/sha1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SHA1 implementation based on RFC3174. 3 | * 4 | * Copyright (c) 2009 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * @file 21 | * 22 | * SHA1 implementation. 23 | */ 24 | 25 | #ifndef _USUAL_CRYPTO_SHA1_H_ 26 | #define _USUAL_CRYPTO_SHA1_H_ 27 | 28 | #include 29 | 30 | /** Block length for SHA1 */ 31 | #define SHA1_BLOCK_SIZE 64 32 | 33 | /** Result length for SHA1 */ 34 | #define SHA1_DIGEST_LENGTH 20 35 | 36 | 37 | /** SHA1 state */ 38 | struct sha1_ctx { 39 | uint64_t nbytes; 40 | uint32_t a, b, c, d, e; 41 | uint32_t buf[SHA1_BLOCK_SIZE / 4]; 42 | }; 43 | 44 | /** Clean state */ 45 | void sha1_reset(struct sha1_ctx *ctx); 46 | 47 | /** Update state with more data */ 48 | void sha1_update(struct sha1_ctx *ctx, const void *data, unsigned int len); 49 | 50 | /** Get final result */ 51 | void sha1_final(struct sha1_ctx *ctx, uint8_t *dst); 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /include/system.h: -------------------------------------------------------------------------------- 1 | /* 2 | * PgBouncer - Lightweight connection pooler for PostgreSQL. 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /* 20 | * Required system headers 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #ifdef HAVE_LIBGEN_H 34 | #include 35 | #endif 36 | #ifdef HAVE_SYS_UIO_H 37 | #include 38 | #endif 39 | 40 | /* 41 | * libc compat functions. 42 | */ 43 | 44 | #ifndef HAVE_LSTAT 45 | static inline int lstat(const char *path, struct stat *st) { return stat(path, st); } 46 | #endif 47 | 48 | bool check_unix_peer_name(int fd, const char *username); 49 | 50 | void change_user(const char *user); 51 | 52 | void change_file_mode(const char *fn, mode_t mode, const char *user, const char *group); 53 | -------------------------------------------------------------------------------- /lib/usual/pgutil.h: -------------------------------------------------------------------------------- 1 | /* 2 | * libusual - Utility library for C 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * @file 21 | * 22 | * Utility functions for PostgreSQL data formats. 23 | */ 24 | #ifndef _USUAL_PGUTIL_H_ 25 | #define _USUAL_PGUTIL_H_ 26 | 27 | #include 28 | 29 | /** Check if string is reserver word for PostgreSQL. */ 30 | bool pg_is_reserved_word(const char *str); 31 | 32 | /** Quote value as string for PostgreSQL */ 33 | bool pg_quote_literal(char *_dst, const char *_src, int dstlen); 34 | 35 | /** Quote value as ident for PostgreSQL */ 36 | bool pg_quote_ident(char *_dst, const char *_src, int dstlen); 37 | 38 | /** Quote fully-qualified ident for PostgreSQL */ 39 | bool pg_quote_fqident(char *_dst, const char *_src, int dstlen); 40 | 41 | /** Parse PostgreSQL array. */ 42 | struct StrList *pg_parse_array(const char *pgarr, CxMem *cx); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /lib/test/test_fileutil.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | #include "test_common.h" 7 | 8 | /* 9 | * LN1 = 4*8 10 | * LN2 = 8*4*8 11 | * LN3 = 8*8*4*8 12 | */ 13 | 14 | #define LN1 "11112222333344445555666677778888" 15 | #define LN2 LN1 LN1 LN1 LN1 LN1 LN1 LN1 LN1 16 | #define LN3 LN2 LN2 LN2 LN2 LN2 LN2 LN2 LN2 17 | 18 | static const char fdata[] = "1\n" 19 | "line 2\n" 20 | "\n" 21 | LN3 22 | "noln"; 23 | static const char filename[] = "test_fileutil.tmp"; 24 | 25 | static bool createfile(void) 26 | { 27 | FILE *f = fopen(filename, "wb+"); 28 | if (!f) return false; 29 | fwrite(fdata, 1, strlen(fdata), f); 30 | fclose(f); 31 | return true; 32 | } 33 | 34 | static void test_fsize(void *p) 35 | { 36 | int_check(createfile(), 1); 37 | 38 | tt_assert(file_size(filename) == (int)strlen(fdata)); 39 | tt_assert(file_size(filename) == (int)sizeof(fdata) - 1); 40 | tt_assert(file_size("nonexist") == -1); 41 | end:; 42 | } 43 | 44 | static bool addln(void *arg, const char *ln, ssize_t len) 45 | { 46 | struct MBuf *buf = arg; 47 | int xlen = len; 48 | if (len < 0) 49 | return false; 50 | if (len > 0 && ln[len - 1] == '\n') 51 | xlen--; 52 | if (memchr(ln, '\n', xlen)) 53 | return false; 54 | return mbuf_write(buf, ln, len); 55 | } 56 | 57 | static void test_getline(void *p) 58 | { 59 | struct MBuf buf; 60 | 61 | mbuf_init_dynamic(&buf); 62 | 63 | tt_assert(foreach_line(filename, addln, &buf)); 64 | tt_assert(mbuf_write_byte(&buf, 0)); 65 | end: 66 | unlink(filename); 67 | mbuf_free(&buf); 68 | } 69 | 70 | struct testcase_t fileutil_tests[] = { 71 | { "file_size", test_fsize }, 72 | { "getline", test_getline }, 73 | END_OF_TESTCASES 74 | }; 75 | -------------------------------------------------------------------------------- /lib/mk/amext-libusual.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Merge libusual sources with target sources 3 | # 4 | # Usage: 5 | # USUAL_DIR = 6 | # 7 | # _EMBED_LIBUSUAL = 1 8 | # 9 | # It adds module sources into _SOURCES 10 | # and -I$(USUAL_DIR) to _CPPFLAGS. 11 | # 12 | 13 | ## 14 | ## Utility functions for libusual link 15 | ## 16 | 17 | _USUAL_DIR = $(call JoinPath,$(srcdir),$(USUAL_DIR)) 18 | _USUAL_DIR2 = $(call JoinPath,$(_USUAL_DIR),usual) 19 | 20 | # module names from sources (plus headers) 21 | UsualMods = $(trace1)$(shell $(_USUAL_DIR)/find_modules.sh $(_USUAL_DIR) $(sort $(wildcard $(addprefix $(srcdir)/,$(1))))) 22 | 23 | # full-path sources based on module list 24 | UsualSrcsFull = $(trace1)$(sort $(wildcard $(addprefix $(_USUAL_DIR2)/,$(addsuffix *.[ch],$(1))))) 25 | 26 | # remove USUAL_DIR 27 | UsualStrip = $(trace1)$(subst $(_USUAL_DIR)/,,$(1)) 28 | 29 | # simple-path sources based on module list 30 | UsualSrcs = $(call UsualStrip,$(call UsualSrcsFull,$(1))) 31 | 32 | # usual sources from user source file list 33 | UsualSources = $(if $(1),$(call UsualSrcsFull,$(call UsualMods,$(1)))) 34 | 35 | # 1=cleantgt,2=rawtgt,3=prim,4=dest,5=flags 36 | define EmbedLibUsual 37 | $(trace5) 38 | 39 | # embed libusual objects directly 40 | $(IFEQ) ($$($(1)_EMBED_LIBUSUAL),1) 41 | 42 | $(1)_SOURCES := $$($(1)_SOURCES) $$(call UsualSources, $$($(1)_SOURCES)) 43 | EXTRA_$(1)_SOURCES := $$(EXTRA_$(1)_SOURCES) \ 44 | $$(call UsualSources, \ 45 | $$(EXTRA_$(1)_SOURCES) \ 46 | $$(nodist_$(1)_SOURCES) \ 47 | $$(nodist_EXTRA_$(1)_SOURCES)) 48 | 49 | $(1)_CPPFLAGS += -I$$(USUAL_DIR) 50 | 51 | $(ENDIF) 52 | 53 | endef 54 | 55 | AM_TARGET_HOOKS += EmbedLibUsual 56 | 57 | EXTRA_DIST += $(_USUAL_DIR)/find_modules.sh $(_USUAL_DIR)/usual/config.h.in 58 | -------------------------------------------------------------------------------- /lib/test/compile.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | static bool heap_is_better(const void *a, const void *b) 31 | { 32 | return 1; 33 | } 34 | 35 | int main(void) 36 | { 37 | struct AATree aatree; 38 | struct CBTree *cbtree; 39 | struct md5_ctx md5; 40 | struct Heap *heap; 41 | char buf[128]; 42 | 43 | static_assert(sizeof(int) >= 4, "unsupported int size"); 44 | 45 | heap = heap_create(heap_is_better, NULL, NULL); 46 | heap_top(heap); 47 | aatree_init(&aatree, NULL, NULL); 48 | cbtree = cbtree_create(NULL, NULL, NULL, NULL); 49 | cbtree_destroy(cbtree); 50 | daemonize(NULL, false); 51 | hash_lookup3("foo", 3); 52 | if (!parse_ini_file("foo", NULL, NULL)) 53 | log_debug("test"); 54 | log_stats("1"); 55 | file_size("foo"); 56 | md5_reset(&md5); 57 | strlcpy(buf, "foo", sizeof(buf)); 58 | printf("xmalloc: %p\n", xmalloc(128)); 59 | if (0) die("0"); 60 | csrandom(); 61 | tls_init(); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /lib/mk/temos/src/libusual5.temo: -------------------------------------------------------------------------------- 1 | 2 | . ./libtemo.sh || exit 1 3 | 4 | test_start 5 | 6 | title Use installed libusual with antimake. 7 | 8 | longmsg <<-MSG 9 | Install libusual and link against it. 10 | MSG 11 | 12 | title2 Build libusual 13 | 14 | usual_clone libusual 15 | 16 | run cd libusual 17 | 18 | runq ./autogen.sh 19 | runq './configure --disable-shared --prefix=`pwd`/../inst' 20 | runq make 21 | runq make install 22 | run cd .. 23 | 24 | title2 Build our own code 25 | 26 | msg Now we prepare our own code. 27 | 28 | msg First, this is the source file: 29 | cat_file prog.c <<"EOF" 30 | #include 31 | #include 32 | #include 33 | 34 | int main(void) 35 | { 36 | const char *data = "CECSFXX"; 37 | uint32_t crc; 38 | 39 | crc = calc_crc32(data, strlen(data), 0); 40 | printf("crc: %08x\n", crc); 41 | return 0; 42 | } 43 | EOF 44 | 45 | msg Here is corresponding Makefile: 46 | cat_file Makefile <<"EOF" 47 | # temo hacks to support local install, not needed otherwise 48 | PKG_CONFIG = PKG_CONFIG_PATH=$(CURDIR)/inst/lib/pkgconfig pkg-config 49 | 50 | # use pkg-config to get libusual info 51 | USUAL_CPPFLAGS = $(shell $(PKG_CONFIG) libusual --cflags) 52 | USUAL_LDFLAGS = $(shell $(PKG_CONFIG) libusual --libs-only-L) 53 | USUAL_LIBS = $(shell $(PKG_CONFIG) libusual --libs-only-l) 54 | 55 | # Generic Antimake 56 | bin_PROGRAMS = prog 57 | prog_SOURCES = prog.c 58 | prog_CPPFLAGS = $(USUAL_CPPFLAGS) 59 | prog_LDFLAGS = $(USUAL_LDFLAGS) 60 | prog_LDADD = $(USUAL_LIBS) 61 | 62 | # use installed Antimake 63 | ANTIMAKE = $(shell $(PKG_CONFIG) libusual --variable=antimake) 64 | include $(ANTIMAKE) 65 | EOF 66 | 67 | msg Build the project 68 | 69 | run make 70 | run ls 71 | run ./prog 72 | 73 | msg Done! 74 | -------------------------------------------------------------------------------- /lib/test/ssl/ca2_client2.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEpQIBAAKCAQEA0xDxUrBafdsSMJ67/r9ZYZCbJyuiDTtnzOD4rQWbELagQptF 3 | qpzIenv4er+92WJ5Bz4+5W3/NcYeBQt2Xizn860MyJ7Ii0Zz1Zq5asOHYP39AoeI 4 | wVmQpsxhn9nE29qg0/9PHfkVVMZZtkEteW/qeVQZZdrvA1gZJiHfUVgs6h8/MNHI 5 | Yxlw/WjhmHYKoIkcRHTKh6peQR2VISowSpXM8ekt8GA+Ij1/LH4nG+zdb9sDSa6C 6 | fvEd+agTDCj+rtB/OWutAO805RTrBrFmFgubb6J1mzxp6LyuEs8z9LnGVcwg5qV8 7 | vh46Q8xVNrgTR7GZspce/vVkSo9n2/3JFod5swIDAQABAoIBAQCeWhrD7/2kaQTH 8 | p3/iC4uykDxncev9w3EIHE1V+luxeWLZNgdZjpE2m+X7qfl0q9WjooaruQ/9Nyt0 9 | m3nuBADuKAnsRnytSWlEe/XabOIX2dbF3Au1DE5jZflkZYy1ady50NV55lOjq8DI 10 | 5TOJ5XZkGr2HVm918dXEbytNqzrfVr8/hWpcE0TQxpZvXxBKulgpz8JUVmcDZP8o 11 | r47AT4+uXFW1hftJAA7WDXGOpIkgUAoRXLQjVKtpoe37v5pgyl8IEwXBvni3ZHGA 12 | Kls+qhctAJHKFVs5zvy89jXeg6ZCi3qCILW/jAdwVFZ7beOvt5L8GPPn1aDq6WM0 13 | PptiO48JAoGBAO2oaIQsCxpoGI1LEY+7Efd+wt2oRaAVLWXuCK/sn8b53MRM/1Ha 14 | iReegy2db/VqYZsnPnWkXOMpOwVTFgysgfQe64rgs7Kj++emBO7rklDUjOCWUsZB 15 | 7ezJ88SddLWcTPz15H/99Gy1+HorgDfiIoQwCRsU55xkdC8jGqqqKgaPAoGBAONb 16 | JGPORXHZgcD6fdJvU2wUgbeepX/4YhG2KkcB4cJwzF3h8SEZuATJPrAE4OIk3RLN 17 | dTZlhYR1gKXVXlMqg1FVsCduvZYfhRtIaXiM/XRzQXK/E6Da1d1CcrzWG89YXBb5 18 | F/U3J5BwGB6VjHJrYONUxNG5crSgbPv+wS9BwUydAoGAEcTZq6gvXFYUH9xQ/9IY 19 | 0cxkfXYrXoC22Dwn4h9rAtGDtBmdrV0NivqLDEoNKOg/S/5vKRSU7VcT1p4Ts/bh 20 | 8IKvUb7jMKQaHYPIMqTiV7DMp64VmkUrqNygoT60jy+UggVQhFFUiRxB68B84bDC 21 | q+d7hs7VUlggZ7XUNJZsdfkCgYEAmsydtE7nvaLI0+o57SPbewVjZPBh4wXtT/3m 22 | Pz6qxkci/vEtysnGH4weYJ6Rt6g91eWKXyU0iYGvvDnQ5XNWwfRD4jVmZGbFrFPb 23 | bDcjAzzOXSB4btdRMjth/+0QNlcFLKchhtas241W/sv4Oped+mrnL5zC2vU3/0Pv 24 | qQtuX4kCgYEAptsF2pqurg6B6g9HZ9Fxft0LMNssULoxx8xuiZAZ9A1xW8fphgRq 25 | xKw1cTVb96AIyUaHMoBvKvO2danqUI7+nBexFsPNuRZ//P1L3UFyqyDhM/4hoLPm 26 | Ob7HcnE9Ejwaia7UoRZDOcBUFX0JQ6S0i6YNMaAKAGzqeV7/d25jtIE= 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /lib/test/ssl/ca2_root.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEpQIBAAKCAQEA1fhwyW8oSPMPxOVf/RV/Y4WsyIg4CH4gYxfEjMThgWkU7hZ8 3 | IYE3fpHQj5y+FLE0/z6oBybewT9zpTO5Pq6T9tBUGIfqk3/+FP6igMRZMNNtLCJe 4 | QAbWnOBCOOZ8VuiMTWLjUrkYg03kHsajl/GLl/5x1ITlQm9WjSZXhiOQ3t7NmUFO 5 | FFkqeJ0hXo48x3fOJTEBtoLjDtvovhxV2op0cKn77fkgwfNV5iJkZpAnBwKjhX61 6 | zx9wP74Q9Z6i/x8PMtPec0W5TpqAaI6zWF5a37s9yKFnmaEFryh2++rF8ruHZiz2 7 | z3ti0pwdER2G86PwpRHHrS7o0o2OAZz/83c8QwIDAQABAoIBAQC9HJZ6wl7lksX2 8 | LRCYe56alHPwzMxRXUA8tK4ICJ6SpRBVmdPePPmSSUf/Ffa+DR0dFwClSYqJXA5X 9 | DY8FQoEOHHSnNcFy2iXz4lY33tJBoCwWXM3WYvTdtCtzZOqyWVhfsS3fWC69JmOf 10 | e8rtKRRKox1ySn7iJ8tkd2XV7atF/iIWEmSXfyzjnOKcUM8xP0Hk7pxOvlvwN3pE 11 | fcpYdJiTgP3pW2eOuffKcUOIxiwfCxi7KylGGRRCzBv7wdKXqJiAKurK+mWJc4Gx 12 | uAA5KHfy4vdnejJHUJKepkfSiPkZ3cPlEbYuEoGOUMMnYkCSxNdnvQe4xZzXlDGp 13 | CYSn3bwxAoGBAOyufoTdVjr1WtA2OA3Kbg7RGbgmAaTnTNOYcneFbtxXzn1gF5XR 14 | k5bXrTlr5ozoqIVvSvbbKXMLE6+jsQVVOjYFzQ58tvXEEQoMr8eGpvSVApGxjiYk 15 | pNOGlT5xyUkmqdJhCkz0idWUkY9fn8JO1m55XO3yPUcQwYKX3MPYtG9pAoGBAOdv 16 | ZI0tv7ccJjzpA6qOtme8APiwAnmeVwIbfBzKPggwJqK1vZSQfLXiRQmy7aSmUCXU 17 | LEHOAfk/CpDyZwpC7IHE8RcDmEbvFYQtm2yjatrhhmW1od7RFSrUaFJQ23otT6ZT 18 | TsQk88Ed35+Ncni/zzoZ5vIA/5mFuG75SHRaE0TLAoGBAIE+2IAdjhUsHqdMtFdz 19 | j54vuWfB+WvfkyiJOxjhmKRinrQAYWDCzWG17TZKIeAYvM4A9GLRU1UrW1iyftx2 20 | qg8OI+ghWpfitZtTSjoe4dT1+i0tVH6nzApml+E9qbl0j4/+xe04LHyhYMt2YHtv 21 | KdGyHHkjqyq/j6yM2VVpZT7hAoGBAIBQ0n80DkeH4+uBhfbaiWeaZS985uJnV/Zj 22 | Jr0PpLUFfWZZPXNRaNOvUHfALOJNE8LlkGQrxAxdV45l15to35hTiRgpdhReGp/e 23 | aX4iXOxefhWGTukrI4FLo0uAaS+kT7T4G4PEuSEpXwMneYBZWJvuII2s4ApOiaN9 24 | sx5Pkr9hAoGAfiPN/VLmNaVgWkyp+atfFTlYHqbUsRLITn0MC1YlwWyqnYrGZpdZ 25 | yKCukg75rga3Sbd16jgg923UqSUpsB+aCFwx8t+TwMyKCkxseBayHxlTcKq0KG2g 26 | RsW9++WVTj3rDro8ZXhLi1WQUnRFurR4Vr5x4VZCy8dRnAz/NCCdEQ8= 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /lib/test/ssl/ca2_server2.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEpAIBAAKCAQEAx+hUJAjKYkcimZq7hPkXpr8npmeznk85Z1xZjuNQ+4kZDN96 3 | E9UgT634Jom9ViNrRox9v6TUCtUIxA/lBZbzSBZPB1I0woYAeX/hT9D9dvVUC5mh 4 | UMkTo4aaxN1d2L57zSlggg/AI7bdVgwL2Bnp6Xb7rWjO84R0Ouzw7Y9fmYnjF741 5 | cB538eYuM2ctfiGyV1MSI6NE3/uU6ThgKTOat6xT7cpjLJFm74+hx2YSPLLo7uos 6 | bXqELh8Trv7JfBB2Cg7AyyJPJiV5udtHDEx6RKOEpofotDOTULIUapja1Od3OrID 7 | ERYA0BDvdV45rXetJq2t2bwahtGmWC7AS6WtYwIDAQABAoIBAQCD3t6iZqoa6pII 8 | genxbeAMP00ImKAEbn6PkHwDpjBlvaqos0L0kkMeTMTFp1ce236Iq2TIZxL5kk6W 9 | B2YsqccSZqT7T18+cucdPx6CaHrLXkpy3IVtpTcvSDwQYXbm48EHNaRUJ8+3v4mC 10 | ma+2fmGvgK3FVr4uJdIGjQbhrxKBVSnd92KE1tEO16Vn1c5qItznkwwbDqN5me4+ 11 | UgZ+Upkpzpi1rV0vZlBi8fHFFUtb9QN7iFd5V4fAN7ZyUrBoT9t8CxROhB5emVx5 12 | EaJJrP4CTGuWlfErnkiFVyfe+iTOkSAWZxbTLbWSWOwC9RZ26QPasNNbmGBUc3rp 13 | jTU19FMBAoGBAOTAeAZ06BQGJa4NRjvSLNN9M15RtBiWwjizJKnaZjW9+9Y2J1WI 14 | fobuRLjZ5YGAUAaDzc7ejMQD8BRcw12XFcR3pBpW6Eo9WpSSMm6Ex9uYJOvLPzhN 15 | iSIBfQMyiUnWPGJN3BbDkwy9o07Yl1/ALDvD3Jhc6tNivVzG5FydMNpvAoGBAN+4 16 | SQXibcfwDwTLZEU8n7+GRm5iKq5qluvj1koDPKzQJTOaCmcZWMw+S1sNrG31CpX0 17 | zKrcJsfLECp3IOB1v1xruM/9djNRcIm2Qu2XJ7l9oQB9Scey8D6xvl5mSMUNghC2 18 | OGEP5pKsljAUhwhH36eO2GQNVAyzyMeYcW52sKZNAoGBAIZVGKcCBD+uFHaPyQzc 19 | MgLIZWAgLti5v7i4SvGMFbW9qaR0DMlH7LWkOqsrGoHgLh6FTFkNjDCX92adywSs 20 | 8tV7RN1K6AKg4Q6ahA8u3obwY84sfyW7MqrLoPQ9uy2nJ7K68mJT24kB3GMzAkHy 21 | godeT68R8bzNIVLYYcaZcNT1AoGAYMJ6ytHA+32ncUJqzstGqHZx1eIdZaxyM/op 22 | v38g6flobaYNI8RBtKyouOv0DOazumHTd36aQ0Vfa450qMAkkNuCZmrQiaeKjfVH 23 | gy0iIEbmsKh/laCd6e/ET13b0WZjP8kgH1LaPcvpdUgbkwhLAlR04Okm1guYJkOX 24 | o9gv1O0CgYBv64R9ugOtabWjjScG3C2UrwnA6f37rwGhvVLNSRinT77ueOBVzmQJ 25 | NEeBByoIU7Mzxco5g8MGyaIc6P+jeCzHjz/Xfdyrn9jLbowVYDXU8LK/0BY1i0wh 26 | dWYeOwbX7NbfTw99cqh7C96EY+6FQQKtEMYces1O5OdPCwangapWBg== 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /lib/usual/crypto/hmac.h: -------------------------------------------------------------------------------- 1 | /* 2 | * HMAC implementation based on OpenBSD 3 | * 4 | * Copyright (c) 2012 Daniel Farina 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * @file 21 | * HMAC-SHA1 implementation (RFC2104). 22 | */ 23 | 24 | #ifndef _USUAL_CRYPTO_HMAC_H_ 25 | #define _USUAL_CRYPTO_HMAC_H_ 26 | 27 | #include 28 | 29 | /** HMAC Context */ 30 | struct HMAC; 31 | 32 | /** Create context with key */ 33 | struct HMAC *hmac_new(const struct DigestInfo *impl, 34 | const void *key, unsigned int key_len, 35 | CxMem *cx); 36 | 37 | /** Free context */ 38 | void hmac_free(struct HMAC *ctx); 39 | 40 | /** Initialize context */ 41 | void hmac_reset(struct HMAC *ctx); 42 | 43 | /** Hash more data */ 44 | void hmac_update(struct HMAC *ctx, const void *data, unsigned int len); 45 | 46 | /** Get final result */ 47 | void hmac_final(struct HMAC *ctx, uint8_t *dst); 48 | 49 | unsigned hmac_block_len(struct HMAC *ctx); 50 | unsigned hmac_result_len(struct HMAC *ctx); 51 | 52 | #endif /* _USUAL_HMAC_H_ */ 53 | -------------------------------------------------------------------------------- /lib/usual/misc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009 Marko Kreen 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | /** @file 18 | * Random stuff that does not fit elsewhere. 19 | */ 20 | #ifndef _USUAL_MISC_H_ 21 | #define _USUAL_MISC_H_ 22 | 23 | #include 24 | 25 | #ifdef WORDS_BIGENDIAN 26 | #define FOURCC(a,b,c,d) \ 27 | ( ((unsigned int)(unsigned char)(a) << 24) \ 28 | | ((unsigned int)(unsigned char)(b) << 16) \ 29 | | ((unsigned int)(unsigned char)(c) << 8) \ 30 | | ((unsigned int)(unsigned char)(d))) 31 | #else 32 | /** Four-byte identifier as integer */ 33 | #define FOURCC(a,b,c,d) \ 34 | ( ((unsigned int)(unsigned char)(a)) \ 35 | | ((unsigned int)(unsigned char)(b) << 8) \ 36 | | ((unsigned int)(unsigned char)(c) << 16) \ 37 | | ((unsigned int)(unsigned char)(d) << 24)) 38 | #endif 39 | 40 | #if defined(__i386__) || defined(__x86_64__) 41 | #define mb() asm volatile("mfence":::"memory") 42 | #define rmb() asm volatile("lfence":::"memory") 43 | #define wmb() asm volatile("sfence":::"memory") 44 | #endif 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /bpf/mp_bouncer.bpf.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // clang-format off 4 | #include "vmlinux.h" // Needs to be included before bpf_helpers.h 5 | #include 6 | // clang-format on 7 | 8 | #include "mp_common.h" 9 | 10 | struct { 11 | __uint(type, BPF_MAP_TYPE_ARRAY); 12 | __uint(key_size, sizeof(u32)); 13 | __uint(value_size, sizeof(SocketState)); 14 | __uint(max_entries, 65536); 15 | } socket_states SEC(".maps"); 16 | 17 | struct { 18 | __uint(type, BPF_MAP_TYPE_SOCKMAP); 19 | __uint(key_size, sizeof(u32)); 20 | __uint(value_size, sizeof(u32)); 21 | __uint(max_entries, 65536); 22 | } client_sockets SEC(".maps"); 23 | 24 | struct { 25 | __uint(type, BPF_MAP_TYPE_SOCKMAP); 26 | __uint(key_size, sizeof(u32)); 27 | __uint(value_size, sizeof(u32)); 28 | __uint(max_entries, 65536); 29 | } server_sockets SEC(".maps"); 30 | 31 | struct { 32 | __uint(type, BPF_MAP_TYPE_SOCKMAP); 33 | __uint(key_size, sizeof(u32)); 34 | __uint(value_size, sizeof(u32)); 35 | __uint(max_entries, 65536); 36 | } mirror_udp_sockets SEC(".maps"); 37 | 38 | struct { 39 | __uint(type, BPF_MAP_TYPE_SOCKMAP); 40 | __uint(key_size, sizeof(u32)); 41 | __uint(value_size, sizeof(u32)); 42 | __uint(max_entries, 65536); 43 | } mirror_tcp_sockets SEC(".maps"); 44 | 45 | // TODO(Matt): idle_server_sockets size should be the same as bpf_pool_size in pgbouncer.ini. 46 | struct { 47 | __uint(type, BPF_MAP_TYPE_STACK); 48 | __uint(key_size, 0); 49 | __uint(value_size, sizeof(u32)); 50 | __uint(max_entries, 65536); 51 | } idle_server_sockets SEC(".maps"); 52 | 53 | enum { 54 | MAX_MESSAGES = 2048 // Arbitrary limit to make the verifier happy. I had to raise this from 255 to make pg_dump happy 55 | // since it sends a lot of stuff back in a single buffer sometimes. 56 | }; -------------------------------------------------------------------------------- /lib/usual/crypto/keccak_prng.c: -------------------------------------------------------------------------------- 1 | /* 2 | * PRNG based on Keccak. 3 | * 4 | * Copyright (c) 2014 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | #include 20 | 21 | bool keccak_prng_init(struct KeccakPRNG *prng, int capacity) 22 | { 23 | if (!keccak_init(&prng->ctx, capacity)) 24 | return false; 25 | prng->extracting = false; 26 | prng->have_data = false; 27 | return true; 28 | } 29 | 30 | void keccak_prng_add_data(struct KeccakPRNG *prng, const void *data, size_t len) 31 | { 32 | if (prng->extracting) { 33 | keccak_rewind(&prng->ctx); 34 | prng->extracting = false; 35 | } 36 | 37 | keccak_absorb(&prng->ctx, data, len); 38 | 39 | if (!prng->have_data && len > 0) 40 | prng->have_data = true; 41 | } 42 | 43 | bool keccak_prng_extract(struct KeccakPRNG *prng, void *data, size_t len) 44 | { 45 | if (!prng->have_data) 46 | return false; 47 | if (!prng->extracting) { 48 | keccak_pad(&prng->ctx, "\x01", 1); 49 | prng->extracting = true; 50 | } 51 | keccak_squeeze(&prng->ctx, data, len); 52 | return true; 53 | } 54 | -------------------------------------------------------------------------------- /lib/usual/hashing/memhash.c: -------------------------------------------------------------------------------- 1 | /* 2 | * memhash.h - Randomized in-memory hashing. 3 | * 4 | * Copyright (c) 2014 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | 27 | uint32_t memhash_seed(const void *data, size_t len, uint32_t seed) 28 | { 29 | if (sizeof(void *) == 8 || sizeof(long) == 8) { 30 | uint64_t hash[2]; 31 | hash[0] = seed; 32 | hash[1] = 0; 33 | spookyhash(data, len, &hash[0], &hash[1]); 34 | return hash[0]; 35 | } else { 36 | return xxhash(data, len, seed); 37 | } 38 | } 39 | 40 | uint32_t memhash(const void *data, size_t len) 41 | { 42 | static bool initialized; 43 | static uint32_t rand_seed; 44 | 45 | if (!initialized) { 46 | initialized = true; 47 | rand_seed = csrandom(); 48 | } 49 | return memhash_seed(data, len, rand_seed); 50 | } 51 | 52 | uint32_t memhash_string(const char *s) 53 | { 54 | return memhash(s, strlen(s)); 55 | } 56 | -------------------------------------------------------------------------------- /lib/usual/hashing/lookup3.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The contents of this file are public domain. 3 | * 4 | * Based on: lookup3.c, by Bob Jenkins, May 2006, Public Domain. 5 | */ 6 | 7 | /* 8 | * Compact version of Bob Jenkins' lookup3.c hash. 9 | */ 10 | 11 | #include 12 | 13 | #include 14 | 15 | #define rot(x, k) (((x)<<(k)) | ((x)>>(32-(k)))) 16 | 17 | #define mix(a, b, c) do { \ 18 | a -= c; a ^= rot(c, 4); c += b; \ 19 | b -= a; b ^= rot(a, 6); a += c; \ 20 | c -= b; c ^= rot(b, 8); b += a; \ 21 | a -= c; a ^= rot(c,16); c += b; \ 22 | b -= a; b ^= rot(a,19); a += c; \ 23 | c -= b; c ^= rot(b, 4); b += a; \ 24 | } while (0) 25 | 26 | #define final(a, b, c) do { \ 27 | c ^= b; c -= rot(b,14); \ 28 | a ^= c; a -= rot(c,11); \ 29 | b ^= a; b -= rot(a,25); \ 30 | c ^= b; c -= rot(b,16); \ 31 | a ^= c; a -= rot(c, 4); \ 32 | b ^= a; b -= rot(a,14); \ 33 | c ^= b; c -= rot(b,24); \ 34 | } while (0) 35 | 36 | /* variable length copy of ~6 bytes, avoid call to libc */ 37 | static inline void simple_memcpy(void *dst_, const void *src_, size_t len) 38 | { 39 | const uint8_t *src = src_; 40 | uint8_t *dst = dst_; 41 | while (len--) 42 | *dst++ = *src++; 43 | } 44 | 45 | uint64_t hash_lookup3(const void *data, size_t len) 46 | { 47 | uint32_t a, b, c; 48 | uint32_t buf[3]; 49 | const uint8_t *p = data; 50 | 51 | a = b = c = 0xdeadbeef + len; 52 | if (len == 0) 53 | goto done; 54 | 55 | while (len > 12) { 56 | memcpy(buf, p, 12); 57 | a += buf[0]; 58 | b += buf[1]; 59 | c += buf[2]; 60 | mix(a, b, c); 61 | p += 12; 62 | len -= 12; 63 | } 64 | 65 | buf[0] = buf[1] = buf[2] = 0; 66 | simple_memcpy(buf, p, len); 67 | a += buf[0]; 68 | b += buf[1]; 69 | c += buf[2]; 70 | final(a, b, c); 71 | done: 72 | return ((uint64_t)b << 32) | c; 73 | } 74 | -------------------------------------------------------------------------------- /lib/mk/temos/src/antimake1.temo: -------------------------------------------------------------------------------- 1 | 2 | . ./libtemo.sh || exit 1 3 | 4 | test_start 5 | 6 | title Test Antimake 7 | 8 | title2 Simplest usage 9 | 10 | msg Here we avoid use of any autotools. 11 | 12 | msg First, this is the source file: 13 | cat_file hello.c <<"EOF" 14 | #include 15 | 16 | int main(void) 17 | { 18 | printf("Hello, world\n"); 19 | return 0; 20 | } 21 | EOF 22 | 23 | msg Here is corresponding Makefile: 24 | 25 | cat_file Makefile <<"EOF" 26 | # This is target list - it's name describes target type 27 | # and how it is installed, it's value target files to be built. 28 | # bin - the targets will be installed under $(bindir) 29 | # PROGRAMS - the target is executable built from many sources 30 | bin_PROGRAMS = hello 31 | 32 | # The target 'hello'-s source file list. 33 | hello_SOURCES = hello.c 34 | 35 | # Run Antimake 36 | include antimake.mk 37 | EOF 38 | 39 | msg Also install Antimake and we are ready to build: 40 | run cp ../../antimake.mk . 41 | run ls 42 | 43 | msg Build the project 44 | 45 | run make 46 | run ls 47 | run ./hello 48 | 49 | msg We can even install it already: 50 | run make install prefix=/opt DESTDIR=./inst 51 | run ls ./inst/opt/bin 52 | 53 | msg For creating source package, we need to provide additional info: 54 | 55 | cat_file Makefile <<"EOF" 56 | # Package name and version for tarball filename 57 | PACKAGE_NAME = myhello 58 | PACKAGE_VERSION = 1.0 59 | 60 | # Non-source files to put into tarball 61 | EXTRA_DIST = Makefile antimake.mk 62 | 63 | bin_PROGRAMS = hello 64 | hello_SOURCES = hello.c 65 | include antimake.mk 66 | EOF 67 | 68 | msg Now we can create package that can be given to others. 69 | run make dist 70 | run ls 71 | run 'tar tzf myhello-1.0.tar.gz | sort' 72 | 73 | msg Clean the tree 74 | run make clean 75 | run ls 76 | 77 | msg Done! 78 | -------------------------------------------------------------------------------- /test/README.md: -------------------------------------------------------------------------------- 1 | Tests 2 | ===== 3 | 4 | Various ways to test PgBouncer: 5 | 6 | - `test.sh` 7 | 8 | General test of basic functionality and different configuration 9 | parameters including timeouts, pool size, online restart, 10 | pause/resume, etc. To invoke, just run `./test.sh`. This needs 11 | PostgreSQL server programs (`initdb`, `pg_ctl`) in the path, so if 12 | you are on a system that doesn't have those in the normal path 13 | (e.g., Debian, Ubuntu), set `PATH` beforehand. 14 | 15 | This test is run by `make check`. 16 | 17 | Optionally, this test suite can use `iptables`/`pfctl` to simulate 18 | various network conditions. To include these tests, set the 19 | environment variable USE_SUDO to a nonempty value, for example 20 | `make check USE_SUDO=1`. This will ask for sudo access, so it 21 | might convenient to run `sudo -v` before the test, or set up 22 | `/etc/sudoers` appropriately at your peril. Check the source if 23 | there are any doubts. 24 | 25 | - `ssl/test.sh` 26 | 27 | Tests SSL/TLS functionality. Otherwise very similar to `test.sh`. 28 | 29 | This test is run by `make check` if TLS support is enabled. 30 | 31 | - `hba_test` 32 | 33 | Tests hba parsing. Run `make all` to build and `./hba_test` to execute. 34 | 35 | This test is run by `make check`. 36 | 37 | - `run-conntest.sh` 38 | 39 | This is a more complex setup that continuously runs queries 40 | through PgBouncer while messing around with the network, checking 41 | whether PgBouncer correctly reconnects and all the queries get 42 | processed. First, run `make asynctest` to build, then see 43 | `run-conntest.sh` how to run the different pieces. 44 | 45 | - `stress.py` 46 | 47 | Stress test, see source for details. Requires Python and `psycopg2` module. 48 | -------------------------------------------------------------------------------- /lib/mk/temos/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # This Makefile does not use Antimake because it must work 3 | # even when Antimake is broken. 4 | # 5 | 6 | TEMOS = \ 7 | antimake1 antimake2 antimake3 antimake4 antimake5 antimake6 \ 8 | libusual1 libusual2 libusual3 libusual4 libusual5 libusual6 \ 9 | 10 | 11 | 12 | OUT = $(addsuffix .txt,$(addprefix output/, $(TEMOS))) 13 | HTML = $(addsuffix .html,$(addprefix html/, $(TEMOS))) \ 14 | html/index.html html/antimake.html 15 | 16 | ExpFile = $(subst .temo,.txt,$(subst src/,expected/,$(1))) 17 | OutFile = $(subst .temo,.txt,$(subst src/,output/,$(1))) 18 | 19 | V ?= 0 20 | ifeq ($(V),0) 21 | E = @echo 22 | Q = @ 23 | else 24 | E = @true 25 | Q = 26 | endif 27 | 28 | all: qtest 29 | 30 | .PHONY: all test ack clean html 31 | 32 | html: $(HTML) 33 | 34 | am: 35 | rm -f html/anti* 36 | $(MAKE) 37 | 38 | ftest: clean qtest 39 | qtest: $(OUT) 40 | @diff -urN expected output > regressions.diff || { \ 41 | cat regressions.diff; \ 42 | echo "FAIL: Result does not match expected output"; \ 43 | exit 1; \ 44 | } 45 | $(Q) rm -f regressions.diff 46 | $(E) "All OK" 47 | 48 | #../antimake.mk ../../m4/usual.m4 49 | output/%.txt: src/%.temo libtemo.sh 50 | @mkdir -p output 51 | @printf "$< ... " 52 | @bash $< > $@ 2>&1 && { cmp -s $@ $(call ExpFile,$<) && echo ok || echo failed; } \ 53 | || { echo "$< failed:" ; tail $(call OutFile,$<); exit 1; } 54 | 55 | html/%.html: output/%.txt 56 | @mkdir -p html 57 | asciidoc -o - $< | grep -v '^Last updated ' > $@ 58 | 59 | ack: 60 | cp output/*.txt expected/ 61 | 62 | clean: 63 | rm -rf tmp html output regressions.diff 64 | 65 | html/index.html: index.txt 66 | @mkdir -p html 67 | asciidoc -o - $< | grep -v '^Last updated ' > $@ 68 | 69 | html/antimake.html: ../antimake.txt 70 | @mkdir -p html 71 | asciidoc -a toc -o - $< | grep -v '^Last updated ' > $@ 72 | -------------------------------------------------------------------------------- /lib/test/ssl/ca2_complex2.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIE4zCCA8ugAwIBAgIUHiF3n8sA/Z8BRgPPcYVuekIjfrcwDQYJKoZIhvcNAQEL 3 | BQAwEjEQMA4GA1UEAwwHVGVzdENBMjAgFw0xMDAxMDEwODA1MDBaGA8yMDYwMTIz 4 | MTIzNTUwMFowYTEVMBMGA1UEAwwMY29tcGxleDIuY29tMRAwDgYDVQQHDAdLw7V6 5 | esOkMTYwNAYDVQQIDC3mp5jjgIXjgaroq5bkuonjgpLlvJXjgY3otbfjgZPjgZfj 6 | gabjgY3jgZ/jgIIwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQCzv5jY 7 | 7kTkrjtojvcU/ZKPpcq5bmIlSTnU4Dv5gVXtualrWXFy9rnK4UkljLQVDqBM/ZK+ 8 | 2rqLwdKInLt2EvQn+OXTZ+F3QkuQLCn+dCuMJWlTE4/sS7O9wEXDVAI5/nQ0QQmR 9 | tvi2ZA5Ou3HP1ct1mHbkt7g52uRiuLNZqRCAV1LjNXoNS6lVQOZYOrDisLaPU8dQ 10 | QQX7/P3YExWxFYjZ2Iy+c5CErqtB/Ls/wmAHQPm9Id2XsCOFKVQFJtqb1MqKP/6w 11 | 56VKUN2ljR+MAjkWQ/VZ1fDIhZGggbvEr/QdagCP5kAkm4AYG4WFlboDHK30FBCT 12 | n/Gx0RuH5+Yd4Dvq7faVbK5oIZqPqPEqiyFRAKBzfxZpaRS16tp5Fjuel2Txu6xY 13 | /xJeJBDY8IQ2mScNl+dbRQ5jer3Ex92VYyIo7bQ0v8t8gBTDZeP2TY4Qcv5J/uGN 14 | Vy4MtXXby+BlJsCa9Rtd7986SCD22Ua4pd5Si02ckl6+HbOhvxbC2kIRUFEDxXQQ 15 | 1iLamm7FGpA8Yh1SxxqAOSQo1v2reR0LSQQodu9MX23CTTXUKCyt+se46qIpFPxi 16 | krfN0n0Awq4ctF62KKppBbOfC9TiI5l94KZTrbIQO60MdmXkFqBafIwuyOnrlHux 17 | QrlvKNP2Ao62O3oncSdN846PwPbMYgXdocK4BwIDAQABo4HfMIHcMAwGA1UdEwEB 18 | /wQCMAAwDgYDVR0PAQH/BAQDAgWgMBMGA1UdJQQMMAoGCCsGAQUFBwMBMB0GA1Ud 19 | DgQWBBRdhtMTotqiitgkWRP0l+tq+2EWQjAfBgNVHSMEGDAWgBQf6RHAn6f+NF9J 20 | FKBgtXrNQYtPKjBnBgNVHREEYDBeggxjb21wbGV4Mi5jb22CEHd3dy5jb21wbGV4 21 | Mi5jb22HBH8AAAGHEP/+AAAAAAAAAAAAAAAAAAGGEWh0dHA6Ly9sb2NhbGhvc3Qv 22 | gRFmb294YUBleGFtcGxlLmNvbTANBgkqhkiG9w0BAQsFAAOCAQEAuceX/X8mzfb2 23 | LVeZ7YhrEG86ejxtTJW46yJdsa8gA2ZSlJO2/EpvGY4OF96eL6ZwEHnJbLNo4iyV 24 | rqM1rCrEpgWdgpixgeM0IynUrmhOySqICf9CiUX3AFuWQ1NgLrOV/igo0pqUeJmh 25 | UspLB4sw9svmdjyCpPbkh/RLrQp9JleBCIj4/8G+1DsJYUewls3/XB+t4LSUaAIa 26 | O7UuqEN3hBrDKrI2dUPXt1I66QsaP0N+NbwQNP0GXFAUS3AayYIYL9pqEyBR1o1R 27 | WvSNtAVjmbwl17P7tH7Atre+L/PyaMgEjUrZUrNCPKzMJxbc5YnH9cwj0Txdwan2 28 | u4+RkIlSxQ== 29 | -----END CERTIFICATE----- 30 | -------------------------------------------------------------------------------- /lib/mk/temos/src/antimake4.temo: -------------------------------------------------------------------------------- 1 | 2 | . ./libtemo.sh || exit 1 3 | 4 | test_start 5 | 6 | title Using Antimake with autoconf 7 | 8 | msg Autoconf setup 9 | 10 | cat_file autogen.sh <<"EOF" 11 | ../../std-autogen.sh ../../.. 12 | 13 | # fetch Antimake template from libusual 14 | cp ../../antimake.mk antimake.mk.in 15 | EOF 16 | 17 | cat_file configure.ac <<"EOF" 18 | AC_INIT([actest], [0.1]) 19 | AC_CONFIG_SRCDIR([prog.c]) 20 | AC_PREREQ([2.59]) 21 | 22 | AC_USUAL_INIT 23 | AC_USUAL_PROGRAM_CHECK 24 | 25 | AC_OUTPUT([antimake.mk]) 26 | EOF 27 | 28 | msg Here is the source we want to build: 29 | 30 | cat_file prog.c <<"EOF" 31 | #include 32 | #include 33 | 34 | int main(void) 35 | { 36 | printf("hello\n"); 37 | return 0; 38 | } 39 | EOF 40 | 41 | msg Antimake based Makefile 42 | 43 | cat_file Makefile <<"EOF" 44 | # the automake-style build description for 'prog' 45 | noinst_PROGRAMS = prog 46 | prog_SOURCES = prog.c 47 | 48 | EXTRA_DIST = Makefile $(MAINTAINERCLEANFILES) 49 | 50 | # clean configured files 51 | DISTCLEANFILES = config.status config.log antimake.mk 52 | 53 | # clean generated files 54 | MAINTAINERCLEANFILES = configure config.guess config.sub install-sh antimake.mk.in 55 | 56 | # launch Antimake 57 | include antimake.mk 58 | EOF 59 | 60 | msg Build the project 61 | runq sh ./autogen.sh 62 | runq ./configure 63 | run make 64 | run ls 65 | run ./prog 66 | 67 | msg Create distribution package 68 | run make dist 69 | run 'tar tzf actest-0.1.tar.gz | sort' 70 | 71 | msg Test the distribution package and separate build dir 72 | run mkdir -p test 73 | run cd test 74 | run tar xf ../actest-0.1.tar.gz 75 | run mkdir build 76 | run cd build 77 | runq ../actest-0.1/configure 78 | run make 79 | run ls 80 | run ./prog 81 | run cd ../.. 82 | 83 | msg Clean up 84 | run make maintainer-clean 85 | run ls 86 | 87 | msg Done 88 | -------------------------------------------------------------------------------- /lib/usual/safeio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | /** @file 18 | * 19 | * EINTR-safe I/O functions. 20 | */ 21 | #ifndef _USUAL_SAFEIO_H_ 22 | #define _USUAL_SAFEIO_H_ 23 | 24 | #include 25 | 26 | /** read */ 27 | ssize_t safe_read(int fd, void *buf, size_t len) _MUSTCHECK; 28 | /** write */ 29 | ssize_t safe_write(int fd, const void *buf, size_t len) _MUSTCHECK; 30 | /** recv */ 31 | ssize_t safe_recv(int fd, void *buf, size_t len, int flags) _MUSTCHECK; 32 | /** send */ 33 | ssize_t safe_send(int fd, const void *buf, size_t len, int flags) _MUSTCHECK; 34 | /** close */ 35 | int safe_close(int fd); 36 | /** recvmsg */ 37 | ssize_t safe_recvmsg(int fd, struct msghdr *msg, int flags) _MUSTCHECK; 38 | /** sendmsg */ 39 | ssize_t safe_sendmsg(int fd, const struct msghdr *msg, int flags) _MUSTCHECK; 40 | /** connect */ 41 | int safe_connect(int fd, const struct sockaddr *sa, socklen_t sa_len) _MUSTCHECK; 42 | /** accept */ 43 | int safe_accept(int fd, struct sockaddr *sa, socklen_t *sa_len) _MUSTCHECK; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /lib/test/test_socket.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include "test_common.h" 6 | 7 | static const char *ntop(int af, const void *src) 8 | { 9 | static char buf[128]; 10 | const char *res; 11 | res = inet_ntop(af, src, buf, sizeof(buf)); 12 | return res ? res : "NULL"; 13 | } 14 | 15 | static void test_ntop(void *z) 16 | { 17 | static const uint8_t data[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; 18 | str_check(ntop(AF_INET, data), "1.2.3.4"); 19 | str_check(ntop(AF_INET6, data), "102:304:506:708:90a:b0c:d0e:f10"); 20 | end:; 21 | } 22 | 23 | static const char *pton(int af, const char *s) 24 | { 25 | static char str[128]; 26 | unsigned char buf[128]; 27 | int res; 28 | int len = (af == AF_INET) ? 4 : 16; 29 | 30 | memset(buf, 0xCC, sizeof(buf)); 31 | 32 | res = inet_pton(af, s, buf); 33 | if (res < 0) return "EAFBAD"; 34 | if (res == 0) return "FAIL"; 35 | if (buf[len] != 0xCC || buf[len + 1] != 0xCC) 36 | return "EOVER"; 37 | if (buf[len - 1] == 0xCC || buf[0] == 0xCC) 38 | return "EUNDER"; 39 | 40 | s = inet_ntop(af, buf, str, sizeof(str)); 41 | return s ? s : "NULL"; 42 | } 43 | 44 | static void test_pton(void *z) 45 | { 46 | str_check(pton(AF_INET, "127.0.0.255"), "127.0.0.255"); 47 | str_check(pton(AF_INET, "127.0.0"), "FAIL"); 48 | str_check(pton(AF_INET, "127.1.1.a"), "FAIL"); 49 | str_check(pton(AF_INET, "127.1.1.300"), "FAIL"); 50 | 51 | str_check(pton(AF_INET6, "0001:0002:ffff:4444:5555:6666:7777:8888"), "1:2:ffff:4444:5555:6666:7777:8888"); 52 | str_check(pton(AF_INET6, "::"), "::"); 53 | str_check(pton(AF_INET6, "F00F::5060"), "f00f::5060"); 54 | str_check(pton(AF_INET6, "F00F::127.0.0.1"), "f00f::7f00:1"); 55 | str_check(pton(AF_INET6, "::1:2:3:4:5:6:7:8"), "FAIL"); 56 | end:; 57 | } 58 | 59 | struct testcase_t socket_tests[] = { 60 | { "inet_ntop", test_ntop }, 61 | { "inet_pton", test_pton }, 62 | END_OF_TESTCASES 63 | }; 64 | -------------------------------------------------------------------------------- /lib/test/test_common.c: -------------------------------------------------------------------------------- 1 | #include "test_common.h" 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | 8 | unsigned long long test_seed1, test_seed2; 9 | 10 | struct testgroup_t groups[] = { 11 | { "aatree/", aatree_tests }, 12 | { "base/", base_tests }, 13 | { "bits/", bits_tests }, 14 | { "cbtree/", cbtree_tests }, 15 | { "cfparser/", cfparser_tests }, 16 | { "crypto/", crypto_tests }, 17 | { "ctype/", ctype_tests }, 18 | { "cxalloc/", cxalloc_tests }, 19 | { "endian/", endian_tests }, 20 | { "fileutil/", fileutil_tests }, 21 | { "fnmatch/", fnmatch_tests }, 22 | { "getopt/", getopt_tests }, 23 | { "hashing/", hashing_tests }, 24 | { "hashtab/", hashtab_tests }, 25 | { "heap/", heap_tests }, 26 | { "json/", json_tests }, 27 | { "list/", list_tests }, 28 | { "mdict/", mdict_tests }, 29 | { "netdb/", netdb_tests }, 30 | { "pgutil/", pgutil_tests }, 31 | { "psrandom/", psrandom_tests }, 32 | { "regex/", regex_tests }, 33 | { "shlist/", shlist_tests }, 34 | { "socket/", socket_tests }, 35 | { "string/", string_tests }, 36 | { "strpool/", strpool_tests }, 37 | { "talloc/", talloc_tests }, 38 | { "time/", time_tests }, 39 | { "tls/", tls_tests }, 40 | { "utf8/", utf8_tests }, 41 | { "wchar/", wchar_tests }, 42 | END_OF_GROUPS 43 | }; 44 | 45 | const char *tdata(const char *fn) 46 | { 47 | static char buf[256]; 48 | const char *dir = getenv("LIBUSUAL_TEST_DATA_DIR"); 49 | snprintf(buf, sizeof buf, "%s/%s", dir ? dir : ".", fn); 50 | return buf; 51 | } 52 | 53 | int main(int argc, const char *argv[]) 54 | { 55 | if (getenv("USE_LOCALE")) 56 | setlocale(LC_ALL, ""); 57 | 58 | test_seed1 = pseudo_random(); 59 | test_seed2 = pseudo_random(); 60 | pseudo_random_seed(test_seed1, test_seed2); 61 | printf("inital seed: %" PRIu64 " %" PRIu64 "\n", (uint64_t)test_seed1, (uint64_t)test_seed2); 62 | 63 | return tinytest_main(argc, argv, groups); 64 | } 65 | -------------------------------------------------------------------------------- /config.mak.in: -------------------------------------------------------------------------------- 1 | PACKAGE_NAME = @PACKAGE_NAME@ 2 | PACKAGE_TARNAME = @PACKAGE_TARNAME@ 3 | PACKAGE_VERSION = @PACKAGE_VERSION@ 4 | PACKAGE_STRING = @PACKAGE_STRING@ 5 | PACKAGE_URL = @PACKAGE_URL@ 6 | PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ 7 | PORTNAME = @PORTNAME@ 8 | EXEEXT = @EXEEXT@ 9 | HAVE_CC_DEPFLAG = @HAVE_CC_DEPFLAG@ 10 | CC = @CC@ 11 | CPP = @CPP@ 12 | CPPFLAGS = @CPPFLAGS@ 13 | CFLAGS = @CFLAGS@ 14 | DEFS = @DEFS@ 15 | WFLAGS = @WFLAGS@ 16 | CXX = @CXX@ 17 | CXXFLAGS = @CXXFLAGS@ 18 | LD = @LD@ 19 | LDFLAGS = @LDFLAGS@ 20 | LIBS = @LIBS@ 21 | AR = @AR@ 22 | ARFLAGS = @ARFLAGS@ 23 | RANLIB = @RANLIB@ 24 | LIBTOOL = @LIBTOOL@ 25 | INSTALL = @INSTALL@ 26 | INSTALL_PROGRAM = @INSTALL_PROGRAM@ 27 | INSTALL_SCRIPT = @INSTALL_SCRIPT@ 28 | INSTALL_DATA = @INSTALL_DATA@ 29 | MKDIR_P = @MKDIR_P@ 30 | SED = @SED@ 31 | AWK = @AWK@ 32 | GREP = @GREP@ 33 | EGREP = @EGREP@ 34 | STRIP = @STRIP@ 35 | prefix = @prefix@ 36 | exec_prefix = @exec_prefix@ 37 | bindir = @bindir@ 38 | includedir = @includedir@ 39 | sbindir = @sbindir@ 40 | libexecdir = @libexecdir@ 41 | datarootdir = @datarootdir@ 42 | datadir = @datadir@ 43 | sysconfdir = @sysconfdir@ 44 | docdir = @docdir@ 45 | mandir = @mandir@ 46 | libdir = @libdir@ 47 | localedir = @localedir@ 48 | pkgdatadir = @pkgdatadir@ 49 | pkgconfigdir = @pkgconfigdir@ 50 | abs_top_srcdir ?= @abs_top_srcdir@ 51 | abs_top_builddir ?= @abs_top_builddir@ 52 | nosub_top_srcdir ?= @top_srcdir@ 53 | nosub_top_builddir ?= @top_builddir@ 54 | 55 | CARES_CFLAGS = @CARES_CFLAGS@ 56 | CARES_LIBS = @CARES_LIBS@ 57 | 58 | LIBEVENT_CFLAGS = @LIBEVENT_CFLAGS@ 59 | LIBEVENT_LIBS = @LIBEVENT_LIBS@ 60 | 61 | TLS_CPPFLAGS = @TLS_CPPFLAGS@ 62 | TLS_LDFLAGS = @TLS_LDFLAGS@ 63 | TLS_LIBS = @TLS_LIBS@ 64 | 65 | PANDOC = @PANDOC@ 66 | PYTHON = @PYTHON@ 67 | DLLWRAP = @DLLWRAP@ 68 | DLLTOOL = @DLLTOOL@ 69 | WINDRES = @WINDRES@ 70 | 71 | enable_debug = @enable_debug@ 72 | tls_support = @tls_support@ 73 | 74 | host_cpu = @host_cpu@ 75 | -------------------------------------------------------------------------------- /lib/usual/cxextra.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | /** 17 | * @file 18 | * 19 | * Extra allocators for cxalloc. 20 | */ 21 | 22 | #ifndef _USUAL_CXEXTRA_H_ 23 | #define _USUAL_CXEXTRA_H_ 24 | 25 | #include 26 | 27 | /** Allocator that exits on error. .ctx should be pointer to actual allocator */ 28 | extern const struct CxOps cx_nofail_ops; 29 | /** nofail for libc */ 30 | extern CxMem cx_libc_nofail; 31 | 32 | /** 33 | * Creates allocator that pools all memory together, 34 | * without keeping track of single objects, to be 35 | * freed all together in one shot. 36 | * 37 | * realloc(), free() are partially supported for the last 38 | * objec only. 39 | */ 40 | CxMem *cx_new_pool(CxMem *parent, size_t initial_size, unsigned int align); 41 | 42 | CxMem *cx_new_pool_from_area(CxMem *parent, void *buf, size_t size, bool allow_free, unsigned int align); 43 | 44 | /** 45 | * Creates allocator that remebers all allocations done 46 | * under it and allows all of it to be freed together. 47 | * 48 | * Supports hierarchical trees. 49 | */ 50 | CxMem *cx_new_tree(CxMem *parent); 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /lib/mk/temos/src/libusual4.temo: -------------------------------------------------------------------------------- 1 | 2 | . ./libtemo.sh || exit 1 3 | 4 | test_start 5 | 6 | title Use installed libusual 7 | 8 | longmsg <<-MSG 9 | Install libusual and link against it. 10 | MSG 11 | 12 | title2 Build libusual 13 | 14 | usual_clone libusual 15 | 16 | run cd libusual 17 | 18 | runq ./autogen.sh 19 | runq './configure --disable-static --prefix=`pwd`/../inst' 20 | runq make 21 | runq make install 22 | run cd .. 23 | run 'find inst | sort' 24 | 25 | title2 Build our own code 26 | 27 | msg Now we prepare our own code. 28 | 29 | msg First, this is the source file: 30 | cat_file prog.c <<"EOF" 31 | #include 32 | #include 33 | #include 34 | 35 | int main(void) 36 | { 37 | const char *data = "CECSFXX"; 38 | uint32_t crc; 39 | 40 | crc = calc_crc32(data, strlen(data), 0); 41 | printf("crc: %08x\n", crc); 42 | return 0; 43 | } 44 | EOF 45 | 46 | msg Here is corresponding Makefile: 47 | cat_file Makefile <<"EOF" 48 | 49 | CC = gcc 50 | CFLAGS = -O -g -Wall 51 | 52 | # here we describe our program 53 | SRCS = prog.c 54 | OBJS = $(SRCS:.c=.o) 55 | 56 | # put libusual flags to proper place 57 | CPPFLAGS = $(USUAL_CPPFLAGS) 58 | LIBS = $(USUAL_LIBS) 59 | 60 | # use pkg-config to get libusual info 61 | USUAL_CPPFLAGS = $(shell $(PKG_CONFIG) --cflags libusual) 62 | USUAL_LIBS = $(shell $(PKG_CONFIG) --libs libusual) 63 | 64 | # temo hacks to support local install, not needed otherwise 65 | PKG_CONFIG := PKG_CONFIG_PATH=$(CURDIR)/inst/lib/pkgconfig pkg-config 66 | CPPFLAGS := $(subst $(CURDIR)/libusual/../,./,$(CPPFLAGS)) 67 | LIBS := $(subst $(CURDIR)/libusual/../,./,$(LIBS)) 68 | 69 | all: prog 70 | 71 | %.o: %.c 72 | $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $< 73 | 74 | prog: $(OBJS) 75 | $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@ 76 | EOF 77 | 78 | msg Build the project 79 | 80 | run make 81 | run ls 82 | run LD_LIBRARY_PATH=./inst/lib ./prog 83 | 84 | msg Done! 85 | -------------------------------------------------------------------------------- /test/hba_test.eval: -------------------------------------------------------------------------------- 1 | # peer 2 | md5 db user unix 3 | peer dbp user unix 4 | password db userp unix 5 | trust dbz userz unix 6 | scram-sha-256 dbs users unix 7 | 8 | # hostssl 9 | cert db user 10.1.1.1 tls 10 | reject db user 10.1.1.1 11 | reject db user 13.1.1.1 12 | 13 | # hostnossl 14 | reject db user 11.1.1.1 tls 15 | md5 db user 11.1.1.1 16 | reject db user 13.1.1.1 17 | 18 | # host 19 | password db user 127.0.0.2 tls 20 | password db user 127.0.0.3 21 | reject db user 127.0.1.4 22 | 23 | # db1 filt 24 | reject db1x user 127.0.1.4 25 | md5 db1 user 127.0.1.4 26 | 27 | # user1 filt 28 | md5 db1z user1 15.0.0.1 29 | reject db1z user2 15.0.0.1 30 | 31 | # someusers 32 | reject db2 user 16.0.0.1 33 | md5 db2 user1 16.0.0.1 34 | md5 db2 user2 16.0.0.1 35 | md5 db2 user3 16.0.0.1 36 | reject db2 user4 16.0.0.1 37 | 38 | # manyusers 39 | md5 db2 u1 17.0.0.1 40 | md5 db2 u2 17.0.0.1 41 | md5 db2 u3 17.0.0.1 42 | md5 db2 u4 17.0.0.1 43 | md5 db2 u5 17.0.0.1 44 | md5 db2 u6 17.0.0.1 45 | md5 db2 u7 17.0.0.1 46 | md5 db2 u8 17.0.0.1 47 | md5 db2 u9 17.0.0.1 48 | md5 db2 u10 17.0.0.1 49 | md5 db2 u11 17.0.0.1 50 | 51 | # manydbs 52 | reject d1 user 18.0.0.2 53 | trust d1 t18user 18.0.0.2 54 | trust d2 t18user 18.0.0.2 55 | trust d3 t18user 18.0.0.2 56 | trust d4 t18user 18.0.0.2 57 | trust d5 t18user 18.0.0.2 58 | trust d6 t18user 18.0.0.2 59 | trust d7 t18user 18.0.0.2 60 | trust d8 t18user 18.0.0.2 61 | trust d9 t18user 18.0.0.2 62 | trust d10 t18user 18.0.0.2 63 | trust d11 t18user 18.0.0.2 64 | 65 | # quoting 66 | reject db t19user 19.0.0.2 67 | cert all all 19.0.0.2 68 | cert q1"q2 a , b 19.0.0.2 69 | 70 | # bitmask 71 | cert mdb muser 199.199.199.199 72 | reject mdb muser 199.199.199.198 73 | reject mdb muser 199.199.199.200 74 | 75 | cert mdb2 muser 254.1.1.1 76 | 77 | # ipv6 78 | md5 mdb muser ff11:2::1 79 | md5 mdb muser ff22:3::1 80 | trust mdb muser ::1 81 | reject mdb muser ::2 82 | -------------------------------------------------------------------------------- /lib/usual/crypto/keccak_prng.h: -------------------------------------------------------------------------------- 1 | /* 2 | * PRNG based on Keccak. 3 | * 4 | * Copyright (c) 2014 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * @file 21 | * 22 | * Implements PRNG mode for Keccak sponge function. 23 | */ 24 | 25 | #ifndef _USUAL_CRYPTO_KECCAK_PRNG_H_ 26 | #define _USUAL_CRYPTO_KECCAK_PRNG_H_ 27 | 28 | #include 29 | 30 | /** 31 | * State structure. 32 | */ 33 | struct KeccakPRNG { 34 | struct KeccakContext ctx; 35 | bool extracting; 36 | bool have_data; 37 | }; 38 | 39 | /** 40 | * Setup Keccak with specified capacity. 41 | * 42 | * @param prng State structure to be initialized. 43 | * @param capacity Keccak capacity in bits. 44 | * @return False if invalid capacity, true otherwise. 45 | */ 46 | bool keccak_prng_init(struct KeccakPRNG *prng, int capacity); 47 | 48 | /** 49 | * Merge entropy data into state. 50 | */ 51 | void keccak_prng_add_data(struct KeccakPRNG *prng, const void *data, size_t len); 52 | 53 | /** 54 | * Extract PRNG bytes from state. 55 | * 56 | * @return True, if extraction was successful. False if state has not been initialzed with keccak_prng_add_data(). 57 | */ 58 | bool keccak_prng_extract(struct KeccakPRNG *prng, void *data, size_t len); 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /lib/usual/fnmatch.h: -------------------------------------------------------------------------------- 1 | /* 2 | * fnmatch.h 3 | * 4 | * Copyright (c) 2012 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * \file 21 | * Theme include for strings. 22 | */ 23 | 24 | #ifndef _USUAL_FNMATCH_H_ 25 | #define _USUAL_FNMATCH_H_ 26 | 27 | #include 28 | 29 | #if defined(HAVE_FNMATCH_H) && defined(FNM_CASEFOLD) 30 | #include 31 | #else 32 | /* fnmatch missing or incomplete */ 33 | #define NEED_USUAL_FNMATCH 34 | #endif 35 | 36 | #ifdef NEED_USUAL_FNMATCH 37 | #define fnmatch(p,s,f) usual_fnmatch(p,s,f) 38 | 39 | /** Do not allow wildcard to match '/' */ 40 | #define FNM_PATHNAME 1 41 | /** Treat '\\' as literal value */ 42 | #define FNM_NOESCAPE 2 43 | /** Do not allow wildcard to match leading '.' */ 44 | #define FNM_PERIOD 4 45 | /** (GNU) Match case-insensitively */ 46 | #define FNM_CASEFOLD 8 47 | /** (GNU) Match leading directory in path */ 48 | #define FNM_LEADING_DIR 16 49 | 50 | /* (GNU) random alias */ 51 | #define FNM_FILE_NAME FNM_PATHNAME 52 | 53 | /** Returned on no match */ 54 | #define FNM_NOMATCH 1 55 | 56 | /** 57 | * Compat: fnmatch() 58 | */ 59 | int fnmatch(const char *pat, const char *str, int flags); 60 | 61 | #endif /* NEED_USUAL_FNMATCH */ 62 | 63 | #endif /* !_USUAL_FNMATCH_H_ */ 64 | -------------------------------------------------------------------------------- /lib/usual/base.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Basic C environment. 3 | * 4 | * Copyright (c) 2007-2009 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | #if defined(HAVE_MALLOC_H) && defined(__darwin__) 23 | #include 24 | #endif 25 | 26 | /* define posix_memalign() only when possible to emulate */ 27 | #if !defined(HAVE_POSIX_MEMALIGN) \ 28 | && (defined(HAVE_MEMALIGN) || defined(HAVE_VALLOC)) 29 | 30 | int posix_memalign(void **ptr_p, size_t align, size_t len) 31 | { 32 | void *p; 33 | int ret, old_errno = errno; 34 | 35 | #ifdef HAVE_MEMALIGN 36 | p = memalign(align, len); 37 | #else /* !HAVE_MEMALIGN */ 38 | #ifdef HAVE_VALLOC 39 | /* assuming less than pagesize alignment */ 40 | p = valloc(len); 41 | #endif /* !VALLOC */ 42 | #endif /* !MEMALIGN */ 43 | 44 | *ptr_p = p; 45 | if (p) 46 | return 0; 47 | 48 | /* on error restore old errno */ 49 | ret = errno; 50 | errno = old_errno; 51 | return ret; 52 | } 53 | #endif 54 | 55 | #ifndef HAVE_REALLOCARRAY 56 | 57 | void *reallocarray(void *p, size_t count, size_t size) 58 | { 59 | size_t total; 60 | if (!safe_mul_size(&total, count, size)) { 61 | errno = ENOMEM; 62 | return NULL; 63 | } 64 | return realloc(p, total); 65 | } 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /lib/usual/fileutil.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | /** 18 | * @file 19 | * 20 | * File access utils. 21 | */ 22 | #ifndef _USUAL_FILEUTIL_H_ 23 | #define _USUAL_FILEUTIL_H_ 24 | 25 | #include 26 | 27 | #include 28 | 29 | /** Info about mapped file */ 30 | struct MappedFile { 31 | int fd; 32 | unsigned len; 33 | void *ptr; 34 | }; 35 | 36 | /** Signature for per-line callback */ 37 | typedef bool (*procline_cb)(void *arg, const char *line, ssize_t len); 38 | 39 | /** Read file into memory */ 40 | void *load_file(const char *fn, size_t *len_p); 41 | 42 | /** Loop over lines in file */ 43 | bool foreach_line(const char *fn, procline_cb proc_line, void *arg); 44 | 45 | /** Get file size */ 46 | ssize_t file_size(const char *fn); 47 | 48 | /** Map file into memory */ 49 | int map_file(struct MappedFile *m, const char *fname, int rw) _MUSTCHECK; 50 | 51 | /** Unmap previously mapped file */ 52 | void unmap_file(struct MappedFile *m); 53 | 54 | #if !defined(HAVE_GETLINE) 55 | #define getline(a,b,c) compat_getline(a,b,c) 56 | 57 | /** 58 | * Compat: Read line from file 59 | */ 60 | int getline(char **line_p, size_t *size_p, void *f); 61 | 62 | #endif 63 | 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /lib/mk/temos/src/libusual2.temo: -------------------------------------------------------------------------------- 1 | 2 | . ./libtemo.sh || exit 1 3 | 4 | test_start 5 | 6 | title Embedding libusual as part of the source. 7 | 8 | longmsg <<-"MSG" 9 | Here we build libusual as part of top-level tree. 10 | This gives the advantage of building only the modules 11 | that are actually used in main tree and without the 12 | intermediate `libusual.a` step. 13 | 14 | This method is for projects that are developed 15 | in parallel with libusual. Not recommended for 16 | casual usage. 17 | MSG 18 | 19 | title2 Configure libusual 20 | 21 | msg Here we configure libusual, but do not build it. 22 | 23 | usual_clone lib 24 | 25 | run cd lib 26 | 27 | runq ./autogen.sh 28 | runq ./configure 29 | 30 | run cd .. 31 | 32 | title2 Prepare own code 33 | 34 | msg Here is the source that needs to be linked with libusual: 35 | 36 | cat_file prog.c <<"EOF" 37 | #include 38 | #include 39 | #include 40 | 41 | int main(void) 42 | { 43 | const char *data = "CECSFXX"; 44 | uint32_t crc; 45 | 46 | crc = calc_crc32(data, strlen(data), 0); 47 | printf("crc: %08x\n", crc); 48 | return 0; 49 | } 50 | EOF 51 | 52 | title2 Build with Antimake. 53 | 54 | longmsg <<-"MSG" 55 | Antimake is build framework on plain GNU Make that reads 56 | build instructons with Automake syntax. It has also hooks 57 | for libusual integration. 58 | 59 | Here is Makefile that uses Antimake: 60 | MSG 61 | 62 | cat_file Makefile <<"EOF" 63 | # the automake-style build description for 'prog' 64 | noinst_PROGRAMS = prog 65 | prog_SOURCES = prog.c 66 | 67 | # location of configured libusual 68 | USUAL_DIR = lib 69 | 70 | # mention that 'prog' wants embedded libusual 71 | prog_EMBED_LIBUSUAL = 1 72 | 73 | # Load Antimake plugin that handles libusual embedding 74 | AM_FEATURES = libusual 75 | 76 | # launch Antimake 77 | include $(USUAL_DIR)/mk/antimake.mk 78 | EOF 79 | 80 | msg Build the project 81 | run make 82 | run ls 83 | run ./prog 84 | run make clean 85 | run ls 86 | 87 | msg Done 88 | -------------------------------------------------------------------------------- /lib/usual/crypto/chacha.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ChaCha cipher. 3 | * 4 | * Copyright (c) 2014 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * @file 21 | * ChaCha cipher. 22 | */ 23 | 24 | #ifndef _CHACHA_CLEAN_H_ 25 | #define _CHACHA_CLEAN_H_ 26 | 27 | #include 28 | 29 | #define CHACHA_KEY_SIZE 32 30 | #define CHACHA_IV_SIZE 8 31 | #define CHACHA_BLOCK_SIZE 64 32 | 33 | /** 34 | * ChaCha state. 35 | */ 36 | struct ChaCha { 37 | uint32_t state[16]; 38 | union { 39 | uint32_t output32[16]; 40 | uint8_t output8[16*4]; 41 | } u; 42 | unsigned int pos; 43 | }; 44 | 45 | /** 46 | * Set 256-bit key. 47 | */ 48 | void chacha_set_key_256(struct ChaCha *ctx, const void *key); 49 | 50 | /** 51 | * Set 128-bit key. 52 | */ 53 | void chacha_set_key_128(struct ChaCha *ctx, const void *key); 54 | 55 | /** 56 | * Set 2x32-bit counter and 8-byte IV. 57 | */ 58 | void chacha_set_nonce(struct ChaCha *ctx, uint32_t counter_low, uint32_t counter_high, const void *iv); 59 | 60 | /** 61 | * Extract plain keystream. 62 | */ 63 | void chacha_keystream(struct ChaCha *ctx, void *stream, size_t bytes); 64 | 65 | /** 66 | * XOR data with keystream. 67 | */ 68 | void chacha_keystream_xor(struct ChaCha *ctx, const void *plain, void *encrypted, size_t bytes); 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /include/dnslookup.h: -------------------------------------------------------------------------------- 1 | /* 2 | * PgBouncer - Lightweight connection pooler for PostgreSQL. 3 | * 4 | * Copyright (c) 2007-2010 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | struct DNSContext; 20 | struct DNSToken; 21 | struct addrinfo; 22 | 23 | typedef void (*adns_callback_f)(void *arg, const struct sockaddr *sa, int salen); 24 | 25 | struct DNSContext *adns_create_context(void); 26 | void adns_reload(struct DNSContext *ctx); 27 | void adns_free_context(struct DNSContext *ctx); 28 | 29 | struct DNSToken *adns_resolve(struct DNSContext *ctx, const char *name, adns_callback_f cb_func, void *arg); 30 | 31 | void adns_cancel(struct DNSContext *ctx, struct DNSToken *tk); 32 | 33 | const char *adns_get_backend(void); 34 | 35 | void adns_zone_cache_maint(struct DNSContext *ctx); 36 | 37 | void adns_info(struct DNSContext *ctx, int *names, int *zones, int *queries, int *pending); 38 | 39 | typedef void (*adns_walk_name_f)(void *arg, const char *name, const struct addrinfo *ai, usec_t ttl); 40 | typedef void (*adns_walk_zone_f)(void *arg, const char *name, uint32_t serial, int nhosts); 41 | 42 | void adns_walk_names(struct DNSContext *ctx, adns_walk_name_f cb, void *arg); 43 | void adns_walk_zones(struct DNSContext *ctx, adns_walk_zone_f cb, void *arg); 44 | 45 | void adns_per_loop(struct DNSContext *ctx); 46 | -------------------------------------------------------------------------------- /lib/test/test_hashing.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include "test_common.h" 7 | 8 | static uint32_t xcrc32(const char *s) 9 | { 10 | return calc_crc32(s, strlen(s), 0); 11 | } 12 | 13 | static uint32_t xlookup3(const char *s) 14 | { 15 | return hash_lookup3(s, strlen(s)); 16 | } 17 | 18 | static void test_crc32(void *p) 19 | { 20 | int_check(xcrc32(""), 0); 21 | int_check(xcrc32("a"), 3904355907); 22 | int_check(xcrc32("abc"), 891568578); 23 | int_check(xcrc32("message digest"), 538287487); 24 | int_check(xcrc32("abcdefghijklmnopqrstuvwxyz"), 1277644989); 25 | int_check(xcrc32("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 532866770); 26 | int_check(xcrc32("12345678901234567890123456789012345678901234567890123456789012345678901234567890"), 2091469426); 27 | end:; 28 | } 29 | 30 | static void test_lookup3(void *p) 31 | { 32 | #ifdef WORDS_BIGENDIAN 33 | int_check(xlookup3(""), 3735928559); 34 | int_check(xlookup3("a"), -454251968); 35 | int_check(xlookup3("abc"), -1186250080); 36 | int_check(xlookup3("message digest"), 670730672); 37 | int_check(xlookup3("abcdefghijklmnopqrstuvwxyz"), 251682059); 38 | int_check(xlookup3("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 567386262); 39 | int_check(xlookup3("12345678901234567890123456789012345678901234567890123456789012345678901234567890"), 312582506); 40 | #else 41 | int_check(xlookup3(""), 3735928559); 42 | int_check(xlookup3("a"), 1490454280); 43 | int_check(xlookup3("abc"), 238646833); 44 | int_check(xlookup3("message digest"), 2512672053); 45 | int_check(xlookup3("abcdefghijklmnopqrstuvwxyz"), 1966650813); 46 | int_check(xlookup3("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 3992286962); 47 | int_check(xlookup3("12345678901234567890123456789012345678901234567890123456789012345678901234567890"), 2776963519); 48 | #endif 49 | end:; 50 | } 51 | 52 | struct testcase_t hashing_tests[] = { 53 | { "crc32", test_crc32 }, 54 | { "lookup3", test_lookup3 }, 55 | END_OF_TESTCASES 56 | }; 57 | -------------------------------------------------------------------------------- /lib/usual/psrandom.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Pseudo-random bytes. 3 | * 4 | * Copyright (c) 2015 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * Pseudo-random number generator for non-cryptographic purposes. 21 | * 22 | * By default it's seeded with csrandom so returns unpredictable 23 | * values (but not cryptographically stong). But when 24 | * pseudo_random_seed() is used, all following values 25 | * are determined completely by seed. 26 | */ 27 | 28 | #ifndef _USUAL_PSRANDOM_H_ 29 | #define _USUAL_PSRANDOM_H_ 30 | 31 | #include 32 | 33 | /** 34 | * Return value with uniform probability over whole 32-bit range. 35 | */ 36 | uint32_t pseudo_random(void); 37 | 38 | /** 39 | * Return with with uniform probability over specific range. 40 | */ 41 | uint32_t pseudo_random_range(uint32_t upper_bound); 42 | 43 | /** 44 | * Fill buffer with random bytes. 45 | */ 46 | void pseudo_random_bytes(void *dst, size_t count); 47 | 48 | /** 49 | * Set 128-bit seed. Following values will be 50 | * fully deterministic based on seed. 51 | */ 52 | void pseudo_random_seed(uint64_t a, uint64_t b); 53 | 54 | /* 128-bit state. Period: 2**128 - 1 */ 55 | uint64_t xorshift128plus(uint64_t *s0, uint64_t *s1); 56 | 57 | /* 1024-bit state. Period: 2**1024 - 1 */ 58 | uint64_t xorshift1024plus(uint64_t state[16], unsigned int counter); 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /lib/usual/hashing/xxhash.h: -------------------------------------------------------------------------------- 1 | /* 2 | xxHash - Fast Hash algorithm 3 | Header File 4 | Copyright (C) 2012-2014, Yann Collet. 5 | BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are 9 | met: 10 | 11 | * Redistributions of source code must retain the above copyright 12 | notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above 14 | copyright notice, this list of conditions and the following disclaimer 15 | in the documentation and/or other materials provided with the 16 | distribution. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | 30 | You can contact the author at : 31 | - xxHash source repository : http://code.google.com/p/xxhash/ 32 | */ 33 | 34 | /** 35 | * @file 36 | * 37 | * xxHash - fast hash for 32-bit cpu's. 38 | */ 39 | 40 | #ifndef _USUAL_HASHING_XXHASH_H_ 41 | #define _USUAL_HASHING_XXHASH_H_ 42 | 43 | #include 44 | 45 | /** 46 | * Calculate the 32-bits hash of sequence of length "len" stored at memory address "input". 47 | */ 48 | uint32_t xxhash(const void *input, size_t len, uint32_t seed); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /lib/mk/temos/expected/libusual1.txt: -------------------------------------------------------------------------------- 1 | 2 | = Use libusual the simplest way = 3 | 4 | 5 | Simplest usage would be to configure and build libusual 6 | locally and point your projects CPPFLAGS and LDFLAGS there. 7 | 8 | That way you get access to not only code but also 9 | various autoconfigued symbols without any complexities 10 | in your project. 11 | 12 | 13 | == Build libusual == 14 | 15 | --------------------------------- 16 | $ git clone git://github.com/libusual/libusual.git lib 17 | Cloning into 'lib'... 18 | $ cd lib 19 | $ ./autogen.sh 20 | [...] 21 | $ ./configure --disable-shared --prefix=/opt 22 | [...] 23 | $ make 24 | [...] 25 | $ make install DESTDIR=`pwd`/../inst 26 | [...] 27 | $ cd .. 28 | --------------------------------- 29 | 30 | == Build our own code == 31 | 32 | 33 | Now we prepare our own code. 34 | 35 | 36 | First, this is the source file: 37 | 38 | .File: prog.c 39 | [source,c] 40 | ----------------------------------- 41 | #include 42 | #include 43 | #include 44 | 45 | int main(void) 46 | { 47 | const char *data = "CECSFXX"; 48 | uint32_t crc; 49 | 50 | crc = calc_crc32(data, strlen(data), 0); 51 | printf("crc: %08x\n", crc); 52 | return 0; 53 | } 54 | ----------------------------------- 55 | 56 | Here is corresponding Makefile: 57 | 58 | .File: Makefile 59 | [source,makefile] 60 | ----------------------------------- 61 | # here we describe our program 62 | SRCS = prog.c 63 | OBJS = $(SRCS:.c=.o) 64 | 65 | # here we link to libusual 66 | CPPFLAGS = -I./inst/opt/include 67 | LDFLAGS = -L./inst/opt/lib 68 | LIBS = -lusual 69 | 70 | CC = gcc 71 | CFLAGS = -O -g -Wall 72 | 73 | all: prog 74 | 75 | prog: $(OBJS) 76 | $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@ 77 | ----------------------------------- 78 | 79 | Build the project 80 | 81 | --------------------------------- 82 | $ make 83 | gcc -O -g -Wall -I./inst/opt/include -c -o prog.o prog.c 84 | gcc -O -g -Wall -L./inst/opt/lib prog.o -lusual -o prog 85 | $ ls 86 | Makefile inst lib prog prog.c prog.o 87 | $ ./prog 88 | crc: 12345678 89 | --------------------------------- 90 | 91 | Done! 92 | 93 | -------------------------------------------------------------------------------- /lib/usual/strpool.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Pool for shared strings. 3 | * 4 | * Copyright (c) 2010 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * @file 21 | * 22 | * Storage for shared strings. 23 | * 24 | * This provides refcounted searchable string pool for cases 25 | * where lot of objects reference same strings. 26 | */ 27 | 28 | #ifndef _USUAL_STRPOOL_H_ 29 | #define _USUAL_STRPOOL_H_ 30 | 31 | #include 32 | 33 | /** Handle for the pool */ 34 | struct StrPool; 35 | 36 | /** Pooled String */ 37 | struct PStr { 38 | /** Parent pool */ 39 | struct StrPool *pool; 40 | /** String length */ 41 | size_t len; 42 | /** Reference count */ 43 | int refcnt; 44 | /** Zero-terminated value */ 45 | char str[FLEX_ARRAY]; 46 | }; 47 | 48 | /** Create new pool */ 49 | struct StrPool *strpool_create(CxMem *ca); 50 | 51 | /** Release pool */ 52 | void strpool_free(struct StrPool *sp); 53 | 54 | /** Return either existing or new PStr for given value */ 55 | struct PStr *strpool_get(struct StrPool *sp, const char *str, ssize_t len); 56 | 57 | /** Increase reference count for existing PStr */ 58 | void strpool_incref(struct PStr *str); 59 | 60 | /** Decrease reference count for existing PStr */ 61 | void strpool_decref(struct PStr *str); 62 | 63 | /** Return count of strings in the pool */ 64 | int strpool_total(struct StrPool *sp); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /lib/usual/mempool.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Simple memory pool for variable-length allocations. 3 | * 4 | * Copyright (c) 2009 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | #include 20 | 21 | /* 22 | * Allows allocation of several variable-sized objects, 23 | * freeing them all together. 24 | * 25 | * ToDo: make it more 'obstack'-like (???) 26 | * - free_last 27 | * - resize_last 28 | * - append 29 | */ 30 | 31 | struct MemPool { 32 | struct MemPool *prev; 33 | unsigned size; 34 | unsigned used; 35 | }; 36 | 37 | void *mempool_alloc(struct MemPool **pool, unsigned size) 38 | { 39 | struct MemPool *cur = *pool; 40 | void *ptr; 41 | unsigned nsize; 42 | 43 | size = ALIGN(size); 44 | if (cur && cur->used + size <= cur->size) { 45 | ptr = (char *)(cur + 1) + cur->used; 46 | cur->used += size; 47 | return ptr; 48 | } else { 49 | nsize = cur ? (2 * cur->size) : 512; 50 | while (nsize < size) 51 | nsize *= 2; 52 | cur = calloc(1, sizeof(*cur) + nsize); 53 | if (cur == NULL) 54 | return NULL; 55 | cur->used = size; 56 | cur->size = nsize; 57 | cur->prev = *pool; 58 | *pool = cur; 59 | return (char *)(cur + 1); 60 | } 61 | } 62 | 63 | void mempool_destroy(struct MemPool **pool) 64 | { 65 | struct MemPool *cur, *tmp; 66 | if (!pool) 67 | return; 68 | for (cur = *pool, *pool = NULL; cur; ) { 69 | tmp = cur->prev; 70 | free(cur); 71 | cur = tmp; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /lib/mk/temos/expected/libusual5.txt: -------------------------------------------------------------------------------- 1 | 2 | = Use installed libusual with antimake. = 3 | 4 | 5 | Install libusual and link against it. 6 | 7 | 8 | == Build libusual == 9 | 10 | --------------------------------- 11 | $ git clone git://github.com/libusual/libusual.git libusual 12 | Cloning into 'libusual'... 13 | $ cd libusual 14 | $ ./autogen.sh 15 | [...] 16 | $ ./configure --disable-shared --prefix=`pwd`/../inst 17 | [...] 18 | $ make 19 | [...] 20 | $ make install 21 | [...] 22 | $ cd .. 23 | --------------------------------- 24 | 25 | == Build our own code == 26 | 27 | 28 | Now we prepare our own code. 29 | 30 | 31 | First, this is the source file: 32 | 33 | .File: prog.c 34 | [source,c] 35 | ----------------------------------- 36 | #include 37 | #include 38 | #include 39 | 40 | int main(void) 41 | { 42 | const char *data = "CECSFXX"; 43 | uint32_t crc; 44 | 45 | crc = calc_crc32(data, strlen(data), 0); 46 | printf("crc: %08x\n", crc); 47 | return 0; 48 | } 49 | ----------------------------------- 50 | 51 | Here is corresponding Makefile: 52 | 53 | .File: Makefile 54 | [source,makefile] 55 | ----------------------------------- 56 | # temo hacks to support local install, not needed otherwise 57 | PKG_CONFIG = PKG_CONFIG_PATH=$(CURDIR)/inst/lib/pkgconfig pkg-config 58 | 59 | # use pkg-config to get libusual info 60 | USUAL_CPPFLAGS = $(shell $(PKG_CONFIG) libusual --cflags) 61 | USUAL_LDFLAGS = $(shell $(PKG_CONFIG) libusual --libs-only-L) 62 | USUAL_LIBS = $(shell $(PKG_CONFIG) libusual --libs-only-l) 63 | 64 | # Generic Antimake 65 | bin_PROGRAMS = prog 66 | prog_SOURCES = prog.c 67 | prog_CPPFLAGS = $(USUAL_CPPFLAGS) 68 | prog_LDFLAGS = $(USUAL_LDFLAGS) 69 | prog_LDADD = $(USUAL_LIBS) 70 | 71 | # use installed Antimake 72 | ANTIMAKE = $(shell $(PKG_CONFIG) libusual --variable=antimake) 73 | include $(ANTIMAKE) 74 | ----------------------------------- 75 | 76 | Build the project 77 | 78 | --------------------------------- 79 | $ make 80 | CC prog.c 81 | CCLD prog 82 | $ ls 83 | Makefile inst libusual prog prog.c 84 | $ ./prog 85 | crc: 12345678 86 | --------------------------------- 87 | 88 | Done! 89 | 90 | -------------------------------------------------------------------------------- /lib/test/test_common.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "tinytest.h" 4 | #include "tinytest_macros.h" 5 | 6 | #define str_check(a, b) tt_str_op(a, ==, b) 7 | #define int_check(a, b) tt_int_op(a, ==, b) 8 | #define ull_check(a, b) tt_assert_op_type(a, ==, b, uint64_t, "%" PRIu64) 9 | 10 | #define str_any2(val, a, b) \ 11 | do { \ 12 | const char *res = (val); \ 13 | if (strcmp(res, a) && strcmp(res, b)) \ 14 | str_check(res, a); \ 15 | } while (0) 16 | 17 | #define str_any3(val, a, b, c) \ 18 | do { \ 19 | const char *res = (val); \ 20 | if (strcmp(res, a) && strcmp(res, b) && strcmp(res, c)) \ 21 | str_check(res, a); \ 22 | } while (0) 23 | 24 | const char *tdata(const char *fn); 25 | 26 | extern struct testcase_t aatree_tests[]; 27 | extern struct testcase_t base_tests[]; 28 | extern struct testcase_t bits_tests[]; 29 | extern struct testcase_t cbtree_tests[]; 30 | extern struct testcase_t cfparser_tests[]; 31 | extern struct testcase_t crypto_tests[]; 32 | extern struct testcase_t ctype_tests[]; 33 | extern struct testcase_t cxalloc_tests[]; 34 | extern struct testcase_t endian_tests[]; 35 | extern struct testcase_t event_tests[]; 36 | extern struct testcase_t fileutil_tests[]; 37 | extern struct testcase_t fnmatch_tests[]; 38 | extern struct testcase_t getopt_tests[]; 39 | extern struct testcase_t hashing_tests[]; 40 | extern struct testcase_t hashtab_tests[]; 41 | extern struct testcase_t heap_tests[]; 42 | extern struct testcase_t json_tests[]; 43 | extern struct testcase_t list_tests[]; 44 | extern struct testcase_t mdict_tests[]; 45 | extern struct testcase_t netdb_tests[]; 46 | extern struct testcase_t pgutil_tests[]; 47 | extern struct testcase_t psrandom_tests[]; 48 | extern struct testcase_t regex_tests[]; 49 | extern struct testcase_t shlist_tests[]; 50 | extern struct testcase_t socket_tests[]; 51 | extern struct testcase_t string_tests[]; 52 | extern struct testcase_t strpool_tests[]; 53 | extern struct testcase_t talloc_tests[]; 54 | extern struct testcase_t time_tests[]; 55 | extern struct testcase_t tls_tests[]; 56 | extern struct testcase_t utf8_tests[]; 57 | extern struct testcase_t wchar_tests[]; 58 | 59 | extern unsigned long long test_seed1, test_seed2; 60 | -------------------------------------------------------------------------------- /lib/test/attregex/data/nullsubexpr.dat: -------------------------------------------------------------------------------- 1 | NOTE null subexpression matches : 2002-06-06 2 | 3 | E (a*)* a (0,1)(0,1) 4 | E SAME x (0,0)(0,0) 5 | E SAME aaaaaa (0,6)(0,6) 6 | E SAME aaaaaax (0,6)(0,6) 7 | E (a*)+ a (0,1)(0,1) 8 | E SAME x (0,0)(0,0) 9 | E SAME aaaaaa (0,6)(0,6) 10 | E SAME aaaaaax (0,6)(0,6) 11 | E (a+)* a (0,1)(0,1) 12 | E SAME x (0,0) 13 | E SAME aaaaaa (0,6)(0,6) 14 | E SAME aaaaaax (0,6)(0,6) 15 | E (a+)+ a (0,1)(0,1) 16 | E SAME x NOMATCH 17 | E SAME aaaaaa (0,6)(0,6) 18 | E SAME aaaaaax (0,6)(0,6) 19 | 20 | E ([a]*)* a (0,1)(0,1) 21 | E SAME x (0,0)(0,0) 22 | E SAME aaaaaa (0,6)(0,6) 23 | E SAME aaaaaax (0,6)(0,6) 24 | E ([a]*)+ a (0,1)(0,1) 25 | E SAME x (0,0)(0,0) 26 | E SAME aaaaaa (0,6)(0,6) 27 | E SAME aaaaaax (0,6)(0,6) 28 | E ([^b]*)* a (0,1)(0,1) 29 | E SAME b (0,0)(0,0) 30 | E SAME aaaaaa (0,6)(0,6) 31 | E SAME aaaaaab (0,6)(0,6) 32 | E ([ab]*)* a (0,1)(0,1) 33 | E SAME aaaaaa (0,6)(0,6) 34 | E SAME ababab (0,6)(0,6) 35 | E SAME bababa (0,6)(0,6) 36 | E SAME b (0,1)(0,1) 37 | E SAME bbbbbb (0,6)(0,6) 38 | E SAME aaaabcde (0,5)(0,5) 39 | E ([^a]*)* b (0,1)(0,1) 40 | E SAME bbbbbb (0,6)(0,6) 41 | E SAME aaaaaa (0,0)(0,0) 42 | E ([^ab]*)* ccccxx (0,6)(0,6) 43 | E SAME ababab (0,0)(0,0) 44 | 45 | E ((z)+|a)* zabcde (0,2)(1,2) 46 | 47 | {E a+? aaaaaa (0,1) no *? +? mimimal match ops 48 | E (a) aaa (0,1)(0,1) 49 | E (a*?) aaa (0,0)(0,0) 50 | E (a)*? aaa (0,0) 51 | E (a*?)*? aaa (0,0) 52 | } 53 | 54 | B \(a*\)*\(x\) x (0,1)(0,0)(0,1) 55 | B \(a*\)*\(x\) ax (0,2)(0,1)(1,2) 56 | B \(a*\)*\(x\) axa (0,2)(0,1)(1,2) 57 | B \(a*\)*\(x\)\(\1\) x (0,1)(0,0)(0,1)(1,1) 58 | B \(a*\)*\(x\)\(\1\) ax (0,2)(1,1)(1,2)(2,2) 59 | B \(a*\)*\(x\)\(\1\) axa (0,3)(0,1)(1,2)(2,3) 60 | B \(a*\)*\(x\)\(\1\)\(x\) axax (0,4)(0,1)(1,2)(2,3)(3,4) 61 | B \(a*\)*\(x\)\(\1\)\(x\) axxa (0,3)(1,1)(1,2)(2,2)(2,3) 62 | 63 | E (a*)*(x) x (0,1)(0,0)(0,1) 64 | E (a*)*(x) ax (0,2)(0,1)(1,2) 65 | E (a*)*(x) axa (0,2)(0,1)(1,2) 66 | 67 | E (a*)+(x) x (0,1)(0,0)(0,1) 68 | E (a*)+(x) ax (0,2)(0,1)(1,2) 69 | E (a*)+(x) axa (0,2)(0,1)(1,2) 70 | 71 | E (a*){2}(x) x (0,1)(0,0)(0,1) 72 | E (a*){2}(x) ax (0,2)(1,1)(1,2) 73 | E (a*){2}(x) axa (0,2)(1,1)(1,2) 74 | -------------------------------------------------------------------------------- /lib/usual/pgutil_kwlookup.g: -------------------------------------------------------------------------------- 1 | /* gperf header for kwlookup */ 2 | 3 | %language=ANSI-C 4 | %readonly-tables 5 | %pic 6 | %enum 7 | 8 | %define lookup-function-name pg_keyword_lookup_real 9 | %define hash-function-name pg_keyword_lookup_hash 10 | %define string-pool-name pgkw 11 | 12 | %% 13 | all 14 | analyse 15 | analyze 16 | and 17 | any 18 | array 19 | as 20 | asc 21 | asymmetric 22 | authorization 23 | between 24 | bigint 25 | binary 26 | bit 27 | boolean 28 | both 29 | case 30 | cast 31 | char 32 | character 33 | check 34 | coalesce 35 | collate 36 | column 37 | concurrently 38 | constraint 39 | create 40 | cross 41 | current_catalog 42 | current_date 43 | current_role 44 | current_schema 45 | current_time 46 | current_timestamp 47 | current_user 48 | dec 49 | decimal 50 | default 51 | deferrable 52 | desc 53 | distinct 54 | do 55 | else 56 | end 57 | except 58 | exists 59 | extract 60 | false 61 | fetch 62 | float 63 | for 64 | foreign 65 | freeze 66 | from 67 | full 68 | grant 69 | greatest 70 | group 71 | having 72 | ilike 73 | in 74 | initially 75 | inner 76 | inout 77 | int 78 | integer 79 | intersect 80 | interval 81 | into 82 | is 83 | isnull 84 | join 85 | leading 86 | least 87 | left 88 | like 89 | limit 90 | localtime 91 | localtimestamp 92 | national 93 | natural 94 | nchar 95 | new 96 | none 97 | not 98 | notnull 99 | null 100 | nullif 101 | numeric 102 | off 103 | offset 104 | old 105 | on 106 | only 107 | or 108 | order 109 | out 110 | outer 111 | over 112 | overlaps 113 | overlay 114 | placing 115 | position 116 | precision 117 | primary 118 | real 119 | references 120 | returning 121 | right 122 | row 123 | select 124 | session_user 125 | setof 126 | similar 127 | smallint 128 | some 129 | substring 130 | symmetric 131 | table 132 | then 133 | time 134 | timestamp 135 | to 136 | trailing 137 | treat 138 | trim 139 | true 140 | union 141 | unique 142 | user 143 | using 144 | values 145 | varchar 146 | variadic 147 | verbose 148 | when 149 | where 150 | window 151 | with 152 | xmlattributes 153 | xmlconcat 154 | xmlelement 155 | xmlexists 156 | xmlforest 157 | xmlparse 158 | xmlpi 159 | xmlroot 160 | xmlserialize 161 | -------------------------------------------------------------------------------- /lib/mk/temos/src/libusual3.temo: -------------------------------------------------------------------------------- 1 | 2 | . ./libtemo.sh || exit 1 3 | 4 | test_start 5 | 6 | title Using Autoconf and embedded libusual 7 | 8 | longmsg <<-"MSG" 9 | MSG 10 | 11 | title2 Fetch libusual 12 | 13 | msg Here we close libusual repo, but do not configure nor build it. 14 | 15 | usual_clone lib 16 | 17 | msg Autoconf setup 18 | 19 | cat_file autogen.sh <<"EOF" 20 | # use prepared autgen logic 21 | ./lib/mk/std-autogen.sh ./lib 22 | 23 | # fetch Antimake template from libusual 24 | cp lib/mk/antimake.mk antimake.mk.in 25 | EOF 26 | 27 | cat_file configure.ac <<"EOF" 28 | AC_INIT([achello], [0.1], [https://libusual.github.com]) 29 | AC_CONFIG_SRCDIR([prog.c]) 30 | AC_CONFIG_HEADER([lib/usual/config.h]) 31 | AC_PREREQ([2.59]) 32 | 33 | AC_USUAL_PORT_CHECK 34 | AC_USUAL_PROGRAM_CHECK 35 | AC_USUAL_HEADER_CHECK 36 | AC_USUAL_TYPE_CHECK 37 | AC_USUAL_FUNCTION_CHECK 38 | 39 | AC_OUTPUT([antimake.mk]) 40 | EOF 41 | 42 | msg Here is the source that needs to be linked with libusual: 43 | 44 | cat_file prog.c <<"EOF" 45 | #include 46 | #include 47 | #include 48 | 49 | int main(void) 50 | { 51 | const char *data = "CECSFXX"; 52 | uint32_t crc; 53 | 54 | crc = calc_crc32(data, strlen(data), 0); 55 | printf("crc: %08x\n", crc); 56 | return 0; 57 | } 58 | EOF 59 | 60 | msg Antimake based Makefile 61 | 62 | cat_file Makefile <<"EOF" 63 | # the automake-style build description for 'prog' 64 | noinst_PROGRAMS = prog 65 | prog_SOURCES = prog.c 66 | 67 | # location of configured libusual 68 | USUAL_DIR = lib 69 | 70 | # mention that 'prog' wants embedded libusual 71 | prog_EMBED_LIBUSUAL = 1 72 | AM_FEATURES = libusual 73 | 74 | # clean configured files 75 | DISTCLEANFILES = config.status config.log \ 76 | antimake.mk $(USUAL_DIR)/usual/config.h 77 | 78 | # clean generated files 79 | MAINTAINERCLEANFILES = configure config.guess config.sub install-sh \ 80 | antimake.mk.in $(USUAL_DIR)/usual/config.h.in 81 | 82 | # launch Antimake 83 | include $(USUAL_DIR)/mk/antimake.mk 84 | EOF 85 | 86 | msg Build the project 87 | runq sh ./autogen.sh 88 | runq ./configure 89 | run make 90 | run ls 91 | run ./prog 92 | run make maintainer-clean 93 | run ls 94 | 95 | msg Done 96 | -------------------------------------------------------------------------------- /test/test.ini: -------------------------------------------------------------------------------- 1 | [databases] 2 | p0 = port=6666 host=127.0.0.1 dbname=p0 user=bouncer pool_size=2 3 | p1 = port=6666 host=127.0.0.1 dbname=p1 user=bouncer 4 | p2 = port=6666 host=127.0.0.1 dbname=p0 max_db_connections=4 5 | p3 = port=6666 host=127.0.0.1 dbname=p0 user=bouncer pool_mode=session 6 | p4 = port=6666 host=127.0.0.1 dbname=p4 user=puser1 password=foo 7 | p4x= port=6666 host=127.0.0.1 dbname=p4 user=puser1 password=wrong 8 | p4y= port=6666 host=127.0.0.1 dbname=p4 user=puser1 9 | p4z= port=6666 host=127.0.0.1 dbname=p4 user=puser2 10 | p4l= port=6666 host=127.0.0.1 dbname=p4 user=longpass 11 | p5 = port=6666 host=127.0.0.1 dbname=p5 user=muser1 password=foo 12 | p5x= port=6666 host=127.0.0.1 dbname=p5 user=muser1 password=wrong 13 | p5y= port=6666 host=127.0.0.1 dbname=p5 user=muser1 14 | p5z= port=6666 host=127.0.0.1 dbname=p5 user=muser2 15 | p6 = port=6666 host=127.0.0.1 dbname=p6 user=scramuser1 password=foo 16 | p6x= port=6666 host=127.0.0.1 dbname=p6 user=scramuser1 password=wrong 17 | p6y= port=6666 host=127.0.0.1 dbname=p6 user=scramuser1 18 | p6z= port=6666 host=127.0.0.1 dbname=p6 user=scramuser2 19 | p61= port=6666 host=127.0.0.1 dbname=p6 user=scramuser3 20 | p62= port=6666 host=127.0.0.1 dbname=p6 21 | p7a= port=6666 host=127.0.0.1 dbname=p7 22 | p7b= port=6666 host=127.0.0.1 dbname=p7 23 | p7c= port=6666 host=127.0.0.1 dbname=p7 24 | p8 = port=6666 host=127.0.0.1 dbname=p0 connect_query='set enable_seqscan=off; set enable_nestloop=off' 25 | 26 | authdb = port=6666 host=127.0.0.1 dbname=p1 auth_user=pswcheck 27 | 28 | hostlist1 = port=6666 host=127.0.0.1,::1 dbname=p0 user=bouncer 29 | hostlist2 = port=6666 host=127.0.0.1,127.0.0.1 dbname=p0 user=bouncer 30 | 31 | ; commented out except for auto-database tests 32 | ;* = port=6666 host=127.0.0.1 33 | 34 | [users] 35 | maxedout = max_user_connections=3 36 | 37 | [pgbouncer] 38 | logfile = test.log 39 | pidfile = test.pid 40 | 41 | listen_addr = 127.0.0.1 42 | listen_port = 6667 43 | unix_socket_dir = /tmp 44 | 45 | auth_type = trust 46 | auth_file = userlist.txt 47 | 48 | pool_mode = statement 49 | 50 | server_check_delay = 10 51 | max_client_conn = 10 52 | default_pool_size = 5 53 | 54 | server_lifetime = 120 55 | server_idle_timeout = 60 56 | 57 | tcp_defer_accept = 0 58 | tcp_keepalive = 0 59 | -------------------------------------------------------------------------------- /lib/usual/utf8.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * Low-level UTF8 handling. 3 | */ 4 | 5 | /* 6 | * Copyright (c) 2009 Marko Kreen 7 | * 8 | * Permission to use, copy, modify, and/or distribute this software for any 9 | * purpose with or without fee is hereby granted, provided that the above 10 | * copyright notice and this permission notice appear in all copies. 11 | * 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 | */ 20 | 21 | #ifndef _USUAL_UTF8_H_ 22 | #define _USUAL_UTF8_H_ 23 | 24 | #include 25 | 26 | /** 27 | * Parse Unicode codepoint from UTF8 stream. 28 | * 29 | * On invalid UTF8 sequence returns negative byte value and 30 | * inreases src_p by one. 31 | * 32 | * @param src_p Location of data pointer. Will be incremented in-place. 33 | * @param srcend Pointer to end of data. 34 | * @return UNOCODE codepoint or negative byte value on error. 35 | */ 36 | int utf8_get_char(const char **src_p, const char *srcend); 37 | 38 | /** 39 | * Write Unicode codepoint as UTF8 sequence. 40 | * 41 | * Skips invalid Unicode values without error. 42 | * 43 | * @param c Unicode codepoint. 44 | * @param dst_p Location of dest pointer, will be increased in-place. 45 | * @param dstend Pointer to end of buffer. 46 | * @return false if not room, true otherwise. 47 | */ 48 | bool utf8_put_char(unsigned int c, char **dst_p, const char *dstend); 49 | 50 | /** Return UTF8 seq length based on unicode codepoint */ 51 | int utf8_char_size(unsigned int c); 52 | 53 | /** Return UTF8 seq length based on first byte */ 54 | int utf8_seq_size(unsigned char c); 55 | 56 | /** Return sequence length if all bytes are valid, 0 otherwise. */ 57 | int utf8_validate_seq(const char *src, const char *srcend); 58 | 59 | bool utf8_validate_string(const char *src, const char *end); 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /lib/usual/crypto/digest.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Common API for cryptographic digests. 3 | * 4 | * Copyright (c) 2012 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | #include 20 | 21 | #include 22 | 23 | struct DigestContext { 24 | const struct DigestInfo *impl; 25 | CxMem *cx; 26 | uint64_t state[1]; 27 | }; 28 | 29 | struct DigestContext *digest_new(const struct DigestInfo *impl, CxMem *cx) 30 | { 31 | struct DigestContext *ctx; 32 | unsigned alloc; 33 | 34 | alloc = offsetof(struct DigestContext, state) + impl->state_len; 35 | ctx = cx_alloc(cx, alloc); 36 | if (!ctx) 37 | return NULL; 38 | 39 | ctx->impl = impl; 40 | ctx->cx = cx; 41 | impl->init(ctx->state); 42 | return ctx; 43 | } 44 | 45 | void digest_update(struct DigestContext *ctx, const void *data, size_t len) 46 | { 47 | ctx->impl->update(ctx->state, data, len); 48 | } 49 | 50 | void digest_final(struct DigestContext *ctx, uint8_t *res) 51 | { 52 | ctx->impl->final(ctx->state, res); 53 | } 54 | 55 | void digest_reset(struct DigestContext *ctx) 56 | { 57 | ctx->impl->init(ctx->state); 58 | } 59 | 60 | void digest_free(struct DigestContext *ctx) 61 | { 62 | CxMem *cx = ctx->cx; 63 | unsigned alloc = offsetof(struct DigestContext, state) + ctx->impl->state_len; 64 | 65 | memset(ctx, 0, alloc); 66 | cx_free(cx, ctx); 67 | } 68 | 69 | unsigned digest_block_len(struct DigestContext *ctx) 70 | { 71 | return ctx->impl->block_len; 72 | } 73 | 74 | unsigned digest_result_len(struct DigestContext *ctx) 75 | { 76 | return ctx->impl->result_len; 77 | } 78 | -------------------------------------------------------------------------------- /lib/usual/err.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Cmdline error reporting. 3 | * 4 | * Copyright (c) 2009 Marko Kreen 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * @file 21 | * 22 | * Error printing for command-line utilities. 23 | */ 24 | #ifndef _USUAL_ERR_H_ 25 | #define _USUAL_ERR_H_ 26 | 27 | #include 28 | 29 | #ifdef HAVE_ERR_H 30 | #include 31 | #endif 32 | 33 | #ifndef HAVE_ERR 34 | /** Print formatted message and strerror(errno) to stderr and exit with given error code */ 35 | void err(int e, const char *fmt, ...) _PRINTF(2, 3); 36 | #endif 37 | #ifndef HAVE_ERRX 38 | /** Print formatted message to stderr and exit with given error code */ 39 | void errx(int e, const char *fmt, ...) _PRINTF(2, 3); 40 | #endif 41 | #ifndef HAVE_WARN 42 | /** Print formatted message and strerror(errno) to stderr */ 43 | void warn(const char *fmt, ...) _PRINTF(1, 2); 44 | #endif 45 | #ifndef HAVE_WARNX 46 | /** Print formatted message to stderr */ 47 | void warnx(const char *fmt, ...) _PRINTF(1, 2); 48 | #endif 49 | #ifndef HAVE_SETPROGNAME 50 | /** Set program name to that will printed as prefix to error messages */ 51 | void setprogname(const char *s); 52 | #endif 53 | #ifndef HAVE_GETPROGNAME 54 | /** Return program name set with @ref setprogname */ 55 | const char *getprogname(void); 56 | #endif 57 | 58 | /** Malloc that exits on failure */ 59 | void *xmalloc(size_t len); 60 | 61 | /** Realloc that exits on failure */ 62 | void *xrealloc(void *p, size_t len); 63 | 64 | /** strdup that exits on failure */ 65 | char *xstrdup(const char *s); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /lib/mk/temos/libtemo.sh: -------------------------------------------------------------------------------- 1 | LANG=C 2 | LC_ALL=C 3 | export LANG LC_ALL 4 | 5 | PATH=`pwd`/bin:$PATH 6 | export PATH 7 | 8 | set -e 9 | set -o pipefail 10 | 11 | SH="bash" 12 | 13 | unset MAKELEVEL MAKEFLAGS 14 | export MAKELEVEL MAKEFLAGS 15 | 16 | code=0 17 | 18 | # we want to test local commits 19 | real_repo=../../.. 20 | 21 | # but final html should have fixed public url 22 | show_repo=git://github.com/libusual/libusual.git 23 | 24 | usual_clone() { 25 | enter_code 26 | echo "$ git clone $show_repo" "$@" 27 | git clone $real_repo "$@" 28 | } 29 | 30 | test_start() { 31 | rm -rf tmp 32 | mkdir tmp 33 | cd tmp 34 | } 35 | 36 | enter_code() { 37 | if test "$code" = "0"; then 38 | echo "---------------------------------" 39 | code=1 40 | fi 41 | } 42 | 43 | leave_code() { 44 | if test "$code" = "1"; then 45 | echo "---------------------------------" 46 | code=0 47 | fi 48 | } 49 | 50 | ls() { 51 | /bin/ls -C "$@" 52 | } 53 | 54 | title() { 55 | leave_code 56 | echo "" 57 | echo "=" "$@" "=" 58 | echo "" 59 | } 60 | 61 | title2() { 62 | leave_code 63 | echo "" 64 | echo "==" "$@" "==" 65 | echo "" 66 | } 67 | 68 | title3() { 69 | leave_code 70 | echo "" 71 | echo "===" "$@" "===" 72 | echo "" 73 | } 74 | 75 | run() { 76 | enter_code 77 | echo "$ $*" 78 | case "$1" in 79 | cd|ls|export) $* ;; 80 | *) $SH -c "$*" 2>&1 81 | esac 82 | } 83 | 84 | runq() { 85 | enter_code 86 | echo "$ $*" 87 | echo "[...]" 88 | $SH -c "$*" > quiet.log 2>&1 || { tail -5 quiet.log; exit 1; } 89 | rm -f quiet.log 90 | } 91 | 92 | msg() { 93 | leave_code 94 | echo "" 95 | echo "$@" 96 | echo "" 97 | } 98 | 99 | longmsg() { 100 | leave_code 101 | echo "" 102 | sed 's/^ //' 103 | echo "" 104 | } 105 | 106 | cat_file() { 107 | leave_code 108 | mkdir -p `dirname $1` 109 | echo ".File: $1" 110 | case "$1" in 111 | *Makefile) echo "[source,makefile]" ;; 112 | *.[ch]) echo "[source,c]" ;; 113 | *.ac) echo "[source,autoconf]" ;; 114 | *.sh) echo "[source,shell]" ;; 115 | esac 116 | echo "-----------------------------------" 117 | sed 's/^ //' > $1 118 | cat $1 119 | echo "-----------------------------------" 120 | } 121 | -------------------------------------------------------------------------------- /lib/usual/netdb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * libusual - Utility library for C 3 | * 4 | * Copyright (c) 2010 Marko Kreen, Skype Technologies 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** @file 20 | * 21 | * DNS lookup. 22 | */ 23 | 24 | #ifndef _USUAL_NETDB_H_ 25 | #define _USUAL_NETDB_H_ 26 | 27 | #include 28 | 29 | #ifdef HAVE_NETDB_H 30 | #include 31 | #endif 32 | 33 | #ifndef HAVE_GETADDRINFO_A 34 | 35 | /** Async execution */ 36 | #ifndef GAI_WAIT 37 | #define GAI_WAIT 0 38 | #endif 39 | 40 | /** Synchronous execution */ 41 | #ifndef GAI_NOWAIT 42 | #define GAI_NOWAIT 1 43 | #endif 44 | 45 | /* avoid name conflicts */ 46 | #define gaicb usual_gaicb 47 | #define getaddrinfo_a(a,b,c,d) usual_getaddrinfo_a(a,b,c,d) 48 | 49 | /** 50 | * Request data for getaddrinfo_a(). 51 | * 52 | * Fields correspond to getaddrinfo() parameters. 53 | */ 54 | struct gaicb { 55 | /** node name */ 56 | const char *ar_name; 57 | /** service name */ 58 | const char *ar_service; 59 | /** hints */ 60 | const struct addrinfo *ar_request; 61 | /** result */ 62 | struct addrinfo *ar_result; 63 | /* internal state */ 64 | int _state; 65 | }; 66 | 67 | #ifndef EAI_INPROGRESS 68 | #define EAI_INPROGRESS -100 69 | #endif 70 | 71 | #ifndef EAI_SYSTEM 72 | #define EAI_SYSTEM -10 73 | #endif 74 | 75 | #define gai_error(gcb) ((gcb)->_state) 76 | 77 | /** 78 | * Compat: Async DNS lookup. 79 | */ 80 | int getaddrinfo_a(int mode, struct gaicb *list[], int nitems, struct sigevent *sevp); 81 | 82 | #endif /* HAVE_GETADDRINFO_A */ 83 | 84 | #endif /* _USUAL_NETDB_H_ */ 85 | -------------------------------------------------------------------------------- /lib/usual/pthread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2009 Marko Kreen 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | /** @file 18 | * 19 | * Pthreads compat for win32. 20 | */ 21 | #ifndef _USUAL_PTHREAD_H_ 22 | #define _USUAL_PTHREAD_H_ 23 | 24 | #include 25 | 26 | #ifdef HAVE_PTHREAD_H 27 | #include 28 | 29 | #else 30 | 31 | #ifdef WIN32 32 | 33 | #define pthread_create(a,b,c,d) compat_pthread_create(a,b,c,d) 34 | #define pthread_mutex_init(a,b) compat_pthread_mutex_init(a,b) 35 | #define pthread_mutex_destroy(a) compat_pthread_mutex_destroy(a) 36 | #define pthread_mutex_lock(a) compat_pthread_mutex_lock(a) 37 | #define pthread_mutex_unlock(a) compat_pthread_mutex_unlock(a) 38 | #define pthread_join(a,b) compat_pthread_join(a,b) 39 | #define pthread_once(a,b) compat_pthread_once(a,b) 40 | 41 | typedef HANDLE pthread_t; 42 | typedef HANDLE pthread_mutex_t; 43 | typedef int pthread_attr_t; 44 | 45 | int pthread_create(pthread_t *t, pthread_attr_t *attr, void *(*fn)(void *), void *arg); 46 | int pthread_mutex_init(pthread_mutex_t *lock, void *unused); 47 | int pthread_mutex_destroy(pthread_mutex_t *lock); 48 | int pthread_mutex_lock(pthread_mutex_t *lock); 49 | int pthread_mutex_unlock(pthread_mutex_t *lock); 50 | int pthread_join(pthread_t *t, void **ret); 51 | 52 | #ifdef INIT_ONCE_STATIC_INIT 53 | #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT 54 | typedef INIT_ONCE pthread_once_t; 55 | int pthread_once(pthread_once_t *once, void (*once_func)(void)); 56 | #endif 57 | 58 | #endif /* WIN32 */ 59 | 60 | #endif /* HAVE_PTHREAD_H */ 61 | 62 | #endif 63 | --------------------------------------------------------------------------------