static PyObject * psyco_conn_get_dsn_parameters(connectionObject *self) { #if PG_VERSION_NUM >= 90300 PyObject *res = NULL; PQconninfoOption *options = NULL; EXC_IF_CONN_CLOSED(self); if (!(options = PQconninfo(self->pgconn))) { PyErr_NoMemory(); goto exit; } res = psycopg_dict_from_conninfo_options(options, /* include_password = */ 0); exit: PQconninfoFree(options); return res; #else PyErr_SetString(NotSupportedError, "PQconninfo not available in libpq < 9.3"); return NULL; #endif }
static PyObject * psyco_parse_dsn(PyObject *self, PyObject *args, PyObject *kwargs) { char *err = NULL; PQconninfoOption *options = NULL; PyObject *res = NULL, *dsn; static char *kwlist[] = {"dsn", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &dsn)) { return NULL; } Py_INCREF(dsn); /* for ensure_bytes */ if (!(dsn = psycopg_ensure_bytes(dsn))) { goto exit; } options = PQconninfoParse(Bytes_AS_STRING(dsn), &err); if (options == NULL) { if (err != NULL) { PyErr_Format(ProgrammingError, "invalid dsn: %s", err); PQfreemem(err); } else { PyErr_SetString(OperationalError, "PQconninfoParse() failed"); } goto exit; } res = psycopg_dict_from_conninfo_options(options, /* include_password = */ 1); exit: PQconninfoFree(options); /* safe on null */ Py_XDECREF(dsn); return res; }
RAISES_NEG static int obscure_password(connectionObject *conn) { PQconninfoOption *options; PyObject *d = NULL, *v = NULL, *dsn = NULL; char *tmp; int rv = -1; if (!conn || !conn->dsn) { return 0; } if (!(options = PQconninfoParse(conn->dsn, NULL))) { /* unlikely: the dsn was already tested valid */ return 0; } if (!(d = psycopg_dict_from_conninfo_options( options, /* include_password = */ 1))) { goto exit; } if (NULL == PyDict_GetItemString(d, "password")) { /* the dsn doesn't have a password */ rv = 0; 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 (!(dsn = psycopg_make_dsn(Py_None, d))) { goto exit; } if (!(dsn = psycopg_ensure_bytes(dsn))) { goto exit; } /* Replace the connection string on the connection object */ tmp = conn->dsn; psycopg_strdup(&conn->dsn, Bytes_AS_STRING(dsn), -1); PyMem_Free(tmp); rv = 0; exit: PQconninfoFree(options); Py_XDECREF(v); Py_XDECREF(d); Py_XDECREF(dsn); return rv; }