void do_get(request_t* req) { req->reply.value = datastore_lookup(req->client->datastore, req->argv[0]); req->reply.name = req->argv[0]; if(req->reply.value == NULL) { req->reply.rc = -1; req->reply.message = "Entry not found"; } else req->reply.rc = 0; }
bool client_authenticate(client_t* cli) { _log(LVL_DEBUG, "Asking authentication\n"); char* auth_tok = datastore_lookup(cli->server->datastore, "DB_ADM.USER.AUTH_HASH"); if(auth_tok == NULL) { _log(LVL_WARNING, "Authentication is activated, but no password has been set. Skipping authentication.\n"); return true; } else { _log(LVL_DEBUG, "Stored hash : %s\n", auth_tok); char* r = "Authentication needed\r\n"; _log(LVL_DEBUG, "%s", r); send(cli->sock, r, strlen(r), 0); //Authenticate user char username[32]; char pass[32]; char cat[128]; cat[0] = '\0'; if(read_line(cli->sock, username, 32, false) <= 0) return false; if(read_line(cli->sock, pass, 32, false) <= 0) return false; strcat(cat, username); strcat(cat, ":"); strcat(cat, pass); hash_algo_t* algo = crypto_get_hash_algo("sha256"); char digest_str[algo->digest_str_len]; crypto_hash_str(algo, cat, strlen(cat), digest_str); _log(LVL_DEBUG, "Auth token : %s\n", digest_str); if(!strcmp(digest_str, auth_tok)) { r = "Authentication success\r\n"; _log(LVL_DEBUG, r); send(cli->sock, r, strlen(r), 0); return true; } else { r = "Authentication failed\r\n"; _log(LVL_ERROR, r); send(cli->sock, r, strlen(r), 0); return false; } } }
void do_dump(request_t* req) { int n = datastore_keys_number(req->client->datastore); char* keys[n]; datastore_list_keys(req->client->datastore, keys, n); size_t size = n*(2048)*sizeof(char); size = size>0 ? size : 1; req->reply.message = malloc(size); memset(req->reply.message, '\0', size); int i; for(i = 0; i < n; i++) { char* value = datastore_lookup(req->client->datastore, keys[i]); strcat(req->reply.message, "SET "); strcat(req->reply.message, keys[i]); strcat(req->reply.message, " "); strcat(req->reply.message, value); strcat(req->reply.message, "\r\n"); free(value); } req->reply.rc = 0; }