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);
77 len = read(fd, str, 4);
80 /* strtol returns zero if there isn't a number */
81 return strtol(str, NULL, 10);
84 int auth_get_password(const char *username, char *password) {
89 if (!valid_name(username))
92 sprintf(filename, "%s/%s/password", DATA_PATH, username);
93 fd = open(filename, O_RDONLY);
96 switch(auth_get_password_version(username)) {
98 len = read(fd, password, MD5_DIGEST_SIZE);
101 len = read(fd, password, SCRYPT_OUTPUT_SIZE);
111 int auth_get_salt(const char *username, char *salt) {
115 if (!valid_name(username))
118 sprintf(filename, "%s/%s/password_salt", DATA_PATH, username);
119 fd = open(filename, O_RDONLY);
122 len = read(fd, salt, SCRYPT_SALT_SIZE);
128 int auth_check_password_v0(const char *username, const char *password) {
129 char epw[MD5_DIGEST_SIZE + 1];
130 unsigned char givenpw[MD5_DIGEST_SIZE];
131 struct MD5Context ctx;
133 if (auth_get_password(username, epw) == 0)
137 MD5Update(&ctx, username, strlen(username));
138 MD5Update(&ctx, password, strlen(password));
139 MD5Final(givenpw, &ctx);
141 if (strncmp(givenpw, epw, MD5_DIGEST_SIZE) == 0)
147 int auth_check_password_v1(const char *username, const char *password) {
148 unsigned char epw[SCRYPT_OUTPUT_SIZE];
149 unsigned char esalt[SCRYPT_SALT_SIZE];
150 unsigned char givenpw[SCRYPT_OUTPUT_SIZE];
153 if (auth_get_password(username, epw) == 0)
156 if (auth_get_salt(username, esalt) == 0)
159 r = crypto_scrypt(password, strlen(password), esalt, SCRYPT_SALT_SIZE, SCRYPT_N, SCRYPT_r, SCRYPT_p, givenpw, SCRYPT_OUTPUT_SIZE);
161 printf("Failure in scrypt for %s\n", username);
165 if (memcmp(givenpw, epw, SCRYPT_OUTPUT_SIZE) == 0)
171 int auth_check_password(const char *username, const char *password) {
172 int version = auth_get_password_version(username);
176 if (auth_check_password_v0(username, password)) {
177 /* Refresh to the newest version */
178 auth_set_password(username, password);
185 return auth_check_password_v1(username, password);
189 void hexify(char *dst, char *src, int len) {
190 static char hex[] = "0123456789abcdef";
193 for (i = 0; i < len; i++) {
194 dst[i * 2] = hex[(src[i] & 0xF0) >> 4];
195 dst[i * 2 + 1] = hex[src[i] & 0xF];
199 char *create_random_token() {
200 unsigned char buf[TOKEN_SIZE];
204 rand_fd = open("/dev/urandom", O_RDONLY);
206 perror("Could not open /dev/urandom\n");
209 read(rand_fd, buf, TOKEN_SIZE);
212 token = malloc(TOKEN_SIZE * 2 + 1);
213 hexify(token, buf, TOKEN_SIZE);
214 token[TOKEN_SIZE * 2] = 0;
219 char * auth_login(const char *username, const char *password) {
223 if (!auth_check_password(username, password))
226 char *token = create_random_token();
228 sprintf(filename, "%s/%s/tokens", DATA_PATH, username);
229 if (access(filename, F_OK) != 0) {
230 if (mkdir(filename, 0700) == -1) {
231 perror("Could not create auth token dir");
236 sprintf(filename, "%s/%s/tokens/%s", DATA_PATH, username, token);
237 token_fd = open(filename, O_WRONLY | O_CREAT, 0600);
238 if (token_fd == -1) {
239 perror("Could not open token");
247 int auth_logout(const char *username, const char *token) {
250 if (!valid_name(username))
253 sprintf(filename, "%s/%s/tokens", DATA_PATH, username);
254 if (access(filename, F_OK) != 0) {
258 sprintf(filename, "%s/%s/tokens/%s", DATA_PATH, username, token);
259 if (unlink(filename) == -1)
265 int auth_check_token(const char *username, const char *given_token) {
268 sprintf(filename, "%s/%s/tokens/%s", DATA_PATH, username, given_token);
270 return (access(filename, F_OK) == 0);