+ **********************************************************************/
+\f
+
+#include "config.h"
+
+/*
+ * FIXME: Following includes copied from cgi.c - which are actually needed?
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+#include <assert.h>
+#include <limits.h>
+
+#ifdef _WIN32
+#define snprintf _snprintf
+#endif /* def _WIN32 */
+
+#include "project.h"
+#include "cgi.h"
+#include "cgiedit.h"
+#include "cgisimple.h"
+#include "list.h"
+#include "encode.h"
+#include "actions.h"
+#include "miscutil.h"
+#include "errlog.h"
+
+const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
+
+
+#ifdef FEATURE_CGI_EDIT_ACTIONS
+
+struct file_line
+{
+ struct file_line * next;
+ char * raw;
+ char * prefix;
+ char * unprocessed;
+ int type;
+
+ union
+ {
+ struct action_spec action[1];
+
+ struct
+ {
+ char * name;
+ char * svalue;
+ int ivalue;
+ } setting;
+
+ /* Add more data types here... e.g.
+
+
+ struct url_spec url[1];
+
+ struct
+ {
+ struct action_spec action[1];
+ const char * name;
+ } alias;
+
+ */
+
+ } data;
+};
+
+#define FILE_LINE_UNPROCESSED 1
+#define FILE_LINE_BLANK 2
+#define FILE_LINE_ALIAS_HEADER 3
+#define FILE_LINE_ALIAS_ENTRY 4
+#define FILE_LINE_ACTION 5
+#define FILE_LINE_URL 6
+#define FILE_LINE_SETTINGS_HEADER 7
+#define FILE_LINE_SETTINGS_ENTRY 8
+#define FILE_LINE_DESCRIPTION_HEADER 9
+#define FILE_LINE_DESCRIPTION_ENTRY 10
+
+/* FIXME: Following list of prototypes is not complete */
+/* FIXME: Following non-static functions should be prototyped in .h or made static */
+static int simple_read_line(char **dest, FILE *fp);
+static int edit_read_line (FILE *fp, char **raw_out, char **prefix_out, char **data_out);
+ int edit_read_file (FILE *fp, struct file_line ** pfile);
+ int edit_write_file (const char * filename, const struct file_line * file);
+ void edit_free_file (struct file_line * file);
+
+
+/* FIXME: This should be in project.h and used everywhere */
+#define JB_ERR_OK 0 /* Success, no error */
+#define JB_ERR_MEMORY 1 /* Out of memory */
+#define JB_ERR_CGI_PARAMS 2 /* Missing or corrupt CGI parameters */
+#define JB_ERR_FILE 3 /* Error opening, reading or writing a file */
+#define JB_ERR_PARSE 4 /* Error parsing file */
+#define JB_ERR_MODIFIED 5 /* File has been modified outside of the */
+ /* CGI actions editor. */
+
+
+/*********************************************************************
+ *
+ * Function : simple_read_line
+ *
+ * Description : Read a single line from a file and return it.
+ * This is basically a version of fgets() that malloc()s
+ * it's own line buffer. Note that the buffer will
+ * always be a multiple of BUFFER_SIZE bytes long.
+ * Therefore if you are going to keep the string for
+ * an extended period of time, you should probably
+ * strdup() it and free() the original, to save memory.
+ *
+ *
+ * Parameters :
+ * 1 : dest = destination for newly malloc'd pointer to
+ * line data. Will be set to NULL on error.
+ * 2 : fp = File to read from
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_MEMORY on out-of-memory
+ * JB_ERR_FILE on EOF.
+ *
+ *********************************************************************/
+static int simple_read_line(char **dest, FILE *fp)
+{
+ int len;
+ char * buf;
+ char * newbuf;
+
+ assert(fp);
+ assert(dest);
+
+ *dest = NULL;
+
+ if (NULL == (buf = malloc(BUFFER_SIZE)))
+ {
+ return JB_ERR_MEMORY;
+ }
+
+ *buf = '\0';
+ len = 0;
+
+ while (FOREVER)
+ {
+ newbuf = buf + len;
+ if ((!fgets(newbuf, BUFFER_SIZE, fp)) || (*newbuf == '\0'))
+ {
+ /* (*newbuf == '\0') should never happen unless fgets fails */
+ if (*buf == '\0')
+ {
+ free(buf);
+ return JB_ERR_FILE;
+ }
+ else
+ {
+ *dest = buf;
+ return JB_ERR_OK;
+ }
+ }
+ len = strlen(buf);
+ if ((buf[len - 1] == '\n') || (buf[len - 1] == '\r'))
+ {
+ *dest = buf;
+ return JB_ERR_OK;
+ }
+
+ if (NULL == (newbuf = realloc(buf, len + BUFFER_SIZE)))
+ {
+ free(buf);
+ return JB_ERR_MEMORY;
+ }
+ buf = newbuf;
+ }
+}
+
+
+/*********************************************************************
+ *
+ * Function : edit_read_line
+ *
+ * Description : Read a single non-empty line from a file and return
+ * it. Trims comments, leading and trailing whitespace
+ * and respects escaping of newline and comment char.
+ * Provides the line in 2 alternative forms: raw and
+ * preprocessed.
+ * - raw is the raw data read from the file. If the
+ * line is not modified, then this should be written
+ * to the new file.
+ * - prefix is any comments and blank lines that were
+ * read from the file. If the line is modified, then
+ * this should be written out to the file followed
+ * by the modified data. (If this string is non-empty
+ * then it will have a newline at the end).
+ * - data is the actual data that will be parsed
+ * further by appropriate routines.
+ * On EOF, the 3 strings will all be set to NULL and
+ * 0 will be returned.
+ *
+ * Parameters :
+ * 1 : fp = File to read from
+ * 2 : raw_out = destination for newly malloc'd pointer to
+ * raw line data. May be NULL if you don't want it.
+ * 3 : prefix_out = destination for newly malloc'd pointer to
+ * comments. May be NULL if you don't want it.
+ * 4 : data_out = destination for newly malloc'd pointer to
+ * line data with comments and leading/trailing spaces
+ * removed, and line continuation performed. May be
+ * NULL if you don't want it.
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_MEMORY on out-of-memory
+ * JB_ERR_FILE on EOF.
+ *
+ *********************************************************************/
+static int edit_read_line(FILE *fp, char **raw_out, char **prefix_out, char **data_out)
+{
+ char *p; /* Temporary pointer */
+ char *linebuf; /* Line read from file */
+ char *linestart; /* Start of linebuf, usually first non-whitespace char */
+ char newline[3]; /* Used to store the newline - "\n", "\r", or "\r\n" */
+ int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
+ char *raw; /* String to be stored in raw_out */
+ char *prefix; /* String to be stored in prefix_out */
+ char *data; /* String to be stored in data_out */
+ int rval = JB_ERR_OK;
+
+ assert(fp);
+
+ /* Set output parameters to NULL */
+ if (raw_out)
+ {
+ *raw_out = NULL;
+ }
+ if (prefix_out)
+ {
+ *prefix_out = NULL;
+ }
+ if (data_out)
+ {
+ *data_out = NULL;
+ }
+
+ /* Set string variables to new, empty strings. */
+
+ raw = malloc(1);
+ prefix = malloc(1);
+ data = malloc(1);
+
+ if ((raw == NULL) || (prefix == NULL) || (data == NULL))
+ {
+ freez(raw);
+ freez(prefix);
+ freez(data);
+ return JB_ERR_MEMORY;
+ }
+
+ *raw = '\0';
+ *prefix = '\0';
+ *data = '\0';
+
+ /* Main loop. Loop while we need more data & it's not EOF. */
+
+ while ( (contflag || (*data == '\0'))
+ && (JB_ERR_OK == (rval = simple_read_line(&linebuf, fp))))
+ {
+ if (string_append(&raw,linebuf))
+ {
+ free(prefix);
+ free(data);
+ free(linebuf);
+ return JB_ERR_MEMORY;
+ }
+
+ /* Trim off newline */
+ p = linebuf + strlen(linebuf);
+ if ((p != linebuf) && ((p[-1] == '\r') || (p[-1] == '\n')))
+ {
+ p--;
+ if ((p != linebuf) && ((p[-1] == '\r') || (p[-1] == '\n')))
+ {
+ p--;
+ }
+ }
+ strcpy(newline, p);
+ *p = '\0';
+
+ /* Line continuation? Trim escape and set flag. */
+ contflag = ((p != linebuf) && (*--p == '\\'));
+ if (contflag)
+ {
+ *p = '\0';
+ }
+
+ /* Trim leading spaces if we're at the start of the line */
+ linestart = linebuf;
+ if (*data == '\0')
+ {
+ /* Trim leading spaces */
+ while (*linestart && isspace((int)(unsigned char)*linestart))
+ {
+ linestart++;
+ }
+ }
+
+ /* Handle comment characters. */
+ p = linestart;
+ while ((p = strchr(p, '#')) != NULL)
+ {
+ /* Found a comment char.. */
+ if ((p != linebuf) && (*(p-1) == '\\'))
+ {
+ /* ..and it's escaped, left-shift the line over the escape. */
+ char *q = p - 1;
+ while ((*q = *(q + 1)) != '\0')
+ {
+ q++;
+ }
+ /* Now scan from just after the "#". */
+ }
+ else
+ {
+ /* Real comment. Save it... */
+ if (p == linestart)
+ {
+ /* Special case: Line only contains a comment, so all the
+ * previous whitespace is considered part of the comment.
+ * Undo the whitespace skipping, if any.
+ */
+ linestart = linebuf;
+ p = linestart;
+ }
+ string_append(&prefix,p);
+ if (string_append(&prefix,newline))
+ {
+ free(raw);
+ free(data);
+ free(linebuf);
+ return JB_ERR_MEMORY;
+ }
+ *newline = '\0';
+
+ /* ... and chop off the rest of the line */
+ *p = '\0';
+ }
+ } /* END while (there's a # character) */
+
+ /* Write to the buffer */
+ if (*linestart)
+ {
+ if (string_append(&data, linestart))
+ {
+ free(raw);
+ free(prefix);
+ free(linebuf);
+ return JB_ERR_MEMORY;
+ }
+ }
+
+ free(linebuf);
+ } /* END while(we need more data) */
+
+ /* Handle simple_read_line() errors - ignore EOF */
+ if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
+ {
+ free(raw);
+ free(prefix);
+ free(data);
+ return rval;
+ }
+
+
+ if (*raw)
+ {
+ /* Got at least some data */
+
+ /* Remove trailing whitespace */
+ chomp(data);
+
+ if (raw_out)
+ {
+ *raw_out = raw;
+ }
+ else
+ {
+ free(raw);
+ }
+ if (prefix_out)
+ {
+ *prefix_out = prefix;
+ }
+ else
+ {
+ free(prefix);
+ }
+ if (data_out)
+ {
+ *data_out = data;
+ }
+ else
+ {
+ free(data);
+ }
+ return(0);
+ }
+ else
+ {
+ /* EOF and no data there. */
+
+ free(raw);
+ free(prefix);
+ free(data);
+
+ return JB_ERR_FILE;
+ }
+}
+
+
+/*********************************************************************
+ *
+ * Function : edit_read_file
+ *
+ * Description : Read a complete file into memory.
+ * Handles whitespace, comments and line continuation.
+ *
+ * Parameters :
+ * 1 : fp = File to read from
+ * 2 : pfile = Destination for a linked list of file_lines.
+ * Will be set to NULL on error.
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_MEMORY on out-of-memory
+ *
+ *********************************************************************/
+int edit_read_file(FILE *fp, struct file_line ** pfile)
+{
+ struct file_line * first_line; /* Keep for return value or to free */
+ struct file_line * cur_line; /* Current line */
+ struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
+ int rval;
+
+ assert(fp);
+ assert(pfile);
+
+ *pfile = NULL;
+
+ cur_line = first_line = zalloc(sizeof(struct file_line));
+ if (cur_line == NULL)
+ {
+ return JB_ERR_MEMORY;
+ }
+
+ cur_line->type = FILE_LINE_UNPROCESSED;
+
+ rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed);
+ if (rval)
+ {
+ /* Out of memory or empty file. */
+ /* Note that empty file is not an error we propogate up */
+ free(cur_line);
+ return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
+ }
+
+ do
+ {
+ prev_line = cur_line;
+ cur_line = prev_line->next = zalloc(sizeof(struct file_line));
+ if (cur_line == NULL)
+ {
+ /* Out of memory */
+ edit_free_file(first_line);
+ return JB_ERR_MEMORY;
+ }
+
+ cur_line->type = FILE_LINE_UNPROCESSED;
+
+ rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed);
+ if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
+ {
+ /* Out of memory */
+ edit_free_file(first_line);
+ return JB_ERR_MEMORY;
+ }
+
+ }
+ while (rval != JB_ERR_FILE);
+
+ /* EOF */
+
+ /* We allocated one too many - free it */
+ prev_line->next = NULL;
+ free(cur_line);
+
+ *pfile = first_line;
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : edit_write_file
+ *
+ * Description : Write a complete file to disk.
+ *
+ * Parameters :
+ * 1 : filename = File to write to.
+ * 2 : file = Data structure to write.
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_FILE on error writing to file.
+ *
+ *********************************************************************/
+int edit_write_file(const char * filename, const struct file_line * file)
+{
+ FILE * fp;
+
+ assert(filename);
+
+ if (NULL == (fp = fopen(filename, "wt")))
+ {
+ return JB_ERR_FILE;
+ }
+
+ while (file != NULL)
+ {
+ if (file->raw)
+ {
+ if (fputs(file->raw, fp) < 0)
+ {
+ fclose(fp);
+ return JB_ERR_FILE;
+ }
+ }
+ else
+ {
+ if (file->prefix)
+ {
+ if (fputs(file->prefix, fp) < 0)
+ {
+ fclose(fp);
+ return JB_ERR_FILE;
+ }
+ }
+ if (file->unprocessed)
+ {
+ if (fputs(file->unprocessed, fp) < 0)
+ {
+ fclose(fp);
+ return JB_ERR_FILE;
+ }
+ if (fputs("\n", fp) < 0)
+ {
+ fclose(fp);
+ return JB_ERR_FILE;
+ }
+ }
+ else
+ {
+ /* FIXME: Write data from file->data->whatever */
+ assert(0);
+ }
+ }
+ file = file->next;
+ }
+
+ fclose(fp);
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : edit_free_file
+ *
+ * Description : Free a complete file in memory.
+ *
+ * Parameters :
+ * 1 : file = Data structure to free.
+ *
+ * Returns : N/A
+ *
+ *********************************************************************/
+void edit_free_file(struct file_line * file)
+{
+ struct file_line * next;
+
+ while (file != NULL)
+ {
+ next = file->next;
+ file->next = NULL;
+ freez(file->raw);
+ freez(file->prefix);
+ freez(file->unprocessed);
+ switch(file->type)
+ {
+ case 0: /* special case if memory zeroed */
+ case FILE_LINE_UNPROCESSED:
+ case FILE_LINE_BLANK:
+ case FILE_LINE_ALIAS_HEADER:
+ case FILE_LINE_SETTINGS_HEADER:
+ case FILE_LINE_DESCRIPTION_HEADER:
+ case FILE_LINE_DESCRIPTION_ENTRY:
+ case FILE_LINE_ALIAS_ENTRY:
+ case FILE_LINE_URL:
+ /* No data is stored for these */
+ break;
+
+ case FILE_LINE_ACTION:
+ free_action(file->data.action);
+ break;
+
+ case FILE_LINE_SETTINGS_ENTRY:
+ freez(file->data.setting.name);
+ freez(file->data.setting.svalue);
+ break;
+ default:
+ /* Should never happen */
+ assert(0);
+ break;
+ }
+ file->type = 0; /* paranoia */
+ free(file);
+ file = next;
+ }
+}
+
+
+/*********************************************************************
+ *
+ * Function : match_actions_file_header_line
+ *
+ * Description : Match an actions file {{header}} line
+ *
+ * Parameters :
+ * 1 : line - String from file
+ * 2 : name - Header to match against
+ *
+ * Returns : 0 iff they match.
+ *
+ *********************************************************************/
+static int match_actions_file_header_line(const char * line, const char * name)
+{
+ int len;
+
+ assert(line);
+ assert(name);
+
+ /* Look for "{{" */
+ if ((line[0] != '{') || (line[1] != '{'))
+ {
+ return 1;
+ }
+ line += 2;
+
+ /* Look for optional whitespace */
+ while ( (*line == ' ') || (*line == '\t') )
+ {
+ line++;
+ }
+
+ /* Look for the specified name (case-insensitive) */
+ len = strlen(name);
+ if (0 != strncmpic(line, name, len))
+ {
+ return 1;
+ }
+ line += len;
+
+ /* Look for optional whitespace */
+ while ( (*line == ' ') || (*line == '\t') )
+ {
+ line++;
+ }
+
+ /* Look for "}}" and end of string*/
+ if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
+ {
+ return 1;
+ }
+
+ /* It matched!! */
+ return 0;
+}
+
+
+/*********************************************************************
+ *
+ * Function : match_actions_file_header_line
+ *
+ * Description : Match an actions file {{header}} line
+ *
+ * Parameters :
+ * 1 : line - String from file. Must not start with
+ * whitespace (else infinite loop!)
+ * 2 : name - Destination for name
+ * 2 : name - Destination for value
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_MEMORY on out-of-memory
+ * JB_ERR_PARSE if there's no "=" sign, or if there's
+ * nothing before the "=" sign (but empty
+ * values *after* the "=" sign are legal).
+ *
+ *********************************************************************/
+static int split_line_on_equals(const char * line, char ** pname, char ** pvalue)
+{
+ const char * name_end;
+ const char * value_start;
+ int name_len;
+
+ assert(line);
+ assert(pname);
+ assert(pvalue);
+ assert(*line != ' ');
+ assert(*line != '\t');
+
+ *pname = NULL;
+ *pvalue = NULL;
+
+ value_start = strchr(line, '=');
+ if ((value_start == NULL) || (value_start == line))
+ {
+ return JB_ERR_PARSE;
+ }
+
+ name_end = value_start - 1;
+
+ /* Eat any whitespace before the '=' */
+ while ((*name_end == ' ') || (*name_end == '\t'))
+ {
+ /*
+ * we already know we must have at least 1 non-ws char
+ * at start of buf - no need to check
+ */
+ name_end--;
+ }
+
+ name_len = name_end - line + 1; /* Length excluding \0 */
+ if (NULL == (*pname = (char *) malloc(name_len + 1)))
+ {
+ return JB_ERR_MEMORY;
+ }
+ strncpy(*pname, line, name_len);
+ (*pname)[name_len] = '\0';
+
+ /* Eat any the whitespace after the '=' */
+ value_start++;
+ while ((*value_start == ' ') || (*value_start == '\t'))
+ {
+ value_start++;
+ }
+
+ if (NULL == (*pvalue = strdup(value_start)))
+ {
+ free(*pname);
+ *pname = NULL;
+ return JB_ERR_MEMORY;
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : edit_parse_actions_file
+ *
+ * Description : Parse an actions file in memory.
+ *
+ * Passed linked list must have the "data" member
+ * zeroed, and must contain valid "next" and
+ * "unprocessed" fields. The "raw" and "prefix"
+ * fields are ignored, and "type" is just overwritten.
+ *
+ * Note that on error the file may have been
+ * partially parsed.
+ *
+ * Parameters :
+ * 1 : file = Actions file to be parsed in-place.
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_MEMORY on out-of-memory
+ * JB_ERR_PARSE on error
+ *
+ *********************************************************************/
+int edit_parse_actions_file(struct file_line * file)
+{
+ struct file_line * cur_line;
+ int len;
+ const char * text; /* Text from a line */
+ char * name; /* For lines of the form name=value */
+ char * value; /* For lines of the form name=value */
+ struct action_alias * alias_list = NULL;
+ int rval = JB_ERR_OK;
+
+ /* alias_list contains the aliases defined in this file.
+ * It might be better to use the "file_line.data" fields
+ * in the relavent places instead.
+ */
+
+ cur_line = file;
+
+ /* A note about blank line support: Blank lines should only
+ * ever occur as the last line in the file. This function
+ * is more forgiving than that - FILE_LINE_BLANK can occur
+ * anywhere.
+ */
+
+ /* Skip leading blanks. Should only happen if file is
+ * empty (which is valid, but pointless).
+ */
+ while ( (cur_line != NULL)
+ && (cur_line->unprocessed[0] == '\0') )
+ {
+ /* Blank line */
+ cur_line->type = FILE_LINE_BLANK;
+ cur_line = cur_line->next;
+ }
+
+ if ( (cur_line != NULL)
+ && (cur_line->unprocessed[0] != '{') )
+ {
+ /* File doesn't start with a header */
+ return JB_ERR_PARSE;
+ }
+
+ if ( (cur_line != NULL) && (0 ==
+ match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
+ {
+ cur_line->type = FILE_LINE_SETTINGS_HEADER;
+
+ cur_line = cur_line->next;
+ while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
+ {
+ if (cur_line->unprocessed[0])
+ {
+ cur_line->type = FILE_LINE_SETTINGS_ENTRY;
+
+ rval = split_line_on_equals(cur_line->unprocessed,
+ &cur_line->data.setting.name,
+ &cur_line->data.setting.svalue);
+ if (rval != JB_ERR_OK)
+ {
+ /* Line does not contain a name=value pair, or out-of-memory */
+ return rval;
+ }
+ }
+ else
+ {
+ cur_line->type = FILE_LINE_BLANK;
+ }
+ cur_line = cur_line->next;
+ }
+ }
+
+ if ( (cur_line != NULL) && (0 ==
+ match_actions_file_header_line(cur_line->unprocessed, "description") ) )
+ {
+ cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
+
+ cur_line = cur_line->next;
+ while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
+ {
+ if (cur_line->unprocessed[0])
+ {
+ cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
+ }
+ else
+ {
+ cur_line->type = FILE_LINE_BLANK;
+ }
+ cur_line = cur_line->next;
+ }
+ }
+
+ if ( (cur_line != NULL) && (0 ==
+ match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
+ {
+ cur_line->type = FILE_LINE_ALIAS_HEADER;
+
+ cur_line = cur_line->next;
+ while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
+ {
+ if (cur_line->unprocessed[0])
+ {
+ /* define an alias */
+ struct action_alias * new_alias;
+
+ cur_line->type = FILE_LINE_ALIAS_ENTRY;
+
+ rval = split_line_on_equals(cur_line->unprocessed, &name, &value);
+ if (rval != JB_ERR_OK)
+ {
+ /* Line does not contain a name=value pair, or out-of-memory */
+ return rval;
+ }
+
+ if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
+ {
+ /* Out of memory */
+ free(name);
+ free(value);
+ free_alias_list(alias_list);
+ return JB_ERR_MEMORY;
+ }
+
+ if (get_actions(value, alias_list, new_alias->action))
+ {
+ /* Invalid action or out of memory */
+ free(name);
+ free(value);
+ free(new_alias);
+ free_alias_list(alias_list);
+ return JB_ERR_PARSE; /* FIXME: or JB_ERR_MEMORY */
+ }
+
+ free(value);
+
+ new_alias->name = name;
+
+ /* add to list */
+ new_alias->next = alias_list;
+ alias_list = new_alias;
+ }
+ else
+ {
+ cur_line->type = FILE_LINE_BLANK;
+ }
+ cur_line = cur_line->next;
+ }
+ }
+
+ /* Header done, process the main part of the file */
+ while (cur_line != NULL)
+ {
+ /* At this point, (cur_line->unprocessed[0] == '{') */
+ assert(cur_line->unprocessed[0] == '{');
+ text = cur_line->unprocessed + 1;
+ len = strlen(text) - 1;
+ if (text[len] != '}')
+ {
+ /* No closing } on header */
+ free_alias_list(alias_list);
+ return JB_ERR_PARSE;
+ }
+
+ if (text[0] == '{')
+ {
+ /* An invalid {{ header. */
+ free_alias_list(alias_list);
+ return JB_ERR_PARSE;
+ }
+
+ while ( (*text == ' ') || (*text == '\t') )
+ {
+ text++;
+ len--;
+ }
+ while ( (len > 0)
+ && ( (text[len - 1] == ' ')
+ || (text[len - 1] == '\t') ) )
+ {
+ len--;
+ }
+ if (len <= 0)
+ {
+ /* A line containing just { } */
+ free_alias_list(alias_list);
+ return JB_ERR_PARSE;
+ }
+
+ cur_line->type = FILE_LINE_ACTION;
+
+ /* Remove {} and make copy */
+ if (NULL == (value = (char *) malloc(len + 1)))
+ {
+ /* Out of memory */
+ free_alias_list(alias_list);
+ return JB_ERR_MEMORY;
+ }
+ strncpy(value, text, len);
+ value[len] = '\0';
+
+ /* Get actions */
+ if (get_actions(value, alias_list, cur_line->data.action))
+ {
+ /* Invalid action or out of memory */
+ free(value);
+ free_alias_list(alias_list);
+ return JB_ERR_PARSE; /* FIXME: or JB_ERR_MEMORY */
+ }
+
+ /* Done with string - it was clobbered anyway */
+ free(value);
+
+ /* Process next line */
+ cur_line = cur_line->next;
+
+ /* Loop processing URL patterns */
+ while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
+ {
+ if (cur_line->unprocessed[0])
+ {
+ /* Could parse URL here, but this isn't currently needed */
+
+ cur_line->type = FILE_LINE_URL;
+ }
+ else
+ {
+ cur_line->type = FILE_LINE_BLANK;
+ }
+ cur_line = cur_line->next;
+ }
+ } /* End main while(cur_line != NULL) loop */
+
+ free_alias_list(alias_list);
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : edit_read_file
+ *
+ * Description : Read a complete actions file into memory and
+ * parses it.
+ *
+ * Parameters :
+ * 1 : filename = Path to file to read from
+ * 2 : pfile = Destination for a linked list of file_lines.
+ * Will be set to NULL on error.
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_MEMORY on out-of-memory
+ * JB_ERR_FILE if the file cannot be opened or
+ * contains no data
+ *
+ *********************************************************************/
+int edit_read_actions_file(const char * filename, struct file_line ** pfile)
+{
+ struct file_line * file;
+ FILE * fp;
+ int rval;
+
+ assert(filename);
+ assert(pfile);
+
+ *pfile = NULL;
+
+ if (NULL == (fp = fopen(filename,"rt")))
+ {
+ return JB_ERR_FILE;
+ }
+
+ rval = edit_read_file(fp, &file);
+
+ fclose(fp);
+
+ if (JB_ERR_OK != rval)
+ {
+ return rval;
+ }
+
+ if (JB_ERR_OK != (rval = edit_parse_actions_file(file)))
+ {
+ edit_free_file(file);
+ return rval;
+ }
+
+ *pfile = file;
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : get_file_name_param
+ *
+ * Description : Get the name of the file to edit from the parameters
+ * passed to a CGI function. This function handles
+ * security checks such as blocking urls containing
+ * "/" or ".", prepending the config file directory,
+ * and adding the specified suffix.
+ *
+ * (This is an essential security check, otherwise
+ * users may be able to pass "../../../etc/passwd"
+ * and overwrite the password file [linux], "prn:"
+ * and print random data [Windows], etc...)
+ *
+ * This function only allows filenames contining the
+ * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
+ * That's probably too restrictive but at least it's
+ * secure.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : parameters = map of cgi parameters
+ * 3 : suffix = File extension, e.g. ".actions"
+ * 4 : pfilename = destination for full filename. Caller
+ * free()s. Set to NULL on error.
+ * 5 : pparam = destination for partial filename,
+ * suitable for use in another URL. Allocated as part
+ * of the map "parameters", so don't free it.
+ * Set to NULL if not specified.
+ *
+ * CGI Parameters : None
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_MEMORY on out-of-memory
+ * JB_ERR_CGI_PARAMS if "filename" was not specified
+ * or is not valid.
+ *
+ *********************************************************************/
+static int get_file_name_param(struct client_state *csp,
+ struct map *parameters,
+ char *suffix,
+ char **pfilename,
+ const char **pparam)
+{
+ const char *param;
+ const char *s;
+ char *name;
+ char *fullpath;
+ char ch;
+ int len;
+
+ assert(csp);
+ assert(parameters);
+ assert(suffix);
+ assert(pfilename);
+ assert(pparam);
+
+ *pfilename = NULL;
+ *pparam = NULL;
+
+ param = lookup(parameters, "filename");
+ if (!*param)
+ {
+ return JB_ERR_CGI_PARAMS;
+ }
+
+ *pparam = param;
+
+ len = strlen(param);
+ if (len >= FILENAME_MAX)
+ {
+ /* Too long. */
+ return JB_ERR_CGI_PARAMS;
+ }
+
+ /* Check every character to see if it's legal */
+ s = param;
+ while ((ch = *s++) != '\0')
+ {
+ if ( ((ch < 'A') || (ch > 'Z'))
+ && ((ch < 'a') || (ch > 'z'))
+ && ((ch < '0') || (ch > '9'))
+ && (ch != '-')
+ && (ch != '_') )
+ {
+ /* Probable hack attempt. */
+ return JB_ERR_CGI_PARAMS;
+ }
+ }
+
+ /* Append extension */
+ name = malloc(len + strlen(suffix) + 1);
+ if (name == NULL)
+ {
+ return JB_ERR_MEMORY;
+ }
+ strcpy(name, param);
+ strcpy(name + len, suffix);
+
+ /* Prepend path */
+ fullpath = make_path(csp->config->confdir, name);
+ free(name);
+ if (fullpath == NULL)
+ {
+ return JB_ERR_MEMORY;
+ }
+
+ /* Success */
+ *pfilename = fullpath;
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : get_number_param
+ *
+ * Description : Get a non-negative integer from the parameters
+ * passed to a CGI function.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : parameters = map of cgi parameters
+ * 3 : name = Name of CGI parameter to read
+ * 4 : pvalue = destination for value.
+ * Set to -1 on error.
+ *
+ * CGI Parameters : None
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_MEMORY on out-of-memory
+ * JB_ERR_CGI_PARAMS if "filename" was not specified
+ * or is not valid.
+ *
+ *********************************************************************/
+static int get_number_param(struct client_state *csp,
+ struct map *parameters,
+ char *name,
+ int *pvalue)
+{
+ const char *param;
+ char ch;
+ int value;