blob: b40a2b7211195aad9329bc022cb407124ac39e3c [file] [log] [blame]
Igor Sysoev4d656dc2005-03-22 16:02:46 +00001
2/*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7#include <ngx_config.h>
8#include <ngx_core.h>
9
10
11/*
12 * Solaris has thread-safe crypt()
13 * Linux has crypt_r(); "struct crypt_data" is more than 128K
14 * FreeBSD needs the mutex to protect crypt()
15 *
16 * TODO:
17 * ngx_crypt_init() to init mutex
18 */
19
20
21#if (NGX_CRYPT)
22
Igor Sysoev5192b362005-07-08 14:34:20 +000023#if (NGX_HAVE_GNU_CRYPT_R)
Igor Sysoev4d656dc2005-03-22 16:02:46 +000024
25ngx_int_t
26ngx_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
27{
28 char *value;
29 size_t len;
Igor Sysoev899b44e2005-05-12 14:58:06 +000030 ngx_err_t err;
Igor Sysoev4d656dc2005-03-22 16:02:46 +000031 struct crypt_data cd;
32
Igor Sysoev899b44e2005-05-12 14:58:06 +000033 ngx_set_errno(0);
34
35 cd.initialized = 0;
Igor Sysoev187b7d92005-07-14 12:51:53 +000036 /* work around the glibc bug */
37 cd.current_salt[0] = ~salt[0];
Igor Sysoev899b44e2005-05-12 14:58:06 +000038
Igor Sysoev4d656dc2005-03-22 16:02:46 +000039 value = crypt_r((char *) key, (char *) salt, &cd);
40
Igor Sysoev899b44e2005-05-12 14:58:06 +000041 err = ngx_errno;
42
43 if (err == 0) {
Igor Sysoev4d656dc2005-03-22 16:02:46 +000044 len = ngx_strlen(value);
45
46 *encrypted = ngx_palloc(pool, len);
47 if (*encrypted) {
48 ngx_memcpy(*encrypted, value, len + 1);
49 return NGX_OK;
50 }
51 }
52
Igor Sysoev899b44e2005-05-12 14:58:06 +000053 ngx_log_error(NGX_LOG_CRIT, pool->log, err, "crypt_r() failed");
54
Igor Sysoev4d656dc2005-03-22 16:02:46 +000055 return NGX_ERROR;
56}
57
58#else
59
60ngx_int_t
61ngx_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
62{
63 char *value;
64 size_t len;
Igor Sysoev899b44e2005-05-12 14:58:06 +000065 ngx_err_t err;
Igor Sysoev4d656dc2005-03-22 16:02:46 +000066
67#if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
68
69 /* crypt() is a time consuming funtion, so we only try to lock */
70
71 if (ngx_mutex_trylock(ngx_crypt_mutex) != NGX_OK) {
72 return NGX_AGAIN;
73 }
74
75#endif
76
Igor Sysoev899b44e2005-05-12 14:58:06 +000077 ngx_set_errno(0);
Igor Sysoev4d656dc2005-03-22 16:02:46 +000078
79 value = crypt((char *) key, (char *) salt);
80
81 if (value) {
82 len = ngx_strlen(value);
83
84 *encrypted = ngx_palloc(pool, len);
85 if (*encrypted) {
86 ngx_memcpy(*encrypted, value, len + 1);
Igor Sysoev4d656dc2005-03-22 16:02:46 +000087 }
Igor Sysoev899b44e2005-05-12 14:58:06 +000088
89#if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
90 ngx_mutex_unlock(ngx_crypt_mutex);
91#endif
92 return NGX_OK;
Igor Sysoev4d656dc2005-03-22 16:02:46 +000093 }
94
Igor Sysoev899b44e2005-05-12 14:58:06 +000095 err = ngx_errno;
96
Igor Sysoev4d656dc2005-03-22 16:02:46 +000097#if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
98 ngx_mutex_unlock(ngx_crypt_mutex);
99#endif
100
Igor Sysoev899b44e2005-05-12 14:58:06 +0000101 ngx_log_error(NGX_LOG_CRIT, pool->log, err, "crypt() failed");
102
103 return NGX_ERROR;
Igor Sysoev4d656dc2005-03-22 16:02:46 +0000104}
105
106#endif
107
108#endif /* NGX_CRYPT */