userdb-blocking.c revision 08d6658a4e2ec8104cd1307f6baa75fdb07a24f8
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2005-2009 Dovecot authors, see the included COPYING file */
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger#include "auth-common.h"
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include "str.h"
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include "auth-worker-server.h"
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include "userdb.h"
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include "userdb-blocking.h"
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include <stdlib.h>
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainenstatic void user_callback(struct auth_request *request, const char *reply)
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen{
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen enum userdb_result result;
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen if (strncmp(reply, "FAIL\t", 5) == 0)
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen result = USERDB_RESULT_INTERNAL_FAILURE;
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen else if (strncmp(reply, "NOTFOUND\t", 9) == 0)
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen result = USERDB_RESULT_USER_UNKNOWN;
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen else if (strncmp(reply, "OK\t", 3) == 0) {
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen result = USERDB_RESULT_OK;
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen request->userdb_reply = auth_stream_reply_init(request->pool);
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen auth_stream_reply_import(request->userdb_reply, reply + 3);
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen } else {
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen result = USERDB_RESULT_INTERNAL_FAILURE;
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen i_error("BUG: auth-worker sent invalid user reply");
58bc77731bb25e900498a28409337e747f622722Timo Sirainen }
c2f24d55319fad0b6c03425f402f0cb0cb1a318bTimo Sirainen
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen auth_request_userdb_callback(result, request);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen}
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
e9503210d3521a6833ed62dc332fc42ffb0e7a13Timo Sirainenvoid userdb_blocking_lookup(struct auth_request *request)
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen{
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen struct auth_stream_reply *reply;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen reply = auth_stream_reply_init(pool_datastack_create());
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen auth_stream_reply_add(reply, "USER", NULL);
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen auth_stream_reply_add(reply, NULL, dec2str(request->userdb->num));
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen auth_request_export(request, reply);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
226259ee6fb9830dafc1a5ba1e95bf5a4345b406Timo Sirainen auth_worker_call(request, reply, user_callback);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen}