--- /dev/null
+---
+# $Id$
+Language: Cpp
+UseTab: Always
+TabWidth: 8
+IndentWidth: 8
+PointerAlignment: Left
+ColumnLimit: 1024
+AllowShortIfStatementsOnASingleLine: Always
+AllowShortBlocksOnASingleLine: Never
+AllowShortLoopsOnASingleLine: true
+SpaceBeforeParens: Never
+AlignEscapedNewlines: DontAlign
+SortIncludes: false
+AllowShortEnumsOnASingleLine: false
.PHONY: all clean
.SUFFIXES: .c .o
-OBJS = main.o sanity.o version.o util.o query.o page.o $(EXTOBJS)
+OBJS = main.o sanity.o version.o util.o query.o page.o sha512.o $(EXTOBJS)
all: repoview.cgi
/* $Id$ */
+
+#include "rv_auth.h"
+
+#include "rv_util.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+extern char** environ;
+
+struct cookie_entry {
+ char* key;
+ char* value;
+};
+
+struct cookie_entry** cookie_entries;
+
+void parse_cookie(void) {
+ cookie_entries = malloc(sizeof(*cookie_entries));
+ cookie_entries[0] = NULL;
+ char* cookie = getenv("HTTP_COOKIE");
+ if(cookie != NULL) {
+ cookie = rv_strdup(cookie);
+ int i;
+ int incr = 0;
+ for(i = 0;; i++) {
+ if(cookie[i] == 0 || cookie[i] == ';') {
+ char oldc = cookie[i];
+ cookie[i] = 0;
+
+ char* key = cookie + incr;
+ char* value = "";
+
+ int j;
+ for(j = 0; key[j] != 0; j++) {
+ if(key[j] == '=') {
+ key[j] = 0;
+ value = key + j + 1;
+ break;
+ }
+ }
+ struct cookie_entry* entry = malloc(sizeof(*entry));
+ entry->key = rv_strdup(key);
+ entry->value = rv_strdup(value);
+
+ struct cookie_entry** old_entries = cookie_entries;
+ for(j = 0; old_entries[j] != NULL; j++)
+ ;
+ cookie_entries = malloc(sizeof(*cookie_entries) * (j + 2));
+ for(j = 0; old_entries[j] != NULL; j++) {
+ cookie_entries[j] = old_entries[j];
+ }
+ cookie_entries[j] = entry;
+ cookie_entries[j + 1] = NULL;
+
+ int oldi = i;
+ i++;
+ for(; cookie[i] != 0 && (cookie[i] == ' ' || cookie[i] == '\t'); i++)
+ ;
+ i--;
+ incr = i + 1;
+ if(oldc == 0) break;
+ }
+ }
+ free(cookie);
+ }
+}
+
+char* rv_logged_in(void) {
+ parse_cookie();
+ return NULL;
+}
+
+void rv_free_auth(void) {
+ int i;
+ for(i = 0; cookie_entries[i] != NULL; i++) {
+ free(cookie_entries[i]->key);
+ free(cookie_entries[i]->value);
+ free(cookie_entries[i]);
+ }
+ free(cookie_entries);
+}
sqlite3* sql;
-void rv_init_db(void){
+void rv_init_db(void) {
int ret;
ret = sqlite3_open(DB_ROOT "/db.sqlite3", &sql);
- if(ret != SQLITE_OK){
+ if(ret != SQLITE_OK) {
rv_error_http();
printf("SQLite3 database error\n");
exit(1);
}
char* err;
- ret = sqlite3_exec(
- sql,
- "create table if not exists users(user text, password text)",
- NULL,
- NULL,
- &err
- );
- if(ret != SQLITE_OK){
+ ret = sqlite3_exec(sql, "create table if not exists users(user text, password text)", NULL, NULL, &err);
+ if(ret != SQLITE_OK) {
sqlite3_free(err);
rv_error_http();
printf("SQLite3 database error\n");
exit(1);
}
- ret = sqlite3_exec(
- sql,
- "create table if not exists tokens(user text, token text)",
- NULL,
- NULL,
- &err
- );
- if(ret != SQLITE_OK){
+ ret = sqlite3_exec(sql, "create table if not exists tokens(user text, token text)", NULL, NULL, &err);
+ if(ret != SQLITE_OK) {
sqlite3_free(err);
rv_error_http();
printf("SQLite3 database error\n");
}
}
-void rv_close_db(void){
- sqlite3_close(sql);
-}
+void rv_close_db(void) { sqlite3_close(sql); }
int count = 0;
-int sqlcount(void* param, int ncol, char** row, char** col){
+int sqlcount(void* param, int ncol, char** row, char** col) {
count = ncol;
fprintf(stderr, "%d\n", ncol);
return 0;
}
-bool rv_has_user(const char* username){
+bool rv_has_user(const char* username) {
char* err;
char cbuf[2];
cbuf[1] = 0;
char* query = rv_strdup("select * from users where user = '");
int i;
- for(i = 0; username[i] != 0; i++){
- if(username[i] == '\''){
+ for(i = 0; username[i] != 0; i++) {
+ if(username[i] == '\'') {
cbuf[0] = username[i];
char* tmp = query;
tmp = rv_strcat(tmp, cbuf);
tmp = query;
query = rv_strcat(tmp, cbuf);
free(tmp);
- }else{
+ } else {
cbuf[0] = username[i];
char* tmp = query;
query = rv_strcat(tmp, cbuf);
free(tmp);
int ret;
fprintf(stderr, "%s\n", query);
- ret = sqlite3_exec(
- sql,
- query,
- sqlcount,
- NULL,
- &err
- );
+ ret = sqlite3_exec(sql, query, sqlcount, NULL, &err);
free(query);
- if(ret != SQLITE_OK){
+ if(ret != SQLITE_OK) {
sqlite3_free(err);
}
return count > 0;
#include "rv_page.h"
#include "rv_util.h"
#include "rv_db.h"
+#include "rv_auth.h"
#include <stdlib.h>
char* postdata;
-int main(){
+int main() {
rv_check_sanity();
rv_init_db();
rv_parse_query(getenv("QUERY_STRING"));
postdata[0] = 0;
char cbuf[2];
cbuf[1] = 0;
- while(1){
+ while(1) {
fread(cbuf, 1, 1, stdin);
if(feof(stdin)) break;
char* tmp = postdata;
printf("Content-Type: text/html\r\n");
printf("\r\n");
rv_print_page();
+ rv_logged_in();
+ rv_load_query('Q');
+ rv_free_query();
+ rv_load_query('P');
rv_free_query();
rv_close_db();
+ rv_free_auth();
}
void render_page(void);
-void add_data(char** data, const char* txt){
+void add_data(char** data, const char* txt) {
char* tmp = *data;
*data = rv_strcat(tmp, txt);
free(tmp);
}
-void rv_process_page(void){
+void rv_process_page(void) {
buffer = malloc(1);
buffer[0] = 0;
render_page();
}
-void rv_print_page(void){
- printf("%s\n", buffer);
-}
+void rv_print_page(void) { printf("%s\n", buffer); }
struct query_entry** query;
struct query_entry** postquery;
-void rv_save_query(char c){
- if(c == 'Q'){
+void rv_save_query(char c) {
+ if(c == 'Q') {
query = qentries;
- }else if(c == 'P'){
+ } else if(c == 'P') {
postquery = qentries;
}
}
-void rv_load_query(char c){
- if(c == 'Q'){
+void rv_load_query(char c) {
+ if(c == 'Q') {
qentries = query;
- }else if(c == 'P'){
+ } else if(c == 'P') {
qentries = postquery;
}
}
-void rv_parse_query(const char* oldquery){
+void rv_parse_query(const char* oldquery) {
char* query = rv_strdup(oldquery);
int i;
int incr = 0;
qentries = malloc(sizeof(*qentries));
qentries[0] = NULL;
- for(i = 0;; i++){
- if(query[i] == '&' || query[i] == 0){
+ for(i = 0;; i++) {
+ if(query[i] == '&' || query[i] == 0) {
char oldc = query[i];
query[i] = 0;
char* value = "";
int j;
- for(j = 0; key[j] != 0; j++){
- if(key[j] == '='){
+ for(j = 0; key[j] != 0; j++) {
+ if(key[j] == '=') {
key[j] = 0;
value = key + j + 1;
break;
entry->value = rv_url_decode(value);
struct query_entry** old_entries = qentries;
- for(j = 0; old_entries[j] != NULL; j++);
+ for(j = 0; old_entries[j] != NULL; j++)
+ ;
qentries = malloc(sizeof(*qentries) * (j + 2));
- for(j = 0; old_entries[j] != NULL; j++){
+ for(j = 0; old_entries[j] != NULL; j++) {
qentries[j] = old_entries[j];
}
qentries[j] = entry;
free(query);
}
-void rv_free_query(void){
+void rv_free_query(void) {
int i;
- for(i = 0; qentries[i] != NULL; i++){
+ for(i = 0; qentries[i] != NULL; i++) {
free(qentries[i]->key);
free(qentries[i]->value);
free(qentries[i]);
free(qentries);
}
-char* rv_get_query(const char* key){
+char* rv_get_query(const char* key) {
int i;
- for(i = 0; qentries[i] != NULL; i++){
- if(strcmp(qentries[i]->key, key) == 0){
+ for(i = 0; qentries[i] != NULL; i++) {
+ if(strcmp(qentries[i]->key, key) == 0) {
return qentries[i]->value;
}
}
--- /dev/null
+/* $Id$ */
+
+#ifndef __RV_AUTH_H__
+#define __RV_AUTH_H__
+
+#include <stdbool.h>
+
+char* rv_logged_in(void);
+void rv_free_auth(void);
+
+#endif
--- /dev/null
+/* $Id$ */
+
+#ifndef __RV_SHA512_H__
+#define __RV_SHA512_H__
+
+unsigned char* rv_sha512(const char* string);
+
+#endif
#include <stdio.h>
#include <unistd.h>
-bool rv_find_executable(const char* name){
+bool rv_find_executable(const char* name) {
#ifdef USE_PATH
char* path = rv_strcat(USE_PATH, "");
#else
#endif
int i;
int incr = 0;
- for(i = 0;; i++){
- if(path[i] == 0 || path[i] == PATH_DELIM){
+ for(i = 0;; i++) {
+ if(path[i] == 0 || path[i] == PATH_DELIM) {
char oldc = path[i];
path[i] = 0;
char* exec = rv_strcat3(path + incr, "/", name);
exec = rv_strcat(exec, ".exe");
free(tmp);
#endif
- if(access(exec, F_OK) == 0){
+ if(access(exec, F_OK) == 0) {
free(exec);
free(path);
return true;
return false;
}
-void rv_check_sanity(void){
+void rv_check_sanity(void) {
bool sane = true;
bool svnlook = rv_find_executable("svnlook");
if(!svnadmin) sane = false;
if(!htpasswd) sane = false;
- if(!sane){
+ if(!sane) {
rv_error_http();
if(!svnlook) printf("svnlook not found\n");
if(!svnadmin) printf("svnadmin not found\n");
--- /dev/null
+/* $Id$ */
+
+#include "rv_sha512.h"
+
+#include <openssl/sha.h>
+
+#include <stdlib.h>
+#include <string.h>
+
+unsigned char* rv_sha512(const char* string) {
+ unsigned char* hash = malloc(SHA512_DIGEST_LENGTH);
+ SHA512((const unsigned char*)string, strlen(string), hash);
+ return hash;
+}
#include "rv_util.h"
#include "rv_version.h"
+#include "rv_auth.h"
#include "rv_db.h"
#include "../../config.h"
char* desc = NULL;
char* page = NULL;
-void render_page(void){
+void render_page(void) {
rv_load_query('Q');
char* query = rv_get_query("page");
if(query == NULL) query = "welcome";
- if(strcmp(query, "welcome") == 0){
+ if(strcmp(query, "welcome") == 0) {
title = rv_strdup("Welcome");
desc = rv_strdup("Welcome to " INSTANCE_NAME ".");
page = rv_strcat3("Welcome to " INSTANCE_NAME ".<br>This instance is running RepoView version ", rv_get_version(), ".");
- }else if(strcmp(query, "login") == 0){
+ } else if(strcmp(query, "login") == 0) {
title = rv_strdup("Login");
desc = rv_strdup("You can log in to your account here.");
page = rv_strdup("");
add_data(&page, " </table>\n");
add_data(&page, " <input type=\"submit\" value=\"Login\">\n");
add_data(&page, "</form>\n");
- }else if(strcmp(query, "sendlogin") == 0){
+ } else if(strcmp(query, "sendlogin") == 0) {
title = rv_strdup("Login Result");
page = rv_strdup("");
rv_load_query('P');
- if(rv_get_query("username") == NULL || rv_get_query("password") == NULL){
+ if(rv_get_query("username") == NULL || rv_get_query("password") == NULL) {
add_data(&page, "Invalid form\n");
- }else{
- if(rv_has_user(rv_get_query("username"))){
- }else{
+ } else {
+ if(rv_has_user(rv_get_query("username"))) {
+ } else {
add_data(&page, "User does not exist");
}
}
free(title);
}
-char* escape(const char* str){
+char* escape(const char* str) {
char* r = malloc(1);
r[0] = 0;
char cbuf[2];
cbuf[1] = 0;
int i;
- for(i = 0; str[i] != 0; i++){
- if(str[i] == '<'){
+ for(i = 0; str[i] != 0; i++) {
+ if(str[i] == '<') {
char* tmp = r;
r = rv_strcat(tmp, "<");
free(tmp);
- }else if(str[i] == '>'){
+ } else if(str[i] == '>') {
char* tmp = r;
r = rv_strcat(tmp, ">");
free(tmp);
- }else{
+ } else {
cbuf[0] = str[i];
char* tmp = r;
r = rv_strcat(tmp, cbuf);
return r;
}
-void render_stuff(void){
+void render_stuff(void) {
char* escaped;
add_data(&buffer, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n");
add_data(&buffer, "<html>\n");
extern char* buffer;
void add_data(char** data, const char* txt);
-void render_page(void){
-}
+void render_page(void) {}
#include <stdlib.h>
#include <string.h>
-char* rv_strcat(const char* a, const char* b){
+char* rv_strcat(const char* a, const char* b) {
char* str = malloc(strlen(a) + strlen(b) + 1);
memcpy(str, a, strlen(a));
memcpy(str + strlen(a), b, strlen(b));
return str;
}
-char* rv_strcat3(const char* a, const char* b, const char* c){
+char* rv_strcat3(const char* a, const char* b, const char* c) {
char* tmp = rv_strcat(a, b);
char* str = rv_strcat(tmp, c);
free(tmp);
return str;
}
-char* rv_strdup(const char* str){
- return rv_strcat(str, "");
-}
+char* rv_strdup(const char* str) { return rv_strcat(str, ""); }
-void rv_error_http(void){
+void rv_error_http(void) {
printf("Content-Type: text/plain\r\n");
printf("Status: 500 Internal Server Error\r\n");
printf("\r\n");
printf("-----\n");
}
-int hex_to_num(char c){
- if('0' <= c && c <= '9'){
+int hex_to_num(char c) {
+ if('0' <= c && c <= '9') {
return c - '0';
- }else if('a' <= c && c <= 'f'){
+ } else if('a' <= c && c <= 'f') {
return c - 'a' + 10;
- }else if('A' <= c && c <= 'F'){
+ } else if('A' <= c && c <= 'F') {
return c - 'A' + 10;
}
return 0;
}
-char* rv_url_decode(const char* str){
+char* rv_url_decode(const char* str) {
char* r = malloc(1);
r[0] = 0;
int i;
char cbuf[2];
cbuf[1] = 0;
- for(i = 0; str[i] != 0; i++){
- if(str[i] == '%'){
+ for(i = 0; str[i] != 0; i++) {
+ if(str[i] == '%') {
if(str[i + 1] == 0) break;
if(str[i + 2] == 0) break;
cbuf[0] = (hex_to_num(str[i + 1]) << 4) | hex_to_num(str[i + 2]);
r = rv_strcat(tmp, cbuf);
free(tmp);
i += 2;
- }else{
+ } else {
cbuf[0] = str[i];
char* tmp = r;
r = rv_strcat(tmp, cbuf);
const char* rv_version = "1.00";
-const char* rv_get_version(void){
- return rv_version;
-}
+const char* rv_get_version(void) { return rv_version; }
FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM) EXTOBJS="`./objs`" EXTLIBS="`./libs`"
-.PHONY: all clean ./CGI
+.PHONY: all clean format ./CGI
all: ./CGI
clean:
$(MAKE) -C ./CGI clean $(FLAGS)
rm -f objs libs check
+
+format:
+ clang-format --verbose -i `find . -name "*.c" -or -name "*.h"`
CC = cc
CFLAGS = -std=c99
LDFLAGS =
-LIBS =
+LIBS = -lcrypto
#include <stdio.h>
-int check_db(void){
+int check_db(void) {
int counter = 0;
const char* db = "";
#ifdef USE_SQLITE
counter++;
db = "NDBM";
#endif
- if(counter > 1){
+ if(counter > 1) {
fprintf(stderr, "You cannot use multiple database types at once.\n");
return 1;
- }else if(counter == 0){
+ } else if(counter == 0) {
fprintf(stderr, "You must select a database type.\n");
return 1;
- }else{
+ } else {
printf("Database type is %s\n", db);
}
return 0;
}
-int check_theme(void){
+int check_theme(void) {
int counter = 0;
const char* theme = "";
#ifdef USE_MODERN
counter++;
theme = "Optimized";
#endif
- if(counter > 1){
+ if(counter > 1) {
fprintf(stderr, "You cannot use multiple themes at once.\n");
return 1;
- }else if(counter == 0){
+ } else if(counter == 0) {
fprintf(stderr, "You must select a theme.\n");
return 1;
- }else{
+ } else {
printf("Theme is %s\n", theme);
}
return 0;
}
-int check_auth(void){
+int check_auth(void) {
int counter = 0;
const char* method = "";
#ifdef USE_COOKIE
counter++;
method = "Cookie";
#endif
- if(counter > 1){
+ if(counter > 1) {
fprintf(stderr, "You cannot use multiple authentication methods at once.\n");
return 1;
- }else if(counter == 0){
+ } else if(counter == 0) {
fprintf(stderr, "You must select an authentication method.\n");
return 1;
- }else{
+ } else {
printf("Authentication method is %s\n", method);
}
return 0;
}
-int main(){
+int main() {
int st;
st = check_db();
if(st != 0) goto fail;
#include <stdio.h>
-int main(){
+int main() {
#if defined(USE_SQLITE)
printf("-lsqlite3");
#elif defined(USE_GDBM)
#include <stdio.h>
-int main(){
+int main() {
#if defined(USE_SQLITE)
printf("db/sqlite.o");
#elif defined(USE_GDBM) || defined(USE_NDBM)