Exemplo n.º 1
0
static int
load(ErlNifEnv *env, void **priv_data, ERL_NIF_TERM load_info)
{
    PRIV *priv = NULL;

    atom_ok = enif_make_atom(env, "ok");
    atom_error = enif_make_atom(env, "error");
    atom_enomem = enif_make_atom(env, "enomem");

    priv = (PRIV *)enif_alloc(sizeof(PRIV));
    if (priv == NULL)
        return (-1);

    priv->dictpath = enif_make_string(env, GetDefaultCracklibDict(), ERL_NIF_LATIN1);

    priv->lock = enif_mutex_create("cerck_lock");
    if (priv->lock == NULL)
        return (-1);

    *priv_data = priv;

    return (0);
}
Exemplo n.º 2
0
static PyObject *
_cracklib_FascistCheck(PyObject *self, PyObject *args, PyObject *kwargs)
{
    char *candidate, *dict;
    char *defaultdict = NULL;
    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, dictfile);
            free(dictfile);
            return NULL;
        }
        free(dictfile);
    } else
    {
        defaultdict = strdup(GetDefaultCracklibDict());
        if (errno == ENOMEM) {
            PyErr_SetFromErrno(PyExc_OSError);
            return NULL;
        }
        dictfile = malloc(strlen(defaultdict) + sizeof(DICT_SUFFIX));
        if (dictfile == NULL)
        {
            PyErr_SetFromErrnoWithFilename(PyExc_OSError, defaultdict);
            free(defaultdict);
            return NULL;
        }
        sprintf(dictfile, "%s" DICT_SUFFIX, defaultdict);
        if (lstat(dictfile, &st) == -1)
        {
            PyErr_SetFromErrnoWithFilename(PyExc_OSError, dictfile);
            free(defaultdict);
            free(dictfile);
            return NULL;
        }
        free(dictfile);
    }

	setlocale(LC_ALL, "");
#ifdef ENABLE_NLS
	textdomain("cracklib");
#endif

    LOCK();
    result = FascistCheck(candidate, dict ? dict : defaultdict);
    UNLOCK();

    if (defaultdict != NULL)
    {
        free(defaultdict);
    }

    if (result != NULL)
    {
    	PyErr_SetString(PyExc_ValueError, result);
        return NULL;
    }
    return Py_BuildValue("s", candidate);
}