1
0

ns-mta-md5_pwd.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * slapd hashed password routines
  14. *
  15. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <sys/types.h>
  19. #include "pwdstorage.h"
  20. #include "md5.h" /* JCM - This is a core server header... These functions could be made part of the slapi API. */
  21. /*
  22. * Netscape Mail Server MD5 support (compare-only; no support for encoding)
  23. */
  24. static char * ns_mta_hextab = "0123456789abcdef";
  25. static void
  26. ns_mta_hexify(char *buffer, char *str, int len)
  27. {
  28. char *pch = str;
  29. char ch;
  30. int i;
  31. for(i = 0;i < len; i ++) {
  32. ch = pch[i];
  33. buffer[2*i] = ns_mta_hextab[(ch>>4)&15];
  34. buffer[2*i+1] = ns_mta_hextab[ch&15];
  35. }
  36. return;
  37. }
  38. static char *
  39. ns_mta_hash_alg(char *buffer, char *salt, const char *passwd)
  40. {
  41. mta_MD5_CTX context;
  42. char *saltstr;
  43. unsigned char digest[16];
  44. if ( (saltstr = slapi_ch_malloc(strlen(salt)*2 + strlen(passwd) + 3))
  45. == NULL ) {
  46. return( NULL );
  47. }
  48. sprintf(saltstr,"%s%c%s%c%s",salt,89,passwd,247,salt);
  49. mta_MD5Init(&context);
  50. mta_MD5Update(&context,(unsigned char *)saltstr,strlen(saltstr));
  51. mta_MD5Final(digest,&context);
  52. ns_mta_hexify(buffer,(char*)digest,16);
  53. buffer[32] = '\0';
  54. slapi_ch_free((void**)&saltstr);
  55. return(buffer);
  56. }
  57. int
  58. ns_mta_md5_pw_cmp(const char * clear, const char *mangled)
  59. {
  60. char mta_hash[33];
  61. char mta_salt[33];
  62. char buffer[65];
  63. strncpy(mta_hash,mangled,32);
  64. strncpy(mta_salt,&mangled[32],32);
  65. mta_hash[32] = mta_salt[32] = 0;
  66. return( strcmp(mta_hash,ns_mta_hash_alg(buffer,mta_salt,clear)));
  67. }