Stop using FILE *; make tokens go into a log instead of one per file

This commit is contained in:
Neale Pickett 2010-09-03 15:51:54 -06:00
parent b9ee25cfe8
commit 074da9af7d
3 changed files with 26 additions and 23 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*~ *~
*# *#
*.pyc *.pyc
*.o

View File

@ -1,6 +1,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -11,7 +12,7 @@
#define itokenlen 3 #define itokenlen 3
char const *keydir = "/var/lib/ctf/tokend/keys"; char const *keydir = "/var/lib/ctf/tokend/keys";
char const *tokendir = "/var/lib/ctf/tokend/tokens"; char const *tokenlog = "/var/lib/ctf/tokend/tokens.log";
char const consonants[] = "bcdfghklmnprstvz"; char const consonants[] = "bcdfghklmnprstvz";
char const vowels[] = "aeiouy"; char const vowels[] = "aeiouy";
@ -82,7 +83,7 @@ main(int argc, char *argv[])
size_t len; size_t len;
int i; int i;
len = fread(service, 1, sizeof(service) - 1, stdin); len = read(0, service, sizeof(service) - 1);
for (i = 0; (i < len) && isalnum(service[i]); i += 1); for (i = 0; (i < len) && isalnum(service[i]); i += 1);
service[i] = '\0'; service[i] = '\0';
} }
@ -90,25 +91,25 @@ main(int argc, char *argv[])
/* Read in that service's key. */ /* Read in that service's key. */
{ {
char path[100]; char path[100];
FILE *f = NULL; int fd;
size_t len; size_t len;
int ret; int ret;
ret = snprintf(path, sizeof(path), ret = snprintf(path, sizeof(path),
"%s/%s", keydir, service); "%s/%s.key", keydir, service);
if (ret < sizeof(path)) { if (ret < sizeof(path)) {
f = fopen(path, "r"); fd = open(path, O_RDONLY);
} }
if (! f) { if (-1 == fd) {
printf("!Unregistered service"); write(1, "!nosvc", 6);
return 0; return 0;
} }
len = fread(&key, sizeof(uint32_t), 4, f); len = read(fd, &key, 16);
fclose(f); close(fd);
if (4 != len) { if (16 != len) {
printf("!Key file too short"); write(1, "!shortkey", 9);
return 0; return 0;
} }
} }
@ -134,19 +135,20 @@ main(int argc, char *argv[])
/* Write that token out now. */ /* Write that token out now. */
{ {
char path[100]; int fd;
FILE *f = NULL;
int ret; int ret;
struct flock lock;
ret = snprintf(path, sizeof(path), fd = open(tokenlog, O_WRONLY | O_CREAT, 0644);
"%s/%s", tokendir, token); if (-1 == fd) {
f = fopen(path, "w"); write(1, "!write", 6);
if (f) {
fclose(f);
} else {
printf("!Unable to write token");
return 0; return 0;
} }
lockf(fd, F_LOCK, 0);
lseek(fd, 0, SEEK_END);
write(fd, token, tokenlen);
write(fd, "\n", 1);
close(fd);
} }
/* Encrypt the token. Note that now tokenlen is in uint32_ts, not /* Encrypt the token. Note that now tokenlen is in uint32_ts, not
@ -159,7 +161,7 @@ main(int argc, char *argv[])
/* Send it back. If there's an error here, it's okay. Better to have /* Send it back. If there's an error here, it's okay. Better to have
unclaimed tokens than unclaimable ones. */ unclaimed tokens than unclaimable ones. */
fwrite(token, tokenlen, sizeof(uint32_t), stdout); write(1, token, tokenlen * sizeof(uint32_t));
return 0; return 0;
} }

View File

@ -86,7 +86,7 @@ main(int argc, char *argv[])
"The full path to the team hash file is too long.\n")); "The full path to the team hash file is too long.\n"));
return 0; return 0;
} }
fd = creat(filename, 0444); fd = open(filename, 0444, O_WRONLY | O_CREAT | O_EXCL);
if (-1 == fd) { if (-1 == fd) {
page("Bad team name", page("Bad team name",
("<p>Either that team name is already in use, or you " ("<p>Either that team name is already in use, or you "