Broke out unpacking of a username/password stored in a Python
authorTim Potter <tpot@samba.org>
Fri, 2 Aug 2002 05:28:54 +0000 (05:28 +0000)
committerTim Potter <tpot@samba.org>
Fri, 2 Aug 2002 05:28:54 +0000 (05:28 +0000)
dictionary into a separate function.

source/python/py_common.c
source/python/py_common_proto.h

index 890422536e00d4cf0b6c4ee3f42e9ea6704ff37f..a65206e02237f40c198271c0346d3d2ea6625546 100644 (file)
@@ -126,50 +126,94 @@ PyObject *py_setup_logging(PyObject *self, PyObject *args, PyObject *kw)
        return Py_None;
 }
 
-/* Return a cli_state to a RPC pipe on the given server.  Use the
-   credentials passed if not NULL.  If an error occurs errstr is set to a
-   string describing the error and NULL is returned.  If set, errstr must
-   be freed by calling free(). */
-
-struct cli_state *open_pipe_creds(char *server, PyObject *creds, 
-                                 char *pipe_name, char **errstr)
+/* Parse credentials from a python dictionary.  The dictionary can
+   only have the keys "username", "domain" and "password".  Return
+   True for valid credentials in which case the username, domain and
+   password are set to pointers to their values from the dicationary.
+   If returns False, the errstr is set to point at some mallocated
+   memory describing the error. */
+
+BOOL py_parse_creds(PyObject *creds, char **username, char **domain, 
+                   char **password, char **errstr)
 {
-       char *username = "", *password = "", *domain = "";
-       struct cli_state *cli;
-       NTSTATUS result;
-       
-       /* Extract credentials from the python dictionary */
+       /* Initialise anonymous credentials */
+
+       *username = "";
+       *domain = "";
+       *password = "";
 
        if (creds && PyDict_Size(creds) > 0) {
                PyObject *username_obj, *password_obj, *domain_obj;
 
-               /* Check credentials passed are valid.  This means the
-                  username, domain and password keys must exist and be
-                  string objects. */
+               /* Check for presence of required fields */
 
                username_obj = PyDict_GetItemString(creds, "username");
                domain_obj = PyDict_GetItemString(creds, "domain");
                password_obj = PyDict_GetItemString(creds, "password");
 
-               if (!username_obj || !domain_obj || !password_obj) {
-               creds_error:
-                       *errstr = strdup("invalid credentials");
-                       return NULL;
+               if (!username_obj) {
+                       *errstr = strdup("no username field in credential");
+                       return False;
+               }
+
+               if (!domain_obj) {
+                       *errstr = strdup("no domain field in credential");
+                       return False;
                }
 
-               if (!PyString_Check(username_obj) || 
-                   !PyString_Check(domain_obj) || 
-                   !PyString_Check(password_obj))
-                       goto creds_error;
+               if (!password_obj) {
+                       *errstr = strdup("no password field in credential");
+                       return False;
+               }
+
+               /* Look for any other fields */
+
+               /* Check type of required fields */
 
-               username = PyString_AsString(username_obj);
-               domain = PyString_AsString(domain_obj);
-               password = PyString_AsString(password_obj);
+               if (!PyString_Check(username_obj)) {
+                       *errstr = strdup("username field is not string type");
+                       return False;
+               }
+
+               if (!PyString_Check(domain_obj)) {
+                       *errstr = strdup("domain field is not string type");
+                       return False;
+               }
+
+               if (!PyString_Check(password_obj)) {
+                       *errstr = strdup("password field is not string type");
+                       return False;
+               }
 
-               if (!username || !domain || !password)
-                       goto creds_error;
+               /* Assign values */
+
+               *username = PyString_AsString(username_obj);
+               *domain = PyString_AsString(domain_obj);
+               *password = PyString_AsString(password_obj);
        }
 
+       *errstr = NULL;
+
+       return True;
+}
+
+/* Return a cli_state to a RPC pipe on the given server.  Use the
+   credentials passed if not NULL.  If an error occurs errstr is set to a
+   string describing the error and NULL is returned.  If set, errstr must
+   be freed by calling free(). */
+
+struct cli_state *open_pipe_creds(char *server, PyObject *creds, 
+                                 char *pipe_name, char **errstr)
+{
+       char *username, *password, *domain;
+       struct cli_state *cli;
+       NTSTATUS result;
+       
+       /* Extract credentials from the python dictionary */
+
+       if (!py_parse_creds(creds, &username, &password, &domain, errstr))
+               return NULL;
+
        /* Now try to connect */
 
        result = cli_full_connection(
index 143ea2947c3af516cbbbda5163460090b932c501..89f0f35fc936e3a8071bceccd970e855729b4106 100644 (file)
@@ -12,6 +12,8 @@ void py_samba_init(void);
 PyObject *get_debuglevel(PyObject *self, PyObject *args);
 PyObject *set_debuglevel(PyObject *self, PyObject *args);
 PyObject *py_setup_logging(PyObject *self, PyObject *args, PyObject *kw);
+BOOL py_parse_creds(PyObject *creds, char **username, char **domain, 
+                   char **password, char **errstr);
 struct cli_state *open_pipe_creds(char *server, PyObject *creds, 
                                  char *pipe_name, char **errstr);
 BOOL get_level_value(PyObject *dict, uint32 *level);