s4-samba-tool: add password verification in change user pass
[Samba/gbeck.git] / source4 / libcli / pysmb.c
blob2f9a579f1a486ee97c1cf9caa83217739d10c6ea
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;
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;
70 NTSTATUS status;
71 struct smbcli_options options;
72 struct smbcli_session_options session_options;
74 *tree = NULL;
76 gensec_init();
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),
85 service,
86 NULL,
87 lpcfg_socket_options(spdata->lp_ctx),
88 spdata->creds,
89 lpcfg_resolve_context(spdata->lp_ctx),
90 spdata->ev_ctx,
91 &options,
92 &session_options,
93 lpcfg_gensec_settings(mem_ctx, spdata->lp_ctx));
95 if (NT_STATUS_IS_OK(status)) {
96 *tree = smb_state->tree;
99 return status;
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;
110 NTSTATUS status;
111 struct smb_private_data *spdata;
113 if (!PyArg_ParseTuple(args, "s:loadfile", &filename)) {
114 return NULL;
117 ZERO_STRUCT(io);
119 io.in.fname = filename;
121 spdata = self->ptr;
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;
135 char *data;
136 NTSTATUS status;
137 struct smb_private_data *spdata;
139 if (!PyArg_ParseTuple(args, "ss:savefile", &filename, &data)) {
140 return NULL;
143 io.in.fname = filename;
144 io.in.data = (unsigned char *)data;
145 io.in.size = strlen(data);
147 spdata = self->ptr;
148 status = smb_composite_savefile(spdata->tree, &io);
149 PyErr_NTSTATUS_IS_ERR_RAISE(status);
151 Py_RETURN_NONE;
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;
161 PyObject *dict;
163 if(!ISDOT(f->name) && !ISDOTDOT(f->name)) {
164 py_dirlist = (PyObject *)state;
166 dict = PyDict_New();
167 if(dict) {
168 PyDict_SetItemString(dict, "name", PyString_FromString(f->name));
170 /* Windows does not always return short_name */
171 if (f->short_name) {
172 PyDict_SetItemString(dict, "short_name", PyString_FromString(f->short_name));
173 } else {
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 };
195 char *base_dir;
196 char *user_mask = NULL;
197 char *mask;
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)) {
204 return NULL;
207 if (user_mask == NULL) {
208 mask = talloc_asprintf(self->talloc_ctx, "%s\\*", base_dir);
209 } else {
210 mask = talloc_asprintf(self->talloc_ctx, "%s\\%s", base_dir, user_mask);
212 dos_format(mask);
214 spdata = self->ptr;
216 if((py_dirlist = PyList_New(0)) == NULL) {
217 PyErr_NoMemory();
218 return NULL;
221 smbcli_list(spdata->tree, mask, attribute, py_smb_list_callback, (void *)py_dirlist);
223 talloc_free(mask);
225 return py_dirlist;
230 * Create a directory
232 static PyObject *py_smb_mkdir(pytalloc_Object *self, PyObject *args)
234 NTSTATUS status;
235 const char *dirname;
236 struct smb_private_data *spdata;
238 if (!PyArg_ParseTuple(args, "s:mkdir", &dirname)) {
239 return NULL;
242 spdata = self->ptr;
243 status = smbcli_mkdir(spdata->tree, dirname);
244 PyErr_NTSTATUS_IS_ERR_RAISE(status);
246 Py_RETURN_NONE;
251 * Remove a directory
253 static PyObject *py_smb_rmdir(pytalloc_Object *self, PyObject *args)
255 NTSTATUS status;
256 const char *dirname;
257 struct smb_private_data *spdata;
259 if (!PyArg_ParseTuple(args, "s:rmdir", &dirname)) {
260 return NULL;
263 spdata = self->ptr;
264 status = smbcli_rmdir(spdata->tree, dirname);
265 PyErr_NTSTATUS_IS_ERR_RAISE(status);
267 Py_RETURN_NONE;
272 * Check existence of a path
274 static PyObject *py_smb_chkpath(pytalloc_Object *self, PyObject *args)
276 NTSTATUS status;
277 const char *path;
278 struct smb_private_data *spdata;
280 if (!PyArg_ParseTuple(args, "s:chkpath", &path)) {
281 return NULL;
284 spdata = self->ptr;
285 status = smbcli_chkpath(spdata->tree, path);
287 if (NT_STATUS_IS_OK(status)) {
288 Py_RETURN_TRUE;
291 Py_RETURN_FALSE;
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)
300 NTSTATUS status;
301 union smb_open io;
302 union smb_fileinfo fio;
303 struct smb_private_data *spdata;
304 const char *filename;
305 int fnum;
307 if (!PyArg_ParseTuple(args, "s:get_acl", &filename)) {
308 return NULL;
311 ZERO_STRUCT(io);
313 spdata = self->ptr;
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;
334 ZERO_STRUCT(fio);
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 |
339 SECINFO_GROUP |
340 SECINFO_DACL |
341 SECINFO_PROTECTED_DACL |
342 SECINFO_UNPROTECTED_DACL |
343 SECINFO_SACL |
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)
363 NTSTATUS status;
364 union smb_open io;
365 union smb_setfileinfo fio;
366 struct smb_private_data *spdata;
367 const char *filename;
368 PyObject *py_sd;
369 struct security_descriptor *sd;
370 int fnum;
372 if (!PyArg_ParseTuple(args, "sO:set_acl", &filename, &py_sd)) {
373 return NULL;
376 spdata = self->ptr;
378 sd = pytalloc_get_type(py_sd, struct security_descriptor);
379 if (!sd) {
380 PyErr_Format(PyExc_TypeError,
381 "Expected dcerpc.security.descriptor as argument, got %s",
382 talloc_get_name(pytalloc_get_ptr(py_sd)));
383 return NULL;
386 ZERO_STRUCT(io);
388 spdata = self->ptr;
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;
409 ZERO_STRUCT(fio);
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);
421 Py_RETURN_NONE;
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." },
455 { NULL },
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;
468 NTSTATUS status;
470 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "zz|OO",
471 discard_const_p(char *, kwnames),
472 &hostname, &service, &py_creds, &py_lp)) {
473 return NULL;
476 smb = (pytalloc_Object *)type->tp_alloc(type, 0);
477 if (smb == NULL) {
478 PyErr_NoMemory();
479 return NULL;
481 smb->talloc_ctx = talloc_new(NULL);
482 if (smb->talloc_ctx == NULL) {
483 PyErr_NoMemory();
484 return NULL;
487 spdata = talloc_zero(smb->talloc_ctx, struct smb_private_data);
488 if (spdata == NULL) {
489 PyErr_NoMemory();
490 Py_DECREF(smb);
491 return NULL;
494 spdata->lp_ctx = lpcfg_from_py_object(smb->talloc_ctx, py_lp);
495 if (spdata->lp_ctx == NULL) {
496 Py_DECREF(smb);
497 return 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) {
502 PyErr_NoMemory();
503 Py_DECREF(smb);
504 return 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) {
510 Py_DECREF(smb);
511 return NULL;
514 smb->ptr = spdata;
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",
529 void initsmb(void)
531 PyObject *m;
532 PyTypeObject *talloc_type = pytalloc_GetObjectType();
533 if (talloc_type == NULL) {
534 return;
537 PySMB.tp_base = talloc_type;
539 if (PyType_Ready(&PySMB) < 0) {
540 return;
543 m = Py_InitModule3("smb", NULL, "SMB File I/O support");
544 if (m == NULL) {
545 return;
548 Py_INCREF(&PySMB);
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);