Update and clean Tomato RAF files
[tomato.git] / release / src / router / nginx / src / os / unix / ngx_user.c
blob7a71203cb8f9eea33264ffb8d408a31e91cc842d
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
8 #include <ngx_config.h>
9 #include <ngx_core.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()
17 * TODO:
18 * ngx_crypt_init() to init mutex
22 #if (NGX_CRYPT)
24 #if (NGX_HAVE_GNU_CRYPT_R)
26 ngx_int_t
27 ngx_libc_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
29 char *value;
30 size_t len;
31 struct crypt_data cd;
33 cd.initialized = 0;
34 /* work around the glibc bug */
35 cd.current_salt[0] = ~salt[0];
37 value = crypt_r((char *) key, (char *) salt, &cd);
39 if (value) {
40 len = ngx_strlen(value) + 1;
42 *encrypted = ngx_pnalloc(pool, len);
43 if (*encrypted == NULL) {
44 return NGX_ERROR;
47 ngx_memcpy(*encrypted, value, len);
48 return NGX_OK;
51 ngx_log_error(NGX_LOG_CRIT, pool->log, ngx_errno, "crypt_r() failed");
53 return NGX_ERROR;
56 #else
58 ngx_int_t
59 ngx_libc_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
61 char *value;
62 size_t len;
63 ngx_err_t err;
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) {
70 return NGX_AGAIN;
73 #endif
75 value = crypt((char *) key, (char *) salt);
77 if (value) {
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);
84 #endif
85 return NGX_ERROR;
88 ngx_memcpy(*encrypted, value, len);
89 #if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
90 ngx_mutex_unlock(ngx_crypt_mutex);
91 #endif
92 return NGX_OK;
95 err = ngx_errno;
97 #if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
98 ngx_mutex_unlock(ngx_crypt_mutex);
99 #endif
101 ngx_log_error(NGX_LOG_CRIT, pool->log, err, "crypt() failed");
103 return NGX_ERROR;
106 #endif
108 #endif /* NGX_CRYPT */