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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
|
/* MODULE HTAAServ.c
** SERVER SIDE ACCESS AUTHORIZATION MODULE
**
** Contains the means for checking the user access
** authorization for a file.
**
** IMPORTANT:
** Routines in this module use dynamic allocation, but free
** automatically all the memory reserved by them.
**
** Therefore the caller never has to (and never should)
** free() any object returned by these functions.
**
** Therefore also all the strings returned by this package
** are only valid until the next call to the same function
** is made. This approach is selected, because of the nature
** of access authorization: no string returned by the package
** needs to be valid longer than until the next call.
**
** This also makes it easy to plug the AA package in:
** you don't have to ponder whether to free() something
** here or is it done somewhere else (because it is always
** done somewhere else).
**
** The strings that the package needs to store are copied
** so the original strings given as parameters to AA
** functions may be freed or modified with no side effects.
**
** The AA package does not free() anything else than what
** it has itself allocated.
**
** AUTHORS:
** AL Ari Luotonen luotonen@dxcern.cern.ch
**
** HISTORY:
**
**
** BUGS:
**
**
*/
#include "HTUtils.h"
/*#include <stdio.h> included by HTUtils.h -- FM *//* FILE */
#include <string.h> /* strchr() */
#include "HTString.h"
#include "HTAccess.h" /* HTSecure */
#include "HTFile.h" /* HTLocalName */
#include "HTRules.h" /* */
#include "HTParse.h" /* URL parsing function */
#include "HTList.h" /* HTList object */
#include "HTAAUtil.h" /* AA common parts */
#include "HTAuth.h" /* Authentication */
#include "HTACL.h" /* Access Control List */
#include "HTGroup.h" /* Group handling */
#include "HTAAProt.h" /* Protection file parsing */
#include "HTAAServ.h" /* Implemented here */
#include "LYLeaks.h"
/*
** Global variables
*/
PUBLIC time_t theTime;
/*
** Module-wide global variables
*/
PRIVATE FILE * htaa_logfile = NULL; /* Log file */
PRIVATE HTAAUser *htaa_user = NULL; /* Authenticated user */
PRIVATE HTAAFailReasonType HTAAFailReason = HTAA_OK; /* AA fail reason */
/* SERVER PUBLIC HTAA_statusMessage()
** RETURN A STRING EXPLAINING ACCESS
** AUTHORIZATION FAILURE
** (Can be used in server reply status line
** with 401/403 replies.)
** ON EXIT:
** returns a string containing the error message
** corresponding to internal HTAAFailReason.
*/
PUBLIC char *HTAA_statusMessage NOARGS
{
switch (HTAAFailReason) {
/* 401 cases */
case HTAA_NO_AUTH:
return "Unauthorized -- authentication failed";
break;
case HTAA_NOT_MEMBER:
return "Unauthorized to access the document";
break;
/* 403 cases */
case HTAA_BY_RULE:
return "Forbidden -- by rule";
break;
case HTAA_IP_MASK:
return "Forbidden -- server refuses to serve to your IP address";
break;
case HTAA_NO_ACL:
case HTAA_NO_ENTRY:
return "Forbidden -- access to file is never allowed";
break;
case HTAA_SETUP_ERROR:
return "Forbidden -- server protection setup error";
break;
case HTAA_DOTDOT:
return "Forbidden -- URL containing /../ disallowed";
break;
case HTAA_HTBIN:
return "Forbidden -- /htbin feature not enabled on this server";
break;
/* 404 cases */
case HTAA_NOT_FOUND:
return "Not found -- file doesn't exist or is read protected";
break;
/* Success */
case HTAA_OK:
return "AA: Access should be ok but something went wrong";
break;
case HTAA_OK_GATEWAY:
return "AA check bypassed (gatewaying) but something went wrong";
break;
/* Others */
default:
return "Access denied -- unable to specify reason (bug)";
} /* switch */
}
PRIVATE char *status_name ARGS1(HTAAFailReasonType, reason)
{
switch (HTAAFailReason) {
/* 401 cases */
case HTAA_NO_AUTH:
return "NO-AUTHENTICATION";
break;
case HTAA_NOT_MEMBER:
return "NOT-AUTHORIZED";
break;
/* 403 cases */
case HTAA_BY_RULE:
return "FORB-RULE";
break;
case HTAA_IP_MASK:
return "FORB-IP";
break;
case HTAA_NO_ACL:
return "NO-ACL-FILE";
break;
case HTAA_NO_ENTRY:
return "NO-ACL-ENTRY";
break;
case HTAA_SETUP_ERROR:
return "SETUP-ERROR";
break;
case HTAA_DOTDOT:
return "SLASH-DOT-DOT";
break;
case HTAA_HTBIN:
return "HTBIN-OFF";
break;
/* 404 cases */
case HTAA_NOT_FOUND:
return "NOT-FOUND";
break;
/* Success */
case HTAA_OK:
return "OK";
break;
case HTAA_OK_GATEWAY:
return "OK-GATEWAY";
break;
/* Others */
default:
return "SERVER-BUG";
} /* switch */
}
/* PRIVATE check_uthorization()
** CHECK IF USER IS AUTHORIZED TO ACCESS A FILE
** ON ENTRY:
** pathname is the physical file pathname
** to access.
** method method, e.g. METHOD_GET, METHOD_PUT, ...
** scheme authentication scheme.
** scheme_specifics authentication string (or other
** scheme specific parameters, like
** Kerberos-ticket).
**
** ON EXIT:
** returns HTAA_OK on success.
** Otherwise the reason for failing.
** NOTE:
** This function does not check whether the file
** exists or not -- so the status 404 Not found
** must be returned from somewhere else (this is
** to avoid unnecessary overhead of opening the
** file twice).
*/
PRIVATE HTAAFailReasonType check_authorization ARGS4(CONST char *, pathname,
HTAAMethod, method,
HTAAScheme, scheme,
char *, scheme_specifics)
{
HTAAFailReasonType reason;
GroupDef *allowed_groups;
FILE *acl_file = NULL;
HTAAProt *prot = NULL; /* Protection mode */
htaa_user = NULL;
if (!pathname) {
if (TRACE)
fprintf(stderr, "HTAA_checkAuthorization: Forbidden by rule\n");
return HTAA_BY_RULE;
}
if (TRACE)
fprintf(stderr, "%s `%s' %s %s\n",
"HTAA_checkAuthorization: translated path:",
pathname, "method:", HTAAMethod_name(method));
/*
** Get protection setting (set up by callbacks from rule system)
** NULL, if not protected by a "protect" rule.
*/
prot = HTAA_getCurrentProtection();
/*
** Check ACL existence
*/
if (!(acl_file = HTAA_openAcl(pathname))) {
if (prot) { /* protect rule, but no ACL */
if (prot->mask_group) {
/*
** Only mask enabled, check that
*/
GroupDefList *group_def_list =
HTAA_readGroupFile(HTAssocList_lookup(prot->values,
"group"));
/*
** Authenticate if authentication info given
*/
if (scheme != HTAA_UNKNOWN && scheme != HTAA_NONE) {
htaa_user = HTAA_authenticate(scheme,
scheme_specifics,
prot);
if (TRACE)
fprintf(stderr, "Authentication returned: %s\n",
(htaa_user ? htaa_user->username
: "NOT-AUTHENTICATED"));
}
HTAA_resolveGroupReferences(prot->mask_group, group_def_list);
reason = HTAA_userAndInetInGroup(prot->mask_group,
htaa_user
? htaa_user->username : "",
HTClientHost,
NULL);
if (TRACE) {
if (reason != HTAA_OK)
fprintf(stderr, "%s %s %s %s\n",
"HTAA_checkAuthorization: access denied",
"by mask (no ACL, only Protect rule)",
"host", HTClientHost);
else
fprintf(stderr, "%s %s %s %s\n",
"HTAA_checkAuthorization: request from",
HTClientHost,
"accepted by only mask match (no ACL, only",
"Protect rule, and only mask enabled)");
}
return reason;
}
else { /* 403 Forbidden */
if (TRACE)
fprintf(stderr, "%s %s\n",
"HTAA_checkAuthorization: Protected, but",
"no mask group nor ACL -- forbidden");
return HTAA_NO_ACL;
}
}
else { /* No protect rule and no ACL => OK 200 */
if (TRACE)
fprintf(stderr, "HTAA_checkAuthorization: %s\n",
"no protect rule nor ACL -- ok\n");
return HTAA_OK;
}
}
/*
** Now we know that ACL exists
*/
if (!prot) { /* Not protected by "protect" rule */
if (TRACE)
fprintf(stderr, "HTAA_checkAuthorization: default protection\n");
prot = HTAA_getDefaultProtection(); /* Also sets current protection */
if (!prot) { /* @@ Default protection not set ?? */
if (TRACE)
fprintf(stderr, "%s %s\n",
"HTAA_checkAuthorization: default protection",
"not set (internal server error)!!");
return HTAA_SETUP_ERROR;
}
}
/*
** Now we know that document is protected and ACL exists.
** Check against ACL entry.
*/
{
GroupDefList *group_def_list =
HTAA_readGroupFile(HTAssocList_lookup(prot->values, "group"));
/*
** Authenticate now that we know protection mode
*/
if (scheme != HTAA_UNKNOWN && scheme != HTAA_NONE) {
htaa_user = HTAA_authenticate(scheme,
scheme_specifics,
prot);
if (TRACE)
fprintf(stderr, "Authentication returned: %s\n",
(htaa_user
? htaa_user->username : "NOT-AUTHENTICATED"));
}
/*
** Check mask group
*/
if (prot->mask_group) {
HTAA_resolveGroupReferences(prot->mask_group, group_def_list);
reason=HTAA_userAndInetInGroup(prot->mask_group,
htaa_user ? htaa_user->username : "",
HTClientHost,
NULL);
if (reason != HTAA_OK) {
if (TRACE)
fprintf(stderr, "%s %s %s\n",
"HTAA_checkAuthorization: access denied",
"by mask, host:", HTClientHost);
return reason;
}
else {
if (TRACE)
fprintf(stderr, "%s %s %s %s %s\n",
"HTAA_checkAuthorization: request from",
HTClientHost,
"accepted by just mask group match",
"(no ACL, only Protect rule, and only",
"mask enabled)");
/* And continue authorization checking */
}
}
/*
** Get ACL entries; get first one first, the loop others
** Remember, allowed_groups is automatically freed by
** HTAA_getAclEntry().
*/
allowed_groups = HTAA_getAclEntry(acl_file, pathname, method);
if (!allowed_groups) {
if (TRACE)
fprintf(stderr, "%s `%s' %s\n",
"No entry for file", pathname, "in ACL");
HTAA_closeAcl(acl_file);
return HTAA_NO_ENTRY; /* Forbidden -- no entry in the ACL */
}
else {
do {
HTAA_resolveGroupReferences(allowed_groups, group_def_list);
reason = HTAA_userAndInetInGroup(allowed_groups,
htaa_user
? htaa_user->username : "",
HTClientHost,
NULL);
if (reason == HTAA_OK) {
HTAA_closeAcl(acl_file);
return HTAA_OK; /* OK */
}
allowed_groups = HTAA_getAclEntry(acl_file, pathname, method);
} while (allowed_groups);
HTAA_closeAcl(acl_file);
return HTAA_NOT_MEMBER; /* Unauthorized */
}
}
}
/* PUBLIC HTAA_checkAuthorization()
** CHECK IF USER IS AUTHORIZED TO ACCESS A FILE
** ON ENTRY:
** url is the document to be accessed.
** method_name name of the method, e.g. "GET"
** scheme_name authentication scheme name.
** scheme_specifics authentication string (or other
** scheme specific parameters, like
** Kerberos-ticket).
**
** ON EXIT:
** returns status codes uniform with those of HTTP:
** 200 OK if file access is ok.
** 401 Unauthorized if user is not authorized to
** access the file.
** 403 Forbidden if there is no entry for the
** requested file in the ACL.
**
** NOTE:
** This function does not check whether the file
** exists or not -- so the status 404 Not found
** must be returned from somewhere else (this is
** to avoid unnecessary overhead of opening the
** file twice).
**
*/
PUBLIC int HTAA_checkAuthorization ARGS4(CONST char *, url,
CONST char *, method_name,
CONST char *, scheme_name,
char *, scheme_specifics)
{
static char *pathname = NULL;
char *local_copy = NULL;
HTAAMethod method = HTAAMethod_enum(method_name);
HTAAScheme scheme = HTAAScheme_enum(scheme_name);
HTAAFailReason = HTAA_OK;
/*
** Translate into absolute pathname, and
** check for "protect" and "defprot" rules.
*/
FREE(pathname); /* From previous call */
StrAllocCopy(local_copy, url);
{
char *keywords = strchr(local_copy, '?');
if (keywords)
*keywords = '\0'; /* Chop off keywords */
}
HTSimplify(local_copy); /* Remove ".." etc. */
/* HTSimplify will leave in a "/../" at the top, which can
** be a security hole.
*/
if (strstr(local_copy, "/../")) {
if (TRACE)
fprintf(stderr, "HTAA_checkAuthorization: %s (`%s')\n",
"Illegal attempt to use /../", url);
HTAAFailReason = HTAA_DOTDOT;
}
else {
pathname = HTTranslate(local_copy); /* Translate rules even if */
/* a /htbin call to set up */
/* protections. */
if (0 == strncmp(local_copy, "/htbin/", 7)) {
if (!HTBinDir)
HTAAFailReason = HTAA_HTBIN;
else {
char *end = strchr(local_copy+7, '/');
if (end)
*end = '\0';
FREE(pathname);
pathname=(char*)malloc(strlen(HTBinDir)+strlen(local_copy)+1);
strcpy(pathname, HTBinDir);
strcat(pathname, local_copy+6);
}
}
if (!pathname) { /* Forbidden by rule */
if (TRACE)
fprintf(stderr, "HTAA_checkAuthorization: Forbidden by rule\n");
HTAAFailReason = HTAA_BY_RULE;
}
else if (HTAAFailReason != HTAA_HTBIN) {
/* pathname != NULL */
char *access = HTParse(pathname, "", PARSE_ACCESS);
if (!*access || 0 == strcmp(access,"file")) { /*Local file, do AA*/
if (!HTSecure && 0 != strncmp(local_copy, "/htbin/", 7)) {
char *localname = HTLocalName(pathname);
FREE(pathname);
pathname = localname;
}
HTAAFailReason = check_authorization(pathname, method,
scheme, scheme_specifics);
}
else { /* Not local access */
HTAAFailReason = HTAA_OK_GATEWAY;
if (TRACE)
fprintf(stderr,
"HTAA_checkAuthorization: %s (%s access)\n",
"Gatewaying -- skipping authorization check",
access);
}
} /* pathname */
}
FREE(local_copy);
if (htaa_logfile) {
time(&theTime);
fprintf(htaa_logfile, "%24.24s %s %s %s %s %s\n",
ctime(&theTime),
HTClientHost ? HTClientHost : "local",
method_name,
url,
status_name(HTAAFailReason),
htaa_user && htaa_user->username
? htaa_user->username : "");
fflush(htaa_logfile); /* Actually update it on disk */
if (TRACE)
fprintf(stderr, "Log: %24.24s %s %s %s %s %s\n",
ctime(&theTime),
HTClientHost ? HTClientHost : "local",
method_name,
url,
status_name(HTAAFailReason),
htaa_user && htaa_user->username
? htaa_user->username : "");
}
switch (HTAAFailReason) {
case HTAA_NO_AUTH:
case HTAA_NOT_MEMBER:
return 401;
break;
case HTAA_BY_RULE:
case HTAA_IP_MASK:
case HTAA_NO_ACL:
case HTAA_NO_ENTRY:
case HTAA_SETUP_ERROR:
case HTAA_DOTDOT:
case HTAA_HTBIN:
return 403;
break;
case HTAA_NOT_FOUND:
return 404;
break;
case HTAA_OK:
case HTAA_OK_GATEWAY:
return 200;
break;
default:
return 500;
} /* switch */
}
/* PRIVATE compose_scheme_specifics()
** COMPOSE SCHEME-SPECIFIC PARAMETERS
** TO BE SENT ALONG WITH SERVER REPLY
** IN THE WWW-Authenticate: FIELD.
** ON ENTRY:
** scheme is the authentication scheme for which
** parameters are asked for.
** prot protection setup structure.
**
** ON EXIT:
** returns scheme specific parameters in an
** auto-freed string.
*/
PRIVATE char *compose_scheme_specifics ARGS2(HTAAScheme, scheme,
HTAAProt *, prot)
{
static char *result = NULL;
FREE(result); /* From previous call */
switch (scheme) {
case HTAA_BASIC:
{
char *realm = HTAssocList_lookup(prot->values, "server");
result = (char*)malloc(60);
sprintf(result, "realm=\"%s\"",
(realm ? realm : "UNKNOWN"));
return result;
}
break;
case HTAA_PUBKEY:
{
char *realm = HTAssocList_lookup(prot->values, "server");
result = (char*)malloc(200);
sprintf(result, "realm=\"%s\", key=\"%s\"",
(realm ? realm : "UNKNOWN"),
"PUBKEY-NOT-IMPLEMENTED");
return result;
}
break;
default:
return NULL;
}
}
/* SERVER PUBLIC HTAA_composeAuthHeaders()
** COMPOSE WWW-Authenticate: HEADER LINES
** INDICATING VALID AUTHENTICATION SCHEMES
** FOR THE REQUESTED DOCUMENT
** ON ENTRY:
** No parameters, but HTAA_checkAuthorization() must
** just before have failed because a wrong (or none)
** authentication scheme was used.
**
** ON EXIT:
** returns a buffer containing all the WWW-Authenticate:
** fields including CRLFs (this buffer is auto-freed).
** NULL, if authentication won't help in accessing
** the requested document.
**
*/
PUBLIC char *HTAA_composeAuthHeaders NOARGS
{
static char *result = NULL;
HTAAScheme scheme;
char *scheme_name;
char *scheme_params;
HTAAProt *prot = HTAA_getCurrentProtection();
if (!prot) {
if (TRACE)
fprintf(stderr, "%s %s\n",
"HTAA_composeAuthHeaders: Document not protected",
"-- why was this function called??");
return NULL;
}
else if (TRACE)
fprintf(stderr, "HTAA_composeAuthHeaders: for file `%s'\n",
prot->filename);
FREE(result); /* From previous call */
if (!(result = (char*)malloc(4096))) /* @@ */
outofmem(__FILE__, "HTAA_composeAuthHeaders");
*result = '\0';
for (scheme=0; scheme < HTAA_MAX_SCHEMES; scheme++) {
if (-1 < HTList_indexOf(prot->valid_schemes, (void*)scheme)) {
if ((scheme_name = HTAAScheme_name(scheme))) {
scheme_params = compose_scheme_specifics(scheme,prot);
strcat(result, "WWW-Authenticate: ");
strcat(result, scheme_name);
if (scheme_params) {
strcat(result, " ");
strcat(result, scheme_params);
}
strcat(result, "\r\n");
} /* scheme name found */
else if (TRACE)
fprintf(stderr, "HTAA_composeAuthHeaders: %s %d\n",
"No name found for scheme number", scheme);
} /* scheme valid for requested document */
} /* for every scheme */
return result;
}
/* PUBLIC HTAA_startLogging()
** START UP ACCESS AUTHORIZATION LOGGING
** ON ENTRY:
** fp is the open log file.
**
*/
PUBLIC void HTAA_startLogging ARGS1(FILE *, fp)
{
htaa_logfile = fp;
}
|