CGI_LIBDIRS = -Lcgi-util-2.2.1 -Lyajl/build/yajl-1.0.11/lib
targets = blerg.a blergtool http_blerg cgi_blerg
-blerg_a_objects = database/database.o database/tags.o
+blerg_a_objects = database/database.o database/tags.o database/util.o
blergtool_objects = tools/blergtool.o blerg.a
http_blerg_objects = http/http_blerg.o http/canned_responses.o common/app.o common/auth.o blerg.a
cgi_blerg_objects = cgi/cgi_blerg.o common/app.o common/auth.o blerg.a
#include <stdlib.h>
#include "config.h"
#include "auth.h"
+#include "util.h"
#define TOKEN_SIZE 16
char filename[512];
int fd;
- if (!blerg_exists(username))
+ if (!valid_name(username) || !blerg_exists(username))
return 0;
snprintf(filename, 512, "%s/%s/password", DATA_PATH, username);
int fd;
int len = 0;
+ if (!valid_name(username))
+ return 0;
+
sprintf(filename, "%s/%s/password", DATA_PATH, username);
fd = open(filename, O_RDONLY);
if (fd == -1)
int auth_logout(const char *username) {
char filename[512];
+
+ if (!valid_name(username))
+ return 0;
+
sprintf(filename, "%s/%s/token", DATA_PATH, username);
if (unlink(filename) == -1)
return 0;
char *token;
int token_fd;
+ if (!valid_name(username))
+ return 0;
+
sprintf(filename, "%s/%s/token", DATA_PATH, username);
token_fd = open(filename, O_RDONLY, 0600);
if (token_fd == -1) {
#include <sys/file.h>
#include <fcntl.h>
#include "database.h"
+#include "util.h"
#include "config.h"
uint64_t blerg_get_record_count(struct blerg *blerg) {
int namelen = strlen(name);
char filename[512];
- if (namelen > 32) {
- perror("Name too long");
+ if (!valid_name(name)) {
+ fprintf(stderr, "Invalid name\n");
return 0;
}
struct stat st;
uint64_t sequence;
- if (namelen > 32) {
- perror("Name too long");
+ if (!valid_name(name)) {
+ fprintf(stderr, "Invalid name\n");
return NULL;
}
struct blerg *blerg = malloc(sizeof(struct blerg));
int blerg_store(struct blerg *blerg, const char *data, int len) {
if (len > MAX_RECORD_SIZE) {
- printf("len > 64K\n");
+ fprintf(stderr, "len > 64K\n");
return -1;
}
uint64_t record = blerg_increment_record_count(blerg);
if (record == -1) {
- printf("Could not find free record\n");
+ fprintf(stderr, "Could not find free record\n");
return -1;
}
int segment = record / RECORDS_PER_SEGMENT;
int blerg_fetch(struct blerg *blerg, int record, char **data, int *length) {
if (record < 0) {
- printf("Invalid record\n");
+ fprintf(stderr, "Invalid record\n");
return 0;
}
int seg_rec = record % RECORDS_PER_SEGMENT;
if ((blerg->index[seg_rec].flags & 0x1) == 0) {
- printf("Invalid record\n");
+ fprintf(stderr, "Invalid record\n");
return 0;
}
fstat(blerg->data_fd, &st);
blerg->data_size = st.st_size;
if (rec_offset > blerg->data_size) {
- printf("Record offset outside of data!?");
+ fprintf(stderr, "Record offset outside of data!?");
return 0;
}
time_t blerg_get_timestamp(struct blerg *blerg, int record) {
if (record < 0) {
- printf("Invalid record\n");
+ fprintf(stderr, "Invalid record\n");
return 0;
}
int seg_rec = record % RECORDS_PER_SEGMENT;
if ((blerg->index[seg_rec].flags & 0x1) == 0) {
- printf("Invalid record\n");
+ fprintf(stderr, "Invalid record\n");
return 0;
}
#include <sys/file.h>
#include <sys/mman.h>
#include "tags.h"
+#include "util.h"
#include "config.h"
#define MAX_TAG_LENGTH 64
struct tag *taglist;
struct tag *retlist;
uint64_t n_tag_records;
+
+ if (!valid_name(tag + 1))
+ return NULL;
switch(tag[0]) {
case '#':
break;
default:
fprintf(stderr, "Invalid tag type: %s\n", tag);
- return 0;
+ return NULL;
}
int tag_fd = open(filename, O_RDONLY, 0600);
}
int tag_exists(const char *tag) {
- int taglen = strlen(tag);
char filename[512];
- if (taglen < 2) {
- fprintf(stderr, "Tag too short\n");
+ if (!valid_name(tag + 1))
return 0;
- } else if (taglen > 33) {
- fprintf(stderr, "Tag too long\n");
- return 0;
- }
+
if (!(tag[0] == '@' || tag[0] == '#')) {
fprintf(stderr, "Invalid tag: %s\n", tag);
return 0;
--- /dev/null
+#define VALID_CHAR(x) (x == ' ' || x == '\'' || x == '-' || x == '.' || (x >= '0' && x <= '9') || (x >= 'A' && x <= 'Z') || x == '_' || (x >= 'a' && x <= 'z'))
+
+int valid_name(const char *name) {
+ int i;
+
+ for (i = 0; i < 32; i++) {
+ if (name[i] == 0) break;
+ if (!VALID_CHAR(name[i])) return 0;
+ }
+
+ if (i >= 32)
+ return 0;
+
+ return 1;
+}
--- /dev/null
+int valid_name(const char *name);
free(data);
} else if (strncmp(argv[1], "list", 4) == 0) {
char *tag = argv[2];
- uint64_t count = 50;
+ int count = 50;
struct tag *list = tag_list(tag, 0, &count, -1);
if (list == NULL) {
printf("No entries");