3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
8 #include <ngx_config.h>
13 * Solaris has thread-safe crypt()
14 * Linux has crypt_r(); "struct crypt_data" is more than 128K
15 * FreeBSD needs the mutex to protect crypt()
18 * ngx_crypt_init() to init mutex
24 #if (NGX_HAVE_GNU_CRYPT_R)
27 ngx_libc_crypt(ngx_pool_t
*pool
, u_char
*key
, u_char
*salt
, u_char
**encrypted
)
34 /* work around the glibc bug */
35 cd
.current_salt
[0] = ~salt
[0];
37 value
= crypt_r((char *) key
, (char *) salt
, &cd
);
40 len
= ngx_strlen(value
) + 1;
42 *encrypted
= ngx_pnalloc(pool
, len
);
43 if (*encrypted
== NULL
) {
47 ngx_memcpy(*encrypted
, value
, len
);
51 ngx_log_error(NGX_LOG_CRIT
, pool
->log
, ngx_errno
, "crypt_r() failed");
59 ngx_libc_crypt(ngx_pool_t
*pool
, u_char
*key
, u_char
*salt
, u_char
**encrypted
)
65 #if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
67 /* crypt() is a time consuming function, so we only try to lock */
69 if (ngx_mutex_trylock(ngx_crypt_mutex
) != NGX_OK
) {
75 value
= crypt((char *) key
, (char *) salt
);
78 len
= ngx_strlen(value
) + 1;
80 *encrypted
= ngx_pnalloc(pool
, len
);
81 if (*encrypted
== NULL
) {
82 #if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
83 ngx_mutex_unlock(ngx_crypt_mutex
);
88 ngx_memcpy(*encrypted
, value
, len
);
89 #if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
90 ngx_mutex_unlock(ngx_crypt_mutex
);
97 #if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
98 ngx_mutex_unlock(ngx_crypt_mutex
);
101 ngx_log_error(NGX_LOG_CRIT
, pool
->log
, err
, "crypt() failed");
108 #endif /* NGX_CRYPT */