r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[sfrench/samba-autobuild/.git] / source3 / python / py_smb.c
index bb84a337c93214f84add2cd96f2cbaa162047196..17b2a2d5aa13782b35f07e517089f7dea0a5f798 100644 (file)
@@ -5,7 +5,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "python/py_smb.h"
@@ -43,12 +42,12 @@ static PyObject *py_smb_connect(PyObject *self, PyObject *args, PyObject *kw)
        if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &server))
                return NULL;
 
-       if (!(cli = cli_initialise(NULL)))
+       if (!(cli = cli_initialise()))
                return NULL;
 
        ZERO_STRUCT(ip);
 
-       if (!cli_connect(cli, server, &ip))
+       if (!NT_STATUS_IS_OK(cli_connect(cli, server, &ip)))
                return NULL;
 
        return new_cli_state_object(cli);
@@ -99,7 +98,7 @@ static PyObject *py_smb_session_setup(PyObject *self, PyObject *args,
        static char *kwlist[] = { "creds", NULL };
        PyObject *creds;
        char *username, *domain, *password, *errstr;
-       BOOL result;
+       NTSTATUS result;
 
        if (!PyArg_ParseTupleAndKeywords(args, kw, "|O", kwlist, &creds))
                return NULL;
@@ -118,7 +117,7 @@ static PyObject *py_smb_session_setup(PyObject *self, PyObject *args,
                return NULL;
        }
 
-       return Py_BuildValue("i", result);
+       return Py_BuildValue("i", NT_STATUS_IS_OK(result));
 }
 
 static PyObject *py_smb_tconx(PyObject *self, PyObject *args, PyObject *kw)
@@ -149,17 +148,18 @@ static PyObject *py_smb_nt_create_andx(PyObject *self, PyObject *args,
        cli_state_object *cli = (cli_state_object *)self;
        static char *kwlist[] = { "filename", "desired_access", 
                                  "file_attributes", "share_access",
-                                 "create_disposition", NULL };
+                                 "create_disposition", "create_options",
+                                 NULL };
        char *filename;
        uint32 desired_access, file_attributes = 0, 
                share_access = FILE_SHARE_READ | FILE_SHARE_WRITE,
-               create_disposition = FILE_EXISTS_OPEN, create_options = 0;
+               create_disposition = OPENX_FILE_EXISTS_OPEN, create_options = 0;
        int result;
 
        /* Parse parameters */
 
        if (!PyArg_ParseTupleAndKeywords(
-                   args, kw, "si|iii", kwlist, &filename, &desired_access,
+                   args, kw, "si|iiii", kwlist, &filename, &desired_access,
                    &file_attributes, &share_access, &create_disposition,
                    &create_options))
                return NULL;
@@ -178,6 +178,83 @@ static PyObject *py_smb_nt_create_andx(PyObject *self, PyObject *args,
        return PyInt_FromLong(result);
 }
 
+static PyObject *py_smb_open(PyObject *self, PyObject *args, PyObject *kw)
+{
+       cli_state_object *cli = (cli_state_object *)self;
+       static char *kwlist[] = { "filename", "flags", 
+                                 "share_mode", NULL };
+       char *filename;
+       uint32 flags, share_mode = DENY_NONE;
+       int result;
+
+       /* Parse parameters */
+
+       if (!PyArg_ParseTupleAndKeywords(
+                   args, kw, "si|i", kwlist, &filename, &flags, &share_mode))
+               return NULL;
+
+       result = cli_open(cli->cli, filename, flags, share_mode);
+
+       if (cli_is_error(cli->cli)) {
+               PyErr_SetString(PyExc_RuntimeError, "open failed");
+               return NULL;
+       }
+
+       /* Return FID */
+
+       return PyInt_FromLong(result);
+}
+
+static PyObject *py_smb_read(PyObject *self, PyObject *args, PyObject *kw)
+{
+       cli_state_object *cli = (cli_state_object *)self;
+       static char *kwlist[] = { "fnum", "offset", "size", NULL };
+       int fnum, offset=0, size=0;
+       ssize_t result;
+       SMB_OFF_T fsize;
+       char *data;
+       PyObject *ret;
+
+       /* Parse parameters */
+
+       if (!PyArg_ParseTupleAndKeywords(
+                   args, kw, "i|ii", kwlist, &fnum, &offset, &size))
+               return NULL;
+
+       if (!cli_qfileinfo(cli->cli, fnum, NULL, &fsize, NULL, NULL,
+                   NULL, NULL, NULL) &&
+           !cli_getattrE(cli->cli, fnum, NULL, &fsize, NULL, NULL, NULL)) {
+               PyErr_SetString(PyExc_RuntimeError, "getattrib failed");
+               return NULL;
+       }
+
+       if (offset < 0)
+               offset = 0;
+
+       if (size < 1 || size > fsize - offset)
+               size = fsize - offset;
+
+       if (!(data = SMB_XMALLOC_ARRAY(char, size))) {
+               PyErr_SetString(PyExc_RuntimeError, "malloc failed");
+               return NULL;
+       }
+
+       result = cli_read(cli->cli, fnum, data, (off_t) offset, (size_t) size);
+
+       if (result==-1 || cli_is_error(cli->cli)) {
+               SAFE_FREE(data);
+               PyErr_SetString(PyExc_RuntimeError, "read failed");
+               return NULL;
+       }
+
+       /* Return a python string */
+
+       ret = Py_BuildValue("s#", data, result);
+       SAFE_FREE(data);
+
+       return ret;
+}
+
 static PyObject *py_smb_close(PyObject *self, PyObject *args,
                              PyObject *kw)
 {
@@ -221,10 +298,10 @@ static PyObject *py_smb_query_secdesc(PyObject *self, PyObject *args,
 {
        cli_state_object *cli = (cli_state_object *)self;
        static char *kwlist[] = { "fnum", NULL };
-       PyObject *result;
+       PyObject *result = NULL;
        SEC_DESC *secdesc = NULL;
        int fnum;
-       TALLOC_CTX *mem_ctx;
+       TALLOC_CTX *mem_ctx = NULL;
 
        /* Parse parameters */
 
@@ -238,7 +315,6 @@ static PyObject *py_smb_query_secdesc(PyObject *self, PyObject *args,
 
        if (cli_is_error(cli->cli)) {
                PyErr_SetString(PyExc_RuntimeError, "query_secdesc failed");
-               result = NULL;
                goto done;
        }
 
@@ -252,7 +328,6 @@ static PyObject *py_smb_query_secdesc(PyObject *self, PyObject *args,
                PyErr_SetString(
                        PyExc_TypeError,
                        "Invalid security descriptor returned");
-               result = NULL;
                goto done;
        }
 
@@ -268,11 +343,12 @@ static PyObject *py_smb_set_secdesc(PyObject *self, PyObject *args,
 {
        cli_state_object *cli = (cli_state_object *)self;
        static char *kwlist[] = { "fnum", "security_descriptor", NULL };
+       PyObject *result = NULL;
        PyObject *py_secdesc;
        SEC_DESC *secdesc;
-       TALLOC_CTX *mem_ctx = talloc_init("py_smb_set_secdesc");
+       TALLOC_CTX *mem_ctx = NULL;
        int fnum;
-       BOOL result;
+       BOOL err;
 
        /* Parse parameters */
 
@@ -280,20 +356,26 @@ static PyObject *py_smb_set_secdesc(PyObject *self, PyObject *args,
                    args, kw, "iO", kwlist, &fnum, &py_secdesc))
                return NULL;
 
+       mem_ctx = talloc_init("py_smb_set_secdesc");
+
        if (!py_to_SECDESC(&secdesc, py_secdesc, mem_ctx)) {
                PyErr_SetString(PyExc_TypeError, 
                                "Invalid security descriptor");
-               return NULL;
+               goto done;
        }
 
-       result = cli_set_secdesc(cli->cli, fnum, secdesc);
+       err = cli_set_secdesc(cli->cli, fnum, secdesc);
 
        if (cli_is_error(cli->cli)) {
                PyErr_SetString(PyExc_RuntimeError, "set_secdesc failed");
-               return NULL;
+               goto done;
        }
 
-       return PyInt_FromLong(result);
+       result =  PyInt_FromLong(err);
+ done:
+       talloc_destroy(mem_ctx);
+
+       return result;
 }
 
 static PyMethodDef smb_hnd_methods[] = {
@@ -317,6 +399,33 @@ static PyMethodDef smb_hnd_methods[] = {
        { "nt_create_andx", (PyCFunction)py_smb_nt_create_andx,
          METH_VARARGS | METH_KEYWORDS, "NT Create&X" },
 
+       { "open", (PyCFunction)py_smb_open,
+         METH_VARARGS | METH_KEYWORDS,
+         "Open a file\n"
+"\n"
+"This function returns a fnum handle to an open file.  The file is\n"
+"opened with flags and optional share mode.  If unspecified, the\n"
+"default share mode is DENY_NONE\n"
+"\n"
+"Example:\n"
+"\n"
+">>> fnum=conn.open(filename, os.O_RDONLY)" },
+
+       { "read", (PyCFunction)py_smb_read,
+         METH_VARARGS | METH_KEYWORDS,
+         "Read from an open file\n"
+"\n"
+"This function returns a string read from an open file starting at\n"
+"offset for size bytes (until EOF is reached).  If unspecified, the\n"
+"default offset is 0, and default size is the remainder of the file.\n"
+"\n"
+"Example:\n"
+"\n"
+">>> conn.read(fnum)           # read entire file\n"
+">>> conn.read(fnum,5)         # read entire file from offset 5\n"
+">>> conn.read(fnum,size=64)   # read 64 bytes from start of file\n"
+">>> conn.read(fnum,4096,1024) # read 1024 bytes from offset 4096\n" },
+
        { "close", (PyCFunction)py_smb_close,
          METH_VARARGS | METH_KEYWORDS, "Close" },