Browse files

Erlang NIF interface to libcrack

  • Loading branch information...
0 parents commit 93dc44c9c8cf16af6b1a0bce2218e96814d6f0f1 @msantos committed Aug 16, 2010
Showing with 256 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +6 −0 Emakefile
  3. +39 −0 Makefile
  4. +36 −0 README
  5. +109 −0 c_src/cerck.c
  6. +63 −0 src/cerck.erl
3 .gitignore
@@ -0,0 +1,3 @@
+*.[oa]
+*.beam
+*.swp
6 Emakefile
@@ -0,0 +1,6 @@
+{["src/*"],
+ [{i, "include"},
+ {outdir, "ebin"},
+ debug_info]
+}.
+
39 Makefile
@@ -0,0 +1,39 @@
+
+ERL=erl
+APP=cerck
+
+CC=gcc
+
+# Use "-m64" for 64-bit Erlang installs
+ARCH=-m32
+
+# Mac OS X
+#FLAGS=$(ARCH) -O3 -fPIC -bundle -flat_namespace -undefined suppress -fno-common
+
+# Linux
+FLAGS=-fPIC -shared
+
+ERL_ROOT=/usr/local/lib/erlang
+CFLAGS=-g -Wall
+
+LIB=-lcrack
+
+all: dir erl nif
+
+dir:
+ -@mkdir -p ebin
+
+erl:
+ @$(ERL) -noinput +B \
+ -eval 'case make:all() of up_to_date -> halt(0); error -> halt(1) end.'
+
+nif:
+ (cd c_src && \
+ $(CC) $(ARCH) -g -Wall $(LIB) $(FLAGS) -o ../priv/cerck.so \
+ cerck.c -I $(ERL_ROOT)/usr/include/ )
+
+
+clean:
+ @rm -fv ebin/*.beam priv/$(APP) priv/$(APP).so c_src/*.a c_src/*.o
+
+
36 README
@@ -0,0 +1,36 @@
+
+Erlang interface to libcrack.
+
+
+HOW TO BUILD IT
+
+make
+
+
+HOW TO USE IT
+
+check(Password) -> ok | {error, string()}
+ Types Passwod = [ binary() | string() ]
+
+
+EXAMPLES
+
+1> cerck:check(<<"foobar">>).
+{error,"it is based on a dictionary word"}
+2> 4> cerck:check(<<"f00b4r">>).
+ok
+
+
+*** WARNING ***
+
+Do NOT call check/2 without checking the .pwi, .pwd and .hwm files exit
+for the passed in dictionary path. cracklib calls exit(-1) if these
+files do not exist. This will cause the Erlang VM to exit.
+
+
+TODO
+
+* test the dictionaries exist before calling FascistOpen()
+
+* do all of this in pure Erlang
+
109 c_src/cerck.c
@@ -0,0 +1,109 @@
+/* Copyright (c) 2010, Michael Santos <michael.santos@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the author nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <crack.h>
+#include "erl_nif.h"
+
+
+static ERL_NIF_TERM atom_ok;
+static ERL_NIF_TERM atom_error;
+static ERL_NIF_TERM atom_enomem;
+
+static ERL_NIF_TERM error_tuple(ErlNifEnv *env, char *err);
+
+
+ static int
+load(ErlNifEnv *env, void **priv_data, ERL_NIF_TERM load_info)
+{
+ atom_ok = enif_make_atom(env, "ok");
+ atom_error = enif_make_atom(env, "error");
+ atom_enomem = enif_make_atom(env, "enomem");
+
+ return (0);
+}
+
+
+/* 1: password, 2: dictpath */
+ static ERL_NIF_TERM
+nif_check(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
+{
+ ErlNifBinary passwd;
+ ErlNifBinary path;
+
+ char *err = NULL;
+
+
+ if (!enif_inspect_iolist_as_binary(env, argv[0], &passwd))
+ return enif_make_badarg(env);
+
+ if (!enif_inspect_iolist_as_binary(env, argv[1], &path))
+ return enif_make_badarg(env);
+
+ /* NULL terminate strings */
+ if (!enif_realloc_binary(&passwd, passwd.size+1))
+ return atom_enomem;
+ if (!enif_realloc_binary(&path, path.size+1))
+ return atom_enomem;
+
+ passwd.data[passwd.size-1] = '\0';
+ path.data[path.size-1] = '\0';
+
+ err = (char *)FascistCheck((char *)passwd.data, (char *)path.data);
+
+ enif_release_binary(&passwd);
+ enif_release_binary(&path);
+
+ return ( (err == NULL) ? atom_ok : error_tuple(env, err));
+}
+
+ static ERL_NIF_TERM
+nif_dictpath(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
+{
+ return enif_make_string(env, GetDefaultCracklibDict(), ERL_NIF_LATIN1);
+}
+
+ static ERL_NIF_TERM
+error_tuple(ErlNifEnv *env, char *err)
+{
+ return enif_make_tuple(env, 2,
+ atom_error,
+ enif_make_string(env, err, ERL_NIF_LATIN1));
+}
+
+
+static ErlNifFunc nif_funcs[] = {
+ {"check", 2, nif_check},
+ {"dictpath", 0, nif_dictpath}
+};
+
+ERL_NIF_INIT(cerck, nif_funcs, load, NULL, NULL, NULL)
+
+
63 src/cerck.erl
@@ -0,0 +1,63 @@
+%% Copyright (c) 2010, Michael Santos <michael.santos@gmail.com>
+%% All rights reserved.
+%%
+%% Redistribution and use in source and binary forms, with or without
+%% modification, are permitted provided that the following conditions
+%% are met:
+%%
+%% Redistributions of source code must retain the above copyright
+%% notice, this list of conditions and the following disclaimer.
+%%
+%% Redistributions in binary form must reproduce the above copyright
+%% notice, this list of conditions and the following disclaimer in the
+%% documentation and/or other materials provided with the distribution.
+%%
+%% Neither the name of the author nor the names of its contributors
+%% may be used to endorse or promote products derived from this software
+%% without specific prior written permission.
+%%
+%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+%% POSSIBILITY OF SUCH DAMAGE.
+-module(cerck).
+
+-export([
+ dictpath/0,
+ check/1, check/2
+ ]).
+
+-on_load(on_load/0).
+
+
+on_load() ->
+ erlang:load_nif(niflib(), []).
+
+dictpath() ->
+ erlang:error(not_implemented).
+check(_,_) ->
+ erlang:error(not_implemented).
+
+check(Passwd) ->
+ check(Passwd, dictpath()).
+
+
+privdir(File) ->
+ filename:join([
+ filename:dirname(code:which(?MODULE)),
+ "..",
+ "priv",
+ File
+ ]).
+
+niflib() ->
+ privdir(?MODULE).
+

0 comments on commit 93dc44c

Please sign in to comment.