Ejemplo n.º 1
0
/* Convert a PostgreSQL encoding to a Python codec.
 *
 * Set 'codec' to a new copy of the codec name allocated on the Python heap.
 * Return 0 in case of success, else -1 and set an exception.
 *
 * 'enc' should be already normalized (uppercase, no - or _).
 */
RAISES_NEG static int
conn_encoding_to_codec(const char *enc, char **codec)
{
    char *tmp;
    Py_ssize_t size;
    PyObject *pyenc = NULL;
    int rv = -1;

    /* Find the Py codec name from the PG encoding */
    if (!(pyenc = PyDict_GetItemString(psycoEncodings, enc))) {
        PyErr_Format(OperationalError,
            "no Python codec for client encoding '%s'", enc);
        goto exit;
    }

    /* Convert the codec in a bytes string to extract the c string. */
    Py_INCREF(pyenc);
    if (!(pyenc = psycopg_ensure_bytes(pyenc))) {
        goto exit;
    }

    if (-1 == Bytes_AsStringAndSize(pyenc, &tmp, &size)) {
        goto exit;
    }

    /* have our own copy of the python codec name */
    rv = psycopg_strdup(codec, tmp, size);

exit:
    Py_XDECREF(pyenc);
    return rv;
}
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 3
0
static int
connection_setup(connectionObject *self, const char *dsn, long int async)
{
    int res = -1;

    Dprintf("connection_setup: init connection object at %p, "
	    "async %ld, refcnt = " FORMAT_CODE_PY_SSIZE_T,
            self, async, Py_REFCNT(self)
      );

    if (0 > psycopg_strdup(&self->dsn, dsn, -1)) { goto exit; }
    if (!(self->notice_list = PyList_New(0))) { goto exit; }
    if (!(self->notifies = PyList_New(0))) { goto exit; }
    self->async = async;
    self->status = CONN_STATUS_SETUP;
    self->async_status = ASYNC_DONE;
    if (!(self->string_types = PyDict_New())) { goto exit; }
    if (!(self->binary_types = PyDict_New())) { goto exit; }
    self->isolevel = ISOLATION_LEVEL_DEFAULT;
    self->readonly = STATE_DEFAULT;
    self->deferrable = STATE_DEFAULT;
    /* other fields have been zeroed by tp_alloc */

    pthread_mutex_init(&(self->lock), NULL);

    if (conn_connect(self, async) != 0) {
        Dprintf("connection_init: FAILED");
        goto exit;
    }
    else {
        Dprintf("connection_setup: good connection object at %p, refcnt = "
            FORMAT_CODE_PY_SSIZE_T,
            self, Py_REFCNT(self)
          );
        res = 0;
    }

exit:
    /* here we obfuscate the password even if there was a connection error */
    {
        PyObject *ptype = NULL, *pvalue = NULL, *ptb = NULL;
        PyErr_Fetch(&ptype, &pvalue, &ptb);
        obscure_password(self);
        PyErr_Restore(ptype, pvalue, ptb);
    }
    return res;
}
Ejemplo n.º 4
0
static int
connection_setup(connectionObject *self, const char *dsn, long int async)
{
    char *pos;
    int res = -1;

    Dprintf("connection_setup: init connection object at %p, "
	    "async %ld, refcnt = " FORMAT_CODE_PY_SSIZE_T,
            self, async, Py_REFCNT(self)
      );

    if (0 > psycopg_strdup(&self->dsn, dsn, 0)) { goto exit; }
    if (!(self->notice_list = PyList_New(0))) { goto exit; }
    if (!(self->notifies = PyList_New(0))) { goto exit; }
    self->async = async;
    self->status = CONN_STATUS_SETUP;
    self->async_status = ASYNC_DONE;
    if (!(self->string_types = PyDict_New())) { goto exit; }
    if (!(self->binary_types = PyDict_New())) { goto exit; }
    /* other fields have been zeroed by tp_alloc */

    pthread_mutex_init(&(self->lock), NULL);

    if (conn_connect(self, async) != 0) {
        Dprintf("connection_init: FAILED");
        goto exit;
    }
    else {
        Dprintf("connection_setup: good connection object at %p, refcnt = "
            FORMAT_CODE_PY_SSIZE_T,
            self, Py_REFCNT(self)
          );
        res = 0;
    }

exit:
    /* here we obfuscate the password even if there was a connection error */
    pos = strstr(self->dsn, "password");
    if (pos != NULL) {
        for (pos = pos+9 ; *pos != '\0' && *pos != ' '; pos++)
            *pos = 'x';
    }

    return res;
}