crypt_pwd.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. void
  34. crypt_init()
  35. {
  36. cryptlock = PR_NewLock();
  37. }
  38. int
  39. crypt_pw_cmp( const char *userpwd, const char *dbpwd )
  40. {
  41. int rc;
  42. char *cp;
  43. PR_Lock(cryptlock);
  44. /* we use salt (first 2 chars) of encoded password in call to crypt() */
  45. cp = crypt( userpwd, dbpwd );
  46. if (cp) {
  47. rc= strcmp( dbpwd, cp);
  48. } else {
  49. rc = -1;
  50. }
  51. PR_Unlock(cryptlock);
  52. return rc;
  53. }
  54. char *
  55. crypt_pw_enc( const char *pwd )
  56. {
  57. char *cry, salt[3];
  58. char *enc= NULL;
  59. long v;
  60. static unsigned int seed = 0;
  61. if ( seed == 0)
  62. {
  63. seed = (unsigned int)slapi_rand();
  64. }
  65. v = slapi_rand_r(&seed);
  66. salt[0] = itoa64[v & 0x3f];
  67. v >>= 6;
  68. salt[1] = itoa64[v & 0x3f];
  69. salt[2] = '\0';
  70. PR_Lock(cryptlock);
  71. cry = crypt( pwd, salt );
  72. if ( cry != NULL )
  73. {
  74. enc = slapi_ch_smprintf("%c%s%c%s", PWD_HASH_PREFIX_START, CRYPT_SCHEME_NAME, PWD_HASH_PREFIX_END, cry );
  75. }
  76. PR_Unlock(cryptlock);
  77. return( enc );
  78. }