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.
10 #include <crypto_scrypt.h>
16 int auth_set_password(const char *username, const char *password) {
18 unsigned char pwhash[SCRYPT_OUTPUT_SIZE];
19 unsigned char salt[SCRYPT_SALT_SIZE];
22 if (!valid_name(username) || !blerg_exists(username))
26 if (n > MAX_PASSWORD_LENGTH)
29 /* Gather some salt */
30 fd = open("/dev/urandom", O_RDONLY);
32 perror("Could not open /dev/urandom\n");
35 read(fd, salt, SCRYPT_SALT_SIZE);
38 r = crypto_scrypt(password, n, salt, SCRYPT_SALT_SIZE, SCRYPT_N, SCRYPT_r, SCRYPT_p, pwhash, SCRYPT_OUTPUT_SIZE);
40 printf("Failure in scrypt for %s\n", username);
44 /* Write the password */
45 snprintf(filename, 512, "%s/%s/password", DATA_PATH, username);
46 fd = open(filename, O_WRONLY | O_CREAT, 0600);
47 write(fd, pwhash, SCRYPT_OUTPUT_SIZE);
51 snprintf(filename, 512, "%s/%s/password_salt", DATA_PATH, username);
52 fd = open(filename, O_WRONLY | O_CREAT, 0600);
53 write(fd, salt, SCRYPT_SALT_SIZE);
56 /* Mark this as a version 1 password */
57 snprintf(filename, 512, "%s/%s/password_version", DATA_PATH, username);
58 fd = open(filename, O_WRONLY | O_CREAT, 0600);
65 int auth_get_password_version(const char *username) {
71 sprintf(filename, "%s/%s/password_version", DATA_PATH, username);
72 if (access(filename, F_OK) != 0) {
76 fd = open(filename, O_RDONLY);
79 /* strtol returns zero if there isn't a number */
80 return strtol(str, NULL, 10);
83 int auth_get_password(const char *username, char *password) {
88 if (!valid_name(username))
91 sprintf(filename, "%s/%s/password", DATA_PATH, username);
92 fd = open(filename, O_RDONLY);
95 switch(auth_get_password_version(username)) {
97 len = read(fd, password, MD5_DIGEST_SIZE);
100 len = read(fd, password, SCRYPT_OUTPUT_SIZE);
110 int auth_get_salt(const char *username, char *salt) {
114 if (!valid_name(username))
117 sprintf(filename, "%s/%s/password_salt", DATA_PATH, username);
118 fd = open(filename, O_RDONLY);
121 len = read(fd, salt, SCRYPT_SALT_SIZE);
127 int auth_check_password_v0(const char *username, const char *password) {
128 char epw[MD5_DIGEST_SIZE + 1];
129 unsigned char givenpw[MD5_DIGEST_SIZE];
130 struct MD5Context ctx;
132 if (auth_get_password(username, epw) == 0)
136 MD5Update(&ctx, username, strlen(username));
137 MD5Update(&ctx, password, strlen(password));
138 MD5Final(givenpw, &ctx);
140 if (strncmp(givenpw, epw, MD5_DIGEST_SIZE) == 0)
146 int auth_check_password_v1(const char *username, const char *password) {
147 unsigned char epw[SCRYPT_OUTPUT_SIZE];
148 unsigned char esalt[SCRYPT_SALT_SIZE];
149 unsigned char givenpw[SCRYPT_OUTPUT_SIZE];
152 if (auth_get_password(username, epw) == 0)
155 if (auth_get_salt(username, esalt) == 0)
158 r = crypto_scrypt(password, strlen(password), esalt, SCRYPT_SALT_SIZE, SCRYPT_N, SCRYPT_r, SCRYPT_p, givenpw, SCRYPT_OUTPUT_SIZE);
160 printf("Failure in scrypt for %s\n", username);
164 if (memcmp(givenpw, epw, SCRYPT_OUTPUT_SIZE) == 0)
170 int auth_check_password(const char *username, const char *password) {
171 int version = auth_get_password_version(username);
175 if (auth_check_password_v0(username, password)) {
176 /* Refresh to the newest version */
177 auth_set_password(username, password);
184 return auth_check_password_v1(username, password);
188 void hexify(char *dst, char *src, int len) {
189 static char hex[] = "0123456789abcdef";
192 for (i = 0; i < len; i++) {
193 dst[i * 2] = hex[(src[i] & 0xF0) >> 4];
194 dst[i * 2 + 1] = hex[src[i] & 0xF];
198 char *create_random_token() {
199 unsigned char buf[TOKEN_SIZE];
203 rand_fd = open("/dev/urandom", O_RDONLY);
205 perror("Could not open /dev/urandom\n");
208 read(rand_fd, buf, TOKEN_SIZE);
211 token = malloc(TOKEN_SIZE * 2 + 1);
212 hexify(token, buf, TOKEN_SIZE);
213 token[TOKEN_SIZE * 2] = 0;
218 char * auth_login(const char *username, const char *password) {
222 if (!auth_check_password(username, password))
225 char *token = create_random_token();
227 sprintf(filename, "%s/%s/tokens", DATA_PATH, username);
228 if (access(filename, F_OK) != 0) {
229 if (mkdir(filename, 0700) == -1) {
230 perror("Could not create auth token dir");
235 sprintf(filename, "%s/%s/tokens/%s", DATA_PATH, username, token);
236 token_fd = open(filename, O_WRONLY | O_CREAT, 0600);
237 if (token_fd == -1) {
238 perror("Could not open token");
246 int auth_logout(const char *username, const char *token) {
249 if (!valid_name(username))
252 sprintf(filename, "%s/%s/tokens", DATA_PATH, username);
253 if (access(filename, F_OK) != 0) {
257 sprintf(filename, "%s/%s/tokens/%s", DATA_PATH, username, token);
258 if (unlink(filename) == -1)
264 int auth_check_token(const char *username, const char *given_token) {
267 sprintf(filename, "%s/%s/tokens/%s", DATA_PATH, username, given_token);
269 return (access(filename, F_OK) == 0);