crypt_pwd.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <sys/socket.h>
  20. #if defined( hpux ) || defined (LINUX)
  21. #ifndef __USE_XOPEN
  22. #define __USE_XOPEN /* linux */
  23. #endif /* __USE_XOPEN */
  24. #include <unistd.h>
  25. #else /* hpux */
  26. #include <crypt.h>
  27. #endif /* hpux */
  28. #include "pwdstorage.h"
  29. static PRLock *cryptlock; /* Some implementations of crypt are not thread safe. ie. ours & Irix */
  30. /* characters used in crypt encoding */
  31. static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
  32. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  33. int
  34. crypt_start(Slapi_PBlock *pb __attribute__((unused)))
  35. {
  36. cryptlock = PR_NewLock();
  37. return 0;
  38. }
  39. int
  40. crypt_close(Slapi_PBlock *pb __attribute__((unused)))
  41. {
  42. PR_DestroyLock(cryptlock);
  43. return 0;
  44. }
  45. int
  46. crypt_pw_cmp( const char *userpwd, const char *dbpwd )
  47. {
  48. int rc;
  49. char *cp;
  50. PR_Lock(cryptlock);
  51. /* we use salt (first 2 chars) of encoded password in call to crypt() */
  52. cp = crypt( userpwd, dbpwd );
  53. if (cp) {
  54. rc= slapi_ct_memcmp( dbpwd, cp, strlen(dbpwd));
  55. } else {
  56. rc = -1;
  57. }
  58. PR_Unlock(cryptlock);
  59. return rc;
  60. }
  61. char *
  62. crypt_pw_enc( const char *pwd )
  63. {
  64. char *cry, salt[3];
  65. char *enc= NULL;
  66. long v;
  67. static unsigned int seed = 0;
  68. if ( seed == 0)
  69. {
  70. seed = (unsigned int)slapi_rand();
  71. }
  72. v = slapi_rand_r(&seed);
  73. salt[0] = itoa64[v & 0x3f];
  74. v >>= 6;
  75. salt[1] = itoa64[v & 0x3f];
  76. salt[2] = '\0';
  77. PR_Lock(cryptlock);
  78. cry = crypt( pwd, salt );
  79. if ( cry != NULL )
  80. {
  81. enc = slapi_ch_smprintf("%c%s%c%s", PWD_HASH_PREFIX_START, CRYPT_SCHEME_NAME, PWD_HASH_PREFIX_END, cry );
  82. }
  83. PR_Unlock(cryptlock);
  84. return( enc );
  85. }