diff options
author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2019-06-24 12:11:12 +0100 |
---|---|---|
committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2019-06-24 12:11:12 +0100 |
commit | 3465ce282e818a455d4ba3ed6cb5a3923734d6b4 (patch) | |
tree | 7698abe042ac8f9ed9499d6c7122fedaf2e1a1c9 | |
parent | f40ad0f3ae8f14ac4c88084eb1e201a94aa9759b (diff) | |
download | psycopg2-3465ce282e818a455d4ba3ed6cb5a3923734d6b4.tar.gz |
Function to obscure password moved to connection_int
-rw-r--r-- | psycopg/connection.h | 1 | ||||
-rw-r--r-- | psycopg/connection_int.c | 54 | ||||
-rw-r--r-- | psycopg/connection_type.c | 54 |
3 files changed, 56 insertions, 53 deletions
diff --git a/psycopg/connection.h b/psycopg/connection.h index 3d94dbf..829ef5a 100644 --- a/psycopg/connection.h +++ b/psycopg/connection.h @@ -165,6 +165,7 @@ HIDDEN void conn_notice_clean(connectionObject *self); HIDDEN void conn_notifies_process(connectionObject *self); RAISES_NEG HIDDEN int conn_setup(connectionObject *self); HIDDEN int conn_connect(connectionObject *self, const char *dsn, long int async); +HIDDEN char *conn_obscure_password(const char *dsn); HIDDEN void conn_close(connectionObject *self); HIDDEN void conn_close_locked(connectionObject *self); RAISES_NEG HIDDEN int conn_commit(connectionObject *self); diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c index 8d3b06a..0629523 100644 --- a/psycopg/connection_int.c +++ b/psycopg/connection_int.c @@ -1157,6 +1157,60 @@ conn_close(connectionObject *self) Py_END_ALLOW_THREADS; } + +/* Return a copy of the 'dsn' string with the password scrubbed. + * + * The string returned is allocated on the Python heap. + * + * In case of error return NULL and raise an exception. + */ +char * +conn_obscure_password(const char *dsn) +{ + PQconninfoOption *options = NULL; + PyObject *d = NULL, *v = NULL, *pydsn = NULL; + char *rv = NULL; + + if (!dsn) { + PyErr_SetString(InternalError, "unexpected null string"); + goto exit; + } + + if (!(options = PQconninfoParse(dsn, NULL))) { + /* unlikely: the dsn was already tested valid */ + PyErr_SetString(InternalError, "the connection string is not valid"); + goto exit; + } + + if (!(d = psyco_dict_from_conninfo_options( + options, /* include_password = */ 1))) { + goto exit; + } + if (NULL == PyDict_GetItemString(d, "password")) { + /* the dsn doesn't have a password */ + psyco_strdup(&rv, dsn, -1); + goto exit; + } + + /* scrub the password and put back the connection string together */ + if (!(v = Text_FromUTF8("xxx"))) { goto exit; } + if (0 > PyDict_SetItemString(d, "password", v)) { goto exit; } + if (!(pydsn = psyco_make_dsn(Py_None, d))) { goto exit; } + if (!(pydsn = psyco_ensure_bytes(pydsn))) { goto exit; } + + /* Return the connection string with the password replaced */ + psyco_strdup(&rv, Bytes_AS_STRING(pydsn), -1); + +exit: + PQconninfoFree(options); + Py_XDECREF(v); + Py_XDECREF(d); + Py_XDECREF(pydsn); + + return rv; +} + + /* conn_close_locked - shut down the connection with the lock already taken */ void conn_close_locked(connectionObject *self) diff --git a/psycopg/connection_type.c b/psycopg/connection_type.c index 99ef6ea..b94dd8b 100644 --- a/psycopg/connection_type.c +++ b/psycopg/connection_type.c @@ -1297,58 +1297,6 @@ static struct PyGetSetDef connectionObject_getsets[] = { /* initialization and finalization methods */ -/* Return a copy of the 'dsn' string with the password scrubbed. - * - * The string returned is allocated on the Python heap. - * - * In case of error return NULL and raise an exception. - */ -static char * -obscure_password(const char *dsn) -{ - PQconninfoOption *options = NULL; - PyObject *d = NULL, *v = NULL, *pydsn = NULL; - char *rv = NULL; - - if (!dsn) { - PyErr_SetString(InternalError, "unexpected null string"); - goto exit; - } - - if (!(options = PQconninfoParse(dsn, NULL))) { - /* unlikely: the dsn was already tested valid */ - PyErr_SetString(InternalError, "the connection string is not valid"); - goto exit; - } - - if (!(d = psyco_dict_from_conninfo_options( - options, /* include_password = */ 1))) { - goto exit; - } - if (NULL == PyDict_GetItemString(d, "password")) { - /* the dsn doesn't have a password */ - psyco_strdup(&rv, dsn, -1); - goto exit; - } - - /* scrub the password and put back the connection string together */ - if (!(v = Text_FromUTF8("xxx"))) { goto exit; } - if (0 > PyDict_SetItemString(d, "password", v)) { goto exit; } - if (!(pydsn = psyco_make_dsn(Py_None, d))) { goto exit; } - if (!(pydsn = psyco_ensure_bytes(pydsn))) { goto exit; } - - /* Return the connection string with the password replaced */ - psyco_strdup(&rv, Bytes_AS_STRING(pydsn), -1); - -exit: - PQconninfoFree(options); - Py_XDECREF(v); - Py_XDECREF(d); - Py_XDECREF(pydsn); - - return rv; -} - static int connection_setup(connectionObject *self, const char *dsn, long int async) { @@ -1359,7 +1307,7 @@ connection_setup(connectionObject *self, const char *dsn, long int async) self, async, Py_REFCNT(self) ); - if (!(self->dsn = obscure_password(dsn))) { goto exit; } + if (!(self->dsn = conn_obscure_password(dsn))) { goto exit; } if (!(self->notice_list = PyList_New(0))) { goto exit; } if (!(self->notifies = PyList_New(0))) { goto exit; } self->async = async; |