1 const char actions_rcs[] = "$Id: actions.c,v 1.8 2001/06/29 13:19:52 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/actions.c,v $
6 * Purpose : Declares functions to work with actions files
7 * Functions declared include: FIXME
9 * Copyright : Written by and Copyright (C) 2001 the SourceForge
10 * IJBSWA team. http://ijbswa.sourceforge.net
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will
23 * be useful, but WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A
25 * PARTICULAR PURPOSE. See the GNU General Public
26 * License for more details.
28 * The GNU General Public License should be included with
29 * this file. If not, you can view it at
30 * http://www.gnu.org/copyleft/gpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 * Revision 1.8 2001/06/29 13:19:52 oes
37 * Removed logentry from cancelled commit
39 * Revision 1.7 2001/06/09 10:55:28 jongfoster
40 * Changing BUFSIZ ==> BUFFER_SIZE
42 * Revision 1.6 2001/06/07 23:04:34 jongfoster
43 * Made get_actions() static.
45 * Revision 1.5 2001/06/03 19:11:48 oes
46 * adapted to new enlist_unique arg format
48 * Revision 1.4 2001/06/01 20:03:42 jongfoster
49 * Better memory management - current_action->strings[] now
50 * contains copies of the strings, not the original.
52 * Revision 1.3 2001/06/01 18:49:17 jongfoster
53 * Replaced "list_share" with "list" - the tiny memory gain was not
54 * worth the extra complexity.
56 * Revision 1.2 2001/05/31 21:40:00 jongfoster
57 * Removing some commented out, obsolete blocks of code.
59 * Revision 1.1 2001/05/31 21:16:46 jongfoster
60 * Moved functions to process the action list into this new file.
63 *********************************************************************/
79 const char actions_h_rcs[] = ACTIONS_H_VERSION;
82 /* Turn off everything except forwarding */
83 /* This structure is used to hold user-defined aliases */
87 struct action_spec action[1];
88 struct action_alias * next;
93 * Must declare this in this file for the above structure.
95 static int get_actions (char *line,
96 struct action_alias * alias_list,
97 struct action_spec *cur_action);
100 * We need the main list of options.
102 * First, we need a way to tell between boolean, string, and multi-string
103 * options. For string and multistring options, we also need to be
104 * able to tell the difference between a "+" and a "-". (For bools,
105 * the "+"/"-" information is encoded in "add" and "mask"). So we use
106 * an enumerated type (well, the preprocessor equivalent). Here are
109 #define AV_NONE 0 /* +opt -opt */
110 #define AV_ADD_STRING 1 /* +stropt{string} */
111 #define AV_REM_STRING 2 /* -stropt */
112 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
113 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt{*} */
116 * We need a structure to hold the name, flag changes,
117 * type, and string index.
122 unsigned mask; /* a bit set to "0" = remove action */
123 unsigned add; /* a bit set to "1" = add action */
124 int takes_value; /* an AV_... constant */
125 int index; /* index into strings[] or multi[] */
129 * And with those building blocks in place, here's the array.
131 static const struct action_name action_names[] =
134 * Well actually there's no data here - it's in actionlist.h
135 * This keeps it together to make it easy to change.
137 * Here's the macros used to format it:
139 #define DEFINE_ACTION_MULTI(name,index) \
140 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
141 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
142 #define DEFINE_ACTION_STRING(name,flag,index) \
143 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
144 { "-" name, ~flag, 0, AV_REM_STRING, index },
145 #define DEFINE_ACTION_BOOL(name,flag) \
146 { "+" name, ACTION_MASK_ALL, flag }, \
147 { "-" name, ~flag, 0 },
148 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
150 #include "actionlist.h"
152 #undef DEFINE_ACTION_MULTI
153 #undef DEFINE_ACTION_STRING
154 #undef DEFINE_ACTION_BOOL
155 #undef DEFINE_ACTION_ALIAS
157 { NULL, 0, 0 } /* End marker */
161 /*********************************************************************
163 * Function : merge_actions
165 * Description : Merge two actions together.
166 * Similar to "cur_action += new_action".
169 * 1 : cur_action = Current actions, to modify.
170 * 2 : new_action = Action to add.
174 *********************************************************************/
175 void merge_actions (struct action_spec *dest,
176 const struct action_spec *src)
180 dest->mask &= src->mask;
181 dest->add &= src->mask;
182 dest->add |= src->add;
184 for (i = 0; i < ACTION_STRING_COUNT; i++)
186 char * str = src->string[i];
189 freez(dest->string[i]);
190 dest->string[i] = strdup(str);
194 for (i = 0; i < ACTION_MULTI_COUNT; i++)
196 if (src->multi_remove_all[i])
198 /* Remove everything from dest */
199 destroy_list(dest->multi_remove[i]);
200 destroy_list(dest->multi_add[i]);
201 dest->multi_remove_all[i] = 1;
202 list_duplicate(dest->multi_add[i], src->multi_add[i]);
204 else if (dest->multi_remove_all[i])
207 * dest already removes everything, so we only need to worry
210 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
211 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
215 /* No "remove all"s to worry about. */
216 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
217 list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
218 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
224 /*********************************************************************
226 * Function : copy_action
228 * Description : Copy an action_specs.
229 * Similar to "cur_action = new_action".
232 * 1 : dest = Destination of copy.
233 * 2 : src = Source for copy.
237 *********************************************************************/
238 void copy_action (struct action_spec *dest,
239 const struct action_spec *src)
243 dest->mask = src->mask;
244 dest->add = src->add;
246 for (i = 0; i < ACTION_STRING_COUNT; i++)
248 char * str = src->string[i];
249 dest->string[i] = (str ? strdup(str) : NULL);
252 for (i = 0; i < ACTION_MULTI_COUNT; i++)
254 dest->multi_remove_all[i] = src->multi_remove_all[i];
255 list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
256 list_duplicate(dest->multi_add[i], src->multi_add[i]);
261 /*********************************************************************
263 * Function : free_action
265 * Description : Free an action_specs.
268 * 1 : src = Source to free.
272 *********************************************************************/
273 void free_action (struct action_spec *src)
277 for (i = 0; i < ACTION_STRING_COUNT; i++)
279 freez(src->string[i]);
282 for (i = 0; i < ACTION_MULTI_COUNT; i++)
284 destroy_list(src->multi_remove[i]);
285 destroy_list(src->multi_add[i]);
288 memset(src, '\0', sizeof(*src));
292 /*********************************************************************
294 * Function : get_action_token
296 * Description : Parses a line for the first action.
297 * Modifies it's input array, doesn't allocate memory.
299 * *line=" +abc{def} -ghi "
306 * 1 : line = [in] The line containing the action.
307 * [out] Start of next action on line, or
308 * NULL if we reached the end of line before
309 * we found an action.
310 * 2 : name = [out] Start of action name, null
311 * terminated. NULL on EOL
312 * 3 : value = [out] Start of action value, null
313 * terminated. NULL if none or EOL.
316 * nonzero => Mismatched {} (line was trashed anyway)
318 *********************************************************************/
319 int get_action_token(char **line, char **name, char **value)
324 /* set default returns */
329 /* Eat any leading whitespace */
330 while ((*str == ' ') || (*str == '\t'))
342 /* null name, just value is prohibited */
349 while (((ch = *str) != '\0') &&
350 (ch != ' ') && (ch != '\t') && (ch != '{'))
366 /* EOL - be careful not to run off buffer */
371 /* More to parse next time. */
380 str = strchr(str, '}');
398 /*********************************************************************
400 * Function : get_actions
402 * Description : Parses a list of actions.
405 * 1 : line = The string containing the actions.
406 * Will be written to by this function.
407 * 2 : aliases = Custom alias list, or NULL for none.
408 * 3 : cur_action = Where to store the action. Caller
412 * nonzero => Error (line was trashed anyway)
414 *********************************************************************/
415 static int get_actions(char *line,
416 struct action_alias * alias_list,
417 struct action_spec *cur_action)
419 memset(cur_action, '\0', sizeof(*cur_action));
420 cur_action->mask = ACTION_MASK_ALL;
424 char * option = NULL;
427 if (get_action_token(&line, &option, &value))
434 /* handle option in 'option' */
436 /* Check for standard action name */
437 const struct action_name * action = action_names;
439 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
443 if (action->name != NULL)
446 cur_action->mask &= action->mask;
447 cur_action->add &= action->mask;
448 cur_action->add |= action->add;
450 switch (action->takes_value)
453 /* ignore any option. */
457 /* add single string. */
459 if ((value == NULL) || (*value == '\0'))
463 /* FIXME: should validate option string here */
464 freez (cur_action->string[action->index]);
465 cur_action->string[action->index] = strdup(value);
470 /* remove single string. */
472 freez (cur_action->string[action->index]);
477 /* append multi string. */
479 struct list * remove = cur_action->multi_remove[action->index];
480 struct list * add = cur_action->multi_add[action->index];
482 if ((value == NULL) || (*value == '\0'))
487 list_remove_item(remove, value);
488 enlist_unique(add, value, 0);
493 /* remove multi string. */
495 struct list * remove = cur_action->multi_remove[action->index];
496 struct list * add = cur_action->multi_add[action->index];
498 if ( (value == NULL) || (*value == '\0')
499 || ((*value == '*') && (value[1] == '\0')) )
502 * no option, or option == "*".
506 destroy_list(remove);
508 cur_action->multi_remove_all[action->index] = 1;
512 /* Valid option - remove only 1 option */
514 if ( !cur_action->multi_remove_all[action->index] )
516 /* there isn't a catch-all in the remove list already */
517 enlist_unique(remove, value, 0);
519 list_remove_item(add, value);
524 /* Shouldn't get here unless there's memory corruption. */
530 /* try user aliases. */
531 const struct action_alias * alias = alias_list;
533 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
540 merge_actions(cur_action, alias->action);
544 /* Bad action name */
555 /*********************************************************************
557 * Function : actions_to_text
559 * Description : Converts a actionsfile entry from numeric form
560 * ("mask" and "add") to text.
563 * 1 : mask = As from struct url_actions
564 * 2 : add = As from struct url_actions
566 * Returns : A string. Caller must free it.
568 *********************************************************************/
569 char * actions_to_text(struct action_spec *action)
571 unsigned mask = action->mask;
572 unsigned add = action->add;
573 char * result = strdup("");
576 /* sanity - prevents "-feature +feature" */
580 #define DEFINE_ACTION_BOOL(__name, __bit) \
581 if (!(mask & __bit)) \
583 result = strsav(result, " -" __name); \
585 else if (add & __bit) \
587 result = strsav(result, " +" __name); \
590 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
591 if (!(mask & __bit)) \
593 result = strsav(result, " -" __name); \
595 else if (add & __bit) \
597 result = strsav(result, " +" __name "{"); \
598 result = strsav(result, action->string[__index]); \
599 result = strsav(result, "}"); \
602 #define DEFINE_ACTION_MULTI(__name, __index) \
603 if (action->multi_remove_all[__index]) \
605 result = strsav(result, " -" __name "{*}"); \
609 lst = action->multi_remove[__index]->next; \
612 result = strsav(result, " -" __name "{"); \
613 result = strsav(result, lst->str); \
614 result = strsav(result, "}"); \
618 lst = action->multi_add[__index]->next; \
621 result = strsav(result, " +" __name "{"); \
622 result = strsav(result, lst->str); \
623 result = strsav(result, "}"); \
627 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
629 #include "actionlist.h"
631 #undef DEFINE_ACTION_MULTI
632 #undef DEFINE_ACTION_STRING
633 #undef DEFINE_ACTION_BOOL
634 #undef DEFINE_ACTION_ALIAS
640 /*********************************************************************
642 * Function : current_actions_to_text
644 * Description : Converts a actionsfile entry to text.
647 * 1 : action = Action
649 * Returns : A string. Caller must free it.
651 *********************************************************************/
652 char * current_action_to_text(struct current_action_spec *action)
654 unsigned flags = action->flags;
655 char * result = strdup("");
658 #define DEFINE_ACTION_BOOL(__name, __bit) \
661 result = strsav(result, " +" __name); \
665 result = strsav(result, " -" __name); \
668 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
671 result = strsav(result, " +" __name "{"); \
672 result = strsav(result, action->string[__index]); \
673 result = strsav(result, "}"); \
677 result = strsav(result, " -" __name); \
680 #define DEFINE_ACTION_MULTI(__name, __index) \
681 lst = action->multi[__index]->next; \
684 result = strsav(result, " -" __name); \
690 result = strsav(result, " +" __name "{"); \
691 result = strsav(result, lst->str); \
692 result = strsav(result, "}"); \
697 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
699 #include "actionlist.h"
701 #undef DEFINE_ACTION_MULTI
702 #undef DEFINE_ACTION_STRING
703 #undef DEFINE_ACTION_BOOL
704 #undef DEFINE_ACTION_ALIAS
710 /*********************************************************************
712 * Function : init_current_action
714 * Description : Zero out an action.
717 * 1 : dest = An uninitialized current_action_spec.
721 *********************************************************************/
722 void init_current_action (struct current_action_spec *dest)
724 memset(dest, '\0', sizeof(*dest));
725 dest->flags = ACTION_MOST_COMPATIBLE;
729 /*********************************************************************
731 * Function : merge_current_action
733 * Description : Merge two actions together.
734 * Similar to "dest += src".
735 * Differences between this and merge_actions()
736 * is that this one doesn't allocate memory for
737 * strings (so "src" better be in memory for at least
738 * as long as "dest" is, and you'd better free
739 * "dest" using "current_free_action").
740 * Also, there is no mask or remove lists in dest.
741 * (If we're applying it to a URL, we don't need them)
744 * 1 : dest = Current actions, to modify.
745 * 2 : src = Action to add.
749 *********************************************************************/
750 void merge_current_action (struct current_action_spec *dest,
751 const struct action_spec *src)
755 dest->flags &= src->mask;
756 dest->flags |= src->add;
758 for (i = 0; i < ACTION_STRING_COUNT; i++)
760 char * str = src->string[i];
763 freez(dest->string[i]);
764 dest->string[i] = strdup(str);
768 for (i = 0; i < ACTION_MULTI_COUNT; i++)
770 if (src->multi_remove_all[i])
772 /* Remove everything from dest */
773 destroy_list(dest->multi[i]);
774 list_duplicate(dest->multi[i], src->multi_add[i]);
778 list_remove_list(dest->multi[i], src->multi_remove[i]);
779 list_append_list_unique(dest->multi[i], src->multi_add[i]);
785 /*********************************************************************
787 * Function : free_current_action
789 * Description : Free a current_action_spec.
792 * 1 : src = Source to free.
796 *********************************************************************/
797 void free_current_action (struct current_action_spec *src)
801 for (i = 0; i < ACTION_STRING_COUNT; i++)
803 freez(src->string[i]);
806 for (i = 0; i < ACTION_MULTI_COUNT; i++)
808 destroy_list(src->multi[i]);
811 memset(src, '\0', sizeof(*src));
815 /*********************************************************************
817 * Function : unload_actions_file
819 * Description : Unloads an actions module.
822 * 1 : file_data = the data structure associated with the
827 *********************************************************************/
828 void unload_actions_file(void *file_data)
830 struct url_actions * next;
831 struct url_actions * cur = (struct url_actions *)file_data;
843 /*********************************************************************
845 * Function : load_actions_file
847 * Description : Read and parse a action file and add to files
851 * 1 : csp = Current client state (buffers, headers, etc...)
853 * Returns : 0 => Ok, everything else is an error.
855 *********************************************************************/
856 int load_actions_file(struct client_state *csp)
858 static struct file_list *current_actions_file = NULL;
862 struct url_actions *last_perm;
863 struct url_actions *perm;
864 char buf[BUFFER_SIZE];
865 struct file_list *fs;
866 #define MODE_START_OF_FILE 1
867 #define MODE_ACTIONS 2
869 int mode = MODE_START_OF_FILE;
870 struct action_spec cur_action[1];
871 struct action_alias * alias_list = NULL;
873 memset(cur_action, '\0', sizeof(*cur_action));
875 if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
877 /* No need to load */
880 csp->actions_list = current_actions_file;
886 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
887 csp->config->actions_file);
888 return 1; /* never get here */
891 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
892 if (last_perm == NULL)
894 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
895 csp->config->actions_file);
896 return 1; /* never get here */
899 if ((fp = fopen(csp->config->actions_file, "r")) == NULL)
901 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
902 csp->config->actions_file);
903 return 1; /* never get here */
906 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
910 /* It's a header block */
913 /* It's {{settings}} or {{alias}} */
914 int len = strlen(buf);
915 char * start = buf + 2;
916 char * end = buf + len - 1;
917 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
921 log_error(LOG_LEVEL_FATAL,
922 "can't load actions file '%s': invalid line: %s",
923 csp->config->actions_file, buf);
924 return 1; /* never get here */
927 /* Trim leading and trailing whitespace. */
935 log_error(LOG_LEVEL_FATAL,
936 "can't load actions file '%s': invalid line: {{ }}",
937 csp->config->actions_file);
938 return 1; /* never get here */
941 if (0 == strcmpic(start, "alias"))
943 /* it's an {{alias}} block */
949 /* invalid {{something}} block */
951 log_error(LOG_LEVEL_FATAL,
952 "can't load actions file '%s': invalid line: {{%s}}",
953 csp->config->actions_file, start);
954 return 1; /* never get here */
959 /* It's an actions block */
961 char actions_buf[BUFFER_SIZE];
967 /* free old action */
968 free_action(cur_action);
971 strcpy(actions_buf, buf + 1);
973 /* check we have a trailing } and then trim it */
974 end = actions_buf + strlen(actions_buf) - 1;
979 log_error(LOG_LEVEL_FATAL,
980 "can't load actions file '%s': invalid line: %s",
981 csp->config->actions_file, buf);
982 return 1; /* never get here */
986 /* trim any whitespace immediately inside {} */
989 if (*actions_buf == '\0')
993 log_error(LOG_LEVEL_FATAL,
994 "can't load actions file '%s': invalid line: %s",
995 csp->config->actions_file, buf);
996 return 1; /* never get here */
999 if (get_actions(actions_buf, alias_list, cur_action))
1003 log_error(LOG_LEVEL_FATAL,
1004 "can't load actions file '%s': invalid line: %s",
1005 csp->config->actions_file, buf);
1006 return 1; /* never get here */
1010 else if (mode == MODE_ALIAS)
1012 /* define an alias */
1013 char actions_buf[BUFFER_SIZE];
1014 struct action_alias * new_alias;
1017 char * start = strchr(buf, '=');
1020 if ((start == NULL) || (start == buf))
1022 log_error(LOG_LEVEL_FATAL,
1023 "can't load actions file '%s': invalid alias line: %s",
1024 csp->config->actions_file, buf);
1025 return 1; /* never get here */
1028 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1031 log_error(LOG_LEVEL_FATAL,
1032 "can't load actions file '%s': out of memory!",
1033 csp->config->actions_file);
1034 return 1; /* never get here */
1037 /* Eat any the whitespace before the '=' */
1039 while ((*end == ' ') || (*end == '\t'))
1042 * we already know we must have at least 1 non-ws char
1043 * at start of buf - no need to check
1049 /* Eat any the whitespace after the '=' */
1051 while ((*start == ' ') || (*start == '\t'))
1057 log_error(LOG_LEVEL_FATAL,
1058 "can't load actions file '%s': invalid alias line: %s",
1059 csp->config->actions_file, buf);
1060 return 1; /* never get here */
1063 new_alias->name = strdup(buf);
1065 strcpy(actions_buf, start);
1067 if (get_actions(actions_buf, alias_list, new_alias->action))
1071 log_error(LOG_LEVEL_FATAL,
1072 "can't load actions file '%s': invalid alias line: %s = %s",
1073 csp->config->actions_file, buf, start);
1074 return 1; /* never get here */
1078 new_alias->next = alias_list;
1079 alias_list = new_alias;
1081 else if (mode == MODE_ACTIONS)
1083 /* it's a URL pattern */
1085 /* allocate a new node */
1086 if ((perm = zalloc(sizeof(*perm))) == NULL)
1089 log_error(LOG_LEVEL_FATAL,
1090 "can't load actions file '%s': out of memory!",
1091 csp->config->actions_file);
1092 return 1; /* never get here */
1096 copy_action (perm->action, cur_action);
1098 /* Save the URL pattern */
1099 if (create_url_spec(perm->url, buf))
1102 log_error(LOG_LEVEL_FATAL,
1103 "can't load actions file '%s': cannot create URL pattern from: %s",
1104 csp->config->actions_file, buf);
1105 return 1; /* never get here */
1108 /* add it to the list */
1109 last_perm->next = perm;
1112 else if (mode == MODE_START_OF_FILE)
1114 /* oops - please have a {} line as 1st line in file. */
1116 log_error(LOG_LEVEL_FATAL,
1117 "can't load actions file '%s': first line is invalid: %s",
1118 csp->config->actions_file, buf);
1119 return 1; /* never get here */
1123 /* How did we get here? This is impossible! */
1125 log_error(LOG_LEVEL_FATAL,
1126 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1127 csp->config->actions_file, mode);
1128 return 1; /* never get here */
1134 free_action(cur_action);
1136 while (alias_list != NULL)
1138 struct action_alias * next = alias_list->next;
1139 freez((char *)alias_list->name);
1140 free_action(alias_list->action);
1145 /* the old one is now obsolete */
1146 if (current_actions_file)
1148 current_actions_file->unloader = unload_actions_file;
1151 fs->next = files->next;
1153 current_actions_file = fs;
1157 csp->actions_list = fs;