1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
/* Copyright (C) 2001,2002 Red Hat, Inc.
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <Python.h>
#include <config.h>
#include <grp.h>
#include <limits.h>
#include <pwd.h>
#include <stdlib.h>
#include <unistd.h>
#include <glib.h>
#include "../lib/user.h"
#include "../lib/user_private.h"
#include "common.h"
static PyTypeObject PromptType;
#define Prompt_Check(__x) ((__x)->ob_type == &PromptType)
static gboolean
libuser_admin_python_prompter(struct lu_prompt *prompts, int count,
gpointer callback_data,
struct lu_error **error)
{
PyObject **prompt_data = (PyObject **) callback_data;
DEBUG_ENTRY;
if (count > 0) {
PyObject *list, *tuple, *ret;
int i;
if (!PyCallable_Check(prompt_data[0])) {
lu_error_new(error, lu_error_generic, NULL);
PyErr_SetString(PyExc_RuntimeError,
"prompter is not callable");
DEBUG_EXIT;
return FALSE;
}
list = PyList_New(0);
for (i = 0; i < count; i++) {
struct libuser_prompt *prompt;
prompt = (struct libuser_prompt *)
libuser_prompt_new(NULL, NULL);
prompt->prompt.key = g_strdup(prompts[i].key);
prompt->prompt.prompt = g_strdup(prompts[i].prompt);
prompt->prompt.domain = g_strdup(prompts[i].domain);
prompt->prompt.visible = prompts[i].visible;
prompt->prompt.default_value
= g_strdup(prompts[i].default_value);
prompt->prompt.value = g_strdup(prompts[i].value);
prompt->prompt.free_value = g_free;
PyList_Append(list, (PyObject *) prompt);
Py_DECREF(prompt);
}
tuple = PyTuple_New(PyTuple_Check(prompt_data[1]) ?
PyTuple_Size(prompt_data[1]) + 1 : 1);
PyTuple_SetItem(tuple, 0, list);
if (PyTuple_Check(prompt_data[1])) {
Py_ssize_t j;
for (j = 0; j < PyTuple_Size(prompt_data[1]); j++) {
PyObject *obj;
obj = PyTuple_GetItem(prompt_data[1], j);
Py_INCREF(obj);
PyTuple_SetItem(tuple, j + 1, obj);
}
}
ret = PyObject_CallObject(prompt_data[0], tuple);
if (PyErr_Occurred()) {
PyErr_Print();
Py_DECREF(tuple);
DEBUG_EXIT;
lu_error_new(error, lu_error_generic,
_
("error while prompting for necessary information"));
return FALSE;
}
for (i = 0; i < count; i++) {
struct libuser_prompt *prompt;
/* i doesn't have to be Py_ssize_t because count is int
as well. */
prompt = (struct libuser_prompt *)PyList_GetItem(list,
i);
prompts[i].value = g_strdup(prompt->prompt.value);
prompts[i].free_value = g_free;
}
Py_DECREF(tuple);
Py_DECREF(ret);
}
DEBUG_EXIT;
return TRUE;
}
static PyObject *
libuser_admin_prompt(struct libuser_admin *self, PyObject * args,
PyObject * kwargs, lu_prompt_fn * prompter)
{
Py_ssize_t count;
int i;
PyObject *list = NULL, *moreargs = NULL;
struct lu_prompt *prompts;
struct lu_error *error = NULL;
char *keywords[] = { "prompt_list", "more_args", NULL };
g_return_val_if_fail(self != NULL, NULL);
DEBUG_ENTRY;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|O", keywords,
&PyList_Type, &list,
&moreargs)) {
DEBUG_EXIT;
return NULL;
}
count = PyList_Size(list);
if (count > INT_MAX) {
PyErr_SetString(PyExc_ValueError, "too many prompts");
DEBUG_EXIT;
return NULL;
}
/* i now may be int because count fits in int */
for (i = 0; i < count; i++) {
PyObject *item;
item = PyList_GetItem(list, i);
DEBUG_CALL;
if (!Prompt_Check(item)) {
PyErr_SetString(PyExc_TypeError,
"expected list of Prompt objects");
DEBUG_EXIT;
return NULL;
}
DEBUG_CALL;
}
DEBUG_CALL;
prompts = g_malloc0(count * sizeof(struct lu_prompt));
DEBUG_CALL;
for (i = 0; i < count; i++) {
struct libuser_prompt *obj;
obj = (struct libuser_prompt *) PyList_GetItem(list, i);
Py_INCREF(obj);
prompts[i].key = g_strdup(obj->prompt.key ? : "");
prompts[i].domain = g_strdup(obj->prompt.domain ? : "");
prompts[i].prompt = g_strdup(obj->prompt.prompt ? : "");
prompts[i].default_value =
obj->prompt.default_value ? g_strdup(obj->prompt.default_value) :
NULL;
prompts[i].visible = obj->prompt.visible;
/* FIXME: free the values sometime? */
}
#ifdef DEBUG_BINDING
fprintf(stderr, "Prompter function promptConsole is at <%p>.\n",
lu_prompt_console);
fprintf(stderr,
"Prompter function promptConsoleQuiet is at <%p>.\n",
lu_prompt_console_quiet);
fprintf(stderr, "Calling prompter function at <%p>.\n", prompter);
#endif
if (prompter(prompts, count, self->prompt_data, &error) != FALSE) {
for (i = 0; i < count; i++) {
struct libuser_prompt *obj;
obj = (struct libuser_prompt *)PyList_GetItem(list, i);
obj->prompt.value = g_strdup(prompts[i].value ? : "");
obj->prompt.free_value = g_free;
if (prompts[i].value && prompts[i].free_value) {
prompts[i].free_value(prompts[i].value);
prompts[i].value = NULL;
prompts[i].free_value = NULL;
}
Py_DECREF(obj);
}
DEBUG_EXIT;
Py_RETURN_NONE;
} else {
if (error != NULL)
lu_error_free(&error);
for (i = 0; i < count; i++) {
PyObject *obj;
obj = PyList_GetItem(list, i);
Py_DECREF(obj);
}
PyErr_SetString(PyExc_RuntimeError,
"error prompting the user for information");
DEBUG_EXIT;
return NULL;
}
}
static PyObject *
libuser_admin_prompt_console(PyObject * self, PyObject * args,
PyObject * kwargs)
{
DEBUG_CALL;
return libuser_admin_prompt((struct libuser_admin *) self, args,
kwargs, lu_prompt_console);
}
static PyObject *
libuser_admin_prompt_console_quiet(PyObject * self, PyObject * args,
PyObject * kwargs)
{
DEBUG_CALL;
return libuser_admin_prompt((struct libuser_admin *) self, args,
kwargs, lu_prompt_console_quiet);
}
static void
libuser_prompt_destroy(PyObject *self)
{
struct libuser_prompt *me;
DEBUG_ENTRY;
me = (struct libuser_prompt *)self;
if (me->prompt.value && me->prompt.free_value)
me->prompt.free_value(me->prompt.value);
g_free((void *)me->prompt.key);
g_free((void *)me->prompt.prompt);
g_free((void *)me->prompt.domain);
g_free((void *)me->prompt.default_value);
memset(&me->prompt, 0, sizeof(me->prompt));
PyObject_DEL(me);
DEBUG_EXIT;
}
static PyObject *
libuser_prompt_getattr(PyObject *self, char *attr)
{
struct libuser_prompt *me;
DEBUG_ENTRY;
me = (struct libuser_prompt *)self;
if (strcmp(attr, "key") == 0) {
DEBUG_EXIT;
return PyString_FromString(me->prompt.key);
}
if (strcmp(attr, "prompt") == 0) {
DEBUG_EXIT;
return PyString_FromString(me->prompt.prompt);
}
if (strcmp(attr, "domain") == 0) {
DEBUG_EXIT;
return PyString_FromString(me->prompt.domain ?: "");
}
if (strcmp(attr, "visible") == 0) {
DEBUG_EXIT;
return PyInt_FromLong(me->prompt.visible);
}
if ((strcmp(attr, "default_value") == 0) ||
(strcmp(attr, "defaultValue") == 0)) {
DEBUG_EXIT;
if (me->prompt.default_value != NULL)
return PyString_FromString(me->prompt.default_value);
else
Py_RETURN_NONE;
}
if (strcmp(attr, "value") == 0) {
DEBUG_EXIT;
if (me->prompt.value != NULL)
return PyString_FromString(me->prompt.value);
else
Py_RETURN_NONE;
}
DEBUG_EXIT;
return Py_FindMethod(NULL, self, attr);
}
static int
libuser_prompt_setattr(PyObject *self, char *attr, PyObject * args)
{
struct libuser_prompt *me;
DEBUG_ENTRY;
me = (struct libuser_prompt *)self;
if (strcmp(attr, "prompt") == 0) {
if (!PyString_Check(args)) {
PyErr_SetString(PyExc_TypeError,
"prompt must be a string");
DEBUG_EXIT;
return -1;
}
g_free((char *)me->prompt.prompt);
me->prompt.prompt = g_strdup(PyString_AsString(args));
DEBUG_EXIT;
return 0;
}
if (strcmp(attr, "domain") == 0) {
if (!PyString_Check(args)) {
PyErr_SetString(PyExc_TypeError,
"domain must be a string");
DEBUG_EXIT;
return -1;
}
g_free((char *)me->prompt.domain);
me->prompt.domain = g_strdup(PyString_AsString(args));
DEBUG_EXIT;
return 0;
}
if (strcmp(attr, "key") == 0) {
if (!PyString_Check(args)) {
PyErr_SetString(PyExc_TypeError,
"key must be a string");
DEBUG_EXIT;
return -1;
}
g_free((char *)me->prompt.key);
me->prompt.key = g_strdup(PyString_AsString(args));
DEBUG_EXIT;
return 0;
}
if (strcmp(attr, "visible") == 0) {
me->prompt.visible = PyObject_IsTrue(args);
DEBUG_EXIT;
return 0;
}
if ((strcmp(attr, "default_value") == 0) ||
(strcmp(attr, "defaultValue") == 0)) {
if (!PyString_Check(args)) {
PyErr_SetString(PyExc_TypeError,
"default value must be a string");
DEBUG_EXIT;
return -1;
}
g_free((char *)me->prompt.default_value);
me->prompt.default_value = (args == Py_None)
? NULL : g_strdup(PyString_AsString(args));
DEBUG_EXIT;
return 0;
}
if (strcmp(attr, "value") == 0) {
if (!PyString_Check(args)) {
PyErr_SetString(PyExc_TypeError,
"value must be a string");
DEBUG_EXIT;
return -1;
}
if (me->prompt.value && me->prompt.free_value)
me->prompt.free_value(me->prompt.value);
me->prompt.value = g_strdup(PyString_AsString(args));
me->prompt.free_value = g_free;
DEBUG_EXIT;
return 0;
}
DEBUG_EXIT;
PyErr_SetString(PyExc_AttributeError, "invalid attribute");
return -1;
}
static int
libuser_prompt_print(PyObject *self, FILE *fp, int flags)
{
struct libuser_prompt *me;
(void)flags;
me = (struct libuser_prompt *)self;
fprintf(fp,
"(key = \"%s\", prompt = \"%s\", domain = \"%s\", "
"visible = %s, default_value = \"%s\", value = \"%s\")",
me->prompt.key ? me->prompt.key : "",
me->prompt.prompt ? me->prompt.prompt : "",
me->prompt.domain ? me->prompt.domain : "",
me->prompt.visible ? "true" : "false",
me->prompt.default_value ? me->prompt.default_value : "",
me->prompt.value ? me->prompt.value : "");
return 0;
}
static PyObject *
libuser_prompt_new(PyObject *ignored_self, PyObject *ignore)
{
struct libuser_prompt *ret;
(void)ignored_self;
(void)ignore;
DEBUG_ENTRY;
ret = PyObject_NEW(struct libuser_prompt, &PromptType);
if (ret != NULL) {
memset(&ret->prompt, 0, sizeof(ret->prompt));
}
DEBUG_EXIT;
return (PyObject *)ret;
}
static PyTypeObject PromptType = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"Prompt", /* tp_name */
sizeof(struct libuser_prompt), /* tp_basicsize */
0, /* tp_itemsize */
libuser_prompt_destroy, /* tp_dealloc */
libuser_prompt_print, /* tp_print */
libuser_prompt_getattr, /* tp_getattr */
libuser_prompt_setattr, /* tp_setattr */
NULL, /* tp_compare */
NULL, /* tp_repr */
};
|