src/os/unix/ngx_user.c - nginx source code

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>


  7. #if (NGX_CRYPT)

  8. #if (NGX_HAVE_GNU_CRYPT_R)

  9. ngx_int_t
  10. ngx_libc_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
  11. {
  12.     char               *value;
  13.     size_t              len;
  14.     struct crypt_data   cd;

  15.     cd.initialized = 0;

  16.     value = crypt_r((char *) key, (char *) salt, &cd);

  17.     if (value) {
  18.         len = ngx_strlen(value) + 1;

  19.         *encrypted = ngx_pnalloc(pool, len);
  20.         if (*encrypted == NULL) {
  21.             return NGX_ERROR;
  22.         }

  23.         ngx_memcpy(*encrypted, value, len);
  24.         return NGX_OK;
  25.     }

  26.     ngx_log_error(NGX_LOG_CRIT, pool->log, ngx_errno, "crypt_r() failed");

  27.     return NGX_ERROR;
  28. }

  29. #else

  30. ngx_int_t
  31. ngx_libc_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
  32. {
  33.     char       *value;
  34.     size_t      len;
  35.     ngx_err_t   err;

  36.     value = crypt((char *) key, (char *) salt);

  37.     if (value) {
  38.         len = ngx_strlen(value) + 1;

  39.         *encrypted = ngx_pnalloc(pool, len);
  40.         if (*encrypted == NULL) {
  41.             return NGX_ERROR;
  42.         }

  43.         ngx_memcpy(*encrypted, value, len);
  44.         return NGX_OK;
  45.     }

  46.     err = ngx_errno;

  47.     ngx_log_error(NGX_LOG_CRIT, pool->log, err, "crypt() failed");

  48.     return NGX_ERROR;
  49. }

  50. #endif

  51. #endif /* NGX_CRYPT */