Paranoia fix for reading password version
[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 len;
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         len = read(fd, str, 4);
78         close(fd);
79         str[len] = 0;
80         /* strtol returns zero if there isn't a number */
81         return strtol(str, NULL, 10);
82 }
83
84 int auth_get_password(const char *username, char *password) {
85         char filename[512];
86         int fd;
87         int len = 0;
88
89         if (!valid_name(username))
90                 return 0;
91
92         sprintf(filename, "%s/%s/password", DATA_PATH, username);
93         fd = open(filename, O_RDONLY);
94         if (fd == -1)
95                 return 0;
96         switch(auth_get_password_version(username)) {
97         case 0:
98                 len = read(fd, password, MD5_DIGEST_SIZE);
99                 break;
100         case 1:
101                 len = read(fd, password, SCRYPT_OUTPUT_SIZE);
102                 break;
103         }
104         close(fd);
105
106         password[len] = 0;
107
108         return 1;
109 }
110
111 int auth_get_salt(const char *username, char *salt) {
112         char filename[512];
113         int fd, len;
114
115         if (!valid_name(username))
116                 return 0;
117
118         sprintf(filename, "%s/%s/password_salt", DATA_PATH, username);
119         fd = open(filename, O_RDONLY);
120         if (fd == -1)
121                 return 0;
122         len = read(fd, salt, SCRYPT_SALT_SIZE);
123         close(fd);
124
125         return 1;
126 }
127
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;
132
133         if (auth_get_password(username, epw) == 0)
134                 return 0;
135
136         MD5Init(&ctx);
137         MD5Update(&ctx, username, strlen(username));
138         MD5Update(&ctx, password, strlen(password));
139         MD5Final(givenpw, &ctx);
140
141         if (strncmp(givenpw, epw, MD5_DIGEST_SIZE) == 0)
142                 return 1;
143         else
144                 return 0;
145 }
146
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];
151         int r;
152
153         if (auth_get_password(username, epw) == 0)
154                 return 0;
155
156         if (auth_get_salt(username, esalt) == 0)
157                 return 0;
158
159         r = crypto_scrypt(password, strlen(password), esalt, SCRYPT_SALT_SIZE, SCRYPT_N, SCRYPT_r, SCRYPT_p, givenpw, SCRYPT_OUTPUT_SIZE);
160         if (r != 0) {
161                 printf("Failure in scrypt for %s\n", username);
162                 return 0;
163         }
164
165         if (memcmp(givenpw, epw, SCRYPT_OUTPUT_SIZE) == 0)
166                 return 1;
167         else
168                 return 0;
169 }
170
171 int auth_check_password(const char *username, const char *password) {
172         int version = auth_get_password_version(username);
173
174         switch(version) {
175         case 0:
176                 if (auth_check_password_v0(username, password)) {
177                         /* Refresh to the newest version */
178                         auth_set_password(username, password);
179                         return 1;
180                 } else {
181                         return 0;
182                 }
183                 break;
184         case 1:
185                 return auth_check_password_v1(username, password);
186         }
187 }
188
189 void hexify(char *dst, char *src, int len) {
190         static char hex[] = "0123456789abcdef";
191         int i;
192
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];
196         }
197 }
198
199 char *create_random_token() {
200         unsigned char buf[TOKEN_SIZE];
201         char *token;
202         int rand_fd;
203
204         rand_fd = open("/dev/urandom", O_RDONLY);
205         if (rand_fd == -1) {
206                 perror("Could not open /dev/urandom\n");
207                 return 0;
208         }
209         read(rand_fd, buf, TOKEN_SIZE);
210         close(rand_fd);
211
212         token = malloc(TOKEN_SIZE * 2 + 1);
213         hexify(token, buf, TOKEN_SIZE);
214         token[TOKEN_SIZE * 2] = 0;
215
216         return token;
217 }
218
219 char * auth_login(const char *username, const char *password) {
220         char filename[512];
221         int token_fd;
222
223         if (!auth_check_password(username, password))
224                 return NULL;
225
226         char *token = create_random_token();
227
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");
232                         return NULL;
233                 }
234         }
235
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");
240                 return NULL;
241         }
242         close(token_fd);
243
244         return token;
245 }
246
247 int auth_logout(const char *username, const char *token) {
248         char filename[512];
249
250         if (!valid_name(username))
251                 return 0;
252
253         sprintf(filename, "%s/%s/tokens", DATA_PATH, username);
254         if (access(filename, F_OK) != 0) {
255                 return 0;
256         }
257
258         sprintf(filename, "%s/%s/tokens/%s", DATA_PATH, username, token);
259         if (unlink(filename) == -1)
260                 return 0;
261
262         return 1;
263 }
264
265 int auth_check_token(const char *username, const char *given_token) {
266         char filename[512];
267
268         sprintf(filename, "%s/%s/tokens/%s", DATA_PATH, username, given_token);
269
270         return (access(filename, F_OK) == 0);
271 }