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/>.
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
45 staticforward PyTypeObject PySMB
;
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
;
57 static void dos_format(char *s
)
59 string_replace(s
, '/', '\\');
64 * Connect to SMB share using smb_full_connection
66 static NTSTATUS
do_smb_connect(TALLOC_CTX
*mem_ctx
, struct smb_private_data
*spdata
,
67 const char *hostname
, const char *service
, struct smbcli_tree
**tree
)
69 struct smbcli_state
*smb_state
;
71 struct smbcli_options options
;
72 struct smbcli_session_options session_options
;
78 smb_state
= smbcli_state_init(mem_ctx
);
80 lpcfg_smbcli_options(spdata
->lp_ctx
, &options
);
81 lpcfg_smbcli_session_options(spdata
->lp_ctx
, &session_options
);
83 status
= smbcli_full_connection(mem_ctx
, &smb_state
, hostname
,
84 lpcfg_smb_ports(spdata
->lp_ctx
),
87 lpcfg_socket_options(spdata
->lp_ctx
),
89 lpcfg_resolve_context(spdata
->lp_ctx
),
93 lpcfg_gensec_settings(mem_ctx
, spdata
->lp_ctx
));
95 if (NT_STATUS_IS_OK(status
)) {
96 *tree
= smb_state
->tree
;
104 * Read SMB file and return the contents of the file as python string
106 static PyObject
* py_smb_loadfile(pytalloc_Object
*self
, PyObject
*args
)
108 struct smb_composite_loadfile io
;
109 const char *filename
;
111 struct smb_private_data
*spdata
;
113 if (!PyArg_ParseTuple(args
, "s:loadfile", &filename
)) {
119 io
.in
.fname
= filename
;
122 status
= smb_composite_loadfile(spdata
->tree
, self
->talloc_ctx
, &io
);
123 PyErr_NTSTATUS_IS_ERR_RAISE(status
);
125 return Py_BuildValue("s#", io
.out
.data
, io
.out
.size
);
129 * Create a SMB file with given string as the contents
131 static PyObject
* py_smb_savefile(pytalloc_Object
*self
, PyObject
*args
)
133 struct smb_composite_savefile io
;
134 const char *filename
;
137 struct smb_private_data
*spdata
;
139 if (!PyArg_ParseTuple(args
, "ss:savefile", &filename
, &data
)) {
143 io
.in
.fname
= filename
;
144 io
.in
.data
= (unsigned char *)data
;
145 io
.in
.size
= strlen(data
);
148 status
= smb_composite_savefile(spdata
->tree
, &io
);
149 PyErr_NTSTATUS_IS_ERR_RAISE(status
);
156 * Callback function to accumulate directory contents in a python list
158 static void py_smb_list_callback(struct clilist_file_info
*f
, const char *mask
, void *state
)
160 PyObject
*py_dirlist
;
163 if(!ISDOT(f
->name
) && !ISDOTDOT(f
->name
)) {
164 py_dirlist
= (PyObject
*)state
;
168 PyDict_SetItemString(dict
, "name", PyString_FromString(f
->name
));
170 /* Windows does not always return short_name */
172 PyDict_SetItemString(dict
, "short_name", PyString_FromString(f
->short_name
));
174 PyDict_SetItemString(dict
, "short_name", Py_None
);
177 PyDict_SetItemString(dict
, "size", PyLong_FromUnsignedLongLong(f
->size
));
178 PyDict_SetItemString(dict
, "attrib", PyInt_FromLong(f
->attrib
));
179 PyDict_SetItemString(dict
, "mtime", PyInt_FromLong(f
->mtime
));
181 PyList_Append(py_dirlist
, dict
);
188 * List the directory contents for specified directory (Ignore '.' and '..' dirs)
190 static PyObject
*py_smb_list(pytalloc_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
192 struct smb_private_data
*spdata
;
193 PyObject
*py_dirlist
;
194 const char *kwnames
[] = { "directory", "mask", "attribs", NULL
};
196 char *user_mask
= NULL
;
198 uint16_t attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_DIRECTORY
199 | FILE_ATTRIBUTE_ARCHIVE
;
201 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "z|sH:list",
202 discard_const_p(char *, kwnames
),
203 &base_dir
, &user_mask
, &attribute
)) {
207 if (user_mask
== NULL
) {
208 mask
= talloc_asprintf(self
->talloc_ctx
, "%s\\*", base_dir
);
210 mask
= talloc_asprintf(self
->talloc_ctx
, "%s\\%s", base_dir
, user_mask
);
216 if((py_dirlist
= PyList_New(0)) == NULL
) {
221 smbcli_list(spdata
->tree
, mask
, attribute
, py_smb_list_callback
, (void *)py_dirlist
);
232 static PyObject
*py_smb_mkdir(pytalloc_Object
*self
, PyObject
*args
)
236 struct smb_private_data
*spdata
;
238 if (!PyArg_ParseTuple(args
, "s:mkdir", &dirname
)) {
243 status
= smbcli_mkdir(spdata
->tree
, dirname
);
244 PyErr_NTSTATUS_IS_ERR_RAISE(status
);
253 static PyObject
*py_smb_rmdir(pytalloc_Object
*self
, PyObject
*args
)
257 struct smb_private_data
*spdata
;
259 if (!PyArg_ParseTuple(args
, "s:rmdir", &dirname
)) {
264 status
= smbcli_rmdir(spdata
->tree
, dirname
);
265 PyErr_NTSTATUS_IS_ERR_RAISE(status
);
272 * Check existence of a path
274 static PyObject
*py_smb_chkpath(pytalloc_Object
*self
, PyObject
*args
)
278 struct smb_private_data
*spdata
;
280 if (!PyArg_ParseTuple(args
, "s:chkpath", &path
)) {
285 status
= smbcli_chkpath(spdata
->tree
, path
);
287 if (NT_STATUS_IS_OK(status
)) {
296 * Read ACL on a given file/directory as a security descriptor object
298 static PyObject
*py_smb_getacl(pytalloc_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
302 union smb_fileinfo fio
;
303 struct smb_private_data
*spdata
;
304 const char *filename
;
307 if (!PyArg_ParseTuple(args
, "s:get_acl", &filename
)) {
315 io
.generic
.level
= RAW_OPEN_NTCREATEX
;
316 io
.ntcreatex
.in
.root_fid
.fnum
= 0;
317 io
.ntcreatex
.in
.flags
= 0;
318 io
.ntcreatex
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
319 io
.ntcreatex
.in
.create_options
= 0;
320 io
.ntcreatex
.in
.file_attr
= FILE_ATTRIBUTE_NORMAL
;
321 io
.ntcreatex
.in
.share_access
= NTCREATEX_SHARE_ACCESS_READ
|
322 NTCREATEX_SHARE_ACCESS_WRITE
;
323 io
.ntcreatex
.in
.alloc_size
= 0;
324 io
.ntcreatex
.in
.open_disposition
= NTCREATEX_DISP_OPEN
;
325 io
.ntcreatex
.in
.impersonation
= NTCREATEX_IMPERSONATION_ANONYMOUS
;
326 io
.ntcreatex
.in
.security_flags
= 0;
327 io
.ntcreatex
.in
.fname
= filename
;
329 status
= smb_raw_open(spdata
->tree
, self
->talloc_ctx
, &io
);
330 PyErr_NTSTATUS_IS_ERR_RAISE(status
);
332 fnum
= io
.ntcreatex
.out
.file
.fnum
;
336 fio
.query_secdesc
.level
= RAW_FILEINFO_SEC_DESC
;
337 fio
.query_secdesc
.in
.file
.fnum
= fnum
;
338 fio
.query_secdesc
.in
.secinfo_flags
= SECINFO_OWNER
|
341 SECINFO_PROTECTED_DACL
|
342 SECINFO_UNPROTECTED_DACL
|
344 SECINFO_PROTECTED_SACL
|
345 SECINFO_UNPROTECTED_SACL
;
348 status
= smb_raw_query_secdesc(spdata
->tree
, self
->talloc_ctx
, &fio
);
349 smbcli_close(spdata
->tree
, fnum
);
351 PyErr_NTSTATUS_IS_ERR_RAISE(status
);
353 return py_return_ndr_struct("samba.dcerpc.security", "descriptor",
354 self
->talloc_ctx
, fio
.query_secdesc
.out
.sd
);
359 * Set ACL on file/directory using given security descriptor object
361 static PyObject
*py_smb_setacl(pytalloc_Object
*self
, PyObject
*args
, PyObject
*kwargs
)
365 union smb_setfileinfo fio
;
366 struct smb_private_data
*spdata
;
367 const char *filename
;
369 struct security_descriptor
*sd
;
372 if (!PyArg_ParseTuple(args
, "sO:set_acl", &filename
, &py_sd
)) {
378 sd
= pytalloc_get_type(py_sd
, struct security_descriptor
);
380 PyErr_Format(PyExc_TypeError
,
381 "Expected dcerpc.security.descriptor as argument, got %s",
382 talloc_get_name(pytalloc_get_ptr(py_sd
)));
390 io
.generic
.level
= RAW_OPEN_NTCREATEX
;
391 io
.ntcreatex
.in
.root_fid
.fnum
= 0;
392 io
.ntcreatex
.in
.flags
= 0;
393 io
.ntcreatex
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
394 io
.ntcreatex
.in
.create_options
= 0;
395 io
.ntcreatex
.in
.file_attr
= FILE_ATTRIBUTE_NORMAL
;
396 io
.ntcreatex
.in
.share_access
= NTCREATEX_SHARE_ACCESS_READ
|
397 NTCREATEX_SHARE_ACCESS_WRITE
;
398 io
.ntcreatex
.in
.alloc_size
= 0;
399 io
.ntcreatex
.in
.open_disposition
= NTCREATEX_DISP_OPEN
;
400 io
.ntcreatex
.in
.impersonation
= NTCREATEX_IMPERSONATION_ANONYMOUS
;
401 io
.ntcreatex
.in
.security_flags
= 0;
402 io
.ntcreatex
.in
.fname
= filename
;
404 status
= smb_raw_open(spdata
->tree
, self
->talloc_ctx
, &io
);
405 PyErr_NTSTATUS_IS_ERR_RAISE(status
);
407 fnum
= io
.ntcreatex
.out
.file
.fnum
;
411 fio
.set_secdesc
.level
= RAW_SFILEINFO_SEC_DESC
;
412 fio
.set_secdesc
.in
.file
.fnum
= fnum
;
413 fio
.set_secdesc
.in
.secinfo_flags
= 0;
414 fio
.set_secdesc
.in
.sd
= sd
;
416 status
= smb_raw_set_secdesc(spdata
->tree
, &fio
);
417 smbcli_close(spdata
->tree
, fnum
);
419 PyErr_NTSTATUS_IS_ERR_RAISE(status
);
425 static PyMethodDef py_smb_methods
[] = {
426 { "loadfile", (PyCFunction
)py_smb_loadfile
, METH_VARARGS
,
427 "loadfile(path) -> file contents as a string\n\n \
428 Read contents of a file." },
429 { "savefile", (PyCFunction
)py_smb_savefile
, METH_VARARGS
,
430 "savefile(path, str) -> None\n\n \
431 Write string str to file." },
432 { "list", (PyCFunction
)py_smb_list
, METH_VARARGS
|METH_KEYWORDS
,
433 "list(path) -> directory contents as a dictionary\n\n \
434 List contents of a directory. The keys are, \n \
435 \tname: Long name of the directory item\n \
436 \tshort_name: Short name of the directory item\n \
437 \tsize: File size in bytes\n \
438 \tattrib: Attributes\n \
439 \tmtime: Modification time\n" },
440 { "mkdir", (PyCFunction
)py_smb_mkdir
, METH_VARARGS
,
441 "mkdir(path) -> None\n\n \
442 Create a directory." },
443 { "rmdir", (PyCFunction
)py_smb_rmdir
, METH_VARARGS
,
444 "rmdir(path) -> None\n\n \
445 Delete a directory." },
446 { "chkpath", (PyCFunction
)py_smb_chkpath
, METH_VARARGS
,
447 "chkpath(path) -> True or False\n\n \
448 Return true if path exists, false otherwise." },
449 { "get_acl", (PyCFunction
)py_smb_getacl
, METH_VARARGS
,
450 "get_acl(path) -> security_descriptor object\n\n \
451 Get security descriptor for file." },
452 { "set_acl", (PyCFunction
)py_smb_setacl
, METH_VARARGS
,
453 "set_acl(path, security_descriptor) -> None\n\n \
454 Set security descriptor for file." },
459 static PyObject
*py_smb_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
461 PyObject
*py_creds
= Py_None
;
462 PyObject
*py_lp
= Py_None
;
463 const char *kwnames
[] = { "hostname", "service", "creds", "lp", NULL
};
464 const char *hostname
= NULL
;
465 const char *service
= NULL
;
466 pytalloc_Object
*smb
;
467 struct smb_private_data
*spdata
;
470 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "zz|OO",
471 discard_const_p(char *, kwnames
),
472 &hostname
, &service
, &py_creds
, &py_lp
)) {
476 smb
= (pytalloc_Object
*)type
->tp_alloc(type
, 0);
481 smb
->talloc_ctx
= talloc_new(NULL
);
482 if (smb
->talloc_ctx
== NULL
) {
487 spdata
= talloc_zero(smb
->talloc_ctx
, struct smb_private_data
);
488 if (spdata
== NULL
) {
494 spdata
->lp_ctx
= lpcfg_from_py_object(smb
->talloc_ctx
, py_lp
);
495 if (spdata
->lp_ctx
== NULL
) {
499 spdata
->creds
= PyCredentials_AsCliCredentials(py_creds
);
500 spdata
->ev_ctx
= s4_event_context_init(smb
->talloc_ctx
);
501 if (spdata
->ev_ctx
== NULL
) {
507 status
= do_smb_connect(smb
->talloc_ctx
, spdata
, hostname
, service
, &spdata
->tree
);
508 PyErr_NTSTATUS_IS_ERR_RAISE(status
);
509 if (spdata
->tree
== NULL
) {
515 return (PyObject
*)smb
;
519 static PyTypeObject PySMB
= {
520 .tp_name
= "smb.SMB",
521 .tp_basicsize
= sizeof(pytalloc_Object
),
522 .tp_new
= py_smb_new
,
523 .tp_flags
= Py_TPFLAGS_DEFAULT
,
524 .tp_methods
= py_smb_methods
,
525 .tp_doc
= "SMB(hostname, service[, lp[, creds]]) -> SMB connection object\n",
532 PyTypeObject
*talloc_type
= pytalloc_GetObjectType();
533 if (talloc_type
== NULL
) {
537 PySMB
.tp_base
= talloc_type
;
539 if (PyType_Ready(&PySMB
) < 0) {
543 m
= Py_InitModule3("smb", NULL
, "SMB File I/O support");
549 PyModule_AddObject(m
, "SMB", (PyObject
*)&PySMB
);
551 #define ADD_FLAGS(val) PyModule_AddObject(m, #val, PyInt_FromLong(val))
553 ADD_FLAGS(FILE_ATTRIBUTE_READONLY
);
554 ADD_FLAGS(FILE_ATTRIBUTE_HIDDEN
);
555 ADD_FLAGS(FILE_ATTRIBUTE_SYSTEM
);
556 ADD_FLAGS(FILE_ATTRIBUTE_VOLUME
);
557 ADD_FLAGS(FILE_ATTRIBUTE_DIRECTORY
);
558 ADD_FLAGS(FILE_ATTRIBUTE_ARCHIVE
);
559 ADD_FLAGS(FILE_ATTRIBUTE_DEVICE
);
560 ADD_FLAGS(FILE_ATTRIBUTE_NORMAL
);
561 ADD_FLAGS(FILE_ATTRIBUTE_TEMPORARY
);
562 ADD_FLAGS(FILE_ATTRIBUTE_SPARSE
);
563 ADD_FLAGS(FILE_ATTRIBUTE_REPARSE_POINT
);
564 ADD_FLAGS(FILE_ATTRIBUTE_COMPRESSED
);
565 ADD_FLAGS(FILE_ATTRIBUTE_OFFLINE
);
566 ADD_FLAGS(FILE_ATTRIBUTE_NONINDEXED
);
567 ADD_FLAGS(FILE_ATTRIBUTE_ENCRYPTED
);
568 ADD_FLAGS(FILE_ATTRIBUTE_ALL_MASK
);