1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2 * BSD-style license. Please see the COPYING file for details.
11 #include <crypto_scrypt.h>
13 #include "configuration.h"
19 int auth_set_password(const char *username, const char *password) {
20 char filename[FILENAME_MAX];
24 if (!valid_name(username) || !blerg_exists(username))
28 if (n > MAX_PASSWORD_LENGTH)
31 /* Clear the auth structure */
32 memset(&auth, 0, sizeof(struct auth_v2));
34 /* Set the auth version */
35 auth.header.version = 2;
37 /* Gather some salt */
38 fd = open("/dev/urandom", O_RDONLY);
40 perror("Could not open /dev/urandom");
43 read(fd, auth.salt, SCRYPT_SALT_SIZE);
46 r = crypto_scrypt((const uint8_t *)password, n, auth.salt, SCRYPT_SALT_SIZE, SCRYPT_N, SCRYPT_r, SCRYPT_p, auth.password, SCRYPT_OUTPUT_SIZE);
48 fprintf(stderr, "Failure in scrypt for %s\n", username);
53 snprintf(filename, FILENAME_MAX, "%s/%s/auth", blergconf.data_path, username);
54 fd = open(filename, O_WRONLY | O_CREAT, 0600);
56 write(fd, &auth, sizeof(struct auth_v2));
63 int auth_get_password_version(const char *username) {
64 char filename[FILENAME_MAX];
67 struct auth_header ah;
70 snprintf(filename, FILENAME_MAX, "%s/%s/auth", blergconf.data_path, username);
71 if (access(filename, F_OK) == 0) {
72 fd = open(filename, O_RDONLY);
75 len = read(fd, &ah, sizeof(struct auth_header));
79 perror("reading auth file");
81 } else if (len < sizeof(struct auth_header)) {
82 fprintf(stderr, "Short read on while determining auth version for %s", username);
89 snprintf(filename, FILENAME_MAX, "%s/%s/password_version", blergconf.data_path, username);
90 if (access(filename, F_OK) != 0) {
94 fd = open(filename, O_RDONLY);
97 len = read(fd, str, 4);
101 perror("auth_get_password_version");
107 /* strtol returns zero if there isn't a number */
108 return strtol(str, NULL, 10);
111 int auth_get_password(const char *username, char *password) {
112 char filename[FILENAME_MAX];
117 if (!valid_name(username))
120 switch(auth_get_password_version(username)) {
122 read_size = MD5_DIGEST_SIZE;
125 read_size = SCRYPT_OUTPUT_SIZE;
131 snprintf(filename, FILENAME_MAX, "%s/%s/password", blergconf.data_path, username);
132 fd = open(filename, O_RDONLY);
135 len = read(fd, password, read_size);
139 perror("auth_get_password");
141 } else if (len < read_size) {
142 fprintf(stderr, "Short read getting password\n");
151 int auth_get_salt(const char *username, uint8_t *salt) {
152 char filename[FILENAME_MAX];
156 if (!valid_name(username))
159 snprintf(filename, FILENAME_MAX, "%s/%s/password_salt", blergconf.data_path, username);
160 fd = open(filename, O_RDONLY);
163 len = read(fd, salt, SCRYPT_SALT_SIZE);
167 perror("auth_get_salt");
169 } else if (len < SCRYPT_SALT_SIZE) {
170 fprintf(stderr, "Short read getting salt\n");
177 int auth_check_password_v0(const char *username, const char *password) {
178 char epw[MD5_DIGEST_SIZE + 1];
179 char givenpw[MD5_DIGEST_SIZE];
180 struct MD5Context ctx;
182 if (auth_get_password(username, epw) == 0)
186 MD5Update(&ctx, username, strlen(username));
187 MD5Update(&ctx, password, strlen(password));
188 MD5Final((unsigned char *)givenpw, &ctx);
190 if (strncmp(givenpw, epw, MD5_DIGEST_SIZE) == 0)
196 int auth_get_data(const char *username, void *data, size_t data_len) {
197 char filename[FILENAME_MAX];
201 if (!valid_name(username))
204 snprintf(filename, FILENAME_MAX, "%s/%s/auth", blergconf.data_path, username);
205 fd = open(filename, O_RDONLY);
209 len = read(fd, data, data_len);
214 perror("auth_get_data");
216 } else if (len < data_len) {
217 fprintf(stderr, "Short read getting auth data\n");
224 int auth_check_scrypt(struct auth_v2 *auth, const char *username, const char *password) {
225 unsigned char givenpw[SCRYPT_OUTPUT_SIZE];
228 r = crypto_scrypt(password, strlen(password), auth->salt, SCRYPT_SALT_SIZE, SCRYPT_N, SCRYPT_r, SCRYPT_p, givenpw, SCRYPT_OUTPUT_SIZE);
230 fprintf(stderr, "Failure in scrypt for %s\n", username);
234 if (memcmp(givenpw, auth->password, SCRYPT_OUTPUT_SIZE) == 0)
240 int auth_check_password_v1(const char *username, const char *password) {
244 if (auth_get_password(username, (char *)auth.password) == 0)
247 if (auth_get_salt(username, auth.salt) == 0)
250 return auth_check_scrypt(&auth, username, password);
253 int auth_check_password_v2(const char *username, const char *password) {
257 if (auth_get_data(username, (void *) &auth, sizeof(struct auth_v2)) == 0)
260 return auth_check_scrypt(&auth, username, password);
263 int auth_check_password(const char *username, const char *password) {
264 int version = auth_get_password_version(username);
268 if (auth_check_password_v0(username, password)) {
269 /* Refresh to the newest version */
270 auth_set_password(username, password);
277 if (auth_check_password_v1(username, password)) {
278 /* Refresh to the newest version */
279 auth_set_password(username, password);
286 return auth_check_password_v2(username, password);
288 fprintf(stderr, "auth_check_password fell through. Bad password version?\n");
292 void hexify(char *dst, char *src, int len) {
293 static char hex[] = "0123456789abcdef";
296 for (i = 0; i < len; i++) {
297 dst[i * 2] = hex[(src[i] & 0xF0) >> 4];
298 dst[i * 2 + 1] = hex[src[i] & 0xF];
302 char *create_random_token() {
303 char buf[TOKEN_SIZE];
307 rand_fd = open("/dev/urandom", O_RDONLY);
309 perror("Could not open /dev/urandom\n");
312 read(rand_fd, buf, TOKEN_SIZE);
315 token = malloc(TOKEN_SIZE * 2 + 1);
316 hexify(token, buf, TOKEN_SIZE);
317 token[TOKEN_SIZE * 2] = 0;
322 char * auth_login(const char *username, const char *password) {
323 char filename[FILENAME_MAX];
326 if (!auth_check_password(username, password))
329 char *token = create_random_token();
331 snprintf(filename, FILENAME_MAX, "%s/%s/tokens", blergconf.data_path, username);
332 if (access(filename, F_OK) != 0) {
333 if (mkdir(filename, 0700) == -1) {
334 perror("Could not create auth token dir");
339 snprintf(filename, FILENAME_MAX, "%s/%s/tokens/%s", blergconf.data_path, username, token);
340 token_fd = open(filename, O_WRONLY | O_CREAT, 0600);
341 if (token_fd == -1) {
342 perror("Could not open token");
350 int auth_logout(const char *username, const char *token) {
351 char filename[FILENAME_MAX];
353 if (!valid_name(username))
356 snprintf(filename, FILENAME_MAX, "%s/%s/tokens", blergconf.data_path, username);
357 if (access(filename, F_OK) != 0) {
361 snprintf(filename, FILENAME_MAX, "%s/%s/tokens/%s", blergconf.data_path, username, token);
362 if (unlink(filename) == -1)
368 int auth_check_token(const char *username, const char *given_token) {
369 char filename[FILENAME_MAX];
371 snprintf(filename, FILENAME_MAX, "%s/%s/tokens/%s", blergconf.data_path, username, given_token);
373 return (access(filename, F_OK) == 0);