]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslmodes.cpp
Merge insp20
[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 Channel::MemberMap& userlist = channel->GetUsers();
52                                         for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
53                                         {
54                                                 ssl_cert* cert = API->GetCertificate(i->first);
55                                                 if (!cert && !i->first->server->IsULine())
56                                                 {
57                                                         source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, "all members of the channel must be connected via SSL");
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         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
95         {
96                 if(chan && chan->IsModeSet(sslm))
97                 {
98                         if (!sslm.API)
99                                 return MOD_RES_DENY;
100
101                         ssl_cert* cert = sslm.API->GetCertificate(user);
102                         if (cert)
103                         {
104                                 // Let them in
105                                 return MOD_RES_PASSTHRU;
106                         }
107                         else
108                         {
109                                 // Deny
110                                 user->WriteNumeric(489, cname, "Cannot join channel; SSL users only (+z)");
111                                 return MOD_RES_DENY;
112                         }
113                 }
114
115                 return MOD_RES_PASSTHRU;
116         }
117
118         ModResult OnCheckBan(User *user, Channel *c, const std::string& mask) CXX11_OVERRIDE
119         {
120                 if ((mask.length() > 2) && (mask[0] == 'z') && (mask[1] == ':'))
121                 {
122                         if (!sslm.API)
123                                 return MOD_RES_DENY;
124
125                         ssl_cert* cert = sslm.API->GetCertificate(user);
126                         if (cert && InspIRCd::Match(cert->GetFingerprint(), mask.substr(2)))
127                                 return MOD_RES_DENY;
128                 }
129                 return MOD_RES_PASSTHRU;
130         }
131
132         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
133         {
134                 tokens["EXTBAN"].push_back('z');
135         }
136
137         Version GetVersion() CXX11_OVERRIDE
138         {
139                 return Version("Provides channel mode +z to allow for Secure/SSL only channels", VF_VENDOR);
140         }
141 };
142
143 MODULE_INIT(ModuleSSLModes)