2010-09-03 21:00:02 -06:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2010-09-11 22:57:46 -06:00
|
|
|
#include <stdarg.h>
|
2010-09-03 21:00:02 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <time.h>
|
2010-09-11 22:57:46 -06:00
|
|
|
#include "cgi.h"
|
2010-09-03 21:00:02 -06:00
|
|
|
#include "common.h"
|
|
|
|
|
2010-09-10 23:07:40 -06:00
|
|
|
#define EOL(c) ((EOF == (c)) || (0 == (c)) || ('\n' == (c)))
|
|
|
|
|
|
|
|
int
|
|
|
|
fgrepx(char const *needle, char const *filename)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int found = 0;
|
|
|
|
char const *p = needle;
|
|
|
|
|
|
|
|
f = fopen(filename, "r");
|
|
|
|
if (f) {
|
|
|
|
while (1) {
|
|
|
|
int c = fgetc(f);
|
|
|
|
|
|
|
|
/* This list of cases would have looked so much nicer in OCaml. I
|
|
|
|
apologize. */
|
|
|
|
if (EOL(c) && (0 == *p)) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
} else if (EOF == c) {
|
|
|
|
break;
|
|
|
|
} else if ((0 == p) || (*p != c)) {
|
|
|
|
p = needle;
|
|
|
|
do {
|
|
|
|
c = fgetc(f);
|
|
|
|
} while (! EOL(c));
|
|
|
|
} else if ('\n' == c) {
|
|
|
|
p = needle;
|
|
|
|
} else {
|
|
|
|
p += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2010-09-03 21:00:02 -06:00
|
|
|
int
|
2010-09-11 22:57:46 -06:00
|
|
|
my_snprintf(char *buf, size_t buflen, char *fmt, ...)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
len = vsnprintf(buf, buflen - 1, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (len >= 0) {
|
|
|
|
buf[len] = '\0';
|
|
|
|
return len;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
team_exists(char const *teamhash)
|
2010-09-03 21:00:02 -06:00
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
char filename[100];
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Check for invalid characters. */
|
|
|
|
for (i = 0; teamhash[i]; i += 1) {
|
|
|
|
if (! isalnum(teamhash[i])) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Build filename. */
|
|
|
|
ret = snprintf(filename, sizeof(filename),
|
|
|
|
"%s/%s", teamdir, teamhash);
|
2010-09-11 22:57:46 -06:00
|
|
|
if (sizeof(filename) <= ret) {
|
2010-09-03 21:00:02 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lstat seems to be the preferred way to check for existence. */
|
|
|
|
ret = lstat(filename, &buf);
|
|
|
|
if (-1 == ret) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-09-11 22:57:46 -06:00
|
|
|
award_points(char const *teamhash,
|
|
|
|
char const *category,
|
|
|
|
int points)
|
2010-09-03 21:00:02 -06:00
|
|
|
{
|
2010-09-06 21:39:59 -06:00
|
|
|
char line[100];
|
|
|
|
int linelen;
|
2010-09-07 22:25:42 -06:00
|
|
|
char filename[100];
|
|
|
|
int filenamelen;
|
2010-09-06 21:39:59 -06:00
|
|
|
int fd;
|
|
|
|
int ret;
|
2010-09-07 22:25:42 -06:00
|
|
|
time_t now = time(NULL);
|
2010-09-03 21:00:02 -06:00
|
|
|
|
|
|
|
if (! team_exists(teamhash)) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
linelen = snprintf(line, sizeof(line),
|
2010-09-06 21:39:59 -06:00
|
|
|
"%u %s %s %d\n",
|
2010-09-07 22:25:42 -06:00
|
|
|
now, teamhash, category, points);
|
2010-09-11 22:57:46 -06:00
|
|
|
if (sizeof(line) <= linelen) {
|
2010-09-03 21:00:02 -06:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-07 22:25:42 -06:00
|
|
|
/* At one time I had this writing to a single log file, using lockf.
|
|
|
|
This works, as long as nobody ever tries to edit the log file.
|
|
|
|
Editing the log file would require locking it, which would block
|
|
|
|
everything trying to score, effectively taking down the entire
|
2010-09-11 22:57:46 -06:00
|
|
|
contest. If you can't lock it first--and nothing in busybox lets
|
|
|
|
you do this--you have to bring down pretty much everything manually
|
2010-09-07 22:25:42 -06:00
|
|
|
anyway.
|
2010-09-03 21:00:02 -06:00
|
|
|
|
2010-09-07 22:25:42 -06:00
|
|
|
By putting new scores into new files and periodically appending
|
|
|
|
those files to the main log file, it is possible to stop the thing
|
|
|
|
that appends, edit the file at leisure, and then start the appender
|
|
|
|
back up, all without affecting things trying to score: they're
|
|
|
|
still able to record their score and move on. You don't even
|
|
|
|
really need an appender, but it does make things look a little
|
|
|
|
nicer on the fs.
|
|
|
|
|
|
|
|
The fact that this makes the code simpler is just gravy.
|
2010-09-11 22:57:46 -06:00
|
|
|
|
|
|
|
Note that doing this means there's a little time between when a
|
|
|
|
score's written and when the scoreboard (which only reads the log
|
|
|
|
file) picks it up. It's not a big deal for the points log, but
|
|
|
|
this situation makes this technique unsuitable for writing log
|
|
|
|
files that prevent people from double-scoring, like the puzzler or
|
|
|
|
token log.
|
2010-09-07 22:25:42 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
filenamelen = snprintf(filename, sizeof(filename),
|
|
|
|
"%s/%d.%d.%s.%s.%d",
|
|
|
|
pointsdir, now, getpid(),
|
|
|
|
teamhash, category, points);
|
2010-09-11 22:57:46 -06:00
|
|
|
if (sizeof(filename) <= filenamelen) {
|
2010-09-03 21:00:02 -06:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-07 22:25:42 -06:00
|
|
|
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
|
|
|
if (-1 == fd) {
|
2010-09-03 21:00:02 -06:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-07 22:43:24 -06:00
|
|
|
if (-1 == write(fd, line, linelen)) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-03 21:00:02 -06:00
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-09-11 22:57:46 -06:00
|
|
|
|
|
|
|
void
|
|
|
|
award_and_log_uniquely(char const *team,
|
|
|
|
char const *category,
|
|
|
|
int points,
|
|
|
|
char const *logfile,
|
|
|
|
char const *fmt, ...)
|
|
|
|
{
|
|
|
|
char line[100];
|
|
|
|
int len;
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
/* Make sure they haven't already claimed these points */
|
|
|
|
/* Leave room for a newline later on */
|
|
|
|
va_start(ap, fmt);
|
|
|
|
len = vsnprintf(line, sizeof(line)-1, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (sizeof(line) <= len) {
|
|
|
|
cgi_error("Log line too long");
|
|
|
|
}
|
|
|
|
if (fgrepx(line, logfile)) {
|
|
|
|
cgi_page("Already claimed",
|
|
|
|
"<p>Your team has already claimed these points.</p>");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open and lock logfile */
|
|
|
|
fd = open(logfile, O_WRONLY | O_CREAT, 0666);
|
|
|
|
if (-1 == fd) {
|
|
|
|
cgi_error("Unable to open log");
|
|
|
|
}
|
|
|
|
if (-1 == lockf(fd, F_LOCK, 0)) {
|
|
|
|
cgi_error("Unable to lock log");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Award points */
|
|
|
|
if (0 != award_points(team, category, points)) {
|
|
|
|
cgi_error("Unable to award points");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Log that we did so */
|
|
|
|
/* We can turn that trailing NUL into a newline now since write
|
|
|
|
doesn't use C strings */
|
|
|
|
line[len] = '\n';
|
|
|
|
lseek(fd, 0, SEEK_END);
|
|
|
|
if (-1 == write(fd, line, len+1)) {
|
|
|
|
cgi_error("Unable to log your award");
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|