userdb-blocking.c revision 0f39a57760d93cddbce3ca43096d78e0fe2f42fd
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen/* Copyright (C) 2005 Timo Sirainen */
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include "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 struct auth_stream_reply *stream_reply = NULL;
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;
58bc77731bb25e900498a28409337e747f622722Timo Sirainen stream_reply = auth_stream_reply_init(request);
58bc77731bb25e900498a28409337e747f622722Timo Sirainen auth_stream_reply_import(stream_reply, reply);
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
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen auth_request_userdb_callback(result, stream_reply, request);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen}
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
e9503210d3521a6833ed62dc332fc42ffb0e7a13Timo Sirainenvoid userdb_blocking_lookup(struct auth_request *request)
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen{
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen string_t *str;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen str = t_str_new(64);
e9503210d3521a6833ed62dc332fc42ffb0e7a13Timo Sirainen str_printfa(str, "USER\t%u\t", request->userdb->num);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen auth_request_export(request, str);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen auth_worker_call(request, str_c(str), user_callback);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen}