Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ngx_user.c
Go to the documentation of this file.
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) Nginx, Inc.
5  */
6 
7 
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 
11 
12 /*
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()
16  *
17  * TODO:
18  * ngx_crypt_init() to init mutex
19  */
20 
21 
22 #if (NGX_CRYPT)
23 
24 #if (NGX_HAVE_GNU_CRYPT_R)
25 
27 ngx_libc_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
28 {
29  char *value;
30  size_t len;
31  struct crypt_data cd;
32 
33  cd.initialized = 0;
34  /* work around the glibc bug */
35  cd.current_salt[0] = ~salt[0];
36 
37  value = crypt_r((char *) key, (char *) salt, &cd);
38 
39  if (value) {
40  len = ngx_strlen(value) + 1;
41 
42  *encrypted = ngx_pnalloc(pool, len);
43  if (*encrypted == NULL) {
44  return NGX_ERROR;
45  }
46 
47  ngx_memcpy(*encrypted, value, len);
48  return NGX_OK;
49  }
50 
51  ngx_log_error(NGX_LOG_CRIT, pool->log, ngx_errno, "crypt_r() failed");
52 
53  return NGX_ERROR;
54 }
55 
56 #else
57 
59 ngx_libc_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
60 {
61  char *value;
62  size_t len;
63  ngx_err_t err;
64 
65 #if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
66 
67  /* crypt() is a time consuming function, so we only try to lock */
68 
69  if (ngx_mutex_trylock(ngx_crypt_mutex) != NGX_OK) {
70  return NGX_AGAIN;
71  }
72 
73 #endif
74 
75  value = crypt((char *) key, (char *) salt);
76 
77  if (value) {
78  len = ngx_strlen(value) + 1;
79 
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;
86  }
87 
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;
93  }
94 
95  err = ngx_errno;
96 
97 #if (NGX_THREADS && NGX_NONREENTRANT_CRYPT)
98  ngx_mutex_unlock(ngx_crypt_mutex);
99 #endif
100 
101  ngx_log_error(NGX_LOG_CRIT, pool->log, err, "crypt() failed");
102 
103  return NGX_ERROR;
104 }
105 
106 #endif
107 
108 #endif /* NGX_CRYPT */