]> granicus.if.org Git - cracklib/commitdiff
add python binding support
authorNathan Neulinger <nneul@neulinger.org>
Fri, 9 Sep 2005 23:17:40 +0000 (23:17 +0000)
committerNathan Neulinger <nneul@neulinger.org>
Fri, 9 Sep 2005 23:17:40 +0000 (23:17 +0000)
git-svn-id: file:///tmp/cracklib-svn/trunk/cracklib@65 4175fe1e-86d5-4fdc-8e6a-506fab9d8533

ChangeLog
Makefile.am
configure.in
python/.cvsignore [new file with mode: 0644]
python/Makefile.am [new file with mode: 0644]
python/cracklibmodule.c [new file with mode: 0644]

index 93969daee22912fb67f9d574ce6b5d4af50754ee..f503acb4287b6906af308003bef416ebacce696f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,6 @@
+2005-09-09:
+    * Patch from Nalin Dahyabhai <nalin@redhat.com> for python binding support.
+
 2005-06-24:
     * More updates and patch from Thorsten Kukuk for internationalization support.
 
index 8b148b4bef6cba396f825bafd694312d5f950e05..6e1829eef995730107d653e3d3a4baae9a9cdb8d 100644 (file)
@@ -1,4 +1,4 @@
-SUBDIRS = m4 lib util po doc
+SUBDIRS = m4 lib util po doc python
 
 EXTRA_DIST = config.rpath mkinstalldirs        \
                autogen.sh \
index 67fed849be88f744b85959c5978ed947616caebd..d23e863bdf07a70dd48b9c8b6b12a756a45ee8ed 100644 (file)
@@ -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/python/.cvsignore b/python/.cvsignore
new file mode 100644 (file)
index 0000000..30ca9d3
--- /dev/null
@@ -0,0 +1,7 @@
+.deps
+.libs
+Makefile
+Makefile.in
+cracklibmodule.la
+cracklibmodule.lo
+
diff --git a/python/Makefile.am b/python/Makefile.am
new file mode 100644 (file)
index 0000000..f6abc0c
--- /dev/null
@@ -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/python/cracklibmodule.c b/python/cracklibmodule.c
new file mode 100644 (file)
index 0000000..2b2bbd7
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ *  A Python binding for cracklib.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <Python.h>
+#ifdef HAVE_PTHREAD_H
+#include <pthread.h>
+#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);
+}