From 286d3a80fdcd316ebbf63b007a24e787143313a4 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Thu, 16 May 2002 04:00:31 +0000 Subject: More cleanups. - removed dodgy fprintf() error handling - return an error string from open_pipe_creds() so the appropriate exception can be raised by the caller - reformatting (This used to be commit d35cad359df3bd53e6bb49f6655d4c7b9993abb0) --- source3/python/py_common.c | 43 +++++++--------------- source3/python/py_common_proto.h | 4 +- source3/python/py_lsa.c | 15 +++++--- source3/python/py_samr.c | 12 ++++-- source3/python/py_spoolss_drivers.c | 50 +++++++++++++++---------- source3/python/py_spoolss_forms.c | 9 +++-- source3/python/py_spoolss_jobs.c | 16 ++++---- source3/python/py_spoolss_ports.c | 22 ++++++++--- source3/python/py_spoolss_printerdata.c | 8 ++-- source3/python/py_spoolss_printers.c | 65 ++++++++++++++++++++------------- source3/python/py_spoolss_proto.h | 2 +- 11 files changed, 137 insertions(+), 109 deletions(-) (limited to 'source3') diff --git a/source3/python/py_common.c b/source3/python/py_common.c index d286ed68f0..61eacced27 100644 --- a/source3/python/py_common.c +++ b/source3/python/py_common.c @@ -95,8 +95,8 @@ PyObject *py_setup_logging(PyObject *self, PyObject *args, PyObject *kw) char *logfilename = NULL; static char *kwlist[] = {"interactive", "logfilename", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kw, "|is", kwlist, - &interactive, &logfilename)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "|is", kwlist, &interactive, &logfilename)) return NULL; if (interactive && logfilename) { @@ -119,18 +119,19 @@ PyObject *py_setup_logging(PyObject *self, PyObject *args, PyObject *kw) } /* Return a cli_state to a RPC pipe on the given server. Use the - credentials passed if not NULL. Set an exception and return NULL if - there was an error creating the connection. */ + 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 *system_name, PyObject *creds, - cli_pipe_fn *connect_fn) +struct cli_state *open_pipe_creds(char *server, PyObject *creds, + cli_pipe_fn *connect_fn, char **errstr) { struct ntuser_creds nt_creds; struct cli_state *cli; cli = (struct cli_state *)malloc(sizeof(struct cli_state)); if (!cli) { - PyErr_SetString(PyExc_RuntimeError, "out of memory"); + *errstr = strdup("out of memory"); return NULL; } @@ -156,13 +157,7 @@ struct cli_state *open_pipe_creds(char *system_name, PyObject *creds, if (!username_obj || !domain_obj || !password_obj) { creds_error: - - /* TODO: Either pass in the exception for the - module calling open_pipe_creds() or have a - global samba python module exception. */ - - PyErr_SetString(PyExc_RuntimeError, - "invalid credentials"); + *errstr = strdup("invalid credentials"); return NULL; } @@ -193,24 +188,12 @@ struct cli_state *open_pipe_creds(char *system_name, PyObject *creds, /* Now try to connect */ - if (!connect_fn(cli, system_name, &nt_creds)) { - if (cli) { - NTSTATUS error = cli_nt_error(cli); - - /* Raise an exception if something went wrong. - FIXME: This should be a more appropriate - exception than PyExc_RuntimeError */ - - if (!NT_STATUS_IS_OK(error)) - PyErr_SetObject(PyExc_RuntimeError, - py_ntstatus_tuple(error)); - else - PyErr_SetString(PyExc_RuntimeError, - "error connecting to pipe"); - } - + if (!connect_fn(cli, server, &nt_creds)) { + *errstr = strdup("error connecting to RPC pipe"); return NULL; } + *errstr = NULL; + return cli; } diff --git a/source3/python/py_common_proto.h b/source3/python/py_common_proto.h index 98d970d7bc..dd823dd4c0 100644 --- a/source3/python/py_common_proto.h +++ b/source3/python/py_common_proto.h @@ -12,8 +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); -struct cli_state *open_pipe_creds(char *system_name, PyObject *creds, - cli_pipe_fn *connect_fn); +struct cli_state *open_pipe_creds(char *server, PyObject *creds, + cli_pipe_fn *connect_fn, char **errstr); /* The following definitions come from python/py_ntsec.c */ diff --git a/source3/python/py_lsa.c b/source3/python/py_lsa.c index cf96928790..82055181cc 100644 --- a/source3/python/py_lsa.c +++ b/source3/python/py_lsa.c @@ -53,7 +53,7 @@ static PyObject *lsa_open_policy(PyObject *self, PyObject *args, PyObject *kw) { static char *kwlist[] = { "servername", "creds", "access", NULL }; - char *server_name; + char *server, *errstr; PyObject *creds = NULL, *result; uint32 desired_access = MAXIMUM_ALLOWED_ACCESS; struct cli_state *cli; @@ -62,17 +62,20 @@ static PyObject *lsa_open_policy(PyObject *self, PyObject *args, POLICY_HND hnd; if (!PyArg_ParseTupleAndKeywords( - args, kw, "s|O!i", kwlist, &server_name, &PyDict_Type, - &creds, &desired_access)) + args, kw, "s|O!i", kwlist, &server, &PyDict_Type, + &creds, &desired_access)) return NULL; - if (!(cli = open_pipe_creds(server_name, creds, cli_lsa_initialise))) { - fprintf(stderr, "could not initialise cli state\n"); + if (!(cli = open_pipe_creds( + server, creds, cli_lsa_initialise, &errstr))) { + PyErr_SetString(lsa_error, errstr); + free(errstr); return NULL; } if (!(mem_ctx = talloc_init())) { - fprintf(stderr, "unable to initialise talloc context\n"); + PyErr_SetString( + lsa_error, "unable to initialise talloc context\n"); return NULL; } diff --git a/source3/python/py_samr.c b/source3/python/py_samr.c index 66d6266d10..d0573171f3 100644 --- a/source3/python/py_samr.c +++ b/source3/python/py_samr.c @@ -275,7 +275,7 @@ static PyObject *samr_connect(PyObject *self, PyObject *args, PyObject *kw) { static char *kwlist[] = { "server", "creds", "access", NULL }; uint32 desired_access = MAXIMUM_ALLOWED_ACCESS; - char *server_name; + char *server, *errstr; struct cli_state *cli; POLICY_HND hnd; TALLOC_CTX *mem_ctx; @@ -283,12 +283,16 @@ static PyObject *samr_connect(PyObject *self, PyObject *args, PyObject *kw) NTSTATUS ntstatus; if (!PyArg_ParseTupleAndKeywords( - args, kw, "s|O!i", kwlist, &server_name, &PyDict_Type, + args, kw, "s|O!i", kwlist, &server, &PyDict_Type, &creds, &desired_access)) return NULL; - if (!(cli = open_pipe_creds(server_name, creds, cli_samr_initialise))) - goto done; + if (!(cli = open_pipe_creds( + server, creds, cli_lsa_initialise, &errstr))) { + PyErr_SetString(samr_error, errstr); + free(errstr); + return NULL; + } if (!(mem_ctx = talloc_init())) { PyErr_SetString(samr_ntstatus, diff --git a/source3/python/py_spoolss_drivers.c b/source3/python/py_spoolss_drivers.c index 416d46f1d2..64d3a37afa 100644 --- a/source3/python/py_spoolss_drivers.c +++ b/source3/python/py_spoolss_drivers.c @@ -30,28 +30,30 @@ PyObject *spoolss_enumprinterdrivers(PyObject *self, PyObject *args, PRINTER_DRIVER_CTR ctr; int level = 1, i; uint32 needed, num_drivers; - char *arch = "Windows NT x86", *server_name; + char *arch = "Windows NT x86", *server, *errstr; static char *kwlist[] = {"server", "creds", "level", "arch", NULL}; struct cli_state *cli = NULL; TALLOC_CTX *mem_ctx = NULL; /* Parse parameters */ - if (!PyArg_ParseTupleAndKeywords(args, kw, "s|O!is", kwlist, - &server_name, &PyDict_Type, &creds, - &level, &arch)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "s|O!is", kwlist, &server, &PyDict_Type, + &creds, &level, &arch)) return NULL; /* Call rpc function */ if (!(cli = open_pipe_creds( - server_name, creds, cli_spoolss_initialise))) { - fprintf(stderr, "could not initialise cli state\n"); + server, creds, cli_spoolss_initialise, &errstr))) { + PyErr_SetString(spoolss_error, errstr); + free(errstr); goto done; } if (!(mem_ctx = talloc_init())) { - fprintf(stderr, "unable to initialise talloc context\n"); + PyErr_SetString( + spoolss_error, "unable to initialise talloc context\n"); goto done; } @@ -181,8 +183,8 @@ PyObject *spoolss_hnd_getprinterdriver(PyObject *self, PyObject *args, /* Parse parameters */ - if (!PyArg_ParseTupleAndKeywords(args, kw, "|is", kwlist, - &level, &arch)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "|is", kwlist, &level, &arch)) return NULL; /* Call rpc function */ @@ -230,28 +232,30 @@ PyObject *spoolss_getprinterdriverdir(PyObject *self, PyObject *args, PyObject *result = Py_None, *creds = NULL; DRIVER_DIRECTORY_CTR ctr; uint32 needed, level; - char *arch = "Windows NT x86", *server_name; + char *arch = "Windows NT x86", *server, *errstr; static char *kwlist[] = {"server", "level", "arch", "creds", NULL}; struct cli_state *cli = NULL; TALLOC_CTX *mem_ctx = NULL; /* Parse parameters */ - if (!PyArg_ParseTupleAndKeywords(args, kw, "s|isO!", kwlist, - &server_name, &level, &arch, - &PyDict_Type, &creds)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "s|isO!", kwlist, &server, &level, + &arch, &PyDict_Type, &creds)) return NULL; /* Call rpc function */ if (!(cli = open_pipe_creds( - server_name, creds, cli_spoolss_initialise))) { - fprintf(stderr, "could not initialise cli state\n"); + server, creds, cli_spoolss_initialise, &errstr))) { + PyErr_SetString(spoolss_error, errstr); + free(errstr); goto done; } if (!(mem_ctx = talloc_init())) { - fprintf(stderr, "unable to initialise talloc context\n"); + PyErr_SetString( + spoolss_error, "unable to initialise talloc context\n"); goto done; } @@ -290,7 +294,7 @@ PyObject *spoolss_addprinterdriver(PyObject *self, PyObject *args, PyObject *kw) { static char *kwlist[] = { "server", "info", "creds", NULL }; - char *server; + char *server, *errstr; uint32 level; PyObject *info, *result = NULL, *creds = NULL, *level_obj; WERROR werror; @@ -309,10 +313,18 @@ PyObject *spoolss_addprinterdriver(PyObject *self, PyObject *args, if (server[0] == '\\' && server[1] == '\\') server += 2; - mem_ctx = talloc_init(); + if (!(mem_ctx = talloc_init())) { + PyErr_SetString( + spoolss_error, "unable to initialise talloc context\n"); + return NULL; + } - if (!(cli = open_pipe_creds(server, creds, cli_spoolss_initialise))) + if (!(cli = open_pipe_creds( + server, creds, cli_spoolss_initialise, &errstr))) { + PyErr_SetString(spoolss_error, errstr); + free(errstr); goto done; + } if ((level_obj = PyDict_GetItemString(info, "level"))) { diff --git a/source3/python/py_spoolss_forms.c b/source3/python/py_spoolss_forms.c index 83cdf9badd..762b13f73b 100644 --- a/source3/python/py_spoolss_forms.c +++ b/source3/python/py_spoolss_forms.c @@ -84,8 +84,8 @@ PyObject *spoolss_hnd_getform(PyObject *self, PyObject *args, PyObject *kw) /* Parse parameters */ - if (!PyArg_ParseTupleAndKeywords(args, kw, "s|i", kwlist, - &form_name, &level)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "s|i", kwlist, &form_name, &level)) return NULL; /* Call rpc function */ @@ -129,8 +129,9 @@ PyObject *spoolss_hnd_setform(PyObject *self, PyObject *args, PyObject *kw) /* Parse parameters */ - if (!PyArg_ParseTupleAndKeywords(args, kw, "O!|i", kwlist, - &PyDict_Type, &py_form, &level)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "O!|i", kwlist, &PyDict_Type, &py_form, + &level)) return NULL; /* Call rpc function */ diff --git a/source3/python/py_spoolss_jobs.c b/source3/python/py_spoolss_jobs.c index d98fdba137..29475f9e3b 100644 --- a/source3/python/py_spoolss_jobs.c +++ b/source3/python/py_spoolss_jobs.c @@ -96,8 +96,8 @@ PyObject *spoolss_hnd_setjob(PyObject *self, PyObject *args, PyObject *kw) /* Parse parameters */ - if (!PyArg_ParseTupleAndKeywords(args, kw, "ii|i", kwlist, &jobid, - &command, &level)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "ii|i", kwlist, &jobid, &command, &level)) return NULL; /* Call rpc function */ @@ -127,8 +127,8 @@ PyObject *spoolss_hnd_getjob(PyObject *self, PyObject *args, PyObject *kw) /* Parse parameters */ - if (!PyArg_ParseTupleAndKeywords(args, kw, "i|i", kwlist, &jobid, - &level)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "i|i", kwlist, &jobid, &level)) return NULL; /* Call rpc function */ @@ -228,8 +228,8 @@ PyObject *spoolss_hnd_startdocprinter(PyObject *self, PyObject *args, PyObject * /* Parse parameters */ - if (!PyArg_ParseTupleAndKeywords(args, kw, "O!", kwlist, - &PyDict_Type, &doc_info)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "O!", kwlist, &PyDict_Type, &doc_info)) return NULL; /* Check document_info parameter */ @@ -362,8 +362,8 @@ PyObject *spoolss_hnd_writeprinter(PyObject *self, PyObject *args, PyObject *kw) /* Parse parameters */ - if (!PyArg_ParseTupleAndKeywords(args, kw, "O!", kwlist, - &PyString_Type, &data)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "O!", kwlist, &PyString_Type, &data)) return NULL; /* Call rpc function */ diff --git a/source3/python/py_spoolss_ports.c b/source3/python/py_spoolss_ports.c index 8e74017f4a..fb3f47c746 100644 --- a/source3/python/py_spoolss_ports.c +++ b/source3/python/py_spoolss_ports.c @@ -31,21 +31,31 @@ PyObject *spoolss_enumports(PyObject *self, PyObject *args, PyObject *kw) static char *kwlist[] = {"server", "level", "creds", NULL}; TALLOC_CTX *mem_ctx = NULL; struct cli_state *cli = NULL; - char *server; + char *server, *errstr; PORT_INFO_CTR ctr; /* Parse parameters */ - if (!PyArg_ParseTupleAndKeywords(args, kw, "s|iO!", kwlist, - &server, &creds, &level, - &PyDict_Type)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "s|iO!", kwlist, &server, &creds, &level, + &PyDict_Type)) return NULL; if (server[0] == '\\' && server[1] == '\\') server += 2; - mem_ctx = talloc_init(); - cli = open_pipe_creds(server, creds, cli_spoolss_initialise); + if (!(cli = open_pipe_creds( + server, creds, cli_spoolss_initialise, &errstr))) { + PyErr_SetString(spoolss_error, errstr); + free(errstr); + return NULL; + } + + if (!(mem_ctx = talloc_init())) { + PyErr_SetString( + spoolss_error, "unable to initialise talloc context\n"); + return NULL; + } /* Call rpc function */ diff --git a/source3/python/py_spoolss_printerdata.c b/source3/python/py_spoolss_printerdata.c index 24e2340059..e1e43fa736 100644 --- a/source3/python/py_spoolss_printerdata.c +++ b/source3/python/py_spoolss_printerdata.c @@ -100,7 +100,7 @@ PyObject *spoolss_hnd_getprinterdata(PyObject *self, PyObject *args, PyObject *k /* Parse parameters */ if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &value)) - return NULL; + return NULL; /* Call rpc function */ @@ -132,8 +132,8 @@ PyObject *spoolss_hnd_setprinterdata(PyObject *self, PyObject *args, PyObject *k uint32 data_size, data_type; WERROR werror; - if (!PyArg_ParseTupleAndKeywords(args, kw, "O!", kwlist, - &PyDict_Type, &py_data)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "O!", kwlist, &PyDict_Type, &py_data)) return NULL; if (!py_to_printerdata(&value, &data_type, &data, &data_size, py_data)) @@ -209,7 +209,7 @@ PyObject *spoolss_hnd_deleteprinterdata(PyObject *self, PyObject *args, PyObject /* Parse parameters */ if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &value)) - return NULL; + return NULL; /* Call rpc function */ diff --git a/source3/python/py_spoolss_printers.c b/source3/python/py_spoolss_printers.c index 36ba22a7c4..7ee94a48f7 100644 --- a/source3/python/py_spoolss_printers.c +++ b/source3/python/py_spoolss_printers.c @@ -24,7 +24,7 @@ PyObject *spoolss_openprinter(PyObject *self, PyObject *args, PyObject *kw) { - char *full_name, *computer_name = NULL; + char *unc_name, *server = NULL, *errstr; TALLOC_CTX *mem_ctx; POLICY_HND hnd; WERROR werror; @@ -34,25 +34,26 @@ PyObject *spoolss_openprinter(PyObject *self, PyObject *args, PyObject *kw) struct cli_state *cli; if (!PyArg_ParseTupleAndKeywords( - args, kw, "s|O!i", kwlist, &full_name, &PyDict_Type, &creds, - &desired_access)) { - + args, kw, "s|O!i", kwlist, &unc_name, &PyDict_Type, &creds, + &desired_access)) goto done; - } /* FIXME: Return name format exception for names without a UNC prefix */ - computer_name = strdup(full_name + 2); + server = strdup(unc_name + 2); - if (strchr(computer_name, '\\')) { - char *c = strchr(computer_name, '\\'); + if (strchr(server, '\\')) { + char *c = strchr(server, '\\'); *c = 0; } if (!(cli = open_pipe_creds( - computer_name, creds, cli_spoolss_initialise))) + server, creds, cli_spoolss_initialise, &errstr))) { + PyErr_SetString(spoolss_error, errstr); + free(errstr); goto done; + } if (!(mem_ctx = talloc_init())) { PyErr_SetString(spoolss_error, @@ -61,7 +62,7 @@ PyObject *spoolss_openprinter(PyObject *self, PyObject *args, PyObject *kw) } werror = cli_spoolss_open_printer_ex( - cli, mem_ctx, full_name, "", desired_access, computer_name, + cli, mem_ctx, unc_name, "", desired_access, server, "", &hnd); if (!W_ERROR_IS_OK(werror)) { @@ -74,7 +75,7 @@ PyObject *spoolss_openprinter(PyObject *self, PyObject *args, PyObject *kw) result = new_spoolss_policy_hnd_object(cli, mem_ctx, &hnd); done: - SAFE_FREE(computer_name); + SAFE_FREE(server); return result; } @@ -187,8 +188,8 @@ PyObject *spoolss_hnd_setprinter(PyObject *self, PyObject *args, PyObject *kw) /* Parse parameters */ - if (!PyArg_ParseTupleAndKeywords(args, kw, "O!", kwlist, - &PyDict_Type, &info)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "O!", kwlist, &PyDict_Type, &info)) return NULL; /* Check dictionary contains a level */ @@ -277,20 +278,30 @@ PyObject *spoolss_enumprinters(PyObject *self, PyObject *args, PyObject *kw) "creds", NULL}; TALLOC_CTX *mem_ctx; struct cli_state *cli; - char *server, *name = NULL; + char *server, *errstr; /* Parse parameters */ - if (!PyArg_ParseTupleAndKeywords(args, kw, "s|siiO!", kwlist, - &server, &name, &level, &flags, - &PyDict_Type, &creds)) + if (!PyArg_ParseTupleAndKeywords( + args, kw, "s|iiO!", kwlist, &server, &level, &flags, + &PyDict_Type, &creds)) return NULL; if (server[0] == '\\' && server[1] == '\\') server += 2; - mem_ctx = talloc_init(); - cli = open_pipe_creds(server, creds, cli_spoolss_initialise); + if (!(cli = open_pipe_creds( + server, creds, cli_spoolss_initialise, &errstr))) { + PyErr_SetString(spoolss_error, errstr); + free(errstr); + return NULL; + } + + if (!(mem_ctx = talloc_init())) { + PyErr_SetString( + spoolss_error, "unable to initialise talloc context\n"); + return NULL; + } /* Call rpc function */ @@ -369,7 +380,7 @@ PyObject *spoolss_addprinterex(PyObject *self, PyObject *args, PyObject *kw) { static char *kwlist[] = { "server", "printername", "info", "creds", NULL}; - char *printername, *server; + char *printername, *server, *errstr; PyObject *info, *result = NULL, *creds = NULL; struct cli_state *cli = NULL; TALLOC_CTX *mem_ctx = NULL; @@ -383,13 +394,17 @@ PyObject *spoolss_addprinterex(PyObject *self, PyObject *args, PyObject *kw) return NULL; if (!(cli = open_pipe_creds( - server, creds, cli_spoolss_initialise))) + server, creds, cli_spoolss_initialise, &errstr))) { + PyErr_SetString(spoolss_error, errstr); + free(errstr); goto done; + } - mem_ctx = talloc_init(); - - if (!(cli = open_pipe_creds(server, creds, cli_spoolss_initialise))) - goto done; + if (!(mem_ctx = talloc_init())) { + PyErr_SetString( + spoolss_error, "unable to initialise talloc context\n"); + return NULL; + } if (!py_to_PRINTER_INFO_2(&info2, info, mem_ctx)) { PyErr_SetString(spoolss_error, diff --git a/source3/python/py_spoolss_proto.h b/source3/python/py_spoolss_proto.h index b197003c79..5b68ef815a 100644 --- a/source3/python/py_spoolss_proto.h +++ b/source3/python/py_spoolss_proto.h @@ -21,7 +21,7 @@ PyObject *spoolss_getprinterdriverdir(PyObject *self, PyObject *args, PyObject *spoolss_addprinterdriver(PyObject *self, PyObject *args, PyObject *kw); PyObject *spoolss_addprinterdriverex(PyObject *self, PyObject *args, - PyObject *kw); + PyObject *kw); PyObject *spoolss_deleteprinterdriver(PyObject *self, PyObject *args, PyObject *kw); PyObject *spoolss_deleteprinterdriverex(PyObject *self, PyObject *args, -- cgit