source: vendor/current/lib/ldb/pyldb_util.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 2.9 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 Python interface to ldb - utility functions.
5
6 Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org>
7
8 ** NOTE! The following LGPL license applies to the ldb
9 ** library. This does NOT imply that all of Samba is released
10 ** under the LGPL
11
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
16
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
21
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <Python.h>
27#include "ldb.h"
28#include "pyldb.h"
29
30static PyObject *ldb_module = NULL;
31
32#if PY_MAJOR_VERSION >= 3
33#define PyStr_Check PyUnicode_Check
34#define PyStr_AsUTF8 PyUnicode_AsUTF8
35#else
36#define PyStr_Check PyString_Check
37#define PyStr_AsUTF8 PyString_AsString
38#endif
39
40/**
41 * Find out PyTypeObject in ldb module for a given typename
42 */
43static PyTypeObject * PyLdb_GetPyType(const char *typename)
44{
45 PyObject *py_obj = NULL;
46
47 if (ldb_module == NULL) {
48 ldb_module = PyImport_ImportModule("ldb");
49 if (ldb_module == NULL) {
50 return NULL;
51 }
52 }
53
54 py_obj = PyObject_GetAttrString(ldb_module, typename);
55
56 return (PyTypeObject*)py_obj;
57}
58
59/**
60 * Obtain a ldb DN from a Python object.
61 *
62 * @param mem_ctx Memory context
63 * @param object Python object
64 * @param ldb_ctx LDB context
65 * @return Whether or not the conversion succeeded
66 */
67bool pyldb_Object_AsDn(TALLOC_CTX *mem_ctx, PyObject *object,
68 struct ldb_context *ldb_ctx, struct ldb_dn **dn)
69{
70 struct ldb_dn *odn;
71 PyTypeObject *PyLdb_Dn_Type;
72
73 if (ldb_ctx != NULL && PyStr_Check(object)) {
74 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyStr_AsUTF8(object));
75 *dn = odn;
76 return true;
77 }
78
79 if (ldb_ctx != NULL && PyBytes_Check(object)) {
80 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyBytes_AsString(object));
81 *dn = odn;
82 return true;
83 }
84
85 PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
86 if (PyLdb_Dn_Type == NULL) {
87 return false;
88 }
89
90 if (PyObject_TypeCheck(object, PyLdb_Dn_Type)) {
91 *dn = pyldb_Dn_AsDn(object);
92 return true;
93 }
94
95 PyErr_SetString(PyExc_TypeError, "Expected DN");
96 return false;
97}
98
99PyObject *pyldb_Dn_FromDn(struct ldb_dn *dn)
100{
101 PyLdbDnObject *py_ret;
102 PyTypeObject *PyLdb_Dn_Type;
103
104 if (dn == NULL) {
105 Py_RETURN_NONE;
106 }
107
108 PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
109 if (PyLdb_Dn_Type == NULL) {
110 return NULL;
111 }
112
113 py_ret = (PyLdbDnObject *)PyLdb_Dn_Type->tp_alloc(PyLdb_Dn_Type, 0);
114 if (py_ret == NULL) {
115 PyErr_NoMemory();
116 return NULL;
117 }
118 py_ret->mem_ctx = talloc_new(NULL);
119 py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
120 return (PyObject *)py_ret;
121}
Note: See TracBrowser for help on using the repository browser.