Libpq++ autenthication problem II

Started by Dario Fumagallialmost 28 years ago4 messagesgeneral
Jump to latest
#1Dario Fumagalli
swdevel@art-media.it

Original message:

Hi all,

I'm having some problems trying to use the "password" and "crypt"
authentication protocols in my libpq++ apps.

Until now my programs were always on the same machine and were CGIs. So
I used "trust" authentication in my pg_hba.conf file.
Since I'm spreading the PostgreSQL fame as much as I can some customers
are asking my company for some PostgreSQL-based apps.
For one of those customers I must provide a degree of security better
than "trust", possibly "crypt".

I'm searching documentation or examples about "password" and "crypt"
connections using libpq++. I searched on the WEB site (libpq IS
documented, but perhaps I'm too a newbie to translate the given
explainations to working code (= code that compiles AND succeeds in the
connections)).
I also tried searching the interfaces mailing list archives, but with no
luck.

Following there is an example (it is
src/interfaces/libpq++/examples/testlibpq0.cc with some lines added) of
my first efforts. I know that everything else is OK. My v. 6.3.1
installation works OK since the last year; people can view my data using
libpq++ based CGIs (those that use the "trust" authentication); JDBC
both on server (listening on a port) and on client (JDBC drivers
downloaded with applets) work even with "password" authentication; ODBC
connections are OK (thanks Byron :) ) with "trust" and "password"
authentication (not for "crypt" but it seems it is not implemented yet
on the driver's side...); PHP 3 connections work both with "password"
and "crypt" protocols. All the listed means of connection work with
other databases AND with this specific database.

I'm really sure I'm making a little mistake somewhere in the code. If
someone could help, I'll be grateful...

Listing of src/interfaces/libpq++/examples/testlibpq0.cc with temptative
(first 5 lines changed) authentication support (those username and
password work in psql):

/*-------------------------------------------------------------------------
*
* testlibpq0.c--
* small test program for libpq++,
* small interactive loop where queries can be entered interactively
* and sent to the backend
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header:
/usr/local/cvsroot/pgsql/src/interfaces/libpq++/examples/testlibpq0.cc,v
1.3 1997/02/13 10:00:42 scrappy Exp $
*

*-------------------------------------------------------------------------
*/

#include <iostream.h>
#include <libpq++.h>

int main()
{
PgEnv Env; // Costruisce l'ambiente per PostgreSQL. Necessario se si
usa
// l'autenticazione (come nel caso di Jia)
Env.Auth("password"); // Sostituire con "crypt" se necessario
Env.Option("user=jiaguest password=c3e0scx");

// Open the connection to the database and make sure it's OK
PgDatabase data(Env, "template1");
if ( data.ConnectionBad() ) {
cout << "Connection was unsuccessful..." << endl
<< "Error message returned: " << data.ErrorMessage() << endl;
return 1;
}
else
cout << "Connection successful... Enter queries below:" << endl;

// Interactively obtain and execute queries
ExecStatusType status;
string buf;
int done = 0;
while (!done)
{
cout << "> ";
cout.flush();
getline(cin, buf);
if ( buf != "" )
if ( (status = data.Exec( buf.c_str() )) ==
PGRES_TUPLES_OK )
data.DisplayTuples();
else
cout << "No tuples returned..." << endl
<< "status = " << status << endl
<< "Error returned: " << data.ErrorMessage() <<
endl;
else
done = 1;
}
return 0;
} // End main()

One possible answer (since I received none :( ):

I looked at the sources under interfaces/libpq++ and they seem rather
obsolete. The following are my findings (hope to be correct):

In fact they use an old method (valid before password and crypt
authentication appeared) of connecting to the backend.

This older method is unable to connect via those new authentication
protocols. I also found some old bits here and there (related to the
connection) that perhaps could be removed.

Since I want to use a C++ interface to PostgreSQL (and I'm bragging sooo
much with my colleagues and friends about its "mighty powers") I could
rewrite some pieces here and there.

Given that:
- I found a workaround (using environment variables), but it's not
elegant.

- due to the new connection method (a string) I have collisions with
overloaded constructors' parameters. I can write code that can cope with
the new authentication methods, but the current C++ interface will be
broken.

- I'm not the only one on this planet who wants to use libpq++.

- I have proofs (other mails) of people who need / like to connect to
PostgreSQL using libpq++ AND password or crypt authentication methods.
In those days a library that does not permit password protected
connections is virtually useless.

I would like to ask the authors if I can rewrite some parts of libpq++
and post them.
It will NOT be only a patch and will NOT be backwardly compatible.

Moreover, if The Authors agree with it (and I modify the library), I
would like to be put on the developers' list. This will help me spread
the PostgreSQL fame even more in others Italian programmers' mailing
lists where I know there are a lot of people interested in DBMS.

Cheers,

Dario Fumagalli

Art & Media
Via Villa Giusti, 11 - 10142 Torino (Italy)
Tel. +39-11-7707412 PBX - Fax +39-11-704283
email swdevel@art-media.it
dfumagalli@art-media.it (personal mail slot)
http://www.art-media.it

#2Dario Fumagalli
swdevel@art-media.it
In reply to: Dario Fumagalli (#1)
Re: [GENERAL] Libpq++ autenthication problem II

I'm posting this also to the mailing list since I think it could be of
public interest...

Bruce Tong wrote:

I'm having some problems trying to use the "password" and "crypt"
authentication protocols in my libpq++ apps.

I've had trouble finding information about LIBPQ++ too, as have a number
of others. Before I knew LIBPQ++ existed, I started writing my own classes
to surrounded the LIBPQ library. I'm not finished, but I'd be happy to
share the code with you if you think it would be helpful.

Bruce Tong
Systems Programmer
Electronic Vision / FITNE

mailto: zztong@laxmi.ev.net
http://www.ev.net/fitne

Tanx. In my intentions I would simply rewrite some pieces of libpq++ in
order to make it more "modern". In my opinion in fact libpq++ is not
mantained by anyone of the main developers.

I do hope to be wrong but I'm comparing the sources and see no updates
in the last versions.

They are mantaining libpq since it is the source they use to make their
psql shell work with. Libpq++ on the other side is seen as a "plus" that
a serious package "must" have to be considered full featured, even if it
really does not work at 100%.
It was true also for ODBC drivers, until Byron of Insight (thanks
again!) rewrote them from scratch and made them commercial quality (and
PostgreSQL must AT LEAST and at a whole be commercial quality to compete
with other products).

I'll appreciate your sources, expecially the .h files, since there will
be surely some good idea I forgot of putting in my sources.

Anyhow, since you are interested in libpq would you comment about my
ideas:

- The concepts we see in libpq++ are not bad. So I won't rewrite the
bulk of it.

- Remove the actual connection code from all constructors. I hate the
concept that every new feature you add in a base class to provide new
means of connections (say to PgConnection) you have to propagate it to
all derived classes (since constructors are not inheritable). It's
against easy reusability. Moreover I see some constructors to be useless
if not conflictual to an upgraded management.

Take for example PgConnection(const char* dbName). In the docs it states
it performs a connection to database dbName with "reasonable" defaults.

First, in C++ we can specify defaults that we could use to write
something like:

PgConnection(const char* dbName, const char* host = 0 and so on...)

in order to see by our eyes what are the reasonable defaults (and docs
to search for infos about PostgreSQL stuff are never enough abundant or
easy to understand for us "poor C++ suckers" or for novices).

Moreover, given PgConnection(const char* dbName) I cannot override it
with such an useful statement like:

PgConnection(const char* ConnectionString)

where ConnectionString is similar to the new connection string we can
use with libpq's PQconnectdb() and appears like:

"user=username password=userpwd dbname=mydbname host=myhost port=5432"

- I'd rather follow the good, old Borland style of giving minimal
constructors and to put the big stuff in another conventional function,
like Init(). If we make this function virtual, we can avoid recalling it
"recursively" as it is now using constructors.

Any idea and discussion will be much appreciated!

Best regards,

Dario Fumagalli

Art & Media
Via Villa Giusti, 11 - 10142 Torino (Italy)
Tel. +39-11-7707412 PBX - Fax +39-11-704283
email swdevel@art-media.it
dfumagalli@art-media.it (personal mail slot)
http://www.art-media.it

#3Bruce Tong
zztong@laxmi.ev.net
In reply to: Dario Fumagalli (#2)
Re: [GENERAL] Libpq++ autenthication problem II

of others. Before I knew LIBPQ++ existed, I started writing my own classes
to surrounded the LIBPQ library. I'm not finished, but I'd be happy to
share the code with you if you think it would be helpful.

http://vishnu.ev.net/~zztong/postgres/libpq

I'll appreciate your sources, expecially the .h files, since there will
be surely some good idea I forgot of putting in my sources.

My efforts were mostly to design something which would not be as prone to
memory leaks as I thought the LIBPQ libraries might be for a developer who
doesn't spend a lot of time working with the library.

- The concepts we see in libpq++ are not bad. So I won't rewrite the
bulk of it.

The organization of the classes confused me. I remember thinking it
strange certain classes inherited from others. Its been a few weeks since
I looked at LIBPQ++. I was content to build around LIBPQ since it was
mostly documented. I'm weak on database theory, so the LIBPQ++ design
might make sense, and my C++ classes might be rather shallow. As I
mentioned before, my attempts were mostly to bullet-proof LIBPQ.

Bruce Tong
Systems Programmer
Electronic Vision / FITNE

mailto: zztong@laxmi.ev.net
http://www.ev.net/fitne

#4Bruce Momjian
bruce@momjian.us
In reply to: Bruce Tong (#3)
Re: [GENERAL] Libpq++ autenthication problem II

The organization of the classes confused me. I remember thinking it
strange certain classes inherited from others. Its been a few weeks since
I looked at LIBPQ++. I was content to build around LIBPQ since it was
mostly documented. I'm weak on database theory, so the LIBPQ++ design
might make sense, and my C++ classes might be rather shallow. As I
mentioned before, my attempts were mostly to bullet-proof LIBPQ.

I have always felt the libpq++ really did not take full advantage of
C++, but did not have the time to really think about how it should be
improved. Good luck.

-- 
Bruce Momjian                          |  830 Blythe Avenue
maillist@candle.pha.pa.us              |  Drexel Hill, Pennsylvania 19026
  +  If your life is a hard drive,     |  (610) 353-9879(w)
  +  Christ can be your backup.        |  (610) 853-3000(h)