1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.2 2001/09/16 17:05:14 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include:
11 * Copyright : Written by and Copyright (C) 2001 the SourceForge
12 * IJBSWA team. http://ijbswa.sourceforge.net
14 * Based on the Internet Junkbuster originally written
15 * by and Copyright (C) 1997 Anonymous Coders and
16 * Junkbusters Corporation. http://www.junkbusters.com
18 * This program is free software; you can redistribute it
19 * and/or modify it under the terms of the GNU General
20 * Public License as published by the Free Software
21 * Foundation; either version 2 of the License, or (at
22 * your option) any later version.
24 * This program is distributed in the hope that it will
25 * be useful, but WITHOUT ANY WARRANTY; without even the
26 * implied warranty of MERCHANTABILITY or FITNESS FOR A
27 * PARTICULAR PURPOSE. See the GNU General Public
28 * License for more details.
30 * The GNU General Public License should be included with
31 * this file. If not, you can view it at
32 * http://www.gnu.org/copyleft/gpl.html
33 * or write to the Free Software Foundation, Inc., 59
34 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 * Revision 1.2 2001/09/16 17:05:14 jongfoster
39 * Removing unused #include showarg.h
41 * Revision 1.1 2001/09/16 15:47:37 jongfoster
42 * First version of CGI-based edit interface. This is very much a
43 * work-in-progress, and you can't actually use it to edit anything
44 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
48 **********************************************************************/
54 * FIXME: Following includes copied from cgi.c - which are actually needed?
59 #include <sys/types.h>
67 #define snprintf _snprintf
68 #endif /* def _WIN32 */
73 #include "cgisimple.h"
80 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
83 #ifdef FEATURE_CGI_EDIT_ACTIONS
87 struct file_line * next;
95 struct action_spec action[1];
104 /* Add more data types here... e.g.
107 struct url_spec url[1];
111 struct action_spec action[1];
120 #define FILE_LINE_UNPROCESSED 1
121 #define FILE_LINE_BLANK 2
122 #define FILE_LINE_ALIAS_HEADER 3
123 #define FILE_LINE_ALIAS_ENTRY 4
124 #define FILE_LINE_ACTION 5
125 #define FILE_LINE_URL 6
126 #define FILE_LINE_SETTINGS_HEADER 7
127 #define FILE_LINE_SETTINGS_ENTRY 8
128 #define FILE_LINE_DESCRIPTION_HEADER 9
129 #define FILE_LINE_DESCRIPTION_ENTRY 10
131 /* FIXME: Following list of prototypes is not complete */
132 /* FIXME: Following non-static functions should be prototyped in .h or made static */
133 static int simple_read_line(char **dest, FILE *fp);
134 static int edit_read_line (FILE *fp, char **raw_out, char **prefix_out, char **data_out);
135 int edit_read_file (FILE *fp, struct file_line ** pfile);
136 int edit_write_file (const char * filename, const struct file_line * file);
137 void edit_free_file (struct file_line * file);
140 /* FIXME: This should be in project.h and used everywhere */
141 #define JB_ERR_OK 0 /* Success, no error */
142 #define JB_ERR_MEMORY 1 /* Out of memory */
143 #define JB_ERR_CGI_PARAMS 2 /* Missing or corrupt CGI parameters */
144 #define JB_ERR_FILE 3 /* Error opening, reading or writing a file */
145 #define JB_ERR_PARSE 4 /* Error parsing file */
146 #define JB_ERR_MODIFIED 5 /* File has been modified outside of the */
147 /* CGI actions editor. */
150 /*********************************************************************
152 * Function : simple_read_line
154 * Description : Read a single line from a file and return it.
155 * This is basically a version of fgets() that malloc()s
156 * it's own line buffer. Note that the buffer will
157 * always be a multiple of BUFFER_SIZE bytes long.
158 * Therefore if you are going to keep the string for
159 * an extended period of time, you should probably
160 * strdup() it and free() the original, to save memory.
164 * 1 : dest = destination for newly malloc'd pointer to
165 * line data. Will be set to NULL on error.
166 * 2 : fp = File to read from
168 * Returns : JB_ERR_OK on success
169 * JB_ERR_MEMORY on out-of-memory
170 * JB_ERR_FILE on EOF.
172 *********************************************************************/
173 static int simple_read_line(char **dest, FILE *fp)
184 if (NULL == (buf = malloc(BUFFER_SIZE)))
186 return JB_ERR_MEMORY;
195 if ((!fgets(newbuf, BUFFER_SIZE, fp)) || (*newbuf == '\0'))
197 /* (*newbuf == '\0') should never happen unless fgets fails */
210 if ((buf[len - 1] == '\n') || (buf[len - 1] == '\r'))
216 if (NULL == (newbuf = realloc(buf, len + BUFFER_SIZE)))
219 return JB_ERR_MEMORY;
226 /*********************************************************************
228 * Function : edit_read_line
230 * Description : Read a single non-empty line from a file and return
231 * it. Trims comments, leading and trailing whitespace
232 * and respects escaping of newline and comment char.
233 * Provides the line in 2 alternative forms: raw and
235 * - raw is the raw data read from the file. If the
236 * line is not modified, then this should be written
238 * - prefix is any comments and blank lines that were
239 * read from the file. If the line is modified, then
240 * this should be written out to the file followed
241 * by the modified data. (If this string is non-empty
242 * then it will have a newline at the end).
243 * - data is the actual data that will be parsed
244 * further by appropriate routines.
245 * On EOF, the 3 strings will all be set to NULL and
246 * 0 will be returned.
249 * 1 : fp = File to read from
250 * 2 : raw_out = destination for newly malloc'd pointer to
251 * raw line data. May be NULL if you don't want it.
252 * 3 : prefix_out = destination for newly malloc'd pointer to
253 * comments. May be NULL if you don't want it.
254 * 4 : data_out = destination for newly malloc'd pointer to
255 * line data with comments and leading/trailing spaces
256 * removed, and line continuation performed. May be
257 * NULL if you don't want it.
259 * Returns : JB_ERR_OK on success
260 * JB_ERR_MEMORY on out-of-memory
261 * JB_ERR_FILE on EOF.
263 *********************************************************************/
264 static int edit_read_line(FILE *fp, char **raw_out, char **prefix_out, char **data_out)
266 char *p; /* Temporary pointer */
267 char *linebuf; /* Line read from file */
268 char *linestart; /* Start of linebuf, usually first non-whitespace char */
269 char newline[3]; /* Used to store the newline - "\n", "\r", or "\r\n" */
270 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
271 char *raw; /* String to be stored in raw_out */
272 char *prefix; /* String to be stored in prefix_out */
273 char *data; /* String to be stored in data_out */
274 int rval = JB_ERR_OK;
278 /* Set output parameters to NULL */
292 /* Set string variables to new, empty strings. */
298 if ((raw == NULL) || (prefix == NULL) || (data == NULL))
303 return JB_ERR_MEMORY;
310 /* Main loop. Loop while we need more data & it's not EOF. */
312 while ( (contflag || (*data == '\0'))
313 && (JB_ERR_OK == (rval = simple_read_line(&linebuf, fp))))
315 if (string_append(&raw,linebuf))
320 return JB_ERR_MEMORY;
323 /* Trim off newline */
324 p = linebuf + strlen(linebuf);
325 if ((p != linebuf) && ((p[-1] == '\r') || (p[-1] == '\n')))
328 if ((p != linebuf) && ((p[-1] == '\r') || (p[-1] == '\n')))
336 /* Line continuation? Trim escape and set flag. */
337 contflag = ((p != linebuf) && (*--p == '\\'));
343 /* Trim leading spaces if we're at the start of the line */
347 /* Trim leading spaces */
348 while (*linestart && isspace((int)(unsigned char)*linestart))
354 /* Handle comment characters. */
356 while ((p = strchr(p, '#')) != NULL)
358 /* Found a comment char.. */
359 if ((p != linebuf) && (*(p-1) == '\\'))
361 /* ..and it's escaped, left-shift the line over the escape. */
363 while ((*q = *(q + 1)) != '\0')
367 /* Now scan from just after the "#". */
371 /* Real comment. Save it... */
374 /* Special case: Line only contains a comment, so all the
375 * previous whitespace is considered part of the comment.
376 * Undo the whitespace skipping, if any.
381 string_append(&prefix,p);
382 if (string_append(&prefix,newline))
387 return JB_ERR_MEMORY;
391 /* ... and chop off the rest of the line */
394 } /* END while (there's a # character) */
396 /* Write to the buffer */
399 if (string_append(&data, linestart))
404 return JB_ERR_MEMORY;
409 } /* END while(we need more data) */
411 /* Handle simple_read_line() errors - ignore EOF */
412 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
423 /* Got at least some data */
425 /* Remove trailing whitespace */
438 *prefix_out = prefix;
456 /* EOF and no data there. */
467 /*********************************************************************
469 * Function : edit_read_file
471 * Description : Read a complete file into memory.
472 * Handles whitespace, comments and line continuation.
475 * 1 : fp = File to read from
476 * 2 : pfile = Destination for a linked list of file_lines.
477 * Will be set to NULL on error.
479 * Returns : JB_ERR_OK on success
480 * JB_ERR_MEMORY on out-of-memory
482 *********************************************************************/
483 int edit_read_file(FILE *fp, struct file_line ** pfile)
485 struct file_line * first_line; /* Keep for return value or to free */
486 struct file_line * cur_line; /* Current line */
487 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
495 cur_line = first_line = zalloc(sizeof(struct file_line));
496 if (cur_line == NULL)
498 return JB_ERR_MEMORY;
501 cur_line->type = FILE_LINE_UNPROCESSED;
503 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed);
506 /* Out of memory or empty file. */
507 /* Note that empty file is not an error we propogate up */
509 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
514 prev_line = cur_line;
515 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
516 if (cur_line == NULL)
519 edit_free_file(first_line);
520 return JB_ERR_MEMORY;
523 cur_line->type = FILE_LINE_UNPROCESSED;
525 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed);
526 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
529 edit_free_file(first_line);
530 return JB_ERR_MEMORY;
534 while (rval != JB_ERR_FILE);
538 /* We allocated one too many - free it */
539 prev_line->next = NULL;
547 /*********************************************************************
549 * Function : edit_write_file
551 * Description : Write a complete file to disk.
554 * 1 : filename = File to write to.
555 * 2 : file = Data structure to write.
557 * Returns : JB_ERR_OK on success
558 * JB_ERR_FILE on error writing to file.
560 *********************************************************************/
561 int edit_write_file(const char * filename, const struct file_line * file)
567 if (NULL == (fp = fopen(filename, "wt")))
576 if (fputs(file->raw, fp) < 0)
586 if (fputs(file->prefix, fp) < 0)
592 if (file->unprocessed)
594 if (fputs(file->unprocessed, fp) < 0)
599 if (fputs("\n", fp) < 0)
607 /* FIXME: Write data from file->data->whatever */
619 /*********************************************************************
621 * Function : edit_free_file
623 * Description : Free a complete file in memory.
626 * 1 : file = Data structure to free.
630 *********************************************************************/
631 void edit_free_file(struct file_line * file)
633 struct file_line * next;
641 freez(file->unprocessed);
644 case 0: /* special case if memory zeroed */
645 case FILE_LINE_UNPROCESSED:
646 case FILE_LINE_BLANK:
647 case FILE_LINE_ALIAS_HEADER:
648 case FILE_LINE_SETTINGS_HEADER:
649 case FILE_LINE_DESCRIPTION_HEADER:
650 case FILE_LINE_DESCRIPTION_ENTRY:
651 case FILE_LINE_ALIAS_ENTRY:
653 /* No data is stored for these */
656 case FILE_LINE_ACTION:
657 free_action(file->data.action);
660 case FILE_LINE_SETTINGS_ENTRY:
661 freez(file->data.setting.name);
662 freez(file->data.setting.svalue);
665 /* Should never happen */
669 file->type = 0; /* paranoia */
676 /*********************************************************************
678 * Function : match_actions_file_header_line
680 * Description : Match an actions file {{header}} line
683 * 1 : line - String from file
684 * 2 : name - Header to match against
686 * Returns : 0 iff they match.
688 *********************************************************************/
689 static int match_actions_file_header_line(const char * line, const char * name)
697 if ((line[0] != '{') || (line[1] != '{'))
703 /* Look for optional whitespace */
704 while ( (*line == ' ') || (*line == '\t') )
709 /* Look for the specified name (case-insensitive) */
711 if (0 != strncmpic(line, name, len))
717 /* Look for optional whitespace */
718 while ( (*line == ' ') || (*line == '\t') )
723 /* Look for "}}" and end of string*/
724 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
734 /*********************************************************************
736 * Function : match_actions_file_header_line
738 * Description : Match an actions file {{header}} line
741 * 1 : line - String from file. Must not start with
742 * whitespace (else infinite loop!)
743 * 2 : name - Destination for name
744 * 2 : name - Destination for value
746 * Returns : JB_ERR_OK on success
747 * JB_ERR_MEMORY on out-of-memory
748 * JB_ERR_PARSE if there's no "=" sign, or if there's
749 * nothing before the "=" sign (but empty
750 * values *after* the "=" sign are legal).
752 *********************************************************************/
753 static int split_line_on_equals(const char * line, char ** pname, char ** pvalue)
755 const char * name_end;
756 const char * value_start;
762 assert(*line != ' ');
763 assert(*line != '\t');
768 value_start = strchr(line, '=');
769 if ((value_start == NULL) || (value_start == line))
774 name_end = value_start - 1;
776 /* Eat any whitespace before the '=' */
777 while ((*name_end == ' ') || (*name_end == '\t'))
780 * we already know we must have at least 1 non-ws char
781 * at start of buf - no need to check
786 name_len = name_end - line + 1; /* Length excluding \0 */
787 if (NULL == (*pname = (char *) malloc(name_len + 1)))
789 return JB_ERR_MEMORY;
791 strncpy(*pname, line, name_len);
792 (*pname)[name_len] = '\0';
794 /* Eat any the whitespace after the '=' */
796 while ((*value_start == ' ') || (*value_start == '\t'))
801 if (NULL == (*pvalue = strdup(value_start)))
805 return JB_ERR_MEMORY;
812 /*********************************************************************
814 * Function : edit_parse_actions_file
816 * Description : Parse an actions file in memory.
818 * Passed linked list must have the "data" member
819 * zeroed, and must contain valid "next" and
820 * "unprocessed" fields. The "raw" and "prefix"
821 * fields are ignored, and "type" is just overwritten.
823 * Note that on error the file may have been
827 * 1 : file = Actions file to be parsed in-place.
829 * Returns : JB_ERR_OK on success
830 * JB_ERR_MEMORY on out-of-memory
831 * JB_ERR_PARSE on error
833 *********************************************************************/
834 int edit_parse_actions_file(struct file_line * file)
836 struct file_line * cur_line;
838 const char * text; /* Text from a line */
839 char * name; /* For lines of the form name=value */
840 char * value; /* For lines of the form name=value */
841 struct action_alias * alias_list = NULL;
842 int rval = JB_ERR_OK;
844 /* alias_list contains the aliases defined in this file.
845 * It might be better to use the "file_line.data" fields
846 * in the relavent places instead.
851 /* A note about blank line support: Blank lines should only
852 * ever occur as the last line in the file. This function
853 * is more forgiving than that - FILE_LINE_BLANK can occur
857 /* Skip leading blanks. Should only happen if file is
858 * empty (which is valid, but pointless).
860 while ( (cur_line != NULL)
861 && (cur_line->unprocessed[0] == '\0') )
864 cur_line->type = FILE_LINE_BLANK;
865 cur_line = cur_line->next;
868 if ( (cur_line != NULL)
869 && (cur_line->unprocessed[0] != '{') )
871 /* File doesn't start with a header */
875 if ( (cur_line != NULL) && (0 ==
876 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
878 cur_line->type = FILE_LINE_SETTINGS_HEADER;
880 cur_line = cur_line->next;
881 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
883 if (cur_line->unprocessed[0])
885 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
887 rval = split_line_on_equals(cur_line->unprocessed,
888 &cur_line->data.setting.name,
889 &cur_line->data.setting.svalue);
890 if (rval != JB_ERR_OK)
892 /* Line does not contain a name=value pair, or out-of-memory */
898 cur_line->type = FILE_LINE_BLANK;
900 cur_line = cur_line->next;
904 if ( (cur_line != NULL) && (0 ==
905 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
907 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
909 cur_line = cur_line->next;
910 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
912 if (cur_line->unprocessed[0])
914 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
918 cur_line->type = FILE_LINE_BLANK;
920 cur_line = cur_line->next;
924 if ( (cur_line != NULL) && (0 ==
925 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
927 cur_line->type = FILE_LINE_ALIAS_HEADER;
929 cur_line = cur_line->next;
930 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
932 if (cur_line->unprocessed[0])
934 /* define an alias */
935 struct action_alias * new_alias;
937 cur_line->type = FILE_LINE_ALIAS_ENTRY;
939 rval = split_line_on_equals(cur_line->unprocessed, &name, &value);
940 if (rval != JB_ERR_OK)
942 /* Line does not contain a name=value pair, or out-of-memory */
946 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
951 free_alias_list(alias_list);
952 return JB_ERR_MEMORY;
955 if (get_actions(value, alias_list, new_alias->action))
957 /* Invalid action or out of memory */
961 free_alias_list(alias_list);
962 return JB_ERR_PARSE; /* FIXME: or JB_ERR_MEMORY */
967 new_alias->name = name;
970 new_alias->next = alias_list;
971 alias_list = new_alias;
975 cur_line->type = FILE_LINE_BLANK;
977 cur_line = cur_line->next;
981 /* Header done, process the main part of the file */
982 while (cur_line != NULL)
984 /* At this point, (cur_line->unprocessed[0] == '{') */
985 assert(cur_line->unprocessed[0] == '{');
986 text = cur_line->unprocessed + 1;
987 len = strlen(text) - 1;
988 if (text[len] != '}')
990 /* No closing } on header */
991 free_alias_list(alias_list);
997 /* An invalid {{ header. */
998 free_alias_list(alias_list);
1002 while ( (*text == ' ') || (*text == '\t') )
1008 && ( (text[len - 1] == ' ')
1009 || (text[len - 1] == '\t') ) )
1015 /* A line containing just { } */
1016 free_alias_list(alias_list);
1017 return JB_ERR_PARSE;
1020 cur_line->type = FILE_LINE_ACTION;
1022 /* Remove {} and make copy */
1023 if (NULL == (value = (char *) malloc(len + 1)))
1026 free_alias_list(alias_list);
1027 return JB_ERR_MEMORY;
1029 strncpy(value, text, len);
1033 if (get_actions(value, alias_list, cur_line->data.action))
1035 /* Invalid action or out of memory */
1037 free_alias_list(alias_list);
1038 return JB_ERR_PARSE; /* FIXME: or JB_ERR_MEMORY */
1041 /* Done with string - it was clobbered anyway */
1044 /* Process next line */
1045 cur_line = cur_line->next;
1047 /* Loop processing URL patterns */
1048 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1050 if (cur_line->unprocessed[0])
1052 /* Could parse URL here, but this isn't currently needed */
1054 cur_line->type = FILE_LINE_URL;
1058 cur_line->type = FILE_LINE_BLANK;
1060 cur_line = cur_line->next;
1062 } /* End main while(cur_line != NULL) loop */
1064 free_alias_list(alias_list);
1070 /*********************************************************************
1072 * Function : edit_read_file
1074 * Description : Read a complete actions file into memory and
1078 * 1 : filename = Path to file to read from
1079 * 2 : pfile = Destination for a linked list of file_lines.
1080 * Will be set to NULL on error.
1082 * Returns : JB_ERR_OK on success
1083 * JB_ERR_MEMORY on out-of-memory
1084 * JB_ERR_FILE if the file cannot be opened or
1087 *********************************************************************/
1088 int edit_read_actions_file(const char * filename, struct file_line ** pfile)
1090 struct file_line * file;
1099 if (NULL == (fp = fopen(filename,"rt")))
1104 rval = edit_read_file(fp, &file);
1108 if (JB_ERR_OK != rval)
1113 if (JB_ERR_OK != (rval = edit_parse_actions_file(file)))
1115 edit_free_file(file);
1124 /*********************************************************************
1126 * Function : get_file_name_param
1128 * Description : Get the name of the file to edit from the parameters
1129 * passed to a CGI function. This function handles
1130 * security checks such as blocking urls containing
1131 * "/" or ".", prepending the config file directory,
1132 * and adding the specified suffix.
1134 * (This is an essential security check, otherwise
1135 * users may be able to pass "../../../etc/passwd"
1136 * and overwrite the password file [linux], "prn:"
1137 * and print random data [Windows], etc...)
1139 * This function only allows filenames contining the
1140 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1141 * That's probably too restrictive but at least it's
1145 * 1 : csp = Current client state (buffers, headers, etc...)
1146 * 2 : parameters = map of cgi parameters
1147 * 3 : suffix = File extension, e.g. ".actions"
1148 * 4 : pfilename = destination for full filename. Caller
1149 * free()s. Set to NULL on error.
1150 * 5 : pparam = destination for partial filename,
1151 * suitable for use in another URL. Allocated as part
1152 * of the map "parameters", so don't free it.
1153 * Set to NULL if not specified.
1155 * CGI Parameters : None
1157 * Returns : JB_ERR_OK on success
1158 * JB_ERR_MEMORY on out-of-memory
1159 * JB_ERR_CGI_PARAMS if "filename" was not specified
1162 *********************************************************************/
1163 static int get_file_name_param(struct client_state *csp,
1164 struct map *parameters,
1167 const char **pparam)
1185 param = lookup(parameters, "filename");
1188 return JB_ERR_CGI_PARAMS;
1193 len = strlen(param);
1194 if (len >= FILENAME_MAX)
1197 return JB_ERR_CGI_PARAMS;
1200 /* Check every character to see if it's legal */
1202 while ((ch = *s++) != '\0')
1204 if ( ((ch < 'A') || (ch > 'Z'))
1205 && ((ch < 'a') || (ch > 'z'))
1206 && ((ch < '0') || (ch > '9'))
1210 /* Probable hack attempt. */
1211 return JB_ERR_CGI_PARAMS;
1215 /* Append extension */
1216 name = malloc(len + strlen(suffix) + 1);
1219 return JB_ERR_MEMORY;
1221 strcpy(name, param);
1222 strcpy(name + len, suffix);
1225 fullpath = make_path(csp->config->confdir, name);
1227 if (fullpath == NULL)
1229 return JB_ERR_MEMORY;
1233 *pfilename = fullpath;
1239 /*********************************************************************
1241 * Function : get_number_param
1243 * Description : Get a non-negative integer from the parameters
1244 * passed to a CGI function.
1247 * 1 : csp = Current client state (buffers, headers, etc...)
1248 * 2 : parameters = map of cgi parameters
1249 * 3 : name = Name of CGI parameter to read
1250 * 4 : pvalue = destination for value.
1251 * Set to -1 on error.
1253 * CGI Parameters : None
1255 * Returns : JB_ERR_OK on success
1256 * JB_ERR_MEMORY on out-of-memory
1257 * JB_ERR_CGI_PARAMS if "filename" was not specified
1260 *********************************************************************/
1261 static int get_number_param(struct client_state *csp,
1262 struct map *parameters,
1277 param = lookup(parameters, name);
1280 return JB_ERR_CGI_PARAMS;
1283 /* We don't use atoi because I want to check this carefully... */
1286 while ((ch = *param++) != '\0')
1288 if ((ch < '0') || (ch > '9'))
1290 return JB_ERR_CGI_PARAMS;
1297 * <limits.h> defines INT_MAX
1299 * (INT_MAX - ch) / 10 is the largest number that
1300 * can be safely multiplied by 10 then have ch added.
1302 if (value > ((INT_MAX - ch) / 10))
1304 return JB_ERR_CGI_PARAMS;
1307 value = value * 10 + ch;
1317 /*********************************************************************
1319 * Function : cgi_edit_actions
1321 * Description : CGI function that allows the user to choose which
1322 * actions file to edit.
1325 * 1 : csp = Current client state (buffers, headers, etc...)
1326 * 2 : rsp = http_response data structure for output
1327 * 3 : parameters = map of cgi parameters
1329 * CGI Parameters : None
1331 * Returns : 0 on success, nonzero on error
1333 *********************************************************************/
1334 int cgi_edit_actions(struct client_state *csp,
1335 struct http_response *rsp,
1336 struct map *parameters)
1339 /* FIXME: Incomplete */
1340 rsp->status = strdup("302 Local Redirect from Junkbuster");
1341 enlist_unique_header(rsp->headers, "Location", "http://ijbswa.sourceforge.net/config/edit-actions-list?filename=edit");
1347 /*********************************************************************
1349 * Function : cgi_edit_actions_list
1351 * Description : CGI function that edits the actions list.
1352 * FIXME: This function shouldn't FATAL ever.
1353 * FIXME: This function doesn't check the retval of map()
1355 * 1 : csp = Current client state (buffers, headers, etc...)
1356 * 2 : rsp = http_response data structure for output
1357 * 3 : parameters = map of cgi parameters
1359 * CGI Parameters : None
1361 * Returns : 0 on success, 1 on error.
1363 *********************************************************************/
1364 int cgi_edit_actions_list(struct client_state *csp, struct http_response *rsp,
1365 struct map *parameters)
1367 char * section_template;
1368 char * url_template;
1373 struct map * exports;
1374 struct map * section_exports;
1375 struct map * url_exports;
1376 struct file_line * file;
1377 struct file_line * cur_line;
1378 int line_number = 0;
1382 char * filename_param;
1384 rval = get_file_name_param(csp, parameters, ".action", &filename, &filename_param);
1387 /* No filename specified. */
1388 /* FIXME: Shouldn't FATAL here */
1389 log_error(LOG_LEVEL_FATAL, "No filename specified");
1393 if (edit_read_actions_file(filename, &file))
1395 /* FIXME: Shouldn't FATAL here */
1396 log_error(LOG_LEVEL_FATAL, "Cannot load file '%s' for editing", filename);
1402 if (NULL == (exports = default_exports(csp, NULL)))
1404 log_error(LOG_LEVEL_FATAL, "Out of memory in cgi_edit_actions_list");
1408 map(exports, "filename", 1, filename_param, 1);
1410 /* Should do all global exports above this point */
1412 if (NULL == (section_template = template_load(csp, "edit-actions-list-section")))
1414 log_error(LOG_LEVEL_FATAL, "Out of memory in cgi_edit_actions_list");
1417 if (NULL == (url_template = template_load(csp, "edit-actions-list-url")))
1419 log_error(LOG_LEVEL_FATAL, "Out of memory in cgi_edit_actions_list");
1423 template_fill(§ion_template, exports);
1424 template_fill(&url_template, exports);
1426 /* Find start of actions in file */
1429 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
1431 cur_line = cur_line->next;
1435 if (NULL == (sections = strdup("")))
1437 log_error(LOG_LEVEL_FATAL, "Out of memory in cgi_edit_actions_list");
1441 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
1443 if (NULL == (section_exports = new_map()))
1445 log_error(LOG_LEVEL_FATAL, "Out of memory in cgi_edit_actions_list");
1449 snprintf(buf, 50, "%d", line_number);
1450 map(section_exports, "sectionid", 1, buf, 1);
1452 if (NULL == (s = actions_to_html(cur_line->data.action)))
1454 log_error(LOG_LEVEL_FATAL, "Out of memory in cgi_edit_actions_list");
1457 map(section_exports, "actions", 1, s, 0);
1459 /* Should do all section-specific exports above this point */
1461 if (NULL == (urls = strdup("")))
1463 log_error(LOG_LEVEL_FATAL, "Out of memory in cgi_edit_actions_list");
1469 cur_line = cur_line->next;
1472 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
1474 if (NULL == (url_exports = new_map()))
1476 log_error(LOG_LEVEL_FATAL, "Out of memory in cgi_edit_actions_list");
1480 snprintf(buf, 50, "%d", line_number);
1481 map(url_exports, "urlid", 1, buf, 1);
1483 snprintf(buf, 50, "%d", url_1_2);
1484 map(url_exports, "url-1-2", 1, buf, 1);
1486 if (NULL == (s = html_encode(cur_line->unprocessed)))
1488 log_error(LOG_LEVEL_FATAL, "Out of memory in cgi_edit_actions_list");
1492 map(url_exports, "url", 1, s, 0);
1494 if (NULL == (s = strdup(url_template)))
1496 log_error(LOG_LEVEL_FATAL, "Out of memory in cgi_edit_actions_list");
1499 template_fill(&s, section_exports);
1500 template_fill(&s, url_exports);
1501 urls = strsav(urls, s);
1502 free_map(url_exports);
1504 url_1_2 = 3 - url_1_2;
1506 cur_line = cur_line->next;
1510 map(section_exports, "urls", 1, urls, 0);
1512 /* Could also do section-specific exports here, but it wouldn't be as fast */
1514 s = strdup(section_template);
1515 template_fill(&s, section_exports);
1516 sections = strsav(sections, s);
1517 free_map(section_exports);
1520 edit_free_file(file);
1522 map(exports, "sections", 1, sections, 0);
1524 /* Could also do global exports here, but it wouldn't be as fast */
1526 rsp->body = template_load(csp, "edit-actions-list");
1527 template_fill(&rsp->body, exports);
1534 /*********************************************************************
1536 * Function : map_radio
1538 * Description : Map a set of radio button values. E.g. if you have
1539 * 3 radio buttons, declare them as:
1540 * <option type="radio" name="xyz" @xyz-a@>
1541 * <option type="radio" name="xyz" @xyz-b@>
1542 * <option type="radio" name="xyz" @xyz-c@>
1543 * Then map one of the @xyz-?@ variables to "checked"
1544 * and all the others to empty by calling:
1545 * map_radio(exports, "xyz", "abc", sel)
1546 * Where 'sel' is 'a', 'b', or 'c'.
1549 * 1 : exports = Exports map to modify.
1550 * 2 : optionname = name for map
1551 * 3 : values = null-terminated list of values;
1552 * 4 : value = Selected value.
1554 * CGI Parameters : None
1556 * Returns : JB_ERR_OK on success
1557 * JB_ERR_MEMORY on out-of-memory
1559 *********************************************************************/
1560 static int map_radio(struct map * exports,
1561 const char * optionname,
1562 const char * values,
1574 len = strlen(optionname);
1575 buf = malloc(len + 3);
1578 return JB_ERR_MEMORY;
1581 strcpy(buf, optionname);
1586 while ((c = *values++) != '\0')
1591 if (map(exports, buf, 1, "", 1))
1594 return JB_ERR_MEMORY;
1600 if (map(exports, buf, 0, "checked", 1))
1603 return JB_ERR_MEMORY;
1610 /*********************************************************************
1612 * Function : actions_to_radio
1614 * Description : Converts a actionsfile entry into settings for
1615 * radio buttons and edit boxes on a HTML form.
1618 * 1 : exports = List of substitutions to add to.
1619 * 2 : action = Action to read
1621 * Returns : JB_ERR_OK on success
1622 * JB_ERR_MEMORY on out-of-memory
1624 *********************************************************************/
1625 static int actions_to_radio(struct map * exports, const struct action_spec *action)
1627 unsigned mask = action->mask;
1628 unsigned add = action->add;
1636 mask = action->mask;
1639 /* sanity - prevents "-feature +feature" */
1643 #define DEFINE_ACTION_BOOL(name, bit) \
1644 if (!(mask & bit)) \
1646 current_mode = 'n'; \
1648 else if (add & bit) \
1650 current_mode = 'y'; \
1654 current_mode = 'x'; \
1656 if (map_radio(exports, name, "ynx", current_mode)) \
1658 return JB_ERR_MEMORY; \
1661 #define DEFINE_ACTION_STRING(name, bit, index) \
1662 DEFINE_ACTION_BOOL(name, bit); \
1665 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
1668 checked = !strcmp(action->string[index], value); \
1672 checked = is_default; \
1674 mapped_param |= checked; \
1675 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
1677 return JB_ERR_MEMORY; \
1680 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
1681 if (map(exports, name "-param-custom", 1, \
1682 ((!mapped_param) ? "checked" : ""), 1)) \
1684 return JB_ERR_MEMORY; \
1686 if (map(exports, name "-param", 1, \
1687 (((add & bit) && !mapped_param) ? \
1688 action->string[index] : default_val), 1)) \
1690 return JB_ERR_MEMORY; \
1693 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
1694 if (map(exports, name "-param", 1, \
1695 ((add & bit) ? action->string[index] : default_val), 1)) \
1697 return JB_ERR_MEMORY; \
1700 #define DEFINE_ACTION_MULTI(name, index) \
1701 if (action->multi_add[index]->first) \
1703 current_mode = 'y'; \
1705 else if (action->multi_remove_all[index]) \
1707 current_mode = 'n'; \
1709 else if (action->multi_remove[index]->first) \
1711 current_mode = 'y'; \
1715 current_mode = 'x'; \
1717 if (map_radio(exports, name, "ynx", current_mode)) \
1719 return JB_ERR_MEMORY; \
1722 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1724 #include "actionlist.h"
1726 #undef DEFINE_ACTION_MULTI
1727 #undef DEFINE_ACTION_STRING
1728 #undef DEFINE_ACTION_BOOL
1729 #undef DEFINE_ACTION_ALIAS
1730 #undef DEFINE_CGI_PARAM_CUSTOM
1731 #undef DEFINE_CGI_PARAM_RADIO
1732 #undef DEFINE_CGI_PARAM_NO_RADIO
1738 /*********************************************************************
1740 * Function : cgi_edit_actions
1742 * Description : CGI function that edits the Actions list.
1745 * 1 : csp = Current client state (buffers, headers, etc...)
1746 * 2 : rsp = http_response data structure for output
1747 * 3 : parameters = map of cgi parameters
1749 * CGI Parameters : None
1751 * Returns : 0 on success, nonzero on error
1753 *********************************************************************/
1754 int cgi_edit_actions_for_url(struct client_state *csp,
1755 struct http_response *rsp,
1756 struct map *parameters)
1758 struct map * exports;
1761 struct file_line * file;
1762 struct file_line * cur_line;
1766 char * filename_param;
1768 rval = get_file_name_param(csp, parameters, ".action", &filename, &filename_param);
1771 /* No filename specified. */
1772 /* FIXME: Shouldn't FATAL here */
1773 log_error(LOG_LEVEL_FATAL, "No filename specified");
1777 if (get_number_param(csp, parameters, "section", §ionid))
1779 /* FIXME: Shouldn't FATAL here */
1780 log_error(LOG_LEVEL_FATAL, "No 'section' parameter");
1781 return cgi_default(csp, rsp, parameters);
1784 if (edit_read_actions_file(filename, &file))
1786 /* FIXME: Shouldn't FATAL here */
1787 log_error(LOG_LEVEL_FATAL, "Cannot load file '%s' for editing", filename);
1792 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
1794 cur_line = cur_line->next;
1797 if ( (cur_line == NULL)
1798 || (line_number != sectionid)
1800 || (cur_line->type != FILE_LINE_ACTION))
1802 /* FIXME: Shouldn't FATAL here */
1803 log_error(LOG_LEVEL_FATAL, "Bad sectionid!");
1807 exports = default_exports(csp, NULL);
1808 map(exports, "filename", 1, filename_param, 1);
1809 map(exports, "section", 1, lookup(parameters, "section"), 1);
1811 actions_to_radio(exports, cur_line->data.action);
1813 edit_free_file(file);
1815 rsp->body = template_load(csp, "edit-actions-for-url");
1816 template_fill(&rsp->body, exports);
1823 /*********************************************************************
1825 * Function : actions_from_radio
1827 * Description : Converts a map of parameters passed to a CGI function
1828 * into an actionsfile entry.
1831 * 1 : parameters = parameters to the CGI call
1832 * 2 : action = Action to change. Must be valid before
1833 * the call, actions not specified will be
1836 * Returns : JB_ERR_OK on success
1837 * JB_ERR_MEMORY on out-of-memory
1839 *********************************************************************/
1840 static int actions_from_radio(const struct map * parameters,
1841 struct action_spec *action)
1850 #define DEFINE_ACTION_BOOL(name, bit) \
1851 if (NULL != (param = lookup(parameters, name))) \
1853 ch = toupper((int)param[0]); \
1856 action->add |= bit; \
1857 action->mask |= bit; \
1859 else if (ch == 'N') \
1861 action->add &= ~bit; \
1862 action->mask &= ~bit; \
1864 else if (ch == 'X') \
1866 action->add &= ~bit; \
1867 action->mask |= bit; \
1871 #define DEFINE_ACTION_STRING(name, bit, index) \
1872 if (NULL != (param = lookup(parameters, name))) \
1874 ch = toupper((int)param[0]); \
1877 param = lookup(parameters, name "-mode"); \
1878 if ((*param == '\0') || (0 == strcmp(param, "CUSTOM"))) \
1880 param = lookup(parameters, name "-param"); \
1882 if (*param != '\0') \
1884 if (NULL == (param_dup = strdup(param))) \
1886 return JB_ERR_MEMORY; \
1888 freez(action->string[index]); \
1889 action->add |= bit; \
1890 action->mask |= bit; \
1891 action->string[index] = param_dup; \
1894 else if (ch == 'N') \
1896 if (action->add & bit) \
1898 freez(action->string[index]); \
1900 action->add &= ~bit; \
1901 action->mask &= ~bit; \
1903 else if (ch == 'X') \
1905 if (action->add & bit) \
1907 freez(action->string[index]); \
1909 action->add &= ~bit; \
1910 action->mask |= bit; \
1914 #define DEFINE_ACTION_MULTI(name, index) \
1915 if (NULL != (param = lookup(parameters, name))) \
1917 ch = toupper((int)param[0]); \
1922 else if (ch == 'N') \
1924 list_remove_all(action->multi_add[index]); \
1925 list_remove_all(action->multi_remove[index]); \
1926 action->multi_remove_all[index] = 1; \
1928 else if (ch == 'X') \
1930 list_remove_all(action->multi_add[index]); \
1931 list_remove_all(action->multi_remove[index]); \
1932 action->multi_remove_all[index] = 0; \
1936 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val)
1937 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default)
1938 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val)
1940 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
1942 #include "actionlist.h"
1944 #undef DEFINE_ACTION_MULTI
1945 #undef DEFINE_ACTION_STRING
1946 #undef DEFINE_ACTION_BOOL
1947 #undef DEFINE_ACTION_ALIAS
1948 #undef DEFINE_CGI_PARAM_CUSTOM
1949 #undef DEFINE_CGI_PARAM_RADIO
1950 #undef DEFINE_CGI_PARAM_NO_RADIO
1956 /*********************************************************************
1958 * Function : cgi_edit_actions_submit
1960 * Description : CGI function that actually edits the Actions list.
1963 * 1 : csp = Current client state (buffers, headers, etc...)
1964 * 2 : rsp = http_response data structure for output
1965 * 3 : parameters = map of cgi parameters
1967 * CGI Parameters : None
1969 * Returns : 0 on success, nonzero on error
1971 *********************************************************************/
1972 int cgi_edit_actions_submit(struct client_state *csp, struct http_response *rsp,
1973 struct map *parameters)
1976 struct map * exports;
1983 struct file_line * file;
1984 struct file_line * cur_line;
1988 const char * filename_param;
1991 rval = get_file_name_param(csp, parameters, ".action", &filename, &filename_param);
1994 /* No filename specified. */
1995 /* FIXME: Shouldn't FATAL here */
1996 log_error(LOG_LEVEL_FATAL, "No filename specified");
2000 if (get_number_param(csp, parameters, "section", §ionid))
2002 /* FIXME: Shouldn't FATAL here */
2003 log_error(LOG_LEVEL_FATAL, "No 'section' parameter");
2004 return cgi_default(csp, rsp, parameters);
2007 if (edit_read_actions_file(filename, &file))
2009 /* FIXME: Shouldn't FATAL here */
2010 log_error(LOG_LEVEL_FATAL, "Cannot load file '%s' for editing", filename);
2015 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2017 cur_line = cur_line->next;
2020 if ( (cur_line == NULL)
2021 || (line_number != sectionid)
2023 || (cur_line->type != FILE_LINE_ACTION))
2025 edit_free_file(file);
2026 return cgi_default(csp, rsp, parameters);
2029 if (actions_from_radio(parameters, cur_line->data.action))
2032 edit_free_file(file);
2036 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
2039 edit_free_file(file);
2043 len = strlen(actiontext);
2044 if (NULL == (newtext = malloc(len + 2)))
2048 edit_free_file(file);
2051 strcpy(newtext, actiontext);
2055 newtext[len + 1] = '\0';
2057 freez(cur_line->raw);
2058 freez(cur_line->unprocessed);
2059 cur_line->unprocessed = newtext;
2061 if (edit_write_file(filename, file))
2063 /* Error writing file */
2064 edit_free_file(file);
2065 /* FIXME: Shouldn't FATAL here */
2066 log_error(LOG_LEVEL_FATAL, "Cannot save file '%s' after editing", filename);
2070 edit_free_file(file);
2074 target = strdup("http://ijbswa.sourceforge.net/config/edit-actions-list?filename=");
2075 string_append(&target, filename_param);
2080 /* FIXME: Shouldn't FATAL here */
2081 log_error(LOG_LEVEL_FATAL, "Out of memory");
2085 rsp->status = strdup("302 Local Redirect from Junkbuster");
2086 enlist_unique_header(rsp->headers, "Location", target);
2090 /* return cgi_edit_actions_list(csp, rsp, parameters); */
2094 #endif /* def FEATURE_CGI_EDIT_ACTIONS */