@@ -225,10 +225,12 @@ ECPGnoticeProcessor(void *arg, const char *message)
225
225
sqlca .sqlwarn [0 ]= 'W' ;
226
226
}
227
227
228
+ /* this contains some quick hacks, needs to be cleaned up, but it works */
228
229
bool
229
- ECPGconnect (int lineno , const char * dbname , const char * user , const char * passwd , const char * connection_name , int autocommit )
230
+ ECPGconnect (int lineno , const char * name , const char * user , const char * passwd , const char * connection_name , int autocommit )
230
231
{
231
232
struct connection * this ;
233
+ char * dbname = strdup (name ), * host = NULL , * tmp , * port = NULL , * realname = NULL , * options = NULL ;
232
234
233
235
init_sqlca ();
234
236
@@ -238,11 +240,126 @@ ECPGconnect(int lineno, const char *dbname, const char *user, const char *passwd
238
240
if (dbname == NULL && connection_name == NULL )
239
241
connection_name = "DEFAULT" ;
240
242
243
+ /* get the detail information out of dbname */
244
+ if (strchr (dbname , '@' ) != NULL )
245
+ {
246
+ /* old style: dbname[@server][:port] */
247
+ tmp = strrchr (dbname , ':' );
248
+ if (tmp != NULL ) /* port number given */
249
+ {
250
+ port = strdup (tmp + 1 );
251
+ * tmp = '\0' ;
252
+ }
253
+
254
+ tmp = strrchr (dbname , '@' );
255
+ if (tmp != NULL ) /* host name given */
256
+ {
257
+ host = strdup (tmp + 1 );
258
+ * tmp = '\0' ;
259
+ }
260
+ realname = strdup (dbname );
261
+ }
262
+ else if (strncmp (dbname , "tcp:" , 4 ) == 0 || strncmp (dbname , "unix:" , 5 ) == 0 )
263
+ {
264
+ int offset = 0 ;
265
+
266
+ /*
267
+ * only allow protocols tcp and unix
268
+ */
269
+ if (strncmp (dbname , "tcp:" , 4 ) == 0 )
270
+ offset = 4 ;
271
+ else if (strncmp (dbname , "unix:" , 5 ) == 0 )
272
+ offset = 5 ;
273
+
274
+ if (strncmp (dbname + offset , "postgresql://" , strlen ("postgresql://" )) == 0 )
275
+ {
276
+ /*
277
+ * new style:
278
+ * <tcp|unix>:postgresql://server[:port|:/unixsocket/path:][/dbname][?options]
279
+ */
280
+ offset += strlen ("postgresql://" );
281
+
282
+ tmp = strrchr (dbname + offset , '?' );
283
+ if (tmp != NULL ) /* options given */
284
+ {
285
+ options = strdup (tmp + 1 );
286
+ * tmp = '\0' ;
287
+ }
288
+
289
+ tmp = strrchr (dbname + offset , '/' );
290
+ if (tmp != NULL ) /* database name given */
291
+ {
292
+ realname = strdup (tmp + 1 );
293
+ * tmp = '\0' ;
294
+ }
295
+
296
+ tmp = strrchr (dbname + offset , ':' );
297
+ if (tmp != NULL ) /* port number or Unix socket path given */
298
+ {
299
+ char * tmp2 ;
300
+
301
+ * tmp = '\0' ;
302
+ if ((tmp2 = strchr (tmp + 1 , ':' )) != NULL )
303
+ {
304
+ * tmp2 = '\0' ;
305
+ host = strdup (tmp + 1 );
306
+ if (strncmp (dbname , "unix:" , 5 ) != 0 )
307
+ {
308
+ ECPGlog ("connect: socketname %s given for TCP connection in line %d\n" , host , lineno );
309
+ ECPGraise (lineno , ECPG_CONNECT , realname ? realname : "<DEFAULT>" );
310
+ if (host )
311
+ free (host );
312
+ if (port )
313
+ free (port );
314
+ if (options )
315
+ free (options );
316
+ if (realname )
317
+ free (realname );
318
+ if (dbname )
319
+ free (dbname );
320
+ return false;
321
+ }
322
+ }
323
+ else
324
+ {
325
+ port = strdup (tmp + 1 );
326
+ }
327
+ }
328
+
329
+ if (strncmp (dbname , "unix:" , 5 ) == 0 )
330
+ {
331
+ if (strcmp (dbname + offset , "localhost" ) != 0 && strcmp (dbname + offset , "127.0.0.1" ) != 0 )
332
+ {
333
+ ECPGlog ("connect: non-localhost access via sockets in line %d\n" , lineno );
334
+ ECPGraise (lineno , ECPG_CONNECT , realname ? realname : "<DEFAULT>" );
335
+ if (host )
336
+ free (host );
337
+ if (port )
338
+ free (port );
339
+ if (options )
340
+ free (options );
341
+ if (realname )
342
+ free (realname );
343
+ if (dbname )
344
+ free (dbname );
345
+ return false;
346
+ }
347
+ }
348
+ else
349
+ {
350
+ host = strdup (dbname + offset );
351
+ }
352
+
353
+ }
354
+ }
355
+ else
356
+ realname = strdup (dbname );
357
+
241
358
/* add connection to our list */
242
359
if (connection_name != NULL )
243
360
this -> name = ecpg_strdup (connection_name , lineno );
244
361
else
245
- this -> name = ecpg_strdup (dbname , lineno );
362
+ this -> name = ecpg_strdup (realname , lineno );
246
363
247
364
this -> cache_head = NULL ;
248
365
@@ -253,15 +370,37 @@ ECPGconnect(int lineno, const char *dbname, const char *user, const char *passwd
253
370
254
371
actual_connection = all_connections = this ;
255
372
256
- ECPGlog ("ECPGconnect: opening database %s %s%s\n" , dbname ? dbname : "<DEFAULT>" , user ? "for user " : "" , user ? user : "" );
257
-
258
- this -> connection = PQsetdbLogin (NULL , NULL , NULL , NULL , dbname , user , passwd );
259
-
373
+ ECPGlog ("ECPGconnect: opening database %s on %s port %s %s%s%s%s\n" ,
374
+ realname ? realname : "<DEFAULT>" ,
375
+ host ? host : "<DEFAULT>" ,
376
+ port ? port : "<DEFAULT>" ,
377
+ options ? "with options " : "" , options ? options : "" ,
378
+ user ? "for user " : "" , user ? user : "" );
379
+
380
+ this -> connection = PQsetdbLogin (host , port , options , NULL , realname , user , passwd );
381
+
382
+ if (host )
383
+ free (host );
384
+ if (port )
385
+ free (port );
386
+ if (options )
387
+ free (options );
388
+ if (realname )
389
+ free (realname );
390
+ if (dbname )
391
+ free (dbname );
392
+
260
393
if (PQstatus (this -> connection ) == CONNECTION_BAD )
261
394
{
262
395
ecpg_finish (this );
263
- ECPGlog ("connect: could not open database %s %s%s in line %d\n" , dbname ? dbname : "<DEFAULT>" , user ? "for user " : "" , user ? user : "" , lineno );
264
- ECPGraise (lineno , ECPG_CONNECT , dbname ? dbname : "<DEFAULT>" );
396
+ ECPGlog ("connect: could not open database %s on %s port %s %s%s%s%s in line %d\n" ,
397
+ realname ? realname : "<DEFAULT>" ,
398
+ host ? host : "<DEFAULT>" ,
399
+ port ? port : "<DEFAULT>" ,
400
+ options ? "with options " : "" , options ? options : "" ,
401
+ user ? "for user " : "" , user ? user : "" ,
402
+ lineno );
403
+ ECPGraise (lineno , ECPG_CONNECT , realname ? realname : "<DEFAULT>" );
265
404
return false;
266
405
}
267
406
0 commit comments