Debian Codegraph @

WWW/Library/Implementation/HTAAProt.c

raw
1 /*
2 * $LynxId: HTAAProt.c,v 1.36 2025/06/19 18:54:14 Eric.Lindblad Exp $
3 *
4 * MODULE HTAAProt.c
5 * PROTECTION FILE PARSING MODULE
6 *
7 * AUTHORS:
8 * AL Ari Luotonen [email protected]
9 * MD Mark Donszelmann [email protected]
10 *
11 * HISTORY:
12 * 20 Oct 93 AL Now finds uid/gid for nobody/nogroup by name
13 * (doesn't use default 65534 right away).
14 * Also understands negative uids/gids.
15 * 14 Nov 93 MD Added VMS compatibility
16 *
17 * BUGS:
18 *
19 *
20 */
21
22 #include <HTUtils.h>
23
24 #ifndef VMS
25 #ifndef NOUSERS
26 #include <pwd.h> /* Unix password file routine: getpwnam() */
27 #include <grp.h> /* Unix group file routine: getgrnam() */
28 #endif /* NOUSERS */
29 #endif /* not VMS */
30
31 #include <HTAAUtil.h>
32 #include <HTLex.h> /* Lexical analyser */
33 #include <HTAAProt.h> /* Implemented here */
34
35 #include <LYUtils.h>
36 #include <LYLeaks.h>
37
38 #define NOBODY 65534 /* -2 in 16-bit environment */
39 #define NONESUCH 65533 /* -3 in 16-bit environment */
40
41 /*
42 * Protection setup caching
43 */
44 typedef struct {
45 char *prot_filename;
46 HTAAProt *prot;
47 } HTAAProtCache;
48
49 static HTList *prot_cache = NULL; /* Protection setup cache. */
50 static HTAAProt *default_prot = NULL; /* Default protection. */
51 static HTAAProt *current_prot = NULL; /* Current protection mode */
52
53 /* which is set up by callbacks */
54 /* from the rule system when */
55 /* a "protect" rule is matched. */
56
57 #ifndef NOUSERS
58 /* static isNumber()
59 * DOES A CHARACTER STRING REPRESENT A NUMBER
60 */
61 static BOOL isNumber(const char *s)
62 {
63 const char *cur = s;
64
65 if (isEmpty(s))
66 return NO;
67
68 if (*cur == '-')
69 cur++; /* Allow initial minus sign in a number */
70
71 while (*cur) {
72 if (*cur < '0' || *cur > '9')
73 return NO;
74 cur++;
75 }
76 return YES;
77 }
78
79 /* PUBLIC HTAA_getUid()
80 * GET THE USER ID TO CHANGE THE PROCESS UID TO
81 * ON ENTRY:
82 * No arguments.
83 *
84 * ON EXIT:
85 * returns the uid number to give to setuid() system call.
86 * Default is 65534 (nobody).
87 */
88 int HTAA_getUid(void)
89 {
90 int uid;
91
92 if (current_prot && current_prot->uid_name) {
93 if (isNumber(current_prot->uid_name)) {
94 uid = atoi(current_prot->uid_name);
95 if ((*HTAA_UidToName(uid)) != '\0') {
96 return uid;
97 }
98 } else { /* User name (not a number) */
99 if ((uid = HTAA_NameToUid(current_prot->uid_name)) != NONESUCH) {
100 return uid;
101 }
102 }
103 }
104 /*
105 * Ok, then let's get uid for nobody.
106 */
107 if ((uid = HTAA_NameToUid("nobody")) != NONESUCH) {
108 return uid;
109 }
110 /*
111 * Ok, then use default.
112 */
113 return NOBODY; /* nobody */
114 }
115
116 /* PUBLIC HTAA_getGid()
117 * GET THE GROUP ID TO CHANGE THE PROCESS GID TO
118 * ON ENTRY:
119 * No arguments.
120 *
121 * ON EXIT:
122 * returns the uid number to give to setgid() system call.
123 * Default is 65534 (nogroup).
124 */
125 int HTAA_getGid(void)
126 {
127 int gid;
128
129 if (current_prot && current_prot->gid_name) {
130 if (isNumber(current_prot->gid_name)) {
131 gid = atoi(current_prot->gid_name);
132 if (*HTAA_GidToName(gid) != '\0') {
133 return gid;
134 }
135 } else { /* Group name (not number) */
136 if ((gid = HTAA_NameToGid(current_prot->gid_name)) != NONESUCH) {
137 return gid;
138 }
139 }
140 }
141 /*
142 * Ok, then let's get gid for nogroup.
143 */
144 if ((gid = HTAA_NameToGid("nogroup")) != NONESUCH) {
145 return gid;
146 }
147 /*
148 * Ok, then use default.
149 */
150 return NOBODY; /* nogroup */
151 }
152 #endif /* !NOUSERS */
153
154 /* static HTAA_setIds()
155 * SET UID AND GID (AS NAMES OR NUMBERS)
156 * TO HTAAProt STRUCTURE
157 * ON ENTRY:
158 * prot destination.
159 * ids is a string like "james.www" or "1422.69" etc.
160 * giving uid and gid.
161 *
162 * ON EXIT:
163 * returns nothing.
164 */
165 static void HTAA_setIds(HTAAProt *prot, const char *ids)
166 {
167 if (ids) {
168 char *local_copy = NULL;
169 char *point;
170
171 StrAllocCopy(local_copy, ids);
172 point = StrChr(local_copy, '.');
173 if (point) {
174 *(point++) = (char) 0;
175 StrAllocCopy(prot->gid_name, point);
176 } else {
177 StrAllocCopy(prot->gid_name, "nogroup");
178 }
179 StrAllocCopy(prot->uid_name, local_copy);
180 FREE(local_copy);
181 } else {
182 StrAllocCopy(prot->uid_name, "nobody");
183 StrAllocCopy(prot->gid_name, "nogroup");
184 }
185 }
186
187 /* static HTAA_parseProtFile()
188 * PARSE A PROTECTION SETUP FILE AND
189 * PUT THE RESULT IN A HTAAProt STRUCTURE
190 * ON ENTRY:
191 * prot destination structure.
192 * fp open protection file.
193 *
194 * ON EXIT:
195 * returns nothing.
196 */
197 static void HTAA_parseProtFile(HTAAProt *prot, FILE *fp)
198 {
199 if (prot && fp) {
200 LexItem lex_item;
201 char *fieldname = NULL;
202
203 while (LEX_EOF != (lex_item = lex(fp))) {
204
205 while (lex_item == LEX_REC_SEP) /* Ignore empty lines */
206 lex_item = lex(fp);
207
208 if (lex_item == LEX_EOF) /* End of file */
209 break;
210
211 if (lex_item == LEX_ALPH_STR) { /* Valid setup record */
212
213 StrAllocCopy(fieldname, HTlex_buffer);
214
215 if (LEX_FIELD_SEP != (lex_item = lex(fp)))
216 unlex(lex_item); /* If someone wants to use colon */
217 /* after field name it's ok, but */
218 /* not required. Here we read it. */
219
220 if (0 == strncasecomp(fieldname, "Auth", 4)) {
221 lex_item = lex(fp);
222 while (lex_item == LEX_ALPH_STR) {
223 HTAAScheme scheme = HTAAScheme_enum(HTlex_buffer);
224
225 if (scheme != HTAA_UNKNOWN) {
226 if (!prot->valid_schemes)
227 prot->valid_schemes = HTList_new();
228 HTList_addObject(prot->valid_schemes, (void *) scheme);
229 CTRACE((tfp, "%s %s `%s'\n",
230 "HTAA_parseProtFile: valid",
231 "authentication scheme:",
232 HTAAScheme_name(scheme)));
233 } else {
234 CTRACE((tfp, "%s %s `%s'\n",
235 "HTAA_parseProtFile: unknown",
236 "authentication scheme:",
237 HTlex_buffer));
238 }
239
240 if (LEX_ITEM_SEP != (lex_item = lex(fp)))
241 break;
242 /*
243 * Here lex_item == LEX_ITEM_SEP; after item separator
244 * it is ok to have one or more newlines (LEX_REC_SEP)
245 * and they are ignored (continuation line).
246 */
247 do {
248 lex_item = lex(fp);
249 } while (lex_item == LEX_REC_SEP);
250 } /* while items in list */
251 }
252 /* if "Authenticate" */
253 else if (0 == strncasecomp(fieldname, "mask", 4)) {
254 prot->mask_group = HTAA_parseGroupDef(fp);
255 lex_item = LEX_REC_SEP; /*groupdef parser read this already */
256 if (TRACE) {
257 if (prot->mask_group) {
258 fprintf(tfp,
259 "HTAA_parseProtFile: Mask group:\n");
260 HTAA_printGroupDef(prot->mask_group);
261 } else
262 fprintf(tfp,
263 "HTAA_parseProtFile: Mask group syntax error\n");
264 }
265 }
266 /* if "Mask" */
267 else { /* Just a name-value pair, put it to assoclist */
268
269 if (LEX_ALPH_STR == (lex_item = lex(fp))) {
270 if (!prot->values)
271 prot->values = HTAssocList_new();
272 HTAssocList_add(prot->values, fieldname, HTlex_buffer);
273 lex_item = lex(fp); /* Read record separator */
274 CTRACE((tfp, "%s `%s' bound to value `%s'\n",
275 "HTAA_parseProtFile: Name",
276 fieldname, HTlex_buffer));
277 }
278 } /* else name-value pair */
279
280 }
281 /* if valid field */
282 if (lex_item != LEX_EOF && lex_item != LEX_REC_SEP) {
283 CTRACE((tfp, "%s %s %d (that line ignored)\n",
284 "HTAA_parseProtFile: Syntax error",
285 "in protection setup file at line",
286 HTlex_line));
287 do {
288 lex_item = lex(fp);
289 } while (lex_item != LEX_EOF && lex_item != LEX_REC_SEP);
290 } /* if syntax error */
291 } /* while not end-of-file */
292 FREE(fieldname);
293 } /* if valid parameters */
294 }
295
296 /* static HTAAProt_new()
297 * ALLOCATE A NEW HTAAProt STRUCTURE AND
298 * INITIALIZE IT FROM PROTECTION SETUP FILE
299 * ON ENTRY:
300 * cur_docname current filename after rule translations.
301 * prot_filename protection setup file name.
302 * If NULL, not an error.
303 * ids Uid and gid names or numbers,
304 * examples:
305 * james ( <=> james.nogroup)
306 * .www ( <=> nobody.www)
307 * james.www
308 * james.69
309 * 1422.69
310 * 1422.www
311 *
312 * May be NULL, defaults to nobody.nogroup.
313 * Should be NULL, if prot_file is NULL.
314 *
315 * ON EXIT:
316 * returns returns a new and initialized protection
317 * setup structure.
318 * If setup file is already read in (found
319 * in cache), only sets uid_name and gid
320 * fields, and returns that.
321 */
322 static HTAAProt *HTAAProt_new(const char *cur_docname,
323 const char *prot_filename,
324 const char *ids)
325 {
326 HTList *cur = prot_cache;
327 HTAAProtCache *cache_item = NULL;
328 HTAAProt *prot;
329 FILE *fp;
330
331 if (!prot_cache)
332 prot_cache = HTList_new();
333
334 while (NULL != (cache_item = (HTAAProtCache *) HTList_nextObject(cur))) {
335 if (!strcmp(cache_item->prot_filename, prot_filename))
336 break;
337 }
338 if (cache_item) {
339 prot = cache_item->prot;
340 CTRACE((tfp, "%s `%s' already in cache\n",
341 "HTAAProt_new: Protection file", prot_filename));
342 } else {
343 CTRACE((tfp, "HTAAProt_new: Loading protection file `%s'\n",
344 prot_filename));
345
346 if ((prot = typecalloc(HTAAProt)) == NULL)
347 outofmem(__FILE__, "HTAAProt_new");
348
349 prot->ctemplate = NULL;
350 prot->filename = NULL;
351 prot->uid_name = NULL;
352 prot->gid_name = NULL;
353 prot->valid_schemes = HTList_new();
354 prot->mask_group = NULL; /* Masking disabled by defaults */
355 prot->values = HTAssocList_new();
356
357 if (prot_filename && NULL != (fp = fopen(prot_filename, TXT_R))) {
358 HTAA_parseProtFile(prot, fp);
359 fclose(fp);
360 if ((cache_item = typecalloc(HTAAProtCache)) == NULL)
361 outofmem(__FILE__, "HTAAProt_new");
362
363 cache_item->prot = prot;
364 cache_item->prot_filename = NULL;
365 StrAllocCopy(cache_item->prot_filename, prot_filename);
366 HTList_addObject(prot_cache, (void *) cache_item);
367 } else {
368 CTRACE((tfp, "HTAAProt_new: %s `%s'\n",
369 "Unable to open protection setup file",
370 NONNULL(prot_filename)));
371 }
372 }
373
374 if (cur_docname)
375 StrAllocCopy(prot->filename, cur_docname);
376 HTAA_setIds(prot, ids);
377
378 return prot;
379 }
380
381 /* PUBLIC HTAA_setDefaultProtection()
382 * SET THE DEFAULT PROTECTION MODE
383 * (called by rule system when a
384 * "defprot" rule is matched)
385 * ON ENTRY:
386 * cur_docname is the current result of rule translations.
387 * prot_filename is the protection setup file (second argument
388 * for "defprot" rule, optional)
389 * ids contains user and group names separated by
390 * a dot, corresponding to the uid
391 * gid under which the server should run,
392 * default is "nobody.nogroup" (third argument
393 * for "defprot" rule, optional; can be given
394 * only if protection setup file is also given).
395 *
396 * ON EXIT:
397 * returns nothing.
398 * Sets the module-wide variable default_prot.
399 */
400 void HTAA_setDefaultProtection(const char *cur_docname,
401 const char *prot_filename,
402 const char *ids)
403 {
404 default_prot = NULL; /* Not free()'d because this is in cache */
405
406 if (prot_filename) {
407 default_prot = HTAAProt_new(cur_docname, prot_filename, ids);
408 } else {
409 CTRACE((tfp, "%s %s\n",
410 "HTAA_setDefaultProtection: ERROR: Protection file",
411 "not specified (obligatory for DefProt rule)!!\n"));
412 }
413 }
414
415 /* PUBLIC HTAA_setCurrentProtection()
416 * SET THE CURRENT PROTECTION MODE
417 * (called by rule system when a
418 * "protect" rule is matched)
419 * ON ENTRY:
420 * cur_docname is the current result of rule translations.
421 * prot_filename is the protection setup file (second argument
422 * for "protect" rule, optional)
423 * ids contains user and group names separated by
424 * a dot, corresponding to the uid
425 * gid under which the server should run,
426 * default is "nobody.nogroup" (third argument
427 * for "protect" rule, optional; can be given
428 * only if protection setup file is also given).
429 *
430 * ON EXIT:
431 * returns nothing.
432 * Sets the module-wide variable current_prot.
433 */
434 void HTAA_setCurrentProtection(const char *cur_docname,
435 const char *prot_filename,
436 const char *ids)
437 {
438 current_prot = NULL; /* Not free()'d because this is in cache */
439
440 if (prot_filename) {
441 current_prot = HTAAProt_new(cur_docname, prot_filename, ids);
442 } else {
443 if (default_prot) {
444 current_prot = default_prot;
445 HTAA_setIds(current_prot, ids);
446 CTRACE((tfp, "%s %s %s\n",
447 "HTAA_setCurrentProtection: Protection file",
448 "not specified for Protect rule",
449 "-- using default protection"));
450 } else {
451 CTRACE((tfp, "%s %s %s\n",
452 "HTAA_setCurrentProtection: ERROR: Protection",
453 "file not specified for Protect rule, and",
454 "default protection is not set!!"));
455 }
456 }
457 }
458
459 /* PUBLIC HTAA_getCurrentProtection()
460 * GET CURRENT PROTECTION SETUP STRUCTURE
461 * (this is set up by callbacks made from
462 * the rule system when matching "protect"
463 * (and "defprot") rules)
464 * ON ENTRY:
465 * HTTranslate() must have been called before calling
466 * this function.
467 *
468 * ON EXIT:
469 * returns a HTAAProt structure representing the
470 * protection setup of the HTTranslate()'d file.
471 * This must not be free()'d.
472 */
473 HTAAProt *HTAA_getCurrentProtection(void)
474 {
475 return current_prot;
476 }
477
478 /* PUBLIC HTAA_getDefaultProtection()
479 * GET DEFAULT PROTECTION SETUP STRUCTURE
480 * AND SET IT TO CURRENT PROTECTION
481 * (this is set up by callbacks made from
482 * the rule system when matching "defprot"
483 * rules)
484 * ON ENTRY:
485 * HTTranslate() must have been called before calling
486 * this function.
487 *
488 * ON EXIT:
489 * returns a HTAAProt structure representing the
490 * default protection setup of the HTTranslate()'d
491 * file (if HTAA_getCurrentProtection() returned
492 * NULL, i.e., if there is no "protect" rule
493 * but ACL exists, and we need to know default
494 * protection settings).
495 * This must not be free()'d.
496 * IMPORTANT:
497 * As a side-effect this tells the protection system that
498 * the file is in fact protected and sets the current
499 * protection mode to default.
500 */
501 HTAAProt *HTAA_getDefaultProtection(void)
502 {
503 if (!current_prot) {
504 current_prot = default_prot;
505 default_prot = NULL;
506 }
507 return current_prot;
508 }
509
510 /* SERVER INTERNAL HTAA_clearProtections()
511 * CLEAR DOCUMENT PROTECTION MODE
512 * (ALSO DEFAULT PROTECTION)
513 * (called by the rule system)
514 * ON ENTRY:
515 * No arguments.
516 *
517 * ON EXIT:
518 * returns nothing.
519 * Frees the memory used by protection information.
520 */
521 void HTAA_clearProtections(void)
522 {
523 current_prot = NULL; /* These are not freed because */
524 default_prot = NULL; /* they are actually in cache. */
525 }
526
527 typedef struct {
528 char *name;
529 int user;
530 } USER_DATA;
531
532 #ifndef NOUSERS
533 static HTList *known_grp = NULL;
534 static HTList *known_pwd = NULL;
535 static BOOL uidgid_cache_inited = NO;
536 #endif
537
538 #ifdef LY_FIND_LEAKS
539 static void clear_uidgid_cache(void)
540 {
541 #ifndef NOUSERS
542 USER_DATA *data;
543
544 if (known_grp) {
545 while ((data = HTList_removeLastObject(known_grp)) != NULL) {
546 FREE(data->name);
547 FREE(data);
548 }
549 FREE(known_grp);
550 }
551 if (known_pwd) {
552 while ((data = HTList_removeLastObject(known_pwd)) != NULL) {
553 FREE(data->name);
554 FREE(data);
555 }
556 FREE(known_pwd);
557 }
558 #endif
559 }
560 #endif /* LY_FIND_LEAKS */
561
562 #ifndef NOUSERS
563 static void save_gid_info(const char *name, int user)
564 {
565 USER_DATA *data = typecalloc(USER_DATA);
566
567 if (!data)
568 return;
569 if (!known_grp) {
570 known_grp = HTList_new();
571 if (!uidgid_cache_inited) {
572 #ifdef LY_FIND_LEAKS
573 atexit(clear_uidgid_cache);
574 #endif
575 uidgid_cache_inited = YES;
576 }
577 }
578 StrAllocCopy(data->name, name);
579 data->user = user;
580 HTList_addObject(known_grp, data);
581 }
582 #endif /* NOUSERS */
583
584 #ifndef NOUSERS
585 static void save_uid_info(const char *name, int user)
586 {
587 USER_DATA *data = typecalloc(USER_DATA);
588
589 if (!data)
590 return;
591 if (!known_pwd) {
592 known_pwd = HTList_new();
593 if (!uidgid_cache_inited) {
594 #ifdef LY_FIND_LEAKS
595 atexit(clear_uidgid_cache);
596 #endif
597 uidgid_cache_inited = YES;
598 }
599 }
600 StrAllocCopy(data->name, name);
601 data->user = user;
602 HTList_addObject(known_pwd, data);
603 }
604 #endif /* !NOUSERS */
605
606 /* PUBLIC HTAA_UidToName
607 * GET THE USER NAME
608 * ON ENTRY:
609 * The user-id
610 *
611 * ON EXIT:
612 * returns the user name, or an empty string if not found.
613 */
614 const char *HTAA_UidToName(int uid GCC_UNUSED)
615 {
616 #ifndef NOUSERS
617 struct passwd *pw;
618 HTList *me = known_pwd;
619
620 while (HTList_nextObject(me)) {
621 USER_DATA *data = (USER_DATA *) (me->object);
622
623 if (uid == data->user)
624 return data->name;
625 }
626
627 if ((pw = getpwuid((uid_t) uid)) != NULL
628 && pw->pw_name != NULL) {
629 CTRACE((tfp, "%s(%d) returned (%s:%d:...)\n",
630 "HTAA_UidToName: getpwuid",
631 uid,
632 pw->pw_name, (int) pw->pw_uid));
633 save_uid_info(pw->pw_name, (int) pw->pw_uid);
634 return pw->pw_name;
635 }
636 #endif
637 return "";
638 }
639
640 /* PUBLIC HTAA_NameToUid
641 * GET THE USER ID
642 * ON ENTRY:
643 * The user-name
644 *
645 * ON EXIT:
646 * returns the user id, or NONESUCH if not found.
647 */
648 int HTAA_NameToUid(const char *name GCC_UNUSED)
649 {
650 #ifndef NOUSERS
651 struct passwd *pw;
652 HTList *me = known_pwd;
653
654 while (HTList_nextObject(me)) {
655 USER_DATA *data = (USER_DATA *) (me->object);
656
657 if (!strcmp(name, data->name))
658 return data->user;
659 }
660
661 if ((pw = getpwnam(name)) != NULL) {
662 CTRACE((tfp, "%s(%s) returned (%s:%d:...)\n",
663 "HTAA_NameToUid: getpwnam",
664 name,
665 pw->pw_name, (int) pw->pw_uid));
666 save_uid_info(pw->pw_name, (int) pw->pw_uid);
667 return (int) pw->pw_uid;
668 }
669 #endif
670 return NONESUCH;
671 }
672
673 /* PUBLIC HTAA_GidToName
674 * GET THE GROUP NAME
675 * ON ENTRY:
676 * The group-id
677 *
678 * ON EXIT:
679 * returns the group name, or an empty string if not found.
680 */
681 const char *HTAA_GidToName(int gid GCC_UNUSED)
682 {
683 #ifndef NOUSERS
684 struct group *gr;
685 HTList *me = known_grp;
686
687 while (HTList_nextObject(me)) {
688 USER_DATA *data = (USER_DATA *) (me->object);
689
690 if (gid == data->user)
691 return data->name;
692 }
693
694 if ((gr = getgrgid((gid_t) gid)) != NULL
695 && gr->gr_name != NULL) {
696 CTRACE((tfp, "%s(%d) returned (%s:%d:...)\n",
697 "HTAA_GidToName: getgrgid",
698 gid,
699 gr->gr_name, (int) gr->gr_gid));
700 save_gid_info(gr->gr_name, (int) gr->gr_gid);
701 return gr->gr_name;
702 }
703 #endif
704 return "";
705 }
706
707 /* PUBLIC HTAA_NameToGid
708 * GET THE GROUP ID
709 * ON ENTRY:
710 * The group-name
711 *
712 * ON EXIT:
713 * returns the group id, or NONESUCH if not found.
714 */
715 int HTAA_NameToGid(const char *name GCC_UNUSED)
716 {
717 #ifndef NOUSERS
718 struct group *gr;
719 HTList *me = known_grp;
720
721 while (HTList_nextObject(me)) {
722 USER_DATA *data = (USER_DATA *) (me->object);
723
724 if (!strcmp(name, data->name))
725 return data->user;
726 }
727
728 if ((gr = getgrnam(name)) != NULL) {
729 CTRACE((tfp, "%s(%s) returned (%s:%d:...)\n",
730 "HTAA_NameToGid: getgrnam",
731 name,
732 gr->gr_name, (int) gr->gr_gid));
733 save_gid_info(gr->gr_name, (int) gr->gr_gid);
734 return (int) gr->gr_gid;
735 }
736 #endif
737 return NONESUCH;
738 }