*** auth_pgsql.c.orig mar jul 15 01:31:20 2003 --- auth_pgsql.c mer nov 30 18:38:37 2005 *************** *** 150,155 **** --- 150,179 ---- * debugger. */ PGconn *pg_conn; + PGresult *do_query(const char *query) { + int conn_status; + int loop=0; + PGresult *res=NULL; + + + while ((conn_status = (pg_conn ? PQstatus(pg_conn) : CONNECTION_BAD)) + == CONNECTION_BAD) { + if (pg_conn) + PQreset(pg_conn); + if (!pg_conn || (conn_status=PQstatus(pg_conn)) == CONNECTION_BAD) { + sleep(10); + loop++; + if (loop == 5) + break; + } + } + if (loop == 5) + log_print(LOG_ERR, "auth_pgsql_init: PQconnectdb: failed (%s) giving up after %d times", PQerrorMessage(pg_conn),5); + else + res=PQexec(pg_conn,query); + return res; + } + int auth_pgsql_init(void) { char *username = NULL, *password = NULL, *hostname = NULL, *database = NULL, *localhost = "localhost", *s; char *dbconnect = NULL; *************** *** 219,231 **** password ? password : "" ); ! pg_conn = PQconnectdb(dbconnect); ! if (PQstatus(pg_conn) == CONNECTION_BAD) { log_print(LOG_ERR, "auth_pgsql_init: PQconnectdb: failed (%s)", PQerrorMessage(pg_conn)); - goto fail; - } ! ret = 1; fail: if (username) strclr(username); --- 243,253 ---- password ? password : "" ); ! pg_conn = PQconnectdb(dbconnect); ! if (PQstatus(pg_conn) == CONNECTION_BAD) log_print(LOG_ERR, "auth_pgsql_init: PQconnectdb: failed (%s)", PQerrorMessage(pg_conn)); ! ret=1; fail: if (username) strclr(username); *************** *** 250,256 **** who = username_string(name, local_part, domain); ! if (!pg_conn || !apop_query_template) return NULL; /* Obtain the actual query to use. */ if (!(query = substitute_query_params(apop_query_template, name, local_part, domain, NULL, serverhost))) --- 272,278 ---- who = username_string(name, local_part, domain); ! if (!apop_query_template) return NULL; /* Obtain the actual query to use. */ if (!(query = substitute_query_params(apop_query_template, name, local_part, domain, NULL, serverhost))) *************** *** 259,265 **** if (verbose) log_print(LOG_DEBUG, "auth_pgsql_new_apop: SQL query: %s", query); ! if ((res = PQexec(pg_conn, query))) { int i; if (PQresultStatus(res) != PGRES_TUPLES_OK) { --- 281,287 ---- if (verbose) log_print(LOG_DEBUG, "auth_pgsql_new_apop: SQL query: %s", query); ! if ((res = do_query(query))) { int i; if (PQresultStatus(res) != PGRES_TUPLES_OK) { *************** *** 344,350 **** who = username_string(user, local_part, domain); ! if (!pg_conn || !user_pass_query_template) return NULL; /* Obtain the actual query to use. */ if (!(query = substitute_query_params(user_pass_query_template, user, local_part, domain, NULL, serverhost))) --- 366,372 ---- who = username_string(user, local_part, domain); ! if (!user_pass_query_template) return NULL; /* Obtain the actual query to use. */ if (!(query = substitute_query_params(user_pass_query_template, user, local_part, domain, NULL, serverhost))) *************** *** 353,359 **** if (verbose) log_print(LOG_DEBUG, "auth_pgsql_new_user_pass: SQL query: %s", query); ! if ((res = PQexec(pg_conn, query))) { int i; if (PQresultStatus(res) != PGRES_TUPLES_OK) { --- 375,381 ---- if (verbose) log_print(LOG_DEBUG, "auth_pgsql_new_user_pass: SQL query: %s", query); ! if ((res = do_query(query))) { int i; if (PQresultStatus(res) != PGRES_TUPLES_OK) { *************** *** 434,440 **** char *query; PGresult *res = NULL; ! if (!pg_conn || !onlogin_query_template) return; query = substitute_query_params(onlogin_query_template, A->user, A->local_part, A->domain, clienthost, serverhost); if (!query) --- 456,462 ---- char *query; PGresult *res = NULL; ! if (!onlogin_query_template) return; query = substitute_query_params(onlogin_query_template, A->user, A->local_part, A->domain, clienthost, serverhost); if (!query) *************** *** 443,449 **** if (verbose) log_print(LOG_DEBUG, _("auth_pgsql_onlogin: SQL query: %s"), query); ! if ((res = PQexec(pg_conn, query))) { /* XXX transactions? */ if (PQresultStatus(res) != PGRES_TUPLES_OK) { log_print(LOG_ERR, "auth_pgsql_onlogin: bad status after PQexec: %s", pg_strerror(pg_conn)); } else if (PQntuples(res)) { --- 465,471 ---- if (verbose) log_print(LOG_DEBUG, _("auth_pgsql_onlogin: SQL query: %s"), query); ! if ((res = do_query(query))) { /* XXX transactions? */ if (PQresultStatus(res) != PGRES_TUPLES_OK) { log_print(LOG_ERR, "auth_pgsql_onlogin: bad status after PQexec: %s", pg_strerror(pg_conn)); } else if (PQntuples(res)) {