6ce78b249232e81fffe02619e030554eeb6e9599
[blerg.git] / 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_check_password(const char *username, const char *password) {
25         char filename[512];
26         char epw[33];
27         int fd, len;
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         if (strncmp(password, epw, 32) == 0)
37                 return 1;
38         else
39                 return 0;
40 }