X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fusers.cpp;h=1f8a62eece621d8ee253e8081a075dc0381b993f;hb=aec772bdc98bdcfe35c2fc8e74942403c9efcc4d;hp=7f06cfdecb6ea0021affe7b0fae888b5eab13ec3;hpb=d8e1dec0c551b2f5a1d8827a6819e16934da95aa;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/users.cpp b/src/users.cpp index 7f06cfdec..1f8a62eec 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -19,7 +19,6 @@ #include "socketengine.h" #include "wildcard.h" #include "xline.h" -#include "cull_list.h" #include "commands/cmd_whowas.h" static unsigned long already_sent[MAX_DESCRIPTORS] = {0}; @@ -37,7 +36,7 @@ bool InitTypes(ServerConfig* conf, const char* tag) delete[] n->second; } } - + conf->opertypes.clear(); return true; } @@ -52,7 +51,7 @@ bool InitClasses(ServerConfig* conf, const char* tag) delete[] n->second; } } - + conf->operclass.clear(); return true; } @@ -61,7 +60,7 @@ bool DoType(ServerConfig* conf, const char* tag, char** entries, ValueList &valu { const char* TypeName = values[0].GetString(); const char* Classes = values[1].GetString(); - + conf->opertypes[TypeName] = strdup(Classes); return true; } @@ -70,7 +69,7 @@ bool DoClass(ServerConfig* conf, const char* tag, char** entries, ValueList &val { const char* ClassName = values[0].GetString(); const char* CommandList = values[1].GetString(); - + conf->operclass[ClassName] = strdup(CommandList); return true; } @@ -142,7 +141,14 @@ void userrec::StartDNSLookup() try { bool cached; - res_reverse = new UserResolver(this->ServerInstance, this, this->GetIPString(), DNS_QUERY_REVERSE, cached); + const char* ip = this->GetIPString(); + + /* Special case for 4in6 (Have i mentioned i HATE 4in6?) */ + if (!strncmp(ip, "0::ffff:", 8)) + res_reverse = new UserResolver(this->ServerInstance, this, ip + 8, DNS_QUERY_PTR4, cached); + else + res_reverse = new UserResolver(this->ServerInstance, this, ip, this->GetProtocolFamily() == AF_INET ? DNS_QUERY_PTR4 : DNS_QUERY_PTR6, cached); + this->ServerInstance->AddResolver(res_reverse, cached); } catch (CoreException& e) @@ -170,9 +176,19 @@ void UserResolver::OnLookupComplete(const std::string &result, unsigned int ttl, { bool cached; #ifdef IPV6 - const char *ip = this->bound_user->GetIPString(); - bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, (strstr(ip,"0::ffff:") == ip ? DNS_QUERY_A : DNS_QUERY_AAAA), cached); + if (this->bound_user->GetProtocolFamily() == AF_INET6) + { + /* IPV6 forward lookup (with possibility of 4in6) */ + const char* ip = this->bound_user->GetIPString(); + bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, (!strncmp(ip, "0::ffff:", 8) ? DNS_QUERY_A : DNS_QUERY_AAAA), cached); + } + else + { + /* IPV4 lookup (mixed protocol mode) */ + bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, DNS_QUERY_A, cached); + } #else + /* IPV4 lookup (ipv4 only mode) */ bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, DNS_QUERY_A, cached); #endif this->ServerInstance->AddResolver(bound_user->res_forward, cached); @@ -224,9 +240,14 @@ void UserResolver::OnError(ResolverError e, const std::string &errormessage) { if (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user) { - /* Error message here */ - this->bound_user->WriteServ("NOTICE Auth :*** Could not resolve your hostname: %s; using your IP address (%s) instead.", errormessage.c_str(), this->bound_user->GetIPString()); - this->bound_user->dns_done = true; + /* Since dns timeout is implemented outside of the resolver, this was a race condition that could result in this message being sent *after* + * the user was fully connected. This check fixes that issue - Special */ + if (!this->bound_user->dns_done) + { + /* Error message here */ + this->bound_user->WriteServ("NOTICE Auth :*** Could not resolve your hostname: %s; using your IP address (%s) instead.", errormessage.c_str(), this->bound_user->GetIPString()); + this->bound_user->dns_done = true; + } } } @@ -303,7 +324,7 @@ userrec::userrec(InspIRCd* Instance) : ServerInstance(Instance) age = ServerInstance->Time(true); lines_in = lastping = signon = idle_lastmsg = nping = registered = 0; ChannelCount = timeout = flood = bytes_in = bytes_out = cmds_in = cmds_out = 0; - exempt = haspassed = dns_done = false; + muted = exempt = haspassed = dns_done = false; fd = -1; recvq = ""; sendq = ""; @@ -315,13 +336,15 @@ userrec::userrec(InspIRCd* Instance) : ServerInstance(Instance) memset(modes,0,sizeof(modes)); memset(snomasks,0,sizeof(snomasks)); /* Invalidate cache */ - cached_fullhost = cached_hostip = cached_makehost = cached_fullrealhost = NULL; + operquit = cached_fullhost = cached_hostip = cached_makehost = cached_fullrealhost = NULL; } userrec::~userrec() { this->InvalidateCache(); this->DecrementModes(); + if (operquit) + free(operquit); if (ip) { clonemap::iterator x = ServerInstance->local_clones.find(this->GetIPString()); @@ -333,7 +356,7 @@ userrec::~userrec() ServerInstance->local_clones.erase(x); } } - + clonemap::iterator y = ServerInstance->global_clones.find(this->GetIPString()); if (y != ServerInstance->global_clones.end()) { @@ -402,7 +425,7 @@ void userrec::CloseSocket() shutdown(this->fd,2); close(this->fd); } - + char* userrec::GetFullHost() { if (this->cached_fullhost) @@ -509,7 +532,7 @@ bool userrec::HasPermission(const std::string &command) char* mycmd; char* savept; char* savept2; - + /* * users on remote servers can completely bypass all permissions based checks. * This prevents desyncs when one server has different type/class tags to another. @@ -519,7 +542,7 @@ bool userrec::HasPermission(const std::string &command) */ if (!IS_LOCAL(this)) return true; - + // are they even an oper at all? if (*this->oper) { @@ -566,7 +589,7 @@ bool userrec::AddBuffer(std::string a) try { std::string::size_type i = a.rfind('\r'); - + while (i != std::string::npos) { a.erase(i, 1); @@ -575,14 +598,14 @@ bool userrec::AddBuffer(std::string a) if (a.length()) recvq.append(a); - + if (recvq.length() > (unsigned)this->recvqmax) { this->SetWriteError("RecvQ exceeded"); ServerInstance->WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax); return false; } - + return true; } @@ -609,14 +632,14 @@ std::string userrec::GetBuffer() { if (!recvq.length()) return ""; - + /* Strip any leading \r or \n off the string. * Usually there are only one or two of these, * so its is computationally cheap to do. */ while ((*recvq.begin() == '\r') || (*recvq.begin() == '\n')) recvq.erase(recvq.begin()); - + for (std::string::iterator x = recvq.begin(); x != recvq.end(); x++) { /* Find the first complete line, return it as the @@ -643,7 +666,7 @@ void userrec::AddWriteBuf(const std::string &data) { if (*this->GetWriteError()) return; - + if (sendq.length() + data.length() > (unsigned)this->sendqmax) { /* @@ -656,7 +679,7 @@ void userrec::AddWriteBuf(const std::string &data) return; } - try + try { if (data.length() > 512) sendq.append(data.substr(0,510)).append("\r\n"); @@ -718,6 +741,11 @@ void userrec::FlushWriteBuf() { ServerInstance->Log(DEBUG,"Exception in userrec::FlushWriteBuf()"); } + + if (this->sendq.empty()) + { + FOREACH_MOD(I_OnBufferFlushed,OnBufferFlushed(this)); + } } void userrec::SetWriteError(const std::string &error) @@ -784,79 +812,12 @@ void userrec::UnOper() } } -void userrec::QuitUser(InspIRCd* Instance, userrec *user, const std::string &quitreason) +void userrec::QuitUser(InspIRCd* Instance, userrec *user, const std::string &quitreason, const char* operreason) { - user_hash::iterator iter = Instance->clientlist->find(user->nick); - std::string reason = quitreason; - - if (reason.length() > MAXQUIT - 1) - reason.resize(MAXQUIT - 1); - - if (user->registered != REG_ALL) - if (Instance->unregistered_count) - Instance->unregistered_count--; - - if (IS_LOCAL(user)) - { - user->Write("ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason.c_str()); - if ((!user->sendq.empty()) && (!(*user->GetWriteError()))) - user->FlushWriteBuf(); - } - - if (user->registered == REG_ALL) - { - user->PurgeEmptyChannels(); - user->WriteCommonExcept("QUIT :%s",reason.c_str()); - FOREACH_MOD_I(Instance,I_OnUserQuit,OnUserQuit(user,reason)); - } - - FOREACH_MOD_I(Instance,I_OnUserDisconnect,OnUserDisconnect(user)); - - if (IS_LOCAL(user)) - { - if (Instance->Config->GetIOHook(user->GetPort())) - { - try - { - Instance->Config->GetIOHook(user->GetPort())->OnRawSocketClose(user->fd); - } - catch (CoreException& modexcept) - { - Instance->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason()); - } - } - - Instance->SE->DelFd(user); - user->CloseSocket(); - } - - /* - * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything - * if they were an oper with +sn +qQ. - */ - if (user->registered == REG_ALL) - { - if (IS_LOCAL(user)) - Instance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason.c_str()); - else - Instance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",user->server,user->nick,user->ident,user->host,reason.c_str()); - user->AddToWhoWas(); - } - - if (iter != Instance->clientlist->end()) - { - if (IS_LOCAL(user)) - { - std::vector::iterator x = find(Instance->local_users.begin(),Instance->local_users.end(),user); - if (x != Instance->local_users.end()) - Instance->local_users.erase(x); - } - Instance->clientlist->erase(iter); - DELETE(user); - } + user->muted = true; + Instance->GlobalCulls.AddItem(user, quitreason.c_str(), operreason); } - /* adds or updates an entry in the whowas list */ void userrec::AddToWhoWas() { @@ -870,11 +831,19 @@ void userrec::AddToWhoWas() } /* add a client connection to the sockets list */ -void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached, insp_inaddr ip) +void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached, int socketfamily, sockaddr* ip) { std::string tempnick = ConvToStr(socket) + "-unknown"; user_hash::iterator iter = Instance->clientlist->find(tempnick); - const char *ipaddr = insp_ntoa(ip); + char ipaddr[MAXBUF]; +#ifdef IPV6 + if (socketfamily == AF_INET6) + inet_ntop(AF_INET6, &((const sockaddr_in6*)ip)->sin6_addr, ipaddr, sizeof(ipaddr)); + else + inet_ntop(AF_INET, &((const sockaddr_in*)ip)->sin_addr, ipaddr, sizeof(ipaddr)); +#else + inet_ntop(AF_INET, &((const sockaddr_in*)ip)->sin_addr, ipaddr, sizeof(ipaddr)); +#endif userrec* New; int j = 0; @@ -909,7 +878,7 @@ void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached, New->signon = Instance->Time() + Instance->Config->dns_timeout; New->lastping = 1; - New->SetSockAddr(AF_FAMILY, ipaddr, port); + New->SetSockAddr(socketfamily, ipaddr, port); /* Smarter than your average bear^H^H^H^Hset of strlcpys. */ for (const char* temp = New->GetIPString(); *temp && j < 64; temp++, j++) @@ -1006,7 +975,7 @@ unsigned long userrec::LocalCloneCount() return 0; } -void userrec::FullConnect(CullList* Goners) +void userrec::FullConnect() { ServerInstance->stats->statsConnects++; this->idle_lastmsg = ServerInstance->Time(); @@ -1015,25 +984,29 @@ void userrec::FullConnect(CullList* Goners) if ((!a) || (a->GetType() == CC_DENY)) { - Goners->AddItem(this,"Unauthorised connection"); + this->muted = true; + ServerInstance->GlobalCulls.AddItem(this,"Unauthorised connection"); return; } if ((!a->GetPass().empty()) && (!this->haspassed)) { - Goners->AddItem(this,"Invalid password"); + this->muted = true; + ServerInstance->GlobalCulls.AddItem(this,"Invalid password"); return; } - + if (this->LocalCloneCount() > a->GetMaxLocal()) { - Goners->AddItem(this, "No more connections allowed from your host via this connect class (local)"); + this->muted = true; + ServerInstance->GlobalCulls.AddItem(this, "No more connections allowed from your host via this connect class (local)"); ServerInstance->WriteOpers("*** WARNING: maximum LOCAL connections (%ld) exceeded for IP %s", a->GetMaxLocal(), this->GetIPString()); return; } else if (this->GlobalCloneCount() > a->GetMaxGlobal()) { - Goners->AddItem(this, "No more connections allowed from your host via this connect class (global)"); + this->muted = true; + ServerInstance->GlobalCulls.AddItem(this, "No more connections allowed from your host via this connect class (global)"); ServerInstance->WriteOpers("*** WARNING: maximum GLOBAL connections (%ld) exceeded for IP %s",a->GetMaxGlobal(), this->GetIPString()); return; } @@ -1041,22 +1014,24 @@ void userrec::FullConnect(CullList* Goners) if (!this->exempt) { GLine* r = ServerInstance->XLines->matches_gline(this); - + if (r) { + this->muted = true; char reason[MAXBUF]; snprintf(reason,MAXBUF,"G-Lined: %s",r->reason); - Goners->AddItem(this, reason); + ServerInstance->GlobalCulls.AddItem(this, reason); return; } - + KLine* n = ServerInstance->XLines->matches_kline(this); - + if (n) { + this->muted = true; char reason[MAXBUF]; snprintf(reason,MAXBUF,"K-Lined: %s",n->reason); - Goners->AddItem(this, reason); + ServerInstance->GlobalCulls.AddItem(this, reason); return; } @@ -1140,7 +1115,7 @@ bool userrec::ForceNickChange(const char* newnick) int MOD_RESULT = 0; this->InvalidateCache(); - + FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(this, newnick)); if (MOD_RESULT) @@ -1148,7 +1123,7 @@ bool userrec::ForceNickChange(const char* newnick) ServerInstance->stats->statsCollisions++; return false; } - + if (ServerInstance->XLines->matches_qline(newnick)) { ServerInstance->stats->statsCollisions++; @@ -1251,7 +1226,7 @@ const char* userrec::GetIPString() case AF_INET6: { static char temp[1024]; - + sockaddr_in6* sin = (sockaddr_in6*)this->ip; inet_ntop(sin->sin6_family, &sin->sin6_addr, buf, sizeof(buf)); /* IP addresses starting with a : on irc are a Bad Thing (tm) */ @@ -1292,7 +1267,7 @@ const char* userrec::GetIPString(char* buf) case AF_INET6: { static char temp[1024]; - + sockaddr_in6* sin = (sockaddr_in6*)this->ip; inet_ntop(sin->sin6_family, &sin->sin6_addr, buf, sizeof(buf)); /* IP addresses starting with a : on irc are a Bad Thing (tm) */ @@ -1333,7 +1308,11 @@ void userrec::Write(std::string text) try { - ServerInstance->Log(DEBUG,"<- :%s %s", this->nick, text.c_str()); + /* ServerInstance->Log(DEBUG,"<- %s", text.c_str()); + * WARNING: The above debug line is VERY loud, do NOT + * enable it till we have a good way of filtering it + * out of the logs (e.g. 1.2 would be good). + */ text.append("\r\n"); } catch (...) @@ -1404,7 +1383,7 @@ void userrec::WriteFrom(userrec *user, const std::string &text) char tb[MAXBUF]; snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str()); - + this->Write(std::string(tb)); } @@ -1465,16 +1444,16 @@ void userrec::WriteCommon(const std::string &text) { bool sent_to_at_least_one = false; char tb[MAXBUF]; - + if (this->registered != REG_ALL) return; - + uniq_id++; /* We dont want to be doing this n times, just once */ snprintf(tb,MAXBUF,":%s %s",this->GetFullHost(),text.c_str()); std::string out = tb; - + for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++) { CUList* ulist = v->first->GetUsers(); @@ -1488,7 +1467,7 @@ void userrec::WriteCommon(const std::string &text) } } } - + /* * if the user was not in any channels, no users will receive the text. Make sure the user * receives their OWN message for WriteCommon @@ -1522,67 +1501,47 @@ void userrec::WriteCommonExcept(const char* text, ...) this->WriteCommonExcept(std::string(textbuffer)); } -void userrec::WriteCommonExcept(const std::string &text) +void userrec::WriteCommonQuit(const std::string &normal_text, const std::string &oper_text) { - bool quit_munge = false; - char oper_quit[MAXBUF]; - char textbuffer[MAXBUF]; char tb1[MAXBUF]; char tb2[MAXBUF]; - std::string out1; - std::string out2; - - strlcpy(textbuffer, text.c_str(), MAXBUF); if (this->registered != REG_ALL) return; uniq_id++; + snprintf(tb1,MAXBUF,":%s QUIT :%s",this->GetFullHost(),normal_text.c_str()); + snprintf(tb2,MAXBUF,":%s QUIT :%s",this->GetFullHost(),oper_text.c_str()); + std::string out1 = tb1; + std::string out2 = tb2; - snprintf(tb1,MAXBUF,":%s %s",this->GetFullHost(),textbuffer); - - /* TODO: We need some form of WriteCommonExcept that will send two lines, one line to - * opers and the other line to non-opers, then all this hidebans and hidesplits gunk - * can go byebye. - */ - if (ServerInstance->Config->HideSplits) + for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++) { - char* check = textbuffer + 6; - - if (!strncasecmp(textbuffer, "QUIT :",6)) + CUList *ulist = v->first->GetUsers(); + for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++) { - std::stringstream split(check); - std::string server_one; - std::string server_two; - - split >> server_one; - split >> server_two; - - if ((ServerInstance->FindServerName(server_one)) && (ServerInstance->FindServerName(server_two))) + if (this != i->second) { - strlcpy(oper_quit,textbuffer,MAXQUIT); - strlcpy(check,"*.net *.split",MAXQUIT); - quit_munge = true; - snprintf(tb2,MAXBUF,":%s %s",this->GetFullHost(),oper_quit); - out2 = tb2; + if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id)) + { + already_sent[i->second->fd] = uniq_id; + i->second->Write(*i->second->oper ? out2 : out1); + } } } } +} - if ((ServerInstance->Config->HideBans) && (!quit_munge)) - { - if ((!strncasecmp(textbuffer, "QUIT :G-Lined:",14)) || (!strncasecmp(textbuffer, "QUIT :K-Lined:",14)) - || (!strncasecmp(textbuffer, "QUIT :Q-Lined:",14)) || (!strncasecmp(textbuffer, "QUIT :Z-Lined:",14))) - { - char* check = textbuffer + 13; - strlcpy(oper_quit,textbuffer,MAXQUIT); - *check = 0; // We don't need to strlcpy, we just chop it from the : - quit_munge = true; - snprintf(tb2,MAXBUF,":%s %s",this->GetFullHost(),oper_quit); - out2 = tb2; - } - } +void userrec::WriteCommonExcept(const std::string &text) +{ + char tb1[MAXBUF]; + std::string out1; + + if (this->registered != REG_ALL) + return; + uniq_id++; + snprintf(tb1,MAXBUF,":%s %s",this->GetFullHost(),text.c_str()); out1 = tb1; for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++) @@ -1595,10 +1554,7 @@ void userrec::WriteCommonExcept(const std::string &text) if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id)) { already_sent[i->second->fd] = uniq_id; - if (quit_munge) - i->second->Write(*i->second->oper ? out2 : out1); - else - i->second->Write(out1); + i->second->Write(out1); } } } @@ -1633,16 +1589,16 @@ void userrec::WriteWallOps(const std::string &text) } void userrec::WriteWallOps(const char* text, ...) -{ +{ char textbuffer[MAXBUF]; va_list argsPtr; va_start(argsPtr, text); vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - + va_end(argsPtr); + this->WriteWallOps(std::string(textbuffer)); -} +} /* return 0 or 1 depending if users u and u2 share one or more common channels * (used by QUIT, NICK etc which arent channel specific notices) @@ -1724,7 +1680,7 @@ bool userrec::ChangeDisplayedHost(const char* host) } if (IS_LOCAL(this)) - this->WriteServ("396 %s %s :is now your hidden host",this->nick,this->dhost); + this->WriteServ("396 %s %s :is now your displayed host",this->nick,this->dhost); return true; } @@ -1755,7 +1711,7 @@ bool userrec::ChangeIdent(const char* newident) return true; } -void userrec::NoticeAll(char* text, ...) +void userrec::SendAll(const char* command, char* text, ...) { char textbuffer[MAXBUF]; char formatbuffer[MAXBUF]; @@ -1765,7 +1721,7 @@ void userrec::NoticeAll(char* text, ...) vsnprintf(textbuffer, MAXBUF, text, argsPtr); va_end(argsPtr); - snprintf(formatbuffer,MAXBUF,":%s NOTICE $* :%s", this->GetFullHost(), textbuffer); + snprintf(formatbuffer,MAXBUF,":%s %s $* :%s", this->GetFullHost(), command, textbuffer); std::string fmt = formatbuffer; for (std::vector::const_iterator i = ServerInstance->local_users.begin(); i != ServerInstance->local_users.end(); i++) @@ -1811,17 +1767,17 @@ void userrec::SplitChanList(userrec* dest, const std::string &cl) prefix << this->nick << " " << dest->nick << " :"; line = prefix.str(); int namelen = strlen(ServerInstance->Config->ServerName) + 6; - + for (start = 0; (pos = cl.find(' ', start)) != std::string::npos; start = pos+1) { length = (pos == std::string::npos) ? cl.length() : pos; - + if (line.length() + namelen + length - start > 510) { ServerInstance->SendWhoisLine(this, dest, 319, "%s", line.c_str()); line = prefix.str(); } - + if(pos == std::string::npos) { line.append(cl.substr(start, length - start)); @@ -1832,7 +1788,7 @@ void userrec::SplitChanList(userrec* dest, const std::string &cl) line.append(cl.substr(start, length - start + 1)); } } - + if (line.length()) { ServerInstance->SendWhoisLine(this, dest, 319, "%s", line.c_str()); @@ -1965,3 +1921,17 @@ void userrec::HandleEvent(EventType et, int errornum) } } +void userrec::SetOperQuit(const std::string &oquit) +{ + if (operquit) + return; + + operquit = strdup(oquit.c_str()); +} + +const char* userrec::GetOperQuit() +{ + return operquit ? operquit : ""; +} + +