2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
c_crypt.c
|
|
|
|
|
2013-08-03 17:38:01 +02:00
|
|
|
(c) 2000-2013 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
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
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
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
|
2011-06-03 02:51:09 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
2011-12-31 03:39:20 +01:00
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __C_CRYPT_C
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
|
2012-07-31 20:24:47 +02:00
|
|
|
#ifndef OS_BSD
|
|
|
|
#include <crypt.h>
|
|
|
|
#endif
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "c_crypt.h"
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
|
|
|
|
static char *do_crypt(const char *passwd, const char *prefix, bool use_md5)
|
|
|
|
{
|
|
|
|
static char key_table[65] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcedefghijklmnopqrstuvwxyz./";
|
|
|
|
static bool init = FALSE;
|
|
|
|
|
|
|
|
int i, n;
|
|
|
|
char key[16];
|
|
|
|
char *result;
|
|
|
|
char errormsg[35];
|
|
|
|
|
|
|
|
if (prefix)
|
|
|
|
{
|
|
|
|
n = use_md5 ? 8 : 2;
|
|
|
|
if (strlen(prefix) != n)
|
|
|
|
{
|
|
|
|
snprintf(errormsg, 35, "size must be %d characters long", n);
|
|
|
|
goto __BAD_PREFIX;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
if (strchr(key_table, prefix[i]) == NULL)
|
|
|
|
{
|
|
|
|
snprintf(errormsg, 35, "character '%c' not allowed" , prefix[i]);
|
|
|
|
goto __BAD_PREFIX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_md5)
|
|
|
|
{
|
|
|
|
strcpy(key, "$1$");
|
|
|
|
strcat(key, prefix);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(key, prefix);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!init)
|
|
|
|
{
|
|
|
|
srandom((unsigned int)time(NULL));
|
|
|
|
init = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_md5)
|
|
|
|
{
|
|
|
|
strcpy(key, "$1$");
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
key[i + 3] = key_table[(int)((random() / (RAND_MAX + 1.0)) * sizeof(key_table))];
|
|
|
|
key[11] = '$';
|
|
|
|
key[12] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
key[i] = key_table[(int)((random() / (RAND_MAX + 1.0)) * sizeof(key_table))];
|
|
|
|
key[2] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = crypt(passwd, key);
|
|
|
|
|
|
|
|
if (!result && errno == ENOSYS)
|
|
|
|
GB.Error("Crypting is not supported on this system");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
__BAD_PREFIX:
|
|
|
|
|
|
|
|
GB.Error("Bad prefix, &1",errormsg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool check_crypt(const char *passwd, const char *crypted)
|
|
|
|
{
|
|
|
|
char *result = crypt(passwd, crypted);
|
|
|
|
|
|
|
|
if (!result && errno == ENOSYS)
|
|
|
|
GB.Error("Crypting is not supported on this system");
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return TRUE;
|
|
|
|
else
|
2012-07-13 01:34:14 +02:00
|
|
|
return strcmp(result, crypted) != 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(CCRYPT_call_md5, GB_STRING password; GB_STRING key)
|
|
|
|
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
result = do_crypt(GB.ToZeroString(ARG(password)), MISSING(key) ? NULL : GB.ToZeroString(ARG(key)), TRUE);
|
|
|
|
if (result)
|
|
|
|
GB.ReturnNewZeroString(result);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(CCRYPT_call_des, GB_STRING password; GB_STRING key)
|
|
|
|
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
result = do_crypt(GB.ToZeroString(ARG(password)), MISSING(key) ? NULL : GB.ToZeroString(ARG(key)), FALSE);
|
|
|
|
if (result)
|
|
|
|
GB.ReturnNewZeroString(result);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(CCRYPT_check, GB_STRING password; GB_STRING crypt)
|
|
|
|
|
|
|
|
GB.ReturnBoolean(check_crypt(GB.ToZeroString(ARG(password)), GB.ToZeroString(ARG(crypt))));
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
GB_DESC CCryptDesc[] =
|
|
|
|
{
|
|
|
|
GB_DECLARE("Crypt", 0), GB_VIRTUAL_CLASS(),
|
|
|
|
|
|
|
|
GB_STATIC_METHOD("_call", "s", CCRYPT_call_md5, "(Password)s[(Prefix)s]"),
|
|
|
|
GB_STATIC_METHOD("Check", "b", CCRYPT_check, "(Password)s(Crypt)s"),
|
|
|
|
GB_STATIC_METHOD("MD5", "s", CCRYPT_call_md5, "(Password)s[(Prefix)s]"),
|
|
|
|
GB_STATIC_METHOD("DES", "s", CCRYPT_call_des, "(Password)s[(Prefix)s]"),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|
|
|
|
|