Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 
0011 
0012 /*
0013  * Solaris has thread-safe crypt()
0014  * Linux has crypt_r(); "struct crypt_data" is more than 128K
0015  * FreeBSD needs the mutex to protect crypt()
0016  *
0017  * TODO:
0018  *     ngx_crypt_init() to init mutex
0019  */
0020 
0021 
0022 #if (NGX_CRYPT)
0023 
0024 #if (NGX_HAVE_GNU_CRYPT_R)
0025 
0026 ngx_int_t
0027 ngx_libc_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
0028 {
0029     char               *value;
0030     size_t              len;
0031     struct crypt_data   cd;
0032 
0033     cd.initialized = 0;
0034 #ifdef __GLIBC__
0035     /* work around the glibc bug */
0036     cd.current_salt[0] = ~salt[0];
0037 #endif
0038 
0039     value = crypt_r((char *) key, (char *) salt, &cd);
0040 
0041     if (value) {
0042         len = ngx_strlen(value) + 1;
0043 
0044         *encrypted = ngx_pnalloc(pool, len);
0045         if (*encrypted == NULL) {
0046             return NGX_ERROR;
0047         }
0048 
0049         ngx_memcpy(*encrypted, value, len);
0050         return NGX_OK;
0051     }
0052 
0053     ngx_log_error(NGX_LOG_CRIT, pool->log, ngx_errno, "crypt_r() failed");
0054 
0055     return NGX_ERROR;
0056 }
0057 
0058 #else
0059 
0060 ngx_int_t
0061 ngx_libc_crypt(ngx_pool_t *pool, u_char *key, u_char *salt, u_char **encrypted)
0062 {
0063     char       *value;
0064     size_t      len;
0065     ngx_err_t   err;
0066 
0067     value = crypt((char *) key, (char *) salt);
0068 
0069     if (value) {
0070         len = ngx_strlen(value) + 1;
0071 
0072         *encrypted = ngx_pnalloc(pool, len);
0073         if (*encrypted == NULL) {
0074             return NGX_ERROR;
0075         }
0076 
0077         ngx_memcpy(*encrypted, value, len);
0078         return NGX_OK;
0079     }
0080 
0081     err = ngx_errno;
0082 
0083     ngx_log_error(NGX_LOG_CRIT, pool->log, err, "crypt() failed");
0084 
0085     return NGX_ERROR;
0086 }
0087 
0088 #endif
0089 
0090 #endif /* NGX_CRYPT */