s3-pylibsmb: Factor out py_tevent_cond_wait
[bbaumbach/samba-autobuild/.git] / source3 / libsmb / pylibsmb.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Samba-internal work in progress Python binding for libsmbclient
4  *
5  * Copyright (C) Volker Lendecke 2012
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <Python.h>
22 #include "includes.h"
23 #include "libsmb/libsmb.h"
24 #include "libcli/security/security.h"
25 #include "system/select.h"
26 #include "source4/libcli/util/pyerrors.h"
27 #include "auth/credentials/pycredentials.h"
28
29 static PyTypeObject *get_pytype(const char *module, const char *type)
30 {
31         PyObject *mod;
32         PyTypeObject *result;
33
34         mod = PyImport_ImportModule(module);
35         if (mod == NULL) {
36                 PyErr_Format(PyExc_RuntimeError,
37                              "Unable to import %s to check type %s",
38                              module, type);
39                 return NULL;
40         }
41         result = (PyTypeObject *)PyObject_GetAttrString(mod, type);
42         Py_DECREF(mod);
43         if (result == NULL) {
44                 PyErr_Format(PyExc_RuntimeError,
45                              "Unable to find type %s in module %s",
46                              module, type);
47                 return NULL;
48         }
49         return result;
50 }
51
52 struct py_cli_thread;
53
54 struct py_cli_state {
55         PyObject_HEAD
56         struct cli_state *cli;
57         struct tevent_context *ev;
58         struct py_cli_thread *thread_state;
59 };
60
61 #if HAVE_PTHREAD
62
63 #include <pthread.h>
64
65 struct py_cli_thread {
66
67         /*
68          * Pipe to make the poll thread wake up in our destructor, so
69          * that we can exit and join the thread.
70          */
71         int shutdown_pipe[2];
72         struct tevent_fd *shutdown_fde;
73         bool do_shutdown;
74         pthread_t id;
75
76         /*
77          * Thread state to release the GIL during the poll(2) syscall
78          */
79         PyThreadState *py_threadstate;
80 };
81
82 static void *py_cli_state_poll_thread(void *private_data)
83 {
84         struct py_cli_state *self = (struct py_cli_state *)private_data;
85         struct py_cli_thread *t = self->thread_state;
86         PyGILState_STATE gstate;
87
88         gstate = PyGILState_Ensure();
89
90         while (!t->do_shutdown) {
91                 int ret;
92                 ret = tevent_loop_once(self->ev);
93                 assert(ret == 0);
94         }
95         PyGILState_Release(gstate);
96         return NULL;
97 }
98
99 static void py_cli_state_trace_callback(enum tevent_trace_point point,
100                                         void *private_data)
101 {
102         struct py_cli_state *self = (struct py_cli_state *)private_data;
103         struct py_cli_thread *t = self->thread_state;
104
105         switch(point) {
106         case TEVENT_TRACE_BEFORE_WAIT:
107                 assert(t->py_threadstate == NULL);
108                 t->py_threadstate = PyEval_SaveThread();
109                 break;
110         case TEVENT_TRACE_AFTER_WAIT:
111                 assert(t->py_threadstate != NULL);
112                 PyEval_RestoreThread(t->py_threadstate);
113                 t->py_threadstate = NULL;
114                 break;
115         default:
116                 break;
117         }
118 }
119
120 static void py_cli_state_shutdown_handler(struct tevent_context *ev,
121                                           struct tevent_fd *fde,
122                                           uint16_t flags,
123                                           void *private_data)
124 {
125         struct py_cli_state *self = (struct py_cli_state *)private_data;
126         struct py_cli_thread *t = self->thread_state;
127
128         if ((flags & TEVENT_FD_READ) == 0) {
129                 return;
130         }
131         TALLOC_FREE(t->shutdown_fde);
132         t->do_shutdown = true;
133 }
134
135 static int py_cli_thread_destructor(struct py_cli_thread *t)
136 {
137         char c = 0;
138         ssize_t written;
139         int ret;
140
141         do {
142                 /*
143                  * This will wake the poll thread from the poll(2)
144                  */
145                 written = write(t->shutdown_pipe[1], &c, 1);
146         } while ((written == -1) && (errno == EINTR));
147
148         /*
149          * Allow the poll thread to do its own cleanup under the GIL
150          */
151         Py_BEGIN_ALLOW_THREADS
152         ret = pthread_join(t->id, NULL);
153         Py_END_ALLOW_THREADS
154         assert(ret == 0);
155
156         if (t->shutdown_pipe[0] != -1) {
157                 close(t->shutdown_pipe[0]);
158                 t->shutdown_pipe[0] = -1;
159         }
160         if (t->shutdown_pipe[1] != -1) {
161                 close(t->shutdown_pipe[1]);
162                 t->shutdown_pipe[1] = -1;
163         }
164         return 0;
165 }
166
167 static bool py_cli_state_setup_ev(struct py_cli_state *self)
168 {
169         struct py_cli_thread *t = NULL;
170         int ret;
171
172         self->ev = tevent_context_init_byname(NULL, "poll_mt");
173         if (self->ev == NULL) {
174                 goto fail;
175         }
176         tevent_set_trace_callback(self->ev, py_cli_state_trace_callback, self);
177
178         self->thread_state = talloc_zero(NULL, struct py_cli_thread);
179         if (self->thread_state == NULL) {
180                 goto fail;
181         }
182         t = self->thread_state;
183
184         ret = pipe(t->shutdown_pipe);
185         if (ret == -1) {
186                 goto fail;
187         }
188         t->shutdown_fde = tevent_add_fd(
189                 self->ev, self->ev, t->shutdown_pipe[0], TEVENT_FD_READ,
190                 py_cli_state_shutdown_handler, self);
191         if (t->shutdown_fde == NULL) {
192                 goto fail;
193         }
194
195         PyEval_InitThreads();
196
197         ret = pthread_create(&t->id, NULL, py_cli_state_poll_thread, self);
198         if (ret != 0) {
199                 goto fail;
200         }
201         talloc_set_destructor(self->thread_state, py_cli_thread_destructor);
202         return true;
203
204 fail:
205         if (t != NULL) {
206                 TALLOC_FREE(t->shutdown_fde);
207
208                 if (t->shutdown_pipe[0] != -1) {
209                         close(t->shutdown_pipe[0]);
210                         t->shutdown_pipe[0] = -1;
211                 }
212                 if (t->shutdown_pipe[1] != -1) {
213                         close(t->shutdown_pipe[1]);
214                         t->shutdown_pipe[1] = -1;
215                 }
216         }
217
218         TALLOC_FREE(self->thread_state);
219         TALLOC_FREE(self->ev);
220         return false;
221 }
222
223 struct py_tevent_cond {
224         pthread_mutex_t mutex;
225         pthread_cond_t cond;
226         bool is_done;
227 };
228
229 static void py_tevent_signalme(struct tevent_req *req);
230
231 static int py_tevent_cond_wait(struct py_tevent_cond *cond)
232 {
233         int ret, result;
234
235         result = pthread_mutex_init(&cond->mutex, NULL);
236         if (result != 0) {
237                 goto fail;
238         }
239         result = pthread_cond_init(&cond->cond, NULL);
240         if (result != 0) {
241                 goto fail_mutex;
242         }
243
244         result = pthread_mutex_lock(&cond->mutex);
245         if (result != 0) {
246                 goto fail_cond;
247         }
248
249         cond->is_done = false;
250
251         while (!cond->is_done) {
252
253                 Py_BEGIN_ALLOW_THREADS
254                 result = pthread_cond_wait(&cond->cond, &cond->mutex);
255                 Py_END_ALLOW_THREADS
256
257                 if (result != 0) {
258                         goto fail_unlock;
259                 }
260         }
261
262 fail_unlock:
263         ret = pthread_mutex_unlock(&cond->mutex);
264         assert(ret == 0);
265 fail_cond:
266         ret = pthread_cond_destroy(&cond->cond);
267         assert(ret == 0);
268 fail_mutex:
269         ret = pthread_mutex_destroy(&cond->mutex);
270         assert(ret == 0);
271 fail:
272         return result;
273 }
274
275 static int py_tevent_req_wait(struct tevent_context *ev,
276                               struct tevent_req *req)
277 {
278         struct py_tevent_cond cond;
279         tevent_req_set_callback(req, py_tevent_signalme, &cond);
280         return py_tevent_cond_wait(&cond);
281 }
282
283 static void py_tevent_signalme(struct tevent_req *req)
284 {
285         struct py_tevent_cond *cond = (struct py_tevent_cond *)
286                 tevent_req_callback_data_void(req);
287         int ret;
288
289         ret = pthread_mutex_lock(&cond->mutex);
290         assert(ret == 0);
291
292         cond->is_done = true;
293
294         ret = pthread_cond_signal(&cond->cond);
295         assert(ret == 0);
296         ret = pthread_mutex_unlock(&cond->mutex);
297         assert(ret == 0);
298 }
299
300 #else
301
302 static bool py_cli_state_setup_ev(struct py_cli_state *self)
303 {
304         self->ev = tevent_context_init(NULL);
305         return (self->ev != NULL);
306 }
307
308 static int py_tevent_req_wait(struct tevent_context *ev,
309                               struct tevent_req *req)
310 {
311         while (tevent_req_is_in_progress(req)) {
312                 int ret;
313
314                 ret = tevent_loop_once(ev);
315                 if (ret != 0) {
316                         return ret;
317                 }
318         }
319         return 0;
320 }
321
322 #endif
323
324 static PyObject *py_cli_state_new(PyTypeObject *type, PyObject *args,
325                                   PyObject *kwds)
326 {
327         struct py_cli_state *self;
328
329         self = (struct py_cli_state *)type->tp_alloc(type, 0);
330         if (self == NULL) {
331                 return NULL;
332         }
333         self->cli = NULL;
334         self->ev = NULL;
335         self->thread_state = NULL;
336         return (PyObject *)self;
337 }
338
339 static int py_cli_state_init(struct py_cli_state *self, PyObject *args,
340                              PyObject *kwds)
341 {
342         NTSTATUS status;
343         char *host, *share;
344         PyObject *creds;
345         struct cli_credentials *cli_creds;
346         bool ret;
347
348         static const char *kwlist[] = {
349                 "host", "share", "credentials", NULL
350         };
351
352         PyTypeObject *py_type_Credentials = get_pytype(
353                 "samba.credentials", "Credentials");
354         if (py_type_Credentials == NULL) {
355                 return -1;
356         }
357
358         ret = PyArg_ParseTupleAndKeywords(
359                 args, kwds, "ss|O!", (char **)kwlist,
360                 &host, &share, py_type_Credentials, &creds);
361
362         Py_DECREF(py_type_Credentials);
363
364         if (!ret) {
365                 return -1;
366         }
367
368         if (!py_cli_state_setup_ev(self)) {
369                 return -1;
370         }
371
372         cli_creds = cli_credentials_from_py_object(creds);
373         if (cli_creds == NULL) {
374                 PyErr_SetString(PyExc_TypeError, "Expected credentials");
375                 return -1;
376         }
377
378         status = cli_full_connection(
379                 &self->cli, "myname", host, NULL, 0, share, "?????",
380                 cli_credentials_get_username(cli_creds),
381                 cli_credentials_get_domain(cli_creds),
382                 cli_credentials_get_password(cli_creds),
383                 0, 0);
384         if (!NT_STATUS_IS_OK(status)) {
385                 PyErr_SetNTSTATUS(status);
386                 return -1;
387         }
388         return 0;
389 }
390
391 static void py_cli_state_dealloc(struct py_cli_state *self)
392 {
393         TALLOC_FREE(self->thread_state);
394         TALLOC_FREE(self->ev);
395
396         if (self->cli != NULL) {
397                 cli_shutdown(self->cli);
398                 self->cli = NULL;
399         }
400         self->ob_type->tp_free((PyObject *)self);
401 }
402
403 static bool py_tevent_req_wait_exc(struct tevent_context *ev,
404                                    struct tevent_req *req)
405 {
406         int ret;
407
408         if (req == NULL) {
409                 PyErr_NoMemory();
410                 return false;
411         }
412         ret = py_tevent_req_wait(ev, req);
413         if (ret != 0) {
414                 TALLOC_FREE(req);
415                 errno = ret;
416                 PyErr_SetFromErrno(PyExc_RuntimeError);
417                 return false;
418         }
419         return true;
420 }
421
422 static PyObject *py_cli_create(struct py_cli_state *self, PyObject *args,
423                                PyObject *kwds)
424 {
425         char *fname;
426         unsigned CreateFlags = 0;
427         unsigned DesiredAccess = FILE_GENERIC_READ;
428         unsigned FileAttributes = 0;
429         unsigned ShareAccess = 0;
430         unsigned CreateDisposition = FILE_OPEN;
431         unsigned CreateOptions = 0;
432         unsigned SecurityFlags = 0;
433         uint16_t fnum;
434         struct tevent_req *req;
435         NTSTATUS status;
436
437         static const char *kwlist[] = {
438                 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
439                 "ShareAccess", "CreateDisposition", "CreateOptions",
440                 "SecurityFlags", NULL };
441
442         if (!PyArg_ParseTupleAndKeywords(
443                     args, kwds, "s|IIIIIII", (char **)kwlist,
444                     &fname, &CreateFlags, &DesiredAccess, &FileAttributes,
445                     &ShareAccess, &CreateDisposition, &CreateOptions,
446                     &SecurityFlags)) {
447                 return NULL;
448         }
449
450         req = cli_ntcreate_send(NULL, self->ev, self->cli, fname, CreateFlags,
451                                 DesiredAccess, FileAttributes, ShareAccess,
452                                 CreateDisposition, CreateOptions,
453                                 SecurityFlags);
454         if (!py_tevent_req_wait_exc(self->ev, req)) {
455                 return NULL;
456         }
457         status = cli_ntcreate_recv(req, &fnum);
458         TALLOC_FREE(req);
459
460         if (!NT_STATUS_IS_OK(status)) {
461                 PyErr_SetNTSTATUS(status);
462                 return NULL;
463         }
464         return Py_BuildValue("I", (unsigned)fnum);
465 }
466
467 static PyObject *py_cli_close(struct py_cli_state *self, PyObject *args)
468 {
469         struct tevent_req *req;
470         int fnum;
471         NTSTATUS status;
472
473         if (!PyArg_ParseTuple(args, "i", &fnum)) {
474                 return NULL;
475         }
476
477         req = cli_close_send(NULL, self->ev, self->cli, fnum);
478         if (!py_tevent_req_wait_exc(self->ev, req)) {
479                 return NULL;
480         }
481         status = cli_close_recv(req);
482         TALLOC_FREE(req);
483
484         if (!NT_STATUS_IS_OK(status)) {
485                 PyErr_SetNTSTATUS(status);
486                 return NULL;
487         }
488         Py_INCREF(Py_None);
489         return Py_None;
490 }
491
492 static PyObject *py_cli_write(struct py_cli_state *self, PyObject *args,
493                               PyObject *kwds)
494 {
495         int fnum;
496         unsigned mode = 0;
497         char *buf;
498         int buflen;
499         unsigned long long offset;
500         struct tevent_req *req;
501         NTSTATUS status;
502         size_t written;
503
504         static const char *kwlist[] = {
505                 "fnum", "buffer", "offset", "mode", NULL };
506
507         if (!PyArg_ParseTupleAndKeywords(
508                     args, kwds, "Is#K|I", (char **)kwlist,
509                     &fnum, &buf, &buflen, &offset, &mode)) {
510                 return NULL;
511         }
512
513         req = cli_write_andx_send(NULL, self->ev, self->cli, fnum, mode,
514                                   (uint8_t *)buf, offset, buflen);
515         if (!py_tevent_req_wait_exc(self->ev, req)) {
516                 return NULL;
517         }
518         status = cli_write_andx_recv(req, &written);
519         TALLOC_FREE(req);
520
521         if (!NT_STATUS_IS_OK(status)) {
522                 PyErr_SetNTSTATUS(status);
523                 return NULL;
524         }
525         return Py_BuildValue("K", (unsigned long long)written);
526 }
527
528 static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
529                              PyObject *kwds)
530 {
531         int fnum;
532         unsigned long long offset;
533         unsigned size;
534         struct tevent_req *req;
535         NTSTATUS status;
536         uint8_t *buf;
537         ssize_t buflen;
538         PyObject *result;
539
540         static const char *kwlist[] = {
541                 "fnum", "offset", "size", NULL };
542
543         if (!PyArg_ParseTupleAndKeywords(
544                     args, kwds, "IKI", (char **)kwlist, &fnum, &offset,
545                     &size)) {
546                 return NULL;
547         }
548
549         req = cli_read_andx_send(NULL, self->ev, self->cli, fnum,
550                                  offset, size);
551         if (!py_tevent_req_wait_exc(self->ev, req)) {
552                 return NULL;
553         }
554         status = cli_read_andx_recv(req, &buflen, &buf);
555
556         if (!NT_STATUS_IS_OK(status)) {
557                 TALLOC_FREE(req);
558                 PyErr_SetNTSTATUS(status);
559                 return NULL;
560         }
561         result = Py_BuildValue("s#", (char *)buf, (int)buflen);
562         TALLOC_FREE(req);
563         return result;
564 }
565
566 static PyObject *py_cli_ftruncate(struct py_cli_state *self, PyObject *args,
567                                   PyObject *kwds)
568 {
569         int fnum;
570         unsigned long long size;
571         struct tevent_req *req;
572         NTSTATUS status;
573
574         static const char *kwlist[] = {
575                 "fnum", "size", NULL };
576
577         if (!PyArg_ParseTupleAndKeywords(
578                     args, kwds, "IK", (char **)kwlist, &fnum, &size)) {
579                 return NULL;
580         }
581
582         req = cli_ftruncate_send(NULL, self->ev, self->cli, fnum, size);
583         if (!py_tevent_req_wait_exc(self->ev, req)) {
584                 return NULL;
585         }
586         status = cli_ftruncate_recv(req);
587         TALLOC_FREE(req);
588
589         if (!NT_STATUS_IS_OK(status)) {
590                 PyErr_SetNTSTATUS(status);
591                 return NULL;
592         }
593         Py_INCREF(Py_None);
594         return Py_None;
595 }
596
597 static PyObject *py_cli_delete_on_close(struct py_cli_state *self,
598                                         PyObject *args,
599                                         PyObject *kwds)
600 {
601         unsigned fnum, flag;
602         struct tevent_req *req;
603         NTSTATUS status;
604
605         static const char *kwlist[] = {
606                 "fnum", "flag", NULL };
607
608         if (!PyArg_ParseTupleAndKeywords(
609                     args, kwds, "II", (char **)kwlist, &fnum, &flag)) {
610                 return NULL;
611         }
612
613         req = cli_nt_delete_on_close_send(NULL, self->ev, self->cli, fnum,
614                                           flag);
615         if (!py_tevent_req_wait_exc(self->ev, req)) {
616                 return NULL;
617         }
618         status = cli_nt_delete_on_close_recv(req);
619         TALLOC_FREE(req);
620
621         if (!NT_STATUS_IS_OK(status)) {
622                 PyErr_SetNTSTATUS(status);
623                 return NULL;
624         }
625         Py_INCREF(Py_None);
626         return Py_None;
627 }
628
629 static PyMethodDef py_cli_state_methods[] = {
630         { "create", (PyCFunction)py_cli_create, METH_VARARGS|METH_KEYWORDS,
631           "Open a file" },
632         { "close", (PyCFunction)py_cli_close, METH_VARARGS,
633           "Close a file handle" },
634         { "write", (PyCFunction)py_cli_write, METH_VARARGS|METH_KEYWORDS,
635           "Write to a file handle" },
636         { "read", (PyCFunction)py_cli_read, METH_VARARGS|METH_KEYWORDS,
637           "Read from a file handle" },
638         { "truncate", (PyCFunction)py_cli_ftruncate,
639           METH_VARARGS|METH_KEYWORDS,
640           "Truncate a file" },
641         { "delete_on_close", (PyCFunction)py_cli_delete_on_close,
642           METH_VARARGS|METH_KEYWORDS,
643           "Set/Reset the delete on close flag" },
644         { NULL, NULL, 0, NULL }
645 };
646
647 static PyTypeObject py_cli_state_type = {
648         PyObject_HEAD_INIT(NULL)
649         .tp_name = "libsmb_samba_internal.Conn",
650         .tp_basicsize = sizeof(struct py_cli_state),
651         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
652         .tp_doc = "libsmb connection",
653         .tp_new = py_cli_state_new,
654         .tp_init = (initproc)py_cli_state_init,
655         .tp_dealloc = (destructor)py_cli_state_dealloc,
656         .tp_methods = py_cli_state_methods,
657 };
658
659 static PyMethodDef py_libsmb_methods[] = {
660         { NULL },
661 };
662
663 void initlibsmb_samba_internal(void);
664 void initlibsmb_samba_internal(void)
665 {
666         PyObject *m;
667
668         talloc_stackframe();
669
670         m = Py_InitModule3("libsmb_samba_internal", py_libsmb_methods,
671                            "libsmb wrapper");
672
673         if (PyType_Ready(&py_cli_state_type) < 0) {
674                 return;
675         }
676         Py_INCREF(&py_cli_state_type);
677         PyModule_AddObject(m, "Conn", (PyObject *)&py_cli_state_type);
678 }