From: Nishi Date: Tue, 20 Aug 2024 21:05:24 +0000 (+0000) Subject: format X-Git-Url: https://git.chaotic.ninja/gitweb/nishi/?a=commitdiff_plain;h=2ced771fa2b20ec4439e99c7bbc6532bdcd47033;p=repoview.git format git-svn-id: file:///raid/svn-personal/repoview/trunk@3 7e8b2a19-8934-dd40-8cb3-db22cdd5a80f --- diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..f09c9d2 --- /dev/null +++ b/.clang-format @@ -0,0 +1,15 @@ +--- +# $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 diff --git a/CGI/Makefile b/CGI/Makefile index 33e4761..f64fc55 100644 --- a/CGI/Makefile +++ b/CGI/Makefile @@ -5,7 +5,7 @@ include $(PWD)/Platform/$(PLATFORM).mk .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 diff --git a/CGI/auth/cookie.c b/CGI/auth/cookie.c index a0749a0..a2924f9 100644 --- a/CGI/auth/cookie.c +++ b/CGI/auth/cookie.c @@ -1 +1,83 @@ /* $Id$ */ + +#include "rv_auth.h" + +#include "rv_util.h" + +#include +#include + +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); +} diff --git a/CGI/db/sqlite.c b/CGI/db/sqlite.c index b513e6f..e2b1e2a 100644 --- a/CGI/db/sqlite.c +++ b/CGI/db/sqlite.c @@ -14,36 +14,24 @@ 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"); @@ -51,26 +39,24 @@ void rv_init_db(void){ } } -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); @@ -79,7 +65,7 @@ bool rv_has_user(const char* username){ tmp = query; query = rv_strcat(tmp, cbuf); free(tmp); - }else{ + } else { cbuf[0] = username[i]; char* tmp = query; query = rv_strcat(tmp, cbuf); @@ -91,15 +77,9 @@ bool rv_has_user(const char* username){ 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; diff --git a/CGI/logo.png b/CGI/logo.png new file mode 100644 index 0000000..9aac683 Binary files /dev/null and b/CGI/logo.png differ diff --git a/CGI/main.c b/CGI/main.c index a3dd2bc..2170c13 100644 --- a/CGI/main.c +++ b/CGI/main.c @@ -9,12 +9,13 @@ #include "rv_page.h" #include "rv_util.h" #include "rv_db.h" +#include "rv_auth.h" #include char* postdata; -int main(){ +int main() { rv_check_sanity(); rv_init_db(); rv_parse_query(getenv("QUERY_STRING")); @@ -23,7 +24,7 @@ int main(){ 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; @@ -36,6 +37,11 @@ int main(){ 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(); } diff --git a/CGI/page.c b/CGI/page.c index 1f73d11..c938e75 100644 --- a/CGI/page.c +++ b/CGI/page.c @@ -14,18 +14,16 @@ char* buffer; 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); } diff --git a/CGI/query.c b/CGI/query.c index 61a91b3..7f3b754 100644 --- a/CGI/query.c +++ b/CGI/query.c @@ -18,30 +18,30 @@ struct query_entry** qentries; 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; @@ -49,8 +49,8 @@ void rv_parse_query(const char* oldquery){ 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; @@ -62,9 +62,10 @@ void rv_parse_query(const char* oldquery){ 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; @@ -78,9 +79,9 @@ void rv_parse_query(const char* oldquery){ 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]); @@ -88,10 +89,10 @@ void rv_free_query(void){ 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; } } diff --git a/CGI/rv_auth.h b/CGI/rv_auth.h new file mode 100644 index 0000000..c9595aa --- /dev/null +++ b/CGI/rv_auth.h @@ -0,0 +1,11 @@ +/* $Id$ */ + +#ifndef __RV_AUTH_H__ +#define __RV_AUTH_H__ + +#include + +char* rv_logged_in(void); +void rv_free_auth(void); + +#endif diff --git a/CGI/rv_sha512.h b/CGI/rv_sha512.h new file mode 100644 index 0000000..8628fe8 --- /dev/null +++ b/CGI/rv_sha512.h @@ -0,0 +1,8 @@ +/* $Id$ */ + +#ifndef __RV_SHA512_H__ +#define __RV_SHA512_H__ + +unsigned char* rv_sha512(const char* string); + +#endif diff --git a/CGI/sanity.c b/CGI/sanity.c index b7fe418..2be7a4b 100644 --- a/CGI/sanity.c +++ b/CGI/sanity.c @@ -11,7 +11,7 @@ #include #include -bool rv_find_executable(const char* name){ +bool rv_find_executable(const char* name) { #ifdef USE_PATH char* path = rv_strcat(USE_PATH, ""); #else @@ -19,8 +19,8 @@ bool rv_find_executable(const char* name){ #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); @@ -29,7 +29,7 @@ bool rv_find_executable(const char* 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; @@ -43,7 +43,7 @@ bool rv_find_executable(const char* name){ return false; } -void rv_check_sanity(void){ +void rv_check_sanity(void) { bool sane = true; bool svnlook = rv_find_executable("svnlook"); @@ -54,7 +54,7 @@ void rv_check_sanity(void){ 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"); diff --git a/CGI/sha512.c b/CGI/sha512.c new file mode 100644 index 0000000..83d0214 --- /dev/null +++ b/CGI/sha512.c @@ -0,0 +1,14 @@ +/* $Id$ */ + +#include "rv_sha512.h" + +#include + +#include +#include + +unsigned char* rv_sha512(const char* string) { + unsigned char* hash = malloc(SHA512_DIGEST_LENGTH); + SHA512((const unsigned char*)string, strlen(string), hash); + return hash; +} diff --git a/CGI/theme/modern.c b/CGI/theme/modern.c index 56016a6..2e8e730 100644 --- a/CGI/theme/modern.c +++ b/CGI/theme/modern.c @@ -4,6 +4,7 @@ #include "rv_util.h" #include "rv_version.h" +#include "rv_auth.h" #include "rv_db.h" #include "../../config.h" @@ -19,16 +20,16 @@ char* title = NULL; 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 ".
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(""); @@ -52,16 +53,16 @@ void render_page(void){ add_data(&page, " \n"); add_data(&page, " \n"); add_data(&page, "\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"); } } @@ -76,22 +77,22 @@ void render_page(void){ 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); @@ -101,7 +102,7 @@ char* escape(const char* str){ return r; } -void render_stuff(void){ +void render_stuff(void) { char* escaped; add_data(&buffer, "\n"); add_data(&buffer, "\n"); diff --git a/CGI/theme/optimized.c b/CGI/theme/optimized.c index c39e650..51622dc 100644 --- a/CGI/theme/optimized.c +++ b/CGI/theme/optimized.c @@ -3,5 +3,4 @@ extern char* buffer; void add_data(char** data, const char* txt); -void render_page(void){ -} +void render_page(void) {} diff --git a/CGI/util.c b/CGI/util.c index 7e8d1b3..7efe558 100644 --- a/CGI/util.c +++ b/CGI/util.c @@ -10,7 +10,7 @@ #include #include -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)); @@ -18,18 +18,16 @@ char* rv_strcat(const char* a, const char* 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"); @@ -40,25 +38,25 @@ void rv_error_http(void){ 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]); @@ -66,7 +64,7 @@ char* rv_url_decode(const char* str){ r = rv_strcat(tmp, cbuf); free(tmp); i += 2; - }else{ + } else { cbuf[0] = str[i]; char* tmp = r; r = rv_strcat(tmp, cbuf); diff --git a/CGI/version.c b/CGI/version.c index 6359256..08fae8c 100644 --- a/CGI/version.c +++ b/CGI/version.c @@ -4,6 +4,4 @@ const char* rv_version = "1.00"; -const char* rv_get_version(void){ - return rv_version; -} +const char* rv_get_version(void) { return rv_version; } diff --git a/Makefile b/Makefile index c348819..1078ee9 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ PLATFORM = generic FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM) EXTOBJS="`./objs`" EXTLIBS="`./libs`" -.PHONY: all clean ./CGI +.PHONY: all clean format ./CGI all: ./CGI @@ -25,3 +25,6 @@ check:: check.c config.h clean: $(MAKE) -C ./CGI clean $(FLAGS) rm -f objs libs check + +format: + clang-format --verbose -i `find . -name "*.c" -or -name "*.h"` diff --git a/Platform/generic.mk b/Platform/generic.mk index c26df9c..f8f14af 100644 --- a/Platform/generic.mk +++ b/Platform/generic.mk @@ -3,4 +3,4 @@ CC = cc CFLAGS = -std=c99 LDFLAGS = -LIBS = +LIBS = -lcrypto diff --git a/check.c b/check.c index d20cf23..e838a3f 100644 --- a/check.c +++ b/check.c @@ -4,7 +4,7 @@ #include -int check_db(void){ +int check_db(void) { int counter = 0; const char* db = ""; #ifdef USE_SQLITE @@ -19,19 +19,19 @@ int check_db(void){ 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 @@ -42,38 +42,38 @@ int check_theme(void){ 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; diff --git a/libs.c b/libs.c index e3ff25a..57db579 100644 --- a/libs.c +++ b/libs.c @@ -4,7 +4,7 @@ #include -int main(){ +int main() { #if defined(USE_SQLITE) printf("-lsqlite3"); #elif defined(USE_GDBM) diff --git a/objs.c b/objs.c index 1ac24ab..afeb2f3 100644 --- a/objs.c +++ b/objs.c @@ -2,7 +2,7 @@ #include -int main(){ +int main() { #if defined(USE_SQLITE) printf("db/sqlite.o"); #elif defined(USE_GDBM) || defined(USE_NDBM)