From 299baeb1e1f2599aec5f6d2dd229f2bf0a1922b7 Mon Sep 17 00:00:00 2001 From: Chip Black Date: Thu, 2 Jan 2014 04:29:49 -0600 Subject: [PATCH] Use blergconf for paths Also use FILENAME_MAX everywhere instead of hard-code filename lengths --- common/auth.c | 37 +++++++++++++++++++------------------ database/database.c | 4 ++-- database/subscription.c | 33 +++++++++++++++++---------------- database/tags.c | 19 ++++++++++--------- tools/blerglatest.c | 5 +++-- 5 files changed, 51 insertions(+), 47 deletions(-) diff --git a/common/auth.c b/common/auth.c index 439e2ad..a048c2f 100644 --- a/common/auth.c +++ b/common/auth.c @@ -9,12 +9,13 @@ #include #include #include "config.h" +#include "configuration.h" #include "auth.h" #include "util.h" #include "md5.h" int auth_set_password(const char *username, const char *password) { - char filename[512]; + char filename[FILENAME_MAX]; unsigned char pwhash[SCRYPT_OUTPUT_SIZE]; unsigned char salt[SCRYPT_SALT_SIZE]; int fd, n, r; @@ -42,19 +43,19 @@ int auth_set_password(const char *username, const char *password) { } /* Write the password */ - snprintf(filename, 512, "%s/%s/password", DATA_PATH, username); + snprintf(filename, FILENAME_MAX, "%s/%s/password", blergconf.data_path, username); fd = open(filename, O_WRONLY | O_CREAT, 0600); write(fd, pwhash, SCRYPT_OUTPUT_SIZE); close(fd); /* Write the salt */ - snprintf(filename, 512, "%s/%s/password_salt", DATA_PATH, username); + snprintf(filename, FILENAME_MAX, "%s/%s/password_salt", blergconf.data_path, username); fd = open(filename, O_WRONLY | O_CREAT, 0600); write(fd, salt, SCRYPT_SALT_SIZE); close(fd); /* Mark this as a version 1 password */ - snprintf(filename, 512, "%s/%s/password_version", DATA_PATH, username); + snprintf(filename, FILENAME_MAX, "%s/%s/password_version", blergconf.data_path, username); fd = open(filename, O_WRONLY | O_CREAT, 0600); write(fd, "1\n", 2); close(fd); @@ -63,12 +64,12 @@ int auth_set_password(const char *username, const char *password) { } int auth_get_password_version(const char *username) { - char filename[512]; + char filename[FILENAME_MAX]; int fd; char str[4]; int len; - sprintf(filename, "%s/%s/password_version", DATA_PATH, username); + snprintf(filename, FILENAME_MAX, "%s/%s/password_version", blergconf.data_path, username); if (access(filename, F_OK) != 0) { return 0; } @@ -82,14 +83,14 @@ int auth_get_password_version(const char *username) { } int auth_get_password(const char *username, char *password) { - char filename[512]; + char filename[FILENAME_MAX]; int fd; int len = 0; if (!valid_name(username)) return 0; - sprintf(filename, "%s/%s/password", DATA_PATH, username); + snprintf(filename, FILENAME_MAX, "%s/%s/password", blergconf.data_path, username); fd = open(filename, O_RDONLY); if (fd == -1) return 0; @@ -109,13 +110,13 @@ int auth_get_password(const char *username, char *password) { } int auth_get_salt(const char *username, char *salt) { - char filename[512]; + char filename[FILENAME_MAX]; int fd, len; if (!valid_name(username)) return 0; - sprintf(filename, "%s/%s/password_salt", DATA_PATH, username); + snprintf(filename, FILENAME_MAX, "%s/%s/password_salt", blergconf.data_path, username); fd = open(filename, O_RDONLY); if (fd == -1) return 0; @@ -217,7 +218,7 @@ char *create_random_token() { } char * auth_login(const char *username, const char *password) { - char filename[512]; + char filename[FILENAME_MAX]; int token_fd; if (!auth_check_password(username, password)) @@ -225,7 +226,7 @@ char * auth_login(const char *username, const char *password) { char *token = create_random_token(); - sprintf(filename, "%s/%s/tokens", DATA_PATH, username); + snprintf(filename, FILENAME_MAX, "%s/%s/tokens", blergconf.data_path, username); if (access(filename, F_OK) != 0) { if (mkdir(filename, 0700) == -1) { perror("Could not create auth token dir"); @@ -233,7 +234,7 @@ char * auth_login(const char *username, const char *password) { } } - sprintf(filename, "%s/%s/tokens/%s", DATA_PATH, username, token); + snprintf(filename, FILENAME_MAX, "%s/%s/tokens/%s", blergconf.data_path, username, token); token_fd = open(filename, O_WRONLY | O_CREAT, 0600); if (token_fd == -1) { perror("Could not open token"); @@ -245,17 +246,17 @@ char * auth_login(const char *username, const char *password) { } int auth_logout(const char *username, const char *token) { - char filename[512]; + char filename[FILENAME_MAX]; if (!valid_name(username)) return 0; - sprintf(filename, "%s/%s/tokens", DATA_PATH, username); + snprintf(filename, FILENAME_MAX, "%s/%s/tokens", blergconf.data_path, username); if (access(filename, F_OK) != 0) { return 0; } - sprintf(filename, "%s/%s/tokens/%s", DATA_PATH, username, token); + snprintf(filename, FILENAME_MAX, "%s/%s/tokens/%s", blergconf.data_path, username, token); if (unlink(filename) == -1) return 0; @@ -263,9 +264,9 @@ int auth_logout(const char *username, const char *token) { } int auth_check_token(const char *username, const char *given_token) { - char filename[512]; + char filename[FILENAME_MAX]; - sprintf(filename, "%s/%s/tokens/%s", DATA_PATH, username, given_token); + snprintf(filename, FILENAME_MAX, "%s/%s/tokens/%s", blergconf.data_path, username, given_token); return (access(filename, F_OK) == 0); } diff --git a/database/database.c b/database/database.c index 91e6aa2..0386132 100644 --- a/database/database.c +++ b/database/database.c @@ -156,7 +156,7 @@ int blerg_exists(const char *name) { return 0; } - snprintf(filename, 512, "%s/%s", DATA_PATH, name); + snprintf(filename, 512, "%s/%s", blergconf.data_path, name); if (access(filename, F_OK) == -1) return 0; else @@ -187,7 +187,7 @@ struct blerg *blerg_open(const char *name) { /* Make the directory if it doesn't exist */ blerg->base_path = malloc(512); - snprintf(blerg->base_path, 512, "%s/%s", DATA_PATH, name); + snprintf(blerg->base_path, 512, "%s/%s", blergconf.data_path, name); if (access(blerg->base_path, F_OK) == -1) mkdir(blerg->base_path, 0755); diff --git a/database/subscription.c b/database/subscription.c index e55e8f1..b2c89d4 100644 --- a/database/subscription.c +++ b/database/subscription.c @@ -11,17 +11,18 @@ #include "subscription.h" #include "stringbucket.h" #include "config.h" +#include "configuration.h" int subscription_add(const char *from, const char *to) { - char filename[512]; + char filename[FILENAME_MAX]; struct stringbucket * sb; - snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from); + snprintf(filename, FILENAME_MAX, "%s/%s/subscriptions", blergconf.data_path, from); sb = stringbucket_open(filename); stringbucket_add(sb, to); stringbucket_close(sb); - snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, to); + snprintf(filename, FILENAME_MAX, "%s/%s/subscribers", blergconf.data_path, to); sb = stringbucket_open(filename); stringbucket_add(sb, from); stringbucket_close(sb); @@ -30,15 +31,15 @@ int subscription_add(const char *from, const char *to) { } int subscription_remove(const char *from, const char *to) { - char filename[512]; + char filename[FILENAME_MAX]; struct stringbucket * sb; - snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from); + snprintf(filename, FILENAME_MAX, "%s/%s/subscriptions", blergconf.data_path, from); sb = stringbucket_open(filename); stringbucket_delete(sb, to); stringbucket_close(sb); - snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, to); + snprintf(filename, FILENAME_MAX, "%s/%s/subscribers", blergconf.data_path, to); sb = stringbucket_open(filename); stringbucket_delete(sb, from); stringbucket_close(sb); @@ -47,22 +48,22 @@ int subscription_remove(const char *from, const char *to) { } void subscription_notify_add_item(char *to, void *stuff) { - char filename[512]; + char filename[FILENAME_MAX]; - snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, to); + snprintf(filename, FILENAME_MAX, "%s/%s/subscription_feed", blergconf.data_path, to); int fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600); write(fd, stuff, sizeof(struct blergref)); close(fd); } int subscription_notify(const char *author, uint64_t record) { - char filename[512]; + char filename[FILENAME_MAX]; struct blergref r; strncpy(r.author, author, 32); r.record = record; - snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, author); + snprintf(filename, FILENAME_MAX, "%s/%s/subscribers", blergconf.data_path, author); struct stringbucket * sb = stringbucket_open(filename); stringbucket_iterate(sb, subscription_notify_add_item, &r); stringbucket_close(sb); @@ -71,7 +72,7 @@ int subscription_notify(const char *author, uint64_t record) { } struct blergref * subscription_list(const char *author, uint64_t offset, int *count, int direction) { - char filename[512]; + char filename[FILENAME_MAX]; struct stat st; struct blergref * slist; struct blergref * retlist; @@ -80,7 +81,7 @@ struct blergref * subscription_list(const char *author, uint64_t offset, int *co if (!valid_name(author)) return NULL; - snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, author); + snprintf(filename, FILENAME_MAX, "%s/%s/subscription_feed", blergconf.data_path, author); int feed_fd = open(filename, O_RDONLY); if (feed_fd == -1) { @@ -136,11 +137,11 @@ subscription_list_open_failed: } int is_subscribed(const char *from, const char *to) { - char filename[512]; + char filename[FILENAME_MAX]; struct stringbucket * sb; int ret = 0; - snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from); + snprintf(filename, FILENAME_MAX, "%s/%s/subscriptions", blergconf.data_path, from); sb = stringbucket_open(filename); if (stringbucket_find(sb, to) != -1) ret = 1; @@ -150,13 +151,13 @@ int is_subscribed(const char *from, const char *to) { } int subscription_count_items(const char *user) { - char filename[512]; + char filename[FILENAME_MAX]; struct stat st; if (!valid_name(user)) return -1; - snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, user); + snprintf(filename, FILENAME_MAX, "%s/%s/subscription_feed", blergconf.data_path, user); if (access(filename, R_OK) != 0) return 0; diff --git a/database/tags.c b/database/tags.c index 0d7a27f..07aa0e9 100644 --- a/database/tags.c +++ b/database/tags.c @@ -14,6 +14,7 @@ #include "util.h" #include "database.h" #include "config.h" +#include "configuration.h" int tag_scan(const char *author, const char *data, int len, uint64_t record) { @@ -61,7 +62,7 @@ tag_scan_start: } int tag_add(const char *author, const char *tag, uint64_t record) { - char filename[512]; + char filename[FILENAME_MAX]; struct blergref t; memset(t.author, 0, 32); @@ -70,10 +71,10 @@ int tag_add(const char *author, const char *tag, uint64_t record) { switch(tag[0]) { case '#': - snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1); + snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.hash_tags_path, tag + 1); break; case '@': - snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1); + snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.ref_tags_path, tag + 1); break; default: fprintf(stderr, "Invalid tag type: %s\n", tag); @@ -98,7 +99,7 @@ int tag_add(const char *author, const char *tag, uint64_t record) { } struct blergref * tag_list(const char *tag, uint64_t offset, int *count, int direction) { - char filename[512]; + char filename[FILENAME_MAX]; struct stat st; struct blergref *taglist; struct blergref *retlist; @@ -114,10 +115,10 @@ struct blergref * tag_list(const char *tag, uint64_t offset, int *count, int dir switch(tag[0]) { case '#': - snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1); + snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.hash_tags_path, tag + 1); break; case '@': - snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1); + snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.ref_tags_path, tag + 1); break; default: fprintf(stderr, "Invalid tag type: %s\n", tag); @@ -175,7 +176,7 @@ tag_list_open_failed: } int tag_exists(const char *tag) { - char filename[512]; + char filename[FILENAME_MAX]; if (!valid_tag_name(tag + 1)) return 0; @@ -187,10 +188,10 @@ int tag_exists(const char *tag) { switch(tag[0]) { case '#': - snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1); + snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.hash_tags_path, tag + 1); break; case '@': - snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1); + snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.ref_tags_path, tag + 1); break; default: fprintf(stderr, "Invalid tag type: %s\n", tag); diff --git a/tools/blerglatest.c b/tools/blerglatest.c index cedab2d..c0279af 100644 --- a/tools/blerglatest.c +++ b/tools/blerglatest.c @@ -12,6 +12,7 @@ #include "database.h" #include "json.h" #include "config.h" +#include "configuration.h" yajl_gen_config yajl_config = {0, 0}; @@ -102,7 +103,7 @@ struct things * latest_things(const char *path, const char *file, int minlen) { void latest_tags(yajl_gen g) { int i; - struct things * things = latest_things(HASH_TAGS_PATH, NULL, 3); + struct things * things = latest_things(blergconf.hash_tags_path, NULL, 3); unsigned int count = (things->len >= 50 ? 50 : things->len); yajl_gen_array_open(g); @@ -116,7 +117,7 @@ void latest_tags(yajl_gen g) { void latest_records(yajl_gen g) { int i; - struct things * things = latest_things(DATA_PATH, "meta", 0); + struct things * things = latest_things(blergconf.data_path, "meta", 0); unsigned int count = (things->len >= 50 ? 50 : things->len); yajl_gen_array_open(g); -- 2.25.1