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 uint8_t 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);
82 len = read(fd, str, 4);
86 perror("auth_get_password_version");
92 /* strtol returns zero if there isn't a number */
93 return strtol(str, NULL, 10);
96 int auth_get_password(const char *username, char *password) {
97 char filename[FILENAME_MAX];
102 if (!valid_name(username))
105 switch(auth_get_password_version(username)) {
107 read_size = MD5_DIGEST_SIZE;
110 read_size = SCRYPT_OUTPUT_SIZE;
116 snprintf(filename, FILENAME_MAX, "%s/%s/password", blergconf.data_path, username);
117 fd = open(filename, O_RDONLY);
120 len = read(fd, password, read_size);
124 perror("auth_get_password");
126 } else if (len < read_size) {
127 fprintf(stderr, "Short read getting password\n");
136 int auth_get_salt(const char *username, uint8_t *salt) {
137 char filename[FILENAME_MAX];
141 if (!valid_name(username))
144 snprintf(filename, FILENAME_MAX, "%s/%s/password_salt", blergconf.data_path, username);
145 fd = open(filename, O_RDONLY);
148 len = read(fd, salt, SCRYPT_SALT_SIZE);
152 perror("auth_get_salt");
154 } else if (len < SCRYPT_SALT_SIZE) {
155 fprintf(stderr, "Short read getting salt\n");
162 int auth_check_password_v0(const char *username, const char *password) {
163 char epw[MD5_DIGEST_SIZE + 1];
164 char givenpw[MD5_DIGEST_SIZE];
165 struct MD5Context ctx;
167 if (auth_get_password(username, epw) == 0)
171 MD5Update(&ctx, username, strlen(username));
172 MD5Update(&ctx, password, strlen(password));
173 MD5Final((unsigned char *)givenpw, &ctx);
175 if (strncmp(givenpw, epw, MD5_DIGEST_SIZE) == 0)
181 int auth_check_password_v1(const char *username, const char *password) {
182 unsigned char epw[SCRYPT_OUTPUT_SIZE];
183 uint8_t esalt[SCRYPT_SALT_SIZE];
184 unsigned char givenpw[SCRYPT_OUTPUT_SIZE];
187 if (auth_get_password(username, (char *)epw) == 0)
190 if (auth_get_salt(username, esalt) == 0)
193 r = crypto_scrypt((const uint8_t *)password, strlen(password), esalt, SCRYPT_SALT_SIZE, SCRYPT_N, SCRYPT_r, SCRYPT_p, givenpw, SCRYPT_OUTPUT_SIZE);
195 fprintf(stderr, "Failure in scrypt for %s\n", username);
199 if (memcmp(givenpw, epw, SCRYPT_OUTPUT_SIZE) == 0)
205 int auth_check_password(const char *username, const char *password) {
206 int version = auth_get_password_version(username);
210 if (auth_check_password_v0(username, password)) {
211 /* Refresh to the newest version */
212 auth_set_password(username, password);
219 return auth_check_password_v1(username, password);
221 fprintf(stderr, "auth_check_password fell through. Bad password version?\n");
225 void hexify(char *dst, char *src, int len) {
226 static char hex[] = "0123456789abcdef";
229 for (i = 0; i < len; i++) {
230 dst[i * 2] = hex[(src[i] & 0xF0) >> 4];
231 dst[i * 2 + 1] = hex[src[i] & 0xF];
235 char *create_random_token() {
236 char buf[TOKEN_SIZE];
240 rand_fd = open("/dev/urandom", O_RDONLY);
242 perror("Could not open /dev/urandom\n");
245 read(rand_fd, buf, TOKEN_SIZE);
248 token = malloc(TOKEN_SIZE * 2 + 1);
249 hexify(token, buf, TOKEN_SIZE);
250 token[TOKEN_SIZE * 2] = 0;
255 char * auth_login(const char *username, const char *password) {
256 char filename[FILENAME_MAX];
259 if (!auth_check_password(username, password))
262 char *token = create_random_token();
264 snprintf(filename, FILENAME_MAX, "%s/%s/tokens", blergconf.data_path, username);
265 if (access(filename, F_OK) != 0) {
266 if (mkdir(filename, 0700) == -1) {
267 perror("Could not create auth token dir");
272 snprintf(filename, FILENAME_MAX, "%s/%s/tokens/%s", blergconf.data_path, username, token);
273 token_fd = open(filename, O_WRONLY | O_CREAT, 0600);
274 if (token_fd == -1) {
275 perror("Could not open token");
283 int auth_logout(const char *username, const char *token) {
284 char filename[FILENAME_MAX];
286 if (!valid_name(username))
289 snprintf(filename, FILENAME_MAX, "%s/%s/tokens", blergconf.data_path, username);
290 if (access(filename, F_OK) != 0) {
294 snprintf(filename, FILENAME_MAX, "%s/%s/tokens/%s", blergconf.data_path, username, token);
295 if (unlink(filename) == -1)
301 int auth_check_token(const char *username, const char *given_token) {
302 char filename[FILENAME_MAX];
304 snprintf(filename, FILENAME_MAX, "%s/%s/tokens/%s", blergconf.data_path, username, given_token);
306 return (access(filename, F_OK) == 0);