From: Nathan Neulinger Date: Fri, 9 Sep 2005 23:17:40 +0000 (+0000) Subject: add python binding support X-Git-Tag: v2.8.4~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=60079ffc151fd719620b5b81cf6c029aecf1e60b;p=cracklib add python binding support git-svn-id: file:///tmp/cracklib-svn/trunk@65 4175fe1e-86d5-4fdc-8e6a-506fab9d8533 --- diff --git a/cracklib/ChangeLog b/cracklib/ChangeLog index 93969da..f503acb 100644 --- a/cracklib/ChangeLog +++ b/cracklib/ChangeLog @@ -1,3 +1,6 @@ +2005-09-09: + * Patch from Nalin Dahyabhai for python binding support. + 2005-06-24: * More updates and patch from Thorsten Kukuk for internationalization support. diff --git a/cracklib/Makefile.am b/cracklib/Makefile.am index 8b148b4..6e1829e 100644 --- a/cracklib/Makefile.am +++ b/cracklib/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = m4 lib util po doc +SUBDIRS = m4 lib util po doc python EXTRA_DIST = config.rpath mkinstalldirs \ autogen.sh \ diff --git a/cracklib/configure.in b/cracklib/configure.in index 67fed84..d23e863 100644 --- a/cracklib/configure.in +++ b/cracklib/configure.in @@ -21,6 +21,7 @@ AC_CHECK_HEADERS(unistd.h) AC_CHECK_HEADERS(fcntl.h) AC_CHECK_HEADERS(inttypes.h) AC_CHECK_HEADERS(stdint.h) +AC_CHECK_HEADERS(pthread.h) dnl Cygwin workaround AC_MSG_CHECKING(if LINE_MAX is defined) @@ -47,6 +48,25 @@ dnl internationalization macros AM_GNU_GETTEXT_VERSION AM_GNU_GETTEXT([external]) -AC_OUTPUT(util/Makefile lib/Makefile doc/Makefile Makefile \ +dnl Check for python, unless we were told to not try to build a python module +AC_ARG_WITH(python, +AC_HELP_STRING(--without,[Build a python module (default auto)]), +build_python=$withval,build_python=auto) +if test "$build_python" != no ; then + AM_PATH_PYTHON(,,[ + if test "$build_python" = auto ; then + AC_MSG_WARN([python was not found, continuing]) + build_python=no + else + AC_MSG_ERROR([python was required but not found]) + fi + ]) + if test "$build_python" != no ; then + build_python=yes + fi +fi +AM_CONDITIONAL(BUILD_PYTHON,[test "$build_python" = "yes"]) + +AC_OUTPUT(util/Makefile lib/Makefile doc/Makefile python/Makefile Makefile \ po/Makefile.in m4/Makefile cracklib.spec) diff --git a/cracklib/python/.cvsignore b/cracklib/python/.cvsignore new file mode 100644 index 0000000..30ca9d3 --- /dev/null +++ b/cracklib/python/.cvsignore @@ -0,0 +1,7 @@ +.deps +.libs +Makefile +Makefile.in +cracklibmodule.la +cracklibmodule.lo + diff --git a/cracklib/python/Makefile.am b/cracklib/python/Makefile.am new file mode 100644 index 0000000..f6abc0c --- /dev/null +++ b/cracklib/python/Makefile.am @@ -0,0 +1,7 @@ +if BUILD_PYTHON +pyexec_LTLIBRARIES = cracklibmodule.la +cracklibmodule_la_LDFLAGS = -module -avoid-version +INCLUDES = -I${PYTHON_PREFIX}/include/python${PYTHON_VERSION} -I/usr/include/python${PYTHON_VERSION} +DEFS += '-DDEFAULT_CRACKLIB_DICT="$(pkgdatadir)/pw_dict"' +endif + diff --git a/cracklib/python/cracklibmodule.c b/cracklib/python/cracklibmodule.c new file mode 100644 index 0000000..2b2bbd7 --- /dev/null +++ b/cracklib/python/cracklibmodule.c @@ -0,0 +1,95 @@ +/* + * A Python binding for cracklib. + */ + +#include +#include +#include +#ifdef HAVE_PTHREAD_H +#include +#endif +#include "../lib/crack.h" + +#ifdef HAVE_PTHREAD_H +static pthread_mutex_t cracklib_mutex = PTHREAD_MUTEX_INITIALIZER; +#define LOCK() pthread_mutex_lock(&cracklib_mutex) +#define UNLOCK() pthread_mutex_unlock(&cracklib_mutex) +#else +#define LOCK() +#define UNLOCK() +#endif + +static PyObject * +cracklib_FascistCheck(PyObject *self, PyObject *args, PyObject *kwargs) +{ + int i; + char *candidate, *dict; + const char *result; + struct stat st; + char *keywords[] = {"pw", "dictpath", NULL}; + + self = NULL; + candidate = NULL; + dict = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|s", keywords, + &candidate, &dict)) + { + PyErr_SetString(PyExc_ValueError, "error parsing arguments"); + return NULL; + } + + if (candidate == NULL) + { + PyErr_SetString(PyExc_ValueError, "first argument was not a string!"); + return NULL; + } + if (dict != NULL) + { + if (dict[0] != '/') + { + PyErr_SetString(PyExc_ValueError, + "second argument was not an absolute path!"); + return NULL; + } + if (lstat(dict, &st) == -1) + { + PyErr_SetFromErrnoWithFilename(PyExc_OSError, dict); + return NULL; + } + } else + { + if (lstat(DEFAULT_CRACKLIB_DICT ".pwd", &st) == -1) + { + PyErr_SetFromErrnoWithFilename(PyExc_OSError, + DEFAULT_CRACKLIB_DICT); + return NULL; + } + } + + LOCK(); + result = FascistCheck(candidate, dict ? dict : DEFAULT_CRACKLIB_DICT); + UNLOCK(); + + if (result != NULL) + { + return PyString_FromString(result); + } else + { + Py_INCREF(Py_None); + return Py_None; + } +} + +static PyMethodDef +cracklibmethods[] = +{ + {"FascistCheck", cracklib_FascistCheck, METH_VARARGS | METH_KEYWORDS}, + {NULL, NULL}, +}; + +void +initcracklib(void) +{ + Py_InitModule("cracklib", cracklibmethods); +}