2010-09-02 17:10:11 -06:00
|
|
|
#include <sys/types.h>
|
2010-09-03 15:59:08 -06:00
|
|
|
#include <errno.h>
|
2010-09-02 17:10:11 -06:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2010-09-03 15:51:54 -06:00
|
|
|
#include <fcntl.h>
|
2010-09-02 17:10:11 -06:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
2010-09-16 12:21:16 -06:00
|
|
|
#include <string.h>
|
2010-09-02 17:10:11 -06:00
|
|
|
#include <ctype.h>
|
2010-09-13 12:23:39 -06:00
|
|
|
#include "common.h"
|
2010-09-16 23:18:16 -06:00
|
|
|
#include "arc4.h"
|
2010-09-02 17:10:11 -06:00
|
|
|
|
|
|
|
#define itokenlen 3
|
|
|
|
|
|
|
|
char const consonants[] = "bcdfghklmnprstvz";
|
|
|
|
char const vowels[] = "aeiouy";
|
|
|
|
|
|
|
|
#define bubblebabble_len(n) (6*(((n)/2)+1))
|
|
|
|
|
|
|
|
/** Compute bubble babble for input buffer.
|
|
|
|
*
|
|
|
|
* The generated output will be of length 6*((inlen/2)+1), including the
|
|
|
|
* trailing NULL.
|
|
|
|
*
|
|
|
|
* Test vectors:
|
|
|
|
* `' (empty string) `xexax'
|
|
|
|
* `1234567890' `xesef-disof-gytuf-katof-movif-baxux'
|
|
|
|
* `Pineapple' `xigak-nyryk-humil-bosek-sonax'
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
bubblebabble(char *out, char const *in, const size_t inlen)
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
int seed = 1;
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
out[pos++] = 'x';
|
|
|
|
while (1) {
|
|
|
|
unsigned char c;
|
|
|
|
|
|
|
|
if (i == inlen) {
|
|
|
|
out[pos++] = vowels[seed % 6];
|
|
|
|
out[pos++] = 'x';
|
|
|
|
out[pos++] = vowels[seed / 6];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = in[i++];
|
|
|
|
out[pos++] = vowels[(((c >> 6) & 3) + seed) % 6];
|
|
|
|
out[pos++] = consonants[(c >> 2) & 15];
|
|
|
|
out[pos++] = vowels[((c & 3) + (seed / 6)) % 6];
|
|
|
|
if (i == inlen) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
seed = ((seed * 5) + (c * 7) + in[i]) % 36;
|
|
|
|
|
|
|
|
c = in[i++];
|
|
|
|
out[pos++] = consonants[(c >> 4) & 15];
|
|
|
|
out[pos++] = '-';
|
|
|
|
out[pos++] = consonants[c & 15];
|
|
|
|
}
|
|
|
|
|
|
|
|
out[pos++] = 'x';
|
|
|
|
out[pos] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2010-09-16 23:18:16 -06:00
|
|
|
char service[50];
|
|
|
|
size_t servicelen;
|
|
|
|
char token[80];
|
|
|
|
size_t tokenlen;
|
|
|
|
uint8_t key[256];
|
|
|
|
size_t keylen;
|
2010-09-02 17:10:11 -06:00
|
|
|
|
2010-09-03 15:26:51 -06:00
|
|
|
/* Seed the random number generator. This ought to be unpredictable
|
|
|
|
enough for a contest. */
|
2010-09-02 17:10:11 -06:00
|
|
|
srand((int)time(NULL) * (int)getpid());
|
|
|
|
|
2010-09-03 15:26:51 -06:00
|
|
|
/* Read service name. */
|
2010-09-02 17:10:11 -06:00
|
|
|
{
|
2010-09-16 17:11:55 -06:00
|
|
|
ssize_t len;
|
2010-09-02 17:10:11 -06:00
|
|
|
|
2010-09-16 23:18:16 -06:00
|
|
|
len = read(0, service, sizeof(service));
|
2010-09-16 17:11:55 -06:00
|
|
|
for (servicelen = 0;
|
|
|
|
(servicelen < len) && isalnum(service[servicelen]);
|
|
|
|
servicelen += 1);
|
2010-09-02 17:10:11 -06:00
|
|
|
}
|
|
|
|
|
2010-09-03 15:26:51 -06:00
|
|
|
/* Read in that service's key. */
|
2010-09-02 17:10:11 -06:00
|
|
|
{
|
2010-09-16 23:18:16 -06:00
|
|
|
int fd;
|
|
|
|
int ret;
|
2010-09-02 17:10:11 -06:00
|
|
|
|
2010-09-16 23:18:16 -06:00
|
|
|
fd = open(srv_path("token.keys/%*s", servicelen, service), O_RDONLY);
|
2010-09-03 15:51:54 -06:00
|
|
|
if (-1 == fd) {
|
|
|
|
write(1, "!nosvc", 6);
|
2010-09-02 17:10:11 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-16 23:18:16 -06:00
|
|
|
ret = read(fd, &key, sizeof(key));
|
|
|
|
if (-1 == ret) {
|
|
|
|
write(1, "!read", 5);
|
2010-09-02 17:10:11 -06:00
|
|
|
return 0;
|
|
|
|
}
|
2010-09-16 23:18:16 -06:00
|
|
|
keylen = (size_t)ret;
|
|
|
|
|
|
|
|
close(fd);
|
2010-09-02 17:10:11 -06:00
|
|
|
}
|
|
|
|
|
2010-09-03 15:26:51 -06:00
|
|
|
/* Create the token. */
|
2010-09-02 17:10:11 -06:00
|
|
|
{
|
|
|
|
uint8_t crap[itokenlen];
|
2010-09-03 15:26:51 -06:00
|
|
|
char digest[bubblebabble_len(itokenlen)];
|
|
|
|
int i;
|
2010-09-02 17:10:11 -06:00
|
|
|
|
2010-09-03 15:26:51 -06:00
|
|
|
/* Digest some random junk. */
|
2010-09-02 17:10:11 -06:00
|
|
|
for (i = 0; i < itokenlen; i += 1) {
|
|
|
|
crap[i] = (uint8_t)random();
|
|
|
|
}
|
2010-09-16 12:21:16 -06:00
|
|
|
bubblebabble(digest, (char *)crap, itokenlen);
|
2010-09-02 17:10:11 -06:00
|
|
|
|
2010-09-07 14:18:39 -06:00
|
|
|
/* Append digest to service name. */
|
2010-09-16 17:11:55 -06:00
|
|
|
tokenlen = (size_t)snprintf(token, sizeof(token),
|
|
|
|
"%*s:%s",
|
|
|
|
servicelen, service, digest);
|
2010-09-02 17:10:11 -06:00
|
|
|
}
|
|
|
|
|
2010-09-03 15:26:51 -06:00
|
|
|
/* Write that token out now. */
|
|
|
|
{
|
2010-09-14 18:04:33 -06:00
|
|
|
int fd;
|
|
|
|
int ret;
|
2010-09-03 15:26:51 -06:00
|
|
|
|
2010-09-03 15:59:08 -06:00
|
|
|
do {
|
2010-09-14 18:04:33 -06:00
|
|
|
fd = open(srv_path("tokens.db"), O_WRONLY | O_CREAT, 0666);
|
2010-09-03 15:59:08 -06:00
|
|
|
if (-1 == fd) break;
|
|
|
|
|
|
|
|
ret = lockf(fd, F_LOCK, 0);
|
|
|
|
if (-1 == ret) break;
|
|
|
|
|
|
|
|
ret = lseek(fd, 0, SEEK_END);
|
|
|
|
if (-1 == ret) break;
|
|
|
|
|
|
|
|
ret = write(fd, token, tokenlen);
|
|
|
|
if (-1 == ret) break;
|
|
|
|
|
|
|
|
ret = write(fd, "\n", 1);
|
|
|
|
if (-1 == ret) break;
|
|
|
|
|
|
|
|
ret = close(fd);
|
|
|
|
if (-1 == ret) break;
|
|
|
|
} while (0);
|
|
|
|
|
2010-09-14 18:04:33 -06:00
|
|
|
if ((-1 == fd) || (-1 == ret)) {
|
2010-09-03 15:59:08 -06:00
|
|
|
printf("!%s", strerror(errno));
|
2010-09-03 15:26:51 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-16 23:18:16 -06:00
|
|
|
/* Encrypt the token. */
|
2010-09-02 17:10:11 -06:00
|
|
|
{
|
2010-09-16 23:18:16 -06:00
|
|
|
arc4_crypt_buffer(key, keylen, (uint8_t *)token, tokenlen);
|
2010-09-02 17:10:11 -06:00
|
|
|
}
|
|
|
|
|
2010-09-03 15:26:51 -06:00
|
|
|
/* Send it back. If there's an error here, it's okay. Better to have
|
|
|
|
unclaimed tokens than unclaimable ones. */
|
2010-09-16 23:18:16 -06:00
|
|
|
write(1, token, tokenlen);
|
2010-09-02 17:10:11 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|