Use blergconf for paths
Also use FILENAME_MAX everywhere instead of hard-code filename lengths
#include <stdlib.h>
#include <crypto_scrypt.h>
#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;
}
/* 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);
}
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;
}
}
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;
}
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;
}
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))
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");
}
}
- 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");
}
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;
}
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);
}
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
/* 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);
#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);
}
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);
}
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);
}
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;
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) {
}
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;
}
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;
#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) {
}
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);
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);
}
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;
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);
}
int tag_exists(const char *tag) {
- char filename[512];
+ char filename[FILENAME_MAX];
if (!valid_tag_name(tag + 1))
return 0;
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);
#include "database.h"
#include "json.h"
#include "config.h"
+#include "configuration.h"
yajl_gen_config yajl_config = {0, 0};
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);
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);