Revert "s3: temporary hack to make the waf build work withouth autotools being required"
[Samba.git] / source4 / libcli / pysmb.c
blob3f2efe9c87218a3e6db0522454305f5dec11bbfc
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Amitay Isaacs 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <Python.h>
21 #include <tevent.h>
22 #include <pytalloc.h>
23 #include "includes.h"
24 #include "param/param.h"
25 #include "param/pyparam.h"
26 #include "system/dir.h"
27 #include "system/filesys.h"
28 #include "lib/events/events.h"
29 #include "auth/credentials/credentials.h"
30 #include "auth/credentials/pycredentials.h"
31 #include "auth/gensec/gensec.h"
32 #include "libcli/libcli.h"
33 #include "libcli/raw/libcliraw.h"
34 #include "libcli/raw/raw_proto.h"
35 #include "libcli/resolve/resolve.h"
36 #include "libcli/util/pyerrors.h"
37 #include "libcli/smb_composite/smb_composite.h"
38 #include "libcli/security/security_descriptor.h"
39 #include "librpc/rpc/pyrpc_util.h"
41 #ifndef Py_RETURN_NONE
42 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
43 #endif
45 staticforward PyTypeObject PySMB;
47 void initsmb(void);
49 struct smb_private_data {
50 struct loadparm_context *lp_ctx;
51 struct cli_credentials *creds;
52 struct tevent_context *ev_ctx;
53 struct smbcli_tree *tree;
56 static void dos_format(char *s)
58 string_replace(s, '/', '\\');
63 * Connect to SMB share using smb_full_connection
65 static NTSTATUS do_smb_connect(TALLOC_CTX *mem_ctx, struct smb_private_data *spdata,
66 const char *hostname, const char *service, struct smbcli_tree **tree)
68 struct smbcli_state *smb_state;
69 NTSTATUS status;
70 struct smbcli_options options;
71 struct smbcli_session_options session_options;
73 *tree = NULL;
75 gensec_init();
77 smb_state = smbcli_state_init(mem_ctx);
79 lpcfg_smbcli_options(spdata->lp_ctx, &options);
80 lpcfg_smbcli_session_options(spdata->lp_ctx, &session_options);
82 status = smbcli_full_connection(mem_ctx, &smb_state, hostname,
83 lpcfg_smb_ports(spdata->lp_ctx),
84 service,
85 NULL,
86 lpcfg_socket_options(spdata->lp_ctx),
87 spdata->creds,
88 lpcfg_resolve_context(spdata->lp_ctx),
89 spdata->ev_ctx,
90 &options,
91 &session_options,
92 lpcfg_gensec_settings(mem_ctx, spdata->lp_ctx));
94 if (NT_STATUS_IS_OK(status)) {
95 *tree = smb_state->tree;
98 return status;
103 * Read SMB file and return the contents of the file as python string
105 static PyObject * py_smb_loadfile(pytalloc_Object *self, PyObject *args)
107 struct smb_composite_loadfile io;
108 const char *filename;
109 NTSTATUS status;
110 struct smb_private_data *spdata;
112 if (!PyArg_ParseTuple(args, "s:loadfile", &filename)) {
113 return NULL;
116 ZERO_STRUCT(io);
118 io.in.fname = filename;
120 spdata = self->ptr;
121 status = smb_composite_loadfile(spdata->tree, self->talloc_ctx, &io);
122 PyErr_NTSTATUS_IS_ERR_RAISE(status);
124 return Py_BuildValue("s#", io.out.data, io.out.size);
128 * Create a SMB file with given string as the contents
130 static PyObject * py_smb_savefile(pytalloc_Object *self, PyObject *args)
132 struct smb_composite_savefile io;
133 const char *filename;
134 char *data;
135 NTSTATUS status;
136 struct smb_private_data *spdata;
138 if (!PyArg_ParseTuple(args, "ss:savefile", &filename, &data)) {
139 return NULL;
142 io.in.fname = filename;
143 io.in.data = (unsigned char *)data;
144 io.in.size = strlen(data);
146 spdata = self->ptr;
147 status = smb_composite_savefile(spdata->tree, &io);
148 PyErr_NTSTATUS_IS_ERR_RAISE(status);
150 Py_RETURN_NONE;
154 * Callback function to accumulate directory contents in a python list
156 static void py_smb_list_callback(struct clilist_file_info *f, const char *mask, void *state)
158 PyObject *py_dirlist;
159 PyObject *dict;
161 if(!ISDOT(f->name) && !ISDOTDOT(f->name)) {
162 py_dirlist = (PyObject *)state;
164 dict = PyDict_New();
165 if(dict) {
166 PyDict_SetItemString(dict, "name", PyString_FromString(f->name));
168 /* Windows does not always return short_name */
169 if (f->short_name) {
170 PyDict_SetItemString(dict, "short_name", PyString_FromString(f->short_name));
171 } else {
172 PyDict_SetItemString(dict, "short_name", Py_None);
175 PyDict_SetItemString(dict, "size", PyLong_FromUnsignedLongLong(f->size));
176 PyDict_SetItemString(dict, "attrib", PyInt_FromLong(f->attrib));
177 PyDict_SetItemString(dict, "mtime", PyInt_FromLong(f->mtime));
179 PyList_Append(py_dirlist, dict);
185 * List the directory contents for specified directory (Ignore '.' and '..' dirs)
187 static PyObject *py_smb_list(pytalloc_Object *self, PyObject *args, PyObject *kwargs)
189 struct smb_private_data *spdata;
190 PyObject *py_dirlist;
191 const char *kwnames[] = { "directory", "mask", "attribs", NULL };
192 char *base_dir;
193 char *user_mask = NULL;
194 char *mask;
195 uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY
196 | FILE_ATTRIBUTE_ARCHIVE;
198 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z|sH:list",
199 discard_const_p(char *, kwnames),
200 &base_dir, &user_mask, &attribute)) {
201 return NULL;
204 if (user_mask == NULL) {
205 mask = talloc_asprintf(self->talloc_ctx, "%s\\*", base_dir);
206 } else {
207 mask = talloc_asprintf(self->talloc_ctx, "%s\\%s", base_dir, user_mask);
209 dos_format(mask);
211 spdata = self->ptr;
213 if((py_dirlist = PyList_New(0)) == NULL) {
214 PyErr_NoMemory();
215 return NULL;
218 smbcli_list(spdata->tree, mask, attribute, py_smb_list_callback, (void *)py_dirlist);
220 talloc_free(mask);
222 return py_dirlist;
226 * Create a directory
228 static PyObject *py_smb_mkdir(pytalloc_Object *self, PyObject *args)
230 NTSTATUS status;
231 const char *dirname;
232 struct smb_private_data *spdata;
234 if (!PyArg_ParseTuple(args, "s:mkdir", &dirname)) {
235 return NULL;
238 spdata = self->ptr;
239 status = smbcli_mkdir(spdata->tree, dirname);
240 PyErr_NTSTATUS_IS_ERR_RAISE(status);
242 Py_RETURN_NONE;
246 * Remove a directory
248 static PyObject *py_smb_rmdir(pytalloc_Object *self, PyObject *args)
250 NTSTATUS status;
251 const char *dirname;
252 struct smb_private_data *spdata;
254 if (!PyArg_ParseTuple(args, "s:rmdir", &dirname)) {
255 return NULL;
258 spdata = self->ptr;
259 status = smbcli_rmdir(spdata->tree, dirname);
260 PyErr_NTSTATUS_IS_ERR_RAISE(status);
262 Py_RETURN_NONE;
266 * Check existence of a path
268 static PyObject *py_smb_chkpath(pytalloc_Object *self, PyObject *args)
270 NTSTATUS status;
271 const char *path;
272 struct smb_private_data *spdata;
274 if (!PyArg_ParseTuple(args, "s:chkpath", &path)) {
275 return NULL;
278 spdata = self->ptr;
279 status = smbcli_chkpath(spdata->tree, path);
281 if (NT_STATUS_IS_OK(status)) {
282 Py_RETURN_TRUE;
285 Py_RETURN_FALSE;
289 * Read ACL on a given file/directory as a security descriptor object
291 static PyObject *py_smb_getacl(pytalloc_Object *self, PyObject *args, PyObject *kwargs)
293 NTSTATUS status;
294 union smb_open io;
295 union smb_fileinfo fio;
296 struct smb_private_data *spdata;
297 const char *filename;
298 int sinfo = 0;
299 int fnum;
301 if (!PyArg_ParseTuple(args, "s|i:get_acl", &filename, &sinfo)) {
302 return NULL;
305 ZERO_STRUCT(io);
307 spdata = self->ptr;
309 io.generic.level = RAW_OPEN_NTCREATEX;
310 io.ntcreatex.in.root_fid.fnum = 0;
311 io.ntcreatex.in.flags = 0;
312 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
313 io.ntcreatex.in.create_options = 0;
314 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
315 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
316 NTCREATEX_SHARE_ACCESS_WRITE;
317 io.ntcreatex.in.alloc_size = 0;
318 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
319 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
320 io.ntcreatex.in.security_flags = 0;
321 io.ntcreatex.in.fname = filename;
323 status = smb_raw_open(spdata->tree, self->talloc_ctx, &io);
324 PyErr_NTSTATUS_IS_ERR_RAISE(status);
326 fnum = io.ntcreatex.out.file.fnum;
328 ZERO_STRUCT(fio);
330 fio.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
331 fio.query_secdesc.in.file.fnum = fnum;
332 if (sinfo)
333 fio.query_secdesc.in.secinfo_flags = sinfo;
334 else
335 fio.query_secdesc.in.secinfo_flags = SECINFO_OWNER |
336 SECINFO_GROUP |
337 SECINFO_DACL |
338 SECINFO_PROTECTED_DACL |
339 SECINFO_UNPROTECTED_DACL |
340 SECINFO_SACL |
341 SECINFO_PROTECTED_SACL |
342 SECINFO_UNPROTECTED_SACL;
344 status = smb_raw_query_secdesc(spdata->tree, self->talloc_ctx, &fio);
345 smbcli_close(spdata->tree, fnum);
347 PyErr_NTSTATUS_IS_ERR_RAISE(status);
349 return py_return_ndr_struct("samba.dcerpc.security", "descriptor",
350 self->talloc_ctx, fio.query_secdesc.out.sd);
354 * Set ACL on file/directory using given security descriptor object
356 static PyObject *py_smb_setacl(pytalloc_Object *self, PyObject *args, PyObject *kwargs)
358 NTSTATUS status;
359 union smb_open io;
360 union smb_setfileinfo fio;
361 struct smb_private_data *spdata;
362 const char *filename;
363 PyObject *py_sd;
364 struct security_descriptor *sd;
365 uint32_t sinfo = 0;
366 int fnum;
368 if (!PyArg_ParseTuple(args, "sO|i:get_acl", &filename, &py_sd, &sinfo)) {
369 return NULL;
372 spdata = self->ptr;
374 sd = pytalloc_get_type(py_sd, struct security_descriptor);
375 if (!sd) {
376 PyErr_Format(PyExc_TypeError,
377 "Expected dcerpc.security.descriptor as argument, got %s",
378 talloc_get_name(pytalloc_get_ptr(py_sd)));
379 return NULL;
382 ZERO_STRUCT(io);
384 spdata = self->ptr;
386 io.generic.level = RAW_OPEN_NTCREATEX;
387 io.ntcreatex.in.root_fid.fnum = 0;
388 io.ntcreatex.in.flags = 0;
389 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
390 io.ntcreatex.in.create_options = 0;
391 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
392 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
393 NTCREATEX_SHARE_ACCESS_WRITE;
394 io.ntcreatex.in.alloc_size = 0;
395 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
396 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
397 io.ntcreatex.in.security_flags = 0;
398 io.ntcreatex.in.fname = filename;
400 status = smb_raw_open(spdata->tree, self->talloc_ctx, &io);
401 PyErr_NTSTATUS_IS_ERR_RAISE(status);
403 fnum = io.ntcreatex.out.file.fnum;
405 ZERO_STRUCT(fio);
407 fio.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
408 fio.set_secdesc.in.file.fnum = fnum;
409 if (sinfo)
410 fio.set_secdesc.in.secinfo_flags = sinfo;
411 else
412 fio.set_secdesc.in.secinfo_flags = SECINFO_OWNER |
413 SECINFO_GROUP |
414 SECINFO_DACL |
415 SECINFO_PROTECTED_DACL |
416 SECINFO_UNPROTECTED_DACL |
417 SECINFO_SACL |
418 SECINFO_PROTECTED_SACL |
419 SECINFO_UNPROTECTED_SACL;
421 fio.set_secdesc.in.sd = sd;
423 status = smb_raw_set_secdesc(spdata->tree, &fio);
424 smbcli_close(spdata->tree, fnum);
426 PyErr_NTSTATUS_IS_ERR_RAISE(status);
428 Py_RETURN_NONE;
432 * Open the file with the parameters passed in and return an object if OK
434 static PyObject *py_open_file(pytalloc_Object *self, PyObject *args, PyObject *kwargs)
436 NTSTATUS status;
437 union smb_open io;
438 struct smb_private_data *spdata;
439 const char *filename;
440 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
441 uint32_t share_access = NTCREATEX_SHARE_ACCESS_READ |
442 NTCREATEX_SHARE_ACCESS_WRITE;
443 uint32_t open_disposition = NTCREATEX_DISP_OPEN;
444 uint32_t create_options = 0;
445 TALLOC_CTX *mem_ctx;
446 int fnum;
448 if (!PyArg_ParseTuple(args, "s|iiii:open_file",
449 &filename,
450 &access_mask,
451 &share_access,
452 &open_disposition,
453 &create_options)) {
454 return NULL;
457 ZERO_STRUCT(io);
459 spdata = self->ptr;
461 mem_ctx = talloc_new(NULL);
463 io.generic.level = RAW_OPEN_NTCREATEX;
464 io.ntcreatex.in.root_fid.fnum = 0;
465 io.ntcreatex.in.flags = 0;
466 io.ntcreatex.in.access_mask = access_mask;
467 io.ntcreatex.in.create_options = create_options;
468 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
469 io.ntcreatex.in.share_access = share_access;
470 io.ntcreatex.in.alloc_size = 0;
471 io.ntcreatex.in.open_disposition = open_disposition;
472 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
473 io.ntcreatex.in.security_flags = 0;
474 io.ntcreatex.in.fname = filename;
476 status = smb_raw_open(spdata->tree, mem_ctx, &io);
477 talloc_free(mem_ctx);
479 PyErr_NTSTATUS_IS_ERR_RAISE(status);
481 fnum = io.ntcreatex.out.file.fnum;
483 return Py_BuildValue("i", fnum);
487 * Close the file based on the fnum passed in
489 static PyObject *py_close_file(pytalloc_Object *self, PyObject *args, PyObject *kwargs)
491 struct smb_private_data *spdata;
492 int fnum;
494 if (!PyArg_ParseTuple(args, "i:close_file", &fnum)) {
495 return NULL;
498 spdata = self->ptr;
501 * Should check the status ...
503 smbcli_close(spdata->tree, fnum);
505 Py_RETURN_NONE;
508 static PyMethodDef py_smb_methods[] = {
509 { "loadfile", (PyCFunction)py_smb_loadfile, METH_VARARGS,
510 "loadfile(path) -> file contents as a string\n\n \
511 Read contents of a file." },
512 { "savefile", (PyCFunction)py_smb_savefile, METH_VARARGS,
513 "savefile(path, str) -> None\n\n \
514 Write string str to file." },
515 { "list", (PyCFunction)py_smb_list, METH_VARARGS|METH_KEYWORDS,
516 "list(path) -> directory contents as a dictionary\n\n \
517 List contents of a directory. The keys are, \n \
518 \tname: Long name of the directory item\n \
519 \tshort_name: Short name of the directory item\n \
520 \tsize: File size in bytes\n \
521 \tattrib: Attributes\n \
522 \tmtime: Modification time\n" },
523 { "mkdir", (PyCFunction)py_smb_mkdir, METH_VARARGS,
524 "mkdir(path) -> None\n\n \
525 Create a directory." },
526 { "rmdir", (PyCFunction)py_smb_rmdir, METH_VARARGS,
527 "rmdir(path) -> None\n\n \
528 Delete a directory." },
529 { "chkpath", (PyCFunction)py_smb_chkpath, METH_VARARGS,
530 "chkpath(path) -> True or False\n\n \
531 Return true if path exists, false otherwise." },
532 { "get_acl", (PyCFunction)py_smb_getacl, METH_VARARGS,
533 "get_acl(path[, security_info=0]) -> security_descriptor object\n\n \
534 Get security descriptor for file." },
535 { "set_acl", (PyCFunction)py_smb_setacl, METH_VARARGS,
536 "set_acl(path, security_descriptor[, security_info=0]) -> None\n\n \
537 Set security descriptor for file." },
538 { "open_file", (PyCFunction)py_open_file, METH_VARARGS,
539 "open_file(path, access_mask[, share_access[, open_disposition[, create_options]]] -> fnum\n\n \
540 Open a file. Throws NTSTATUS exceptions on errors." },
541 { "close_file", (PyCFunction)py_close_file, METH_VARARGS,
542 "close_file(fnum) -> None\n\n \
543 Close the file based on fnum."},
544 { NULL },
547 static PyObject *py_smb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
549 PyObject *py_creds = Py_None;
550 PyObject *py_lp = Py_None;
551 const char *kwnames[] = { "hostname", "service", "creds", "lp", NULL };
552 const char *hostname = NULL;
553 const char *service = NULL;
554 pytalloc_Object *smb;
555 struct smb_private_data *spdata;
556 NTSTATUS status;
558 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "zz|OO",
559 discard_const_p(char *, kwnames),
560 &hostname, &service, &py_creds, &py_lp)) {
561 return NULL;
564 smb = (pytalloc_Object *)type->tp_alloc(type, 0);
565 if (smb == NULL) {
566 PyErr_NoMemory();
567 return NULL;
569 smb->talloc_ctx = talloc_new(NULL);
570 if (smb->talloc_ctx == NULL) {
571 PyErr_NoMemory();
572 return NULL;
575 spdata = talloc_zero(smb->talloc_ctx, struct smb_private_data);
576 if (spdata == NULL) {
577 PyErr_NoMemory();
578 Py_DECREF(smb);
579 return NULL;
582 spdata->lp_ctx = lpcfg_from_py_object(smb->talloc_ctx, py_lp);
583 if (spdata->lp_ctx == NULL) {
584 Py_DECREF(smb);
585 return NULL;
587 spdata->creds = PyCredentials_AsCliCredentials(py_creds);
588 spdata->ev_ctx = s4_event_context_init(smb->talloc_ctx);
589 if (spdata->ev_ctx == NULL) {
590 PyErr_NoMemory();
591 Py_DECREF(smb);
592 return NULL;
595 status = do_smb_connect(smb->talloc_ctx, spdata, hostname, service, &spdata->tree);
596 PyErr_NTSTATUS_IS_ERR_RAISE(status);
597 if (spdata->tree == NULL) {
598 Py_DECREF(smb);
599 return NULL;
602 smb->ptr = spdata;
603 return (PyObject *)smb;
606 static PyTypeObject PySMB = {
607 .tp_name = "smb.SMB",
608 .tp_basicsize = sizeof(pytalloc_Object),
609 .tp_new = py_smb_new,
610 .tp_flags = Py_TPFLAGS_DEFAULT,
611 .tp_methods = py_smb_methods,
612 .tp_doc = "SMB(hostname, service[, creds[, lp]]) -> SMB connection object\n",
616 void initsmb(void)
618 PyObject *m;
619 PyTypeObject *talloc_type = pytalloc_GetObjectType();
620 if (talloc_type == NULL) {
621 return;
624 PySMB.tp_base = talloc_type;
626 if (PyType_Ready(&PySMB) < 0) {
627 return;
630 m = Py_InitModule3("smb", NULL, "SMB File I/O support");
631 if (m == NULL) {
632 return;
635 Py_INCREF(&PySMB);
636 PyModule_AddObject(m, "SMB", (PyObject *)&PySMB);
638 #define ADD_FLAGS(val) PyModule_AddObject(m, #val, PyInt_FromLong(val))
640 ADD_FLAGS(FILE_ATTRIBUTE_READONLY);
641 ADD_FLAGS(FILE_ATTRIBUTE_HIDDEN);
642 ADD_FLAGS(FILE_ATTRIBUTE_SYSTEM);
643 ADD_FLAGS(FILE_ATTRIBUTE_VOLUME);
644 ADD_FLAGS(FILE_ATTRIBUTE_DIRECTORY);
645 ADD_FLAGS(FILE_ATTRIBUTE_ARCHIVE);
646 ADD_FLAGS(FILE_ATTRIBUTE_DEVICE);
647 ADD_FLAGS(FILE_ATTRIBUTE_NORMAL);
648 ADD_FLAGS(FILE_ATTRIBUTE_TEMPORARY);
649 ADD_FLAGS(FILE_ATTRIBUTE_SPARSE);
650 ADD_FLAGS(FILE_ATTRIBUTE_REPARSE_POINT);
651 ADD_FLAGS(FILE_ATTRIBUTE_COMPRESSED);
652 ADD_FLAGS(FILE_ATTRIBUTE_OFFLINE);
653 ADD_FLAGS(FILE_ATTRIBUTE_NONINDEXED);
654 ADD_FLAGS(FILE_ATTRIBUTE_ENCRYPTED);
655 ADD_FLAGS(FILE_ATTRIBUTE_ALL_MASK);