added gnutls backend and moved backend code into new files
[ircu2.10.12-pk.git] / ircd / ircd_crypt.c
index 98ee5882cd09d69b975b3824437941e74ef3f40c..2f50fe04856cb78f2e7064b16d07a0b14c663506 100644 (file)
  * @version $Id$
  * 
  * This is a new look crypto API for ircu, it can handle different
- * password formats by the grace of magic tokens at the begining of the 
+ * password formats by the grace of magic tokens at the beginning of the 
  * password e.g. $SMD5 for Salted MD5, $CRYPT for native crypt(), etc.
  *
  * Currently crypt routines are implemented for: the native crypt() 
  * function, Salted MD5 and a plain text mechanism which should only
- * be used for testing.  I intend to add Blowish, 3DES and possibly
+ * be used for testing.  I intend to add Blowfish, 3DES and possibly
  * SHA1 support as well at some point, but I'll need to check the
  * possible problems that'll cause with stupid crypto laws.
  *
@@ -47,6 +47,7 @@
 #include "ircd_crypt.h"
 #include "ircd_alloc.h"
 #include "ircd_features.h"
+#include "ircd_log.h"
 #include "ircd_string.h"
 #include "s_debug.h"
 
@@ -55,7 +56,7 @@
 #include "ircd_crypt_plain.h"
 #include "ircd_crypt_smd5.h"
 
-#include <assert.h>
+/* #include <assert.h> -- Now using assert in ircd_log.h */
 #include <unistd.h>
 #include <string.h>
 
@@ -73,7 +74,7 @@ int ircd_crypt_register_mech(crypt_mech_t* mechanism)
 {
 crypt_mechs_t* crypt_mech;
 
- Debug((DEBUG_INFO, "ircd_crypt_register_mech: resistering mechanism: %s", mechanism->shortname));
+ Debug((DEBUG_INFO, "ircd_crypt_register_mech: registering mechanism: %s", mechanism->shortname));
 
  /* try to allocate some memory for the new mechanism */
  if ((crypt_mech = (crypt_mechs_t*)MyMalloc(sizeof(crypt_mechs_t))) == NULL)
@@ -103,7 +104,7 @@ crypt_mechs_t* crypt_mech;
  }
   
  /* we're done */
- Debug((DEBUG_INFO, "ircd_crypt_register_mech: resistered mechanism: %s, crypt_function is at 0x%X.", crypt_mech->mech->shortname, &crypt_mech->mech->crypt_function));
+ Debug((DEBUG_INFO, "ircd_crypt_register_mech: registered mechanism: %s, crypt_function is at 0x%X.", crypt_mech->mech->shortname, &crypt_mech->mech->crypt_function));
  Debug((DEBUG_INFO, "ircd_crypt_register_mech: %s: %s", crypt_mech->mech->shortname, crypt_mech->mech->description));
  return 0;
 }
@@ -121,13 +122,13 @@ return 0;
 /** Wrapper for generating a hashed password passed on the supplied password
  * @param key Pointer to the password we want crypted
  * @param salt Pointer to the password we're comparing to (for the salt)
- * @return Pointer to the generated password.
- *  
+ * @return Pointer to the generated password (must be MyFree()'d).
+ *
  * This is a wrapper function which attempts to establish the password
- * format and funnel it off to the correct mechanism handler function.  The 
+ * format and funnel it off to the correct mechanism handler function.  The
  * returned password is compared in the oper_password_match() routine.
 */
-const char* ircd_crypt(const char* key, const char* salt)
+char* ircd_crypt(const char* key, const char* salt)
 {
 char *hashed_pass = NULL;
 const char *temp_hashed_pass, *mysalt;
@@ -190,7 +191,7 @@ crypt_mechs_t* crypt_mech;
    ircd_strncpy(hashed_pass + crypt_mech->mech->crypt_token_size, temp_hashed_pass, strlen(temp_hashed_pass));
    Debug((DEBUG_DEBUG, "ircd_crypt: tagged pass is %s", hashed_pass));
   } else {
-   Debug((DEBUG_DEBUG, "ircd_crypt: will try next mechansim at 0x%X", 
+   Debug((DEBUG_DEBUG, "ircd_crypt: will try next mechanism at 0x%X", 
     crypt_mech->next));
    crypt_mech = crypt_mech->next;
    continue;
@@ -201,9 +202,13 @@ crypt_mechs_t* crypt_mech;
  /* try to use native crypt for an old-style (untagged) password */
  if (strlen(salt) > 2)
  {
+   char *s;
    temp_hashed_pass = (char*)ircd_crypt_native(key, salt);
    if (!ircd_strcmp(temp_hashed_pass, salt))
-    return strdup(temp_hashed_pass);
+   {
+     DupString(s, temp_hashed_pass);
+     return s;
+   }
  }
 
  return NULL;
@@ -231,8 +236,8 @@ void ircd_crypt_init(void)
  crypt_mechs_root->mech = NULL;
  crypt_mechs_root->next = crypt_mechs_root->prev = NULL;
 
-/* temporary kludge until we're modular.  manualy call the
-   register funtions for crypt mechanisms */
+/* temporary kludge until we're modular.  manually call the
+   register functions for crypt mechanisms */
  ircd_register_crypt_smd5();
  ircd_register_crypt_plain();
  ircd_register_crypt_native();