Make points a long

This commit is contained in:
Neale Pickett 2010-09-12 22:00:58 -06:00
parent 5199cf5cac
commit df3da9c6d3
4 changed files with 39 additions and 20 deletions

View File

@ -6,6 +6,7 @@ in.tokend: in.tokend.o xxtea.o
claim.cgi: claim.cgi.o common.o claim.cgi: claim.cgi.o common.o
puzzler.cgi: puzzler.cgi.o common.o puzzler.cgi: puzzler.cgi.o common.o
puzzles.cgi: puzzles.cgi.o common.o
pointscli: common.o pointscli.o pointscli: common.o pointscli.o
clean: clean:

View File

@ -100,10 +100,8 @@ cgi_item(char *str, size_t maxlen)
} }
void void
cgi_page(char *title, char *fmt, ...) cgi_head(char *title)
{ {
va_list ap;
printf(("Content-type: text/html\r\n" printf(("Content-type: text/html\r\n"
"\r\n" "\r\n"
"<!DOCTYPE html>\n" "<!DOCTYPE html>\n"
@ -115,12 +113,26 @@ cgi_page(char *title, char *fmt, ...)
" <body>\n" " <body>\n"
" <h1>%s</h1>\n"), " <h1>%s</h1>\n"),
title, title); title, title);
va_start(ap, fmt); }
vprintf(fmt, ap);
va_end(ap); void
cgi_foot()
{
printf("\n" printf("\n"
" </body>\n" " </body>\n"
"</html>\n"); "</html>\n");
}
void
cgi_page(char *title, char *fmt, ...)
{
va_list ap;
cgi_head(title);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
cgi_foot();
exit(0); exit(0);
} }
@ -135,6 +147,7 @@ cgi_error(char *fmt, ...)
va_start(ap, fmt); va_start(ap, fmt);
vprintf(fmt, ap); vprintf(fmt, ap);
va_end(ap); va_end(ap);
printf("\n");
exit(0); exit(0);
} }
@ -233,7 +246,7 @@ team_exists(char const *teamhash)
int int
award_points(char const *teamhash, award_points(char const *teamhash,
char const *category, char const *category,
int points) long points)
{ {
char line[100]; char line[100];
int linelen; int linelen;
@ -248,7 +261,7 @@ award_points(char const *teamhash,
} }
linelen = snprintf(line, sizeof(line), linelen = snprintf(line, sizeof(line),
"%u %s %s %d\n", "%u %s %s %ld\n",
now, teamhash, category, points); now, teamhash, category, points);
if (sizeof(line) <= linelen) { if (sizeof(line) <= linelen) {
return -1; return -1;
@ -281,7 +294,7 @@ award_points(char const *teamhash,
*/ */
filenamelen = snprintf(filename, sizeof(filename), filenamelen = snprintf(filename, sizeof(filename),
"%s/%d.%d.%s.%s.%d", "%s/%d.%d.%s.%s.%ld",
pointsdir, now, getpid(), pointsdir, now, getpid(),
teamhash, category, points); teamhash, category, points);
if (sizeof(filename) <= filenamelen) { if (sizeof(filename) <= filenamelen) {
@ -305,11 +318,11 @@ award_points(char const *teamhash,
void void
award_and_log_uniquely(char const *team, award_and_log_uniquely(char const *team,
char const *category, char const *category,
int points, long points,
char const *logfile, char const *logfile,
char const *fmt, ...) char const *fmt, ...)
{ {
char line[100]; char line[200];
int len; int len;
int ret; int ret;
int fd; int fd;

View File

@ -3,8 +3,13 @@
#include <stddef.h> #include <stddef.h>
#define TEAM_MAX 40
#define CAT_MAX 40
int cgi_init(); int cgi_init();
size_t cgi_item(char *str, size_t maxlen); size_t cgi_item(char *str, size_t maxlen);
void cgi_head(char *title);
void cgi_foot();
void cgi_page(char *title, char *fmt, ...); void cgi_page(char *title, char *fmt, ...);
void cgi_error(char *fmt, ...); void cgi_error(char *fmt, ...);
@ -16,10 +21,10 @@ int fgrepx(char const *needle, char const *filename);
int team_exists(char const *teamhash); int team_exists(char const *teamhash);
int award_points(char const *teamhash, int award_points(char const *teamhash,
char const *category, char const *category,
int point); long point);
void award_and_log_uniquely(char const *team, void award_and_log_uniquely(char const *team,
char const *category, char const *category,
int points, long points,
char const *logfile, char const *logfile,
char const *fmt, ...); char const *fmt, ...);

View File

@ -6,11 +6,11 @@ char const *logfile = "/var/lib/ctf/puzzler.log";
int int
main(int argc, char *argv) main(int argc, char *argv)
{ {
char team[9]; char team[TEAM_MAX];
char category[30]; char category[CAT_MAX];
char points_str[5]; char points_str[5];
char answer[500]; char answer[500];
int points; long points;
if (-1 == cgi_init()) { if (-1 == cgi_init()) {
return 0; return 0;
@ -32,7 +32,7 @@ main(int argc, char *argv)
break; break;
case 'p': case 'p':
cgi_item(points_str, sizeof(points_str)); cgi_item(points_str, sizeof(points_str));
points = atoi(points_str); points = atol(points_str);
break; break;
case 'a': case 'a':
cgi_item(answer, sizeof(answer)); cgi_item(answer, sizeof(answer));
@ -59,19 +59,19 @@ main(int argc, char *argv)
/* Check answer (also assures category exists) */ /* Check answer (also assures category exists) */
{ {
char filename[100]; char filename[100];
char needle[100]; char needle[400];
my_snprintf(filename, sizeof(filename), my_snprintf(filename, sizeof(filename),
"/srv/%s/answers.txt", category); "/srv/%s/answers.txt", category);
my_snprintf(needle, sizeof(needle), my_snprintf(needle, sizeof(needle),
"%d %s", points, answer); "%ld %s", points, answer);
if (! fgrepx(needle, filename)) { if (! fgrepx(needle, filename)) {
cgi_page("Wrong answer", ""); cgi_page("Wrong answer", "");
} }
} }
award_and_log_uniquely(team, category, points, award_and_log_uniquely(team, category, points,
logfile, "%s %s %d", team, category, points); logfile, "%s %s %ld", team, category, points);
cgi_page("Points awarded", cgi_page("Points awarded",
"<p>%d points for %s.</p>", points, team); "<p>%d points for %s.</p>", points, team);