Example #1
0
int fw_perform_return( gchar *action, GHashTable *conf, peer *p, gchar *result, int size) {
    GHashTable *data;
    gchar *cmd;
    FILE * fd;
    int ret=0;

    gchar dummy_result[30];
    if ( !result ) {
        result = dummy_result;
        size = sizeof(dummy_result);
    }
    
    data = g_hash_dup( conf );
    //
    // Than add specifics about this particular client, if any
    if (p != NULL) {
	g_hash_set( data, "IP",    p->ip );
	g_hash_set( data, "MAC",   p->hw );
	g_hash_set( data, "Class", "Public" );
    }

    cmd = conf_string( conf, action );
    cmd = parse_template( cmd, data );
    g_warning("Got command %s from action %s", cmd, action );
    
    // add data to the environment
    g_hash_table_foreach( data, (GHFunc) fw_popen_set_env, NULL );

    fd = popen(cmd, "r");
    if(fd == NULL) { 
        g_warning( "popen %s failed: %m", cmd );
        ret=1;
    }
    else if ( fgets(result, size, fd) == NULL ) {
        g_warning( "fgets %s failed: %m", cmd );
        ret=1;
    }
    if ( fd && pclose(fd) != 0 ) {
        g_warning( "pclose %s failed: %m", cmd );
        ret=1;
    }
    g_hash_free(data);
    g_free(cmd);
    return ret;
}
Example #2
0
GHashTable *http_parse_header (http_request *h, gchar *req) {
    GHashTable *head = g_hash_new();
    gchar **lines, **items, *key, *val, *p, prefix[50];
    guint i;

    h->method = NULL;
    h->uri    = NULL;
    h->header = head;
    
    if (req == NULL)
	return head;
    
    lines = g_strsplit( req, "\r\n", 0 );

    if (lines == NULL || lines[0] == NULL)
	return head;

    items = g_strsplit( lines[0], " ", 3 );

    h->method = g_strdup( items[0] );
    h->uri    = g_strdup( items[1] );
    // if (CONFd("Verbosity") >= 8) g_message( "http_parse_header: method_len: %d, uri_len: %d", strlen(h->method), strlen(h->uri));
    if (CONFd("Verbosity") >= 8) g_message( "http_parse_header: Method: %s", h->method );
    if (CONFd("Verbosity") >= 8) g_message( "http_parse_header: URI: %s", url_decode(h->uri) );
    g_strfreev( items );

    for (i = 1; lines[i] != NULL && lines[i][0] != '\0'; i++ ) {
	key = lines[i];
	val = strchr(key, ':');
	if (val != NULL) {
	    /* Separate the key from the value */
	    *val = '\0';

	    /* Normalize key -- lowercase every after 1st char */
	    for (p = key + 1; *p != '\0'; p++)
		*p = tolower(*p);

	    /* Strip ": " plus leading and trailing space from val */
	    g_strchomp( val += 2 ); // ": "

	    //if ( strcmp(key, "Referer" )== 0) {
	    //    if (CONFd("Verbosity") >= 8) g_message("http_parse_header: Referer: %s", url_decode(val) );
	    //} 
            //else {
	        if (CONFd("Verbosity") >= 8) {
                    g_snprintf(prefix, 50, "http_parse_header: %s: ", key);
                    syslog_message(prefix, url_decode(val), strlen(url_decode(val)) );
                }
	        g_hash_set( head, key, val );
	    //}
	}
    }

    g_strfreev( lines );
    h->header = head;
    return head;
}
Example #3
0
static int fw_exec( fw_action *act, GHashTable *conf ) {
    GHashTable *data;
    GPtrArray *env;
    gchar *cmd, **arg, **n;

    data = g_hash_dup( conf );
    //
    // Than add specifics about this particular client, if any
    if (act->p != NULL) {
	g_hash_set( data, "IP",    act->p->ip );
	g_hash_set( data, "MAC",   act->p->hw );
	g_hash_set( data, "Class", "Public" );
    }

    cmd = conf_string( conf, act->cmd );
    cmd = parse_template( cmd, data );
    g_message("Got command %s from action %s", cmd, act->cmd );
    arg = g_strsplit( cmd, " ", 0 );

    // prime the environment with our existing environment
    env = g_ptr_array_new();
    for ( n = environ; *n != NULL; n++ )
	g_ptr_array_add( env, *n );

    // Then add everything from the conf file
    g_hash_table_foreach( data, (GHFunc) fw_exec_add_env, env );

    // Add a closing NULL so execve knows where to lay off.
    g_ptr_array_add( env, NULL );

    /* We're not cleaning up memory references because 
     * hopefully the exec won't fail... */
    execve( *arg, arg, (char **)env->pdata );
    g_error( "execve %s failed: %m", cmd ); // Shouldn't happen.
    return -1;
}
Example #4
0
GHashTable *parse_query_string( gchar *query ) {
    GHashTable *data = g_hash_new();
    gchar **items, *key, *val, prefix[50];
    guint i;

    if (!query)
	return data;

    items = g_strsplit( query, "&", 0 );
    for ( i = 0; items[i] != NULL; i++ ) {
	key = items[i];
	if (key == NULL)
	    break;

	val = strchr( key, '=' );
	if (val != NULL)
	    *(val++) = '\0';
	else
	    val = "1";

	key = url_decode( key );	
	val = url_decode( val );	
	/* Irving - fix from Yurgi - check to see if the key is already in the
	   hash table.  This deals with keys that are set twice by web sites */
	if(g_hash_table_lookup_extended( data, key, NULL, NULL ) == FALSE ) {
		g_hash_set( data, key, val );
		if(CONFd("Verbosity") >= 8) {
                     g_snprintf(prefix, 50, "parse_query_string: %s=", key);
                     syslog_message(prefix, val, strlen(val));
                }
	}
	else
		if(CONFd("Verbosity") >= 8) g_message("parse_query_string: DUPLICATE key %s=%s, discarded.", key, val);

	g_free( key );
	g_free( val );
    }
    g_strfreev(items);

    return data;
}
Example #5
0
void http_add_header ( http_request *h, const gchar *key, gchar *val ) {
    if ( h->response == NULL )
	h->response = g_hash_new();
    g_hash_set( h->response, key, val );
}