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