Begin basic login/logout
[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 <stdlib.h>
7 #include "config.h"
8 #include "auth.h"
9
10 #define TOKEN_SIZE 16
11
12 int auth_set_password(const char *username, const char *password) {
13         char filename[512];
14         int fd;
15
16         if (!blerg_exists(username))
17                 return 0;
18
19         snprintf(filename, 512, "%s/%s/password", DATA_PATH, username);
20         fd = open(filename, O_WRONLY | O_CREAT, 0600);
21         write(fd, password, strlen(password));
22         close(fd);
23         
24         return 1;
25 }
26
27 int auth_get_password(const char *username, char *password) {
28         char filename[512];
29         int fd;
30         int len = 0;
31
32         sprintf(filename, "%s/%s/password", DATA_PATH, username);
33         fd = open(filename, O_RDONLY);
34         if (fd == -1)
35                 return 0;
36         len = read(fd, password, 32);
37         close(fd);
38
39         password[len] = 0;
40
41         return 1;
42 }
43
44 int auth_check_password(const char *username, const char *password) {
45         char epw[33];
46
47         if (auth_get_password(username, epw) == 0)
48                 return 0;
49
50         if (strncmp(password, epw, 32) == 0)
51                 return 1;
52         else
53                 return 0;
54 }
55
56 void hexify(char *dst, char *src, int len) {
57         static char hex[] = "0123456789abcdef";
58         int i;
59
60         for (i = 0; i < len; i++) {
61                 dst[i * 2]     = hex[(src[i] & 0xF0) >> 4];
62                 dst[i * 2 + 1] = hex[src[i] & 0xF];
63         }
64 }
65
66 char *create_random_token() {
67         unsigned char buf[TOKEN_SIZE];
68         char *token;
69         int rand_fd;
70
71         rand_fd = open("/dev/urandom", O_RDONLY);
72         if (rand_fd == -1) {
73                 perror("Could not open /dev/urandom\n");
74                 return 0;
75         }
76         read(rand_fd, buf, TOKEN_SIZE);
77         close(rand_fd);
78
79         token = malloc(TOKEN_SIZE * 2 + 1);
80         hexify(token, buf, TOKEN_SIZE);
81         token[TOKEN_SIZE * 2] = 0;
82
83         return token;
84 }
85
86 int auth_login(const char *username, const char *password) {
87         char filename[512];
88         int token_fd;
89
90         if (!auth_check_password(username, password))
91                 return 0;
92
93         sprintf(filename, "%s/%s/token", DATA_PATH, username);
94         token_fd = open(filename, O_WRONLY | O_CREAT, 0600);
95         if (token_fd == -1) {
96                 perror("Could not open token");
97                 return 0;
98         }
99
100         char *token = create_random_token();
101         write(token_fd, token, TOKEN_SIZE * 2);
102         close(token_fd);
103         free(token);
104
105         return 1;
106 }
107
108 int auth_logout(const char *username) {
109         char filename[512];
110         sprintf(filename, "%s/%s/token", DATA_PATH, username);
111         if (unlink(filename) == -1)
112                 return 0;
113
114         return 1;
115 }
116
117 char *auth_get_token(const char *username) {
118         char filename[512];
119         char *token;
120         int token_fd;
121
122         sprintf(filename, "%s/%s/token", DATA_PATH, username);
123         token_fd = open(filename, O_RDONLY, 0600);
124         if (token_fd == -1) {
125                 perror("Could not open token");
126                 return NULL;
127         }
128
129         token = malloc(TOKEN_SIZE * 2 + 1);
130         read(token_fd, token, TOKEN_SIZE * 2);
131         close(token_fd);
132
133         return token;
134 }