python/py_common.c

説明を見る。
00001 /* 
00002    Python wrappers for DCERPC/SMB client routines.
00003 
00004    Copyright (C) Tim Potter, 2002
00005    
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 
00021 #include "python/py_common.h"
00022 
00023 /* Return a tuple of (error code, error string) from a WERROR */
00024 
00025 PyObject *py_werror_tuple(WERROR werror)
00026 {
00027         return Py_BuildValue("[is]", W_ERROR_V(werror), 
00028                              dos_errstr(werror));
00029 }
00030 
00031 /* Return a tuple of (error code, error string) from a WERROR */
00032 
00033 PyObject *py_ntstatus_tuple(NTSTATUS ntstatus)
00034 {
00035         return Py_BuildValue("[is]", NT_STATUS_V(ntstatus), 
00036                              nt_errstr(ntstatus));
00037 }
00038 
00039 /* Initialise samba client routines */
00040 
00041 static BOOL initialised;
00042 
00043 void py_samba_init(void)
00044 {
00045         if (initialised)
00046                 return;
00047 
00048         load_case_tables();
00049 
00050         /* Load configuration file */
00051 
00052         if (!lp_load(dyn_CONFIGFILE, True, False, False, True))
00053                 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
00054 
00055         /* Misc other stuff */
00056 
00057         load_interfaces();
00058         init_names();
00059 
00060         initialised = True;
00061 }
00062 
00063 /* Debuglevel routines */
00064 
00065 PyObject *get_debuglevel(PyObject *self, PyObject *args)
00066 {
00067         PyObject *debuglevel;
00068 
00069         if (!PyArg_ParseTuple(args, ""))
00070                 return NULL;
00071 
00072         debuglevel = PyInt_FromLong(DEBUGLEVEL);
00073 
00074         return debuglevel;
00075 }
00076 
00077 PyObject *set_debuglevel(PyObject *self, PyObject *args)
00078 {
00079         int debuglevel;
00080 
00081         if (!PyArg_ParseTuple(args, "i", &debuglevel))
00082                 return NULL;
00083 
00084         DEBUGLEVEL = debuglevel;
00085 
00086         Py_INCREF(Py_None);
00087         return Py_None;
00088 }
00089 
00090 /* Initialise logging */
00091 
00092 PyObject *py_setup_logging(PyObject *self, PyObject *args, PyObject *kw)
00093 {
00094         BOOL interactive = False;
00095         char *logfilename = NULL;
00096         static char *kwlist[] = {"interactive", "logfilename", NULL};
00097 
00098         if (!PyArg_ParseTupleAndKeywords(
00099                     args, kw, "|is", kwlist, &interactive, &logfilename))
00100                 return NULL;
00101         
00102         if (interactive && logfilename) {
00103                 PyErr_SetString(PyExc_RuntimeError,
00104                                 "can't be interactive and set log file name");
00105                 return NULL;
00106         }
00107 
00108         if (interactive)
00109                 setup_logging("spoolss", True);
00110 
00111         if (logfilename) {
00112                 lp_set_logfile(logfilename);
00113                 setup_logging(logfilename, False);
00114                 reopen_logs();
00115         }
00116 
00117         Py_INCREF(Py_None);
00118         return Py_None;
00119 }
00120 
00121 /* Parse credentials from a python dictionary.  The dictionary can
00122    only have the keys "username", "domain" and "password".  Return
00123    True for valid credentials in which case the username, domain and
00124    password are set to pointers to their values from the dicationary.
00125    If returns False, the errstr is set to point at some mallocated
00126    memory describing the error. */
00127 
00128 BOOL py_parse_creds(PyObject *creds, char **username, char **domain, 
00129                     char **password, char **errstr)
00130 {
00131         /* Initialise anonymous credentials */
00132 
00133         *username = "";
00134         *domain = "";
00135         *password = "";
00136 
00137         if (creds && PyDict_Size(creds) > 0) {
00138                 PyObject *username_obj, *password_obj, *domain_obj;
00139                 PyObject *key, *value;
00140                 int i;
00141 
00142                 /* Check for presence of required fields */
00143 
00144                 username_obj = PyDict_GetItemString(creds, "username");
00145                 domain_obj = PyDict_GetItemString(creds, "domain");
00146                 password_obj = PyDict_GetItemString(creds, "password");
00147 
00148                 if (!username_obj) {
00149                         *errstr = SMB_STRDUP("no username field in credential");
00150                         return False;
00151                 }
00152 
00153                 if (!domain_obj) {
00154                         *errstr = SMB_STRDUP("no domain field in credential");
00155                         return False;
00156                 }
00157 
00158                 if (!password_obj) {
00159                         *errstr = SMB_STRDUP("no password field in credential");
00160                         return False;
00161                 }
00162 
00163                 /* Check type of required fields */
00164 
00165                 if (!PyString_Check(username_obj)) {
00166                         *errstr = SMB_STRDUP("username field is not string type");
00167                         return False;
00168                 }
00169 
00170                 if (!PyString_Check(domain_obj)) {
00171                         *errstr = SMB_STRDUP("domain field is not string type");
00172                         return False;
00173                 }
00174 
00175                 if (!PyString_Check(password_obj)) {
00176                         *errstr = SMB_STRDUP("password field is not string type");
00177                         return False;
00178                 }
00179 
00180                 /* Look for any extra fields */
00181 
00182                 i = 0;
00183 
00184                 while (PyDict_Next(creds, &i, &key, &value)) {
00185                         if (strcmp(PyString_AsString(key), "domain") != 0 &&
00186                             strcmp(PyString_AsString(key), "username") != 0 &&
00187                             strcmp(PyString_AsString(key), "password") != 0) {
00188                                 asprintf(errstr,
00189                                          "creds contain extra field '%s'",
00190                                          PyString_AsString(key));
00191                                 return False;
00192                         }
00193                 }
00194 
00195                 /* Assign values */
00196 
00197                 *username = PyString_AsString(username_obj);
00198                 *domain = PyString_AsString(domain_obj);
00199                 *password = PyString_AsString(password_obj);
00200         }
00201 
00202         *errstr = NULL;
00203 
00204         return True;
00205 }
00206 
00207 /* Return a cli_state to a RPC pipe on the given server.  Use the
00208    credentials passed if not NULL.  If an error occurs errstr is set to a
00209    string describing the error and NULL is returned.  If set, errstr must
00210    be freed by calling free(). */
00211 
00212 struct cli_state *open_pipe_creds(char *server, PyObject *creds, 
00213                                   int pipe_idx, char **errstr)
00214 {
00215         char *username, *password, *domain;
00216         struct cli_state *cli;
00217         struct rpc_pipe_client *pipe_hnd;
00218         NTSTATUS result;
00219         
00220         /* Extract credentials from the python dictionary */
00221 
00222         if (!py_parse_creds(creds, &username, &domain, &password, errstr))
00223                 return NULL;
00224 
00225         /* Now try to connect */
00226 
00227         result = cli_full_connection(
00228                 &cli, NULL, server, NULL, 0, "IPC$", "IPC",
00229                 username, domain, password, 0, Undefined, NULL);
00230         
00231         if (!NT_STATUS_IS_OK(result)) {
00232                 *errstr = SMB_STRDUP("error connecting to IPC$ pipe");
00233                 return NULL;
00234         }
00235 
00236         pipe_hnd = cli_rpc_pipe_open_noauth(cli, pipe_idx, &result);
00237         if (!pipe_hnd) {
00238                 cli_shutdown(cli);
00239                 asprintf(errstr, "error opening pipe index %d", pipe_idx);
00240                 return NULL;
00241         }
00242 
00243         *errstr = NULL;
00244 
00245         return cli;
00246 }
00247 
00248 /* Return true if a dictionary contains a "level" key with an integer
00249    value.  Set the value if so. */
00250 
00251 BOOL get_level_value(PyObject *dict, uint32 *level)
00252 {
00253         PyObject *obj;
00254 
00255         if (!(obj = PyDict_GetItemString(dict, "level")) ||
00256             !PyInt_Check(obj))
00257                 return False;
00258 
00259         if (level)
00260                 *level = PyInt_AsLong(obj);
00261 
00262         return True;
00263 }

Sambaに対してSat Aug 29 21:23:14 2009に生成されました。  doxygen 1.4.7