Add scrypt library and scrypt support to auth
[blerg.git] / common / auth.c
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.
3  */
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <crypto_scrypt.h>
11 #include "config.h"
12 #include "auth.h"
13 #include "util.h"
14 #include "md5.h"
15
16 int auth_set_password(const char *username, const char *password) {
17         char filename[512];
18         unsigned char pwhash[SCRYPT_OUTPUT_SIZE];
19         unsigned char salt[SCRYPT_SALT_SIZE];
20         int fd, n, r;
21
22         if (!valid_name(username) || !blerg_exists(username))
23                 return 0;
24
25         n = strlen(password);
26         if (n > MAX_PASSWORD_LENGTH)
27                 return 0;
28
29         /* Gather some salt */
30         fd = open("/dev/urandom", O_RDONLY);
31         if (fd == -1) {
32                 perror("Could not open /dev/urandom\n");
33                 return 0;
34         }
35         read(fd, salt, SCRYPT_SALT_SIZE);
36         close(fd);
37
38         r = crypto_scrypt(password, n, salt, SCRYPT_SALT_SIZE, SCRYPT_N, SCRYPT_r, SCRYPT_p, pwhash, SCRYPT_OUTPUT_SIZE);
39         if (r != 0) {
40                 printf("Failure in scrypt for %s\n", username);
41                 return 0;
42         }
43
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);
48         close(fd);
49
50         /* Write the salt */
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);
54         close(fd);
55
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);
59         write(fd, "1\n", 2);
60         close(fd);
61         
62         return 1;
63 }
64
65 int auth_get_password_version(const char *username) {
66         char filename[512];
67         int fd;
68         char str[4];
69         int version;
70
71         sprintf(filename, "%s/%s/password_version", DATA_PATH, username);
72         if (access(filename, F_OK) != 0) {
73                 return 0;
74         }
75
76         fd = open(filename, O_RDONLY);
77         read(fd, str, 4);
78         close(fd);
79         /* strtol returns zero if there isn't a number */
80         return strtol(str, NULL, 10);
81 }
82
83 int auth_get_password(const char *username, char *password) {
84         char filename[512];
85         int fd;
86         int len = 0;
87
88         if (!valid_name(username))
89                 return 0;
90
91         sprintf(filename, "%s/%s/password", DATA_PATH, username);
92         fd = open(filename, O_RDONLY);
93         if (fd == -1)
94                 return 0;
95         switch(auth_get_password_version(username)) {
96         case 0:
97                 len = read(fd, password, MD5_DIGEST_SIZE);
98                 break;
99         case 1:
100                 len = read(fd, password, SCRYPT_OUTPUT_SIZE);
101                 break;
102         }
103         close(fd);
104
105         password[len] = 0;
106
107         return 1;
108 }
109
110 int auth_get_salt(const char *username, char *salt) {
111         char filename[512];
112         int fd, len;
113
114         if (!valid_name(username))
115                 return 0;
116
117         sprintf(filename, "%s/%s/password_salt", DATA_PATH, username);
118         fd = open(filename, O_RDONLY);
119         if (fd == -1)
120                 return 0;
121         len = read(fd, salt, SCRYPT_SALT_SIZE);
122         close(fd);
123
124         return 1;
125 }
126
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;
131
132         if (auth_get_password(username, epw) == 0)
133                 return 0;
134
135         MD5Init(&ctx);
136         MD5Update(&ctx, username, strlen(username));
137         MD5Update(&ctx, password, strlen(password));
138         MD5Final(givenpw, &ctx);
139
140         if (strncmp(givenpw, epw, MD5_DIGEST_SIZE) == 0)
141                 return 1;
142         else
143                 return 0;
144 }
145
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];
150         int r;
151
152         if (auth_get_password(username, epw) == 0)
153                 return 0;
154
155         if (auth_get_salt(username, esalt) == 0)
156                 return 0;
157
158         r = crypto_scrypt(password, strlen(password), esalt, SCRYPT_SALT_SIZE, SCRYPT_N, SCRYPT_r, SCRYPT_p, givenpw, SCRYPT_OUTPUT_SIZE);
159         if (r != 0) {
160                 printf("Failure in scrypt for %s\n", username);
161                 return 0;
162         }
163
164         if (memcmp(givenpw, epw, SCRYPT_OUTPUT_SIZE) == 0)
165                 return 1;
166         else
167                 return 0;
168 }
169
170 int auth_check_password(const char *username, const char *password) {
171         int version = auth_get_password_version(username);
172
173         switch(version) {
174         case 0:
175                 if (auth_check_password_v0(username, password)) {
176                         /* Refresh to the newest version */
177                         auth_set_password(username, password);
178                         return 1;
179                 } else {
180                         return 0;
181                 }
182                 break;
183         case 1:
184                 return auth_check_password_v1(username, password);
185         }
186 }
187
188 void hexify(char *dst, char *src, int len) {
189         static char hex[] = "0123456789abcdef";
190         int i;
191
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];
195         }
196 }
197
198 char *create_random_token() {
199         unsigned char buf[TOKEN_SIZE];
200         char *token;
201         int rand_fd;
202
203         rand_fd = open("/dev/urandom", O_RDONLY);
204         if (rand_fd == -1) {
205                 perror("Could not open /dev/urandom\n");
206                 return 0;
207         }
208         read(rand_fd, buf, TOKEN_SIZE);
209         close(rand_fd);
210
211         token = malloc(TOKEN_SIZE * 2 + 1);
212         hexify(token, buf, TOKEN_SIZE);
213         token[TOKEN_SIZE * 2] = 0;
214
215         return token;
216 }
217
218 char * auth_login(const char *username, const char *password) {
219         char filename[512];
220         int token_fd;
221
222         if (!auth_check_password(username, password))
223                 return NULL;
224
225         char *token = create_random_token();
226
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");
231                         return NULL;
232                 }
233         }
234
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");
239                 return NULL;
240         }
241         close(token_fd);
242
243         return token;
244 }
245
246 int auth_logout(const char *username, const char *token) {
247         char filename[512];
248
249         if (!valid_name(username))
250                 return 0;
251
252         sprintf(filename, "%s/%s/tokens", DATA_PATH, username);
253         if (access(filename, F_OK) != 0) {
254                 return 0;
255         }
256
257         sprintf(filename, "%s/%s/tokens/%s", DATA_PATH, username, token);
258         if (unlink(filename) == -1)
259                 return 0;
260
261         return 1;
262 }
263
264 int auth_check_token(const char *username, const char *given_token) {
265         char filename[512];
266
267         sprintf(filename, "%s/%s/tokens/%s", DATA_PATH, username, given_token);
268
269         return (access(filename, F_OK) == 0);
270 }