]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslmodes.cpp
Deduplicate code in modmanager_static and modmanager_dynamic
[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 /** Handle channel mode +z
28  */
29 class SSLMode : public ModeHandler
30 {
31  public:
32         UserCertificateAPI API;
33
34         SSLMode(Module* Creator)
35                 : ModeHandler(Creator, "sslonly", 'z', PARAM_NONE, MODETYPE_CHANNEL)
36                 , API(Creator)
37         {
38         }
39
40         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
41         {
42                 if (adding)
43                 {
44                         if (!channel->IsModeSet(this))
45                         {
46                                 if (IS_LOCAL(source))
47                                 {
48                                         if (!API)
49                                                 return MODEACTION_DENY;
50
51                                         const UserMembList* userlist = channel->GetUsers();
52                                         for(UserMembCIter i = userlist->begin(); i != userlist->end(); i++)
53                                         {
54                                                 ssl_cert* cert = API->GetCertificate(i->first);
55                                                 if (!cert && !ServerInstance->ULine(i->first->server))
56                                                 {
57                                                         source->WriteNumeric(ERR_ALLMUSTSSL, "%s %s :all members of the channel must be connected via SSL", source->nick.c_str(), channel->name.c_str());
58                                                         return MODEACTION_DENY;
59                                                 }
60                                         }
61                                 }
62                                 channel->SetMode(this, true);
63                                 return MODEACTION_ALLOW;
64                         }
65                         else
66                         {
67                                 return MODEACTION_DENY;
68                         }
69                 }
70                 else
71                 {
72                         if (channel->IsModeSet(this))
73                         {
74                                 channel->SetMode(this, false);
75                                 return MODEACTION_ALLOW;
76                         }
77
78                         return MODEACTION_DENY;
79                 }
80         }
81 };
82
83 class ModuleSSLModes : public Module
84 {
85
86         SSLMode sslm;
87
88  public:
89         ModuleSSLModes()
90                 : sslm(this)
91         {
92         }
93
94         void init() CXX11_OVERRIDE
95         {
96                 ServerInstance->Modules->AddService(sslm);
97         }
98
99         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
100         {
101                 if(chan && chan->IsModeSet(sslm))
102                 {
103                         if (!sslm.API)
104                                 return MOD_RES_DENY;
105
106                         ssl_cert* cert = sslm.API->GetCertificate(user);
107                         if (cert)
108                         {
109                                 // Let them in
110                                 return MOD_RES_PASSTHRU;
111                         }
112                         else
113                         {
114                                 // Deny
115                                 user->WriteServ( "489 %s %s :Cannot join channel; SSL users only (+z)", user->nick.c_str(), cname.c_str());
116                                 return MOD_RES_DENY;
117                         }
118                 }
119
120                 return MOD_RES_PASSTHRU;
121         }
122
123         ModResult OnCheckBan(User *user, Channel *c, const std::string& mask) CXX11_OVERRIDE
124         {
125                 if ((mask.length() > 2) && (mask[0] == 'z') && (mask[1] == ':'))
126                 {
127                         if (!sslm.API)
128                                 return MOD_RES_DENY;
129
130                         ssl_cert* cert = sslm.API->GetCertificate(user);
131                         if (cert && InspIRCd::Match(cert->GetFingerprint(), mask.substr(2)))
132                                 return MOD_RES_DENY;
133                 }
134                 return MOD_RES_PASSTHRU;
135         }
136
137         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
138         {
139                 tokens["EXTBAN"].push_back('z');
140         }
141
142         Version GetVersion() CXX11_OVERRIDE
143         {
144                 return Version("Provides channel mode +z to allow for Secure/SSL only channels", VF_VENDOR);
145         }
146 };
147
148 MODULE_INIT(ModuleSSLModes)