1 const char actions_rcs[] = "$Id: actions.c,v 1.1 2001/05/31 21:16:46 jongfoster 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.1 2001/05/31 21:16:46 jongfoster
37 * Moved functions to process the action list into this new file.
40 *********************************************************************/
56 const char actions_h_rcs[] = ACTIONS_H_VERSION;
58 /* This structure is used to hold user-defined aliases */
62 struct action_spec action[1];
63 struct action_alias * next;
68 * Must declare this in this file for the above structure.
69 * FIXME: Make this static or put structure in header.
71 extern int get_actions (char *line, struct action_alias * alias_list,
72 struct action_spec *cur_action);
75 * We need the main list of options.
77 * First, we need a way to tell between boolean, string, and multi-string
78 * options. For string and multistring options, we also need to be
79 * able to tell the difference between a "+" and a "-". (For bools,
80 * the "+"/"-" information is encoded in "add" and "mask"). So we use
81 * an enumerated type (well, the preprocessor equivalent). Here are
84 #define AV_NONE 0 /* +opt -opt */
85 #define AV_ADD_STRING 1 /* +stropt{string} */
86 #define AV_REM_STRING 2 /* -stropt */
87 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
88 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt{*} */
91 * We need a structure to hold the name, flag changes,
92 * type, and string index.
97 unsigned mask; /* a bit set to "0" = remove action */
98 unsigned add; /* a bit set to "1" = add action */
99 int takes_value; /* an AV_... constant */
100 int index; /* index into strings[] or multi[] */
104 * And with those building blocks in place, here's the array.
106 static const struct action_name action_names[] =
109 * Well actually there's no data here - it's in actionlist.h
110 * This keeps it together to make it easy to change.
112 * Here's the macros used to format it:
114 #define DEFINE_ACTION_MULTI(name,index) \
115 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
116 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
117 #define DEFINE_ACTION_STRING(name,flag,index) \
118 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
119 { "-" name, ~flag, 0, AV_REM_STRING, index },
120 #define DEFINE_ACTION_BOOL(name,flag) \
121 { "+" name, ACTION_MASK_ALL, flag }, \
122 { "-" name, ~flag, 0 },
123 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
125 #include "actionlist.h"
127 #undef DEFINE_ACTION_MULTI
128 #undef DEFINE_ACTION_STRING
129 #undef DEFINE_ACTION_BOOL
130 #undef DEFINE_ACTION_ALIAS
132 { NULL, 0, 0 } /* End marker */
136 /*********************************************************************
138 * Function : merge_actions
140 * Description : Merge two actions together.
141 * Similar to "cur_action += new_action".
144 * 1 : cur_action = Current actions, to modify.
145 * 2 : new_action = Action to add.
149 *********************************************************************/
150 void merge_actions (struct action_spec *dest,
151 const struct action_spec *src)
155 dest->mask &= src->mask;
156 dest->add &= src->mask;
157 dest->add |= src->add;
159 for (i = 0; i < ACTION_STRING_COUNT; i++)
161 char * str = src->string[i];
164 freez(dest->string[i]);
165 dest->string[i] = strdup(str);
169 for (i = 0; i < ACTION_MULTI_COUNT; i++)
171 if (src->multi_remove_all[i])
173 /* Remove everything from dest */
174 destroy_list(dest->multi_remove[i]);
175 destroy_list(dest->multi_add[i]);
176 dest->multi_remove_all[i] = 1;
177 list_duplicate(dest->multi_add[i], src->multi_add[i]);
179 else if (dest->multi_remove_all[i])
182 * dest already removes everything, so we only need to worry
185 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
186 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
190 /* No "remove all"s to worry about. */
191 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
192 list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
193 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
199 /*********************************************************************
201 * Function : copy_action
203 * Description : Copy an action_specs.
204 * Similar to "cur_action = new_action".
207 * 1 : dest = Destination of copy.
208 * 2 : src = Source for copy.
212 *********************************************************************/
213 void copy_action (struct action_spec *dest,
214 const struct action_spec *src)
218 dest->mask = src->mask;
219 dest->add = src->add;
221 for (i = 0; i < ACTION_STRING_COUNT; i++)
223 char * str = src->string[i];
224 dest->string[i] = (str ? strdup(str) : NULL);
227 for (i = 0; i < ACTION_MULTI_COUNT; i++)
229 dest->multi_remove_all[i] = src->multi_remove_all[i];
230 list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
231 list_duplicate(dest->multi_add[i], src->multi_add[i]);
236 /*********************************************************************
238 * Function : free_action
240 * Description : Free an action_specs.
243 * 1 : src = Source to free.
247 *********************************************************************/
248 void free_action (struct action_spec *src)
252 for (i = 0; i < ACTION_STRING_COUNT; i++)
254 freez(src->string[i]);
257 for (i = 0; i < ACTION_MULTI_COUNT; i++)
259 destroy_list(src->multi_remove[i]);
260 destroy_list(src->multi_add[i]);
263 memset(src, '\0', sizeof(*src));
267 /*********************************************************************
269 * Function : get_action_token
271 * Description : Parses a line for the first action.
272 * Modifies it's input array, doesn't allocate memory.
274 * *line=" +abc{def} -ghi "
281 * 1 : line = [in] The line containing the action.
282 * [out] Start of next action on line, or
283 * NULL if we reached the end of line before
284 * we found an action.
285 * 2 : name = [out] Start of action name, null
286 * terminated. NULL on EOL
287 * 3 : value = [out] Start of action value, null
288 * terminated. NULL if none or EOL.
291 * nonzero => Mismatched {} (line was trashed anyway)
293 *********************************************************************/
294 int get_action_token(char **line, char **name, char **value)
299 /* set default returns */
304 /* Eat any leading whitespace */
305 while ((*str == ' ') || (*str == '\t'))
317 /* null name, just value is prohibited */
324 while (((ch = *str) != '\0') &&
325 (ch != ' ') && (ch != '\t') && (ch != '{'))
341 /* EOL - be careful not to run off buffer */
346 /* More to parse next time. */
355 str = strchr(str, '}');
373 /*********************************************************************
375 * Function : get_actions
377 * Description : Parses a list of actions.
380 * 1 : line = The string containing the actions.
381 * Will be written to by this function.
382 * 2 : aliases = Custom alias list, or NULL for none.
383 * 3 : cur_action = Where to store the action. Caller
387 * nonzero => Error (line was trashed anyway)
389 *********************************************************************/
390 int get_actions(char *line, struct action_alias * alias_list,
391 struct action_spec *cur_action)
393 memset(cur_action, '\0', sizeof(*cur_action));
394 cur_action->mask = ACTION_MASK_ALL;
398 char * option = NULL;
401 if (get_action_token(&line, &option, &value))
408 /* handle option in 'option' */
410 /* Check for standard action name */
411 const struct action_name * action = action_names;
413 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
417 if (action->name != NULL)
420 cur_action->mask &= action->mask;
421 cur_action->add &= action->mask;
422 cur_action->add |= action->add;
424 switch (action->takes_value)
427 /* ignore any option. */
431 /* add single string. */
433 if ((value == NULL) || (*value == '\0'))
437 /* FIXME: should validate option string here */
438 freez (cur_action->string[action->index]);
439 cur_action->string[action->index] = strdup(value);
444 /* remove single string. */
446 freez (cur_action->string[action->index]);
451 /* append multi string. */
453 struct list * remove = cur_action->multi_remove[action->index];
454 struct list * add = cur_action->multi_add[action->index];
456 if ((value == NULL) || (*value == '\0'))
461 list_remove_item(remove, value);
462 enlist_unique(add, value);
467 /* remove multi string. */
469 struct list * remove = cur_action->multi_remove[action->index];
470 struct list * add = cur_action->multi_add[action->index];
472 if ( (value == NULL) || (*value == '\0')
473 || ((*value == '*') && (value[1] == '\0')) )
476 * no option, or option == "*".
480 destroy_list(remove);
482 cur_action->multi_remove_all[action->index] = 1;
486 /* Valid option - remove only 1 option */
488 if ( !cur_action->multi_remove_all[action->index] )
490 /* there isn't a catch-all in the remove list already */
491 enlist_unique(remove, value);
493 list_remove_item(add, value);
498 /* Shouldn't get here unless there's memory corruption. */
504 /* try user aliases. */
505 const struct action_alias * alias = alias_list;
507 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
514 merge_actions(cur_action, alias->action);
518 /* Bad action name */
529 /*********************************************************************
531 * Function : actions_to_text
533 * Description : Converts a actionsfile entry from numeric form
534 * ("mask" and "add") to text.
537 * 1 : mask = As from struct url_actions
538 * 2 : add = As from struct url_actions
540 * Returns : A string. Caller must free it.
542 *********************************************************************/
543 char * actions_to_text(struct action_spec *action)
545 unsigned mask = action->mask;
546 unsigned add = action->add;
547 char * result = strdup("");
550 /* sanity - prevents "-feature +feature" */
554 #define DEFINE_ACTION_BOOL(__name, __bit) \
555 if (!(mask & __bit)) \
557 result = strsav(result, " -" __name); \
559 else if (add & __bit) \
561 result = strsav(result, " +" __name); \
564 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
565 if (!(mask & __bit)) \
567 result = strsav(result, " -" __name); \
569 else if (add & __bit) \
571 result = strsav(result, " +" __name "{"); \
572 result = strsav(result, action->string[__index]); \
573 result = strsav(result, "}"); \
576 #define DEFINE_ACTION_MULTI(__name, __index) \
577 if (action->multi_remove_all[__index]) \
579 result = strsav(result, " -" __name "{*}"); \
583 lst = action->multi_remove[__index]->next; \
586 result = strsav(result, " -" __name "{"); \
587 result = strsav(result, lst->str); \
588 result = strsav(result, "}"); \
592 lst = action->multi_add[__index]->next; \
595 result = strsav(result, " +" __name "{"); \
596 result = strsav(result, lst->str); \
597 result = strsav(result, "}"); \
601 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
603 #include "actionlist.h"
605 #undef DEFINE_ACTION_MULTI
606 #undef DEFINE_ACTION_STRING
607 #undef DEFINE_ACTION_BOOL
608 #undef DEFINE_ACTION_ALIAS
614 /*********************************************************************
616 * Function : current_actions_to_text
618 * Description : Converts a actionsfile entry to text.
621 * 1 : action = Action
623 * Returns : A string. Caller must free it.
625 *********************************************************************/
626 char * current_action_to_text(struct current_action_spec *action)
628 unsigned flags = action->flags;
629 char * result = strdup("");
630 struct list_share * lst;
632 #define DEFINE_ACTION_BOOL(__name, __bit) \
635 result = strsav(result, " +" __name); \
639 result = strsav(result, " -" __name); \
642 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
645 result = strsav(result, " +" __name "{"); \
646 result = strsav(result, action->string[__index]); \
647 result = strsav(result, "}"); \
651 result = strsav(result, " -" __name); \
654 #define DEFINE_ACTION_MULTI(__name, __index) \
655 lst = action->multi[__index]->next; \
658 result = strsav(result, " -" __name); \
664 result = strsav(result, " +" __name "{"); \
665 result = strsav(result, lst->str); \
666 result = strsav(result, "}"); \
671 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
673 #include "actionlist.h"
675 #undef DEFINE_ACTION_MULTI
676 #undef DEFINE_ACTION_STRING
677 #undef DEFINE_ACTION_BOOL
678 #undef DEFINE_ACTION_ALIAS
684 /*********************************************************************
686 * Function : init_current_action
688 * Description : Zero out an action.
691 * 1 : dest = An uninitialized current_action_spec.
695 *********************************************************************/
696 void init_current_action (struct current_action_spec *dest)
698 memset(dest, '\0', sizeof(*dest));
699 dest->flags = ACTION_MOST_COMPATIBLE;
703 /*********************************************************************
705 * Function : merge_current_action
707 * Description : Merge two actions together.
708 * Similar to "dest += src".
709 * Differences between this and merge_actions()
710 * is that this one doesn't allocate memory for
711 * strings (so "src" better be in memory for at least
712 * as long as "dest" is, and you'd better free
713 * "dest" using "current_free_action").
714 * Also, there is no mask or remove lists in dest.
715 * (If we're applying it to a URL, we don't need them)
718 * 1 : dest = Current actions, to modify.
719 * 2 : src = Action to add.
723 *********************************************************************/
724 void merge_current_action (struct current_action_spec *dest,
725 const struct action_spec *src)
729 dest->flags &= src->mask;
730 dest->flags |= src->add;
732 for (i = 0; i < ACTION_STRING_COUNT; i++)
734 char * str = src->string[i];
737 dest->string[i] = str;
741 for (i = 0; i < ACTION_MULTI_COUNT; i++)
743 if (src->multi_remove_all[i])
745 /* Remove everything from dest */
746 destroy_list_share(dest->multi[i]);
747 list_duplicate_share(dest->multi[i], src->multi_add[i]);
751 list_remove_list_share(dest->multi[i], src->multi_remove[i]);
752 list_append_list_unique_share(dest->multi[i], src->multi_add[i]);
758 /*********************************************************************
760 * Function : free_current_action
762 * Description : Free a current_action_spec.
765 * 1 : src = Source to free.
769 *********************************************************************/
770 void free_current_action (struct current_action_spec *src)
774 for (i = 0; i < ACTION_MULTI_COUNT; i++)
776 destroy_list_share(src->multi[i]);
779 memset(src, '\0', sizeof(*src));
783 /*********************************************************************
785 * Function : unload_actions_file
787 * Description : Unloads an actions module.
790 * 1 : file_data = the data structure associated with the
795 *********************************************************************/
796 void unload_actions_file(void *file_data)
798 struct url_actions * next;
799 struct url_actions * cur = (struct url_actions *)file_data;
811 /*********************************************************************
813 * Function : load_actions_file
815 * Description : Read and parse a action file and add to files
819 * 1 : csp = Current client state (buffers, headers, etc...)
821 * Returns : 0 => Ok, everything else is an error.
823 *********************************************************************/
824 int load_actions_file(struct client_state *csp)
826 static struct file_list *current_actions_file = NULL;
830 struct url_actions *last_perm;
831 struct url_actions *perm;
833 struct file_list *fs;
834 #define MODE_START_OF_FILE 1
835 #define MODE_ACTIONS 2
837 int mode = MODE_START_OF_FILE;
838 struct action_spec cur_action[1];
839 struct action_alias * alias_list = NULL;
841 memset(cur_action, '\0', sizeof(*cur_action));
843 if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
845 /* No need to load */
848 csp->actions_list = current_actions_file;
854 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
855 csp->config->actions_file);
856 return 1; /* never get here */
859 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
860 if (last_perm == NULL)
862 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
863 csp->config->actions_file);
864 return 1; /* never get here */
867 if ((fp = fopen(csp->config->actions_file, "r")) == NULL)
869 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
870 csp->config->actions_file);
871 return 1; /* never get here */
874 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
878 /* It's a header block */
881 /* It's {{settings}} or {{alias}} */
882 int len = strlen(buf);
883 char * start = buf + 2;
884 char * end = buf + len - 1;
885 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
889 log_error(LOG_LEVEL_FATAL,
890 "can't load actions file '%s': invalid line: %s",
891 csp->config->actions_file, buf);
892 return 1; /* never get here */
895 /* Trim leading and trailing whitespace. */
903 log_error(LOG_LEVEL_FATAL,
904 "can't load actions file '%s': invalid line: {{ }}",
905 csp->config->actions_file);
906 return 1; /* never get here */
909 if (0 == strcmpic(start, "alias"))
911 /* it's an {{alias}} block */
917 /* invalid {{something}} block */
919 log_error(LOG_LEVEL_FATAL,
920 "can't load actions file '%s': invalid line: {{%s}}",
921 csp->config->actions_file, start);
922 return 1; /* never get here */
927 /* It's an actions block */
929 char actions_buf[BUFSIZ];
935 /* free old action */
936 free_action(cur_action);
939 strcpy(actions_buf, buf + 1);
941 /* check we have a trailing } and then trim it */
942 end = actions_buf + strlen(actions_buf) - 1;
947 log_error(LOG_LEVEL_FATAL,
948 "can't load actions file '%s': invalid line: %s",
949 csp->config->actions_file, buf);
950 return 1; /* never get here */
954 /* trim any whitespace immediately inside {} */
957 if (*actions_buf == '\0')
961 log_error(LOG_LEVEL_FATAL,
962 "can't load actions file '%s': invalid line: %s",
963 csp->config->actions_file, buf);
964 return 1; /* never get here */
967 if (get_actions(actions_buf, alias_list, cur_action))
971 log_error(LOG_LEVEL_FATAL,
972 "can't load actions file '%s': invalid line: %s",
973 csp->config->actions_file, buf);
974 return 1; /* never get here */
978 else if (mode == MODE_ALIAS)
980 /* define an alias */
981 char actions_buf[BUFSIZ];
982 struct action_alias * new_alias;
985 char * start = strchr(buf, '=');
988 if ((start == NULL) || (start == buf))
990 log_error(LOG_LEVEL_FATAL,
991 "can't load actions file '%s': invalid alias line: %s",
992 csp->config->actions_file, buf);
993 return 1; /* never get here */
996 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
999 log_error(LOG_LEVEL_FATAL,
1000 "can't load actions file '%s': out of memory!",
1001 csp->config->actions_file);
1002 return 1; /* never get here */
1005 /* Eat any the whitespace before the '=' */
1007 while ((*end == ' ') || (*end == '\t'))
1010 * we already know we must have at least 1 non-ws char
1011 * at start of buf - no need to check
1017 /* Eat any the whitespace after the '=' */
1019 while ((*start == ' ') || (*start == '\t'))
1025 log_error(LOG_LEVEL_FATAL,
1026 "can't load actions file '%s': invalid alias line: %s",
1027 csp->config->actions_file, buf);
1028 return 1; /* never get here */
1031 new_alias->name = strdup(buf);
1033 strcpy(actions_buf, start);
1035 if (get_actions(actions_buf, alias_list, new_alias->action))
1039 log_error(LOG_LEVEL_FATAL,
1040 "can't load actions file '%s': invalid alias line: %s = %s",
1041 csp->config->actions_file, buf, start);
1042 return 1; /* never get here */
1046 new_alias->next = alias_list;
1047 alias_list = new_alias;
1049 else if (mode == MODE_ACTIONS)
1051 /* it's a URL pattern */
1053 /* allocate a new node */
1054 if ((perm = zalloc(sizeof(*perm))) == NULL)
1057 log_error(LOG_LEVEL_FATAL,
1058 "can't load actions file '%s': out of memory!",
1059 csp->config->actions_file);
1060 return 1; /* never get here */
1064 copy_action (perm->action, cur_action);
1066 /* Save the URL pattern */
1067 if (create_url_spec(perm->url, buf))
1070 log_error(LOG_LEVEL_FATAL,
1071 "can't load actions file '%s': cannot create URL pattern from: %s",
1072 csp->config->actions_file, buf);
1073 return 1; /* never get here */
1076 /* add it to the list */
1077 last_perm->next = perm;
1080 else if (mode == MODE_START_OF_FILE)
1082 /* oops - please have a {} line as 1st line in file. */
1084 log_error(LOG_LEVEL_FATAL,
1085 "can't load actions file '%s': first line is invalid: %s",
1086 csp->config->actions_file, buf);
1087 return 1; /* never get here */
1091 /* How did we get here? This is impossible! */
1093 log_error(LOG_LEVEL_FATAL,
1094 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1095 csp->config->actions_file, mode);
1096 return 1; /* never get here */
1102 free_action(cur_action);
1104 while (alias_list != NULL)
1106 struct action_alias * next = alias_list->next;
1107 freez((char *)alias_list->name);
1108 free_action(alias_list->action);
1113 #ifndef SPLIT_PROXY_ARGS
1114 if (!suppress_blocklists)
1116 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
1118 #endif /* ndef SPLIT_PROXY_ARGS */
1120 /* the old one is now obsolete */
1121 if (current_actions_file)
1123 current_actions_file->unloader = unload_actions_file;
1126 fs->next = files->next;
1128 current_actions_file = fs;
1132 csp->actions_list = fs;