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