1/*
2   Unix SMB/CIFS implementation.
3
4   Python interface to tdb.
5
6   Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
7   Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
8
9     ** NOTE! The following LGPL license applies to the tdb
10     ** library. This does NOT imply that all of Samba is released
11     ** under the LGPL
12
13   This library is free software; you can redistribute it and/or
14   modify it under the terms of the GNU Lesser General Public
15   License as published by the Free Software Foundation; either
16   version 3 of the License, or (at your option) any later version.
17
18   This library is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21   Lesser General Public License for more details.
22
23   You should have received a copy of the GNU Lesser General Public
24   License along with this library; if not, see <http://www.gnu.org/licenses/>.
25*/
26
27#include "replace.h"
28#include "system/filesys.h"
29
30#include <Python.h>
31#ifndef Py_RETURN_NONE
32#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
33#endif
34
35/* Include tdb headers */
36#include <tdb.h>
37
38typedef struct {
39	PyObject_HEAD
40	TDB_CONTEXT *ctx;
41	bool closed;
42} PyTdbObject;
43
44PyAPI_DATA(PyTypeObject) PyTdb;
45
46static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
47{
48	PyErr_SetObject(PyExc_RuntimeError,
49		Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
50}
51
52static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
53{
54	TDB_DATA ret;
55	ret.dptr = (unsigned char *)PyString_AsString(data);
56	ret.dsize = PyString_Size(data);
57	return ret;
58}
59
60static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
61{
62	if (data.dptr == NULL && data.dsize == 0) {
63		Py_RETURN_NONE;
64	} else {
65		PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
66												   data.dsize);
67		free(data.dptr);
68		return ret;
69    }
70}
71
72#define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
73	if (ret != 0) { \
74		PyErr_SetTDBError(tdb); \
75		return NULL; \
76	}
77
78static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
79{
80	char *name;
81	int hash_size = 0, tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
82	TDB_CONTEXT *ctx;
83	PyTdbObject *ret;
84	const char *kwnames[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL };
85
86	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
87		return NULL;
88
89	ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
90	if (ctx == NULL) {
91		PyErr_SetFromErrno(PyExc_IOError);
92		return NULL;
93	}
94
95	ret = PyObject_New(PyTdbObject, &PyTdb);
96	ret->ctx = ctx;
97	ret->closed = false;
98	return (PyObject *)ret;
99}
100
101static PyObject *obj_transaction_cancel(PyTdbObject *self)
102{
103	int ret = tdb_transaction_cancel(self->ctx);
104	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
105	Py_RETURN_NONE;
106}
107
108static PyObject *obj_transaction_commit(PyTdbObject *self)
109{
110	int ret = tdb_transaction_commit(self->ctx);
111	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
112	Py_RETURN_NONE;
113}
114
115static PyObject *obj_transaction_recover(PyTdbObject *self)
116{
117	int ret = tdb_transaction_recover(self->ctx);
118	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
119	Py_RETURN_NONE;
120}
121
122static PyObject *obj_transaction_start(PyTdbObject *self)
123{
124	int ret = tdb_transaction_start(self->ctx);
125	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
126	Py_RETURN_NONE;
127}
128
129static PyObject *obj_reopen(PyTdbObject *self)
130{
131	int ret = tdb_reopen(self->ctx);
132	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
133	Py_RETURN_NONE;
134}
135
136static PyObject *obj_lockall(PyTdbObject *self)
137{
138	int ret = tdb_lockall(self->ctx);
139	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
140	Py_RETURN_NONE;
141}
142
143static PyObject *obj_unlockall(PyTdbObject *self)
144{
145	int ret = tdb_unlockall(self->ctx);
146	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
147	Py_RETURN_NONE;
148}
149
150static PyObject *obj_lockall_read(PyTdbObject *self)
151{
152	int ret = tdb_lockall_read(self->ctx);
153	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
154	Py_RETURN_NONE;
155}
156
157static PyObject *obj_unlockall_read(PyTdbObject *self)
158{
159	int ret = tdb_unlockall_read(self->ctx);
160	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
161	Py_RETURN_NONE;
162}
163
164static PyObject *obj_close(PyTdbObject *self)
165{
166	int ret;
167	if (self->closed)
168		Py_RETURN_NONE;
169	ret = tdb_close(self->ctx);
170	self->closed = true;
171	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
172	Py_RETURN_NONE;
173}
174
175static PyObject *obj_get(PyTdbObject *self, PyObject *args)
176{
177	TDB_DATA key;
178	PyObject *py_key;
179	if (!PyArg_ParseTuple(args, "O", &py_key))
180		return NULL;
181
182	key = PyString_AsTDB_DATA(py_key);
183
184	return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
185}
186
187static PyObject *obj_append(PyTdbObject *self, PyObject *args)
188{
189	TDB_DATA key, data;
190	PyObject *py_key, *py_data;
191	int ret;
192	if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
193		return NULL;
194
195	key = PyString_AsTDB_DATA(py_key);
196	data = PyString_AsTDB_DATA(py_data);
197
198	ret = tdb_append(self->ctx, key, data);
199	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
200	Py_RETURN_NONE;
201}
202
203static PyObject *obj_firstkey(PyTdbObject *self)
204{
205	return PyString_FromTDB_DATA(tdb_firstkey(self->ctx));
206}
207
208static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
209{
210	TDB_DATA key;
211	PyObject *py_key;
212	if (!PyArg_ParseTuple(args, "O", &py_key))
213		return NULL;
214
215	key = PyString_AsTDB_DATA(py_key);
216
217	return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
218}
219
220static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
221{
222	TDB_DATA key;
223	PyObject *py_key;
224	int ret;
225	if (!PyArg_ParseTuple(args, "O", &py_key))
226		return NULL;
227
228	key = PyString_AsTDB_DATA(py_key);
229	ret = tdb_delete(self->ctx, key);
230	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
231	Py_RETURN_NONE;
232}
233
234static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
235{
236	TDB_DATA key;
237	int ret;
238	PyObject *py_key;
239	if (!PyArg_ParseTuple(args, "O", &py_key))
240		return NULL;
241
242	key = PyString_AsTDB_DATA(py_key);
243	ret = tdb_exists(self->ctx, key);
244	if (ret != TDB_ERR_NOEXIST) {
245		PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
246	}
247
248	return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True;
249}
250
251static PyObject *obj_store(PyTdbObject *self, PyObject *args)
252{
253	TDB_DATA key, value;
254	int ret;
255	int flag = TDB_REPLACE;
256	PyObject *py_key, *py_value;
257
258	if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
259		return NULL;
260
261	key = PyString_AsTDB_DATA(py_key);
262	value = PyString_AsTDB_DATA(py_value);
263
264	ret = tdb_store(self->ctx, key, value, flag);
265	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
266	Py_RETURN_NONE;
267}
268
269
270typedef struct {
271	PyObject_HEAD
272	TDB_DATA current;
273	PyTdbObject *iteratee;
274} PyTdbIteratorObject;
275
276static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
277{
278	TDB_DATA current;
279	PyObject *ret;
280	if (self->current.dptr == NULL && self->current.dsize == 0)
281		return NULL;
282	current = self->current;
283	self->current = tdb_nextkey(self->iteratee->ctx, self->current);
284	ret = PyString_FromTDB_DATA(current);
285	return ret;
286}
287
288static void tdb_iter_dealloc(PyTdbIteratorObject *self)
289{
290	Py_DECREF(self->iteratee);
291	PyObject_Del(self);
292}
293
294PyTypeObject PyTdbIterator = {
295	.tp_name = "Iterator",
296	.tp_basicsize = sizeof(PyTdbIteratorObject),
297	.tp_iternext = (iternextfunc)tdb_iter_next,
298	.tp_dealloc = (destructor)tdb_iter_dealloc,
299	.tp_flags = Py_TPFLAGS_DEFAULT,
300	.tp_iter = PyObject_SelfIter,
301};
302
303static PyObject *tdb_object_iter(PyTdbObject *self)
304{
305	PyTdbIteratorObject *ret;
306
307	ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
308	ret->current = tdb_firstkey(self->ctx);
309	ret->iteratee = self;
310	Py_INCREF(self);
311	return (PyObject *)ret;
312}
313
314static PyObject *obj_clear(PyTdbObject *self)
315{
316	int ret = tdb_wipe_all(self->ctx);
317	PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
318	Py_RETURN_NONE;
319}
320
321static PyMethodDef tdb_object_methods[] = {
322	{ "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
323		"S.transaction_cancel() -> None\n"
324		"Cancel the currently active transaction." },
325	{ "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
326		"S.transaction_commit() -> None\n"
327		"Commit the currently active transaction." },
328	{ "transaction_recover", (PyCFunction)obj_transaction_recover, METH_NOARGS,
329		"S.transaction_recover() -> None\n"
330		"Recover the currently active transaction." },
331	{ "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
332		"S.transaction_start() -> None\n"
333		"Start a new transaction." },
334	{ "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
335	{ "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
336	{ "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
337	{ "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
338	{ "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
339	{ "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
340	{ "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
341		"Fetch a value." },
342	{ "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
343		"Append data to an existing key." },
344	{ "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
345		"Return the first key in this database." },
346	{ "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
347		"Return the next key in this database." },
348	{ "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
349		"Delete an entry." },
350	{ "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
351		"Check whether key exists in this database." },
352	{ "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
353		"Store data." },
354	{ "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
355	{ "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
356		"Wipe the entire database." },
357	{ NULL }
358};
359
360static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
361{
362	return PyInt_FromLong(tdb_hash_size(self->ctx));
363}
364
365static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
366{
367	if (!PyInt_Check(max_dead))
368		return -1;
369	tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
370	return 0;
371}
372
373static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
374{
375	return PyInt_FromLong(tdb_map_size(self->ctx));
376}
377
378static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
379{
380	return PyInt_FromLong(tdb_get_flags(self->ctx));
381}
382
383static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
384{
385	return PyString_FromString(tdb_name(self->ctx));
386}
387
388static PyGetSetDef tdb_object_getsetters[] = {
389	{ (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL },
390	{ (char *)"map_size", (getter)obj_get_map_size, NULL, NULL },
391	{ (char *)"flags", (getter)obj_get_flags, NULL, NULL },
392	{ (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL },
393	{ (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
394	{ NULL }
395};
396
397static PyObject *tdb_object_repr(PyTdbObject *self)
398{
399	return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
400}
401
402static void tdb_object_dealloc(PyTdbObject *self)
403{
404	if (!self->closed)
405		tdb_close(self->ctx);
406	PyObject_Del(self);
407}
408
409static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
410{
411	TDB_DATA tkey, val;
412	if (!PyString_Check(key)) {
413		PyErr_SetString(PyExc_TypeError, "Expected string as key");
414		return NULL;
415	}
416
417	tkey.dptr = (unsigned char *)PyString_AsString(key);
418	tkey.dsize = PyString_Size(key);
419
420	val = tdb_fetch(self->ctx, tkey);
421	if (val.dptr == NULL) {
422		PyErr_SetString(PyExc_KeyError, "No such TDB entry");
423		return NULL;
424	} else {
425		return PyString_FromTDB_DATA(val);
426	}
427}
428
429static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
430{
431	TDB_DATA tkey, tval;
432	int ret;
433	if (!PyString_Check(key)) {
434		PyErr_SetString(PyExc_TypeError, "Expected string as key");
435		return -1;
436	}
437
438	tkey = PyString_AsTDB_DATA(key);
439
440	if (value == NULL) {
441		ret = tdb_delete(self->ctx, tkey);
442	} else {
443		if (!PyString_Check(value)) {
444			PyErr_SetString(PyExc_TypeError, "Expected string as value");
445			return -1;
446		}
447
448		tval = PyString_AsTDB_DATA(value);
449
450		ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
451	}
452
453	if (ret != 0) {
454		PyErr_SetTDBError(self->ctx);
455		return -1;
456	}
457
458	return ret;
459}
460
461static PyMappingMethods tdb_object_mapping = {
462	.mp_subscript = (binaryfunc)obj_getitem,
463	.mp_ass_subscript = (objobjargproc)obj_setitem,
464};
465PyTypeObject PyTdb = {
466	.tp_name = "Tdb",
467	.tp_basicsize = sizeof(PyTdbObject),
468	.tp_methods = tdb_object_methods,
469	.tp_getset = tdb_object_getsetters,
470	.tp_new = py_tdb_open,
471	.tp_doc = "A TDB file",
472	.tp_repr = (reprfunc)tdb_object_repr,
473	.tp_dealloc = (destructor)tdb_object_dealloc,
474	.tp_as_mapping = &tdb_object_mapping,
475	.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
476	.tp_iter = (getiterfunc)tdb_object_iter,
477};
478
479static PyMethodDef tdb_methods[] = {
480	{ "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
481		"Open a TDB file." },
482	{ NULL }
483};
484
485void inittdb(void)
486{
487	PyObject *m;
488
489	if (PyType_Ready(&PyTdb) < 0)
490		return;
491
492	if (PyType_Ready(&PyTdbIterator) < 0)
493		return;
494
495	m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
496	if (m == NULL)
497		return;
498
499	PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
500	PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
501	PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
502
503	PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
504	PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST));
505	PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
506	PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
507	PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
508	PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
509	PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN));
510	PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
511
512	Py_INCREF(&PyTdb);
513	PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
514
515	Py_INCREF(&PyTdbIterator);
516}
517