Explorar o código

- removing forgotten files

Jan Janak %!s(int64=17) %!d(string=hai) anos
pai
achega
2b05a5d59e

+ 0 - 133
modules/db_postgres/copy_to_psql

@@ -1,133 +0,0 @@
-#!/usr/local/bin/perl
-#
-# $Id$
-#
-# sc: ser control; tool for maintaining ser's databases
-#
-# History:
-# --------
-# 2003-04-07 initial attempt at file copy script
-#
-# To-DO:
-# -----
-# - generalization for other than mysql databases
-# - front-end to updating administrative mysql password would
-#   be a convenient thing to have
-#
-# quick and dirty script to copy 0.8.10 mysql location and subscription table
-# out and create insert statements for postgres new CVS version table
-# this script only copies 2 tables, location and subscriber.
-# you may need to modify the open(FD,"sdfdsf") line to suite your needs.
-#
-
-$q = <<EOT;
-select
-	user_id, domain, contact, expires, q, callid, cseq,
-	last_modified
-  from
-	location
-EOT
-if(!open(FD,"/usr/local/mysql/bin/mysql --batch ser -e \"$q\"|"))
-{
-	die("can't open mysql process");
-}
-
-print "/* insert location tuples */\n";
-print "delete from location;\n";
-while(<FD>)
-{
-	chop;
-	($user_id,$domain,$contact,$expires,$q,$callid,$cseq, $lastmodified)=
-		split("\t");
-
-	$i = <<EOT;
-	insert
-	  into
-	  	location
-	     (
-	     	username,
-		domain,
-		contact,
-		expires,
-		q,
-		callid,
-		cseq,
-		last_modified,
-		replicate,
-		state
-	     )
-	values
-	     (
-	     	'$user_id',
-		'$domain',
-		'$contact',
-		'$expires',
-		$q,
-		'$callid',
-		$cseq,
-		'$expires',
-		null,
-		null
-	      );
-EOT
-
-	$i =~ s/\n/ /g;
-	$i =~ s/\t+/ /g;
-	$i =~ s/^\s+//;
-	$i =~ s/\s+$//;
-	print "$i\n";
-}
-$q = <<EOT;
-select
-	phplib_id, user_id, password, first_name, last_name, phone,
-	email_address, datetime_created, datetime_modified, confirmation,
-	flag, sendnotification, greeting, ha1, domain, ha1b, perms,
-	allow_find, timezone
-  from
-	subscriber
-EOT
-if(!open(FD,"/usr/local/mysql/bin/mysql --batch ser -e \"$q\"|"))
-{
-	die("can't open mysql process");
-}
-
-print "/* insert subscriber tuples */\n";
-print "delete from subscriber;\n";
-while(<FD>)
-{
-	chop;
-	( $phplib_id, $user_id, $password, $first_name, $last_name,
-	  $phone, $email_address, $datetime_created, $datetime_modified,
-	  $confirmation, $flag, $sendnotification, $greeting, $ha1,
-	  $domain, $ha1b, $perms, $allow_find, $timezone) =
-		split("\t");
-
-	$i = <<EOT;
-	insert
-	  into
-	  	subscriber
-	     (
-		phplib_id, username, password, first_name,
-		last_name, phone, email_address, datetime_created,
-		datetime_modified, confirmation, flag,
-		sendnotification, greeting, ha1, domain,
-		ha1b, perms, allow_find, timezone
-	     )
-	values
-	     (
-		'$phplib_id', '$user_id', '$password', '$first_name',
-		'$last_name', '$phone', '$email_address', '$datetime_created',
-		'$datetime_created', '$confirmation', '$flag',
-		'$sendnotification', '$greeting', '$ha1', '$domain',
-		'$ha1b', '$perms', '$allow_find', '$timezone'
-	      );
-EOT
-
-	$i =~ s/\n/ /g;
-	$i =~ s/\t+/ /g;
-	$i =~ s/^\s+//;
-	$i =~ s/\s+$//;
-	print "$i\n";
-}
-
-exit 0;

+ 0 - 52
modules/db_postgres/db_con.c

@@ -1,52 +0,0 @@
-/* 
- * $Id$ 
- *
- * Database connection related functions
- *
- * Portions Copyright (C) 2001-2003 FhG FOKUS
- * Copyright (C) 2003 August.Net Services, LLC
- * Portions Copyright (C) 2005 iptelorg GmbH
- *
- * This file is part of ser, a free SIP server.
- *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License 
- * along with this program; if not, write to the Free Software 
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-
-#include <string.h>
-#include "../../db/db.h"
-#include "../../dprint.h"
-#include "../../mem/mem.h"
-
-
-/*
- * Store name of table that will be used by
- * subsequent database functions
- */
-int pg_use_table(db_con_t* _h, const char* _t)
-{
-	if ((!_h) || (!_t)) {
-		ERR("Invalid parameter value\n");
-		return -1;
-	}
-
-	CON_TABLE(_h) = _t;
-	return 0;
-}

+ 0 - 191
modules/db_postgres/db_mod.c

@@ -1,191 +0,0 @@
-/*
- * $Id$
- *
- * Postgres module interface
- *
- * Portions Copyright (C) 2001-2003 FhG FOKUS
- * Copyright (C) 2003 August.Net Services, LLC
- * Portions Copyright (C) 2005 iptelorg GmbH
- *
- * This file is part of ser, a free SIP server.
- *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#include <stdio.h>
-#include "../../sr_module.h"
-#include "dbase.h"
-
-MODULE_VERSION
-
-int connect_timeout = 0; /* Default is unlimited */
-int reconnect_attempts = 2; /* How many times should the module try to reconnect if
-			     * the connection is lost. 0 disables reconnecting */
-
-/*
- * Postgres database module interface
- */
-static cmd_export_t cmds[]={
-	{"db_use_table",   (cmd_function)pg_use_table,      2, 0, 0},
-	{"db_init",        (cmd_function)pg_init,           1, 0, 0},
-	{"db_close",       (cmd_function)pg_close,          2, 0, 0},
-	{"db_query",       (cmd_function)pg_query,          2, 0, 0},
-	{"db_raw_query",   (cmd_function)pg_raw_query,      2, 0, 0},
-	{"db_free_result", (cmd_function)pg_db_free_result, 2, 0, 0},
-	{"db_insert",      (cmd_function)pg_insert,         2, 0, 0},
-	{"db_delete",      (cmd_function)pg_delete,         2, 0, 0},
-	{"db_update",      (cmd_function)pg_update,         2, 0, 0},
-	{0, 0, 0, 0, 0}
-};
-
-
-/*
- * Exported parameters
- */
-static param_export_t params[] = {
-	{"connect_timeout",    PARAM_INT, &connect_timeout   },
-	{"reconnect_attempts", PARAM_INT, &reconnect_attempts},
-	{0, 0, 0}
-};
-
-
-/*
- * create table test (
- *	bool_col BOOL NULL,
- *	int2_col INT2 NULL,
- *	int4_col INT4 NULL,
- *	int8_col INT8 NULL,
- *	float4_col FLOAT4 NULL,
- *	float8_col FLOAT8 NULL,
- *	timestamp_col TIMESTAMP NULL,
- *	char_col CHAR NULL,
- *	text_col TEXT NULL,
- *	bpchar_col BPCHAR NULL,
- *	varchar_col VARCHAR(255) NULL,
- *	bytea_col BYTEA NULL,
- *	bit_col BIT(32) NULL,
- *	varbit_col VARBIT NULL
- * );
- *
- * insert into test (bool_col, int2_col, int4_col, int8_col, float4_col, float8_col,
- *                   timestamp_col, char_col, text_col, bpchar_col, varchar_col,
- *                   bytea_col, bit_col, varbit_col)
- *                   values
- *                   (true, 22, 23, 24, 25.21, 25.22, '1999-10-18 21:35:00', 'a',
- *                   'ab', 'a', 'abcde', 'abcdddd', B'00110011001100110011001100110011',
- *                    b'10101010101010101010101010101010');
- */
-#if 0
-static int pg_test(void)
-{
-	int row, col;
-	db_res_t* res;
-	db_con_t* con;
-	struct tm* tt;
-	db_key_t keys[1];
-	db_val_t vals[1];
-
-	con = pg_init("postgres://ser:heslo@localhost/ser");
-	if (!con) {
-		ERR("Unable to connect database\n");
-		return -1;
-	}
-	INFO("Successfuly connected\n");
-	pg_use_table(con, "test");
-
-	keys[0] = "int4_col";
-	vals[0].type = DB_INT;
-	vals[0].nul = 1;
-	vals[0].val.int_val = 1;
-
-	pg_query(con, keys, 0, vals, 0, 1, 0, 0, &res);
-	if (!res) {
-		ERR("No result received\n");
-		return -1;
-	}
-	if (!res->n) {
-		ERR("Result contains no rows\n");
-		return -1;
-	} else {
-		INFO("Result contains %d rows\n", res->n);
-	}
-
-	INFO("Result contains %d columns\n", res->col.n);
-
-	for(row = 0; row < res->n; row++) {
-		for(col = 0; col < res->col.n; col++) {
-			switch(res->col.types[col]) {
-			case DB_INT:
-				INFO("INT(%d)", res->rows[row].values[col].val.int_val);
-				break;
-
-			case DB_FLOAT:
-				INFO("FLOAT(%f)", res->rows[row].values[col].val.float_val);
-				break;
-
-			case DB_DOUBLE:
-				INFO("DOUBLE(%f)", res->rows[row].values[col].val.double_val);
-				break;
-
-			case DB_STRING:
-				INFO("STRING(%s)", res->rows[row].values[col].val.string_val);
-				break;
-
-			case DB_STR:
-				INFO("STR(%.*s)", res->rows[row].values[col].val.str_val.len, res->rows[row].values[col].val.str_val.s);
-				break;
-
-			case DB_DATETIME:
-				tt = gmtime(&res->rows[row].values[col].val.time_val);
-				INFO("DATETIME(%s)", asctime(tt));
-				break;
-
-			case DB_BLOB:
-				INFO("BLOB(%.*s)", res->rows[row].values[col].val.str_val.len, res->rows[row].values[col].val.str_val.s);
-				break;
-
-			case DB_BITMAP:
-				INFO("INT(%x)", res->rows[row].values[col].val.bitmap_val);
-				break;
-
-			default:
-				ERR("Unsupported column type\n");
-				return -1;
-			}
-		}
-	}
-
-	pg_close(con);
-	return -1;
-}
-#endif
-
-
-struct module_exports exports = {
-	"postgres",
-	cmds,
-	0,         /* RPC methods */
-	params,    /*  module parameters */
-        0,         /* module initialization function */
-	0,         /* response function*/
-	0,         /* destroy function */
-	0,         /* oncancel function */
-	0          /* per-child init function */
-};

+ 0 - 40
modules/db_postgres/db_mod.h

@@ -1,40 +0,0 @@
-/* 
- * $Id$ 
- *
- * Postgres module interface
- *
- * Portions Copyright (C) 2001-2003 FhG FOKUS
- * Copyright (C) 2003 August.Net Services, LLC
- * Portions Copyright (C) 2005 iptelorg GmbH
- *
- * This file is part of ser, a free SIP server.
- *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License 
- * along with this program; if not, write to the Free Software 
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-#ifndef _DB_MOD_H
-#define _DB_MOD_H
-
-/* 2000-01-01 00:00:00 +0000 as the value of time_t in UTC */
-#define PG_EPOCH_TIME ((long long)946684800)
-
-extern int connect_timeout;
-extern int reconnect_attempts;
-
-#endif /* _DB_MOD_H */

+ 0 - 1083
modules/db_postgres/dbase.c

@@ -1,1083 +0,0 @@
-/* 
- * $Id$ 
- *
- * Postgres module core functions
- *
- * Portions Copyright (C) 2001-2003 FhG FOKUS
- * Copyright (C) 2003 August.Net Services, LLC
- * Portions Copyright (C) 2005 iptelorg GmbH
- *
- * This file is part of ser, a free SIP server.
- *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License 
- * along with this program; if not, write to the Free Software 
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <time.h>
-#include <libpq-fe.h>
-#include <netinet/in.h>
-#include "../../mem/mem.h"
-#include "../../dprint.h"
-#include "../../db/db_pool.h"
-#include "../../ut.h"
-#include "../../globals.h"
-#include "../../pt.h"
-#include "pg_con.h"
-#include "pg_type.h"
-#include "db_mod.h"
-#include "res.h"
-#include "dbase.h"
-
-
-#define SELECTALL "select * "
-#define SELECT    "select "
-#define FROM      "from "
-#define ORDER     "order by "
-#define WHERE     "where "
-#define AND       " and "
-#define INSERT    "insert into "
-#define VALUES    ") values ("
-#define DELETE    "delete from "
-#define UPDATE    "update "
-#define SET       "set "
-
-#define MAX_OPERATOR_LEN (sizeof(" is NULL") - 1)
-
-struct pg_params {
-	int n;
-	int cur;
-	const char** data;
-	int* len;
-	int* formats;
-	Oid* types;
-};
-
-
-static void free_pg_params(struct pg_params* ptr)
-{
-	if (!ptr) return;
-	if (ptr->data) pkg_free(ptr->data);
-	if (ptr->len) pkg_free(ptr->len);
-	if (ptr->formats) pkg_free(ptr->formats);
-	if (ptr->types) pkg_free(ptr->types);
-	pkg_free(ptr);
-}
-
-
-static struct pg_params* new_pg_params(int n)
-{
-	struct pg_params* ptr;
-	
-	ptr = (struct pg_params*)pkg_malloc(sizeof(struct pg_params));
-	if (!ptr) goto error;
-
-	ptr->formats = (int*)pkg_malloc(sizeof(int) * n);
-	if (!ptr->formats) goto error;
-
-	ptr->data = (const char**)pkg_malloc(sizeof(const char*) * n);
-	if (!ptr->data) goto error;
-
-	ptr->len = (int*)pkg_malloc(sizeof(int) * n);
-	if (!ptr->len) goto error;
-
-	ptr->types = (int*)pkg_malloc(sizeof(Oid) * n);
-	if (!ptr->types) goto error;
-	
-	memset((char*)ptr->data, 0, sizeof(const char*) * n);
-	memset(ptr->len, 0, sizeof(int) * n);
-	memset(ptr->types, 0, sizeof(Oid) * n);
-	ptr->n = n;
-	ptr->cur = 0;
-	return ptr;
-
- error:
-	ERR("No memory left\n");
-	free_pg_params(ptr);
-	return 0;
-}
-
-
-static inline int params_add(struct pg_params* p, db_con_t* con, db_val_t* vals, int n, int skip_null)
-{
-	int i, i1, i2;
-	db_val_t* val;
-
-	if (!p) {
-		ERR("Invalid parameter value\n");
-		return -1;
-	}
-
-	if (p->cur + n > p->n) {
-		ERR("Arrays too short (bug in postgres module)\n");
-		return -1;
-	}
-
-	for(i = 0; i < n; i++) {
-		val = &vals[i];
-		p->formats[p->cur] = 1;
-		if (val->nul) {
-			     /* When assembling parameters for where clause we skip parameters
-			      * that have null values because they are expressed as is null.
-			      * At other places we include them.
-			      */
-			if (!skip_null) p->cur++;
-			continue;
-		}
-		switch(val->type) {
-		case DB_INT:      
-			val->val.int_val = ntohl(val->val.int_val);
-			p->data[p->cur] = (const char*)&val->val.int_val;
-			p->len[p->cur] = 4;
-			p->types[p->cur] = INT4OID;
-			break;
-
-		case DB_FLOAT:
-			     /* Change the byte order of 4-byte value to network
-			      * byte order if necessary
-			      */
-			val->val.int_val = htonl(val->val.int_val);
-			p->data[p->cur] = (const char*)&val->val.int_val;
-			p->len[p->cur] = 4;
-			p->types[p->cur] = FLOAT4OID;
-			break;
-			
-		case DB_DOUBLE:
-			     /* Change the byte order of 8-byte value to network
-			      * byte order if necessary
-			      */
-			i1 = htonl(val->val.int8_val >> 32);
-			i2 = htonl(val->val.int8_val & 0xffffffff);
-			val->val.int_val = i1;
-			(&val->val.int_val)[1] = i2;
-			p->data[p->cur] = (const char*)&val->val.int_val;
-			p->len[p->cur] = 8;
-			p->types[p->cur] = FLOAT8OID;
-			break;
-			
-		case DB_STRING:
-			p->formats[p->cur] = 0;
-			p->data[p->cur] = val->val.string_val;
-			break;
-			
-		case DB_STR:
-			p->data[p->cur] = val->val.str_val.s;
-			p->len[p->cur] = val->val.str_val.len;
-			break;
-			
-		case DB_DATETIME:
-			if (CON_FLAGS(con) & PG_INT8_TIMESTAMP) {
-				val->val.int8_val = ((long long)val->val.time_val - PG_EPOCH_TIME) * 1000000;
-			} else {
-				val->val.double_val = (double)val->val.time_val - (double)PG_EPOCH_TIME;
-				
-			}
-			i1 = htonl(val->val.int8_val >> 32);
-			i2 = htonl(val->val.int8_val & 0xffffffff);
-			val->val.int_val = i1;
-			(&val->val.int_val)[1] = i2;
-			p->data[p->cur] = (const char*)&val->val.int_val;
-			p->len[p->cur] = 8;
-			p->types[p->cur] = TIMESTAMPOID;
-			break;
-			
-		case DB_BLOB:
-			p->data[p->cur] = val->val.blob_val.s;
-			p->len[p->cur] = val->val.blob_val.len;
-			break;
-			
-		case DB_BITMAP: 
-			(&val->val.int_val)[1] = htonl(val->val.int_val);
-			val->val.int_val = htonl(32);
-			p->data[p->cur] = (const char*)&val->val.int_val;
-			p->len[p->cur] = 8;
-			p->types[p->cur] = BITOID;
-			break;
-		}
-		
-		p->cur++;
-	}
-	
-	return 0;
-}
-
-static inline void free_params(struct pg_params* p)
-{
-	if (p->data) pkg_free(p->data);
-	if (p->len) pkg_free(p->len);
-	if (p->formats) pkg_free(p->formats);
-}
-
-
-/*
- * Initialize database module
- * No function should be called before this
- */
-db_con_t* pg_init(const char* url)
-{
-	struct db_id* id;
-	struct pg_con* con;
-	db_con_t* res;
-
-	id = 0;
-	res = 0;
-
-	/* if called from PROC_MAIN, allow it only from mod_init( when pt==0)*/
-	if (is_main && fixup_complete){
-		LOG(L_ERR, "BUG: postgres: pg_init: called from the main process,"
-					" ignoring...\n");
-	}
-	if (!url) {
-		ERR("Invalid parameter value\n");
-		return 0;
-	}
-
-	res = pkg_malloc(sizeof(db_con_t) + sizeof(struct pg_con*));
-	if (!res) {
-		ERR("No memory left\n");
-		return 0;
-	}
-	memset(res, 0, sizeof(db_con_t) + sizeof(struct pg_con*));
-
-	id = new_db_id(url);
-	if (!id) {
-		ERR("Cannot parse URL '%s'\n", url);
-		goto err;
-	}
-
-	     /* Find the connection in the pool */
-	con = (struct pg_con*)pool_get(id);
-	if (!con) {
-		DBG("Connection '%s' not found in pool\n", url);
-		     /* Not in the pool yet */
-		con = pg_new_connection(id);
-		if (!con) {
-			goto err;
-		}
-		pool_insert((struct pool_con*)con);
-	} else {
-		DBG("Connection '%s' found in pool\n", url);
-	}
-
-	res->tail = (unsigned long)con;
-	return res;
-
- err:
-	if (id) free_db_id(id);
-	if (res) pkg_free(res);
-	return 0;
-}
-
-
-/*
- * Shut down database module
- * No function should be called after this
- */
-void pg_close(db_con_t* handle)
-{
-	struct pool_con* con;
-
-	if (!handle) {
-		ERR("Invalid parameter value\n");
-		return;
-	}
-
-	con = (struct pool_con*)handle->tail;
-	if (pool_remove(con) != 0) {
-		pg_free_connection((struct pg_con*)con);
-	}
-
-	pkg_free(handle);
-}
-
-
-static int calc_param_len(start, num)
-{
-	int max, len, order;
-
-	if (!num) return 0;
-
-	max = start + num - 1;
-	len = num; /* $ */
-	
-	order = 0;
-	while(max) {
-		order++;
-		max /= 10;
-	}
-
-	return len + order * num;
-}
-
-/*
- * Append a constant string, uses sizeof to figure the length
- * of the string
- */
-#define append(buf, ptr)                                  \
-    do {                                                  \
-        if ((buf).len < (sizeof(ptr) - 1)) goto shortbuf; \
-        memcpy((buf).s, (ptr), sizeof(ptr) - 1);          \
-        (buf).s += sizeof(ptr) - 1;                       \
-        (buf).len -= sizeof(ptr) - 1;                     \
-    } while(0);
-
-
-/*
- * Append zero terminated string, uses strlen to obtain the
- * length of the string
- */
-#define append_str(buf, op)                  \
-    do {                                     \
-	int len;                             \
-        len = strlen(op);                    \
-        if ((buf).len < len) goto shortbuf;  \
-        memcpy((buf).s, (op), len);          \
-        (buf).s += len;                      \
-        (buf).len -= len;                    \
-    } while(0);
-
-
-/*
- * Append a parameter, accepts the number of the
- * parameter to be appended
- */
-#define append_param(buf, num)                  \
-    do {                                        \
-        const char* c;                          \
-        int len;                                \
-        c = int2str((num), &len);               \
-        if ((buf).len < len + 1) goto shortbuf; \
-        *(buf).s='$'; (buf).s++; (buf).len--;   \
-        memcpy((buf).s, c, len);                \
-        (buf).s += len; (buf).len -= len;       \
-    } while(0); 
-
-
-/*
- * Calculate the length of buffer needed to hold the insert query
- */
-static unsigned int calc_insert_len(db_con_t* con, db_key_t* keys, int n)
-{
-	int i;
-	unsigned int len;
-
-	if (!n) return 0;
-
-	len = sizeof(INSERT) - 1;
-	len += strlen(CON_TABLE(con)); /* Table name */
-	len += 2; /* _( */
-	for(i = 0; i < n; i++) {
-		len += strlen(keys[i]); /* Key names */
-	}
-	len += n - 1; /* , */
-	len += sizeof(VALUES);
-	len += calc_param_len(1, n);
-	len += n - 1;
-	len += 1; /* ) */
-	return len;
-}
-
-
-/*
- * Calculate the length of buffer needed to hold the delete query
- */
-static unsigned int calc_delete_len(db_con_t* con, db_key_t* keys, int n)
-{
-	int i;
-	unsigned int len;
-
-	len = sizeof(DELETE) - 1;
-	len += strlen(CON_TABLE(con));
-	if (n) {
-		len += 1; /* _ */
-		len += sizeof(WHERE) - 1;
-		len += n * MAX_OPERATOR_LEN;
-		len += (sizeof(AND) - 1) * (n - 1);
-		for(i = 0; i < n; i++) {
-			len += strlen(keys[i]);
-		}
-		len += calc_param_len(1, n);
-	}
-	return len;
-}
-
-static unsigned int calc_select_len(db_con_t* con, db_key_t* cols, db_key_t* keys, int n, int ncol, db_key_t order)
-{
-	int i;
-	unsigned int len;
-
-	if (!cols) {
-		len = sizeof(SELECTALL) - 1;
-	} else {
-		len = sizeof(SELECT);
-		for(i = 0; i < ncol; i++) {
-			len += strlen(cols[i]);
-		}
-		len += ncol - 1; /* , */
-		len++; /* space */
-	}
-	len += sizeof(FROM) - 1;
-	len += strlen(CON_TABLE(con));
-	len += 1; /* _ */
-	if (n) {
-		len += sizeof(WHERE) - 1;
-		len += n * MAX_OPERATOR_LEN;
-		len += (sizeof(AND) - 1) * (n - 1);
-		for(i = 0; i < n; i++) {
-			len += strlen(keys[i]);
-		}
-		len += calc_param_len(1, n);
-		len++; /* space */
-	}
-	if (order) {
-		len += sizeof(ORDER);
-		len += strlen(order);
-	}
-	return len;
-}
-
-static unsigned int calc_update_len(db_con_t* con, db_key_t* ukeys, db_key_t* keys, int un, int n)
-{
-	int i;
-	unsigned int len;
-
-	if (!un) return 0;
-
-	len = sizeof(UPDATE) - 1;
-	len += strlen(CON_TABLE(con));
-	len += 1; /* _ */
-	len += sizeof(SET) - 1;
-	len += un;  /* = */
-	for (i = 0; i < un; i++) {
-		len += strlen(ukeys[i]);
-	}
-	len += calc_param_len(1, un);
-	len += un; /* , and last space */
-	
-	if (n) {
-		len += sizeof(WHERE) - 1;
-		len += n * MAX_OPERATOR_LEN;
-		len += (sizeof(AND) - 1) * (n - 1);
-		for(i = 0; i < n; i++) {
-			len += strlen(keys[i]);
-		}
-		len += calc_param_len(1 + un, n);
-	}
-	return len;
-}
-
-
-static char* print_insert(db_con_t* con, db_key_t* keys, int n)
-{
-	unsigned int len;
-	int i;
-	char* s;
-	str p;
-
-	if (!n || !keys) {
-		ERR("Nothing to insert\n");
-		return 0;
-	}
-
-	len = calc_insert_len(con, keys, n);
-	
-	s = (char*)pkg_malloc(len + 1);
-	if (!s) {
-		ERR("Unable to allocate %d of memory\n", len);
-		return 0;
-	}
-	p.s = s;
-	p.len = len;
-	
-	append(p, INSERT);
-	append_str(p, CON_TABLE(con));
-	append(p, " (");
-
-	append_str(p, keys[0]);
-	for(i = 1; i < n; i++) {
-		append(p, ",");
-		append_str(p, keys[i]);
-	}
-	append(p, VALUES);
-
-	append_param(p, 1);
-	for(i = 1; i < n; i++) {
-		append(p, ",");
-		append_param(p, i + 1);
-	}
-	append(p, ")");
-	*p.s = '\0';
-	return s;
-
- shortbuf:
-	ERR("Buffer too short (bug in postgres module)\n");
-	pkg_free(s);
-	return 0;
-}
-
-
-
-static char* print_select(db_con_t* con, db_key_t* cols, db_key_t* keys, db_val_t* vals, 
-			  int n, int ncol, db_op_t* ops, db_key_t order)
-{
-	unsigned int len;
-	int i;
-	char* s;
-	str p;
-
-	len = calc_select_len(con, cols, keys, n, ncol, order);
-
-	s = (char*)pkg_malloc(len + 1);
-	if (!s) {
-		ERR("Unable to allocate %d of memory\n", len);
-		return 0;
-	}
-	p.s = s;
-	p.len = len;
-	
-	if (!cols || !ncol) {
-		append(p, SELECTALL);
-	} else {
-		append(p, SELECT);
-		append_str(p, cols[0]);
-		for(i = 1; i < ncol; i++) {
-			append(p, ",");
-			append_str(p, cols[i]);
-		}
-		append(p, " ");
-	}
-	append(p, FROM);
-	append_str(p, CON_TABLE(con));
-	append(p, " ");
-	if (n) {
-		append(p, WHERE);
-		append_str(p, keys[0]);
-		if (vals[0].nul) {
-			append(p, " is NULL");
-		} else {
-			if (ops) {
-				append_str(p, *ops);
-				ops++;
-			} else {
-				append(p, "=");
-			}
-			append_param(p, 1);
-		}
-		for(i = 1; i < n; i++) {
-			append(p, AND);
-			append_str(p, keys[i]);
-			if (vals[i].nul) {
-				append(p, " is NULL");
-			} else {
-				if (ops) {
-					append_str(p, *ops);
-					ops++;
-				} else {
-					append(p, "=");
-				}
-				append_param(p, i + 1);
-			}
-		}
-		append(p, " ");
-	}
-	if (order) {
-		append(p, ORDER);
-		append_str(p, order);
-	}
-
-	*p.s = '\0'; /* Zero termination */
-	return s;
-
- shortbuf:
-	ERR("Buffer too short (bug in postgres module)\n");
-	pkg_free(s);
-	return 0;
-}
-
-
-static char* print_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals, int n)
-{
-	unsigned int len;
-	int i;
-	char* s;
-	str p;
-
-	len = calc_delete_len(con, keys, n);
-
-	s = (char*)pkg_malloc(len + 1);
-	if (!s) {
-		ERR("Unable to allocate %d of memory\n", len);
-		return 0;
-	}
-	p.s = s;
-	p.len = len;
-
-	append(p, DELETE);
-	append_str(p, CON_TABLE(con));
-	append(p, " ");
-	if (n) {
-		append(p, WHERE);
-		append_str(p, keys[0]);
-		if (vals[0].nul) {
-			append(p, " is NULL");
-		} else {
-			if (ops) {
-				append_str(p, *ops);
-				ops++;
-			} else {
-				append(p, "=");
-			}
-			append_param(p, 1);
-		}
-		for(i = 1; i < n; i++) {
-			append(p, AND);
-			append_str(p, keys[i]);
-			if (vals[i].nul) {
-				append(p, " is NULL");
-			} else {
-				if (ops) {
-					append_str(p, *ops);
-					ops++;
-				} else {
-					append(p, "=");
-				}
-				append_param(p, i + 1);
-			}
-		}
-	}
-
-	*p.s = '\0';
-	return s;
-
- shortbuf:
-	ERR("Buffer too short (bug in postgres module)\n");
-	pkg_free(s);
-	return 0;
-}
-
-
-static char* print_update(db_con_t* con, db_key_t* ukeys, db_key_t* keys, db_op_t* ops, 
-			  db_val_t* vals, int un, int n)
-{
-	unsigned int len, param_no;
-	char* s;
-	int i;
-	str p;
-
-	if (!un) {
-		ERR("Nothing to update\n");
-		return 0;
-	}
-
-	param_no = 1;
-	len = calc_update_len(con, ukeys, keys, un, n);
-
-	s = (char*)pkg_malloc(len + 1);
-	if (!s) {
-		ERR("Unable to allocate %d of memory\n", len);
-		return 0;
-	}
-	p.s = s;
-	p.len = len;
-
-	append(p, UPDATE);
-	append_str(p, CON_TABLE(con));
-	append(p, " " SET);
-
-	append_str(p, ukeys[0]);
-	append(p, "=");
-	append_param(p, param_no++);
-
-	for(i = 1; i < un; i++) {
-		append(p, ",");
-		append_str(p, ukeys[i]);
-		append(p, "=");
-		append_param(p, param_no++);
-	}
-	append(p, " ");
-
-	if (n) {
-		append(p, WHERE);
-		append_str(p, keys[0]);
-		if (vals[0].nul) {
-			append(p, " is NULL");
-		} else {
-			if (ops) {
-				append_str(p, *ops);
-				ops++;
-			} else {
-				append(p, "=");
-			}
-			append_param(p, param_no++);
-		}
-		
-		for(i = 1; i < n; i++) {
-			append(p, AND);
-			append_str(p, keys[i]);
-			if (vals[i].nul) {
-				append(p, " is NULL");
-			} else {
-				if (ops) {
-					append_str(p, *ops);
-					ops++;
-				} else {
-					append(p, "=");
-				}
-				append_param(p, param_no++);
-			}
-		}
-	}	
-
-	*p.s = '\0';
-	return s;
-
- shortbuf:
-	ERR("Buffer too short (bug in postgres module)\n");
-	pkg_free(s);
-	return 0; 
-}
-
-/*
- * Return values: 1 Query failed, bad connection
- *                0 Query succeeded
- *               -1 Query failed due to some other reason
- */
-static int submit_query(db_res_t** res, db_con_t* con, const char* query, struct pg_params* params)
-{
-	PGresult* pgres;
-
-	DBG("Executing '%s'\n", query);
-	if (params && params->cur) {
-	        pgres = PQexecParams(CON_CONNECTION(con), query,
-				     params->cur, params->types,
-				     params->data, params->len,
-				     params->formats, 1);
-	} else {
-		pgres = PQexecParams(CON_CONNECTION(con), query, 0, 0, 0, 0, 0, 1);
-	}
-	switch(PQresultStatus(pgres)) {
-	case PGRES_EMPTY_QUERY:
-		ERR("BUG: db_raw_query received an empty query\n");
-		goto error;
-		
-	case PGRES_COMMAND_OK:
-	case PGRES_NONFATAL_ERROR:
-	case PGRES_TUPLES_OK:
-		     /* Success */
-		break;
-		
-	case PGRES_COPY_OUT:
-	case PGRES_COPY_IN:
-		ERR("Unsupported transfer mode\n");
-		goto error;
-
-	case PGRES_BAD_RESPONSE:
-	case PGRES_FATAL_ERROR:
-		ERR("Error: %s", PQresultErrorMessage(pgres));
-		if (PQstatus(CON_CONNECTION(con)) != CONNECTION_BAD) {
-				goto error;
-		}
-		ERR("Bad connection\n");
-		PQclear(pgres);
-		return 1;
-	}
-
-	if (res) {
-		*res = pg_new_result(pgres);
-		if (!(*res)) goto error;
-	} else {
-		PQclear(pgres);
-	}
-	return 0;
-
- error:
-	PQclear(pgres);
-	return -1;
-}
-
-
-static int reconnect(db_con_t* con)
-{
-	int attempts_left = reconnect_attempts;
-	while(attempts_left) {
-		ERR("Trying to recover the connection\n");
-		PQreset(CON_CONNECTION(con));
-		if (PQstatus(CON_CONNECTION(con)) == CONNECTION_OK) {
-			ERR("Successfuly reconnected\n");
-			return 0;
-		}
-		ERR("Reconnect attempt failed\n");
-		attempts_left--;
-	}
-        ERR("No more reconnect attempts left, giving up\n");
-	return -1;
-}
-
-
-/*
- * Query table for specified rows
- * con:   structure representing database connection
- * keys:  key names
- * ops:   operators
- * vals:  values of the keys that must match
- * cols:  column names to return
- * n:     number of key=values pairs to compare
- * ncol:  number of columns to return
- * order: order by the specified column
- * res:   query result
- */
-int pg_query(db_con_t* con, db_key_t* keys, db_op_t* ops,
-	     db_val_t* vals, db_key_t* cols, int n, int ncols,
-	     db_key_t order, db_res_t** res)
-{
-	int ret;
-	char* select;
-	struct pg_params* params;
-	
-	params = 0;
-	select = 0;
-	if (res) *res = 0;
-
-	if (!con) {
-		ERR("Invalid parameter value\n");
-		return -1;
-	}
-
-	select = print_select(con, cols, keys, vals, n, ncols, ops, order);
-	if (!select) goto err;
-
-	params = new_pg_params(n);
-	if (!params) goto err;
-	if (params_add(params, con, vals, n, 1) < 0) goto err;
-
-	do {
-		ret = submit_query(res, con, select, params);
-		if (ret < 0) goto err;                       /* Unknown error, bail out */
-		if (ret > 0) {                               /* Disconnected, try to reconnect */
-			if (reconnect(con) < 0) goto err;    /* Failed to reconnect */
-			else continue;                       /* Try one more time (ret is > 0) */
-		}
-	} while(ret != 0);
-	
-	if (res && pg_convert_result(*res, con) < 0) {
-		pg_free_result(*res);
-		goto err;
-	}
-
-	free_pg_params(params);
-	pkg_free(select);
-	return 0;
-
- err:
-	if (params) free_pg_params(params);
-	if (select) pkg_free(select);
-	return -1;
-}
-
-
-/*
- * Execute a raw SQL query
- */
-int pg_raw_query(db_con_t* con, char* query, db_res_t** res)
-{
-	int ret;
-
-	if (!con || !query) {
-		ERR("Invalid parameter value\n");
-		return -1;
-	}
-
-	do {
-		ret = submit_query(res, con, query, 0);
-		if (ret < 0) return -1;                      /* Unknown error, bail out */
-		if (ret > 0) {                               /* Disconnected, try to reconnect */
-			if (reconnect(con) < 0) return -1;   /* Failed to reconnect */
-			else continue;                       /* Try one more time (ret is > 0) */
-		}
-	} while(ret != 0);
-	
-	if (res && (pg_convert_result(*res, con) < 0)) {
-		pg_free_result(*res);
-		return -1;
-	}
-	return 0;
-}
-
-
-/*
- * Insert a row into specified table
- * con:  structure representing database connection
- * keys: key names
- * vals: values of the keys
- * n:    number of key=value pairs
- */
-int pg_insert(db_con_t* con, db_key_t* keys, db_val_t* vals, int n)
-{
-	int ret;
-	char* insert;
-	struct pg_params* params;
-
-	if (!con || !keys || !vals || !n) {
-		ERR("Invalid parameter value\n");
-		return -1;
-	}
-
-	params = 0;
-	insert = 0;
-
-	insert = print_insert(con, keys, n);
-	if (!insert) goto err;
-
-	params = new_pg_params(n);
-	if (!params) goto err;
-	if (params_add(params, con, vals, n, 0) < 0) goto err;
-
-	do {
-		ret = submit_query(0, con, insert, params);
-		if (ret < 0) goto err;                       /* Unknown error, bail out */
-		if (ret > 0) {                               /* Disconnected, try to reconnect */
-			if (reconnect(con) < 0) goto err;    /* Failed to reconnect */
-			else continue;                       /* Try one more time (ret is > 0) */
-		}
-	} while(ret != 0);
-	
-	free_pg_params(params);
-	pkg_free(insert);
-	return 0;
-
- err:
-	if (params) free_pg_params(params);
-	if (insert) pkg_free(insert);
-	return -1;
-}
-
-
-/*
- * Delete a row from the specified table
- * con : structure representing database connection
- * keys: key names
- * ops : operators
- * vals: values of the keys that must match
- * n   : number of key=value pairs
- */
-int pg_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals, int n)
-{
-	int ret;
-	char* delete;
-	struct pg_params* params;
-
-	if (!con) {
-		ERR("Invalid parameter value\n");
-		return -1;
-	}
-
-	params = 0;
-	delete = 0;
-
-        delete = print_delete(con, keys, ops, vals, n);
-	if (!delete) goto err;
-
-	params = new_pg_params(n);
-	if (!params) goto err;
-	if (params_add(params, con, vals, n, 1) < 0) goto err;
-
-	do {
-		ret = submit_query(0, con, delete, params);
-		if (ret < 0) goto err;                       /* Unknown error, bail out */
-		if (ret > 0) {                               /* Disconnected, try to reconnect */
-			if (reconnect(con) < 0) goto err;    /* Failed to reconnect */
-			else continue;                       /* Try one more time (ret is > 0) */
-		}
-	} while(ret != 0);
-	
-	free_pg_params(params);
-	pkg_free(delete);
-	return 0;
-
- err:
-	if (params) free_pg_params(params);
-	if (delete) pkg_free(delete);
-	return -1;
-}
-
-
-/*
- * Update some rows in the specified table
- * con  : structure representing database connection
- * keys : key names
- * ops  : operators
- * vals : values of the keys that must match
- * ucols: updated columns
- * uvals: updated values of the columns
- * n    : number of key=value pairs
- * un   : number of columns to update
- */
-int pg_update(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals,
-	      db_key_t* ucols, db_val_t* uvals, int n, int un)
-{
-	int ret;
-	char* update;
-	struct pg_params* params;
-
-	if (!con || !ucols || !uvals || !un) {
-		ERR("Invalid parameter value\n");
-		return -1;
-	}
-
-	params = 0;
-	update = 0;
-
-	update = print_update(con, ucols, keys, ops, vals, un, n);
-	if (!update) goto err;
-
-	params = new_pg_params(n + un);
-	if (!params) goto err;
-	if (params_add(params, con, uvals, un, 0) < 0) goto err;
-	if (params_add(params, con, vals, n, 1) < 0) goto err;
-
-	do {
-		ret = submit_query(0, con, update, params);
-		if (ret < 0) goto err;                       /* Unknown error, bail out */
-		if (ret > 0) {                               /* Disconnected, try to reconnect */
-			if (reconnect(con) < 0) goto err;    /* Failed to reconnect */
-			else continue;                       /* Try one more time (ret is > 0) */
-		}
-	} while(ret != 0);
-
-	free_pg_params(params);
-	pkg_free(update);
-	return 0;
-
- err:
-	if (params) free_pg_params(params);
-	if (update) pkg_free(update);
-	return -1;
-}
-
-
-/*
- * Release a result set from memory
- */
-int pg_db_free_result(db_con_t* con, db_res_t* res)
-{
-	pg_free_result(res);
-	return 0;
-}

+ 0 - 102
modules/db_postgres/dbase.h

@@ -1,102 +0,0 @@
-/*
- * $Id$
- *
- * Postgres module core functions
- *
- * Portions Copyright (C) 2001-2005 FhG FOKUS
- * Copyright (C) 2003 August.Net Services, LLC
- * Portions Copyright (C) 2005 iptelorg GmbH
- *
- * This file is part of ser, a free SIP server.
- *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License 
- * along with this program; if not, write to the Free Software 
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-
-#ifndef _DBASE_H
-#define _DBASE_H
-
-
-#include "../../db/db_con.h"
-#include "../../db/db_res.h"
-#include "../../db/db_key.h"
-#include "../../db/db_op.h"
-#include "../../db/db_val.h"
-
-
-/*
- * Initialize database connection
- */
-db_con_t* pg_init(const char* uri);
-
-
-/*
- * Close a database connection
- */
-void pg_close(db_con_t* con);
-
-
-/*
- * Free all memory allocated by get_result
- */
-int pg_db_free_result(db_con_t* con, db_res_t* res);
-
-
-/*
- * Do a query
- */
-int pg_query(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals, db_key_t* cols, int n, int nc,
-	     db_key_t order, db_res_t** res);
-
-
-/*
- * Raw SQL query
- */
-int pg_raw_query(db_con_t* con, char* query, db_res_t** res);
-
-
-/*
- * Insert a row into table
- */
-int pg_insert(db_con_t* con, db_key_t* keys, db_val_t* vals, int n);
-
-
-/*
- * Delete a row from table
- */
-int pg_delete(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals, int n);
-
-
-/*
- * Update a row in table
- */
-int pg_update(db_con_t* con, db_key_t* keys, db_op_t* ops, db_val_t* vals,
-	      db_key_t* ucols, db_val_t* uvals, int n, int un);
-
-
-
-/*
- * Store name of table that will be used by
- * subsequent database functions
- */
-int pg_use_table(db_con_t* con, const char* table);
-
-
-#endif /* _DBASE_H */

+ 0 - 83
modules/db_postgres/pg_type.h

@@ -1,83 +0,0 @@
-/*-------------------------------------------------------------------------
- *
- * pg_type.h
- *	  definition of the system "type" relation (pg_type)
- *	  along with the relation's initial contents.
- *
- *
- * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- * $PostgreSQL: pgsql/src/include/catalog/pg_type.h,v 1.158 2004/12/31 22:03:26 pgsql Exp $
- *
- * NOTES
- *	  the genbki.sh script reads this file and generates .bki
- *	  information from the DATA() statements.
- *
- *-------------------------------------------------------------------------
- */
-#ifndef _PG_TYPE_H
-#define _PG_TYPE_H
-
-/* OIDS 1 - 99 */
-
-#define BOOLOID	         16  /* boolean, 'true'/'false' */
-#define BYTEAOID         17  /* variable-length string, binary values escaped" */
-#define CHAROID	         18  /* single character */
-#define NAMEOID	         19  /* 63-character type for storing system identifiers */
-#define INT8OID	         20  /* ~18 digit integer, 8-byte storage */
-#define INT2OID	         21  /* -32 thousand to 32 thousand, 2-byte storage */
-#define INT2VECTOROID    22  /* array of INDEX_MAX_KEYS int2 integers, used in system tables */
-#define INT4OID	         23  /* -2 billion to 2 billion integer, 4-byte storage */
-#define REGPROCOID       24  /* registered procedure */
-#define TEXTOID	         25  /* variable-length string, no limit specified */
-#define OIDOID	         26  /* object identifier(oid), maximum 4 billion */
-#define TIDOID	         27  /* (Block, offset), physical location of tuple */
-#define XIDOID           28  /* transaction id */
-#define CIDOID           29  /* command identifier type, sequence in transaction id */
-#define OIDVECTOROID     30  /* array of INDEX_MAX_KEYS oids, used in system tables */
-
-/* OIDS 700 - 799 */
-
-#define FLOAT4OID       700  /* single-precision floating point number, 4-byte storage */
-#define FLOAT8OID       701  /* double-precision floating point number, 8-byte storage */
-#define ABSTIMEOID      702  /* absolute, limited-range date and time (Unix system time) */
-#define RELTIMEOID      703  /* relative, limited-range time interval (Unix delta time) */
-#define TINTERVALOID    704  /* (abstime,abstime), time interval */
-#define UNKNOWNOID      705
-#define CIRCLEOID       718  /* geometric circle '(center,radius)' */
-#define CASHOID         790  /* monetary amounts, $d,ddd.cc */
-
-/* OIDS 800 - 899 */
-
-#define MACADDROID      829  /* XX:XX:XX:XX:XX:XX, MAC address */
-#define INETOID         869  /* IP address/netmask, host address, netmask optional */
-#define CIDROID         650  /* network IP address/netmask, network address */
-
-/* OIDS 1000 - 1099 */
-
-#define BPCHAROID      1042  /* char(length), blank-padded string, fixed storage length */
-#define VARCHAROID     1043  /* varchar(length), non-blank-padded string, variable storage length */
-#define DATEOID	       1082  /* ANSI SQL date */
-#define TIMEOID	       1083  /* hh:mm:ss, ANSI SQL time */
-
-/* OIDS 1100 - 1199 */
-
-#define TIMESTAMPOID   1114  /* date and time */
-#define TIMESTAMPTZOID 1184  /* date and time with time zone */
-#define INTERVALOID    1186  /* @ <number> <units>, time interval */
-
-/* OIDS 1200 - 1299 */
-
-#define TIMETZOID      1266  /* hh:mm:ss, ANSI SQL time */
-
-/* OIDS 1500 - 1599 */
-
-#define BITOID	       1560  /* fixed-length bit string */
-#define VARBITOID      1562  /* variable-length bit string */
-
-/* OIDS 1700 - 1799 */
-
-#define NUMERICOID     1700  /* numeric(precision, decimal), arbitrary precision number */
-
-#endif /* _PG_TYPE_H */

+ 0 - 413
modules/db_postgres/res.c

@@ -1,413 +0,0 @@
-/* 
- * $Id$ 
- *
- * Postgres module result related functions
- *
- * Portions Copyright (C) 2001-2003 FhG FOKUS
- * Copyright (C) 2003 August.Net Services, LLC
- * Portions Copyright (C) 2005 iptelorg GmbH
- *
- * This file is part of ser, a free SIP server.
- *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License 
- * along with this program; if not, write to the Free Software 
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#include "../../mem/mem.h"
-#include "../../dprint.h"
-#include "pg_type.h"
-#include "pg_con.h"
-#include "db_mod.h"
-#include "res.h"
-#include <netinet/in.h>
-#include <string.h>
-
-
-/*
- * Get and convert columns from a result
- */
-static inline int get_columns(db_res_t* res)
-{
-	PGresult* pgres;
-	int n, i, type;
-
-	if (!res) {
-	        ERR("Invalid parameter\n");
-		goto err;
-	}
-
-	pgres = (PGresult*)res->data;
-	if (!pgres) {
-		ERR("No postgres result found\n");
-		goto err;
-	}
-
-	n = PQnfields(pgres);
-	if (!n) {
-		ERR("No columns\n");
-		goto err;
-	}
-
-	if (n == 0) return 0;
-
-        res->col.names = (db_key_t*)pkg_malloc(sizeof(db_key_t) * n);
-	if (!res->col.names) {
-		ERR("No memory left\n");
-		goto err;
-	}
-
-	res->col.types = (db_type_t*)pkg_malloc(sizeof(db_type_t) * n);
-	if (!res->col.types) {
-		ERR("No memory left\n");
-		goto err;
-	}
-
-	res->col.n = n;
-
-	for(i = 0; i < n; i++) {
-		if (PQfformat(pgres, i) == 0) {
-			ERR("Text format of columns not supported\n");
-			goto err;
-		}
-
-		res->col.names[i] = PQfname(pgres, i);
-		type = PQftype(pgres, i);
-		switch(type) {
-		case BOOLOID:    /* boolean, 'true'/'false' */
-		case INT2OID:    /* -32 thousand to 32 thousand, 2-byte storage */
-		case INT4OID:    /* -2 billion to 2 billion integer, 4-byte storage */
-		case INT8OID:    /* ~18 digit integer, 8-byte storage */
-			res->col.types[i] = DB_INT;
-			break;
-
-		case FLOAT4OID:  /* single-precision floating point number, 4-byte storage */
-			res->col.types[i] = DB_FLOAT;
-			break;
-
-		case FLOAT8OID:  /* double-precision floating point number, 8-byte storage */
-			res->col.types[i] = DB_DOUBLE;
-			break;
-
-		case TIMESTAMPOID:   /* date and time */
-		case TIMESTAMPTZOID: /* date and time with time zone */
-			res->col.types[i] = DB_DATETIME;
-			break;
-
-		case CHAROID:    /* single character */
-		case TEXTOID:    /* variable-length string, no limit specified */
-		case BPCHAROID:  /* char(length), blank-padded string, fixed storage length */
-		case VARCHAROID: /* varchar(length), non-blank-padded string, variable storage length */
-			res->col.types[i] = DB_STRING;
-			break;
-
-		case BYTEAOID: /* variable-length string, binary values escaped" */
-			res->col.types[i] = DB_BLOB;
-			break;
-
-		case BITOID:    /* fixed-length bit string */
-		case VARBITOID: /* variable-length bit string */
-			res->col.types[i] = DB_BITMAP;
-			break;
-
-		default:
-			ERR("Unsupported column type with oid %d\n", type);
-			goto err;
-		}
-	}
-	return 0;
-
- err:
-	if (res->col.types) pkg_free(res->col.types);
-	if (res->col.names) pkg_free(res->col.names);
-	res->col.types = 0;
-	res->col.names = 0;
-	return -1;
-}
-
-
-/*
- * Release memory used by columns
- */
-static inline int free_columns(db_res_t* res)
-{
-	if (!res) {
-		ERR("Invalid parameter\n");
-		return -1;
-	}
-
-	if (res->col.names) pkg_free(res->col.names);
-	if (res->col.types) pkg_free(res->col.types);
-	return 0;
-}
-
-
-/*
- * Release memory used by rows
- */
-static inline void free_rows(db_res_t* res)
-{
-	int r;
-
-	if (!res->rows) return;
-
-	for(r = 0; r < res->n; r++) {
-		pkg_free(res->rows[r].values);
-	}
-	
-	pkg_free(res->rows);
-}
-
-
-static inline int convert_cell(db_con_t* con, db_res_t* res, int row, int col)
-{
-	static str dummy_str = STR_STATIC_INIT("");
-	PGresult* pgres;
-	db_val_t* val;
-	int type, pglen;
-	const char* pgval;
-	union {
-		int i4;
-		long long i8;
-		float f4;
-		double f8;
-		char c[8];
-	} tmp;
-
-	val = &res->rows[row].values[col];
-	pgres = (PGresult*)res->data;
-
-	val->type = res->col.types[col];
-
-	if (PQgetisnull(pgres, row, col)) {
-		val->nul = 1;
-		switch(res->col.types[col]) {
-		case DB_INT:      val->val.int_val = 0;              break;
-		case DB_FLOAT:    val->val.float_val = 0;            break;
-		case DB_DOUBLE:   val->val.double_val = 0;           break;
-		case DB_STRING:   val->val.string_val = dummy_str.s; break;
-		case DB_STR:      val->val.str_val = dummy_str;      break;
-		case DB_DATETIME: val->val.time_val = 0;             break;
-		case DB_BLOB:     val->val.blob_val = dummy_str;     break;
-		case DB_BITMAP:   val->val.bitmap_val = 0;           break;
-		}
-		return 0;
-	}
-
-	val->nul = 0;
-	type = PQftype(pgres, col);
-	pgval = PQgetvalue(pgres, row, col);
-	pglen = PQgetlength(pgres, row, col);
-	
-	     /* Postgres delivers binary parameters in network byte order,
-	      * thus we have to convert them to host byte order. All data 
-	      * returned by PQgetvalue is zero terminated. Memory allocator
-	      * in libpq aligns data in memory properly so reading multibyte
-	      * values from memory at once is safe.
-	      */
-	switch(type) {
-	case BOOLOID: 
-		val->val.int_val = *pgval; 
-		break;
-
-	case INT2OID: 
-		val->val.int_val = ntohs(*(unsigned short*)pgval);
-		break;
-
-	case FLOAT4OID:
-		     /* FLOAT4 will be stored in (8-byte) double */
-		     /* FIXME: More efficient implementation could be done here
-		      * provided that we know that the numbers are stored in IEEE 754
-		      */
-		tmp.i4 = ntohl(*(unsigned int*)pgval);
-		val->val.double_val = tmp.f4;
-		break;
-
-	case INT4OID: 
-		val->val.int_val = ntohl(*(unsigned int*)pgval);
-		break;
-		
-	case INT8OID: 
-		val->val.int_val = ntohl(*(unsigned int*)(pgval + 4));
-		break; 
-
-	case FLOAT8OID:
-		tmp.i8 = (((unsigned long long)ntohl(*(unsigned int*)pgval)) << 32) + 
-			(unsigned int)ntohl(*(unsigned int*)(pgval + 4));
-		val->val.double_val = tmp.f8;
-		break;
-
-	case TIMESTAMPOID:
-	case TIMESTAMPTZOID:
-		tmp.i8 = (((unsigned long long)ntohl(*(unsigned int*)pgval)) << 32) + 
-			(unsigned int)ntohl(*(unsigned int*)(pgval + 4));
-		if (CON_FLAGS(con) & PG_INT8_TIMESTAMP) {
-			     /* int8 format */
-			val->val.time_val = tmp.i8 / 1000000 + PG_EPOCH_TIME;
-		} else {
-			     /* double format */
-			val->val.time_val = PG_EPOCH_TIME + (long long)tmp.f8;
-		}
-		break;
-		
-	case CHAROID:    /* single character */
-	case TEXTOID:    /* variable-length string, no limit specified */
-	case BPCHAROID:  /* char(length), blank-padded string, fixed storage length */
-	case VARCHAROID: /* varchar(length), non-blank-padded string, variable storage length */
-		val->val.str_val.s = (char*)pgval;
-		val->val.str_val.len = pglen;
-		break;
-		
-	case BYTEAOID: /* variable-length string, binary values escaped" */
-		val->val.blob_val.s = (char*)pgval;
-		val->val.blob_val.len = pglen;
-		break;
-		
-	case BITOID:    /* fixed-length bit string */
-	case VARBITOID: /* variable-length bit string */
-		if (ntohl(*(unsigned int*)pgval) != 32) {
-			ERR("Only 32-bit long bitfieds supported\n");
-			return -1;
-		}
-		val->val.bitmap_val = ntohl(*(unsigned int*)(pgval + 4));
-		break;
-		
-	default:
-		ERR("Unsupported column type with oid %d\n", type);
-		return -1;
-		
-	}
-
-	return 0;
-}
- 
- 
-/*
- * Convert rows from postgres to db API representation
- */
-static inline int convert_rows(db_con_t* con, db_res_t* res)
-{
-	db_row_t* row;
-	int r, c;
-
-	if (!res) {
-		ERR("Invalid parameter\n");
-		return -1;
-	}
-
-	res->n = PQntuples((PGresult*)res->data); /* Number of rows */
-
-	     /* Assert: number of columns is > 0, otherwise get_columns would fail */
-	if (!res->n) {
-		res->rows = 0;
-		return 0;
-	}
-
-	res->rows = (struct db_row*)pkg_malloc(sizeof(db_row_t) * res->n);
-	if (!res->rows) {
-		ERR("No memory left\n");
-		goto err;
-	}
-
-	for(r = 0; r < res->n; r++) {
-		row = &res->rows[r];
-		row->values = (db_val_t*)pkg_malloc(sizeof(db_val_t) * res->col.n);
-		if (!row->values) {
-			ERR("No memory left to allocate row\n");
-			res->n = r; /* This is to make sure that the cleanup function release only rows
-				     * that has been really allocated
-				     */
-			goto err;
-		}
-		row->n = res->col.n;
-
-		for(c = 0; c < row->n; c++) {
-			if (convert_cell(con, res, r, c) < 0) {
-				row->n = c;
-				res->n = r;
-				goto err;
-			}
-		}
-	}
-
-	return 0;
-
- err:
-	     /*	free_rows(res); Do not free here, pg_free_result will take care of it */
-	return -1;
-}
-
-
-/*
- * Create a new result structure and initialize it
- */
-db_res_t* pg_new_result(PGresult* pgres)
-{
-	db_res_t* r;
-	r = (db_res_t*)pkg_malloc(sizeof(db_res_t));
-	if (!r) {
-		ERR("No memory left\n");
-		return 0;
-	}
-
-	memset(r, 0, sizeof(db_res_t));
-	r->data = pgres;
-	return r;
-}
-
-
-/*
- * Fill the structure with data from database
- */
-int pg_convert_result(db_res_t* res, db_con_t* con)
-{
-	if (!res) {
-		ERR("Invalid parameter\n");
-		return -1;
-	}
-
-	if (get_columns(res) < 0) {
-		ERR("Error while getting column names\n");
-		return -2;
-	}
-
-	if (convert_rows(con, res) < 0) {
-		ERR("Error while converting rows\n");
-		     /* Do not free columns here, pg_free_result will do it */
-		return -3;
-	}
-	return 0;
-}
-
-
-/*
- * Release memory used by a result structure
- */
-int pg_free_result(db_res_t* res)
-{
-	if (!res) {
-		ERR("Invalid parameter\n");
-		return -1;
-	}
-
-	free_columns(res);
-	free_rows(res);
-	if (res->data) PQclear((PGresult*)res->data);
-	pkg_free(res);
-	return 0;
-}

+ 0 - 57
modules/db_postgres/res.h

@@ -1,57 +0,0 @@
-/* 
- * $Id$ 
- *
- * Postgres module result related functions
- *
- * Portions Copyright (C) 2001-2003 FhG FOKUS
- * Copyright (C) 2003 August.Net Services,, LLC
- * Portions Copyright (C) iptelorg GmbH
- *
- * This file is part of ser, a free SIP server.
- *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License 
- * along with this program; if not, write to the Free Software 
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#ifndef _RES_H
-#define _RES_H
-
-#include <libpq-fe.h>
-#include "../../db/db_res.h"
-#include "../../db/db_con.h"
-
-/*
- * Create a new result structure and initialize it
- */
-db_res_t* pg_new_result(PGresult* pgres);
-
-
-/*
- * Fill the structure with data from database
- */
-int pg_convert_result(db_res_t* res, db_con_t* con);
-
-
-/*
- * Release memory used by a result structure
- */
-int pg_free_result(db_res_t* res);
-
-
-#endif /* _RES_H */

+ 0 - 783
modules/db_postgres/serpgctl

@@ -1,783 +0,0 @@
-#!/bin/sh 
-#
-# $Id$
-#
-# sc: ser control; tool for maintaining ser's databases
-#
-# History:
-# --------
-# 2003-04-06 this is a hack, from serctl, to make work with postgres
-# 2003-02-23 Thomas's start|stop commands checked in
-#
-# To-DO:
-# -----
-# - generalization for other than mysql databases
-# - front-end to updating administrative mysql password would
-#   be a convenient thing to have
-#
-# I copy this over the top of the existing serctl program.
-#
-
-
-
-# configuration for starting/stopping ser
-PID_FILE=/var/run/ser.pid
-SYSLOG=1 # 0=output to console, 1=output to syslog
-STARTOPTIONS= # for example -dddd
-
-# ser's FIFO server
-if [ -z "$SER_FIFO" ]; then
-	SER_FIFO=/tmp/ser_fifo
-fi
-# period in which stats are reprinted
-if [ -z "$WATCH_PERIOD" ] ; then
-	WATCH_PERIOD=2
-fi
-PW="test"
-
-# SQL config
-if [ -z "$SQL_DB" ] ; then
-	SQL_DB=ser
-fi
-if [ -z "$SQL_HOST" ] ; then
-	SQL_HOST=localhost
-fi
-if [ -z "$SQL_PORT" ] ; then
-	SQL_PORT=5432
-fi
-if [ -z "$SQL_USER" ] ; then
-	SQL_USER=ser
-fi
-
-# the read-only user for whom password may be stored here
-if [ -z "$RO_USER" ] ; then
-	RO_USER=serro
-fi
-if [ -z "$RO_PW" ] ; then
-	RO_PW=47serro11
-fi
-
-# binaries
-GENHA1='gen_ha1'
-PGSQL='psql'
-SER='sr'
-LAST_LINE='tail -1'
-
-# ACL name verification
-VERIFY_ACL=1
-ACL_GROUPS="local ld int voicemail free-pstn"
-
-# expiration time for alias table
-FOREVER='2020-05-28 21:32:15'
-FOREVER_REL=1073741823
-
-VERSION='$Revision$'
-#### SQL names
-
-# Usr Loc Table
-USRLOC=location
-USER_COLUMN=username
-CALLID_COLUMN=callid
-
-# subscriber table
-TABLE=subscriber
-REALM_COLUMN=domain
-HA1_COLUMN=HA1
-HA1B_COLUMN=HA1B
-PASSWORD_COLUMN=password
-SUBSCRIBER_COLUMN='username'
-EMAIL_COLUMN=email_address
-SUB_CREATED_COLUMN=datetime_created
-SUB_MODIFIED_COLUMN=datetime_modified
-PHP_LIB_COLUMN=phplib_id
-
-# acl table
-ACL_TABLE=grp
-ACL_USER_COLUMN=user
-ACL_GROUP_COLUMN=grp
-ACL_MODIFIED_COLUMN=last_modified
-
-# aliases table
-A_TABLE=aliases
-A_USER_COLUMN=user
-A_CONTACT_COLUMN=contact
-A_EXPIRES_COLUMN=expires
-A_Q_COLUMN=q
-A_CALLID_COLUMN=callid
-A_CSEQ_COLUMN=cseq
-A_LAST_MODIFIED_COLUMN=last_modified
-
-FIFO_DBG=0
-#===================================================================
-
-
-usage() {
-CMD=`basename $0`
-if [ "0$VERIFY_ACL" -eq 1 ] ; then
-	EXTRA_TEXT="ACL privileges are: $ACL_GROUPS"
-fi
-cat <<EOF
-$0 $VERSION
-usage: 
-           * subscribers *
- $CMD add <username> <password> <email> .. add a new subscriber (*)
- $CMD passwd <username> <passwd> ......... change user's password (*)
- $CMD rm <username> ...................... delete a user (*)
- $CMD mail <username> .................... send an email to a user
- $CMD alias show [<alias>] ............... show aliases
- $CMD alias rm <alias> ................... remove an alias
- $CMD alias add <alias> <uri> ............ add an aliases 
-
-           * access control lists *
- $CMD acl show [<username>] .............. show user membership
- $CMD acl grant <username> <group> ....... grant user membership (*)
- $CMD acl revoke <username> [<group>] .... grant user membership(s) (*)
-
-           * usrloc *
- $CMD ul show [<username>]................ show in-RAM online users
- $CMD ul rm <username> ................... delete user's UsrLoc entries
- $CMD ul add <username> <uri> ............ introduce a permanent UrLoc entry
- $CMD showdb [<username>] ................ show online users flushed in DB
-
-           * server health *
- $CMD monitor ............................ show internal status
- $CMD ps ................................. show runnig processes 
- $CMD fifo ............................... send raw commands to FIFO
-
-           * server control *
- $CMD start .............................. start ser
- $CMD stop ............................... stop ser
- $CMD restart ............................ restart ser
-
-
-   Commands labeled with (*) will prompt for a MySQL password.
-   If the variable PW is set, the password will not be prompted.
-
-     $EXTRA_TEXT
-
-EOF
-}
-
-# check the parameter if it is a valid SIP URI
-# quite simplified now -- it captures just very basic
-# errors
-check_uri() {
-	echo "$1" | grep -E "^sip:([a-zA-Z0-9_]+@)?.*\..*" 
-}
-
-
-#params: none
-# output: PW
-prompt_pw() {
-	if [ -z "$PW" ] ; then
-		savetty=`stty -g`
-		printf "Postgres password: "
-		stty -echo
-    	read PW
-		stty $savetty
-    	echo
-	fi
-}
-
-# process output from FIFO server; if everything is ok
-# skip the first "ok" line and proceed to returned 
-# parameters
-filter_fl()
-{
-#	tail +2
-	
-	awk 'BEGIN {line=0;IGNORECASE=1;}
-		{line++}
-		line==1 && /^200 ok/ { next }
-		{ print }'
-}
-
-
-fifo_cmd()
-{
-	if [ "0${FIFO_DBG}" -eq 1 ] ; then
-		echo "entering fifo_cmd $*"
-	fi
-	if [ "$#" -lt 1 ]; then
-		echo "ERROR: fifo_cmd must take at least command name as parameter"
-		exit
-	fi
-	name=ser_receiver_$$
-	path=/tmp/$name
-	if [ ! -w $SER_FIFO ]; then
-		echo "Error opening ser's FIFO $SER_FIFO"
-		echo "Make sure you have line fifo=$SER_FIFO in your config"
-		exit 1
-	fi
-	mkfifo $path
-	if [ $? -ne 0 ] ; then
-		echo "error opening read fifo $path"
-		exit 1
-	fi
-	chmod a+w $path
-
-	# construct the command now
-	CMD=":$1:$name\n";
-	shift
-	while [ -n "$1" ] ; do
-		CMD="${CMD}${1}\n"
-		shift
-	done
-	CMD="${CMD}\n"
-
-	trap "rm -f $path; exit 1" 2
-
-	# start reader now so that it is ready for replies
-	# immediately after a request was sent out
-	cat < $path | filter_fl &
-
-	# issue FIFO request (printf taken to deal with \n)
-	printf "$CMD" > $SER_FIFO
-
-	# wait for the reader to complete
-	wait
-	rm $path
-
-	if [ "0${FIFO_DBG}" -eq 1 ] ; then
-		printf "FIFO command was:\n$CMD"
-	fi
-}
-
-
-# $1 = name $2=path $3=attempt
-print_stats() {
-
-echo "[cycle #: $3; if constant make sure server lives and fifo is on]"
-
-cat < $2 | filter_fl &
-cat > $SER_FIFO <<EOF
-:version:$1
-
-EOF
-wait
-
-cat < $2 | filter_fl &
-cat > $SER_FIFO << EOF
-:uptime:$1
-
-EOF
-wait
-echo
-
-echo Transaction Statistics
-cat < $2 | filter_fl &
-cat > $SER_FIFO <<EOF
-:t_stats:$1
-
-EOF
-wait
-echo
-
-echo Stateless Server Statistics
-cat < $2 | filter_fl &
-cat > $SER_FIFO <<EOF
-:sl_stats:$1
-
-EOF
-wait
-echo
-
-echo UsrLoc Stats
-cat < $2 | filter_fl &
-cat > $SER_FIFO <<EOF
-:ul_stats:$1
-
-EOF
-wait
-}
-
-
-# input: sql query, optional mysql command-line params
-sql_query() {
-	# if password not yet queried, query it now
-	if [ -z "$PW" ] ; then
-		savetty=`stty -g`
-		printf "MySql password: "
-		stty -echo
-    	read PW >&2
-		stty $savetty
-    	echo >&2
-	fi
-	$PGSQL $2\
-		-A -q -t \
-		-P fieldsep="	" \
-		-h $SQL_HOST \
-		-U $SQL_USER \
-		$SQL_DB \
-		-c "$1"
-}
-
-# input: sql query, optional mysql command-line params
-sql_ro_query() {
-	$PGSQL $2\
-		-h $SQL_HOST \
-		-U $SQL_USER \
-		$SQL_DB \
-		-c "$1"
-}
-
-# input: sql query, optional mysql command-line params
-sql_ro_raw_query() {
-	$PGSQL $2\
-		-A -q -t \
-		-P fieldsep="	" \
-		-h $SQL_HOST \
-		-U $SQL_USER \
-		$SQL_DB \
-		-c "$1"
-}
-
-
-usrloc() {
-	if [ "$#" -lt 2 ] ; then
-		echo "usrloc: too few parameters"
-		exit 1
-	fi
-	if [ "$1" = "alias" ] ; then
-		TABLE="$A_TABLE"
-	elif [ "$1" = "ul" ] ; then
-		TABLE="$USRLOC"
-	else
-		echo "usrloc: unknown table name"
-		exit 1
-	fi
-	shift
-
-	case $1 in 
-		show)
-			if [ $# -eq 2 ] ; then
-				fifo_cmd ul_show_contact $TABLE $2
-			elif [ $# -eq 1 ] ; then
-				fifo_cmd ul_dump
-			else
-				echo "wrong number of params for usrloc show"
-				usage
-				exit 1
-			fi
-			exit $?
-			;;
-		add)
-			if [ $# -ne 3 ] ; then
-				usage
-				exit 1
-			fi
-			shift
-			check_uri "$2"
-			if [ "$?" -ne "0" ] ; then
-				echo "$2 is not a valid URI"
-				exit 1
-			fi
-
-			fifo_cmd ul_add "$TABLE" "$1" "$2" "$FOREVER_REL" "1.00" "0"
-			exit $?
-			;;
-		rm)
-            if [ $# -ne 2 ] ; then
-                usage
-                exit 1
-            fi
-			shift
-			fifo_cmd ul_rm $TABLE $1
-
-            ;;
-
-		*)
-			usage
-			exit 1
-			;;
-	esac
-}
-
-acl() {
-	case $1 in
-		show)
-			if [ $# -eq 2 ] ; then
-				is_user $2
-				if [ $? -ne 0 ] ; then
-					echo non-existent user
-					exit 1;
-				fi
-				CLAUSE=" WHERE $ACL_USER_COLUMN='$2' "
-			elif [ $# -ne 1 ] ; then
-				usage
-				exit 1
-			fi
-			QUERY="select * FROM $ACL_TABLE $CLAUSE ; "
-			sql_ro_query "$QUERY"
-
-			;;
-
-		grant)
-			if [ $# -lt 3 ] ; then
-				usage
-				exit 1
-			fi
-			prompt_pw
-			is_user $2
-			if [ $? -ne 0 ] ; then
-				echo non-existent user
-				exit 1
-			fi
-			SIP_USER="$2"
-			shift 2
-			while [ $# -gt 0 ] ; do
-
-				if [ $VERIFY_ACL -eq 1 ] ; then
-					found=0
-					for i in $ACL_GROUPS ; do
-						if [ "$1" = "$i" ] ; then
-							found=1
-							break
-						fi
-					done	
-					if [ $found -eq 0 ] ; then
-						echo "Invalid privilege: $1 ignored"
-						shift
-						continue
-					fi
-				fi
-
-        		QUERY="insert into $ACL_TABLE \
-                	($ACL_USER_COLUMN,$ACL_GROUP_COLUMN,$ACL_MODIFIED_COLUMN) \
-                	values ('$SIP_USER','$1', now());"
-				sql_query "$QUERY"
-				if [ $? -ne 0 ] ; then
-					echo "SQL Error"
-					exit 1
-				fi
-				shift
-			done
-
-			$0 acl show $SIP_USER
-
-			;;
-
-		revoke)
-			if [ $# -eq 3 ] ; then
-				CLAUSE=" and $ACL_GROUP_COLUMN='$3' "
-			elif [ $# -ne 2 ] ; then
-				usage
-				exit 1
-			fi	
-
-			QUERY="delete from $ACL_TABLE where \
-				$ACL_TABLE.$ACL_USER_COLUMN='$2' $CLAUSE"
-			sql_query "$QUERY"
-
-			$0 acl show $2
-
-			;;
-
-		*)
-			usage
-			exit 1
-			;;
-	esac
-}
-
-# params: user
-# output: false if exists, true otherwise
-is_user() {
-	QUERY="select count(*) from $TABLE \
-		where $SUBSCRIBER_COLUMN='$1' and $REALM_COLUMN='$SIP_DOMAIN';"
-	CNT=`sql_ro_raw_query "$QUERY" | grep -v ERROR | $LAST_LINE`
-	if [ "0$CNT" -eq 0 ] ; then
-		false
-	else
-		true
-	fi
-
-}
-
-
-# params: user, password
-# output: HA1, HA1B
-credentials()
-{
-	HA1=`$GENHA1 $1 $SIP_DOMAIN $2`
-	if [ $? -ne 0 ] ; then
-		echo "HA1 calculation failed"
-		exit 1
-	fi
-	HA1B=`$GENHA1 "$1@$SIP_DOMAIN" $SIP_DOMAIN $2`
-	if [ $? -ne 0 ] ; then
-		echo "HA1B calculation failed"
-		exit 1
-	fi
-}
-
-#================================================================
-
-if [ -z "$SIP_DOMAIN" ] ; then
-	echo
-	echo "You need to set environment variable SIP_DOMAIN (e.g. to 'foobar.com') first"
-	echo
-#	This confuses new users cause its easy to miss the information above
-#	usage
-	exit 1
-fi
-
-# if the script calls itself ...
-export PW
-
-case $1 in
-
-	start)
-		DIR=`dirname $0`
-		echo
-		printf "Starting SER : "
-		if [ -r $PID_FILE ] ; then
-			echo "PID file exists! ($PID_FILE) already running?"
-			exit 1
-		else
-			if [ $SYSLOG = 1 ] ; then
-				$DIR/ser -P $PID_FILE $STARTOPTIONS 1>/dev/null 2>/dev/null
-			else
-			 	$DIR/ser -P $PID_FILE -E $STARTOPTIONS
-			fi
-			sleep 1
-			echo "started pid(`cat $PID_FILE`)"
-		fi
-		exit 0
-	;;
-
-	stop)
-		printf "Stopping SER : "
-		if [ -r $PID_FILE ] ; then
-			kill `cat $PID_FILE`
-			echo "stopped"
-		else
-			echo No PID file found!
-			exit 1
-		fi
-		exit 0
-	;;
-
-	restart)
-	        DIR=`dirname $0`
-		printf "Stopping SER : "
-		if [ -r $PID_FILE ] ; then
-			kill `cat $PID_FILE`
-			echo "stopped"
-		else
-			echo No PID file found! SER problably not running
-			exit 1
-		fi
-		sleep 2
-		printf "Starting SER : "
-		if [ -r $PID_FILE ] ; then
-			echo "PID file exists! ($PID_FILE) already running?"
-			exit 1
-		else
-			if [ $SYSLOG = 1 ] ; then
-			        $DIR/ser -P $PID_FILE $STARTOPTIONS 1>/dev/null 2>/dev/null
-			else
-			        $DIR/ser -P $PID_FILE -E $STARTOPTIONS
-			fi
-			sleep 1
-			echo "started pid(`cat $PID_FILE`)"
-		fi
-		exit 0
-	;;
-
-	passwd)
-		if [ $# -ne 3 ] ; then
-			usage
-			exit 1
-		fi
-		shift
-		credentials $1 $2
-		prompt_pw
-
-		is_user $1
-		if [ $? -ne 0 ] ; then
-			echo non-existent user
-			exit 1
-		fi
-		QUERY="update $TABLE \
-			set $HA1_COLUMN='$HA1', $HA1B_COLUMN='$HA1B', $PASSWORD_COLUMN='$2' \
-			, $SUB_MODIFIED_COLUMN=now() \
-			WHERE $SUBSCRIBER_COLUMN='$1' and $REALM_COLUMN='$SIP_DOMAIN';"
-		sql_query "$QUERY"
-		if [ $? -ne 0 ] ; then
-			echo "password change failed"
-		else
-			echo "password change succeeded"
-		fi
-
-		;;		
-			
-		
-
-	add)
-		if [ $# -ne 4 ] ; then
-			usage
-			exit 1
-		fi
-		shift
-		credentials $1 $2
-		prompt_pw
-        is_user $1
-        if [ $? -eq 0 ] ; then
-            echo user already exists
-            exit 1
-        fi
-
-		QUERY="insert into $TABLE \
-				($SUBSCRIBER_COLUMN,$REALM_COLUMN,$HA1_COLUMN,\
-				$HA1B_COLUMN,$PASSWORD_COLUMN,$EMAIL_COLUMN, $SUB_CREATED_COLUMN,  \
-				$PHP_LIB_COLUMN,datetime_modified ) \
-				values ('$1','$SIP_DOMAIN','$HA1','$HA1B','$2', '$3', now(), '$HA1', now() );";
-		sql_query "$QUERY"
-		if [ $? -ne 0 ] ; then
-			echo "introducing a new user to the database failed"
-		else
-			echo "new user added"
-		fi
-
-		;;
-
-	monitor|console|moni|con)
-		name=ser_receiver_$$
-		path=/tmp/$name
-		if [ ! -w $SER_FIFO ]; then
-			echo "Error opening ser's FIFO $SER_FIFO"
-			echo "Make sure you have line fifo=$SER_FIFO in your config"
-			exit 1
-		fi
-		mkfifo $path
-		if [ $? -ne 0 ] ; then
-			echo "error opening read fifo $path"
-			exit 1
-		fi
-		trap "rm $path;  clear; echo sc monitor ^C-ed; exit 1" 2
-		attempt=0
-		clear
-		while [ 1 -eq 1 ]; do
-			attempt=`expr $attempt + 1`
-			#clear
-			tput cup 0 0
-			print_stats $name $path $attempt
-			sleep $WATCH_PERIOD
-		done
-		rm $path
-		exit 0
-		;;
-
-	mail)
-		if [ $# -ne 2 ] ; then
-			usage
-			exit 1
-		fi
-		shift
-		QUERY="select $TABLE.$EMAIL_COLUMN from $TABLE where  \
-			$TABLE.$SUBSCRIBER_COLUMN='$1'"
-		EA=`sql_ro_raw_query "$QUERY" "-B" | grep -v ERROR | $LAST_LINE`
-		if [ $? -ne 0 ] ; then
-			echo "MySql query failed"
-			exit 1
-		fi
-		echo "Write email to $1: $EA now ..."
-		mail -s "Message from $SIP_DOMAIN SIP admin" $EA
-		if [ $? -eq 0 ] ; then
-			echo message sent
-		else
-			echo sending message failed
-		fi
-
-		;;
-
-	alias|ul)
-		usrloc "$@"
-		;;
-
-	online)
-		fifo_cmd ul_dump |grep aor| awk '{print $3}' | sort | sort -mu
-		exit $?
-		;;
-
-
-	showdb|userdb)
-		if [ $# -eq 2 ] ; then
-			is_user $2
-			if [ $? -ne 0 ] ; then
-				echo non-existent user
-				exit 1;
-			fi
-		elif [ $# -ne 1  ] ; then
-			usage
-			exit 1
-		fi
-
-		shift
-
-		QUERY1="select $TABLE.$EMAIL_COLUMN from $TABLE where  \
-			$TABLE.$SUBSCRIBER_COLUMN='$1'"
-		QUERY2="select $USRLOC.* from $USRLOC where \
-			$USRLOC.$USER_COLUMN='$1' order by expires desc"
-		QUERY3="select $USRLOC.$USER_COLUMN, $TABLE.$EMAIL_COLUMN, $USRLOC.$CALLID_COLUMN \
-			from $TABLE, $USRLOC where  \
-			$TABLE.$SUBSCRIBER_COLUMN=$USRLOC.$USER_COLUMN  order by $USRLOC.$USER_COLUMN" 
-
-		if [ $# -eq 1 ] ; then
-			sql_ro_query "$QUERY1"
-			sql_ro_query "$QUERY2"
-		else
-			sql_ro_query "$QUERY3"
-		fi
-		echo "Note: Due to usage of cache, server's list " \
-			"may differ from DB list."
-
-		;;
-
-	rm)
-        if [ $# -ne 2 ] ; then
-            usage
-            exit 1
-        fi
-		shift 
-		prompt_pw 
-
-        is_user $1
-        if [ $? -ne 0 ] ; then
-            echo non-existent user
-            exit 1
-        fi
-
-        QUERY="delete from $TABLE where $TABLE.$SUBSCRIBER_COLUMN='$1'"
-		sql_query "$QUERY"
-
-		$0 acl revoke $1 > /dev/null 2>&1
-		$0 dul $1   > /dev/null 2>&1
-        ;;
-			
-	ps)
-		fifo_cmd ps
-		;;
-
-	acl)
-		shift
-		acl "$@"
-		;;
-
-	fifo)
-		shift
-		fifo_cmd "$@"
-		;;
-
-	version)
-		echo  "$0 $VERSION"
-		;;
-		
-	*)
-		usage
-		exit 1
-		;;
-
-esac
-