2 Writen by Devrim SERAL(devrim@tef.gazi.edu.tr)
3 For PostgreSQL Authentication And Accounting
5 This program protected with GPL License.
11 #if defined(DB_PGSQL) && defined(DB)
25 static void exit_nicely TAC_ARGS((PGconn *cn, PGresult *r));
30 #define AUTHSQL "SELECT %s FROM %s WHERE %s='%s'"
31 #define ACCTSQL "INSERT INTO %s (usern,s_name,c_name,elapsed_time,bytes_in,bytes_out,fin_t) VALUES ('%s','%s','%s',%s,%s,%s,NOW())"
37 int pgsql_db_verify TAC_ARGS((const char *user, const char *users_passwd, const char *db_user, const char *db_password, const char *db_hostname, const char *db_name, const char *db_table, const char *dbfield_name, const char *dbfield_passwd));
39 int pgsql_db_verify(user, users_passwd, db_user, db_password,
40 db_hostname, db_name, db_table, dbfield_name, dbfield_passwd)
41 const char *user; /* username ... */
42 const char *users_passwd; /* ... and given password */
43 const char *db_user; /* db's parameters */
44 const char *db_password;
45 const char *db_hostname;
48 const char *dbfield_name;
49 const char *dbfield_passwd;
56 if (debug & DEBUG_AUTHEN_FLAG)
57 report(LOG_DEBUG, "PGSQL: verify %s", user);
59 /* Connect database server */
61 conn=PQsetdbLogin(db_hostname,NULL,NULL,NULL,db_name,db_user,db_password);
63 if ( PQstatus(conn) == CONNECTION_BAD ) {
64 if (debug & DEBUG_AUTHEN_FLAG)
65 report(LOG_DEBUG, "PGSQL: Connection to database %s failed", db_name);
69 /* Check select string length */
71 sql_len=strlen(dbfield_passwd)+strlen(dbfield_name)+strlen(db_table)+strlen(user)+strlen(AUTHSQL);
73 if ( sql_len> SQLCMDL ) {
74 if (debug & DEBUG_AUTHEN_FLAG)
75 report(LOG_DEBUG, "PGSQL: Sql cmd exceed alowed limits");
79 /* Prepare select string */
81 pgsqlcmd=(char *) tac_malloc(sql_len);
83 sprintf(pgsqlcmd,AUTHSQL,dbfield_passwd,db_table,dbfield_name,user);
86 res=PQexec(conn,pgsqlcmd);
88 if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
89 if (debug & DEBUG_AUTHEN_FLAG) {
90 report(LOG_DEBUG, "PGSQL: cannot query database ");
91 report(LOG_DEBUG, "PGSQL: Error message->%s", PQerrorMessage(conn) );
94 exit_nicely(conn,res);
100 if ( (nrow=PQntuples(res)) !=1 ) {
101 if (debug & DEBUG_AUTHEN_FLAG)
102 report(LOG_DEBUG, "PGSQL: Have we got more than one password!!");
103 exit_nicely(conn,res);
107 if ( PQgetisnull(res,0,PQfnumber(res,dbfield_passwd)) ) {
108 if (debug & DEBUG_AUTHEN_FLAG)
109 report(LOG_DEBUG, "PGSQL: DB passwd entry is NULL");
110 exit_nicely(conn,res);
114 /* Allocate memory for real_passwd */
115 real_passwd=(char *) tac_malloc(PWLEN+1);
116 strncpy(real_passwd,PQgetvalue(res,0,PQfnumber(res,dbfield_passwd)),PWLEN);
117 real_passwd[PWLEN]='\0';
119 exit_nicely(conn,res);
121 if (debug & DEBUG_AUTHEN_FLAG)
122 report(LOG_DEBUG, "PGSQL: verify password '%s' to DES encrypted string '%s'", users_passwd, real_passwd);
124 /* Try to verify the password */
125 if (!des_verify(users_passwd, real_passwd))
128 return (1); /* Return 1 if verified, 0 otherwise. */
131 /* PGSQL ACCOUNTING function */
133 int pgsql_db_acct TAC_ARGS((const char *db_user, const char *db_password, const char *db_hostname, const char *db_name, const char *db_table, const char *s_name, const char *c_name, const char *a_username, const char *elapsed_time, const char *bytes_in, const char *bytes_out));
135 int pgsql_db_acct(db_user,db_password,db_hostname,db_name,db_table,s_name,c_name,a_username,elapsed_time,bytes_in,bytes_out)
136 const char *db_user; /* db's parameters */
137 const char *db_password;
138 const char *db_hostname;
140 const char *db_table;
143 const char *a_username;
144 const char *elapsed_time;
145 const char *bytes_in;
146 const char *bytes_out;
151 if (debug & DEBUG_ACCT_FLAG)
152 report(LOG_DEBUG, "PGSQL: Accounting for %s begin", a_username);
154 /* Connect database server */
156 conn=PQsetdbLogin(db_hostname,NULL,NULL,NULL,db_name,db_user,db_password);
158 if ( PQstatus(conn) == CONNECTION_BAD ) {
159 if (debug & DEBUG_ACCT_FLAG) {
160 report(LOG_DEBUG, "PGSQL: Connection to database %s failed", db_name);
161 report(LOG_DEBUG, "PGSQL: Error message->%s", PQerrorMessage(conn) );
166 /* Check select string length */
168 sql_len=strlen(db_table)+strlen(a_username)+strlen(s_name)+strlen(c_name)+strlen(elapsed_time)+strlen(bytes_in)+strlen(bytes_out)+strlen(ACCTSQL);
170 if ( sql_len> SQLCMDL ) {
171 if (debug & DEBUG_ACCT_FLAG)
172 report(LOG_DEBUG, "PGSQL: Sql cmd exceed alowed limits");
176 /* Prepare select string */
178 pgsqlcmd=(char *) tac_malloc(sql_len);
180 sprintf(pgsqlcmd,ACCTSQL,db_table,a_username,s_name,c_name,elapsed_time,bytes_in,bytes_out);
183 res=PQexec(conn,pgsqlcmd);
185 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK ) {
186 if (debug & DEBUG_ACCT_FLAG) {
187 report(LOG_DEBUG, "PGSQL: cannot establish database query");
188 report(LOG_DEBUG, "PGSQL: Error message->%s", PQerrorMessage(conn) );
191 exit_nicely(conn,res);
197 /* Flush all result and close connection */
198 exit_nicely(conn,res);
200 if (debug & DEBUG_ACCT_FLAG)
201 report(LOG_DEBUG, "PGSQL: Accounting for %s finished", a_username);
203 return (1); /* Return 1 if verified, 0 otherwise. */
207 static void exit_nicely TAC_ARGS((PGconn *cn, PGresult *r));
218 #else /* defined(DB_PGSQL) && defined(DB) */
222 #endif /* defined(DB_PGSQL) && defined(DB) */