aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2016-01-24 17:41:38 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2016-01-24 17:41:38 +0200
commit8c67fbd51faa3c175fef8160e187c4ac6b123fb6 (patch)
tree5805ebc16b20aadc29798a6dadfefa8d48e298da
parenteb80640a4753350f000cbe0ee49f5a6a2bd5bc75 (diff)
downloadvmod-basicauth-8c67fbd51faa3c175fef8160e187c4ac6b123fb6.tar.gz
vmod-basicauth-8c67fbd51faa3c175fef8160e187c4ac6b123fb6.tar.bz2
Use thread-specific storage for crypt_r data.varnish-4.0
* src/vmod_basicauth.c (thread_once, thread_key): New statics. (init_function): Initialize thread_once. (getpriv): New function. (crypt_match): Take two parameters. Retrieve crypt_r data from the thread-specific storage. (plain_match, apr_match, sha1_match): Remove the pd parameter. (match): Likewise. (vmod_match): Change declaration. * src/vmod_basicauth.vcc (match): Remove PRIV_VCL.
-rw-r--r--src/vmod_basicauth.c49
-rw-r--r--src/vmod_basicauth.vcc2
2 files changed, 35 insertions, 16 deletions
diff --git a/src/vmod_basicauth.c b/src/vmod_basicauth.c
index 9dc99c3..76dbbf3 100644
--- a/src/vmod_basicauth.c
+++ b/src/vmod_basicauth.c
@@ -84,39 +84,58 @@ base64_decode(const unsigned char *input, size_t input_len,
84 } while (input_len > 0); 84 } while (input_len > 0);
85 return out - output; 85 return out - output;
86} 86}
87
88static pthread_once_t thread_once = PTHREAD_ONCE_INIT;
89static pthread_key_t thread_key;
87 90
88struct priv_data { 91struct priv_data {
89 struct crypt_data cdat; 92 struct crypt_data cdat;
90}; 93};
91 94
95static void
96make_key()
97{
98 pthread_key_create(&thread_key, free);
99}
100
101
92int 102int
93init_function(struct vmod_priv *priv, const struct VCL_conf *conf) 103init_function(struct vmod_priv *priv, const struct VCL_conf *conf)
94{ 104{
95 struct priv_data *p = malloc(sizeof(*p)); 105 pthread_once(&thread_once, make_key);
106}
107
108static struct priv_data *
109getpriv(void)
110{
111 struct priv_data *p = pthread_getspecific(thread_key);
96 112
97 p->cdat.initialized = 0; 113 if (!p) {
98 priv->priv = p; 114 p = malloc(sizeof(*p));
99 priv->free = free; 115 p->cdat.initialized = 0;
100 116 if (pthread_setspecific(thread_key, p))
101 return 0; 117 abort();
118 }
119 return p;
102} 120}
103 121
104/* Matchers */ 122/* Matchers */
105 123
106static int 124static int
107crypt_match(const char *pass, const char *hash, struct priv_data *pd) 125crypt_match(const char *pass, const char *hash)
108{ 126{
127 struct priv_data *pd = getpriv();
109 return strcmp(crypt_r(pass, hash, &pd->cdat), hash); 128 return strcmp(crypt_r(pass, hash, &pd->cdat), hash);
110} 129}
111 130
112static int 131static int
113plain_match(const char *pass, const char *hash, struct priv_data *pd) 132plain_match(const char *pass, const char *hash)
114{ 133{
115 return strcmp(pass, hash); 134 return strcmp(pass, hash);
116} 135}
117 136
118static int 137static int
119apr_match(const char *pass, const char *hash, struct priv_data *pd) 138apr_match(const char *pass, const char *hash)
120{ 139{
121 unsigned char buf[120]; 140 unsigned char buf[120];
122 return strcmp(apr_md5_encode(pass, hash, buf, sizeof(buf)), hash); 141 return strcmp(apr_md5_encode(pass, hash, buf, sizeof(buf)), hash);
@@ -125,7 +144,7 @@ apr_match(const char *pass, const char *hash, struct priv_data *pd)
125#define SHA1_DIGEST_SIZE 20 144#define SHA1_DIGEST_SIZE 20
126 145
127static int 146static int
128sha1_match(const char *pass, const char *hash, struct priv_data *pd) 147sha1_match(const char *pass, const char *hash)
129{ 148{
130 char hashbuf[SHA1_DIGEST_SIZE], resbuf[SHA1_DIGEST_SIZE]; 149 char hashbuf[SHA1_DIGEST_SIZE], resbuf[SHA1_DIGEST_SIZE];
131 int n; 150 int n;
@@ -149,7 +168,7 @@ sha1_match(const char *pass, const char *hash, struct priv_data *pd)
149struct matcher { 168struct matcher {
150 char *cm_pfx; 169 char *cm_pfx;
151 size_t cm_len; 170 size_t cm_len;
152 int (*cm_match)(const char *, const char *, struct priv_data *); 171 int (*cm_match)(const char *, const char *);
153}; 172};
154 173
155static struct matcher match_tab[] = { 174static struct matcher match_tab[] = {
@@ -162,7 +181,7 @@ static struct matcher match_tab[] = {
162}; 181};
163 182
164static int 183static int
165match(const char *pass, const char *hash, struct priv_data *pd) 184match(const char *pass, const char *hash)
166{ 185{
167 struct matcher *p; 186 struct matcher *p;
168 size_t plen = strlen(hash); 187 size_t plen = strlen(hash);
@@ -170,7 +189,7 @@ match(const char *pass, const char *hash, struct priv_data *pd)
170 for (p = match_tab; p->cm_match; p++) { 189 for (p = match_tab; p->cm_match; p++) {
171 if (p->cm_len < plen && 190 if (p->cm_len < plen &&
172 memcmp(p->cm_pfx, hash, p->cm_len) == 0 && 191 memcmp(p->cm_pfx, hash, p->cm_len) == 0 &&
173 p->cm_match(pass, hash, pd) == 0) 192 p->cm_match(pass, hash) == 0)
174 return 0; 193 return 0;
175 } 194 }
176 return 1; 195 return 1;
@@ -180,7 +199,7 @@ match(const char *pass, const char *hash, struct priv_data *pd)
180#define BASICLEN (sizeof(BASICPREF)-1) 199#define BASICLEN (sizeof(BASICPREF)-1)
181 200
182VCL_BOOL 201VCL_BOOL
183vmod_match(MOD_CTX sp, struct vmod_priv *priv, VCL_STRING file, VCL_STRING s) 202vmod_match(MOD_CTX sp, VCL_STRING file, VCL_STRING s)
184{ 203{
185 char buf[1024]; 204 char buf[1024];
186 char lbuf[1024]; 205 char lbuf[1024];
@@ -237,7 +256,7 @@ vmod_match(MOD_CTX sp, struct vmod_priv *priv, VCL_STRING file, VCL_STRING s)
237 *q++ = 0; 256 *q++ = 0;
238 if (strcmp(p, buf)) 257 if (strcmp(p, buf))
239 continue; 258 continue;
240 rc = match(pass, q, priv->priv) == 0; 259 rc = match(pass, q) == 0;
241// syslog(LOG_AUTHPRIV|LOG_DEBUG, "user=%s, rc=%d",p,rc); 260// syslog(LOG_AUTHPRIV|LOG_DEBUG, "user=%s, rc=%d",p,rc);
242 break; 261 break;
243 } 262 }
diff --git a/src/vmod_basicauth.vcc b/src/vmod_basicauth.vcc
index cda0d66..bebc1a2 100644
--- a/src/vmod_basicauth.vcc
+++ b/src/vmod_basicauth.vcc
@@ -30,7 +30,7 @@ Four kinds of password hashes are supported: Apache MD5, crypt, SHA1,
30and plaintext. 30and plaintext.
31 31
32$Init init_function 32$Init init_function
33$Function BOOL match(PRIV_VCL, STRING, STRING) 33$Function BOOL match(STRING, STRING)
34 34
35Description 35Description
36 The **match** function returns **TRUE** or **FALSE** depending on whether 36 The **match** function returns **TRUE** or **FALSE** depending on whether

Return to:

Send suggestions and report system problems to the System administrator.