From f6d38365b7e7e1e88ba02136ffe6f0fb08903ef0 Mon Sep 17 00:00:00 2001 From: Jonathan McDowell Date: Thu, 14 Sep 2023 23:47:22 +0530 Subject: [PATCH] Move the CGI specific routines into cgi/ and split out UID escaping The only piece from getcgi.c that anything other than the CGI needs is the code to escape a UID. Move that into keyindex.c and shuffle the CGI routines off to the subdirectory. The escaping routine could do with a lot of improvement, but this is a start in terms of cleaning things up. --- CMakeLists.txt | 2 +- cgi/CMakeLists.txt | 8 +++---- getcgi.c => cgi/getcgi.c | 44 ------------------------------------ getcgi.h => cgi/getcgi.h | 8 ------- keyindex.c | 49 +++++++++++++++++++++++++++++++++++++++- keyindex.h | 8 +++++++ stats.c | 2 +- 7 files changed, 62 insertions(+), 59 deletions(-) rename getcgi.c => cgi/getcgi.c (84%) rename getcgi.h => cgi/getcgi.h (88%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 30e5929..acacb34 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,7 +51,7 @@ endif() # Core objects add_library(libonak STATIC armor.c charfuncs.c cleankey.c cleanup.c decodekey.c - getcgi.c hash.c hash-helper.c key-store.c keyarray.c keyid.c keyindex.c + hash.c hash-helper.c key-store.c keyarray.c keyid.c keyindex.c ll.c log.c marshal.c mem.c merge.c onak-conf.c parsekey.c photoid.c rsa.c sigcheck.c sendsync.c sha1x.c wordlist.c) set(LIBONAK_LIBRARIES "") diff --git a/cgi/CMakeLists.txt b/cgi/CMakeLists.txt index 977f2a2..da430d4 100644 --- a/cgi/CMakeLists.txt +++ b/cgi/CMakeLists.txt @@ -1,9 +1,9 @@ # CGI -add_executable(add add.c) +add_executable(add add.c getcgi.c) target_link_libraries(add libonak) -add_executable(gpgwww gpgwww.c ../stats.c) +add_executable(gpgwww gpgwww.c ../stats.c getcgi.c) target_link_libraries(gpgwww libonak) -add_executable(hashquery hashquery.c) +add_executable(hashquery hashquery.c getcgi.c) target_link_libraries(hashquery libonak) -add_executable(lookup lookup.c) +add_executable(lookup lookup.c getcgi.c) target_link_libraries(lookup libonak) diff --git a/getcgi.c b/cgi/getcgi.c similarity index 84% rename from getcgi.c rename to cgi/getcgi.c index 7c06560..ded1c93 100644 --- a/getcgi.c +++ b/cgi/getcgi.c @@ -26,50 +26,6 @@ #include "getcgi.h" -/** - * txt2html - Takes a string and converts it to HTML. - * @string: The string to HTMLize. - * - * Takes a string and escapes any HTML entities. - */ -char *txt2html(const char *string) -{ - static char buf[1024]; - char *ptr = NULL; - char *nextptr = NULL; - - memset(buf, 0, 1024); - - ptr = strchr(string, '<'); - if (ptr != NULL) { - nextptr = ptr + 1; - *ptr = 0; - strncpy(buf, string, 1023); - strncat(buf, "<", 1023 - strlen(buf)); - string = nextptr; - } - - ptr = strchr(string, '>'); - if (ptr != NULL) { - nextptr = ptr + 1; - *ptr = 0; - strncat(buf, string, 1023 - strlen(buf)); - strncat(buf, ">", 1023 - strlen(buf)); - string = nextptr; - } - - /* - * TODO: We need to while() this really as each entity may appear more - * than once. We need to start with & and ; as we replace with those - * throughout. Fuck it for the moment though; it's Easter and < & > are - * the most common and tend to only appear once. - */ - - strncat(buf, string, 1023 - strlen(buf)); - - return buf; -} - /* * start_html - Start HTML output. * @title: The title for the HTML. diff --git a/getcgi.h b/cgi/getcgi.h similarity index 88% rename from getcgi.h rename to cgi/getcgi.h index 1495671..b4cc44b 100644 --- a/getcgi.h +++ b/cgi/getcgi.h @@ -19,14 +19,6 @@ #ifndef __GETCGI_H_ #define __GETCGI_H_ -/** - * txt2html - Takes a string and converts it to HTML. - * @string: The string to HTMLize. - * - * Takes a string and escapes any HTML entities. - */ -char *txt2html(const char *string); - /* * start_html - Start HTML output. * @title: The title for the HTML. diff --git a/keyindex.c b/keyindex.c index e8d569a..fbd32c7 100644 --- a/keyindex.c +++ b/keyindex.c @@ -24,7 +24,6 @@ #include #include "decodekey.h" -#include "getcgi.h" #include "keydb.h" #include "keyid.h" #include "keyindex.h" @@ -74,6 +73,54 @@ char pkalgo2char(uint8_t algo) return typech; } +/** + * txt2html - Takes a string and converts it to HTML. + * @string: The string to HTMLize. + * + * Takes a string and escapes any HTML entities. + */ +const char *txt2html(const char *string) +{ + static char buf[1024]; + char *ptr = NULL; + char *nextptr = NULL; + + if (strlen(string) > 1000) { + return string; + } + + memset(buf, 0, 1024); + + ptr = strchr(string, '<'); + if (ptr != NULL) { + nextptr = ptr + 1; + *ptr = 0; + strncpy(buf, string, 1023); + strncat(buf, "<", 1023 - strlen(buf)); + string = nextptr; + } + + ptr = strchr(string, '>'); + if (ptr != NULL) { + nextptr = ptr + 1; + *ptr = 0; + strncat(buf, string, 1023 - strlen(buf)); + strncat(buf, ">", 1023 - strlen(buf)); + string = nextptr; + } + + /* + * TODO: We need to while() this really as each entity may appear more + * than once. We need to start with & and ; as we replace with those + * throughout. Fuck it for the moment though; it's Easter and < & > are + * the most common and tend to only appear once. + */ + + strncat(buf, string, 1023 - strlen(buf)); + + return buf; +} + /* * Given a public key/subkey packet return the key length. */ diff --git a/keyindex.h b/keyindex.h index 4b10e45..c3e78ef 100644 --- a/keyindex.h +++ b/keyindex.h @@ -47,4 +47,12 @@ int key_index(struct onak_dbctx *dbctx, * machine readable list of them. */ int mrkey_index(struct openpgp_publickey *keys); + +/** + * txt2html - Takes a string and converts it to HTML. + * @string: The string to HTMLize. + * + * Takes a string and escapes any HTML entities. + */ +const char *txt2html(const char *string); #endif diff --git a/stats.c b/stats.c index 91c9166..7fa6894 100644 --- a/stats.c +++ b/stats.c @@ -21,9 +21,9 @@ #include #include "cleanup.h" -#include "getcgi.h" #include "hash.h" #include "keydb.h" +#include "keyindex.h" #include "ll.h" #include "stats.h" -- 2.39.2