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];
21 unsigned char pwhash[SCRYPT_OUTPUT_SIZE];
22 uint8_t salt[SCRYPT_SALT_SIZE];
25 if (!valid_name(username) || !blerg_exists(username))
29 if (n > MAX_PASSWORD_LENGTH)
32 /* Gather some salt */
33 fd = open("/dev/urandom", O_RDONLY);
35 perror("Could not open /dev/urandom\n");
38 read(fd, salt, SCRYPT_SALT_SIZE);
41 r = crypto_scrypt((const uint8_t *)password, n, salt, SCRYPT_SALT_SIZE, SCRYPT_N, SCRYPT_r, SCRYPT_p, pwhash, SCRYPT_OUTPUT_SIZE);
43 fprintf(stderr, "Failure in scrypt for %s\n", username);
47 /* Write the password */
48 snprintf(filename, FILENAME_MAX, "%s/%s/password", blergconf.data_path, username);
49 fd = open(filename, O_WRONLY | O_CREAT, 0600);
50 write(fd, pwhash, SCRYPT_OUTPUT_SIZE);
54 snprintf(filename, FILENAME_MAX, "%s/%s/password_salt", blergconf.data_path, username);
55 fd = open(filename, O_WRONLY | O_CREAT, 0600);
56 write(fd, salt, SCRYPT_SALT_SIZE);
59 /* Mark this as a version 1 password */
60 snprintf(filename, FILENAME_MAX, "%s/%s/password_version", blergconf.data_path, username);
61 fd = open(filename, O_WRONLY | O_CREAT, 0600);
68 int auth_get_password_version(const char *username) {
69 char filename[FILENAME_MAX];
74 snprintf(filename, FILENAME_MAX, "%s/%s/password_version", blergconf.data_path, username);
75 if (access(filename, F_OK) != 0) {
79 fd = open(filename, O_RDONLY);
80 len = read(fd, str, 4);
83 /* strtol returns zero if there isn't a number */
84 return strtol(str, NULL, 10);
87 int auth_get_password(const char *username, char *password) {
88 char filename[FILENAME_MAX];
92 if (!valid_name(username))
95 snprintf(filename, FILENAME_MAX, "%s/%s/password", blergconf.data_path, username);
96 fd = open(filename, O_RDONLY);
99 switch(auth_get_password_version(username)) {
101 len = read(fd, password, MD5_DIGEST_SIZE);
104 len = read(fd, password, SCRYPT_OUTPUT_SIZE);
114 int auth_get_salt(const char *username, uint8_t *salt) {
115 char filename[FILENAME_MAX];
118 if (!valid_name(username))
121 snprintf(filename, FILENAME_MAX, "%s/%s/password_salt", blergconf.data_path, username);
122 fd = open(filename, O_RDONLY);
125 len = read(fd, salt, SCRYPT_SALT_SIZE);
131 int auth_check_password_v0(const char *username, const char *password) {
132 char epw[MD5_DIGEST_SIZE + 1];
133 char givenpw[MD5_DIGEST_SIZE];
134 struct MD5Context ctx;
136 if (auth_get_password(username, epw) == 0)
140 MD5Update(&ctx, username, strlen(username));
141 MD5Update(&ctx, password, strlen(password));
142 MD5Final((unsigned char *)givenpw, &ctx);
144 if (strncmp(givenpw, epw, MD5_DIGEST_SIZE) == 0)
150 int auth_check_password_v1(const char *username, const char *password) {
151 unsigned char epw[SCRYPT_OUTPUT_SIZE];
152 uint8_t esalt[SCRYPT_SALT_SIZE];
153 unsigned char givenpw[SCRYPT_OUTPUT_SIZE];
156 if (auth_get_password(username, epw) == 0)
159 if (auth_get_salt(username, esalt) == 0)
162 r = crypto_scrypt(password, strlen(password), esalt, SCRYPT_SALT_SIZE, SCRYPT_N, SCRYPT_r, SCRYPT_p, givenpw, SCRYPT_OUTPUT_SIZE);
164 fprintf(stderr, "Failure in scrypt for %s\n", username);
168 if (memcmp(givenpw, epw, SCRYPT_OUTPUT_SIZE) == 0)
174 int auth_check_password(const char *username, const char *password) {
175 int version = auth_get_password_version(username);
179 if (auth_check_password_v0(username, password)) {
180 /* Refresh to the newest version */
181 auth_set_password(username, password);
188 return auth_check_password_v1(username, password);
192 void hexify(char *dst, char *src, int len) {
193 static char hex[] = "0123456789abcdef";
196 for (i = 0; i < len; i++) {
197 dst[i * 2] = hex[(src[i] & 0xF0) >> 4];
198 dst[i * 2 + 1] = hex[src[i] & 0xF];
202 char *create_random_token() {
203 unsigned char buf[TOKEN_SIZE];
207 rand_fd = open("/dev/urandom", O_RDONLY);
209 perror("Could not open /dev/urandom\n");
212 read(rand_fd, buf, TOKEN_SIZE);
215 token = malloc(TOKEN_SIZE * 2 + 1);
216 hexify(token, buf, TOKEN_SIZE);
217 token[TOKEN_SIZE * 2] = 0;
222 char * auth_login(const char *username, const char *password) {
223 char filename[FILENAME_MAX];
226 if (!auth_check_password(username, password))
229 char *token = create_random_token();
231 snprintf(filename, FILENAME_MAX, "%s/%s/tokens", blergconf.data_path, username);
232 if (access(filename, F_OK) != 0) {
233 if (mkdir(filename, 0700) == -1) {
234 perror("Could not create auth token dir");
239 snprintf(filename, FILENAME_MAX, "%s/%s/tokens/%s", blergconf.data_path, username, token);
240 token_fd = open(filename, O_WRONLY | O_CREAT, 0600);
241 if (token_fd == -1) {
242 perror("Could not open token");
250 int auth_logout(const char *username, const char *token) {
251 char filename[FILENAME_MAX];
253 if (!valid_name(username))
256 snprintf(filename, FILENAME_MAX, "%s/%s/tokens", blergconf.data_path, username);
257 if (access(filename, F_OK) != 0) {
261 snprintf(filename, FILENAME_MAX, "%s/%s/tokens/%s", blergconf.data_path, username, token);
262 if (unlink(filename) == -1)
268 int auth_check_token(const char *username, const char *given_token) {
269 char filename[FILENAME_MAX];
271 snprintf(filename, FILENAME_MAX, "%s/%s/tokens/%s", blergconf.data_path, username, given_token);
273 return (access(filename, F_OK) == 0);