| 1 |
#include "utils.h" |
| 2 |
|
| 3 |
/* Converts char* to a lower-case form. Returns with the lower-cased char *. */ |
| 4 |
char * |
| 5 |
lowercase(char *str) { |
| 6 |
int i; |
| 7 |
|
| 8 |
if (str == NULL) return NULL; |
| 9 |
|
| 10 |
for(i = 0; str[i]; i++){ |
| 11 |
str[i] = tolower(str[i]); |
| 12 |
} |
| 13 |
return str; |
| 14 |
} |
| 15 |
|
| 16 |
/* Create a berval structure from a char*. */ |
| 17 |
struct berval * |
| 18 |
create_berval(char *value, long int len) { |
| 19 |
struct berval *bval = NULL; |
| 20 |
|
| 21 |
bval = malloc(sizeof(struct berval)); |
| 22 |
if (bval == NULL) return NULL; |
| 23 |
if (len < 0) { |
| 24 |
bval->bv_len = (unsigned long)strlen(value); |
| 25 |
} else { |
| 26 |
bval->bv_len = (unsigned long)len; |
| 27 |
} |
| 28 |
bval->bv_val = value; |
| 29 |
return bval; |
| 30 |
} |
| 31 |
|
| 32 |
/* Converts a berval structure to a Python bytearray or if it's possible |
| 33 |
to string, bool or long (LDAP has no support for float as far as I know). |
| 34 |
If `keepbytes` param is non-zero, then return bytearray anyway. */ |
| 35 |
PyObject * |
| 36 |
berval2PyObject(struct berval *bval, int keepbytes) { |
| 37 |
PyObject *bytes = NULL; |
| 38 |
PyObject *obj = NULL; |
| 39 |
|
| 40 |
if (keepbytes == 0) { |
| 41 |
/* Check that the value is a boolean True. */ |
| 42 |
if (strcmp(bval->bv_val, "TRUE") == 0) { |
| 43 |
Py_RETURN_TRUE; |
| 44 |
} |
| 45 |
/* Check that the value is a boolean False. */ |
| 46 |
if (strcmp(bval->bv_val, "FALSE") == 0) { |
| 47 |
Py_RETURN_FALSE; |
| 48 |
} |
| 49 |
/* Try to convert into Long. */ |
| 50 |
obj = PyLong_FromString(bval->bv_val, NULL, 0); |
| 51 |
if (obj == NULL || PyErr_Occurred()) { |
| 52 |
if (PyErr_ExceptionMatches(PyExc_ValueError) == 1) { |
| 53 |
/* ValueError is excepted and will be ignored.*/ |
| 54 |
PyErr_Clear(); |
| 55 |
} |
| 56 |
} else { |
| 57 |
return obj; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
bytes = PyBytes_FromStringAndSize(bval->bv_val, bval->bv_len); |
| 62 |
if (bytes == NULL) { |
| 63 |
PyErr_BadInternalCall(); |
| 64 |
return NULL; |
| 65 |
} |
| 66 |
|
| 67 |
if (keepbytes) return bytes; |
| 68 |
|
| 69 |
obj = PyUnicode_FromEncodedObject(bytes, NULL, NULL); |
| 70 |
/* Unicode converting is failed, set bytearray to return value. */ |
| 71 |
if (obj == NULL) { |
| 72 |
obj = bytes; |
| 73 |
} else { |
| 74 |
Py_DECREF(bytes); |
| 75 |
} |
| 76 |
/* Check for errors. */ |
| 77 |
if (PyErr_Occurred()) { |
| 78 |
/* Should be a reason why there is nothing about |
| 79 |
PyExc_UnicodeDecodeError in the official documentation. */ |
| 80 |
if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError) == 1) { |
| 81 |
/* UnicodeDecode error is excepted and will be ignored. */ |
| 82 |
PyErr_Clear(); |
| 83 |
} |
| 84 |
} |
| 85 |
return obj; |
| 86 |
} |
| 87 |
|
| 88 |
/* Converts any Python objects to C string for `output` with length. |
| 89 |
For string object it uses UTF-8 encoding to convert bytes first, |
| 90 |
then char *. For None object sets empty string, for bool it sets |
| 91 |
TRUE or FALSE C strings. |
| 92 |
*/ |
| 93 |
int |
| 94 |
PyObject2char_withlength(PyObject *obj, char **output, long int *len) { |
| 95 |
int rc = 0; |
| 96 |
long int size = 0; |
| 97 |
char *tmp = NULL; |
| 98 |
PyObject *tmpobj = NULL; |
| 99 |
|
| 100 |
if (obj == NULL) return -1; |
| 101 |
|
| 102 |
/* If Python object is a None return an empty("") char*. */ |
| 103 |
if (obj == Py_None) { |
| 104 |
*output = strdup(""); |
| 105 |
if (len != NULL) *len = 0; |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
if (PyBytes_Check(obj)) { |
| 110 |
/* Get the buffer of the Python bytes. */ |
| 111 |
rc = PyBytes_AsStringAndSize(obj, &tmp, (Py_ssize_t *)&size); |
| 112 |
if (rc != 0) return -1; |
| 113 |
/* Copy the content of the buffer to avoid invalid freeing. */ |
| 114 |
*output = (char *)malloc(size + 1); |
| 115 |
if (*output == NULL) return -1; |
| 116 |
memcpy(*output, tmp, size + 1); |
| 117 |
|
| 118 |
if (len != NULL) *len = size; |
| 119 |
|
| 120 |
} else if (PyUnicode_Check(obj)) { |
| 121 |
/* Use UTF-8 encoding on Python string to get bytes. */ |
| 122 |
tmpobj = PyUnicode_AsUTF8String(obj); |
| 123 |
if (tmpobj == NULL) return -1; |
| 124 |
|
| 125 |
rc = PyObject2char_withlength(tmpobj, output, len); |
| 126 |
Py_DECREF(tmpobj); |
| 127 |
} else if (PyBool_Check(obj)) { |
| 128 |
/* Python boolean converting to TRUE or FALSE (see RFC4517 3.3.3). */ |
| 129 |
if (obj == Py_True) { |
| 130 |
*output = strdup("TRUE"); |
| 131 |
if (len != NULL) *len = 4; |
| 132 |
} else { |
| 133 |
*output = strdup("FALSE"); |
| 134 |
if (len != NULL) *len = 5; |
| 135 |
} |
| 136 |
} else { |
| 137 |
tmpobj = PyObject_Str(obj); |
| 138 |
if (tmpobj == NULL) { |
| 139 |
PyErr_BadInternalCall(); |
| 140 |
return -1; |
| 141 |
} |
| 142 |
|
| 143 |
rc = PyObject2char_withlength(tmpobj, output, len); |
| 144 |
Py_DECREF(tmpobj); |
| 145 |
} |
| 146 |
return rc; |
| 147 |
} |
| 148 |
|
| 149 |
/* Converts any Python objects to C string. */ |
| 150 |
char * |
| 151 |
PyObject2char(PyObject *obj) { |
| 152 |
int rc = 0; |
| 153 |
char *str = NULL; |
| 154 |
|
| 155 |
rc = PyObject2char_withlength(obj, &str, NULL); |
| 156 |
if (rc != 0) return NULL; |
| 157 |
else return str; |
| 158 |
} |
| 159 |
|
| 160 |
/* Create a berval list from a Python list by converting the list element |
| 161 |
using PyObject2char. Returns NULL if the parameter is not a list or NULL. */ |
| 162 |
struct berval ** |
| 163 |
PyList2BervalList(PyObject *list) { |
| 164 |
int i = 0, rc = 0; |
| 165 |
long int len = 0; |
| 166 |
char *strvalue; |
| 167 |
struct berval **berval_arr = NULL; |
| 168 |
PyObject *iter; |
| 169 |
PyObject *item; |
| 170 |
|
| 171 |
if (list == NULL || !PyList_Check(list)) return NULL; |
| 172 |
|
| 173 |
berval_arr = (struct berval **)malloc(sizeof(struct berval *) * ((int)PyList_Size(list) + 1)); |
| 174 |
if (berval_arr == NULL) return NULL; |
| 175 |
|
| 176 |
iter = PyObject_GetIter(list); |
| 177 |
if (iter == NULL) { |
| 178 |
free(berval_arr); |
| 179 |
return NULL; |
| 180 |
} |
| 181 |
|
| 182 |
for (item = PyIter_Next(iter); item != NULL; item = PyIter_Next(iter)) { |
| 183 |
rc = PyObject2char_withlength(item, &strvalue, &len); |
| 184 |
Py_DECREF(item); |
| 185 |
if (rc != 0) goto end; |
| 186 |
berval_arr[i++] = create_berval(strvalue, len); |
| 187 |
} |
| 188 |
end: |
| 189 |
Py_DECREF(iter); |
| 190 |
berval_arr[i] = NULL; |
| 191 |
return berval_arr; |
| 192 |
} |
| 193 |
|
| 194 |
/* Converts Python list to a C string list. Returns NULL if it's failed. */ |
| 195 |
char ** |
| 196 |
PyList2StringList(PyObject *list) { |
| 197 |
int i = 0; |
| 198 |
char **strlist; |
| 199 |
PyObject *iter; |
| 200 |
PyObject *item; |
| 201 |
|
| 202 |
if (list == NULL || !PyList_Check(list)) return NULL; |
| 203 |
|
| 204 |
strlist = malloc(sizeof(char*) * ((int)PyList_Size(list) + 1)); |
| 205 |
if (strlist == NULL) return NULL; |
| 206 |
|
| 207 |
iter = PyObject_GetIter(list); |
| 208 |
if (iter == NULL) { |
| 209 |
free(strlist); |
| 210 |
return NULL; |
| 211 |
} |
| 212 |
|
| 213 |
for (item = PyIter_Next(iter); item != NULL; item = PyIter_Next(iter)) { |
| 214 |
strlist[i++] = PyObject2char(item); |
| 215 |
Py_DECREF(item); |
| 216 |
} |
| 217 |
Py_DECREF(iter); |
| 218 |
strlist[i] = NULL; |
| 219 |
return strlist; |
| 220 |
} |
| 221 |
|
| 222 |
/* Create a null delimitered LDAPSortKey list from a Python list which |
| 223 |
contains tuples of attribute name aad reverse order. */ |
| 224 |
LDAPSortKey ** |
| 225 |
PyList2LDAPSortKeyList(PyObject *list) { |
| 226 |
int i = 0; |
| 227 |
char *attr = NULL; |
| 228 |
LDAPSortKey **sortlist; |
| 229 |
LDAPSortKey *elem; |
| 230 |
PyObject *iter; |
| 231 |
PyObject *item; |
| 232 |
PyObject *tmp = NULL; |
| 233 |
|
| 234 |
if (list == NULL || !PyList_Check(list)) return NULL; |
| 235 |
|
| 236 |
sortlist = malloc(sizeof(LDAPSortKey*) * ((int)PyList_Size(list) + 1)); |
| 237 |
if (sortlist == NULL) return NULL; |
| 238 |
|
| 239 |
iter = PyObject_GetIter(list); |
| 240 |
if (iter == NULL) { |
| 241 |
free(sortlist); |
| 242 |
return NULL; |
| 243 |
} |
| 244 |
|
| 245 |
for (item = PyIter_Next(iter); item != NULL; item = PyIter_Next(iter)) { |
| 246 |
/* Mark the end of the list first, it's important for error-handling. */ |
| 247 |
sortlist[i] = NULL; |
| 248 |
if (!PyTuple_Check(item) || PyTuple_Size(item) != 2) goto error; |
| 249 |
|
| 250 |
/* Get attribute's name and reverse order from the tuple. */ |
| 251 |
tmp = PyTuple_GetItem(item, 0); /* Returns borrowed ref. */ |
| 252 |
if (tmp == NULL) goto error; |
| 253 |
attr = PyObject2char(tmp); |
| 254 |
if (attr == NULL) goto error; |
| 255 |
tmp = PyTuple_GetItem(item, 1); |
| 256 |
if (tmp == NULL) { |
| 257 |
free(attr); |
| 258 |
goto error; |
| 259 |
} |
| 260 |
|
| 261 |
/* Malloc and set LDAPSortKey struct. */ |
| 262 |
elem = (LDAPSortKey *)malloc(sizeof(LDAPSortKey)); |
| 263 |
if (elem == NULL) { |
| 264 |
free(attr); |
| 265 |
goto error; |
| 266 |
} |
| 267 |
|
| 268 |
elem->attributeType = attr; |
| 269 |
elem->orderingRule = NULL; |
| 270 |
|
| 271 |
/* If the second tuple element is True reverseOrder will be 1, |
| 272 |
otherwise 0. */ |
| 273 |
elem->reverseOrder = PyObject_IsTrue(tmp); |
| 274 |
sortlist[i++] = elem; |
| 275 |
|
| 276 |
Py_DECREF(item); |
| 277 |
} |
| 278 |
Py_DECREF(iter); |
| 279 |
sortlist[i] = NULL; |
| 280 |
return sortlist; |
| 281 |
error: |
| 282 |
Py_DECREF(iter); |
| 283 |
Py_XDECREF(item); |
| 284 |
/* Free all successfully allocated item. */ |
| 285 |
for (i = 0; sortlist[i] != NULL; i++) { |
| 286 |
free(sortlist[i]->attributeType); |
| 287 |
free(sortlist[i]); |
| 288 |
} |
| 289 |
free(sortlist); |
| 290 |
return NULL; |
| 291 |
} |
| 292 |
|
| 293 |
/* Compare lower-case representations of two Python objects. |
| 294 |
Returns 1 they are matched, -1 if it's failed, and 0 otherwise. */ |
| 295 |
int |
| 296 |
lower_case_match(PyObject *o1, PyObject *o2) { |
| 297 |
int match = 0; |
| 298 |
char *str1 = NULL; |
| 299 |
char *str2 = NULL; |
| 300 |
|
| 301 |
str1 = lowercase(PyObject2char(o1)); |
| 302 |
if (str1 == NULL) return -1; |
| 303 |
|
| 304 |
str2 = lowercase(PyObject2char(o2)); |
| 305 |
if (str2 == NULL) { |
| 306 |
free(str1); |
| 307 |
return -1; |
| 308 |
} |
| 309 |
|
| 310 |
if (strcmp(str1, str2) == 0) match = 1; |
| 311 |
|
| 312 |
free(str1); |
| 313 |
free(str2); |
| 314 |
|
| 315 |
return match; |
| 316 |
} |
| 317 |
|
| 318 |
/* Load the `object_name` Python object from the `module_name` Python module. |
| 319 |
Returns the object or Py_None if it's failed. |
| 320 |
*/ |
| 321 |
PyObject * |
| 322 |
load_python_object(char *module_name, char *object_name) { |
| 323 |
PyObject *module, *object; |
| 324 |
|
| 325 |
module = PyImport_ImportModule(module_name); |
| 326 |
if (module == NULL) { |
| 327 |
PyErr_Format(PyExc_ImportError, "The import of %s is failed.", module_name); |
| 328 |
return NULL; |
| 329 |
} |
| 330 |
|
| 331 |
object = PyObject_GetAttrString(module, object_name); |
| 332 |
if (object == NULL) { |
| 333 |
PyErr_Format(PyExc_ImportError, "%s is not found in %s module.", object_name, module_name); |
| 334 |
Py_DECREF(module); |
| 335 |
return NULL; |
| 336 |
} |
| 337 |
|
| 338 |
Py_DECREF(module); |
| 339 |
return object; |
| 340 |
} |
| 341 |
|
| 342 |
/* Get an error by code calling the get_error function from |
| 343 |
the bonsai.errors Python module. */ |
| 344 |
PyObject * |
| 345 |
get_error_by_code(int code) { |
| 346 |
PyObject *error; |
| 347 |
PyObject *get_error_func = load_python_object("bonsai.errors", "_get_error"); |
| 348 |
if (get_error_func == NULL) return NULL; |
| 349 |
|
| 350 |
error = PyObject_CallFunction(get_error_func, "(i)", code); |
| 351 |
Py_DECREF(get_error_func); |
| 352 |
|
| 353 |
return error; |
| 354 |
} |
| 355 |
|
| 356 |
/* Set a Python exception using the return code from an LDAP function. |
| 357 |
If it's possible append additional error message from the LDAP session. */ |
| 358 |
void |
| 359 |
set_exception(LDAP *ld, int code) { |
| 360 |
int err = -1; |
| 361 |
char *opt_errorstr = NULL; |
| 362 |
char *errorstr = NULL; |
| 363 |
PyObject *ldaperror = NULL; |
| 364 |
PyObject *errormsg = NULL; |
| 365 |
|
| 366 |
/* Check that an error is already set. */ |
| 367 |
if (PyErr_Occurred()) return; |
| 368 |
|
| 369 |
if (code == 0) { |
| 370 |
/* Getting the error code from the session. */ |
| 371 |
/* 0x31: LDAP_OPT_RESULT_CODE or LDAP_OPT_ERROR_NUMBER */ |
| 372 |
ldap_get_option(ld, 0x0031, &err); |
| 373 |
} else { |
| 374 |
/* Use the parameter for error code. */ |
| 375 |
err = code; |
| 376 |
} |
| 377 |
|
| 378 |
ldaperror = get_error_by_code(err); |
| 379 |
if (ldaperror == NULL) return; |
| 380 |
|
| 381 |
/* Get additional error message from the session. */ |
| 382 |
opt_errorstr = _ldap_get_opt_errormsg(ld); |
| 383 |
errorstr = ldap_err2string(err); |
| 384 |
|
| 385 |
if (errorstr != NULL && strlen(errorstr) > 0) { |
| 386 |
if (opt_errorstr != NULL && strlen(opt_errorstr) > 0) { |
| 387 |
if (strcmp(errorstr, opt_errorstr) != 0) { |
| 388 |
errormsg = PyUnicode_FromFormat("%s. %s", errorstr, opt_errorstr); |
| 389 |
goto end; |
| 390 |
} |
| 391 |
} |
| 392 |
/* Optional string is empty or equals to the error string. */ |
| 393 |
errormsg = PyUnicode_FromFormat("%s.", errorstr); |
| 394 |
} else if (opt_errorstr != NULL && strlen(opt_errorstr) > 0) { |
| 395 |
errormsg = PyUnicode_FromFormat("%s.", opt_errorstr); |
| 396 |
} |
| 397 |
end: |
| 398 |
if (errormsg != NULL) { |
| 399 |
PyErr_SetObject(ldaperror, errormsg); |
| 400 |
Py_DECREF(errormsg); |
| 401 |
} else { |
| 402 |
PyErr_SetString(ldaperror, ""); |
| 403 |
} |
| 404 |
if (opt_errorstr) ldap_memfree(opt_errorstr); |
| 405 |
Py_DECREF(ldaperror); |
| 406 |
} |
| 407 |
|
| 408 |
/* Add a pending LDAP operations to a dictionary. The key is the |
| 409 |
* corresponding message id, the value depends on the type of operation. */ |
| 410 |
int |
| 411 |
add_to_pending_ops(PyObject *pending_ops, int msgid, PyObject *item) { |
| 412 |
PyObject *key = NULL; |
| 413 |
|
| 414 |
key = PyLong_FromLong((long int)msgid); |
| 415 |
if (key == NULL) return -1; |
| 416 |
|
| 417 |
if (PyDict_SetItem(pending_ops, key, item) != 0) { |
| 418 |
Py_DECREF(key); |
| 419 |
PyErr_BadInternalCall(); |
| 420 |
return -1; |
| 421 |
} |
| 422 |
if (item != Py_None) Py_DECREF(item); |
| 423 |
Py_DECREF(key); |
| 424 |
|
| 425 |
return 0; |
| 426 |
} |
| 427 |
|
| 428 |
/* Get a pending LDAP operations from a dictionary. The key is the |
| 429 |
* corresponding message id, the return value depends on the type |
| 430 |
* of operation. */ |
| 431 |
PyObject * |
| 432 |
get_from_pending_ops(PyObject *pending_ops, int msgid) { |
| 433 |
PyObject *key = NULL; |
| 434 |
PyObject *item = NULL; |
| 435 |
|
| 436 |
key = PyLong_FromLong((long int)msgid); |
| 437 |
if (key == NULL) return NULL; |
| 438 |
|
| 439 |
item = PyDict_GetItem(pending_ops, key); |
| 440 |
Py_DECREF(key); |
| 441 |
|
| 442 |
Py_XINCREF(item); |
| 443 |
return item; |
| 444 |
} |
| 445 |
|
| 446 |
/* Delete a pending LDAP operations from a dictionary. The key is the |
| 447 |
* corresponding message id, on error returns non-zero value. */ |
| 448 |
int |
| 449 |
del_from_pending_ops(PyObject *pending_ops, int msgid) { |
| 450 |
PyObject *key = NULL; |
| 451 |
|
| 452 |
key = PyLong_FromLong((long int)msgid); |
| 453 |
if (key == NULL) return -1; |
| 454 |
|
| 455 |
if (PyDict_DelItem(pending_ops, key) != 0) { |
| 456 |
Py_DECREF(key); |
| 457 |
PyErr_BadInternalCall(); |
| 458 |
return -1; |
| 459 |
} |
| 460 |
Py_DECREF(key); |
| 461 |
|
| 462 |
return 0; |
| 463 |
} |
| 464 |
|
| 465 |
/* Get a socketpair in `tup` by calling socket.socketpair(). The socket |
| 466 |
descriptors are set to `csock` and `ssock` parameters respectively. |
| 467 |
If the function call is failed, it returns with -1. */ |
| 468 |
int |
| 469 |
get_socketpair(PyObject **tup, SOCKET *csock, SOCKET *ssock) { |
| 470 |
PyObject *tmp = NULL; |
| 471 |
|
| 472 |
tmp = load_python_object("socket", "socketpair"); |
| 473 |
if (tmp == NULL) return -1; |
| 474 |
|
| 475 |
*tup = PyObject_CallObject(tmp, NULL); |
| 476 |
if (*tup == NULL) { |
| 477 |
Py_DECREF(tmp); |
| 478 |
return -1; |
| 479 |
} |
| 480 |
Py_DECREF(tmp); |
| 481 |
/* Sanity check. */ |
| 482 |
if (PyTuple_Check(*tup) && PyTuple_Size(*tup) == 2) { |
| 483 |
tmp = PyTuple_GetItem(*tup, 0); |
| 484 |
if (tmp == NULL) goto error; |
| 485 |
|
| 486 |
/* Get the socket descriptor for the first one. */ |
| 487 |
tmp = PyObject_CallMethod(tmp, "fileno", NULL); |
| 488 |
if (tmp == NULL) goto error; |
| 489 |
*ssock = (SOCKET)PyLong_AsLong(tmp); |
| 490 |
Py_DECREF(tmp); |
| 491 |
|
| 492 |
tmp = PyTuple_GetItem(*tup, 1); |
| 493 |
if (tmp == NULL) goto error; |
| 494 |
/* Get the socket descriptor for the second one. */ |
| 495 |
tmp = PyObject_CallMethod(tmp, "fileno", NULL); |
| 496 |
if (tmp == NULL) goto error; |
| 497 |
*csock = (SOCKET)PyLong_AsLong(tmp); |
| 498 |
Py_DECREF(tmp); |
| 499 |
} |
| 500 |
return 0; |
| 501 |
error: |
| 502 |
Py_DECREF(*tup); |
| 503 |
return -1; |
| 504 |
} |
| 505 |
|
| 506 |
/* Close and dispose the dummy sockets in the socketpair. */ |
| 507 |
void |
| 508 |
close_socketpair(PyObject *tup) { |
| 509 |
PyObject *tmp = NULL; |
| 510 |
PyObject *ret = NULL; |
| 511 |
|
| 512 |
/* Sanity check. */ |
| 513 |
if (tup != NULL && PyTuple_Check(tup) && PyTuple_Size(tup) == 2) { |
| 514 |
tmp = PyTuple_GetItem(tup, 0); |
| 515 |
if (tmp) { |
| 516 |
ret = PyObject_CallMethod(tmp, "close", NULL); |
| 517 |
if (ret) Py_DECREF(ret); |
| 518 |
} |
| 519 |
|
| 520 |
tmp = PyTuple_GetItem(tup, 1); |
| 521 |
if (tmp) { |
| 522 |
ret = PyObject_CallMethod(tmp, "close", NULL); |
| 523 |
if (ret) Py_DECREF(ret); |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
/* Set the parameters of an ldapsearchparams struct. */ |
| 529 |
int |
| 530 |
set_search_params(ldapsearchparams *params, char **attrs, int attrsonly, |
| 531 |
char *base, char *filter, int len, int scope, int sizelimit, double timeout, |
| 532 |
LDAPSortKey **sort_list) { |
| 533 |
|
| 534 |
params->attrs = attrs; |
| 535 |
params->attrsonly = attrsonly; |
| 536 |
|
| 537 |
/* Copying base string and filter string, because there is no |
| 538 |
guarantee that someone will not free them prematurely. */ |
| 539 |
params->base = (char *)malloc(sizeof(char) * (strlen(base)+1)); |
| 540 |
strcpy(params->base, base); |
| 541 |
|
| 542 |
/* If empty filter string is given, set to NULL. */ |
| 543 |
if (filter == NULL || len == 0) { |
| 544 |
params->filter = NULL; |
| 545 |
} else { |
| 546 |
params->filter = (char *)malloc(sizeof(char) * (len + 1)); |
| 547 |
memcpy(params->filter, filter, len + 1); |
| 548 |
} |
| 549 |
params->scope = scope; |
| 550 |
params->sizelimit = sizelimit; |
| 551 |
params->timeout = timeout; |
| 552 |
|
| 553 |
params->sort_list = sort_list; |
| 554 |
|
| 555 |
return 0; |
| 556 |
} |
| 557 |
|
| 558 |
/* Free an ldapsearchparam struct. */ |
| 559 |
void |
| 560 |
free_search_params(ldapsearchparams *params) { |
| 561 |
int i = 0; |
| 562 |
|
| 563 |
if (params != NULL) { |
| 564 |
free(params->base); |
| 565 |
free(params->filter); |
| 566 |
if (params->attrs != NULL) { |
| 567 |
for (i = 0; params->attrs[i] != NULL; i++) { |
| 568 |
free(params->attrs[i]); |
| 569 |
} |
| 570 |
free(params->attrs); |
| 571 |
} |
| 572 |
/* Free LDAPSortKey list. */ |
| 573 |
if (params->sort_list != NULL) { |
| 574 |
for (i = 0; params->sort_list[i] != NULL; i++) { |
| 575 |
free(params->sort_list[i]->attributeType); |
| 576 |
free(params->sort_list[i]); |
| 577 |
} |
| 578 |
free(params->sort_list); |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
int |
| 584 |
create_ppolicy_control(LDAP *ld, LDAPControl **returned_ctrls, |
| 585 |
PyObject **ctrl_obj, unsigned int *pperr) { |
| 586 |
int rc = 0; |
| 587 |
int expire = 1; |
| 588 |
int grace = -1; |
| 589 |
|
| 590 |
rc = _ldap_parse_passwordpolicy_control(ld, ldap_control_find( |
| 591 |
LDAP_CONTROL_PASSWORDPOLICYRESPONSE, returned_ctrls, NULL), |
| 592 |
&expire, &grace, pperr); |
| 593 |
if (rc == LDAP_CONTROL_NOT_FOUND) return 0; |
| 594 |
if (rc != LDAP_SUCCESS) return -1; |
| 595 |
/* Create ppolicy ctrl dict. */ |
| 596 |
*ctrl_obj = Py_BuildValue("{s,s,s,i,s,i}", |
| 597 |
"oid", LDAP_CONTROL_PASSWORDPOLICYRESPONSE, |
| 598 |
"expire", expire, |
| 599 |
"grace", grace); |
| 600 |
if (*ctrl_obj == NULL) return -1; |
| 601 |
|
| 602 |
return 1; |
| 603 |
} |
| 604 |
|
| 605 |
void |
| 606 |
set_ppolicy_err(unsigned int pperr, PyObject *ctrl_obj) { |
| 607 |
PyObject *ldaperror = NULL; |
| 608 |
|
| 609 |
ldaperror = get_error_by_code(-200 - pperr); |
| 610 |
if (ldaperror == NULL) return; |
| 611 |
PyObject_SetAttrString(ldaperror, "control", ctrl_obj); |
| 612 |
PyErr_SetNone(ldaperror); |
| 613 |
Py_DECREF(ldaperror); |
| 614 |
} |
| 615 |
|
| 616 |
int |
| 617 |
uniqueness_check(PyObject *list, PyObject *value) { |
| 618 |
int rc = 0; |
| 619 |
PyObject *iter = NULL; |
| 620 |
PyObject *item = NULL; |
| 621 |
|
| 622 |
iter = PyObject_GetIter(list); |
| 623 |
if (iter == NULL) return -1; |
| 624 |
|
| 625 |
for (item = PyIter_Next(iter); item != NULL; item = PyIter_Next(iter)) { |
| 626 |
rc = lower_case_match(item, value); |
| 627 |
if (rc != 0) goto end; |
| 628 |
Py_DECREF(item); |
| 629 |
} |
| 630 |
|
| 631 |
end: |
| 632 |
Py_DECREF(iter); |
| 633 |
Py_XDECREF(item); |
| 634 |
return rc; |
| 635 |
} |
| 636 |
|
| 637 |
/* Remove an item from the list without set an error. |
| 638 |
Return 0 if item is not in the list, 1 if item is successfully removed |
| 639 |
and -1 for error. */ |
| 640 |
int |
| 641 |
uniqueness_remove(PyObject *list, PyObject *value) { |
| 642 |
int cmp; |
| 643 |
Py_ssize_t i; |
| 644 |
|
| 645 |
for (i = 0; i < Py_SIZE(list); i++) { |
| 646 |
cmp = lower_case_match(PyList_GET_ITEM(list, i), value); |
| 647 |
if (cmp > 0) { |
| 648 |
if (PyList_SetSlice(list, i, i+1, NULL) == 0) { |
| 649 |
return 1; |
| 650 |
} |
| 651 |
return -1; |
| 652 |
} else if (cmp < 0) return -1; |
| 653 |
} |
| 654 |
return 0; |
| 655 |
} |
| 656 |
|
| 657 |
/* Check that the `value` is in the `list` by converting both the |
| 658 |
value and the list elements lower case C char* strings. The |
| 659 |
return value is a tuple of two items: the True/False that the |
| 660 |
`value` is in the list and the list element that is matched. */ |
| 661 |
PyObject * |
| 662 |
unique_contains(PyObject *list, PyObject *value) { |
| 663 |
int rc = 0; |
| 664 |
PyObject *retval = NULL; |
| 665 |
PyObject *iter = NULL, *item = NULL; |
| 666 |
|
| 667 |
iter = PyObject_GetIter(list); |
| 668 |
if (iter == NULL) return NULL; |
| 669 |
|
| 670 |
for (item = PyIter_Next(iter); item != NULL; item = PyIter_Next(iter)) { |
| 671 |
rc = lower_case_match(item, value); |
| 672 |
if (rc == -1) goto end; |
| 673 |
if (rc == 1) { |
| 674 |
/* Item found, build the return value of (True, item). */ |
| 675 |
retval = Py_BuildValue("(OO)", Py_True, item); |
| 676 |
goto end; |
| 677 |
} |
| 678 |
Py_DECREF(item); |
| 679 |
} |
| 680 |
/* No item found, return (False, None). */ |
| 681 |
retval = Py_BuildValue("(OO)", Py_False, Py_None); |
| 682 |
end: |
| 683 |
Py_DECREF(iter); |
| 684 |
Py_XDECREF(item); |
| 685 |
return retval; |
| 686 |
} |
| 687 |
|
| 688 |
int |
| 689 |
get_ldapvaluelist_status(PyObject *lvl) { |
| 690 |
int status = -1; |
| 691 |
PyObject *tmp = NULL; |
| 692 |
|
| 693 |
tmp = PyObject_GetAttrString(lvl, "status"); |
| 694 |
if (tmp == NULL) return -1; |
| 695 |
|
| 696 |
status = (int)PyLong_AsSize_t(tmp); |
| 697 |
Py_DECREF(tmp); |
| 698 |
|
| 699 |
return status; |
| 700 |
} |
| 701 |
|
| 702 |
int |
| 703 |
set_ldapvaluelist_status(PyObject *lvl, int status) { |
| 704 |
int rc = 0; |
| 705 |
PyObject *tmp = NULL; |
| 706 |
|
| 707 |
tmp = PyLong_FromLong((long int)status); |
| 708 |
if (tmp == NULL) return -1; |
| 709 |
|
| 710 |
rc = PyObject_SetAttrString(lvl, "status", tmp); |
| 711 |
Py_DECREF(tmp); |
| 712 |
|
| 713 |
return rc; |
| 714 |
} |