1 const char actions_rcs[] = "$Id: actions.c,v 1.2 2001/05/31 21:40:00 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.2 2001/05/31 21:40:00 jongfoster
37 * Removing some commented out, obsolete blocks of code.
39 * Revision 1.1 2001/05/31 21:16:46 jongfoster
40 * Moved functions to process the action list into this new file.
43 *********************************************************************/
59 const char actions_h_rcs[] = ACTIONS_H_VERSION;
61 /* This structure is used to hold user-defined aliases */
65 struct action_spec action[1];
66 struct action_alias * next;
71 * Must declare this in this file for the above structure.
72 * FIXME: Make this static or put structure in header.
74 extern int get_actions (char *line, struct action_alias * alias_list,
75 struct action_spec *cur_action);
78 * We need the main list of options.
80 * First, we need a way to tell between boolean, string, and multi-string
81 * options. For string and multistring options, we also need to be
82 * able to tell the difference between a "+" and a "-". (For bools,
83 * the "+"/"-" information is encoded in "add" and "mask"). So we use
84 * an enumerated type (well, the preprocessor equivalent). Here are
87 #define AV_NONE 0 /* +opt -opt */
88 #define AV_ADD_STRING 1 /* +stropt{string} */
89 #define AV_REM_STRING 2 /* -stropt */
90 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
91 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt{*} */
94 * We need a structure to hold the name, flag changes,
95 * type, and string index.
100 unsigned mask; /* a bit set to "0" = remove action */
101 unsigned add; /* a bit set to "1" = add action */
102 int takes_value; /* an AV_... constant */
103 int index; /* index into strings[] or multi[] */
107 * And with those building blocks in place, here's the array.
109 static const struct action_name action_names[] =
112 * Well actually there's no data here - it's in actionlist.h
113 * This keeps it together to make it easy to change.
115 * Here's the macros used to format it:
117 #define DEFINE_ACTION_MULTI(name,index) \
118 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
119 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
120 #define DEFINE_ACTION_STRING(name,flag,index) \
121 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
122 { "-" name, ~flag, 0, AV_REM_STRING, index },
123 #define DEFINE_ACTION_BOOL(name,flag) \
124 { "+" name, ACTION_MASK_ALL, flag }, \
125 { "-" name, ~flag, 0 },
126 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
128 #include "actionlist.h"
130 #undef DEFINE_ACTION_MULTI
131 #undef DEFINE_ACTION_STRING
132 #undef DEFINE_ACTION_BOOL
133 #undef DEFINE_ACTION_ALIAS
135 { NULL, 0, 0 } /* End marker */
139 /*********************************************************************
141 * Function : merge_actions
143 * Description : Merge two actions together.
144 * Similar to "cur_action += new_action".
147 * 1 : cur_action = Current actions, to modify.
148 * 2 : new_action = Action to add.
152 *********************************************************************/
153 void merge_actions (struct action_spec *dest,
154 const struct action_spec *src)
158 dest->mask &= src->mask;
159 dest->add &= src->mask;
160 dest->add |= src->add;
162 for (i = 0; i < ACTION_STRING_COUNT; i++)
164 char * str = src->string[i];
167 freez(dest->string[i]);
168 dest->string[i] = strdup(str);
172 for (i = 0; i < ACTION_MULTI_COUNT; i++)
174 if (src->multi_remove_all[i])
176 /* Remove everything from dest */
177 destroy_list(dest->multi_remove[i]);
178 destroy_list(dest->multi_add[i]);
179 dest->multi_remove_all[i] = 1;
180 list_duplicate(dest->multi_add[i], src->multi_add[i]);
182 else if (dest->multi_remove_all[i])
185 * dest already removes everything, so we only need to worry
188 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
189 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
193 /* No "remove all"s to worry about. */
194 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
195 list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
196 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
202 /*********************************************************************
204 * Function : copy_action
206 * Description : Copy an action_specs.
207 * Similar to "cur_action = new_action".
210 * 1 : dest = Destination of copy.
211 * 2 : src = Source for copy.
215 *********************************************************************/
216 void copy_action (struct action_spec *dest,
217 const struct action_spec *src)
221 dest->mask = src->mask;
222 dest->add = src->add;
224 for (i = 0; i < ACTION_STRING_COUNT; i++)
226 char * str = src->string[i];
227 dest->string[i] = (str ? strdup(str) : NULL);
230 for (i = 0; i < ACTION_MULTI_COUNT; i++)
232 dest->multi_remove_all[i] = src->multi_remove_all[i];
233 list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
234 list_duplicate(dest->multi_add[i], src->multi_add[i]);
239 /*********************************************************************
241 * Function : free_action
243 * Description : Free an action_specs.
246 * 1 : src = Source to free.
250 *********************************************************************/
251 void free_action (struct action_spec *src)
255 for (i = 0; i < ACTION_STRING_COUNT; i++)
257 freez(src->string[i]);
260 for (i = 0; i < ACTION_MULTI_COUNT; i++)
262 destroy_list(src->multi_remove[i]);
263 destroy_list(src->multi_add[i]);
266 memset(src, '\0', sizeof(*src));
270 /*********************************************************************
272 * Function : get_action_token
274 * Description : Parses a line for the first action.
275 * Modifies it's input array, doesn't allocate memory.
277 * *line=" +abc{def} -ghi "
284 * 1 : line = [in] The line containing the action.
285 * [out] Start of next action on line, or
286 * NULL if we reached the end of line before
287 * we found an action.
288 * 2 : name = [out] Start of action name, null
289 * terminated. NULL on EOL
290 * 3 : value = [out] Start of action value, null
291 * terminated. NULL if none or EOL.
294 * nonzero => Mismatched {} (line was trashed anyway)
296 *********************************************************************/
297 int get_action_token(char **line, char **name, char **value)
302 /* set default returns */
307 /* Eat any leading whitespace */
308 while ((*str == ' ') || (*str == '\t'))
320 /* null name, just value is prohibited */
327 while (((ch = *str) != '\0') &&
328 (ch != ' ') && (ch != '\t') && (ch != '{'))
344 /* EOL - be careful not to run off buffer */
349 /* More to parse next time. */
358 str = strchr(str, '}');
376 /*********************************************************************
378 * Function : get_actions
380 * Description : Parses a list of actions.
383 * 1 : line = The string containing the actions.
384 * Will be written to by this function.
385 * 2 : aliases = Custom alias list, or NULL for none.
386 * 3 : cur_action = Where to store the action. Caller
390 * nonzero => Error (line was trashed anyway)
392 *********************************************************************/
393 int get_actions(char *line, struct action_alias * alias_list,
394 struct action_spec *cur_action)
396 memset(cur_action, '\0', sizeof(*cur_action));
397 cur_action->mask = ACTION_MASK_ALL;
401 char * option = NULL;
404 if (get_action_token(&line, &option, &value))
411 /* handle option in 'option' */
413 /* Check for standard action name */
414 const struct action_name * action = action_names;
416 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
420 if (action->name != NULL)
423 cur_action->mask &= action->mask;
424 cur_action->add &= action->mask;
425 cur_action->add |= action->add;
427 switch (action->takes_value)
430 /* ignore any option. */
434 /* add single string. */
436 if ((value == NULL) || (*value == '\0'))
440 /* FIXME: should validate option string here */
441 freez (cur_action->string[action->index]);
442 cur_action->string[action->index] = strdup(value);
447 /* remove single string. */
449 freez (cur_action->string[action->index]);
454 /* append multi string. */
456 struct list * remove = cur_action->multi_remove[action->index];
457 struct list * add = cur_action->multi_add[action->index];
459 if ((value == NULL) || (*value == '\0'))
464 list_remove_item(remove, value);
465 enlist_unique(add, value);
470 /* remove multi string. */
472 struct list * remove = cur_action->multi_remove[action->index];
473 struct list * add = cur_action->multi_add[action->index];
475 if ( (value == NULL) || (*value == '\0')
476 || ((*value == '*') && (value[1] == '\0')) )
479 * no option, or option == "*".
483 destroy_list(remove);
485 cur_action->multi_remove_all[action->index] = 1;
489 /* Valid option - remove only 1 option */
491 if ( !cur_action->multi_remove_all[action->index] )
493 /* there isn't a catch-all in the remove list already */
494 enlist_unique(remove, value);
496 list_remove_item(add, value);
501 /* Shouldn't get here unless there's memory corruption. */
507 /* try user aliases. */
508 const struct action_alias * alias = alias_list;
510 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
517 merge_actions(cur_action, alias->action);
521 /* Bad action name */
532 /*********************************************************************
534 * Function : actions_to_text
536 * Description : Converts a actionsfile entry from numeric form
537 * ("mask" and "add") to text.
540 * 1 : mask = As from struct url_actions
541 * 2 : add = As from struct url_actions
543 * Returns : A string. Caller must free it.
545 *********************************************************************/
546 char * actions_to_text(struct action_spec *action)
548 unsigned mask = action->mask;
549 unsigned add = action->add;
550 char * result = strdup("");
553 /* sanity - prevents "-feature +feature" */
557 #define DEFINE_ACTION_BOOL(__name, __bit) \
558 if (!(mask & __bit)) \
560 result = strsav(result, " -" __name); \
562 else if (add & __bit) \
564 result = strsav(result, " +" __name); \
567 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
568 if (!(mask & __bit)) \
570 result = strsav(result, " -" __name); \
572 else if (add & __bit) \
574 result = strsav(result, " +" __name "{"); \
575 result = strsav(result, action->string[__index]); \
576 result = strsav(result, "}"); \
579 #define DEFINE_ACTION_MULTI(__name, __index) \
580 if (action->multi_remove_all[__index]) \
582 result = strsav(result, " -" __name "{*}"); \
586 lst = action->multi_remove[__index]->next; \
589 result = strsav(result, " -" __name "{"); \
590 result = strsav(result, lst->str); \
591 result = strsav(result, "}"); \
595 lst = action->multi_add[__index]->next; \
598 result = strsav(result, " +" __name "{"); \
599 result = strsav(result, lst->str); \
600 result = strsav(result, "}"); \
604 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
606 #include "actionlist.h"
608 #undef DEFINE_ACTION_MULTI
609 #undef DEFINE_ACTION_STRING
610 #undef DEFINE_ACTION_BOOL
611 #undef DEFINE_ACTION_ALIAS
617 /*********************************************************************
619 * Function : current_actions_to_text
621 * Description : Converts a actionsfile entry to text.
624 * 1 : action = Action
626 * Returns : A string. Caller must free it.
628 *********************************************************************/
629 char * current_action_to_text(struct current_action_spec *action)
631 unsigned flags = action->flags;
632 char * result = strdup("");
635 #define DEFINE_ACTION_BOOL(__name, __bit) \
638 result = strsav(result, " +" __name); \
642 result = strsav(result, " -" __name); \
645 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
648 result = strsav(result, " +" __name "{"); \
649 result = strsav(result, action->string[__index]); \
650 result = strsav(result, "}"); \
654 result = strsav(result, " -" __name); \
657 #define DEFINE_ACTION_MULTI(__name, __index) \
658 lst = action->multi[__index]->next; \
661 result = strsav(result, " -" __name); \
667 result = strsav(result, " +" __name "{"); \
668 result = strsav(result, lst->str); \
669 result = strsav(result, "}"); \
674 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
676 #include "actionlist.h"
678 #undef DEFINE_ACTION_MULTI
679 #undef DEFINE_ACTION_STRING
680 #undef DEFINE_ACTION_BOOL
681 #undef DEFINE_ACTION_ALIAS
687 /*********************************************************************
689 * Function : init_current_action
691 * Description : Zero out an action.
694 * 1 : dest = An uninitialized current_action_spec.
698 *********************************************************************/
699 void init_current_action (struct current_action_spec *dest)
701 memset(dest, '\0', sizeof(*dest));
702 dest->flags = ACTION_MOST_COMPATIBLE;
706 /*********************************************************************
708 * Function : merge_current_action
710 * Description : Merge two actions together.
711 * Similar to "dest += src".
712 * Differences between this and merge_actions()
713 * is that this one doesn't allocate memory for
714 * strings (so "src" better be in memory for at least
715 * as long as "dest" is, and you'd better free
716 * "dest" using "current_free_action").
717 * Also, there is no mask or remove lists in dest.
718 * (If we're applying it to a URL, we don't need them)
721 * 1 : dest = Current actions, to modify.
722 * 2 : src = Action to add.
726 *********************************************************************/
727 void merge_current_action (struct current_action_spec *dest,
728 const struct action_spec *src)
732 dest->flags &= src->mask;
733 dest->flags |= src->add;
735 for (i = 0; i < ACTION_STRING_COUNT; i++)
737 char * str = src->string[i];
740 dest->string[i] = str;
744 for (i = 0; i < ACTION_MULTI_COUNT; i++)
746 if (src->multi_remove_all[i])
748 /* Remove everything from dest */
749 destroy_list(dest->multi[i]);
750 list_duplicate(dest->multi[i], src->multi_add[i]);
754 list_remove_list(dest->multi[i], src->multi_remove[i]);
755 list_append_list_unique(dest->multi[i], src->multi_add[i]);
761 /*********************************************************************
763 * Function : free_current_action
765 * Description : Free a current_action_spec.
768 * 1 : src = Source to free.
772 *********************************************************************/
773 void free_current_action (struct current_action_spec *src)
777 for (i = 0; i < ACTION_MULTI_COUNT; i++)
779 destroy_list(src->multi[i]);
782 memset(src, '\0', sizeof(*src));
786 /*********************************************************************
788 * Function : unload_actions_file
790 * Description : Unloads an actions module.
793 * 1 : file_data = the data structure associated with the
798 *********************************************************************/
799 void unload_actions_file(void *file_data)
801 struct url_actions * next;
802 struct url_actions * cur = (struct url_actions *)file_data;
814 /*********************************************************************
816 * Function : load_actions_file
818 * Description : Read and parse a action file and add to files
822 * 1 : csp = Current client state (buffers, headers, etc...)
824 * Returns : 0 => Ok, everything else is an error.
826 *********************************************************************/
827 int load_actions_file(struct client_state *csp)
829 static struct file_list *current_actions_file = NULL;
833 struct url_actions *last_perm;
834 struct url_actions *perm;
836 struct file_list *fs;
837 #define MODE_START_OF_FILE 1
838 #define MODE_ACTIONS 2
840 int mode = MODE_START_OF_FILE;
841 struct action_spec cur_action[1];
842 struct action_alias * alias_list = NULL;
844 memset(cur_action, '\0', sizeof(*cur_action));
846 if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
848 /* No need to load */
851 csp->actions_list = current_actions_file;
857 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
858 csp->config->actions_file);
859 return 1; /* never get here */
862 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
863 if (last_perm == NULL)
865 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
866 csp->config->actions_file);
867 return 1; /* never get here */
870 if ((fp = fopen(csp->config->actions_file, "r")) == NULL)
872 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
873 csp->config->actions_file);
874 return 1; /* never get here */
877 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
881 /* It's a header block */
884 /* It's {{settings}} or {{alias}} */
885 int len = strlen(buf);
886 char * start = buf + 2;
887 char * end = buf + len - 1;
888 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
892 log_error(LOG_LEVEL_FATAL,
893 "can't load actions file '%s': invalid line: %s",
894 csp->config->actions_file, buf);
895 return 1; /* never get here */
898 /* Trim leading and trailing whitespace. */
906 log_error(LOG_LEVEL_FATAL,
907 "can't load actions file '%s': invalid line: {{ }}",
908 csp->config->actions_file);
909 return 1; /* never get here */
912 if (0 == strcmpic(start, "alias"))
914 /* it's an {{alias}} block */
920 /* invalid {{something}} block */
922 log_error(LOG_LEVEL_FATAL,
923 "can't load actions file '%s': invalid line: {{%s}}",
924 csp->config->actions_file, start);
925 return 1; /* never get here */
930 /* It's an actions block */
932 char actions_buf[BUFSIZ];
938 /* free old action */
939 free_action(cur_action);
942 strcpy(actions_buf, buf + 1);
944 /* check we have a trailing } and then trim it */
945 end = actions_buf + strlen(actions_buf) - 1;
950 log_error(LOG_LEVEL_FATAL,
951 "can't load actions file '%s': invalid line: %s",
952 csp->config->actions_file, buf);
953 return 1; /* never get here */
957 /* trim any whitespace immediately inside {} */
960 if (*actions_buf == '\0')
964 log_error(LOG_LEVEL_FATAL,
965 "can't load actions file '%s': invalid line: %s",
966 csp->config->actions_file, buf);
967 return 1; /* never get here */
970 if (get_actions(actions_buf, alias_list, cur_action))
974 log_error(LOG_LEVEL_FATAL,
975 "can't load actions file '%s': invalid line: %s",
976 csp->config->actions_file, buf);
977 return 1; /* never get here */
981 else if (mode == MODE_ALIAS)
983 /* define an alias */
984 char actions_buf[BUFSIZ];
985 struct action_alias * new_alias;
988 char * start = strchr(buf, '=');
991 if ((start == NULL) || (start == buf))
993 log_error(LOG_LEVEL_FATAL,
994 "can't load actions file '%s': invalid alias line: %s",
995 csp->config->actions_file, buf);
996 return 1; /* never get here */
999 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1002 log_error(LOG_LEVEL_FATAL,
1003 "can't load actions file '%s': out of memory!",
1004 csp->config->actions_file);
1005 return 1; /* never get here */
1008 /* Eat any the whitespace before the '=' */
1010 while ((*end == ' ') || (*end == '\t'))
1013 * we already know we must have at least 1 non-ws char
1014 * at start of buf - no need to check
1020 /* Eat any the whitespace after the '=' */
1022 while ((*start == ' ') || (*start == '\t'))
1028 log_error(LOG_LEVEL_FATAL,
1029 "can't load actions file '%s': invalid alias line: %s",
1030 csp->config->actions_file, buf);
1031 return 1; /* never get here */
1034 new_alias->name = strdup(buf);
1036 strcpy(actions_buf, start);
1038 if (get_actions(actions_buf, alias_list, new_alias->action))
1042 log_error(LOG_LEVEL_FATAL,
1043 "can't load actions file '%s': invalid alias line: %s = %s",
1044 csp->config->actions_file, buf, start);
1045 return 1; /* never get here */
1049 new_alias->next = alias_list;
1050 alias_list = new_alias;
1052 else if (mode == MODE_ACTIONS)
1054 /* it's a URL pattern */
1056 /* allocate a new node */
1057 if ((perm = zalloc(sizeof(*perm))) == NULL)
1060 log_error(LOG_LEVEL_FATAL,
1061 "can't load actions file '%s': out of memory!",
1062 csp->config->actions_file);
1063 return 1; /* never get here */
1067 copy_action (perm->action, cur_action);
1069 /* Save the URL pattern */
1070 if (create_url_spec(perm->url, buf))
1073 log_error(LOG_LEVEL_FATAL,
1074 "can't load actions file '%s': cannot create URL pattern from: %s",
1075 csp->config->actions_file, buf);
1076 return 1; /* never get here */
1079 /* add it to the list */
1080 last_perm->next = perm;
1083 else if (mode == MODE_START_OF_FILE)
1085 /* oops - please have a {} line as 1st line in file. */
1087 log_error(LOG_LEVEL_FATAL,
1088 "can't load actions file '%s': first line is invalid: %s",
1089 csp->config->actions_file, buf);
1090 return 1; /* never get here */
1094 /* How did we get here? This is impossible! */
1096 log_error(LOG_LEVEL_FATAL,
1097 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1098 csp->config->actions_file, mode);
1099 return 1; /* never get here */
1105 free_action(cur_action);
1107 while (alias_list != NULL)
1109 struct action_alias * next = alias_list->next;
1110 freez((char *)alias_list->name);
1111 free_action(alias_list->action);
1116 #ifndef SPLIT_PROXY_ARGS
1117 if (!suppress_blocklists)
1119 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
1121 #endif /* ndef SPLIT_PROXY_ARGS */
1123 /* the old one is now obsolete */
1124 if (current_actions_file)
1126 current_actions_file->unloader = unload_actions_file;
1129 fs->next = files->next;
1131 current_actions_file = fs;
1135 csp->actions_list = fs;