12 int auth_set_password(const char *username, const char *password) {
16 if (!blerg_exists(username))
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));
27 int auth_get_password(const char *username, char *password) {
32 sprintf(filename, "%s/%s/password", DATA_PATH, username);
33 fd = open(filename, O_RDONLY);
36 len = read(fd, password, 32);
44 int auth_check_password(const char *username, const char *password) {
47 if (auth_get_password(username, epw) == 0)
50 if (strncmp(password, epw, 32) == 0)
56 void hexify(char *dst, char *src, int len) {
57 static char hex[] = "0123456789abcdef";
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];
66 char *create_random_token() {
67 unsigned char buf[TOKEN_SIZE];
71 rand_fd = open("/dev/urandom", O_RDONLY);
73 perror("Could not open /dev/urandom\n");
76 read(rand_fd, buf, TOKEN_SIZE);
79 token = malloc(TOKEN_SIZE * 2 + 1);
80 hexify(token, buf, TOKEN_SIZE);
81 token[TOKEN_SIZE * 2] = 0;
86 int auth_login(const char *username, const char *password) {
90 if (!auth_check_password(username, password))
93 sprintf(filename, "%s/%s/token", DATA_PATH, username);
94 token_fd = open(filename, O_WRONLY | O_CREAT, 0600);
96 perror("Could not open token");
100 char *token = create_random_token();
101 write(token_fd, token, TOKEN_SIZE * 2);
108 int auth_logout(const char *username) {
110 sprintf(filename, "%s/%s/token", DATA_PATH, username);
111 if (unlink(filename) == -1)
117 char *auth_get_token(const char *username) {
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");
129 token = malloc(TOKEN_SIZE * 2 + 1);
130 read(token_fd, token, TOKEN_SIZE * 2);
136 int auth_check_token(const char *username, const char *given_token) {
137 char *token = auth_get_token(username);
138 if (token != NULL && given_token != NULL) {
139 int ret = (strncmp(token, given_token, TOKEN_SIZE * 2) == 0);