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