Esempio n. 1
0
void create_options(FILE *configfile, struct bftpd_option **options, struct directory **directories)
{
    char *str;
    struct bftpd_option *opt = NULL;
    struct directory *dir = NULL;
	str = config_read_line(configfile);
	while (!strchr(str, '}')) {
  		if (str[0] != '\n') {
            if ((strstr(str, "directory")) && (strchr(str, '{')) && (directories)) {
                char *tmp;
                if (dir) {
                    dir = dir->next = malloc(sizeof(struct directory));
                } else {
                    *directories = dir = malloc(sizeof(struct directory));
                }
                tmp = strchr(str, '"') + 1;
                *strchr(tmp, '"') = 0;
                dir->path = strdup(tmp);
                create_options(configfile, &(dir->options), NULL);
            } else {
       			if (opt) {
       				opt = opt->next = malloc(sizeof(struct bftpd_option));
       			} else {
       				*options = opt = malloc(sizeof(struct bftpd_option));
       			}
       			opt->name = (char *) malloc(strlen(str));
       			opt->value = (char *) malloc(strlen(str));
       			sscanf(str, "%[^=]=\"%[^\n\"]", opt->name, opt->value);
            }
   		}
		str = config_read_line(configfile);
	}
}
Esempio n. 2
0
void create_options(FILE *configfile, struct bftpd_option **options, struct directory **directories)
{
    char *str;
    struct bftpd_option *opt = NULL;
    struct directory *dir = NULL;

	str = config_read_line(configfile);
        if (! str)    /* no data from the file */
           return;
	while (!strchr(str, '}')) {
  		if (str[0] != '\n') {
            if ((strstr(str, "directory")) && (strchr(str, '{')) && (directories)) {
                char *tmp;
                if (dir) {
                    dir = dir->next = malloc(sizeof(struct directory));
                } else {
                    *directories = dir = malloc(sizeof(struct directory));
                }
                /* avoid memory bug */
                if (! dir) return;

                /* tmp = strchr(str, '"') + 1; */
                tmp = strchr(str, '"');
                if (tmp)
                {
                   tmp++;
                   *strchr(tmp, '"') = 0;
                   dir->path = strdup(tmp);
                }
                if (! dir->path) 
                   dir->path = "";
                create_options(configfile, &(dir->options), NULL);
            } else {
       			if (opt) {
       				opt = opt->next = malloc(sizeof(struct bftpd_option));
       			} else {
       				*options = opt = malloc(sizeof(struct bftpd_option));
       			}
                        /* bail out on memory error */
                        if (! opt)
                           return;

       			opt->name = (char *) malloc( strlen(str) + 2 );
       			// opt->value = (char *) malloc(strlen(str));
                        opt->value = (char *) malloc( strlen(str) + 256);
       			sscanf(str, "%[^=]=\"%[^\n\"]", opt->name, opt->value);
            }
   		}
		str = config_read_line(configfile);
                if (! str)    /* avoid segfault */
                   return;
	}
}
Esempio n. 3
0
File: utils.c Progetto: oau/streamer
// Reads plugin configuration
int config_plugin( uint32_t i_ident, char* dst, char* req_token ) {
  int ret = 0;
  char *value, *token;
  char ident[ 5 ], *p_ident = ident;
  *( uint32_t* )ident = i_ident;
  ident[ 4 ] = 0;
  FILE *cf;
  if( cf = config_open() ) {
    if( config_find_line( &p_ident, "plugin", cf ) ) {
      if( req_token == NULL ) {
        ret = 1;
      } else {
        while( config_read_line( &value, &token, cf ) ) {
          if( strcmp( token, "plugin" ) == 0 ) break;
          if( strcmp( token, req_token ) == 0 ) {
            if( dst ) strcpy( dst, value );
            ret = 1;
            break;
          }
        }
      }
    }
    fclose( cf );
  }
  return( ret );
}
Esempio n. 4
0
static void parse_listener(config_t *c, config_line_t *l, lwan_t *lwan)
{
    lwan->config.listener = strdup(l->section.param);

    while (config_read_line(c, l)) {
        switch (l->type) {
        case CONFIG_LINE_TYPE_LINE:
            config_error(c, "Expecting prefix section");
            return;
        case CONFIG_LINE_TYPE_SECTION:
            if (!strcmp(l->section.name, "prefix")) {
                parse_listener_prefix(c, l, lwan, NULL);
            } else {
                const lwan_module_t *module = lwan_module_find(lwan, l->section.name);
                if (!module) {
                    config_error(c, "Invalid section name or module not found: %s",
                        l->section.name);
                } else {
                    parse_listener_prefix(c, l, lwan, module);
                }
            }
            break;
        case CONFIG_LINE_TYPE_SECTION_END:
            return;
        }
    }

    config_error(c, "Expecting section end while parsing listener");
}
Esempio n. 5
0
File: lwan.c Progetto: diviaki/lwan
static bool setup_from_config(lwan_t *lwan)
{
    config_t conf;
    config_line_t line;
    bool has_listener = false;
    char path_buf[PATH_MAX];
    const char *path;

    path = get_config_path(path_buf);
    lwan_status_info("Loading configuration file: %s", path);

    lwan->url_map_trie = lwan_trie_new(destroy_urlmap);

    if (!config_open(&conf, path))
        return false;

    while (config_read_line(&conf, &line)) {
        switch (line.type) {
        case CONFIG_LINE_TYPE_LINE:
            if (!strcmp(line.line.key, "keep_alive_timeout"))
                lwan->config.keep_alive_timeout = parse_int(line.line.value,
                            default_config.keep_alive_timeout);
            else if (!strcmp(line.line.key, "quiet"))
                lwan->config.quiet = parse_bool(line.line.value,
                            default_config.quiet);
            else if (!strcmp(line.line.key, "reuse_port"))
                lwan->config.reuse_port = parse_bool(line.line.value,
                            default_config.reuse_port);
            else
                config_error(&conf, "Unknown config key: %s", line.line.key);
            break;
        case CONFIG_LINE_TYPE_SECTION:
            if (!has_listener) {
                has_listener = true;
                if (!strcmp(line.section.name, "listener"))
                    parse_listener(&conf, &line, lwan);
                else
                    config_error(&conf, "Unknown section type: %s", line.section.name);
            } else {
                config_error(&conf, "Only one listener supported");
            }
            break;
        case CONFIG_LINE_TYPE_SECTION_END:
            config_error(&conf, "Unexpected section end");
        }
    }

    if (conf.error_message) {
        lwan_status_critical("Error on config file \"%s\", line %d: %s",
              path, conf.line, conf.error_message);
    }

    config_close(&conf);

    return true;
}
Esempio n. 6
0
File: utils.c Progetto: oau/streamer
// Parse whole file using callback
void config_parse( int( *callback )( char*, char* ) ) {
  char *value, *token;
  FILE *cf;
  if( cf = config_open() ) {
    printf( "Config [info]: Reading rc...\n" );
    while( config_read_line( &value, &token, cf ) ) {
      if( callback( value, token ) ) break;
    }
    callback( NULL, NULL );
    fclose( cf );
  }
}
Esempio n. 7
0
void lwan_straitjacket_enforce(config_t *c, config_line_t *l)
{
    char *user_name = NULL;
    char *chroot_path = NULL;
    uid_t uid;
    gid_t gid;

    if (geteuid() != 0) {
        config_error(c, "Straitjacket requires root privileges");
        return;
    }

    while (config_read_line(c, l)) {
        switch (l->type) {
        case CONFIG_LINE_TYPE_LINE:
            /* TODO: limit_syscalls */
            if (!strcmp(l->line.key, "user")) {
                user_name = strdupa(l->line.value);
            } else if (!strcmp(l->line.key, "chroot")) {
                chroot_path = strdupa(l->line.value);
            } else {
                config_error(c, "Invalid key: %s", l->line.key);
                return;
            }
            break;
        case CONFIG_LINE_TYPE_SECTION:
            config_error(c, "Straitjacket accepts no sections");
            return;
        case CONFIG_LINE_TYPE_SECTION_END:
            if (!get_user_uid_gid(user_name, &uid, &gid)) {
                config_error(c, "Unknown user: %s", user_name);
                return;
            }

            if (chroot_path) {
                if (chroot(chroot_path) < 0) {
                    lwan_status_critical_perror("Could not chroot() to %s",
                        chroot_path);
                }
                lwan_status_debug("Jailed to %s", chroot_path);
            }

            if (!switch_to_user(uid, gid, user_name)) {
                lwan_status_critical("Could not drop privileges to %s, aborting",
                    user_name);
            }
            return;
        }
    }

    config_error(c, "Expecting section end while parsing straitjacket");
}
Esempio n. 8
0
void config_init()
{
	FILE *configfile;
	char *str;
    struct group_of_users *grp = NULL;
    struct user *usr = NULL;
    config_global.options = NULL;
    config_global.directories = NULL;
	if (!configpath)
		return;
	configfile = fopen(configpath, "r");
	if (!configfile) {
		control_printf(SL_FAILURE, "421 Unable to open configuration file.");
		exit(1);
	}
	while ((str = config_read_line(configfile))) {
		if (strchr(str, '{')) {
            replace(str, " {", "{");
            replace(str, "{ ", "{");
            replace(str, " }", "}");
            replace(str, "} ", "}");
            if (!strcasecmp(str, "global{\n")) {
                create_options(configfile, &(config_global.options), &(config_global.directories));
            } else if (strstr(str, "user ") == str) {
                if (usr) {
                    usr = usr->next = malloc(sizeof(struct user));
                } else {
                    config_users = usr = malloc(sizeof(struct user));
                }
                usr->name = strdup(str + 5);
                *strchr(usr->name, '{') = 0;
                create_options(configfile, &(usr->options), &(usr->directories));
            } else if (strstr(str, "group ") == str) {
                if (grp) {
                    grp = grp->next = malloc(sizeof(struct group_of_users));
                } else {
                    config_groups = grp = malloc(sizeof(struct group_of_users));
                }
                cutto(str, 6);
                *strchr(str, '{') = 0;
                grp->users = NULL;
                grp->next = NULL;
                grp->temp_members = strdup(str);
                create_options(configfile, &(grp->options), &(grp->directories));
            }
		}
	}
	fclose(configfile);
}
Esempio n. 9
0
File: utils.c Progetto: oau/streamer
// Search for and return the specified token(=value) pair
int config_find_line( char **find_value, char *find_token, FILE *f ) {
  char *value, *token;
  while( config_read_line( &value, &token, f ) ) {
    if( strcmp( token, find_token ) == 0 ) {
      if( *find_value == NULL ) {
        strcpy( *find_value, value );
        return( 1 );
      } else {
        if( strcmp( value, *find_value ) == 0 ) {
          return( 1 );
        }
      }
    }
  }
  return( 0 );
}
Esempio n. 10
0
 bool ConfigSections::ReadNext(std::string& line) {
   if(!fin) return false;
   if(!*fin) return false;
   current_section_changed=false;
   for(;;) {
     line=config_read_line(*fin);
     if(line=="") { // eof
       current_section="";
       current_section_n=-1;
       current_section_p=section_names.end();
       current_section_changed=true;
       return true;
     };
     std::string::size_type n=line.find_first_not_of(" \t");
     if(n == std::string::npos) continue; // should never happen
     if(line[n] == '[') {  // section
       n++; std::string::size_type nn = line.find(']',n);
       if(nn == std::string::npos) { line=""; return false; }; // missing ']'
       current_section=line.substr(n,nn-n);
       current_section_n=-1;
       current_section_p=section_names.end();
       current_section_changed=true;
       continue;
     };
     if(!section_names.empty()) { // only limited sections allowed
       bool match = false;
       int s_n = -1;
       for(std::list<std::string>::iterator sec = section_names.begin();
                                        sec!=section_names.end();++sec) {
         std::string::size_type len = sec->length();
         s_n++;
         if(strncasecmp(sec->c_str(),current_section.c_str(),len) != 0) continue;
         if(len != current_section.length()) {
           if(current_section[len] != '/') continue;
         };
         current_section_n=s_n;
         current_section_p=sec;
         match=true; break;
       };
       if(!match) continue;
     };
     line.erase(0,n);
     break;
   };
   return true;
 }
Esempio n. 11
0
static void parse_listener(struct config *c, struct config_line *l,
                           struct lwan *lwan)
{
    free(lwan->config.listener);
    lwan->config.listener = strdup(l->value);

    while (config_read_line(c, l)) {
        switch (l->type) {
        case CONFIG_LINE_TYPE_LINE:
            config_error(c, "Expecting prefix section");
            return;
        case CONFIG_LINE_TYPE_SECTION:
            if (streq(l->key, "prefix")) {
                parse_listener_prefix(c, l, lwan, NULL, NULL);
                continue;
            }

            if (l->key[0] == '&') {
                l->key++;

                void *handler = find_handler(l->key);
                if (handler) {
                    parse_listener_prefix(c, l, lwan, NULL, handler);
                    continue;
                }

                config_error(c, "Could not find handler name: %s", l->key);
                return;
            }

            const struct lwan_module *module = find_module(l->key);
            if (module) {
                parse_listener_prefix(c, l, lwan, module, NULL);
                continue;
            }

            config_error(c, "Invalid section or module not found: %s", l->key);
            return;
        case CONFIG_LINE_TYPE_SECTION_END:
            return;
        }
    }

    config_error(c, "Expecting section end while parsing listener");
}
Esempio n. 12
0
File: lwan.c Progetto: lpereira/lwan
static void parse_listener_prefix_authorization(struct config *c,
                                                struct config_line *l,
                                                struct lwan_url_map *url_map)
{
    if (!streq(l->value, "basic")) {
        config_error(c, "Only basic authorization supported");
        return;
    }

    memset(&url_map->authorization, 0, sizeof(url_map->authorization));

    while (config_read_line(c, l)) {
        switch (l->type) {
        case CONFIG_LINE_TYPE_LINE:
            if (streq(l->key, "realm")) {
                free(url_map->authorization.realm);
                url_map->authorization.realm = strdup(l->value);
            } else if (streq(l->key, "password_file")) {
                free(url_map->authorization.password_file);
                url_map->authorization.password_file = strdup(l->value);
            }
            break;

        case CONFIG_LINE_TYPE_SECTION:
            config_error(c, "Unexpected section: %s", l->key);
            goto error;

        case CONFIG_LINE_TYPE_SECTION_END:
            if (!url_map->authorization.realm)
                url_map->authorization.realm = strdup("Lwan");
            if (!url_map->authorization.password_file)
                url_map->authorization.password_file = strdup("htpasswd");

            url_map->flags |= HANDLER_MUST_AUTHORIZE;
            goto out;
        }
    }

out:
    return;

error:
    free(url_map->authorization.realm);
    free(url_map->authorization.password_file);
}
Esempio n. 13
0
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int config_load(char *filename, module* caller, const char *section)
{
	//Open the file
	FILE *config_file_in = config_open(filename, "rt");

	char config_line[BBI_MAX_LINE_LENGTH];
	if (config_file_in)
	{
		bool wanted_section = NULL == section;
		while (config_read_line(config_line, config_file_in))
		{
			if (config_line[0] == '[')
			{
				if (wanted_section) break;
				if (0 == stricmp(config_line, section))
					wanted_section = true;
				continue;
			}

			if (false == wanted_section)
				continue;

			if (config_line[0] == '@')
			{
				//Interpret the message
				message_interpret(config_line, false, caller);
			}
		}
		//Close the file
		fclose(config_file_in);
		return 0;
	}

	//Must have been an error
	if (!plugin_suppresserrors)
	{
		sprintf(config_line, "%s:\nThere was an error loading the configuration file.", filename);
		MessageBox(NULL, config_line, szAppName, MB_OK|MB_SYSTEMMODAL);
	}
	return 1;
}
Esempio n. 14
0
File: lwan.c Progetto: diviaki/lwan
static void parse_listener(config_t *c, config_line_t *l, lwan_t *lwan)
{
    lwan->config.port = parse_int(l->section.param, 8080);

    while (config_read_line(c, l)) {
        switch (l->type) {
        case CONFIG_LINE_TYPE_LINE:
            config_error(c, "Expecting prefix section");
            return;
        case CONFIG_LINE_TYPE_SECTION:
            if (!strcmp(l->section.name, "prefix"))
                parse_listener_prefix(c, l, lwan);
            else
                config_error(c, "Unknown section type: %s", l->section.name);
            break;
        case CONFIG_LINE_TYPE_SECTION_END:
            return;
        }
    }

    config_error(c, "Expecting section end while parsing listener");
}
Esempio n. 15
0
static bool find_section_end(config_t *config, config_line_t *line, int recursion_level)
{
    if (recursion_level > 10) {
        config_error(config, "Recursion level too deep");
        return false;
    }

    while (config_read_line(config, line)) {
        switch (line->type) {
        case CONFIG_LINE_TYPE_LINE:
            continue;
        case CONFIG_LINE_TYPE_SECTION:
            if (!find_section_end(config, line, recursion_level + 1))
                return false;
            break;
        case CONFIG_LINE_TYPE_SECTION_END:
            return true;
        }
    }

    return false;
}
Esempio n. 16
0
File: lwan.c Progetto: diviaki/lwan
static void parse_listener_prefix(config_t *c, config_line_t *l, lwan_t *lwan)
{
    lwan_url_map_t url_map = {0};
    struct hash *hash = hash_str_new(free, free);
    lwan_handler_t *handler = NULL;
    void *callback = NULL;
    char *prefix = strdupa(l->line.value);
    void *data = NULL;

    while (config_read_line(c, l)) {
      switch (l->type) {
      case CONFIG_LINE_TYPE_LINE:
          if (!strcmp(l->line.key, "handler")) {
              handler = find_symbol(l->line.value);
              if (!handler) {
                  config_error(c, "Could not find handler \"%s\"", l->line.value);
                  goto out;
              }
          } else if (!strcmp(l->line.key, "callback")) {
              callback = find_symbol(l->line.value);
              if (!callback) {
                  config_error(c, "Could not find callback \"%s\"", l->line.value);
                  goto out;
              }
          } else {
              hash_add(hash, strdup(l->line.key), strdup(l->line.value));
          }

          break;
      case CONFIG_LINE_TYPE_SECTION:
          if (!strcmp(l->section.name, "authorization")) {
              parse_listener_prefix_authorization(c, l, &url_map);
          } else {
              config_error(c, "Unknown section type: \"%s\"", l->section.name);
              goto out;
          }

          break;
      case CONFIG_LINE_TYPE_SECTION_END:
          goto add_map;
      }
    }

    config_error(c, "Expecting section end while parsing prefix");
    goto out;

add_map:
    if (handler == callback && !callback) {
        config_error(c, "Missing callback or handler");
        goto out;
    }
    if (handler && callback) {
        config_error(c, "Callback and handler are mutually exclusive");
        goto out;
    }

    if (callback) {
        url_map.callback = callback;
        url_map.flags |= HANDLER_PARSE_MASK;
        url_map.data = data;
        url_map.handler = NULL;
    } else if (handler && handler->init_from_hash && handler->handle) {
        url_map.data = handler->init_from_hash(hash);
        url_map.callback = handler->handle;
        url_map.flags |= handler->flags;
        url_map.handler = handler;
    } else {
        config_error(c, "Invalid handler");
        goto out;
    }

    add_url_map(lwan->url_map_trie, prefix, &url_map);

out:
    hash_free(hash);
}
Esempio n. 17
0
int parse_metadata() {
	int returnV = 0;
	int done = 0;
	char *line;
	
	do {
		do {
			line = config_read_line();
			if (*line == '\n' || *line == '#') {
				free(line);
				line = NULL;
			}
		} while (!line);
		
		// Required metadata
		if (config_key_match(line, "name")) {
			packager.pkgname = config_get_string(line);
			packager.mdlen++;
		} else if (config_key_match(line, "repo")) {
			packager.repo = config_get_string(line);
			packager.mdlen++;
		} else if (config_key_match(line, "version")) {
			config_get_version(line, '=', &packager.version);
			packager.mdlen++;
		}
		// Optional metadata
		else if (config_key_match(line, "description")) {
			packager.description = config_get_string(line);
			packager.mdlen++;
		} else if (config_key_match(line, "dependencies")) {
			// A bit more complicated
			packager.mdlen++;
			char *deplist = config_get_string(line);
			int i, j = 0;
			for (i = 0; 1; ++i) {
				if (deplist[i] == ' ' || deplist[i] == '\0') {
					char *dep = malloc(strlen(deplist));
					strcpy(dep, deplist + j);
					dep[i - j] = '\0';
					j = i + 1;
					versionData *version = malloc(sizeof(version));
					version->major = version->minor = version->patch = 0;
					if (strchr(dep, ':') != NULL) {
						config_get_version(dep, ':', version);
						*strchr(dep, ':') = '\0';
					}
					packageDependency *depv = malloc(sizeof(packageDependency));
					depv->version = *version;
					depv->name = dep;
					packager.dependencies[packager.dependencies_len] = depv;
					packager.dependencies_len++;
				}
				if (deplist[i] == '\0') {
					break;
				}
			}
		} else if (config_key_match(line, "author")) {
			packager.author = config_get_string(line);
			packager.mdlen++;
		} else if (config_key_match(line, "maintainer")) {
			packager.maintainer = config_get_string(line);
			packager.mdlen++;
		} else if (config_key_match(line, "copyright")) {
			packager.copyright = config_get_string(line);
			packager.mdlen++;
		} else if (config_key_match(line, "infourl")) {
			packager.infourl = config_get_string(line);
			packager.mdlen++;
		}
		
		done = *line == '\0';
		free(line);
	} while (!done);
	
	if (!packager.pkgname) {
		printf("Error: configuration does not include package name.\n");
		exit(1);
	}
	if (!packager.repo) {
		printf("Error: configuration does not include package repository.\n");
		exit(1);
	}
	if (packager.version.major == 0xff || packager.version.minor == 0xff || packager.version.patch == 0xff) {
		printf("Error: configuration does not include valid package version.\n");
		exit(1);
	}
	
	return returnV;
}
Esempio n. 18
0
static void parse_listener_prefix(config_t *c, config_line_t *l, lwan_t *lwan,
    const lwan_module_t *module)
{
    lwan_url_map_t url_map = { };
    struct hash *hash = hash_str_new(free, free);
    void *handler = NULL;
    char *prefix = strdupa(l->line.value);
    config_t isolated = { };

    if (!config_isolate_section(c, l, &isolated)) {
        config_error(c, "Could not isolate configuration file");
        goto out;
    }

    while (config_read_line(c, l)) {
      switch (l->type) {
      case CONFIG_LINE_TYPE_LINE:
          if (!strcmp(l->line.key, "module")) {
              if (module) {
                  config_error(c, "Module already specified");
                  goto out;
              }
              module = lwan_module_find(lwan, l->line.value);
              if (!module) {
                  config_error(c, "Could not find module \"%s\"", l->line.value);
                  goto out;
              }
          } else if (!strcmp(l->line.key, "handler")) {
              handler = find_handler_symbol(l->line.value);
              if (!handler) {
                  config_error(c, "Could not find handler \"%s\"", l->line.value);
                  goto out;
              }
          } else {
              hash_add(hash, strdup(l->line.key), strdup(l->line.value));
          }

          break;
      case CONFIG_LINE_TYPE_SECTION:
          if (!strcmp(l->section.name, "authorization")) {
              parse_listener_prefix_authorization(c, l, &url_map);
          } else {
              if (!config_skip_section(c, l)) {
                  config_error(c, "Could not skip section");
                  goto out;
              }
          }

          break;
      case CONFIG_LINE_TYPE_SECTION_END:
          goto add_map;
      }
    }

    config_error(c, "Expecting section end while parsing prefix");
    goto out;

add_map:
    if (module == handler && !handler) {
        config_error(c, "Missing module or handler");
        goto out;
    }
    if (module && handler) {
        config_error(c, "Handler and module are mutually exclusive");
        goto out;
    }

    if (handler) {
        url_map.handler = handler;
        url_map.flags |= HANDLER_PARSE_MASK;
        url_map.data = hash;
        url_map.module = NULL;

        hash = NULL;
    } else if (module && module->init_from_hash && module->handle) {
        url_map.data = module->init_from_hash(hash);
        if (isolated.file && module->parse_conf) {
            if (!module->parse_conf(url_map.data, &isolated)) {
                config_error(c, "Error from module: %s",
                    isolated.error_message ? isolated.error_message : "Unknown");
                goto out;
            }
        }
        url_map.handler = module->handle;
        url_map.flags |= module->flags;
        url_map.module = module;
    } else {
        config_error(c, "Invalid handler");
        goto out;
    }

    add_url_map(&lwan->url_map_trie, prefix, &url_map);

out:
    hash_free(hash);
    config_close(&isolated);
}
Esempio n. 19
0
static bool setup_from_config(lwan_t *lwan)
{
    config_t conf;
    config_line_t line;
    bool has_listener = false;
    char path_buf[PATH_MAX];
    const char *path;

    path = get_config_path(path_buf);
    lwan_status_info("Loading configuration file: %s", path);

    if (!lwan_trie_init(&lwan->url_map_trie, destroy_urlmap))
        return false;

    if (!config_open(&conf, path))
        return false;

    while (config_read_line(&conf, &line)) {
        switch (line.type) {
        case CONFIG_LINE_TYPE_LINE:
            if (!strcmp(line.line.key, "keep_alive_timeout"))
                lwan->config.keep_alive_timeout = (unsigned short)parse_long(line.line.value,
                            default_config.keep_alive_timeout);
            else if (!strcmp(line.line.key, "quiet"))
                lwan->config.quiet = parse_bool(line.line.value,
                            default_config.quiet);
            else if (!strcmp(line.line.key, "reuse_port"))
                lwan->config.reuse_port = parse_bool(line.line.value,
                            default_config.reuse_port);
            else if (!strcmp(line.line.key, "proxy_protocol"))
                lwan->config.proxy_protocol = parse_bool(line.line.value,
                            default_config.proxy_protocol);
            else if (!strcmp(line.line.key, "expires"))
                lwan->config.expires = parse_time_period(line.line.value,
                            default_config.expires);
            else if (!strcmp(line.line.key, "error_template")) {
                free(lwan->config.error_template);
                lwan->config.error_template = strdup(line.line.value);
            } else if (!strcmp(line.line.key, "threads")) {
                long n_threads = parse_long(line.line.value, default_config.n_threads);
                if (n_threads < 0)
                    config_error(&conf, "Invalid number of threads: %d", n_threads);
                lwan->config.n_threads = (unsigned short int)n_threads;
            }
            else
                config_error(&conf, "Unknown config key: %s", line.line.key);
            break;
        case CONFIG_LINE_TYPE_SECTION:
            if (!strcmp(line.section.name, "listener")) {
                if (!has_listener) {
                    parse_listener(&conf, &line, lwan);
                    has_listener = true;
                } else {
                    config_error(&conf, "Only one listener supported");
                }
            } else if (!strcmp(line.section.name, "straitjacket")) {
                lwan_straitjacket_enforce(&conf, &line);
            } else {
                config_error(&conf, "Unknown section type: %s", line.section.name);
            }
            break;
        case CONFIG_LINE_TYPE_SECTION_END:
            config_error(&conf, "Unexpected section end");
        }
    }

    if (conf.error_message) {
        lwan_status_critical("Error on config file \"%s\", line %d: %s",
              path, conf.line, conf.error_message);
    }

    config_close(&conf);

    return true;
}
Esempio n. 20
0
File: lwan.c Progetto: lpereira/lwan
static bool setup_from_config(struct lwan *lwan, const char *path)
{
    struct config *conf;
    struct config_line line;
    bool has_listener = false;
    char path_buf[PATH_MAX];

    if (!path)
        path = lwan_get_config_path(path_buf, sizeof(path_buf));
    lwan_status_info("Loading configuration file: %s", path);

    conf = config_open(path);
    if (!conf)
        return false;

    if (!lwan_trie_init(&lwan->url_map_trie, destroy_urlmap))
        return false;

    while (config_read_line(conf, &line)) {
        switch (line.type) {
        case CONFIG_LINE_TYPE_LINE:
            if (streq(line.key, "keep_alive_timeout")) {
                lwan->config.keep_alive_timeout = (unsigned short)parse_long(
                    line.value, default_config.keep_alive_timeout);
            } else if (streq(line.key, "quiet")) {
                lwan->config.quiet =
                    parse_bool(line.value, default_config.quiet);
            } else if (streq(line.key, "reuse_port")) {
                lwan->config.reuse_port =
                    parse_bool(line.value, default_config.reuse_port);
            } else if (streq(line.key, "proxy_protocol")) {
                lwan->config.proxy_protocol =
                    parse_bool(line.value, default_config.proxy_protocol);
            } else if (streq(line.key, "allow_cors")) {
                lwan->config.allow_cors =
                    parse_bool(line.value, default_config.allow_cors);
            } else if (streq(line.key, "expires")) {
                lwan->config.expires =
                    parse_time_period(line.value, default_config.expires);
            } else if (streq(line.key, "error_template")) {
                free(lwan->config.error_template);
                lwan->config.error_template = strdup(line.value);
            } else if (streq(line.key, "threads")) {
                long n_threads =
                    parse_long(line.value, default_config.n_threads);
                if (n_threads < 0)
                    config_error(conf, "Invalid number of threads: %ld",
                                 n_threads);
                lwan->config.n_threads = (unsigned short int)n_threads;
            } else if (streq(line.key, "max_post_data_size")) {
                long max_post_data_size = parse_long(
                    line.value, (long)default_config.max_post_data_size);
                if (max_post_data_size < 0)
                    config_error(conf, "Negative maximum post data size");
                else if (max_post_data_size > 128 * (1 << 20))
                    config_error(conf,
                                 "Maximum post data can't be over 128MiB");
                lwan->config.max_post_data_size = (size_t)max_post_data_size;
            } else if (streq(line.key, "allow_temp_files")) {
                lwan->config.allow_post_temp_file =
                    !!strstr(line.value, "post");
            } else {
                config_error(conf, "Unknown config key: %s", line.key);
            }
            break;
        case CONFIG_LINE_TYPE_SECTION:
            if (streq(line.key, "listener")) {
                if (!has_listener) {
                    parse_listener(conf, &line, lwan);
                    has_listener = true;
                } else {
                    config_error(conf, "Only one listener supported");
                }
            } else if (streq(line.key, "straitjacket")) {
                lwan_straitjacket_enforce_from_config(conf);
            } else {
                config_error(conf, "Unknown section type: %s", line.key);
            }
            break;
        case CONFIG_LINE_TYPE_SECTION_END:
            config_error(conf, "Unexpected section end");
        }
    }

    if (config_last_error(conf)) {
        lwan_status_critical("Error on config file \"%s\", line %d: %s", path,
                             config_cur_line(conf), config_last_error(conf));
    }

    config_close(conf);

    return true;
}
Esempio n. 21
0
File: lwan.c Progetto: lpereira/lwan
static void parse_listener_prefix(struct config *c,
                                  struct config_line *l,
                                  struct lwan *lwan,
                                  const struct lwan_module *module,
                                  void *handler)
{
    struct lwan_url_map url_map = {};
    struct hash *hash = hash_str_new(free, free);
    char *prefix = strdupa(l->value);
    struct config *isolated;

    isolated = config_isolate_section(c, l);
    if (!isolated) {
        config_error(c, "Could not isolate configuration file");
        goto out;
    }

    while (config_read_line(c, l)) {
        switch (l->type) {
        case CONFIG_LINE_TYPE_LINE:
            hash_add(hash, strdup(l->key), strdup(l->value));
            break;

        case CONFIG_LINE_TYPE_SECTION:
            if (streq(l->key, "authorization")) {
                parse_listener_prefix_authorization(c, l, &url_map);
            } else if (!config_skip_section(c, l)) {
                config_error(c, "Could not skip section");
                goto out;
            }
            break;

        case CONFIG_LINE_TYPE_SECTION_END:
            goto add_map;
        }
    }

    config_error(c, "Expecting section end while parsing prefix");
    goto out;

add_map:
    assert((handler && !module) || (!handler && module));

    if (handler) {
        url_map.handler = handler;
        url_map.flags |= HANDLER_PARSE_MASK | HANDLER_DATA_IS_HASH_TABLE;
        url_map.data = hash;
        url_map.module = NULL;

        hash = NULL;
    } else if (module->create_from_hash && module->handle_request) {
        url_map.data = module->create_from_hash(prefix, hash);
        if (!url_map.data) {
            config_error(c, "Could not create module instance");
            goto out;
        }

        if (module->parse_conf && !module->parse_conf(url_map.data, isolated)) {
            const char *msg = config_last_error(isolated);

            config_error(c, "Error from module: %s", msg ? msg : "Unknown");
            goto out;
        }

        url_map.handler = module->handle_request;
        url_map.flags |= module->flags;
        url_map.module = module;
    } else if (UNLIKELY(!module->create_from_hash)) {
        config_error(c, "Module isn't prepared to load settings from a file; "
                        "create_from_hash() method isn't present");
        goto out;
    } else if (UNLIKELY(!module->handle_request)) {
        config_error(c, "Module does not have handle_request() method");
        goto out;
    }

    add_url_map(&lwan->url_map_trie, prefix, &url_map);

out:
    hash_free(hash);
    config_close(isolated);
}
Esempio n. 22
0
/*
This function opens the config file and
tries to reset some of the option values
in memory.
-- Jesse
*/
void Reread_Config_File()
{
    char *line;     // line in config file
    char *config_value;    // value stored in memory
    char *new_value;
    FILE *config_file;
    int xfer_delay;
    int section = 0;     // where are we in the config file
    unsigned long get_value;

    // open config file
    config_file = fopen(configpath, "r");
    if (! config_file)
       return;

    /* read a line from the config file */
    line = config_read_line(config_file);
    while ( line )
    {
       if ( strchr(line, '{') )
          section++;

       /* look for reconized option name */
       if ( strstr(line, "HELLO_STRING") )
          config_value = config_getoption_reread("HELLO_STRING");
       else if ( strstr(line, "QUIT_MSG") )
          config_value = config_getoption_reread("QUIT_MSG");
       else if ( strstr(line, "XFERBUFSIZE") )
          config_value = config_getoption_reread("XFERBUFSIZE");
       else if ( strstr(line, "DATA_TIMEOUT") )
          config_value = config_getoption_reread("DATA_TIMEOUT");
       else if ( strstr(line, "CONTROL_TIMEOUT") )
          config_value = config_getoption_reread("CONTROL_TIMEOUT");
       else if ( strstr(line, "USERLIMIT_GLOBAL") )
          config_value = config_getoption_reread("USERLIMIT_GLOBAL");
       else if ( strstr(line, "USERLIMIT_SINGLEUSER") )
          config_value = config_getoption_reread("USERLIMIT_SINGLEUSER");
       else if ( strstr(line, "USERLIMIT_HOST") )
          config_value = config_getoption_reread("USERLIMIT_HOST");
       else if ( strstr(line, "DENY_LOGIN") )
          config_value = config_getoption_reread("DENY_LOGIN");
       else if ( strstr(line, "XFER_DELAY") )
          config_value = config_getoption_reread("XFER_DELAY");
       else if ( strstr(line, "GZ_UPLOAD") )
          config_value = config_getoption_reread("GZ_UPLOAD");
       else
          config_value = NULL;

       /* get new value from input */
       new_value = strchr(line, '"') ;
       if (new_value)
       {
           char *temp;
           new_value++;      // go to first character after quote
           temp = strchr(new_value, '"');  
           if (temp)
              temp[0] = '\0';    // null terminal string
       }

       /* set value of option */
       if ( (config_value) && (new_value) && (section == 1) )
       {
           // make sure it will fit.
           if ( strlen(new_value) < 256) 
              strcpy(config_value, new_value);
       }

       line = config_read_line(config_file);
    }       /* while not end of file */

    fclose(config_file);

    /* reset numeric values */
    get_value = strtoul( config_getoption("XFERBUFSIZE"), NULL, 0 );
    if (get_value <= INT_MAX)
       xfer_bufsize = get_value;
    else
       xfer_bufsize = XFER_BUFSIZE;

    if (! xfer_bufsize )
       xfer_bufsize = XFER_BUFSIZE;

    control_timeout = atoi( config_getoption("CONTROL_TIMEOUT") );
    if (! control_timeout)
       control_timeout = CONTROL_TIMEOUT;

    data_timeout = atoi( config_getoption("DATA_TIMEOUT") );
    if (! data_timeout)
       data_timeout = DATA_TIMEOUT;

    xfer_delay = atoi( config_getoption("XFER_DELAY") );
}
Esempio n. 23
0
static void parse_listener_prefix(struct config *c, struct config_line *l,
                                  struct lwan *lwan,
                                  const struct lwan_module *module,
                                  void *handler)
{
    struct lwan_url_map url_map = {};
    struct hash *hash = hash_str_new(free, free);
    char *prefix = strdupa(l->value);
    struct config *isolated;

    isolated = config_isolate_section(c, l);
    if (!isolated) {
        config_error(c, "Could not isolate configuration file");
        goto out;
    }

    while (config_read_line(c, l)) {
        switch (l->type) {
        case CONFIG_LINE_TYPE_LINE:
            if (streq(l->key, "module")) {
                if (module) {
                    config_error(c, "Module already specified");
                    goto out;
                }
                module = find_module(l->value);
                if (!module) {
                    config_error(c, "Could not find module \"%s\"", l->value);
                    goto out;
                }
            } else if (streq(l->key, "handler")) {
                if (handler) {
                    config_error(c, "Handler already specified");
                    goto out;
                }
                handler = find_handler(l->value);
                if (!handler) {
                    config_error(c, "Could not find handler \"%s\"", l->value);
                    goto out;
                }
            } else {
                hash_add(hash, strdup(l->key), strdup(l->value));
            }

            break;
        case CONFIG_LINE_TYPE_SECTION:
            if (streq(l->key, "authorization")) {
                parse_listener_prefix_authorization(c, l, &url_map);
            } else {
                if (!config_skip_section(c, l)) {
                    config_error(c, "Could not skip section");
                    goto out;
                }
            }

            break;
        case CONFIG_LINE_TYPE_SECTION_END:
            goto add_map;
        }
    }

    config_error(c, "Expecting section end while parsing prefix");
    goto out;

add_map:
    if (module == handler && !handler) {
        config_error(c, "Missing module or handler");
        goto out;
    }
    if (module && handler) {
        config_error(c, "Handler and module are mutually exclusive");
        goto out;
    }

    if (handler) {
        url_map.handler = handler;
        url_map.flags |= HANDLER_PARSE_MASK | HANDLER_DATA_IS_HASH_TABLE;
        url_map.data = hash;
        url_map.module = NULL;

        hash = NULL;
    } else if (module && module->create_from_hash && module->handle_request) {
        url_map.data = module->create_from_hash(prefix, hash);

        if (module->parse_conf && !module->parse_conf(url_map.data, isolated)) {
            const char *msg = config_last_error(isolated);

            config_error(c, "Error from module: %s", msg ? msg : "Unknown");
            goto out;
        }

        url_map.handler = module->handle_request;
        url_map.flags |= module->flags;
        url_map.module = module;
    } else {
        config_error(c, "Invalid handler");
        goto out;
    }

    add_url_map(&lwan->url_map_trie, prefix, &url_map);

out:
    hash_free(hash);
    config_close(isolated);
}