Skip to content

Commit

Permalink
skip bad lines and make TOFU lookup more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Jun 21, 2022
1 parent c12d3cb commit 22318de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ additional documentation and more details are available in `man gplaces`. type `
## Statistic
Language|files|blank|comment|code
:-------|-------:|-------:|-------:|-------:
C|1|236|58|1074
C|1|236|58|1082
36 changes: 22 additions & 14 deletions gplaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <sys/wait.h>
#include <fcntl.h>
#include <regex.h>
#include <sys/mman.h>
#include "queue.h"

#include <openssl/ssl.h>
Expand Down Expand Up @@ -514,13 +515,14 @@ static void print_gemtext(FILE *fp, SelectorList *list, const char *filter) {

/*============================================================================*/
static int tofu(X509 *cert, const char *host) {
static char hosts[1024], buffer[1024 + 1 + EVP_MAX_MD_SIZE * 2 + 2], hex[EVP_MAX_MD_SIZE * 2 + 1];
static char hosts[1024], hex[EVP_MAX_MD_SIZE * 2 + 1];
static unsigned char md[EVP_MAX_MD_SIZE];
struct stat stbuf;
size_t hlen;
const char *home, *line;
FILE *fp;
FILE *fp = NULL;
const char *home, *start, *end, *p = MAP_FAILED;
unsigned int mdlen, i;
int trust = 1;
int fd, found, trust;

if (X509_digest(cert, EVP_sha512(), md, &mdlen) == 0) return 0;

Expand All @@ -536,18 +538,24 @@ static int tofu(X509 *cert, const char *host) {
else if ((home = getenv("HOME")) != NULL) snprintf(hosts, sizeof(hosts), "%s/.gplaces_hosts", home);
else return 0;

if ((fp = fopen(hosts, "r")) == NULL) return 1;
while ((line = fgets(buffer, sizeof(buffer), fp)) != NULL) {
if (strncmp(line, host, hlen) || line[hlen] != ' ') continue;
trust = strncmp(&line[hlen + 1], hex, mdlen * 2) == 0 && line[hlen + 1 + mdlen * 2] == '\n';
goto out;
}
fclose(fp); fp = NULL;
if (stat(hosts, &stbuf) == 0 && (fd = open(hosts, O_RDONLY)) != -1) {
if (stbuf.st_size > 0) {
if ((p = mmap(NULL, stbuf.st_size % SIZE_MAX, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) { close(fd); return 0; }
for (found = 0, trust = 0, end = (const char *)p; !found && (start = memmem(end, stbuf.st_size - (end - p), host, hlen)) != NULL; end = start + hlen + 1) {
if (!(found = ((start == p || *(start - 1) == '\n') && (size_t)stbuf.st_size - (start - p) >= hlen + 2 && start[hlen] == ' ' && start[hlen + 1] != '\n'))) continue;
trust = (size_t)stbuf.st_size - (start - p) >= hlen + 1 + mdlen * 2 + 1 && memcmp(&start[hlen + 1], hex, mdlen * 2) == 0 && start[hlen + 1 + mdlen * 2] == '\n';
}
munmap((void *)p, stbuf.st_size);
}
close(fd);
if (found) return trust;
} else if (errno != ENOENT) return 0;

trust = (fp = fopen(hosts, "a")) != NULL && fprintf(fp, "%s %s\n", host, hex) > 0;
if ((fp = fopen(hosts, "a")) != NULL) {
trust = fprintf(fp, "%s %s\n", host, hex) > 0;
fclose(fp);
}

out:
if (fp) fclose(fp);
return trust;
}

Expand Down

0 comments on commit 22318de

Please sign in to comment.