File: rbcrack.c

package info (click to toggle)
ruby-password 0.5.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 240 kB
  • ctags: 76
  • sloc: ruby: 407; ansic: 48; makefile: 4
file content (113 lines) | stat: -rw-r--r-- 3,355 bytes parent folder | download | duplicates (2)
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
108
109
110
111
112
113
/* rbcrack.c - a Ruby interface to CrackLib
 * 
 * $Id: rbcrack.c,v 1.20 2006/03/02 19:41:44 ianmacd Exp $
 *
 * Version : 0.5.3
 * Author  : Ian Macdonald <ian@caliban.org>
 *
 * Copyright (C) 2002-2006 Ian Macdonald
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2, or (at your option)
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software Foundation,
 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <ruby.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <crack.h>

#include "rbcrack.h"


VALUE ePassword_DictionaryError;
VALUE ePassword_WeakPassword;


/* <b>check</b><em>(dict=nil)</em>
 *
 * This interfaces to LibCrack to check the strength of the password. If
 * _dict_ is given, it is the path to the CrackLib dictionary, minus the
 * file's extension. For example, if the dictionary is located at
 * <tt>/usr/lib/cracklib_dict.pwd</tt>, _dict_ would be
 * <tt>/usr/lib/cracklib_dict</tt>. If it is not given, the dictionary found
 * at build time will be used.
 *
 * If a path is given that does not lead to a legible dictionary, a
 * Password::DictionaryError exception is raised. On success, +true+ is
 * returned. On failure, a Password::WeakPassword exception is raised.
 */
static VALUE passwd_check(VALUE self, VALUE args)
{
    VALUE dict;
    char *objection;
    char *buffer;

    /* pop the one and only argument we may have been passed */
    dict = rb_ary_pop(args);

    if (dict == Qnil || strcmp(StringValuePtr(dict), "") == 0) {
	/* no argument passed, so use default location from rbcrack.h */
	dict = rb_str_new2(CRACK_DICT);
    } else {
	buffer = malloc(strlen(StringValuePtr(dict)) + 8);
	strcpy(buffer, StringValuePtr(dict));
	strcat(buffer, ".pwd");

	if (access(buffer, R_OK) != 0) {
	    free(buffer);
	    rb_raise(ePassword_DictionaryError, "%s", strerror(errno));
	}

	free(buffer);

    }

    /* perform check on password */
    objection = FascistCheck(StringValuePtr(self), StringValuePtr(dict));

    /* return true on success; raise an exception otherwise */
    if (objection) {
	rb_raise(ePassword_WeakPassword, "%s", objection);
    } else {
	return Qtrue;
    }

}

/* initialize this class */
void Init_password_crack()
{
    VALUE cPassword;

    /* define the Password class */
    cPassword = rb_define_class("Password", rb_cString);

    /* define the Password::DictionaryError exception */
    ePassword_DictionaryError =
	rb_define_class_under(cPassword, "DictionaryError",
			      rb_eStandardError);

    /* define the Password::WeakPassword exception */
    ePassword_WeakPassword =
	rb_define_class_under(cPassword, "WeakPassword",
			      rb_eStandardError);

    /* define the Password.check method */
    rb_define_method(cPassword, "check", passwd_check, -2);

    return;
}