]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslmodes.cpp
m_callerid Route ACCEPT to the server of the target user only, do not send METADATA...
[user/henk/code/inspircd.git] / src / modules / m_sslmodes.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "inspircd.h"
25 #include "modules/ssl.h"
26
27 /* $ModDesc: Provides channel mode +z to allow for Secure/SSL only channels */
28
29 /** Handle channel mode +z
30  */
31 class SSLMode : public ModeHandler
32 {
33  public:
34         UserCertificateAPI API;
35
36         SSLMode(Module* Creator)
37                 : ModeHandler(Creator, "sslonly", 'z', PARAM_NONE, MODETYPE_CHANNEL)
38                 , API(Creator)
39         {
40         }
41
42         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
43         {
44                 if (adding)
45                 {
46                         if (!channel->IsModeSet('z'))
47                         {
48                                 if (IS_LOCAL(source))
49                                 {
50                                         if (!API)
51                                                 return MODEACTION_DENY;
52
53                                         const UserMembList* userlist = channel->GetUsers();
54                                         for(UserMembCIter i = userlist->begin(); i != userlist->end(); i++)
55                                         {
56                                                 ssl_cert* cert = API->GetCertificate(i->first);
57                                                 if (!cert && !ServerInstance->ULine(i->first->server))
58                                                 {
59                                                         source->WriteNumeric(ERR_ALLMUSTSSL, "%s %s :all members of the channel must be connected via SSL", source->nick.c_str(), channel->name.c_str());
60                                                         return MODEACTION_DENY;
61                                                 }
62                                         }
63                                 }
64                                 channel->SetMode('z',true);
65                                 return MODEACTION_ALLOW;
66                         }
67                         else
68                         {
69                                 return MODEACTION_DENY;
70                         }
71                 }
72                 else
73                 {
74                         if (channel->IsModeSet('z'))
75                         {
76                                 channel->SetMode('z',false);
77                                 return MODEACTION_ALLOW;
78                         }
79
80                         return MODEACTION_DENY;
81                 }
82         }
83 };
84
85 class ModuleSSLModes : public Module
86 {
87
88         SSLMode sslm;
89
90  public:
91         ModuleSSLModes()
92                 : sslm(this)
93         {
94         }
95
96         void init() CXX11_OVERRIDE
97         {
98                 ServerInstance->Modules->AddService(sslm);
99                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnCheckBan, I_On005Numeric };
100                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
101         }
102
103         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
104         {
105                 if(chan && chan->IsModeSet('z'))
106                 {
107                         if (!sslm.API)
108                                 return MOD_RES_DENY;
109
110                         ssl_cert* cert = sslm.API->GetCertificate(user);
111                         if (cert)
112                         {
113                                 // Let them in
114                                 return MOD_RES_PASSTHRU;
115                         }
116                         else
117                         {
118                                 // Deny
119                                 user->WriteServ( "489 %s %s :Cannot join channel; SSL users only (+z)", user->nick.c_str(), cname.c_str());
120                                 return MOD_RES_DENY;
121                         }
122                 }
123
124                 return MOD_RES_PASSTHRU;
125         }
126
127         ModResult OnCheckBan(User *user, Channel *c, const std::string& mask) CXX11_OVERRIDE
128         {
129                 if ((mask.length() > 2) && (mask[0] == 'z') && (mask[1] == ':'))
130                 {
131                         if (!sslm.API)
132                                 return MOD_RES_DENY;
133
134                         ssl_cert* cert = sslm.API->GetCertificate(user);
135                         if (cert && InspIRCd::Match(cert->GetFingerprint(), mask.substr(2)))
136                                 return MOD_RES_DENY;
137                 }
138                 return MOD_RES_PASSTHRU;
139         }
140
141         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
142         {
143                 tokens["EXTBAN"].push_back('z');
144         }
145
146         Version GetVersion() CXX11_OVERRIDE
147         {
148                 return Version("Provides channel mode +z to allow for Secure/SSL only channels", VF_VENDOR);
149         }
150 };
151
152 MODULE_INIT(ModuleSSLModes)