root/source4/auth/credentials/pycredentials.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. cli_credentials_from_py_object
  2. PyString_FromStringOrNULL
  3. py_creds_new
  4. py_creds_get_username
  5. py_creds_set_username
  6. py_creds_get_password
  7. py_creds_set_password
  8. py_creds_get_domain
  9. py_creds_set_domain
  10. py_creds_get_realm
  11. py_creds_set_realm
  12. py_creds_get_bind_dn
  13. py_creds_set_bind_dn
  14. py_creds_get_workstation
  15. py_creds_set_workstation
  16. py_creds_is_anonymous
  17. py_creds_set_anonymous
  18. py_creds_authentication_requested
  19. py_creds_wrong_password
  20. py_creds_set_cmdline_callbacks
  21. py_creds_parse_string
  22. py_creds_get_nt_hash
  23. py_creds_set_kerberos_state
  24. py_creds_guess
  25. py_creds_set_machine_account
  26. initcredentials

   1 /* 
   2    Unix SMB/CIFS implementation.
   3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
   4    
   5    This program is free software; you can redistribute it and/or modify
   6    it under the terms of the GNU General Public License as published by
   7    the Free Software Foundation; either version 3 of the License, or
   8    (at your option) any later version.
   9    
  10    This program is distributed in the hope that it will be useful,
  11    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13    GNU General Public License for more details.
  14    
  15    You should have received a copy of the GNU General Public License
  16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17 */
  18 
  19 #include "includes.h"
  20 #include <Python.h>
  21 #include "pycredentials.h"
  22 #include "param/param.h"
  23 #include "lib/cmdline/credentials.h"
  24 #include "librpc/gen_ndr/samr.h" /* for struct samr_Password */
  25 #include "libcli/util/pyerrors.h"
  26 #include "param/pyparam.h"
  27 
  28 #ifndef Py_RETURN_NONE
  29 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
  30 #endif
  31 
  32 struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj)
     /* [<][>][^][v][top][bottom][index][help] */
  33 {
  34     if (py_obj == Py_None) {
  35         return cli_credentials_init_anon(NULL);
  36     }
  37 
  38     return PyCredentials_AsCliCredentials(py_obj);
  39 }
  40 
  41 static PyObject *PyString_FromStringOrNULL(const char *str)
     /* [<][>][^][v][top][bottom][index][help] */
  42 {
  43         if (str == NULL)
  44                 Py_RETURN_NONE;
  45         return PyString_FromString(str);
  46 }
  47 
  48 static PyObject *py_creds_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     /* [<][>][^][v][top][bottom][index][help] */
  49 {
  50         return py_talloc_import(type, cli_credentials_init(NULL));
  51 }
  52 
  53 static PyObject *py_creds_get_username(py_talloc_Object *self)
     /* [<][>][^][v][top][bottom][index][help] */
  54 {
  55         return PyString_FromStringOrNULL(cli_credentials_get_username(PyCredentials_AsCliCredentials(self)));
  56 }
  57 
  58 static PyObject *py_creds_set_username(py_talloc_Object *self, PyObject *args)
     /* [<][>][^][v][top][bottom][index][help] */
  59 {
  60         char *newval;
  61         enum credentials_obtained obt = CRED_SPECIFIED;
  62         if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
  63                 return NULL;
  64 
  65         return PyBool_FromLong(cli_credentials_set_username(PyCredentials_AsCliCredentials(self), newval, obt));
  66 }
  67 
  68 static PyObject *py_creds_get_password(py_talloc_Object *self)
     /* [<][>][^][v][top][bottom][index][help] */
  69 {
  70         return PyString_FromStringOrNULL(cli_credentials_get_password(PyCredentials_AsCliCredentials(self)));
  71 }
  72 
  73 
  74 static PyObject *py_creds_set_password(py_talloc_Object *self, PyObject *args)
     /* [<][>][^][v][top][bottom][index][help] */
  75 {
  76         char *newval;
  77         enum credentials_obtained obt = CRED_SPECIFIED;
  78         if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
  79                 return NULL;
  80 
  81         return PyBool_FromLong(cli_credentials_set_password(PyCredentials_AsCliCredentials(self), newval, obt));
  82 }
  83 
  84 static PyObject *py_creds_get_domain(py_talloc_Object *self)
     /* [<][>][^][v][top][bottom][index][help] */
  85 {
  86         return PyString_FromStringOrNULL(cli_credentials_get_domain(PyCredentials_AsCliCredentials(self)));
  87 }
  88 
  89 static PyObject *py_creds_set_domain(py_talloc_Object *self, PyObject *args)
     /* [<][>][^][v][top][bottom][index][help] */
  90 {
  91         char *newval;
  92         enum credentials_obtained obt = CRED_SPECIFIED;
  93         if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
  94                 return NULL;
  95 
  96         return PyBool_FromLong(cli_credentials_set_domain(PyCredentials_AsCliCredentials(self), newval, obt));
  97 }
  98 
  99 static PyObject *py_creds_get_realm(py_talloc_Object *self)
     /* [<][>][^][v][top][bottom][index][help] */
 100 {
 101         return PyString_FromStringOrNULL(cli_credentials_get_realm(PyCredentials_AsCliCredentials(self)));
 102 }
 103 
 104 static PyObject *py_creds_set_realm(py_talloc_Object *self, PyObject *args)
     /* [<][>][^][v][top][bottom][index][help] */
 105 {
 106         char *newval;
 107         enum credentials_obtained obt = CRED_SPECIFIED;
 108         if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
 109                 return NULL;
 110 
 111         return PyBool_FromLong(cli_credentials_set_realm(PyCredentials_AsCliCredentials(self), newval, obt));
 112 }
 113 
 114 static PyObject *py_creds_get_bind_dn(py_talloc_Object *self)
     /* [<][>][^][v][top][bottom][index][help] */
 115 {
 116         return PyString_FromStringOrNULL(cli_credentials_get_bind_dn(PyCredentials_AsCliCredentials(self)));
 117 }
 118 
 119 static PyObject *py_creds_set_bind_dn(py_talloc_Object *self, PyObject *args)
     /* [<][>][^][v][top][bottom][index][help] */
 120 {
 121         char *newval;
 122         if (!PyArg_ParseTuple(args, "s", &newval))
 123                 return NULL;
 124 
 125         return PyBool_FromLong(cli_credentials_set_bind_dn(PyCredentials_AsCliCredentials(self), newval));
 126 }
 127 
 128 static PyObject *py_creds_get_workstation(py_talloc_Object *self)
     /* [<][>][^][v][top][bottom][index][help] */
 129 {
 130         return PyString_FromStringOrNULL(cli_credentials_get_workstation(PyCredentials_AsCliCredentials(self)));
 131 }
 132 
 133 static PyObject *py_creds_set_workstation(py_talloc_Object *self, PyObject *args)
     /* [<][>][^][v][top][bottom][index][help] */
 134 {
 135         char *newval;
 136         enum credentials_obtained obt = CRED_SPECIFIED;
 137         if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
 138                 return NULL;
 139 
 140         return PyBool_FromLong(cli_credentials_set_workstation(PyCredentials_AsCliCredentials(self), newval, obt));
 141 }
 142 
 143 static PyObject *py_creds_is_anonymous(py_talloc_Object *self)
     /* [<][>][^][v][top][bottom][index][help] */
 144 {
 145         return PyBool_FromLong(cli_credentials_is_anonymous(PyCredentials_AsCliCredentials(self)));
 146 }
 147 
 148 static PyObject *py_creds_set_anonymous(py_talloc_Object *self)
     /* [<][>][^][v][top][bottom][index][help] */
 149 {
 150         cli_credentials_set_anonymous(PyCredentials_AsCliCredentials(self));
 151         Py_RETURN_NONE;
 152 }
 153 
 154 static PyObject *py_creds_authentication_requested(py_talloc_Object *self)
     /* [<][>][^][v][top][bottom][index][help] */
 155 {
 156         return PyBool_FromLong(cli_credentials_authentication_requested(PyCredentials_AsCliCredentials(self)));
 157 }
 158 
 159 static PyObject *py_creds_wrong_password(py_talloc_Object *self)
     /* [<][>][^][v][top][bottom][index][help] */
 160 {
 161         return PyBool_FromLong(cli_credentials_wrong_password(PyCredentials_AsCliCredentials(self)));
 162 }
 163 
 164 static PyObject *py_creds_set_cmdline_callbacks(py_talloc_Object *self)
     /* [<][>][^][v][top][bottom][index][help] */
 165 {
 166         return PyBool_FromLong(cli_credentials_set_cmdline_callbacks(PyCredentials_AsCliCredentials(self)));
 167 }
 168 
 169 static PyObject *py_creds_parse_string(py_talloc_Object *self, PyObject *args)
     /* [<][>][^][v][top][bottom][index][help] */
 170 {
 171         char *newval;
 172         enum credentials_obtained obt = CRED_SPECIFIED;
 173         if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
 174                 return NULL;
 175 
 176         cli_credentials_parse_string(PyCredentials_AsCliCredentials(self), newval, obt);
 177         Py_RETURN_NONE;
 178 }
 179 
 180 static PyObject *py_creds_get_nt_hash(py_talloc_Object *self)
     /* [<][>][^][v][top][bottom][index][help] */
 181 {
 182         const struct samr_Password *ntpw = cli_credentials_get_nt_hash(PyCredentials_AsCliCredentials(self), self->ptr);
 183 
 184         return PyString_FromStringAndSize(discard_const_p(char, ntpw->hash), 16);
 185 }
 186 
 187 static PyObject *py_creds_set_kerberos_state(py_talloc_Object *self, PyObject *args)
     /* [<][>][^][v][top][bottom][index][help] */
 188 {
 189         int state;
 190         if (!PyArg_ParseTuple(args, "i", &state))
 191                 return NULL;
 192 
 193         cli_credentials_set_kerberos_state(PyCredentials_AsCliCredentials(self), state);
 194         Py_RETURN_NONE;
 195 }
 196 
 197 static PyObject *py_creds_guess(py_talloc_Object *self, PyObject *args)
     /* [<][>][^][v][top][bottom][index][help] */
 198 {
 199         PyObject *py_lp_ctx = Py_None;
 200         struct loadparm_context *lp_ctx;
 201         if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
 202                 return NULL;
 203 
 204         lp_ctx = lp_from_py_object(py_lp_ctx);
 205         if (lp_ctx == NULL) 
 206                 return NULL;
 207 
 208         cli_credentials_guess(PyCredentials_AsCliCredentials(self), lp_ctx);
 209 
 210         Py_RETURN_NONE;
 211 }
 212 
 213 static PyObject *py_creds_set_machine_account(py_talloc_Object *self, PyObject *args)
     /* [<][>][^][v][top][bottom][index][help] */
 214 {
 215         PyObject *py_lp_ctx = Py_None;
 216         struct loadparm_context *lp_ctx;
 217         NTSTATUS status;
 218         if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
 219                 return NULL;
 220 
 221         lp_ctx = lp_from_py_object(py_lp_ctx);
 222         if (lp_ctx == NULL) 
 223                 return NULL;
 224 
 225         status = cli_credentials_set_machine_account(PyCredentials_AsCliCredentials(self), lp_ctx);
 226         PyErr_NTSTATUS_IS_ERR_RAISE(status);
 227 
 228         Py_RETURN_NONE;
 229 }
 230 
 231 static PyMethodDef py_creds_methods[] = {
 232         { "get_username", (PyCFunction)py_creds_get_username, METH_NOARGS,
 233                 "S.get_username() -> username\nObtain username." },
 234         { "set_username", (PyCFunction)py_creds_set_username, METH_VARARGS,
 235                 "S.set_username(name, obtained=CRED_SPECIFIED) -> None\n"
 236                 "Change username." },
 237         { "get_password", (PyCFunction)py_creds_get_password, METH_NOARGS,
 238                 "S.get_password() -> password\n"
 239                 "Obtain password." },
 240         { "set_password", (PyCFunction)py_creds_set_password, METH_VARARGS,
 241                 "S.set_password(password, obtained=CRED_SPECIFIED) -> None\n"
 242                 "Change password." },
 243         { "get_domain", (PyCFunction)py_creds_get_domain, METH_NOARGS,
 244                 "S.get_domain() -> domain\n"
 245                 "Obtain domain name." },
 246         { "set_domain", (PyCFunction)py_creds_set_domain, METH_VARARGS,
 247                 "S.set_domain(domain, obtained=CRED_SPECIFIED) -> None\n"
 248                 "Change domain name." },
 249         { "get_realm", (PyCFunction)py_creds_get_realm, METH_NOARGS,
 250                 "S.get_realm() -> realm\n"
 251                 "Obtain realm name." },
 252         { "set_realm", (PyCFunction)py_creds_set_realm, METH_VARARGS,
 253                 "S.set_realm(realm, obtained=CRED_SPECIFIED) -> None\n"
 254                 "Change realm name." },
 255         { "get_bind_dn", (PyCFunction)py_creds_get_bind_dn, METH_NOARGS,
 256                 "S.get_bind_dn() -> bind dn\n"
 257                 "Obtain bind DN." },
 258         { "set_bind_dn", (PyCFunction)py_creds_set_bind_dn, METH_VARARGS,
 259                 "S.set_bind_dn(bind_dn) -> None\n"
 260                 "Change bind DN." },
 261         { "is_anonymous", (PyCFunction)py_creds_is_anonymous, METH_NOARGS,
 262                 NULL },
 263         { "set_anonymous", (PyCFunction)py_creds_set_anonymous, METH_NOARGS,
 264                 "S.set_anonymous() -> None\n"
 265                 "Use anonymous credentials." },
 266         { "get_workstation", (PyCFunction)py_creds_get_workstation, METH_NOARGS,
 267                 NULL },
 268         { "set_workstation", (PyCFunction)py_creds_set_workstation, METH_VARARGS,
 269                 NULL },
 270         { "authentication_requested", (PyCFunction)py_creds_authentication_requested, METH_NOARGS,
 271                 NULL },
 272         { "wrong_password", (PyCFunction)py_creds_wrong_password, METH_NOARGS,
 273                 "S.wrong_password() -> bool\n"
 274                 "Indicate the returned password was incorrect." },
 275         { "set_cmdline_callbacks", (PyCFunction)py_creds_set_cmdline_callbacks, METH_NOARGS,
 276                 "S.set_cmdline_callbacks() -> bool\n"
 277                 "Use command-line to obtain credentials not explicitly set." },
 278         { "parse_string", (PyCFunction)py_creds_parse_string, METH_VARARGS,
 279                 "S.parse_string(text, obtained=CRED_SPECIFIED) -> None\n"
 280                 "Parse credentials string." },
 281         { "get_nt_hash", (PyCFunction)py_creds_get_nt_hash, METH_NOARGS,
 282                 NULL },
 283         { "set_kerberos_state", (PyCFunction)py_creds_set_kerberos_state, METH_VARARGS,
 284                 NULL },
 285         { "guess", (PyCFunction)py_creds_guess, METH_VARARGS, NULL },
 286         { "set_machine_account", (PyCFunction)py_creds_set_machine_account, METH_VARARGS, NULL },
 287         { NULL }
 288 };
 289 
 290 PyTypeObject PyCredentials = {
 291         .tp_name = "Credentials",
 292         .tp_basicsize = sizeof(py_talloc_Object),
 293         .tp_dealloc = py_talloc_dealloc,
 294         .tp_new = py_creds_new,
 295         .tp_flags = Py_TPFLAGS_DEFAULT,
 296         .tp_methods = py_creds_methods,
 297 };
 298 
 299 void initcredentials(void)
     /* [<][>][^][v][top][bottom][index][help] */
 300 {
 301         PyObject *m;
 302 
 303         if (PyType_Ready(&PyCredentials) < 0)
 304                 return;
 305 
 306         m = Py_InitModule3("credentials", NULL, "Credentials management.");
 307         if (m == NULL)
 308                 return;
 309 
 310         PyModule_AddObject(m, "AUTO_USE_KERBEROS", PyInt_FromLong(CRED_AUTO_USE_KERBEROS));
 311         PyModule_AddObject(m, "DONT_USE_KERBEROS", PyInt_FromLong(CRED_DONT_USE_KERBEROS));
 312         PyModule_AddObject(m, "MUST_USE_KERBEROS", PyInt_FromLong(CRED_MUST_USE_KERBEROS));
 313 
 314         Py_INCREF(&PyCredentials);
 315         PyModule_AddObject(m, "Credentials", (PyObject *)&PyCredentials);
 316 }

/* [<][>][^][v][top][bottom][index][help] */