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
|
/* MODULE HTAAProt.c
** PROTECTION FILE PARSING MODULE
**
** AUTHORS:
** AL Ari Luotonen luotonen@dxcern.cern.ch
** MD Mark Donszelmann duns@vxdeop.cern.ch
**
** HISTORY:
** 20 Oct 93 AL Now finds uid/gid for nobody/nogroup by name
** (doesn't use default 65534 right away).
** Also understands negative uids/gids.
** 14 Nov 93 MD Added VMS compatibility
**
** BUGS:
**
**
*/
#include "HTUtils.h"
#include <string.h>
#ifndef VMS
#ifndef NOUSERS
#include <pwd.h> /* Unix password file routine: getpwnam() */
#include <grp.h> /* Unix group file routine: getgrnam() */
#endif /* NOUSERS */
#endif /* not VMS */
#include "HTAAUtil.h"
#include "HTAAFile.h"
#include "HTLex.h" /* Lexical analysor */
#include "HTAssoc.h" /* Association list */
#include "HTAAProt.h" /* Implemented here */
#include "LYLeaks.h"
/*
** Protection setup caching
*/
typedef struct {
char * prot_filename;
HTAAProt * prot;
} HTAAProtCache;
PRIVATE HTList * prot_cache = NULL; /* Protection setup cache. */
PRIVATE HTAAProt *default_prot = NULL; /* Default protection. */
PRIVATE HTAAProt *current_prot = NULL; /* Current protection mode */
/* which is set up by callbacks */
/* from the rule system when */
/* a "protect" rule is matched. */
/* PRIVATE isNumber()
** DOES A CHARACTER STRING REPRESENT A NUMBER
*/
PRIVATE BOOL isNumber ARGS1(CONST char *, s)
{
CONST char *cur = s;
if (!s || !*s) return NO;
if (*cur == '-')
cur++; /* Allow initial minus sign in a number */
while (*cur) {
if (*cur < '0' || *cur > '9')
return NO;
cur++;
}
return YES;
}
#if defined (VMS) || defined (NOUSERS)
/* PUBLIC HTAA_getUidName()
** GET THE USER ID NAME (VMS ONLY)
** ON ENTRY:
** No arguments.
**
** ON EXIT:
** returns the user name
** Default is "" (nobody).
*/
PUBLIC char * HTAA_getUidName NOARGS
{
if (current_prot && current_prot->uid_name
&& (0 != strcmp(current_prot->uid_name,"nobody")) )
return(current_prot->uid_name);
else
return("");
}
/* PUBLIC HTAA_getFileName
** GET THE FILENAME (VMS ONLY)
** ON ENTRY:
** No arguments.
**
** ON EXIT:
** returns the filename
*/
PUBLIC char * HTAA_getFileName NOARGS
{
if (current_prot && current_prot->filename)
return(current_prot->filename);
else
return("");
}
#else /* not VMS */
/* PUBLIC HTAA_getUid()
** GET THE USER ID TO CHANGE THE PROCESS UID TO
** ON ENTRY:
** No arguments.
**
** ON EXIT:
** returns the uid number to give to setuid() system call.
** Default is 65534 (nobody).
*/
PUBLIC int HTAA_getUid NOARGS
{
struct passwd *pw = NULL;
if (current_prot && current_prot->uid_name) {
if (isNumber(current_prot->uid_name)) {
if (NULL != (pw = getpwuid(atoi(current_prot->uid_name)))) {
if (TRACE) fprintf(stderr,
"%s(%s) returned (%s:%s:%d:%d:...)\n",
"HTAA_getUid: getpwuid",
current_prot->uid_name,
pw->pw_name, pw->pw_passwd,
pw->pw_uid, pw->pw_gid);
return pw->pw_uid;
}
}
else { /* User name (not a number) */
if (NULL != (pw = getpwnam(current_prot->uid_name))) {
if (TRACE) fprintf(stderr, "%s(\"%s\") %s (%s:%s:%d:%d:...)\n",
"HTAA_getUid: getpwnam",
current_prot->uid_name, "returned",
pw->pw_name, pw->pw_passwd,
pw->pw_uid, pw->pw_gid);
return pw->pw_uid;
}
}
}
/*
** Ok, then let's get uid for nobody.
*/
if (NULL != (pw = getpwnam("nobody"))) {
if (TRACE) fprintf(stderr, "HTAA_getUid: Uid for `nobody' is %d\n",
pw->pw_uid);
return pw->pw_uid;
}
/*
** Ok, then use default.
*/
return 65534; /* nobody */
}
/* PUBLIC HTAA_getGid()
** GET THE GROUP ID TO CHANGE THE PROCESS GID TO
** ON ENTRY:
** No arguments.
**
** ON EXIT:
** returns the uid number to give to setgid() system call.
** Default is 65534 (nogroup).
*/
PUBLIC int HTAA_getGid NOARGS
{
struct group *gr = NULL;
if (current_prot && current_prot->gid_name) {
if (isNumber(current_prot->gid_name)) {
if (NULL != (gr = getgrgid(atoi(current_prot->gid_name)))) {
if (TRACE) fprintf(stderr,
"%s(%s) returned (%s:%s:%d:...)\n",
"HTAA_getGid: getgrgid",
current_prot->gid_name,
gr->gr_name, gr->gr_passwd, gr->gr_gid);
return gr->gr_gid;
}
}
else { /* Group name (not number) */
if (NULL != (gr = getgrnam(current_prot->gid_name))) {
if (TRACE) fprintf(stderr,
"%s(\"%s\") returned (%s:%s:%d:...)\n",
"HTAA_getGid: getgrnam",
current_prot->gid_name,
gr->gr_name, gr->gr_passwd, gr->gr_gid);
return gr->gr_gid;
}
}
}
/*
** Ok, then let's get gid for nogroup.
*/
if (NULL != (gr = getgrnam("nogroup"))) {
if (TRACE) fprintf(stderr, "HTAA_getGid: Gid for `nogroup' is %d\n",
gr->gr_gid);
return gr->gr_gid;
}
/*
** Ok, then use default.
*/
return 65534; /* nogroup */
}
#endif /* not VMS */
/* PRIVATE HTAA_setIds()
** SET UID AND GID (AS NAMES OR NUMBERS)
** TO HTAAProt STRUCTURE
** ON ENTRY:
** prot destination.
** ids is a string like "james.www" or "1422.69" etc.
** giving uid and gid.
**
** ON EXIT:
** returns nothing.
*/
PRIVATE void HTAA_setIds ARGS2(HTAAProt *, prot,
CONST char *, ids)
{
if (ids) {
char *local_copy = NULL;
char *point;
StrAllocCopy(local_copy, ids);
point = strchr(local_copy, '.');
if (point) {
*(point++) = (char)0;
StrAllocCopy(prot->gid_name, point);
}
else {
StrAllocCopy(prot->gid_name, "nogroup");
}
StrAllocCopy(prot->uid_name, local_copy);
FREE(local_copy);
}
else {
StrAllocCopy(prot->uid_name, "nobody");
StrAllocCopy(prot->gid_name, "nogroup");
}
}
/* PRIVATE HTAA_parseProtFile()
** PARSE A PROTECTION SETUP FILE AND
** PUT THE RESULT IN A HTAAProt STRUCTURE
** ON ENTRY:
** prot destination structure.
** fp open protection file.
**
** ON EXIT:
** returns nothing.
*/
PRIVATE void HTAA_parseProtFile ARGS2(HTAAProt *, prot,
FILE *, fp)
{
if (prot && fp) {
LexItem lex_item;
char *fieldname = NULL;
while (LEX_EOF != (lex_item = lex(fp))) {
while (lex_item == LEX_REC_SEP) /* Ignore empty lines */
lex_item = lex(fp);
if (lex_item == LEX_EOF) /* End of file */
break;
if (lex_item == LEX_ALPH_STR) { /* Valid setup record */
StrAllocCopy(fieldname, HTlex_buffer);
if (LEX_FIELD_SEP != (lex_item = lex(fp)))
unlex(lex_item); /* If someone wants to use colon */
/* after field name it's ok, but */
/* not required. Here we read it.*/
if (0==strncasecomp(fieldname, "Auth", 4)) {
lex_item = lex(fp);
while (lex_item == LEX_ALPH_STR) {
HTAAScheme scheme = HTAAScheme_enum(HTlex_buffer);
if (scheme != HTAA_UNKNOWN) {
if (!prot->valid_schemes)
prot->valid_schemes = HTList_new();
HTList_addObject(prot->valid_schemes,(void*)scheme);
if (TRACE) fprintf(stderr, "%s %s `%s'\n",
"HTAA_parseProtFile: valid",
"authentication scheme:",
HTAAScheme_name(scheme));
}
else if (TRACE) fprintf(stderr, "%s %s `%s'\n",
"HTAA_parseProtFile: unknown",
"authentication scheme:",
HTlex_buffer);
if (LEX_ITEM_SEP != (lex_item = lex(fp)))
break;
/*
** Here lex_item == LEX_ITEM_SEP; after item separator
** it is ok to have one or more newlines (LEX_REC_SEP)
** and they are ignored (continuation line).
*/
do {
lex_item = lex(fp);
} while (lex_item == LEX_REC_SEP);
} /* while items in list */
} /* if "Authenticate" */
else if (0==strncasecomp(fieldname, "mask", 4)) {
prot->mask_group = HTAA_parseGroupDef(fp);
lex_item=LEX_REC_SEP; /*groupdef parser read this already*/
if (TRACE) {
if (prot->mask_group) {
fprintf(stderr,
"HTAA_parseProtFile: Mask group:\n");
HTAA_printGroupDef(prot->mask_group);
} else fprintf(stderr, "HTAA_parseProtFile: %s\n",
"Mask group syntax error");
}
} /* if "Mask" */
else { /* Just a name-value pair, put it to assoclist */
if (LEX_ALPH_STR == (lex_item = lex(fp))) {
if (!prot->values)
prot->values = HTAssocList_new();
HTAssocList_add(prot->values, fieldname, HTlex_buffer);
lex_item = lex(fp); /* Read record separator */
if (TRACE) fprintf(stderr,
"%s `%s' bound to value `%s'\n",
"HTAA_parseProtFile: Name",
fieldname, HTlex_buffer);
}
} /* else name-value pair */
} /* if valid field */
if (lex_item != LEX_EOF && lex_item != LEX_REC_SEP) {
if (TRACE) fprintf(stderr, "%s %s %d (that line ignored)\n",
"HTAA_parseProtFile: Syntax error",
"in protection setup file at line",
HTlex_line);
do {
lex_item = lex(fp);
} while (lex_item != LEX_EOF && lex_item != LEX_REC_SEP);
} /* if syntax error */
} /* while not end-of-file */
FREE(fieldname);
} /* if valid parameters */
}
/* PRIVATE HTAAProt_new()
** ALLOCATE A NEW HTAAProt STRUCTURE AND
** INITIALIZE IT FROM PROTECTION SETUP FILE
** ON ENTRY:
** cur_docname current filename after rule translations.
** prot_filename protection setup file name.
** If NULL, not an error.
** ids Uid and gid names or numbers,
** examples:
** james ( <=> james.nogroup)
** .www ( <=> nobody.www)
** james.www
** james.69
** 1422.69
** 1422.www
**
** May be NULL, defaults to nobody.nogroup.
** Should be NULL, if prot_file is NULL.
**
** ON EXIT:
** returns returns a new and initialized protection
** setup structure.
** If setup file is already read in (found
** in cache), only sets uid_name and gid
** fields, and returns that.
*/
PRIVATE HTAAProt *HTAAProt_new ARGS3(CONST char *, cur_docname,
CONST char *, prot_filename,
CONST char *, ids)
{
HTList *cur = prot_cache;
HTAAProtCache *cache_item = NULL;
HTAAProt *prot;
FILE *fp;
if (!prot_cache)
prot_cache = HTList_new();
while (NULL != (cache_item = (HTAAProtCache*)HTList_nextObject(cur))) {
if (!strcmp(cache_item->prot_filename, prot_filename))
break;
}
if (cache_item) {
prot = cache_item->prot;
if (TRACE) fprintf(stderr, "%s `%s' already in cache\n",
"HTAAProt_new: Protection file", prot_filename);
} else {
if (TRACE) fprintf(stderr,
"HTAAProt_new: Loading protection file `%s'\n",
prot_filename);
if (!(prot = (HTAAProt*)calloc(1, sizeof(HTAAProt))))
outofmem(__FILE__, "HTAAProt_new");
prot->template = NULL;
prot->filename = NULL;
prot->uid_name = NULL;
prot->gid_name = NULL;
prot->valid_schemes = HTList_new();
prot->mask_group= NULL; /* Masking disabled by defaults */
prot->values = HTAssocList_new();
if (prot_filename && NULL != (fp = fopen(prot_filename, "r"))) {
HTAA_parseProtFile(prot, fp);
fclose(fp);
if (!(cache_item =
(HTAAProtCache*)calloc(1, sizeof(HTAAProtCache))))
outofmem(__FILE__, "HTAAProt_new");
cache_item->prot = prot;
cache_item->prot_filename = NULL;
StrAllocCopy(cache_item->prot_filename, prot_filename);
HTList_addObject(prot_cache, (void*)cache_item);
}
else if (TRACE) fprintf(stderr, "HTAAProt_new: %s `%s'\n",
"Unable to open protection setup file",
(prot_filename ? prot_filename : "(null)"));
}
if (cur_docname)
StrAllocCopy(prot->filename, cur_docname);
HTAA_setIds(prot, ids);
return prot;
}
/* PUBLIC HTAA_setDefaultProtection()
** SET THE DEFAULT PROTECTION MODE
** (called by rule system when a
** "defprot" rule is matched)
** ON ENTRY:
** cur_docname is the current result of rule translations.
** prot_filename is the protection setup file (second argument
** for "defprot" rule, optional)
** ids contains user and group names separated by
** a dot, corresponding to the uid
** gid under which the server should run,
** default is "nobody.nogroup" (third argument
** for "defprot" rule, optional; can be given
** only if protection setup file is also given).
**
** ON EXIT:
** returns nothing.
** Sets the module-wide variable default_prot.
*/
PUBLIC void HTAA_setDefaultProtection ARGS3(CONST char *, cur_docname,
CONST char *, prot_filename,
CONST char *, ids)
{
default_prot = NULL; /* Not free()'d because this is in cache */
if (prot_filename) {
default_prot = HTAAProt_new(cur_docname, prot_filename, ids);
} else {
if (TRACE) fprintf(stderr, "%s %s\n",
"HTAA_setDefaultProtection: ERROR: Protection file",
"not specified (obligatory for DefProt rule)!!\n");
}
}
/* PUBLIC HTAA_setCurrentProtection()
** SET THE CURRENT PROTECTION MODE
** (called by rule system when a
** "protect" rule is matched)
** ON ENTRY:
** cur_docname is the current result of rule translations.
** prot_filename is the protection setup file (second argument
** for "protect" rule, optional)
** ids contains user and group names separated by
** a dot, corresponding to the uid
** gid under which the server should run,
** default is "nobody.nogroup" (third argument
** for "protect" rule, optional; can be given
** only if protection setup file is also given).
**
** ON EXIT:
** returns nothing.
** Sets the module-wide variable current_prot.
*/
PUBLIC void HTAA_setCurrentProtection ARGS3(CONST char *, cur_docname,
CONST char *, prot_filename,
CONST char *, ids)
{
current_prot = NULL; /* Not free()'d because this is in cache */
if (prot_filename) {
current_prot = HTAAProt_new(cur_docname, prot_filename, ids);
} else {
if (default_prot) {
current_prot = default_prot;
HTAA_setIds(current_prot, ids);
if (TRACE) fprintf(stderr, "%s %s %s\n",
"HTAA_setCurrentProtection: Protection file",
"not specified for Protect rule",
"-- using default protection");
} else {
if (TRACE) fprintf(stderr, "%s %s %s\n",
"HTAA_setCurrentProtection: ERROR: Protection",
"file not specified for Protect rule, and",
"default protection is not set!!");
}
}
}
/* PUBLIC HTAA_getCurrentProtection()
** GET CURRENT PROTECTION SETUP STRUCTURE
** (this is set up by callbacks made from
** the rule system when matching "protect"
** (and "defprot") rules)
** ON ENTRY:
** HTTranslate() must have been called before calling
** this function.
**
** ON EXIT:
** returns a HTAAProt structure representing the
** protection setup of the HTTranslate()'d file.
** This must not be free()'d.
*/
PUBLIC HTAAProt *HTAA_getCurrentProtection NOARGS
{
return current_prot;
}
/* PUBLIC HTAA_getDefaultProtection()
** GET DEFAULT PROTECTION SETUP STRUCTURE
** AND SET IT TO CURRENT PROTECTION
** (this is set up by callbacks made from
** the rule system when matching "defprot"
** rules)
** ON ENTRY:
** HTTranslate() must have been called before calling
** this function.
**
** ON EXIT:
** returns a HTAAProt structure representing the
** default protection setup of the HTTranslate()'d
** file (if HTAA_getCurrentProtection() returned
** NULL, i.e. if there is no "protect" rule
** but ACL exists, and we need to know default
** protection settings).
** This must not be free()'d.
** IMPORTANT:
** As a side-effect this tells the protection system that
** the file is in fact protected and sets the current
** protection mode to default.
*/
PUBLIC HTAAProt *HTAA_getDefaultProtection NOARGS
{
if (!current_prot) {
current_prot = default_prot;
default_prot = NULL;
}
return current_prot;
}
/* SERVER INTERNAL HTAA_clearProtections()
** CLEAR DOCUMENT PROTECTION MODE
** (ALSO DEFAULT PROTECTION)
** (called by the rule system)
** ON ENTRY:
** No arguments.
**
** ON EXIT:
** returns nothing.
** Frees the memory used by protection information.
*/
PUBLIC void HTAA_clearProtections NOARGS
{
current_prot = NULL; /* These are not freed because */
default_prot = NULL; /* they are actually in cache. */
}
|