<commit_before>#include "ldapclient.h"
#include "ldapentry.h"
#include "utils.h"

/*	Dealloc the LDAPClient object. */
static void
LDAPClient_dealloc(LDAPClient* self) {
    Py_XDECREF(self->uri);
    Py_TYPE(self)->tp_free((PyObject*)self);
}

/*	Create a new LDAPClient object. */
static PyObject *
LDAPClient_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
    LDAPClient *self;

	self = (LDAPClient *)type->tp_alloc(type, 0);
	if (self != NULL) {
		/* Create az empty python string for uri. */
        self->uri = PyUnicode_FromString("");
        if (self->uri == NULL) {
            Py_DECREF(self);
            return NULL;
        }
	}
	self->connected = 0;
	self->tls = 0;
    return (PyObject *)self;
}

/*	Initialize the LDAPObject. */
static int
LDAPClient_init(LDAPClient *self, PyObject *args, PyObject *kwds) {
	int tls = 0;
	char *uristr = NULL;
	PyObject *tlso = NULL;
	PyObject *ldapurl = NULL;
	PyObject *ldapurl_type = NULL;
	PyObject *scheme = NULL;
    static char *kwlist[] = {"uri", "tls", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sO!", kwlist, &uristr, &PyBool_Type, &tlso)) {
    	return -1;
    }

    ldapurl_type = load_python_object("pyLDAP.ldapurl", "LDAPURL");
    if (ldapurl_type == NULL) return -1;

    ldapurl = PyObject_CallFunction(ldapurl_type, "s", uristr);
    if (ldapurl == NULL) {
    	return -1;
    }
    self->uri = ldapurl;

	if (tlso != NULL) {
		tls = PyObject_IsTrue(tlso);
	}

	self->connected = 0;
	self->tls = tls;
	scheme = PyObject_GetAttrString(ldapurl, "scheme");
	/* If connection uses SSL set TLS to 0, to avoid duplicated TLS session. */
	if (PyUnicode_CompareWithASCIIString(scheme, "ldaps") == 0) {
		self->tls = 0;
	}
	Py_DECREF(ldapurl_type);
	Py_DECREF(scheme);
    return 0;
}

/*	Opens a connection to the LDAP server. Initializes LDAP structure.
	If TLS is true, starts TLS session.
*/
static PyObject *
LDAPClient_Connect(LDAPClient *self, PyObject *args, PyObject *kwds) {
	int rc = -1;
	void *defaults;
    LDAPControl	**sctrlsp = NULL;
	struct berval passwd;
	struct berval *servdata;
	const int version = LDAP_VERSION3;
	char *binddn = NULL;
	char *pswstr = NULL;
	char *mech = NULL;
	char *authzid = NULL;
	char *realm = NULL;
	char *authcid = NULL;
	char *uristr = NULL;
	PyObject *uri;
	static char *kwlist[] = {"binddn", "password", "mechanism", "username", "realm", "authname", NULL};

	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ssssss", kwlist, &binddn, &pswstr, &mech, &authzid, &realm, &authcid)) {
		PyErr_SetString(PyExc_AttributeError, "Wrong parameters (binddn<str>, password<str>)");
		return NULL;
	}

	/* Get the LDAP URI. */
	uri = PyObject_Str(self->uri);
	uristr = PyObject2char(uri);<commit_msg>Fixed to get only the URL address instead of the full LDAP URL for connection.<commit_after>#include "ldapclient.h"
#include "ldapentry.h"
#include "utils.h"

/*	Dealloc the LDAPClient object. */
static void
LDAPClient_dealloc(LDAPClient* self) {
    Py_XDECREF(self->uri);
    Py_TYPE(self)->tp_free((PyObject*)self);
}

/*	Create a new LDAPClient object. */
static PyObject *
LDAPClient_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
    LDAPClient *self;

	self = (LDAPClient *)type->tp_alloc(type, 0);
	if (self != NULL) {
		/* Create az empty python string for uri. */
        self->uri = PyUnicode_FromString("");
        if (self->uri == NULL) {
            Py_DECREF(self);
            return NULL;
        }
	}
	self->connected = 0;
	self->tls = 0;
    return (PyObject *)self;
}

/*	Initialize the LDAPObject. */
static int
LDAPClient_init(LDAPClient *self, PyObject *args, PyObject *kwds) {
	int tls = 0;
	char *uristr = NULL;
	PyObject *tlso = NULL;
	PyObject *ldapurl = NULL;
	PyObject *ldapurl_type = NULL;
	PyObject *scheme = NULL;
    static char *kwlist[] = {"uri", "tls", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sO!", kwlist, &uristr, &PyBool_Type, &tlso)) {
    	return -1;
    }

    ldapurl_type = load_python_object("pyLDAP.ldapurl", "LDAPURL");
    if (ldapurl_type == NULL) return -1;

    ldapurl = PyObject_CallFunction(ldapurl_type, "s", uristr);
    if (ldapurl == NULL) {
    	return -1;
    }
    self->uri = ldapurl;

	if (tlso != NULL) {
		tls = PyObject_IsTrue(tlso);
	}

	self->connected = 0;
	self->tls = tls;
	scheme = PyObject_GetAttrString(ldapurl, "scheme");
	/* If connection uses SSL set TLS to 0, to avoid duplicated TLS session. */
	if (PyUnicode_CompareWithASCIIString(scheme, "ldaps") == 0) {
		self->tls = 0;
	}
	Py_DECREF(ldapurl_type);
	Py_DECREF(scheme);
    return 0;
}

/*	Opens a connection to the LDAP server. Initializes LDAP structure.
	If TLS is true, starts TLS session.
*/
static PyObject *
LDAPClient_Connect(LDAPClient *self, PyObject *args, PyObject *kwds) {
	int rc = -1;
	void *defaults;
    LDAPControl	**sctrlsp = NULL;
	struct berval passwd;
	struct berval *servdata;
	const int version = LDAP_VERSION3;
	char *binddn = NULL;
	char *pswstr = NULL;
	char *mech = NULL;
	char *authzid = NULL;
	char *realm = NULL;
	char *authcid = NULL;
	char *uristr = NULL;
	PyObject *uri;
	static char *kwlist[] = {"binddn", "password", "mechanism", "username", "realm", "authname", NULL};

	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ssssss", kwlist, &binddn, &pswstr, &mech, &authzid, &realm, &authcid)) {
		PyErr_SetString(PyExc_AttributeError, "Wrong parameters (binddn<str>, password<str>)");
		return NULL;
	}

	/* Get the LDAP URI. */
	uri = PyObject_CallMethod(self->uri, "get_address", NULL);
	if (uri == NULL) return NULL;
	uristr = PyObject2char(uri);
{'fraction_non_alphanumeric': 0.14475574712643677, 'fraction_numerical': 0.005387931034482759, 'mean_word_length': 3.872265966754156, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1414814', 'n_tokens_mistral': 2091, 'n_tokens_neox': 2104, 'n_words': 542}