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