ns-mta-md5_pwd.c 1.6 KB

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