2004-08-02 23:46:49 +02:00
|
|
|
/*
|
|
|
|
* idpc - IDP as a C CGI program
|
|
|
|
* Copyright (C) 2004 Entr'ouvert
|
|
|
|
*
|
|
|
|
* Author: Frederic Peters <fpeters@entrouvert.com>
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "idpc.h"
|
|
|
|
|
|
|
|
#ifdef USE_POSTGRESQL
|
|
|
|
|
|
|
|
#include <libpq-fe.h>
|
|
|
|
|
|
|
|
static PGconn *sqlconn;
|
|
|
|
|
|
|
|
int db_init()
|
|
|
|
{
|
2004-08-03 16:41:23 +02:00
|
|
|
sqlconn = PQsetdbLogin(
|
|
|
|
get_config_string("//idpc:dbhost"),
|
|
|
|
get_config_string("//idpc:dbport"),
|
|
|
|
get_config_string("//idpc:dboptions"),
|
|
|
|
get_config_string("//idpc:dbtty"),
|
|
|
|
get_config_string("//idpc:dbname"),
|
|
|
|
get_config_string("//idpc:dblogin"),
|
|
|
|
get_config_string("//idpc:dbpassword"));
|
2004-08-02 23:46:49 +02:00
|
|
|
if (PQstatus(sqlconn) == CONNECTION_BAD) {
|
|
|
|
PQfinish(sqlconn);
|
2004-09-08 12:42:20 +02:00
|
|
|
sqlconn = NULL;
|
2004-08-02 23:46:49 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-05 12:00:37 +02:00
|
|
|
int db_get_dumps(char *user_id, char **user_dump, char **session_dump)
|
2004-08-04 17:34:27 +02:00
|
|
|
{
|
|
|
|
PGresult *res;
|
2004-08-07 15:54:01 +02:00
|
|
|
const char *params[1];
|
2004-08-04 17:34:27 +02:00
|
|
|
|
2004-08-07 15:54:01 +02:00
|
|
|
params[0] = user_id;
|
2004-08-04 17:34:27 +02:00
|
|
|
|
2004-08-07 15:54:01 +02:00
|
|
|
res = PQexecParams(sqlconn,
|
|
|
|
"SELECT user_dump, session_dump FROM users where user_id = $1",
|
|
|
|
1, NULL, params, NULL, NULL, 1);
|
2004-08-05 16:14:01 +02:00
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK || PQntuples(res) == 0) {
|
2004-08-04 17:34:27 +02:00
|
|
|
/* error or no tuple for this nameidentifier */
|
|
|
|
PQclear(res);
|
2004-08-05 12:00:37 +02:00
|
|
|
*user_dump = NULL;
|
|
|
|
*session_dump = NULL;
|
|
|
|
return 0;
|
2004-08-04 17:34:27 +02:00
|
|
|
}
|
2004-08-05 12:00:37 +02:00
|
|
|
*user_dump = strdup(PQgetvalue(res, 0, 0));
|
|
|
|
*session_dump = strdup(PQgetvalue(res, 0, 1));
|
2004-08-05 16:14:01 +02:00
|
|
|
PQclear(res);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ensure_user(char *user_id)
|
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
const char *params[1];
|
|
|
|
|
|
|
|
params[0] = user_id;
|
|
|
|
|
|
|
|
res = PQexecParams(sqlconn,
|
|
|
|
"SELECT * from users where user_id = $1",
|
|
|
|
1, NULL, params ,NULL, NULL, 1);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (PQntuples(res) == 1) {
|
|
|
|
PQclear(res);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
/* no result; means we will insert a new tuple */
|
|
|
|
res = PQexecParams(sqlconn,
|
|
|
|
"INSERT INTO users VALUES ($1, NULL, NULL)",
|
|
|
|
1,
|
|
|
|
NULL,
|
|
|
|
params,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
1);
|
2004-08-05 17:10:00 +02:00
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
2004-08-05 16:14:01 +02:00
|
|
|
PQclear(res);
|
|
|
|
|
2004-08-05 12:00:37 +02:00
|
|
|
return 0;
|
2004-08-04 17:34:27 +02:00
|
|
|
}
|
|
|
|
|
2004-08-05 16:14:01 +02:00
|
|
|
|
2004-08-05 12:00:37 +02:00
|
|
|
int db_save_identity(char *user_id, char *identity_dump)
|
2004-08-04 17:34:27 +02:00
|
|
|
{
|
2004-08-05 16:14:01 +02:00
|
|
|
PGresult *res;
|
|
|
|
const char *params[2];
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ensure_user(user_id);
|
2004-08-07 16:09:28 +02:00
|
|
|
if (rc) {
|
2004-08-05 16:14:01 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
params[0] = identity_dump;
|
|
|
|
params[1] = user_id;
|
|
|
|
res = PQexecParams(sqlconn,
|
|
|
|
"UPDATE users SET user_dump = $1 where user_id = $2",
|
|
|
|
2, NULL, params, NULL, NULL, 1);
|
2004-08-05 17:10:00 +02:00
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
2004-08-05 16:14:01 +02:00
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
return 0;
|
2004-08-05 12:00:37 +02:00
|
|
|
}
|
2004-08-04 17:34:27 +02:00
|
|
|
|
2004-08-05 12:00:37 +02:00
|
|
|
int db_save_session(char *user_id, char *session_dump)
|
|
|
|
{
|
2004-08-05 16:14:01 +02:00
|
|
|
PGresult *res;
|
|
|
|
const char *params[2];
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ensure_user(user_id);
|
2004-08-07 16:09:28 +02:00
|
|
|
if (rc) {
|
2004-08-05 16:14:01 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
params[0] = session_dump;
|
|
|
|
params[1] = user_id;
|
|
|
|
res = PQexecParams(sqlconn,
|
|
|
|
"UPDATE users SET session_dump = $1 where user_id = $2",
|
|
|
|
2, NULL, params, NULL, NULL, 1);
|
2004-08-05 17:10:00 +02:00
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
2004-08-05 16:14:01 +02:00
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
return 0;
|
2004-08-04 17:34:27 +02:00
|
|
|
}
|
|
|
|
|
2004-08-05 12:00:37 +02:00
|
|
|
int db_save_name_identifier(char *name_identifier, char *user_id)
|
2004-08-04 17:34:27 +02:00
|
|
|
{
|
2004-08-05 16:14:01 +02:00
|
|
|
PGresult *res;
|
|
|
|
const char *params[2];
|
|
|
|
|
|
|
|
params[0] = name_identifier;
|
|
|
|
params[1] = user_id;
|
|
|
|
|
|
|
|
res = PQexecParams(sqlconn,
|
|
|
|
"SELECT * from nameidentifiers where name_identifier = $1",
|
|
|
|
1, NULL, params ,NULL, NULL, 1);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (PQntuples(res) == 1) {
|
|
|
|
PQclear(res);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = PQexecParams(sqlconn,
|
|
|
|
"INSERT INTO nameidentifiers VALUES ($1, $2)",
|
|
|
|
2, NULL, params, NULL, NULL, 1);
|
2004-08-05 17:10:00 +02:00
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
2004-08-05 16:14:01 +02:00
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
return 0;
|
2004-08-04 17:34:27 +02:00
|
|
|
}
|
|
|
|
|
2004-09-08 17:09:27 +02:00
|
|
|
int db_save_assertion(char *assertion_artifact, LassoAssertion *assertion)
|
2004-08-05 12:00:37 +02:00
|
|
|
{
|
2004-08-05 16:14:01 +02:00
|
|
|
PGresult *res;
|
|
|
|
const char *params[2];
|
2004-09-08 17:09:27 +02:00
|
|
|
char *assertion_dump;
|
|
|
|
|
|
|
|
assertion_dump = lasso_node_export(LASSO_NODE(assertion));
|
2004-08-05 16:14:01 +02:00
|
|
|
|
|
|
|
params[0] = assertion_artifact;
|
2004-09-08 17:09:27 +02:00
|
|
|
params[1] = assertion_dump;
|
2004-08-05 16:14:01 +02:00
|
|
|
|
|
|
|
res = PQexecParams(sqlconn,
|
|
|
|
"INSERT INTO assertions VALUES ($1, $2)",
|
|
|
|
2, NULL, params, NULL, NULL, 1);
|
2004-08-05 17:10:00 +02:00
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
2004-08-05 16:14:01 +02:00
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-09-10 11:37:11 +02:00
|
|
|
int db_get_assertion(char *artifact, char **assertion)
|
2004-08-05 16:14:01 +02:00
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
const char *params[1];
|
|
|
|
|
2004-09-10 11:37:11 +02:00
|
|
|
params[0] = artifact;
|
2004-08-05 16:14:01 +02:00
|
|
|
|
|
|
|
res = PQexecParams(sqlconn,
|
2004-09-10 11:37:11 +02:00
|
|
|
"SELECT assertion from assertions where artifact = $1",
|
2004-08-05 16:14:01 +02:00
|
|
|
1, NULL, params, NULL, NULL, 1);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PQntuples(res) == 0) {
|
|
|
|
fprintf(stderr, "res: 0 tuples\n");
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-09-10 11:37:11 +02:00
|
|
|
*assertion = strdup(PQgetvalue(res, 0, 0));
|
2004-08-05 16:14:01 +02:00
|
|
|
PQclear(res);
|
|
|
|
return 0;
|
2004-08-05 12:00:37 +02:00
|
|
|
}
|
2004-08-04 17:34:27 +02:00
|
|
|
|
2004-09-10 11:37:11 +02:00
|
|
|
int db_remove_assertion(char *artifact)
|
2004-08-10 20:32:05 +02:00
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
const char *params[1];
|
|
|
|
|
2004-09-10 11:37:11 +02:00
|
|
|
params[0] = artifact;
|
2004-08-10 20:32:05 +02:00
|
|
|
|
|
|
|
res = PQexecParams(sqlconn,
|
2004-09-10 11:37:11 +02:00
|
|
|
"DELETE from assertions where artifact = $1",
|
2004-08-10 20:32:05 +02:00
|
|
|
1, NULL, params, NULL, NULL, 1);
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-05 22:00:11 +02:00
|
|
|
int db_get_user_id(char *name_identifier, char **user_id)
|
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
const char *params[1];
|
|
|
|
|
|
|
|
params[0] = name_identifier;
|
|
|
|
|
|
|
|
res = PQexecParams(sqlconn,
|
|
|
|
"SELECT user_id from nameidentifiers "
|
|
|
|
"where name_identifier = $1",
|
|
|
|
1, NULL, params, NULL, NULL, 1);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PQntuples(res) == 0) {
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*user_id = strdup(PQgetvalue(res, 0, 0));
|
|
|
|
PQclear(res);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-08-05 16:14:01 +02:00
|
|
|
void db_finish()
|
|
|
|
{
|
|
|
|
if (sqlconn) {
|
|
|
|
PQfinish(sqlconn);
|
|
|
|
sqlconn = NULL;
|
|
|
|
}
|
|
|
|
}
|
2004-08-04 17:34:27 +02:00
|
|
|
|
2004-08-02 23:46:49 +02:00
|
|
|
#endif /* USE_POSTGRESQL */
|
|
|
|
|