Refactor a bunch of stuff for cgi fork
[blerg.git] / common / auth.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include "config.h"
7 #include "auth.h"
8
9 int auth_set_password(const char *username, const char *password) {
10         char filename[512];
11         int fd;
12
13         if (!blerg_exists(username))
14                 return 0;
15
16         snprintf(filename, 512, "%s/%s/password", DATA_PATH, username);
17         fd = open(filename, O_WRONLY | O_CREAT, 0600);
18         write(fd, password, strlen(password));
19         close(fd);
20         
21         return 1;
22 }
23
24 int auth_get_password(const char *username, char *password) {
25         char filename[512];
26         int fd;
27         int len = 0;
28
29         sprintf(filename, "%s/%s/password", DATA_PATH, username);
30         fd = open(filename, O_RDONLY);
31         if (fd == -1)
32                 return 0;
33         len = read(fd, password, 32);
34         close(fd);
35
36         password[len] = 0;
37
38         return 1;
39 }
40
41 int auth_check_password(const char *username, const char *password) {
42         char epw[33];
43
44         if (auth_get_password(username, epw) == 0)
45                 return 0;
46
47         if (strncmp(password, epw, 32) == 0)
48                 return 1;
49         else
50                 return 0;
51 }