00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "inspircd.h"
00015 #include "m_cap.h"
00016 #include "account.h"
00017
00018
00019
00020 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
00021 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
00022
00026 class SaslAuthenticator : public classbase
00027 {
00028 private:
00029 InspIRCd *ServerInstance;
00030 Module *Creator;
00031 std::string agent;
00032 User *user;
00033 SaslState state;
00034 SaslResult result;
00035 bool state_announced;
00036
00037 public:
00038 SaslAuthenticator(User *user_, std::string method, InspIRCd *instance, Module *ctor)
00039 : ServerInstance(instance), Creator(ctor), user(user_), state(SASL_INIT), state_announced(false)
00040 {
00041 this->user->Extend("sasl_authenticator", this);
00042
00043 std::deque<std::string> params;
00044 params.push_back("*");
00045 params.push_back("SASL");
00046 params.push_back(user->uuid);
00047 params.push_back("*");
00048 params.push_back("S");
00049 params.push_back(method);
00050
00051 ServerInstance->PI->SendEncapsulatedData(params);
00052 }
00053
00054 SaslResult GetSaslResult(std::string &result_)
00055 {
00056 if (result_ == "F")
00057 return SASL_FAIL;
00058
00059 if (result_ == "A")
00060 return SASL_ABORT;
00061
00062 return SASL_OK;
00063 }
00064
00065
00066 SaslState ProcessInboundMessage(std::deque<std::string> &msg)
00067 {
00068 switch (this->state)
00069 {
00070 case SASL_INIT:
00071 this->agent = msg[2];
00072 this->user->Write("AUTHENTICATE %s", msg[5].c_str());
00073 this->state = SASL_COMM;
00074 break;
00075 case SASL_COMM:
00076 if (msg[2] != this->agent)
00077 return this->state;
00078
00079 if (msg[4] != "D")
00080 this->user->Write("AUTHENTICATE %s", msg[5].c_str());
00081 else
00082 {
00083 this->state = SASL_DONE;
00084 this->result = this->GetSaslResult(msg[5]);
00085 }
00086
00087 break;
00088 case SASL_DONE:
00089 break;
00090 default:
00091 ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
00092 break;
00093 }
00094
00095 return this->state;
00096 }
00097
00098 void Abort(void)
00099 {
00100 this->state = SASL_DONE;
00101 this->result = SASL_ABORT;
00102 }
00103
00104 bool SendClientMessage(const std::vector<std::string>& parameters)
00105 {
00106 if (this->state != SASL_COMM)
00107 return true;
00108
00109 std::deque<std::string> params;
00110 params.push_back("*");
00111 params.push_back("SASL");
00112 params.push_back(this->user->uuid);
00113 params.push_back(this->agent);
00114 params.push_back("C");
00115
00116 params.insert(params.end(), parameters.begin(), parameters.end());
00117
00118 ServerInstance->PI->SendEncapsulatedData(params);
00119
00120 if (parameters[0][0] == '*')
00121 {
00122 this->Abort();
00123 return false;
00124 }
00125
00126 return true;
00127 }
00128
00129 void AnnounceState(void)
00130 {
00131 if (this->state_announced)
00132 return;
00133
00134 switch (this->result)
00135 {
00136 case SASL_OK:
00137 this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick.c_str());
00138 break;
00139 case SASL_ABORT:
00140 this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick.c_str());
00141 break;
00142 case SASL_FAIL:
00143 this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick.c_str());
00144 break;
00145 default:
00146 break;
00147 }
00148
00149 this->state_announced = true;
00150 }
00151
00152 ~SaslAuthenticator()
00153 {
00154 this->user->Shrink("sasl_authenticator");
00155 this->AnnounceState();
00156 }
00157 };
00158
00159 class CommandAuthenticate : public Command
00160 {
00161 Module* Creator;
00162 public:
00163 CommandAuthenticate (InspIRCd* Instance, Module* creator) : Command(Instance,"AUTHENTICATE", 0, 1, true), Creator(creator)
00164 {
00165 this->source = "m_sasl.so";
00166 }
00167
00168 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
00169 {
00170
00171 if (user->registered != REG_ALL)
00172 {
00173 if (!user->GetExt("sasl"))
00174 return CMD_FAILURE;
00175
00176 SaslAuthenticator *sasl;
00177 if (!(user->GetExt("sasl_authenticator", sasl)))
00178 sasl = new SaslAuthenticator(user, parameters[0], ServerInstance, Creator);
00179 else if (sasl->SendClientMessage(parameters) == false)
00180 delete sasl;
00181 }
00182 return CMD_FAILURE;
00183 }
00184 };
00185
00186
00187 class ModuleSASL : public Module
00188 {
00189 CommandAuthenticate* sasl;
00190 public:
00191
00192 ModuleSASL(InspIRCd* Me)
00193 : Module(Me)
00194 {
00195 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnPostConnect, I_OnUserDisconnect, I_OnCleanup };
00196 ServerInstance->Modules->Attach(eventlist, this, 5);
00197
00198 sasl = new CommandAuthenticate(ServerInstance, this);
00199 ServerInstance->AddCommand(sasl);
00200
00201 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
00202 ServerInstance->Logs->Log("m_sasl", DEFAULT, "WARNING: m_services_account.so and m_cap.so are not loaded! m_sasl.so will NOT function correctly until these two modules are loaded!");
00203 }
00204
00205 virtual int OnUserRegister(User *user)
00206 {
00207 SaslAuthenticator *sasl_;
00208 if (user->GetExt("sasl_authenticator", sasl_))
00209 {
00210 sasl_->Abort();
00211 delete sasl_;
00212 user->Shrink("sasl_authenticator");
00213 }
00214
00215 return 0;
00216 }
00217
00218 virtual void OnCleanup(int target_type, void *item)
00219 {
00220 if (target_type == TYPE_USER)
00221 OnUserDisconnect((User*)item);
00222 }
00223
00224 virtual void OnUserDisconnect(User *user)
00225 {
00226 SaslAuthenticator *sasl_;
00227 if (user->GetExt("sasl_authenticator", sasl_))
00228 {
00229 delete sasl_;
00230 user->Shrink("sasl_authenticator");
00231 }
00232 }
00233
00234 virtual void OnPostConnect(User* user)
00235 {
00236 if (!IS_LOCAL(user))
00237 return;
00238
00239 std::string* str = NULL;
00240
00241 if (user->GetExt("accountname", str))
00242 ServerInstance->PI->SendMetaData(user, TYPE_USER, "accountname", *str);
00243
00244 return;
00245 }
00246
00247 virtual ~ModuleSASL()
00248 {
00249 }
00250
00251 virtual Version GetVersion()
00252 {
00253 return Version("$Id: m_sasl.cpp 10291 2008-08-25 20:35:51Z w00t $",VF_VENDOR,API_VERSION);
00254 }
00255
00256 virtual void OnEvent(Event *ev)
00257 {
00258 GenericCapHandler(ev, "sasl", "sasl");
00259
00260 if (ev->GetEventID() == "encap_received")
00261 {
00262 std::deque<std::string>* parameters = (std::deque<std::string>*)ev->GetData();
00263
00264 if ((*parameters)[1] != "SASL")
00265 return;
00266
00267 User* target = ServerInstance->FindNick((*parameters)[3]);
00268 if (!target)
00269 {
00270 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", (*parameters)[3].c_str());
00271 return;
00272 }
00273
00274 SaslAuthenticator *sasl_;
00275 if (!target->GetExt("sasl_authenticator", sasl_))
00276 return;
00277
00278 SaslState state = sasl_->ProcessInboundMessage(*parameters);
00279 if (state == SASL_DONE)
00280 {
00281 delete sasl_;
00282 target->Shrink("sasl");
00283 }
00284 }
00285 }
00286 };
00287
00288 MODULE_INIT(ModuleSASL)