File: cracklibmodule.c

package info (click to toggle)
cracklib2 2.8.12-8lenny1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,952 kB
  • ctags: 168
  • sloc: sh: 9,047; ansic: 2,392; xml: 365; makefile: 203; sed: 16; python: 1
file content (107 lines) | stat: -rw-r--r-- 2,526 bytes parent folder | download
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
/*
 *  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

#define DICT_SUFFIX ".pwd"

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};
    char *dictfile;

    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;
        }
        dictfile = malloc(strlen(dict) + sizeof(DICT_SUFFIX));
        if (dictfile == NULL)
        {
            PyErr_SetFromErrnoWithFilename(PyExc_OSError, dict);
            return NULL;
        }
        sprintf(dictfile, "%s" DICT_SUFFIX, dict);
        if (lstat(dictfile, &st) == -1)
        {
            PyErr_SetFromErrnoWithFilename(PyExc_OSError, dict);
            free(dictfile);
            return NULL;
        }
        free(dictfile);
    } else
    {
        if (lstat(DEFAULT_CRACKLIB_DICT DICT_SUFFIX, &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);
}