Exemplo n.º 1
0
int
main(void)
{
	unsigned int    logged_in = 0;
	char           *username, *password = { NULL };
	cookie_t *cookies = cookies_init();
	cookie_t *our_cookie = cookie(COOKIE_NAME);
	char *template_path = "templates/portal";	
	cookie_t *c = NULL; 
	
	// Check for session cookie
	if (our_cookie) { // && cookie_valid(our_cookie)) {
		logged_in = 1;
		c = our_cookie;
		c->expires = 3600;
	} else {
	// New connection or expired...
		get_params();
		username = param("username");
		password = param("password");
		logged_in = CheckUserCredentials(username, password);
		if (logged_in) {
			c = cookie_create(username, password);
			c->expires = 3600;
		} else
			WebBork("HERE");
	}

	cookie_add(cookies, c);

	content_type_cookies("text/html", cookies);
	template_t    **t = template_init();
	template_input(t, "USERNAME", username);
	template_output(template_path, t);
	template_free(t);

	exit(EXIT_SUCCESS);
}
Exemplo n.º 2
0
static Cookie *parse_cookie(Octstr *cookiestr)
{
	char *v = NULL;
	char *p = NULL;
	int delta = 0;
	Cookie *c = NULL;
	Octstr **f = NULL;

	if (cookiestr == NULL) {
		error(0, "parse_cookie: NULL argument");
		return NULL;
	}

	v = gw_strdup(octstr_get_cstr (cookiestr));
	p = strtok(v, ";");
	
	c = cookie_create();	/* Never returns NULL */

	while (p != NULL) {
		while (isspace((int)*p)) p++;		/* Skip leading whitespace */

		if (strncasecmp("version", p, 7) == 0)
			f = &c -> version;
		else if (strncasecmp("path", p, 4) == 0)
			f = &c -> path;
		else if (strncasecmp("domain", p, 6) == 0)
			f = &c -> domain;	/* XXX DAVI: Shouldn't we check if domain is similar 
						 *           to real domain, and to set domain to
						 *           real domain if not set by header ??? */
		else if (strncasecmp("max-age", p, 7) == 0) {
			c -> max_age = atol(strrchr (p, '=') + 1);
			p = strtok(NULL, ";");
			continue;
		} 
		else if (strncasecmp("expires", p, 7) == 0) {
			delta = parse_http_date(p);
			if (delta != -1) 
				c->max_age = delta;
			p = strtok(NULL, ";");
			continue;
		}
		else if (strncasecmp("comment", p, 7) == 0 ) { /* Ignore comments */
			p = strtok(NULL, ";");
			continue;
		}
		else if (strncasecmp("secure", p, 6) == 0 ) { /* XXX DAVI: this should processed */
			p = strtok(NULL, ";");
			continue;
		}
		else {		/* Name value pair - this should be first */
			char *equals = NULL;

			if ((equals = strchr(p, '=')) != NULL) {
				*equals = '\0';

				c->name = octstr_create(p);
				c->value = octstr_create(equals + 1);
			} else {
				error(0, "parse_cookie: Bad name=value cookie component (%s)", p);
				cookie_destroy(c);
				return NULL;
			}
			p = strtok(NULL, ";");
			continue;
		}

		if (*f != NULL) {	/* Undefined behaviour - 4.2.2 */
			error(0, "parse_cookie: Duplicate cookie field (%s), discarding", p);
			p = strtok(NULL, ";");
			continue;
		}

		*f = octstr_create("$");
		octstr_append_cstr(*f, p);
		p = strtok(NULL, ";");
	}

	/* Process version - 4.3.4 
         * XXX DAVI: Altough it seems to be "MUST" in RFC, no one sends a Version 
         * tag when it's value is "0" 
	if (c->version == NULL) {
		c->version = octstr_create("");
		octstr_append_cstr(c->version, "$Version=\"0\";");
	}
	*/

	gw_free (v);
	return c;
}
Exemplo n.º 3
0
bool IOProxy::init( const char *config_file )
{
	FILE *file=0;
	int fd=-1;

	server = new ReliSock;
	if ( !server ) {
		dprintf(D_ALWAYS,"IOProxy: couldn't create socket\n");
		return false;
	}

	/* passing FALSE to bind means this is an incomming connection.
	 * however, here we are going to pass TRUE because only machines
	 * on this host need to connect to the ioproxy (chirp) socket,
	 * so there is no need to register it with a ccb broker. 
	 **/
	if(!server->bind(TRUE)) {
		dprintf(D_ALWAYS,"IOProxy: couldn't bind: %s\n",strerror(errno));
		return false;
	}

	if(!server->listen()) {
		dprintf(D_ALWAYS,"IOProxy: couldn't listen: %s\n",strerror(errno));
		return false;
	}

	cookie = cookie_create(IO_PROXY_COOKIE_SIZE);
	if(!cookie) {
		dprintf(D_ALWAYS,"IOProxy: couldn't create cookie: %s\n",strerror(errno));
		goto failure;
	}
	fd = safe_open_wrapper_follow(config_file,
	                       O_CREAT|O_TRUNC|O_WRONLY,
	                       0700);
	if(fd<0) {
		dprintf(D_ALWAYS,
		        "IOProxy: couldn't write to %s: %s\n",
		        config_file,
		        strerror(errno));
		goto failure;
	}

	file = fdopen(fd,"w");
	if(!file) {
		dprintf(D_ALWAYS,"IOProxy: couldn't create I/O stream: %s\n",strerror(errno));
		goto failure;
	}

	fprintf(file,"%s %d %s\n",my_ip_string(),server->get_port(),cookie);
	fclose(file);

	daemonCore->Register_Socket( server, "IOProxy listen socket", 
		(SocketHandlercpp) &IOProxy::connect_callback, 
		"IOProxy connect callback", this );
	socket_registered = true;
	return true;

	failure:
	if(cookie) free(cookie);
	if(file) fclose(file);
	unlink(config_file);
	server->close();
	return false;
}