1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
#include "usuals.h"
#include "httpauthd.h"
#include "md5.h"
#include <syslog.h>
#include <err.h>
extern int g_debugging;
extern int g_daemonized;
extern pthread_mutex_t g_mutex;
/* -----------------------------------------------------------------------
* Error Handling
*/
const char kMsgDelimiter[] = ": ";
#define MAX_MSGLEN 128
void ha_messagex(int level, const char* msg, ...)
{
va_list ap;
va_start(ap, msg);
/* Either to syslog or stderr */
if(g_daemonized)
vsyslog(level, msg, ap);
else
{
if(g_debugging || level > LOG_INFO)
vwarnx(msg, ap);
}
va_end(ap);
}
void ha_message(int level, const char* msg, ...)
{
va_list ap;
va_start(ap, msg);
/* Either to syslog or stderr */
if(g_daemonized)
{
char* m = (char*)alloca(strlen(msg) + MAX_MSGLEN + sizeof(kMsgDelimiter));
if(m)
{
strcpy(m, msg);
strcat(m, kMsgDelimiter);
strerror_r(errno, m + strlen(m), MAX_MSGLEN);
}
vsyslog(LOG_ERR, m ? m : msg, ap);
}
else
{
if(g_debugging || level > LOG_INFO)
vwarnx(msg, ap);
}
va_end(ap);
}
/* -----------------------------------------------------------------------
* Header Functionality
*/
ha_header_t* ha_findheader(ha_request_t* req, const char* name)
{
int i;
for(i = 0; i < MAX_HEADERS; i++)
{
if(req->headers[i].name)
{
if(strcasecmp(req->headers[i].name, name) == 0)
return &(req->headers[i]);
}
}
return NULL;
}
const char* ha_getheader(ha_request_t* req, const char* name, const char* prefix)
{
int i, l;
for(i = 0; i < MAX_HEADERS; i++)
{
if(req->headers[i].name)
{
if(strcasecmp(req->headers[i].name, name) == 0)
{
if(!prefix)
return req->headers[i].data;
l = strlen(prefix)
if(strncasecmp(prefix, req->headers[i].value, l) == 0)
return req->headers[i].data + l;
}
}
}
return NULL;
}
void ha_addheader(ha_response_t* resp, const char* name, const char* data)
{
int i = 0;
for(i = 0; i < MAX_HEADERS; i++)
{
if(!(resp->headers[i].name))
{
resp->headers[i].name = name;
resp->headers[i].data = data;
return;
}
}
ha_messagex(LOG_ERR, "too many headers in response. discarding '%s'", name);
}
/* -----------------------------------------------------------------------
* Locking
*/
void ha_lock(pthread_mutex_t* mtx)
{
int r = pthread_mutex_lock(mtx ? mtx : &g_mutex);
if(r != 0)
{
errno = r;
ha_message(LOG_CRIT, "threading problem. couldn't lock mutex");
}
}
void ha_unlock(pthread_mutex_t* mtx)
{
int r = pthread_mutex_unlock(mtx ? mtx : &g_mutex);
if(r != 0)
{
errno = r;
ha_message(LOG_CRIT, "threading problem. couldn't unlock mutex");
}
}
/* -----------------------------------------------------------------------
* Configuration
*/
int ha_confbool(const char* name, const char* conf, int* value)
{
if(value == NULL ||
value[0] == 0 ||
strcasecmp(conf, "0") == 0 ||
strcasecmp(conf, "no") == 0 ||
strcasecmp(conf, "false") == 0 ||
strcasecmp(conf, "f") == 0 ||
strcasecmp(conf, "off"))
{
*value = 0;
return HA_OK;
}
if(strcasecmp(conf, "1") == 0 ||
strcasecmp(conf, "yes") == 0 ||
strcasecmp(conf, "true") == 0 ||
strcasecmp(conf, "t") == 0 ||
strcasecmp(conf, "on"))
{
*value = 1;
return HA_OK;
}
ha_messagex(LOG_ERR, "invalid configuration value '%s': must be 'on' or 'off'.", name);
return HA_ERROR;
}
int ha_confint(const char* name, const char* conf, int min, int max, int* value)
{
const char* p;
errno = 0;
*value = strtol(conf, &p, 10);
if(p != (name + strlen(name)) || errno == ERANGE ||
(*value < min) || (*value > max))
{
ha_messagex(LOG_ERR, "invalid configuration value '%s': must be a number between %d and %d", name, min, max);
return HA_ERROR;
}
return HA_OK;
}
/* -----------------------------------------------------------------------
* Hash Stuff
*/
void ha_md5string(const char* data, unsigned char* hash)
{
struct MD5Context md5;
MD5Init(&md5);
MD5Update(&md5, data, strlen(data));
MD5Final(hash, &md5);
}
/* -----------------------------------------------------------------------
* Client Authentication Functions
*/
int ha_parsebasic(char* header, ha_buffer_t* buf, ha_basic_header_t* rec)
{
char* t;
memset(rec, 0, sizeof(*rec));
/* Trim the white space */
while(*header && isspace(*header))
header++;
/*
* Authorization header is in this format:
*
* "Basic " B64(user ":" password)
*/
ha_bufnext(buf);
ha_bufdec64(buf, header);
header = ha_bufdata(buf);
if(!header)
return HA_ERROR;
/* We have a cache key at this point so hash it */
ha_md5string(header, rec->key);
/* Parse the user. We need it in any case */
t = strchr(header, ':');
if(t != NULL)
{
/* Break the string in half */
*t = 0;
rec->user = header;
rec->password = t + 1;
}
return HA_OK;
}
/*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
int ha_parsedigest(char* header, ha_buffer_t* buf, ha_digest_header_t* rec)
{
/*
* This function destroys the contents of header by
* terminating strings in it all over the place.
*/
char next;
char* key;
char* value;
ha_bufnext(buf);
ha_bufcat(buf, header, NULL);
header = ha_bufdata(buf);
if(!header)
return HA_ERROR;
memset(rec, 0, sizeof(rec));
while(header[0])
{
/* find key */
while(header[0] && isspace(header[0]))
header++;
key = header;
while(header[0] && header[0] != '=' && header[0] != ',' &&
!isspace(header[0]))
header++;
/* null terminate and move on */
next = header[0];
header[0] = '\0';
header++;
if(!next)
break;
if(isspace(t))
{
while(header[0] && isspace(header[0]))
header++;
next = header[0];
}
/* find value */
if(next == '=')
{
header++;
while(header[0] && isspace(header[0]))
header++;
vv = 0;
if(header[0] == '\"') /* quoted string */
{
header++;
value = header;
while(header[0] && header[0] != '\"')
header++;
header[0] = 0;
header++;
}
else /* token */
{
value = header;
while(header[0] && header[0] != ',' && !isspace(header[0]))
header++;
header[0] = 0;
header++;
}
while(header[0] && header[0] != ',')
header++;
if(header[0])
header++;
if(!strcasecmp(key, "username"))
rec->username = value;
else if(!strcasecmp(key, "realm"))
rec->realm = value;
else if (!strcasecmp(key, "nonce"))
rec->nonce = value;
else if (!strcasecmp(key, "uri"))
rec->uri = value;
else if (!strcasecmp(key, "response"))
rec->digest = value;
else if (!strcasecmp(key, "algorithm"))
rec->algorithm = value;
else if (!strcasecmp(key, "cnonce"))
rec->cnonce = value;
else if (!strcasecmp(key, "opaque"))
rec->opaque = value;
else if (!strcasecmp(key, "qop"))
rec->qop = value;
else if (!strcasecmp(key, "nc"))
rec->nc = value;
}
}
if(rec->nonce)
ha_md5string(rec->nonce, rec->key);
return HA_OK;
}
int ha_digestcheck(const char* realm, const char* method, const char* uri,
ha_buffer_t* buf, ha_digest_header_t* dg, ha_digest_record_t* rec)
{
unsigned char hash[MD5_LEN];
struct MD5Context md5;
const char* digest;
const char* t;
/* Check for digest */
if(!dg->digest || !dg->digest[0])
{
ha_messagex(LOG_WARNING, "digest response missing digest");
return HA_FALSE;
}
/* Username */
if(!dg->username || !dg->username[0] ||
ha_md5strcmp(dg->username, rec->userhash) != 0)
{
ha_messagex(LOG_WARNING, "digest response missing username");
return HA_FALSE;
}
/* The realm */
if(!dg->realm || strcmp(dg->realm, realm) != 0)
{
ha_messagex(LOG_WARNING, "digest response contains invalid realm: '%s'",
dg->realm ? dg->realm : "");
return HA_FALSE;
}
/* Components in the new RFC */
if(dg->qop)
{
/*
* We only support 'auth' qop. We don't have access to the data
* and wouldn't be able to support anything else.
*/
if(strcmp(dg->qop, "auth") != 0)
{
ha_messagex(LOG_WARNING, "digest response contains unknown or unsupported qop: '%s'",
dg->qop ? dg->qop : "");
return HA_FALSE;
}
/* The nonce */
if(!dg->cnonce || !dg->cnonce[0])
{
ha_messagex(LOG_WARNING, "digest response is missing cnonce value");
return HA_FALSE;
}
/* The nonce count */
if(!dg->nc || !dg->nc[0])
{
ha_messagex(LOG_WARNING, "digest response is missing nc value");
return HA_FALSE;
}
/* Validate the nc */
else
{
char* e;
long nc = strtol(dg->nc, &e, 10);
if(e != (dg->nc + strlen(e)) || nc != rec->nc)
{
ha_messagex(LOG_WARNING, "digest response has invalid nc value: %s",
dg->nc);
return HA_FALSE;
}
}
}
/* The algorithm */
if(dg->algorithm && strcasecmp(dg->algorithm, "MD5") != 0)
{
ha_messagex(LOG_WARNING, "digest response contains unknown or unsupported algorithm: '%s'",
dg->algorithm ? dg->algorithm : "");
return HA_FALSE;
}
/* Request URI */
if(!dg->uri)
{
ha_messagex(LOG_WARNING, "digest response is missing uri");
return HA_FALSE;
}
if(strcmp(dg->uri, uri) != 0)
{
ha_uri_t d_uri;
ha_uri_t s_uri;
if(ha_uriparse(dg->uri, &d_uri) == HA_ERROR)
{
ha_messagex(LOG_WARNING, "digest response constains invalid uri: %s", dg->uri);
return HA_FALSE;
}
if(ha_uriparse(uri, &s_uri) == HA_ERROR)
{
ha_messagex(LOG_ERR, "server sent us an invalid uri");
return HA_ERROR;
}
if(ha_uricmp(&d_uri, &s_uri) != 0)
{
ha_messagex(LOG_WARNING, "digest response contains wrong uri: %s "
"(should be %s)", dg->uri, uri);
return HA_ERROR;
}
}
/*
* nonce: should have already been validated by the caller who
* found this nice rec structure to pass us.
*
* opaque: We also don't use opaque. The caller should have validated it
* if it's used there.
*/
/*
* Now we validate the digest response
*/
/* Encode ha1 */
ha_bufnext(buf);
ha_bufenchex(buf, rec->ha1, MD5_LEN);
if((t = ha_bufdata(buf)) == NULL)
return HA_ERROR;
/* Encode ha2 */
MD5Init(&md5);
MD5Update(&md5, method, strlen(method));
MD5Update(&md5, ":", 1);
MD5Update(&md5, dg->uri, strlen(dg->uri));
MD5Final(hash, &md5);
ha_bufnext(buf);
ha_bufenchex(buf, hash, MD5_LEN);
if(!ha_bufdata(buf))
return HA_ERROR;
/* Old style digest (RFC 2069) */
if(!dg->qop)
{
MD5Init(&md5);
MD5Update(&md5, t, MD5_LEN * 2); /* ha1 */
MD5Update(&md5, ":", 1);
MD5Update(&md5, dg->nonce, strlen(dg->nonce)); /* nonce */
MD5Update(&md5, ":", 1);
MD5Update(&md5, ha_bufdata(buf), MD5_LEN * 2); /* ha1 */
MD5Final(hash, &md5);
}
/* New style 'auth' digest (RFC 2617) */
else
{
MD5Init(&md5);
MD5Update(&md5, t, MD5_LEN * 2); /* ha1 */
MD5Update(&md5, ":", 1);
MD5Update(&md5, dg->nonce, strlen(dg->nonce)); /* nonce */
MD5Update(&md5, ":", 1);
MD5Update(&md5, dg->nc, strlen(dg->nc)); /* nc */
MD5Update(&md5, ":", 1);
MD5Update(&md5, dg->cnonce, strlen(dg->cnonce)); /* cnonce */
MD5Update(&md5, ":", 1);
MD5Update(&md5, dg->qop, strlen(dg->qop)); /* qop */
MD5Update(&md5, ":", 1);
MD5Update(&md5, ha_bufdata(buf), MD5_LEN * 2); /* ha2 */
MD5Final(hash, &md5);
}
/* Encode the digest */
ha_bufnext(buf);
ha_bufenchex(buf, hash, MD5_LEN);
if((digest = ha_bufdata(buf)) == NULL)
return HA_ERROR;
if(strcasecmp(dg->digest, digest) != 0)
{
ha_messagex(LOG_WARNING, "digest authentication failed for user: %s", dg->username);
return HA_FALSE;
}
return HA_OK;
}
const char* ha_digestrespond(ha_buffer_t* buf, ha_digest_header_t* dg,
ha_digest_rec_t* rec)
{
unsigned char hash[MD5_LEN];
struct MD5Context md5;
const char* t;
const char* digest;
/* Encode ha1 */
ha_bufnext(buf);
ha_bufenchex(buf, rec->ha1, MD5_LEN);
if((t = ha_bufdata(buf)) == NULL)
return HA_ERROR;
/* Encode ha2 */
MD5Init(&md5);
MD5Update(&md5, method, strlen(method));
MD5Update(&md5, ":", 1);
MD5Update(&md5, dg->uri, strlen(dg->uri));
MD5Final(hash, &md5);
ha_bufnext(buf);
ha_bufenchex(buf, hash, MD5_LEN);
if(!ha_bufdata(buf))
return HA_ERROR;
}
char* ha_uriformat(const ha_uri_t* uri, ha_buffer_t* buf);
int ha_uriparse(const char* str, ha_uri_t* uri);
|