Esempio n. 1
0
static int	
WebServletRun(struct http_servlet *servlet,
                         struct http_request *req, struct http_response *resp)
{
    FILE *f;
    const char *path;
    const char *query;
    int priv = 0;
    
    if (Enabled(&gWeb.options, WEB_AUTH)) {
	const char *username;
	const char *password;
	ConsoleUser		u;
	struct console_user	iu;

	/* Get username and password */
	if ((username = http_request_get_username(req)) == NULL)
    	    username = "";
	if ((password = http_request_get_password(req)) == NULL)
    	    password = "";

	strlcpy(iu.username, username, sizeof(iu.username));
	RWLOCK_RDLOCK(gUsersLock);
	u = ghash_get(gUsers, &iu);
	RWLOCK_UNLOCK(gUsersLock);

	if ((u == NULL) || strcmp(u->password, password)) {
		http_response_send_basic_auth(resp, "Access Restricted");
		return (1);
	}
	priv = u->priv;
    }

    if (!(f = http_response_get_output(resp, 1))) {
	return 0;
    }
    if (!(path = http_request_get_path(req)))
	return 0;
    if (!(query = http_request_get_query_string(req)))
	return 0;

    if (!strcmp(path,"/mpd.css")) {
	http_response_set_header(resp, 0, "Content-Type", "text/css");
	WebShowCSS(f);
    } else if (!strcmp(path,"/bincmd")) {
	http_response_set_header(resp, 0, "Content-Type", "text/plain");
	http_response_set_header(resp, 1, "Pragma", "no-cache");
	http_response_set_header(resp, 1, "Cache-Control", "no-cache, must-revalidate");
	
	pthread_cleanup_push(WebServletRunCleanup, NULL);
	GIANT_MUTEX_LOCK();
	WebRunBinCmd(f, query, priv);
	GIANT_MUTEX_UNLOCK();
	pthread_cleanup_pop(0);
    } else if (!strcmp(path,"/") || !strcmp(path,"/cmd")) {
	http_response_set_header(resp, 0, "Content-Type", "text/html");
	http_response_set_header(resp, 1, "Pragma", "no-cache");
	http_response_set_header(resp, 1, "Cache-Control", "no-cache, must-revalidate");
	
	pthread_cleanup_push(WebServletRunCleanup, NULL);
	GIANT_MUTEX_LOCK();
	fprintf(f, "<!DOCTYPE HTML "
	    "PUBLIC \"-//W3C//DTD HTML 4.01//EN\" "
	    "\"http://www.w3.org/TR/html4/strict.dtd\">\n");
	fprintf(f, "<HTML>\n");
	fprintf(f, "<HEAD><TITLE>Multi-link PPP Daemon for FreeBSD (mpd)</TITLE>\n");
	fprintf(f, "<LINK rel='stylesheet' href='/mpd.css' type='text/css'>\n");
	fprintf(f, "</HEAD>\n<BODY>\n");
	fprintf(f, "<H1>Multi-link PPP Daemon for FreeBSD</H1>\n");
    
	if (!strcmp(path,"/"))
	    WebShowSummary(f, priv);
	else if (!strcmp(path,"/cmd"))
	    WebRunCmd(f, query, priv);
	    
	GIANT_MUTEX_UNLOCK();
	pthread_cleanup_pop(0);
	
	fprintf(f, "</BODY>\n</HTML>\n");
    } else {
	http_response_send_error(resp, 404, NULL);
    }
    return 1;
}
Esempio n. 2
0
int
main(int ac, char **av)
{
	char buf[1024];
	struct ghash *g;

	if ((g = ghash_create(NULL, 3, 0, "ghash", ghash_test_hash,
	    ghash_test_equal, ghash_test_add, ghash_test_del)) == NULL)
		err(1, "ghash_create");

	while (1) {
		char *args[MAX_ARGS];
		char *tokctx;
		char *s;

		/* Prompt */
		printf("Current size is %d items (%d buckets)\n",
		    ghash_size(g), g->nbuckets);
getline:
		printf("> ");
		if (fgets(buf, sizeof(buf), stdin) == NULL)
			break;

		/* Parse line */
		for (ac = 0, s = strtok_r(buf, WS, &tokctx);
		    ac < MAX_ARGS && s != NULL;
		    ac++, s = strtok_r(NULL, WS, &tokctx)) {
			if ((args[ac] = STRDUP(NULL, s)) == NULL)
				err(1, "strdup");
		}
		if (ac == 0)
			goto getline;

		/* Do cmd */
		if (strcmp(args[0], "put") == 0) {
			if (ac != 2)
				err(1, "usage: put <token>");
			printf("Putting \"%s\"...\n", args[1]);
			if (ghash_put(g, args[1]) == -1)
				err(1, "ghash_put");
			printf("Done\n");
		} else if (strcmp(args[0], "get") == 0) {
			const char *s;

			if (ac != 2)
				err(1, "usage: get <token>");
			if ((s = ghash_get(g, args[1])) == NULL)
				printf("\"%s\" was not found\n", args[1]);
			else
				printf("Found \"%s\"\n", s);
		} else if (strcmp(args[0], "del") == 0) {
			int rtn;

			if (ac != 2)
				err(1, "usage: del <token>");
			if ((rtn = ghash_remove(g, args[1])) == -1)
				err(1, "ghash_remove");
			if (rtn)
				printf("Removed \"%s\"\n", args[1]);
			else
				printf("\"%s\" was not found\n", args[1]);
		} else if (strcmp(args[0], "dump") == 0) {
			struct ghash_iter *iter;

			if ((iter = ghash_iter_create(g)) == NULL)
				err(1, "ghash_iter_create");
			printf("Iterating contents...\n");
			while (ghash_iter_has_next(iter)) {
				const char *s = ghash_iter_next(iter);

				if (s == NULL)
					err(1, "ghash_iter_next");
				printf("\t\"%s\"\n", s);
			}
			ghash_iter_destroy(&iter);
			printf("Done\n");
		} else if (strcmp(args[0], "sort") == 0) {
			void **list;
			int i, num;

			if ((num = ghash_dump(g, &list, TYPED_MEM_TEMP)) == -1)
				err(1, "ghash_get_sorted");
			printf("Sorting contents...\n");
			qsort(list, num, sizeof(*list), ghash_test_sort);
			for (i = 0; i < num; i++)
				printf("\t\"%s\"\n", (const char *)list[i]);
			FREE(TYPED_MEM_TEMP, list);
			printf("Done\n");
		} else {
			printf("Commands:\n"
			"\tget <token>\n"
			"\tput <token>\n"
			"\tdel <token>\n"
			"\tdump\n"
			"\tsort\n");
		}
	}
	if (ferror(stdin))
		err(1, "stdin");
	printf("\n");
	ghash_destroy(&g);
	typed_mem_dump(stdout);
	return (0);
}